| 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/trx0undo.h |
| 29 |
Transaction undo log |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef trx0undo_h |
| 35 |
#define trx0undo_h |
| 36 |
|
| 37 |
#include "mtr0mtr.h" |
| 38 |
#include "page0types.h" |
| 39 |
#include "sql/xa.h" |
| 40 |
#include "trx0sys.h" |
| 41 |
#include "trx0types.h" |
| 42 |
#include "trx0xa.h" |
| 43 |
#include "univ.i" |
| 44 |
|
| 45 |
#ifndef UNIV_HOTBACKUP |
| 46 |
/** Returns true if the roll pointer is of the insert type. |
| 47 |
@return true if insert undo log */ |
| 48 |
static inline bool trx_undo_roll_ptr_is_insert( |
| 49 |
roll_ptr_t roll_ptr); /*!< in: roll pointer */ |
| 50 |
/** Returns true if the record is of the insert type. |
| 51 |
@return true if the record was freshly inserted (not updated). */ |
| 52 |
[[nodiscard]] static inline bool trx_undo_trx_id_is_insert( |
| 53 |
const byte *trx_id); /*!< in: DB_TRX_ID, followed by DB_ROLL_PTR */ |
| 54 |
#endif /* !UNIV_HOTBACKUP */ |
| 55 |
|
| 56 |
/** Writes a roll ptr to an index page. In case that the size changes in |
| 57 |
some future version, this function should be used instead of |
| 58 |
mach_write_... |
| 59 |
@param[in] ptr pointer to memory where written |
| 60 |
@param[in] roll_ptr roll ptr */ |
| 61 |
static inline void trx_write_roll_ptr(byte *ptr, roll_ptr_t roll_ptr); |
| 62 |
|
| 63 |
/** Reads a roll ptr from an index page. In case that the roll ptr size |
| 64 |
changes in some future version, this function should be used instead of |
| 65 |
mach_read_... |
| 66 |
@return roll ptr */ |
| 67 |
static inline roll_ptr_t trx_read_roll_ptr( |
| 68 |
const byte *ptr); /*!< in: pointer to memory from where to read */ |
| 69 |
#ifndef UNIV_HOTBACKUP |
| 70 |
|
| 71 |
/** Gets an undo log page and x-latches it. |
| 72 |
@param[in] page_id Page id |
| 73 |
@param[in] page_size Page size |
| 74 |
@param[in,out] mtr Mini-transaction |
| 75 |
@return pointer to page x-latched */ |
| 76 |
static inline page_t *trx_undo_page_get(const page_id_t &page_id, |
| 77 |
const page_size_t &page_size, |
| 78 |
mtr_t *mtr); |
| 79 |
|
| 80 |
/** Gets an undo log page and s-latches it. |
| 81 |
@param[in] page_id Page id |
| 82 |
@param[in] page_size Page size |
| 83 |
@param[in,out] mtr Mini-transaction |
| 84 |
@return pointer to page s-latched */ |
| 85 |
static inline page_t *trx_undo_page_get_s_latched(const page_id_t &page_id, |
| 86 |
const page_size_t &page_size, |
| 87 |
mtr_t *mtr); |
| 88 |
|
| 89 |
/** Returns the previous undo record on the page in the specified log, or |
| 90 |
NULL if none exists. |
| 91 |
@param[in] rec undo log record |
| 92 |
@param[in] page_no undo log header page number |
| 93 |
@param[in] offset undo log header offset on page |
| 94 |
@return pointer to record, NULL if none */ |
| 95 |
static inline trx_undo_rec_t *trx_undo_page_get_prev_rec(trx_undo_rec_t *rec, |
| 96 |
page_no_t page_no, |
| 97 |
ulint offset); |
| 98 |
|
| 99 |
/** Returns the next undo log record on the page in the specified log, or |
| 100 |
NULL if none exists. |
| 101 |
@param[in] rec undo log record |
| 102 |
@param[in] page_no undo log header page number |
| 103 |
@param[in] offset undo log header offset on page |
| 104 |
@return pointer to record, NULL if none */ |
| 105 |
static inline trx_undo_rec_t *trx_undo_page_get_next_rec(trx_undo_rec_t *rec, |
| 106 |
page_no_t page_no, |
| 107 |
ulint offset); |
| 108 |
|
| 109 |
/** Returns the last undo record on the page in the specified undo log, or |
| 110 |
NULL if none exists. |
| 111 |
@param[in] undo_page undo log page |
| 112 |
@param[in] page_no undo log header page number |
| 113 |
@param[in] offset undo log header offset on page |
| 114 |
@return pointer to record, NULL if none */ |
| 115 |
static inline trx_undo_rec_t *trx_undo_page_get_last_rec(page_t *undo_page, |
| 116 |
page_no_t page_no, |
| 117 |
ulint offset); |
| 118 |
|
| 119 |
/** Returns the first undo record on the page in the specified undo log, or |
| 120 |
NULL if none exists. |
| 121 |
@param[in] undo_page undo log page |
| 122 |
@param[in] page_no undo log header page number |
| 123 |
@param[in] offset undo log header offset on page |
| 124 |
@return pointer to record, NULL if none */ |
| 125 |
static inline trx_undo_rec_t *trx_undo_page_get_first_rec(page_t *undo_page, |
| 126 |
page_no_t page_no, |
| 127 |
ulint offset); |
| 128 |
|
| 129 |
/** Gets the previous record in an undo log. |
| 130 |
@return undo log record, the page s-latched, NULL if none */ |
| 131 |
trx_undo_rec_t *trx_undo_get_prev_rec( |
| 132 |
trx_undo_rec_t *rec, /*!< in: undo record */ |
| 133 |
page_no_t page_no, /*!< in: undo log header page number */ |
| 134 |
ulint offset, /*!< in: undo log header offset on page */ |
| 135 |
bool shared, /*!< in: true=S-latch, false=X-latch */ |
| 136 |
mtr_t *mtr); /*!< in: mtr */ |
| 137 |
/** Gets the next record in an undo log. |
| 138 |
@return undo log record, the page s-latched, NULL if none */ |
| 139 |
trx_undo_rec_t *trx_undo_get_next_rec( |
| 140 |
trx_undo_rec_t *rec, /*!< in: undo record */ |
| 141 |
page_no_t page_no, /*!< in: undo log header page number */ |
| 142 |
ulint offset, /*!< in: undo log header offset on page */ |
| 143 |
mtr_t *mtr); /*!< in: mtr */ |
| 144 |
|
| 145 |
/** Gets the first record in an undo log. |
| 146 |
@param[out] modifier_trx_id The modifier trx identifier. |
| 147 |
@param[in] space Undo log header space |
| 148 |
@param[in] page_size Page size |
| 149 |
@param[in] page_no Undo log header page number |
| 150 |
@param[in] offset Undo log header offset on page |
| 151 |
@param[in] mode Latching mode: RW_S_LATCH or RW_X_LATCH |
| 152 |
@param[in,out] mtr Mini-transaction |
| 153 |
@return undo log record, the page latched, NULL if none */ |
| 154 |
trx_undo_rec_t *trx_undo_get_first_rec(trx_id_t *modifier_trx_id, |
| 155 |
space_id_t space, |
| 156 |
const page_size_t &page_size, |
| 157 |
page_no_t page_no, ulint offset, |
| 158 |
ulint mode, mtr_t *mtr); |
| 159 |
|
| 160 |
/** Tries to add a page to the undo log segment where the undo log is placed. |
| 161 |
@return X-latched block if success, else NULL */ |
| 162 |
[[nodiscard]] buf_block_t *trx_undo_add_page( |
| 163 |
trx_t *trx, /*!< in: transaction */ |
| 164 |
trx_undo_t *undo, /*!< in: undo log memory object */ |
| 165 |
trx_undo_ptr_t *undo_ptr, /*!< in: assign undo log from |
| 166 |
referred rollback segment. */ |
| 167 |
mtr_t *mtr); /*!< in: mtr which does not have |
| 168 |
a latch to any undo log page; |
| 169 |
the caller must have reserved |
| 170 |
the rollback segment mutex */ |
| 171 |
/** Frees the last undo log page. |
| 172 |
The caller must hold the rollback segment mutex. |
| 173 |
@param[in] trx transaction |
| 174 |
@param[in,out] undo undo log memory copy |
| 175 |
@param[in,out] mtr mini-transaction which does not have a latch to any undo log |
| 176 |
page or which has allocated the undo log page */ |
| 177 |
void trx_undo_free_last_page_func(IF_DEBUG(const trx_t *trx, ) trx_undo_t *undo, |
| 178 |
mtr_t *mtr); |
| 179 |
|
| 180 |
static inline void trx_undo_free_last_page(const trx_t *trx [[maybe_unused]], |
| 181 |
trx_undo_t *undo, mtr_t *mtr) { |
| 182 |
trx_undo_free_last_page_func(IF_DEBUG(trx, ) undo, mtr); |
| 183 |
} |
| 184 |
|
| 185 |
/** Truncates an undo log from the end. This function is used during |
| 186 |
a rollback to free space from an undo log. |
| 187 |
@param[in] trx transaction for this undo log |
| 188 |
@param[in] undo undo log |
| 189 |
@param[in] limit all undo records with undo number; |
| 190 |
This value should be truncated. */ |
| 191 |
void trx_undo_truncate_end_func(IF_DEBUG(const trx_t *trx, ) trx_undo_t *undo, |
| 192 |
undo_no_t limit); |
| 193 |
|
| 194 |
static inline void trx_undo_truncate_end(const trx_t *trx [[maybe_unused]], |
| 195 |
trx_undo_t *undo, undo_no_t limit) { |
| 196 |
trx_undo_truncate_end_func(IF_DEBUG(trx, ) undo, limit); |
| 197 |
} |
| 198 |
|
| 199 |
/** Truncate the head of an undo log. |
| 200 |
NOTE that only whole pages are freed; the header page is not |
| 201 |
freed, but emptied, if all the records there are below the limit. |
| 202 |
@param[in,out] rseg rollback segment |
| 203 |
@param[in] hdr_page_no header page number |
| 204 |
@param[in] hdr_offset header offset on the page |
| 205 |
@param[in] limit first undo number to preserve |
| 206 |
(everything below the limit will be truncated) */ |
| 207 |
void trx_undo_truncate_start(trx_rseg_t *rseg, page_no_t hdr_page_no, |
| 208 |
ulint hdr_offset, undo_no_t limit); |
| 209 |
/** Initializes the undo log lists for a rollback segment memory copy. |
| 210 |
This function is only called when the database is started or a new |
| 211 |
rollback segment created. |
| 212 |
@return the combined size of undo log segments in pages */ |
| 213 |
ulint trx_undo_lists_init( |
| 214 |
trx_rseg_t *rseg); /*!< in: rollback segment memory object */ |
| 215 |
/** Assigns an undo log for a transaction. A new undo log is created or a cached |
| 216 |
undo log reused. |
| 217 |
@return DB_SUCCESS if undo log assign successful, possible error codes |
| 218 |
are: DB_TOO_MANY_CONCURRENT_TRXS DB_OUT_OF_FILE_SPACE DB_READ_ONLY |
| 219 |
DB_OUT_OF_MEMORY */ |
| 220 |
[[nodiscard]] dberr_t trx_undo_assign_undo( |
| 221 |
trx_t *trx, /*!< in: transaction */ |
| 222 |
trx_undo_ptr_t *undo_ptr, /*!< in: assign undo log from |
| 223 |
referred rollback segment. */ |
| 224 |
ulint type); /*!< in: TRX_UNDO_INSERT or |
| 225 |
TRX_UNDO_UPDATE */ |
| 226 |
/** Sets the state of the undo log segment at a transaction finish. |
| 227 |
@return undo log segment header page, x-latched */ |
| 228 |
page_t *trx_undo_set_state_at_finish( |
| 229 |
trx_undo_t *undo, /*!< in: undo log memory copy */ |
| 230 |
mtr_t *mtr); /*!< in: mtr */ |
| 231 |
|
| 232 |
/** Set the state of the undo log segment at a XA PREPARE or XA ROLLBACK. |
| 233 |
@param[in,out] trx Transaction |
| 234 |
@param[in,out] undo Insert_undo or update_undo log |
| 235 |
@param[in] rollback false=XA PREPARE, true=XA ROLLBACK |
| 236 |
@param[in,out] mtr Mini-transaction |
| 237 |
@return undo log segment header page, x-latched */ |
| 238 |
page_t *trx_undo_set_state_at_prepare(trx_t *trx, trx_undo_t *undo, |
| 239 |
bool rollback, mtr_t *mtr); |
| 240 |
|
| 241 |
/** Set the state of the undo log segment as prepared in TC. |
| 242 |
@param[in,out] trx Transaction |
| 243 |
@param[in,out] undo Insert_undo or update_undo log |
| 244 |
@param[in,out] mtr Mini-transaction |
| 245 |
@return undo log segment header page, x-latched */ |
| 246 |
page_t *trx_undo_set_prepared_in_tc(trx_t *trx, trx_undo_t *undo, mtr_t *mtr); |
| 247 |
|
| 248 |
/** Adds the update undo log header as the first in the history list, and |
| 249 |
frees the memory object, or puts it to the list of cached update undo log |
| 250 |
segments. |
| 251 |
@param[in] trx Trx owning the update undo log |
| 252 |
@param[in] undo_ptr Update undo log. |
| 253 |
@param[in] undo_page Update undo log header page, x-latched |
| 254 |
@param[in] update_rseg_history_len If true: update rseg history len else |
| 255 |
skip updating it. |
| 256 |
@param[in] n_added_logs Number of logs added |
| 257 |
@param[in] mtr Mini-transaction */ |
| 258 |
void trx_undo_update_cleanup(trx_t *trx, trx_undo_ptr_t *undo_ptr, |
| 259 |
page_t *undo_page, bool update_rseg_history_len, |
| 260 |
|
| 261 |
ulint n_added_logs, mtr_t *mtr); |
| 262 |
|
| 263 |
/** Frees an insert undo log after a transaction commit or rollback. |
| 264 |
Knowledge of inserts is not needed after a commit or rollback, therefore |
| 265 |
the data can be discarded. |
| 266 |
@param[in,out] undo_ptr undo log to clean up |
| 267 |
@param[in] noredo whether the undo tablespace is redo logged */ |
| 268 |
void trx_undo_insert_cleanup(trx_undo_ptr_t *undo_ptr, bool noredo); |
| 269 |
|
| 270 |
/** At shutdown, frees the undo logs of a transaction which was either |
| 271 |
PREPARED or (ACTIVE and recovered). |
| 272 |
@param[in] trx transaction which undo logs are freed |
| 273 |
@param[in] prepared whether or not the undo segment is in prepared or |
| 274 |
prepared in tc states */ |
| 275 |
void trx_undo_free_trx_with_prepared_or_active_logs(trx_t *trx, |
| 276 |
bool prepared) UNIV_COLD; |
| 277 |
|
| 278 |
/* Forward declaration. */ |
| 279 |
namespace undo { |
| 280 |
struct Tablespace; |
| 281 |
class Truncate; |
| 282 |
} // namespace undo |
| 283 |
|
| 284 |
/** Truncate UNDO tablespace, reinitialize header and rseg. |
| 285 |
@param[in] marked_space UNDO tablespace to truncate |
| 286 |
@return true if success else false. */ |
| 287 |
bool trx_undo_truncate_tablespace(undo::Tablespace *marked_space); |
| 288 |
|
| 289 |
#endif /* !UNIV_HOTBACKUP */ |
| 290 |
/** Parses the redo log entry of an undo log page initialization. |
| 291 |
@return end of log record or NULL */ |
| 292 |
byte *trx_undo_parse_page_init(const byte *ptr, /*!< in: buffer */ |
| 293 |
const byte *end_ptr, /*!< in: buffer end */ |
| 294 |
page_t *page, /*!< in: page or NULL */ |
| 295 |
mtr_t *mtr); /*!< in: mtr or NULL */ |
| 296 |
/** Parse the redo log entry of an undo log page header create or reuse. |
| 297 |
@param[in] type MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE |
| 298 |
@param[in] ptr Redo log record |
| 299 |
@param[in] end_ptr End of log buffer |
| 300 |
@param[in,out] page Page frame or NULL |
| 301 |
@param[in,out] mtr Mini-transaction or NULL |
| 302 |
@return end of log record or NULL */ |
| 303 |
byte *trx_undo_parse_page_header(mlog_id_t type, const byte *ptr, |
| 304 |
const byte *end_ptr, page_t *page, mtr_t *mtr); |
| 305 |
/************************************************************************ |
| 306 |
Frees an undo log memory copy. */ |
| 307 |
void trx_undo_mem_free(trx_undo_t *undo); /* in: the undo object to be freed */ |
| 308 |
|
| 309 |
/** Types of an undo log segment */ |
| 310 |
/** contains undo entries for inserts */ |
| 311 |
constexpr uint32_t TRX_UNDO_INSERT = 1; |
| 312 |
/** contains undo entries for updates and delete markings: in short, modifys |
| 313 |
(the name 'UPDATE' is a historical relic) */ |
| 314 |
constexpr uint32_t TRX_UNDO_UPDATE = 2; |
| 315 |
|
| 316 |
/* States of an undo log segment */ |
| 317 |
/** contains an undo log of an active transaction */ |
| 318 |
constexpr uint32_t TRX_UNDO_ACTIVE = 1; |
| 319 |
/** cached for quick reuse */ |
| 320 |
constexpr uint32_t TRX_UNDO_CACHED = 2; |
| 321 |
/** insert undo segment can be freed */ |
| 322 |
constexpr uint32_t TRX_UNDO_TO_FREE = 3; |
| 323 |
/** update undo segment will not be reused: it can be freed in purge when all |
| 324 |
undo data in it is removed */ |
| 325 |
constexpr uint32_t TRX_UNDO_TO_PURGE = 4; |
| 326 |
/** contains an undo log of an prepared transaction for a server version older |
| 327 |
* than 8.0.29 */ |
| 328 |
constexpr uint32_t TRX_UNDO_PREPARED_80028 = 5; |
| 329 |
/** contains an undo log of an prepared transaction */ |
| 330 |
constexpr uint32_t TRX_UNDO_PREPARED = 6; |
| 331 |
/* contains an undo log of a prepared transaction that has been processed by the |
| 332 |
* transaction coordinator */ |
| 333 |
constexpr uint32_t TRX_UNDO_PREPARED_IN_TC = 7; |
| 334 |
|
| 335 |
#ifndef UNIV_HOTBACKUP |
| 336 |
/** Transaction undo log memory object; this is protected by the undo_mutex |
| 337 |
in the corresponding transaction object */ |
| 338 |
|
| 339 |
struct trx_undo_t { |
| 340 |
/** Undo log may could be allocated to store transaction GTIDs. */ |
| 341 |
enum class Gtid_storage { |
| 342 |
/* No storage is allocated for GTID. */ |
| 343 |
NONE, |
| 344 |
/* Storage is allocated for commit GTID. */ |
| 345 |
COMMIT, |
| 346 |
/* Storage is allocated for both prepare and commit GTID. For external |
| 347 |
XA transaction, we have GTID fr both prepare and commit. */ |
| 348 |
PREPARE_AND_COMMIT |
| 349 |
}; |
| 350 |
|
| 351 |
/** Check if space for GTID is allocated in undo. |
| 352 |
@param[in] is_prepare if XA prepare GTID |
| 353 |
@return true iff space for GTID is allocated. */ |
| 354 |
bool gtid_allocated(bool is_prepare) const; |
| 355 |
|
| 356 |
/** Get offset and flag for GTID stored in undo. |
| 357 |
@param[in] is_prepare if XA prepare GTID |
| 358 |
@return GTID flag and offset in a tuple. */ |
| 359 |
std::tuple<int, size_t> gtid_get_details(bool is_prepare) const; |
| 360 |
|
| 361 |
/* Set undo segment to prepared state and set XID |
| 362 |
@param[in] in_xid transaction XID. */ |
| 363 |
inline void set_prepared(const XID *in_xid); |
| 364 |
|
| 365 |
/* Set undo segment to prepared in TC state and set XID */ |
| 366 |
inline void set_prepared_in_tc(); |
| 367 |
|
| 368 |
/* Checks whether or not this undo log segment is in prepared state, meaning, |
| 369 |
the `state` member variable is either `TRX_UNDO_PREPARED_80028`. |
| 370 |
`TRX_UNDO_PREPARED` or `TRX_UNDO_PREPARED_IN_TC`. |
| 371 |
@return true is the undo log segment is in prepared state, false otherwise.*/ |
| 372 |
inline bool is_prepared() const; |
| 373 |
|
| 374 |
/*-----------------------------*/ |
| 375 |
ulint id; /*!< undo log slot number within the |
| 376 |
rollback segment */ |
| 377 |
ulint type; /*!< TRX_UNDO_INSERT or |
| 378 |
TRX_UNDO_UPDATE */ |
| 379 |
ulint state; /*!< state of the corresponding undo log |
| 380 |
segment */ |
| 381 |
bool del_marks; /*!< relevant only in an update undo |
| 382 |
log: this is true if the transaction may |
| 383 |
have delete marked records, because of |
| 384 |
a delete of a row or an update of an |
| 385 |
indexed field; purge is then |
| 386 |
necessary; also true if the transaction |
| 387 |
has updated an externally stored |
| 388 |
field */ |
| 389 |
trx_id_t trx_id; /*!< id of the trx assigned to the undo |
| 390 |
log */ |
| 391 |
XID xid; /*!< X/Open XA transaction |
| 392 |
identification */ |
| 393 |
ulint flag; /*!< flag for current transaction XID and GTID. |
| 394 |
Persisted in TRX_UNDO_FLAGS flag of undo header. */ |
| 395 |
|
| 396 |
/** Storage space allocated for GTIDs. */ |
| 397 |
Gtid_storage m_gtid_storage{Gtid_storage::NONE}; |
| 398 |
|
| 399 |
bool dict_operation; /*!< true if a dict operation trx */ |
| 400 |
trx_rseg_t *rseg; /*!< rseg where the undo log belongs */ |
| 401 |
/*-----------------------------*/ |
| 402 |
space_id_t space; /*!< space id where the undo log |
| 403 |
placed */ |
| 404 |
page_size_t page_size; |
| 405 |
page_no_t hdr_page_no; /*!< page number of the header page in |
| 406 |
the undo log */ |
| 407 |
ulint hdr_offset; /*!< header offset of the undo log on |
| 408 |
the page */ |
| 409 |
page_no_t last_page_no; /*!< page number of the last page in the |
| 410 |
undo log; this may differ from |
| 411 |
top_page_no during a rollback */ |
| 412 |
ulint size; /*!< current size in pages */ |
| 413 |
/*-----------------------------*/ |
| 414 |
ulint empty; /*!< true if the stack of undo log |
| 415 |
records is currently empty */ |
| 416 |
page_no_t top_page_no; /*!< page number where the latest undo |
| 417 |
log record was catenated; during |
| 418 |
rollback the page from which the latest |
| 419 |
undo record was chosen */ |
| 420 |
ulint top_offset; /*!< offset of the latest undo record, |
| 421 |
i.e., the topmost element in the undo |
| 422 |
log if we think of it as a stack */ |
| 423 |
undo_no_t top_undo_no; /*!< undo number of the latest record */ |
| 424 |
buf_block_t *guess_block; /*!< guess for the buffer block where |
| 425 |
the top page might reside */ |
| 426 |
/*-----------------------------*/ |
| 427 |
UT_LIST_NODE_T(trx_undo_t) undo_list; |
| 428 |
/*!< undo log objects in the rollback |
| 429 |
segment are chained into lists */ |
| 430 |
}; |
| 431 |
|
| 432 |
UT_LIST_NODE_GETTER_DEFINITION(trx_undo_t, undo_list) |
| 433 |
|
| 434 |
/** For saving GTID add update undo slot, if required. |
| 435 |
@param[in] trx transaction |
| 436 |
@param[in] prepare operation is prepare |
| 437 |
@param[in] rollback operation is rollback |
| 438 |
@return innodb error code. */ |
| 439 |
dberr_t trx_undo_gtid_add_update_undo(trx_t *trx, bool prepare, bool rollback); |
| 440 |
|
| 441 |
/** Set GTID flag in undo if transaction has GTID/ |
| 442 |
@param[in,out] trx transaction |
| 443 |
@param[in,out] undo undo log memory object |
| 444 |
@param[in] is_xa_prepare GTID is for XA prepared transaction. */ |
| 445 |
void trx_undo_gtid_set(trx_t *trx, trx_undo_t *undo, bool is_xa_prepare); |
| 446 |
|
| 447 |
/** Read and persist GTID from undo header during recovery. |
| 448 |
@param[in] undo_log undo log header */ |
| 449 |
void trx_undo_gtid_read_and_persist(trx_ulogf_t *undo_log); |
| 450 |
|
| 451 |
/** Write GTID information to undo log header. |
| 452 |
@param[in,out] trx transaction |
| 453 |
@param[in,out] undo_header undo log header |
| 454 |
@param[in,out] undo undo log memory object |
| 455 |
@param[in,out] mtr minit transaction for write |
| 456 |
@param[in] is_xa_prepare GTID is for XA prepared transaction. */ |
| 457 |
void trx_undo_gtid_write(trx_t *trx, trx_ulogf_t *undo_header, trx_undo_t *undo, |
| 458 |
mtr_t *mtr, bool is_xa_prepare); |
| 459 |
|
| 460 |
#endif /* !UNIV_HOTBACKUP */ |
| 461 |
|
| 462 |
/** The offset of the undo log page header on pages of the undo log */ |
| 463 |
constexpr uint32_t TRX_UNDO_PAGE_HDR = FSEG_PAGE_DATA; |
| 464 |
/*-------------------------------------------------------------*/ |
| 465 |
/** Transaction undo log page header offsets */ |
| 466 |
/** @{ */ |
| 467 |
/** TRX_UNDO_INSERT or TRX_UNDO_UPDATE */ |
| 468 |
constexpr uint32_t TRX_UNDO_PAGE_TYPE = 0; |
| 469 |
/** Byte offset where the undo log records for the LATEST transaction start on |
| 470 |
this page (remember that in an update undo log, the first page can contain |
| 471 |
several undo logs) */ |
| 472 |
constexpr uint32_t TRX_UNDO_PAGE_START = 2; |
| 473 |
/** On each page of the undo log this field contains the byte offset of the |
| 474 |
first free byte on the page */ |
| 475 |
constexpr uint32_t TRX_UNDO_PAGE_FREE = 4; |
| 476 |
/** The file list node in the chain of undo log pages */ |
| 477 |
constexpr uint32_t TRX_UNDO_PAGE_NODE = 6; |
| 478 |
/*-------------------------------------------------------------*/ |
| 479 |
/** Size of the transaction undo log page header, in bytes */ |
| 480 |
constexpr uint32_t TRX_UNDO_PAGE_HDR_SIZE = 6 + FLST_NODE_SIZE; |
| 481 |
|
| 482 |
/** @} */ |
| 483 |
|
| 484 |
/** An update undo segment with just one page can be reused if it has |
| 485 |
at most this many bytes used; we must leave space at least for one new undo |
| 486 |
log header on the page */ |
| 487 |
|
| 488 |
#define TRX_UNDO_PAGE_REUSE_LIMIT (3 * UNIV_PAGE_SIZE / 4) |
| 489 |
|
| 490 |
/* An update undo log segment may contain several undo logs on its first page |
| 491 |
if the undo logs took so little space that the segment could be cached and |
| 492 |
reused. All the undo log headers are then on the first page, and the last one |
| 493 |
owns the undo log records on subsequent pages if the segment is bigger than |
| 494 |
one page. If an undo log is stored in a segment, then on the first page it is |
| 495 |
allowed to have zero undo records, but if the segment extends to several |
| 496 |
pages, then all the rest of the pages must contain at least one undo log |
| 497 |
record. */ |
| 498 |
|
| 499 |
/** The offset of the undo log segment header on the first page of the undo |
| 500 |
log segment */ |
| 501 |
|
| 502 |
constexpr uint32_t TRX_UNDO_SEG_HDR = |
| 503 |
TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_HDR_SIZE; |
| 504 |
/** Undo log segment header */ |
| 505 |
/** @{ */ |
| 506 |
/*-------------------------------------------------------------*/ |
| 507 |
/** TRX_UNDO_ACTIVE, ... */ |
| 508 |
constexpr uint32_t TRX_UNDO_STATE = 0; |
| 509 |
/** Offset of the last undo log header on the segment header page, 0 if none */ |
| 510 |
constexpr uint32_t TRX_UNDO_LAST_LOG = 2; |
| 511 |
/** Header for the file segment which the undo log segment occupies */ |
| 512 |
constexpr uint32_t TRX_UNDO_FSEG_HEADER = 4; |
| 513 |
/** Base node for the list of pages in the undo log segment; defined only on the |
| 514 |
undo log segment's first page */ |
| 515 |
constexpr uint32_t TRX_UNDO_PAGE_LIST = 4 + FSEG_HEADER_SIZE; |
| 516 |
/*-------------------------------------------------------------*/ |
| 517 |
/** Size of the undo log segment header */ |
| 518 |
constexpr uint32_t TRX_UNDO_SEG_HDR_SIZE = |
| 519 |
4 + FSEG_HEADER_SIZE + FLST_BASE_NODE_SIZE; |
| 520 |
/** @} */ |
| 521 |
|
| 522 |
/** The undo log header. There can be several undo log headers on the first |
| 523 |
page of an update undo log segment. */ |
| 524 |
/** @{ */ |
| 525 |
/*-------------------------------------------------------------*/ |
| 526 |
/** Transaction id */ |
| 527 |
constexpr uint32_t TRX_UNDO_TRX_ID = 0; |
| 528 |
/** Transaction number of the transaction; defined only if the log is in a |
| 529 |
history list */ |
| 530 |
constexpr uint32_t TRX_UNDO_TRX_NO = 8; |
| 531 |
/** Defined only in an update undo log: true if the transaction may have done |
| 532 |
delete markings of records, and thus purge is necessary */ |
| 533 |
constexpr uint32_t TRX_UNDO_DEL_MARKS = 16; |
| 534 |
/** Offset of the first undo log record of this log on the header page; purge |
| 535 |
may remove undo log record from the log start, and therefore this is not |
| 536 |
necessarily the same as this log header end offset */ |
| 537 |
constexpr uint32_t TRX_UNDO_LOG_START = 18; |
| 538 |
/** Transaction UNDO flags in one byte. This is backward compatible as earlier |
| 539 |
we were storing either 1 or 0 for TRX_UNDO_XID_EXISTS. */ |
| 540 |
constexpr uint32_t TRX_UNDO_FLAGS = 20; |
| 541 |
/** true if undo log header includes X/Open XA transaction identification XID */ |
| 542 |
constexpr uint32_t TRX_UNDO_FLAG_XID = 0x01; |
| 543 |
/** true if undo log header includes GTID information from replication */ |
| 544 |
constexpr uint32_t TRX_UNDO_FLAG_GTID = 0x02; |
| 545 |
/** true if undo log header includes GTID information for XA PREPARE */ |
| 546 |
constexpr uint32_t TRX_UNDO_FLAG_XA_PREPARE_GTID = 0x04; |
| 547 |
/** true if the transaction is a table create, index create, or drop |
| 548 |
transaction: in recovery the transaction cannot be rolled back in the usual |
| 549 |
way: a 'rollback' rather means dropping the created or dropped table, if it |
| 550 |
still exists */ |
| 551 |
constexpr uint32_t TRX_UNDO_DICT_TRANS = 21; |
| 552 |
/** Id of the table if the preceding field is true. Note: deprecated */ |
| 553 |
constexpr uint32_t TRX_UNDO_TABLE_ID = 22; |
| 554 |
/** Offset of the next undo log header on this page, 0 if none */ |
| 555 |
constexpr uint32_t TRX_UNDO_NEXT_LOG = 30; |
| 556 |
/** Offset of the previous undo log header on this page, 0 if none */ |
| 557 |
constexpr uint32_t TRX_UNDO_PREV_LOG = 32; |
| 558 |
/** If the log is put to the history list, the file list node is here */ |
| 559 |
constexpr uint32_t TRX_UNDO_HISTORY_NODE = 34; |
| 560 |
/*-------------------------------------------------------------*/ |
| 561 |
/** Size of the undo log header without XID information */ |
| 562 |
constexpr uint32_t TRX_UNDO_LOG_OLD_HDR_SIZE = 34 + FLST_NODE_SIZE; |
| 563 |
|
| 564 |
/* Note: the writing of the undo log old header is coded by a log record |
| 565 |
MLOG_UNDO_HDR_CREATE or MLOG_UNDO_HDR_REUSE. The appending of an XID to the |
| 566 |
header is logged separately. In this sense, the XID is not really a member |
| 567 |
of the undo log header. TODO: do not append the XID to the log header if XA |
| 568 |
is not needed by the user. The XID wastes about 150 bytes of space in every |
| 569 |
undo log. In the history list we may have millions of undo logs, which means |
| 570 |
quite a large overhead. */ |
| 571 |
/** @} */ |
| 572 |
|
| 573 |
/** X/Open XA Transaction Identification (XID) */ |
| 574 |
/** @{ */ |
| 575 |
/** xid_t::formatID */ |
| 576 |
constexpr uint32_t TRX_UNDO_XA_FORMAT = TRX_UNDO_LOG_OLD_HDR_SIZE; |
| 577 |
/** xid_t::gtrid_length */ |
| 578 |
constexpr uint32_t TRX_UNDO_XA_TRID_LEN = TRX_UNDO_XA_FORMAT + 4; |
| 579 |
/** xid_t::bqual_length */ |
| 580 |
constexpr uint32_t TRX_UNDO_XA_BQUAL_LEN = TRX_UNDO_XA_TRID_LEN + 4; |
| 581 |
/** Distributed transaction identifier data */ |
| 582 |
constexpr uint32_t TRX_UNDO_XA_XID = TRX_UNDO_XA_BQUAL_LEN + 4; |
| 583 |
/*--------------------------------------------------------------*/ |
| 584 |
constexpr uint32_t TRX_UNDO_LOG_XA_HDR_SIZE = TRX_UNDO_XA_XID + XIDDATASIZE; |
| 585 |
/*!< Total size of the undo log header |
| 586 |
with the XA XID */ |
| 587 |
/** @} */ |
| 588 |
|
| 589 |
/* GTID is generated by replication when binlog and GTID mode is on. We |
| 590 |
persist GTID with undo record till it is written to gtid_exeuted table. |
| 591 |
GTID information is present when TRX_UNDO_FLAG_GTID set. It follows XID |
| 592 |
information */ |
| 593 |
|
| 594 |
/** GTID version offset */ |
| 595 |
constexpr uint32_t TRX_UNDO_LOG_GTID_VERSION = TRX_UNDO_LOG_XA_HDR_SIZE; |
| 596 |
|
| 597 |
/** GTID offset */ |
| 598 |
constexpr uint32_t TRX_UNDO_LOG_GTID = TRX_UNDO_LOG_XA_HDR_SIZE + 1; |
| 599 |
|
| 600 |
/** Total length of GTID */ |
| 601 |
constexpr uint32_t TRX_UNDO_LOG_GTID_LEN = 64; |
| 602 |
|
| 603 |
/** Total size with GTID information. */ |
| 604 |
constexpr uint32_t TRX_UNDO_LOG_GTID_HDR_SIZE = |
| 605 |
TRX_UNDO_LOG_GTID + TRX_UNDO_LOG_GTID_LEN; |
| 606 |
|
| 607 |
/** GTID offset for XA Prepare. */ |
| 608 |
constexpr uint32_t TRX_UNDO_LOG_GTID_XA = TRX_UNDO_LOG_GTID_HDR_SIZE; |
| 609 |
|
| 610 |
/** Total size with XA GTID information. For external XA transaction we need |
| 611 |
to store both prepare and commit GTID. */ |
| 612 |
constexpr uint32_t TRX_UNDO_LOG_GTID_XA_HDR_SIZE = |
| 613 |
TRX_UNDO_LOG_GTID_HDR_SIZE + TRX_UNDO_LOG_GTID_LEN; |
| 614 |
|
| 615 |
#include "trx0undo.ic" |
| 616 |
#endif |