1
/******************************************************
6
Created 3/26/1996 Heikki Tuuri
7
*******************************************************/
13
#include "trx0types.h"
14
#include "lock0types.h"
15
#include "usr0types.h"
16
#include "que0types.h"
18
#include "read0types.h"
19
#include "dict0types.h"
22
extern ulint trx_n_mysql_transactions;
24
/************************************************************************
25
Releases the search latch if trx has reserved it. */
28
trx_search_latch_release_if_reserved(
29
/*=================================*/
30
trx_t* trx); /* in: transaction */
31
/**********************************************************************
32
Set detailed error message for the transaction. */
34
trx_set_detailed_error(
35
/*===================*/
36
trx_t* trx, /* in: transaction struct */
37
const char* msg); /* in: detailed error message */
38
/*****************************************************************
39
Set detailed error message for the transaction from a file. Note that the
40
file is rewinded before reading from it. */
43
trx_set_detailed_error_from_file(
44
/*=============================*/
45
trx_t* trx, /* in: transaction struct */
46
FILE* file); /* in: file to read message from */
47
/********************************************************************
48
Retrieves the error_info field from a trx. */
53
/* out: the error info */
54
trx_t* trx); /* in: trx object */
55
/********************************************************************
56
Creates and initializes a transaction object. */
61
/* out, own: the transaction */
62
sess_t* sess); /* in: session or NULL */
63
/************************************************************************
64
Creates a transaction object for MySQL. */
67
trx_allocate_for_mysql(void);
68
/*========================*/
69
/* out, own: transaction object */
70
/************************************************************************
71
Creates a transaction object for background operations by the master thread. */
74
trx_allocate_for_background(void);
75
/*=============================*/
76
/* out, own: transaction object */
77
/************************************************************************
78
Frees a transaction object. */
83
trx_t* trx); /* in, own: trx object */
84
/************************************************************************
85
Frees a transaction object for MySQL. */
90
trx_t* trx); /* in, own: trx object */
91
/************************************************************************
92
Frees a transaction object of a background operation of the master thread. */
95
trx_free_for_background(
96
/*====================*/
97
trx_t* trx); /* in, own: trx object */
98
/********************************************************************
99
Creates trx objects for transactions and initializes the trx list of
100
trx_sys at database start. Rollback segment and undo log lists must
101
already exist when this function is called, because the lists of
102
transactions to be rolled back or cleaned up are built based on the
106
trx_lists_init_at_db_start(void);
107
/*============================*/
108
/********************************************************************
109
Starts a new transaction. */
114
/* out: TRUE if success, FALSE if the rollback
115
segment could not support this many transactions */
116
trx_t* trx, /* in: transaction */
117
ulint rseg_id);/* in: rollback segment id; if ULINT_UNDEFINED
118
is passed, the system chooses the rollback segment
119
automatically in a round-robin fashion */
120
/********************************************************************
121
Starts a new transaction. */
127
trx_t* trx, /* in: transaction */
128
ulint rseg_id);/* in: rollback segment id; if ULINT_UNDEFINED
129
is passed, the system chooses the rollback segment
130
automatically in a round-robin fashion */
131
/*****************************************************************
132
Starts the transaction if it is not yet started. */
135
trx_start_if_not_started(
136
/*=====================*/
137
trx_t* trx); /* in: transaction */
138
/*****************************************************************
139
Starts the transaction if it is not yet started. Assumes we have reserved
143
trx_start_if_not_started_low(
144
/*=========================*/
145
trx_t* trx); /* in: transaction */
146
/*****************************************************************
147
Starts the transaction if it is not yet started. */
150
trx_start_if_not_started_noninline(
151
/*===============================*/
152
trx_t* trx); /* in: transaction */
153
/********************************************************************
154
Commits a transaction. */
157
trx_commit_off_kernel(
158
/*==================*/
159
trx_t* trx); /* in: transaction */
160
/********************************************************************
161
Cleans up a transaction at database startup. The cleanup is needed if
162
the transaction already got to the middle of a commit when the database
163
crashed, andf we cannot roll it back. */
166
trx_cleanup_at_db_startup(
167
/*======================*/
168
trx_t* trx); /* in: transaction */
169
/**************************************************************************
170
Does the transaction commit for MySQL. */
173
trx_commit_for_mysql(
174
/*=================*/
175
/* out: 0 or error number */
176
trx_t* trx); /* in: trx handle */
177
/**************************************************************************
178
Does the transaction prepare for MySQL. */
181
trx_prepare_for_mysql(
182
/*==================*/
183
/* out: 0 or error number */
184
trx_t* trx); /* in: trx handle */
185
/**************************************************************************
186
This function is used to find number of prepared transactions and
187
their transaction objects for a recovery. */
190
trx_recover_for_mysql(
191
/*==================*/
192
/* out: number of prepared transactions */
193
XID* xid_list, /* in/out: prepared transactions */
194
ulint len); /* in: number of slots in xid_list */
195
/***********************************************************************
196
This function is used to find one X/Open XA distributed transaction
197
which is in the prepared state */
201
/* out: trx or NULL */
202
XID* xid); /* in: X/Open XA transaction identification */
203
/**************************************************************************
204
If required, flushes the log to disk if we called trx_commit_for_mysql()
205
with trx->flush_log_later == TRUE. */
208
trx_commit_complete_for_mysql(
209
/*==========================*/
210
/* out: 0 or error number */
211
trx_t* trx); /* in: trx handle */
212
/**************************************************************************
213
Marks the latest SQL statement ended. */
216
trx_mark_sql_stat_end(
217
/*==================*/
218
trx_t* trx); /* in: trx handle */
219
/************************************************************************
220
Assigns a read view for a consistent read query. All the consistent reads
221
within the same transaction will get the same read view, which is created
222
when this function is first called for a new started transaction. */
225
trx_assign_read_view(
226
/*=================*/
227
/* out: consistent read view */
228
trx_t* trx); /* in: active transaction */
229
/***************************************************************
230
The transaction must be in the TRX_QUE_LOCK_WAIT state. Puts it to
231
the TRX_QUE_RUNNING state and releases query threads which were
232
waiting for a lock in the wait_thrs list. */
237
trx_t* trx); /* in: transaction */
238
/********************************************************************
239
Sends a signal to a trx object. */
244
trx_t* trx, /* in: trx handle */
245
ulint type, /* in: signal type */
246
ulint sender, /* in: TRX_SIG_SELF or
247
TRX_SIG_OTHER_SESS */
248
que_thr_t* receiver_thr, /* in: query thread which wants the
249
reply, or NULL; if type is
250
TRX_SIG_END_WAIT, this must be NULL */
251
trx_savept_t* savept, /* in: possible rollback savepoint, or
253
que_thr_t** next_thr); /* in/out: next query thread to run;
254
if the value which is passed in is
255
a pointer to a NULL pointer, then the
256
calling function can start running
257
a new query thread; if the parameter
258
is NULL, it is ignored */
259
/********************************************************************
260
Send the reply message when a signal in the queue of the trx has
266
trx_sig_t* sig, /* in: signal */
267
que_thr_t** next_thr); /* in/out: next query thread to run;
268
if the value which is passed in is
269
a pointer to a NULL pointer, then the
270
calling function can start running
271
a new query thread */
272
/********************************************************************
273
Removes the signal object from a trx signal queue. */
278
trx_t* trx, /* in: trx handle */
279
trx_sig_t* sig); /* in, own: signal */
280
/********************************************************************
281
Starts handling of a trx signal. */
284
trx_sig_start_handle(
285
/*=================*/
286
trx_t* trx, /* in: trx handle */
287
que_thr_t** next_thr); /* in/out: next query thread to run;
288
if the value which is passed in is
289
a pointer to a NULL pointer, then the
290
calling function can start running
291
a new query thread */
292
/********************************************************************
293
Ends signal handling. If the session is in the error state, and
294
trx->graph_before_signal_handling != NULL, returns control to the error
295
handling routine of the graph (currently only returns the control to the
296
graph root which then sends an error message to the client). */
299
trx_end_signal_handling(
300
/*====================*/
301
trx_t* trx); /* in: trx */
302
/*************************************************************************
303
Creates a commit command node struct. */
308
/* out, own: commit node struct */
309
mem_heap_t* heap); /* in: mem heap where created */
310
/***************************************************************
311
Performs an execution step for a commit type node in a query graph. */
316
/* out: query thread to run next, or NULL */
317
que_thr_t* thr); /* in: query thread */
319
/**************************************************************************
320
Prints info about a transaction to the given file. The caller must own the
326
FILE* f, /* in: output stream */
327
trx_t* trx, /* in: transaction */
328
ulint max_query_len); /* in: max query length to print, or 0 to
329
use the default max length */
331
#ifndef UNIV_HOTBACKUP
332
/**************************************************************************
333
Determines if the currently running transaction has been interrupted. */
338
/* out: TRUE if interrupted */
339
trx_t* trx); /* in: transaction */
340
#else /* !UNIV_HOTBACKUP */
341
#define trx_is_interrupted(trx) FALSE
342
#endif /* !UNIV_HOTBACKUP */
344
/***********************************************************************
345
Compares the "weight" (or size) of two transactions. The weight of one
346
transaction is estimated as the number of altered rows + the number of
347
locked rows. Transactions that have edited non-transactional tables are
348
considered heavier than ones that have not. */
353
/* out: <0, 0 or >0; similar to strcmp(3) */
354
trx_t* a, /* in: the first transaction to be compared */
355
trx_t* b); /* in: the second transaction to be compared */
357
/* Signal to a transaction */
358
struct trx_sig_struct{
359
ulint type; /* signal type */
360
ulint sender; /* TRX_SIG_SELF or
361
TRX_SIG_OTHER_SESS */
362
que_thr_t* receiver; /* non-NULL if the sender of the signal
363
wants reply after the operation induced
364
by the signal is completed */
365
trx_savept_t savept; /* possible rollback savepoint */
366
UT_LIST_NODE_T(trx_sig_t)
367
signals; /* queue of pending signals to the
369
UT_LIST_NODE_T(trx_sig_t)
370
reply_signals; /* list of signals for which the sender
371
transaction is waiting a reply */
374
#define TRX_MAGIC_N 91118598
376
/* The transaction handle; every session has a trx object which is freed only
377
when the session is freed; in addition there may be session-less transactions
378
rolling back after a database recovery */
382
/* All the next fields are protected by the kernel mutex, except the
383
undo logs which are protected by undo_mutex */
384
const char* op_info; /* English text describing the
385
current operation, or an empty
387
unsigned is_purge:1; /* 0=user transaction, 1=purge */
388
ulint conc_state; /* state of the trx from the point
389
of view of concurrency control:
390
TRX_ACTIVE, TRX_COMMITTED_IN_MEMORY,
392
time_t start_time; /* time the trx object was created
393
or the state last time became
395
ulint isolation_level;/* TRX_ISO_REPEATABLE_READ, ... */
396
ibool check_foreigns; /* normally TRUE, but if the user
397
wants to suppress foreign key checks,
398
(in table imports, for example) we
400
ibool check_unique_secondary;
401
/* normally TRUE, but if the user
402
wants to speed up inserts by
403
suppressing unique key checks
404
for secondary indexes when we decide
405
if we can use the insert buffer for
406
them, we set this FALSE */
407
dulint id; /* transaction id */
408
XID xid; /* X/Open XA transaction
409
identification to identify a
410
transaction branch */
411
ibool support_xa; /* normally we do the XA two-phase
412
commit steps, but by setting this to
413
FALSE, one can save CPU time and about
414
150 bytes in the undo log size as then
416
dulint no; /* transaction serialization number ==
417
max trx id when the transaction is
418
moved to COMMITTED_IN_MEMORY state */
419
ibool flush_log_later;/* when we commit the transaction
420
in MySQL's binlog write, we will
421
flush the log to disk later in
423
ibool must_flush_log_later;/* this flag is set to TRUE in
424
trx_commit_off_kernel() if
425
flush_log_later was TRUE, and there
426
were modifications by the transaction;
427
in that case we must flush the log
428
in trx_commit_complete_for_mysql() */
429
dulint commit_lsn; /* lsn at the time of the commit */
430
ibool dict_operation; /* TRUE if the trx is used to create
431
a table, create an index, or drop a
432
table. This is a hint that the table
433
may need to be dropped in crash
435
dulint table_id; /* table id if the preceding field is
437
/*------------------------------*/
438
unsigned duplicates:2; /* TRX_DUP_IGNORE | TRX_DUP_REPLACE */
439
unsigned active_trans:2; /* 1 - if a transaction in MySQL
440
is active. 2 - if prepare_commit_mutex
442
void* mysql_thd; /* MySQL thread handle corresponding
443
to this trx, or NULL */
444
char** mysql_query_str;/* pointer to the field in mysqld_thd
445
which contains the pointer to the
446
current SQL query string */
447
const char* mysql_log_file_name;
448
/* if MySQL binlog is used, this field
449
contains a pointer to the latest file
450
name; this is NULL if binlog is not
452
ib_longlong mysql_log_offset;/* if MySQL binlog is used, this field
453
contains the end offset of the binlog
455
os_thread_id_t mysql_thread_id;/* id of the MySQL thread associated
456
with this transaction object */
457
ulint mysql_process_no;/* since in Linux, 'top' reports
458
process id's and not thread id's, we
459
store the process number too */
460
/*------------------------------*/
461
ulint n_mysql_tables_in_use; /* number of Innobase tables
462
used in the processing of the current
463
SQL statement in MySQL */
464
ulint mysql_n_tables_locked;
465
/* how many tables the current SQL
466
statement uses, except those
467
in consistent read */
468
ibool dict_operation_lock_mode;
469
/* 0, RW_S_LATCH, or RW_X_LATCH:
470
the latch mode trx currently holds
471
on dict_operation_lock */
472
ibool has_search_latch;
473
/* TRUE if this trx has latched the
474
search system latch in S-mode */
475
ulint search_latch_timeout;
476
/* If we notice that someone is
477
waiting for our S-lock on the search
478
latch to be released, we wait in
479
row0sel.c for BTR_SEA_TIMEOUT new
480
searches until we try to keep
481
the search latch again over
482
calls from MySQL; this is intended
483
to reduce contention on the search
485
/*------------------------------*/
486
ibool declared_to_be_inside_innodb;
487
/* this is TRUE if we have declared
489
srv_conc_enter_innodb to be inside the
491
ulint n_tickets_to_enter_innodb;
492
/* this can be > 0 only when
493
declared_to_... is TRUE; when we come
494
to srv_conc_innodb_enter, if the value
495
here is > 0, we decrement this by 1 */
496
/*------------------------------*/
497
lock_t* auto_inc_lock; /* possible auto-inc lock reserved by
498
the transaction; note that it is also
499
in the lock list trx_locks */
500
UT_LIST_NODE_T(trx_t)
501
trx_list; /* list of transactions */
502
UT_LIST_NODE_T(trx_t)
503
mysql_trx_list; /* list of transactions created for
505
/*------------------------------*/
506
ulint error_state; /* 0 if no error, otherwise error
507
number; NOTE That ONLY the thread
508
doing the transaction is allowed to
509
set this field: this is NOT protected
510
by the kernel mutex */
511
void* error_info; /* if the error number indicates a
512
duplicate key error, a pointer to
513
the problematic index is stored here */
514
sess_t* sess; /* session of the trx, NULL if none */
515
ulint que_state; /* TRX_QUE_RUNNING, TRX_QUE_LOCK_WAIT,
517
que_t* graph; /* query currently run in the session,
518
or NULL if none; NOTE that the query
519
belongs to the session, and it can
520
survive over a transaction commit, if
521
it is a stored procedure with a COMMIT
522
WORK statement, for instance */
523
ulint n_active_thrs; /* number of active query threads */
524
ibool handling_signals;/* this is TRUE as long as the trx
525
is handling signals */
526
que_t* graph_before_signal_handling;
527
/* value of graph when signal handling
528
for this trx started: this is used to
529
return control to the original query
530
graph for error processing */
531
trx_sig_t sig; /* one signal object can be allocated
532
in this space, avoiding mem_alloc */
533
UT_LIST_BASE_NODE_T(trx_sig_t)
534
signals; /* queue of processed or pending
535
signals to the trx */
536
UT_LIST_BASE_NODE_T(trx_sig_t)
537
reply_signals; /* list of signals sent by the query
538
threads of this trx for which a thread
539
is waiting for a reply; if this trx is
540
killed, the reply requests in the list
542
/*------------------------------*/
543
lock_t* wait_lock; /* if trx execution state is
544
TRX_QUE_LOCK_WAIT, this points to
545
the lock request, otherwise this is
547
ibool was_chosen_as_deadlock_victim;
548
/* when the transaction decides to wait
549
for a lock, it sets this to FALSE;
550
if another transaction chooses this
551
transaction as a victim in deadlock
552
resolution, it sets this to TRUE */
553
time_t wait_started; /* lock wait started at this time */
554
UT_LIST_BASE_NODE_T(que_thr_t)
555
wait_thrs; /* query threads belonging to this
556
trx that are in the QUE_THR_LOCK_WAIT
558
ulint deadlock_mark; /* a mark field used in deadlock
559
checking algorithm */
560
/*------------------------------*/
561
mem_heap_t* lock_heap; /* memory heap for the locks of the
563
UT_LIST_BASE_NODE_T(lock_t)
564
trx_locks; /* locks reserved by the transaction */
565
/*------------------------------*/
566
mem_heap_t* global_read_view_heap;
567
/* memory heap for the global read
569
read_view_t* global_read_view;
570
/* consistent read view associated
571
to a transaction or NULL */
572
read_view_t* read_view; /* consistent read view used in the
573
transaction or NULL, this read view
574
if defined can be normal read view
575
associated to a transaction (i.e.
576
same as global_read_view) or read view
577
associated to a cursor */
578
/*------------------------------*/
579
UT_LIST_BASE_NODE_T(trx_named_savept_t)
580
trx_savepoints; /* savepoints set with SAVEPOINT ...,
582
/*------------------------------*/
583
mutex_t undo_mutex; /* mutex protecting the fields in this
584
section (down to undo_no_arr), EXCEPT
585
last_sql_stat_start, which can be
586
accessed only when we know that there
587
cannot be any activity in the undo
589
dulint undo_no; /* next undo log record number to
590
assign; since the undo log is
591
private for a transaction, this
592
is a simple ascending sequence
593
with no gaps; thus it represents
594
the number of modified/inserted
595
rows in a transaction */
596
trx_savept_t last_sql_stat_start;
597
/* undo_no when the last sql statement
598
was started: in case of an error, trx
599
is rolled back down to this undo
600
number; see note at undo_mutex! */
601
trx_rseg_t* rseg; /* rollback segment assigned to the
602
transaction, or NULL if not assigned
604
trx_undo_t* insert_undo; /* pointer to the insert undo log, or
605
NULL if no inserts performed yet */
606
trx_undo_t* update_undo; /* pointer to the update undo log, or
607
NULL if no update performed yet */
608
dulint roll_limit; /* least undo number to undo during
610
ulint pages_undone; /* number of undo log pages undone
611
since the last undo log truncation */
612
trx_undo_arr_t* undo_no_arr; /* array of undo numbers of undo log
613
records which are currently processed
614
by a rollback operation */
615
ulint n_autoinc_rows; /* no. of AUTO-INC rows required for
616
an SQL statement. This is useful for
618
/*------------------------------*/
619
char detailed_error[256]; /* detailed error message for last
623
#define TRX_MAX_N_THREADS 32 /* maximum number of
624
concurrent threads running a
625
single operation of a
626
transaction, e.g., a parallel
628
/* Transaction concurrency states (trx->conc_state) */
629
#define TRX_NOT_STARTED 1
631
#define TRX_COMMITTED_IN_MEMORY 3
632
#define TRX_PREPARED 4 /* Support for 2PC/XA */
634
/* Transaction execution states when trx->conc_state == TRX_ACTIVE */
635
#define TRX_QUE_RUNNING 1 /* transaction is running */
636
#define TRX_QUE_LOCK_WAIT 2 /* transaction is waiting for a lock */
637
#define TRX_QUE_ROLLING_BACK 3 /* transaction is rolling back */
638
#define TRX_QUE_COMMITTING 4 /* transaction is committing */
640
/* Transaction isolation levels (trx->isolation_level) */
641
#define TRX_ISO_READ_UNCOMMITTED 1 /* dirty read: non-locking
642
SELECTs are performed so that
643
we do not look at a possible
644
earlier version of a record;
645
thus they are not 'consistent'
646
reads under this isolation
647
level; otherwise like level
650
#define TRX_ISO_READ_COMMITTED 2 /* somewhat Oracle-like
651
isolation, except that in
652
range UPDATE and DELETE we
653
must block phantom rows
655
SELECT ... FOR UPDATE and ...
656
LOCK IN SHARE MODE only lock
657
the index records, NOT the
658
gaps before them, and thus
659
allow free inserting;
660
each consistent read reads its
663
#define TRX_ISO_REPEATABLE_READ 3 /* this is the default;
664
all consistent reads in the
665
same trx read the same
667
full next-key locking used
668
in locking reads to block
669
insertions into gaps */
671
#define TRX_ISO_SERIALIZABLE 4 /* all plain SELECTs are
672
converted to LOCK IN SHARE
675
/* Treatment of duplicate values (trx->duplicates; for example, in inserts).
676
Multiple flags can be combined with bitwise OR. */
677
#define TRX_DUP_IGNORE 1 /* duplicate rows are to be updated */
678
#define TRX_DUP_REPLACE 2 /* duplicate rows are to be replaced */
681
/* Types of a trx signal */
682
#define TRX_SIG_NO_SIGNAL 100
683
#define TRX_SIG_TOTAL_ROLLBACK 1
684
#define TRX_SIG_ROLLBACK_TO_SAVEPT 2
685
#define TRX_SIG_COMMIT 3
686
#define TRX_SIG_ERROR_OCCURRED 4
687
#define TRX_SIG_BREAK_EXECUTION 5
689
/* Sender types of a signal */
690
#define TRX_SIG_SELF 1 /* sent by the session itself, or
691
by an error occurring within this
693
#define TRX_SIG_OTHER_SESS 2 /* sent by another session (which
694
must hold rights to this) */
696
/* Commit command node in a query graph */
697
struct commit_node_struct{
698
que_common_t common; /* node type: QUE_NODE_COMMIT */
699
ulint state; /* node execution state */
702
/* Commit node states */
703
#define COMMIT_NODE_SEND 1
704
#define COMMIT_NODE_WAIT 2
708
#include "trx0trx.ic"