MySQL 26.7.0
Source Code Documentation
trx0purge.h
Go to the documentation of this file.
1/*****************************************************************************
2
3Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License, version 2.0, as published by the
7Free Software Foundation.
8
9This program is designed to work with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation. The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have either included with
15the program or referenced in the documentation.
16
17This program is distributed in the hope that it will be useful, but WITHOUT
18ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20for more details.
21
22You should have received a copy of the GNU General Public License along with
23this program; if not, write to the Free Software Foundation, Inc.,
2451 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26*****************************************************************************/
27
28/** @file include/trx0purge.h
29 Purge old versions
30
31 Created 3/26/1996 Heikki Tuuri
32 *******************************************************/
33
34#ifndef trx0purge_h
35#define trx0purge_h
36
37#include "fil0fil.h"
38#include "mtr0mtr.h"
39#include "page0page.h"
40#include "que0types.h"
42#include "trx0sys.h"
43#include "trx0types.h"
44#include "trx0undo_trunc.h"
45#include "univ.i"
46#include "usr0sess.h"
47#ifdef UNIV_HOTBACKUP
48#include "trx0sys.h"
49#endif /* UNIV_HOTBACKUP */
50
51/** The global data structure coordinating a purge */
53
54/** Calculates the file address of an undo log header when we have the file
55 address of its history list node.
56 @return file address of the log */
58 fil_addr_t node_addr); /*!< in: file address of the history
59 list node of the log */
60
61/** Initialize in-memory purge structures */
63
64/** Creates the global purge system control structure and inits the history
65mutex.
66@param[in] n_purge_threads number of purge threads
67@param[in,out] purge_queue UNDO log min binary heap */
68void trx_purge_sys_initialize(uint32_t n_purge_threads,
69 purge_pq_t *purge_queue);
70
71/** Frees the global purge system control structure. */
72void trx_purge_sys_close(void);
73
74/************************************************************************
75Adds the update undo log as the first log in the history list. Removes the
76update undo log segment from the rseg slot if it is too big for reuse. */
78 trx_t *trx, /*!< in: transaction */
79 trx_undo_ptr_t *undo_ptr, /*!< in: update undo log. */
80 page_t *undo_page, /*!< in: update undo log header page,
81 x-latched */
82 bool update_rseg_history_len,
83 /*!< in: if true: update rseg history
84 len else skip updating it. */
85 ulint n_added_logs, /*!< in: number of logs added */
86 mtr_t *mtr); /*!< in: mtr */
87
88/** This function runs a purge batch.
89 @return number of undo log pages handled in the batch */
90ulint trx_purge(ulint n_purge_threads, /*!< in: number of purge tasks to
91 submit to task queue. */
92 ulint limit, /*!< in: the maximum number of
93 records to purge in one batch */
94 bool truncate); /*!< in: truncate history if true */
95
96/** Stop purge and wait for it to stop, move to PURGE_STATE_STOP. */
97void trx_purge_stop(void);
98/** Resume purge, move to PURGE_STATE_RUN. */
99void trx_purge_run();
100
101/** Purge states */
103 PURGE_STATE_INIT, /*!< Purge instance created */
104 PURGE_STATE_RUN, /*!< Purge should be running */
105 PURGE_STATE_STOP, /*!< Purge should be stopped */
106 PURGE_STATE_EXIT, /*!< Purge has been shutdown */
107 PURGE_STATE_DISABLED /*!< Purge was never started */
109
110/** Get the purge state.
111 @return purge state. */
113
114// Forward declaration
116
117/** This is the purge pointer/iterator. We need both the undo no and the
118transaction no up to which purge has parsed and applied the records. */
121 // Do nothing
122 }
123
124 /** Purge has advanced past all transactions whose number
125 is less than this */
127
128 /** Purge has advanced past all records whose undo number
129 is less than this. */
131
132 /** The last undo record resided in this space id */
134
135 /** The transaction that created the undo log record,
136 the Modifier trx id */
138};
139
140/** The control structure used in the purge operation */
142 /** System session running the purge query */
144
145 /** System transaction running the purge query: this trx is not in the trx
146 list of the trx system and it never ends */
148#ifndef UNIV_HOTBACKUP
149 /** The latch protecting the purge view. A purge operation must acquire an
150 x-latch here for the instant at which it changes the purge view: an undo
151 log operation can prevent this by obtaining an s-latch here. It also
152 protects state and running */
154#endif /* !UNIV_HOTBACKUP */
155
156 /** State signal event */
158
159 /** Counter to track number stops */
161
162 /** true, if purge is active, we check this without the latch too */
163 std::atomic<bool> running;
164
165 /** Purge coordinator thread states, we check this in several places without
166 holding the latch. */
168
169 /** The query graph which will do the parallelized purge operation */
171
172 /** The purge will not remove undo logs which are >= this view (purge view) */
174
175 /** This is computed as a lower-bound of minimum of:
176 - the smallest trx->no still needed by the oldest open read view
177 - the smallest trx->no still needed by GTID persistor
178 The purge can remove only the Undo Logs which have TRX_UNDO_TRX_NO strictly
179 smaller than this value. */
181
182 /** Count of total tasks submitted to the task queue */
184
185 /** Count of total tasks completed */
186 std::atomic<ulint> n_completed;
187
188 /* The following two fields form the 'purge pointer' which advances
189 during a purge, and which is used in history list truncation */
190
191 /** Limit up to which we have read and parsed the UNDO log records. Not
192 necessarily purged from the indexes. Note that this can never be less than
193 the limit below, we check for this invariant in trx0purge.cc */
195
196 /** The 'purge pointer' which advances during a purge, and which is used in
197 history list truncation */
199#ifdef UNIV_DEBUG
200 /** Indicate 'purge pointer' which have purged already accurately. */
202#endif /* UNIV_DEBUG */
203
204 /** true if the info of the next record to purge is stored below: if yes, then
205 the transaction number and the undo number of the record are stored in
206 purge_trx_no and purge_undo_no above */
208
209 /** Rollback segment for the next undo record to purge */
211
212 /** Page number for the next undo record to purge, page number of the log
213 header, if dummy record */
215
216 /** Page offset for the next undo record to purge, 0 if the dummy record */
218
219 /** Header page of the undo log where the next record to purge belongs */
221
222 /** Header byte offset on the page */
224
225 /** Iterator to get the next rseg to process */
227
228 /** Binary min-heap, ordered on TrxUndoRsegs::trx_no. It is protected
229 by the pq_mutex */
231
232 /** Mutex protecting purge_queue */
234
235 /** Track UNDO tablespace marked for truncate. */
237
238 /** Heap for reading the undo log records */
240
241 /** Is the this thread related to purge? This is false by default and set to
242 true by srv_purge_coordinator_thread() and srv_worker_thread() only. */
243 static inline thread_local bool is_this_a_purge_thread{false};
244
245 /** Set of all rseg queue. */
246 std::vector<trx_rseg_t *> rsegs_queue;
247};
248
249/** Choose the rollback segment with the smallest trx_no. */
251 /** Constructor */
253
254 /** Sets the next rseg to purge in m_purge_sys.
255 @return page size of the table for which the log is.
256 NOTE: if rseg is NULL when this function returns this means that
257 there are no rollback segments to purge and then the returned page
258 size object should not be used. */
259 const page_size_t set_next();
260
261 private:
262 // Disable copying
265
266 /** The purge system pointer */
268
269 /** The current element to process */
271
272 /** Track the current element in m_trx_undo_rseg */
274
275 /** Sentinel value */
277};
278
279#include "trx0purge.ic"
280
281#endif /* trx0purge_h */
uint32_t space_id_t
Tablespace identifier.
Definition: api0api.h:49
uint32_t page_no_t
Page number.
Definition: api0api.h:47
Definition: read0read_view_interface.h:33
Rollback segments from a given transaction with trx-no scheduled for purge.
Definition: trx0types.h:554
Page size descriptor.
Definition: page0size.h:50
Track an UNDO tablespace marked for truncate.
Definition: trx0undo_trunc.h:654
The low-level file system.
Mini-transaction buffer.
std::string truncate(const std::string &str, const size_t max_length)
Truncates the given string to max_length code points.
Definition: utils_string.cc:600
Index page routines.
byte page_t
Type of the index page.
Definition: page0types.h:152
Query graph global types.
Choose the rollback segment with the smallest trx_no.
Definition: trx0purge.h:250
const page_size_t set_next()
Sets the next rseg to purge in m_purge_sys.
Definition: trx0purge.cc:110
static const TrxUndoRsegs NullElement
Sentinel value.
Definition: trx0purge.h:276
TrxUndoRsegsIterator(trx_purge_t *purge_sys)
Constructor.
Definition: trx0purge.cc:100
Rsegs_array< 2 >::iterator m_iter
Track the current element in m_trx_undo_rseg.
Definition: trx0purge.h:273
TrxUndoRsegsIterator(const TrxUndoRsegsIterator &)
TrxUndoRsegs m_trx_undo_rsegs
The current element to process.
Definition: trx0purge.h:270
TrxUndoRsegsIterator & operator=(const TrxUndoRsegsIterator &)
trx_purge_t * m_purge_sys
The purge system pointer.
Definition: trx0purge.h:267
File space address.
Definition: fil0fil.h:1513
The info structure stored at the beginning of a heap block.
Definition: mem0mem.h:295
Mini-transaction handle and buffer.
Definition: mtr0mtr.h:174
InnoDB condition variable.
Definition: os0event.cc:63
This is the purge pointer/iterator.
Definition: trx0purge.h:119
purge_iter_t()
Definition: trx0purge.h:120
undo_no_t undo_no
Purge has advanced past all records whose undo number is less than this.
Definition: trx0purge.h:130
trx_id_t modifier_trx_id
The transaction that created the undo log record, the Modifier trx id.
Definition: trx0purge.h:137
space_id_t undo_rseg_space
The last undo record resided in this space id.
Definition: trx0purge.h:133
trx_id_t trx_no
Purge has advanced past all transactions whose number is less than this.
Definition: trx0purge.h:126
Definition: que0que.h:301
The structure used in the spin lock implementation of a read-write lock.
Definition: sync0rw.h:362
Definition: usr0sess.h:54
The control structure used in the purge operation.
Definition: trx0purge.h:141
purge_pq_t * purge_queue
Binary min-heap, ordered on TrxUndoRsegs::trx_no.
Definition: trx0purge.h:230
bool next_stored
true if the info of the next record to purge is stored below: if yes, then the transaction number and...
Definition: trx0purge.h:207
volatile purge_state_t state
Purge coordinator thread states, we check this in several places without holding the latch.
Definition: trx0purge.h:167
ulint offset
Page offset for the next undo record to purge, 0 if the dummy record.
Definition: trx0purge.h:217
purge_iter_t iter
Limit up to which we have read and parsed the UNDO log records.
Definition: trx0purge.h:194
TrxUndoRsegsIterator * rseg_iter
Iterator to get the next rseg to process.
Definition: trx0purge.h:226
std::vector< trx_rseg_t * > rsegs_queue
Set of all rseg queue.
Definition: trx0purge.h:246
Read_view_interface * view
The purge will not remove undo logs which are >= this view (purge view)
Definition: trx0purge.h:173
mem_heap_t * heap
Heap for reading the undo log records.
Definition: trx0purge.h:239
purge_iter_t limit
The 'purge pointer' which advances during a purge, and which is used in history list truncation.
Definition: trx0purge.h:198
static thread_local bool is_this_a_purge_thread
Is the this thread related to purge? This is false by default and set to true by srv_purge_coordinato...
Definition: trx0purge.h:243
purge_iter_t done
Indicate 'purge pointer' which have purged already accurately.
Definition: trx0purge.h:201
trx_id_t m_lowest_needed_trx_no
This is computed as a lower-bound of minimum of:
Definition: trx0purge.h:180
que_t * query
The query graph which will do the parallelized purge operation.
Definition: trx0purge.h:170
trx_t * trx
System transaction running the purge query: this trx is not in the trx list of the trx system and it ...
Definition: trx0purge.h:147
ulint hdr_offset
Header byte offset on the page.
Definition: trx0purge.h:223
ulint n_submitted
Count of total tasks submitted to the task queue.
Definition: trx0purge.h:183
page_no_t page_no
Page number for the next undo record to purge, page number of the log header, if dummy record.
Definition: trx0purge.h:214
page_no_t hdr_page_no
Header page of the undo log where the next record to purge belongs.
Definition: trx0purge.h:220
ulint n_stop
Counter to track number stops.
Definition: trx0purge.h:160
PQMutex pq_mutex
Mutex protecting purge_queue.
Definition: trx0purge.h:233
std::atomic< ulint > n_completed
Count of total tasks completed.
Definition: trx0purge.h:186
sess_t * sess
System session running the purge query.
Definition: trx0purge.h:143
trx_rseg_t * rseg
Rollback segment for the next undo record to purge.
Definition: trx0purge.h:210
undo_truncate::Truncate undo_trunc
Track UNDO tablespace marked for truncate.
Definition: trx0purge.h:236
rw_lock_t latch
The latch protecting the purge view.
Definition: trx0purge.h:153
std::atomic< bool > running
true, if purge is active, we check this without the latch too
Definition: trx0purge.h:163
os_event_t event
State signal event.
Definition: trx0purge.h:157
The rollback segment memory object.
Definition: trx0types.h:214
Definition: trx0trx.h:670
The transaction handle.
Definition: trx0trx.h:630
void trx_purge_add_update_undo_to_history(trx_t *trx, trx_undo_ptr_t *undo_ptr, page_t *undo_page, bool update_rseg_history_len, ulint n_added_logs, mtr_t *mtr)
in: mtr
Definition: trx0purge.cc:370
void trx_purge_sys_initialize(uint32_t n_purge_threads, purge_pq_t *purge_queue)
Creates the global purge system control structure and inits the history mutex.
Definition: trx0purge.cc:287
void trx_purge_sys_close(void)
Frees the global purge system control structure.
Definition: trx0purge.cc:321
purge_state_t
Purge states.
Definition: trx0purge.h:102
@ PURGE_STATE_DISABLED
Purge was never started.
Definition: trx0purge.h:107
@ PURGE_STATE_EXIT
Purge has been shutdown.
Definition: trx0purge.h:106
@ PURGE_STATE_RUN
Purge should be running.
Definition: trx0purge.h:104
@ PURGE_STATE_STOP
Purge should be stopped.
Definition: trx0purge.h:105
@ PURGE_STATE_INIT
Purge instance created.
Definition: trx0purge.h:103
void trx_purge_run()
Resume purge, move to PURGE_STATE_RUN.
Definition: trx0purge.cc:2022
trx_purge_t * purge_sys
The global data structure coordinating a purge.
Definition: trx0purge.cc:79
void trx_purge_sys_mem_create()
Initialize in-memory purge structures.
Definition: trx0purge.cc:217
ulint trx_purge(ulint n_purge_threads, ulint limit, bool truncate)
This function runs a purge batch.
Definition: trx0purge.cc:1862
purge_state_t trx_purge_state(void)
Get the purge state.
Definition: trx0purge.cc:1950
static fil_addr_t trx_purge_get_log_from_hist(fil_addr_t node_addr)
Calculates the file address of an undo log header when we have the file address of its history list n...
void trx_purge_stop(void)
Stop purge and wait for it to stop, move to PURGE_STATE_STOP.
Definition: trx0purge.cc:1963
Purge old versions.
Transaction system.
Transaction system global type definitions.
std::priority_queue< TrxUndoRsegs, std::vector< TrxUndoRsegs, ut::allocator< TrxUndoRsegs > >, TrxUndoRsegs > purge_pq_t
Definition: trx0types.h:631
ib_id_t undo_no_t
Undo number.
Definition: trx0types.h:142
std::array< trx_rseg_t *, N > Rsegs_array
Definition: trx0types.h:550
ib_mutex_t PQMutex
Definition: trx0types.h:173
ib_id_t trx_id_t
Transaction identifier (DB_TRX_ID, DATA_TRX_ID)
Definition: trx0types.h:138
Undo truncation handling.
Version control for database, common definitions, and include files.
unsigned long int ulint
Definition: univ.i:403
constexpr space_id_t SPACE_UNKNOWN
Unknown space id.
Definition: univ.i:452
Sessions.