MySQL 8.4.3
Source Code Documentation
trx0trx.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1996, 2024, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/trx0trx.h
29 The transaction
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34#ifndef trx0trx_h
35#define trx0trx_h
36
37#include <atomic>
38#include <set>
39
40#include "ha_prototypes.h"
41
42#include "dict0types.h"
43#include "trx0types.h"
44#include "ut0new.h"
45
46#include "lock0types.h"
47#include "mem0mem.h"
48#include "que0types.h"
49#include "trx0xa.h"
50#include "usr0types.h"
51#include "ut0vec.h"
52#ifndef UNIV_HOTBACKUP
53#include "fts0fts.h"
54#endif /* !UNIV_HOTBACKUP */
55#include "read0read.h"
56#include "sql/handler.h" // Xa_state_list
57#include "srv0srv.h"
58
59/* std::vector to store the trx id & table id of tables that needs to be
60 * rollbacked. We take SHARED MDL on these tables inside
61 * trx_recovery_rollback_thread before letting server accept connections */
62extern std::vector<std::pair<trx_id_t, table_id_t>> to_rollback_trx_tables;
63
64// Forward declaration
65struct mtr_t;
66
67// Forward declaration
68class ReadView;
69
70// Forward declaration
71class Flush_observer;
72
73/** Dummy session used currently in MySQL interface */
75
76#ifndef UNIV_HOTBACKUP
77/** Set flush observer for the transaction
78@param[in,out] trx transaction struct
79@param[in] observer flush observer */
80void trx_set_flush_observer(trx_t *trx, Flush_observer *observer);
81
82/** Set detailed error message for the transaction.
83@param[in] trx Transaction struct
84@param[in] msg Detailed error message */
85void trx_set_detailed_error(trx_t *trx, const char *msg);
86
87/** Set detailed error message for the transaction from a file. Note that the
88 file is rewinded before reading from it. */
90 trx_t *trx, /*!< in: transaction struct */
91 FILE *file); /*!< in: file to read message from */
92/** Retrieves the error_info field from a trx.
93 @return the error index */
94static inline const dict_index_t *trx_get_error_index(
95 const trx_t *trx); /*!< in: trx object */
96/** Creates a transaction object for MySQL.
97 @return own: transaction object */
99/** Creates a transaction object for background operations by the master thread.
100 @return own: transaction object */
102
103/** Resurrect table locks for resurrected transactions.
104@param[in] all false: resurrect locks for dictionary transactions,
105 true : resurrect locks for all transactions. */
106void trx_resurrect_locks(bool all);
107
108/** Clear all resurrected table IDs. Needs to be called after all tables locks
109are resurrected. */
111
112/** Free and initialize a transaction object instantiated during recovery.
113@param[in,out] trx transaction object to free and initialize */
114void trx_free_resurrected(trx_t *trx);
115
116/** Free a transaction that was allocated by background or user threads.
117@param[in,out] trx transaction object to free */
119
120/** At shutdown, frees a transaction object that represents either:
121 - a PREPARED transaction,
122 - or a recovered ACTIVE transaction.
123@param[in, out] trx transaction object to free */
125
126/** Free a transaction object for MySQL.
127@param[in,out] trx transaction */
128void trx_free_for_mysql(trx_t *trx);
129
130/** Disconnect a transaction from MySQL.
131@param[in,out] trx transaction */
132void trx_disconnect_plain(trx_t *trx);
133
134/** Disconnect a prepared transaction from MySQL.
135@param[in,out] trx transaction */
137
138/** Creates trx objects for transactions and initializes the trx list of
139 trx_sys at database start. Rollback segment and undo log lists must
140 already exist when this function is called, because the lists of
141 transactions to be rolled back or cleaned up are built based on the
142 undo log lists. */
144
145/** Starts the transaction if it is not yet started.
146@param[in,out] trx Transaction
147@param[in] read_write True if read write transaction */
148void trx_start_if_not_started_xa_low(trx_t *trx, bool read_write);
149
150/** Starts the transaction if it is not yet started.
151@param[in] trx Transaction
152@param[in] read_write True if read write transaction */
153void trx_start_if_not_started_low(trx_t *trx, bool read_write);
154
155/** Starts a transaction for internal processing. */
156void trx_start_internal_low(trx_t *trx); /*!< in/out: transaction */
157
158/** Starts a read-only transaction for internal processing.
159@param[in,out] trx transaction to be started */
161
162/** Commits a transaction. */
163void trx_commit(trx_t *trx); /*!< in/out: transaction */
164
165/** Commits a transaction and a mini-transaction.
166@param[in,out] trx Transaction
167@param[in,out] mtr Mini-transaction (will be committed), or null if trx made no
168modifications */
169void trx_commit_low(trx_t *trx, mtr_t *mtr);
170
171/** Cleans up a transaction at database startup. The cleanup is needed if
172 the transaction already got to the middle of a commit when the database
173 crashed, and we cannot roll it back. */
174void trx_cleanup_at_db_startup(trx_t *trx); /*!< in: transaction */
175/** Does the transaction commit for MySQL.
176 @return DB_SUCCESS or error number */
177dberr_t trx_commit_for_mysql(trx_t *trx); /*!< in/out: transaction */
178
179/**
180Does the transaction prepare for MySQL.
181@param[in, out] trx Transaction instance to prepare */
182
184
185/** This function is used to find number of prepared transactions and
186 their transaction objects for a recovery.
187 @return number of prepared transactions */
189 XA_recover_txn *txn_list, /*!< in/out: prepared transactions */
190 ulint len, /*!< in: number of slots in xid_list */
191 MEM_ROOT *mem_root); /*!< in: memory for table names */
192
193/** Find prepared transactions that are marked as prepared in TC, for recovery
194purposes.
195@param[in,out] xa_list prepared transactions state
196@return 0 if successful or error number */
198
199/** This function is used to find one X/Open XA distributed transaction
200 which is in the prepared state
201 @param[in] xid X/Open XA transaction identifier
202 @return trx or NULL; on match, the trx->xid will be invalidated;
203 note that the trx may have been committed */
204trx_t *trx_get_trx_by_xid(const XID *xid);
205
206/** If required, flushes the log to disk if we called trx_commit_for_mysql()
207 with trx->flush_log_later == true. */
208void trx_commit_complete_for_mysql(trx_t *trx); /*!< in/out: transaction */
209/** Marks the latest SQL statement ended. */
210void trx_mark_sql_stat_end(trx_t *trx); /*!< in: trx handle */
211/** Assigns a read view for a consistent read query. All the consistent reads
212 within the same transaction will get the same read view, which is created
213 when this function is first called for a new started transaction. */
214ReadView *trx_assign_read_view(trx_t *trx); /*!< in: active transaction */
215
216/** @return the transaction's read view or NULL if one not assigned. */
217static inline ReadView *trx_get_read_view(trx_t *trx);
218
219/** @return the transaction's read view or NULL if one not assigned. */
220static inline const ReadView *trx_get_read_view(const trx_t *trx);
221
222/** Prepares a transaction for commit/rollback. */
223void trx_commit_or_rollback_prepare(trx_t *trx); /*!< in/out: transaction */
224/** Creates a commit command node struct.
225 @return own: commit node struct */
227 mem_heap_t *heap); /*!< in: mem heap where created */
228/** Performs an execution step for a commit type node in a query graph.
229 @return query thread to run next, or NULL */
230que_thr_t *trx_commit_step(que_thr_t *thr); /*!< in: query thread */
231
232/** Prints info about a transaction.
233 Caller must hold trx_sys->mutex. */
234void trx_print_low(FILE *f,
235 /*!< in: output stream */
236 const trx_t *trx,
237 /*!< in: transaction */
238 ulint max_query_len,
239 /*!< in: max query length to print,
240 must be positive */
241 ulint n_rec_locks,
242 /*!< in: lock_number_of_rows_locked(&trx->lock) */
243 ulint n_trx_locks,
244 /*!< in: length of trx->lock.trx_locks */
245 ulint heap_size);
246/*!< in: mem_heap_get_size(trx->lock.lock_heap) */
247
248/** Prints info about a transaction.
249The caller must hold lock_sys exclusive global latch and trx_sys->mutex.
250@param[in] f output stream
251@param[in] trx transaction
252@param[in] max_query_len max query length to print, must be positive */
253void trx_print_latched(FILE *f, const trx_t *trx, ulint max_query_len);
254
255/** Prints info about a transaction.
256Acquires and releases lock_sys exclusive global latch and trx_sys->mutex.
257@param[in] f output stream
258@param[in] trx transaction
259@param[in] max_query_len max query length to print, must be positive */
260void trx_print(FILE *f, const trx_t *trx, ulint max_query_len);
261
262/** Determine if a transaction is a dictionary operation.
263 @return dictionary operation mode */
264[[nodiscard]] static inline enum trx_dict_op_t trx_get_dict_operation(
265 const trx_t *trx); /*!< in: transaction */
266
267/** Flag a transaction a dictionary operation.
268@param[in,out] trx transaction
269@param[in] op operation, not TRX_DICT_OP_NONE */
270static inline void trx_set_dict_operation(trx_t *trx, enum trx_dict_op_t op);
271
272/** Determines if a transaction is in the given state.
273The caller must hold trx_sys->mutex, or it must be the thread
274that is serving a running transaction.
275A running RW transaction must be in trx_sys->rw_trx_list.
276@param[in] trx Transaction.
277@param[in] state State.
278@return true if trx->state == state */
279[[nodiscard]] static inline bool trx_state_eq(const trx_t *trx,
280 trx_state_t state);
281#ifdef UNIV_DEBUG
282/** Determines if trx can be handled by current thread, which is when
283trx->mysql_thd is nullptr (a "background" trx) or equals current_thd.
284@param[in] trx The transaction to check
285@return true iff current thread can handle the transaction
286*/
288
289/** Determines if trx can be handled by current thread, which is when
290trx->mysql_thd is nullptr (a "background" trx) or equals current_thd,
291or is a victim being killed by HP transaction run by the current thread.
292@param[in] trx The transaction to check
293@return true iff current thread can handle the transaction
294*/
296
297/** Asserts that a transaction has been started.
298 The caller must hold trx_sys->mutex.
299 @return true if started */
300[[nodiscard]] bool trx_assert_started(const trx_t *trx); /*!< in: transaction */
301#endif /* UNIV_DEBUG */
302
303/** Determines if the currently running transaction has been interrupted.
304 @return true if interrupted */
305bool trx_is_interrupted(const trx_t *trx); /*!< in: transaction */
306/** Determines if the currently running transaction is in strict mode.
307 @return true if strict */
308bool trx_is_strict(trx_t *trx); /*!< in: transaction */
309
310/** Compares the "weight" (or size) of two transactions. Transactions that
311 have edited non-transactional tables are considered heavier than ones
312 that have not.
313 @return true if weight(a) >= weight(b) */
314bool trx_weight_ge(const trx_t *a, /*!< in: the transaction to be compared */
315 const trx_t *b); /*!< in: the transaction to be compared */
316/* Maximum length of a string that can be returned by
317trx_get_que_state_str(). */
318constexpr uint32_t TRX_QUE_STATE_STR_MAX_LEN = 12; /* "ROLLING BACK" */
319
320/** Retrieves transaction's que state in a human readable string. The string
321 should not be free()'d or modified.
322 @return string in the data segment */
323static inline const char *trx_get_que_state_str(
324 const trx_t *trx); /*!< in: transaction */
325
326/** Retreieves the transaction ID.
327In a given point in time it is guaranteed that IDs of the running
328transactions are unique. The values returned by this function for readonly
329transactions may be reused, so a subsequent RO transaction may get the same ID
330as a RO transaction that existed in the past. The values returned by this
331function should be used for printing purposes only.
332@param[in] trx transaction whose id to retrieve
333@return transaction id */
334static inline trx_id_t trx_get_id_for_print(const trx_t *trx);
335
336/** Assign a temp-tablespace bound rollback-segment to a transaction.
337@param[in,out] trx transaction that involves write to temp-table. */
338void trx_assign_rseg_temp(trx_t *trx);
339
340/** Create the trx_t pool */
341void trx_pool_init();
342
343/** Destroy the trx_t pool */
344void trx_pool_close();
345
346/**
347Set the transaction as a read-write transaction if it is not already
348tagged as such.
349@param[in,out] trx Transaction that needs to be "upgraded" to RW from RO */
350void trx_set_rw_mode(trx_t *trx);
351
352/**
353@param[in] requestor Transaction requesting the lock
354@param[in] holder Transaction holding the lock
355@return the transaction that will be rolled back, null don't care */
356
357static inline const trx_t *trx_arbitrate(const trx_t *requestor,
358 const trx_t *holder);
359
360/**
361@param[in] trx Transaction to check
362@return true if the transaction is a high priority transaction.*/
363static inline bool trx_is_high_priority(const trx_t *trx);
364
365/**
366If this is a high priority transaction,
367kill all transactions that are blocking this transaction from acquiring locks.
368@param[in,out] trx High priority transaction */
369void trx_kill_blocking(trx_t *trx);
370
371/**
372Check if redo/noredo rseg is modified for insert/update.
373@param[in] trx Transaction to check */
374static inline bool trx_is_rseg_updated(const trx_t *trx);
375#endif /* !UNIV_HOTBACKUP */
376
377typedef std::vector<ib_lock_t *, ut::allocator<ib_lock_t *>> lock_pool_t;
378
379/** Latching protocol for trx_lock_t::que_state. trx_lock_t::que_state
380 captures the state of the query thread during the execution of a query.
381 This is different from a transaction state. The query state of a transaction
382 can be updated asynchronously by other threads. The other threads can be
383 system threads, like the timeout monitor thread or user threads executing
384 other queries. Another thing to be mindful of is that there is a delay between
385 when a query thread is put into LOCK_WAIT state and before it actually starts
386 waiting. Between these two events it is possible that the query thread is
387 granted the lock it was waiting for, which implies that the state can be
388 changed asynchronously.
389
390 All these operations take place within the context of locking. Therefore state
391 changes within the locking code must latch the shard with the wait_lock and
392 the trx->mutex when changing trx->lock.que_state to TRX_QUE_LOCK_WAIT or
393 trx->lock.wait_lock to non-NULL but when the lock wait ends it is sufficient
394 to only acquire the trx->mutex.
395 To query the state either of the mutexes is sufficient within the locking
396 code and no mutex is required when the query thread is no longer waiting. */
397
398/** The locks and state of an active transaction.
399Protected by exclusive lock_sys latch or trx->mutex combined with shared
400lock_sys latch (unless stated otherwise for particular field). */
402 /** Default constructor. */
403 trx_lock_t() = default;
404
405 ulint n_active_thrs; /*!< number of active query threads */
406
407 trx_que_t que_state; /*!< valid when trx->state
408 == TRX_STATE_ACTIVE: TRX_QUE_RUNNING,
409 TRX_QUE_LOCK_WAIT, ... */
410
411 /** Incremented each time a lock is added or removed from the
412 trx->lock.trx_locks, so that the thread which iterates over the list can spot
413 a change if it occurred while it was reacquiring latches.
414 Protected by trx->mutex. */
416
417 /** If this transaction is waiting for a lock, then blocking_trx points to a
418 transaction which holds a conflicting lock.
419 It is possible that the transaction has trx->lock.wait_lock == nullptr, yet it
420 has non-null value of trx->lock.blocking_trx. For example this can happen when
421 we are in the process of moving locks from one heap_no to another. This
422 however is always done while the lock_sys shards which contain the queues
423 involved are latched and conceptually it is true that the blocking_trx is
424 the one for which the transaction waits, even though temporarily there is no
425 pointer to a particular WAITING lock object.
426
427 This field is changed from null to non-null, when holding this->mutex and
428 mutex for lock_sys shard containing the new value of trx->lock.wait_lock.
429 The field is changed from non-null to different non-null value, while holding
430 mutex for lock_sys shard containing the trx->lock.wait_lock.
431 The field is changed from non-null to null, while holding this->mutex,
432 mutex for lock_sys shard containing the old value of trx->lock.wait_lock,
433 before it was changed to null.
434
435 Readers might read it without any latch, but then they should validate the
436 value, i.e. test if it is not-null, and points to a valid trx.
437 To make any definite judgments one needs to latch the lock_sys shard
438 containing the trx->lock.wait_lock. */
439 std::atomic<trx_t *> blocking_trx;
440
441 /** The lock request of this transaction is waiting for.
442 It might be NULL if the transaction is not currently waiting, or if the lock
443 was temporarily removed during B-tree reorganization and will be recreated in
444 a different queue. Such move is protected by latching the shards containing
445 both queues, so the intermediate state should not be observed by readers who
446 latch the old shard.
447
448 Changes from NULL to non-NULL while holding trx->mutex and latching the shard
449 containing the new wait_lock value.
450 Changes from non-NULL to NULL while latching the shard containing the old
451 wait_lock value.
452 Never changes from non-NULL to other non-NULL directly.
453
454 Readers should hold exclusive global latch on lock_sys, as in general they
455 can't know what shard the lock belongs to before reading it.
456 However, in debug assertions, where we strongly believe to know the value of
457 this field in advance, we can:
458 - read without any latch if we believe the value should be NULL
459 - read latching only the shard containing the wait_lock we expect */
460 std::atomic<lock_t *> wait_lock;
461
462 /** Stores the type of the most recent lock for which this trx had to wait.
463 Set to lock_get_type_low(wait_lock) together with wait_lock in
464 lock_set_lock_and_trx_wait().
465 This field is not cleared when wait_lock is set to NULL during
466 lock_reset_lock_and_trx_wait() as in lock_wait_suspend_thread() we are
467 interested in reporting the last known value of this field via
468 thd_wait_begin(). When a thread has to wait for a lock, it first releases
469 lock-sys latch, and then calls lock_wait_suspend_thread() where among other
470 things it tries to report statistic via thd_wait_begin() about the kind of
471 lock (THD_WAIT_ROW_LOCK vs THD_WAIT_TABLE_LOCK) that caused the wait. But
472 there is a possibility that before it gets to call thd_wait_begin() some other
473 thread could latch lock-sys and grant the lock and call
474 lock_reset_lock_and_trx_wait(). In other words: in case another thread was
475 quick enough to grant the lock, we still would like to report the reason for
476 attempting to sleep.
477 Another common scenario of "setting trx->lock.wait_lock to NULL" is page
478 reorganization: when we have to move records between pages, we also move
479 locks, and when doing so, we temporarily remove the old waiting lock, and then
480 add another one. For example look at lock_rec_move_low(). It first calls
481 lock_reset_lock_and_trx_wait() which changes trx->lock.wait_lock to NULL, but
482 then calls lock_rec_add_to_queue() -> RecLock::create() -> RecLock::lock_add()
483 -> lock_set_lock_and_trx_wait() to set it again to the new lock. This all
484 happens while holding lock-sys latch, but we read wait_lock_type without this
485 latch, so we should not clear the wait_lock_type simply because somebody
486 changed wait_lock to NULL.
487 Protected by trx->mutex. */
489
491 /*!< when the transaction decides to
492 wait for a lock, it sets this to false;
493 if another transaction chooses this
494 transaction as a victim in deadlock
495 resolution, it sets this to true.
496 Protected by trx->mutex. */
497
498 /** Lock wait started at this time.
499 Writes under shared lock_sys latch combined with trx->mutex.
500 Reads require either trx->mutex or exclusive lock_sys latch. */
501 std::chrono::system_clock::time_point wait_started;
502
503 /** query thread belonging to this trx that is in QUE_THR_LOCK_WAIT state.
504 For threads suspended in a lock wait, this is protected by lock_sys latch for
505 the wait_lock's shard.
506 Otherwise, this may only be modified by the thread that is serving the running
507 transaction.
508 */
510
511 /** Pre-allocated record locks. Protected by trx->mutex. */
513
514 /** Pre-allocated table locks. Protected by trx->mutex. */
516
517 /** Next free record lock in pool. Protected by trx->mutex. */
519
520 /** Next free table lock in pool. Protected by trx->mutex. */
522
523 /** Memory heap for trx_locks. Protected by trx->mutex */
525
526 /** Locks requested by the transaction.
527 It is sorted so that LOCK_TABLE locks are before LOCK_REC locks.
528 Modifications are protected by trx->mutex and shard of lock_sys mutex.
529 Reads can be performed while holding trx->mutex or exclusive lock_sys latch.
530 One can also check if this list is empty or not from the thread running this
531 transaction without holding any latches, keeping in mind that other threads
532 might modify the list in parallel (for example during implicit-to-explicit
533 conversion, or when B-tree split or merge causes locks to be moved from one
534 page to another) - we rely on assumption that such operations do not change
535 the "emptiness" of the list and that one can check for emptiness in a safe
536 manner (in current implementation length of the list is stored explicitly so
537 one can read it without risking unsafe pointer operations) */
538 trx_lock_list_t trx_locks;
539
540 /** AUTOINC locks held by this transaction.
541 Note that these are also in the trx_locks list.
542 This vector needs to be freed explicitly when the trx instance is destroyed.
543 Protected by trx->mutex. */
545
546 /** Number of rec locks in this trx.
547 It is modified with shared lock_sys latch.
548 It is read with exclusive lock_sys latch. */
549 std::atomic<ulint> n_rec_locks;
550
551 /** Used to indicate that every lock of this transaction placed on a record
552 which is being purged should be inherited to the gap.
553 Readers should hold a latch on the lock they'd like to learn about whether or
554 not it should be inherited.
555 Writers who want to set it to true, should hold a latch on the lock-sys queue
556 they intend to add a lock to.
557 Writers may set it to false at any time. */
558 std::atomic<bool> inherit_all;
559
560 /** Weight of the waiting transaction used for scheduling.
561 The higher the weight the more we are willing to grant a lock to this
562 transaction.
563 Values are updated and read without any synchronization beyond that provided
564 by atomics, as slightly stale values do not hurt correctness, just the
565 performance. */
566 std::atomic<trx_schedule_weight_t> schedule_weight;
567
568#ifdef UNIV_DEBUG
569 /** When a transaction is forced to rollback due to a deadlock
570 check or by another high priority transaction this is true. Used
571 by debug checks in lock0lock.cc */
573#endif /* UNIV_DEBUG */
574
575 /** The transaction called ha_innobase::start_stmt() to
576 lock a table. Most likely a temporary table. */
578};
579
580/** Type used to store the list of tables that are modified by a given
581transaction. We store pointers to the table objects in memory because
582we know that a table object will not be destroyed while a transaction
583that modified it is running. */
584typedef std::set<dict_table_t *, std::less<dict_table_t *>,
587
588/** The transaction handle
589
590Normally, there is a 1:1 relationship between a transaction handle
591(trx) and a session (client connection). One session is associated
592with exactly one user transaction. There are some exceptions to this:
593
594* For DDL operations, a subtransaction is allocated that modifies the
595data dictionary tables. Lock waits and deadlocks are prevented by
596acquiring the dict_operation_lock before starting the subtransaction
597and releasing it after committing the subtransaction.
598
599* The purge system uses a special transaction that is not associated
600with any session.
601
602* If the system crashed or it was quickly shut down while there were
603transactions in the ACTIVE or PREPARED state, these transactions would
604no longer be associated with a session when the server is restarted.
605
606A session may be served by at most one thread at a time. The serving
607thread of a session might change in some MySQL implementations.
608Therefore we do not have std::this_thread::get_id() assertions in the code.
609
610Normally, only the thread that is currently associated with a running
611transaction may access (read and modify) the trx object, and it may do
612so without holding any mutex. The following are exceptions to this:
613
614* trx_rollback_resurrected() may access resurrected (connectionless)
615transactions while the system is already processing new user
616transactions. The trx_sys->mutex prevents a race condition between it
617and lock_trx_release_locks() [invoked by trx_commit()].
618
619* Print of transactions may access transactions not associated with
620the current thread. The caller must be holding trx_sys->mutex and
621exclusive global lock_sys latch.
622
623* When a transaction handle is in the trx_sys->mysql_trx_list or
624trx_sys->trx_list, some of its fields must not be modified without
625holding trx_sys->mutex exclusively.
626
627* The locking code (in particular, deadlock checking and implicit to
628explicit conversion) will access transactions associated to other
629connections. The locks of transactions are protected by lock_sys latches
630and sometimes by trx->mutex.
631
632* Killing of asynchronous transactions. */
633
634/** Represents an instance of rollback segment along with its state variables.*/
636 /** @return true iff no undo segment is allocated yet. */
637 bool is_empty() { return (insert_undo == nullptr && update_undo == nullptr); }
638
639 /** @return true iff only insert undo segment is allocated. */
641 return (insert_undo != nullptr && update_undo == nullptr);
642 }
643
644 /** @return true iff update undo segment is allocated. */
645 bool is_update() { return update_undo != nullptr; }
646
647 trx_rseg_t *rseg; /*!< rollback segment assigned to the
648 transaction, or NULL if not assigned
649 yet */
650 trx_undo_t *insert_undo; /*!< pointer to the insert undo log, or
651 NULL if no inserts performed yet */
652 trx_undo_t *update_undo; /*!< pointer to the update undo log, or
653 NULL if no update performed yet */
654};
655
656/** Rollback segments assigned to a transaction for undo logging. */
658 /** undo log ptr holding reference to a rollback segment that resides in
659 system/undo tablespace used for undo logging of tables that needs
660 to be recovered on crash. */
662
663 /** undo log ptr holding reference to a rollback segment that resides in
664 temp tablespace used for undo logging of tables that doesn't need
665 to be recovered on crash. */
667};
668
670 TRX_RSEG_TYPE_NONE = 0, /*!< void rollback segment type. */
671 TRX_RSEG_TYPE_REDO, /*!< redo rollback segment. */
672 TRX_RSEG_TYPE_NOREDO /*!< non-redo rollback segment. */
674
675struct trx_t {
677
678 /** dirty read: non-locking SELECTs are performed so that we
679 do not look at a possible earlier version of a record; thus
680 they are not 'consistent' reads under this isolation level;
681 otherwise like level 2 */
683
684 /** somewhat Oracle-like isolation, except that in range UPDATE
685 and DELETE we must block phantom rows with next-key locks;
686 SELECT ... FOR UPDATE and ... LOCK IN SHARE MODE only lock
687 the index records, NOT the gaps before them, and thus allow
688 free inserting; each consistent read reads its own snapshot */
690
691 /** this is the default; all consistent reads in the same trx
692 read the same snapshot; full next-key locking used in locking
693 reads to block insertions into gaps */
695
696 /** all plain SELECTs are converted to LOCK IN SHARE MODE
697 reads */
699 };
700
701 /** Default constructor */
702 trx_t() = default;
703
704 /** Mutex protecting the fields `state` and `lock` (except some fields of
705 `lock`, which are protected by lock_sys latches) */
707
708 /* Note: in_depth was split from in_innodb for fixing a RO
709 performance issue. Acquiring the trx_t::mutex for each row
710 costs ~3% in performance. It is not required for correctness.
711 Therefore we increment/decrement in_depth without holding any
712 mutex. The assumption is that the Server will only ever call
713 the handler from one thread. This is not true for kill_connection.
714 Therefore in innobase_kill_connection. We don't increment this
715 counter via TrxInInnoDB. */
716
717 uint32_t in_depth; /*!< Track nested TrxInInnoDB
718 count */
719
720 uint32_t in_innodb; /*!< if the thread is executing
721 in the InnoDB context count > 0. */
722
723 bool abort; /*!< if this flag is set then
724 this transaction must abort when
725 it can */
726
727 trx_id_t id; /*!< transaction id */
728
729 trx_id_t no; /*!< transaction serialization number:
730 max trx id shortly before the
731 transaction is moved to
732 COMMITTED_IN_MEMORY state.
733 Protected by trx_sys_t::mutex
734 when trx->in_rw_trx_list. Initially
735 set to TRX_ID_MAX. */
736
737 /** State of the trx from the point of view of concurrency control
738 and the valid state transitions.
739
740 Possible states:
741
742 TRX_STATE_NOT_STARTED
743 TRX_STATE_FORCED_ROLLBACK
744 TRX_STATE_ACTIVE
745 TRX_STATE_PREPARED
746 TRX_STATE_COMMITTED_IN_MEMORY (alias below COMMITTED)
747
748 Valid state transitions are:
749
750 Regular transactions:
751 * NOT_STARTED -> ACTIVE -> COMMITTED -> NOT_STARTED
752
753 Auto-commit non-locking read-only:
754 * NOT_STARTED -> ACTIVE -> NOT_STARTED
755
756 XA (2PC):
757 * NOT_STARTED -> ACTIVE -> PREPARED -> COMMITTED -> NOT_STARTED
758
759 Recovered XA:
760 * NOT_STARTED -> PREPARED -> COMMITTED -> (freed)
761
762 XA (2PC) (shutdown or disconnect before ROLLBACK or COMMIT):
763 * NOT_STARTED -> PREPARED -> (freed)
764
765 Disconnected XA can become recovered:
766 * ... -> ACTIVE -> PREPARED (connected) -> PREPARED (disconnected)
767 Disconnected means from mysql e.g due to the mysql client disconnection.
768 Latching and various transaction lists membership rules:
769
770 XA (2PC) transactions are always treated as non-autocommit.
771
772 Transitions to ACTIVE or NOT_STARTED occur when
773 !in_rw_trx_list (no trx_sys->mutex needed).
774
775 Autocommit non-locking read-only transactions move between states
776 without holding any mutex. They are !in_rw_trx_list.
777
778 All transactions, unless they are determined to be ac-nl-ro,
779 explicitly tagged as read-only or read-write, will first be put
780 on the read-only transaction list. Only when a !read-only transaction
781 in the read-only list tries to acquire an X or IX lock on a table
782 do we remove it from the read-only list and put it on the read-write
783 list. During this switch we assign it a rollback segment.
784
785 When a transaction is NOT_STARTED, it can be in_mysql_trx_list if
786 it is a user transaction. It cannot be in rw_trx_list.
787
788 ACTIVE->PREPARED->COMMITTED is only possible when trx->in_rw_trx_list.
789 The transition ACTIVE->PREPARED is protected by trx_sys->mutex.
790
791 ACTIVE->COMMITTED is possible when the transaction is in
792 rw_trx_list.
793
794 Transitions to COMMITTED are protected by trx->mutex.
795
796 NOTE: Some of these state change constraints are an overkill,
797 currently only required for a consistent view for printing stats.
798 This unnecessarily adds a huge cost for the general case. */
799
800 std::atomic<trx_state_t> state;
801
802 /* If set, this transaction should stop inheriting (GAP)locks.
803 Generally set to true during transaction prepare for RC or lower
804 isolation, if requested. Needed for replication replay where
805 we don't want to get blocked on GAP locks taken for protecting
806 concurrent unique insert or replace operation. */
808
809 ReadView *read_view; /*!< consistent read view used in the
810 transaction, or NULL if not yet set */
811
813 trx_list; /*!< list of transactions;
814 protected by trx_sys->mutex. */
816 no_list; /*!< Required during view creation
817 to check for the view limit for
818 transactions that are committing */
819
820 /** Information about the transaction locks and state.
821 Protected by trx->mutex or lock_sys latches or both */
823
824 /**
825 false: a normal transaction
826 true: a recovered transaction
827
828 Set to true when srv_is_being_started for recovered transactions.
829 Set to false without any protection in trx_init (where no other thread should
830 access this object anyway).
831 Can be read safely when holding trx_sys->mutex and trx belongs to rw_trx_list,
832 as trx_init can not be called until trx leaves rw_trx_list which requires the
833 trx_sys->mutex.
834 */
836
837 std::atomic<std::thread::id> killed_by; /*!< The thread ID that wants to
838 kill this transaction asynchronously.
839 This is required because we recursively
840 enter the handlerton methods and need
841 to distinguish between the kill thread
842 and the transaction thread.
843
844 Note: We need to be careful w.r.t the
845 Thread Pool. The thread doing the kill
846 should not leave InnoDB between the
847 mark and the actual async kill because
848 the running thread can change. */
849
850 /* These fields are not protected by any mutex. */
851 const char *op_info; /*!< English text describing the
852 current operation, or an empty
853 string */
854
855 /** Current isolation level */
857
858 bool check_foreigns; /*!< normally true, but if the user
859 wants to suppress foreign key checks,
860 (in table imports, for example) we
861 set this false */
862 /*------------------------------*/
863 /* MySQL has a transaction coordinator to coordinate two phase
864 commit between multiple storage engines and the binary log. When
865 an engine participates in a transaction, it's responsible for
866 registering itself using the trans_register_ha() API. */
867 bool is_registered; /* This flag is set to true after the
868 transaction has been registered with
869 the coordinator using the XA API, and
870 is set to false after commit or
871 rollback. */
872 /*------------------------------*/
874 /*!< normally true, but if the user
875 wants to speed up inserts by
876 suppressing unique key checks
877 for secondary indexes when we decide
878 if we can use the insert buffer for
879 them, we set this false */
880 bool flush_log_later; /* In 2PC, we hold the
881 prepare_commit mutex across
882 both phases. In that case, we
883 defer flush of the logs to disk
884 until after we release the
885 mutex. */
886 bool must_flush_log_later; /*!< this flag is set to true in
887 trx_commit() if flush_log_later was
888 true, and there were modifications by
889 the transaction; in that case we must
890 flush the log in
891 trx_commit_complete_for_mysql() */
893 /*!< true if this trx has latched the
894 search system latch in S-mode.
895 This now can only be true in
896 row_search_mvcc, the btr search latch
897 must has been released before exiting,
898 and this flag would be set to false */
899 trx_dict_op_t dict_operation; /**< @see enum trx_dict_op_t */
900
901 bool ddl_operation; /*!< True if this trx involves dd table
902 change */
903 bool ddl_must_flush; /*!< True if this trx involves dd table
904 change, and must flush */
905 bool in_truncate; /* This trx is doing truncation */
906
907 /* Fields protected by the srv_conc_mutex. */
909 /*!< this is true if we have declared
910 this transaction in
911 srv_conc_enter_innodb to be inside the
912 InnoDB engine */
914 /*!< this can be > 0 only when
915 declared_to_... is true; when we come
916 to srv_conc_innodb_enter, if the value
917 here is > 0, we decrement this by 1 */
919 /*!< 0, RW_S_LATCH, or RW_X_LATCH:
920 the latch mode trx currently holds
921 on dict_operation_lock. Protected
922 by dict_operation_lock. */
923
924 /** Time the state last time became TRX_STATE_ACTIVE. */
925 std::atomic<std::chrono::system_clock::time_point> start_time{
926 std::chrono::system_clock::time_point{}};
927 static_assert(decltype(start_time)::is_always_lock_free);
928
929 lsn_t commit_lsn; /*!< lsn at the time of the commit */
930
931 /*------------------------------*/
932 THD *mysql_thd; /*!< MySQL thread handle corresponding
933 to this trx, or NULL */
934
936 /*!< if MySQL binlog is used, this field
937 contains a pointer to the latest file
938 name; this is NULL if binlog is not
939 used */
941 /*!< if MySQL binlog is used, this
942 field contains the end offset of the
943 binlog entry */
944 /*------------------------------*/
945 uint32_t n_mysql_tables_in_use; /*!< number of Innobase tables
946 used in the processing of the current
947 SQL statement in MySQL */
949 /*!< how many tables the current SQL
950 statement uses, except those
951 in consistent read */
952 /*------------------------------*/
953#ifdef UNIV_DEBUG
954 /** True iff in trx_sys->rw_trx_list */
956
957#endif /* UNIV_DEBUG */
959 mysql_trx_list; /*!< list of transactions created for
960 MySQL; protected by trx_sys->mutex */
961#ifdef UNIV_DEBUG
963 /*!< true if in
964 trx_sys->mysql_trx_list */
965#endif /* UNIV_DEBUG */
966 /*------------------------------*/
967
968 /** DB_SUCCESS if no error, otherwise error number.
969 Accessed without any mutex only by the thread doing the transaction or, if it
970 is suspended (waiting for a lock), by the thread holding this->mutex which
971 has changed trx->lock.wait_lock to nullptr and will wake up the transaction.*/
973
974 const dict_index_t *error_index; /*!< if the error number indicates a
975 duplicate key error, a pointer to
976 the problematic index is stored here */
977 ulint error_key_num; /*!< if the index creation fails to a
978 duplicate key error, a mysql key
979 number of that index is stored here */
980 sess_t *sess; /*!< session of the trx, NULL if none */
981 que_t *graph; /*!< query currently run in the session,
982 or NULL if none; NOTE that the query
983 belongs to the session, and it can
984 survive over a transaction commit, if
985 it is a stored procedure with a COMMIT
986 WORK statement, for instance */
987 /*------------------------------*/
989 trx_savepoints{}; /*!< savepoints set with SAVEPOINT ..., oldest first */
990 /*------------------------------*/
991 UndoMutex undo_mutex; /*!< mutex protecting the fields in this
992 section (down to undo_no_arr), EXCEPT
993 last_sql_stat_start, which can be
994 accessed only when we know that there
995 cannot be any activity in the undo
996 logs! */
997 undo_no_t undo_no; /*!< next undo log record number to
998 assign; since the undo log is
999 private for a transaction, this
1000 is a simple ascending sequence
1001 with no gaps; thus it represents
1002 the number of modified/inserted
1003 rows in a transaction */
1005 /*!< space id where last undo record
1006 was written */
1008 /*!< undo_no when the last sql statement
1009 was started: in case of an error, trx
1010 is rolled back down to this undo
1011 number; see note at undo_mutex! */
1012 trx_rsegs_t rsegs; /* rollback segments for undo logging */
1013 undo_no_t roll_limit; /*!< least undo number to undo during
1014 a partial rollback; 0 otherwise */
1015#ifdef UNIV_DEBUG
1016 bool in_rollback; /*!< true when the transaction is
1017 executing a partial or full rollback */
1018#endif /* UNIV_DEBUG */
1019 ulint pages_undone; /*!< number of undo log pages undone
1020 since the last undo log truncation */
1021 /*------------------------------*/
1022 ulint n_autoinc_rows; /*!< no. of AUTO-INC rows required for
1023 an SQL statement. This is useful for
1024 multi-row INSERTs */
1025 /*------------------------------*/
1026 bool read_only; /*!< true if transaction is flagged
1027 as a READ-ONLY transaction.
1028 if auto_commit && will_lock == 0
1029 then it will be handled as a
1030 AC-NL-RO-SELECT (Auto Commit Non-Locking
1031 Read Only Select). A read only
1032 transaction will not be assigned an
1033 UNDO log. */
1034 bool auto_commit; /*!< true if it is an autocommit */
1035 uint32_t will_lock; /*!< Will acquire some locks. Increment
1036 each time we determine that a lock will
1037 be acquired by the MySQL layer. */
1038#ifndef UNIV_HOTBACKUP
1039 /*------------------------------*/
1040 fts_trx_t *fts_trx; /*!< FTS information, or NULL if
1041 transaction hasn't modified tables
1042 with FTS indexes (yet). */
1043 doc_id_t fts_next_doc_id; /* The document id used for updates */
1044 /*------------------------------*/
1045 uint32_t flush_tables; /*!< if "covering" the FLUSH TABLES",
1046 count of tables being flushed. */
1047
1048 /*------------------------------*/
1049 bool internal; /*!< true if it is a system/internal
1050 transaction background task. Such
1051 transactions are always treated as
1052 read-write. */
1053 /*------------------------------*/
1054 /** Transaction persists GTID. */
1056
1057#ifdef UNIV_DEBUG
1058 ulint start_line; /*!< Track where it was started from */
1059 const char *start_file; /*!< Filename where it was started */
1060#endif /* UNIV_DEBUG */
1061
1062 lint n_ref; /*!< Count of references, protected
1063 by trx_t::mutex. We can't release the
1064 locks nor commit the transaction until
1065 this reference is 0. We can change
1066 the state to COMMITTED_IN_MEMORY to
1067 signify that it is no longer
1068 "active". */
1069
1070 /** Version of this instance. It is incremented each time the
1071 instance is re-used in trx_start_low(). It is used to track
1072 whether a transaction has been restarted since it was tagged
1073 for asynchronous rollback. */
1074 std::atomic_uint64_t version;
1075
1076 XID *xid; /*!< X/Open XA transaction
1077 identification to identify a
1078 transaction branch */
1079 trx_mod_tables_t mod_tables; /*!< List of tables that were modified
1080 by this transaction */
1081#endif /* !UNIV_HOTBACKUP */
1082 /*------------------------------*/
1083 bool api_trx; /*!< trx started by InnoDB API */
1084 bool api_auto_commit; /*!< automatic commit */
1085 bool read_write; /*!< if read and write operation */
1086
1087 /** This flag is set for trx_t objects used by the purge sys. We use the flag
1088 when validating mysql_trx_list in trx_sys_before_pre_dd_shutdown_validate.
1089 Purge threads can have allocated trx_t objects visible in the mysql_trx_list
1090 at this point during shutdown, this is acceptable so we need a way to signal
1091 this fact. */
1093 /*------------------------------*/
1094 char *detailed_error; /*!< detailed error message for last
1095 error, or empty. */
1096 Flush_observer *flush_observer; /*!< flush observer */
1097
1098#ifdef UNIV_DEBUG
1099 bool is_dd_trx; /*!< True if the transaction is used for
1100 doing Non-locking Read-only Read
1101 Committed on DD tables */
1102#endif /* UNIV_DEBUG */
1104
1105 bool is_read_uncommitted() const {
1107 }
1108
1111 }
1112
1113 bool skip_gap_locks() const {
1114 switch (isolation_level) {
1115 case READ_UNCOMMITTED:
1116 case READ_COMMITTED:
1117 return (true);
1118 case REPEATABLE_READ:
1119 case SERIALIZABLE:
1120 return (false);
1121 }
1122 ut_d(ut_error);
1123 ut_o(return (false));
1124 }
1125
1126 bool allow_semi_consistent() const { return (skip_gap_locks()); }
1127 /** Checks if this transaction releases locks on non matching records due to
1128 low isolation level.
1129 @return true iff in this transaction's isolation level locks on records which
1130 do not match the WHERE clause are released */
1132};
1133
1134#ifndef UNIV_HOTBACKUP
1135/**
1136Transactions that aren't started by the MySQL server don't set
1137the trx_t::mysql_thd field. For such transactions we set the lock
1138wait timeout to 0 instead of the user configured value that comes
1139from innodb_lock_wait_timeout via trx_t::mysql_thd.
1140@param t transaction
1141@return lock wait timeout in seconds */
1144}
1145
1146/**
1147Determine if the transaction is a non-locking autocommit select
1148(implied read-only).
1149@param t transaction
1150@return true if non-locking autocommit select transaction. */
1151static inline bool trx_is_autocommit_non_locking(const trx_t *t) {
1152 return t->auto_commit && t->will_lock == 0;
1153}
1154
1155/** Check transaction state */
1156static inline void check_trx_state(const trx_t *t) {
1158 switch (t->state) {
1159 case TRX_STATE_PREPARED:
1160 /* fall through */
1161 case TRX_STATE_ACTIVE:
1163 return;
1166 break;
1167 }
1168 ut_error;
1169}
1170
1171/**
1172Assert that the transaction is in the trx_sys_t::rw_trx_list */
1173static inline void assert_trx_in_rw_list(const trx_t *t) {
1174 ut_ad(!t->read_only);
1175 ut_ad(t->in_rw_trx_list == !(t->read_only || !t->rsegs.m_redo.rseg));
1176 check_trx_state(t);
1177}
1178
1179/** Check if transaction is free so that it can be re-initialized.
1180@param t transaction handle */
1181static inline void assert_trx_is_free(const trx_t *t) {
1186 ut_ad((t)->lock.wait_thr == nullptr);
1187 ut_ad(UT_LIST_GET_LEN((t)->lock.trx_locks) == 0);
1188 ut_ad((t)->dict_operation == TRX_DICT_OP_NONE);
1189}
1190
1191/** Check if transaction is in-active so that it can be freed and put back to
1192transaction pool.
1193@param t transaction handle */
1194static inline void assert_trx_is_inactive(const trx_t *t) {
1197}
1198
1199#ifdef UNIV_DEBUG
1200/** Assert that an autocommit non-locking select cannot be in the
1201 rw_trx_list and that it is a read-only transaction.
1202 The transaction must be in the mysql_trx_list. */
1203static inline void assert_trx_nonlocking_or_in_list(const trx_t *t) {
1205 trx_state_t t_state = t->state;
1206 ut_ad(t->read_only);
1207 ut_ad(!t->is_recovered);
1208 ut_ad(!t->in_rw_trx_list);
1210 ut_ad(t_state == TRX_STATE_NOT_STARTED ||
1211 t_state == TRX_STATE_FORCED_ROLLBACK || t_state == TRX_STATE_ACTIVE);
1212 } else {
1213 check_trx_state(t);
1214 }
1215}
1216#else /* UNIV_DEBUG */
1217/** Assert that an autocommit non-locking select cannot be in the
1218 rw_trx_list and that it is a read-only transaction.
1219 The transaction must be in the mysql_trx_list. */
1220#define assert_trx_nonlocking_or_in_list(trx) ((void)0)
1221#endif /* UNIV_DEBUG */
1222
1223/**
1224Determine if the transaction is a non-locking autocommit select
1225with an explicit check for the read-only status.
1226@param t transaction
1227@return true if non-locking autocommit read-only transaction. */
1228static inline bool trx_is_ac_nl_ro(const trx_t *t) {
1230}
1231
1232/**
1233Increase the reference count. If the transaction is in state
1234TRX_STATE_COMMITTED_IN_MEMORY then the transaction is considered
1235committed and the reference count is not incremented.
1236@param trx Transaction that is being referenced */
1237static inline void trx_reference(trx_t *trx);
1238
1239/**
1240Release the transaction. Decrease the reference count.
1241@param trx Transaction that is being released */
1242static inline void trx_release_reference(trx_t *trx);
1243
1244/**
1245Check if the transaction is being referenced. */
1246static inline bool trx_is_referenced(const trx_t *t) { return t->n_ref > 0; }
1247
1248/** Calculates the "weight" of a transaction. The weight of one transaction
1249 is estimated as the number of altered rows + the number of locked rows.
1250 @param t transaction
1251 @return transaction weight */
1252static inline uint64_t TRX_WEIGHT(const trx_t *t) {
1253 return t->undo_no + UT_LIST_GET_LEN(t->lock.trx_locks);
1254}
1255
1256#ifdef UNIV_DEBUG
1257static inline void trx_start_if_not_started_xa(trx_t *t, bool rw,
1258 ut::Location loc) {
1259 t->start_line = loc.line;
1260 t->start_file = loc.filename;
1262}
1263
1264static inline void trx_start_if_not_started(trx_t *t, bool rw, ut::Location l) {
1265 t->start_line = l.line;
1266 t->start_file = l.filename;
1268}
1269
1270static inline void trx_start_internal(trx_t *t, ut::Location loc) {
1271 t->start_line = loc.line;
1272 t->start_file = loc.filename;
1274}
1275
1277 t->start_line = loc.line;
1278 t->start_file = loc.filename;
1280}
1281#else
1282static inline void trx_start_if_not_started_xa(trx_t *t, bool rw,
1283 ut::Location loc) {
1285}
1286
1287static inline void trx_start_internal(trx_t *t, ut::Location loc) {
1289}
1290
1291static inline void trx_start_internal_read_only(trx_t *t, ut::Location loc) {
1293}
1294
1295static inline void trx_start_if_not_started(trx_t *t, bool rw, ut::Location l) {
1297}
1298#endif /* UNIV_DEBUG */
1299
1300/* Transaction isolation levels (trx->isolation_level) */
1301#define TRX_ISO_READ_UNCOMMITTED trx_t::READ_UNCOMMITTED
1302#define TRX_ISO_READ_COMMITTED trx_t::READ_COMMITTED
1303#define TRX_ISO_REPEATABLE_READ trx_t::REPEATABLE_READ
1304#define TRX_ISO_SERIALIZABLE trx_t::SERIALIZABLE
1305
1306/**
1307Check if transaction was started. Note, that after the check
1308situation might have already been changed (and note that holding
1309the trx_sys->mutex does not prevent state transitions for read-only
1310transactions).
1311@param[in] trx Transaction whose state we need to check
1312@return true if transaction is in state started */
1313inline bool trx_was_started(const trx_t *trx) {
1314 const auto trx_state = trx->state.load(std::memory_order_relaxed);
1315 return trx_state != TRX_STATE_NOT_STARTED &&
1316 trx_state != TRX_STATE_FORCED_ROLLBACK;
1317}
1318
1319/**
1320Check if transaction is started.
1321@param[in] trx Transaction whose state we need to check
1322@return true if transaction is in state started */
1323inline bool trx_is_started(const trx_t *trx) {
1325 return trx_was_started(trx);
1326}
1327
1328/** Commit node states */
1330 COMMIT_NODE_SEND = 1, /*!< about to send a commit signal to
1331 the transaction */
1332 COMMIT_NODE_WAIT /*!< commit signal sent to the transaction,
1333 waiting for completion */
1335
1336/** Commit command node in a query graph */
1338 que_common_t common; /*!< node type: QUE_NODE_COMMIT */
1339 enum commit_node_state state; /*!< node execution state */
1340};
1341
1342#ifdef UNIV_DEBUG
1343
1344/** Test if trx->mutex is owned by the current thread. */
1345bool inline trx_mutex_own(const trx_t *trx) { return mutex_own(&trx->mutex); }
1346
1347/**
1348Verifies the invariants and records debug state related to latching rules.
1349Called during trx_mutex_enter before the actual mutex acquisition.
1350@param[in] trx The transaction for which trx_mutex_enter(trx) is
1351 called
1352@param[in] allow_another If false, then no further calls to trx_mutex_enter
1353 are allowed, until trx_mutex_exit().
1354 If true, then this must be the first trx acquisition
1355 and we will allow one more.
1356*/
1357void trx_before_mutex_enter(const trx_t *trx, bool allow_another);
1358
1359/**
1360Verifies the invariants and records debug state related to latching rules.
1361Called during trx_mutex_exit before the actual mutex release.
1362@param[in] trx The transaction for which trx_mutex_exit(trx) is called
1363*/
1364void trx_before_mutex_exit(const trx_t *trx);
1365#endif
1366
1367/**
1368Please do not use this low-level macro.
1369Use trx_mutex_enter(t) instead.
1370In rare cases where you need to take two trx->mutex-es, take the first one
1371using trx_mutex_enter_first_of_two(t1), and the second one with
1372trx_mutex(2)
1373*/
1374#define trx_mutex_enter_low(t, first_of_two) \
1375 do { \
1376 ut_ad(!trx_mutex_own(t)); \
1377 ut_d(trx_before_mutex_enter(t, first_of_two)); \
1378 mutex_enter(&t->mutex); \
1379 } while (0)
1380
1381/** Acquire the trx->mutex (and promise not to request any more). */
1382#define trx_mutex_enter(t) trx_mutex_enter_low(t, false)
1383
1384/** Acquire the trx->mutex (and indicate we might request one more). */
1385#define trx_mutex_enter_first_of_two(t) trx_mutex_enter_low(t, true)
1386
1387/** Release the trx->mutex. */
1388#define trx_mutex_exit(t) \
1389 do { \
1390 ut_ad(trx_mutex_own(t)); \
1391 ut_d(trx_before_mutex_exit(t)); \
1392 mutex_exit(&t->mutex); \
1393 } while (0)
1394
1395/** Track if a transaction is executing inside InnoDB code. It acts
1396like a gate between the Server and InnoDB. */
1398 public:
1399 /**
1400 @param[in,out] trx Transaction entering InnoDB via the handler
1401 @param[in] disable true if called from COMMIT/ROLLBACK method */
1402 TrxInInnoDB(trx_t *trx, bool disable = false) : m_trx(trx) {
1403 enter(trx, disable);
1404 }
1405
1406 /**
1407 Destructor */
1409
1410 /**
1411 @return true if the transaction has been marked for asynchronous
1412 rollback */
1413 bool is_aborted() const { return (is_aborted(m_trx)); }
1414
1415 /**
1416 @return true if the transaction can't be rolled back asynchronously */
1418 return ((m_trx->in_innodb & TRX_FORCE_ROLLBACK_DISABLE) > 0);
1419 }
1420
1421 /**
1422 @return true if the transaction has been marked for asynchronous
1423 rollback */
1424 static bool is_aborted(const trx_t *trx) {
1426
1427 const auto trx_state = trx->state.load(std::memory_order_relaxed);
1428
1429 if (trx_state == TRX_STATE_NOT_STARTED) {
1430 return (false);
1431 }
1432
1433 ut_ad(srv_read_only_mode || trx->in_depth > 0);
1434 ut_ad(srv_read_only_mode || trx->in_innodb > 0);
1435
1436 return (trx->abort || trx_state == TRX_STATE_FORCED_ROLLBACK);
1437 }
1438
1439 /**
1440 Start statement requested for transaction.
1441 @param[in, out] trx Transaction at the start of a SQL statement */
1442 static void begin_stmt(trx_t *trx) { enter(trx, false); }
1443
1444 /**
1445 Note an end statement for transaction
1446 @param[in, out] trx Transaction at end of a SQL statement */
1447 static void end_stmt(trx_t *trx) { exit(trx); }
1448
1449 /**
1450 @return true if the rollback is being initiated by the thread that
1451 marked the transaction for asynchronous rollback */
1452 static bool is_async_rollback(const trx_t *trx) {
1453 return trx->killed_by == std::this_thread::get_id();
1454 }
1455
1456 private:
1457 /** Note that we have crossed into InnoDB code.
1458 @param[in] trx transaction
1459 @param[in] disable true if called from COMMIT/ROLLBACK method */
1460 static void enter(trx_t *trx, bool disable) {
1461 if (srv_read_only_mode) {
1462 return;
1463 }
1464
1465 ut_ad(!is_async_rollback(trx));
1467
1468 /* If it hasn't already been marked for async rollback.
1469 and it will be committed/rolled back. */
1470 if (disable) {
1471 trx_mutex_enter(trx);
1472 if (!is_forced_rollback(trx) && is_started(trx) &&
1474 ut_ad(trx->killed_by == std::thread::id{});
1475
1476 /* This transaction has crossed the point of
1477 no return and cannot be rolled back
1478 asynchronously now. It must commit or rollback
1479 synchronously. */
1480
1482 }
1483 trx_mutex_exit(trx);
1484 }
1485
1486 /* Avoid excessive mutex acquire/release */
1487 ++trx->in_depth;
1488
1489 /* If trx->in_depth is greater than 1 then
1490 transaction is already in InnoDB. */
1491 if (trx->in_depth > 1) {
1492 return;
1493 }
1494
1495 trx_mutex_enter(trx);
1496
1497 wait(trx);
1498
1500
1501 ++trx->in_innodb;
1502
1503 trx_mutex_exit(trx);
1504 }
1505
1506 /**
1507 Note that we are exiting InnoDB code */
1508 static void exit(trx_t *trx) {
1509 if (srv_read_only_mode) {
1510 return;
1511 }
1512
1513 /* Avoid excessive mutex acquire/release */
1514
1515 ut_ad(trx->in_depth > 0);
1516
1517 --trx->in_depth;
1518
1519 if (trx->in_depth > 0) {
1520 return;
1521 }
1522
1523 trx_mutex_enter(trx);
1524
1526
1527 --trx->in_innodb;
1528
1529 trx_mutex_exit(trx);
1530 }
1531
1532 /*
1533 @return true if it is a forced rollback, asynchronously */
1534 static bool is_forced_rollback(const trx_t *trx) {
1535 ut_ad(trx_mutex_own(trx));
1536
1537 return ((trx->in_innodb & TRX_FORCE_ROLLBACK)) > 0;
1538 }
1539
1540 /**
1541 Wait for the asynchronous rollback to complete, if it is in progress */
1542 static void wait(const trx_t *trx) {
1543 ut_ad(trx_mutex_own(trx));
1544
1545 ulint loop_count = 0;
1546 /* start with optimistic sleep time - 20 micro seconds. */
1547 ulint sleep_time = 20;
1548
1549 while (is_forced_rollback(trx)) {
1550 /* Wait for the async rollback to complete */
1551
1552 trx_mutex_exit(trx);
1553
1554 loop_count++;
1555 /* If the wait is long, don't hog the cpu. */
1556 if (loop_count < 100) {
1557 /* 20 microseconds */
1558 sleep_time = 20;
1559 } else if (loop_count < 1000) {
1560 /* 1 millisecond */
1561 sleep_time = 1000;
1562 } else {
1563 /* 100 milliseconds */
1564 sleep_time = 100000;
1565 }
1566
1567 std::this_thread::sleep_for(std::chrono::microseconds(sleep_time));
1568
1569 trx_mutex_enter(trx);
1570 }
1571 }
1572
1573 private:
1574 /**
1575 @return true if transaction is started */
1576 static bool is_started(const trx_t *trx) {
1577 ut_ad(trx_mutex_own(trx));
1578
1579 return trx_is_started(trx);
1580 }
1581
1582 /**
1583 Transaction instance crossing the handler boundary from the Server. */
1585};
1586
1587/** Check if transaction is internal XA transaction
1588@param[in] trx transaction
1589@return true, iff internal XA transaction. */
1590bool trx_is_mysql_xa(const trx_t *trx);
1591
1592/** Update transaction binlog file name and position from session THD.
1593@param[in,out] trx current transaction. */
1595
1596/** Checks whether or not the transaction has been marked as prepared in TC.
1597@param[in] trx the transaction
1598@return true if the transaction is marked as prepared in TC, false otherwise. */
1599bool trx_is_prepared_in_tc(trx_t const *trx);
1600
1601/** Does the 2nd phase of an XA transaction prepare for MySQL.
1602@param[in,out] trx Transaction instance to finish prepare
1603@return DB_SUCCESS or error number */
1605
1606/** Human readable transaction state, for diagnostic purposes.
1607@return pointer to static string, nullptr if state is not valid */
1608inline const char *trx_state_string(trx_state_t state);
1609
1610#include "trx0trx.ic"
1611#endif /* !UNIV_HOTBACKUP */
1612
1613#endif
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:48
We use Flush_observer to track flushing of non-redo logged pages in bulk create index(btr0load....
Definition: buf0flu.h:270
static bool is_view_active(ReadView *view)
Definition: read0read.h:80
Read view lists the trx ids of those transactions for which a consistent read should not see the modi...
Definition: read0types.h:48
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
Track if a transaction is executing inside InnoDB code.
Definition: trx0trx.h:1397
static void begin_stmt(trx_t *trx)
Start statement requested for transaction.
Definition: trx0trx.h:1442
static void end_stmt(trx_t *trx)
Note an end statement for transaction.
Definition: trx0trx.h:1447
static void enter(trx_t *trx, bool disable)
Note that we have crossed into InnoDB code.
Definition: trx0trx.h:1460
static void wait(const trx_t *trx)
Wait for the asynchronous rollback to complete, if it is in progress.
Definition: trx0trx.h:1542
static bool is_aborted(const trx_t *trx)
Definition: trx0trx.h:1424
~TrxInInnoDB()
Destructor.
Definition: trx0trx.h:1408
static bool is_forced_rollback(const trx_t *trx)
Definition: trx0trx.h:1534
static bool is_async_rollback(const trx_t *trx)
Definition: trx0trx.h:1452
bool is_aborted() const
Definition: trx0trx.h:1413
static bool is_started(const trx_t *trx)
Definition: trx0trx.h:1576
static void exit(trx_t *trx)
Note that we are exiting InnoDB code.
Definition: trx0trx.h:1508
bool is_rollback_disabled() const
Definition: trx0trx.h:1417
trx_t * m_trx
Transaction instance crossing the handler boundary from the Server.
Definition: trx0trx.h:1584
TrxInInnoDB(trx_t *trx, bool disable=false)
Definition: trx0trx.h:1402
Class to maintain list of externally coordinated transactions and their current state at recovery.
Definition: handler.h:1259
Allocator that allows std::* containers to manage their memory through ut::malloc* and ut::free libra...
Definition: ut0new.h:2182
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
dberr_t
Definition: db0err.h:39
Data dictionary global types.
Full text search header file.
uint64_t doc_id_t
Document id type.
Definition: fts0fts.h:79
std::chrono::seconds thd_lock_wait_timeout(THD *thd)
Returns the lock wait timeout for the current connection.
Definition: ha_innodb.cc:1957
Prototypes for global functions in ha_innodb.cc that are called by InnoDB C code.
The transaction lock system global types.
uint64_t lsn_t
Type used for all log sequence number storage and arithmetic.
Definition: log0types.h:63
The memory management.
const std::string FILE("FILE")
Definition: os0file.h:89
Provides atomic access in shared-exclusive modes.
Definition: shared_spin_lock.h:79
Definition: gcs_xcom_synode.h:64
pid_type get_id()
Definition: process.h:48
Query graph global types.
Cursor read.
The server main program.
bool srv_read_only_mode
Set if InnoDB must operate in read-only mode.
Definition: srv0srv.cc:200
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Commit command node in a query graph.
Definition: trx0trx.h:1337
enum commit_node_state state
node execution state
Definition: trx0trx.h:1339
que_common_t common
node type: QUE_NODE_COMMIT
Definition: trx0trx.h:1338
Data structure for an index.
Definition: dict0mem.h:1046
Information about changes in a single transaction affecting the FTS system.
Definition: fts0fts.h:229
Definition: ut0vec.h:213
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:302
Mini-transaction handle and buffer.
Definition: mtr0mtr.h:177
Definition: que0types.h:51
Definition: que0que.h:301
Definition: que0que.h:242
Definition: usr0sess.h:54
Plain structure to store information about XA transaction id and a list of table names involved into ...
Definition: xa.h:290
Latching protocol for trx_lock_t::que_state.
Definition: trx0trx.h:401
std::atomic< bool > inherit_all
Used to indicate that every lock of this transaction placed on a record which is being purged should ...
Definition: trx0trx.h:558
trx_lock_t()=default
Default constructor.
std::atomic< ulint > n_rec_locks
Number of rec locks in this trx.
Definition: trx0trx.h:549
mem_heap_t * lock_heap
Memory heap for trx_locks.
Definition: trx0trx.h:524
bool start_stmt
The transaction called ha_innobase::start_stmt() to lock a table.
Definition: trx0trx.h:577
std::atomic< trx_t * > blocking_trx
If this transaction is waiting for a lock, then blocking_trx points to a transaction which holds a co...
Definition: trx0trx.h:439
trx_lock_list_t trx_locks
Locks requested by the transaction.
Definition: trx0trx.h:538
std::atomic< lock_t * > wait_lock
The lock request of this transaction is waiting for.
Definition: trx0trx.h:460
std::chrono::system_clock::time_point wait_started
Lock wait started at this time.
Definition: trx0trx.h:501
uint64_t trx_locks_version
Incremented each time a lock is added or removed from the trx->lock.trx_locks, so that the thread whi...
Definition: trx0trx.h:415
trx_que_t que_state
valid when trx->state == TRX_STATE_ACTIVE: TRX_QUE_RUNNING, TRX_QUE_LOCK_WAIT, ...
Definition: trx0trx.h:407
bool in_rollback
When a transaction is forced to rollback due to a deadlock check or by another high priority transact...
Definition: trx0trx.h:572
lock_pool_t table_pool
Pre-allocated table locks.
Definition: trx0trx.h:515
ulint rec_cached
Next free record lock in pool.
Definition: trx0trx.h:518
ulint n_active_thrs
number of active query threads
Definition: trx0trx.h:405
std::atomic< trx_schedule_weight_t > schedule_weight
Weight of the waiting transaction used for scheduling.
Definition: trx0trx.h:566
bool was_chosen_as_deadlock_victim
when the transaction decides to wait for a lock, it sets this to false; if another transaction choose...
Definition: trx0trx.h:490
ib_vector_t * autoinc_locks
AUTOINC locks held by this transaction.
Definition: trx0trx.h:544
uint32_t wait_lock_type
Stores the type of the most recent lock for which this trx had to wait.
Definition: trx0trx.h:488
lock_pool_t rec_pool
Pre-allocated record locks.
Definition: trx0trx.h:512
ulint table_cached
Next free table lock in pool.
Definition: trx0trx.h:521
que_thr_t * wait_thr
query thread belonging to this trx that is in QUE_THR_LOCK_WAIT state.
Definition: trx0trx.h:509
A savepoint set with SQL's "SAVEPOINT savepoint_id" command.
Definition: trx0roll.h:168
The rollback segment memory object.
Definition: trx0types.h:214
Rollback segments assigned to a transaction for undo logging.
Definition: trx0trx.h:657
trx_undo_ptr_t m_noredo
undo log ptr holding reference to a rollback segment that resides in temp tablespace used for undo lo...
Definition: trx0trx.h:666
trx_undo_ptr_t m_redo
undo log ptr holding reference to a rollback segment that resides in system/undo tablespace used for ...
Definition: trx0trx.h:661
Transaction savepoint.
Definition: trx0types.h:148
Definition: trx0trx.h:675
const char * op_info
English text describing the current operation, or an empty string.
Definition: trx0trx.h:851
bool releases_gap_locks_at_prepare() const
Definition: trx0trx.h:1109
space_id_t undo_rseg_space
space id where last undo record was written
Definition: trx0trx.h:1004
bool is_recovered
false: a normal transaction true: a recovered transaction
Definition: trx0trx.h:835
uint32_t dict_operation_lock_mode
0, RW_S_LATCH, or RW_X_LATCH: the latch mode trx currently holds on dict_operation_lock.
Definition: trx0trx.h:918
uint32_t n_mysql_tables_in_use
number of Innobase tables used in the processing of the current SQL statement in MySQL
Definition: trx0trx.h:945
char * detailed_error
detailed error message for last error, or empty.
Definition: trx0trx.h:1094
bool allow_semi_consistent() const
Definition: trx0trx.h:1126
ulint magic_n
Definition: trx0trx.h:1103
ulint n_autoinc_rows
no.
Definition: trx0trx.h:1022
bool must_flush_log_later
this flag is set to true in trx_commit() if flush_log_later was true, and there were modifications by...
Definition: trx0trx.h:886
bool flush_log_later
Definition: trx0trx.h:880
no_list
Required during view creation to check for the view limit for transactions that are committing.
Definition: trx0trx.h:816
trx_dict_op_t dict_operation
Definition: trx0trx.h:899
THD * mysql_thd
MySQL thread handle corresponding to this trx, or NULL.
Definition: trx0trx.h:932
XID * xid
X/Open XA transaction identification to identify a transaction branch.
Definition: trx0trx.h:1076
fts_trx_t * fts_trx
FTS information, or NULL if transaction hasn't modified tables with FTS indexes (yet).
Definition: trx0trx.h:1040
undo_no_t undo_no
next undo log record number to assign; since the undo log is private for a transaction,...
Definition: trx0trx.h:997
bool releases_non_matching_rows() const
Checks if this transaction releases locks on non matching records due to low isolation level.
Definition: trx0trx.h:1131
uint32_t will_lock
Will acquire some locks.
Definition: trx0trx.h:1035
ulint start_line
Track where it was started from.
Definition: trx0trx.h:1058
que_t * graph
query currently run in the session, or NULL if none; NOTE that the query belongs to the session,...
Definition: trx0trx.h:981
std::atomic< std::chrono::system_clock::time_point > start_time
Time the state last time became TRX_STATE_ACTIVE.
Definition: trx0trx.h:925
Flush_observer * flush_observer
flush observer
Definition: trx0trx.h:1096
TrxMutex mutex
Mutex protecting the fields state and lock (except some fields of lock, which are protected by lock_s...
Definition: trx0trx.h:706
doc_id_t fts_next_doc_id
Definition: trx0trx.h:1043
const char * mysql_log_file_name
if MySQL binlog is used, this field contains a pointer to the latest file name; this is NULL if binlo...
Definition: trx0trx.h:935
lint n_ref
Count of references, protected by trx_t::mutex.
Definition: trx0trx.h:1062
uint32_t in_innodb
if the thread is executing in the InnoDB context count > 0.
Definition: trx0trx.h:720
std::atomic_uint64_t version
Version of this instance.
Definition: trx0trx.h:1074
bool auto_commit
true if it is an autocommit
Definition: trx0trx.h:1034
bool is_dd_trx
True if the transaction is used for doing Non-locking Read-only Read Committed on DD tables.
Definition: trx0trx.h:1099
uint32_t flush_tables
if "covering" the FLUSH TABLES", count of tables being flushed.
Definition: trx0trx.h:1045
std::atomic< trx_state_t > state
State of the trx from the point of view of concurrency control and the valid state transitions.
Definition: trx0trx.h:800
isolation_level_t isolation_level
Current isolation level.
Definition: trx0trx.h:856
bool in_mysql_trx_list
true if in trx_sys->mysql_trx_list
Definition: trx0trx.h:962
bool api_auto_commit
automatic commit
Definition: trx0trx.h:1084
uint32_t in_depth
Track nested TrxInInnoDB count.
Definition: trx0trx.h:717
uint32_t n_tickets_to_enter_innodb
this can be > 0 only when declared_to_... is true; when we come to srv_conc_innodb_enter,...
Definition: trx0trx.h:913
bool skip_lock_inheritance
Definition: trx0trx.h:807
ulint pages_undone
number of undo log pages undone since the last undo log truncation
Definition: trx0trx.h:1019
bool ddl_must_flush
True if this trx involves dd table change, and must flush.
Definition: trx0trx.h:903
undo_no_t roll_limit
least undo number to undo during a partial rollback; 0 otherwise
Definition: trx0trx.h:1013
trx_rsegs_t rsegs
Definition: trx0trx.h:1012
bool is_registered
Definition: trx0trx.h:867
trx_t()=default
Default constructor.
uint64_t mysql_log_offset
if MySQL binlog is used, this field contains the end offset of the binlog entry
Definition: trx0trx.h:940
const char * start_file
Filename where it was started.
Definition: trx0trx.h:1059
lsn_t commit_lsn
lsn at the time of the commit
Definition: trx0trx.h:927
bool check_unique_secondary
normally true, but if the user wants to speed up inserts by suppressing unique key checks for seconda...
Definition: trx0trx.h:873
isolation_level_t
Definition: trx0trx.h:676
@ REPEATABLE_READ
this is the default; all consistent reads in the same trx read the same snapshot; full next-key locki...
Definition: trx0trx.h:694
@ READ_COMMITTED
somewhat Oracle-like isolation, except that in range UPDATE and DELETE we must block phantom rows wit...
Definition: trx0trx.h:689
@ SERIALIZABLE
all plain SELECTs are converted to LOCK IN SHARE MODE reads
Definition: trx0trx.h:698
@ READ_UNCOMMITTED
dirty read: non-locking SELECTs are performed so that we do not look at a possible earlier version of...
Definition: trx0trx.h:682
const dict_index_t * error_index
if the error number indicates a duplicate key error, a pointer to the problematic index is stored her...
Definition: trx0trx.h:974
trx_savept_t last_sql_stat_start
undo_no when the last sql statement was started: in case of an error, trx is rolled back down to this...
Definition: trx0trx.h:1007
bool is_read_uncommitted() const
Definition: trx0trx.h:1105
bool purge_sys_trx
This flag is set for trx_t objects used by the purge sys.
Definition: trx0trx.h:1092
trx_id_t id
transaction id
Definition: trx0trx.h:727
bool read_only
true if transaction is flagged as a READ-ONLY transaction.
Definition: trx0trx.h:1026
trx_savepoints
savepoints set with SAVEPOINT ..., oldest first
Definition: trx0trx.h:989
bool in_rw_trx_list
True iff in trx_sys->rw_trx_list.
Definition: trx0trx.h:955
bool api_trx
trx started by InnoDB API
Definition: trx0trx.h:1083
bool skip_gap_locks() const
Definition: trx0trx.h:1113
bool abort
if this flag is set then this transaction must abort when it can
Definition: trx0trx.h:723
bool in_truncate
Definition: trx0trx.h:905
bool declared_to_be_inside_innodb
this is true if we have declared this transaction in srv_conc_enter_innodb to be inside the InnoDB en...
Definition: trx0trx.h:908
std::atomic< std::thread::id > killed_by
The thread ID that wants to kill this transaction asynchronously.
Definition: trx0trx.h:837
bool check_foreigns
normally true, but if the user wants to suppress foreign key checks, (in table imports,...
Definition: trx0trx.h:858
bool persists_gtid
Transaction persists GTID.
Definition: trx0trx.h:1055
dberr_t error_state
DB_SUCCESS if no error, otherwise error number.
Definition: trx0trx.h:972
trx_list
list of transactions; protected by trx_sys->mutex.
Definition: trx0trx.h:813
trx_id_t no
transaction serialization number: max trx id shortly before the transaction is moved to COMMITTED_IN_...
Definition: trx0trx.h:729
ReadView * read_view
consistent read view used in the transaction, or NULL if not yet set
Definition: trx0trx.h:809
sess_t * sess
session of the trx, NULL if none
Definition: trx0trx.h:980
bool in_rollback
true when the transaction is executing a partial or full rollback
Definition: trx0trx.h:1016
bool read_write
if read and write operation
Definition: trx0trx.h:1085
UndoMutex undo_mutex
mutex protecting the fields in this section (down to undo_no_arr), EXCEPT last_sql_stat_start,...
Definition: trx0trx.h:991
bool has_search_latch
true if this trx has latched the search system latch in S-mode.
Definition: trx0trx.h:892
trx_lock_t lock
Information about the transaction locks and state.
Definition: trx0trx.h:822
trx_mod_tables_t mod_tables
List of tables that were modified by this transaction.
Definition: trx0trx.h:1079
bool ddl_operation
True if this trx involves dd table change.
Definition: trx0trx.h:901
ulint error_key_num
if the index creation fails to a duplicate key error, a mysql key number of that index is stored here
Definition: trx0trx.h:977
uint32_t mysql_n_tables_locked
how many tables the current SQL statement uses, except those in consistent read
Definition: trx0trx.h:948
mysql_trx_list
list of transactions created for MySQL; protected by trx_sys->mutex
Definition: trx0trx.h:959
The transaction handle.
Definition: trx0trx.h:635
bool is_update()
Definition: trx0trx.h:645
bool is_insert_only()
Definition: trx0trx.h:640
trx_undo_t * insert_undo
pointer to the insert undo log, or NULL if no inserts performed yet
Definition: trx0trx.h:650
trx_undo_t * update_undo
pointer to the update undo log, or NULL if no update performed yet
Definition: trx0trx.h:652
trx_rseg_t * rseg
rollback segment assigned to the transaction, or NULL if not assigned yet
Definition: trx0trx.h:647
bool is_empty()
Definition: trx0trx.h:637
Transaction undo log memory object; this is protected by the undo_mutex in the corresponding transact...
Definition: trx0undo.h:339
Definition: ut0core.h:36
const char * filename
Definition: ut0core.h:37
size_t line
Definition: ut0core.h:38
struct xid_t is binary compatible with the XID structure as in the X/Open CAE Specification,...
Definition: xa.h:83
double seconds()
Definition: task.cc:310
void trx_start_if_not_started_low(trx_t *trx, bool read_write)
Starts the transaction if it is not yet started.
Definition: trx0trx.cc:3366
bool trx_assert_started(const trx_t *trx)
Asserts that a transaction has been started.
Definition: trx0trx.cc:2702
static ReadView * trx_get_read_view(trx_t *trx)
void trx_set_flush_observer(trx_t *trx, Flush_observer *observer)
Set flush observer for the transaction.
Definition: trx0trx.cc:132
sess_t * trx_dummy_sess
Dummy session used currently in MySQL interface.
Definition: trx0trx.cc:94
bool trx_is_interrupted(const trx_t *trx)
Determines if the currently running transaction has been interrupted.
Definition: ha_innodb.cc:3125
static void assert_trx_is_inactive(const trx_t *t)
Check if transaction is in-active so that it can be freed and put back to transaction pool.
Definition: trx0trx.h:1194
bool trx_is_started(const trx_t *trx)
Check if transaction is started.
Definition: trx0trx.h:1323
static void trx_start_internal(trx_t *t, ut::Location loc)
Definition: trx0trx.h:1270
bool trx_was_started(const trx_t *trx)
Check if transaction was started.
Definition: trx0trx.h:1313
void trx_free_prepared_or_active_recovered(trx_t *trx)
At shutdown, frees a transaction object that represents either:
Definition: trx0trx.cc:601
void trx_free_for_background(trx_t *trx)
Free a transaction that was allocated by background or user threads.
Definition: trx0trx.cc:595
trx_t * trx_allocate_for_background(void)
Creates a transaction object for background operations by the master thread.
Definition: trx0trx.cc:524
que_thr_t * trx_commit_step(que_thr_t *thr)
Performs an execution step for a commit type node in a query graph.
Definition: trx0trx.cc:2397
static bool trx_is_high_priority(const trx_t *trx)
void trx_before_mutex_exit(const trx_t *trx)
Verifies the invariants and records debug state related to latching rules.
Definition: trx0trx.cc:2885
std::set< dict_table_t *, std::less< dict_table_t * >, ut::allocator< dict_table_t * > > trx_mod_tables_t
Type used to store the list of tables that are modified by a given transaction.
Definition: trx0trx.h:586
void trx_pool_init()
Create the trx_t pool.
Definition: trx0trx.cc:432
void trx_free_for_mysql(trx_t *trx)
Free a transaction object for MySQL.
Definition: trx0trx.cc:688
static bool trx_is_ac_nl_ro(const trx_t *t)
Determine if the transaction is a non-locking autocommit select with an explicit check for the read-o...
Definition: trx0trx.h:1228
void trx_kill_blocking(trx_t *trx)
If this is a high priority transaction, kill all transactions that are blocking this transaction from...
Definition: trx0trx.cc:3465
static void trx_start_if_not_started_xa(trx_t *t, bool rw, ut::Location loc)
Definition: trx0trx.h:1257
void trx_disconnect_plain(trx_t *trx)
Disconnect a transaction from MySQL.
Definition: trx0trx.cc:676
bool trx_weight_ge(const trx_t *a, const trx_t *b)
Compares the "weight" (or size) of two transactions.
Definition: trx0trx.cc:2899
static enum trx_dict_op_t trx_get_dict_operation(const trx_t *trx)
Determine if a transaction is a dictionary operation.
void trx_resurrect_locks(bool all)
Resurrect table locks for resurrected transactions.
Definition: trx0trx.cc:776
void trx_free_resurrected(trx_t *trx)
Free and initialize a transaction object instantiated during recovery.
Definition: trx0trx.cc:585
void trx_clear_resurrected_table_ids()
Clear all resurrected table IDs.
Definition: trx0trx.cc:830
void trx_print_latched(FILE *f, const trx_t *trx, ulint max_query_len)
Prints info about a transaction.
Definition: trx0trx.cc:2662
void trx_sys_update_binlog_position(trx_t *trx)
Update transaction binlog file name and position from session THD.
Definition: trx0trx.cc:3624
static void check_trx_state(const trx_t *t)
Check transaction state.
Definition: trx0trx.h:1156
int trx_recover_tc_for_mysql(Xa_state_list &xa_list)
Find prepared transactions that are marked as prepared in TC, for recovery purposes.
Definition: trx0trx.cc:3252
static void trx_start_if_not_started(trx_t *t, bool rw, ut::Location l)
Definition: trx0trx.h:1264
void trx_mark_sql_stat_end(trx_t *trx)
Marks the latest SQL statement ended.
Definition: trx0trx.cc:2516
const char * trx_state_string(trx_state_t state)
Human readable transaction state, for diagnostic purposes.
Definition: trx0trx.ic:310
void trx_commit(trx_t *trx)
Commits a transaction.
Definition: trx0trx.cc:2257
std::vector< std::pair< trx_id_t, table_id_t > > to_rollback_trx_tables
Definition: trx0trx.cc:91
void trx_before_mutex_enter(const trx_t *trx, bool allow_another)
Verifies the invariants and records debug state related to latching rules.
Definition: trx0trx.cc:2858
static uint64_t TRX_WEIGHT(const trx_t *t)
Calculates the "weight" of a transaction.
Definition: trx0trx.h:1252
void trx_set_rw_mode(trx_t *trx)
Set the transaction as a read-write transaction if it is not already tagged as such.
Definition: trx0trx.cc:3422
std::vector< ib_lock_t *, ut::allocator< ib_lock_t * > > lock_pool_t
Definition: trx0trx.h:377
static void trx_reference(trx_t *trx)
Increase the reference count.
static bool trx_is_referenced(const trx_t *t)
Check if the transaction is being referenced.
Definition: trx0trx.h:1246
bool trx_can_be_handled_by_current_thread_or_is_hp_victim(const trx_t *trx)
Determines if trx can be handled by current thread, which is when trx->mysql_thd is nullptr (a "backg...
Definition: trx0trx.cc:2694
ReadView * trx_assign_read_view(trx_t *trx)
Assigns a read view for a consistent read query.
Definition: trx0trx.cc:2319
void trx_assign_rseg_temp(trx_t *trx)
Assign a temp-tablespace bound rollback-segment to a transaction.
Definition: trx0trx.cc:1283
void trx_set_detailed_error_from_file(trx_t *trx, FILE *file)
Set detailed error message for the transaction from a file.
Definition: trx0trx.cc:145
commit_node_t * trx_commit_node_create(mem_heap_t *heap)
Creates a commit command node struct.
Definition: trx0trx.cc:2383
bool trx_can_be_handled_by_current_thread(const trx_t *trx)
Determines if trx can be handled by current thread, which is when trx->mysql_thd is nullptr (a "backg...
Definition: trx0trx.cc:2682
static const char * trx_get_que_state_str(const trx_t *trx)
Retrieves transaction's que state in a human readable string.
bool trx_is_prepared_in_tc(trx_t const *trx)
Checks whether or not the transaction has been marked as prepared in TC.
Definition: trx0trx.cc:3638
static bool trx_state_eq(const trx_t *trx, trx_state_t state)
Determines if a transaction is in the given state.
#define trx_mutex_exit(t)
Release the trx->mutex.
Definition: trx0trx.h:1388
void trx_start_internal_read_only_low(trx_t *trx)
Starts a read-only transaction for internal processing.
Definition: trx0trx.cc:3405
static trx_id_t trx_get_id_for_print(const trx_t *trx)
Retreieves the transaction ID.
constexpr uint32_t TRX_QUE_STATE_STR_MAX_LEN
Definition: trx0trx.h:318
static void trx_set_dict_operation(trx_t *trx, enum trx_dict_op_t op)
Flag a transaction a dictionary operation.
void trx_commit_low(trx_t *trx, mtr_t *mtr)
Commits a transaction and a mini-transaction.
Definition: trx0trx.cc:2165
static void trx_release_reference(trx_t *trx)
Release the transaction.
bool trx_is_mysql_xa(const trx_t *trx)
Check if transaction is internal XA transaction.
Definition: trx0trx.cc:2982
static bool trx_is_rseg_updated(const trx_t *trx)
Check if redo/noredo rseg is modified for insert/update.
static const dict_index_t * trx_get_error_index(const trx_t *trx)
Retrieves the error_info field from a trx.
int trx_recover_for_mysql(XA_recover_txn *txn_list, ulint len, MEM_ROOT *mem_root)
This function is used to find number of prepared transactions and their transaction objects for a rec...
Definition: trx0trx.cc:3196
trx_t * trx_get_trx_by_xid(const XID *xid)
This function is used to find one X/Open XA distributed transaction which is in the prepared state.
Definition: trx0trx.cc:3313
#define trx_mutex_enter(t)
Acquire the trx->mutex (and promise not to request any more).
Definition: trx0trx.h:1382
static bool trx_is_autocommit_non_locking(const trx_t *t)
Determine if the transaction is a non-locking autocommit select (implied read-only).
Definition: trx0trx.h:1151
void trx_pool_close()
Destroy the trx_t pool.
Definition: trx0trx.cc:440
static const trx_t * trx_arbitrate(const trx_t *requestor, const trx_t *holder)
bool trx_is_strict(trx_t *trx)
Determines if the currently running transaction is in strict mode.
Definition: ha_innodb.cc:3132
void trx_print(FILE *f, const trx_t *trx, ulint max_query_len)
Prints info about a transaction.
Definition: trx0trx.cc:2673
static void assert_trx_in_rw_list(const trx_t *t)
Assert that the transaction is in the trx_sys_t::rw_trx_list.
Definition: trx0trx.h:1173
void trx_start_internal_low(trx_t *trx)
Starts a transaction for internal processing.
Definition: trx0trx.cc:3391
dberr_t trx_set_prepared_in_tc_for_mysql(trx_t *trx)
Does the 2nd phase of an XA transaction prepare for MySQL.
Definition: trx0trx.cc:3653
trx_t * trx_allocate_for_mysql(void)
Creates a transaction object for MySQL.
Definition: trx0trx.cc:536
void trx_lists_init_at_db_start(void)
Creates trx objects for transactions and initializes the trx list of trx_sys at database start.
Definition: trx0trx.cc:1085
void trx_commit_complete_for_mysql(trx_t *trx)
If required, flushes the log to disk if we called trx_commit_for_mysql() with trx->flush_log_later ==...
Definition: trx0trx.cc:2498
trx_rseg_type_t
Definition: trx0trx.h:669
@ TRX_RSEG_TYPE_NONE
void rollback segment type.
Definition: trx0trx.h:670
@ TRX_RSEG_TYPE_NOREDO
non-redo rollback segment.
Definition: trx0trx.h:672
@ TRX_RSEG_TYPE_REDO
redo rollback segment.
Definition: trx0trx.h:671
void trx_disconnect_prepared(trx_t *trx)
Disconnect a prepared transaction from MySQL.
Definition: trx0trx.cc:682
static void assert_trx_nonlocking_or_in_list(const trx_t *t)
Assert that an autocommit non-locking select cannot be in the rw_trx_list and that it is a read-only ...
Definition: trx0trx.h:1203
static void assert_trx_is_free(const trx_t *t)
Check if transaction is free so that it can be re-initialized.
Definition: trx0trx.h:1181
commit_node_state
Commit node states.
Definition: trx0trx.h:1329
@ COMMIT_NODE_SEND
about to send a commit signal to the transaction
Definition: trx0trx.h:1330
@ COMMIT_NODE_WAIT
commit signal sent to the transaction, waiting for completion
Definition: trx0trx.h:1332
void trx_commit_or_rollback_prepare(trx_t *trx)
Prepares a transaction for commit/rollback.
Definition: trx0trx.cc:2336
static void trx_start_internal_read_only(trx_t *t, ut::Location loc)
Definition: trx0trx.h:1276
static std::chrono::seconds trx_lock_wait_timeout_get(const trx_t *t)
Transactions that aren't started by the MySQL server don't set the trx_t::mysql_thd field.
Definition: trx0trx.h:1142
void trx_set_detailed_error(trx_t *trx, const char *msg)
Set detailed error message for the transaction.
Definition: trx0trx.cc:139
void trx_cleanup_at_db_startup(trx_t *trx)
Cleans up a transaction at database startup.
Definition: trx0trx.cc:2282
dberr_t trx_commit_for_mysql(trx_t *trx)
Does the transaction commit for MySQL.
Definition: trx0trx.cc:2443
dberr_t trx_prepare_for_mysql(trx_t *trx)
Does the transaction prepare for MySQL.
Definition: trx0trx.cc:3118
void trx_print_low(FILE *f, const trx_t *trx, ulint max_query_len, ulint n_rec_locks, ulint n_trx_locks, ulint heap_size)
Prints info about a transaction.
Definition: trx0trx.cc:2548
bool trx_mutex_own(const trx_t *trx)
Test if trx->mutex is owned by the current thread.
Definition: trx0trx.h:1345
void trx_start_if_not_started_xa_low(trx_t *trx, bool read_write)
Starts the transaction if it is not yet started.
Definition: trx0trx.cc:3334
The transaction.
Transaction system global type definitions.
static const uint32_t TRX_FORCE_ROLLBACK_MASK
For masking out the above flags.
Definition: trx0types.h:68
ib_mutex_t TrxMutex
Definition: trx0types.h:171
trx_state_t
Transaction states (trx_t::state)
Definition: trx0types.h:80
@ TRX_STATE_FORCED_ROLLBACK
Same as not started but with additional semantics that it was rolled back asynchronously the last tim...
Definition: trx0types.h:86
@ TRX_STATE_ACTIVE
Definition: trx0types.h:88
@ TRX_STATE_COMMITTED_IN_MEMORY
Definition: trx0types.h:93
@ TRX_STATE_NOT_STARTED
Definition: trx0types.h:82
@ TRX_STATE_PREPARED
Support for 2PC/XA.
Definition: trx0types.h:91
trx_que_t
Transaction execution states when trx->state == TRX_STATE_ACTIVE.
Definition: trx0types.h:71
ib_id_t undo_no_t
Undo number.
Definition: trx0types.h:142
static const uint32_t TRX_FORCE_ROLLBACK
Mark the transaction for forced rollback.
Definition: trx0types.h:65
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:138
static const uint32_t TRX_FORCE_ROLLBACK_DISABLE
If this flag is set then the transaction cannot be rolled back asynchronously.
Definition: trx0types.h:62
ib_mutex_t UndoMutex
Definition: trx0types.h:172
trx_dict_op_t
Type of data dictionary operation.
Definition: trx0types.h:97
@ TRX_DICT_OP_NONE
The transaction is not modifying the data dictionary.
Definition: trx0types.h:99
unsigned long int ulint
Definition: univ.i:406
long int lint
Definition: univ.i:407
Users and sessions global types.
#define ut_error
Abort execution.
Definition: ut0dbg.h:101
#define ut_ad(EXPR)
Debug assertion.
Definition: ut0dbg.h:105
#define ut_o(EXPR)
Opposite of ut_d().
Definition: ut0dbg.h:109
#define ut_d(EXPR)
Debug statement.
Definition: ut0dbg.h:107
#define UT_LIST_GET_LEN(BASE)
Alternative macro to get the number of nodes in a two-way list, i.e., its length.
Definition: ut0lst.h:441
#define UT_LIST_BASE_NODE_T_EXTERN(t, m)
A variant of UT_LIST_BASE_NODE_T to be used in rare cases where the full definition of t is not yet i...
Definition: ut0lst.h:279
#define UT_LIST_NODE_T(t)
Macro used for legacy reasons.
Definition: ut0lst.h:64
#define mutex_own(M)
Checks that the current thread owns the mutex.
Definition: ut0mutex.h:165
Dynamic memory allocation routines and custom allocators specifically crafted to support memory instr...
A vector of pointers to data items.
unsigned long id[MAX_DEAD]
Definition: xcom_base.cc:510
static int all(site_def const *s, node_no node)
Definition: xcom_transport.cc:885