| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 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 |
#include <stddef.h> |
| 29 |
#include <sys/types.h> |
| 30 |
|
| 31 |
/** @file include/btr0cur.h |
| 32 |
The index tree cursor |
| 33 |
|
| 34 |
Created 10/16/1994 Heikki Tuuri |
| 35 |
*******************************************************/ |
| 36 |
|
| 37 |
#ifndef btr0cur_h |
| 38 |
#define btr0cur_h |
| 39 |
|
| 40 |
#include "btr0types.h" |
| 41 |
#include "dict0dict.h" |
| 42 |
#include "gis0type.h" |
| 43 |
#include "page0cur.h" |
| 44 |
#include "univ.i" |
| 45 |
|
| 46 |
/** Mode flags for btr_cur operations; these can be ORed */ |
| 47 |
enum { |
| 48 |
/** do no undo logging */ |
| 49 |
BTR_NO_UNDO_LOG_FLAG = 1, |
| 50 |
/** do no record lock checking */ |
| 51 |
BTR_NO_LOCKING_FLAG = 2, |
| 52 |
/** sys fields will be found in the update vector or inserted |
| 53 |
entry */ |
| 54 |
BTR_KEEP_SYS_FLAG = 4, |
| 55 |
/** btr_cur_pessimistic_update() must keep cursor position |
| 56 |
when moving columns to big_rec */ |
| 57 |
BTR_KEEP_POS_FLAG = 8, |
| 58 |
/** the caller is creating the index or wants to bypass the |
| 59 |
index->info.online creation log */ |
| 60 |
BTR_CREATE_FLAG = 16, |
| 61 |
/** the caller of btr_cur_optimistic_update() or |
| 62 |
btr_cur_update_in_place() will take care of |
| 63 |
updating IBUF_BITMAP_FREE */ |
| 64 |
BTR_KEEP_IBUF_BITMAP = 32 |
| 65 |
}; |
| 66 |
|
| 67 |
/* btr_cur_latch_leaves() returns latched blocks and savepoints. */ |
| 68 |
struct btr_latch_leaves_t { |
| 69 |
/* left block, target block and right block */ |
| 70 |
buf_block_t *blocks[3]; |
| 71 |
ulint savepoints[3]; |
| 72 |
}; |
| 73 |
|
| 74 |
#ifndef UNIV_HOTBACKUP |
| 75 |
#include "ha0ha.h" |
| 76 |
#include "que0types.h" |
| 77 |
#include "row0types.h" |
| 78 |
#endif /* !UNIV_HOTBACKUP */ |
| 79 |
|
| 80 |
#ifdef UNIV_DEBUG |
| 81 |
/** Returns the page cursor component of a tree cursor. |
| 82 |
@return pointer to page cursor component */ |
| 83 |
static inline page_cur_t *btr_cur_get_page_cur( |
| 84 |
const btr_cur_t *cursor); /*!< in: tree cursor */ |
| 85 |
/** Returns the buffer block on which the tree cursor is positioned. |
| 86 |
@return pointer to buffer block */ |
| 87 |
static inline buf_block_t *btr_cur_get_block( |
| 88 |
const btr_cur_t *cursor); /*!< in: tree cursor */ |
| 89 |
/** Returns the record pointer of a tree cursor. |
| 90 |
@return pointer to record */ |
| 91 |
static inline rec_t *btr_cur_get_rec( |
| 92 |
const btr_cur_t *cursor); /*!< in: tree cursor */ |
| 93 |
#else /* UNIV_DEBUG */ |
| 94 |
#define btr_cur_get_page_cur(cursor) (&(cursor)->page_cur) |
| 95 |
#define btr_cur_get_block(cursor) ((cursor)->page_cur.block) |
| 96 |
#define btr_cur_get_rec(cursor) ((cursor)->page_cur.rec) |
| 97 |
#endif /* UNIV_DEBUG */ |
| 98 |
/** Returns the compressed page on which the tree cursor is positioned. |
| 99 |
@return pointer to compressed page, or NULL if the page is not compressed */ |
| 100 |
static inline page_zip_des_t *btr_cur_get_page_zip( |
| 101 |
btr_cur_t *cursor); /*!< in: tree cursor */ |
| 102 |
/** Returns the page of a tree cursor. |
| 103 |
@return pointer to page */ |
| 104 |
static inline page_t *btr_cur_get_page( |
| 105 |
btr_cur_t *cursor); /*!< in: tree cursor */ |
| 106 |
|
| 107 |
/** Positions a tree cursor at a given record. |
| 108 |
@param[in] index index |
| 109 |
@param[in] rec record in tree |
| 110 |
@param[in] block buffer block of rec |
| 111 |
@param[in] cursor cursor */ |
| 112 |
static inline void btr_cur_position(dict_index_t *index, rec_t *rec, |
| 113 |
buf_block_t *block, btr_cur_t *cursor); |
| 114 |
|
| 115 |
/** Optimistically latches the leaf page or pages requested. |
| 116 |
@param[in] block Guessed buffer block |
| 117 |
@param[in] modify_clock Modify clock value |
| 118 |
@param[in,out] latch_mode BTR_SEARCH_LEAF, ... |
| 119 |
@param[in,out] cursor Cursor |
| 120 |
@param[in] file File name |
| 121 |
@param[in] line Line where called |
| 122 |
@param[in] mtr Mini-transaction |
| 123 |
@return true if success */ |
| 124 |
bool btr_cur_optimistic_latch_leaves(buf_block_t *block, uint64_t modify_clock, |
| 125 |
ulint *latch_mode, btr_cur_t *cursor, |
| 126 |
const char *file, ulint line, mtr_t *mtr); |
| 127 |
|
| 128 |
/** Searches an index tree and positions a tree cursor on a given level. |
| 129 |
NOTE: n_fields_cmp in tuple must be set so that it cannot be compared |
| 130 |
to node pointer page number fields on the upper levels of the tree! |
| 131 |
Note that if mode is PAGE_CUR_LE, which is used in inserts, then |
| 132 |
cursor->up_match and cursor->low_match both will have sensible values. |
| 133 |
If mode is PAGE_CUR_GE, then up_match will a have a sensible value. */ |
| 134 |
void btr_cur_search_to_nth_level( |
| 135 |
dict_index_t *index, /*!< in: index */ |
| 136 |
ulint level, /*!< in: the tree level of search */ |
| 137 |
const dtuple_t *tuple, /*!< in: data tuple; NOTE: n_fields_cmp in |
| 138 |
tuple must be set so that it cannot get |
| 139 |
compared to the node ptr page number field! */ |
| 140 |
page_cur_mode_t mode, /*!< in: PAGE_CUR_L, ...; |
| 141 |
NOTE that if the search is made using a unique |
| 142 |
prefix of a record, mode should be PAGE_CUR_LE, |
| 143 |
not PAGE_CUR_GE, as the latter may end up on |
| 144 |
the previous page of the record! Inserts |
| 145 |
should always be made using PAGE_CUR_LE to |
| 146 |
search the position! */ |
| 147 |
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ..., ORed with |
| 148 |
at most one of BTR_INSERT, BTR_DELETE_MARK, |
| 149 |
BTR_DELETE, or BTR_ESTIMATE; |
| 150 |
cursor->left_block is used to store a pointer |
| 151 |
to the left neighbor page, in the cases |
| 152 |
BTR_SEARCH_PREV and BTR_MODIFY_PREV; |
| 153 |
NOTE that if has_search_latch |
| 154 |
is != 0, we maybe do not have a latch set |
| 155 |
on the cursor page, we assume |
| 156 |
the caller uses his search latch |
| 157 |
to protect the record! */ |
| 158 |
btr_cur_t *cursor, /*!< in/out: tree cursor; the cursor page is |
| 159 |
s- or x-latched, but see also above! */ |
| 160 |
ulint has_search_latch, |
| 161 |
/*!< in: latch mode the caller |
| 162 |
currently has on search system: |
| 163 |
RW_S_LATCH, or 0 */ |
| 164 |
const char *file, /*!< in: file name */ |
| 165 |
ulint line, /*!< in: line where called */ |
| 166 |
mtr_t *mtr); /*!< in: mtr */ |
| 167 |
|
| 168 |
/** Searches an index tree and positions a tree cursor on a given level. |
| 169 |
This function will avoid placing latches while traversing the path and so |
| 170 |
should be used only for cases where-in latching is not needed. |
| 171 |
|
| 172 |
@param[in] index Index |
| 173 |
@param[in] level The tree level of search |
| 174 |
@param[in] tuple Data tuple; Note: n_fields_cmp in compared |
| 175 |
to the node ptr page node field |
| 176 |
@param[in] mode PAGE_CUR_L, .... |
| 177 |
Insert should always be made using PAGE_CUR_LE |
| 178 |
to search the position. |
| 179 |
@param[in,out] cursor Tree cursor; points to record of interest. |
| 180 |
@param[in] file File name |
| 181 |
@param[in] line Line where called from |
| 182 |
@param[in,out] mtr Mini-transaction |
| 183 |
@param[in] mark_dirty if true then mark the block as dirty */ |
| 184 |
void btr_cur_search_to_nth_level_with_no_latch( |
| 185 |
dict_index_t *index, ulint level, const dtuple_t *tuple, |
| 186 |
page_cur_mode_t mode, btr_cur_t *cursor, const char *file, ulint line, |
| 187 |
mtr_t *mtr, bool mark_dirty = true); |
| 188 |
|
| 189 |
/** Opens a cursor at either end of an index. |
| 190 |
@param[in] from_left True if open to the low end, false if to the high |
| 191 |
end |
| 192 |
@param[in] index Index |
| 193 |
@param[in] latch_mode Latch mode |
| 194 |
@param[in,out] cursor Cursor |
| 195 |
@param[in] level Level to search for (0=leaf) |
| 196 |
@param[in] location Location where called |
| 197 |
@param[in,out] mtr Mini-transaction */ |
| 198 |
void btr_cur_open_at_index_side(bool from_left, dict_index_t *index, |
| 199 |
ulint latch_mode, btr_cur_t *cursor, |
| 200 |
ulint level, ut::Location location, mtr_t *mtr); |
| 201 |
|
| 202 |
/** Opens a cursor at either end of an index. |
| 203 |
Avoid taking latches on buffer, just pin (by incrementing fix_count) |
| 204 |
to keep them in buffer pool. This mode is used by intrinsic table |
| 205 |
as they are not shared and so there is no need of latching. |
| 206 |
@param[in] from_left true if open to low end, false if open to high |
| 207 |
end. |
| 208 |
@param[in] index Index |
| 209 |
@param[in,out] cursor Cursor |
| 210 |
@param[in] level Level to search for (0=leaf) |
| 211 |
@param[in] location Location where called |
| 212 |
@param[in,out] mtr Mini-transaction */ |
| 213 |
void btr_cur_open_at_index_side_with_no_latch(bool from_left, |
| 214 |
dict_index_t *index, |
| 215 |
btr_cur_t *cursor, ulint level, |
| 216 |
ut::Location location, |
| 217 |
mtr_t *mtr); |
| 218 |
|
| 219 |
/** Positions a cursor at a randomly chosen position within a B-tree. |
| 220 |
@return true if the index is available and we have put the cursor, false |
| 221 |
if the index is unavailable */ |
| 222 |
bool btr_cur_open_at_rnd_pos(dict_index_t *index, /*!< in: index */ |
| 223 |
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */ |
| 224 |
btr_cur_t *cursor, /*!< in/out: B-tree cursor */ |
| 225 |
const char *file, /*!< in: file name */ |
| 226 |
ulint line, /*!< in: line where called */ |
| 227 |
mtr_t *mtr); /*!< in: mtr */ |
| 228 |
/** Tries to perform an insert to a page in an index tree, next to cursor. |
| 229 |
It is assumed that mtr holds an x-latch on the page. The operation does |
| 230 |
not succeed if there is too little space on the page. If there is just |
| 231 |
one record on the page, the insert will always succeed; this is to |
| 232 |
prevent trying to split a page with just one record. |
| 233 |
@return DB_SUCCESS, DB_WAIT_LOCK, DB_FAIL, or error number */ |
| 234 |
[[nodiscard]] dberr_t btr_cur_optimistic_insert( |
| 235 |
ulint flags, /*!< in: undo logging and locking flags: if not |
| 236 |
zero, the parameters index and thr should be |
| 237 |
specified */ |
| 238 |
btr_cur_t *cursor, /*!< in: cursor on page after which to insert; |
| 239 |
cursor stays valid */ |
| 240 |
ulint **offsets, /*!< out: offsets on *rec */ |
| 241 |
mem_heap_t **heap, /*!< in/out: pointer to memory heap, or NULL */ |
| 242 |
dtuple_t *entry, /*!< in/out: entry to insert */ |
| 243 |
rec_t **rec, /*!< out: pointer to inserted record if |
| 244 |
succeed */ |
| 245 |
big_rec_t **big_rec, /*!< out: big rec vector whose fields have to |
| 246 |
be stored externally by the caller, or |
| 247 |
NULL */ |
| 248 |
que_thr_t *thr, /*!< in: query thread or NULL */ |
| 249 |
mtr_t *mtr); /*!< in/out: mini-transaction; |
| 250 |
if this function returns DB_SUCCESS on |
| 251 |
a leaf page of a secondary index in a |
| 252 |
compressed tablespace, the caller must |
| 253 |
mtr_commit(mtr) before latching |
| 254 |
any further pages */ |
| 255 |
/** Performs an insert on a page of an index tree. It is assumed that mtr |
| 256 |
holds an x-latch on the tree and on the cursor page. If the insert is |
| 257 |
made on the leaf level, to avoid deadlocks, mtr must also own x-latches |
| 258 |
to brothers of page, if those brothers exist. |
| 259 |
@return DB_SUCCESS or error number */ |
| 260 |
[[nodiscard]] dberr_t btr_cur_pessimistic_insert( |
| 261 |
uint32_t flags, /*!< in: undo logging and locking flags: if not |
| 262 |
zero, the parameter thr should be |
| 263 |
specified; if no undo logging is specified, |
| 264 |
then the caller must have reserved enough |
| 265 |
free extents in the file space so that the |
| 266 |
insertion will certainly succeed */ |
| 267 |
btr_cur_t *cursor, /*!< in: cursor after which to insert; |
| 268 |
cursor stays valid */ |
| 269 |
ulint **offsets, /*!< out: offsets on *rec */ |
| 270 |
mem_heap_t **heap, /*!< in/out: pointer to memory heap |
| 271 |
that can be emptied, or NULL */ |
| 272 |
dtuple_t *entry, /*!< in/out: entry to insert */ |
| 273 |
rec_t **rec, /*!< out: pointer to inserted record if |
| 274 |
succeed */ |
| 275 |
big_rec_t **big_rec, /*!< out: big rec vector whose fields have to |
| 276 |
be stored externally by the caller, or |
| 277 |
NULL */ |
| 278 |
que_thr_t *thr, /*!< in: query thread or NULL */ |
| 279 |
mtr_t *mtr); /*!< in/out: mini-transaction */ |
| 280 |
/** See if there is enough place in the page modification log to log |
| 281 |
an update-in-place. |
| 282 |
|
| 283 |
@param[in,out] page_zip Compressed page. |
| 284 |
@param[in,out] cursor B-tree page cursor. |
| 285 |
@param[in] index The index corresponding to cursor. |
| 286 |
@param[in,out] offsets Offsets of the cursor record. |
| 287 |
@param[in] length size needed |
| 288 |
@param[in] create true=delete-and-insert, false=update-in-place |
| 289 |
@param[in,out] mtr Mini-transaction. |
| 290 |
@retval false if out of space; IBUF_BITMAP_FREE will be reset |
| 291 |
outside mtr if the page was re-compressed |
| 292 |
@retval true if enough place; |
| 293 |
|
| 294 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE if this is |
| 295 |
a secondary index leaf page. This has to be done either within the |
| 296 |
same mini-transaction, or by invoking ibuf_reset_free_bits() before |
| 297 |
mtr_commit(mtr). */ |
| 298 |
[[nodiscard]] bool btr_cur_update_alloc_zip_func( |
| 299 |
page_zip_des_t *page_zip, page_cur_t *cursor, dict_index_t *index, |
| 300 |
IF_DEBUG(ulint *offsets, ) ulint length, bool create, mtr_t *mtr); |
| 301 |
|
| 302 |
inline bool btr_cur_update_alloc_zip(page_zip_des_t *page_zip, |
| 303 |
page_cur_t *cursor, dict_index_t *index, |
| 304 |
ulint *offsets [[maybe_unused]], ulint len, |
| 305 |
bool cr, mtr_t *mtr) { |
| 306 |
return btr_cur_update_alloc_zip_func(page_zip, cursor, index, |
| 307 |
IF_DEBUG(offsets, ) len, cr, mtr); |
| 308 |
} |
| 309 |
|
| 310 |
/** Updates a record when the update causes no size changes in its fields. |
| 311 |
@param[in] flags Undo logging and locking flags |
| 312 |
@param[in] cursor Cursor on the record to update; cursor stays valid and |
| 313 |
positioned on the same record |
| 314 |
@param[in,out] offsets Offsets on cursor->page_cur.rec |
| 315 |
@param[in] update Update vector |
| 316 |
@param[in] cmpl_info Compiler info on secondary index updates |
| 317 |
@param[in] thr Query thread, or null if flags & (btr_no_locking_flag | |
| 318 |
btr_no_undo_log_flag | btr_create_flag | btr_keep_sys_flag) |
| 319 |
@param[in] trx_id Transaction id |
| 320 |
@param[in,out] mtr Mini-transaction; if this is a secondary index, the caller |
| 321 |
must mtr_commit(mtr) before latching any further pages |
| 322 |
@return locking or undo log related error code, or |
| 323 |
@retval DB_SUCCESS on success |
| 324 |
@retval DB_ZIP_OVERFLOW if there is not enough space left |
| 325 |
on the compressed page (IBUF_BITMAP_FREE was reset outside mtr) */ |
| 326 |
[[nodiscard]] dberr_t btr_cur_update_in_place(ulint flags, btr_cur_t *cursor, |
| 327 |
ulint *offsets, |
| 328 |
const upd_t *update, |
| 329 |
ulint cmpl_info, que_thr_t *thr, |
| 330 |
trx_id_t trx_id, mtr_t *mtr); |
| 331 |
|
| 332 |
/** Writes a redo log record of updating a record in-place. |
| 333 |
@param[in] flags Undo logging and locking flags |
| 334 |
@param[in] rec Record |
| 335 |
@param[in] index Index of the record |
| 336 |
@param[in] update Update vector |
| 337 |
@param[in] trx_id Transaction id |
| 338 |
@param[in] roll_ptr Roll ptr |
| 339 |
@param[in] mtr Mini-transaction */ |
| 340 |
void btr_cur_update_in_place_log(ulint flags, const rec_t *rec, |
| 341 |
dict_index_t *index, const upd_t *update, |
| 342 |
trx_id_t trx_id, roll_ptr_t roll_ptr, |
| 343 |
mtr_t *mtr); |
| 344 |
|
| 345 |
/** Tries to update a record on a page in an index tree. It is assumed that mtr |
| 346 |
holds an x-latch on the page. The operation does not succeed if there is too |
| 347 |
little space on the page or if the update would result in too empty a page, |
| 348 |
so that tree compression is recommended. We assume here that the ordering |
| 349 |
fields of the record do not change. |
| 350 |
@param[in] flags undo logging and locking flags |
| 351 |
@param[in] cursor cursor on the record to update; cursor stays valid and |
| 352 |
positioned on the same record |
| 353 |
@param[out] offsets offsets on cursor->page_cur.rec |
| 354 |
@param[in,out] heap pointer to nullptr or memory heap |
| 355 |
@param[in] update update vector; this must also contain trx id and roll |
| 356 |
ptr fields |
| 357 |
@param[in] cmpl_info compiler info on secondary index updates |
| 358 |
@param[in] thr query thread, or nullptr if flags & |
| 359 |
(BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_CREATE_FLAG | |
| 360 |
BTR_KEEP_SYS_FLAG) |
| 361 |
@param[in] trx_id transaction id |
| 362 |
@param[in,out] mtr mini-transaction; if this is a secondary index, the |
| 363 |
caller must mtr_commit(mtr) before latching any further pages |
| 364 |
@return error code, including |
| 365 |
@retval DB_SUCCESS on success |
| 366 |
@retval DB_OVERFLOW if the updated record does not fit |
| 367 |
@retval DB_UNDERFLOW if the page would become too empty |
| 368 |
@retval DB_ZIP_OVERFLOW if there is not enough space left |
| 369 |
on the compressed page (IBUF_BITMAP_FREE was reset outside mtr) */ |
| 370 |
[[nodiscard]] dberr_t btr_cur_optimistic_update(ulint flags, btr_cur_t *cursor, |
| 371 |
ulint **offsets, |
| 372 |
mem_heap_t **heap, |
| 373 |
const upd_t *update, |
| 374 |
ulint cmpl_info, que_thr_t *thr, |
| 375 |
trx_id_t trx_id, mtr_t *mtr); |
| 376 |
|
| 377 |
/** Performs an update of a record on a page of a tree. It is assumed |
| 378 |
that mtr holds an x-latch on the tree and on the cursor page. If the |
| 379 |
update is made on the leaf level, to avoid deadlocks, mtr must also |
| 380 |
own x-latches to brothers of page, if those brothers exist. |
| 381 |
@param[in] flags Undo logging, locking, and rollback flags |
| 382 |
@param[in,out] cursor cursor on the record to update; |
| 383 |
cursor may become invalid if *big_rec == NULL |
| 384 |
|| !(flags & BTR_KEEP_POS_FLAG) |
| 385 |
@param[out] offsets Offsets on cursor->page_cur.rec |
| 386 |
@param[in,out] offsets_heap Pointer to memory heap that can be emptied, |
| 387 |
or NULL |
| 388 |
@param[in,out] entry_heap Memory heap for allocating big_rec and the |
| 389 |
index tuple. |
| 390 |
@param[out] big_rec Big rec vector whose fields have to be stored |
| 391 |
externally by the caller, or NULL |
| 392 |
@param[in,out] update Update vector; this is allowed to also contain |
| 393 |
trx id and roll ptr fields. Non-updated columns |
| 394 |
that are moved offpage will be appended to |
| 395 |
this. |
| 396 |
@param[in] cmpl_info Compiler info on secondary index updates |
| 397 |
@param[in] thr Query thread, or NULL if flags & |
| 398 |
(BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | |
| 399 |
BTR_CREATE_FLAG | BTR_KEEP_SYS_FLAG) |
| 400 |
@param[in] trx_id Transaction id |
| 401 |
@param[in] undo_no Undo number of the transaction. This is needed |
| 402 |
for rollback to savepoint of partially updated |
| 403 |
LOB. |
| 404 |
@param[in,out] mtr Mini-transaction; must be committed before |
| 405 |
latching any further pages |
| 406 |
@param[in] pcur The persistent cursor on the record to update. |
| 407 |
@return DB_SUCCESS or error code */ |
| 408 |
[[nodiscard]] dberr_t btr_cur_pessimistic_update( |
| 409 |
ulint flags, btr_cur_t *cursor, ulint **offsets, mem_heap_t **offsets_heap, |
| 410 |
mem_heap_t *entry_heap, big_rec_t **big_rec, upd_t *update, ulint cmpl_info, |
| 411 |
que_thr_t *thr, trx_id_t trx_id, undo_no_t undo_no, mtr_t *mtr, |
| 412 |
btr_pcur_t *pcur = nullptr); |
| 413 |
|
| 414 |
/** Marks a clustered index record deleted. Writes an undo log record to |
| 415 |
undo log on this delete marking. Writes in the trx id field the id |
| 416 |
of the deleting transaction, and in the roll ptr field pointer to the |
| 417 |
undo log record created. |
| 418 |
@return DB_SUCCESS, DB_LOCK_WAIT, or error number */ |
| 419 |
[[nodiscard]] dberr_t btr_cur_del_mark_set_clust_rec( |
| 420 |
ulint flags, /*!< in: undo logging and locking flags */ |
| 421 |
buf_block_t *block, /*!< in/out: buffer block of the record */ |
| 422 |
rec_t *rec, /*!< in/out: record */ |
| 423 |
dict_index_t *index, /*!< in: clustered index of the record */ |
| 424 |
const ulint *offsets, /*!< in: rec_get_offsets(rec) */ |
| 425 |
que_thr_t *thr, /*!< in: query thread */ |
| 426 |
const dtuple_t *entry, /*!< in: dtuple for the deleting record */ |
| 427 |
mtr_t *mtr); /*!< in/out: mini-transaction */ |
| 428 |
/** Sets a secondary index record delete mark to true or false. |
| 429 |
@return DB_SUCCESS, DB_LOCK_WAIT, or error number */ |
| 430 |
[[nodiscard]] dberr_t btr_cur_del_mark_set_sec_rec( |
| 431 |
ulint flags, /*!< in: locking flag */ |
| 432 |
btr_cur_t *cursor, /*!< in: cursor */ |
| 433 |
bool val, /*!< in: value to set */ |
| 434 |
que_thr_t *thr, /*!< in: query thread */ |
| 435 |
mtr_t *mtr); /*!< in/out: mini-transaction */ |
| 436 |
/** Tries to compress a page of the tree if it seems useful. It is assumed |
| 437 |
that mtr holds an x-latch on the tree and on the cursor page. To avoid |
| 438 |
deadlocks, mtr must also own x-latches to brothers of page, if those |
| 439 |
brothers exist. NOTE: it is assumed that the caller has reserved enough |
| 440 |
free extents so that the compression will always succeed if done! |
| 441 |
@return true if compression occurred */ |
| 442 |
bool btr_cur_compress_if_useful( |
| 443 |
btr_cur_t *cursor, /*!< in/out: cursor on the page to compress; |
| 444 |
cursor does not stay valid if compression |
| 445 |
occurs */ |
| 446 |
bool adjust, /*!< in: true if should adjust the |
| 447 |
cursor position even if compression occurs */ |
| 448 |
mtr_t *mtr); /*!< in/out: mini-transaction */ |
| 449 |
|
| 450 |
[[nodiscard]] bool btr_cur_optimistic_delete_func(btr_cur_t *cursor, |
| 451 |
IF_DEBUG(ulint flags, ) |
| 452 |
mtr_t *mtr); |
| 453 |
|
| 454 |
/** Removes the record on which the tree cursor is positioned on a leaf page. |
| 455 |
It is assumed that the mtr has an x-latch on the page where the cursor is |
| 456 |
positioned, but no latch on the whole tree. |
| 457 |
@param[in] cursor cursor on leaf page, on the record to delete; cursor stays |
| 458 |
valid: if deletion succeeds, on function exit it points to the successor of the |
| 459 |
deleted record |
| 460 |
@param[in] flags BTR_CREATE_FLAG or 0 |
| 461 |
@param[in] mtr if this function returns true on a leaf page of a secondary |
| 462 |
index, the mtr must be committed before latching any further pages |
| 463 |
@return true if success, i.e., the page did not become too empty */ |
| 464 |
inline bool btr_cur_optimistic_delete(btr_cur_t *cursor, |
| 465 |
ulint flags [[maybe_unused]], |
| 466 |
mtr_t *mtr) { |
| 467 |
return btr_cur_optimistic_delete_func(cursor, IF_DEBUG(flags, ) mtr); |
| 468 |
} |
| 469 |
|
| 470 |
/** Removes the record on which the tree cursor is positioned. Tries |
| 471 |
to compress the page if its fillfactor drops below a threshold |
| 472 |
or if it is the only page on the level. It is assumed that mtr holds |
| 473 |
an x-latch on the tree and on the cursor page. To avoid deadlocks, |
| 474 |
mtr must also own x-latches to brothers of page, if those brothers |
| 475 |
exist. |
| 476 |
@param[out] err DB_SUCCESS or DB_OUT_OF_FILE_SPACE; the latter may occur |
| 477 |
because we may have to update node pointers on upper |
| 478 |
levels, and in the case of variable length keys these may |
| 479 |
actually grow in size |
| 480 |
@param[in] has_reserved_extents true if the caller has already reserved |
| 481 |
enough free extents so that he knows |
| 482 |
that the operation will succeed |
| 483 |
@param[in] cursor Cursor on the record to delete; if compression does not |
| 484 |
occur, the cursor stays valid: it points to successor of |
| 485 |
deleted record on function exit |
| 486 |
@param[in] flags BTR_CREATE_FLAG or 0 |
| 487 |
@param[in] rollback True if performing rollback, false otherwise. |
| 488 |
@param[in] trx_id The current transaction id. |
| 489 |
@param[in] undo_no Undo number of the transaction. This is needed for |
| 490 |
rollback to savepoint of partially updated LOB. |
| 491 |
@param[in] rec_type Undo record type. |
| 492 |
@param[in] mtr The mini transaction |
| 493 |
@param[in] pcur Persistent cursor on the record to delete. |
| 494 |
@param[in,out] node purge node or nullptr |
| 495 |
@return true if compression occurred */ |
| 496 |
bool btr_cur_pessimistic_delete(dberr_t *err, bool has_reserved_extents, |
| 497 |
btr_cur_t *cursor, uint32_t flags, |
| 498 |
bool rollback, trx_id_t trx_id, |
| 499 |
undo_no_t undo_no, ulint rec_type, mtr_t *mtr, |
| 500 |
btr_pcur_t *pcur, purge_node_t *node); |
| 501 |
|
| 502 |
/** Parses a redo log record of updating a record in-place. |
| 503 |
@return end of log record or NULL */ |
| 504 |
byte *btr_cur_parse_update_in_place( |
| 505 |
byte *ptr, /*!< in: buffer */ |
| 506 |
byte *end_ptr, /*!< in: buffer end */ |
| 507 |
page_t *page, /*!< in/out: page or NULL */ |
| 508 |
page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ |
| 509 |
dict_index_t *index); /*!< in: index corresponding to page */ |
| 510 |
/** Parses the redo log record for delete marking or unmarking of a |
| 511 |
clustered index record. |
| 512 |
@return end of log record or NULL */ |
| 513 |
byte *btr_cur_parse_del_mark_set_clust_rec( |
| 514 |
byte *ptr, /*!< in: buffer */ |
| 515 |
byte *end_ptr, /*!< in: buffer end */ |
| 516 |
page_t *page, /*!< in/out: page or NULL */ |
| 517 |
page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ |
| 518 |
dict_index_t *index); /*!< in: index corresponding to page */ |
| 519 |
/** Parses the redo log record for delete marking or unmarking of a |
| 520 |
secondary index record. |
| 521 |
@return end of log record or NULL */ |
| 522 |
byte *btr_cur_parse_del_mark_set_sec_rec( |
| 523 |
byte *ptr, /*!< in: buffer */ |
| 524 |
byte *end_ptr, /*!< in: buffer end */ |
| 525 |
page_t *page, /*!< in/out: page or NULL */ |
| 526 |
page_zip_des_t *page_zip); /*!< in/out: compressed page, or NULL */ |
| 527 |
#ifndef UNIV_HOTBACKUP |
| 528 |
|
| 529 |
/** Estimates the number of rows in a given index range. |
| 530 |
@param[in] index index |
| 531 |
@param[in] tuple1 range start, may also be empty tuple |
| 532 |
@param[in] mode1 search mode for range start |
| 533 |
@param[in] tuple2 range end, may also be empty tuple |
| 534 |
@param[in] mode2 search mode for range end |
| 535 |
@return estimated number of rows */ |
| 536 |
int64_t btr_estimate_n_rows_in_range(dict_index_t *index, |
| 537 |
const dtuple_t *tuple1, |
| 538 |
page_cur_mode_t mode1, |
| 539 |
const dtuple_t *tuple2, |
| 540 |
page_cur_mode_t mode2); |
| 541 |
|
| 542 |
/** Estimates the number of different key values in a given index, for |
| 543 |
each n-column prefix of the index where 1 <= n <= |
| 544 |
dict_index_get_n_unique(index). The estimates are stored in the array |
| 545 |
index->stat_n_diff_key_vals[] (indexed 0..n_uniq-1) and the number of pages |
| 546 |
that were sampled is saved in index->stat_n_sample_sizes[]. If |
| 547 |
innodb_stats_method is nulls_ignored, we also record the number of non-null |
| 548 |
values for each prefix and stored the estimates in array |
| 549 |
index->stat_n_non_null_key_vals. |
| 550 |
@return true if the index is available and we get the estimated numbers, |
| 551 |
false if the index is unavailable. */ |
| 552 |
bool btr_estimate_number_of_different_key_vals( |
| 553 |
dict_index_t *index); /*!< in: index */ |
| 554 |
|
| 555 |
/** Copies an externally stored field of a record to mem heap. |
| 556 |
@param[in] trx the trx doing the operation. |
| 557 |
@param[in] index index containing the LOB. |
| 558 |
@param[in] rec record in a clustered index; must be |
| 559 |
protected by a lock or a page latch |
| 560 |
@param[in] offsets array returned by rec_get_offsets() |
| 561 |
@param[in] page_size BLOB page size |
| 562 |
@param[in] no field number |
| 563 |
@param[out] len length of the field |
| 564 |
@param[out] lob_version version of lob |
| 565 |
@param[in] is_sdi true for SDI Indexes |
| 566 |
@param[in,out] heap mem heap |
| 567 |
@return the field copied to heap, or NULL if the field is incomplete */ |
| 568 |
byte *btr_rec_copy_externally_stored_field_func( |
| 569 |
trx_t *trx, dict_index_t *index, const rec_t *rec, const ulint *offsets, |
| 570 |
const page_size_t &page_size, ulint no, ulint *len, size_t *lob_version, |
| 571 |
IF_DEBUG(bool is_sdi, ) mem_heap_t *heap); |
| 572 |
|
| 573 |
/** Sets a secondary index record's delete mark to the given value. This |
| 574 |
function is only used by the insert buffer merge mechanism. */ |
| 575 |
void btr_cur_set_deleted_flag_for_ibuf( |
| 576 |
rec_t *rec, /*!< in/out: record */ |
| 577 |
page_zip_des_t *page_zip, /*!< in/out: compressed page |
| 578 |
corresponding to rec, or NULL |
| 579 |
when the tablespace is uncompressed */ |
| 580 |
bool val, /*!< in: value to set */ |
| 581 |
mtr_t *mtr); /*!< in/out: mini-transaction */ |
| 582 |
|
| 583 |
/** The following function is used to set the deleted bit of a record. |
| 584 |
@param[in,out] rec physical record |
| 585 |
@param[in,out] page_zip compressed page (or NULL) |
| 586 |
@param[in] flag nonzero if delete marked */ |
| 587 |
static inline void btr_rec_set_deleted_flag(rec_t *rec, |
| 588 |
page_zip_des_t *page_zip, |
| 589 |
bool flag); |
| 590 |
|
| 591 |
/** Latches the leaf page or pages requested. |
| 592 |
@param[in] block Leaf page where the search converged |
| 593 |
@param[in] page_id Page id of the leaf |
| 594 |
@param[in] page_size Page size |
| 595 |
@param[in] latch_mode BTR_SEARCH_LEAF, ... |
| 596 |
@param[in] cursor Cursor |
| 597 |
@param[in] mtr Mini-transaction |
| 598 |
@return blocks and savepoints which actually latched. */ |
| 599 |
btr_latch_leaves_t btr_cur_latch_leaves(buf_block_t *block, |
| 600 |
const page_id_t &page_id, |
| 601 |
const page_size_t &page_size, |
| 602 |
ulint latch_mode, btr_cur_t *cursor, |
| 603 |
mtr_t *mtr); |
| 604 |
#endif /* !UNIV_HOTBACKUP */ |
| 605 |
|
| 606 |
/*######################################################################*/ |
| 607 |
|
| 608 |
/** In the pessimistic delete, if the page data size drops below this |
| 609 |
limit, merging it to a neighbor is tried */ |
| 610 |
#define BTR_CUR_PAGE_COMPRESS_LIMIT(index) \ |
| 611 |
((UNIV_PAGE_SIZE * (ulint)((index)->merge_threshold)) / 100) |
| 612 |
|
| 613 |
/** A slot in the path array. We store here info on a search path down the |
| 614 |
tree. Each slot contains data on a single level of the tree. */ |
| 615 |
struct btr_path_t { |
| 616 |
/* Assume a page like: |
| 617 |
records: (inf, a, b, c, d, sup) |
| 618 |
index of the record: 0, 1, 2, 3, 4, 5 |
| 619 |
*/ |
| 620 |
|
| 621 |
/** Index of the record where the page cursor stopped on this level |
| 622 |
(index in alphabetical order). Value ULINT_UNDEFINED denotes array |
| 623 |
end. In the above example, if the search stopped on record 'c', then |
| 624 |
nth_rec will be 3. */ |
| 625 |
ulint nth_rec{ULINT_UNDEFINED}; |
| 626 |
|
| 627 |
/** Number of the records on the page, not counting inf and sup. |
| 628 |
In the above example n_recs will be 4. */ |
| 629 |
ulint n_recs{ULINT_UNDEFINED}; |
| 630 |
|
| 631 |
/** Number of the page containing the record. */ |
| 632 |
page_no_t page_no{FIL_NULL}; |
| 633 |
|
| 634 |
/** Level of the page. If later we fetch the page under page_no |
| 635 |
and it is on a different level then we know that the tree has been |
| 636 |
reorganized. */ |
| 637 |
ulint page_level{ULINT_UNDEFINED}; |
| 638 |
}; |
| 639 |
|
| 640 |
/** size of path array (in slots) */ |
| 641 |
constexpr uint32_t BTR_PATH_ARRAY_N_SLOTS = 250; |
| 642 |
|
| 643 |
/** Values for the flag documenting the used search method */ |
| 644 |
enum btr_cur_method { |
| 645 |
/** Flag for initialization only, not in real use.*/ |
| 646 |
BTR_CUR_UNSET = 0, |
| 647 |
/** successful shortcut using the hash index */ |
| 648 |
BTR_CUR_HASH = 1, |
| 649 |
/** a search using hash index was not performed. */ |
| 650 |
BTR_CUR_HASH_NOT_ATTEMPTED, |
| 651 |
/** failure using hash, success using binary search. The record pointing by |
| 652 |
the cursor may need to be updated in AHI. */ |
| 653 |
BTR_CUR_HASH_FAIL, |
| 654 |
/** success using the binary search */ |
| 655 |
BTR_CUR_BINARY, |
| 656 |
/** performed the intended insert to the insert buffer */ |
| 657 |
BTR_CUR_INSERT_TO_IBUF, |
| 658 |
/** performed the intended delete mark in the insert/delete buffer */ |
| 659 |
BTR_CUR_DEL_MARK_IBUF, |
| 660 |
/** performed the intended delete in the insert/delete buffer */ |
| 661 |
BTR_CUR_DELETE_IBUF, |
| 662 |
/** row_purge_poss_sec() failed */ |
| 663 |
BTR_CUR_DELETE_REF |
| 664 |
}; |
| 665 |
|
| 666 |
/** The tree cursor: the definition appears here only for the compiler |
| 667 |
to know struct size! */ |
| 668 |
struct btr_cur_t { |
| 669 |
/** Index on which the cursor is positioned. */ |
| 670 |
dict_index_t *index{nullptr}; |
| 671 |
/** Page cursor. */ |
| 672 |
page_cur_t page_cur; |
| 673 |
/** Purge node, for BTR_DELETE */ |
| 674 |
purge_node_t *purge_node{nullptr}; |
| 675 |
/** this field is used to store a pointer to the left neighbor page, in the |
| 676 |
cases BTR_SEARCH_PREV and BTR_MODIFY_PREV */ |
| 677 |
buf_block_t *left_block{nullptr}; |
| 678 |
|
| 679 |
/** this field is only used when btr_cur_search_to_nth_level is called for an |
| 680 |
index entry insertion: the calling query thread is passed here to be used in |
| 681 |
the insert buffer */ |
| 682 |
que_thr_t *thr{nullptr}; |
| 683 |
|
| 684 |
/** The following fields are used in |
| 685 |
btr_cur_search_to_nth_level to pass information: |
| 686 |
@{ */ |
| 687 |
/** Search method used. */ |
| 688 |
btr_cur_method flag{BTR_CUR_UNSET}; |
| 689 |
/** Tree height if the search is done for a pessimistic insert or update |
| 690 |
operation. */ |
| 691 |
ulint tree_height{0}; |
| 692 |
/** If the search mode was PAGE_CUR_LE, the number of matched fields to the |
| 693 |
the first user record to the right of the cursor record after |
| 694 |
btr_cur_search_to_nth_level; for the mode PAGE_CUR_GE, the matched fields to |
| 695 |
the first user record AT THE CURSOR or to the right of it; NOTE that the |
| 696 |
up_match and low_match values may exceed the correct values for comparison to |
| 697 |
the adjacent user record if that record is on a different leaf page! See the |
| 698 |
note in row_ins_duplicate_error_in_clust. */ |
| 699 |
ulint up_match{0}; |
| 700 |
/** Number of matched bytes to the right at the time cursor positioned; only |
| 701 |
used internally in searches: not defined after the search. */ |
| 702 |
ulint up_bytes{0}; |
| 703 |
/** If search mode was PAGE_CUR_LE, the number of matched fields to the first |
| 704 |
user record AT THE CURSOR or to the left of it after |
| 705 |
btr_cur_search_to_nth_level; NOT defined for PAGE_CUR_GE or any other search |
| 706 |
modes; see also the NOTE in up_match! */ |
| 707 |
ulint low_match{0}; |
| 708 |
/** Number of matched bytes to the left at the time cursor positioned; only |
| 709 |
used internally in searches: not defined after the search. */ |
| 710 |
ulint low_bytes{0}; |
| 711 |
/* Structure for AHI-related fields used in a cursor. */ |
| 712 |
struct { |
| 713 |
/** AHI prefix used in a hash search if flag is any of BTR_CUR_HASH, |
| 714 |
BTR_CUR_HASH_FAIL or BTR_CUR_HASH_NOT_ATTEMPTED. The cursor does |
| 715 |
not fill nor use the `left_side` member and comparisons to other instances |
| 716 |
should be done with equals_without_left_side(). Ideally we could have a |
| 717 |
separate class without this field that btr_search_prefix_info_t inherits or |
| 718 |
composes from, but this would make it larger than 64bits, at least on VC++, |
| 719 |
even if we inherit from a third (empty) class making all these types |
| 720 |
non-POD, and thus unable to do lock-free atomic operations. */ |
| 721 |
btr_search_prefix_info_t prefix_info{}; |
| 722 |
/** hash value used in the search if flag is any of BTR_CUR_HASH, |
| 723 |
BTR_CUR_HASH_FAIL or BTR_CUR_HASH_NOT_ATTEMPTED. */ |
| 724 |
uint64_t ahi_hash_value{0}; |
| 725 |
} ahi; |
| 726 |
/** @} */ |
| 727 |
|
| 728 |
/** In estimating the number of rows in range, we store in this array |
| 729 |
information of the path through the tree. */ |
| 730 |
btr_path_t *path_arr{nullptr}; |
| 731 |
|
| 732 |
/** rtree search info. */ |
| 733 |
rtr_info_t *rtr_info{nullptr}; |
| 734 |
|
| 735 |
/** Ownership of the above rtr_info member. */ |
| 736 |
bool m_own_rtr_info = true; |
| 737 |
|
| 738 |
/** If cursor is used in a scan or simple page fetch. */ |
| 739 |
Page_fetch m_fetch_mode{Page_fetch::NORMAL}; |
| 740 |
}; |
| 741 |
|
| 742 |
/** The following function is used to set the deleted bit of a record. |
| 743 |
@param[in,out] rec physical record |
| 744 |
@param[in,out] page_zip compressed page (or NULL) |
| 745 |
@param[in] flag nonzero if delete marked */ |
| 746 |
static inline void btr_rec_set_deleted_flag(rec_t *rec, |
| 747 |
page_zip_des_t *page_zip, |
| 748 |
bool flag); |
| 749 |
|
| 750 |
/** If pessimistic delete fails because of lack of file space, there |
| 751 |
is still a good change of success a little later. Try this many |
| 752 |
times. */ |
| 753 |
constexpr uint32_t BTR_CUR_RETRY_DELETE_N_TIMES = 100; |
| 754 |
/** If pessimistic delete fails because of lack of file space, there |
| 755 |
is still a good change of success a little later. Sleep this many |
| 756 |
milliseconds between retries. */ |
| 757 |
constexpr uint32_t BTR_CUR_RETRY_SLEEP_TIME_MS = 50; |
| 758 |
|
| 759 |
/** Number of searches down the B-tree in btr_cur_search_to_nth_level(). */ |
| 760 |
extern ulint btr_cur_n_non_sea; |
| 761 |
/** Number of successful adaptive hash index lookups in |
| 762 |
btr_cur_search_to_nth_level(). */ |
| 763 |
extern ulint btr_cur_n_sea; |
| 764 |
/** Old value of btr_cur_n_non_sea. Copied by |
| 765 |
srv_refresh_innodb_monitor_stats(). Referenced by |
| 766 |
srv_printf_innodb_monitor(). */ |
| 767 |
extern ulint btr_cur_n_non_sea_old; |
| 768 |
/** Old value of btr_cur_n_sea. Copied by |
| 769 |
srv_refresh_innodb_monitor_stats(). Referenced by |
| 770 |
srv_printf_innodb_monitor(). */ |
| 771 |
extern ulint btr_cur_n_sea_old; |
| 772 |
|
| 773 |
#ifdef UNIV_DEBUG |
| 774 |
/* Flag to limit optimistic insert records */ |
| 775 |
extern uint btr_cur_limit_optimistic_insert_debug; |
| 776 |
#endif /* UNIV_DEBUG */ |
| 777 |
|
| 778 |
/** If default value of INSTANT ADD column is to be materialize in updated row. |
| 779 |
@param[in] index record descriptor |
| 780 |
@param[in] rec record |
| 781 |
@return true if instant add column(s) to be materialized. */ |
| 782 |
bool materialize_instant_default(const dict_index_t *index, const rec_t *rec); |
| 783 |
|
| 784 |
#include "btr0cur.ic" |
| 785 |
|
| 786 |
#endif |