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