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