| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1997, 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 row/row0umod.cc |
| 29 |
Undo modify of a row |
| 30 |
|
| 31 |
Created 2/27/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <stddef.h> |
| 35 |
|
| 36 |
#include "btr0btr.h" |
| 37 |
#include "dict0boot.h" |
| 38 |
#include "dict0dd.h" |
| 39 |
#include "dict0dict.h" |
| 40 |
#include "ha_prototypes.h" |
| 41 |
#include "log0chkp.h" |
| 42 |
#include "mach0data.h" |
| 43 |
#include "que0que.h" |
| 44 |
#include "row0log.h" |
| 45 |
#include "row0row.h" |
| 46 |
#include "row0umod.h" |
| 47 |
#include "row0undo.h" |
| 48 |
#include "row0upd.h" |
| 49 |
#include "row0vers.h" |
| 50 |
#include "trx0rec.h" |
| 51 |
#include "trx0roll.h" |
| 52 |
#include "trx0trx.h" |
| 53 |
#include "trx0undo.h" |
| 54 |
|
| 55 |
#include "current_thd.h" |
| 56 |
#include "debug_sync.h" |
| 57 |
|
| 58 |
/* Considerations on undoing a modify operation. |
| 59 |
(1) Undoing a delete marking: all index records should be found. Some of |
| 60 |
them may have delete mark already false, if the delete mark operation was |
| 61 |
stopped underway, or if the undo operation ended prematurely because of a |
| 62 |
system crash. |
| 63 |
(2) Undoing an update of a delete unmarked record: the newer version of |
| 64 |
an updated secondary index entry should be removed if no prior version |
| 65 |
of the clustered index record requires its existence. Otherwise, it should |
| 66 |
be delete marked. |
| 67 |
(3) Undoing an update of a delete marked record. In this kind of update a |
| 68 |
delete marked clustered index record was delete unmarked and possibly also |
| 69 |
some of its fields were changed. Now, it is possible that the delete marked |
| 70 |
version has become obsolete at the time the undo is started. */ |
| 71 |
|
| 72 |
/************************************************************************* |
| 73 |
IMPORTANT NOTE: Any operation that generates redo MUST check that there |
| 74 |
is enough space in the redo log before for that operation. This is |
| 75 |
done by calling log_free_check(). The reason for checking the |
| 76 |
availability of the redo log space before the start of the operation is |
| 77 |
that we MUST not hold any synchronization objects when performing the |
| 78 |
check. |
| 79 |
If you make a change in this module make sure that no codepath is |
| 80 |
introduced where a call to log_free_check() is bypassed. */ |
| 81 |
|
| 82 |
/** Undoes a modify in a clustered index record. |
| 83 |
@return DB_SUCCESS, DB_FAIL, or error code: we may run out of file space */ |
| 84 |
[[nodiscard]] static dberr_t row_undo_mod_clust_low( |
| 85 |
undo_node_t *node, /*!< in: row undo node */ |
| 86 |
ulint **offsets, /*!< out: rec_get_offsets() on the record */ |
| 87 |
mem_heap_t **offsets_heap, |
| 88 |
/*!< in/out: memory heap that can be emptied */ |
| 89 |
mem_heap_t *heap, /*!< in/out: memory heap */ |
| 90 |
const dtuple_t **rebuilt_old_pk, |
| 91 |
/*!< out: row_log_table_get_pk() |
| 92 |
before the update, or NULL if |
| 93 |
the table is not being rebuilt online or |
| 94 |
the PRIMARY KEY definition does not change */ |
| 95 |
byte *sys, /*!< out: DB_TRX_ID, DB_ROLL_PTR |
| 96 |
for row_log_table_delete() */ |
| 97 |
que_thr_t *thr, /*!< in: query thread */ |
| 98 |
mtr_t *mtr, /*!< in: mtr; must be committed before |
| 99 |
latching any further pages */ |
| 100 |
ulint mode) /*!< in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE */ |
| 101 |
{ |
| 102 |
DBUG_TRACE; |
| 103 |
|
| 104 |
DBUG_LOG("undo", "undo_no=" << node->undo_no); |
| 105 |
|
| 106 |
btr_pcur_t *pcur; |
| 107 |
btr_cur_t *btr_cur; |
| 108 |
dberr_t err; |
| 109 |
trx_t *trx = thr_get_trx(thr); |
| 110 |
|
| 111 |
pcur = &node->pcur; |
| 112 |
btr_cur = pcur->get_btr_cur(); |
| 113 |
|
| 114 |
#ifdef UNIV_DEBUG |
| 115 |
auto success = |
| 116 |
#endif /* UNIV_DEBUG */ |
| 117 |
pcur->restore_position(mode, mtr, UT_LOCATION_HERE); |
| 118 |
|
| 119 |
ut_ad(success); |
| 120 |
ut_ad(rec_get_trx_id(btr_cur_get_rec(btr_cur), btr_cur->index) == |
| 121 |
thr_get_trx(thr)->id); |
| 122 |
|
| 123 |
if (mode != BTR_MODIFY_LEAF && dict_index_is_online_ddl(btr_cur->index)) { |
| 124 |
*rebuilt_old_pk = row_log_table_get_pk(btr_cur_get_rec(btr_cur), |
| 125 |
btr_cur->index, nullptr, sys, &heap); |
| 126 |
} else { |
| 127 |
*rebuilt_old_pk = nullptr; |
| 128 |
} |
| 129 |
|
| 130 |
/* Update would release the implicit lock. Must convert to |
| 131 |
explicit lock before applying update undo.*/ |
| 132 |
row_convert_impl_to_expl_if_needed(btr_cur, node); |
| 133 |
|
| 134 |
node->update->table = node->table; |
| 135 |
|
| 136 |
if (mode != BTR_MODIFY_TREE) { |
| 137 |
ut_ad((mode & ~BTR_ALREADY_S_LATCHED) == BTR_MODIFY_LEAF); |
| 138 |
|
| 139 |
err = btr_cur_optimistic_update(BTR_NO_LOCKING_FLAG | BTR_NO_UNDO_LOG_FLAG | |
| 140 |
BTR_KEEP_SYS_FLAG | BTR_KEEP_POS_FLAG, |
| 141 |
btr_cur, offsets, offsets_heap, |
| 142 |
node->update, node->cmpl_info, thr, |
| 143 |
thr_get_trx(thr)->id, mtr); |
| 144 |
} else { |
| 145 |
big_rec_t *dummy_big_rec; |
| 146 |
|
| 147 |
err = btr_cur_pessimistic_update( |
| 148 |
BTR_NO_LOCKING_FLAG | BTR_NO_UNDO_LOG_FLAG | BTR_KEEP_SYS_FLAG | |
| 149 |
BTR_KEEP_POS_FLAG, |
| 150 |
btr_cur, offsets, offsets_heap, heap, &dummy_big_rec, node->update, |
| 151 |
node->cmpl_info, thr, thr_get_trx(thr)->id, node->undo_no, mtr, pcur); |
| 152 |
|
| 153 |
const rec_t *rec = btr_cur_get_rec(btr_cur); |
| 154 |
ut_a(!dummy_big_rec || materialize_instant_default(btr_cur->index, rec)); |
| 155 |
|
| 156 |
if (dummy_big_rec) { |
| 157 |
ut_a(err == DB_SUCCESS); |
| 158 |
|
| 159 |
DEBUG_SYNC_C("before_row_upd_extern"); |
| 160 |
err = lob::btr_store_big_rec_extern_fields(trx, pcur, node->update, |
| 161 |
*offsets, dummy_big_rec, mtr, |
| 162 |
lob::OPCODE_UPDATE); |
| 163 |
DEBUG_SYNC_C("after_row_upd_extern"); |
| 164 |
mem_heap_free(dummy_big_rec->heap); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
return err; |
| 169 |
} |
| 170 |
|
| 171 |
/** Purges a clustered index record after undo if possible. |
| 172 |
This is attempted when the record was inserted by updating a |
| 173 |
delete-marked record and there no longer exist transactions |
| 174 |
that would see the delete-marked record. |
| 175 |
@return DB_SUCCESS, DB_FAIL, or error code: we may run out of file space |
| 176 |
*/ |
| 177 |
[[nodiscard]] static dberr_t row_undo_mod_remove_clust_low( |
| 178 |
undo_node_t *node, /*!< in: row undo node */ |
| 179 |
mtr_t *mtr, /*!< in/out: mini-transaction */ |
| 180 |
ulint mode) /*!< in: BTR_MODIFY_LEAF or BTR_MODIFY_TREE */ |
| 181 |
{ |
| 182 |
btr_cur_t *btr_cur; |
| 183 |
dberr_t err; |
| 184 |
ulint trx_id_offset; |
| 185 |
|
| 186 |
ut_ad(node->rec_type == TRX_UNDO_UPD_DEL_REC); |
| 187 |
|
| 188 |
/* Find out if the record has been purged already |
| 189 |
or if we can remove it. */ |
| 190 |
|
| 191 |
if (!node->pcur.restore_position(mode, mtr, UT_LOCATION_HERE) || |
| 192 |
row_vers_must_preserve_del_marked(node->new_trx_id, node->table->name, |
| 193 |
mtr)) { |
| 194 |
return (DB_SUCCESS); |
| 195 |
} |
| 196 |
|
| 197 |
btr_cur = node->pcur.get_btr_cur(); |
| 198 |
|
| 199 |
trx_id_offset = btr_cur->index->trx_id_offset; |
| 200 |
|
| 201 |
if (!trx_id_offset) { |
| 202 |
mem_heap_t *heap = nullptr; |
| 203 |
ulint trx_id_col; |
| 204 |
const ulint *offsets; |
| 205 |
ulint len; |
| 206 |
|
| 207 |
trx_id_col = btr_cur->index->get_sys_col_pos(DATA_TRX_ID); |
| 208 |
ut_ad(trx_id_col > 0); |
| 209 |
ut_ad(trx_id_col != ULINT_UNDEFINED); |
| 210 |
|
| 211 |
offsets = rec_get_offsets(btr_cur_get_rec(btr_cur), btr_cur->index, nullptr, |
| 212 |
trx_id_col + 1, UT_LOCATION_HERE, &heap); |
| 213 |
|
| 214 |
/* nullptr for index as trx_id_col is physical here */ |
| 215 |
trx_id_offset = rec_get_nth_field_offs(nullptr, offsets, trx_id_col, &len); |
| 216 |
ut_ad(len == DATA_TRX_ID_LEN); |
| 217 |
mem_heap_free(heap); |
| 218 |
} |
| 219 |
|
| 220 |
if (trx_read_trx_id(btr_cur_get_rec(btr_cur) + trx_id_offset) != |
| 221 |
node->new_trx_id) { |
| 222 |
/* The record must have been purged and then replaced |
| 223 |
with a different one. */ |
| 224 |
return (DB_SUCCESS); |
| 225 |
} |
| 226 |
|
| 227 |
/* We are about to remove an old, delete-marked version of the |
| 228 |
record that may have been delete-marked by a different transaction |
| 229 |
than the rolling-back one. */ |
| 230 |
ut_ad(rec_get_deleted_flag(btr_cur_get_rec(btr_cur), |
| 231 |
dict_table_is_comp(node->table))); |
| 232 |
|
| 233 |
if (mode == BTR_MODIFY_LEAF) { |
| 234 |
err = btr_cur_optimistic_delete(btr_cur, 0, mtr) ? DB_SUCCESS : DB_FAIL; |
| 235 |
} else { |
| 236 |
ut_ad(mode == (BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE)); |
| 237 |
|
| 238 |
/* This operation is analogous to purge, we can free also |
| 239 |
inherited externally stored fields. |
| 240 |
We can also assume that the record was complete |
| 241 |
(including BLOBs), because it had been delete-marked |
| 242 |
after it had been completely inserted. Therefore, we |
| 243 |
are passing rollback=false, just like purge does. */ |
| 244 |
|
| 245 |
btr_cur_pessimistic_delete(&err, false, btr_cur, 0, false, node->trx->id, |
| 246 |
node->undo_no, node->rec_type, mtr, &node->pcur, |
| 247 |
nullptr); |
| 248 |
|
| 249 |
/* The delete operation may fail if we have little |
| 250 |
file space left: TODO: easiest to crash the database |
| 251 |
and restart with more file space */ |
| 252 |
} |
| 253 |
|
| 254 |
return (err); |
| 255 |
} |
| 256 |
|
| 257 |
/** Undoes a modify in a clustered index record. Sets also the node state for |
| 258 |
the next round of undo. |
| 259 |
@return DB_SUCCESS or error code: we may run out of file space */ |
| 260 |
[[nodiscard]] static dberr_t row_undo_mod_clust( |
| 261 |
undo_node_t *node, /*!< in: row undo node */ |
| 262 |
que_thr_t *thr) /*!< in: query thread */ |
| 263 |
{ |
| 264 |
btr_pcur_t *pcur; |
| 265 |
mtr_t mtr; |
| 266 |
dberr_t err; |
| 267 |
dict_index_t *index; |
| 268 |
bool online; |
| 269 |
|
| 270 |
ut_ad(thr_get_trx(thr) == node->trx); |
| 271 |
ut_ad(node->trx->in_rollback); |
| 272 |
|
| 273 |
log_free_check(); |
| 274 |
pcur = &node->pcur; |
| 275 |
index = pcur->get_btr_cur()->index; |
| 276 |
|
| 277 |
mtr_start(&mtr); |
| 278 |
|
| 279 |
dict_disable_redo_if_temporary(index->table, &mtr); |
| 280 |
|
| 281 |
online = dict_index_is_online_ddl(index); |
| 282 |
DEBUG_SYNC(current_thd, "row_undo_mod_clust"); |
| 283 |
|
| 284 |
if (online) { |
| 285 |
mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 286 |
} |
| 287 |
|
| 288 |
mem_heap_t *heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 289 |
mem_heap_t *offsets_heap = nullptr; |
| 290 |
ulint *offsets = nullptr; |
| 291 |
const dtuple_t *rebuilt_old_pk; |
| 292 |
byte sys[DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN]; |
| 293 |
|
| 294 |
/* Try optimistic processing of the record, keeping changes within |
| 295 |
the index page */ |
| 296 |
|
| 297 |
err = row_undo_mod_clust_low( |
| 298 |
node, &offsets, &offsets_heap, heap, &rebuilt_old_pk, sys, thr, &mtr, |
| 299 |
online ? BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED : BTR_MODIFY_LEAF); |
| 300 |
|
| 301 |
if (err != DB_SUCCESS) { |
| 302 |
pcur->commit_specify_mtr(&mtr); |
| 303 |
|
| 304 |
/* We may have to modify tree structure: do a pessimistic |
| 305 |
descent down the index tree */ |
| 306 |
|
| 307 |
mtr_start(&mtr); |
| 308 |
|
| 309 |
dict_disable_redo_if_temporary(index->table, &mtr); |
| 310 |
|
| 311 |
err = row_undo_mod_clust_low(node, &offsets, &offsets_heap, heap, |
| 312 |
&rebuilt_old_pk, sys, thr, &mtr, |
| 313 |
BTR_MODIFY_TREE); |
| 314 |
ut_ad(err == DB_SUCCESS || err == DB_OUT_OF_FILE_SPACE); |
| 315 |
} |
| 316 |
|
| 317 |
/* Online rebuild cannot be initiated while we are holding |
| 318 |
dict_operation_lock and index->lock. (It can be aborted.) */ |
| 319 |
ut_ad(online || !dict_index_is_online_ddl(index)); |
| 320 |
|
| 321 |
if (err == DB_SUCCESS && online) { |
| 322 |
ut_ad(rw_lock_own_flagged( |
| 323 |
&index->lock, RW_LOCK_FLAG_S | RW_LOCK_FLAG_X | RW_LOCK_FLAG_SX)); |
| 324 |
|
| 325 |
switch (node->rec_type) { |
| 326 |
case TRX_UNDO_DEL_MARK_REC: |
| 327 |
row_log_table_insert(pcur->get_rec(), node->row, index, offsets); |
| 328 |
break; |
| 329 |
case TRX_UNDO_UPD_EXIST_REC: |
| 330 |
row_log_table_update(pcur->get_rec(), index, offsets, rebuilt_old_pk, |
| 331 |
node->undo_row, node->row); |
| 332 |
break; |
| 333 |
case TRX_UNDO_UPD_DEL_REC: |
| 334 |
row_log_table_delete(pcur->get_rec(), node->row, index, offsets, sys); |
| 335 |
break; |
| 336 |
default: |
| 337 |
ut_d(ut_error); |
| 338 |
ut_o(break); |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
ut_ad(rec_get_trx_id(pcur->get_rec(), index) == node->new_trx_id); |
| 343 |
|
| 344 |
pcur->commit_specify_mtr(&mtr); |
| 345 |
|
| 346 |
DEBUG_SYNC_C("ib_undo_mod_before_remove_clust"); |
| 347 |
|
| 348 |
if (err == DB_SUCCESS && node->rec_type == TRX_UNDO_UPD_DEL_REC) { |
| 349 |
mtr_start(&mtr); |
| 350 |
|
| 351 |
dict_disable_redo_if_temporary(index->table, &mtr); |
| 352 |
|
| 353 |
/* It is not necessary to call row_log_table, |
| 354 |
because the record is delete-marked and would thus |
| 355 |
be omitted from the rebuilt copy of the table. */ |
| 356 |
err = row_undo_mod_remove_clust_low(node, &mtr, BTR_MODIFY_LEAF); |
| 357 |
if (err != DB_SUCCESS) { |
| 358 |
pcur->commit_specify_mtr(&mtr); |
| 359 |
|
| 360 |
/* We may have to modify tree structure: do a |
| 361 |
pessimistic descent down the index tree */ |
| 362 |
|
| 363 |
mtr_start(&mtr); |
| 364 |
|
| 365 |
dict_disable_redo_if_temporary(index->table, &mtr); |
| 366 |
|
| 367 |
err = row_undo_mod_remove_clust_low( |
| 368 |
node, &mtr, BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE); |
| 369 |
|
| 370 |
ut_ad(err == DB_SUCCESS || err == DB_OUT_OF_FILE_SPACE); |
| 371 |
} |
| 372 |
|
| 373 |
pcur->commit_specify_mtr(&mtr); |
| 374 |
} |
| 375 |
|
| 376 |
node->state = UNDO_NODE_FETCH_NEXT; |
| 377 |
|
| 378 |
if (offsets_heap) { |
| 379 |
mem_heap_free(offsets_heap); |
| 380 |
} |
| 381 |
mem_heap_free(heap); |
| 382 |
return (err); |
| 383 |
} |
| 384 |
|
| 385 |
/** Delete marks or removes a secondary index entry if found. |
| 386 |
@return DB_SUCCESS, DB_FAIL, or DB_OUT_OF_FILE_SPACE */ |
| 387 |
[[nodiscard]] static dberr_t row_undo_mod_del_mark_or_remove_sec_low( |
| 388 |
undo_node_t *node, /*!< in: row undo node */ |
| 389 |
que_thr_t *thr, /*!< in: query thread */ |
| 390 |
dict_index_t *index, /*!< in: index */ |
| 391 |
dtuple_t *entry, /*!< in: index entry */ |
| 392 |
ulint mode) /*!< in: latch mode BTR_MODIFY_LEAF or |
| 393 |
BTR_MODIFY_TREE */ |
| 394 |
{ |
| 395 |
btr_pcur_t pcur; |
| 396 |
btr_cur_t *btr_cur; |
| 397 |
dberr_t err = DB_SUCCESS; |
| 398 |
mtr_t mtr; |
| 399 |
mtr_t mtr_vers; |
| 400 |
row_search_result search_result; |
| 401 |
bool modify_leaf = false; |
| 402 |
ulint rec_deleted; |
| 403 |
bool success; |
| 404 |
bool old_has; |
| 405 |
|
| 406 |
log_free_check(); |
| 407 |
|
| 408 |
mtr_start(&mtr); |
| 409 |
|
| 410 |
dict_disable_redo_if_temporary(index->table, &mtr); |
| 411 |
|
| 412 |
if (mode == BTR_MODIFY_LEAF) { |
| 413 |
modify_leaf = true; |
| 414 |
} |
| 415 |
|
| 416 |
if (!index->is_committed()) { |
| 417 |
/* The index->online_status may change if the index is |
| 418 |
or was being created online, but not committed yet. It |
| 419 |
is protected by index->lock. */ |
| 420 |
if (mode == BTR_MODIFY_LEAF) { |
| 421 |
mode = BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED; |
| 422 |
mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 423 |
} else { |
| 424 |
ut_ad(mode == (BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE)); |
| 425 |
mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 426 |
} |
| 427 |
|
| 428 |
if (row_log_online_op_try(index, entry, 0)) { |
| 429 |
goto func_exit_no_pcur; |
| 430 |
} |
| 431 |
} else { |
| 432 |
/* For secondary indexes, |
| 433 |
index->online_status==ONLINE_INDEX_COMPLETE if |
| 434 |
index->is_committed(). */ |
| 435 |
ut_ad(!dict_index_is_online_ddl(index)); |
| 436 |
} |
| 437 |
|
| 438 |
btr_cur = pcur.get_btr_cur(); |
| 439 |
|
| 440 |
if (dict_index_is_spatial(index)) { |
| 441 |
if (mode & BTR_MODIFY_LEAF) { |
| 442 |
btr_cur->thr = thr; |
| 443 |
mode |= BTR_RTREE_DELETE_MARK; |
| 444 |
} |
| 445 |
mode |= BTR_RTREE_UNDO_INS; |
| 446 |
} |
| 447 |
|
| 448 |
search_result = row_search_index_entry(index, entry, mode, &pcur, &mtr); |
| 449 |
|
| 450 |
switch (UNIV_EXPECT(search_result, ROW_FOUND)) { |
| 451 |
case ROW_NOT_FOUND: |
| 452 |
/* In crash recovery, the secondary index record may |
| 453 |
be missing if the UPDATE did not have time to insert |
| 454 |
the secondary index records before the crash. When we |
| 455 |
are undoing that UPDATE in crash recovery, the record |
| 456 |
may be missing. |
| 457 |
|
| 458 |
In normal processing, if an update ends in a deadlock |
| 459 |
before it has inserted all updated secondary index |
| 460 |
records, then the undo will not find those records. */ |
| 461 |
goto func_exit; |
| 462 |
case ROW_FOUND: |
| 463 |
break; |
| 464 |
case ROW_BUFFERED: |
| 465 |
case ROW_NOT_DELETED_REF: |
| 466 |
/* These are invalid outcomes, because the mode passed |
| 467 |
to row_search_index_entry() did not include any of the |
| 468 |
flags BTR_INSERT, BTR_DELETE, or BTR_DELETE_MARK. */ |
| 469 |
ut_error; |
| 470 |
} |
| 471 |
|
| 472 |
/* If the key is delete marked then the statement could not modify the |
| 473 |
key yet and the transaction has no implicit lock on it. We must convert |
| 474 |
to explicit lock if and only if we are the transaction which has implicit |
| 475 |
lock on it.Note that it is still ok to purge the delete mark key if it |
| 476 |
is purgeable.*/ |
| 477 |
rec_deleted = |
| 478 |
rec_get_deleted_flag(pcur.get_rec(), dict_table_is_comp(index->table)); |
| 479 |
if (rec_deleted == 0) { |
| 480 |
row_convert_impl_to_expl_if_needed(btr_cur, node); |
| 481 |
} |
| 482 |
|
| 483 |
/* We should remove the index record if no prior version of the row, |
| 484 |
which cannot be purged yet, requires its existence. If some requires, |
| 485 |
we should delete mark the record. */ |
| 486 |
|
| 487 |
mtr_start(&mtr_vers); |
| 488 |
|
| 489 |
success = |
| 490 |
node->pcur.restore_position(BTR_SEARCH_LEAF, &mtr_vers, UT_LOCATION_HERE); |
| 491 |
ut_a(success); |
| 492 |
|
| 493 |
old_has = row_vers_old_has_index_entry(false, node->pcur.get_rec(), &mtr_vers, |
| 494 |
index, entry, 0, 0); |
| 495 |
if (old_has) { |
| 496 |
err = btr_cur_del_mark_set_sec_rec(BTR_NO_LOCKING_FLAG, btr_cur, true, thr, |
| 497 |
&mtr); |
| 498 |
ut_ad(err == DB_SUCCESS); |
| 499 |
} else { |
| 500 |
/* Remove the index record */ |
| 501 |
|
| 502 |
if (dict_index_is_spatial(index)) { |
| 503 |
if (rec_deleted) { |
| 504 |
ib::error(ER_IB_MSG_1038) << "Record found in index " << index->name |
| 505 |
<< " is deleted marked" |
| 506 |
" on rollback update."; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
if (modify_leaf) { |
| 511 |
success = btr_cur_optimistic_delete(btr_cur, 0, &mtr); |
| 512 |
if (success) { |
| 513 |
err = DB_SUCCESS; |
| 514 |
} else { |
| 515 |
err = DB_FAIL; |
| 516 |
} |
| 517 |
} else { |
| 518 |
/* Passing rollback=false, |
| 519 |
because we are deleting a secondary index record: |
| 520 |
the distinction only matters when deleting a |
| 521 |
record that contains externally stored columns. */ |
| 522 |
ut_ad(!index->is_clustered()); |
| 523 |
btr_cur_pessimistic_delete(&err, false, btr_cur, 0, false, node->trx->id, |
| 524 |
node->undo_no, node->rec_type, &mtr, |
| 525 |
&node->pcur, nullptr); |
| 526 |
/* The delete operation may fail if we have little |
| 527 |
file space left: TODO: easiest to crash the database |
| 528 |
and restart with more file space */ |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
node->pcur.commit_specify_mtr(&mtr_vers); |
| 533 |
|
| 534 |
func_exit: |
| 535 |
pcur.close(); |
| 536 |
func_exit_no_pcur: |
| 537 |
mtr_commit(&mtr); |
| 538 |
|
| 539 |
return (err); |
| 540 |
} |
| 541 |
|
| 542 |
/** Delete marks or removes a secondary index entry if found. |
| 543 |
NOTE that if we updated the fields of a delete-marked secondary index record |
| 544 |
so that alphabetically they stayed the same, e.g., 'abc' -> 'aBc', we cannot |
| 545 |
return to the original values because we do not know them. But this should |
| 546 |
not cause problems because in row0sel.cc, in queries we always retrieve the |
| 547 |
clustered index record or an earlier version of it, if the secondary index |
| 548 |
record through which we do the search is delete-marked. |
| 549 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 550 |
[[nodiscard]] static dberr_t row_undo_mod_del_mark_or_remove_sec( |
| 551 |
undo_node_t *node, /*!< in: row undo node */ |
| 552 |
que_thr_t *thr, /*!< in: query thread */ |
| 553 |
dict_index_t *index, /*!< in: index */ |
| 554 |
dtuple_t *entry) /*!< in: index entry */ |
| 555 |
{ |
| 556 |
dberr_t err; |
| 557 |
|
| 558 |
err = row_undo_mod_del_mark_or_remove_sec_low(node, thr, index, entry, |
| 559 |
BTR_MODIFY_LEAF); |
| 560 |
if (err == DB_SUCCESS) { |
| 561 |
return (err); |
| 562 |
} |
| 563 |
|
| 564 |
err = row_undo_mod_del_mark_or_remove_sec_low( |
| 565 |
node, thr, index, entry, BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE); |
| 566 |
return (err); |
| 567 |
} |
| 568 |
|
| 569 |
/** Delete unmarks a secondary index entry which must be found. It might not be |
| 570 |
delete-marked at the moment, but it does not harm to unmark it anyway. We also |
| 571 |
need to update the fields of the secondary index record if we updated its |
| 572 |
fields but alphabetically they stayed the same, e.g., 'abc' -> 'aBc'. |
| 573 |
@retval DB_SUCCESS on success |
| 574 |
@retval DB_FAIL if BTR_MODIFY_TREE should be tried |
| 575 |
@retval DB_OUT_OF_FILE_SPACE when running out of tablespace |
| 576 |
@retval DB_DUPLICATE_KEY if the value was missing |
| 577 |
and an insert would lead to a duplicate exists */ |
| 578 |
[[nodiscard]] static dberr_t row_undo_mod_del_unmark_sec_and_undo_update( |
| 579 |
ulint mode, /*!< in: search mode: BTR_MODIFY_LEAF or |
| 580 |
BTR_MODIFY_TREE */ |
| 581 |
que_thr_t *thr, /*!< in: query thread */ |
| 582 |
dict_index_t *index, /*!< in: index */ |
| 583 |
dtuple_t *entry, /*!< in: index entry */ |
| 584 |
undo_no_t undo_no) |
| 585 |
/*!< in: undo number up to which to rollback.*/ |
| 586 |
{ |
| 587 |
btr_pcur_t pcur; |
| 588 |
btr_cur_t *btr_cur = pcur.get_btr_cur(); |
| 589 |
upd_t *update; |
| 590 |
dberr_t err = DB_SUCCESS; |
| 591 |
big_rec_t *dummy_big_rec; |
| 592 |
mtr_t mtr; |
| 593 |
trx_t *trx = thr_get_trx(thr); |
| 594 |
const ulint flags = BTR_KEEP_SYS_FLAG | BTR_NO_LOCKING_FLAG; |
| 595 |
row_search_result search_result; |
| 596 |
ulint orig_mode = mode; |
| 597 |
|
| 598 |
ut_ad(trx->id != 0); |
| 599 |
|
| 600 |
/* FIXME: Currently we do a 2-pass search for the undo due to |
| 601 |
avoid undel-mark a wrong rec in rolling back in partial update. |
| 602 |
Later, we could log some info in secondary index updates to avoid |
| 603 |
this. */ |
| 604 |
if (dict_index_is_spatial(index)) { |
| 605 |
ut_ad(mode & BTR_MODIFY_LEAF); |
| 606 |
mode |= BTR_RTREE_DELETE_MARK; |
| 607 |
} |
| 608 |
|
| 609 |
try_again: |
| 610 |
log_free_check(); |
| 611 |
|
| 612 |
mtr_start(&mtr); |
| 613 |
|
| 614 |
dict_disable_redo_if_temporary(index->table, &mtr); |
| 615 |
|
| 616 |
if (!index->is_committed()) { |
| 617 |
/* The index->online_status may change if the index is |
| 618 |
or was being created online, but not committed yet. It |
| 619 |
is protected by index->lock. */ |
| 620 |
if (mode == BTR_MODIFY_LEAF) { |
| 621 |
mode = BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED; |
| 622 |
mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 623 |
} else { |
| 624 |
ut_ad(mode == BTR_MODIFY_TREE); |
| 625 |
mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 626 |
} |
| 627 |
|
| 628 |
if (row_log_online_op_try(index, entry, trx->id)) { |
| 629 |
goto func_exit_no_pcur; |
| 630 |
} |
| 631 |
} else { |
| 632 |
/* For secondary indexes, |
| 633 |
index->online_status==ONLINE_INDEX_COMPLETE if |
| 634 |
index->is_committed(). */ |
| 635 |
ut_ad(!dict_index_is_online_ddl(index)); |
| 636 |
} |
| 637 |
|
| 638 |
btr_cur->thr = thr; |
| 639 |
|
| 640 |
search_result = row_search_index_entry(index, entry, mode, &pcur, &mtr); |
| 641 |
|
| 642 |
switch (search_result) { |
| 643 |
mem_heap_t *heap; |
| 644 |
mem_heap_t *offsets_heap; |
| 645 |
ulint *offsets; |
| 646 |
case ROW_BUFFERED: |
| 647 |
case ROW_NOT_DELETED_REF: |
| 648 |
/* These are invalid outcomes, because the mode passed |
| 649 |
to row_search_index_entry() did not include any of the |
| 650 |
flags BTR_INSERT, BTR_DELETE, or BTR_DELETE_MARK. */ |
| 651 |
ut_error; |
| 652 |
case ROW_NOT_FOUND: |
| 653 |
/* For spatial index, if first search didn't find an |
| 654 |
undel-marked rec, try to find a del-marked rec. */ |
| 655 |
if (dict_index_is_spatial(index) && btr_cur->rtr_info->fd_del) { |
| 656 |
if (mode != orig_mode) { |
| 657 |
mode = orig_mode; |
| 658 |
pcur.close(); |
| 659 |
mtr_commit(&mtr); |
| 660 |
goto try_again; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
if (index->is_committed()) { |
| 665 |
/* During online secondary index creation, it |
| 666 |
is possible that MySQL is waiting for a |
| 667 |
meta-data lock upgrade before invoking |
| 668 |
ha_innobase::commit_inplace_alter_table() |
| 669 |
while this ROLLBACK is executing. InnoDB has |
| 670 |
finished building the index, but it does not |
| 671 |
yet exist in MySQL. In this case, we suppress |
| 672 |
the printout to the error log. */ |
| 673 |
ib::warn(ER_IB_MSG_1039) |
| 674 |
<< "Record in index " << index->name << " of table " |
| 675 |
<< index->table->name |
| 676 |
<< " was not found on rollback, trying to" |
| 677 |
" insert: " |
| 678 |
<< *entry |
| 679 |
<< " at: " << rec_index_print(btr_cur_get_rec(btr_cur), index); |
| 680 |
} |
| 681 |
|
| 682 |
if (btr_cur->up_match >= dict_index_get_n_unique(index) || |
| 683 |
btr_cur->low_match >= dict_index_get_n_unique(index)) { |
| 684 |
if (index->is_committed()) { |
| 685 |
ib::warn(ER_IB_MSG_1040) << "Record in index " << index->name |
| 686 |
<< " was not found on rollback, and" |
| 687 |
" a duplicate exists"; |
| 688 |
} |
| 689 |
err = DB_DUPLICATE_KEY; |
| 690 |
break; |
| 691 |
} |
| 692 |
|
| 693 |
/* Insert the missing record that we were trying to |
| 694 |
delete-unmark. */ |
| 695 |
big_rec_t *big_rec; |
| 696 |
rec_t *insert_rec; |
| 697 |
offsets = nullptr; |
| 698 |
offsets_heap = nullptr; |
| 699 |
|
| 700 |
err = btr_cur_optimistic_insert(flags, btr_cur, &offsets, &offsets_heap, |
| 701 |
entry, &insert_rec, &big_rec, thr, &mtr); |
| 702 |
ut_ad(!big_rec); |
| 703 |
|
| 704 |
if (err == DB_FAIL && mode == BTR_MODIFY_TREE) { |
| 705 |
err = |
| 706 |
btr_cur_pessimistic_insert(flags, btr_cur, &offsets, &offsets_heap, |
| 707 |
entry, &insert_rec, &big_rec, thr, &mtr); |
| 708 |
/* There are no off-page columns in |
| 709 |
secondary indexes. */ |
| 710 |
ut_ad(!big_rec); |
| 711 |
} |
| 712 |
|
| 713 |
if (err == DB_SUCCESS) { |
| 714 |
page_update_max_trx_id(btr_cur_get_block(btr_cur), |
| 715 |
btr_cur_get_page_zip(btr_cur), trx->id, &mtr); |
| 716 |
} |
| 717 |
|
| 718 |
if (offsets_heap) { |
| 719 |
mem_heap_free(offsets_heap); |
| 720 |
} |
| 721 |
|
| 722 |
break; |
| 723 |
case ROW_FOUND: |
| 724 |
err = btr_cur_del_mark_set_sec_rec(BTR_NO_LOCKING_FLAG, btr_cur, false, |
| 725 |
thr, &mtr); |
| 726 |
|
| 727 |
ut_a(err == DB_SUCCESS); |
| 728 |
heap = mem_heap_create( |
| 729 |
sizeof(upd_t) + dtuple_get_n_fields(entry) * sizeof(upd_field_t), |
| 730 |
UT_LOCATION_HERE); |
| 731 |
offsets_heap = nullptr; |
| 732 |
offsets = |
| 733 |
rec_get_offsets(btr_cur_get_rec(btr_cur), index, nullptr, |
| 734 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &offsets_heap); |
| 735 |
update = row_upd_build_sec_rec_difference_binary( |
| 736 |
btr_cur_get_rec(btr_cur), index, offsets, entry, heap); |
| 737 |
if (upd_get_n_fields(update) == 0) { |
| 738 |
/* Do nothing */ |
| 739 |
|
| 740 |
} else if (mode != BTR_MODIFY_TREE) { |
| 741 |
/* Try an optimistic updating of the record, keeping |
| 742 |
changes within the page */ |
| 743 |
|
| 744 |
/* TODO: pass offsets, not &offsets */ |
| 745 |
err = btr_cur_optimistic_update(flags, btr_cur, &offsets, &offsets_heap, |
| 746 |
update, 0, thr, thr_get_trx(thr)->id, |
| 747 |
&mtr); |
| 748 |
switch (err) { |
| 749 |
case DB_OVERFLOW: |
| 750 |
case DB_UNDERFLOW: |
| 751 |
case DB_ZIP_OVERFLOW: |
| 752 |
err = DB_FAIL; |
| 753 |
default: |
| 754 |
break; |
| 755 |
} |
| 756 |
} else { |
| 757 |
err = btr_cur_pessimistic_update( |
| 758 |
flags, btr_cur, &offsets, &offsets_heap, heap, &dummy_big_rec, |
| 759 |
update, 0, thr, thr_get_trx(thr)->id, undo_no, &mtr); |
| 760 |
ut_a(!dummy_big_rec); |
| 761 |
} |
| 762 |
|
| 763 |
mem_heap_free(heap); |
| 764 |
mem_heap_free(offsets_heap); |
| 765 |
} |
| 766 |
|
| 767 |
pcur.close(); |
| 768 |
func_exit_no_pcur: |
| 769 |
mtr_commit(&mtr); |
| 770 |
|
| 771 |
return (err); |
| 772 |
} |
| 773 |
|
| 774 |
/** Flags a secondary index corrupted. */ |
| 775 |
static void row_undo_mod_sec_flag_corrupted( |
| 776 |
trx_t *trx, /*!< in/out: transaction */ |
| 777 |
dict_index_t *index) /*!< in: secondary index */ |
| 778 |
{ |
| 779 |
ut_ad(!index->is_clustered()); |
| 780 |
|
| 781 |
switch (trx->dict_operation_lock_mode) { |
| 782 |
case RW_S_LATCH: |
| 783 |
/* This should be the normal rollback */ |
| 784 |
dict_set_corrupted(index); |
| 785 |
break; |
| 786 |
default: |
| 787 |
[[fallthrough]]; |
| 788 |
case RW_X_LATCH: |
| 789 |
/* This should be the rollback of a data dictionary |
| 790 |
transaction. */ |
| 791 |
dict_set_corrupted(index); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
/** Undoes a modify in secondary indexes when undo record type is UPD_DEL. |
| 796 |
This is the specific function to handle the modify on multi-value indexes. |
| 797 |
@param[in,out] node row undo node |
| 798 |
@param[in,out] thr query thread |
| 799 |
@param[in] index the multi-value index |
| 800 |
@param[in,out] heap memory heap |
| 801 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 802 |
[[nodiscard]] static dberr_t row_undo_mod_upd_del_multi_sec(undo_node_t *node, |
| 803 |
que_thr_t *thr, |
| 804 |
dict_index_t *index, |
| 805 |
mem_heap_t *heap) { |
| 806 |
dberr_t err = DB_SUCCESS; |
| 807 |
Multi_value_entry_builder_normal mv_entry_builder(node->row, node->ext, index, |
| 808 |
heap, true, false); |
| 809 |
|
| 810 |
ut_ad(index->is_multi_value()); |
| 811 |
|
| 812 |
for (dtuple_t *entry = mv_entry_builder.begin(); entry != nullptr; |
| 813 |
entry = mv_entry_builder.next()) { |
| 814 |
err = row_undo_mod_del_mark_or_remove_sec(node, thr, index, entry); |
| 815 |
|
| 816 |
if (UNIV_UNLIKELY(err != DB_SUCCESS)) { |
| 817 |
break; |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
return (err); |
| 822 |
} |
| 823 |
|
| 824 |
/** Undoes a modify in secondary indexes when undo record type is UPD_DEL. |
| 825 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 826 |
[[nodiscard]] static dberr_t row_undo_mod_upd_del_sec( |
| 827 |
undo_node_t *node, /*!< in: row undo node */ |
| 828 |
que_thr_t *thr) /*!< in: query thread */ |
| 829 |
{ |
| 830 |
mem_heap_t *heap; |
| 831 |
dberr_t err = DB_SUCCESS; |
| 832 |
|
| 833 |
ut_ad(node->rec_type == TRX_UNDO_UPD_DEL_REC); |
| 834 |
ut_ad(!node->undo_row); |
| 835 |
|
| 836 |
heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 837 |
|
| 838 |
while (node->index != nullptr) { |
| 839 |
dict_index_t *index = node->index; |
| 840 |
dtuple_t *entry; |
| 841 |
|
| 842 |
if (index->type & DICT_FTS) { |
| 843 |
dict_table_next_uncorrupted_index(node->index); |
| 844 |
continue; |
| 845 |
} |
| 846 |
|
| 847 |
if (index->is_multi_value()) { |
| 848 |
err = row_undo_mod_upd_del_multi_sec(node, thr, index, heap); |
| 849 |
if (err != DB_SUCCESS) { |
| 850 |
break; |
| 851 |
} |
| 852 |
|
| 853 |
mem_heap_empty(heap); |
| 854 |
dict_table_next_uncorrupted_index(node->index); |
| 855 |
continue; |
| 856 |
} |
| 857 |
|
| 858 |
/* During online index creation, |
| 859 |
HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE should |
| 860 |
guarantee that any active transaction has not modified |
| 861 |
indexed columns such that col->ord_part was 0 at the |
| 862 |
time when the undo log record was written. When we get |
| 863 |
to roll back an undo log entry TRX_UNDO_DEL_MARK_REC, |
| 864 |
it should always cover all affected indexes. */ |
| 865 |
entry = row_build_index_entry(node->row, node->ext, index, heap); |
| 866 |
|
| 867 |
if (UNIV_UNLIKELY(!entry)) { |
| 868 |
/* The database must have crashed after |
| 869 |
inserting a clustered index record but before |
| 870 |
writing all the externally stored columns of |
| 871 |
that record. Because secondary index entries |
| 872 |
are inserted after the clustered index record, |
| 873 |
we may assume that the secondary index record |
| 874 |
does not exist. However, this situation may |
| 875 |
only occur during the rollback of incomplete |
| 876 |
transactions. */ |
| 877 |
ut_a(thr_is_recv(thr)); |
| 878 |
} else { |
| 879 |
err = row_undo_mod_del_mark_or_remove_sec(node, thr, index, entry); |
| 880 |
|
| 881 |
if (UNIV_UNLIKELY(err != DB_SUCCESS)) { |
| 882 |
break; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
mem_heap_empty(heap); |
| 887 |
dict_table_next_uncorrupted_index(node->index); |
| 888 |
} |
| 889 |
|
| 890 |
mem_heap_free(heap); |
| 891 |
|
| 892 |
return (err); |
| 893 |
} |
| 894 |
|
| 895 |
/** Undoes a modify in secondary indexes when undo record type is DEL_MARK. |
| 896 |
This is the specific function to handle the modify on multi-value indexes. |
| 897 |
@param[in,out] node row undo node |
| 898 |
@param[in,out] thr query thread |
| 899 |
@param[in] index the multi-value index |
| 900 |
@param[in,out] heap memory heap |
| 901 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 902 |
[[nodiscard]] static dberr_t row_undo_mod_del_mark_multi_sec( |
| 903 |
undo_node_t *node, que_thr_t *thr, dict_index_t *index, mem_heap_t *heap) { |
| 904 |
dberr_t err = DB_SUCCESS; |
| 905 |
Multi_value_entry_builder_normal mv_entry_builder(node->row, node->ext, index, |
| 906 |
heap, true, false); |
| 907 |
|
| 908 |
ut_ad(index->is_multi_value()); |
| 909 |
|
| 910 |
for (dtuple_t *entry = mv_entry_builder.begin(); entry != nullptr; |
| 911 |
entry = mv_entry_builder.next()) { |
| 912 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 913 |
BTR_MODIFY_LEAF, thr, index, entry, node->undo_no); |
| 914 |
|
| 915 |
if (err == DB_FAIL) { |
| 916 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 917 |
BTR_MODIFY_TREE, thr, index, entry, node->undo_no); |
| 918 |
} |
| 919 |
|
| 920 |
if (err == DB_DUPLICATE_KEY) { |
| 921 |
row_undo_mod_sec_flag_corrupted(thr_get_trx(thr), index); |
| 922 |
err = DB_SUCCESS; |
| 923 |
/* Do not return any error to the caller. The |
| 924 |
duplicate will be reported by ALTER TABLE or |
| 925 |
CREATE UNIQUE INDEX. Unfortunately we cannot |
| 926 |
report the duplicate key value to the DDL |
| 927 |
thread, because the altered_table object is |
| 928 |
private to its call stack. */ |
| 929 |
} else if (err != DB_SUCCESS) { |
| 930 |
break; |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
return (err); |
| 935 |
} |
| 936 |
|
| 937 |
/** Undoes a modify in secondary indexes when undo record type is DEL_MARK. |
| 938 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 939 |
[[nodiscard]] static dberr_t row_undo_mod_del_mark_sec( |
| 940 |
undo_node_t *node, /*!< in: row undo node */ |
| 941 |
que_thr_t *thr) /*!< in: query thread */ |
| 942 |
{ |
| 943 |
mem_heap_t *heap; |
| 944 |
dberr_t err = DB_SUCCESS; |
| 945 |
|
| 946 |
ut_ad(!node->undo_row); |
| 947 |
|
| 948 |
heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 949 |
|
| 950 |
while (node->index != nullptr) { |
| 951 |
dict_index_t *index = node->index; |
| 952 |
dtuple_t *entry; |
| 953 |
|
| 954 |
if (index->type == DICT_FTS) { |
| 955 |
dict_table_next_uncorrupted_index(node->index); |
| 956 |
continue; |
| 957 |
} |
| 958 |
|
| 959 |
if (index->is_multi_value()) { |
| 960 |
err = row_undo_mod_del_mark_multi_sec(node, thr, index, heap); |
| 961 |
if (err != DB_SUCCESS) { |
| 962 |
break; |
| 963 |
} |
| 964 |
|
| 965 |
mem_heap_empty(heap); |
| 966 |
dict_table_next_uncorrupted_index(node->index); |
| 967 |
continue; |
| 968 |
} |
| 969 |
|
| 970 |
/* During online index creation, |
| 971 |
HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE should |
| 972 |
guarantee that any active transaction has not modified |
| 973 |
indexed columns such that col->ord_part was 0 at the |
| 974 |
time when the undo log record was written. When we get |
| 975 |
to roll back an undo log entry TRX_UNDO_DEL_MARK_REC, |
| 976 |
it should always cover all affected indexes. */ |
| 977 |
entry = row_build_index_entry(node->row, node->ext, index, heap); |
| 978 |
|
| 979 |
ut_a(entry); |
| 980 |
|
| 981 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 982 |
BTR_MODIFY_LEAF, thr, index, entry, node->undo_no); |
| 983 |
if (err == DB_FAIL) { |
| 984 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 985 |
BTR_MODIFY_TREE, thr, index, entry, node->undo_no); |
| 986 |
} |
| 987 |
|
| 988 |
if (err == DB_DUPLICATE_KEY) { |
| 989 |
row_undo_mod_sec_flag_corrupted(thr_get_trx(thr), index); |
| 990 |
err = DB_SUCCESS; |
| 991 |
/* Do not return any error to the caller. The |
| 992 |
duplicate will be reported by ALTER TABLE or |
| 993 |
CREATE UNIQUE INDEX. Unfortunately we cannot |
| 994 |
report the duplicate key value to the DDL |
| 995 |
thread, because the altered_table object is |
| 996 |
private to its call stack. */ |
| 997 |
} else if (err != DB_SUCCESS) { |
| 998 |
break; |
| 999 |
} |
| 1000 |
|
| 1001 |
mem_heap_empty(heap); |
| 1002 |
dict_table_next_uncorrupted_index(node->index); |
| 1003 |
} |
| 1004 |
|
| 1005 |
mem_heap_free(heap); |
| 1006 |
|
| 1007 |
return (err); |
| 1008 |
} |
| 1009 |
|
| 1010 |
/** Undoes a modify in secondary indexes when undo record type is UPD_EXIST. |
| 1011 |
This is the specific function to handle the modify on multi-value indexes. |
| 1012 |
@param[in,out] node row undo node |
| 1013 |
@param[in,out] thr query thread |
| 1014 |
@param[in] index the multi-value index |
| 1015 |
@param[in] non_mv_upd true if any non-multi-value field on the index |
| 1016 |
gets updated too |
| 1017 |
@param[in,out] heap memory heap |
| 1018 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 1019 |
static dberr_t row_undo_mod_upd_exist_multi_sec(undo_node_t *node, |
| 1020 |
que_thr_t *thr, |
| 1021 |
dict_index_t *index, |
| 1022 |
bool non_mv_upd, |
| 1023 |
mem_heap_t *heap) { |
| 1024 |
dberr_t err = DB_SUCCESS; |
| 1025 |
|
| 1026 |
{ |
| 1027 |
Multi_value_entry_builder_normal mv_entry_builder( |
| 1028 |
node->row, node->ext, index, heap, true, !non_mv_upd); |
| 1029 |
|
| 1030 |
ut_ad(index->is_multi_value()); |
| 1031 |
|
| 1032 |
for (dtuple_t *entry = mv_entry_builder.begin(); entry != nullptr; |
| 1033 |
entry = mv_entry_builder.next()) { |
| 1034 |
err = row_undo_mod_del_mark_or_remove_sec(node, thr, index, entry); |
| 1035 |
if (err != DB_SUCCESS) { |
| 1036 |
break; |
| 1037 |
} |
| 1038 |
} |
| 1039 |
} |
| 1040 |
|
| 1041 |
if (err != DB_SUCCESS) { |
| 1042 |
return (err); |
| 1043 |
} |
| 1044 |
|
| 1045 |
Multi_value_entry_builder_normal mv_entry_builder( |
| 1046 |
node->undo_row, node->undo_ext, index, heap, true, !non_mv_upd); |
| 1047 |
|
| 1048 |
for (dtuple_t *entry = mv_entry_builder.begin(); entry != nullptr; |
| 1049 |
entry = mv_entry_builder.next()) { |
| 1050 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 1051 |
BTR_MODIFY_LEAF, thr, index, entry, node->undo_no); |
| 1052 |
|
| 1053 |
if (err == DB_FAIL) { |
| 1054 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 1055 |
BTR_MODIFY_TREE, thr, index, entry, node->undo_no); |
| 1056 |
} |
| 1057 |
|
| 1058 |
if (err != DB_SUCCESS) { |
| 1059 |
break; |
| 1060 |
} |
| 1061 |
} |
| 1062 |
|
| 1063 |
return (err); |
| 1064 |
} |
| 1065 |
|
| 1066 |
/** Undoes a modify in secondary indexes when undo record type is UPD_EXIST. |
| 1067 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 1068 |
[[nodiscard]] static dberr_t row_undo_mod_upd_exist_sec( |
| 1069 |
undo_node_t *node, /*!< in: row undo node */ |
| 1070 |
que_thr_t *thr) /*!< in: query thread */ |
| 1071 |
{ |
| 1072 |
mem_heap_t *heap; |
| 1073 |
dberr_t err = DB_SUCCESS; |
| 1074 |
bool non_mv_upd = true; |
| 1075 |
|
| 1076 |
if (node->index == nullptr || ((node->cmpl_info & UPD_NODE_NO_ORD_CHANGE))) { |
| 1077 |
/* No change in secondary indexes */ |
| 1078 |
|
| 1079 |
return (err); |
| 1080 |
} |
| 1081 |
|
| 1082 |
heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 1083 |
|
| 1084 |
while (node->index != nullptr) { |
| 1085 |
dict_index_t *index = node->index; |
| 1086 |
dtuple_t *entry; |
| 1087 |
|
| 1088 |
if (dict_index_is_spatial(index)) { |
| 1089 |
if (!row_upd_changes_ord_field_binary_func( |
| 1090 |
index, node->update, IF_DEBUG(thr, ) node->row, node->ext, |
| 1091 |
nullptr, ROW_BUILD_FOR_UNDO)) { |
| 1092 |
dict_table_next_uncorrupted_index(node->index); |
| 1093 |
continue; |
| 1094 |
} |
| 1095 |
} else { |
| 1096 |
if (index->type == DICT_FTS || |
| 1097 |
!row_upd_changes_ord_field_binary( |
| 1098 |
index, node->update, thr, node->row, node->ext, |
| 1099 |
(index->is_multi_value() ? &non_mv_upd : nullptr))) { |
| 1100 |
dict_table_next_uncorrupted_index(node->index); |
| 1101 |
continue; |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
if (index->is_multi_value()) { |
| 1106 |
err = |
| 1107 |
row_undo_mod_upd_exist_multi_sec(node, thr, index, non_mv_upd, heap); |
| 1108 |
mem_heap_empty(heap); |
| 1109 |
dict_table_next_uncorrupted_index(node->index); |
| 1110 |
continue; |
| 1111 |
} |
| 1112 |
|
| 1113 |
/* Build the newest version of the index entry */ |
| 1114 |
entry = row_build_index_entry(node->row, node->ext, index, heap); |
| 1115 |
if (UNIV_UNLIKELY(!entry)) { |
| 1116 |
/* The server must have crashed in |
| 1117 |
row_upd_clust_rec_by_insert() before |
| 1118 |
the updated externally stored columns (BLOBs) |
| 1119 |
of the new clustered index entry were written. */ |
| 1120 |
|
| 1121 |
/* The table must be in DYNAMIC or COMPRESSED |
| 1122 |
format. REDUNDANT and COMPACT formats |
| 1123 |
store a local 768-byte prefix of each |
| 1124 |
externally stored column. */ |
| 1125 |
ut_a(dict_table_has_atomic_blobs(index->table)); |
| 1126 |
|
| 1127 |
/* This is only legitimate when |
| 1128 |
rolling back an incomplete transaction |
| 1129 |
after crash recovery. */ |
| 1130 |
ut_a(thr_get_trx(thr)->is_recovered); |
| 1131 |
|
| 1132 |
/* The server must have crashed before |
| 1133 |
completing the insert of the new |
| 1134 |
clustered index entry and before |
| 1135 |
inserting to the secondary indexes. |
| 1136 |
Because node->row was not yet written |
| 1137 |
to this index, we can ignore it. But |
| 1138 |
we must restore node->undo_row. */ |
| 1139 |
} else { |
| 1140 |
/* NOTE that if we updated the fields of a |
| 1141 |
delete-marked secondary index record so that |
| 1142 |
alphabetically they stayed the same, e.g., |
| 1143 |
'abc' -> 'aBc', we cannot return to the |
| 1144 |
original values because we do not know them. |
| 1145 |
But this should not cause problems because |
| 1146 |
in row0sel.cc, in queries we always retrieve |
| 1147 |
the clustered index record or an earlier |
| 1148 |
version of it, if the secondary index record |
| 1149 |
through which we do the search is |
| 1150 |
delete-marked. */ |
| 1151 |
|
| 1152 |
err = row_undo_mod_del_mark_or_remove_sec(node, thr, index, entry); |
| 1153 |
if (err != DB_SUCCESS) { |
| 1154 |
break; |
| 1155 |
} |
| 1156 |
} |
| 1157 |
|
| 1158 |
mem_heap_empty(heap); |
| 1159 |
/* We may have to update the delete mark in the |
| 1160 |
secondary index record of the previous version of |
| 1161 |
the row. We also need to update the fields of |
| 1162 |
the secondary index record if we updated its fields |
| 1163 |
but alphabetically they stayed the same, e.g., |
| 1164 |
'abc' -> 'aBc'. */ |
| 1165 |
if (dict_index_is_spatial(index)) { |
| 1166 |
entry = row_build_index_entry_low(node->undo_row, node->undo_ext, index, |
| 1167 |
heap, ROW_BUILD_FOR_UNDO); |
| 1168 |
} else { |
| 1169 |
entry = |
| 1170 |
row_build_index_entry(node->undo_row, node->undo_ext, index, heap); |
| 1171 |
} |
| 1172 |
|
| 1173 |
ut_a(entry); |
| 1174 |
|
| 1175 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 1176 |
BTR_MODIFY_LEAF, thr, index, entry, node->undo_no); |
| 1177 |
if (err == DB_FAIL) { |
| 1178 |
err = row_undo_mod_del_unmark_sec_and_undo_update( |
| 1179 |
BTR_MODIFY_TREE, thr, index, entry, node->undo_no); |
| 1180 |
} |
| 1181 |
|
| 1182 |
if (err == DB_DUPLICATE_KEY) { |
| 1183 |
row_undo_mod_sec_flag_corrupted(thr_get_trx(thr), index); |
| 1184 |
err = DB_SUCCESS; |
| 1185 |
} else if (err != DB_SUCCESS) { |
| 1186 |
break; |
| 1187 |
} |
| 1188 |
|
| 1189 |
mem_heap_empty(heap); |
| 1190 |
dict_table_next_uncorrupted_index(node->index); |
| 1191 |
} |
| 1192 |
|
| 1193 |
mem_heap_free(heap); |
| 1194 |
|
| 1195 |
return (err); |
| 1196 |
} |
| 1197 |
|
| 1198 |
/** Parses the row reference and other info in a modify undo log record. |
| 1199 |
@param[in] node row undo node |
| 1200 |
@param[in] thd THD associated with the node |
| 1201 |
@param[in,out] mdl MDL ticket or nullptr if unnecessary */ |
| 1202 |
static void row_undo_mod_parse_undo_rec(undo_node_t *node, THD *thd, |
| 1203 |
MDL_ticket **mdl) { |
| 1204 |
dict_index_t *clust_index; |
| 1205 |
byte *ptr; |
| 1206 |
undo_no_t undo_no; |
| 1207 |
table_id_t table_id; |
| 1208 |
trx_id_t trx_id; |
| 1209 |
roll_ptr_t roll_ptr; |
| 1210 |
ulint info_bits; |
| 1211 |
ulint type; |
| 1212 |
ulint cmpl_info; |
| 1213 |
bool dummy_extern; |
| 1214 |
type_cmpl_t type_cmpl; |
| 1215 |
|
| 1216 |
ptr = trx_undo_rec_get_pars(node->undo_rec, &type, &cmpl_info, &dummy_extern, |
| 1217 |
&undo_no, &table_id, type_cmpl); |
| 1218 |
node->rec_type = type; |
| 1219 |
|
| 1220 |
/* Although table IX lock is held now, DROP TABLE could still be |
| 1221 |
done concurrently. To prevent this, MDL for this table should be |
| 1222 |
took here. Notably, there cannot be a race between ROLLBACK and |
| 1223 |
DROP TEMPORARY TABLE, because temporary tables are |
| 1224 |
private to a single connection. */ |
| 1225 |
node->table = dd_table_open_on_id(table_id, thd, mdl, false, true); |
| 1226 |
|
| 1227 |
if (node->table == nullptr) { |
| 1228 |
/* Table was dropped */ |
| 1229 |
return; |
| 1230 |
} |
| 1231 |
|
| 1232 |
if (node->table->ibd_file_missing) { |
| 1233 |
dd_table_close(node->table, thd, mdl, false); |
| 1234 |
|
| 1235 |
/* We skip undo operations to missing .ibd files */ |
| 1236 |
node->table = nullptr; |
| 1237 |
|
| 1238 |
return; |
| 1239 |
} |
| 1240 |
|
| 1241 |
ut_ad(!node->table->skip_alter_undo); |
| 1242 |
|
| 1243 |
clust_index = node->table->first_index(); |
| 1244 |
|
| 1245 |
ptr = trx_undo_update_rec_get_sys_cols(ptr, &trx_id, &roll_ptr, &info_bits); |
| 1246 |
|
| 1247 |
ptr = trx_undo_rec_get_row_ref(ptr, clust_index, &(node->ref), node->heap); |
| 1248 |
|
| 1249 |
ptr = trx_undo_update_rec_get_update(ptr, clust_index, type, trx_id, roll_ptr, |
| 1250 |
info_bits, node->heap, &(node->update), |
| 1251 |
nullptr, type_cmpl); |
| 1252 |
|
| 1253 |
node->new_trx_id = trx_id; |
| 1254 |
node->cmpl_info = cmpl_info; |
| 1255 |
|
| 1256 |
if (!row_undo_search_clust_to_pcur(node)) { |
| 1257 |
dd_table_close(node->table, thd, mdl, false); |
| 1258 |
|
| 1259 |
node->table = nullptr; |
| 1260 |
} |
| 1261 |
|
| 1262 |
/* Extract indexed virtual columns from undo log */ |
| 1263 |
if (node->table && node->table->n_v_cols) { |
| 1264 |
row_upd_replace_vcol( |
| 1265 |
node->row, node->table, node->update, false, node->undo_row, |
| 1266 |
(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE) ? nullptr : ptr); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
/** Undoes a modify operation on a row of a table. |
| 1271 |
@return DB_SUCCESS or error code */ |
| 1272 |
dberr_t row_undo_mod(undo_node_t *node, /*!< in: row undo node */ |
| 1273 |
que_thr_t *thr) /*!< in: query thread */ |
| 1274 |
{ |
| 1275 |
dberr_t err; |
| 1276 |
MDL_ticket *mdl = nullptr; |
| 1277 |
|
| 1278 |
ut_ad(node != nullptr); |
| 1279 |
ut_ad(thr != nullptr); |
| 1280 |
ut_ad(node->state == UNDO_NODE_MODIFY); |
| 1281 |
ut_ad(node->trx->in_rollback); |
| 1282 |
ut_ad(!trx_undo_roll_ptr_is_insert(node->roll_ptr)); |
| 1283 |
|
| 1284 |
ut_ad(thr_get_trx(thr) == node->trx); |
| 1285 |
|
| 1286 |
THD *thd = dd_thd_for_undo(node->trx); |
| 1287 |
|
| 1288 |
row_undo_mod_parse_undo_rec(node, thd, |
| 1289 |
dd_mdl_for_undo(node->trx) ? &mdl : nullptr); |
| 1290 |
|
| 1291 |
if (node->table == nullptr) { |
| 1292 |
/* It is already undone, or will be undone by another query |
| 1293 |
thread, or table was dropped */ |
| 1294 |
|
| 1295 |
node->state = UNDO_NODE_FETCH_NEXT; |
| 1296 |
|
| 1297 |
return (DB_SUCCESS); |
| 1298 |
} |
| 1299 |
|
| 1300 |
node->index = node->table->first_index(); |
| 1301 |
ut_ad(node->index->is_clustered()); |
| 1302 |
/* Skip the clustered index (the first index) */ |
| 1303 |
node->index = node->index->next(); |
| 1304 |
|
| 1305 |
/* Skip all corrupted secondary index */ |
| 1306 |
dict_table_skip_corrupt_index(node->index); |
| 1307 |
|
| 1308 |
switch (node->rec_type) { |
| 1309 |
case TRX_UNDO_UPD_EXIST_REC: |
| 1310 |
err = row_undo_mod_upd_exist_sec(node, thr); |
| 1311 |
break; |
| 1312 |
case TRX_UNDO_DEL_MARK_REC: |
| 1313 |
err = row_undo_mod_del_mark_sec(node, thr); |
| 1314 |
break; |
| 1315 |
case TRX_UNDO_UPD_DEL_REC: |
| 1316 |
err = row_undo_mod_upd_del_sec(node, thr); |
| 1317 |
break; |
| 1318 |
default: |
| 1319 |
ut_error; |
| 1320 |
} |
| 1321 |
|
| 1322 |
if (err == DB_SUCCESS) { |
| 1323 |
err = row_undo_mod_clust(node, thr); |
| 1324 |
} |
| 1325 |
|
| 1326 |
dd_table_close(node->table, thd, &mdl, false); |
| 1327 |
|
| 1328 |
node->table = nullptr; |
| 1329 |
|
| 1330 |
return (err); |
| 1331 |
} |