| 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/trx0rec.h |
| 29 |
Transaction undo log record |
| 30 |
|
| 31 |
Created 3/26/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef trx0rec_h |
| 35 |
#define trx0rec_h |
| 36 |
|
| 37 |
#include "univ.i" |
| 38 |
|
| 39 |
#include "data0data.h" |
| 40 |
#include "dict0types.h" |
| 41 |
#include "lob0undo.h" |
| 42 |
#include "mtr0mtr.h" |
| 43 |
#include "page0types.h" |
| 44 |
#include "rem0types.h" |
| 45 |
#include "row0log.h" |
| 46 |
#include "row0types.h" |
| 47 |
#include "trx0types.h" |
| 48 |
|
| 49 |
#ifndef UNIV_HOTBACKUP |
| 50 |
#include "que0types.h" |
| 51 |
|
| 52 |
/** Copies the undo record to the heap. |
| 53 |
@param[in] undo_page Undo Page |
| 54 |
@param[in] undo_offset offset of the undo record in the page |
| 55 |
@param[in] heap heap where copied |
| 56 |
@return copy of undo log record */ |
| 57 |
static inline trx_undo_rec_t *trx_undo_rec_copy(const page_t *undo_page, |
| 58 |
uint32_t undo_offset, |
| 59 |
mem_heap_t *heap); |
| 60 |
|
| 61 |
/** Reads the undo log record type. |
| 62 |
@return record type */ |
| 63 |
static inline ulint trx_undo_rec_get_type( |
| 64 |
const trx_undo_rec_t *undo_rec); /*!< in: undo log record */ |
| 65 |
/** Reads from an undo log record the record compiler info. |
| 66 |
@return compiler info */ |
| 67 |
static inline ulint trx_undo_rec_get_cmpl_info( |
| 68 |
const trx_undo_rec_t *undo_rec); /*!< in: undo log record */ |
| 69 |
/** Returns true if an undo log record contains an extern storage field. |
| 70 |
@return true if extern */ |
| 71 |
static inline bool trx_undo_rec_get_extern_storage( |
| 72 |
const trx_undo_rec_t *undo_rec); /*!< in: undo log record */ |
| 73 |
/** Reads the undo log record number. |
| 74 |
@return undo no */ |
| 75 |
static inline undo_no_t trx_undo_rec_get_undo_no( |
| 76 |
const trx_undo_rec_t *undo_rec); /*!< in: undo log record */ |
| 77 |
|
| 78 |
/** Reads from an undo log record the table ID |
| 79 |
@param[in] undo_rec Undo log record |
| 80 |
@return the table ID */ |
| 81 |
[[nodiscard]] table_id_t trx_undo_rec_get_table_id( |
| 82 |
const trx_undo_rec_t *undo_rec); |
| 83 |
|
| 84 |
/** Builds a row reference from an undo log record. |
| 85 |
@return pointer to remaining part of undo record */ |
| 86 |
byte *trx_undo_rec_get_row_ref( |
| 87 |
byte *ptr, /*!< in: remaining part of a copy of an undo log |
| 88 |
record, at the start of the row reference; |
| 89 |
NOTE that this copy of the undo log record must |
| 90 |
be preserved as long as the row reference is |
| 91 |
used, as we do NOT copy the data in the |
| 92 |
record! */ |
| 93 |
dict_index_t *index, /*!< in: clustered index */ |
| 94 |
dtuple_t **ref, /*!< out, own: row reference */ |
| 95 |
mem_heap_t *heap); /*!< in: memory heap from which the memory |
| 96 |
needed is allocated */ |
| 97 |
/** Reads from an undo log update record the system field values of the old |
| 98 |
version. |
| 99 |
@return remaining part of undo log record after reading these values */ |
| 100 |
byte *trx_undo_update_rec_get_sys_cols( |
| 101 |
const byte *ptr, /*!< in: remaining part of undo |
| 102 |
log record after reading |
| 103 |
general parameters */ |
| 104 |
trx_id_t *trx_id, /*!< out: trx id */ |
| 105 |
roll_ptr_t *roll_ptr, /*!< out: roll ptr */ |
| 106 |
ulint *info_bits); /*!< out: info bits state */ |
| 107 |
|
| 108 |
struct type_cmpl_t; |
| 109 |
|
| 110 |
/** Builds an update vector based on a remaining part of an undo log record. |
| 111 |
@param[in] ptr Remaining part in update undo log record, after reading the row |
| 112 |
reference. NOTE that this copy of the undo log record must be preserved as long |
| 113 |
as the update vector is used, as we do NOT copy the data in the record! |
| 114 |
@param[in] index Clustered index. |
| 115 |
@param[in] type TRX_UNDO_UPD_EXIST_REC, TRX_UNDO_UPD_DEL_REC, or |
| 116 |
TRX_UNDO_DEL_MARK_REC; in the last case, only trx id and roll ptr fields are |
| 117 |
added to the update vector. |
| 118 |
@param[in] trx_id Transaction id from this undo record. |
| 119 |
@param[in] roll_ptr Roll pointer from this undo record. |
| 120 |
@param[in] info_bits Info bits from this undo record. |
| 121 |
@param[in] heap Memory heap from which the memory needed is allocated. |
| 122 |
@param[out] upd Update vector. |
| 123 |
@param[out] lob_undo LOB undo information. |
| 124 |
@param[out] type_cmpl Type compilation info. |
| 125 |
@return remaining part of the record, NULL if an error detected, which |
| 126 |
means that the record is corrupted. */ |
| 127 |
byte *trx_undo_update_rec_get_update(const byte *ptr, const dict_index_t *index, |
| 128 |
ulint type, trx_id_t trx_id, |
| 129 |
roll_ptr_t roll_ptr, ulint info_bits, |
| 130 |
mem_heap_t *heap, upd_t **upd, |
| 131 |
lob::undo_vers_t *lob_undo, |
| 132 |
type_cmpl_t &type_cmpl); |
| 133 |
|
| 134 |
/** Builds a partial row from an update undo log record, for purge. |
| 135 |
It contains the columns which occur as ordering in any index of the table. |
| 136 |
Any missing columns are indicated by col->mtype == DATA_MISSING. |
| 137 |
@return pointer to remaining part of undo record */ |
| 138 |
[[nodiscard]] byte *trx_undo_rec_get_partial_row( |
| 139 |
const byte *ptr, /*!< in: remaining part in update undo log |
| 140 |
record of a suitable type, at the start of |
| 141 |
the stored index columns; |
| 142 |
NOTE that this copy of the undo log record must |
| 143 |
be preserved as long as the partial row is |
| 144 |
used, as we do NOT copy the data in the |
| 145 |
record! */ |
| 146 |
dict_index_t *index, /*!< in: clustered index */ |
| 147 |
dtuple_t **row, /*!< out, own: partial row */ |
| 148 |
bool ignore_prefix, /*!< in: flag to indicate if we |
| 149 |
expect blob prefixes in undo. Used |
| 150 |
only in the assertion. */ |
| 151 |
mem_heap_t *heap); /*!< in: memory heap from which the memory |
| 152 |
needed is allocated */ |
| 153 |
/** Writes information to an undo log about an insert, update, or a delete |
| 154 |
marking of a clustered index record. This information is used in a rollback of |
| 155 |
the transaction and in consistent reads that must look to the history of this |
| 156 |
transaction. |
| 157 |
@return DB_SUCCESS or error code */ |
| 158 |
[[nodiscard]] dberr_t trx_undo_report_row_operation( |
| 159 |
ulint flags, /*!< in: if BTR_NO_UNDO_LOG_FLAG bit is |
| 160 |
set, does nothing */ |
| 161 |
ulint op_type, /*!< in: TRX_UNDO_INSERT_OP or |
| 162 |
TRX_UNDO_MODIFY_OP */ |
| 163 |
que_thr_t *thr, /*!< in: query thread */ |
| 164 |
dict_index_t *index, /*!< in: clustered index */ |
| 165 |
const dtuple_t *clust_entry, /*!< in: in the case of an insert, |
| 166 |
index entry to insert into the |
| 167 |
clustered index, otherwise NULL */ |
| 168 |
const upd_t *update, /*!< in: in the case of an update, |
| 169 |
the update vector, otherwise NULL */ |
| 170 |
ulint cmpl_info, /*!< in: compiler info on secondary |
| 171 |
index updates */ |
| 172 |
const rec_t *rec, /*!< in: case of an update or delete |
| 173 |
marking, the record in the clustered |
| 174 |
index, otherwise NULL */ |
| 175 |
const ulint *offsets, /*!< in: rec_get_offsets(rec) */ |
| 176 |
roll_ptr_t *roll_ptr); /*!< out: rollback pointer to the |
| 177 |
inserted undo log record, |
| 178 |
0 if BTR_NO_UNDO_LOG |
| 179 |
flag was specified */ |
| 180 |
|
| 181 |
/** status bit used for trx_undo_prev_version_build() */ |
| 182 |
|
| 183 |
/** TRX_UNDO_PREV_IN_PURGE tells trx_undo_prev_version_build() that it |
| 184 |
is being called purge view and we would like to get the purge record |
| 185 |
even it is in the purge view (in normal case, it will return without |
| 186 |
fetching the purge record */ |
| 187 |
constexpr uint32_t TRX_UNDO_PREV_IN_PURGE = 0x1; |
| 188 |
|
| 189 |
/** This tells trx_undo_prev_version_build() to fetch the old value in |
| 190 |
the undo log (which is the after image for an update) */ |
| 191 |
constexpr uint32_t TRX_UNDO_GET_OLD_V_VALUE = 0x2; |
| 192 |
|
| 193 |
/** Build a previous version of a clustered index record. The caller must hold |
| 194 |
a latch on the index page of the clustered index record. |
| 195 |
If the vrow passed to this function is not null, then this function will store |
| 196 |
information about virtual columns from the requested version in vrow, unless the |
| 197 |
change did not affect any secondary index nor ordering field of clustered index |
| 198 |
(the change has UPD_NODE_NO_ORD_CHANGE flag) in which case the requested |
| 199 |
information can not be reconstructed from undo log, and the caller may assume |
| 200 |
that the (virtual) columns of secondary index have the same values they have in |
| 201 |
the more recent version (the one `rec` comes from). |
| 202 |
Equivalently, if the vrow is not returned, it is either because it was not |
| 203 |
requested, or not available due to UPD_NODE_NO_ORD_CHANGE. |
| 204 |
Obviously vrow is also not set in case rec is the oldest version in history, |
| 205 |
in which case we also set old_vers to NULL. |
| 206 |
@param[in] index_rec clustered index record in the index tree |
| 207 |
@param[in] index_mtr mtr which contains the latch to index_rec page |
| 208 |
and purge_view |
| 209 |
@param[in] rec version of a clustered index record |
| 210 |
@param[in] index clustered index |
| 211 |
@param[in,out] offsets rec_get_offsets(rec, index) |
| 212 |
@param[in] heap memory heap from which the memory needed is |
| 213 |
allocated |
| 214 |
@param[out] old_vers previous version, or NULL if rec is the first |
| 215 |
inserted version, or if history data has been |
| 216 |
deleted |
| 217 |
@param[in] v_heap memory heap used to create vrow dtuple if it is |
| 218 |
not yet created. This heap diffs from "heap" |
| 219 |
above in that it could be |
| 220 |
prebuilt->old_vers_heap for selection |
| 221 |
@param[out] vrow virtual column info, if any |
| 222 |
@param[in] v_status status determine if it is going into this |
| 223 |
function by purge thread or not. And if we read |
| 224 |
"after image" of undo log has been rebuilt |
| 225 |
@param[in] lob_undo LOB undo information. |
| 226 |
@retval true if previous version was built, or if it was an insert or the table |
| 227 |
has been rebuilt |
| 228 |
@retval false if the previous version is earlier than purge_view, or being |
| 229 |
purged, which means that it may have been removed */ |
| 230 |
bool trx_undo_prev_version_build(const rec_t *index_rec, mtr_t *index_mtr, |
| 231 |
const rec_t *rec, const dict_index_t *index, |
| 232 |
ulint *offsets, mem_heap_t *heap, |
| 233 |
rec_t **old_vers, mem_heap_t *v_heap, |
| 234 |
const dtuple_t **vrow, ulint v_status, |
| 235 |
lob::undo_vers_t *lob_undo); |
| 236 |
|
| 237 |
#endif /* !UNIV_HOTBACKUP */ |
| 238 |
/** Parses a redo log record of adding an undo log record. |
| 239 |
@return end of log record or NULL */ |
| 240 |
byte *trx_undo_parse_add_undo_rec(byte *ptr, /*!< in: buffer */ |
| 241 |
byte *end_ptr, /*!< in: buffer end */ |
| 242 |
page_t *page); /*!< in: page or NULL */ |
| 243 |
/** Parses a redo log record of erasing of an undo page end. |
| 244 |
@param[in,out] ptr Buffer. |
| 245 |
@param[in,out] end_ptr Buffer end. |
| 246 |
@param[in,out] page Page or nullptr. |
| 247 |
@param[in,out] mtr MTR or nullptr. |
| 248 |
@return end of log record or nullptr */ |
| 249 |
byte *trx_undo_parse_erase_page_end(byte *ptr, byte *end_ptr, page_t *page, |
| 250 |
mtr_t *mtr); |
| 251 |
|
| 252 |
/** Read from an undo log record a non-virtual column value. |
| 253 |
@param[in,out] ptr pointer to remaining part of the undo record |
| 254 |
@param[in,out] field stored field |
| 255 |
@param[in,out] len length of the field, or UNIV_SQL_NULL |
| 256 |
@param[in,out] orig_len original length of the locally stored part |
| 257 |
of an externally stored column, or 0 |
| 258 |
@return remaining part of undo log record after reading these values */ |
| 259 |
byte *trx_undo_rec_get_col_val(const byte *ptr, const byte **field, ulint *len, |
| 260 |
ulint *orig_len); |
| 261 |
|
| 262 |
/** Read virtual column value from undo log |
| 263 |
@param[in] table the table |
| 264 |
@param[in] ptr undo log pointer |
| 265 |
@param[in,out] row the dtuple to fill |
| 266 |
@param[in] in_purge called by purge thread |
| 267 |
@param[in] online true if this is from online DDL log |
| 268 |
@param[in] col_map online rebuild column map |
| 269 |
@param[in,out] heap memory heap to keep value when necessary */ |
| 270 |
void trx_undo_read_v_cols(const dict_table_t *table, const byte *ptr, |
| 271 |
const dtuple_t *row, bool in_purge, bool online, |
| 272 |
const ulint *col_map, mem_heap_t *heap); |
| 273 |
|
| 274 |
/** Read virtual column index from undo log or online log if the log |
| 275 |
contains such info, and in the undo log case, verify the column is |
| 276 |
still indexed, and output its position |
| 277 |
@param[in] table the table |
| 278 |
@param[in] ptr undo log pointer |
| 279 |
@param[in] first_v_col if this is the first virtual column, which |
| 280 |
has the version marker |
| 281 |
@param[in,out] is_undo_log this function is used to parse both undo log, |
| 282 |
and online log for virtual columns. So |
| 283 |
check to see if this is undo log. When |
| 284 |
first_v_col is true, is_undo_log is output, |
| 285 |
when first_v_col is false, is_undo_log is input |
| 286 |
@param[in,out] field_no the column number |
| 287 |
@return remaining part of undo log record after reading these values */ |
| 288 |
const byte *trx_undo_read_v_idx(const dict_table_t *table, const byte *ptr, |
| 289 |
bool first_v_col, bool *is_undo_log, |
| 290 |
ulint *field_no); |
| 291 |
|
| 292 |
#ifndef UNIV_HOTBACKUP |
| 293 |
|
| 294 |
/* Types of an undo log record: these have to be smaller than 16, as the |
| 295 |
compilation info multiplied by 16 is ORed to this value in an undo log |
| 296 |
record */ |
| 297 |
|
| 298 |
/** fresh insert into clustered index */ |
| 299 |
constexpr uint32_t TRX_UNDO_INSERT_REC = 11; |
| 300 |
/** update of a non-delete-marked record */ |
| 301 |
constexpr uint32_t TRX_UNDO_UPD_EXIST_REC = 12; |
| 302 |
/** update of a delete marked record to a not delete marked record; also the |
| 303 |
fields of the record can change */ |
| 304 |
constexpr uint32_t TRX_UNDO_UPD_DEL_REC = 13; |
| 305 |
/* delete marking of a record; fields do not change */ |
| 306 |
constexpr uint32_t TRX_UNDO_DEL_MARK_REC = 14; |
| 307 |
/** compilation info is multiplied by this and ORed to the type above */ |
| 308 |
constexpr uint32_t TRX_UNDO_CMPL_INFO_MULT = 16; |
| 309 |
/** If this bit is set in type_cmpl, then the undo log record has support for |
| 310 |
partial update of BLOBs. Also to make the undo log format extensible, |
| 311 |
introducing a new flag next to the type_cmpl flag. */ |
| 312 |
constexpr uint32_t TRX_UNDO_MODIFY_BLOB = 64; |
| 313 |
/* This bit can be ORed to type_cmpl to denote that we updated external storage |
| 314 |
fields: used by purge to free the external storage */ |
| 315 |
constexpr uint32_t TRX_UNDO_UPD_EXTERN = 128; |
| 316 |
|
| 317 |
/** Operation type flags used in trx_undo_report_row_operation */ |
| 318 |
constexpr uint32_t TRX_UNDO_INSERT_OP = 1; |
| 319 |
constexpr uint32_t TRX_UNDO_MODIFY_OP = 2; |
| 320 |
|
| 321 |
/** The type and compilation info flag in the undo record for update. |
| 322 |
For easier understanding let the 8 bits be numbered as |
| 323 |
7, 6, 5, 4, 3, 2, 1, 0. */ |
| 324 |
struct type_cmpl_t { |
| 325 |
type_cmpl_t() : m_flag(0) {} |
| 326 |
|
| 327 |
const byte *read(const byte *ptr) { |
| 328 |
m_flag = mach_read_from_1(ptr); |
| 329 |
return (ptr + 1); |
| 330 |
} |
| 331 |
|
| 332 |
ulint type_info() { |
| 333 |
/* Get 0-3 */ |
| 334 |
return (m_flag & 0x0F); |
| 335 |
} |
| 336 |
|
| 337 |
ulint cmpl_info() { |
| 338 |
/* Get bits 5 and 4 */ |
| 339 |
return ((m_flag >> 4) & 0x03); |
| 340 |
} |
| 341 |
|
| 342 |
/** Is an LOB updated by this update operation. |
| 343 |
@return true if LOB is updated, false otherwise. */ |
| 344 |
bool is_lob_updated() { |
| 345 |
/* Check if bit 7 is set. */ |
| 346 |
return (m_flag & TRX_UNDO_UPD_EXTERN); |
| 347 |
} |
| 348 |
|
| 349 |
/** Does the undo log record contains information about LOB partial |
| 350 |
update vector. |
| 351 |
@return true if undo contains LOB update info. */ |
| 352 |
bool is_lob_undo() const { |
| 353 |
/* Check if bit 6 is set. */ |
| 354 |
return (m_flag & TRX_UNDO_MODIFY_BLOB); |
| 355 |
} |
| 356 |
|
| 357 |
private: |
| 358 |
uint8_t m_flag; |
| 359 |
}; |
| 360 |
|
| 361 |
/** Reads from an undo log record the general parameters. |
| 362 |
@return remaining part of undo log record after reading these values */ |
| 363 |
byte *trx_undo_rec_get_pars( |
| 364 |
trx_undo_rec_t *undo_rec, /*!< in: undo log record */ |
| 365 |
ulint *type, /*!< out: undo record type: |
| 366 |
TRX_UNDO_INSERT_REC, ... */ |
| 367 |
ulint *cmpl_info, /*!< out: compiler info, relevant only |
| 368 |
for update type records */ |
| 369 |
bool *updated_extern, /*!< out: true if we updated an |
| 370 |
externally stored fild */ |
| 371 |
undo_no_t *undo_no, /*!< out: undo log record number */ |
| 372 |
table_id_t *table_id, /*!< out: table id */ |
| 373 |
type_cmpl_t &type_cmpl); /*!< out: type compilation info. */ |
| 374 |
|
| 375 |
/** Get the max free space of undo log by assuming it's a fresh new page |
| 376 |
and the free space doesn't count for the undo log header too. */ |
| 377 |
size_t trx_undo_max_free_space(); |
| 378 |
|
| 379 |
/** Decide if the following undo log record is a multi-value virtual column |
| 380 |
@param[in] undo_rec undo log record |
| 381 |
@return true if this is a multi-value virtual column log, otherwise false */ |
| 382 |
bool trx_undo_rec_is_multi_value(const byte *undo_rec); |
| 383 |
|
| 384 |
/** Read from an undo log record of a multi-value virtual column. |
| 385 |
@param[in] ptr pointer to remaining part of the undo record |
| 386 |
@param[in,out] field stored field, nullptr if the col is no longer |
| 387 |
indexed or existing, in the latter case, |
| 388 |
this function will only skip the log |
| 389 |
@param[in,out] heap memory heap |
| 390 |
@return remaining part of undo log record after reading these values */ |
| 391 |
const byte *trx_undo_rec_get_multi_value(const byte *ptr, dfield_t *field, |
| 392 |
mem_heap_t *heap); |
| 393 |
#include "trx0rec.ic" |
| 394 |
|
| 395 |
#endif /* !UNIV_HOTBACKUP */ |
| 396 |
|
| 397 |
#endif /* trx0rec_h */ |