| 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/row0purge.cc |
| 29 |
Purge obsolete records |
| 30 |
|
| 31 |
Created 3/14/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "row0purge.h" |
| 35 |
|
| 36 |
#include <stddef.h> |
| 37 |
|
| 38 |
#include "current_thd.h" |
| 39 |
#include "debug_sync.h" |
| 40 |
#include "mysqld.h" |
| 41 |
|
| 42 |
#include "dict0dd.h" |
| 43 |
#include "fsp0fsp.h" |
| 44 |
#include "ha_innodb.h" |
| 45 |
#include "handler.h" |
| 46 |
#include "lob0lob.h" |
| 47 |
#include "log0chkp.h" |
| 48 |
#include "mach0data.h" |
| 49 |
#include "que0que.h" |
| 50 |
#include "row0log.h" |
| 51 |
#include "row0mysql.h" |
| 52 |
#include "row0row.h" |
| 53 |
#include "row0upd.h" |
| 54 |
#include "row0vers.h" |
| 55 |
#include "sql_base.h" |
| 56 |
#include "srv0mon.h" |
| 57 |
#include "srv0start.h" |
| 58 |
#include "sync0types.h" |
| 59 |
#include "table.h" |
| 60 |
#include "trx0purge.h" |
| 61 |
#include "trx0rec.h" |
| 62 |
#include "trx0roll.h" |
| 63 |
#include "trx0rseg.h" |
| 64 |
#include "trx0trx.h" |
| 65 |
#include "trx0undo.h" |
| 66 |
|
| 67 |
/************************************************************************* |
| 68 |
IMPORTANT NOTE: Any operation that generates redo MUST check that there |
| 69 |
is enough space in the redo log before for that operation. This is |
| 70 |
done by calling log_free_check(). The reason for checking the |
| 71 |
availability of the redo log space before the start of the operation is |
| 72 |
that we MUST not hold any synchronization objects when performing the |
| 73 |
check. |
| 74 |
If you make a change in this module make sure that no codepath is |
| 75 |
introduced where a call to log_free_check() is bypassed. */ |
| 76 |
|
| 77 |
/** Create a purge node to a query graph. |
| 78 |
@param[in] parent parent node, i.e., a thr node |
| 79 |
@param[in] heap memory heap where created |
| 80 |
@return own: purge node */ |
| 81 |
purge_node_t *row_purge_node_create(que_thr_t *parent, mem_heap_t *heap) { |
| 82 |
ut_ad(parent != nullptr); |
| 83 |
ut_ad(heap != nullptr); |
| 84 |
|
| 85 |
purge_node_t *node; |
| 86 |
|
| 87 |
node = static_cast<purge_node_t *>(mem_heap_zalloc(heap, sizeof(*node))); |
| 88 |
|
| 89 |
node->common.type = QUE_NODE_PURGE; |
| 90 |
node->common.parent = parent; |
| 91 |
node->done = true; |
| 92 |
|
| 93 |
node->heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 94 |
|
| 95 |
node->recs = nullptr; |
| 96 |
node->init(); |
| 97 |
|
| 98 |
return (node); |
| 99 |
} |
| 100 |
|
| 101 |
/** Repositions the pcur in the purge node on the clustered index record, |
| 102 |
if found. If the record is not found, close pcur. |
| 103 |
@return true if the record was found */ |
| 104 |
static bool row_purge_reposition_pcur( |
| 105 |
ulint mode, /*!< in: latching mode */ |
| 106 |
purge_node_t *node, /*!< in: row purge node */ |
| 107 |
mtr_t *mtr) /*!< in: mtr */ |
| 108 |
{ |
| 109 |
if (node->found_clust) { |
| 110 |
ut_ad(node->validate_pcur()); |
| 111 |
|
| 112 |
node->found_clust = |
| 113 |
node->pcur.restore_position(mode, mtr, UT_LOCATION_HERE); |
| 114 |
|
| 115 |
} else { |
| 116 |
node->found_clust = |
| 117 |
row_search_on_row_ref(&node->pcur, mode, node->table, node->ref, mtr); |
| 118 |
|
| 119 |
if (node->found_clust) { |
| 120 |
node->pcur.store_position(mtr); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/* Close the current cursor if we fail to position it correctly. */ |
| 125 |
if (!node->found_clust) { |
| 126 |
node->pcur.close(); |
| 127 |
} |
| 128 |
|
| 129 |
return (node->found_clust); |
| 130 |
} |
| 131 |
|
| 132 |
/** Removes a delete marked clustered index record if possible. |
| 133 |
@retval true if the row was not found, or it was successfully removed |
| 134 |
@retval false if the row was modified after the delete marking */ |
| 135 |
[[nodiscard]] static bool row_purge_remove_clust_if_poss_low( |
| 136 |
purge_node_t *node, /*!< |
| 137 |
in/out: |
| 138 |
row |
| 139 |
purge |
| 140 |
node |
| 141 |
*/ |
| 142 |
ulint mode) /*!< |
| 143 |
in: |
| 144 |
BTR_MODIFY_LEAF |
| 145 |
or |
| 146 |
BTR_MODIFY_TREE |
| 147 |
*/ |
| 148 |
{ |
| 149 |
dict_index_t *index; |
| 150 |
bool success = true; |
| 151 |
mtr_t mtr; |
| 152 |
rec_t *rec; |
| 153 |
mem_heap_t *heap = nullptr; |
| 154 |
ulint *offsets; |
| 155 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 156 |
rec_offs_init(offsets_); |
| 157 |
|
| 158 |
index = node->table->first_index(); |
| 159 |
|
| 160 |
fil_space_t *space = fil_space_acquire_silent(index->space); |
| 161 |
if (space == nullptr) { |
| 162 |
/* This can happen only for SDI in General Tablespaces. |
| 163 |
*/ |
| 164 |
ut_ad(dict_table_is_sdi(node->table->id)); |
| 165 |
return (true); |
| 166 |
} else { |
| 167 |
fil_space_release(space); |
| 168 |
} |
| 169 |
|
| 170 |
log_free_check(); |
| 171 |
mtr_start(&mtr); |
| 172 |
|
| 173 |
if (!row_purge_reposition_pcur(mode, node, &mtr)) { |
| 174 |
/* The record was already removed. */ |
| 175 |
goto func_exit; |
| 176 |
} |
| 177 |
|
| 178 |
rec = node->pcur.get_rec(); |
| 179 |
|
| 180 |
offsets = rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED, |
| 181 |
UT_LOCATION_HERE, &heap); |
| 182 |
|
| 183 |
if (node->roll_ptr != row_get_rec_roll_ptr(rec, index, offsets)) { |
| 184 |
/* Someone else has modified the record later: do not remove */ |
| 185 |
goto func_exit; |
| 186 |
} |
| 187 |
|
| 188 |
ut_ad(rec_get_deleted_flag(rec, rec_offs_comp(offsets))); |
| 189 |
|
| 190 |
if (mode == BTR_MODIFY_LEAF) { |
| 191 |
success = btr_cur_optimistic_delete(node->pcur.get_btr_cur(), 0, &mtr); |
| 192 |
} else { |
| 193 |
dberr_t err; |
| 194 |
ut_ad(mode == (BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE)); |
| 195 |
|
| 196 |
DBUG_EXECUTE_IF("pessimistic_row_purge_clust", { |
| 197 |
if (!fsp_is_dd_tablespace(index->space)) { |
| 198 |
const char act[] = |
| 199 |
"now SIGNAL pessimistic_row_purge_clust_pause WAIT_FOR " |
| 200 |
"pessimistic_row_purge_clust_continue"; |
| 201 |
assert(opt_debug_sync_timeout > 0); |
| 202 |
assert(!debug_sync_set_action(current_thd, STRING_WITH_LEN(act))); |
| 203 |
} |
| 204 |
}); |
| 205 |
|
| 206 |
btr_cur_pessimistic_delete(&err, false, node->pcur.get_btr_cur(), 0, false, |
| 207 |
node->trx_id, node->undo_no, node->rec_type, |
| 208 |
&mtr, &node->pcur, node); |
| 209 |
|
| 210 |
switch (err) { |
| 211 |
case DB_SUCCESS: |
| 212 |
break; |
| 213 |
case DB_OUT_OF_FILE_SPACE: |
| 214 |
success = false; |
| 215 |
break; |
| 216 |
default: |
| 217 |
ut_error; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func_exit: |
| 222 |
if (heap) { |
| 223 |
mem_heap_free(heap); |
| 224 |
} |
| 225 |
|
| 226 |
/* Persistent cursor is closed if reposition fails. */ |
| 227 |
if (node->found_clust) { |
| 228 |
node->pcur.commit_specify_mtr(&mtr); |
| 229 |
} else { |
| 230 |
mtr_commit(&mtr); |
| 231 |
} |
| 232 |
|
| 233 |
return (success); |
| 234 |
} |
| 235 |
|
| 236 |
/** Removes a clustered index record if it has not been modified after the |
| 237 |
delete marking. |
| 238 |
@retval true if the row was not found, or it was successfully removed |
| 239 |
@retval false the purge needs to be suspended because of running out |
| 240 |
of file space. */ |
| 241 |
[[nodiscard]] static bool row_purge_remove_clust_if_poss( |
| 242 |
purge_node_t *node) /*!< in/out: row purge node */ |
| 243 |
{ |
| 244 |
if (row_purge_remove_clust_if_poss_low(node, BTR_MODIFY_LEAF)) { |
| 245 |
return (true); |
| 246 |
} |
| 247 |
|
| 248 |
for (ulint n_tries = 0; n_tries < BTR_CUR_RETRY_DELETE_N_TIMES; n_tries++) { |
| 249 |
if (row_purge_remove_clust_if_poss_low( |
| 250 |
node, BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE)) { |
| 251 |
return (true); |
| 252 |
} |
| 253 |
|
| 254 |
std::this_thread::sleep_for( |
| 255 |
std::chrono::milliseconds(BTR_CUR_RETRY_SLEEP_TIME_MS)); |
| 256 |
} |
| 257 |
|
| 258 |
return (false); |
| 259 |
} |
| 260 |
|
| 261 |
/** Determines if it is possible to remove a secondary index entry. |
| 262 |
Removal is possible if the secondary index entry does not refer to any |
| 263 |
not delete marked version of a clustered index record where DB_TRX_ID |
| 264 |
is newer than the purge view. |
| 265 |
|
| 266 |
NOTE: This function should only be called by the purge thread, only |
| 267 |
while holding a latch on the leaf page of the secondary index entry |
| 268 |
(or keeping the buffer pool watch on the page). It is possible that |
| 269 |
this function first returns true and then false, if a user transaction |
| 270 |
inserts a record that the secondary index entry would refer to. |
| 271 |
However, in that case, the user transaction would also re-insert the |
| 272 |
secondary index entry after purge has removed it and released the leaf |
| 273 |
page latch. |
| 274 |
@return true if the secondary index record can be purged */ |
| 275 |
bool row_purge_poss_sec(purge_node_t *node, /*!< in/out: row purge node */ |
| 276 |
dict_index_t *index, /*!< in: secondary index */ |
| 277 |
const dtuple_t *entry) /*!< in: secondary index entry */ |
| 278 |
{ |
| 279 |
bool can_delete; |
| 280 |
mtr_t mtr; |
| 281 |
|
| 282 |
ut_ad(!index->is_clustered()); |
| 283 |
mtr_start(&mtr); |
| 284 |
|
| 285 |
can_delete = |
| 286 |
!row_purge_reposition_pcur(BTR_SEARCH_LEAF, node, &mtr) || |
| 287 |
!row_vers_old_has_index_entry(true, node->pcur.get_rec(), &mtr, index, |
| 288 |
entry, node->roll_ptr, node->trx_id); |
| 289 |
|
| 290 |
/* Persistent cursor is closed if reposition fails. */ |
| 291 |
if (node->found_clust) { |
| 292 |
node->pcur.commit_specify_mtr(&mtr); |
| 293 |
} else { |
| 294 |
mtr_commit(&mtr); |
| 295 |
} |
| 296 |
|
| 297 |
return (can_delete); |
| 298 |
} |
| 299 |
|
| 300 |
/*************************************************************** |
| 301 |
Removes a secondary index entry if possible, by modifying the |
| 302 |
index tree. Does not try to buffer the delete. |
| 303 |
@return true if success or if not found */ |
| 304 |
[[nodiscard]] static bool row_purge_remove_sec_if_poss_tree( |
| 305 |
purge_node_t *node, /*!< in: row purge node */ |
| 306 |
dict_index_t *index, /*!< in: index */ |
| 307 |
const dtuple_t *entry) /*!< in: index entry */ |
| 308 |
{ |
| 309 |
btr_pcur_t pcur; |
| 310 |
btr_cur_t *btr_cur; |
| 311 |
bool success = true; |
| 312 |
dberr_t err; |
| 313 |
mtr_t mtr; |
| 314 |
enum row_search_result search_result; |
| 315 |
|
| 316 |
log_free_check(); |
| 317 |
mtr_start(&mtr); |
| 318 |
|
| 319 |
if (!index->is_committed()) { |
| 320 |
/* The index->online_status may change if the index is |
| 321 |
or was being created online, but not committed yet. It |
| 322 |
is protected by index->lock. */ |
| 323 |
mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 324 |
|
| 325 |
if (dict_index_is_online_ddl(index)) { |
| 326 |
/* Online secondary index creation will not |
| 327 |
copy any delete-marked records. Therefore |
| 328 |
there is nothing to be purged. We must also |
| 329 |
skip the purge when a completed index is |
| 330 |
dropped by rollback_inplace_alter_table(). */ |
| 331 |
goto func_exit_no_pcur; |
| 332 |
} |
| 333 |
} else { |
| 334 |
/* For secondary indexes, |
| 335 |
index->online_status==ONLINE_INDEX_COMPLETE if |
| 336 |
index->is_committed(). */ |
| 337 |
ut_ad(!dict_index_is_online_ddl(index)); |
| 338 |
} |
| 339 |
|
| 340 |
search_result = row_search_index_entry( |
| 341 |
index, entry, BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE, &pcur, &mtr); |
| 342 |
|
| 343 |
switch (search_result) { |
| 344 |
case ROW_NOT_FOUND: |
| 345 |
/* Not found. This is a legitimate condition. In a |
| 346 |
rollback, InnoDB will remove secondary recs that would |
| 347 |
be purged anyway. Then the actual purge will not find |
| 348 |
the secondary index record. Also, the purge itself is |
| 349 |
eager: if it comes to consider a secondary index |
| 350 |
record, and notices it does not need to exist in the |
| 351 |
index, it will remove it. Then if/when the purge |
| 352 |
comes to consider the secondary index record a second |
| 353 |
time, it will not exist any more in the index. */ |
| 354 |
|
| 355 |
/* fputs("PURGE:........sec entry not found\n", stderr); */ |
| 356 |
/* dtuple_print(stderr, entry); */ |
| 357 |
goto func_exit; |
| 358 |
case ROW_FOUND: |
| 359 |
break; |
| 360 |
case ROW_BUFFERED: |
| 361 |
case ROW_NOT_DELETED_REF: |
| 362 |
/* These are invalid outcomes, because the mode passed |
| 363 |
to row_search_index_entry() did not include any of the |
| 364 |
flags BTR_INSERT, BTR_DELETE, or BTR_DELETE_MARK. */ |
| 365 |
ut_error; |
| 366 |
} |
| 367 |
|
| 368 |
btr_cur = pcur.get_btr_cur(); |
| 369 |
|
| 370 |
/* We should remove the index record if no later version of the row, |
| 371 |
which cannot be purged yet, requires its existence. If some requires, |
| 372 |
we should do nothing. */ |
| 373 |
|
| 374 |
if (row_purge_poss_sec(node, index, entry)) { |
| 375 |
/* Remove the index record, which should have been |
| 376 |
marked for deletion. */ |
| 377 |
if (!rec_get_deleted_flag(btr_cur_get_rec(btr_cur), |
| 378 |
dict_table_is_comp(index->table))) { |
| 379 |
ib::error(ER_IB_MSG_1007) |
| 380 |
<< "tried to purge non-delete-marked record" |
| 381 |
" in index " |
| 382 |
<< index->name << " of table " << index->table->name |
| 383 |
<< ": tuple: " << *entry |
| 384 |
<< ", record: " << rec_index_print(btr_cur_get_rec(btr_cur), index); |
| 385 |
|
| 386 |
ut_d(ut_error); |
| 387 |
|
| 388 |
ut_o(goto func_exit); |
| 389 |
} |
| 390 |
|
| 391 |
btr_cur_pessimistic_delete(&err, false, btr_cur, 0, false, 0, node->undo_no, |
| 392 |
node->rec_type, &mtr, &pcur, node); |
| 393 |
switch (UNIV_EXPECT(err, DB_SUCCESS)) { |
| 394 |
case DB_SUCCESS: |
| 395 |
break; |
| 396 |
case DB_OUT_OF_FILE_SPACE: |
| 397 |
success = false; |
| 398 |
break; |
| 399 |
default: |
| 400 |
ut_error; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
func_exit: |
| 405 |
pcur.close(); |
| 406 |
func_exit_no_pcur: |
| 407 |
mtr_commit(&mtr); |
| 408 |
|
| 409 |
return (success); |
| 410 |
} |
| 411 |
|
| 412 |
/*************************************************************** |
| 413 |
Removes a secondary index entry without modifying the index tree, |
| 414 |
if possible. |
| 415 |
@retval true if success or if not found |
| 416 |
@retval false if row_purge_remove_sec_if_poss_tree() should be invoked */ |
| 417 |
[[nodiscard]] static bool row_purge_remove_sec_if_poss_leaf( |
| 418 |
purge_node_t *node, /*!< |
| 419 |
in: |
| 420 |
row |
| 421 |
purge |
| 422 |
node |
| 423 |
*/ |
| 424 |
dict_index_t *index, /*!< |
| 425 |
in: |
| 426 |
index |
| 427 |
*/ |
| 428 |
const dtuple_t *entry) /*!< |
| 429 |
in: |
| 430 |
index |
| 431 |
entry |
| 432 |
*/ |
| 433 |
{ |
| 434 |
mtr_t mtr; |
| 435 |
btr_pcur_t pcur; |
| 436 |
ulint mode; |
| 437 |
enum row_search_result search_result; |
| 438 |
bool success = true; |
| 439 |
|
| 440 |
log_free_check(); |
| 441 |
|
| 442 |
mtr_start(&mtr); |
| 443 |
|
| 444 |
if (!index->is_committed()) { |
| 445 |
/* For uncommitted spatial index, we also skip the purge. */ |
| 446 |
if (dict_index_is_spatial(index)) { |
| 447 |
goto func_exit_no_pcur; |
| 448 |
} |
| 449 |
|
| 450 |
/* The index->online_status may change if the the |
| 451 |
index is or was being created online, but not |
| 452 |
committed yet. It is protected by index->lock. */ |
| 453 |
mtr_s_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 454 |
|
| 455 |
if (dict_index_is_online_ddl(index)) { |
| 456 |
/* Online secondary index creation will not |
| 457 |
copy any delete-marked records. Therefore |
| 458 |
there is nothing to be purged. We must also |
| 459 |
skip the purge when a completed index is |
| 460 |
dropped by rollback_inplace_alter_table(). */ |
| 461 |
goto func_exit_no_pcur; |
| 462 |
} |
| 463 |
|
| 464 |
/* Change buffering is disabled for temporary tables. */ |
| 465 |
mode = (index->table->is_temporary()) |
| 466 |
? BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED |
| 467 |
: BTR_MODIFY_LEAF | BTR_ALREADY_S_LATCHED | BTR_DELETE; |
| 468 |
} else { |
| 469 |
/* For secondary indexes, |
| 470 |
index->online_status==ONLINE_INDEX_COMPLETE if |
| 471 |
index->is_committed(). */ |
| 472 |
ut_ad(!dict_index_is_online_ddl(index)); |
| 473 |
|
| 474 |
/* Change buffering is disabled for temporary tables |
| 475 |
and spatial index. */ |
| 476 |
mode = (index->table->is_temporary() || dict_index_is_spatial(index)) |
| 477 |
? BTR_MODIFY_LEAF |
| 478 |
: BTR_MODIFY_LEAF | BTR_DELETE; |
| 479 |
} |
| 480 |
|
| 481 |
/* Set the purge node for the call to row_purge_poss_sec(). */ |
| 482 |
pcur.m_btr_cur.purge_node = node; |
| 483 |
if (dict_index_is_spatial(index)) { |
| 484 |
rw_lock_sx_lock(dict_index_get_lock(index), UT_LOCATION_HERE); |
| 485 |
pcur.m_btr_cur.thr = nullptr; |
| 486 |
} else { |
| 487 |
/* Set the query thread, so that ibuf_insert_low() will be |
| 488 |
able to invoke thd_get_trx(). */ |
| 489 |
pcur.m_btr_cur.thr = static_cast<que_thr_t *>(que_node_get_parent(node)); |
| 490 |
} |
| 491 |
|
| 492 |
search_result = row_search_index_entry(index, entry, mode, &pcur, &mtr); |
| 493 |
|
| 494 |
if (dict_index_is_spatial(index)) { |
| 495 |
rw_lock_sx_unlock(dict_index_get_lock(index)); |
| 496 |
} |
| 497 |
|
| 498 |
switch (search_result) { |
| 499 |
case ROW_FOUND: |
| 500 |
/* Before attempting to purge a record, check |
| 501 |
if it is safe to do so. */ |
| 502 |
if (row_purge_poss_sec(node, index, entry)) { |
| 503 |
btr_cur_t *btr_cur = pcur.get_btr_cur(); |
| 504 |
|
| 505 |
/* Only delete-marked records should be purged. */ |
| 506 |
if (!rec_get_deleted_flag(btr_cur_get_rec(btr_cur), |
| 507 |
dict_table_is_comp(index->table))) { |
| 508 |
ib::error(ER_IB_MSG_1008) |
| 509 |
<< "tried to purge non-delete-marked" |
| 510 |
" record" |
| 511 |
" in index " |
| 512 |
<< index->name << " of table " << index->table->name |
| 513 |
<< ": tuple: " << *entry << ", record: " |
| 514 |
<< rec_index_print(btr_cur_get_rec(btr_cur), index); |
| 515 |
|
| 516 |
pcur.close(); |
| 517 |
|
| 518 |
ut_d(ut_error); |
| 519 |
|
| 520 |
ut_o(goto func_exit_no_pcur); |
| 521 |
} |
| 522 |
|
| 523 |
if (dict_index_is_spatial(index)) { |
| 524 |
const page_t *page; |
| 525 |
const trx_t *trx = nullptr; |
| 526 |
|
| 527 |
if (btr_cur->rtr_info != nullptr && |
| 528 |
btr_cur->rtr_info->thr != nullptr) { |
| 529 |
trx = thr_get_trx(btr_cur->rtr_info->thr); |
| 530 |
} |
| 531 |
|
| 532 |
page = btr_cur_get_page(btr_cur); |
| 533 |
|
| 534 |
if (lock_other_has_prdt_page_lock(trx, page_get_page_id(page)) && |
| 535 |
page_get_n_recs(page) < 2 && |
| 536 |
page_get_page_no(page) != dict_index_get_page(index)) { |
| 537 |
/* this is the last record on page, |
| 538 |
and it has a "page" lock on it, |
| 539 |
which mean search is still depending |
| 540 |
on it, so do not delete */ |
| 541 |
#ifdef UNIV_DEBUG |
| 542 |
ib::info(ER_IB_MSG_1009) << "skip purging last" |
| 543 |
" record on page " |
| 544 |
<< page_get_page_no(page) << "."; |
| 545 |
#endif /* UNIV_DEBUG */ |
| 546 |
|
| 547 |
pcur.close(); |
| 548 |
mtr_commit(&mtr); |
| 549 |
return (success); |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
if (!btr_cur_optimistic_delete(btr_cur, 0, &mtr)) { |
| 554 |
/* The index entry could not be deleted. */ |
| 555 |
success = false; |
| 556 |
} |
| 557 |
} |
| 558 |
/* fall through (the index entry is still needed, |
| 559 |
or the deletion succeeded) */ |
| 560 |
[[fallthrough]]; |
| 561 |
case ROW_NOT_DELETED_REF: |
| 562 |
/* The index entry is still needed. */ |
| 563 |
case ROW_BUFFERED: |
| 564 |
/* The deletion was buffered. */ |
| 565 |
case ROW_NOT_FOUND: |
| 566 |
/* The index entry does not exist, nothing to do. */ |
| 567 |
pcur.close(); |
| 568 |
func_exit_no_pcur: |
| 569 |
mtr_commit(&mtr); |
| 570 |
return (success); |
| 571 |
} |
| 572 |
|
| 573 |
ut_error; |
| 574 |
return (false); |
| 575 |
} |
| 576 |
|
| 577 |
/** Removes a secondary index entry if possible. */ |
| 578 |
static inline void row_purge_remove_sec_if_poss( |
| 579 |
purge_node_t *node, /*!< in: row purge node */ |
| 580 |
dict_index_t *index, /*!< in: index */ |
| 581 |
const dtuple_t *entry) /*!< in: index entry */ |
| 582 |
{ |
| 583 |
ulint n_tries = 0; |
| 584 |
|
| 585 |
/* fputs("Purge: Removing secondary record\n", stderr); */ |
| 586 |
|
| 587 |
if (!entry) { |
| 588 |
/* The node->row must have lacked some fields of this |
| 589 |
index. This is possible when the undo log record was |
| 590 |
written before this index was created. */ |
| 591 |
return; |
| 592 |
} |
| 593 |
|
| 594 |
if (row_purge_remove_sec_if_poss_leaf(node, index, entry)) { |
| 595 |
return; |
| 596 |
} |
| 597 |
retry: |
| 598 |
auto success = row_purge_remove_sec_if_poss_tree(node, index, entry); |
| 599 |
/* The delete operation may fail if we have little |
| 600 |
file space left: TODO: easiest to crash the database |
| 601 |
and restart with more file space */ |
| 602 |
|
| 603 |
if (!success && n_tries < BTR_CUR_RETRY_DELETE_N_TIMES) { |
| 604 |
n_tries++; |
| 605 |
|
| 606 |
std::this_thread::sleep_for( |
| 607 |
std::chrono::milliseconds(BTR_CUR_RETRY_SLEEP_TIME_MS)); |
| 608 |
|
| 609 |
goto retry; |
| 610 |
} |
| 611 |
|
| 612 |
ut_a(success); |
| 613 |
} |
| 614 |
|
| 615 |
/** Skip uncommitted virtual indexes on newly added virtual column. |
| 616 |
@param[in,out] index dict index object */ |
| 617 |
static inline void row_purge_skip_uncommitted_virtual_index( |
| 618 |
dict_index_t *&index) { |
| 619 |
/* We need to skip virtual indexes which is not |
| 620 |
committed yet. It's safe because these indexes are |
| 621 |
newly created by alter table, and because we do |
| 622 |
not support LOCK=NONE when adding an index on newly |
| 623 |
added virtual column.*/ |
| 624 |
while (index != nullptr && dict_index_has_virtual(index) && |
| 625 |
!index->is_committed() && index->has_new_v_col) { |
| 626 |
index = index->next(); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
/** Remove multi-value index entries if possible. |
| 631 |
@param[in,out] node row purge node |
| 632 |
@param[in,out] heap memory heap |
| 633 |
@param[in] selected true if only selected multi-value data should |
| 634 |
be purged */ |
| 635 |
static inline void row_purge_remove_multi_sec_if_poss(purge_node_t *node, |
| 636 |
mem_heap_t *heap, |
| 637 |
bool selected) { |
| 638 |
dict_index_t *index = node->index; |
| 639 |
|
| 640 |
ut_ad(index->is_multi_value()); |
| 641 |
|
| 642 |
Multi_value_entry_builder_normal mv_entry_builder(node->row, nullptr, index, |
| 643 |
heap, false, selected); |
| 644 |
|
| 645 |
for (dtuple_t *entry = mv_entry_builder.begin(); entry != nullptr; |
| 646 |
entry = mv_entry_builder.next()) { |
| 647 |
row_purge_remove_sec_if_poss(node, index, entry); |
| 648 |
} |
| 649 |
} |
| 650 |
|
| 651 |
/** Purges a delete marking of a record. |
| 652 |
@retval true if the row was not found, or it was successfully removed |
| 653 |
@retval false the purge needs to be suspended because of |
| 654 |
running out of file space */ |
| 655 |
[[nodiscard]] static bool row_purge_del_mark( |
| 656 |
purge_node_t *node) /*!< in/out: row purge node */ |
| 657 |
{ |
| 658 |
mem_heap_t *heap; |
| 659 |
|
| 660 |
heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 661 |
|
| 662 |
while (node->index != nullptr) { |
| 663 |
/* skip corrupted secondary index */ |
| 664 |
dict_table_skip_corrupt_index(node->index); |
| 665 |
|
| 666 |
row_purge_skip_uncommitted_virtual_index(node->index); |
| 667 |
|
| 668 |
if (!node->index) { |
| 669 |
break; |
| 670 |
} |
| 671 |
|
| 672 |
if (node->index->type != DICT_FTS) { |
| 673 |
if (node->index->is_multi_value()) { |
| 674 |
row_purge_remove_multi_sec_if_poss(node, heap, false); |
| 675 |
} else { |
| 676 |
dtuple_t *entry = row_build_index_entry_low( |
| 677 |
node->row, nullptr, node->index, heap, ROW_BUILD_FOR_PURGE); |
| 678 |
row_purge_remove_sec_if_poss(node, node->index, entry); |
| 679 |
} |
| 680 |
|
| 681 |
mem_heap_empty(heap); |
| 682 |
} |
| 683 |
|
| 684 |
node->index = node->index->next(); |
| 685 |
} |
| 686 |
|
| 687 |
mem_heap_free(heap); |
| 688 |
|
| 689 |
return (row_purge_remove_clust_if_poss(node)); |
| 690 |
} |
| 691 |
|
| 692 |
/** Purges an update of an existing record. Also purges an update of a delete |
| 693 |
marked record if that record contained an externally stored field. |
| 694 |
@param[in] thr query thread |
| 695 |
@param[in] node row purge node |
| 696 |
@param[in] undo_rec record to purge */ |
| 697 |
static void row_purge_upd_exist_or_extern_func(IF_DEBUG(const que_thr_t *thr, ) |
| 698 |
purge_node_t *node, |
| 699 |
trx_undo_rec_t *undo_rec) { |
| 700 |
mem_heap_t *heap; |
| 701 |
|
| 702 |
ut_ad(!node->table->skip_alter_undo); |
| 703 |
|
| 704 |
if (node->rec_type == TRX_UNDO_UPD_DEL_REC || |
| 705 |
(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) { |
| 706 |
goto skip_secondaries; |
| 707 |
} |
| 708 |
|
| 709 |
heap = mem_heap_create(1024, UT_LOCATION_HERE); |
| 710 |
|
| 711 |
while (node->index != nullptr) { |
| 712 |
bool non_mv_upd = false; |
| 713 |
|
| 714 |
dict_table_skip_corrupt_index(node->index); |
| 715 |
|
| 716 |
row_purge_skip_uncommitted_virtual_index(node->index); |
| 717 |
|
| 718 |
if (!node->index) { |
| 719 |
break; |
| 720 |
} |
| 721 |
|
| 722 |
#ifndef UNIV_DEBUG |
| 723 |
que_thr_t *thr = nullptr; |
| 724 |
#endif |
| 725 |
|
| 726 |
if (row_upd_changes_ord_field_binary( |
| 727 |
node->index, node->update, thr, nullptr, nullptr, |
| 728 |
(node->index->is_multi_value() ? &non_mv_upd : nullptr))) { |
| 729 |
if (node->index->is_multi_value()) { |
| 730 |
row_purge_remove_multi_sec_if_poss(node, heap, !non_mv_upd); |
| 731 |
} else { |
| 732 |
/* Build the older version of the index entry */ |
| 733 |
dtuple_t *entry = row_build_index_entry_low( |
| 734 |
node->row, nullptr, node->index, heap, ROW_BUILD_FOR_PURGE); |
| 735 |
row_purge_remove_sec_if_poss(node, node->index, entry); |
| 736 |
mem_heap_empty(heap); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
node->index = node->index->next(); |
| 741 |
} |
| 742 |
|
| 743 |
mem_heap_free(heap); |
| 744 |
|
| 745 |
skip_secondaries: |
| 746 |
|
| 747 |
/* Free possible externally stored fields */ |
| 748 |
for (ulint i = 0; i < upd_get_n_fields(node->update); i++) { |
| 749 |
const upd_field_t *ufield = upd_get_nth_field(node->update, i); |
| 750 |
|
| 751 |
if (dfield_is_ext(&ufield->new_val)) { |
| 752 |
buf_block_t *block; |
| 753 |
ulint internal_offset; |
| 754 |
byte *data_field; |
| 755 |
dict_index_t *index; |
| 756 |
bool is_insert; |
| 757 |
ulint rseg_id; |
| 758 |
page_no_t page_no; |
| 759 |
space_id_t undo_space_id; |
| 760 |
ulint offset; |
| 761 |
mtr_t mtr; |
| 762 |
|
| 763 |
/* We use the fact that new_val points to |
| 764 |
undo_rec and get thus the offset of |
| 765 |
dfield data inside the undo record. Then we |
| 766 |
can calculate from node->roll_ptr the file |
| 767 |
address of the new_val data */ |
| 768 |
|
| 769 |
internal_offset = |
| 770 |
((const byte *)dfield_get_data(&ufield->new_val)) - undo_rec; |
| 771 |
|
| 772 |
ut_a(internal_offset < UNIV_PAGE_SIZE); |
| 773 |
|
| 774 |
trx_undo_decode_roll_ptr(node->roll_ptr, &is_insert, &rseg_id, &page_no, |
| 775 |
&offset); |
| 776 |
|
| 777 |
/* If table is temp then it can't have its undo log |
| 778 |
residing in rollback segment with REDO log enabled. */ |
| 779 |
bool is_temp = node->table->is_temporary(); |
| 780 |
|
| 781 |
undo_space_id = trx_rseg_id_to_space_id(rseg_id, is_temp); |
| 782 |
|
| 783 |
mtr_start(&mtr); |
| 784 |
|
| 785 |
/* We have to acquire an SX-latch to the clustered |
| 786 |
index tree (exclude other tree changes) */ |
| 787 |
|
| 788 |
index = node->table->first_index(); |
| 789 |
|
| 790 |
mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE); |
| 791 |
|
| 792 |
/* NOTE: we must also acquire an X-latch to the |
| 793 |
root page of the tree. We will need it when we |
| 794 |
free pages from the tree. If the tree is of height 1, |
| 795 |
the tree X-latch does NOT protect the root page, |
| 796 |
because it is also a leaf page. Since we will have a |
| 797 |
latch on an undo log page, we would break the |
| 798 |
latching order if we would only later latch the |
| 799 |
root page of such a tree! */ |
| 800 |
|
| 801 |
btr_root_get(index, &mtr); |
| 802 |
|
| 803 |
block = buf_page_get(page_id_t(undo_space_id, page_no), univ_page_size, |
| 804 |
RW_X_LATCH, UT_LOCATION_HERE, &mtr); |
| 805 |
|
| 806 |
buf_block_dbg_add_level(block, SYNC_TRX_UNDO_PAGE); |
| 807 |
|
| 808 |
data_field = buf_block_get_frame(block) + offset + internal_offset; |
| 809 |
|
| 810 |
ut_a(dfield_get_len(&ufield->new_val) >= BTR_EXTERN_FIELD_REF_SIZE); |
| 811 |
|
| 812 |
byte *field_ref = data_field + dfield_get_len(&ufield->new_val) - |
| 813 |
BTR_EXTERN_FIELD_REF_SIZE; |
| 814 |
|
| 815 |
lob::BtrContext btr_ctx(&mtr, index, block); |
| 816 |
|
| 817 |
lob::DeleteContext ctx(btr_ctx, field_ref, ufield->field_no, false); |
| 818 |
|
| 819 |
lob::purge(&ctx, index, node->modifier_trx_id, |
| 820 |
trx_undo_rec_get_undo_no(undo_rec), node->rec_type, ufield, |
| 821 |
node); |
| 822 |
|
| 823 |
mtr_commit(&mtr); |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
|
| 828 |
inline void row_purge_upd_exist_or_extern(const que_thr_t *thr [[maybe_unused]], |
| 829 |
purge_node_t *node, |
| 830 |
trx_undo_rec_t *undo_rec) { |
| 831 |
row_purge_upd_exist_or_extern_func(IF_DEBUG(thr, ) node, undo_rec); |
| 832 |
} |
| 833 |
|
| 834 |
/** Parses the row reference and other info in a modify undo log record. |
| 835 |
@param[in,out] node row undo node |
| 836 |
@param[in] undo_rec undo record to purge |
| 837 |
@param[out] updated_extern whether an externally stored |
| 838 |
field was updated |
| 839 |
@param[in,out] thd current thread |
| 840 |
@param[in,out] thr execution thread |
| 841 |
@return true if purge operation required */ |
| 842 |
static bool row_purge_parse_undo_rec(purge_node_t *node, |
| 843 |
trx_undo_rec_t *undo_rec, |
| 844 |
bool *updated_extern, THD *thd, |
| 845 |
que_thr_t *thr) { |
| 846 |
dict_index_t *clust_index; |
| 847 |
byte *ptr; |
| 848 |
undo_no_t undo_no; |
| 849 |
table_id_t table_id; |
| 850 |
trx_id_t trx_id; |
| 851 |
roll_ptr_t roll_ptr; |
| 852 |
ulint info_bits; |
| 853 |
ulint type; |
| 854 |
type_cmpl_t type_cmpl; |
| 855 |
|
| 856 |
ut_ad(node != nullptr); |
| 857 |
ut_ad(thr != nullptr); |
| 858 |
|
| 859 |
ptr = trx_undo_rec_get_pars(undo_rec, &type, &node->cmpl_info, updated_extern, |
| 860 |
&undo_no, &table_id, type_cmpl); |
| 861 |
|
| 862 |
node->rec_type = type; |
| 863 |
|
| 864 |
if (type == TRX_UNDO_UPD_DEL_REC && !*updated_extern) { |
| 865 |
return (false); |
| 866 |
} |
| 867 |
|
| 868 |
ptr = trx_undo_update_rec_get_sys_cols(ptr, &trx_id, &roll_ptr, &info_bits); |
| 869 |
node->table = nullptr; |
| 870 |
node->trx_id = trx_id; |
| 871 |
|
| 872 |
/* TODO: Remove all INNODB_DD_VC_SUPPORT, nest opening |
| 873 |
table should never happen again after new DD */ |
| 874 |
#ifdef INNODB_DD_VC_SUPPORT |
| 875 |
try_again: |
| 876 |
#endif /* INNODB_DD_VC_SUPPORT */ |
| 877 |
|
| 878 |
/* Cannot call dd_table_open_on_id() before server is fully up */ |
| 879 |
if (!srv_upgrade_old_undo_found && !dict_table_is_system(table_id)) { |
| 880 |
while (!mysqld_server_started) { |
| 881 |
if (srv_shutdown_state.load() >= SRV_SHUTDOWN_PURGE) { |
| 882 |
return (false); |
| 883 |
} |
| 884 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 885 |
} |
| 886 |
} |
| 887 |
|
| 888 |
/* SDI tables are hidden tables and are not registered with global |
| 889 |
dictionary. Open the table internally. Also acquire shared MDL |
| 890 |
of SDI tables. Concurrent DROP TABLE/TABLESPACE would acquire |
| 891 |
exclusive MDL on SDI tables */ |
| 892 |
ut_ad(!dict_table_is_system(table_id)); |
| 893 |
|
| 894 |
if (dict_table_is_sdi(table_id) || srv_upgrade_old_undo_found) { |
| 895 |
if (dict_table_is_sdi(table_id)) { |
| 896 |
space_id_t space_id = dict_sdi_get_space_id(table_id); |
| 897 |
|
| 898 |
dberr_t err = dd_sdi_acquire_shared_mdl(thd, space_id, &node->mdl); |
| 899 |
if (err != DB_SUCCESS) { |
| 900 |
node->table = nullptr; |
| 901 |
return (false); |
| 902 |
} |
| 903 |
|
| 904 |
node->table = dd_table_open_on_id(table_id, thd, &node->mdl, false, true); |
| 905 |
|
| 906 |
if (node->table == nullptr) { |
| 907 |
/* Tablespace containing SDI table |
| 908 |
is already dropped */ |
| 909 |
dd::release_mdl(thd, node->mdl); |
| 910 |
node->mdl = nullptr; |
| 911 |
} |
| 912 |
} |
| 913 |
} else { |
| 914 |
for (;;) { |
| 915 |
const auto no_mdl = nullptr; |
| 916 |
node->mdl = no_mdl; |
| 917 |
|
| 918 |
dict_sys_mutex_enter(); |
| 919 |
node->table = dd_table_open_on_id(table_id, thd, &node->mdl, true, true); |
| 920 |
|
| 921 |
if (node->table && node->table->is_temporary()) { |
| 922 |
/* Temp table does not do purge */ |
| 923 |
ut_ad(node->mdl == nullptr); |
| 924 |
dd_table_close(node->table, nullptr, nullptr, true); |
| 925 |
dict_sys_mutex_exit(); |
| 926 |
goto err_exit; |
| 927 |
} |
| 928 |
|
| 929 |
dict_sys_mutex_exit(); |
| 930 |
|
| 931 |
if (node->table != nullptr) { |
| 932 |
if (node->table->is_fts_aux()) { |
| 933 |
table_id_t parent_id = node->table->parent_id; |
| 934 |
|
| 935 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 936 |
|
| 937 |
node->parent_mdl = nullptr; |
| 938 |
node->parent = dd_table_open_on_id(parent_id, thd, &node->parent_mdl, |
| 939 |
false, true); |
| 940 |
|
| 941 |
if (node->parent == nullptr) { |
| 942 |
goto err_exit; |
| 943 |
} |
| 944 |
|
| 945 |
ut_ad(node->parent_mdl != nullptr); |
| 946 |
node->mdl = nullptr; |
| 947 |
node->table = |
| 948 |
dd_table_open_on_id(table_id, thd, &node->mdl, false, true); |
| 949 |
} |
| 950 |
break; |
| 951 |
} |
| 952 |
|
| 953 |
if (node->mdl == no_mdl) { |
| 954 |
/* The table has been dropped: no need |
| 955 |
to do purge */ |
| 956 |
node->mdl = nullptr; |
| 957 |
goto err_exit; |
| 958 |
} |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
if (node->table == nullptr) { |
| 963 |
/* The table has been dropped: no need to do purge */ |
| 964 |
goto err_exit; |
| 965 |
} |
| 966 |
|
| 967 |
#ifdef INNODB_DD_VC_SUPPORT |
| 968 |
if (node->table->n_v_cols && !node->table->vc_templ && |
| 969 |
dict_table_has_indexed_v_cols(node->table)) { |
| 970 |
/* Need server fully up for virtual column computation */ |
| 971 |
if (!mysqld_server_started) { |
| 972 |
if (dict_table_is_sdi(node->table->id)) { |
| 973 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 974 |
node->table = nullptr; |
| 975 |
|
| 976 |
} else { |
| 977 |
bool is_aux = node->table->is_fts_aux(); |
| 978 |
|
| 979 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 980 |
if (is_aux && node->parent) { |
| 981 |
dd_table_close(node->parent, thd & node->parent_mdl, false); |
| 982 |
} |
| 983 |
} |
| 984 |
if (srv_shutdown_state.load() >= SRV_SHUTDOWN_PURGE) { |
| 985 |
return (false); |
| 986 |
} |
| 987 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 988 |
goto try_again; |
| 989 |
} |
| 990 |
|
| 991 |
/* Initialize the template for the table */ |
| 992 |
innobase_init_vc_templ(node->table); |
| 993 |
} |
| 994 |
#endif /* INNODB_DD_VC_SUPPORT */ |
| 995 |
|
| 996 |
/* Disable purging for temp-tables as they are short-lived |
| 997 |
and no point in re-organizing such short lived tables */ |
| 998 |
if (node->table->is_temporary()) { |
| 999 |
goto close_exit; |
| 1000 |
} |
| 1001 |
|
| 1002 |
if (node->table->ibd_file_missing) { |
| 1003 |
/* We skip purge of missing .ibd files */ |
| 1004 |
|
| 1005 |
if (dict_table_is_sdi(node->table->id)) { |
| 1006 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 1007 |
node->table = nullptr; |
| 1008 |
} else { |
| 1009 |
bool is_aux = node->table->is_fts_aux(); |
| 1010 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 1011 |
if (is_aux && node->parent) { |
| 1012 |
dd_table_close(node->parent, thd, &node->parent_mdl, false); |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
node->table = nullptr; |
| 1017 |
|
| 1018 |
goto err_exit; |
| 1019 |
} |
| 1020 |
|
| 1021 |
clust_index = node->table->first_index(); |
| 1022 |
|
| 1023 |
if (clust_index == nullptr || clust_index->is_corrupted()) { |
| 1024 |
/* The table was corrupt in the data dictionary. |
| 1025 |
dict_set_corrupted() works on an index, and |
| 1026 |
we do not have an index to call it with. */ |
| 1027 |
close_exit: |
| 1028 |
/* Purge requires no changes to indexes: we may return */ |
| 1029 |
if (dict_table_is_sdi(node->table->id) || srv_upgrade_old_undo_found) { |
| 1030 |
if (dict_table_is_sdi(node->table->id)) { |
| 1031 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 1032 |
} else { |
| 1033 |
dict_table_close(node->table, false, false); |
| 1034 |
} |
| 1035 |
node->table = nullptr; |
| 1036 |
} else { |
| 1037 |
bool is_aux = node->table->is_fts_aux(); |
| 1038 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 1039 |
if (is_aux && node->parent) { |
| 1040 |
dd_table_close(node->parent, thd, &node->parent_mdl, false); |
| 1041 |
} |
| 1042 |
} |
| 1043 |
err_exit: |
| 1044 |
return (false); |
| 1045 |
} |
| 1046 |
|
| 1047 |
if (type == TRX_UNDO_UPD_EXIST_REC && |
| 1048 |
(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE) && !*updated_extern) { |
| 1049 |
/* Purge requires no changes to indexes: we may return */ |
| 1050 |
goto close_exit; |
| 1051 |
} |
| 1052 |
|
| 1053 |
ptr = trx_undo_rec_get_row_ref(ptr, clust_index, &(node->ref), node->heap); |
| 1054 |
|
| 1055 |
ptr = trx_undo_update_rec_get_update(ptr, clust_index, type, trx_id, roll_ptr, |
| 1056 |
info_bits, node->heap, &(node->update), |
| 1057 |
nullptr, type_cmpl); |
| 1058 |
|
| 1059 |
/* Read to the partial row the fields that occur in indexes */ |
| 1060 |
|
| 1061 |
if (!(node->cmpl_info & UPD_NODE_NO_ORD_CHANGE)) { |
| 1062 |
ptr = trx_undo_rec_get_partial_row( |
| 1063 |
ptr, clust_index, &node->row, type == TRX_UNDO_UPD_DEL_REC, node->heap); |
| 1064 |
} |
| 1065 |
|
| 1066 |
return (true); |
| 1067 |
} |
| 1068 |
|
| 1069 |
/** Purges the parsed record. |
| 1070 |
@param[in,out] node row purge node |
| 1071 |
@param[in] undo_rec undo record to purge |
| 1072 |
@param[in,out] thr query thread |
| 1073 |
@param[in] updated_extern whether external columns were updated |
| 1074 |
@param[in,out] thd current thread |
| 1075 |
@return true if purged, false if skipped */ |
| 1076 |
[[nodiscard]] static bool row_purge_record_func( |
| 1077 |
purge_node_t *node, trx_undo_rec_t *undo_rec, |
| 1078 |
IF_DEBUG(const que_thr_t *thr, ) bool updated_extern, THD *thd) { |
| 1079 |
dict_index_t *clust_index; |
| 1080 |
bool purged = true; |
| 1081 |
|
| 1082 |
ut_ad(!node->found_clust); |
| 1083 |
ut_ad(!node->table->skip_alter_undo); |
| 1084 |
|
| 1085 |
clust_index = node->table->first_index(); |
| 1086 |
|
| 1087 |
node->index = clust_index->next(); |
| 1088 |
ut_ad(!trx_undo_roll_ptr_is_insert(node->roll_ptr)); |
| 1089 |
|
| 1090 |
switch (node->rec_type) { |
| 1091 |
case TRX_UNDO_DEL_MARK_REC: |
| 1092 |
purged = row_purge_del_mark(node); |
| 1093 |
if (!purged) { |
| 1094 |
break; |
| 1095 |
} |
| 1096 |
MONITOR_INC(MONITOR_N_DEL_ROW_PURGE); |
| 1097 |
break; |
| 1098 |
default: |
| 1099 |
if (!updated_extern) { |
| 1100 |
break; |
| 1101 |
} |
| 1102 |
[[fallthrough]]; |
| 1103 |
case TRX_UNDO_UPD_EXIST_REC: |
| 1104 |
DBUG_EXECUTE_IF("innodb_purge_sleep_12", |
| 1105 |
std::this_thread::sleep_for(std::chrono::seconds(5));); |
| 1106 |
row_purge_upd_exist_or_extern_func(IF_DEBUG(thr, ) node, undo_rec); |
| 1107 |
MONITOR_INC(MONITOR_N_UPD_EXIST_EXTERN); |
| 1108 |
break; |
| 1109 |
} |
| 1110 |
|
| 1111 |
if (node->update != nullptr) { |
| 1112 |
node->update->reset(); |
| 1113 |
} |
| 1114 |
|
| 1115 |
if (node->found_clust) { |
| 1116 |
node->pcur.close(); |
| 1117 |
node->found_clust = false; |
| 1118 |
} |
| 1119 |
|
| 1120 |
if (node->table != nullptr) { |
| 1121 |
if (node->mysql_table != nullptr) { |
| 1122 |
close_thread_tables(thd); |
| 1123 |
node->mysql_table = nullptr; |
| 1124 |
} |
| 1125 |
|
| 1126 |
if (dict_table_is_sdi(node->table->id)) { |
| 1127 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 1128 |
node->table = nullptr; |
| 1129 |
} else { |
| 1130 |
bool is_aux = node->table->is_fts_aux(); |
| 1131 |
dd_table_close(node->table, thd, &node->mdl, false); |
| 1132 |
if (is_aux && node->parent) { |
| 1133 |
dd_table_close(node->parent, thd, &node->parent_mdl, false); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
return (purged); |
| 1139 |
} |
| 1140 |
|
| 1141 |
inline bool row_purge_record(purge_node_t *node, trx_undo_rec_t *undo_rec, |
| 1142 |
const que_thr_t *thr [[maybe_unused]], |
| 1143 |
bool updated_extern, THD *thd) { |
| 1144 |
return row_purge_record_func(node, undo_rec, IF_DEBUG(thr, ) updated_extern, |
| 1145 |
thd); |
| 1146 |
} |
| 1147 |
|
| 1148 |
/** Fetches an undo log record and does the purge for the recorded operation. |
| 1149 |
If none left, or the current purge completed, returns the control to the |
| 1150 |
parent node, which is always a query thread node. */ |
| 1151 |
static void row_purge(purge_node_t *node, /*!< in: row purge node */ |
| 1152 |
trx_undo_rec_t *undo_rec, /*!< in: record to purge */ |
| 1153 |
que_thr_t *thr) /*!< in: query thread */ |
| 1154 |
{ |
| 1155 |
bool updated_extern; |
| 1156 |
THD *thd = current_thd; |
| 1157 |
|
| 1158 |
DBUG_EXECUTE_IF( |
| 1159 |
"do_not_meta_lock_in_background", |
| 1160 |
while (srv_shutdown_state.load() < SRV_SHUTDOWN_PURGE) { |
| 1161 |
std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 1162 |
} return;); |
| 1163 |
|
| 1164 |
while (row_purge_parse_undo_rec(node, undo_rec, &updated_extern, thd, thr)) { |
| 1165 |
bool purged; |
| 1166 |
|
| 1167 |
purged = row_purge_record(node, undo_rec, thr, updated_extern, thd); |
| 1168 |
|
| 1169 |
if (purged || srv_shutdown_state.load() >= SRV_SHUTDOWN_PURGE) { |
| 1170 |
return; |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* Retry the purge in a second. */ |
| 1174 |
std::this_thread::sleep_for(std::chrono::seconds(1)); |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
/** Reset the purge query thread. |
| 1179 |
@param[in,out] thr The query thread to execute */ |
| 1180 |
static void row_purge_end(que_thr_t *thr) { |
| 1181 |
purge_node_t *node; |
| 1182 |
|
| 1183 |
node = static_cast<purge_node_t *>(thr->run_node); |
| 1184 |
|
| 1185 |
ut_ad(que_node_get_type(node) == QUE_NODE_PURGE); |
| 1186 |
|
| 1187 |
thr->run_node = que_node_get_parent(node); |
| 1188 |
|
| 1189 |
if (node->recs != nullptr) { |
| 1190 |
ut_ad(node->recs->empty()); |
| 1191 |
|
| 1192 |
/* Note: We call the destructor explicitly here, but don't |
| 1193 |
want to free the memory. The Recs (and rows contained within) |
| 1194 |
were allocated from the purge_sys->heap */ |
| 1195 |
|
| 1196 |
call_destructor(node->recs); |
| 1197 |
|
| 1198 |
node->recs = nullptr; |
| 1199 |
} |
| 1200 |
|
| 1201 |
node->done = true; |
| 1202 |
|
| 1203 |
ut_a(thr->run_node != nullptr); |
| 1204 |
|
| 1205 |
mem_heap_empty(node->heap); |
| 1206 |
} |
| 1207 |
|
| 1208 |
/** Does the purge operation for a single undo log record. This is a high-level |
| 1209 |
function used in an SQL execution graph. |
| 1210 |
@param[in,out] thr The query thread to execute |
| 1211 |
@return query thread to run next or nullptr */ |
| 1212 |
que_thr_t *row_purge_step(que_thr_t *thr) { |
| 1213 |
purge_node_t *node; |
| 1214 |
|
| 1215 |
node = static_cast<purge_node_t *>(thr->run_node); |
| 1216 |
|
| 1217 |
node->table = nullptr; |
| 1218 |
node->row = nullptr; |
| 1219 |
node->ref = nullptr; |
| 1220 |
node->index = nullptr; |
| 1221 |
node->update = nullptr; |
| 1222 |
node->found_clust = false; |
| 1223 |
node->rec_type = ULINT_UNDEFINED; |
| 1224 |
node->cmpl_info = ULINT_UNDEFINED; |
| 1225 |
|
| 1226 |
ut_a(!node->done); |
| 1227 |
|
| 1228 |
ut_ad(que_node_get_type(node) == QUE_NODE_PURGE); |
| 1229 |
|
| 1230 |
if (node->recs != nullptr && !node->recs->empty()) { |
| 1231 |
purge_node_t::rec_t rec; |
| 1232 |
|
| 1233 |
rec = node->recs->front(); |
| 1234 |
node->recs->pop_front(); |
| 1235 |
|
| 1236 |
node->roll_ptr = rec.roll_ptr; |
| 1237 |
node->modifier_trx_id = rec.modifier_trx_id; |
| 1238 |
|
| 1239 |
row_purge(node, rec.undo_rec, thr); |
| 1240 |
|
| 1241 |
if (node->recs->empty()) { |
| 1242 |
row_purge_end(thr); |
| 1243 |
} else { |
| 1244 |
thr->run_node = node; |
| 1245 |
} |
| 1246 |
|
| 1247 |
} else { |
| 1248 |
row_purge_end(thr); |
| 1249 |
} |
| 1250 |
|
| 1251 |
return (thr); |
| 1252 |
} |
| 1253 |
|
| 1254 |
#ifdef UNIV_DEBUG |
| 1255 |
/** Validate the persisent cursor. The purge node has two references |
| 1256 |
to the clustered index record - one via the ref member, and the |
| 1257 |
other via the persistent cursor. These two references must match |
| 1258 |
each other if the found_clust flag is set. |
| 1259 |
@return true if the stored copy of persistent cursor is consistent |
| 1260 |
with the ref member.*/ |
| 1261 |
bool purge_node_t::validate_pcur() { |
| 1262 |
if (!found_clust) { |
| 1263 |
return (true); |
| 1264 |
} |
| 1265 |
|
| 1266 |
if (index == nullptr) { |
| 1267 |
return (true); |
| 1268 |
} |
| 1269 |
|
| 1270 |
if (index->type == DICT_FTS) { |
| 1271 |
return (true); |
| 1272 |
} |
| 1273 |
|
| 1274 |
if (!pcur.m_old_stored) { |
| 1275 |
return (true); |
| 1276 |
} |
| 1277 |
|
| 1278 |
dict_index_t *clust_index = pcur.m_btr_cur.index; |
| 1279 |
|
| 1280 |
ulint *offsets = |
| 1281 |
rec_get_offsets(pcur.m_old_rec, clust_index, nullptr, pcur.m_old_n_fields, |
| 1282 |
UT_LOCATION_HERE, &heap); |
| 1283 |
|
| 1284 |
/* Here we are comparing the purge ref record and the stored initial |
| 1285 |
part in persistent cursor. Both cases we store n_uniq fields of the |
| 1286 |
cluster index and so it is fine to do the comparison. We note this |
| 1287 |
dependency here as pcur and ref belong to different modules. */ |
| 1288 |
int st = cmp_dtuple_rec(ref, pcur.m_old_rec, clust_index, offsets); |
| 1289 |
|
| 1290 |
if (st != 0) { |
| 1291 |
ib::error(ER_IB_MSG_1010) << "Purge node pcur validation failed"; |
| 1292 |
ib::error(ER_IB_MSG_1011) << rec_printer(ref).str(); |
| 1293 |
ib::error(ER_IB_MSG_1012) << rec_printer(pcur.m_old_rec, offsets).str(); |
| 1294 |
return (false); |
| 1295 |
} |
| 1296 |
|
| 1297 |
return (true); |
| 1298 |
} |
| 1299 |
#endif /* UNIV_DEBUG */ |
| 1300 |
|
| 1301 |
bool purge_node_t::is_table_id_exists(table_id_t table_id) const { |
| 1302 |
if (recs == nullptr) { |
| 1303 |
return (false); |
| 1304 |
} |
| 1305 |
|
| 1306 |
for (auto iter = recs->begin(); iter != recs->end(); ++iter) { |
| 1307 |
table_id_t table_id2 = trx_undo_rec_get_table_id(iter->undo_rec); |
| 1308 |
if (table_id == table_id2) { |
| 1309 |
return (true); |
| 1310 |
} |
| 1311 |
} |
| 1312 |
return (false); |
| 1313 |
} |
| 1314 |
|
| 1315 |
#ifdef UNIV_DEBUG |
| 1316 |
/** Check if there are more than one undo record with same (trx_id, undo_no) |
| 1317 |
combination. |
| 1318 |
@return true when no duplicates are found, false otherwise. */ |
| 1319 |
bool purge_node_t::check_duplicate_undo_no() const { |
| 1320 |
using Two = std::pair<trx_id_t, undo_no_t>; |
| 1321 |
std::set<Two> trx_info; |
| 1322 |
using Iter = std::set<Two>::iterator; |
| 1323 |
|
| 1324 |
if (recs == nullptr) { |
| 1325 |
return (true); |
| 1326 |
} |
| 1327 |
|
| 1328 |
for (auto iter = recs->begin(); iter != recs->end(); ++iter) { |
| 1329 |
trx_id_t trxid = iter->modifier_trx_id; |
| 1330 |
undo_no_t undo_no = trx_undo_rec_get_undo_no(iter->undo_rec); |
| 1331 |
|
| 1332 |
std::pair<Iter, bool> ret = trx_info.insert(std::make_pair(trxid, undo_no)); |
| 1333 |
ut_ad(ret.second == true); |
| 1334 |
} |
| 1335 |
|
| 1336 |
return (true); |
| 1337 |
} |
| 1338 |
#endif /* UNIV_DEBUG */ |
| 1339 |
|
| 1340 |
void purge_node_t::add_lob_page(dict_index_t *index, const page_id_t &page_id) { |
| 1341 |
const index_id_t id(page_id.space(), index->id); |
| 1342 |
const auto tup = std::make_tuple(id, page_id, index->table->id); |
| 1343 |
ut_ad(m_lob_pages.find(tup) == m_lob_pages.end()); |
| 1344 |
m_lob_pages.insert(tup); |
| 1345 |
} |
| 1346 |
|
| 1347 |
void purge_node_t::free_lob_pages() { |
| 1348 |
#ifdef UNIV_DEBUG |
| 1349 |
{ |
| 1350 |
/** Ensure that the caller does not hold any latches. */ |
| 1351 |
sync_allowed_latches check; |
| 1352 |
ut_ad(!sync_check_iterate(check)); |
| 1353 |
mtr_t::check_my_thread_mtrs_are_not_latching(); |
| 1354 |
} |
| 1355 |
#endif |
| 1356 |
|
| 1357 |
mtr_t local_mtr; |
| 1358 |
|
| 1359 |
THD *thd = current_thd; |
| 1360 |
|
| 1361 |
for (const auto &tup : m_lob_pages) { |
| 1362 |
const index_id_t index_id = std::get<0>(tup); |
| 1363 |
const page_id_t &page_id = std::get<1>(tup); |
| 1364 |
const table_id_t table_id = std::get<2>(tup); |
| 1365 |
const space_id_t space_id = page_id.space(); |
| 1366 |
|
| 1367 |
MDL_ticket *mdl{}; |
| 1368 |
dict_table_t *table = dd_table_open_on_id(table_id, thd, &mdl, false, true); |
| 1369 |
|
| 1370 |
if (table == nullptr) { |
| 1371 |
continue; |
| 1372 |
} |
| 1373 |
|
| 1374 |
const dict_index_t *idx = table->first_index(); |
| 1375 |
|
| 1376 |
if (idx == nullptr || idx->id != index_id.m_index_id || |
| 1377 |
idx->space != space_id || idx->page == FIL_NULL || |
| 1378 |
idx->table->id != table_id) { |
| 1379 |
dd_table_close(table, thd, &mdl, false); |
| 1380 |
continue; |
| 1381 |
} |
| 1382 |
|
| 1383 |
dict_index_t *index = const_cast<dict_index_t *>(idx); |
| 1384 |
const page_size_t page_size = dict_table_page_size(index->table); |
| 1385 |
|
| 1386 |
fil_space_t *space = fil_space_acquire_silent(space_id); |
| 1387 |
|
| 1388 |
if (space != nullptr) { |
| 1389 |
mtr_start(&local_mtr); |
| 1390 |
buf_block_t *block = buf_page_get(page_id, page_size, RW_X_LATCH, |
| 1391 |
UT_LOCATION_HERE, &local_mtr); |
| 1392 |
|
| 1393 |
#ifdef UNIV_DEBUG |
| 1394 |
const page_type_t page_type = block->get_page_type(); |
| 1395 |
ut_ad(page_type == FIL_PAGE_TYPE_LOB_FIRST || |
| 1396 |
page_type == FIL_PAGE_TYPE_ZLOB_FIRST); |
| 1397 |
#endif /* UNIV_DEBUG */ |
| 1398 |
|
| 1399 |
btr_page_free_low(index, block, ULINT_UNDEFINED, &local_mtr); |
| 1400 |
mtr_commit(&local_mtr); |
| 1401 |
|
| 1402 |
fil_space_release(space); |
| 1403 |
} |
| 1404 |
dd_table_close(table, thd, &mdl, false); |
| 1405 |
} |
| 1406 |
|
| 1407 |
m_lob_pages.clear(); |
| 1408 |
} |