| 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/row0undo.cc |
| 29 |
Row undo |
| 30 |
|
| 31 |
Created 1/8/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include <stddef.h> |
| 35 |
|
| 36 |
#include "fsp0fsp.h" |
| 37 |
#include "ha_prototypes.h" |
| 38 |
#include "mach0data.h" |
| 39 |
#include "que0que.h" |
| 40 |
#include "row0mysql.h" |
| 41 |
#include "row0row.h" |
| 42 |
#include "row0uins.h" |
| 43 |
#include "row0umod.h" |
| 44 |
#include "row0undo.h" |
| 45 |
#include "row0upd.h" |
| 46 |
#include "srv0srv.h" |
| 47 |
#include "trx0purge.h" |
| 48 |
#include "trx0rec.h" |
| 49 |
#include "trx0roll.h" |
| 50 |
#include "trx0rseg.h" |
| 51 |
#include "trx0trx.h" |
| 52 |
#include "trx0undo.h" |
| 53 |
|
| 54 |
/* How to undo row operations? |
| 55 |
(1) For an insert, we have stored a prefix of the clustered index record |
| 56 |
in the undo log. Using it, we look for the clustered record, and using |
| 57 |
that we look for the records in the secondary indexes. The insert operation |
| 58 |
may have been left incomplete, if the database crashed, for example. |
| 59 |
We may have look at the trx id and roll ptr to make sure the record in the |
| 60 |
clustered index is really the one for which the undo log record was |
| 61 |
written. We can use the framework we get from the original insert op. |
| 62 |
(2) Delete marking: We can use the framework we get from the original |
| 63 |
delete mark op. We only have to check the trx id. |
| 64 |
(3) Update: This may be the most complicated. We have to use the framework |
| 65 |
we get from the original update op. |
| 66 |
|
| 67 |
What if the same trx repeatedly deletes and inserts an identical row. |
| 68 |
Then the row id changes and also roll ptr. What if the row id was not |
| 69 |
part of the ordering fields in the clustered index? Maybe we have to write |
| 70 |
it to undo log. Well, maybe not, because if we order the row id and trx id |
| 71 |
in descending order, then the only undeleted copy is the first in the |
| 72 |
index. Our searches in row operations always position the cursor before |
| 73 |
the first record in the result set. But, if there is no key defined for |
| 74 |
a table, then it would be desirable that row id is in ascending order. |
| 75 |
So, lets store row id in descending order only if it is not an ordering |
| 76 |
field in the clustered index. |
| 77 |
|
| 78 |
NOTE: Deletes and inserts may lead to situation where there are identical |
| 79 |
records in a secondary index. Is that a problem in the B-tree? Yes. |
| 80 |
Also updates can lead to this, unless trx id and roll ptr are included in |
| 81 |
ord fields. |
| 82 |
(1) Fix in clustered indexes: include row id, trx id, and roll ptr |
| 83 |
in node pointers of B-tree. |
| 84 |
(2) Fix in secondary indexes: include all fields in node pointers, and |
| 85 |
if an entry is inserted, check if it is equal to the right neighbor, |
| 86 |
in which case update the right neighbor: the neighbor must be delete |
| 87 |
marked, set it unmarked and write the trx id of the current transaction. |
| 88 |
|
| 89 |
What if the same trx repeatedly updates the same row, updating a secondary |
| 90 |
index field or not? Updating a clustered index ordering field? |
| 91 |
|
| 92 |
(1) If it does not update the secondary index and not the clustered index |
| 93 |
ord field. Then the secondary index record stays unchanged, but the |
| 94 |
trx id in the secondary index record may be smaller than in the clustered |
| 95 |
index record. This is no problem? |
| 96 |
(2) If it updates secondary index ord field but not clustered: then in |
| 97 |
secondary index there are delete marked records, which differ in an |
| 98 |
ord field. No problem. |
| 99 |
(3) Updates clustered ord field but not secondary, and secondary index |
| 100 |
is unique. Then the record in secondary index is just updated at the |
| 101 |
clustered ord field. |
| 102 |
(4) |
| 103 |
|
| 104 |
Problem with duplicate records: |
| 105 |
Fix 1: Add a trx op no field to all indexes. A problem: if a trx with a |
| 106 |
bigger trx id has inserted and delete marked a similar row, our trx inserts |
| 107 |
again a similar row, and a trx with an even bigger id delete marks it. Then |
| 108 |
the position of the row should change in the index if the trx id affects |
| 109 |
the alphabetical ordering. |
| 110 |
|
| 111 |
Fix 2: If an insert encounters a similar row marked deleted, we turn the |
| 112 |
insert into an 'update' of the row marked deleted. Then we must write undo |
| 113 |
info on the update. A problem: what if a purge operation tries to remove |
| 114 |
the delete marked row? |
| 115 |
|
| 116 |
We can think of the database row versions as a linked list which starts |
| 117 |
from the record in the clustered index, and is linked by roll ptrs |
| 118 |
through undo logs. The secondary index records are references which tell |
| 119 |
what kinds of records can be found in this linked list for a record |
| 120 |
in the clustered index. |
| 121 |
|
| 122 |
How to do the purge? A record can be removed from the clustered index |
| 123 |
if its linked list becomes empty, i.e., the row has been marked deleted |
| 124 |
and its roll ptr points to the record in the undo log we are going through, |
| 125 |
doing the purge. Similarly, during a rollback, a record can be removed |
| 126 |
if the stored roll ptr in the undo log points to a trx already (being) purged, |
| 127 |
or if the roll ptr is NULL, i.e., it was a fresh insert. */ |
| 128 |
|
| 129 |
undo_node_t *row_undo_node_create(trx_t *trx, que_thr_t *parent, |
| 130 |
mem_heap_t *heap, bool partial_rollback) { |
| 131 |
undo_node_t *undo; |
| 132 |
|
| 133 |
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE) || |
| 134 |
trx_state_eq(trx, TRX_STATE_PREPARED)); |
| 135 |
ut_ad(parent); |
| 136 |
|
| 137 |
undo = static_cast<undo_node_t *>(mem_heap_alloc(heap, sizeof(undo_node_t))); |
| 138 |
|
| 139 |
undo->common.type = QUE_NODE_UNDO; |
| 140 |
undo->common.parent = parent; |
| 141 |
|
| 142 |
undo->state = UNDO_NODE_FETCH_NEXT; |
| 143 |
undo->trx = trx; |
| 144 |
|
| 145 |
undo->partial = partial_rollback; |
| 146 |
undo->pcur.init(); |
| 147 |
|
| 148 |
undo->heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 149 |
|
| 150 |
return (undo); |
| 151 |
} |
| 152 |
|
| 153 |
/** Looks for the clustered index record when node has the row reference. |
| 154 |
The pcur in node is used in the search. If found, stores the row to node, |
| 155 |
and stores the position of pcur, and detaches it. The pcur must be closed |
| 156 |
by the caller in any case. |
| 157 |
@return true if found; NOTE the node->pcur must be closed by the |
| 158 |
caller, regardless of the return value */ |
| 159 |
bool row_undo_search_clust_to_pcur( |
| 160 |
undo_node_t *node) /*!< in/out: row undo node */ |
| 161 |
{ |
| 162 |
dict_index_t *clust_index; |
| 163 |
bool found; |
| 164 |
mtr_t mtr; |
| 165 |
row_ext_t **ext; |
| 166 |
const rec_t *rec; |
| 167 |
mem_heap_t *heap = nullptr; |
| 168 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 169 |
ulint *offsets = offsets_; |
| 170 |
rec_offs_init(offsets_); |
| 171 |
|
| 172 |
ut_ad(!node->table->skip_alter_undo); |
| 173 |
|
| 174 |
mtr_start(&mtr); |
| 175 |
dict_disable_redo_if_temporary(node->table, &mtr); |
| 176 |
|
| 177 |
clust_index = node->table->first_index(); |
| 178 |
|
| 179 |
found = row_search_on_row_ref(&node->pcur, BTR_MODIFY_LEAF, node->table, |
| 180 |
node->ref, &mtr); |
| 181 |
|
| 182 |
if (!found) { |
| 183 |
goto func_exit; |
| 184 |
} |
| 185 |
|
| 186 |
rec = node->pcur.get_rec(); |
| 187 |
|
| 188 |
offsets = rec_get_offsets(rec, clust_index, offsets, ULINT_UNDEFINED, |
| 189 |
UT_LOCATION_HERE, &heap); |
| 190 |
|
| 191 |
found = row_get_rec_roll_ptr(rec, clust_index, offsets) == node->roll_ptr; |
| 192 |
|
| 193 |
if (found) { |
| 194 |
ut_ad(row_get_rec_trx_id(rec, clust_index, offsets) == node->trx->id); |
| 195 |
|
| 196 |
if (dict_table_has_atomic_blobs(node->table)) { |
| 197 |
/* There is no prefix of externally stored |
| 198 |
columns in the clustered index record. Build a |
| 199 |
cache of column prefixes. */ |
| 200 |
ext = &node->ext; |
| 201 |
} else { |
| 202 |
/* REDUNDANT and COMPACT formats store a local |
| 203 |
768-byte prefix of each externally stored |
| 204 |
column. No cache is needed. */ |
| 205 |
ext = nullptr; |
| 206 |
node->ext = nullptr; |
| 207 |
} |
| 208 |
|
| 209 |
node->row = row_build(ROW_COPY_DATA, clust_index, rec, offsets, nullptr, |
| 210 |
nullptr, nullptr, ext, node->heap); |
| 211 |
|
| 212 |
/* We will need to parse out virtual column info from undo |
| 213 |
log, first mark them DATA_MISSING. So we will know if the |
| 214 |
value gets updated */ |
| 215 |
if (node->table->n_v_cols && node->state != UNDO_NODE_INSERT && |
| 216 |
!(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) { |
| 217 |
for (ulint i = 0; i < dict_table_get_n_v_cols(node->table); i++) { |
| 218 |
dfield_get_type(dtuple_get_nth_v_field(node->row, i))->mtype = |
| 219 |
DATA_MISSING; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
if (node->rec_type == TRX_UNDO_UPD_EXIST_REC) { |
| 224 |
node->undo_row = dtuple_copy(node->row, node->heap); |
| 225 |
row_upd_replace(node->undo_row, &node->undo_ext, clust_index, |
| 226 |
node->update, node->heap); |
| 227 |
} else { |
| 228 |
node->undo_row = nullptr; |
| 229 |
node->undo_ext = nullptr; |
| 230 |
} |
| 231 |
|
| 232 |
node->pcur.store_position(&mtr); |
| 233 |
} |
| 234 |
|
| 235 |
if (heap) { |
| 236 |
mem_heap_free(heap); |
| 237 |
} |
| 238 |
|
| 239 |
func_exit: |
| 240 |
node->pcur.commit_specify_mtr(&mtr); |
| 241 |
return (found); |
| 242 |
} |
| 243 |
|
| 244 |
/** Fetches an undo log record and does the undo for the recorded operation. |
| 245 |
If none left, or a partial rollback completed, returns control to the |
| 246 |
parent node, which is always a query thread node. |
| 247 |
@return DB_SUCCESS if operation successfully completed, else error code */ |
| 248 |
[[nodiscard]] static dberr_t row_undo( |
| 249 |
undo_node_t *node, /*!< in: row undo node */ |
| 250 |
que_thr_t *thr) /*!< in: query thread */ |
| 251 |
{ |
| 252 |
dberr_t err; |
| 253 |
trx_t *trx; |
| 254 |
roll_ptr_t roll_ptr; |
| 255 |
|
| 256 |
ut_ad(node != nullptr); |
| 257 |
ut_ad(thr != nullptr); |
| 258 |
|
| 259 |
trx = node->trx; |
| 260 |
ut_ad(trx->in_rollback); |
| 261 |
|
| 262 |
if (node->state == UNDO_NODE_FETCH_NEXT) { |
| 263 |
node->undo_rec = trx_roll_pop_top_rec_of_trx(trx, trx->roll_limit, |
| 264 |
&roll_ptr, node->heap); |
| 265 |
|
| 266 |
if (!node->undo_rec) { |
| 267 |
/* Rollback completed for this query thread */ |
| 268 |
|
| 269 |
thr->run_node = que_node_get_parent(node); |
| 270 |
|
| 271 |
/* Mark any partial rollback completed, so |
| 272 |
that if the transaction object is committed |
| 273 |
and reused later, the roll_limit will remain |
| 274 |
at 0. trx->roll_limit will be nonzero during a |
| 275 |
partial rollback only. */ |
| 276 |
trx->roll_limit = 0; |
| 277 |
ut_d(trx->in_rollback = false); |
| 278 |
|
| 279 |
return (DB_SUCCESS); |
| 280 |
} |
| 281 |
|
| 282 |
node->roll_ptr = roll_ptr; |
| 283 |
node->undo_no = trx_undo_rec_get_undo_no(node->undo_rec); |
| 284 |
|
| 285 |
if (trx_undo_roll_ptr_is_insert(roll_ptr)) { |
| 286 |
node->state = UNDO_NODE_INSERT; |
| 287 |
} else { |
| 288 |
node->state = UNDO_NODE_MODIFY; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/* During rollback, trx is holding at least LOCK_IX on each |
| 293 |
modified table. It may also hold MDL. A concurrent DROP TABLE |
| 294 |
or ALTER TABLE should be impossible, because it should be |
| 295 |
holding both LOCK_X and MDL_EXCLUSIVE on the table. */ |
| 296 |
if (node->state == UNDO_NODE_INSERT) { |
| 297 |
err = row_undo_ins(node, thr); |
| 298 |
|
| 299 |
node->state = UNDO_NODE_FETCH_NEXT; |
| 300 |
} else { |
| 301 |
ut_ad(node->state == UNDO_NODE_MODIFY); |
| 302 |
err = row_undo_mod(node, thr); |
| 303 |
} |
| 304 |
|
| 305 |
/* Do some cleanup */ |
| 306 |
node->pcur.close(); |
| 307 |
|
| 308 |
mem_heap_empty(node->heap); |
| 309 |
|
| 310 |
thr->run_node = node; |
| 311 |
|
| 312 |
return (err); |
| 313 |
} |
| 314 |
|
| 315 |
void row_convert_impl_to_expl_if_needed(btr_cur_t *cursor, undo_node_t *node) { |
| 316 |
/* In case of partial rollback implicit lock on the |
| 317 |
record is released in the middle of transaction, which |
| 318 |
can break the serializability of IODKU and REPLACE |
| 319 |
statements. Normal rollback is not affected by this |
| 320 |
because we release the locks after the rollback. So |
| 321 |
to prevent any other transaction modifying the record |
| 322 |
in between the partial rollback we convert the implicit |
| 323 |
lock on the record to explicit. When the record is actually |
| 324 |
deleted this lock will be inherited by the next record. */ |
| 325 |
|
| 326 |
if (!node->partial || (node->trx == nullptr) || |
| 327 |
node->trx->isolation_level < trx_t::REPEATABLE_READ) { |
| 328 |
return; |
| 329 |
} |
| 330 |
|
| 331 |
ut_ad(node->trx->in_rollback); |
| 332 |
auto index = cursor->index; |
| 333 |
auto rec = btr_cur_get_rec(cursor); |
| 334 |
auto block = btr_cur_get_block(cursor); |
| 335 |
auto heap_no = page_rec_get_heap_no(rec); |
| 336 |
|
| 337 |
if (heap_no != PAGE_HEAP_NO_SUPREMUM && !dict_index_is_spatial(index) && |
| 338 |
!index->table->is_temporary() && !index->table->is_intrinsic()) { |
| 339 |
lock_rec_convert_impl_to_expl(block, rec, index, |
| 340 |
Rec_offsets().compute(rec, index)); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/** Undoes a row operation in a table. This is a high-level function used |
| 345 |
in SQL execution graphs. |
| 346 |
@return query thread to run next or NULL */ |
| 347 |
que_thr_t *row_undo_step(que_thr_t *thr) /*!< in: query thread */ |
| 348 |
{ |
| 349 |
dberr_t err; |
| 350 |
undo_node_t *node; |
| 351 |
trx_t *trx; |
| 352 |
|
| 353 |
ut_ad(thr); |
| 354 |
|
| 355 |
srv_inc_activity_count(); |
| 356 |
|
| 357 |
trx = thr_get_trx(thr); |
| 358 |
|
| 359 |
node = static_cast<undo_node_t *>(thr->run_node); |
| 360 |
|
| 361 |
ut_ad(que_node_get_type(node) == QUE_NODE_UNDO); |
| 362 |
|
| 363 |
err = row_undo(node, thr); |
| 364 |
|
| 365 |
trx->error_state = err; |
| 366 |
|
| 367 |
if (err != DB_SUCCESS) { |
| 368 |
/* SQL error detected */ |
| 369 |
|
| 370 |
if (err == DB_OUT_OF_FILE_SPACE) { |
| 371 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1041) |
| 372 |
<< "Out of tablespace during rollback." |
| 373 |
" Consider increasing your tablespace."; |
| 374 |
} |
| 375 |
|
| 376 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_1042) |
| 377 |
<< "Error (" << ut_strerr(err) << ") in rollback."; |
| 378 |
} |
| 379 |
|
| 380 |
return (thr); |
| 381 |
} |