Debian Codegraph @

storage/innobase/row/row0uins.cc

raw
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/row0uins.cc
29 Fresh insert undo
30
31 Created 2/25/1997 Heikki Tuuri
32 *******************************************************/
33
34 #include "row0uins.h"
35
36 #include "btr0btr.h"
37 #include "dict0boot.h"
38 #include "dict0crea.h"
39 #include "dict0dd.h"
40 #include "dict0dict.h"
41 #include "ibuf0ibuf.h"
42 #include "log0chkp.h"
43 #include "mach0data.h"
44 #include "que0que.h"
45 #include "row0log.h"
46 #include "row0row.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 /*************************************************************************
56 IMPORTANT NOTE: Any operation that generates redo MUST check that there
57 is enough space in the redo log before for that operation. This is
58 done by calling log_free_check(). The reason for checking the
59 availability of the redo log space before the start of the operation is
60 that we MUST not hold any synchronization objects when performing the
61 check.
62 If you make a change in this module make sure that no codepath is
63 introduced where a call to log_free_check() is bypassed. */
64
65 /** Removes a clustered index record. The pcur in node was positioned on the
66 record, now it is detached.
67 @return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
68 [[nodiscard]] static dberr_t row_undo_ins_remove_clust_rec(
69 undo_node_t *node) /*!< in: undo node */
70 {
71 btr_cur_t *btr_cur;
72 dberr_t err;
73 ulint n_tries = 0;
74 mtr_t mtr;
75 dict_index_t *index = node->pcur.m_btr_cur.index;
76 bool online;
77
78 ut_ad(index->is_clustered());
79 ut_ad(node->trx->in_rollback);
80
81 mtr_start(&mtr);
82
83 dict_disable_redo_if_temporary(index->table, &mtr);
84
85 /* This is similar to row_undo_mod_clust(). The DDL thread may
86 already have copied this row from the log to the new table.
87 We must log the removal, so that the row will be correctly
88 purged. However, we can log the removal out of sync with the
89 B-tree modification. */
90
91 online = dict_index_is_online_ddl(index);
92 if (online) {
93 ut_ad(node->trx->dict_operation_lock_mode != RW_X_LATCH);
94 ut_ad(node->table->id != DICT_INDEXES_ID);
95 mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
96 }
97
98 auto success = node->pcur.restore_position(
99 online ? BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED : BTR_MODIFY_LEAF, &mtr,
100 UT_LOCATION_HERE);
101 ut_a(success);
102
103 btr_cur = node->pcur.get_btr_cur();
104
105 ut_ad(rec_get_trx_id(btr_cur_get_rec(btr_cur), btr_cur->index) ==
106 node->trx->id);
107 ut_ad(!rec_get_deleted_flag(btr_cur_get_rec(btr_cur),
108 dict_table_is_comp(btr_cur->index->table)));
109
110 if (online && dict_index_is_online_ddl(index)) {
111 const rec_t *rec = btr_cur_get_rec(btr_cur);
112 mem_heap_t *heap = nullptr;
113 const ulint *offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED,
114 UT_LOCATION_HERE, &heap);
115 row_log_table_delete(rec, node->row, index, offsets, nullptr);
116 mem_heap_free(heap);
117 }
118
119 row_convert_impl_to_expl_if_needed(btr_cur, node);
120 if (btr_cur_optimistic_delete(btr_cur, 0, &mtr)) {
121 err = DB_SUCCESS;
122 goto func_exit;
123 }
124
125 node->pcur.commit_specify_mtr(&mtr);
126 retry:
127 /* If did not succeed, try pessimistic descent to tree */
128 mtr_start(&mtr);
129
130 dict_disable_redo_if_temporary(index->table, &mtr);
131
132 success = node->pcur.restore_position(BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE,
133 &mtr, UT_LOCATION_HERE);
134 ut_a(success);
135
136 btr_cur_pessimistic_delete(&err, false, btr_cur, 0, true, node->trx->id,
137 node->undo_no, node->rec_type, &mtr, &node->pcur,
138 nullptr);
139
140 /* The delete operation may fail if we have little
141 file space left: TODO: easiest to crash the database
142 and restart with more file space */
143
144 if (err == DB_OUT_OF_FILE_SPACE && n_tries < BTR_CUR_RETRY_DELETE_N_TIMES) {
145 node->pcur.commit_specify_mtr(&mtr);
146
147 n_tries++;
148
149 std::this_thread::sleep_for(
150 std::chrono::milliseconds(BTR_CUR_RETRY_SLEEP_TIME_MS));
151
152 goto retry;
153 }
154
155 func_exit:
156 node->pcur.commit_specify_mtr(&mtr);
157
158 return (err);
159 }
160
161 /** Removes a secondary index entry if found.
162 @param[in] mode BTR_MODIFY_LEAF or BTR_MODIFY_TREE,
163 depending on whether we wish optimistic or
164 pessimistic descent down the index tree
165 @param[in] index index
166 @param[in] entry index entry to remove
167 @param[in] thr query thread
168 @param[in] node undo node
169 @return DB_SUCCESS, DB_FAIL, or DB_OUT_OF_FILE_SPACE */
170 [[nodiscard]] static dberr_t row_undo_ins_remove_sec_low(ulint mode,
171 dict_index_t *index,
172 dtuple_t *entry,
173 que_thr_t *thr,
174 undo_node_t *node) {
175 btr_pcur_t pcur;
176 btr_cur_t *btr_cur;
177 dberr_t err = DB_SUCCESS;
178 mtr_t mtr;
179 enum row_search_result search_result;
180 bool modify_leaf = false;
181 ulint rec_deleted;
182
183 log_free_check();
184
185 mtr_start(&mtr);
186
187 dict_disable_redo_if_temporary(index->table, &mtr);
188
189 if (mode == BTR_MODIFY_LEAF) {
190 mode = BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED;
191 mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
192 modify_leaf = true;
193 } else {
194 ut_ad(mode == (BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE));
195 mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
196 }
197
198 if (row_log_online_op_try(index, entry, 0)) {
199 goto func_exit_no_pcur;
200 }
201
202 if (dict_index_is_spatial(index)) {
203 if (mode & BTR_MODIFY_LEAF) {
204 mode |= BTR_RTREE_DELETE_MARK;
205 }
206 pcur.get_btr_cur()->thr = thr;
207 mode |= BTR_RTREE_UNDO_INS;
208 }
209
210 search_result = row_search_index_entry(index, entry, mode, &pcur, &mtr);
211
212 switch (search_result) {
213 case ROW_NOT_FOUND:
214 goto func_exit;
215 case ROW_FOUND:
216 break;
217
218 case ROW_BUFFERED:
219 case ROW_NOT_DELETED_REF:
220 /* These are invalid outcomes, because the mode passed
221 to row_search_index_entry() did not include any of the
222 flags BTR_INSERT, BTR_DELETE, or BTR_DELETE_MARK. */
223 ut_error;
224 }
225
226 rec_deleted =
227 rec_get_deleted_flag(pcur.get_rec(), dict_table_is_comp(index->table));
228
229 if (search_result == ROW_FOUND && dict_index_is_spatial(index)) {
230 if (rec_deleted) {
231 ib::error(ER_IB_MSG_1036) << "Record found in index " << index->name
232 << " is deleted marked on insert rollback.";
233 }
234 }
235
236 btr_cur = pcur.get_btr_cur();
237
238 if (rec_deleted == 0) {
239 /* This record is not delete marked and has an implicit
240 lock on it. For delete marked record, INSERT has not
241 modified it yet and we don't have implicit lock on it.
242 We must convert to explicit if and only if we have
243 implicit lock on the record.*/
244 row_convert_impl_to_expl_if_needed(btr_cur, node);
245 }
246
247 if (modify_leaf) {
248 err = btr_cur_optimistic_delete(btr_cur, 0, &mtr) ? DB_SUCCESS : DB_FAIL;
249 } else {
250 /* Passing rollback=false here, because we are
251 deleting a secondary index record: the distinction
252 only matters when deleting a record that contains
253 externally stored columns. */
254 ut_ad(!index->is_clustered());
255 btr_cur_pessimistic_delete(&err, false, btr_cur, 0, false, 0, 0, 0, &mtr,
256 &pcur, nullptr);
257 }
258 func_exit:
259 pcur.close();
260 func_exit_no_pcur:
261 mtr_commit(&mtr);
262
263 return (err);
264 }
265
266 /** Removes a secondary index entry from the index if found. Tries first
267 optimistic, then pessimistic descent down the tree.
268 @param[in] index index
269 @param[in] entry index entry to insert
270 @param[in] thr query thread
271 @param[in] node undo node
272 @return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
273 [[nodiscard]] static dberr_t row_undo_ins_remove_sec(dict_index_t *index,
274 dtuple_t *entry,
275 que_thr_t *thr,
276 undo_node_t *node) {
277 dberr_t err;
278 ulint n_tries = 0;
279
280 /* Try first optimistic descent to the B-tree */
281
282 err = row_undo_ins_remove_sec_low(BTR_MODIFY_LEAF, index, entry, thr, node);
283
284 if (err == DB_SUCCESS) {
285 return (err);
286 }
287
288 /* Try then pessimistic descent to the B-tree */
289 retry:
290 err = row_undo_ins_remove_sec_low(BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE,
291 index, entry, thr, node);
292
293 /* The delete operation may fail if we have little
294 file space left: TODO: easiest to crash the database
295 and restart with more file space */
296
297 if (err != DB_SUCCESS && n_tries < BTR_CUR_RETRY_DELETE_N_TIMES) {
298 n_tries++;
299
300 std::this_thread::sleep_for(
301 std::chrono::milliseconds(BTR_CUR_RETRY_SLEEP_TIME_MS));
302
303 goto retry;
304 }
305
306 return (err);
307 }
308
309 /** Parses the row reference and other info in a fresh insert undo record.
310 @param[in,out] node row undo node
311 @param[in] thd THD associated with the node
312 @param[in,out] mdl MDL ticket or nullptr if unnecessary */
313 static void row_undo_ins_parse_undo_rec(undo_node_t *node, THD *thd,
314 MDL_ticket **mdl) {
315 dict_index_t *clust_index;
316 byte *ptr;
317 undo_no_t undo_no;
318 table_id_t table_id;
319 ulint type;
320 ulint dummy;
321 bool dummy_extern;
322 type_cmpl_t type_cmpl;
323
324 ut_ad(node);
325
326 ptr = trx_undo_rec_get_pars(node->undo_rec, &type, &dummy, &dummy_extern,
327 &undo_no, &table_id, type_cmpl);
328 ut_ad(type == TRX_UNDO_INSERT_REC);
329 node->rec_type = type;
330
331 node->update = nullptr;
332
333 node->table = dd_table_open_on_id(table_id, thd, mdl, false, true);
334
335 /* Skip the UNDO if we can't find the table or the .ibd file. */
336 if (node->table == nullptr) {
337 } else if (node->table->ibd_file_missing) {
338 close_table:
339 dd_table_close(node->table, thd, mdl, false);
340
341 node->table = nullptr;
342 } else {
343 ut_ad(!node->table->skip_alter_undo);
344
345 clust_index = node->table->first_index();
346
347 if (clust_index != nullptr) {
348 ptr = trx_undo_rec_get_row_ref(ptr, clust_index, &node->ref, node->heap);
349
350 if (!row_undo_search_clust_to_pcur(node)) {
351 goto close_table;
352 }
353 if (node->table->n_v_cols) {
354 trx_undo_read_v_cols(node->table, ptr, node->row, false, false, nullptr,
355 node->heap);
356 }
357
358 } else {
359 ib::warn(ER_IB_MSG_1037) << "Table " << node->table->name
360 << " has no indexes,"
361 " ignoring the table";
362 goto close_table;
363 }
364 }
365 }
366
367 /** Removes a secondary index entry from the index, which is built on
368 multi-value field, if found. For each value, it tries first optimistic,
369 then pessimistic descent down the tree.
370 @param[in,out] index multi-value index
371 @param[in] node undo node
372 @param[in] thr query thread
373 @param[in,out] heap memory heap
374 @return DB_SUCCESS or error code */
375 static dberr_t row_undo_ins_remove_multi_sec(dict_index_t *index,
376 undo_node_t *node, que_thr_t *thr,
377 mem_heap_t *heap) {
378 dberr_t err = DB_SUCCESS;
379 Multi_value_entry_builder_normal mv_entry_builder(node->row, node->ext, index,
380 heap, true, false);
381
382 ut_ad(index->is_multi_value());
383
384 for (dtuple_t *entry = mv_entry_builder.begin(); entry != nullptr;
385 entry = mv_entry_builder.next()) {
386 err = row_undo_ins_remove_sec(index, entry, thr, node);
387 if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
388 break;
389 }
390 }
391
392 return (err);
393 }
394
395 /** Removes secondary index records.
396 @return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
397 [[nodiscard]] static dberr_t row_undo_ins_remove_sec_rec(
398 undo_node_t *node, /*!< in/out: row undo node */
399 que_thr_t *thr) /*!< in: query thread */
400 {
401 dberr_t err = DB_SUCCESS;
402 dict_index_t *index = node->index;
403 mem_heap_t *heap;
404
405 heap = mem_heap_create(1024, UT_LOCATION_HERE);
406
407 while (index != nullptr) {
408 dtuple_t *entry;
409
410 if (index->type & DICT_FTS) {
411 dict_table_next_uncorrupted_index(index);
412 continue;
413 }
414
415 if (index->is_multi_value()) {
416 err = row_undo_ins_remove_multi_sec(index, node, thr, heap);
417 if (err != DB_SUCCESS) {
418 goto func_exit;
419 }
420 mem_heap_empty(heap);
421 dict_table_next_uncorrupted_index(index);
422 continue;
423 }
424
425 /* An insert undo record TRX_UNDO_INSERT_REC will
426 always contain all fields of the index. It does not
427 matter if any indexes were created afterwards; all
428 index entries can be reconstructed from the row. */
429 entry = row_build_index_entry(node->row, node->ext, index, heap);
430 if (UNIV_UNLIKELY(!entry)) {
431 /* The database must have crashed after
432 inserting a clustered index record but before
433 writing all the externally stored columns of
434 that record, or a statement is being rolled
435 back because an error occurred while storing
436 off-page columns.
437
438 Because secondary index entries are inserted
439 after the clustered index record, we may
440 assume that the secondary index record does
441 not exist. */
442 } else {
443 err = row_undo_ins_remove_sec(index, entry, thr, node);
444
445 if (UNIV_UNLIKELY(err != DB_SUCCESS)) {
446 goto func_exit;
447 }
448 }
449
450 mem_heap_empty(heap);
451 dict_table_next_uncorrupted_index(index);
452 }
453
454 func_exit:
455 node->index = index;
456 mem_heap_free(heap);
457 return (err);
458 }
459
460 /** Undoes a fresh insert of a row to a table. A fresh insert means that
461 the same clustered index unique key did not have any record, even delete
462 marked, at the time of the insert. InnoDB is eager in a rollback:
463 if it figures out that an index record will be removed in the purge
464 anyway, it will remove it in the rollback.
465 @return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */
466 dberr_t row_undo_ins(undo_node_t *node, /*!< in: row undo node */
467 que_thr_t *thr) /*!< in: query thread */
468 {
469 dberr_t err;
470 MDL_ticket *mdl = nullptr;
471
472 ut_ad(node->state == UNDO_NODE_INSERT);
473 ut_ad(node->trx->in_rollback);
474 ut_ad(trx_undo_roll_ptr_is_insert(node->roll_ptr));
475
476 THD *thd = dd_thd_for_undo(node->trx);
477
478 row_undo_ins_parse_undo_rec(node, thd,
479 dd_mdl_for_undo(node->trx) ? &mdl : nullptr);
480
481 if (node->table == nullptr) {
482 return (DB_SUCCESS);
483 }
484
485 /* Iterate over all the indexes and undo the insert.*/
486
487 node->index = node->table->first_index();
488 ut_ad(node->index->is_clustered());
489 /* Skip the clustered index (the first index) */
490 node->index = node->index->next();
491
492 dict_table_skip_corrupt_index(node->index);
493
494 err = row_undo_ins_remove_sec_rec(node, thr);
495
496 if (err == DB_SUCCESS) {
497 log_free_check();
498
499 // FIXME: We need to update the dict_index_t::space and
500 // page number fields too.
501 err = row_undo_ins_remove_clust_rec(node);
502 }
503
504 dd_table_close(node->table, thd, &mdl, false);
505
506 node->table = nullptr;
507
508 return (err);
509 }