| 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/row0upd.h |
| 29 |
Update of a row |
| 30 |
|
| 31 |
Created 12/27/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef row0upd_h |
| 35 |
#define row0upd_h |
| 36 |
|
| 37 |
#include <stack> |
| 38 |
#include "btr0types.h" |
| 39 |
#include "data0data.h" |
| 40 |
#include "dict0types.h" |
| 41 |
#include "lob0lob.h" |
| 42 |
#include "row0types.h" |
| 43 |
#include "table.h" |
| 44 |
#include "trx0types.h" |
| 45 |
#include "univ.i" |
| 46 |
|
| 47 |
#include "btr0pcur.h" |
| 48 |
#ifndef UNIV_HOTBACKUP |
| 49 |
#include "pars0types.h" |
| 50 |
#include "que0types.h" |
| 51 |
#endif /* !UNIV_HOTBACKUP */ |
| 52 |
|
| 53 |
/** Creates an update vector object. |
| 54 |
@param[in] n number of fields |
| 55 |
@param[in] heap heap from which memory allocated |
| 56 |
@return own: update vector object */ |
| 57 |
inline upd_t *upd_create(ulint n, mem_heap_t *heap); |
| 58 |
|
| 59 |
/** Returns the number of fields in the update vector == number of columns |
| 60 |
to be updated by an update vector. |
| 61 |
@param[in] update update vector |
| 62 |
@return number of fields */ |
| 63 |
inline ulint upd_get_n_fields(const upd_t *update); |
| 64 |
|
| 65 |
/** Returns the nth field of an update vector. |
| 66 |
@param[in] update update vector |
| 67 |
@param[in] n field position in update vector |
| 68 |
@return update vector field */ |
| 69 |
inline upd_field_t *upd_get_nth_field(upd_t *update, ulint n); |
| 70 |
|
| 71 |
/** Returns the nth field of an update vector. |
| 72 |
@param[in] update update vector |
| 73 |
@param[in] n field position in update vector |
| 74 |
@return update vector field */ |
| 75 |
inline const upd_field_t *upd_get_nth_field(const upd_t *update, ulint n); |
| 76 |
|
| 77 |
/** Set which index field is updated by an update vector field. |
| 78 |
@param[in] upd_field update vector field |
| 79 |
@param[in] field_no field position in index |
| 80 |
@param[in] index index */ |
| 81 |
inline void upd_field_set_field_no(upd_field_t *upd_field, ulint field_no, |
| 82 |
const dict_index_t *index); |
| 83 |
|
| 84 |
/** Set for which virtual column the value is updated by an update vector |
| 85 |
field. |
| 86 |
@param[in,out] upd_field update vector field |
| 87 |
@param[in] field_no virtual column number in table |
| 88 |
@param[in] index index */ |
| 89 |
inline void upd_field_set_v_field_no(upd_field_t *upd_field, ulint field_no, |
| 90 |
const dict_index_t *index); |
| 91 |
|
| 92 |
/** Returns a field of an update vector by field_no. |
| 93 |
@param[in] update Update vector. |
| 94 |
@param[in] no "Field number" as stored in the update vector: |
| 95 |
when is_virtual is false the position of the field |
| 96 |
in the updated index, otherwise the column number |
| 97 |
of the virtual field in table. @see upd_field::field_no |
| 98 |
@param[in] is_virtual If it is a virtual column. |
| 99 |
@return update vector field, or nullptr. */ |
| 100 |
[[nodiscard]] inline const upd_field_t *upd_get_field_by_field_no( |
| 101 |
const upd_t *update, ulint no, bool is_virtual); |
| 102 |
|
| 103 |
/** Writes into the redo log the values of trx id and roll ptr and enough info |
| 104 |
to determine their positions within a clustered index record. |
| 105 |
@param[in] index Clustered index. |
| 106 |
@param[in] trx_id Transaction ID. |
| 107 |
@param[in] roll_ptr Roll ptr of the undo log record. |
| 108 |
@param[in] log_ptr Pointer to a buffer of size > 20 opened in mlog. |
| 109 |
@param[in] mtr Mini-transaction. |
| 110 |
@return new pointer to mlog */ |
| 111 |
byte *row_upd_write_sys_vals_to_log(dict_index_t *index, trx_id_t trx_id, |
| 112 |
roll_ptr_t roll_ptr, byte *log_ptr, |
| 113 |
mtr_t *mtr); |
| 114 |
|
| 115 |
#ifndef UNIV_HOTBACKUP |
| 116 |
/** Updates the trx id and roll ptr field in a clustered index record when a |
| 117 |
row is updated or marked deleted. |
| 118 |
@param[in,out] rec record |
| 119 |
@param[in,out] page_zip compressed page whose uncompressed part will |
| 120 |
be updated, or NULL |
| 121 |
@param[in] index clustered index |
| 122 |
@param[in] offsets rec_get_offsets(rec, index) |
| 123 |
@param[in] trx transaction |
| 124 |
@param[in] roll_ptr roll ptr of the undo log record, can be 0 |
| 125 |
during IMPORT */ |
| 126 |
inline void row_upd_rec_sys_fields(rec_t *rec, page_zip_des_t *page_zip, |
| 127 |
const dict_index_t *index, |
| 128 |
const ulint *offsets, const trx_t *trx, |
| 129 |
roll_ptr_t roll_ptr); |
| 130 |
#endif /* !UNIV_HOTBACKUP */ |
| 131 |
|
| 132 |
/** Sets the trx id or roll ptr field of a clustered index entry. |
| 133 |
@param[in,out] entry Index entry, where the memory buffers for sys fields are |
| 134 |
already allocated: the function just copies the new values to them |
| 135 |
@param[in] index Clustered index |
| 136 |
@param[in] type Data_trx_id or data_roll_ptr |
| 137 |
@param[in] val Value to write */ |
| 138 |
void row_upd_index_entry_sys_field(dtuple_t *entry, dict_index_t *index, |
| 139 |
ulint type, uint64_t val); |
| 140 |
|
| 141 |
/** Creates an update node for a query graph. |
| 142 |
@return own: update node */ |
| 143 |
upd_node_t *upd_node_create( |
| 144 |
mem_heap_t *heap); /*!< in: mem heap where created */ |
| 145 |
|
| 146 |
/** Writes to the redo log the new values of the fields occurring in the index. |
| 147 |
@param[in] index index which to be updated |
| 148 |
@param[in] update update vector |
| 149 |
@param[in] log_ptr pointer to mlog buffer: must contain at least |
| 150 |
MLOG_BUF_MARGIN bytes of free space; the buffer |
| 151 |
is closed within this function |
| 152 |
@param[in] mtr mtr into whose log to write */ |
| 153 |
void row_upd_index_write_log(dict_index_t *index, const upd_t *update, |
| 154 |
byte *log_ptr, mtr_t *mtr); |
| 155 |
|
| 156 |
/** Returns true if row update changes size of some field in index or if some |
| 157 |
field to be updated is stored externally in rec or update. |
| 158 |
@return true if the update changes the size of some field in index or |
| 159 |
the field is external in rec or update */ |
| 160 |
bool row_upd_changes_field_size_or_external( |
| 161 |
const dict_index_t *index, /*!< in: index */ |
| 162 |
const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */ |
| 163 |
const upd_t *update); /*!< in: update vector */ |
| 164 |
/** Returns true if row update contains disowned external fields. |
| 165 |
@return true if the update contains disowned external fields. */ |
| 166 |
[[nodiscard]] bool row_upd_changes_disowned_external( |
| 167 |
const upd_t *update); /*!< in: update vector */ |
| 168 |
/** Replaces the new column values stored in the update vector to the |
| 169 |
record given. No field size changes are allowed. This function is |
| 170 |
usually invoked on a clustered index. The only use case for a |
| 171 |
secondary index is row_ins_sec_index_entry_by_modify() or its |
| 172 |
counterpart in ibuf_insert_to_index_page(). */ |
| 173 |
void row_upd_rec_in_place( |
| 174 |
rec_t *rec, /*!< in/out: record where replaced */ |
| 175 |
const dict_index_t *index, /*!< in: the index the record belongs to */ |
| 176 |
const ulint *offsets, /*!< in: array returned by rec_get_offsets() */ |
| 177 |
const upd_t *update, /*!< in: update vector */ |
| 178 |
page_zip_des_t *page_zip); /*!< in: compressed page with enough space |
| 179 |
available, or NULL */ |
| 180 |
/** Builds an update vector from those fields which in a secondary index entry |
| 181 |
differ from a record that has the equal ordering fields. NOTE: we compare |
| 182 |
the fields as binary strings! |
| 183 |
@return own: update vector of differing fields */ |
| 184 |
[[nodiscard]] upd_t *row_upd_build_sec_rec_difference_binary( |
| 185 |
const rec_t *rec, /*!< in: secondary index record */ |
| 186 |
dict_index_t *index, /*!< in: index */ |
| 187 |
const ulint *offsets, /*!< in: rec_get_offsets(rec, index) */ |
| 188 |
const dtuple_t *entry, /*!< in: entry to insert */ |
| 189 |
mem_heap_t *heap); /*!< in: memory heap from which allocated */ |
| 190 |
/** Builds an update vector from those fields, excluding the roll ptr and |
| 191 |
trx id fields, which in an index entry differ from a record that has |
| 192 |
the equal ordering fields. NOTE: we compare the fields as binary strings! |
| 193 |
@param[in] index clustered index |
| 194 |
@param[in] entry clustered index entry to insert |
| 195 |
@param[in] rec clustered index record |
| 196 |
@param[in] offsets rec_get_offsets(rec,index), or NULL |
| 197 |
@param[in] no_sys skip the system columns |
| 198 |
DB_TRX_ID and DB_ROLL_PTR |
| 199 |
@param[in] trx transaction (for diagnostics), |
| 200 |
or NULL |
| 201 |
@param[in] heap memory heap from which allocated |
| 202 |
@param[in] mysql_table NULL, or mysql table object when |
| 203 |
user thread invokes dml |
| 204 |
@param[out] error error number in case of failure |
| 205 |
@return own: update vector of differing fields, excluding roll ptr and |
| 206 |
trx id */ |
| 207 |
[[nodiscard]] upd_t *row_upd_build_difference_binary( |
| 208 |
dict_index_t *index, const dtuple_t *entry, const rec_t *rec, |
| 209 |
const ulint *offsets, bool no_sys, trx_t *trx, mem_heap_t *heap, |
| 210 |
TABLE *mysql_table, dberr_t *error); |
| 211 |
|
| 212 |
/** Replaces the new column values stored in the update vector to the index |
| 213 |
entry given. |
| 214 |
@param[in,out] entry Index entry where replaced; the clustered index record must |
| 215 |
be covered by a lock or a page latch to prevent deletion [rollback or purge] |
| 216 |
@param[in] index Index; note that this may also be a non-clustered index |
| 217 |
@param[in] update An update vector built for the index so that the field number |
| 218 |
in an upd_field is the index position |
| 219 |
@param[in] order_only If true, limit the replacement to ordering fields of |
| 220 |
index; note that this does not work for non-clustered indexes. |
| 221 |
@param[in] heap Memory heap for allocating and copying the new values */ |
| 222 |
void row_upd_index_replace_new_col_vals_index_pos(dtuple_t *entry, |
| 223 |
const dict_index_t *index, |
| 224 |
const upd_t *update, |
| 225 |
bool order_only, |
| 226 |
mem_heap_t *heap); |
| 227 |
|
| 228 |
/** Replaces the new column values stored in the update vector to the index |
| 229 |
entry given. |
| 230 |
@param[in,out] entry Index entry where replaced; the clustered index record must |
| 231 |
be covered by a lock or a page latch to prevent deletion (rollback or purge) |
| 232 |
@param[in] index Index; note that this may also be a non-clustered index |
| 233 |
@param[in] update An update vector built for the clustered index so that the |
| 234 |
field number in an upd_field is the clustered index position |
| 235 |
@param[in] heap Memory heap for allocating and copying the new values */ |
| 236 |
void row_upd_index_replace_new_col_vals(dtuple_t *entry, |
| 237 |
const dict_index_t *index, |
| 238 |
const upd_t *update, mem_heap_t *heap); |
| 239 |
|
| 240 |
/** Replaces the new column values stored in the update vector. |
| 241 |
@param[in,out] row Row where replaced, indexed by col_no; the clustered index |
| 242 |
record must be covered by a lock or a page latch to prevent deletion (rollback |
| 243 |
or purge) |
| 244 |
@param[in,out] ext Null, or externally stored column prefixes |
| 245 |
@param[in] index Clustered index |
| 246 |
@param[in] update An update vector built for the clustered index |
| 247 |
@param[in] heap Memory heap */ |
| 248 |
void row_upd_replace(dtuple_t *row, row_ext_t **ext, const dict_index_t *index, |
| 249 |
const upd_t *update, mem_heap_t *heap); |
| 250 |
|
| 251 |
/** Replaces the virtual column values stored in a dtuple with that of |
| 252 |
a update vector. |
| 253 |
@param[in,out] row dtuple whose column to be updated |
| 254 |
@param[in] table table |
| 255 |
@param[in] update an update vector built for the clustered index |
| 256 |
@param[in] upd_new update to new or old value |
| 257 |
@param[in,out] undo_row undo row (if needs to be updated) |
| 258 |
@param[in] ptr remaining part in update undo log */ |
| 259 |
void row_upd_replace_vcol(dtuple_t *row, const dict_table_t *table, |
| 260 |
const upd_t *update, bool upd_new, dtuple_t *undo_row, |
| 261 |
const byte *ptr); |
| 262 |
|
| 263 |
/** Checks if an update vector changes an ordering field of an index record. |
| 264 |
It will also help check if any non-multi-value field on the multi-value index |
| 265 |
gets updated or not. |
| 266 |
|
| 267 |
This function is fast if the update vector is short or the number of ordering |
| 268 |
fields in the index is small. Otherwise, this can be quadratic. |
| 269 |
NOTE: we compare the fields as binary strings! |
| 270 |
@param[in] index index of the record |
| 271 |
@param[in] update update vector for the row; NOTE: the |
| 272 |
field numbers in this MUST be clustered index |
| 273 |
positions! |
| 274 |
@param[in] thr query thread, or NULL |
| 275 |
@param[in] row old value of row, or NULL if the |
| 276 |
row and the data values in update are not |
| 277 |
known when this function is called, e.g., at |
| 278 |
compile time |
| 279 |
@param[in] ext NULL, or prefixes of the externally |
| 280 |
stored columns in the old row |
| 281 |
@param[in,out] non_mv_upd NULL, or not NULL pointer to get the |
| 282 |
information about whether any non-multi-value |
| 283 |
field on the multi-value index gets updated |
| 284 |
@param[in] flag ROW_BUILD_NORMAL, ROW_BUILD_FOR_PURGE or |
| 285 |
ROW_BUILD_FOR_UNDO |
| 286 |
@return true if update vector changes an ordering field in the index record */ |
| 287 |
[[nodiscard]] bool row_upd_changes_ord_field_binary_func( |
| 288 |
dict_index_t *index, const upd_t *update, |
| 289 |
IF_DEBUG(const que_thr_t *thr, ) const dtuple_t *row, const row_ext_t *ext, |
| 290 |
bool *non_mv_upd, ulint flag); |
| 291 |
|
| 292 |
static inline bool row_upd_changes_ord_field_binary( |
| 293 |
dict_index_t *index, const upd_t *update, |
| 294 |
const que_thr_t *thr [[maybe_unused]], const dtuple_t *row, |
| 295 |
const row_ext_t *ext, bool *non_mv_upd) { |
| 296 |
return row_upd_changes_ord_field_binary_func( |
| 297 |
index, update, IF_DEBUG(thr, ) row, ext, non_mv_upd, 0); |
| 298 |
} |
| 299 |
|
| 300 |
/** Checks if an FTS indexed column is affected by an UPDATE. |
| 301 |
@param[in] table table |
| 302 |
@param[in] upd_field clustered index field update |
| 303 |
@return offset within fts_t::indexes if FTS indexed column updated else |
| 304 |
ULINT_UNDEFINED */ |
| 305 |
ulint row_upd_changes_fts_column(dict_table_t *table, upd_field_t *upd_field); |
| 306 |
/** Checks if an FTS Doc ID column is affected by an UPDATE. |
| 307 |
@return whether Doc ID column is affected */ |
| 308 |
[[nodiscard]] bool row_upd_changes_doc_id( |
| 309 |
dict_table_t *table, /*!< in: table */ |
| 310 |
upd_field_t *upd_field); /*!< in: field to check */ |
| 311 |
/** Checks if an update vector changes an ordering field of an index record. |
| 312 |
This function is fast if the update vector is short or the number of ordering |
| 313 |
fields in the index is small. Otherwise, this can be quadratic. |
| 314 |
NOTE: we compare the fields as binary strings! |
| 315 |
@return true if update vector may change an ordering field in an index |
| 316 |
record */ |
| 317 |
bool row_upd_changes_some_index_ord_field_binary( |
| 318 |
const dict_table_t *table, /*!< in: table */ |
| 319 |
const upd_t *update); /*!< in: update vector for the row */ |
| 320 |
|
| 321 |
/** Stores to the heap the row on which the node->pcur is positioned. |
| 322 |
@param[in] node row update node |
| 323 |
@param[in] thd mysql thread handle |
| 324 |
@param[in,out] mysql_table NULL, or mysql table object when |
| 325 |
user thread invokes dml */ |
| 326 |
void row_upd_store_row(upd_node_t *node, THD *thd, TABLE *mysql_table); |
| 327 |
|
| 328 |
/** Updates a row in a table. This is a high-level function used |
| 329 |
in SQL execution graphs. |
| 330 |
@return query thread to run next or NULL */ |
| 331 |
que_thr_t *row_upd_step(que_thr_t *thr); /*!< in: query thread */ |
| 332 |
/** Parses the log data of system field values. |
| 333 |
@return log data end or NULL */ |
| 334 |
byte *row_upd_parse_sys_vals(const byte *ptr, /*!< in: buffer */ |
| 335 |
const byte *end_ptr, /*!< in: buffer end */ |
| 336 |
ulint *pos, /*!< out: TRX_ID position in record */ |
| 337 |
trx_id_t *trx_id, /*!< out: trx id */ |
| 338 |
roll_ptr_t *roll_ptr); /*!< out: roll ptr */ |
| 339 |
|
| 340 |
/** Updates the trx id and roll ptr field in a clustered index record in |
| 341 |
database recovery. |
| 342 |
@param[in,out] rec Record |
| 343 |
@param[in,out] page_zip Compressed page, or null |
| 344 |
@param[in] offsets Array returned by rec_get_offsets() |
| 345 |
@param[in] pos Trx_id position in rec |
| 346 |
@param[in] trx_id Transaction id |
| 347 |
@param[in] roll_ptr Roll ptr of the undo log record */ |
| 348 |
void row_upd_rec_sys_fields_in_recovery(rec_t *rec, page_zip_des_t *page_zip, |
| 349 |
const ulint *offsets, ulint pos, |
| 350 |
trx_id_t trx_id, roll_ptr_t roll_ptr); |
| 351 |
|
| 352 |
/** Parses the log data written by row_upd_index_write_log. |
| 353 |
@param[in] ptr buffer |
| 354 |
@param[in] end_ptr buffer end |
| 355 |
@param[in] heap memory heap where update vector is built |
| 356 |
@param[out] update_out update vector |
| 357 |
@param[in] index the index corresponding to the update |
| 358 |
@return log data end or NULL */ |
| 359 |
byte *row_upd_index_parse(const byte *ptr, const byte *end_ptr, |
| 360 |
mem_heap_t *heap, upd_t **update_out, |
| 361 |
dict_index_t *index); |
| 362 |
|
| 363 |
/** Get the new autoinc counter from the update vector when there is |
| 364 |
an autoinc field defined in this table. |
| 365 |
@param[in] update update vector for the clustered index |
| 366 |
@param[in] autoinc_field_no autoinc field's order in clustered index |
| 367 |
@return the new counter if we find it in the update vector, otherwise 0. |
| 368 |
We don't mind that the new counter happens to be 0, we just care about |
| 369 |
non-zero counters. */ |
| 370 |
uint64_t row_upd_get_new_autoinc_counter(const upd_t *update, |
| 371 |
ulint autoinc_field_no); |
| 372 |
|
| 373 |
/** This structure is used for undo logging of LOB index changes. */ |
| 374 |
struct lob_index_diff_t { |
| 375 |
trx_id_t m_modifier_trxid; |
| 376 |
undo_no_t m_modifier_undo_no; |
| 377 |
|
| 378 |
/** Print the current object into the given output stream. |
| 379 |
@param[in,out] out the output stream. |
| 380 |
@return the output stream. */ |
| 381 |
std::ostream &print(std::ostream &out) const { |
| 382 |
out << "[lob_index_diff_t: m_modifier_trxid=" << m_modifier_trxid |
| 383 |
<< ", m_modifier_undo_no=" << m_modifier_undo_no << "]"; |
| 384 |
return (out); |
| 385 |
} |
| 386 |
}; |
| 387 |
|
| 388 |
using Lob_index_diff_vec = |
| 389 |
std::vector<lob_index_diff_t, mem_heap_allocator<lob_index_diff_t>>; |
| 390 |
|
| 391 |
/** Overloading the global output operator to print lob_index_diff_t object. |
| 392 |
@param[in,out] out the output stream. |
| 393 |
@param[in] obj the object to be printed. |
| 394 |
@return the output stream.*/ |
| 395 |
inline std::ostream &operator<<(std::ostream &out, |
| 396 |
const lob_index_diff_t &obj) { |
| 397 |
return (obj.print(out)); |
| 398 |
} |
| 399 |
|
| 400 |
/** The modification done to the LOB. */ |
| 401 |
struct Lob_diff { |
| 402 |
/** Constructor. |
| 403 |
@param[in] mem_heap the memory heap in which this object |
| 404 |
has been created. */ |
| 405 |
Lob_diff(mem_heap_t *mem_heap) : heap(mem_heap) { |
| 406 |
m_idx_diffs = static_cast<Lob_index_diff_vec *>( |
| 407 |
mem_heap_alloc(heap, sizeof(Lob_index_diff_vec))); |
| 408 |
new (m_idx_diffs) |
| 409 |
Lob_index_diff_vec(mem_heap_allocator<lob_index_diff_t>(heap)); |
| 410 |
} |
| 411 |
|
| 412 |
/** Read the offset from the undo record. |
| 413 |
@param[in] undo_ptr pointer into the undo log record. |
| 414 |
@return pointer into the undo log record after offset. */ |
| 415 |
const byte *read_offset(const byte *undo_ptr) { |
| 416 |
/* Read the offset. */ |
| 417 |
m_offset = mach_read_next_compressed(&undo_ptr); |
| 418 |
return (undo_ptr); |
| 419 |
} |
| 420 |
|
| 421 |
/** Read the length from the undo record. |
| 422 |
@param[in] undo_ptr pointer into the undo log record. |
| 423 |
@return pointer into the undo log record after length information. */ |
| 424 |
const byte *read_length(const byte *undo_ptr) { |
| 425 |
/* Read the length. */ |
| 426 |
m_length = mach_read_next_compressed(&undo_ptr); |
| 427 |
ut_ad(m_length <= lob::ref_t::LOB_SMALL_CHANGE_THRESHOLD); |
| 428 |
|
| 429 |
return (undo_ptr); |
| 430 |
} |
| 431 |
|
| 432 |
void set_old_data(const byte *undo_ptr) { m_old_data = undo_ptr; } |
| 433 |
|
| 434 |
std::ostream &print(std::ostream &out) const { |
| 435 |
out << "[Lob_diff: offset=" << m_offset << ", length=" << m_length; |
| 436 |
if (m_old_data == nullptr) { |
| 437 |
out << ", m_old_data=nullptr"; |
| 438 |
} else { |
| 439 |
out << ", m_old_data=" << PrintBuffer(m_old_data, m_length); |
| 440 |
} |
| 441 |
|
| 442 |
if (m_idx_diffs != nullptr) { |
| 443 |
for (auto iter = m_idx_diffs->begin(); iter != m_idx_diffs->end(); |
| 444 |
++iter) { |
| 445 |
out << *iter; |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
out << "]"; |
| 450 |
return (out); |
| 451 |
} |
| 452 |
|
| 453 |
/** The offset within LOB where partial update happened. */ |
| 454 |
ulint m_offset = 0; |
| 455 |
|
| 456 |
/** The length of the modification. */ |
| 457 |
ulint m_length = 0; |
| 458 |
|
| 459 |
/** Changes to the LOB data. */ |
| 460 |
const byte *m_old_data = nullptr; |
| 461 |
|
| 462 |
/** Changes to the LOB index. */ |
| 463 |
Lob_index_diff_vec *m_idx_diffs; |
| 464 |
|
| 465 |
/** Memory heap in which this object is allocated. */ |
| 466 |
mem_heap_t *heap; |
| 467 |
}; |
| 468 |
|
| 469 |
using Lob_diff_vector = std::vector<Lob_diff, mem_heap_allocator<Lob_diff>>; |
| 470 |
|
| 471 |
inline std::ostream &operator<<(std::ostream &out, const Lob_diff &obj) { |
| 472 |
return (obj.print(out)); |
| 473 |
} |
| 474 |
|
| 475 |
/* Update vector field */ |
| 476 |
struct upd_field_t { |
| 477 |
upd_field_t() |
| 478 |
: field_no(0), |
| 479 |
orig_len(0), |
| 480 |
exp(nullptr), |
| 481 |
old_v_val(nullptr), |
| 482 |
mysql_field(nullptr), |
| 483 |
ext_in_old(false), |
| 484 |
lob_diffs(nullptr), |
| 485 |
lob_first_page_no(FIL_NULL), |
| 486 |
lob_version(0), |
| 487 |
last_trx_id(0), |
| 488 |
last_undo_no(0), |
| 489 |
heap(nullptr) {} |
| 490 |
|
| 491 |
bool is_virtual() const { return (new_val.is_virtual()); } |
| 492 |
|
| 493 |
unsigned field_no : 16; /*!< for non-virtual fields, the field |
| 494 |
position in an index, usually |
| 495 |
the clustered index, but in updating |
| 496 |
a secondary index record in btr0cur.cc |
| 497 |
this is the position in the secondary |
| 498 |
index; for virtual fields the number |
| 499 |
of correcponding virtual column in |
| 500 |
table metadata */ |
| 501 |
IF_DEBUG(uint16_t field_phy_pos{UINT16_MAX};) |
| 502 |
unsigned orig_len : 16; /*!< original length of the locally |
| 503 |
stored part of an externally stored |
| 504 |
column, or 0 */ |
| 505 |
que_node_t *exp; /*!< expression for calculating a new |
| 506 |
value: it refers to column values and |
| 507 |
constants in the symbol table of the |
| 508 |
query graph */ |
| 509 |
dfield_t old_val; /*!< old value for the column */ |
| 510 |
dfield_t new_val; /*!< new value for the column */ |
| 511 |
dfield_t *old_v_val; /*!< old value for the virtual column */ |
| 512 |
|
| 513 |
Field *mysql_field; /*!< the mysql field object. */ |
| 514 |
|
| 515 |
/** If true, the field was stored externally in the old row. */ |
| 516 |
bool ext_in_old; |
| 517 |
|
| 518 |
void push_lob_diff(const Lob_diff &lob_diff) { |
| 519 |
if (lob_diffs == nullptr) { |
| 520 |
lob_diffs = static_cast<Lob_diff_vector *>( |
| 521 |
mem_heap_alloc(heap, sizeof(Lob_diff_vector))); |
| 522 |
new (lob_diffs) Lob_diff_vector(mem_heap_allocator<Lob_diff>(heap)); |
| 523 |
} |
| 524 |
lob_diffs->push_back(lob_diff); |
| 525 |
} |
| 526 |
|
| 527 |
/** List of changes done to this updated field. This is usually |
| 528 |
populated from the undo log. */ |
| 529 |
Lob_diff_vector *lob_diffs; |
| 530 |
|
| 531 |
/** The LOB first page number. This information is read from |
| 532 |
the undo log. */ |
| 533 |
page_no_t lob_first_page_no; |
| 534 |
|
| 535 |
ulint lob_version; |
| 536 |
|
| 537 |
/** The last trx that modified the LOB. */ |
| 538 |
trx_id_t last_trx_id; |
| 539 |
|
| 540 |
/** The last stmt within trx that modified the LOB. */ |
| 541 |
undo_no_t last_undo_no; |
| 542 |
|
| 543 |
std::ostream &print(std::ostream &out) const; |
| 544 |
|
| 545 |
/** Empty the information collected on LOB diffs. */ |
| 546 |
void reset() { |
| 547 |
if (lob_diffs != nullptr) { |
| 548 |
lob_diffs->clear(); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
/** Memory heap in which this object is allocated. */ |
| 553 |
mem_heap_t *heap; |
| 554 |
}; |
| 555 |
|
| 556 |
inline std::ostream &operator<<(std::ostream &out, const upd_field_t &obj) { |
| 557 |
return (obj.print(out)); |
| 558 |
} |
| 559 |
|
| 560 |
/* check whether an update field is on multi-value virtual column */ |
| 561 |
inline bool upd_fld_is_multi_value_col(const upd_field_t *upd_fld) { |
| 562 |
return dfield_is_multi_value(&upd_fld->new_val); |
| 563 |
} |
| 564 |
|
| 565 |
/* set DATA_VIRTUAL bit on update field to show it is a virtual column */ |
| 566 |
inline void upd_fld_set_virtual_col(upd_field_t *upd_fld) { |
| 567 |
upd_fld->new_val.type.prtype |= DATA_VIRTUAL; |
| 568 |
} |
| 569 |
|
| 570 |
/* Update vector structure */ |
| 571 |
struct upd_t { |
| 572 |
/** Heap from which memory allocated. This is not a new heap, rather |
| 573 |
will point to other heap. Therefore memory allocated from this heap |
| 574 |
is released when the pointed heap is freed or emptied. */ |
| 575 |
mem_heap_t *heap; |
| 576 |
|
| 577 |
/** Heap from which memory is allocated if required only for current |
| 578 |
statement. This heap is emtied at the end of statement from inside |
| 579 |
ha_innobase::end_stmt(). */ |
| 580 |
mem_heap_t *per_stmt_heap; |
| 581 |
|
| 582 |
/** New value of info bits to record; default is 0. */ |
| 583 |
ulint info_bits; |
| 584 |
|
| 585 |
/** Pointer to old row, used for virtual column update now. */ |
| 586 |
dtuple_t *old_vrow; |
| 587 |
|
| 588 |
/** The table object. */ |
| 589 |
const dict_table_t *table; |
| 590 |
|
| 591 |
/** The mysql table object. */ |
| 592 |
TABLE *mysql_table; |
| 593 |
|
| 594 |
/** Number of update fields. */ |
| 595 |
ulint n_fields; |
| 596 |
|
| 597 |
/** Array of update fields. */ |
| 598 |
upd_field_t *fields; |
| 599 |
|
| 600 |
/** Capacity of the fields array */ |
| 601 |
size_t n_capacity; |
| 602 |
|
| 603 |
/** Append an update field to the end of array. If fields array is full, more |
| 604 |
space is allocated on the heap. |
| 605 |
@param[in] field an update field */ |
| 606 |
void append(const upd_field_t &field); |
| 607 |
|
| 608 |
/** Determine if the given field_no is modified. |
| 609 |
@return true if modified, false otherwise. */ |
| 610 |
bool is_modified(const ulint field_no) const { |
| 611 |
if (table == nullptr) { |
| 612 |
ut_d(ut_error); |
| 613 |
ut_o(return false); |
| 614 |
} |
| 615 |
|
| 616 |
return (get_field_by_field_no(field_no, table->first_index()) != nullptr); |
| 617 |
} |
| 618 |
|
| 619 |
/** Reset the update fields. */ |
| 620 |
void reset() { |
| 621 |
for (ulint i = 0; i < n_fields; ++i) { |
| 622 |
fields[i].reset(); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
#ifdef UNIV_DEBUG |
| 627 |
void validate() const { |
| 628 |
for (ulint i = 0; i < n_fields; ++i) { |
| 629 |
dfield_t *field = &fields[i].new_val; |
| 630 |
if (dfield_is_ext(field)) { |
| 631 |
ut_ad(dfield_get_len(field) >= BTR_EXTERN_FIELD_REF_SIZE); |
| 632 |
} |
| 633 |
} |
| 634 |
} |
| 635 |
|
| 636 |
void validate_for_index(const dict_index_t *index) const { |
| 637 |
validate(); |
| 638 |
for (ulint i = 0; i < n_fields; ++i) { |
| 639 |
const upd_field_t &field = fields[i]; |
| 640 |
ut_a(index->is_clustered() || !field.is_virtual()); |
| 641 |
if (!field.is_virtual()) { |
| 642 |
ut_a(field.field_no < dict_index_get_n_fields(index)); |
| 643 |
} else { |
| 644 |
ut_a(field.field_no < index->table->n_v_cols); |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
#endif // UNIV_DEBUG |
| 649 |
|
| 650 |
/** Check if the given field number is partially updated. |
| 651 |
@param[in] field_no the field number. |
| 652 |
@return true if partially updated, false otherwise. */ |
| 653 |
bool is_partially_updated(ulint field_no) const; |
| 654 |
|
| 655 |
const upd_field_t *get_field_by_field_no(ulint field_no, |
| 656 |
const dict_index_t *index) const; |
| 657 |
|
| 658 |
const Binary_diff_vector *get_binary_diff_by_field_no(ulint field_no) const; |
| 659 |
|
| 660 |
/** Calculate the total number of bytes modified in one BLOB. |
| 661 |
@param[in] bdv the binary diff vector containing all the |
| 662 |
modifications to one BLOB. |
| 663 |
@return the total modified bytes. */ |
| 664 |
static size_t get_total_modified_bytes(const Binary_diff_vector &bdv) { |
| 665 |
size_t total = 0; |
| 666 |
for (const Binary_diff &bdiff : bdv) { |
| 667 |
total += bdiff.length(); |
| 668 |
} |
| 669 |
return (total); |
| 670 |
} |
| 671 |
|
| 672 |
/** Empty the per_stmt_heap. */ |
| 673 |
void empty_per_stmt_heap() { |
| 674 |
if (per_stmt_heap != nullptr) { |
| 675 |
mem_heap_empty(per_stmt_heap); |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
/** Free the per_stmt_heap. */ |
| 680 |
void free_per_stmt_heap() { |
| 681 |
if (per_stmt_heap != nullptr) { |
| 682 |
mem_heap_free(per_stmt_heap); |
| 683 |
per_stmt_heap = nullptr; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
std::ostream &print(std::ostream &out) const; |
| 688 |
|
| 689 |
/** Print the partial update vector (puvect) of the given update |
| 690 |
field. |
| 691 |
@param[in,out] out the output stream |
| 692 |
@param[in] uf the updated field. |
| 693 |
@return the output stream. */ |
| 694 |
std::ostream &print_puvect(std::ostream &out, upd_field_t *uf) const; |
| 695 |
}; |
| 696 |
|
| 697 |
#ifdef UNIV_DEBUG |
| 698 |
/** Print the given binary diff into the given output stream. |
| 699 |
@param[in] out the output stream |
| 700 |
@param[in] bdiff binary diff to be printed. |
| 701 |
@param[in] table the table dictionary object. |
| 702 |
@param[in] field mysql field object. |
| 703 |
@param[in] print_old prints old data of the updated field |
| 704 |
@return the output stream */ |
| 705 |
std::ostream &print_binary_diff(std::ostream &out, const Binary_diff *bdiff, |
| 706 |
const dict_table_t *table, const Field *field, |
| 707 |
bool print_old); |
| 708 |
|
| 709 |
std::ostream &print_binary_diff(std::ostream &out, const Binary_diff *bdiff); |
| 710 |
|
| 711 |
inline std::ostream &operator<<(std::ostream &out, const upd_t &obj) { |
| 712 |
return (obj.print(out)); |
| 713 |
} |
| 714 |
|
| 715 |
inline std::ostream &operator<<(std::ostream &out, const Binary_diff_vector &) { |
| 716 |
return (out); |
| 717 |
} |
| 718 |
#endif /* UNIV_DEBUG */ |
| 719 |
|
| 720 |
#ifndef UNIV_HOTBACKUP |
| 721 |
/* Update node structure which also implements the delete operation |
| 722 |
of a row */ |
| 723 |
|
| 724 |
struct upd_node_t { |
| 725 |
que_common_t common; /*!< node type: QUE_NODE_UPDATE */ |
| 726 |
bool is_delete; /* true if delete, false if update */ |
| 727 |
bool searched_update; |
| 728 |
/* true if searched update, false if |
| 729 |
positioned */ |
| 730 |
bool in_mysql_interface; |
| 731 |
/* true if the update node was created |
| 732 |
for the MySQL interface */ |
| 733 |
dict_foreign_t *foreign; /* NULL or pointer to a foreign key |
| 734 |
constraint if this update node is used in |
| 735 |
doing an ON DELETE or ON UPDATE operation */ |
| 736 |
upd_node_t *cascade_node; /* NULL or an update node template which |
| 737 |
is used to implement ON DELETE/UPDATE CASCADE |
| 738 |
or ... SET NULL for foreign keys. If present, |
| 739 |
this node updates the child (referencing) table's |
| 740 |
clustered index */ |
| 741 |
mem_heap_t *cascade_heap; |
| 742 |
/*!< NULL or a mem heap where cascade_upd_nodes |
| 743 |
are created.*/ |
| 744 |
sel_node_t *select; /*!< query graph subtree implementing a base |
| 745 |
table cursor: the rows returned will be |
| 746 |
updated */ |
| 747 |
btr_pcur_t *pcur; /*!< persistent cursor placed on the clustered |
| 748 |
index record which should be updated or |
| 749 |
deleted; the cursor is stored in the graph |
| 750 |
of 'select' field above, except in the case |
| 751 |
of the MySQL interface */ |
| 752 |
dict_table_t *table; /*!< table where updated */ |
| 753 |
upd_t *update; /*!< update vector for the row */ |
| 754 |
ulint update_n_fields; |
| 755 |
/* when this struct is used to implement |
| 756 |
a cascade operation for foreign keys, we store |
| 757 |
here the size of the buffer allocated for use |
| 758 |
as the update vector */ |
| 759 |
sym_node_list_t columns; /* symbol table nodes for the columns |
| 760 |
to retrieve from the table */ |
| 761 |
bool has_clust_rec_x_lock; |
| 762 |
/* true if the select which retrieves the |
| 763 |
records to update already sets an x-lock on |
| 764 |
the clustered record; note that it must always |
| 765 |
set at least an s-lock */ |
| 766 |
ulint cmpl_info; /* information extracted during query |
| 767 |
compilation; speeds up execution: |
| 768 |
UPD_NODE_NO_ORD_CHANGE and |
| 769 |
UPD_NODE_NO_SIZE_CHANGE, ORed */ |
| 770 |
/*----------------------*/ |
| 771 |
/* Local storage for this graph node */ |
| 772 |
ulint state; /*!< node execution state */ |
| 773 |
dict_index_t *index; /*!< NULL, or the next index whose record should |
| 774 |
be updated */ |
| 775 |
dtuple_t *row; /*!< NULL, or a copy (also fields copied to |
| 776 |
heap) of the row to update; this must be reset |
| 777 |
to NULL after a successful update */ |
| 778 |
row_ext_t *ext; /*!< NULL, or prefixes of the externally |
| 779 |
stored columns in the old row */ |
| 780 |
dtuple_t *upd_row; /* NULL, or a copy of the updated row */ |
| 781 |
row_ext_t *upd_ext; /* NULL, or prefixes of the externally |
| 782 |
stored columns in upd_row */ |
| 783 |
mem_heap_t *heap; /*!< memory heap used as auxiliary storage; |
| 784 |
this must be emptied after a successful |
| 785 |
update */ |
| 786 |
/*----------------------*/ |
| 787 |
sym_node_t *table_sym; /* table node in symbol table */ |
| 788 |
que_node_t *col_assign_list; |
| 789 |
/* column assignment list */ |
| 790 |
|
| 791 |
/** When there is a lock wait error, this remembers current position of |
| 792 |
the multi-value field, before which the values have been deleted. |
| 793 |
This will be used for both DELETE and the delete phase of UPDATE. */ |
| 794 |
uint32_t del_multi_val_pos; |
| 795 |
|
| 796 |
/** When there is a lock wait error, this remembers current position of |
| 797 |
the multi-value field, before which the values have been updated. */ |
| 798 |
uint32_t upd_multi_val_pos; |
| 799 |
|
| 800 |
ulint magic_n; |
| 801 |
}; |
| 802 |
|
| 803 |
constexpr uint32_t UPD_NODE_MAGIC_N = 1579975; |
| 804 |
|
| 805 |
/* Node execution states */ |
| 806 |
/** execution came to the node from a node above and if the field |
| 807 |
has_clust_rec_x_lock is false, we should set an intention x-lock on the table |
| 808 |
*/ |
| 809 |
constexpr uint32_t UPD_NODE_SET_IX_LOCK = 1; |
| 810 |
/** clustered index record should be updated */ |
| 811 |
constexpr uint32_t UPD_NODE_UPDATE_CLUSTERED = 2; |
| 812 |
/* clustered index record should be inserted, old record is already delete |
| 813 |
marked */ |
| 814 |
constexpr uint32_t UPD_NODE_INSERT_CLUSTERED = 3; |
| 815 |
/** an ordering field of the clustered index record was changed, or this is a |
| 816 |
delete operation: should update all the secondary index records */ |
| 817 |
constexpr uint32_t UPD_NODE_UPDATE_ALL_SEC = 5; |
| 818 |
/** secondary index entries should be looked at and updated if an ordering field |
| 819 |
changed */ |
| 820 |
constexpr uint32_t UPD_NODE_UPDATE_SOME_SEC = 6; |
| 821 |
|
| 822 |
/* Compilation info flags: these must fit within 2 bits; see trx0rec.h */ |
| 823 |
/** no secondary index record will be changed in the update and no ordering |
| 824 |
field of the clustered index */ |
| 825 |
constexpr uint32_t UPD_NODE_NO_ORD_CHANGE = 1; |
| 826 |
/** no record field size will be changed in the update */ |
| 827 |
constexpr uint32_t UPD_NODE_NO_SIZE_CHANGE = 2; |
| 828 |
#endif /* !UNIV_HOTBACKUP */ |
| 829 |
|
| 830 |
#include "row0upd.ic" |
| 831 |
|
| 832 |
#endif |