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