Debian Codegraph @

storage/innobase/include/trx0types.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/trx0types.h
29 Transaction system global type definitions
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef trx0types_h
35 #define trx0types_h
36
37 #include "page0size.h"
38 #include "sync0rw.h"
39 #include "ut0byte.h"
40 #include "ut0mutex.h"
41 #include "ut0new.h"
42
43 #include <atomic>
44 #include <queue>
45 #include <set>
46 #include <vector>
47
48 /** printf(3) format used for printing DB_TRX_ID and other system fields */
49 #define TRX_ID_FMT IB_ID_FMT
50
51 /** Space id of the transaction system page (the system tablespace) */
52 static const space_id_t TRX_SYS_SPACE = 0;
53
54 /** Page number of the transaction system page */
55 constexpr uint32_t TRX_SYS_PAGE_NO = FSP_TRX_SYS_PAGE_NO;
56
57 /** Random value to check for corruption of trx_t */
58 static const ulint TRX_MAGIC_N = 91118598;
59
60 /** If this flag is set then the transaction cannot be rolled back
61 asynchronously. */
62 static const uint32_t TRX_FORCE_ROLLBACK_DISABLE = 1 << 29;
63
64 /** Mark the transaction for forced rollback */
65 static const uint32_t TRX_FORCE_ROLLBACK = 1U << 31;
66
67 /** For masking out the above flags */
68 static const uint32_t TRX_FORCE_ROLLBACK_MASK = 0x1FFFFFFF;
69
70 /** Transaction execution states when trx->state == TRX_STATE_ACTIVE */
71 enum trx_que_t {
72 TRX_QUE_RUNNING, /*!< transaction is running */
73 TRX_QUE_LOCK_WAIT, /*!< transaction is waiting for
74 a lock */
75 TRX_QUE_ROLLING_BACK, /*!< transaction is rolling back */
76 TRX_QUE_COMMITTING /*!< transaction is committing */
77 };
78
79 /** Transaction states (trx_t::state) */
80 enum trx_state_t {
81
82 TRX_STATE_NOT_STARTED,
83
84 /** Same as not started but with additional semantics that it
85 was rolled back asynchronously the last time it was active. */
86 TRX_STATE_FORCED_ROLLBACK,
87
88 TRX_STATE_ACTIVE,
89
90 /** Support for 2PC/XA */
91 TRX_STATE_PREPARED,
92
93 TRX_STATE_COMMITTED_IN_MEMORY
94 };
95
96 /** Type of data dictionary operation */
97 enum trx_dict_op_t {
98 /** The transaction is not modifying the data dictionary. */
99 TRX_DICT_OP_NONE = 0,
100 /** The transaction is creating a table or an index, or
101 dropping a table. The table must be dropped in crash
102 recovery. This and TRX_DICT_OP_NONE are the only possible
103 operation modes in crash recovery. */
104 TRX_DICT_OP_TABLE = 1,
105 /** The transaction is creating or dropping an index in an
106 existing table. In crash recovery, the data dictionary
107 must be locked, but the table must not be dropped. */
108 TRX_DICT_OP_INDEX = 2
109 };
110
111 /** Memory objects */
112 /** @{ */
113 /** Transaction */
114 struct trx_t;
115 /** The locks and state of an active transaction */
116 struct trx_lock_t;
117 /** Transaction system */
118 struct trx_sys_t;
119 /** Signal */
120 struct trx_sig_t;
121 /** Rollback segment */
122 struct trx_rseg_t;
123 /** Transaction undo log */
124 struct trx_undo_t;
125 /** The control structure used in the purge operation */
126 struct trx_purge_t;
127 /** Rollback command node in a query graph */
128 struct roll_node_t;
129 /** Commit command node in a query graph */
130 struct commit_node_t;
131 /** SAVEPOINT command node in a query graph */
132 struct trx_named_savept_t;
133 /** @} */
134
135 /** Row identifier (DB_ROW_ID, DATA_ROW_ID) */
136 typedef ib_id_t row_id_t;
137 /** Transaction identifier (DB_TRX_ID, DATA_TRX_ID) */
138 typedef ib_id_t trx_id_t;
139 /** Rollback pointer (DB_ROLL_PTR, DATA_ROLL_PTR) */
140 typedef ib_id_t roll_ptr_t;
141 /** Undo number */
142 typedef ib_id_t undo_no_t;
143
144 /** Maximum transaction identifier */
145 constexpr trx_id_t TRX_ID_MAX = IB_ID_MAX;
146
147 /** Transaction savepoint */
148 struct trx_savept_t {
149 undo_no_t least_undo_no; /*!< least undo number to undo */
150 };
151
152 /** File objects */
153 /** @{ */
154 /** Transaction system header */
155 typedef byte trx_sysf_t;
156 /** Rollback segment array header */
157 typedef byte trx_rsegsf_t;
158 /** Rollback segment header */
159 typedef byte trx_rsegf_t;
160 /** Undo segment header */
161 typedef byte trx_usegf_t;
162 /** Undo log header */
163 typedef byte trx_ulogf_t;
164 /** Undo log page header */
165 typedef byte trx_upagef_t;
166 /** Undo log record */
167 typedef byte trx_undo_rec_t;
168 /** @} */
169
170 typedef ib_mutex_t RsegMutex;
171 typedef ib_mutex_t TrxMutex;
172 typedef ib_mutex_t UndoMutex;
173 typedef ib_mutex_t PQMutex;
174 typedef ib_mutex_t TrxSysMutex;
175
176 /** Used to identify trx uniquely over time */
177 struct trx_guid_t {
178 /** The immutable id of trx_t object - if you have a pointer to trx_t then we
179 guarantee that immutable id of it will not change over time. Also there are
180 never two trx_t objects at the same time with same immutable id. However it
181 may happen that two different transactions that do not occur at the same time
182 reuse the same trx_t and thus have same immutable id. Use m_version to detect
183 this situation. */
184 uint64_t m_immutable_id{};
185
186 /** As trx_t objects and thus immutable ids can be reused we need also trx's
187 version, which is incremented each time trx_t object gets reused. */
188 uint64_t m_version{};
189
190 /** Initializes trx_guid_t object to a value which doesn't match any real
191 transaction. */
192 trx_guid_t() = default;
193
194 /** Initializes trx_guid_t with data uniquely identifying the transaction
195 represented by trx_t object.
196 @param[in] trx the object representing the transaction */
197 trx_guid_t(const trx_t &trx);
198
199 /** Checks if two guids represent the same transaction:
200 they refer to the same trx_t struct and it was not reused meanwhile.
201 @param[in] rhs another guid to compare against
202 @return true iff the two guids are equal and thus represent same transaction*/
203 bool operator==(const trx_guid_t &rhs) const {
204 return m_immutable_id == rhs.m_immutable_id && m_version == rhs.m_version;
205 }
206
207 /** Checks if the instance is non-empty, i.e. was not default-constructed,
208 but rather initialized to correspond to a real trx_t.
209 @return true iff this guid was initialized to match a real transaction */
210 operator bool() const { return m_immutable_id != 0; }
211 };
212
213 /** The rollback segment memory object */
214 struct trx_rseg_t {
215 #ifdef UNIV_DEBUG
216 /** Validate the curr_size member by re-calculating it.
217 @param[in] take_mutex take the rseg->mutex. default is true.
218 @return true if valid, false otherwise. */
219 bool validate_curr_size(bool take_mutex = true);
220 #endif /* UNIV_DEBUG */
221
222 /** Enter the rseg->mutex. */
223 void latch() {
224 mutex_enter(&mutex);
225 ut_ad(validate_curr_size(false));
226 }
227
228 /** Exit the rseg->mutex. */
229 void unlatch() {
230 ut_ad(validate_curr_size(false));
231 mutex_exit(&mutex);
232 }
233
234 /** Decrement the current size of the rollback segment by the given number
235 of pages.
236 @param[in] npages number of pages to reduce in size. */
237 void decr_curr_size(page_no_t npages = 1) {
238 ut_ad(curr_size >= npages);
239 curr_size -= npages;
240 }
241
242 /** Increment the current size of the rollback segment by the given number
243 of pages. */
244 void incr_curr_size() { ++curr_size; }
245
246 /* Get the current size of the rollback segment in pages.
247 @return current size of the rollback segment in pages. */
248 page_no_t get_curr_size() const { return (curr_size); }
249
250 /* Set the current size of the rollback segment in pages.
251 @param[in] npages new value for the current size. */
252 void set_curr_size(page_no_t npages) { curr_size = npages; }
253
254 /*--------------------------------------------------------*/
255 /** rollback segment id == the index of its slot in the trx
256 system file copy */
257 size_t id{};
258
259 /** mutex protecting the fields in this struct except id,space,page_no
260 which are constant */
261 RsegMutex mutex;
262
263 /** space ID where the rollback segment header is placed */
264 space_id_t space_id{};
265
266 /** page number of the rollback segment header */
267 page_no_t page_no{};
268
269 /** page size of the relevant tablespace */
270 page_size_t page_size;
271
272 /** maximum allowed size in pages */
273 page_no_t max_size{};
274
275 private:
276 /** current size in pages */
277 page_no_t curr_size{};
278
279 public:
280 using Undo_list = UT_LIST_BASE_NODE_T_EXTERN(trx_undo_t, undo_list);
281 /*--------------------------------------------------------*/
282 /* Fields for update undo logs */
283 /** List of update undo logs */
284 Undo_list update_undo_list;
285
286 /** List of update undo log segments cached for fast reuse */
287 Undo_list update_undo_cached;
288
289 /*--------------------------------------------------------*/
290 /* Fields for insert undo logs */
291 /** List of insert undo logs */
292 Undo_list insert_undo_list;
293
294 /** List of insert undo log segments cached for fast reuse */
295 Undo_list insert_undo_cached;
296
297 /*--------------------------------------------------------*/
298
299 /** Page number of the last not yet purged log header in the history
300 list; FIL_NULL if all list purged */
301 page_no_t last_page_no{};
302
303 /** Byte offset of the last not yet purged log header */
304 size_t last_offset{};
305
306 /** Transaction number of the last not yet purged log */
307 trx_id_t last_trx_no;
308
309 /** true if the last not yet purged log needs purging */
310 bool last_del_marks{};
311
312 /** Reference counter to track rseg allocated transactions. */
313 std::atomic<size_t> trx_ref_count{};
314
315 std::ostream &print(std::ostream &out) const {
316 out << "[trx_rseg_t: this=" << (void *)this << ", id=" << id
317 << ", space_id=" << space_id << ", page_no=" << page_no
318 << ", curr_size=" << curr_size << "]";
319 return (out);
320 }
321 };
322
323 inline std::ostream &operator<<(std::ostream &out, const trx_rseg_t &rseg) {
324 return (rseg.print(out));
325 }
326
327 using Rsegs_Vector = std::vector<trx_rseg_t *, ut::allocator<trx_rseg_t *>>;
328 using Rseg_Iterator = Rsegs_Vector::iterator;
329
330 /** This is a wrapper for a std::vector of trx_rseg_t object pointers. */
331 class Rsegs {
332 public:
333 /** Default constructor */
334 Rsegs() : m_rsegs(), m_latch(), m_state(INIT) {
335 #ifndef UNIV_HOTBACKUP
336 init();
337 #endif /* !UNIV_HOTBACKUP */
338 }
339
340 ~Rsegs() {
341 #ifndef UNIV_HOTBACKUP
342 deinit();
343 #endif /* !UNIV_HOTBACKUP */
344 }
345
346 /** Initialize */
347 void init();
348
349 /** De-initialize */
350 void deinit();
351
352 /** Clear the vector of cached rollback segments leaving the
353 reserved space allocated. */
354 void clear();
355
356 /** Add rollback segment.
357 @param[in] rseg rollback segment to add. */
358 void push_back(trx_rseg_t *rseg) { m_rsegs.push_back(rseg); }
359
360 /** Number of registered rsegs.
361 @return size of rseg list. */
362 ulint size() { return (m_rsegs.size()); }
363
364 /** beginning iterator
365 @return an iterator to the first element */
366 Rseg_Iterator begin() { return (m_rsegs.begin()); }
367
368 /** ending iterator
369 @return an iterator to the end */
370 Rseg_Iterator end() { return (m_rsegs.end()); }
371
372 /** Find the rseg at the given slot in this vector.
373 @param[in] slot a slot within the vector.
374 @return an iterator to the end */
375 trx_rseg_t *at(ulint slot) { return (m_rsegs.at(slot)); }
376
377 /** Find an rseg in the std::vector that uses the rseg_id given.
378 @param[in] rseg_id A slot in a durable array such as
379 the TRX_SYS page or RSEG_ARRAY page.
380 @return a pointer to an trx_rseg_t that uses the rseg_id. */
381 trx_rseg_t *find(ulint rseg_id);
382
383 /** Sort the vector on trx_rseg_t::id */
384 void sort() {
385 if (m_rsegs.empty()) {
386 return;
387 }
388
389 std::sort(
390 m_rsegs.begin(), m_rsegs.end(),
391 [](trx_rseg_t *lhs, trx_rseg_t *rhs) { return (rhs->id > lhs->id); });
392 }
393
394 /** Acquire the shared lock on m_rsegs. */
395 void s_lock() { rw_lock_s_lock(m_latch, UT_LOCATION_HERE); }
396
397 /** Release the shared lock on m_rsegs. */
398 void s_unlock() { rw_lock_s_unlock(m_latch); }
399
400 /** Acquire the exclusive lock on m_rsegs. */
401 void x_lock() { rw_lock_x_lock(m_latch, UT_LOCATION_HERE); }
402
403 /** Release the exclusive lock on m_rsegs. */
404 void x_unlock() { rw_lock_x_unlock(m_latch); }
405
406 /** Return whether the undo tablespace is active.
407 @return true if active */
408 bool is_active() { return (m_state == ACTIVE); }
409
410 /** Return whether the undo tablespace is inactive due to
411 implicit selection by the purge thread.
412 @return true if marked for truncation by the purge thread */
413 bool is_inactive_implicit() { return (m_state == INACTIVE_IMPLICIT); }
414
415 /** Return whether the undo tablespace was made inactive by
416 ALTER TABLESPACE.
417 @return true if altered */
418 bool is_inactive_explicit() { return (m_state == INACTIVE_EXPLICIT); }
419
420 /** Return whether the undo tablespace is empty and ready
421 to be dropped.
422 @return true if empty */
423 bool is_empty() { return (m_state == EMPTY); }
424
425 /** Return whether the undo tablespace is being initialized.
426 @return true if empty */
427 bool is_init() { return (m_state == INIT); }
428
429 /** Set the state of the rollback segments in this undo tablespace
430 to ACTIVE for use by new transactions. */
431 void set_active() { m_state = ACTIVE; }
432
433 /** Set the state of the rollback segments in this undo
434 tablespace to inactive_implicit. This means that it will be
435 truncated and then made active again by the purge thread.
436 It will not be used for new transactions until it becomes
437 active again. */
438 void set_inactive_implicit() {
439 ut_ad(m_state == ACTIVE || m_state == INACTIVE_EXPLICIT);
440 m_state = INACTIVE_IMPLICIT;
441 }
442
443 /** Make the undo tablespace inactive so that it will not be
444 used for new transactions. The purge thread will clear out
445 all the undo logs, truncate it, and then mark it empty. */
446 void set_inactive_explicit() { m_state = INACTIVE_EXPLICIT; }
447
448 /** Set the state of the undo tablespace to empty so that it
449 can be dropped. */
450 void set_empty() {
451 ut_ad(m_state == INACTIVE_EXPLICIT || m_state == ACTIVE ||
452 m_state == INIT || m_state == EMPTY);
453 m_state = EMPTY;
454 }
455
456 /** std::vector of rollback segments */
457 Rsegs_Vector m_rsegs;
458
459 private:
460 /** RW lock to protect m_rsegs vector, m_active, and each
461 trx_rseg_t::trx_ref_count within it.
462 m_rsegs: x for adding elements, s for scanning, size etc.
463 m_active: x for modification, s for read
464 each trx_rseg_t::trx_ref_count within m_rsegs
465 s and atomic increment for modification, x for read */
466 rw_lock_t *m_latch;
467
468 /* The four states of an undo tablespace.
469 INIT: The initial state of an undo space that is being created or opened.
470 ACTIVE: The rollback segments in this tablespace can be allocated to new
471 transactions. The undo tablespace is ready for undo logs.
472 INACTIVE_IMPLICIT: These rollback segments are no longer being used by new
473 transactions. They are 'inactive'. The truncate process
474 is happening. This undo tablespace was selected by the
475 purge thread implicitly. When the truncation process
476 is complete, the next state is ACTIVE.
477 INACTIVE_EXPLICIT: These rollback segments are no longer being used by new
478 transactions. They are 'inactive'. The truncate process
479 is happening. This undo tablespace was selected by the
480 an ALTER UNDO TABLESPACE SET INACTIVE command. When the
481 truncation process is complete, the next state is EMPTY.
482 EMPTY: The undo tablespace has been truncated but is no longer
483 active. It is ready to be either dropped or set active
484 explicitly. This state is also used when the undo tablespace and
485 its rollback segments are being inititalized.
486
487 These states are changed under an exclusive lock on m_latch and are read
488 under a shared lock.
489
490 The following actions can cause changes in these states:
491 Init: Implicit undo spaces are created at startup.
492 Create: Explicit undo tablespace creation at runtime.
493 Mark: Purge thread implicitly selects an undo space to truncate.
494 SetInactive: This ALTER UNDO TABLESPACE causes an explicit truncation.
495 SetActive: This ALTER UNDO TABLESPACE changes the target state from
496 EMPTY to ACTIVE.
497 Truncate: The truncate process is completed by the purge thread.
498 Drop: Delete an EMPTY undo tablespace
499 Crash: A crash occurs
500 Fixup: At startup, if an undo space was being truncated with a crash.
501 SaveDDState: At startup, once the DD is available the state saved there
502 will be applied. INACTIVE_IMPLICIT is never saved to the DD.
503 So the DD state INACTIVE means INACTIVE_EXPLICIT.
504 See apply_dd_undo_state()
505
506 State changes allowed: (Actions on states not mentioned are not allowed.)
507 Init from null -> INIT -> ACTIVE see srv_start()
508 from null -> INIT -> EMPTY see trx_rsegs_init()
509 Create from null -> EMPTY -> ACTIVE
510 Mark from INACTIVE_EXPLICIT -> INACTIVE_EXPLICIT -> Truncate
511 from ACTIVE -> INACTIVE_IMPLICIT -> Truncate
512 SetInactive from ACTIVE -> INACTIVE_EXPLICIT -> Mark
513 from INACTIVE_IMPLICIT -> INACTIVE_EXPLICIT
514 from INACTIVE_EXPLICIT -> INACTIVE_EXPLICIT
515 from EMPTY -> EMPTY
516 SetActive from ACTIVE -> ACTIVE
517 from INACTIVE_IMPLICIT -> INACTIVE_IMPLICIT
518 from INACTIVE_EXPLICIT -> INACTIVE_IMPLICIT
519 from EMPTY -> ACTIVE
520 Truncate from INACTIVE_IMPLICIT -> ACTIVE
521 from INACTIVE_EXPLICIT -> EMPTY
522 Drop if ACTIVE -> error returned
523 if INACTIVE_IMPLICIT -> error returned
524 if INACTIVE_EXPLICIT -> error returned
525 from EMPTY -> null
526 Crash if ACTIVE, at startup: ACTIVE
527 if INACTIVE_IMPLICIT, at startup: Fixup
528 if INACTIVE_EXPLICIT, at startup: Fixup
529 if EMPTY, at startup: EMPTY
530 Fixup from INACTIVE_IMPLICIT before crash -> INACTIVE_IMPLICIT -> Mark
531 from INACTIVE_EXPLICIT before crash -> INACTIVE_IMPLICIT -> Mark
532 SaveDDState from ACTIVE before crash -> ACTIVE
533 from INACTIVE_IMPLICIT before crash -> ACTIVE
534 from INACTIVE_EXPLICIT before crash -> INACTIVE_EXPLICIT -> Mark
535 from EMPTY -> EMPTY
536 */
537 enum undo_space_states {
538 INIT,
539 ACTIVE,
540 INACTIVE_IMPLICIT,
541 INACTIVE_EXPLICIT,
542 EMPTY
543 };
544
545 /** The current state of this undo tablespace. */
546 undo_space_states m_state;
547 };
548
549 template <size_t N>
550 using Rsegs_array = std::array<trx_rseg_t *, N>;
551
552 /** Rollback segments from a given transaction with trx-no
553 scheduled for purge. */
554 class TrxUndoRsegs {
555 public:
556 explicit TrxUndoRsegs(trx_id_t trx_no) : m_trx_no(trx_no) {
557 for (auto &rseg : m_rsegs) {
558 rseg = nullptr;
559 }
560 }
561
562 /** Default constructor */
563 TrxUndoRsegs() : TrxUndoRsegs(0) {}
564
565 void set_trx_no(trx_id_t trx_no) { m_trx_no = trx_no; }
566
567 /** Get transaction number
568 @return trx_id_t - get transaction number. */
569 trx_id_t get_trx_no() const { return (m_trx_no); }
570
571 /** Add rollback segment.
572 @param rseg rollback segment to add. */
573 void insert(trx_rseg_t *rseg) {
574 for (size_t i = 0; i < m_rsegs_n; ++i) {
575 if (m_rsegs[i] == rseg) {
576 return;
577 }
578 }
579 ut_a(m_rsegs_n < 2);
580 m_rsegs[m_rsegs_n++] = rseg;
581 }
582
583 /** Number of registered rsegs.
584 @return size of rseg list. */
585 size_t size() const { return (m_rsegs_n); }
586
587 /**
588 @return an iterator to the first element */
589 typename Rsegs_array<2>::iterator begin() { return m_rsegs.begin(); }
590
591 /**
592 @return an iterator to the end */
593 typename Rsegs_array<2>::iterator end() {
594 return m_rsegs.begin() + m_rsegs_n;
595 }
596
597 /** Append rollback segments from referred instance to current
598 instance. */
599 void insert(const TrxUndoRsegs &append_from) {
600 ut_ad(get_trx_no() == append_from.get_trx_no());
601 for (size_t i = 0; i < append_from.m_rsegs_n; ++i) {
602 insert(append_from.m_rsegs[i]);
603 }
604 }
605
606 /** Compare two TrxUndoRsegs based on trx_no.
607 @param lhs first element to compare
608 @param rhs second element to compare
609 @return true if elem1 > elem2 else false.*/
610 bool operator()(const TrxUndoRsegs &lhs, const TrxUndoRsegs &rhs) {
611 return (lhs.m_trx_no > rhs.m_trx_no);
612 }
613
614 /** Compiler defined copy-constructor/assignment operator
615 should be fine given that there is no reference to a memory
616 object outside scope of class object.*/
617
618 private:
619 /** The rollback segments transaction number. */
620 trx_id_t m_trx_no;
621
622 size_t m_rsegs_n{};
623
624 /** Rollback segments of a transaction, scheduled for purge. */
625 Rsegs_array<2> m_rsegs;
626 };
627
628 typedef std::priority_queue<
629 TrxUndoRsegs, std::vector<TrxUndoRsegs, ut::allocator<TrxUndoRsegs>>,
630 TrxUndoRsegs>
631 purge_pq_t;
632
633 typedef std::vector<trx_id_t, ut::allocator<trx_id_t>> trx_ids_t;
634
635 struct TrxVersion {
636 TrxVersion(trx_t *trx);
637
638 trx_t *m_trx;
639 uint64_t m_version;
640 };
641
642 typedef std::vector<TrxVersion, ut::allocator<TrxVersion>> hit_list_t;
643 #endif /* trx0types_h */