| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1994, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2008, Google Inc. |
| 5 |
Copyright (c) 2012, Facebook Inc. |
| 6 |
|
| 7 |
Portions of this file contain modifications contributed and copyrighted by |
| 8 |
Google, Inc. Those modifications are gratefully acknowledged and are described |
| 9 |
briefly in the InnoDB documentation. The contributions by Google are |
| 10 |
incorporated with their permission, and subject to the conditions contained in |
| 11 |
the file COPYING.Google. |
| 12 |
|
| 13 |
This program is free software; you can redistribute it and/or modify it under |
| 14 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 15 |
Free Software Foundation. |
| 16 |
|
| 17 |
This program is designed to work with certain software (including |
| 18 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 19 |
as designated in a particular file or component or in included license |
| 20 |
documentation. The authors of MySQL hereby grant you an additional |
| 21 |
permission to link the program and your derivative works with the |
| 22 |
separately licensed software that they have either included with |
| 23 |
the program or referenced in the documentation. |
| 24 |
|
| 25 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 26 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 27 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 28 |
for more details. |
| 29 |
|
| 30 |
You should have received a copy of the GNU General Public License along with |
| 31 |
this program; if not, write to the Free Software Foundation, Inc., |
| 32 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 33 |
|
| 34 |
*****************************************************************************/ |
| 35 |
|
| 36 |
/** @file btr/btr0cur.cc |
| 37 |
The index tree cursor |
| 38 |
|
| 39 |
All changes that row operations make to a B-tree or the records |
| 40 |
there must go through this module! Undo log records are written here |
| 41 |
of every modify or insert of a clustered index record. |
| 42 |
|
| 43 |
NOTE!!! |
| 44 |
To make sure we do not run out of disk space during a pessimistic |
| 45 |
insert or update, we have to reserve 2 x the height of the index tree |
| 46 |
many pages in the tablespace before we start the operation, because |
| 47 |
if leaf splitting has been started, it is difficult to undo, except |
| 48 |
by crashing the database and doing a roll-forward. |
| 49 |
|
| 50 |
Created 10/16/1994 Heikki Tuuri |
| 51 |
*******************************************************/ |
| 52 |
|
| 53 |
#include "btr0cur.h" |
| 54 |
|
| 55 |
#include <assert.h> |
| 56 |
|
| 57 |
#include "my_dbug.h" |
| 58 |
|
| 59 |
#ifndef UNIV_HOTBACKUP |
| 60 |
#include <zlib.h> |
| 61 |
#include "btr0btr.h" |
| 62 |
#include "btr0sea.h" |
| 63 |
#include "buf0lru.h" |
| 64 |
#ifdef UNIV_DEBUG |
| 65 |
#include "current_thd.h" |
| 66 |
#include "debug_sync.h" |
| 67 |
#endif /* UNIV_DEBUG */ |
| 68 |
#include "ibuf0ibuf.h" |
| 69 |
#include "lob0lob.h" |
| 70 |
#include "lock0lock.h" |
| 71 |
#include "mtr0log.h" |
| 72 |
#include "row0upd.h" |
| 73 |
#endif /* !UNIV_HOTBACKUP */ |
| 74 |
#include "page0page.h" |
| 75 |
#include "page0zip.h" |
| 76 |
#ifndef UNIV_HOTBACKUP |
| 77 |
#include "que0que.h" |
| 78 |
#endif /* !UNIV_HOTBACKUP */ |
| 79 |
#include "rem0cmp.h" |
| 80 |
#include "rem0rec.h" |
| 81 |
#include "row0log.h" |
| 82 |
#ifndef UNIV_HOTBACKUP |
| 83 |
#include "row0purge.h" |
| 84 |
#include "row0row.h" |
| 85 |
#endif /* !UNIV_HOTBACKUP */ |
| 86 |
#include "row0upd.h" |
| 87 |
#ifndef UNIV_HOTBACKUP |
| 88 |
#include "srv0srv.h" |
| 89 |
#endif /* !UNIV_HOTBACKUP */ |
| 90 |
#include "srv0start.h" |
| 91 |
#ifndef UNIV_HOTBACKUP |
| 92 |
#include "trx0rec.h" |
| 93 |
#include "trx0roll.h" |
| 94 |
#endif /* !UNIV_HOTBACKUP */ |
| 95 |
|
| 96 |
#include <array> |
| 97 |
|
| 98 |
/** Buffered B-tree operation types, introduced as part of delete buffering. */ |
| 99 |
enum btr_op_t { |
| 100 |
BTR_NO_OP = 0, /*!< Not buffered */ |
| 101 |
BTR_INSERT_OP, /*!< Insert, do not ignore UNIQUE */ |
| 102 |
BTR_INSERT_IGNORE_UNIQUE_OP, /*!< Insert, ignoring UNIQUE */ |
| 103 |
BTR_DELETE_OP, /*!< Purge a delete-marked record */ |
| 104 |
BTR_DELMARK_OP /*!< Mark a record for deletion */ |
| 105 |
}; |
| 106 |
|
| 107 |
/** Modification types for the B-tree operation. Note that the order of |
| 108 |
the enum values is important.*/ |
| 109 |
enum btr_intention_t { |
| 110 |
BTR_INTENTION_DELETE, |
| 111 |
BTR_INTENTION_BOTH, |
| 112 |
BTR_INTENTION_INSERT |
| 113 |
}; |
| 114 |
|
| 115 |
/** For the index->lock scalability improvement, only possibility of clear |
| 116 |
performance regression observed was caused by grown huge history list length. |
| 117 |
That is because the exclusive use of index->lock also worked as reserving |
| 118 |
free blocks and read IO bandwidth with priority. To avoid huge glowing history |
| 119 |
list as same level with previous implementation, prioritizes pessimistic tree |
| 120 |
operations by purge as the previous, when it seems to be growing huge. |
| 121 |
|
| 122 |
Experimentally, the history list length starts to affect to performance |
| 123 |
throughput clearly from about 100000. */ |
| 124 |
constexpr uint32_t BTR_CUR_FINE_HISTORY_LENGTH = 100000; |
| 125 |
|
| 126 |
/** Number of searches down the B-tree in btr_cur_search_to_nth_level(). */ |
| 127 |
ulint btr_cur_n_non_sea = 0; |
| 128 |
/** Number of successful adaptive hash index lookups in |
| 129 |
btr_cur_search_to_nth_level(). */ |
| 130 |
ulint btr_cur_n_sea = 0; |
| 131 |
/** Old value of btr_cur_n_non_sea. Copied by |
| 132 |
srv_refresh_innodb_monitor_stats(). Referenced by |
| 133 |
srv_printf_innodb_monitor(). */ |
| 134 |
ulint btr_cur_n_non_sea_old = 0; |
| 135 |
/** Old value of btr_cur_n_sea. Copied by |
| 136 |
srv_refresh_innodb_monitor_stats(). Referenced by |
| 137 |
srv_printf_innodb_monitor(). */ |
| 138 |
ulint btr_cur_n_sea_old = 0; |
| 139 |
|
| 140 |
#ifdef UNIV_DEBUG |
| 141 |
/* Flag to limit optimistic insert records */ |
| 142 |
uint btr_cur_limit_optimistic_insert_debug = 0; |
| 143 |
#endif /* UNIV_DEBUG */ |
| 144 |
|
| 145 |
/** In the optimistic insert, if the insert does not fit, but this much space |
| 146 |
can be released by page reorganize, then it is reorganized */ |
| 147 |
#define BTR_CUR_PAGE_REORGANIZE_LIMIT (UNIV_PAGE_SIZE / 32) |
| 148 |
|
| 149 |
/** Estimated table level stats from sampled value. |
| 150 |
@param value sampled stats |
| 151 |
@param index index being sampled |
| 152 |
@param sample number of sampled rows |
| 153 |
@param ext_size external stored data size |
| 154 |
@param not_empty table not empty |
| 155 |
@return estimated table wide stats from sampled value */ |
| 156 |
constexpr uint64_t BTR_TABLE_STATS_FROM_SAMPLE(uint64_t value, |
| 157 |
dict_index_t *index, |
| 158 |
uint64_t sample, ulint ext_size, |
| 159 |
ulint not_empty) { |
| 160 |
return (value * index->stat_n_leaf_pages + sample - 1 + ext_size + |
| 161 |
not_empty) / |
| 162 |
(sample + ext_size); |
| 163 |
} |
| 164 |
#ifndef UNIV_HOTBACKUP |
| 165 |
/** Adds path information to the cursor for the current page, for which |
| 166 |
the binary search has been performed. |
| 167 |
@param[in, out] cursor Cursor positioned on a page. |
| 168 |
@param[in] height Height of the page in the tree; 0 means leaf. |
| 169 |
@param[in] root_height Root node height in true. */ |
| 170 |
static void btr_cur_add_path_info(btr_cur_t *cursor, ulint height, |
| 171 |
ulint root_height); |
| 172 |
|
| 173 |
/*==================== B-TREE SEARCH =========================*/ |
| 174 |
|
| 175 |
/** Latches the leaf page or pages requested. |
| 176 |
@param[in] block Leaf page where the search converged |
| 177 |
@param[in] page_id Page id of the leaf |
| 178 |
@param[in] page_size Page size |
| 179 |
@param[in] latch_mode BTR_SEARCH_LEAF, ... |
| 180 |
@param[in] cursor Cursor |
| 181 |
@param[in] mtr Mini-transaction |
| 182 |
@return blocks and savepoints which actually latched. */ |
| 183 |
btr_latch_leaves_t btr_cur_latch_leaves(buf_block_t *block, |
| 184 |
const page_id_t &page_id, |
| 185 |
const page_size_t &page_size, |
| 186 |
ulint latch_mode, btr_cur_t *cursor, |
| 187 |
mtr_t *mtr) { |
| 188 |
ulint mode; |
| 189 |
page_no_t left_page_no; |
| 190 |
page_no_t right_page_no; |
| 191 |
buf_block_t *get_block; |
| 192 |
page_t *page = buf_block_get_frame(block); |
| 193 |
bool spatial; |
| 194 |
btr_latch_leaves_t latch_leaves = {{nullptr, nullptr, nullptr}, {0, 0, 0}}; |
| 195 |
|
| 196 |
spatial = dict_index_is_spatial(cursor->index) && cursor->rtr_info; |
| 197 |
ut_ad(buf_page_in_file(&block->page)); |
| 198 |
|
| 199 |
switch (latch_mode) { |
| 200 |
case BTR_SEARCH_LEAF: |
| 201 |
case BTR_MODIFY_LEAF: |
| 202 |
case BTR_SEARCH_TREE: |
| 203 |
if (spatial) { |
| 204 |
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS] = |
| 205 |
mtr_set_savepoint(mtr); |
| 206 |
} |
| 207 |
|
| 208 |
mode = latch_mode == BTR_MODIFY_LEAF ? RW_X_LATCH : RW_S_LATCH; |
| 209 |
latch_leaves.savepoints[1] = mtr_set_savepoint(mtr); |
| 210 |
get_block = btr_block_get(page_id, page_size, mode, UT_LOCATION_HERE, |
| 211 |
cursor->index, mtr); |
| 212 |
latch_leaves.blocks[1] = get_block; |
| 213 |
#ifdef UNIV_BTR_DEBUG |
| 214 |
ut_a(page_is_comp(get_block->frame) == page_is_comp(page)); |
| 215 |
#endif /* UNIV_BTR_DEBUG */ |
| 216 |
if (spatial) { |
| 217 |
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS] = get_block; |
| 218 |
} |
| 219 |
|
| 220 |
return (latch_leaves); |
| 221 |
case BTR_MODIFY_TREE: |
| 222 |
/* It is exclusive for other operations which calls |
| 223 |
btr_page_set_prev() */ |
| 224 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(cursor->index), |
| 225 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) || |
| 226 |
cursor->index->table->is_intrinsic()); |
| 227 |
/* x-latch also siblings from left to right */ |
| 228 |
left_page_no = btr_page_get_prev(page, mtr); |
| 229 |
|
| 230 |
if (left_page_no != FIL_NULL) { |
| 231 |
if (spatial) { |
| 232 |
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS] = |
| 233 |
mtr_set_savepoint(mtr); |
| 234 |
} |
| 235 |
|
| 236 |
latch_leaves.savepoints[0] = mtr_set_savepoint(mtr); |
| 237 |
get_block = |
| 238 |
btr_block_get(page_id_t(page_id.space(), left_page_no), page_size, |
| 239 |
RW_X_LATCH, UT_LOCATION_HERE, cursor->index, mtr); |
| 240 |
latch_leaves.blocks[0] = get_block; |
| 241 |
|
| 242 |
if (spatial) { |
| 243 |
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS] = get_block; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
if (spatial) { |
| 248 |
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS + 1] = |
| 249 |
mtr_set_savepoint(mtr); |
| 250 |
} |
| 251 |
|
| 252 |
latch_leaves.savepoints[1] = mtr_set_savepoint(mtr); |
| 253 |
get_block = btr_block_get(page_id, page_size, RW_X_LATCH, |
| 254 |
UT_LOCATION_HERE, cursor->index, mtr); |
| 255 |
latch_leaves.blocks[1] = get_block; |
| 256 |
|
| 257 |
#ifdef UNIV_BTR_DEBUG |
| 258 |
/* Sanity check only after both the blocks are latched. */ |
| 259 |
if (latch_leaves.blocks[0] != nullptr) { |
| 260 |
ut_a(page_is_comp(latch_leaves.blocks[0]->frame) == page_is_comp(page)); |
| 261 |
ut_a(btr_page_get_next(latch_leaves.blocks[0]->frame, mtr) == |
| 262 |
page_get_page_no(page)); |
| 263 |
} |
| 264 |
ut_a(page_is_comp(get_block->frame) == page_is_comp(page)); |
| 265 |
#endif /* UNIV_BTR_DEBUG */ |
| 266 |
|
| 267 |
if (spatial) { |
| 268 |
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS + 1] = get_block; |
| 269 |
} |
| 270 |
|
| 271 |
right_page_no = btr_page_get_next(page, mtr); |
| 272 |
|
| 273 |
if (right_page_no != FIL_NULL) { |
| 274 |
if (spatial) { |
| 275 |
cursor->rtr_info->tree_savepoints[RTR_MAX_LEVELS + 2] = |
| 276 |
mtr_set_savepoint(mtr); |
| 277 |
} |
| 278 |
latch_leaves.savepoints[2] = mtr_set_savepoint(mtr); |
| 279 |
get_block = |
| 280 |
btr_block_get(page_id_t(page_id.space(), right_page_no), page_size, |
| 281 |
RW_X_LATCH, UT_LOCATION_HERE, cursor->index, mtr); |
| 282 |
latch_leaves.blocks[2] = get_block; |
| 283 |
#ifdef UNIV_BTR_DEBUG |
| 284 |
ut_a(page_is_comp(get_block->frame) == page_is_comp(page)); |
| 285 |
ut_a(btr_page_get_prev(get_block->frame, mtr) == |
| 286 |
page_get_page_no(page)); |
| 287 |
#endif /* UNIV_BTR_DEBUG */ |
| 288 |
if (spatial) { |
| 289 |
cursor->rtr_info->tree_blocks[RTR_MAX_LEVELS + 2] = get_block; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
return (latch_leaves); |
| 294 |
|
| 295 |
case BTR_SEARCH_PREV: |
| 296 |
case BTR_MODIFY_PREV: |
| 297 |
mode = latch_mode == BTR_SEARCH_PREV ? RW_S_LATCH : RW_X_LATCH; |
| 298 |
/* latch also left sibling */ |
| 299 |
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE); |
| 300 |
left_page_no = btr_page_get_prev(page, mtr); |
| 301 |
rw_lock_s_unlock(&block->lock); |
| 302 |
|
| 303 |
if (left_page_no != FIL_NULL) { |
| 304 |
latch_leaves.savepoints[0] = mtr_set_savepoint(mtr); |
| 305 |
get_block = |
| 306 |
btr_block_get(page_id_t(page_id.space(), left_page_no), page_size, |
| 307 |
mode, UT_LOCATION_HERE, cursor->index, mtr); |
| 308 |
latch_leaves.blocks[0] = get_block; |
| 309 |
cursor->left_block = get_block; |
| 310 |
#ifdef UNIV_BTR_DEBUG |
| 311 |
ut_a(page_is_comp(get_block->frame) == page_is_comp(page)); |
| 312 |
ut_a(btr_page_get_next(get_block->frame, mtr) == |
| 313 |
page_get_page_no(page)); |
| 314 |
#endif /* UNIV_BTR_DEBUG */ |
| 315 |
} |
| 316 |
|
| 317 |
latch_leaves.savepoints[1] = mtr_set_savepoint(mtr); |
| 318 |
get_block = btr_block_get(page_id, page_size, mode, UT_LOCATION_HERE, |
| 319 |
cursor->index, mtr); |
| 320 |
latch_leaves.blocks[1] = get_block; |
| 321 |
#ifdef UNIV_BTR_DEBUG |
| 322 |
ut_a(page_is_comp(get_block->frame) == page_is_comp(page)); |
| 323 |
#endif /* UNIV_BTR_DEBUG */ |
| 324 |
return (latch_leaves); |
| 325 |
case BTR_CONT_MODIFY_TREE: |
| 326 |
ut_ad(dict_index_is_spatial(cursor->index)); |
| 327 |
return (latch_leaves); |
| 328 |
} |
| 329 |
|
| 330 |
ut_error; |
| 331 |
} |
| 332 |
|
| 333 |
/** Optimistically latches the leaf page or pages requested. |
| 334 |
@param[in] block Guessed buffer block |
| 335 |
@param[in] modify_clock Modify clock value |
| 336 |
@param[in,out] latch_mode BTR_SEARCH_LEAF, ... |
| 337 |
@param[in,out] cursor Cursor |
| 338 |
@param[in] file File name |
| 339 |
@param[in] line Line where called |
| 340 |
@param[in] mtr Mini-transaction |
| 341 |
@return true if success */ |
| 342 |
bool btr_cur_optimistic_latch_leaves(buf_block_t *block, uint64_t modify_clock, |
| 343 |
ulint *latch_mode, btr_cur_t *cursor, |
| 344 |
const char *file, ulint line, mtr_t *mtr) { |
| 345 |
ulint mode; |
| 346 |
page_no_t left_page_no; |
| 347 |
ut_ad(block->page.buf_fix_count > 0); |
| 348 |
ut_ad(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE); |
| 349 |
|
| 350 |
switch (*latch_mode) { |
| 351 |
case BTR_SEARCH_LEAF: |
| 352 |
case BTR_MODIFY_LEAF: |
| 353 |
return (buf_page_optimistic_get(*latch_mode, block, modify_clock, |
| 354 |
cursor->m_fetch_mode, file, line, mtr)); |
| 355 |
case BTR_SEARCH_PREV: |
| 356 |
case BTR_MODIFY_PREV: |
| 357 |
mode = *latch_mode == BTR_SEARCH_PREV ? RW_S_LATCH : RW_X_LATCH; |
| 358 |
|
| 359 |
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE); |
| 360 |
if (block->modify_clock != modify_clock) { |
| 361 |
rw_lock_s_unlock(&block->lock); |
| 362 |
return false; |
| 363 |
} |
| 364 |
left_page_no = btr_page_get_prev(buf_block_get_frame(block), mtr); |
| 365 |
rw_lock_s_unlock(&block->lock); |
| 366 |
|
| 367 |
if (left_page_no != FIL_NULL) { |
| 368 |
const page_id_t page_id(dict_index_get_space(cursor->index), |
| 369 |
left_page_no); |
| 370 |
|
| 371 |
cursor->left_block = buf_page_get_gen( |
| 372 |
page_id, dict_table_page_size(cursor->index->table), mode, nullptr, |
| 373 |
Page_fetch::POSSIBLY_FREED, UT_LOCATION_HERE, mtr); |
| 374 |
} else { |
| 375 |
cursor->left_block = nullptr; |
| 376 |
} |
| 377 |
if (buf_page_optimistic_get(mode, block, modify_clock, |
| 378 |
cursor->m_fetch_mode, file, line, mtr)) { |
| 379 |
if (btr_page_get_prev(buf_block_get_frame(block), mtr) == |
| 380 |
left_page_no) { |
| 381 |
/* We've entered this function with the block already buffer-fixed, |
| 382 |
and buf_page_optimistic_get() buffer-fixes it again. The caller should |
| 383 |
unfix the block once (to undo their buffer-fixing). */ |
| 384 |
ut_ad(2 <= block->page.buf_fix_count); |
| 385 |
*latch_mode = mode; |
| 386 |
return true; |
| 387 |
} else { |
| 388 |
/* release the block, which will also decrement the buf_fix_count once |
| 389 |
undoing the increment in successful buf_page_optimistic_get() */ |
| 390 |
btr_leaf_page_release(block, mode, mtr); |
| 391 |
} |
| 392 |
} |
| 393 |
/* If we are still here then buf_page_optimistic_get() did not buffer-fix |
| 394 |
the page, but it should still be buffer-fixed as it was before the call.*/ |
| 395 |
ut_ad(0 < block->page.buf_fix_count); |
| 396 |
/* release the left block */ |
| 397 |
if (cursor->left_block != nullptr) { |
| 398 |
btr_leaf_page_release(cursor->left_block, mode, mtr); |
| 399 |
} |
| 400 |
|
| 401 |
return false; |
| 402 |
|
| 403 |
default: |
| 404 |
ut_error; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
Gets intention in btr_intention_t from latch_mode, and cleares the intention |
| 410 |
at the latch_mode. |
| 411 |
@param latch_mode in/out: pointer to latch_mode |
| 412 |
@return intention for latching tree */ |
| 413 |
static btr_intention_t btr_cur_get_and_clear_intention(ulint *latch_mode) { |
| 414 |
btr_intention_t intention; |
| 415 |
|
| 416 |
switch (*latch_mode & (BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE)) { |
| 417 |
case BTR_LATCH_FOR_INSERT: |
| 418 |
intention = BTR_INTENTION_INSERT; |
| 419 |
break; |
| 420 |
case BTR_LATCH_FOR_DELETE: |
| 421 |
intention = BTR_INTENTION_DELETE; |
| 422 |
break; |
| 423 |
default: |
| 424 |
/* both or unknown */ |
| 425 |
intention = BTR_INTENTION_BOTH; |
| 426 |
} |
| 427 |
*latch_mode &= ~(BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE); |
| 428 |
|
| 429 |
return (intention); |
| 430 |
} |
| 431 |
|
| 432 |
/** |
| 433 |
Gets the desired latch type for the root leaf (root page is root leaf) |
| 434 |
at the latch mode. |
| 435 |
@param latch_mode in: BTR_SEARCH_LEAF, ... |
| 436 |
@return latch type */ |
| 437 |
static rw_lock_type_t btr_cur_latch_for_root_leaf(ulint latch_mode) { |
| 438 |
switch (latch_mode) { |
| 439 |
case BTR_SEARCH_LEAF: |
| 440 |
case BTR_SEARCH_TREE: |
| 441 |
case BTR_SEARCH_PREV: |
| 442 |
return (RW_S_LATCH); |
| 443 |
case BTR_MODIFY_LEAF: |
| 444 |
case BTR_MODIFY_TREE: |
| 445 |
case BTR_MODIFY_PREV: |
| 446 |
return (RW_X_LATCH); |
| 447 |
case BTR_CONT_MODIFY_TREE: |
| 448 |
case BTR_CONT_SEARCH_TREE: |
| 449 |
/* A root page should be latched already, |
| 450 |
and don't need to be latched here. |
| 451 |
fall through (RW_NO_LATCH) */ |
| 452 |
case BTR_NO_LATCHES: |
| 453 |
return (RW_NO_LATCH); |
| 454 |
} |
| 455 |
|
| 456 |
ut_error; |
| 457 |
} |
| 458 |
|
| 459 |
/** Detects whether the modifying record might need a modifying tree structure. |
| 460 |
@param[in] index index |
| 461 |
@param[in] page page |
| 462 |
@param[in] lock_intention lock intention for the tree operation |
| 463 |
@param[in] rec record (current node_ptr) |
| 464 |
@param[in] rec_size size of the record or max size of node_ptr |
| 465 |
@param[in] page_size page size |
| 466 |
@param[in] mtr mtr |
| 467 |
@return true if tree modification is needed */ |
| 468 |
static bool btr_cur_will_modify_tree(dict_index_t *index, const page_t *page, |
| 469 |
btr_intention_t lock_intention, |
| 470 |
const rec_t *rec, ulint rec_size, |
| 471 |
const page_size_t &page_size, mtr_t *mtr) { |
| 472 |
ut_ad(!page_is_leaf(page)); |
| 473 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 474 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) || |
| 475 |
index->table->is_intrinsic()); |
| 476 |
|
| 477 |
/* Pessimistic delete of the first record causes delete & insert |
| 478 |
of node_ptr at upper level. And a subsequent page shrink is |
| 479 |
possible. It causes delete of node_ptr at the upper level. |
| 480 |
So we should pay attention also to 2nd record not only |
| 481 |
first record and last record. Because if the "delete & insert" are |
| 482 |
done for the different page, the 2nd record become |
| 483 |
first record and following compress might delete the record and causes |
| 484 |
the uppper level node_ptr modification. */ |
| 485 |
|
| 486 |
if (lock_intention <= BTR_INTENTION_BOTH) { |
| 487 |
ulint margin; |
| 488 |
|
| 489 |
if (lock_intention == BTR_INTENTION_BOTH) { |
| 490 |
ulint level = btr_page_get_level(page); |
| 491 |
|
| 492 |
/* This value is the worst expectation for the node_ptr records to be |
| 493 |
deleted from this page. It is used to expect whether the cursor position |
| 494 |
can be the left_most record in this page or not. */ |
| 495 |
ulint max_nodes_deleted = 0; |
| 496 |
|
| 497 |
/* By modifying tree operations from the under of this level, |
| 498 |
logically (2 ^ (level - 1)) opportunities to deleting records in maximum |
| 499 |
even unreally rare case. */ |
| 500 |
if (level > 7) { |
| 501 |
/* TODO: adjust this practical limit, if needed. */ |
| 502 |
max_nodes_deleted = 64; |
| 503 |
} else if (level > 0) { |
| 504 |
max_nodes_deleted = (ulint)1 << (level - 1); |
| 505 |
} |
| 506 |
|
| 507 |
/* check delete will cause. (BTR_INTENTION_BOTH |
| 508 |
or BTR_INTENTION_DELETE) */ |
| 509 |
if (page_get_n_recs(page) <= max_nodes_deleted * 2 || |
| 510 |
page_rec_is_first(rec, page)) { |
| 511 |
/* The cursor record can be the left_most record in this page. */ |
| 512 |
return (true); |
| 513 |
} |
| 514 |
|
| 515 |
if (fil_page_get_prev(page) != FIL_NULL && |
| 516 |
page_rec_distance_is_at_most(page_get_infimum_rec(page), rec, |
| 517 |
max_nodes_deleted)) { |
| 518 |
return (true); |
| 519 |
} |
| 520 |
|
| 521 |
if (fil_page_get_next(page) != FIL_NULL && |
| 522 |
page_rec_distance_is_at_most(rec, page_get_supremum_rec(page), |
| 523 |
max_nodes_deleted)) { |
| 524 |
return (true); |
| 525 |
} |
| 526 |
|
| 527 |
/* Delete at leftmost record in a page causes delete |
| 528 |
& insert at its parent page. After that, the delete |
| 529 |
might cause btr_compress() and delete record at its |
| 530 |
parent page. Thus we should consider max deletes. */ |
| 531 |
|
| 532 |
margin = rec_size * max_nodes_deleted; |
| 533 |
} else { |
| 534 |
ut_ad(lock_intention == BTR_INTENTION_DELETE); |
| 535 |
|
| 536 |
margin = rec_size; |
| 537 |
} |
| 538 |
/* Safe because we already have SX latch of the index tree */ |
| 539 |
if (page_get_data_size(page) < |
| 540 |
margin + BTR_CUR_PAGE_COMPRESS_LIMIT(index) || |
| 541 |
(fil_page_get_next(page) == FIL_NULL && |
| 542 |
fil_page_get_prev(page) == FIL_NULL)) { |
| 543 |
return (true); |
| 544 |
} |
| 545 |
} |
| 546 |
|
| 547 |
if (lock_intention >= BTR_INTENTION_BOTH) { |
| 548 |
/* check insert will cause. BTR_INTENTION_BOTH |
| 549 |
or BTR_INTENTION_INSERT*/ |
| 550 |
|
| 551 |
/* Once we invoke the btr_cur_limit_optimistic_insert_debug, |
| 552 |
we should check it here in advance, since the max allowable |
| 553 |
records in a page is limited. */ |
| 554 |
LIMIT_OPTIMISTIC_INSERT_DEBUG(page_get_n_recs(page), return (true)); |
| 555 |
|
| 556 |
/* needs 2 records' space for the case the single split and |
| 557 |
insert cannot fit. |
| 558 |
page_get_max_insert_size_after_reorganize() includes space |
| 559 |
for page directory already */ |
| 560 |
ulint max_size = page_get_max_insert_size_after_reorganize(page, 2); |
| 561 |
|
| 562 |
if (max_size < BTR_CUR_PAGE_REORGANIZE_LIMIT + rec_size || |
| 563 |
max_size < rec_size * 2) { |
| 564 |
return (true); |
| 565 |
} |
| 566 |
/* TODO: optimize this condition for compressed page. |
| 567 |
this is based on the worst compress rate. |
| 568 |
currently looking only uncompressed page, but we can look |
| 569 |
also compressed page page_zip_available() if already in the |
| 570 |
buffer pool */ |
| 571 |
/* needs 2 records' space also for worst compress rate. */ |
| 572 |
if (page_size.is_compressed() && |
| 573 |
page_zip_empty_size(index->n_fields, page_size.physical()) < |
| 574 |
rec_size * 2 + page_get_data_size(page) + |
| 575 |
page_dir_calc_reserved_space(page_get_n_recs(page) + 2) + 1) { |
| 576 |
return (true); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
return (false); |
| 581 |
} |
| 582 |
|
| 583 |
/** Detects whether the modifying record might need a opposite modification |
| 584 |
to the intention. |
| 585 |
@param[in] page page |
| 586 |
@param[in] lock_intention lock intention for the tree operation |
| 587 |
@param[in] rec record (current node_ptr) |
| 588 |
@return true if tree modification is needed */ |
| 589 |
static bool btr_cur_need_opposite_intention(const page_t *page, |
| 590 |
btr_intention_t lock_intention, |
| 591 |
const rec_t *rec) { |
| 592 |
switch (lock_intention) { |
| 593 |
case BTR_INTENTION_DELETE: |
| 594 |
return ((mach_read_from_4(page + FIL_PAGE_PREV) != FIL_NULL && |
| 595 |
page_rec_is_first(rec, page)) || |
| 596 |
(mach_read_from_4(page + FIL_PAGE_NEXT) != FIL_NULL && |
| 597 |
page_rec_is_last(rec, page))); |
| 598 |
case BTR_INTENTION_INSERT: |
| 599 |
return (mach_read_from_4(page + FIL_PAGE_NEXT) != FIL_NULL && |
| 600 |
page_rec_is_last(rec, page)); |
| 601 |
case BTR_INTENTION_BOTH: |
| 602 |
return (false); |
| 603 |
} |
| 604 |
|
| 605 |
ut_error; |
| 606 |
} |
| 607 |
|
| 608 |
/** Searches an index tree and positions a tree cursor on a given level. |
| 609 |
NOTE: n_fields_cmp in tuple must be set so that it cannot be compared |
| 610 |
to node pointer page number fields on the upper levels of the tree! |
| 611 |
Note that if mode is PAGE_CUR_LE, which is used in inserts, then |
| 612 |
cursor->up_match and cursor->low_match both will have sensible values. |
| 613 |
If mode is PAGE_CUR_GE, then up_match will a have a sensible value. |
| 614 |
|
| 615 |
If mode is PAGE_CUR_LE , cursor is left at the place where an insert of the |
| 616 |
search tuple should be performed in the B-tree. InnoDB does an insert |
| 617 |
immediately after the cursor. Thus, the cursor may end up on a user record, |
| 618 |
or on a page infimum record. */ |
| 619 |
void btr_cur_search_to_nth_level( |
| 620 |
dict_index_t *index, /*!< in: index */ |
| 621 |
ulint level, /*!< in: the tree level of search */ |
| 622 |
const dtuple_t *tuple, /*!< in: data tuple; NOTE: n_fields_cmp in |
| 623 |
tuple must be set so that it cannot get |
| 624 |
compared to the node ptr page number field! */ |
| 625 |
page_cur_mode_t mode, /*!< in: PAGE_CUR_L, ...; |
| 626 |
Inserts should always be made using |
| 627 |
PAGE_CUR_LE to search the position! */ |
| 628 |
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ..., ORed with |
| 629 |
at most one of BTR_INSERT, BTR_DELETE_MARK, |
| 630 |
BTR_DELETE, or BTR_ESTIMATE; |
| 631 |
cursor->left_block is used to store a pointer |
| 632 |
to the left neighbor page, in the cases |
| 633 |
BTR_SEARCH_PREV and BTR_MODIFY_PREV; |
| 634 |
NOTE that if has_search_latch |
| 635 |
is != 0, we maybe do not have a latch set |
| 636 |
on the cursor page, we assume |
| 637 |
the caller uses his search latch |
| 638 |
to protect the record! */ |
| 639 |
btr_cur_t *cursor, /*!< in/out: tree cursor; the cursor page is |
| 640 |
s- or x-latched, but see also above! */ |
| 641 |
ulint has_search_latch, |
| 642 |
/*!< in: info on the latch mode the |
| 643 |
caller currently has on search system: |
| 644 |
RW_S_LATCH, or 0 */ |
| 645 |
const char *file, /*!< in: file name */ |
| 646 |
ulint line, /*!< in: line where called */ |
| 647 |
mtr_t *mtr) /*!< in: mtr */ |
| 648 |
{ |
| 649 |
page_t *page = nullptr; /* remove warning */ |
| 650 |
buf_block_t *block; |
| 651 |
ulint height; |
| 652 |
ulint up_match; |
| 653 |
ulint up_bytes; |
| 654 |
ulint low_match; |
| 655 |
ulint low_bytes; |
| 656 |
ulint savepoint; |
| 657 |
ulint rw_latch; |
| 658 |
page_cur_mode_t page_mode; |
| 659 |
page_cur_mode_t search_mode = PAGE_CUR_UNSUPP; |
| 660 |
Page_fetch fetch; |
| 661 |
ulint node_ptr_max_size = UNIV_PAGE_SIZE / 2; |
| 662 |
page_cur_t *page_cursor; |
| 663 |
btr_op_t btr_op; |
| 664 |
ulint root_height = 0; /* remove warning */ |
| 665 |
|
| 666 |
ulint upper_rw_latch, root_leaf_rw_latch; |
| 667 |
btr_intention_t lock_intention; |
| 668 |
bool modify_external; |
| 669 |
buf_block_t *tree_blocks[BTR_MAX_LEVELS]; |
| 670 |
ulint tree_savepoints[BTR_MAX_LEVELS]; |
| 671 |
ulint n_blocks = 0; |
| 672 |
ulint n_releases = 0; |
| 673 |
bool detected_same_key_root = false; |
| 674 |
|
| 675 |
bool retrying_for_search_prev = false; |
| 676 |
ulint leftmost_from_level = 0; |
| 677 |
buf_block_t **prev_tree_blocks = nullptr; |
| 678 |
ulint *prev_tree_savepoints = nullptr; |
| 679 |
ulint prev_n_blocks = 0; |
| 680 |
ulint prev_n_releases = 0; |
| 681 |
bool need_path = true; |
| 682 |
bool rtree_parent_modified = false; |
| 683 |
bool mbr_adj = false; |
| 684 |
bool found = false; |
| 685 |
|
| 686 |
DBUG_TRACE; |
| 687 |
|
| 688 |
mem_heap_t *heap = nullptr; |
| 689 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 690 |
ulint *offsets = offsets_; |
| 691 |
ulint offsets2_[REC_OFFS_NORMAL_SIZE]; |
| 692 |
ulint *offsets2 = offsets2_; |
| 693 |
rec_offs_init(offsets_); |
| 694 |
rec_offs_init(offsets2_); |
| 695 |
/* Currently, PAGE_CUR_LE is the only search mode used for searches |
| 696 |
ending to upper levels */ |
| 697 |
|
| 698 |
ut_ad(level == 0 || mode == PAGE_CUR_LE || RTREE_SEARCH_MODE(mode)); |
| 699 |
ut_ad(dict_index_check_search_tuple(index, tuple)); |
| 700 |
ut_ad(!dict_index_is_ibuf(index) || ibuf_inside(mtr)); |
| 701 |
|
| 702 |
UNIV_MEM_INVALID(&cursor->up_match, sizeof cursor->up_match); |
| 703 |
UNIV_MEM_INVALID(&cursor->up_bytes, sizeof cursor->up_bytes); |
| 704 |
UNIV_MEM_INVALID(&cursor->low_match, sizeof cursor->low_match); |
| 705 |
UNIV_MEM_INVALID(&cursor->low_bytes, sizeof cursor->low_bytes); |
| 706 |
#ifdef UNIV_DEBUG |
| 707 |
cursor->up_match = ULINT_UNDEFINED; |
| 708 |
cursor->low_match = ULINT_UNDEFINED; |
| 709 |
#endif /* UNIV_DEBUG */ |
| 710 |
|
| 711 |
bool s_latch_by_caller = latch_mode & BTR_ALREADY_S_LATCHED; |
| 712 |
latch_mode &= ~BTR_ALREADY_S_LATCHED; |
| 713 |
|
| 714 |
ut_ad(!s_latch_by_caller || srv_read_only_mode || |
| 715 |
mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 716 |
MTR_MEMO_S_LOCK | MTR_MEMO_SX_LOCK)); |
| 717 |
|
| 718 |
/* These flags are mutually exclusive, they are lumped together |
| 719 |
with the latch mode for historical reasons. It's possible for |
| 720 |
none of the flags to be set. */ |
| 721 |
switch (UNIV_EXPECT(latch_mode & (BTR_INSERT | BTR_DELETE | BTR_DELETE_MARK), |
| 722 |
0)) { |
| 723 |
case 0: |
| 724 |
btr_op = BTR_NO_OP; |
| 725 |
break; |
| 726 |
case BTR_INSERT: |
| 727 |
btr_op = (latch_mode & BTR_IGNORE_SEC_UNIQUE) |
| 728 |
? BTR_INSERT_IGNORE_UNIQUE_OP |
| 729 |
: BTR_INSERT_OP; |
| 730 |
break; |
| 731 |
case BTR_DELETE: |
| 732 |
btr_op = BTR_DELETE_OP; |
| 733 |
ut_a(cursor->purge_node); |
| 734 |
break; |
| 735 |
case BTR_DELETE_MARK: |
| 736 |
btr_op = BTR_DELMARK_OP; |
| 737 |
break; |
| 738 |
default: |
| 739 |
/* only one of BTR_INSERT, BTR_DELETE, BTR_DELETE_MARK |
| 740 |
should be specified at a time */ |
| 741 |
ut_error; |
| 742 |
} |
| 743 |
|
| 744 |
/* Operations on the insert buffer tree cannot be buffered. */ |
| 745 |
ut_ad(btr_op == BTR_NO_OP || !dict_index_is_ibuf(index)); |
| 746 |
/* Operations on the clustered index cannot be buffered. */ |
| 747 |
ut_ad(btr_op == BTR_NO_OP || !index->is_clustered()); |
| 748 |
/* Operations on the temporary table(indexes) cannot be buffered. */ |
| 749 |
ut_ad(btr_op == BTR_NO_OP || !index->table->is_temporary()); |
| 750 |
/* Operation on the spatial index cannot be buffered. */ |
| 751 |
ut_ad(btr_op == BTR_NO_OP || !dict_index_is_spatial(index)); |
| 752 |
|
| 753 |
auto estimate = latch_mode & BTR_ESTIMATE; |
| 754 |
|
| 755 |
lock_intention = btr_cur_get_and_clear_intention(&latch_mode); |
| 756 |
|
| 757 |
modify_external = latch_mode & BTR_MODIFY_EXTERNAL; |
| 758 |
|
| 759 |
/* Turn the flags unrelated to the latch mode off. */ |
| 760 |
latch_mode = BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode); |
| 761 |
|
| 762 |
ut_ad(!modify_external || latch_mode == BTR_MODIFY_LEAF); |
| 763 |
|
| 764 |
ut_ad(!s_latch_by_caller || latch_mode == BTR_SEARCH_LEAF || |
| 765 |
latch_mode == BTR_SEARCH_TREE || latch_mode == BTR_MODIFY_LEAF); |
| 766 |
|
| 767 |
cursor->flag = BTR_CUR_BINARY; |
| 768 |
cursor->index = index; |
| 769 |
|
| 770 |
#ifdef UNIV_SEARCH_PERF_STAT |
| 771 |
info->n_searches++; |
| 772 |
#endif |
| 773 |
/* Use of AHI is disabled for intrinsic table as these tables re-use |
| 774 |
the index-id and AHI validation is based on index-id. */ |
| 775 |
if (rw_lock_get_writer(btr_get_search_latch(index)) == RW_LOCK_NOT_LOCKED && |
| 776 |
latch_mode <= BTR_MODIFY_LEAF && index->search_info->last_hash_succ && |
| 777 |
!index->disable_ahi && !estimate |
| 778 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 779 |
&& mode != PAGE_CUR_LE_OR_EXTENDS |
| 780 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 781 |
&& !dict_index_is_spatial(index) |
| 782 |
/* If !has_search_latch, we do a dirty read of |
| 783 |
btr_search_enabled below, and btr_search_guess_on_hash() |
| 784 |
will have to check it again. */ |
| 785 |
&& UNIV_LIKELY(btr_search_enabled) && !modify_external && |
| 786 |
btr_search_guess_on_hash(tuple, mode, latch_mode, cursor, |
| 787 |
has_search_latch, mtr)) { |
| 788 |
|
| 789 |
/* Search using the hash index succeeded */ |
| 790 |
|
| 791 |
ut_ad(cursor->up_match != ULINT_UNDEFINED || mode != PAGE_CUR_GE); |
| 792 |
ut_ad(cursor->up_match != ULINT_UNDEFINED || mode != PAGE_CUR_LE); |
| 793 |
ut_ad(cursor->low_match != ULINT_UNDEFINED || mode != PAGE_CUR_LE); |
| 794 |
btr_cur_n_sea++; |
| 795 |
|
| 796 |
return; |
| 797 |
} |
| 798 |
btr_cur_n_non_sea++; |
| 799 |
DBUG_EXECUTE_IF("non_ahi_search", |
| 800 |
assert(!strcmp(index->table->name.m_name, "test/t1"));); |
| 801 |
|
| 802 |
/* If the hash search did not succeed, do binary search down the |
| 803 |
tree */ |
| 804 |
|
| 805 |
if (has_search_latch) { |
| 806 |
/* Release possible search latch to obey latching order */ |
| 807 |
rw_lock_s_unlock(btr_get_search_latch(index)); |
| 808 |
} |
| 809 |
|
| 810 |
/* Store the position of the tree latch we push to mtr so that we |
| 811 |
know how to release it when we have latched leaf node(s) */ |
| 812 |
|
| 813 |
savepoint = mtr_set_savepoint(mtr); |
| 814 |
|
| 815 |
switch (latch_mode) { |
| 816 |
case BTR_MODIFY_TREE: |
| 817 |
/* Most of delete-intended operations are purging. |
| 818 |
Free blocks and read IO bandwidth should be prior |
| 819 |
for them, when the history list is glowing huge. */ |
| 820 |
if (lock_intention == BTR_INTENTION_DELETE && |
| 821 |
trx_sys->rseg_history_len.load() > BTR_CUR_FINE_HISTORY_LENGTH && |
| 822 |
buf_get_n_pending_read_ios()) { |
| 823 |
mtr_x_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 824 |
} else if (dict_index_is_spatial(index) && |
| 825 |
lock_intention <= BTR_INTENTION_BOTH) { |
| 826 |
/* X lock the if there is possibility of |
| 827 |
pessimistic delete on spatial index. As we could |
| 828 |
lock upward for the tree */ |
| 829 |
|
| 830 |
mtr_x_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 831 |
} else { |
| 832 |
mtr_sx_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 833 |
} |
| 834 |
upper_rw_latch = RW_X_LATCH; |
| 835 |
break; |
| 836 |
case BTR_CONT_MODIFY_TREE: |
| 837 |
case BTR_CONT_SEARCH_TREE: |
| 838 |
/* Do nothing */ |
| 839 |
ut_ad(srv_read_only_mode || |
| 840 |
mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 841 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK)); |
| 842 |
if (dict_index_is_spatial(index) && latch_mode == BTR_CONT_MODIFY_TREE) { |
| 843 |
/* If we are about to locating parent page for split |
| 844 |
and/or merge operation for R-Tree index, X latch |
| 845 |
the parent */ |
| 846 |
upper_rw_latch = RW_X_LATCH; |
| 847 |
} else { |
| 848 |
upper_rw_latch = RW_NO_LATCH; |
| 849 |
} |
| 850 |
break; |
| 851 |
default: |
| 852 |
if (!srv_read_only_mode) { |
| 853 |
if (s_latch_by_caller) { |
| 854 |
/* The BTR_ALREADY_S_LATCHED indicates that the index->lock has been |
| 855 |
taken either in RW_S_LATCH or RW_SX_LATCH mode. For parallel reads |
| 856 |
another thread can own the dict index lock. */ |
| 857 |
ut_ad(rw_lock_own_flagged(dict_index_get_lock(index), |
| 858 |
RW_LOCK_FLAG_S | RW_LOCK_FLAG_SX)); |
| 859 |
|
| 860 |
} else if (!modify_external) { |
| 861 |
/* BTR_SEARCH_TREE is intended to be used with |
| 862 |
BTR_ALREADY_S_LATCHED */ |
| 863 |
ut_ad(latch_mode != BTR_SEARCH_TREE); |
| 864 |
|
| 865 |
mtr_s_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 866 |
} else { |
| 867 |
/* BTR_MODIFY_EXTERNAL needs to be excluded */ |
| 868 |
mtr_sx_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 869 |
} |
| 870 |
upper_rw_latch = RW_S_LATCH; |
| 871 |
} else { |
| 872 |
upper_rw_latch = RW_NO_LATCH; |
| 873 |
} |
| 874 |
} |
| 875 |
root_leaf_rw_latch = btr_cur_latch_for_root_leaf(latch_mode); |
| 876 |
|
| 877 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 878 |
|
| 879 |
const space_id_t space = dict_index_get_space(index); |
| 880 |
const page_size_t page_size(dict_table_page_size(index->table)); |
| 881 |
|
| 882 |
/* Start with the root page. */ |
| 883 |
page_id_t page_id(space, dict_index_get_page(index)); |
| 884 |
|
| 885 |
if (root_leaf_rw_latch == RW_X_LATCH) { |
| 886 |
node_ptr_max_size = dict_index_node_ptr_max_size(index); |
| 887 |
} |
| 888 |
|
| 889 |
up_match = 0; |
| 890 |
up_bytes = 0; |
| 891 |
low_match = 0; |
| 892 |
low_bytes = 0; |
| 893 |
|
| 894 |
height = ULINT_UNDEFINED; |
| 895 |
|
| 896 |
/* We use these modified search modes on non-leaf levels of the |
| 897 |
B-tree. These let us end up in the right B-tree leaf. In that leaf |
| 898 |
we use the original search mode. */ |
| 899 |
|
| 900 |
switch (mode) { |
| 901 |
case PAGE_CUR_GE: |
| 902 |
page_mode = PAGE_CUR_L; |
| 903 |
break; |
| 904 |
case PAGE_CUR_G: |
| 905 |
page_mode = PAGE_CUR_LE; |
| 906 |
break; |
| 907 |
default: |
| 908 |
#ifdef PAGE_CUR_LE_OR_EXTENDS |
| 909 |
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE || |
| 910 |
RTREE_SEARCH_MODE(mode) || mode == PAGE_CUR_LE_OR_EXTENDS); |
| 911 |
#else /* PAGE_CUR_LE_OR_EXTENDS */ |
| 912 |
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE || |
| 913 |
RTREE_SEARCH_MODE(mode)); |
| 914 |
#endif /* PAGE_CUR_LE_OR_EXTENDS */ |
| 915 |
page_mode = mode; |
| 916 |
break; |
| 917 |
} |
| 918 |
|
| 919 |
/* Loop and search until we arrive at the desired level */ |
| 920 |
btr_latch_leaves_t latch_leaves = {{nullptr, nullptr, nullptr}, {0, 0, 0}}; |
| 921 |
|
| 922 |
search_loop: |
| 923 |
fetch = cursor->m_fetch_mode; |
| 924 |
rw_latch = RW_NO_LATCH; |
| 925 |
rtree_parent_modified = false; |
| 926 |
|
| 927 |
if (height != 0) { |
| 928 |
/* We are about to fetch the root or a non-leaf page. */ |
| 929 |
if ((latch_mode != BTR_MODIFY_TREE || height == level) && |
| 930 |
!retrying_for_search_prev) { |
| 931 |
/* If doesn't have SX or X latch of index, |
| 932 |
each pages should be latched before reading. */ |
| 933 |
if (modify_external && height == ULINT_UNDEFINED && |
| 934 |
upper_rw_latch == RW_S_LATCH) { |
| 935 |
/* needs sx-latch of root page |
| 936 |
for fseg operation */ |
| 937 |
rw_latch = RW_SX_LATCH; |
| 938 |
} else { |
| 939 |
rw_latch = upper_rw_latch; |
| 940 |
} |
| 941 |
} |
| 942 |
} else if (latch_mode <= BTR_MODIFY_LEAF) { |
| 943 |
rw_latch = latch_mode; |
| 944 |
|
| 945 |
if (btr_op != BTR_NO_OP && |
| 946 |
ibuf_should_try(index, btr_op != BTR_INSERT_OP)) { |
| 947 |
/* Try to buffer the operation if the leaf |
| 948 |
page is not in the buffer pool. */ |
| 949 |
|
| 950 |
fetch = btr_op == BTR_DELETE_OP ? Page_fetch::IF_IN_POOL_OR_WATCH |
| 951 |
: Page_fetch::IF_IN_POOL; |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
retry_page_get: |
| 956 |
ut_ad(n_blocks < BTR_MAX_LEVELS); |
| 957 |
tree_savepoints[n_blocks] = mtr_set_savepoint(mtr); |
| 958 |
block = buf_page_get_gen( |
| 959 |
page_id, page_size, rw_latch, |
| 960 |
(height == ULINT_UNDEFINED ? index->search_info->root_guess : nullptr), |
| 961 |
fetch, {file, line}, mtr); |
| 962 |
|
| 963 |
tree_blocks[n_blocks] = block; |
| 964 |
|
| 965 |
if (block == nullptr) { |
| 966 |
/* This must be a search to perform an insert/delete |
| 967 |
mark/ delete; try using the insert/delete buffer */ |
| 968 |
|
| 969 |
ut_ad(height == 0); |
| 970 |
ut_ad(cursor->thr); |
| 971 |
|
| 972 |
switch (btr_op) { |
| 973 |
case BTR_INSERT_OP: |
| 974 |
case BTR_INSERT_IGNORE_UNIQUE_OP: |
| 975 |
ut_ad(fetch == Page_fetch::IF_IN_POOL); |
| 976 |
ut_ad(!dict_index_is_spatial(index)); |
| 977 |
|
| 978 |
if (ibuf_insert(IBUF_OP_INSERT, tuple, index, page_id, page_size, |
| 979 |
cursor->thr)) { |
| 980 |
cursor->flag = BTR_CUR_INSERT_TO_IBUF; |
| 981 |
|
| 982 |
goto func_exit; |
| 983 |
} |
| 984 |
break; |
| 985 |
|
| 986 |
case BTR_DELMARK_OP: |
| 987 |
ut_ad(fetch == Page_fetch::IF_IN_POOL); |
| 988 |
ut_ad(!dict_index_is_spatial(index)); |
| 989 |
|
| 990 |
if (ibuf_insert(IBUF_OP_DELETE_MARK, tuple, index, page_id, page_size, |
| 991 |
cursor->thr)) { |
| 992 |
cursor->flag = BTR_CUR_DEL_MARK_IBUF; |
| 993 |
|
| 994 |
goto func_exit; |
| 995 |
} |
| 996 |
|
| 997 |
break; |
| 998 |
|
| 999 |
case BTR_DELETE_OP: |
| 1000 |
ut_ad(fetch == Page_fetch::IF_IN_POOL_OR_WATCH); |
| 1001 |
ut_ad(!dict_index_is_spatial(index)); |
| 1002 |
|
| 1003 |
if (!row_purge_poss_sec(cursor->purge_node, index, tuple)) { |
| 1004 |
/* The record cannot be purged yet. */ |
| 1005 |
cursor->flag = BTR_CUR_DELETE_REF; |
| 1006 |
} else if (ibuf_insert(IBUF_OP_DELETE, tuple, index, page_id, page_size, |
| 1007 |
cursor->thr)) { |
| 1008 |
/* The purge was buffered. */ |
| 1009 |
cursor->flag = BTR_CUR_DELETE_IBUF; |
| 1010 |
} else { |
| 1011 |
/* The purge could not be buffered. */ |
| 1012 |
buf_pool_watch_unset(page_id); |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
|
| 1016 |
buf_pool_watch_unset(page_id); |
| 1017 |
goto func_exit; |
| 1018 |
|
| 1019 |
default: |
| 1020 |
ut_error; |
| 1021 |
} |
| 1022 |
|
| 1023 |
/* Insert to the insert/delete buffer did not succeed, we |
| 1024 |
must read the page from disk. */ |
| 1025 |
|
| 1026 |
fetch = cursor->m_fetch_mode; |
| 1027 |
|
| 1028 |
goto retry_page_get; |
| 1029 |
} |
| 1030 |
|
| 1031 |
if (retrying_for_search_prev && height != 0) { |
| 1032 |
/* also latch left sibling */ |
| 1033 |
page_no_t left_page_no; |
| 1034 |
buf_block_t *get_block; |
| 1035 |
|
| 1036 |
ut_ad(rw_latch == RW_NO_LATCH); |
| 1037 |
|
| 1038 |
rw_latch = upper_rw_latch; |
| 1039 |
|
| 1040 |
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE); |
| 1041 |
left_page_no = btr_page_get_prev(buf_block_get_frame(block), mtr); |
| 1042 |
rw_lock_s_unlock(&block->lock); |
| 1043 |
|
| 1044 |
if (left_page_no != FIL_NULL) { |
| 1045 |
ut_ad(prev_n_blocks < leftmost_from_level); |
| 1046 |
|
| 1047 |
prev_tree_savepoints[prev_n_blocks] = mtr_set_savepoint(mtr); |
| 1048 |
get_block = |
| 1049 |
buf_page_get_gen(page_id_t(page_id.space(), left_page_no), page_size, |
| 1050 |
rw_latch, nullptr, fetch, {file, line}, mtr); |
| 1051 |
prev_tree_blocks[prev_n_blocks] = get_block; |
| 1052 |
prev_n_blocks++; |
| 1053 |
|
| 1054 |
/* BTR_MODIFY_TREE doesn't update prev/next_page_no, |
| 1055 |
without their parent page's lock. So, not needed to |
| 1056 |
retry here, because we have the parent page's lock. */ |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* release RW_NO_LATCH page and lock with RW_S_LATCH */ |
| 1060 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_blocks], |
| 1061 |
tree_blocks[n_blocks]); |
| 1062 |
|
| 1063 |
tree_savepoints[n_blocks] = mtr_set_savepoint(mtr); |
| 1064 |
block = buf_page_get_gen(page_id, page_size, rw_latch, nullptr, fetch, |
| 1065 |
{file, line}, mtr); |
| 1066 |
tree_blocks[n_blocks] = block; |
| 1067 |
} |
| 1068 |
|
| 1069 |
page = buf_block_get_frame(block); |
| 1070 |
|
| 1071 |
if (height == ULINT_UNDEFINED && page_is_leaf(page) && |
| 1072 |
rw_latch != RW_NO_LATCH && rw_latch != root_leaf_rw_latch) { |
| 1073 |
/* We should retry to get the page, because the root page |
| 1074 |
is latched with different level as a leaf page. */ |
| 1075 |
ut_ad(root_leaf_rw_latch != RW_NO_LATCH); |
| 1076 |
ut_ad(rw_latch == RW_S_LATCH || rw_latch == RW_SX_LATCH); |
| 1077 |
ut_ad(rw_latch == RW_S_LATCH || modify_external); |
| 1078 |
|
| 1079 |
ut_ad(n_blocks == 0); |
| 1080 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_blocks], |
| 1081 |
tree_blocks[n_blocks]); |
| 1082 |
|
| 1083 |
upper_rw_latch = root_leaf_rw_latch; |
| 1084 |
goto search_loop; |
| 1085 |
} |
| 1086 |
|
| 1087 |
if (rw_latch != RW_NO_LATCH) { |
| 1088 |
#ifdef UNIV_ZIP_DEBUG |
| 1089 |
const page_zip_des_t *page_zip = buf_block_get_page_zip(block); |
| 1090 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 1091 |
#endif /* UNIV_ZIP_DEBUG */ |
| 1092 |
|
| 1093 |
buf_block_dbg_add_level(block, dict_index_is_ibuf(index) |
| 1094 |
? SYNC_IBUF_TREE_NODE |
| 1095 |
: SYNC_TREE_NODE); |
| 1096 |
} |
| 1097 |
|
| 1098 |
ut_ad(fil_page_index_page_check(page)); |
| 1099 |
ut_ad(index->id == btr_page_get_index_id(page)); |
| 1100 |
|
| 1101 |
if (UNIV_UNLIKELY(height == ULINT_UNDEFINED)) { |
| 1102 |
/* We are in the root node */ |
| 1103 |
|
| 1104 |
height = btr_page_get_level(page); |
| 1105 |
root_height = height; |
| 1106 |
cursor->tree_height = root_height + 1; |
| 1107 |
|
| 1108 |
if (dict_index_is_spatial(index)) { |
| 1109 |
ut_ad(cursor->rtr_info); |
| 1110 |
|
| 1111 |
node_seq_t seq_no = rtr_get_current_ssn_id(index); |
| 1112 |
|
| 1113 |
/* If SSN in memory is not initialized, fetch |
| 1114 |
it from root page */ |
| 1115 |
if (seq_no < 1) { |
| 1116 |
node_seq_t root_seq_no; |
| 1117 |
|
| 1118 |
root_seq_no = page_get_ssn_id(page); |
| 1119 |
|
| 1120 |
mutex_enter(&(index->rtr_ssn.mutex)); |
| 1121 |
index->rtr_ssn.seq_no = root_seq_no + 1; |
| 1122 |
mutex_exit(&(index->rtr_ssn.mutex)); |
| 1123 |
} |
| 1124 |
|
| 1125 |
/* Save the MBR */ |
| 1126 |
cursor->rtr_info->thr = cursor->thr; |
| 1127 |
rtr_get_mbr_from_tuple(tuple, &cursor->rtr_info->mbr); |
| 1128 |
} |
| 1129 |
|
| 1130 |
index->search_info->root_guess = block; |
| 1131 |
} |
| 1132 |
|
| 1133 |
if (height == 0) { |
| 1134 |
if (rw_latch == RW_NO_LATCH) { |
| 1135 |
latch_leaves = btr_cur_latch_leaves(block, page_id, page_size, latch_mode, |
| 1136 |
cursor, mtr); |
| 1137 |
} |
| 1138 |
|
| 1139 |
switch (latch_mode) { |
| 1140 |
case BTR_MODIFY_TREE: |
| 1141 |
case BTR_CONT_MODIFY_TREE: |
| 1142 |
case BTR_CONT_SEARCH_TREE: |
| 1143 |
break; |
| 1144 |
default: |
| 1145 |
if (!s_latch_by_caller && !srv_read_only_mode && !modify_external) { |
| 1146 |
/* Release the tree s-latch */ |
| 1147 |
/* NOTE: BTR_MODIFY_EXTERNAL |
| 1148 |
needs to keep tree sx-latch */ |
| 1149 |
mtr_release_s_latch_at_savepoint(mtr, savepoint, |
| 1150 |
dict_index_get_lock(index)); |
| 1151 |
} |
| 1152 |
|
| 1153 |
/* release upper blocks */ |
| 1154 |
if (retrying_for_search_prev) { |
| 1155 |
for (; prev_n_releases < prev_n_blocks; prev_n_releases++) { |
| 1156 |
mtr_release_block_at_savepoint( |
| 1157 |
mtr, prev_tree_savepoints[prev_n_releases], |
| 1158 |
prev_tree_blocks[prev_n_releases]); |
| 1159 |
} |
| 1160 |
} |
| 1161 |
|
| 1162 |
for (; n_releases < n_blocks; n_releases++) { |
| 1163 |
if (n_releases == 0 && modify_external) { |
| 1164 |
/* keep latch of root page */ |
| 1165 |
ut_ad(mtr_memo_contains_flagged( |
| 1166 |
mtr, tree_blocks[n_releases], |
| 1167 |
MTR_MEMO_PAGE_SX_FIX | MTR_MEMO_PAGE_X_FIX)); |
| 1168 |
continue; |
| 1169 |
} |
| 1170 |
|
| 1171 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 1172 |
tree_blocks[n_releases]); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
page_mode = mode; |
| 1177 |
} |
| 1178 |
|
| 1179 |
if (dict_index_is_spatial(index)) { |
| 1180 |
/* Remember the page search mode */ |
| 1181 |
search_mode = page_mode; |
| 1182 |
|
| 1183 |
/* Some adjustment on search mode, when the |
| 1184 |
page search mode is PAGE_CUR_RTREE_LOCATE |
| 1185 |
or PAGE_CUR_RTREE_INSERT, as we are searching |
| 1186 |
with MBRs. When it is not the target level, we |
| 1187 |
should search all sub-trees that "CONTAIN" the |
| 1188 |
search range/MBR. When it is at the target |
| 1189 |
level, the search becomes PAGE_CUR_LE */ |
| 1190 |
if (page_mode == PAGE_CUR_RTREE_LOCATE && level == height) { |
| 1191 |
if (level == 0) { |
| 1192 |
page_mode = PAGE_CUR_LE; |
| 1193 |
} else { |
| 1194 |
page_mode = PAGE_CUR_RTREE_GET_FATHER; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
if (page_mode == PAGE_CUR_RTREE_INSERT) { |
| 1199 |
page_mode = (level == height) ? PAGE_CUR_LE : PAGE_CUR_RTREE_INSERT; |
| 1200 |
|
| 1201 |
ut_ad(!page_is_leaf(page) || page_mode == PAGE_CUR_LE); |
| 1202 |
} |
| 1203 |
|
| 1204 |
/* "need_path" indicates if we need to tracking the parent |
| 1205 |
pages, if it is not spatial comparison, then no need to |
| 1206 |
track it */ |
| 1207 |
if (page_mode < PAGE_CUR_CONTAIN) { |
| 1208 |
need_path = false; |
| 1209 |
} |
| 1210 |
|
| 1211 |
up_match = 0; |
| 1212 |
low_match = 0; |
| 1213 |
|
| 1214 |
if (latch_mode == BTR_MODIFY_TREE || latch_mode == BTR_CONT_MODIFY_TREE || |
| 1215 |
latch_mode == BTR_CONT_SEARCH_TREE) { |
| 1216 |
/* Tree are locked, no need for Page Lock to protect |
| 1217 |
the "path" */ |
| 1218 |
cursor->rtr_info->need_page_lock = false; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
if (dict_index_is_spatial(index) && page_mode >= PAGE_CUR_CONTAIN) { |
| 1223 |
ut_ad(need_path); |
| 1224 |
found = rtr_cur_search_with_match(block, index, tuple, page_mode, |
| 1225 |
page_cursor, cursor->rtr_info); |
| 1226 |
|
| 1227 |
/* Need to use BTR_MODIFY_TREE to do the MBR adjustment */ |
| 1228 |
if (search_mode == PAGE_CUR_RTREE_INSERT && cursor->rtr_info->mbr_adj) { |
| 1229 |
if (latch_mode & BTR_MODIFY_LEAF) { |
| 1230 |
/* Parent MBR needs updated, should retry |
| 1231 |
with BTR_MODIFY_TREE */ |
| 1232 |
goto func_exit; |
| 1233 |
} else if (latch_mode & BTR_MODIFY_TREE) { |
| 1234 |
rtree_parent_modified = true; |
| 1235 |
cursor->rtr_info->mbr_adj = false; |
| 1236 |
mbr_adj = true; |
| 1237 |
} else { |
| 1238 |
ut_d(ut_error); |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
if (found && page_mode == PAGE_CUR_RTREE_GET_FATHER) { |
| 1243 |
cursor->low_match = DICT_INDEX_SPATIAL_NODEPTR_SIZE + 1; |
| 1244 |
} |
| 1245 |
} else if (height == 0 && btr_search_enabled && |
| 1246 |
!dict_index_is_spatial(index)) { |
| 1247 |
/* The adaptive hash index is only used when searching |
| 1248 |
for leaf pages (height==0), but not in r-trees. |
| 1249 |
We only need the byte prefix comparison for the purpose |
| 1250 |
of updating the adaptive hash index. */ |
| 1251 |
page_cur_search_with_match_bytes(block, index, tuple, page_mode, &up_match, |
| 1252 |
&up_bytes, &low_match, &low_bytes, |
| 1253 |
page_cursor); |
| 1254 |
} else { |
| 1255 |
/* Search for complete index fields. */ |
| 1256 |
up_bytes = low_bytes = 0; |
| 1257 |
page_cur_search_with_match(block, index, tuple, page_mode, &up_match, |
| 1258 |
&low_match, page_cursor, |
| 1259 |
need_path ? cursor->rtr_info : nullptr); |
| 1260 |
} |
| 1261 |
|
| 1262 |
if (estimate) { |
| 1263 |
btr_cur_add_path_info(cursor, height, root_height); |
| 1264 |
} |
| 1265 |
|
| 1266 |
/* If this is the desired level, leave the loop */ |
| 1267 |
|
| 1268 |
ut_ad(height == btr_page_get_level(page_cur_get_page(page_cursor))); |
| 1269 |
|
| 1270 |
/* Add Predicate lock if it is serializable isolation |
| 1271 |
and only if it is in the search case */ |
| 1272 |
if (dict_index_is_spatial(index) && cursor->rtr_info->need_prdt_lock && |
| 1273 |
mode != PAGE_CUR_RTREE_INSERT && mode != PAGE_CUR_RTREE_LOCATE && |
| 1274 |
mode >= PAGE_CUR_CONTAIN) { |
| 1275 |
trx_t *trx = thr_get_trx(cursor->thr); |
| 1276 |
lock_prdt_t prdt; |
| 1277 |
|
| 1278 |
trx_mutex_enter(trx); |
| 1279 |
lock_init_prdt_from_mbr(&prdt, &cursor->rtr_info->mbr, mode, |
| 1280 |
trx->lock.lock_heap); |
| 1281 |
trx_mutex_exit(trx); |
| 1282 |
|
| 1283 |
if (rw_latch == RW_NO_LATCH && height != 0) { |
| 1284 |
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE); |
| 1285 |
} |
| 1286 |
|
| 1287 |
lock_prdt_lock(block, &prdt, index, cursor->thr); |
| 1288 |
|
| 1289 |
if (rw_latch == RW_NO_LATCH && height != 0) { |
| 1290 |
rw_lock_s_unlock(&(block->lock)); |
| 1291 |
} |
| 1292 |
} |
| 1293 |
|
| 1294 |
if (level != height) { |
| 1295 |
const rec_t *node_ptr; |
| 1296 |
ut_ad(height > 0); |
| 1297 |
|
| 1298 |
height--; |
| 1299 |
|
| 1300 |
node_ptr = page_cur_get_rec(page_cursor); |
| 1301 |
|
| 1302 |
offsets = rec_get_offsets(node_ptr, index, offsets, ULINT_UNDEFINED, |
| 1303 |
UT_LOCATION_HERE, &heap); |
| 1304 |
|
| 1305 |
ut_ad(rec_offs_size(offsets) <= node_ptr_max_size); |
| 1306 |
|
| 1307 |
/* If the rec is the first or last in the page for |
| 1308 |
pessimistic delete intention, it might cause node_ptr insert |
| 1309 |
for the upper level. We should change the intention and retry. |
| 1310 |
*/ |
| 1311 |
if (latch_mode == BTR_MODIFY_TREE && |
| 1312 |
btr_cur_need_opposite_intention(page, lock_intention, node_ptr)) { |
| 1313 |
need_opposite_intention: |
| 1314 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 1315 |
|
| 1316 |
if (n_releases > 0) { |
| 1317 |
/* release root block */ |
| 1318 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[0], tree_blocks[0]); |
| 1319 |
} |
| 1320 |
|
| 1321 |
/* release all blocks */ |
| 1322 |
for (; n_releases <= n_blocks; n_releases++) { |
| 1323 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 1324 |
tree_blocks[n_releases]); |
| 1325 |
} |
| 1326 |
|
| 1327 |
lock_intention = BTR_INTENTION_BOTH; |
| 1328 |
|
| 1329 |
page_id.reset(space, dict_index_get_page(index)); |
| 1330 |
up_match = 0; |
| 1331 |
low_match = 0; |
| 1332 |
height = ULINT_UNDEFINED; |
| 1333 |
|
| 1334 |
n_blocks = 0; |
| 1335 |
n_releases = 0; |
| 1336 |
|
| 1337 |
goto search_loop; |
| 1338 |
} |
| 1339 |
|
| 1340 |
if (dict_index_is_spatial(index)) { |
| 1341 |
if (page_rec_is_supremum(node_ptr)) { |
| 1342 |
cursor->low_match = 0; |
| 1343 |
cursor->up_match = 0; |
| 1344 |
goto func_exit; |
| 1345 |
} |
| 1346 |
|
| 1347 |
/* If we are doing insertion or record locating, |
| 1348 |
remember the tree nodes we visited */ |
| 1349 |
if (page_mode == PAGE_CUR_RTREE_INSERT || |
| 1350 |
(search_mode == PAGE_CUR_RTREE_LOCATE && |
| 1351 |
(latch_mode != BTR_MODIFY_LEAF))) { |
| 1352 |
bool add_latch = false; |
| 1353 |
|
| 1354 |
if (latch_mode == BTR_MODIFY_TREE && rw_latch == RW_NO_LATCH) { |
| 1355 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 1356 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK)); |
| 1357 |
rw_lock_s_lock(&block->lock, UT_LOCATION_HERE); |
| 1358 |
add_latch = true; |
| 1359 |
} |
| 1360 |
|
| 1361 |
/* Store the parent cursor location */ |
| 1362 |
#ifdef UNIV_DEBUG |
| 1363 |
ulint num_stored = |
| 1364 |
rtr_store_parent_path(block, cursor, latch_mode, height + 1, mtr); |
| 1365 |
#else |
| 1366 |
rtr_store_parent_path(block, cursor, latch_mode, height + 1, mtr); |
| 1367 |
#endif |
| 1368 |
|
| 1369 |
if (page_mode == PAGE_CUR_RTREE_INSERT) { |
| 1370 |
btr_pcur_t *r_cursor = |
| 1371 |
rtr_get_parent_cursor(cursor, height + 1, true); |
| 1372 |
/* If it is insertion, there should |
| 1373 |
be only one parent for each level |
| 1374 |
traverse */ |
| 1375 |
|
| 1376 |
ut_ad(num_stored == 1); |
| 1377 |
|
| 1378 |
node_ptr = r_cursor->get_rec(); |
| 1379 |
} |
| 1380 |
|
| 1381 |
if (add_latch) { |
| 1382 |
rw_lock_s_unlock(&block->lock); |
| 1383 |
} |
| 1384 |
|
| 1385 |
ut_ad(!page_rec_is_supremum(node_ptr)); |
| 1386 |
} |
| 1387 |
|
| 1388 |
ut_ad(page_mode == search_mode || (page_mode == PAGE_CUR_WITHIN && |
| 1389 |
search_mode == PAGE_CUR_RTREE_LOCATE)); |
| 1390 |
|
| 1391 |
page_mode = search_mode; |
| 1392 |
} |
| 1393 |
|
| 1394 |
/* If the first or the last record of the page |
| 1395 |
or the same key value to the first record or last record, |
| 1396 |
the another page might be chosen when BTR_CONT_MODIFY_TREE. |
| 1397 |
So, the parent page should not released to avoiding deadlock |
| 1398 |
with blocking the another search with the same key value. */ |
| 1399 |
if (!detected_same_key_root && lock_intention == BTR_INTENTION_BOTH && |
| 1400 |
!dict_index_is_unique(index) && latch_mode == BTR_MODIFY_TREE && |
| 1401 |
(up_match >= rec_offs_n_fields(offsets) - 1 || |
| 1402 |
low_match >= rec_offs_n_fields(offsets) - 1)) { |
| 1403 |
const rec_t *first_rec = |
| 1404 |
page_rec_get_next_const(page_get_infimum_rec(page)); |
| 1405 |
ulint matched_fields; |
| 1406 |
|
| 1407 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 1408 |
|
| 1409 |
if (node_ptr == first_rec || page_rec_is_last(node_ptr, page)) { |
| 1410 |
detected_same_key_root = true; |
| 1411 |
} else { |
| 1412 |
matched_fields = 0; |
| 1413 |
|
| 1414 |
offsets2 = rec_get_offsets(first_rec, index, offsets2, ULINT_UNDEFINED, |
| 1415 |
UT_LOCATION_HERE, &heap); |
| 1416 |
cmp_rec_rec_with_match(node_ptr, first_rec, offsets, offsets2, index, |
| 1417 |
page_is_spatial_non_leaf(first_rec, index), |
| 1418 |
false, &matched_fields); |
| 1419 |
|
| 1420 |
if (matched_fields >= rec_offs_n_fields(offsets) - 1) { |
| 1421 |
detected_same_key_root = true; |
| 1422 |
} else { |
| 1423 |
const rec_t *last_rec; |
| 1424 |
|
| 1425 |
last_rec = page_rec_get_prev_const(page_get_supremum_rec(page)); |
| 1426 |
|
| 1427 |
matched_fields = 0; |
| 1428 |
|
| 1429 |
offsets2 = rec_get_offsets(last_rec, index, offsets2, ULINT_UNDEFINED, |
| 1430 |
UT_LOCATION_HERE, &heap); |
| 1431 |
cmp_rec_rec_with_match(node_ptr, last_rec, offsets, offsets2, index, |
| 1432 |
page_is_spatial_non_leaf(last_rec, index), |
| 1433 |
false, &matched_fields); |
| 1434 |
if (matched_fields >= rec_offs_n_fields(offsets) - 1) { |
| 1435 |
detected_same_key_root = true; |
| 1436 |
} |
| 1437 |
} |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
/* If the page might cause modify_tree, |
| 1442 |
we should not release the parent page's lock. */ |
| 1443 |
if (!detected_same_key_root && latch_mode == BTR_MODIFY_TREE && |
| 1444 |
!btr_cur_will_modify_tree(index, page, lock_intention, node_ptr, |
| 1445 |
node_ptr_max_size, page_size, mtr) && |
| 1446 |
!rtree_parent_modified) { |
| 1447 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 1448 |
ut_ad(n_releases <= n_blocks); |
| 1449 |
|
| 1450 |
/* we can release upper blocks */ |
| 1451 |
for (; n_releases < n_blocks; n_releases++) { |
| 1452 |
if (n_releases == 0) { |
| 1453 |
/* we should not release root page |
| 1454 |
to pin to same block. */ |
| 1455 |
continue; |
| 1456 |
} |
| 1457 |
|
| 1458 |
/* release unused blocks to unpin */ |
| 1459 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 1460 |
tree_blocks[n_releases]); |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
if (height == level && latch_mode == BTR_MODIFY_TREE) { |
| 1465 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 1466 |
/* we should sx-latch root page, if released already. |
| 1467 |
It contains seg_header. */ |
| 1468 |
if (n_releases > 0) { |
| 1469 |
mtr_block_sx_latch_at_savepoint(mtr, tree_savepoints[0], |
| 1470 |
tree_blocks[0]); |
| 1471 |
} |
| 1472 |
|
| 1473 |
/* x-latch the branch blocks not released yet. */ |
| 1474 |
for (ulint i = n_releases; i <= n_blocks; i++) { |
| 1475 |
mtr_block_x_latch_at_savepoint(mtr, tree_savepoints[i], tree_blocks[i]); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
/* We should consider prev_page of parent page, if the node_ptr |
| 1480 |
is the leftmost of the page. because BTR_SEARCH_PREV and |
| 1481 |
BTR_MODIFY_PREV latches prev_page of the leaf page. */ |
| 1482 |
if ((latch_mode == BTR_SEARCH_PREV || latch_mode == BTR_MODIFY_PREV) && |
| 1483 |
!retrying_for_search_prev) { |
| 1484 |
/* block should be latched for consistent |
| 1485 |
btr_page_get_prev() */ |
| 1486 |
ut_ad(mtr_memo_contains_flagged( |
| 1487 |
mtr, block, MTR_MEMO_PAGE_S_FIX | MTR_MEMO_PAGE_X_FIX)); |
| 1488 |
|
| 1489 |
if (btr_page_get_prev(page, mtr) != FIL_NULL && |
| 1490 |
page_rec_is_first(node_ptr, page)) { |
| 1491 |
if (leftmost_from_level == 0) { |
| 1492 |
leftmost_from_level = height + 1; |
| 1493 |
} |
| 1494 |
} else { |
| 1495 |
leftmost_from_level = 0; |
| 1496 |
} |
| 1497 |
|
| 1498 |
if (height == 0 && leftmost_from_level > 0) { |
| 1499 |
/* should retry to get also prev_page |
| 1500 |
from level==leftmost_from_level. */ |
| 1501 |
retrying_for_search_prev = true; |
| 1502 |
|
| 1503 |
prev_tree_blocks = static_cast<buf_block_t **>( |
| 1504 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, |
| 1505 |
sizeof(buf_block_t *) * leftmost_from_level)); |
| 1506 |
|
| 1507 |
prev_tree_savepoints = static_cast<ulint *>(ut::malloc_withkey( |
| 1508 |
UT_NEW_THIS_FILE_PSI_KEY, sizeof(ulint) * leftmost_from_level)); |
| 1509 |
|
| 1510 |
/* back to the level (leftmost_from_level+1) */ |
| 1511 |
ulint idx = n_blocks - (leftmost_from_level - 1); |
| 1512 |
|
| 1513 |
page_id.reset(space, tree_blocks[idx]->page.id.page_no()); |
| 1514 |
|
| 1515 |
for (ulint i = n_blocks - (leftmost_from_level - 1); i <= n_blocks; |
| 1516 |
i++) { |
| 1517 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[i], |
| 1518 |
tree_blocks[i]); |
| 1519 |
} |
| 1520 |
|
| 1521 |
n_blocks -= (leftmost_from_level - 1); |
| 1522 |
height = leftmost_from_level; |
| 1523 |
ut_ad(n_releases == 0); |
| 1524 |
|
| 1525 |
/* replay up_match, low_match */ |
| 1526 |
up_match = 0; |
| 1527 |
low_match = 0; |
| 1528 |
rtr_info_t *rtr_info = need_path ? cursor->rtr_info : nullptr; |
| 1529 |
|
| 1530 |
for (ulint i = 0; i < n_blocks; i++) { |
| 1531 |
page_cur_search_with_match(tree_blocks[i], index, tuple, page_mode, |
| 1532 |
&up_match, &low_match, page_cursor, |
| 1533 |
rtr_info); |
| 1534 |
} |
| 1535 |
|
| 1536 |
goto search_loop; |
| 1537 |
} |
| 1538 |
} |
| 1539 |
|
| 1540 |
/* Go to the child node */ |
| 1541 |
page_id.reset(space, btr_node_ptr_get_child_page_no(node_ptr, offsets)); |
| 1542 |
|
| 1543 |
n_blocks++; |
| 1544 |
|
| 1545 |
if (UNIV_UNLIKELY(height == 0 && dict_index_is_ibuf(index))) { |
| 1546 |
/* We're doing a search on an ibuf tree and we're one |
| 1547 |
level above the leaf page. */ |
| 1548 |
|
| 1549 |
ut_ad(level == 0); |
| 1550 |
|
| 1551 |
fetch = cursor->m_fetch_mode; |
| 1552 |
rw_latch = RW_NO_LATCH; |
| 1553 |
goto retry_page_get; |
| 1554 |
} |
| 1555 |
|
| 1556 |
if (dict_index_is_spatial(index) && page_mode >= PAGE_CUR_CONTAIN && |
| 1557 |
page_mode != PAGE_CUR_RTREE_INSERT) { |
| 1558 |
ut_ad(need_path); |
| 1559 |
rtr_node_path_t *path = cursor->rtr_info->path; |
| 1560 |
|
| 1561 |
if (!path->empty() && found) { |
| 1562 |
#ifdef UNIV_DEBUG |
| 1563 |
node_visit_t last_visit = path->back(); |
| 1564 |
|
| 1565 |
ut_ad(last_visit.page_no == page_id.page_no()); |
| 1566 |
#endif /* UNIV_DEBUG */ |
| 1567 |
|
| 1568 |
path->pop_back(); |
| 1569 |
|
| 1570 |
#ifdef UNIV_DEBUG |
| 1571 |
if (page_mode == PAGE_CUR_RTREE_LOCATE && |
| 1572 |
(latch_mode != BTR_MODIFY_LEAF)) { |
| 1573 |
btr_pcur_t *cur = cursor->rtr_info->parent_path->back().cursor; |
| 1574 |
rec_t *my_node_ptr = cur->get_rec(); |
| 1575 |
|
| 1576 |
offsets = rec_get_offsets(my_node_ptr, index, offsets, |
| 1577 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 1578 |
|
| 1579 |
page_no_t my_page_no = |
| 1580 |
btr_node_ptr_get_child_page_no(my_node_ptr, offsets); |
| 1581 |
|
| 1582 |
ut_ad(page_id.page_no() == my_page_no); |
| 1583 |
} |
| 1584 |
#endif |
| 1585 |
} |
| 1586 |
} |
| 1587 |
|
| 1588 |
goto search_loop; |
| 1589 |
} else if (!dict_index_is_spatial(index) && latch_mode == BTR_MODIFY_TREE && |
| 1590 |
lock_intention == BTR_INTENTION_INSERT && |
| 1591 |
mach_read_from_4(page + FIL_PAGE_NEXT) != FIL_NULL && |
| 1592 |
page_rec_is_last(page_cur_get_rec(page_cursor), page)) { |
| 1593 |
/* btr_insert_into_right_sibling() might cause |
| 1594 |
deleting node_ptr at upper level */ |
| 1595 |
|
| 1596 |
if (height == 0) { |
| 1597 |
/* release the leaf pages if latched */ |
| 1598 |
for (uint i = 0; i < 3; i++) { |
| 1599 |
if (latch_leaves.blocks[i] != nullptr) { |
| 1600 |
mtr_release_block_at_savepoint(mtr, latch_leaves.savepoints[i], |
| 1601 |
latch_leaves.blocks[i]); |
| 1602 |
latch_leaves.blocks[i] = nullptr; |
| 1603 |
} |
| 1604 |
} |
| 1605 |
} |
| 1606 |
|
| 1607 |
goto need_opposite_intention; |
| 1608 |
} |
| 1609 |
|
| 1610 |
if (level != 0) { |
| 1611 |
if (upper_rw_latch == RW_NO_LATCH) { |
| 1612 |
/* latch the page */ |
| 1613 |
buf_block_t *child_block; |
| 1614 |
|
| 1615 |
if (latch_mode == BTR_CONT_MODIFY_TREE) { |
| 1616 |
child_block = btr_block_get(page_id, page_size, RW_X_LATCH, |
| 1617 |
UT_LOCATION_HERE, index, mtr); |
| 1618 |
} else { |
| 1619 |
ut_ad(latch_mode == BTR_CONT_SEARCH_TREE); |
| 1620 |
child_block = btr_block_get(page_id, page_size, RW_SX_LATCH, |
| 1621 |
UT_LOCATION_HERE, index, mtr); |
| 1622 |
} |
| 1623 |
|
| 1624 |
btr_assert_not_corrupted(child_block, index); |
| 1625 |
} else { |
| 1626 |
ut_ad(mtr_memo_contains(mtr, block, upper_rw_latch)); |
| 1627 |
btr_assert_not_corrupted(block, index); |
| 1628 |
|
| 1629 |
if (s_latch_by_caller) { |
| 1630 |
ut_ad(latch_mode == BTR_SEARCH_TREE); |
| 1631 |
/* to exclude modifying tree operations |
| 1632 |
should sx-latch the index. */ |
| 1633 |
ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index), |
| 1634 |
MTR_MEMO_SX_LOCK)); |
| 1635 |
/* because has sx-latch of index, |
| 1636 |
can release upper blocks. */ |
| 1637 |
for (; n_releases < n_blocks; n_releases++) { |
| 1638 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 1639 |
tree_blocks[n_releases]); |
| 1640 |
} |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
if (page_mode <= PAGE_CUR_LE) { |
| 1645 |
cursor->low_match = low_match; |
| 1646 |
cursor->up_match = up_match; |
| 1647 |
} |
| 1648 |
} else { |
| 1649 |
cursor->low_match = low_match; |
| 1650 |
cursor->low_bytes = low_bytes; |
| 1651 |
cursor->up_match = up_match; |
| 1652 |
cursor->up_bytes = up_bytes; |
| 1653 |
|
| 1654 |
/* We do a dirty read of btr_search_enabled here. We |
| 1655 |
will properly check btr_search_enabled again in |
| 1656 |
btr_search_build_page_hash_index() before building a |
| 1657 |
page hash index, while holding search latch. */ |
| 1658 |
if (btr_search_enabled && !index->disable_ahi) { |
| 1659 |
btr_search_info_update(cursor); |
| 1660 |
} |
| 1661 |
ut_ad(cursor->up_match != ULINT_UNDEFINED || mode != PAGE_CUR_GE); |
| 1662 |
ut_ad(cursor->up_match != ULINT_UNDEFINED || mode != PAGE_CUR_LE); |
| 1663 |
ut_ad(cursor->low_match != ULINT_UNDEFINED || mode != PAGE_CUR_LE); |
| 1664 |
} |
| 1665 |
|
| 1666 |
/* For spatial index, remember what blocks are still latched */ |
| 1667 |
if (dict_index_is_spatial(index) && |
| 1668 |
(latch_mode == BTR_MODIFY_TREE || latch_mode == BTR_MODIFY_LEAF)) { |
| 1669 |
for (ulint i = 0; i < n_releases; i++) { |
| 1670 |
cursor->rtr_info->tree_blocks[i] = nullptr; |
| 1671 |
cursor->rtr_info->tree_savepoints[i] = 0; |
| 1672 |
} |
| 1673 |
|
| 1674 |
for (ulint i = n_releases; i <= n_blocks; i++) { |
| 1675 |
cursor->rtr_info->tree_blocks[i] = tree_blocks[i]; |
| 1676 |
cursor->rtr_info->tree_savepoints[i] = tree_savepoints[i]; |
| 1677 |
} |
| 1678 |
} |
| 1679 |
|
| 1680 |
func_exit: |
| 1681 |
|
| 1682 |
if (UNIV_LIKELY_NULL(heap)) { |
| 1683 |
mem_heap_free(heap); |
| 1684 |
} |
| 1685 |
|
| 1686 |
if (retrying_for_search_prev) { |
| 1687 |
ut::free(prev_tree_blocks); |
| 1688 |
ut::free(prev_tree_savepoints); |
| 1689 |
} |
| 1690 |
|
| 1691 |
if (has_search_latch) { |
| 1692 |
rw_lock_s_lock(btr_get_search_latch(index), UT_LOCATION_HERE); |
| 1693 |
} |
| 1694 |
|
| 1695 |
if (mbr_adj) { |
| 1696 |
/* remember that we will need to adjust parent MBR */ |
| 1697 |
cursor->rtr_info->mbr_adj = true; |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
/** Searches an index tree and positions a tree cursor on a given level. |
| 1702 |
This function will avoid placing latches while traversing the path and so |
| 1703 |
should be used only for cases where-in latching is not needed. |
| 1704 |
|
| 1705 |
@param[in] index Index |
| 1706 |
@param[in] level The tree level of search |
| 1707 |
@param[in] tuple Data tuple; Note: n_fields_cmp in compared |
| 1708 |
to the node ptr page node field |
| 1709 |
@param[in] mode PAGE_CUR_L, .... |
| 1710 |
Insert should always be made using PAGE_CUR_LE |
| 1711 |
to search the position. |
| 1712 |
@param[in,out] cursor Tree cursor; points to record of interest. |
| 1713 |
@param[in] file File name |
| 1714 |
@param[in] line Line where called from |
| 1715 |
@param[in,out] mtr Mini-transaction |
| 1716 |
@param[in] mark_dirty if true then mark the block as dirty */ |
| 1717 |
void btr_cur_search_to_nth_level_with_no_latch(dict_index_t *index, ulint level, |
| 1718 |
const dtuple_t *tuple, |
| 1719 |
page_cur_mode_t mode, |
| 1720 |
btr_cur_t *cursor, |
| 1721 |
const char *file, ulint line, |
| 1722 |
mtr_t *mtr, bool mark_dirty) { |
| 1723 |
page_t *page = nullptr; /* remove warning */ |
| 1724 |
buf_block_t *block; |
| 1725 |
ulint height; |
| 1726 |
ulint up_match; |
| 1727 |
ulint low_match; |
| 1728 |
ulint rw_latch; |
| 1729 |
page_cur_mode_t page_mode; |
| 1730 |
Page_fetch fetch; |
| 1731 |
page_cur_t *page_cursor; |
| 1732 |
ulint root_height = 0; /* remove warning */ |
| 1733 |
ulint n_blocks [[maybe_unused]] = 0; |
| 1734 |
|
| 1735 |
mem_heap_t *heap = nullptr; |
| 1736 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 1737 |
ulint *offsets = offsets_; |
| 1738 |
rec_offs_init(offsets_); |
| 1739 |
|
| 1740 |
DBUG_TRACE; |
| 1741 |
|
| 1742 |
ut_ad(index->table->is_intrinsic()); |
| 1743 |
ut_ad(level == 0 || mode == PAGE_CUR_LE); |
| 1744 |
ut_ad(dict_index_check_search_tuple(index, tuple)); |
| 1745 |
|
| 1746 |
UNIV_MEM_INVALID(&cursor->up_match, sizeof cursor->up_match); |
| 1747 |
UNIV_MEM_INVALID(&cursor->low_match, sizeof cursor->low_match); |
| 1748 |
#ifdef UNIV_DEBUG |
| 1749 |
cursor->up_match = ULINT_UNDEFINED; |
| 1750 |
cursor->low_match = ULINT_UNDEFINED; |
| 1751 |
#endif /* UNIV_DEBUG */ |
| 1752 |
|
| 1753 |
cursor->flag = BTR_CUR_BINARY; |
| 1754 |
cursor->index = index; |
| 1755 |
|
| 1756 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 1757 |
|
| 1758 |
const space_id_t space = dict_index_get_space(index); |
| 1759 |
const page_size_t page_size(dict_table_page_size(index->table)); |
| 1760 |
/* Start with the root page. */ |
| 1761 |
page_id_t page_id(space, dict_index_get_page(index)); |
| 1762 |
|
| 1763 |
up_match = 0; |
| 1764 |
low_match = 0; |
| 1765 |
|
| 1766 |
height = ULINT_UNDEFINED; |
| 1767 |
|
| 1768 |
/* We use these modified search modes on non-leaf levels of the |
| 1769 |
B-tree. These let us end up in the right B-tree leaf. In that leaf |
| 1770 |
we use the original search mode. */ |
| 1771 |
|
| 1772 |
switch (mode) { |
| 1773 |
case PAGE_CUR_GE: |
| 1774 |
page_mode = PAGE_CUR_L; |
| 1775 |
break; |
| 1776 |
case PAGE_CUR_G: |
| 1777 |
page_mode = PAGE_CUR_LE; |
| 1778 |
break; |
| 1779 |
default: |
| 1780 |
page_mode = mode; |
| 1781 |
break; |
| 1782 |
} |
| 1783 |
|
| 1784 |
/* Loop and search until we arrive at the desired level */ |
| 1785 |
bool at_desired_level = false; |
| 1786 |
while (!at_desired_level) { |
| 1787 |
fetch = cursor->m_fetch_mode; |
| 1788 |
rw_latch = RW_NO_LATCH; |
| 1789 |
|
| 1790 |
ut_ad(n_blocks < BTR_MAX_LEVELS); |
| 1791 |
|
| 1792 |
block = buf_page_get_gen(page_id, page_size, rw_latch, nullptr, fetch, |
| 1793 |
{file, line}, mtr, mark_dirty); |
| 1794 |
|
| 1795 |
page = buf_block_get_frame(block); |
| 1796 |
|
| 1797 |
if (height == ULINT_UNDEFINED) { |
| 1798 |
/* We are in the root node */ |
| 1799 |
|
| 1800 |
height = btr_page_get_level(page); |
| 1801 |
root_height = height; |
| 1802 |
cursor->tree_height = root_height + 1; |
| 1803 |
} |
| 1804 |
|
| 1805 |
if (height == 0) { |
| 1806 |
/* On leaf level. Switch back to original search mode.*/ |
| 1807 |
page_mode = mode; |
| 1808 |
} |
| 1809 |
|
| 1810 |
page_cur_search_with_match(block, index, tuple, page_mode, &up_match, |
| 1811 |
&low_match, page_cursor, nullptr); |
| 1812 |
|
| 1813 |
ut_ad(height == btr_page_get_level(page_cur_get_page(page_cursor))); |
| 1814 |
|
| 1815 |
if (level != height) { |
| 1816 |
const rec_t *node_ptr; |
| 1817 |
ut_ad(height > 0); |
| 1818 |
|
| 1819 |
height--; |
| 1820 |
|
| 1821 |
node_ptr = page_cur_get_rec(page_cursor); |
| 1822 |
|
| 1823 |
offsets = rec_get_offsets(node_ptr, index, offsets, ULINT_UNDEFINED, |
| 1824 |
UT_LOCATION_HERE, &heap); |
| 1825 |
|
| 1826 |
/* Go to the child node */ |
| 1827 |
page_id.reset(space, btr_node_ptr_get_child_page_no(node_ptr, offsets)); |
| 1828 |
|
| 1829 |
n_blocks++; |
| 1830 |
} else { |
| 1831 |
/* If this is the desired level, leave the loop */ |
| 1832 |
at_desired_level = true; |
| 1833 |
} |
| 1834 |
} |
| 1835 |
|
| 1836 |
cursor->low_match = low_match; |
| 1837 |
cursor->up_match = up_match; |
| 1838 |
|
| 1839 |
if (heap != nullptr) { |
| 1840 |
mem_heap_free(heap); |
| 1841 |
} |
| 1842 |
} |
| 1843 |
|
| 1844 |
/** Opens a cursor at either end of an index. |
| 1845 |
@param[in] from_left True if open to the low end, false if to the high end |
| 1846 |
@param[in] index Index |
| 1847 |
@param[in] latch_mode Latch mode |
| 1848 |
@param[in,out] cursor Cursor |
| 1849 |
@param[in] level Level to search for (0=leaf) |
| 1850 |
@param[in] location Location where called |
| 1851 |
@param[in,out] mtr Mini-transaction */ |
| 1852 |
void btr_cur_open_at_index_side(bool from_left, dict_index_t *index, |
| 1853 |
ulint latch_mode, btr_cur_t *cursor, |
| 1854 |
ulint level, ut::Location location, |
| 1855 |
mtr_t *mtr) { |
| 1856 |
page_cur_t *page_cursor; |
| 1857 |
ulint node_ptr_max_size = UNIV_PAGE_SIZE / 2; |
| 1858 |
ulint height; |
| 1859 |
ulint root_height = 0; /* remove warning */ |
| 1860 |
rec_t *node_ptr; |
| 1861 |
ulint estimate; |
| 1862 |
ulint savepoint; |
| 1863 |
ulint upper_rw_latch, root_leaf_rw_latch; |
| 1864 |
btr_intention_t lock_intention; |
| 1865 |
buf_block_t *tree_blocks[BTR_MAX_LEVELS]; |
| 1866 |
ulint tree_savepoints[BTR_MAX_LEVELS]; |
| 1867 |
ulint n_blocks = 0; |
| 1868 |
ulint n_releases = 0; |
| 1869 |
mem_heap_t *heap = nullptr; |
| 1870 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 1871 |
ulint *offsets = offsets_; |
| 1872 |
rec_offs_init(offsets_); |
| 1873 |
|
| 1874 |
estimate = latch_mode & BTR_ESTIMATE; |
| 1875 |
latch_mode &= ~BTR_ESTIMATE; |
| 1876 |
|
| 1877 |
ut_ad(level != ULINT_UNDEFINED); |
| 1878 |
|
| 1879 |
const bool s_latch_by_caller = latch_mode & BTR_ALREADY_S_LATCHED; |
| 1880 |
latch_mode &= ~BTR_ALREADY_S_LATCHED; |
| 1881 |
|
| 1882 |
lock_intention = btr_cur_get_and_clear_intention(&latch_mode); |
| 1883 |
|
| 1884 |
ut_ad(!(latch_mode & BTR_MODIFY_EXTERNAL)); |
| 1885 |
|
| 1886 |
/* This function doesn't need to lock left page of the leaf page */ |
| 1887 |
if (latch_mode == BTR_SEARCH_PREV) { |
| 1888 |
latch_mode = BTR_SEARCH_LEAF; |
| 1889 |
} else if (latch_mode == BTR_MODIFY_PREV) { |
| 1890 |
latch_mode = BTR_MODIFY_LEAF; |
| 1891 |
} |
| 1892 |
|
| 1893 |
/* Store the position of the tree latch we push to mtr so that we |
| 1894 |
know how to release it when we have latched the leaf node */ |
| 1895 |
|
| 1896 |
savepoint = mtr_set_savepoint(mtr); |
| 1897 |
|
| 1898 |
switch (latch_mode) { |
| 1899 |
case BTR_CONT_MODIFY_TREE: |
| 1900 |
case BTR_CONT_SEARCH_TREE: |
| 1901 |
upper_rw_latch = RW_NO_LATCH; |
| 1902 |
break; |
| 1903 |
case BTR_MODIFY_TREE: |
| 1904 |
/* Most of delete-intended operations are purging. |
| 1905 |
Free blocks and read IO bandwidth should be prior |
| 1906 |
for them, when the history list is glowing huge. */ |
| 1907 |
if (lock_intention == BTR_INTENTION_DELETE && |
| 1908 |
trx_sys->rseg_history_len.load() > BTR_CUR_FINE_HISTORY_LENGTH && |
| 1909 |
buf_get_n_pending_read_ios()) { |
| 1910 |
mtr_x_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 1911 |
} else { |
| 1912 |
mtr_sx_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 1913 |
} |
| 1914 |
upper_rw_latch = RW_X_LATCH; |
| 1915 |
break; |
| 1916 |
default: |
| 1917 |
ut_ad(!s_latch_by_caller || |
| 1918 |
mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 1919 |
MTR_MEMO_SX_LOCK | MTR_MEMO_S_LOCK)); |
| 1920 |
if (!srv_read_only_mode) { |
| 1921 |
if (!s_latch_by_caller) { |
| 1922 |
/* BTR_SEARCH_TREE is intended to be used with |
| 1923 |
BTR_ALREADY_S_LATCHED */ |
| 1924 |
ut_ad(latch_mode != BTR_SEARCH_TREE); |
| 1925 |
|
| 1926 |
mtr_s_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 1927 |
} |
| 1928 |
upper_rw_latch = RW_S_LATCH; |
| 1929 |
} else { |
| 1930 |
upper_rw_latch = RW_NO_LATCH; |
| 1931 |
} |
| 1932 |
} |
| 1933 |
root_leaf_rw_latch = btr_cur_latch_for_root_leaf(latch_mode); |
| 1934 |
|
| 1935 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 1936 |
cursor->index = index; |
| 1937 |
|
| 1938 |
page_id_t page_id(dict_index_get_space(index), dict_index_get_page(index)); |
| 1939 |
const page_size_t &page_size = dict_table_page_size(index->table); |
| 1940 |
|
| 1941 |
if (root_leaf_rw_latch == RW_X_LATCH) { |
| 1942 |
node_ptr_max_size = dict_index_node_ptr_max_size(index); |
| 1943 |
} |
| 1944 |
|
| 1945 |
height = ULINT_UNDEFINED; |
| 1946 |
|
| 1947 |
for (;;) { |
| 1948 |
buf_block_t *block; |
| 1949 |
page_t *page; |
| 1950 |
ulint rw_latch; |
| 1951 |
|
| 1952 |
ut_ad(n_blocks < BTR_MAX_LEVELS); |
| 1953 |
|
| 1954 |
if (height != 0 && (latch_mode != BTR_MODIFY_TREE || height == level)) { |
| 1955 |
rw_latch = upper_rw_latch; |
| 1956 |
} else { |
| 1957 |
rw_latch = RW_NO_LATCH; |
| 1958 |
} |
| 1959 |
|
| 1960 |
tree_savepoints[n_blocks] = mtr_set_savepoint(mtr); |
| 1961 |
block = buf_page_get_gen(page_id, page_size, rw_latch, nullptr, |
| 1962 |
cursor->m_fetch_mode, location, mtr); |
| 1963 |
tree_blocks[n_blocks] = block; |
| 1964 |
|
| 1965 |
page = buf_block_get_frame(block); |
| 1966 |
|
| 1967 |
if (height == ULINT_UNDEFINED && btr_page_get_level(page) == 0 && |
| 1968 |
rw_latch != RW_NO_LATCH && rw_latch != root_leaf_rw_latch) { |
| 1969 |
/* We should retry to get the page, because the root page |
| 1970 |
is latched with different level as a leaf page. */ |
| 1971 |
ut_ad(root_leaf_rw_latch != RW_NO_LATCH); |
| 1972 |
ut_ad(rw_latch == RW_S_LATCH); |
| 1973 |
|
| 1974 |
ut_ad(n_blocks == 0); |
| 1975 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_blocks], |
| 1976 |
tree_blocks[n_blocks]); |
| 1977 |
|
| 1978 |
upper_rw_latch = root_leaf_rw_latch; |
| 1979 |
continue; |
| 1980 |
} |
| 1981 |
|
| 1982 |
ut_ad(fil_page_index_page_check(page)); |
| 1983 |
ut_ad(index->id == btr_page_get_index_id(page)); |
| 1984 |
|
| 1985 |
if (height == ULINT_UNDEFINED) { |
| 1986 |
/* We are in the root node */ |
| 1987 |
|
| 1988 |
height = btr_page_get_level(page); |
| 1989 |
root_height = height; |
| 1990 |
ut_a(height >= level); |
| 1991 |
} else { |
| 1992 |
/* TODO: flag the index corrupted if this fails */ |
| 1993 |
ut_ad(height == btr_page_get_level(page)); |
| 1994 |
} |
| 1995 |
|
| 1996 |
if (height == level) { |
| 1997 |
if (srv_read_only_mode) { |
| 1998 |
btr_cur_latch_leaves(block, page_id, page_size, latch_mode, cursor, |
| 1999 |
mtr); |
| 2000 |
} else if (height == 0) { |
| 2001 |
if (rw_latch == RW_NO_LATCH) { |
| 2002 |
btr_cur_latch_leaves(block, page_id, page_size, latch_mode, cursor, |
| 2003 |
mtr); |
| 2004 |
} |
| 2005 |
/* In versions <= 3.23.52 we had |
| 2006 |
forgotten to release the tree latch |
| 2007 |
here. If in an index scan we had to |
| 2008 |
scan far to find a record visible to |
| 2009 |
the current transaction, that could |
| 2010 |
starve others waiting for the tree |
| 2011 |
latch. */ |
| 2012 |
|
| 2013 |
switch (latch_mode) { |
| 2014 |
case BTR_MODIFY_TREE: |
| 2015 |
case BTR_CONT_MODIFY_TREE: |
| 2016 |
case BTR_CONT_SEARCH_TREE: |
| 2017 |
break; |
| 2018 |
default: |
| 2019 |
if (!s_latch_by_caller) { |
| 2020 |
/* Release the tree s-latch */ |
| 2021 |
mtr_release_s_latch_at_savepoint(mtr, savepoint, |
| 2022 |
dict_index_get_lock(index)); |
| 2023 |
} |
| 2024 |
|
| 2025 |
/* release upper blocks */ |
| 2026 |
for (; n_releases < n_blocks; n_releases++) { |
| 2027 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 2028 |
tree_blocks[n_releases]); |
| 2029 |
} |
| 2030 |
} |
| 2031 |
} else { /* height != 0 */ |
| 2032 |
/* We already have the block latched. */ |
| 2033 |
ut_ad(latch_mode == BTR_SEARCH_TREE); |
| 2034 |
ut_ad(s_latch_by_caller); |
| 2035 |
ut_ad(upper_rw_latch == RW_S_LATCH); |
| 2036 |
|
| 2037 |
ut_ad(mtr_memo_contains(mtr, block, upper_rw_latch)); |
| 2038 |
|
| 2039 |
if (s_latch_by_caller) { |
| 2040 |
/* to exclude modifying tree operations |
| 2041 |
should sx-latch the index. */ |
| 2042 |
ut_ad(mtr_memo_contains(mtr, dict_index_get_lock(index), |
| 2043 |
MTR_MEMO_SX_LOCK)); |
| 2044 |
/* because has sx-latch of index, |
| 2045 |
can release upper blocks. */ |
| 2046 |
for (; n_releases < n_blocks; n_releases++) { |
| 2047 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 2048 |
tree_blocks[n_releases]); |
| 2049 |
} |
| 2050 |
} |
| 2051 |
} |
| 2052 |
} |
| 2053 |
|
| 2054 |
if (from_left) { |
| 2055 |
page_cur_set_before_first(block, page_cursor); |
| 2056 |
} else { |
| 2057 |
page_cur_set_after_last(block, page_cursor); |
| 2058 |
} |
| 2059 |
|
| 2060 |
if (height == level) { |
| 2061 |
if (estimate) { |
| 2062 |
btr_cur_add_path_info(cursor, height, root_height); |
| 2063 |
} |
| 2064 |
|
| 2065 |
break; |
| 2066 |
} |
| 2067 |
|
| 2068 |
ut_ad(height > 0); |
| 2069 |
|
| 2070 |
if (from_left) { |
| 2071 |
page_cur_move_to_next(page_cursor); |
| 2072 |
} else { |
| 2073 |
page_cur_move_to_prev(page_cursor); |
| 2074 |
} |
| 2075 |
|
| 2076 |
if (estimate) { |
| 2077 |
btr_cur_add_path_info(cursor, height, root_height); |
| 2078 |
} |
| 2079 |
|
| 2080 |
height--; |
| 2081 |
|
| 2082 |
node_ptr = page_cur_get_rec(page_cursor); |
| 2083 |
offsets = rec_get_offsets(node_ptr, cursor->index, offsets, ULINT_UNDEFINED, |
| 2084 |
UT_LOCATION_HERE, &heap); |
| 2085 |
|
| 2086 |
/* If the rec is the first or last in the page for |
| 2087 |
pessimistic delete intention, it might cause node_ptr insert |
| 2088 |
for the upper level. We should change the intention and retry. |
| 2089 |
*/ |
| 2090 |
if (latch_mode == BTR_MODIFY_TREE && |
| 2091 |
btr_cur_need_opposite_intention(page, lock_intention, node_ptr)) { |
| 2092 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 2093 |
/* release all blocks */ |
| 2094 |
for (; n_releases <= n_blocks; n_releases++) { |
| 2095 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 2096 |
tree_blocks[n_releases]); |
| 2097 |
} |
| 2098 |
|
| 2099 |
lock_intention = BTR_INTENTION_BOTH; |
| 2100 |
|
| 2101 |
page_id.set_page_no(dict_index_get_page(index)); |
| 2102 |
|
| 2103 |
height = ULINT_UNDEFINED; |
| 2104 |
|
| 2105 |
n_blocks = 0; |
| 2106 |
n_releases = 0; |
| 2107 |
|
| 2108 |
continue; |
| 2109 |
} |
| 2110 |
|
| 2111 |
if (latch_mode == BTR_MODIFY_TREE && |
| 2112 |
!btr_cur_will_modify_tree(cursor->index, page, lock_intention, node_ptr, |
| 2113 |
node_ptr_max_size, page_size, mtr)) { |
| 2114 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 2115 |
ut_ad(n_releases <= n_blocks); |
| 2116 |
|
| 2117 |
/* we can release upper blocks */ |
| 2118 |
for (; n_releases < n_blocks; n_releases++) { |
| 2119 |
if (n_releases == 0) { |
| 2120 |
/* we should not release root page |
| 2121 |
to pin to same block. */ |
| 2122 |
continue; |
| 2123 |
} |
| 2124 |
|
| 2125 |
/* release unused blocks to unpin */ |
| 2126 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 2127 |
tree_blocks[n_releases]); |
| 2128 |
} |
| 2129 |
} |
| 2130 |
|
| 2131 |
if (height == level && latch_mode == BTR_MODIFY_TREE) { |
| 2132 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 2133 |
/* we should sx-latch root page, if released already. |
| 2134 |
It contains seg_header. */ |
| 2135 |
if (n_releases > 0) { |
| 2136 |
mtr_block_sx_latch_at_savepoint(mtr, tree_savepoints[0], |
| 2137 |
tree_blocks[0]); |
| 2138 |
} |
| 2139 |
|
| 2140 |
/* x-latch the branch blocks not released yet. */ |
| 2141 |
for (ulint i = n_releases; i <= n_blocks; i++) { |
| 2142 |
mtr_block_x_latch_at_savepoint(mtr, tree_savepoints[i], tree_blocks[i]); |
| 2143 |
} |
| 2144 |
} |
| 2145 |
|
| 2146 |
/* Go to the child node */ |
| 2147 |
page_id.set_page_no(btr_node_ptr_get_child_page_no(node_ptr, offsets)); |
| 2148 |
|
| 2149 |
n_blocks++; |
| 2150 |
} |
| 2151 |
|
| 2152 |
if (heap) { |
| 2153 |
mem_heap_free(heap); |
| 2154 |
} |
| 2155 |
} |
| 2156 |
|
| 2157 |
/** Opens a cursor at either end of an index. |
| 2158 |
Avoid taking latches on buffer, just pin (by incrementing fix_count) |
| 2159 |
to keep them in buffer pool. This mode is used by intrinsic table |
| 2160 |
as they are not shared and so there is no need of latching. |
| 2161 |
|
| 2162 |
@param[in] from_left true if open to low end, false if open to high |
| 2163 |
end. |
| 2164 |
@param[in] index Index |
| 2165 |
@param[in,out] cursor Cursor |
| 2166 |
@param[in] level Level to search for (0=leaf) |
| 2167 |
@param[in] location Location where called |
| 2168 |
@param[in,out] mtr Mini-transaction */ |
| 2169 |
void btr_cur_open_at_index_side_with_no_latch(bool from_left, |
| 2170 |
dict_index_t *index, |
| 2171 |
btr_cur_t *cursor, ulint level, |
| 2172 |
ut::Location location, |
| 2173 |
mtr_t *mtr) { |
| 2174 |
page_cur_t *page_cursor; |
| 2175 |
ulint height; |
| 2176 |
rec_t *node_ptr; |
| 2177 |
ulint n_blocks [[maybe_unused]] = 0; |
| 2178 |
mem_heap_t *heap = nullptr; |
| 2179 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 2180 |
ulint *offsets = offsets_; |
| 2181 |
rec_offs_init(offsets_); |
| 2182 |
|
| 2183 |
ut_ad(level != ULINT_UNDEFINED); |
| 2184 |
|
| 2185 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 2186 |
cursor->index = index; |
| 2187 |
page_id_t page_id(dict_index_get_space(index), dict_index_get_page(index)); |
| 2188 |
const page_size_t &page_size = dict_table_page_size(index->table); |
| 2189 |
|
| 2190 |
height = ULINT_UNDEFINED; |
| 2191 |
|
| 2192 |
for (;;) { |
| 2193 |
buf_block_t *block; |
| 2194 |
page_t *page; |
| 2195 |
ulint rw_latch = RW_NO_LATCH; |
| 2196 |
|
| 2197 |
ut_ad(n_blocks < BTR_MAX_LEVELS); |
| 2198 |
|
| 2199 |
block = buf_page_get_gen(page_id, page_size, rw_latch, nullptr, |
| 2200 |
cursor->m_fetch_mode, location, mtr); |
| 2201 |
|
| 2202 |
page = buf_block_get_frame(block); |
| 2203 |
|
| 2204 |
ut_ad(fil_page_index_page_check(page)); |
| 2205 |
ut_ad(index->id == btr_page_get_index_id(page)); |
| 2206 |
|
| 2207 |
if (height == ULINT_UNDEFINED) { |
| 2208 |
/* We are in the root node */ |
| 2209 |
|
| 2210 |
height = btr_page_get_level(page); |
| 2211 |
ut_a(height >= level); |
| 2212 |
} else { |
| 2213 |
/* TODO: flag the index corrupted if this fails */ |
| 2214 |
ut_ad(height == btr_page_get_level(page)); |
| 2215 |
} |
| 2216 |
|
| 2217 |
if (from_left) { |
| 2218 |
page_cur_set_before_first(block, page_cursor); |
| 2219 |
} else { |
| 2220 |
page_cur_set_after_last(block, page_cursor); |
| 2221 |
} |
| 2222 |
|
| 2223 |
if (height == level) { |
| 2224 |
break; |
| 2225 |
} |
| 2226 |
|
| 2227 |
ut_ad(height > 0); |
| 2228 |
|
| 2229 |
if (from_left) { |
| 2230 |
page_cur_move_to_next(page_cursor); |
| 2231 |
} else { |
| 2232 |
page_cur_move_to_prev(page_cursor); |
| 2233 |
} |
| 2234 |
|
| 2235 |
height--; |
| 2236 |
|
| 2237 |
node_ptr = page_cur_get_rec(page_cursor); |
| 2238 |
offsets = rec_get_offsets(node_ptr, cursor->index, offsets, ULINT_UNDEFINED, |
| 2239 |
UT_LOCATION_HERE, &heap); |
| 2240 |
|
| 2241 |
/* Go to the child node */ |
| 2242 |
page_id.set_page_no(btr_node_ptr_get_child_page_no(node_ptr, offsets)); |
| 2243 |
|
| 2244 |
n_blocks++; |
| 2245 |
} |
| 2246 |
|
| 2247 |
if (heap != nullptr) { |
| 2248 |
mem_heap_free(heap); |
| 2249 |
} |
| 2250 |
} |
| 2251 |
|
| 2252 |
/** Positions a cursor at a randomly chosen position within a B-tree. |
| 2253 |
@return true if the index is available and we have put the cursor, false |
| 2254 |
if the index is unavailable */ |
| 2255 |
bool btr_cur_open_at_rnd_pos(dict_index_t *index, /*!< in: index */ |
| 2256 |
ulint latch_mode, /*!< in: BTR_SEARCH_LEAF, ... */ |
| 2257 |
btr_cur_t *cursor, /*!< in/out: B-tree cursor */ |
| 2258 |
const char *file, /*!< in: file name */ |
| 2259 |
ulint line, /*!< in: line where called */ |
| 2260 |
mtr_t *mtr) /*!< in: mtr */ |
| 2261 |
{ |
| 2262 |
page_cur_t *page_cursor; |
| 2263 |
ulint node_ptr_max_size = UNIV_PAGE_SIZE / 2; |
| 2264 |
ulint height; |
| 2265 |
rec_t *node_ptr; |
| 2266 |
ulint savepoint; |
| 2267 |
ulint upper_rw_latch, root_leaf_rw_latch; |
| 2268 |
btr_intention_t lock_intention; |
| 2269 |
buf_block_t *tree_blocks[BTR_MAX_LEVELS]; |
| 2270 |
ulint tree_savepoints[BTR_MAX_LEVELS]; |
| 2271 |
ulint n_blocks = 0; |
| 2272 |
ulint n_releases = 0; |
| 2273 |
mem_heap_t *heap = nullptr; |
| 2274 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 2275 |
ulint *offsets = offsets_; |
| 2276 |
rec_offs_init(offsets_); |
| 2277 |
|
| 2278 |
ut_ad(!dict_index_is_spatial(index)); |
| 2279 |
|
| 2280 |
lock_intention = btr_cur_get_and_clear_intention(&latch_mode); |
| 2281 |
|
| 2282 |
ut_ad(!(latch_mode & BTR_MODIFY_EXTERNAL)); |
| 2283 |
|
| 2284 |
savepoint = mtr_set_savepoint(mtr); |
| 2285 |
|
| 2286 |
switch (latch_mode) { |
| 2287 |
case BTR_MODIFY_TREE: |
| 2288 |
/* Most of delete-intended operations are purging. |
| 2289 |
Free blocks and read IO bandwidth should be prior |
| 2290 |
for them, when the history list is glowing huge. */ |
| 2291 |
if (lock_intention == BTR_INTENTION_DELETE && |
| 2292 |
trx_sys->rseg_history_len.load() > BTR_CUR_FINE_HISTORY_LENGTH && |
| 2293 |
buf_get_n_pending_read_ios()) { |
| 2294 |
mtr_x_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 2295 |
} else { |
| 2296 |
mtr_sx_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 2297 |
} |
| 2298 |
upper_rw_latch = RW_X_LATCH; |
| 2299 |
break; |
| 2300 |
case BTR_SEARCH_PREV: |
| 2301 |
case BTR_MODIFY_PREV: |
| 2302 |
/* This function doesn't support left uncle |
| 2303 |
page lock for left leaf page lock, when |
| 2304 |
needed. */ |
| 2305 |
case BTR_SEARCH_TREE: |
| 2306 |
case BTR_CONT_MODIFY_TREE: |
| 2307 |
case BTR_CONT_SEARCH_TREE: |
| 2308 |
ut_d(ut_error); |
| 2309 |
[[fallthrough]]; |
| 2310 |
default: |
| 2311 |
if (!srv_read_only_mode) { |
| 2312 |
mtr_s_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 2313 |
upper_rw_latch = RW_S_LATCH; |
| 2314 |
} else { |
| 2315 |
upper_rw_latch = RW_NO_LATCH; |
| 2316 |
} |
| 2317 |
} |
| 2318 |
|
| 2319 |
DBUG_EXECUTE_IF("test_index_is_unavailable", return (false);); |
| 2320 |
|
| 2321 |
if (index->page == FIL_NULL) { |
| 2322 |
/* Since we don't hold index lock until just now, the index |
| 2323 |
could be modified by others, for example, if this is a |
| 2324 |
statistics updater for referenced table, it could be marked |
| 2325 |
as unavailable by 'DROP TABLE' in the mean time, since |
| 2326 |
we don't hold lock for statistics updater */ |
| 2327 |
return (false); |
| 2328 |
} |
| 2329 |
|
| 2330 |
root_leaf_rw_latch = btr_cur_latch_for_root_leaf(latch_mode); |
| 2331 |
|
| 2332 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 2333 |
cursor->index = index; |
| 2334 |
|
| 2335 |
page_id_t page_id(dict_index_get_space(index), dict_index_get_page(index)); |
| 2336 |
const page_size_t &page_size = dict_table_page_size(index->table); |
| 2337 |
|
| 2338 |
if (root_leaf_rw_latch == RW_X_LATCH) { |
| 2339 |
node_ptr_max_size = dict_index_node_ptr_max_size(index); |
| 2340 |
} |
| 2341 |
|
| 2342 |
height = ULINT_UNDEFINED; |
| 2343 |
|
| 2344 |
for (;;) { |
| 2345 |
buf_block_t *block; |
| 2346 |
page_t *page; |
| 2347 |
ulint rw_latch; |
| 2348 |
|
| 2349 |
ut_ad(n_blocks < BTR_MAX_LEVELS); |
| 2350 |
|
| 2351 |
if (height != 0 && latch_mode != BTR_MODIFY_TREE) { |
| 2352 |
rw_latch = upper_rw_latch; |
| 2353 |
} else { |
| 2354 |
rw_latch = RW_NO_LATCH; |
| 2355 |
} |
| 2356 |
|
| 2357 |
tree_savepoints[n_blocks] = mtr_set_savepoint(mtr); |
| 2358 |
block = buf_page_get_gen(page_id, page_size, rw_latch, nullptr, |
| 2359 |
cursor->m_fetch_mode, {file, line}, mtr); |
| 2360 |
tree_blocks[n_blocks] = block; |
| 2361 |
|
| 2362 |
page = buf_block_get_frame(block); |
| 2363 |
|
| 2364 |
if (height == ULINT_UNDEFINED && btr_page_get_level(page) == 0 && |
| 2365 |
rw_latch != RW_NO_LATCH && rw_latch != root_leaf_rw_latch) { |
| 2366 |
/* We should retry to get the page, because the root page |
| 2367 |
is latched with different level as a leaf page. */ |
| 2368 |
ut_ad(root_leaf_rw_latch != RW_NO_LATCH); |
| 2369 |
ut_ad(rw_latch == RW_S_LATCH); |
| 2370 |
|
| 2371 |
ut_ad(n_blocks == 0); |
| 2372 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_blocks], |
| 2373 |
tree_blocks[n_blocks]); |
| 2374 |
|
| 2375 |
upper_rw_latch = root_leaf_rw_latch; |
| 2376 |
continue; |
| 2377 |
} |
| 2378 |
|
| 2379 |
ut_ad(fil_page_index_page_check(page)); |
| 2380 |
ut_ad(index->id == btr_page_get_index_id(page)); |
| 2381 |
|
| 2382 |
if (height == ULINT_UNDEFINED) { |
| 2383 |
/* We are in the root node */ |
| 2384 |
|
| 2385 |
height = btr_page_get_level(page); |
| 2386 |
} |
| 2387 |
|
| 2388 |
if (height == 0) { |
| 2389 |
if (rw_latch == RW_NO_LATCH || srv_read_only_mode) { |
| 2390 |
btr_cur_latch_leaves(block, page_id, page_size, latch_mode, cursor, |
| 2391 |
mtr); |
| 2392 |
} |
| 2393 |
|
| 2394 |
/* btr_cur_open_at_index_side() and |
| 2395 |
btr_cur_search_to_nth_level() release |
| 2396 |
tree s-latch here.*/ |
| 2397 |
switch (latch_mode) { |
| 2398 |
case BTR_MODIFY_TREE: |
| 2399 |
case BTR_CONT_MODIFY_TREE: |
| 2400 |
case BTR_CONT_SEARCH_TREE: |
| 2401 |
break; |
| 2402 |
default: |
| 2403 |
/* Release the tree s-latch */ |
| 2404 |
if (!srv_read_only_mode) { |
| 2405 |
mtr_release_s_latch_at_savepoint(mtr, savepoint, |
| 2406 |
dict_index_get_lock(index)); |
| 2407 |
} |
| 2408 |
|
| 2409 |
/* release upper blocks */ |
| 2410 |
for (; n_releases < n_blocks; n_releases++) { |
| 2411 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 2412 |
tree_blocks[n_releases]); |
| 2413 |
} |
| 2414 |
} |
| 2415 |
} |
| 2416 |
|
| 2417 |
page_cur_open_on_rnd_user_rec(block, page_cursor); |
| 2418 |
|
| 2419 |
if (height == 0) { |
| 2420 |
break; |
| 2421 |
} |
| 2422 |
|
| 2423 |
ut_ad(height > 0); |
| 2424 |
|
| 2425 |
height--; |
| 2426 |
|
| 2427 |
node_ptr = page_cur_get_rec(page_cursor); |
| 2428 |
offsets = rec_get_offsets(node_ptr, cursor->index, offsets, ULINT_UNDEFINED, |
| 2429 |
UT_LOCATION_HERE, &heap); |
| 2430 |
|
| 2431 |
/* If the rec is the first or last in the page for |
| 2432 |
pessimistic delete intention, it might cause node_ptr insert |
| 2433 |
for the upper level. We should change the intention and retry. |
| 2434 |
*/ |
| 2435 |
if (latch_mode == BTR_MODIFY_TREE && |
| 2436 |
btr_cur_need_opposite_intention(page, lock_intention, node_ptr)) { |
| 2437 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 2438 |
/* release all blocks */ |
| 2439 |
for (; n_releases <= n_blocks; n_releases++) { |
| 2440 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 2441 |
tree_blocks[n_releases]); |
| 2442 |
} |
| 2443 |
|
| 2444 |
lock_intention = BTR_INTENTION_BOTH; |
| 2445 |
|
| 2446 |
page_id.set_page_no(dict_index_get_page(index)); |
| 2447 |
|
| 2448 |
height = ULINT_UNDEFINED; |
| 2449 |
|
| 2450 |
n_blocks = 0; |
| 2451 |
n_releases = 0; |
| 2452 |
|
| 2453 |
continue; |
| 2454 |
} |
| 2455 |
|
| 2456 |
if (latch_mode == BTR_MODIFY_TREE && |
| 2457 |
!btr_cur_will_modify_tree(cursor->index, page, lock_intention, node_ptr, |
| 2458 |
node_ptr_max_size, page_size, mtr)) { |
| 2459 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 2460 |
ut_ad(n_releases <= n_blocks); |
| 2461 |
|
| 2462 |
/* we can release upper blocks */ |
| 2463 |
for (; n_releases < n_blocks; n_releases++) { |
| 2464 |
if (n_releases == 0) { |
| 2465 |
/* we should not release root page |
| 2466 |
to pin to same block. */ |
| 2467 |
continue; |
| 2468 |
} |
| 2469 |
|
| 2470 |
/* release unused blocks to unpin */ |
| 2471 |
mtr_release_block_at_savepoint(mtr, tree_savepoints[n_releases], |
| 2472 |
tree_blocks[n_releases]); |
| 2473 |
} |
| 2474 |
} |
| 2475 |
|
| 2476 |
if (height == 0 && latch_mode == BTR_MODIFY_TREE) { |
| 2477 |
ut_ad(upper_rw_latch == RW_X_LATCH); |
| 2478 |
/* we should sx-latch root page, if released already. |
| 2479 |
It contains seg_header. */ |
| 2480 |
if (n_releases > 0) { |
| 2481 |
mtr_block_sx_latch_at_savepoint(mtr, tree_savepoints[0], |
| 2482 |
tree_blocks[0]); |
| 2483 |
} |
| 2484 |
|
| 2485 |
/* x-latch the branch blocks not released yet. */ |
| 2486 |
for (ulint i = n_releases; i <= n_blocks; i++) { |
| 2487 |
mtr_block_x_latch_at_savepoint(mtr, tree_savepoints[i], tree_blocks[i]); |
| 2488 |
} |
| 2489 |
} |
| 2490 |
|
| 2491 |
/* Go to the child node */ |
| 2492 |
page_id.set_page_no(btr_node_ptr_get_child_page_no(node_ptr, offsets)); |
| 2493 |
|
| 2494 |
n_blocks++; |
| 2495 |
} |
| 2496 |
|
| 2497 |
if (UNIV_LIKELY_NULL(heap)) { |
| 2498 |
mem_heap_free(heap); |
| 2499 |
} |
| 2500 |
|
| 2501 |
return (true); |
| 2502 |
} |
| 2503 |
|
| 2504 |
/*==================== B-TREE INSERT =========================*/ |
| 2505 |
|
| 2506 |
/** Inserts a record if there is enough space, or if enough space can |
| 2507 |
be freed by reorganizing. Differs from btr_cur_optimistic_insert because |
| 2508 |
no heuristics is applied to whether it pays to use CPU time for |
| 2509 |
reorganizing the page or not. |
| 2510 |
|
| 2511 |
IMPORTANT: The caller will have to update IBUF_BITMAP_FREE |
| 2512 |
if this is a compressed leaf page in a secondary index. |
| 2513 |
This has to be done either within the same mini-transaction, |
| 2514 |
or by invoking ibuf_reset_free_bits() before mtr_commit(). |
| 2515 |
|
| 2516 |
@return pointer to inserted record if succeed, else NULL */ |
| 2517 |
[[nodiscard]] static rec_t *btr_cur_insert_if_possible( |
| 2518 |
btr_cur_t *cursor, /*!< in: cursor on page after which to insert; |
| 2519 |
cursor stays valid */ |
| 2520 |
const dtuple_t *tuple, /*!< in: tuple to insert; the size info need not |
| 2521 |
have been stored to tuple */ |
| 2522 |
ulint **offsets, /*!< out: offsets on *rec */ |
| 2523 |
mem_heap_t **heap, /*!< in/out: pointer to memory heap, or NULL */ |
| 2524 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 2525 |
{ |
| 2526 |
page_cur_t *page_cursor; |
| 2527 |
rec_t *rec; |
| 2528 |
|
| 2529 |
ut_ad(dtuple_check_typed(tuple)); |
| 2530 |
|
| 2531 |
ut_ad(mtr_is_block_fix(mtr, btr_cur_get_block(cursor), MTR_MEMO_PAGE_X_FIX, |
| 2532 |
cursor->index->table)); |
| 2533 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 2534 |
|
| 2535 |
/* Now, try the insert */ |
| 2536 |
rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, offsets, heap, |
| 2537 |
mtr); |
| 2538 |
|
| 2539 |
/* If the record did not fit, reorganize. |
| 2540 |
For compressed pages, page_cur_tuple_insert() |
| 2541 |
attempted this already. */ |
| 2542 |
if (!rec && !page_cur_get_page_zip(page_cursor) && |
| 2543 |
btr_page_reorganize(page_cursor, cursor->index, mtr)) { |
| 2544 |
rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, offsets, |
| 2545 |
heap, mtr); |
| 2546 |
} |
| 2547 |
|
| 2548 |
ut_ad(!rec || rec_offs_validate(rec, cursor->index, *offsets)); |
| 2549 |
return (rec); |
| 2550 |
} |
| 2551 |
|
| 2552 |
/** For an insert, checks the locks and does the undo logging if desired. |
| 2553 |
@return DB_SUCCESS, DB_WAIT_LOCK, DB_FAIL, or error number */ |
| 2554 |
[[nodiscard]] static inline dberr_t btr_cur_ins_lock_and_undo( |
| 2555 |
ulint flags, /*!< in: undo logging and locking flags: if |
| 2556 |
not zero, the parameters index and thr |
| 2557 |
should be specified */ |
| 2558 |
btr_cur_t *cursor, /*!< in: cursor on page after which to insert */ |
| 2559 |
dtuple_t *entry, /*!< in/out: entry to insert */ |
| 2560 |
que_thr_t *thr, /*!< in: query thread or NULL */ |
| 2561 |
mtr_t *mtr, /*!< in/out: mini-transaction */ |
| 2562 |
bool *inherit) /*!< out: true if the inserted new record maybe |
| 2563 |
should inherit LOCK_GAP type locks from the |
| 2564 |
successor record */ |
| 2565 |
{ |
| 2566 |
dict_index_t *index; |
| 2567 |
dberr_t err = DB_SUCCESS; |
| 2568 |
rec_t *rec; |
| 2569 |
roll_ptr_t roll_ptr; |
| 2570 |
|
| 2571 |
/* Check if we have to wait for a lock: enqueue an explicit lock |
| 2572 |
request if yes */ |
| 2573 |
|
| 2574 |
rec = btr_cur_get_rec(cursor); |
| 2575 |
index = cursor->index; |
| 2576 |
|
| 2577 |
ut_ad(!dict_index_is_online_ddl(index) || index->is_clustered() || |
| 2578 |
(flags & BTR_CREATE_FLAG)); |
| 2579 |
|
| 2580 |
/* Check if there is predicate or GAP lock preventing the insertion */ |
| 2581 |
if (!(flags & BTR_NO_LOCKING_FLAG)) { |
| 2582 |
if (dict_index_is_spatial(index)) { |
| 2583 |
lock_prdt_t prdt; |
| 2584 |
rtr_mbr_t mbr; |
| 2585 |
|
| 2586 |
rtr_get_mbr_from_tuple(entry, &mbr); |
| 2587 |
|
| 2588 |
/* Use on stack MBR variable to test if a lock is |
| 2589 |
needed. If so, the predicate (MBR) will be allocated |
| 2590 |
from lock heap in lock_prdt_insert_check_and_lock() */ |
| 2591 |
lock_init_prdt_from_mbr(&prdt, &mbr, 0, nullptr); |
| 2592 |
|
| 2593 |
err = lock_prdt_insert_check_and_lock( |
| 2594 |
flags, rec, btr_cur_get_block(cursor), index, thr, mtr, &prdt); |
| 2595 |
*inherit = false; |
| 2596 |
} else { |
| 2597 |
err = lock_rec_insert_check_and_lock( |
| 2598 |
flags, rec, btr_cur_get_block(cursor), index, thr, mtr, inherit); |
| 2599 |
} |
| 2600 |
} |
| 2601 |
|
| 2602 |
if (err != DB_SUCCESS || !index->is_clustered() || |
| 2603 |
dict_index_is_ibuf(index)) { |
| 2604 |
return (err); |
| 2605 |
} |
| 2606 |
|
| 2607 |
err = trx_undo_report_row_operation(flags, TRX_UNDO_INSERT_OP, thr, index, |
| 2608 |
entry, nullptr, 0, nullptr, nullptr, |
| 2609 |
&roll_ptr); |
| 2610 |
if (err != DB_SUCCESS) { |
| 2611 |
return (err); |
| 2612 |
} |
| 2613 |
|
| 2614 |
/* Now we can fill in the roll ptr field in entry |
| 2615 |
(except if table is intrinsic) */ |
| 2616 |
|
| 2617 |
if (!(flags & BTR_KEEP_SYS_FLAG) && !index->table->is_intrinsic()) { |
| 2618 |
/* Roll_ptr is zero during copy alter table. |
| 2619 |
So pretend to be freshly inserted row. */ |
| 2620 |
if (index->table->skip_alter_undo) { |
| 2621 |
ut_ad(roll_ptr == 0); |
| 2622 |
roll_ptr = trx_undo_build_roll_ptr(true, 0, 0, 0); |
| 2623 |
ut_ad(roll_ptr == (1ULL << 55)); |
| 2624 |
} |
| 2625 |
|
| 2626 |
row_upd_index_entry_sys_field(entry, index, DATA_ROLL_PTR, roll_ptr); |
| 2627 |
} |
| 2628 |
|
| 2629 |
return (DB_SUCCESS); |
| 2630 |
} |
| 2631 |
|
| 2632 |
/** |
| 2633 |
Prefetch siblings of the leaf for the pessimistic operation. |
| 2634 |
@param block leaf page */ |
| 2635 |
static void btr_cur_prefetch_siblings(buf_block_t *block) { |
| 2636 |
page_t *page = buf_block_get_frame(block); |
| 2637 |
|
| 2638 |
ut_ad(page_is_leaf(page)); |
| 2639 |
|
| 2640 |
page_no_t left_page_no = fil_page_get_prev(page); |
| 2641 |
page_no_t right_page_no = fil_page_get_next(page); |
| 2642 |
|
| 2643 |
if (left_page_no != FIL_NULL) { |
| 2644 |
buf_read_page_background(page_id_t(block->page.id.space(), left_page_no), |
| 2645 |
block->page.size, false); |
| 2646 |
} |
| 2647 |
if (right_page_no != FIL_NULL) { |
| 2648 |
buf_read_page_background(page_id_t(block->page.id.space(), right_page_no), |
| 2649 |
block->page.size, false); |
| 2650 |
} |
| 2651 |
if (left_page_no != FIL_NULL || right_page_no != FIL_NULL) { |
| 2652 |
os_aio_simulated_wake_handler_threads(); |
| 2653 |
} |
| 2654 |
} |
| 2655 |
|
| 2656 |
/** Tries to perform an insert to a page in an index tree, next to cursor. |
| 2657 |
It is assumed that mtr holds an x-latch on the page. The operation does |
| 2658 |
not succeed if there is too little space on the page. If there is just |
| 2659 |
one record on the page, the insert will always succeed; this is to |
| 2660 |
prevent trying to split a page with just one record. |
| 2661 |
@return DB_SUCCESS, DB_WAIT_LOCK, DB_FAIL, or error number */ |
| 2662 |
dberr_t btr_cur_optimistic_insert( |
| 2663 |
ulint flags, /*!< in: undo logging and locking flags: if not |
| 2664 |
zero, the parameters index and thr should be |
| 2665 |
specified */ |
| 2666 |
btr_cur_t *cursor, /*!< in: cursor on page after which to insert; |
| 2667 |
cursor stays valid */ |
| 2668 |
ulint **offsets, /*!< out: offsets on *rec */ |
| 2669 |
mem_heap_t **heap, /*!< in/out: pointer to memory heap, or NULL */ |
| 2670 |
dtuple_t *entry, /*!< in/out: entry to insert */ |
| 2671 |
rec_t **rec, /*!< out: pointer to inserted record if |
| 2672 |
succeed */ |
| 2673 |
big_rec_t **big_rec, /*!< out: big rec vector whose fields have to |
| 2674 |
be stored externally by the caller, or |
| 2675 |
NULL */ |
| 2676 |
que_thr_t *thr, /*!< in: query thread or NULL */ |
| 2677 |
mtr_t *mtr) /*!< in/out: mini-transaction; |
| 2678 |
if this function returns DB_SUCCESS on |
| 2679 |
a leaf page of a secondary index in a |
| 2680 |
compressed tablespace, the caller must |
| 2681 |
mtr_commit(mtr) before latching |
| 2682 |
any further pages */ |
| 2683 |
{ |
| 2684 |
big_rec_t *big_rec_vec = nullptr; |
| 2685 |
dict_index_t *index; |
| 2686 |
page_cur_t *page_cursor; |
| 2687 |
buf_block_t *block; |
| 2688 |
page_t *page; |
| 2689 |
rec_t *dummy; |
| 2690 |
bool reorg; |
| 2691 |
bool inherit = true; |
| 2692 |
ulint rec_size; |
| 2693 |
dberr_t err; |
| 2694 |
|
| 2695 |
*big_rec = nullptr; |
| 2696 |
|
| 2697 |
block = btr_cur_get_block(cursor); |
| 2698 |
page = buf_block_get_frame(block); |
| 2699 |
index = cursor->index; |
| 2700 |
|
| 2701 |
/* Block are not latched for insert if table is intrinsic |
| 2702 |
and index is auto-generated clustered index. */ |
| 2703 |
ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table)); |
| 2704 |
ut_ad(!dict_index_is_online_ddl(index) || index->is_clustered() || |
| 2705 |
(flags & BTR_CREATE_FLAG)); |
| 2706 |
ut_ad(dtuple_check_typed(entry)); |
| 2707 |
|
| 2708 |
const page_size_t &page_size = block->page.size; |
| 2709 |
|
| 2710 |
#ifdef UNIV_DEBUG_VALGRIND |
| 2711 |
if (page_size.is_compressed()) { |
| 2712 |
UNIV_MEM_ASSERT_RW(page, page_size.logical()); |
| 2713 |
UNIV_MEM_ASSERT_RW(block->page.zip.data, page_size.physical()); |
| 2714 |
} |
| 2715 |
#endif /* UNIV_DEBUG_VALGRIND */ |
| 2716 |
|
| 2717 |
auto leaf = page_is_leaf(page); |
| 2718 |
|
| 2719 |
/* Calculate the record size when entry is converted to a record */ |
| 2720 |
rec_size = rec_get_converted_size(index, entry); |
| 2721 |
|
| 2722 |
if (page_zip_rec_needs_ext(rec_size, page_is_comp(page), |
| 2723 |
dtuple_get_n_fields(entry), page_size)) { |
| 2724 |
/* The record is so big that we have to store some fields |
| 2725 |
externally on separate database pages */ |
| 2726 |
big_rec_vec = dtuple_convert_big_rec(index, nullptr, entry); |
| 2727 |
|
| 2728 |
if (UNIV_UNLIKELY(big_rec_vec == nullptr)) { |
| 2729 |
return (DB_TOO_BIG_RECORD); |
| 2730 |
} |
| 2731 |
|
| 2732 |
rec_size = rec_get_converted_size(index, entry); |
| 2733 |
} |
| 2734 |
|
| 2735 |
if (page_size.is_compressed() && page_zip_is_too_big(index, entry)) { |
| 2736 |
if (big_rec_vec != nullptr) { |
| 2737 |
dtuple_convert_back_big_rec(entry, big_rec_vec); |
| 2738 |
} |
| 2739 |
|
| 2740 |
return (DB_TOO_BIG_RECORD); |
| 2741 |
} |
| 2742 |
|
| 2743 |
LIMIT_OPTIMISTIC_INSERT_DEBUG(page_get_n_recs(page), goto fail); |
| 2744 |
|
| 2745 |
if (leaf && page_size.is_compressed() && |
| 2746 |
(page_get_data_size(page) + rec_size >= |
| 2747 |
dict_index_zip_pad_optimal_page_size(index))) { |
| 2748 |
/* If compression padding tells us that insertion will |
| 2749 |
result in too packed up page i.e.: which is likely to |
| 2750 |
cause compression failure then don't do an optimistic |
| 2751 |
insertion. */ |
| 2752 |
fail: |
| 2753 |
err = DB_FAIL; |
| 2754 |
|
| 2755 |
/* prefetch siblings of the leaf for the pessimistic |
| 2756 |
operation, if the page is leaf. */ |
| 2757 |
if (page_is_leaf(page)) { |
| 2758 |
btr_cur_prefetch_siblings(block); |
| 2759 |
} |
| 2760 |
fail_err: |
| 2761 |
|
| 2762 |
if (big_rec_vec) { |
| 2763 |
dtuple_convert_back_big_rec(entry, big_rec_vec); |
| 2764 |
} |
| 2765 |
|
| 2766 |
return (err); |
| 2767 |
} |
| 2768 |
|
| 2769 |
ulint max_size = page_get_max_insert_size_after_reorganize(page, 1); |
| 2770 |
|
| 2771 |
if (page_has_garbage(page)) { |
| 2772 |
if ((max_size < rec_size || max_size < BTR_CUR_PAGE_REORGANIZE_LIMIT) && |
| 2773 |
page_get_n_recs(page) > 1 && |
| 2774 |
page_get_max_insert_size(page, 1) < rec_size) { |
| 2775 |
goto fail; |
| 2776 |
} |
| 2777 |
} else if (max_size < rec_size) { |
| 2778 |
goto fail; |
| 2779 |
} |
| 2780 |
|
| 2781 |
/* If there have been many consecutive inserts to the |
| 2782 |
clustered index leaf page of an uncompressed table, check if |
| 2783 |
we have to split the page to reserve enough free space for |
| 2784 |
future updates of records. */ |
| 2785 |
|
| 2786 |
if (leaf && !page_size.is_compressed() && index->is_clustered() && |
| 2787 |
page_get_n_recs(page) >= 2 && |
| 2788 |
dict_index_get_space_reserve() + rec_size > max_size && |
| 2789 |
(btr_page_get_split_rec_to_right(cursor, &dummy) || |
| 2790 |
btr_page_get_split_rec_to_left(cursor, &dummy))) { |
| 2791 |
goto fail; |
| 2792 |
} |
| 2793 |
|
| 2794 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 2795 |
|
| 2796 |
DBUG_PRINT( |
| 2797 |
"ib_cur", |
| 2798 |
("insert %s (" IB_ID_FMT ") by " TRX_ID_FMT ": %s", index->name(), |
| 2799 |
index->id, thr != nullptr ? trx_get_id_for_print(thr_get_trx(thr)) : 0, |
| 2800 |
rec_printer(entry).str().c_str())); |
| 2801 |
|
| 2802 |
DBUG_EXECUTE_IF("do_page_reorganize", |
| 2803 |
btr_page_reorganize(page_cursor, index, mtr);); |
| 2804 |
|
| 2805 |
/* Now, try the insert */ |
| 2806 |
{ |
| 2807 |
const rec_t *page_cursor_rec = page_cur_get_rec(page_cursor); |
| 2808 |
|
| 2809 |
if (index->table->is_intrinsic()) { |
| 2810 |
*rec = page_cur_tuple_direct_insert(page_cursor, entry, index, mtr, |
| 2811 |
rec_size); |
| 2812 |
} else { |
| 2813 |
/* Check locks and write to the undo log, |
| 2814 |
if specified */ |
| 2815 |
err = btr_cur_ins_lock_and_undo(flags, cursor, entry, thr, mtr, &inherit); |
| 2816 |
|
| 2817 |
if (err != DB_SUCCESS) { |
| 2818 |
goto fail_err; |
| 2819 |
} |
| 2820 |
|
| 2821 |
DBUG_EXECUTE_IF( |
| 2822 |
"btr_ins_pause_on_mtr_redo_before_add_dirty_blocks", |
| 2823 |
ut_ad(!debug_sync_set_action( |
| 2824 |
current_thd, STRING_WITH_LEN("mtr_redo_before_add_dirty_blocks " |
| 2825 |
"SIGNAL btr_ins_paused " |
| 2826 |
"WAIT_FOR btr_ins_resume " |
| 2827 |
"NO_CLEAR_EVENT")));); |
| 2828 |
|
| 2829 |
DBUG_EXECUTE_IF( |
| 2830 |
"btr_ins_pause_on_mtr_noredo_before_add_dirty_blocks", |
| 2831 |
ut_ad(!debug_sync_set_action( |
| 2832 |
current_thd, STRING_WITH_LEN("mtr_noredo_before_add_dirty_blocks " |
| 2833 |
"SIGNAL btr_ins_paused " |
| 2834 |
"WAIT_FOR btr_ins_resume " |
| 2835 |
"NO_CLEAR_EVENT")));); |
| 2836 |
|
| 2837 |
*rec = |
| 2838 |
page_cur_tuple_insert(page_cursor, entry, index, offsets, heap, mtr); |
| 2839 |
} |
| 2840 |
|
| 2841 |
reorg = page_cursor_rec != page_cur_get_rec(page_cursor); |
| 2842 |
} |
| 2843 |
|
| 2844 |
if (*rec) { |
| 2845 |
} else if (page_size.is_compressed()) { |
| 2846 |
ut_ad(!index->table->is_temporary()); |
| 2847 |
/* Reset the IBUF_BITMAP_FREE bits, because |
| 2848 |
page_cur_tuple_insert() will have attempted page |
| 2849 |
reorganize before failing. */ |
| 2850 |
if (leaf && !index->is_clustered()) { |
| 2851 |
ibuf_reset_free_bits(block); |
| 2852 |
} |
| 2853 |
|
| 2854 |
goto fail; |
| 2855 |
} else { |
| 2856 |
/* For intrinsic table we take a consistent path |
| 2857 |
to re-organize using pessimistic path. */ |
| 2858 |
if (index->table->is_intrinsic()) { |
| 2859 |
goto fail; |
| 2860 |
} |
| 2861 |
|
| 2862 |
ut_ad(!reorg); |
| 2863 |
|
| 2864 |
/* If the record did not fit, reorganize */ |
| 2865 |
if (!btr_page_reorganize(page_cursor, index, mtr)) { |
| 2866 |
ut_d(ut_error); |
| 2867 |
ut_o(goto fail); |
| 2868 |
} |
| 2869 |
|
| 2870 |
ut_ad(page_get_max_insert_size(page, 1) == max_size); |
| 2871 |
|
| 2872 |
reorg = true; |
| 2873 |
|
| 2874 |
*rec = page_cur_tuple_insert(page_cursor, entry, index, offsets, heap, mtr); |
| 2875 |
|
| 2876 |
if (UNIV_UNLIKELY(!*rec)) { |
| 2877 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_44) |
| 2878 |
<< "Cannot insert tuple " << *entry << "into index " << index->name |
| 2879 |
<< " of table " << index->table->name << ". Max size: " << max_size; |
| 2880 |
} |
| 2881 |
} |
| 2882 |
|
| 2883 |
if (!index->disable_ahi) { |
| 2884 |
if (!reorg && leaf && (cursor->flag == BTR_CUR_HASH)) { |
| 2885 |
btr_search_update_hash_node_on_insert(cursor); |
| 2886 |
} else { |
| 2887 |
btr_search_update_hash_on_insert(cursor); |
| 2888 |
} |
| 2889 |
} |
| 2890 |
|
| 2891 |
if (!(flags & BTR_NO_LOCKING_FLAG) && inherit) { |
| 2892 |
lock_update_insert(block, *rec); |
| 2893 |
} |
| 2894 |
|
| 2895 |
if (leaf && !index->is_clustered() && !index->table->is_temporary()) { |
| 2896 |
/* Update the free bits of the B-tree page in the |
| 2897 |
insert buffer bitmap. */ |
| 2898 |
|
| 2899 |
/* The free bits in the insert buffer bitmap must |
| 2900 |
never exceed the free space on a page. It is safe to |
| 2901 |
decrement or reset the bits in the bitmap in a |
| 2902 |
mini-transaction that is committed before the |
| 2903 |
mini-transaction that affects the free space. */ |
| 2904 |
|
| 2905 |
/* It is unsafe to increment the bits in a separately |
| 2906 |
committed mini-transaction, because in crash recovery, |
| 2907 |
the free bits could momentarily be set too high. */ |
| 2908 |
|
| 2909 |
if (page_size.is_compressed()) { |
| 2910 |
/* Update the bits in the same mini-transaction. */ |
| 2911 |
ibuf_update_free_bits_zip(block, mtr); |
| 2912 |
} else { |
| 2913 |
/* Decrement the bits in a separate |
| 2914 |
mini-transaction. */ |
| 2915 |
ibuf_update_free_bits_if_full(block, max_size, |
| 2916 |
rec_size + PAGE_DIR_SLOT_SIZE); |
| 2917 |
} |
| 2918 |
} |
| 2919 |
|
| 2920 |
*big_rec = big_rec_vec; |
| 2921 |
|
| 2922 |
return (DB_SUCCESS); |
| 2923 |
} |
| 2924 |
|
| 2925 |
/** Performs an insert on a page of an index tree. It is assumed that mtr |
| 2926 |
holds an x-latch on the tree and on the cursor page. If the insert is |
| 2927 |
made on the leaf level, to avoid deadlocks, mtr must also own x-latches |
| 2928 |
to brothers of page, if those brothers exist. |
| 2929 |
@return DB_SUCCESS or error number */ |
| 2930 |
dberr_t btr_cur_pessimistic_insert( |
| 2931 |
uint32_t flags, /*!< in: undo logging and locking flags: if not |
| 2932 |
zero, the parameter thr should be |
| 2933 |
specified; if no undo logging is specified, |
| 2934 |
then the caller must have reserved enough |
| 2935 |
free extents in the file space so that the |
| 2936 |
insertion will certainly succeed */ |
| 2937 |
btr_cur_t *cursor, /*!< in: cursor after which to insert; |
| 2938 |
cursor stays valid */ |
| 2939 |
ulint **offsets, /*!< out: offsets on *rec */ |
| 2940 |
mem_heap_t **heap, /*!< in/out: pointer to memory heap |
| 2941 |
that can be emptied, or NULL */ |
| 2942 |
dtuple_t *entry, /*!< in/out: entry to insert */ |
| 2943 |
rec_t **rec, /*!< out: pointer to inserted record if |
| 2944 |
succeed */ |
| 2945 |
big_rec_t **big_rec, /*!< out: big rec vector whose fields have to |
| 2946 |
be stored externally by the caller, or |
| 2947 |
NULL */ |
| 2948 |
que_thr_t *thr, /*!< in: query thread or NULL */ |
| 2949 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 2950 |
{ |
| 2951 |
dict_index_t *index = cursor->index; |
| 2952 |
big_rec_t *big_rec_vec = nullptr; |
| 2953 |
dberr_t err; |
| 2954 |
bool inherit = false; |
| 2955 |
bool success; |
| 2956 |
ulint n_reserved = 0; |
| 2957 |
|
| 2958 |
ut_ad(dtuple_check_typed(entry)); |
| 2959 |
|
| 2960 |
*big_rec = nullptr; |
| 2961 |
|
| 2962 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(cursor->index), |
| 2963 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) || |
| 2964 |
cursor->index->table->is_intrinsic()); |
| 2965 |
ut_ad(mtr_is_block_fix(mtr, btr_cur_get_block(cursor), MTR_MEMO_PAGE_X_FIX, |
| 2966 |
cursor->index->table)); |
| 2967 |
ut_ad(!dict_index_is_online_ddl(index) || index->is_clustered() || |
| 2968 |
(flags & BTR_CREATE_FLAG)); |
| 2969 |
|
| 2970 |
cursor->flag = BTR_CUR_BINARY; |
| 2971 |
|
| 2972 |
/* Check locks and write to undo log, if specified */ |
| 2973 |
|
| 2974 |
err = btr_cur_ins_lock_and_undo(flags, cursor, entry, thr, mtr, &inherit); |
| 2975 |
|
| 2976 |
if (err != DB_SUCCESS) { |
| 2977 |
return (err); |
| 2978 |
} |
| 2979 |
|
| 2980 |
if (!(flags & BTR_NO_UNDO_LOG_FLAG) || index->table->is_intrinsic()) { |
| 2981 |
/* First reserve enough free space for the file segments |
| 2982 |
of the index tree, so that the insert will not fail because |
| 2983 |
of lack of space */ |
| 2984 |
|
| 2985 |
ulint n_extents = cursor->tree_height / 16 + 3; |
| 2986 |
|
| 2987 |
success = fsp_reserve_free_extents(&n_reserved, index->space, n_extents, |
| 2988 |
FSP_NORMAL, mtr); |
| 2989 |
if (!success) { |
| 2990 |
return (DB_OUT_OF_FILE_SPACE); |
| 2991 |
} |
| 2992 |
} |
| 2993 |
|
| 2994 |
if (page_zip_rec_needs_ext(rec_get_converted_size(index, entry), |
| 2995 |
dict_table_is_comp(index->table), |
| 2996 |
dtuple_get_n_fields(entry), |
| 2997 |
dict_table_page_size(index->table))) { |
| 2998 |
/* The record is so big that we have to store some fields |
| 2999 |
externally on separate database pages */ |
| 3000 |
|
| 3001 |
if (UNIV_LIKELY_NULL(big_rec_vec)) { |
| 3002 |
/* This should never happen, but we handle |
| 3003 |
the situation in a robust manner. */ |
| 3004 |
dtuple_convert_back_big_rec(entry, big_rec_vec); |
| 3005 |
ut_d(ut_error); |
| 3006 |
} |
| 3007 |
|
| 3008 |
big_rec_vec = dtuple_convert_big_rec(index, nullptr, entry); |
| 3009 |
|
| 3010 |
if (big_rec_vec == nullptr) { |
| 3011 |
if (n_reserved > 0) { |
| 3012 |
fil_space_release_free_extents(index->space, n_reserved); |
| 3013 |
} |
| 3014 |
return (DB_TOO_BIG_RECORD); |
| 3015 |
} |
| 3016 |
} |
| 3017 |
|
| 3018 |
if (dict_index_get_page(index) == |
| 3019 |
btr_cur_get_block(cursor)->page.id.page_no()) { |
| 3020 |
/* The page is the root page */ |
| 3021 |
*rec = btr_root_raise_and_insert(flags, cursor, offsets, heap, entry, mtr); |
| 3022 |
} else { |
| 3023 |
*rec = btr_page_split_and_insert(flags, cursor, offsets, heap, entry, mtr); |
| 3024 |
} |
| 3025 |
|
| 3026 |
if (!*rec) { |
| 3027 |
return DB_OUT_OF_FILE_SPACE; |
| 3028 |
} |
| 3029 |
|
| 3030 |
ut_ad(page_rec_get_next(btr_cur_get_rec(cursor)) == *rec || |
| 3031 |
dict_index_is_spatial(index)); |
| 3032 |
|
| 3033 |
if (!(flags & BTR_NO_LOCKING_FLAG)) { |
| 3034 |
ut_ad(!index->table->is_temporary()); |
| 3035 |
if (dict_index_is_spatial(index)) { |
| 3036 |
/* Do nothing */ |
| 3037 |
} else { |
| 3038 |
/* The cursor might be moved to the other page |
| 3039 |
and the max trx id field should be updated after |
| 3040 |
the cursor was fixed. */ |
| 3041 |
if (!index->is_clustered()) { |
| 3042 |
page_update_max_trx_id(btr_cur_get_block(cursor), |
| 3043 |
btr_cur_get_page_zip(cursor), |
| 3044 |
thr_get_trx(thr)->id, mtr); |
| 3045 |
} |
| 3046 |
if (!page_rec_is_infimum(btr_cur_get_rec(cursor)) || |
| 3047 |
btr_page_get_prev(buf_block_get_frame(btr_cur_get_block(cursor)), |
| 3048 |
mtr) == FIL_NULL) { |
| 3049 |
/* split and inserted need to call |
| 3050 |
lock_update_insert() always. */ |
| 3051 |
inherit = true; |
| 3052 |
} |
| 3053 |
} |
| 3054 |
} |
| 3055 |
|
| 3056 |
if (!index->disable_ahi) { |
| 3057 |
btr_search_update_hash_on_insert(cursor); |
| 3058 |
} |
| 3059 |
if (inherit && !(flags & BTR_NO_LOCKING_FLAG)) { |
| 3060 |
lock_update_insert(btr_cur_get_block(cursor), *rec); |
| 3061 |
} |
| 3062 |
|
| 3063 |
if (n_reserved > 0) { |
| 3064 |
fil_space_release_free_extents(index->space, n_reserved); |
| 3065 |
} |
| 3066 |
|
| 3067 |
*big_rec = big_rec_vec; |
| 3068 |
|
| 3069 |
return (DB_SUCCESS); |
| 3070 |
} |
| 3071 |
|
| 3072 |
/*==================== B-TREE UPDATE =========================*/ |
| 3073 |
|
| 3074 |
/** For an update, checks the locks and does the undo logging. |
| 3075 |
@return DB_SUCCESS, DB_WAIT_LOCK, or error number */ |
| 3076 |
[[nodiscard]] static inline dberr_t btr_cur_upd_lock_and_undo( |
| 3077 |
ulint flags, /*!< in: undo logging and locking flags */ |
| 3078 |
btr_cur_t *cursor, /*!< in: cursor on record to update */ |
| 3079 |
const ulint *offsets, /*!< in: rec_get_offsets() on cursor */ |
| 3080 |
const upd_t *update, /*!< in: update vector */ |
| 3081 |
ulint cmpl_info, /*!< in: compiler info on secondary index |
| 3082 |
updates */ |
| 3083 |
que_thr_t *thr, /*!< in: query thread |
| 3084 |
(can be NULL if BTR_NO_LOCKING_FLAG) */ |
| 3085 |
mtr_t *mtr, /*!< in/out: mini-transaction */ |
| 3086 |
roll_ptr_t *roll_ptr) /*!< out: roll pointer */ |
| 3087 |
{ |
| 3088 |
dict_index_t *index; |
| 3089 |
const rec_t *rec; |
| 3090 |
dberr_t err; |
| 3091 |
|
| 3092 |
ut_ad(thr != nullptr || (flags & BTR_NO_LOCKING_FLAG)); |
| 3093 |
|
| 3094 |
rec = btr_cur_get_rec(cursor); |
| 3095 |
index = cursor->index; |
| 3096 |
|
| 3097 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 3098 |
|
| 3099 |
if (!index->is_clustered()) { |
| 3100 |
ut_ad(dict_index_is_online_ddl(index) == !!(flags & BTR_CREATE_FLAG)); |
| 3101 |
|
| 3102 |
/* We do undo logging only when we update a clustered index |
| 3103 |
record */ |
| 3104 |
return (lock_sec_rec_modify_check_and_lock(flags, btr_cur_get_block(cursor), |
| 3105 |
rec, index, thr, mtr)); |
| 3106 |
} |
| 3107 |
|
| 3108 |
/* Check if we have to wait for a lock: enqueue an explicit lock |
| 3109 |
request if yes */ |
| 3110 |
|
| 3111 |
if (!(flags & BTR_NO_LOCKING_FLAG)) { |
| 3112 |
err = lock_clust_rec_modify_check_and_lock(flags, btr_cur_get_block(cursor), |
| 3113 |
rec, index, offsets, thr); |
| 3114 |
if (err != DB_SUCCESS) { |
| 3115 |
return (err); |
| 3116 |
} |
| 3117 |
} |
| 3118 |
|
| 3119 |
/* Append the info about the update in the undo log */ |
| 3120 |
|
| 3121 |
return (trx_undo_report_row_operation(flags, TRX_UNDO_MODIFY_OP, thr, index, |
| 3122 |
nullptr, update, cmpl_info, rec, |
| 3123 |
offsets, roll_ptr)); |
| 3124 |
} |
| 3125 |
|
| 3126 |
/** Writes a redo log record of updating a record in-place. |
| 3127 |
@param[in] flags Undo logging and locking flags |
| 3128 |
@param[in] rec Record |
| 3129 |
@param[in] index Index of the record |
| 3130 |
@param[in] update Update vector |
| 3131 |
@param[in] trx_id Transaction id |
| 3132 |
@param[in] roll_ptr Roll ptr |
| 3133 |
@param[in] mtr Mini-transaction */ |
| 3134 |
void btr_cur_update_in_place_log(ulint flags, const rec_t *rec, |
| 3135 |
dict_index_t *index, const upd_t *update, |
| 3136 |
trx_id_t trx_id, roll_ptr_t roll_ptr, |
| 3137 |
mtr_t *mtr) { |
| 3138 |
byte *log_ptr = nullptr; |
| 3139 |
ut_d(const page_t *page = page_align(rec)); |
| 3140 |
ut_ad(flags < 256); |
| 3141 |
ut_ad(page_is_comp(page) == dict_table_is_comp(index->table)); |
| 3142 |
ut_d(update->validate_for_index(index)); |
| 3143 |
|
| 3144 |
const bool opened = mlog_open_and_write_index( |
| 3145 |
mtr, rec, index, MLOG_REC_UPDATE_IN_PLACE, |
| 3146 |
1 + DATA_ROLL_PTR_LEN + 14 + 2 + MLOG_BUF_MARGIN, log_ptr); |
| 3147 |
|
| 3148 |
if (!opened) { |
| 3149 |
/* Logging in mtr is switched off during crash recovery */ |
| 3150 |
return; |
| 3151 |
} |
| 3152 |
|
| 3153 |
/* For secondary indexes, we could skip writing the dummy system fields |
| 3154 |
to the redo log but we have to change redo log parsing of |
| 3155 |
MLOG_REC_UPDATE_IN_PLACE/MLOG_COMP_REC_UPDATE_IN_PLACE or we have to add |
| 3156 |
new redo log record. For now, just write dummy sys fields to the redo |
| 3157 |
log if we are updating a secondary index record. |
| 3158 |
*/ |
| 3159 |
mach_write_to_1(log_ptr, flags); |
| 3160 |
log_ptr++; |
| 3161 |
|
| 3162 |
if (index->is_clustered()) { |
| 3163 |
log_ptr = |
| 3164 |
row_upd_write_sys_vals_to_log(index, trx_id, roll_ptr, log_ptr, mtr); |
| 3165 |
} else { |
| 3166 |
/* Dummy system fields for a secondary index */ |
| 3167 |
/* TRX_ID Position */ |
| 3168 |
log_ptr += mach_write_compressed(log_ptr, 0); |
| 3169 |
/* ROLL_PTR */ |
| 3170 |
trx_write_roll_ptr(log_ptr, 0); |
| 3171 |
log_ptr += DATA_ROLL_PTR_LEN; |
| 3172 |
/* TRX_ID */ |
| 3173 |
log_ptr += mach_u64_write_compressed(log_ptr, 0); |
| 3174 |
} |
| 3175 |
|
| 3176 |
mach_write_to_2(log_ptr, page_offset(rec)); |
| 3177 |
log_ptr += 2; |
| 3178 |
|
| 3179 |
row_upd_index_write_log(index, update, log_ptr, mtr); |
| 3180 |
} |
| 3181 |
#endif /* UNIV_HOTBACKUP */ |
| 3182 |
|
| 3183 |
/** Parses a redo log record of updating a record in-place. |
| 3184 |
@return end of log record or NULL */ |
| 3185 |
byte *btr_cur_parse_update_in_place( |
| 3186 |
byte *ptr, /*!< in: buffer */ |
| 3187 |
byte *end_ptr, /*!< in: buffer end */ |
| 3188 |
page_t *page, /*!< in/out: page or NULL */ |
| 3189 |
page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ |
| 3190 |
dict_index_t *index) /*!< in: index corresponding to page */ |
| 3191 |
{ |
| 3192 |
ulint flags; |
| 3193 |
rec_t *rec; |
| 3194 |
upd_t *update; |
| 3195 |
ulint pos; |
| 3196 |
trx_id_t trx_id; |
| 3197 |
roll_ptr_t roll_ptr; |
| 3198 |
ulint rec_offset; |
| 3199 |
mem_heap_t *heap; |
| 3200 |
ulint *offsets; |
| 3201 |
|
| 3202 |
if (end_ptr < ptr + 1) { |
| 3203 |
return (nullptr); |
| 3204 |
} |
| 3205 |
|
| 3206 |
flags = mach_read_from_1(ptr); |
| 3207 |
ptr++; |
| 3208 |
|
| 3209 |
ptr = row_upd_parse_sys_vals(ptr, end_ptr, &pos, &trx_id, &roll_ptr); |
| 3210 |
|
| 3211 |
if (ptr == nullptr) { |
| 3212 |
return (nullptr); |
| 3213 |
} |
| 3214 |
|
| 3215 |
if (end_ptr < ptr + 2) { |
| 3216 |
return (nullptr); |
| 3217 |
} |
| 3218 |
|
| 3219 |
rec_offset = mach_read_from_2(ptr); |
| 3220 |
ptr += 2; |
| 3221 |
|
| 3222 |
ut_a(rec_offset <= UNIV_PAGE_SIZE); |
| 3223 |
|
| 3224 |
heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 3225 |
|
| 3226 |
ptr = row_upd_index_parse(ptr, end_ptr, heap, &update, index); |
| 3227 |
|
| 3228 |
if (!ptr || !page) { |
| 3229 |
goto func_exit; |
| 3230 |
} |
| 3231 |
|
| 3232 |
ut_a(page_is_comp(page) == dict_table_is_comp(index->table)); |
| 3233 |
rec = page + rec_offset; |
| 3234 |
|
| 3235 |
/* We do not need to reserve search latch, as the page is only |
| 3236 |
being recovered, and there cannot be a hash index to it. */ |
| 3237 |
|
| 3238 |
offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED, |
| 3239 |
UT_LOCATION_HERE, &heap); |
| 3240 |
|
| 3241 |
if (!(flags & BTR_KEEP_SYS_FLAG)) { |
| 3242 |
row_upd_rec_sys_fields_in_recovery(rec, page_zip, offsets, pos, trx_id, |
| 3243 |
roll_ptr); |
| 3244 |
} |
| 3245 |
|
| 3246 |
row_upd_rec_in_place(rec, index, offsets, update, page_zip); |
| 3247 |
|
| 3248 |
func_exit: |
| 3249 |
mem_heap_free(heap); |
| 3250 |
|
| 3251 |
return (ptr); |
| 3252 |
} |
| 3253 |
|
| 3254 |
#ifndef UNIV_HOTBACKUP |
| 3255 |
bool btr_cur_update_alloc_zip_func(page_zip_des_t *page_zip, page_cur_t *cursor, |
| 3256 |
dict_index_t *index, |
| 3257 |
IF_DEBUG(ulint *offsets, ) ulint length, |
| 3258 |
bool create, mtr_t *mtr) { |
| 3259 |
const page_t *page = page_cur_get_page(cursor); |
| 3260 |
|
| 3261 |
ut_ad(page_zip == page_cur_get_page_zip(cursor)); |
| 3262 |
ut_ad(page_zip); |
| 3263 |
ut_ad(!dict_index_is_ibuf(index)); |
| 3264 |
ut_ad(rec_offs_validate(page_cur_get_rec(cursor), index, offsets)); |
| 3265 |
|
| 3266 |
if (page_zip_available(page_zip, index->is_clustered(), length, create)) { |
| 3267 |
return (true); |
| 3268 |
} |
| 3269 |
|
| 3270 |
if (!page_zip->m_nonempty && !page_has_garbage(page)) { |
| 3271 |
/* The page has been freshly compressed, so |
| 3272 |
reorganizing it will not help. */ |
| 3273 |
return (false); |
| 3274 |
} |
| 3275 |
|
| 3276 |
if (create && page_is_leaf(page) && |
| 3277 |
(length + page_get_data_size(page) >= |
| 3278 |
dict_index_zip_pad_optimal_page_size(index))) { |
| 3279 |
return (false); |
| 3280 |
} |
| 3281 |
|
| 3282 |
if (!btr_page_reorganize(cursor, index, mtr)) { |
| 3283 |
goto out_of_space; |
| 3284 |
} |
| 3285 |
|
| 3286 |
rec_offs_make_valid(page_cur_get_rec(cursor), index, offsets); |
| 3287 |
|
| 3288 |
/* After recompressing a page, we must make sure that the free |
| 3289 |
bits in the insert buffer bitmap will not exceed the free |
| 3290 |
space on the page. Because this function will not attempt |
| 3291 |
recompression unless page_zip_available() fails above, it is |
| 3292 |
safe to reset the free bits if page_zip_available() fails |
| 3293 |
again, below. The free bits can safely be reset in a separate |
| 3294 |
mini-transaction. If page_zip_available() succeeds below, we |
| 3295 |
can be sure that the btr_page_reorganize() above did not reduce |
| 3296 |
the free space available on the page. */ |
| 3297 |
|
| 3298 |
if (page_zip_available(page_zip, index->is_clustered(), length, create)) { |
| 3299 |
return (true); |
| 3300 |
} |
| 3301 |
|
| 3302 |
out_of_space: |
| 3303 |
ut_ad(rec_offs_validate(page_cur_get_rec(cursor), index, offsets)); |
| 3304 |
|
| 3305 |
/* Out of space: reset the free bits. */ |
| 3306 |
if (!index->is_clustered() && !index->table->is_temporary() && |
| 3307 |
page_is_leaf(page)) { |
| 3308 |
ibuf_reset_free_bits(page_cur_get_block(cursor)); |
| 3309 |
} |
| 3310 |
|
| 3311 |
return (false); |
| 3312 |
} |
| 3313 |
|
| 3314 |
/** Updates a record when the update causes no size changes in its fields. |
| 3315 |
@param[in] flags Undo logging and locking flags |
| 3316 |
@param[in] cursor Cursor on the record to update; cursor stays valid and |
| 3317 |
positioned on the same record |
| 3318 |
@param[in,out] offsets Offsets on cursor->page_cur.rec |
| 3319 |
@param[in] update Update vector |
| 3320 |
@param[in] cmpl_info Compiler info on secondary index updates |
| 3321 |
@param[in] thr Query thread, or null if flags & (btr_no_locking_flag | |
| 3322 |
btr_no_undo_log_flag | btr_create_flag | btr_keep_sys_flag) |
| 3323 |
@param[in] trx_id Transaction id |
| 3324 |
@param[in,out] mtr Mini-transaction; if this is a secondary index, the caller |
| 3325 |
must mtr_commit(mtr) before latching any further pages |
| 3326 |
@return locking or undo log related error code, or |
| 3327 |
@retval DB_SUCCESS on success |
| 3328 |
@retval DB_ZIP_OVERFLOW if there is not enough space left |
| 3329 |
on the compressed page (IBUF_BITMAP_FREE was reset outside mtr) */ |
| 3330 |
dberr_t btr_cur_update_in_place(ulint flags, btr_cur_t *cursor, ulint *offsets, |
| 3331 |
const upd_t *update, ulint cmpl_info, |
| 3332 |
que_thr_t *thr, trx_id_t trx_id, mtr_t *mtr) { |
| 3333 |
dict_index_t *index; |
| 3334 |
buf_block_t *block; |
| 3335 |
page_zip_des_t *page_zip; |
| 3336 |
dberr_t err; |
| 3337 |
rec_t *rec; |
| 3338 |
roll_ptr_t roll_ptr = 0; |
| 3339 |
bool was_delete_marked; |
| 3340 |
|
| 3341 |
rec = btr_cur_get_rec(cursor); |
| 3342 |
index = cursor->index; |
| 3343 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 3344 |
ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(index->table)); |
| 3345 |
ut_ad(trx_id > 0 || (flags & BTR_KEEP_SYS_FLAG) || |
| 3346 |
index->table->is_intrinsic()); |
| 3347 |
/* The insert buffer tree should never be updated in place. */ |
| 3348 |
ut_ad(!dict_index_is_ibuf(index)); |
| 3349 |
ut_ad(dict_index_is_online_ddl(index) == !!(flags & BTR_CREATE_FLAG) || |
| 3350 |
index->is_clustered()); |
| 3351 |
ut_ad((flags & ~(BTR_KEEP_POS_FLAG | BTR_KEEP_IBUF_BITMAP)) == |
| 3352 |
(BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_CREATE_FLAG | |
| 3353 |
BTR_KEEP_SYS_FLAG) || |
| 3354 |
thr_get_trx(thr)->id == trx_id); |
| 3355 |
ut_ad(fil_page_index_page_check(btr_cur_get_page(cursor))); |
| 3356 |
ut_ad(btr_page_get_index_id(btr_cur_get_page(cursor)) == index->id); |
| 3357 |
|
| 3358 |
DBUG_PRINT("ib_cur", |
| 3359 |
("update-in-place %s (" IB_ID_FMT ") by " TRX_ID_FMT ": %s", |
| 3360 |
index->name(), index->id, trx_id, |
| 3361 |
rec_printer(rec, offsets).str().c_str())); |
| 3362 |
|
| 3363 |
block = btr_cur_get_block(cursor); |
| 3364 |
page_zip = buf_block_get_page_zip(block); |
| 3365 |
|
| 3366 |
/* Check that enough space is available on the compressed page. */ |
| 3367 |
if (page_zip) { |
| 3368 |
ut_ad(!index->table->is_temporary()); |
| 3369 |
|
| 3370 |
if (!btr_cur_update_alloc_zip(page_zip, btr_cur_get_page_cur(cursor), index, |
| 3371 |
offsets, rec_offs_size(offsets), false, |
| 3372 |
mtr)) { |
| 3373 |
return (DB_ZIP_OVERFLOW); |
| 3374 |
} |
| 3375 |
|
| 3376 |
rec = btr_cur_get_rec(cursor); |
| 3377 |
} |
| 3378 |
|
| 3379 |
/* Do lock checking and undo logging */ |
| 3380 |
err = btr_cur_upd_lock_and_undo(flags, cursor, offsets, update, cmpl_info, |
| 3381 |
thr, mtr, &roll_ptr); |
| 3382 |
if (UNIV_UNLIKELY(err != DB_SUCCESS)) { |
| 3383 |
/* We may need to update the IBUF_BITMAP_FREE |
| 3384 |
bits after a reorganize that was done in |
| 3385 |
btr_cur_update_alloc_zip(). */ |
| 3386 |
goto func_exit; |
| 3387 |
} |
| 3388 |
|
| 3389 |
if (!(flags & BTR_KEEP_SYS_FLAG) && !index->table->is_intrinsic()) { |
| 3390 |
row_upd_rec_sys_fields(rec, nullptr, index, offsets, thr_get_trx(thr), |
| 3391 |
roll_ptr); |
| 3392 |
} |
| 3393 |
|
| 3394 |
was_delete_marked = |
| 3395 |
rec_get_deleted_flag(rec, page_is_comp(buf_block_get_frame(block))); |
| 3396 |
|
| 3397 |
if (block->ahi.index.load() != nullptr) { |
| 3398 |
/* TO DO: Can we skip this if none of the fields |
| 3399 |
index->search_info->curr_n_fields |
| 3400 |
are being updated? */ |
| 3401 |
|
| 3402 |
/* The function row_upd_changes_ord_field_binary works only |
| 3403 |
if the update vector was built for a clustered index, we must |
| 3404 |
NOT call it if index is secondary */ |
| 3405 |
|
| 3406 |
if (!index->is_clustered() || |
| 3407 |
row_upd_changes_ord_field_binary(index, update, thr, nullptr, nullptr, |
| 3408 |
nullptr)) { |
| 3409 |
/* Remove possible hash index pointer to this record */ |
| 3410 |
btr_search_update_hash_on_delete(cursor); |
| 3411 |
} |
| 3412 |
} |
| 3413 |
|
| 3414 |
block->ahi.validate(); |
| 3415 |
row_upd_rec_in_place(rec, index, offsets, update, page_zip); |
| 3416 |
|
| 3417 |
btr_cur_update_in_place_log(flags, rec, index, update, trx_id, roll_ptr, mtr); |
| 3418 |
|
| 3419 |
if (was_delete_marked && |
| 3420 |
!rec_get_deleted_flag(rec, page_is_comp(buf_block_get_frame(block)))) { |
| 3421 |
/* The new updated record owns its possible externally |
| 3422 |
stored fields */ |
| 3423 |
|
| 3424 |
lob::BtrContext btr_ctx(mtr, nullptr, index, rec, offsets, block); |
| 3425 |
btr_ctx.unmark_extern_fields(); |
| 3426 |
} |
| 3427 |
|
| 3428 |
ut_ad(err == DB_SUCCESS); |
| 3429 |
|
| 3430 |
func_exit: |
| 3431 |
if (page_zip && !(flags & BTR_KEEP_IBUF_BITMAP) && !index->is_clustered() && |
| 3432 |
page_is_leaf(buf_block_get_frame(block))) { |
| 3433 |
/* Update the free bits in the insert buffer. */ |
| 3434 |
ibuf_update_free_bits_zip(block, mtr); |
| 3435 |
} |
| 3436 |
|
| 3437 |
return (err); |
| 3438 |
} |
| 3439 |
|
| 3440 |
/** If default value of INSTANT ADD column is to be materialize in updated row. |
| 3441 |
@param[in] index record descriptor |
| 3442 |
@param[in] rec record |
| 3443 |
@return true if instant add column(s) to be materialized. */ |
| 3444 |
bool materialize_instant_default(const dict_index_t *index, const rec_t *rec) { |
| 3445 |
#ifdef UNIV_DEBUG |
| 3446 |
if (rec_new_is_versioned(rec)) { |
| 3447 |
uint8_t v = rec_get_instant_row_version_new(rec); |
| 3448 |
if (v == 0) { |
| 3449 |
ut_ad(index->has_instant_cols()); |
| 3450 |
} else { |
| 3451 |
ut_ad(index->has_row_versions()); |
| 3452 |
} |
| 3453 |
} |
| 3454 |
#endif |
| 3455 |
|
| 3456 |
/* For upgraded table with INSTANT ADD column done in previous implementaion, |
| 3457 |
don't materialize INSTANT columns for existing rows during update. The reason |
| 3458 |
is, earlier INSTANT ADD column is allowed even if after adding that column, |
| 3459 |
max size of a possible row may cross the row_size_limit. This creates an issue |
| 3460 |
with the UPDATE/ROLLBACK. Let's say during UPDATE all INSTANT ADD COLS are |
| 3461 |
materialized, updated row is fitting the row_size_limit. But if a rollback is |
| 3462 |
done and INSTANT columns are kept materialised row may exceed row_size_limit. |
| 3463 |
Thus ROLLBACK will fail. So for these rows, we keep the old behavior. |
| 3464 |
NOTE: Any new row inserted after upgrade will have all INSTANT columns |
| 3465 |
materialized and have version=0. |
| 3466 |
NOTE: In new implementation an INSTANT ADD/DROP will fail if max possible size |
| 3467 |
of a row crosses row_size_limit. So new rows with materialized INSTANT columns |
| 3468 |
will always be within row_size_limit. |
| 3469 |
Thus the above mentioned issue isn't there for rows inserted post upgrade. */ |
| 3470 |
|
| 3471 |
/* If the record is versioned, the old instant add defaults must already have |
| 3472 |
been materialized. */ |
| 3473 |
if (rec_new_is_versioned(rec)) { |
| 3474 |
return true; |
| 3475 |
} |
| 3476 |
|
| 3477 |
/* If table has version (i.e. INSTANT ADD/DROP is done), this is gauranteed |
| 3478 |
that the maximum row will fit within the limit or else INSTANT ADD/DROP would |
| 3479 |
have failed. So it is safe to materialize it. */ |
| 3480 |
if (index->has_row_versions()) { |
| 3481 |
return true; |
| 3482 |
} |
| 3483 |
|
| 3484 |
/* The record is not versioned. If the index has instant default from older |
| 3485 |
version, we must not materialize it. */ |
| 3486 |
if (index->has_instant_cols()) { |
| 3487 |
return false; |
| 3488 |
} |
| 3489 |
|
| 3490 |
/* The record is not versioned and the index doesn't have instant default from |
| 3491 |
older version. It is safe to materialize it. */ |
| 3492 |
return true; |
| 3493 |
} |
| 3494 |
|
| 3495 |
dberr_t btr_cur_optimistic_update(ulint flags, btr_cur_t *cursor, |
| 3496 |
ulint **offsets, mem_heap_t **heap, |
| 3497 |
const upd_t *update, ulint cmpl_info, |
| 3498 |
que_thr_t *thr, trx_id_t trx_id, mtr_t *mtr) { |
| 3499 |
dict_index_t *index; |
| 3500 |
page_cur_t *page_cursor; |
| 3501 |
dberr_t err; |
| 3502 |
buf_block_t *block; |
| 3503 |
page_t *page; |
| 3504 |
page_zip_des_t *page_zip; |
| 3505 |
ulint max_size; |
| 3506 |
ulint new_rec_size; |
| 3507 |
ulint old_rec_size; |
| 3508 |
ulint max_ins_size = 0; |
| 3509 |
dtuple_t *new_entry; |
| 3510 |
roll_ptr_t roll_ptr; |
| 3511 |
ulint i; |
| 3512 |
|
| 3513 |
block = btr_cur_get_block(cursor); |
| 3514 |
page = buf_block_get_frame(block); |
| 3515 |
rec_t *rec = btr_cur_get_rec(cursor); |
| 3516 |
index = cursor->index; |
| 3517 |
ut_ad(trx_id > 0 || (flags & BTR_KEEP_SYS_FLAG) || |
| 3518 |
index->table->is_intrinsic()); |
| 3519 |
ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(index->table)); |
| 3520 |
ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table)); |
| 3521 |
/* This is intended only for leaf page updates */ |
| 3522 |
ut_ad(page_is_leaf(page)); |
| 3523 |
/* The insert buffer tree should never be updated in place. */ |
| 3524 |
ut_ad(!dict_index_is_ibuf(index)); |
| 3525 |
ut_ad(dict_index_is_online_ddl(index) == !!(flags & BTR_CREATE_FLAG) || |
| 3526 |
index->is_clustered()); |
| 3527 |
ut_ad((flags & ~(BTR_KEEP_POS_FLAG | BTR_KEEP_IBUF_BITMAP)) == |
| 3528 |
(BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_CREATE_FLAG | |
| 3529 |
BTR_KEEP_SYS_FLAG) || |
| 3530 |
thr_get_trx(thr)->id == trx_id); |
| 3531 |
ut_ad(fil_page_index_page_check(page)); |
| 3532 |
ut_ad(btr_page_get_index_id(page) == index->id); |
| 3533 |
ut_ad(update); |
| 3534 |
ut_d(update->validate_for_index(index)); |
| 3535 |
|
| 3536 |
DBUG_EXECUTE_IF("DB_ZIP_OVERFLOW_on_btr_cur_optimistic_update", |
| 3537 |
return (DB_ZIP_OVERFLOW);); |
| 3538 |
|
| 3539 |
*offsets = rec_get_offsets(rec, index, *offsets, ULINT_UNDEFINED, |
| 3540 |
UT_LOCATION_HERE, heap); |
| 3541 |
#if defined UNIV_DEBUG || defined UNIV_BLOB_LIGHT_DEBUG |
| 3542 |
ut_a(!rec_offs_any_null_extern(index, rec, *offsets) || |
| 3543 |
(flags & ~(BTR_KEEP_POS_FLAG | BTR_KEEP_IBUF_BITMAP)) == |
| 3544 |
(BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_CREATE_FLAG | |
| 3545 |
BTR_KEEP_SYS_FLAG) || |
| 3546 |
trx_is_recv(thr_get_trx(thr))); |
| 3547 |
#endif /* UNIV_DEBUG || UNIV_BLOB_LIGHT_DEBUG */ |
| 3548 |
|
| 3549 |
if (!row_upd_changes_field_size_or_external(index, *offsets, update)) { |
| 3550 |
/* The simplest and the most common case: the update does not |
| 3551 |
change the size of any field and none of the updated fields is |
| 3552 |
externally stored in rec or update, and there is enough space |
| 3553 |
on the compressed page to log the update. */ |
| 3554 |
|
| 3555 |
return (btr_cur_update_in_place(flags, cursor, *offsets, update, cmpl_info, |
| 3556 |
thr, trx_id, mtr)); |
| 3557 |
} |
| 3558 |
|
| 3559 |
if (rec_offs_any_extern(*offsets)) { |
| 3560 |
any_extern: |
| 3561 |
/* Externally stored fields are treated in pessimistic |
| 3562 |
update */ |
| 3563 |
|
| 3564 |
/* prefetch siblings of the leaf for the pessimistic |
| 3565 |
operation. */ |
| 3566 |
btr_cur_prefetch_siblings(block); |
| 3567 |
|
| 3568 |
return (DB_OVERFLOW); |
| 3569 |
} |
| 3570 |
|
| 3571 |
for (i = 0; i < upd_get_n_fields(update); i++) { |
| 3572 |
if (dfield_is_ext(&upd_get_nth_field(update, i)->new_val)) { |
| 3573 |
goto any_extern; |
| 3574 |
} |
| 3575 |
} |
| 3576 |
|
| 3577 |
DBUG_PRINT("ib_cur", |
| 3578 |
("update %s (" IB_ID_FMT ") by " TRX_ID_FMT ": %s", index->name(), |
| 3579 |
index->id, trx_id, rec_printer(rec, *offsets).str().c_str())); |
| 3580 |
|
| 3581 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 3582 |
|
| 3583 |
if (!*heap) { |
| 3584 |
*heap = mem_heap_create( |
| 3585 |
rec_offs_size(*offsets) + DTUPLE_EST_ALLOC(rec_offs_n_fields(*offsets)), |
| 3586 |
UT_LOCATION_HERE); |
| 3587 |
} |
| 3588 |
|
| 3589 |
new_entry = row_rec_to_index_entry(rec, index, *offsets, *heap); |
| 3590 |
/* We checked above that there are no externally stored fields. */ |
| 3591 |
ut_a(!new_entry->has_ext()); |
| 3592 |
|
| 3593 |
/* The page containing the clustered index record |
| 3594 |
corresponding to new_entry is latched in mtr. |
| 3595 |
Thus the following call is safe. */ |
| 3596 |
row_upd_index_replace_new_col_vals_index_pos(new_entry, index, update, false, |
| 3597 |
*heap); |
| 3598 |
|
| 3599 |
if (!materialize_instant_default(index, rec)) { |
| 3600 |
new_entry->ignore_trailing_default(index); |
| 3601 |
} |
| 3602 |
|
| 3603 |
old_rec_size = rec_offs_size(*offsets); |
| 3604 |
new_rec_size = rec_get_converted_size(index, new_entry); |
| 3605 |
|
| 3606 |
page_zip = buf_block_get_page_zip(block); |
| 3607 |
#ifdef UNIV_ZIP_DEBUG |
| 3608 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 3609 |
#endif /* UNIV_ZIP_DEBUG */ |
| 3610 |
|
| 3611 |
if (page_zip) { |
| 3612 |
ut_ad(!index->table->is_temporary()); |
| 3613 |
|
| 3614 |
if (!btr_cur_update_alloc_zip(page_zip, page_cursor, index, *offsets, |
| 3615 |
new_rec_size, true, mtr)) { |
| 3616 |
return (DB_ZIP_OVERFLOW); |
| 3617 |
} |
| 3618 |
|
| 3619 |
rec = page_cur_get_rec(page_cursor); |
| 3620 |
} |
| 3621 |
|
| 3622 |
/* We limit max record size to 16k even for 64k page size. */ |
| 3623 |
if (new_rec_size >= REC_MAX_DATA_SIZE) { |
| 3624 |
err = DB_OVERFLOW; |
| 3625 |
|
| 3626 |
goto func_exit; |
| 3627 |
} |
| 3628 |
|
| 3629 |
if (UNIV_UNLIKELY(new_rec_size >= |
| 3630 |
(page_get_free_space_of_empty(page_is_comp(page)) / 2))) { |
| 3631 |
/* We may need to update the IBUF_BITMAP_FREE |
| 3632 |
bits after a reorganize that was done in |
| 3633 |
btr_cur_update_alloc_zip(). */ |
| 3634 |
err = DB_OVERFLOW; |
| 3635 |
goto func_exit; |
| 3636 |
} |
| 3637 |
|
| 3638 |
if (UNIV_UNLIKELY(page_get_data_size(page) - old_rec_size + new_rec_size < |
| 3639 |
BTR_CUR_PAGE_COMPRESS_LIMIT(index))) { |
| 3640 |
/* We may need to update the IBUF_BITMAP_FREE |
| 3641 |
bits after a reorganize that was done in |
| 3642 |
btr_cur_update_alloc_zip(). */ |
| 3643 |
|
| 3644 |
/* The page would become too empty */ |
| 3645 |
err = DB_UNDERFLOW; |
| 3646 |
goto func_exit; |
| 3647 |
} |
| 3648 |
|
| 3649 |
/* We do not attempt to reorganize if the page is compressed. |
| 3650 |
This is because the page may fail to compress after reorganization. */ |
| 3651 |
max_size = |
| 3652 |
page_zip |
| 3653 |
? page_get_max_insert_size(page, 1) |
| 3654 |
: (old_rec_size + page_get_max_insert_size_after_reorganize(page, 1)); |
| 3655 |
|
| 3656 |
if (!page_zip) { |
| 3657 |
max_ins_size = page_get_max_insert_size_after_reorganize(page, 1); |
| 3658 |
} |
| 3659 |
|
| 3660 |
if (!(((max_size >= BTR_CUR_PAGE_REORGANIZE_LIMIT) && |
| 3661 |
(max_size >= new_rec_size)) || |
| 3662 |
(page_get_n_recs(page) <= 1))) { |
| 3663 |
/* We may need to update the IBUF_BITMAP_FREE |
| 3664 |
bits after a reorganize that was done in |
| 3665 |
btr_cur_update_alloc_zip(). */ |
| 3666 |
|
| 3667 |
/* There was not enough space, or it did not pay to |
| 3668 |
reorganize: for simplicity, we decide what to do assuming a |
| 3669 |
reorganization is needed, though it might not be necessary */ |
| 3670 |
|
| 3671 |
err = DB_OVERFLOW; |
| 3672 |
goto func_exit; |
| 3673 |
} |
| 3674 |
|
| 3675 |
/* Do lock checking and undo logging */ |
| 3676 |
err = btr_cur_upd_lock_and_undo(flags, cursor, *offsets, update, cmpl_info, |
| 3677 |
thr, mtr, &roll_ptr); |
| 3678 |
if (err != DB_SUCCESS) { |
| 3679 |
/* We may need to update the IBUF_BITMAP_FREE |
| 3680 |
bits after a reorganize that was done in |
| 3681 |
btr_cur_update_alloc_zip(). */ |
| 3682 |
goto func_exit; |
| 3683 |
} |
| 3684 |
|
| 3685 |
/* Ok, we may do the replacement. Store on the page infimum the |
| 3686 |
explicit locks on rec, before deleting rec (see the comment in |
| 3687 |
btr_cur_pessimistic_update). */ |
| 3688 |
if (!dict_table_is_locking_disabled(index->table)) { |
| 3689 |
lock_rec_store_on_page_infimum(block, rec); |
| 3690 |
} |
| 3691 |
|
| 3692 |
btr_search_update_hash_on_delete(cursor); |
| 3693 |
|
| 3694 |
page_cur_delete_rec(page_cursor, index, *offsets, mtr); |
| 3695 |
|
| 3696 |
page_cur_move_to_prev(page_cursor); |
| 3697 |
|
| 3698 |
if (!(flags & BTR_KEEP_SYS_FLAG) && !index->table->is_intrinsic()) { |
| 3699 |
row_upd_index_entry_sys_field(new_entry, index, DATA_ROLL_PTR, roll_ptr); |
| 3700 |
row_upd_index_entry_sys_field(new_entry, index, DATA_TRX_ID, trx_id); |
| 3701 |
} |
| 3702 |
|
| 3703 |
/* There are no externally stored columns in new_entry */ |
| 3704 |
rec = btr_cur_insert_if_possible(cursor, new_entry, offsets, heap, mtr); |
| 3705 |
ut_a(rec); /* <- We calculated above the insert would fit */ |
| 3706 |
|
| 3707 |
/* Restore the old explicit lock state on the record */ |
| 3708 |
if (!dict_table_is_locking_disabled(index->table)) { |
| 3709 |
lock_rec_restore_from_page_infimum(block, rec, block); |
| 3710 |
} |
| 3711 |
|
| 3712 |
page_cur_move_to_next(page_cursor); |
| 3713 |
ut_ad(err == DB_SUCCESS); |
| 3714 |
|
| 3715 |
func_exit: |
| 3716 |
if (!(flags & BTR_KEEP_IBUF_BITMAP) && !index->is_clustered() && |
| 3717 |
!index->table->is_temporary()) { |
| 3718 |
/* Update the free bits in the insert buffer. */ |
| 3719 |
if (page_zip) { |
| 3720 |
ibuf_update_free_bits_zip(block, mtr); |
| 3721 |
} else { |
| 3722 |
ibuf_update_free_bits_low(block, max_ins_size, mtr); |
| 3723 |
} |
| 3724 |
} |
| 3725 |
|
| 3726 |
if (err != DB_SUCCESS) { |
| 3727 |
/* prefetch siblings of the leaf for the pessimistic |
| 3728 |
operation. */ |
| 3729 |
btr_cur_prefetch_siblings(block); |
| 3730 |
} |
| 3731 |
|
| 3732 |
ut_d(update->validate_for_index(index)); |
| 3733 |
return err; |
| 3734 |
} |
| 3735 |
|
| 3736 |
/** If, in a split, a new supremum record was created as the predecessor of the |
| 3737 |
updated record, the supremum record must inherit exactly the locks on the |
| 3738 |
updated record. In the split it may have inherited locks from the successor |
| 3739 |
of the updated record, which is not correct. This function restores the |
| 3740 |
right locks for the new supremum. */ |
| 3741 |
static void btr_cur_pess_upd_restore_supremum( |
| 3742 |
buf_block_t *block, /*!< in: buffer block of rec */ |
| 3743 |
const rec_t *rec, /*!< in: updated record */ |
| 3744 |
mtr_t *mtr) /*!< in: mtr */ |
| 3745 |
{ |
| 3746 |
page_t *page; |
| 3747 |
buf_block_t *prev_block; |
| 3748 |
|
| 3749 |
page = buf_block_get_frame(block); |
| 3750 |
|
| 3751 |
if (page_rec_get_next(page_get_infimum_rec(page)) != rec) { |
| 3752 |
/* Updated record is not the first user record on its page */ |
| 3753 |
|
| 3754 |
return; |
| 3755 |
} |
| 3756 |
|
| 3757 |
const page_no_t prev_page_no = btr_page_get_prev(page, mtr); |
| 3758 |
|
| 3759 |
const page_id_t page_id(block->page.id.space(), prev_page_no); |
| 3760 |
|
| 3761 |
ut_ad(prev_page_no != FIL_NULL); |
| 3762 |
prev_block = buf_page_get_with_no_latch(page_id, block->page.size, |
| 3763 |
UT_LOCATION_HERE, mtr); |
| 3764 |
#ifdef UNIV_BTR_DEBUG |
| 3765 |
ut_a(btr_page_get_next(prev_block->frame, mtr) == page_get_page_no(page)); |
| 3766 |
#endif /* UNIV_BTR_DEBUG */ |
| 3767 |
|
| 3768 |
/* We must already have an x-latch on prev_block! */ |
| 3769 |
ut_ad(mtr_memo_contains(mtr, prev_block, MTR_MEMO_PAGE_X_FIX)); |
| 3770 |
|
| 3771 |
lock_rec_reset_and_inherit_gap_locks(prev_block, block, PAGE_HEAP_NO_SUPREMUM, |
| 3772 |
page_rec_get_heap_no(rec)); |
| 3773 |
} |
| 3774 |
|
| 3775 |
dberr_t btr_cur_pessimistic_update(ulint flags, btr_cur_t *cursor, |
| 3776 |
ulint **offsets, mem_heap_t **offsets_heap, |
| 3777 |
mem_heap_t *entry_heap, big_rec_t **big_rec, |
| 3778 |
upd_t *update, ulint cmpl_info, |
| 3779 |
que_thr_t *thr, trx_id_t trx_id, |
| 3780 |
undo_no_t undo_no, mtr_t *mtr, |
| 3781 |
btr_pcur_t *pcur) { |
| 3782 |
DBUG_TRACE; |
| 3783 |
big_rec_t *big_rec_vec = nullptr; |
| 3784 |
big_rec_t *dummy_big_rec; |
| 3785 |
dict_index_t *index; |
| 3786 |
buf_block_t *block; |
| 3787 |
page_t *page; |
| 3788 |
page_zip_des_t *page_zip; |
| 3789 |
rec_t *rec; |
| 3790 |
page_cur_t *page_cursor; |
| 3791 |
dberr_t err; |
| 3792 |
dberr_t optim_err; |
| 3793 |
roll_ptr_t roll_ptr; |
| 3794 |
bool was_first; |
| 3795 |
ulint n_reserved = 0; |
| 3796 |
ulint max_ins_size = 0; |
| 3797 |
trx_t *const trx = (thr == nullptr) ? nullptr : thr_get_trx(thr); |
| 3798 |
|
| 3799 |
*offsets = nullptr; |
| 3800 |
*big_rec = nullptr; |
| 3801 |
|
| 3802 |
block = btr_cur_get_block(cursor); |
| 3803 |
page = buf_block_get_frame(block); |
| 3804 |
page_zip = buf_block_get_page_zip(block); |
| 3805 |
index = cursor->index; |
| 3806 |
|
| 3807 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 3808 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) || |
| 3809 |
index->table->is_intrinsic()); |
| 3810 |
ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table)); |
| 3811 |
#ifdef UNIV_ZIP_DEBUG |
| 3812 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 3813 |
#endif /* UNIV_ZIP_DEBUG */ |
| 3814 |
ut_ad(!page_zip || !index->table->is_temporary()); |
| 3815 |
/* The insert buffer tree should never be updated in place. */ |
| 3816 |
ut_ad(!dict_index_is_ibuf(index)); |
| 3817 |
ut_ad(trx_id > 0 || (flags & BTR_KEEP_SYS_FLAG) || |
| 3818 |
index->table->is_intrinsic()); |
| 3819 |
ut_ad(dict_index_is_online_ddl(index) == !!(flags & BTR_CREATE_FLAG) || |
| 3820 |
index->is_clustered()); |
| 3821 |
ut_ad((flags & ~BTR_KEEP_POS_FLAG) == |
| 3822 |
(BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_CREATE_FLAG | |
| 3823 |
BTR_KEEP_SYS_FLAG) || |
| 3824 |
thr_get_trx(thr)->id == trx_id); |
| 3825 |
ut_d(update->validate_for_index(index)); |
| 3826 |
|
| 3827 |
err = optim_err = btr_cur_optimistic_update( |
| 3828 |
flags | BTR_KEEP_IBUF_BITMAP, cursor, offsets, offsets_heap, update, |
| 3829 |
cmpl_info, thr, trx_id, mtr); |
| 3830 |
|
| 3831 |
switch (err) { |
| 3832 |
case DB_ZIP_OVERFLOW: |
| 3833 |
case DB_UNDERFLOW: |
| 3834 |
case DB_OVERFLOW: |
| 3835 |
break; |
| 3836 |
default: |
| 3837 |
err_exit: |
| 3838 |
/* We suppressed this with BTR_KEEP_IBUF_BITMAP. |
| 3839 |
For DB_ZIP_OVERFLOW, the IBUF_BITMAP_FREE bits were |
| 3840 |
already reset by btr_cur_update_alloc_zip() if the |
| 3841 |
page was recompressed. */ |
| 3842 |
if (page_zip && optim_err != DB_ZIP_OVERFLOW && !index->is_clustered() && |
| 3843 |
page_is_leaf(page)) { |
| 3844 |
ut_ad(!index->table->is_temporary()); |
| 3845 |
ibuf_update_free_bits_zip(block, mtr); |
| 3846 |
} |
| 3847 |
|
| 3848 |
if (big_rec_vec != nullptr) { |
| 3849 |
dtuple_big_rec_free(big_rec_vec); |
| 3850 |
} |
| 3851 |
|
| 3852 |
return err; |
| 3853 |
} |
| 3854 |
|
| 3855 |
rec = btr_cur_get_rec(cursor); |
| 3856 |
|
| 3857 |
*offsets = rec_get_offsets(rec, index, *offsets, ULINT_UNDEFINED, |
| 3858 |
UT_LOCATION_HERE, offsets_heap); |
| 3859 |
|
| 3860 |
dtuple_t *new_entry = |
| 3861 |
row_rec_to_index_entry(rec, index, *offsets, entry_heap); |
| 3862 |
|
| 3863 |
/* The page containing the clustered index record |
| 3864 |
corresponding to new_entry is latched in mtr. If the |
| 3865 |
clustered index record is delete-marked, then its externally |
| 3866 |
stored fields cannot have been purged yet, because then the |
| 3867 |
purge would also have removed the clustered index record |
| 3868 |
itself. Thus the following call is safe. */ |
| 3869 |
row_upd_index_replace_new_col_vals_index_pos(new_entry, index, update, false, |
| 3870 |
entry_heap); |
| 3871 |
|
| 3872 |
if (!materialize_instant_default(index, rec)) { |
| 3873 |
new_entry->ignore_trailing_default(index); |
| 3874 |
} |
| 3875 |
|
| 3876 |
/* We have to set appropriate extern storage bits in the new |
| 3877 |
record to be inserted: we have to remember which fields were such */ |
| 3878 |
|
| 3879 |
ut_ad(!page_is_comp(page) || !rec_get_node_ptr_flag(rec)); |
| 3880 |
ut_ad(rec_offs_validate(rec, index, *offsets)); |
| 3881 |
|
| 3882 |
if (page_zip_rec_needs_ext(rec_get_converted_size(index, new_entry), |
| 3883 |
page_is_comp(page), dict_index_get_n_fields(index), |
| 3884 |
block->page.size)) { |
| 3885 |
big_rec_vec = dtuple_convert_big_rec(index, update, new_entry); |
| 3886 |
if (UNIV_UNLIKELY(big_rec_vec == nullptr)) { |
| 3887 |
/* We cannot goto return_after_reservations, |
| 3888 |
because we may need to update the |
| 3889 |
IBUF_BITMAP_FREE bits, which was suppressed by |
| 3890 |
BTR_KEEP_IBUF_BITMAP. */ |
| 3891 |
#ifdef UNIV_ZIP_DEBUG |
| 3892 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 3893 |
#endif /* UNIV_ZIP_DEBUG */ |
| 3894 |
if (n_reserved > 0) { |
| 3895 |
fil_space_release_free_extents(index->space, n_reserved); |
| 3896 |
} |
| 3897 |
|
| 3898 |
err = DB_TOO_BIG_RECORD; |
| 3899 |
goto err_exit; |
| 3900 |
} |
| 3901 |
|
| 3902 |
ut_ad(page_is_leaf(page)); |
| 3903 |
ut_ad(index->is_clustered()); |
| 3904 |
ut_ad(flags & BTR_KEEP_POS_FLAG); |
| 3905 |
} |
| 3906 |
|
| 3907 |
/* Do lock checking and undo logging */ |
| 3908 |
err = btr_cur_upd_lock_and_undo(flags, cursor, *offsets, update, cmpl_info, |
| 3909 |
thr, mtr, &roll_ptr); |
| 3910 |
if (err != DB_SUCCESS) { |
| 3911 |
goto err_exit; |
| 3912 |
} |
| 3913 |
|
| 3914 |
if (optim_err == DB_OVERFLOW) { |
| 3915 |
/* We latch the space before latching any lob pages, to avoid deadlock with |
| 3916 |
threads which first latch the space to acquire a free page, which might be |
| 3917 |
one of the pages which we are about to free, but still hold an x-latch on.*/ |
| 3918 |
fil_space_t *space = fil_space_get(index->space); |
| 3919 |
mtr_x_lock_space(space, mtr); |
| 3920 |
} |
| 3921 |
|
| 3922 |
/* Check for an update that moved an ext field to inline */ |
| 3923 |
lob::mark_not_partially_updatable(trx, index, update, mtr); |
| 3924 |
|
| 3925 |
/* UNDO logging is also turned-off during normal operation on intrinsic |
| 3926 |
table so condition needs to ensure that table is not intrinsic. */ |
| 3927 |
if ((flags & BTR_NO_UNDO_LOG_FLAG) && rec_offs_any_extern(*offsets) && |
| 3928 |
!index->table->is_intrinsic()) { |
| 3929 |
/* We are in a transaction rollback undoing a row |
| 3930 |
update: we must free possible externally stored fields |
| 3931 |
which got new values in the update, if they are not |
| 3932 |
inherited values. They can be inherited if we have |
| 3933 |
updated the primary key to another value, and then |
| 3934 |
update it back again. */ |
| 3935 |
|
| 3936 |
/* During rollback of a record in table having INSTANT fields, it is |
| 3937 |
possible to have an external field now which wasn't there before update */ |
| 3938 |
ut_ad(big_rec_vec == nullptr || materialize_instant_default(index, rec)); |
| 3939 |
|
| 3940 |
ut_ad(index->is_clustered()); |
| 3941 |
ut_ad((flags & ~BTR_KEEP_POS_FLAG) == |
| 3942 |
(BTR_NO_LOCKING_FLAG | BTR_CREATE_FLAG | BTR_KEEP_SYS_FLAG) || |
| 3943 |
thr_get_trx(thr)->id == trx_id); |
| 3944 |
|
| 3945 |
DBUG_EXECUTE_IF("ib_blob_update_rollback", DBUG_SUICIDE();); |
| 3946 |
RECOVERY_CRASH(99); |
| 3947 |
|
| 3948 |
lob::BtrContext ctx(mtr, pcur, index, rec, *offsets, block); |
| 3949 |
|
| 3950 |
ctx.free_updated_extern_fields(trx_id, undo_no, update, true, big_rec_vec); |
| 3951 |
|
| 3952 |
/* The cursor position could have changed because of the call to |
| 3953 |
lob::purge() above. */ |
| 3954 |
if (rec != pcur->get_rec()) { |
| 3955 |
cursor = pcur->get_btr_cur(); |
| 3956 |
block = btr_cur_get_block(cursor); |
| 3957 |
page = buf_block_get_frame(block); |
| 3958 |
page_zip = buf_block_get_page_zip(block); |
| 3959 |
rec = pcur->get_rec(); |
| 3960 |
rec_offs_make_valid(rec, index, *offsets); |
| 3961 |
} |
| 3962 |
|
| 3963 |
ut_ad(block == pcur->get_block()); |
| 3964 |
ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table)); |
| 3965 |
ut_ad(rw_lock_own(&(((buf_block_t *)block)->lock), RW_LOCK_X) || |
| 3966 |
rw_lock_own(&(((buf_block_t *)block)->lock), RW_LOCK_S)); |
| 3967 |
} |
| 3968 |
|
| 3969 |
if (optim_err == DB_OVERFLOW) { |
| 3970 |
/* First reserve enough free space for the file segments |
| 3971 |
of the index tree, so that the update will not fail because |
| 3972 |
of lack of space */ |
| 3973 |
DEBUG_SYNC_C("ib_blob_update_rollback_will_reserve"); |
| 3974 |
|
| 3975 |
ulint n_extents = cursor->tree_height / 16 + 3; |
| 3976 |
|
| 3977 |
if (!fsp_reserve_free_extents( |
| 3978 |
&n_reserved, index->space, n_extents, |
| 3979 |
flags & BTR_NO_UNDO_LOG_FLAG ? FSP_CLEANING : FSP_NORMAL, mtr)) { |
| 3980 |
err = DB_OUT_OF_FILE_SPACE; |
| 3981 |
goto err_exit; |
| 3982 |
} |
| 3983 |
} |
| 3984 |
|
| 3985 |
if (!(flags & BTR_KEEP_SYS_FLAG) && !index->table->is_intrinsic()) { |
| 3986 |
row_upd_index_entry_sys_field(new_entry, index, DATA_ROLL_PTR, roll_ptr); |
| 3987 |
row_upd_index_entry_sys_field(new_entry, index, DATA_TRX_ID, trx_id); |
| 3988 |
} |
| 3989 |
|
| 3990 |
if (!page_zip) { |
| 3991 |
max_ins_size = page_get_max_insert_size_after_reorganize(page, 1); |
| 3992 |
} |
| 3993 |
|
| 3994 |
/* Store state of explicit locks on rec on the page infimum record, |
| 3995 |
before deleting rec. The page infimum acts as a dummy carrier of the |
| 3996 |
locks, taking care also of lock releases, before we can move the locks |
| 3997 |
back on the actual record. There is a special case: if we are |
| 3998 |
inserting on the root page and the insert causes a call of |
| 3999 |
btr_root_raise_and_insert. Therefore we cannot in the lock system |
| 4000 |
delete the lock structs set on the root page even if the root |
| 4001 |
page carries just node pointers. */ |
| 4002 |
if (!dict_table_is_locking_disabled(index->table)) { |
| 4003 |
lock_rec_store_on_page_infimum(block, rec); |
| 4004 |
} |
| 4005 |
|
| 4006 |
btr_search_update_hash_on_delete(cursor); |
| 4007 |
|
| 4008 |
#ifdef UNIV_ZIP_DEBUG |
| 4009 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 4010 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4011 |
page_cursor = btr_cur_get_page_cur(cursor); |
| 4012 |
|
| 4013 |
page_cur_delete_rec(page_cursor, index, *offsets, mtr); |
| 4014 |
|
| 4015 |
page_cur_move_to_prev(page_cursor); |
| 4016 |
|
| 4017 |
rec = |
| 4018 |
btr_cur_insert_if_possible(cursor, new_entry, offsets, offsets_heap, mtr); |
| 4019 |
|
| 4020 |
if (rec) { |
| 4021 |
page_cursor->rec = rec; |
| 4022 |
|
| 4023 |
if (!dict_table_is_locking_disabled(index->table)) { |
| 4024 |
lock_rec_restore_from_page_infimum(btr_cur_get_block(cursor), rec, block); |
| 4025 |
} |
| 4026 |
|
| 4027 |
if (!rec_get_deleted_flag(rec, rec_offs_comp(*offsets))) { |
| 4028 |
/* The new inserted record owns its possible externally |
| 4029 |
stored fields */ |
| 4030 |
lob::BtrContext btr_ctx(mtr, pcur, index, rec, *offsets, block); |
| 4031 |
btr_ctx.unmark_extern_fields(); |
| 4032 |
} |
| 4033 |
|
| 4034 |
bool adjust = big_rec_vec && (flags & BTR_KEEP_POS_FLAG); |
| 4035 |
|
| 4036 |
if (btr_cur_compress_if_useful(cursor, adjust, mtr)) { |
| 4037 |
if (adjust) { |
| 4038 |
rec_offs_make_valid(page_cursor->rec, index, *offsets); |
| 4039 |
} |
| 4040 |
} else if (!index->is_clustered() && page_is_leaf(page) && |
| 4041 |
!index->table->is_temporary()) { |
| 4042 |
/* Update the free bits in the insert buffer. |
| 4043 |
This is the same block which was skipped by |
| 4044 |
BTR_KEEP_IBUF_BITMAP. */ |
| 4045 |
if (page_zip) { |
| 4046 |
ibuf_update_free_bits_zip(block, mtr); |
| 4047 |
} else { |
| 4048 |
ibuf_update_free_bits_low(block, max_ins_size, mtr); |
| 4049 |
} |
| 4050 |
} |
| 4051 |
|
| 4052 |
if (!srv_read_only_mode && !big_rec_vec && page_is_leaf(page) && |
| 4053 |
!dict_index_is_online_ddl(index)) { |
| 4054 |
mtr_memo_release(mtr, dict_index_get_lock(index), |
| 4055 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK); |
| 4056 |
|
| 4057 |
/* NOTE: We cannot release root block latch here, because it |
| 4058 |
has segment header and already modified in most of cases.*/ |
| 4059 |
} |
| 4060 |
|
| 4061 |
err = DB_SUCCESS; |
| 4062 |
goto return_after_reservations; |
| 4063 |
} else { |
| 4064 |
/* If the page is compressed and it initially |
| 4065 |
compresses very well, and there is a subsequent insert |
| 4066 |
of a badly-compressing record, it is possible for |
| 4067 |
btr_cur_optimistic_update() to return DB_UNDERFLOW and |
| 4068 |
btr_cur_insert_if_possible() to return false. */ |
| 4069 |
ut_a(page_zip || optim_err != DB_UNDERFLOW); |
| 4070 |
|
| 4071 |
/* Out of space: reset the free bits. |
| 4072 |
This is the same block which was skipped by |
| 4073 |
BTR_KEEP_IBUF_BITMAP. */ |
| 4074 |
if (!index->is_clustered() && !index->table->is_temporary() && |
| 4075 |
page_is_leaf(page)) { |
| 4076 |
ibuf_reset_free_bits(block); |
| 4077 |
} |
| 4078 |
} |
| 4079 |
|
| 4080 |
if (big_rec_vec != nullptr && !index->table->is_intrinsic()) { |
| 4081 |
ut_ad(page_is_leaf(page)); |
| 4082 |
ut_ad(index->is_clustered()); |
| 4083 |
ut_ad(flags & BTR_KEEP_POS_FLAG); |
| 4084 |
|
| 4085 |
/* btr_page_split_and_insert() in |
| 4086 |
btr_cur_pessimistic_insert() invokes |
| 4087 |
mtr_memo_release(mtr, index->lock, MTR_MEMO_SX_LOCK). |
| 4088 |
We must keep the index->lock when we created a |
| 4089 |
big_rec, so that row_upd_clust_rec() can store the |
| 4090 |
big_rec in the same mini-transaction. */ |
| 4091 |
|
| 4092 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 4093 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK)); |
| 4094 |
|
| 4095 |
mtr_sx_lock(dict_index_get_lock(index), mtr, UT_LOCATION_HERE); |
| 4096 |
} |
| 4097 |
|
| 4098 |
/* Was the record to be updated positioned as the first user |
| 4099 |
record on its page? */ |
| 4100 |
was_first = page_cur_is_before_first(page_cursor); |
| 4101 |
|
| 4102 |
/* Lock checks and undo logging were already performed by |
| 4103 |
btr_cur_upd_lock_and_undo(). We do not try |
| 4104 |
btr_cur_optimistic_insert() because |
| 4105 |
btr_cur_insert_if_possible() already failed above. */ |
| 4106 |
|
| 4107 |
err = btr_cur_pessimistic_insert( |
| 4108 |
BTR_NO_UNDO_LOG_FLAG | BTR_NO_LOCKING_FLAG | BTR_KEEP_SYS_FLAG, cursor, |
| 4109 |
offsets, offsets_heap, new_entry, &rec, &dummy_big_rec, nullptr, mtr); |
| 4110 |
ut_a(rec); |
| 4111 |
ut_a(err == DB_SUCCESS); |
| 4112 |
ut_a(dummy_big_rec == nullptr); |
| 4113 |
ut_ad(rec_offs_validate(rec, cursor->index, *offsets)); |
| 4114 |
page_cursor->rec = rec; |
| 4115 |
|
| 4116 |
/* Multiple transactions cannot simultaneously operate on the |
| 4117 |
same temp-table in parallel. |
| 4118 |
max_trx_id is ignored for temp tables because it not required |
| 4119 |
for MVCC. */ |
| 4120 |
if (dict_index_is_sec_or_ibuf(index) && !index->table->is_temporary()) { |
| 4121 |
/* Update PAGE_MAX_TRX_ID in the index page header. |
| 4122 |
It was not updated by btr_cur_pessimistic_insert() |
| 4123 |
because of BTR_NO_LOCKING_FLAG. */ |
| 4124 |
buf_block_t *rec_block; |
| 4125 |
|
| 4126 |
rec_block = btr_cur_get_block(cursor); |
| 4127 |
|
| 4128 |
page_update_max_trx_id(rec_block, buf_block_get_page_zip(rec_block), trx_id, |
| 4129 |
mtr); |
| 4130 |
} |
| 4131 |
|
| 4132 |
if (!rec_get_deleted_flag(rec, rec_offs_comp(*offsets))) { |
| 4133 |
/* The new inserted record owns its possible externally |
| 4134 |
stored fields */ |
| 4135 |
buf_block_t *rec_block = btr_cur_get_block(cursor); |
| 4136 |
|
| 4137 |
#ifdef UNIV_ZIP_DEBUG |
| 4138 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 4139 |
page = buf_block_get_frame(rec_block); |
| 4140 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4141 |
page_zip = buf_block_get_page_zip(rec_block); |
| 4142 |
|
| 4143 |
lob::BtrContext btr_ctx(mtr, nullptr, index, rec, *offsets, rec_block); |
| 4144 |
btr_ctx.unmark_extern_fields(); |
| 4145 |
} |
| 4146 |
|
| 4147 |
if (!dict_table_is_locking_disabled(index->table)) { |
| 4148 |
lock_rec_restore_from_page_infimum(btr_cur_get_block(cursor), rec, block); |
| 4149 |
} |
| 4150 |
|
| 4151 |
/* If necessary, restore also the correct lock state for a new, |
| 4152 |
preceding supremum record created in a page split. While the old |
| 4153 |
record was nonexistent, the supremum might have inherited its locks |
| 4154 |
from a wrong record. */ |
| 4155 |
|
| 4156 |
if (!was_first && !dict_table_is_locking_disabled(index->table)) { |
| 4157 |
btr_cur_pess_upd_restore_supremum(btr_cur_get_block(cursor), rec, mtr); |
| 4158 |
} |
| 4159 |
|
| 4160 |
return_after_reservations: |
| 4161 |
#ifdef UNIV_ZIP_DEBUG |
| 4162 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 4163 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4164 |
|
| 4165 |
if (n_reserved > 0) { |
| 4166 |
fil_space_release_free_extents(index->space, n_reserved); |
| 4167 |
} |
| 4168 |
|
| 4169 |
*big_rec = big_rec_vec; |
| 4170 |
|
| 4171 |
ut_d(update->validate_for_index(index)); |
| 4172 |
return err; |
| 4173 |
} |
| 4174 |
|
| 4175 |
/*==================== B-TREE DELETE MARK AND UNMARK ===============*/ |
| 4176 |
|
| 4177 |
/** Writes the redo log record for delete marking or unmarking of an index |
| 4178 |
record. */ |
| 4179 |
static inline void btr_cur_del_mark_set_clust_rec_log( |
| 4180 |
rec_t *rec, /*!< in: record */ |
| 4181 |
dict_index_t *index, /*!< in: index of the record */ |
| 4182 |
trx_id_t trx_id, /*!< in: transaction id */ |
| 4183 |
roll_ptr_t roll_ptr, /*!< in: roll ptr to the undo log record */ |
| 4184 |
mtr_t *mtr) /*!< in: mtr */ |
| 4185 |
{ |
| 4186 |
byte *log_ptr = nullptr; |
| 4187 |
|
| 4188 |
ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(index->table)); |
| 4189 |
|
| 4190 |
const bool opened = |
| 4191 |
mlog_open_and_write_index(mtr, rec, index, MLOG_REC_CLUST_DELETE_MARK, |
| 4192 |
1 + 1 + DATA_ROLL_PTR_LEN + 14 + 2, log_ptr); |
| 4193 |
|
| 4194 |
if (!opened) { |
| 4195 |
/* Logging in mtr is switched off during crash recovery */ |
| 4196 |
return; |
| 4197 |
} |
| 4198 |
|
| 4199 |
*log_ptr++ = 0; |
| 4200 |
*log_ptr++ = 1; |
| 4201 |
|
| 4202 |
log_ptr = |
| 4203 |
row_upd_write_sys_vals_to_log(index, trx_id, roll_ptr, log_ptr, mtr); |
| 4204 |
mach_write_to_2(log_ptr, page_offset(rec)); |
| 4205 |
log_ptr += 2; |
| 4206 |
|
| 4207 |
mlog_close(mtr, log_ptr); |
| 4208 |
} |
| 4209 |
#endif /* !UNIV_HOTBACKUP */ |
| 4210 |
|
| 4211 |
/** Parses the redo log record for delete marking or unmarking of a clustered |
| 4212 |
index record. |
| 4213 |
@return end of log record or NULL */ |
| 4214 |
byte *btr_cur_parse_del_mark_set_clust_rec( |
| 4215 |
byte *ptr, /*!< in: buffer */ |
| 4216 |
byte *end_ptr, /*!< in: buffer end */ |
| 4217 |
page_t *page, /*!< in/out: page or NULL */ |
| 4218 |
page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */ |
| 4219 |
dict_index_t *index) /*!< in: index corresponding to page */ |
| 4220 |
{ |
| 4221 |
ulint pos; |
| 4222 |
trx_id_t trx_id; |
| 4223 |
roll_ptr_t roll_ptr; |
| 4224 |
ulint offset; |
| 4225 |
rec_t *rec; |
| 4226 |
|
| 4227 |
ut_ad(!page || page_is_comp(page) == dict_table_is_comp(index->table)); |
| 4228 |
|
| 4229 |
if (end_ptr < ptr + 2) { |
| 4230 |
return (nullptr); |
| 4231 |
} |
| 4232 |
|
| 4233 |
auto flags = mach_read_from_1(ptr); |
| 4234 |
ptr++; |
| 4235 |
auto val = mach_read_from_1(ptr); |
| 4236 |
ptr++; |
| 4237 |
|
| 4238 |
ptr = row_upd_parse_sys_vals(ptr, end_ptr, &pos, &trx_id, &roll_ptr); |
| 4239 |
|
| 4240 |
if (ptr == nullptr) { |
| 4241 |
return (nullptr); |
| 4242 |
} |
| 4243 |
|
| 4244 |
if (end_ptr < ptr + 2) { |
| 4245 |
return (nullptr); |
| 4246 |
} |
| 4247 |
|
| 4248 |
offset = mach_read_from_2(ptr); |
| 4249 |
ptr += 2; |
| 4250 |
|
| 4251 |
ut_a(offset <= UNIV_PAGE_SIZE); |
| 4252 |
|
| 4253 |
if (page) { |
| 4254 |
rec = page + offset; |
| 4255 |
|
| 4256 |
/* We do not need to reserve search latch, as the page |
| 4257 |
is only being recovered, and there cannot be a hash index to |
| 4258 |
it. Besides, these fields are being updated in place |
| 4259 |
and the adaptive hash index does not depend on them. */ |
| 4260 |
|
| 4261 |
btr_rec_set_deleted_flag(rec, page_zip, val); |
| 4262 |
|
| 4263 |
if (!(flags & BTR_KEEP_SYS_FLAG)) { |
| 4264 |
mem_heap_t *heap = nullptr; |
| 4265 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 4266 |
rec_offs_init(offsets_); |
| 4267 |
|
| 4268 |
row_upd_rec_sys_fields_in_recovery( |
| 4269 |
rec, page_zip, |
| 4270 |
rec_get_offsets(rec, index, offsets_, ULINT_UNDEFINED, |
| 4271 |
UT_LOCATION_HERE, &heap), |
| 4272 |
pos, trx_id, roll_ptr); |
| 4273 |
if (UNIV_LIKELY_NULL(heap)) { |
| 4274 |
mem_heap_free(heap); |
| 4275 |
} |
| 4276 |
} |
| 4277 |
} |
| 4278 |
|
| 4279 |
return (ptr); |
| 4280 |
} |
| 4281 |
|
| 4282 |
#ifndef UNIV_HOTBACKUP |
| 4283 |
/** Marks a clustered index record deleted. Writes an undo log record to |
| 4284 |
undo log on this delete marking. Writes in the trx id field the id |
| 4285 |
of the deleting transaction, and in the roll ptr field pointer to the |
| 4286 |
undo log record created. |
| 4287 |
@return DB_SUCCESS, DB_LOCK_WAIT, or error number */ |
| 4288 |
dberr_t btr_cur_del_mark_set_clust_rec( |
| 4289 |
ulint flags, /*!< in: undo logging and locking flags */ |
| 4290 |
buf_block_t *block, /*!< in/out: buffer block of the record */ |
| 4291 |
rec_t *rec, /*!< in/out: record */ |
| 4292 |
dict_index_t *index, /*!< in: clustered index of the record */ |
| 4293 |
const ulint *offsets, /*!< in: rec_get_offsets(rec) */ |
| 4294 |
que_thr_t *thr, /*!< in: query thread */ |
| 4295 |
const dtuple_t *entry, /*!< in: dtuple for the deleting record, also |
| 4296 |
contains the virtual cols if there are any */ |
| 4297 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 4298 |
{ |
| 4299 |
roll_ptr_t roll_ptr; |
| 4300 |
dberr_t err; |
| 4301 |
page_zip_des_t *page_zip; |
| 4302 |
trx_t *trx; |
| 4303 |
|
| 4304 |
ut_ad(index->is_clustered()); |
| 4305 |
ut_ad(rec_offs_validate(rec, index, offsets)); |
| 4306 |
ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(index->table)); |
| 4307 |
ut_ad(buf_block_get_frame(block) == page_align(rec)); |
| 4308 |
ut_ad(page_is_leaf(page_align(rec))); |
| 4309 |
|
| 4310 |
if (rec_get_deleted_flag(rec, rec_offs_comp(offsets))) { |
| 4311 |
/* While cascading delete operations, this becomes possible. */ |
| 4312 |
ut_ad(rec_get_trx_id(rec, index) == thr_get_trx(thr)->id); |
| 4313 |
return (DB_SUCCESS); |
| 4314 |
} |
| 4315 |
|
| 4316 |
err = lock_clust_rec_modify_check_and_lock(BTR_NO_LOCKING_FLAG, block, rec, |
| 4317 |
index, offsets, thr); |
| 4318 |
|
| 4319 |
if (err != DB_SUCCESS) { |
| 4320 |
return (err); |
| 4321 |
} |
| 4322 |
|
| 4323 |
err = |
| 4324 |
trx_undo_report_row_operation(flags, TRX_UNDO_MODIFY_OP, thr, index, |
| 4325 |
entry, nullptr, 0, rec, offsets, &roll_ptr); |
| 4326 |
if (err != DB_SUCCESS) { |
| 4327 |
return (err); |
| 4328 |
} |
| 4329 |
|
| 4330 |
/* The search latch is not needed here, because |
| 4331 |
the adaptive hash index does not depend on the delete-mark |
| 4332 |
and the delete-mark is being updated in place. */ |
| 4333 |
|
| 4334 |
page_zip = buf_block_get_page_zip(block); |
| 4335 |
|
| 4336 |
btr_rec_set_deleted_flag(rec, page_zip, true); |
| 4337 |
|
| 4338 |
/* For intrinsic table, roll-ptr is not maintained as there is no UNDO |
| 4339 |
logging. Skip updating it. */ |
| 4340 |
if (index->table->is_intrinsic()) { |
| 4341 |
return (err); |
| 4342 |
} |
| 4343 |
|
| 4344 |
trx = thr_get_trx(thr); |
| 4345 |
/* This function must not be invoked during rollback |
| 4346 |
(of a TRX_STATE_PREPARE transaction or otherwise). */ |
| 4347 |
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE)); |
| 4348 |
ut_ad(!trx->in_rollback); |
| 4349 |
|
| 4350 |
DBUG_PRINT("ib_cur", |
| 4351 |
("delete-mark clust %s (" IB_ID_FMT ") by " TRX_ID_FMT ": %s", |
| 4352 |
index->table_name, index->id, trx_get_id_for_print(trx), |
| 4353 |
rec_printer(rec, offsets).str().c_str())); |
| 4354 |
|
| 4355 |
if (dict_index_is_online_ddl(index)) { |
| 4356 |
row_log_table_delete(rec, entry, index, offsets, nullptr); |
| 4357 |
} |
| 4358 |
|
| 4359 |
row_upd_rec_sys_fields(rec, page_zip, index, offsets, trx, roll_ptr); |
| 4360 |
|
| 4361 |
btr_cur_del_mark_set_clust_rec_log(rec, index, trx->id, roll_ptr, mtr); |
| 4362 |
|
| 4363 |
return (err); |
| 4364 |
} |
| 4365 |
|
| 4366 |
/** Writes the redo log record for a delete mark setting of a secondary |
| 4367 |
index record. */ |
| 4368 |
static inline void btr_cur_del_mark_set_sec_rec_log( |
| 4369 |
rec_t *rec, /*!< in: record */ |
| 4370 |
bool val, /*!< in: value to set */ |
| 4371 |
mtr_t *mtr) /*!< in: mtr */ |
| 4372 |
{ |
| 4373 |
byte *log_ptr = nullptr; |
| 4374 |
|
| 4375 |
if (!mlog_open(mtr, 11 + 1 + 2, log_ptr)) { |
| 4376 |
/* Logging in mtr is switched off during crash recovery: |
| 4377 |
in that case mlog_open returns false */ |
| 4378 |
return; |
| 4379 |
} |
| 4380 |
|
| 4381 |
log_ptr = mlog_write_initial_log_record_fast(rec, MLOG_REC_SEC_DELETE_MARK, |
| 4382 |
log_ptr, mtr); |
| 4383 |
mach_write_to_1(log_ptr, val); |
| 4384 |
log_ptr++; |
| 4385 |
|
| 4386 |
mach_write_to_2(log_ptr, page_offset(rec)); |
| 4387 |
log_ptr += 2; |
| 4388 |
|
| 4389 |
mlog_close(mtr, log_ptr); |
| 4390 |
} |
| 4391 |
#endif /* !UNIV_HOTBACKUP */ |
| 4392 |
|
| 4393 |
/** Parses the redo log record for delete marking or unmarking of a secondary |
| 4394 |
index record. |
| 4395 |
@return end of log record or NULL */ |
| 4396 |
byte *btr_cur_parse_del_mark_set_sec_rec( |
| 4397 |
byte *ptr, /*!< in: buffer */ |
| 4398 |
byte *end_ptr, /*!< in: buffer end */ |
| 4399 |
page_t *page, /*!< in/out: page or NULL */ |
| 4400 |
page_zip_des_t *page_zip) /*!< in/out: compressed page, or NULL */ |
| 4401 |
{ |
| 4402 |
rec_t *rec; |
| 4403 |
|
| 4404 |
if (end_ptr < ptr + 3) { |
| 4405 |
return (nullptr); |
| 4406 |
} |
| 4407 |
|
| 4408 |
auto val = mach_read_from_1(ptr); |
| 4409 |
ptr++; |
| 4410 |
|
| 4411 |
auto offset = mach_read_from_2(ptr); |
| 4412 |
ptr += 2; |
| 4413 |
|
| 4414 |
ut_a(offset <= UNIV_PAGE_SIZE); |
| 4415 |
|
| 4416 |
if (page) { |
| 4417 |
rec = page + offset; |
| 4418 |
|
| 4419 |
/* We do not need to reserve search latch, as the page |
| 4420 |
is only being recovered, and there cannot be a hash index to |
| 4421 |
it. Besides, the delete-mark flag is being updated in place |
| 4422 |
and the adaptive hash index does not depend on it. */ |
| 4423 |
|
| 4424 |
btr_rec_set_deleted_flag(rec, page_zip, val); |
| 4425 |
} |
| 4426 |
|
| 4427 |
return (ptr); |
| 4428 |
} |
| 4429 |
|
| 4430 |
#ifndef UNIV_HOTBACKUP |
| 4431 |
/** Sets a secondary index record delete mark to true or false. |
| 4432 |
@return DB_SUCCESS, DB_LOCK_WAIT, or error number */ |
| 4433 |
dberr_t btr_cur_del_mark_set_sec_rec( |
| 4434 |
ulint flags, /*!< in: locking flag */ |
| 4435 |
btr_cur_t *cursor, /*!< in: cursor */ |
| 4436 |
bool val, /*!< in: value to set */ |
| 4437 |
que_thr_t *thr, /*!< in: query thread */ |
| 4438 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 4439 |
{ |
| 4440 |
buf_block_t *block; |
| 4441 |
rec_t *rec; |
| 4442 |
dberr_t err; |
| 4443 |
|
| 4444 |
block = btr_cur_get_block(cursor); |
| 4445 |
rec = btr_cur_get_rec(cursor); |
| 4446 |
|
| 4447 |
err = lock_sec_rec_modify_check_and_lock(flags, btr_cur_get_block(cursor), |
| 4448 |
rec, cursor->index, thr, mtr); |
| 4449 |
if (err != DB_SUCCESS) { |
| 4450 |
return (err); |
| 4451 |
} |
| 4452 |
|
| 4453 |
ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(cursor->index->table)); |
| 4454 |
|
| 4455 |
DBUG_PRINT("ib_cur", |
| 4456 |
("delete-mark=%u sec %u:%u:%u in %s(" IB_ID_FMT ") by " TRX_ID_FMT, |
| 4457 |
unsigned(val), block->page.id.space(), block->page.id.page_no(), |
| 4458 |
unsigned(page_rec_get_heap_no(rec)), cursor->index->name(), |
| 4459 |
cursor->index->id, trx_get_id_for_print(thr_get_trx(thr)))); |
| 4460 |
|
| 4461 |
/* We do not need to reserve search latch, as the |
| 4462 |
delete-mark flag is being updated in place and the adaptive |
| 4463 |
hash index does not depend on it. */ |
| 4464 |
btr_rec_set_deleted_flag(rec, buf_block_get_page_zip(block), val); |
| 4465 |
|
| 4466 |
btr_cur_del_mark_set_sec_rec_log(rec, val, mtr); |
| 4467 |
|
| 4468 |
return (DB_SUCCESS); |
| 4469 |
} |
| 4470 |
|
| 4471 |
/** Sets a secondary index record's delete mark to the given value. This |
| 4472 |
function is only used by the insert buffer merge mechanism. */ |
| 4473 |
void btr_cur_set_deleted_flag_for_ibuf( |
| 4474 |
rec_t *rec, /*!< in/out: record */ |
| 4475 |
page_zip_des_t *page_zip, /*!< in/out: compressed page |
| 4476 |
corresponding to rec, or NULL |
| 4477 |
when the tablespace is |
| 4478 |
uncompressed */ |
| 4479 |
bool val, /*!< in: value to set */ |
| 4480 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 4481 |
{ |
| 4482 |
/* We do not need to reserve search latch, as the page |
| 4483 |
has just been read to the buffer pool and there cannot be |
| 4484 |
a hash index to it. Besides, the delete-mark flag is being |
| 4485 |
updated in place and the adaptive hash index does not depend |
| 4486 |
on it. */ |
| 4487 |
|
| 4488 |
btr_rec_set_deleted_flag(rec, page_zip, val); |
| 4489 |
|
| 4490 |
btr_cur_del_mark_set_sec_rec_log(rec, val, mtr); |
| 4491 |
} |
| 4492 |
|
| 4493 |
/*==================== B-TREE RECORD REMOVE =========================*/ |
| 4494 |
|
| 4495 |
/** Tries to compress a page of the tree if it seems useful. It is assumed |
| 4496 |
that mtr holds an x-latch on the tree and on the cursor page. To avoid |
| 4497 |
deadlocks, mtr must also own x-latches to brothers of page, if those |
| 4498 |
brothers exist. NOTE: it is assumed that the caller has reserved enough |
| 4499 |
free extents so that the compression will always succeed if done! |
| 4500 |
@return true if compression occurred */ |
| 4501 |
bool btr_cur_compress_if_useful( |
| 4502 |
btr_cur_t *cursor, /*!< in/out: cursor on the page to compress; |
| 4503 |
cursor does not stay valid if !adjust and |
| 4504 |
compression occurs */ |
| 4505 |
bool adjust, /*!< in: true if should adjust the |
| 4506 |
cursor position even if compression occurs */ |
| 4507 |
mtr_t *mtr) /*!< in/out: mini-transaction */ |
| 4508 |
{ |
| 4509 |
/* Avoid applying compression as we don't accept lot of page garbage |
| 4510 |
given the workload of intrinsic table. */ |
| 4511 |
if (cursor->index->table->is_intrinsic()) { |
| 4512 |
return false; |
| 4513 |
} |
| 4514 |
|
| 4515 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(cursor->index), |
| 4516 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) || |
| 4517 |
cursor->index->table->is_intrinsic()); |
| 4518 |
ut_ad(mtr_is_block_fix(mtr, btr_cur_get_block(cursor), MTR_MEMO_PAGE_X_FIX, |
| 4519 |
cursor->index->table)); |
| 4520 |
|
| 4521 |
if (dict_index_is_spatial(cursor->index)) { |
| 4522 |
const page_t *page = btr_cur_get_page(cursor); |
| 4523 |
const trx_t *trx = nullptr; |
| 4524 |
|
| 4525 |
if (cursor->rtr_info->thr != nullptr) { |
| 4526 |
trx = thr_get_trx(cursor->rtr_info->thr); |
| 4527 |
} |
| 4528 |
|
| 4529 |
/* Check whether page lock prevents the compression */ |
| 4530 |
if (lock_other_has_prdt_page_lock(trx, page_get_page_id(page))) { |
| 4531 |
return (false); |
| 4532 |
} |
| 4533 |
} |
| 4534 |
|
| 4535 |
return (btr_cur_compress_recommendation(cursor, mtr) && |
| 4536 |
btr_compress(cursor, adjust, mtr)); |
| 4537 |
} |
| 4538 |
|
| 4539 |
bool btr_cur_optimistic_delete_func(btr_cur_t *cursor, |
| 4540 |
IF_DEBUG(ulint flags, ) mtr_t *mtr) { |
| 4541 |
buf_block_t *block; |
| 4542 |
rec_t *rec; |
| 4543 |
mem_heap_t *heap = nullptr; |
| 4544 |
ulint offsets_[REC_OFFS_NORMAL_SIZE]; |
| 4545 |
ulint *offsets = offsets_; |
| 4546 |
rec_offs_init(offsets_); |
| 4547 |
|
| 4548 |
ut_ad(flags == 0 || flags == BTR_CREATE_FLAG); |
| 4549 |
ut_ad(mtr_memo_contains(mtr, btr_cur_get_block(cursor), MTR_MEMO_PAGE_X_FIX)); |
| 4550 |
ut_ad(mtr_is_block_fix(mtr, btr_cur_get_block(cursor), MTR_MEMO_PAGE_X_FIX, |
| 4551 |
cursor->index->table)); |
| 4552 |
|
| 4553 |
/* This is intended only for leaf page deletions */ |
| 4554 |
|
| 4555 |
block = btr_cur_get_block(cursor); |
| 4556 |
|
| 4557 |
ut_ad(page_is_leaf(buf_block_get_frame(block))); |
| 4558 |
ut_ad(!dict_index_is_online_ddl(cursor->index) || |
| 4559 |
cursor->index->is_clustered() || (flags & BTR_CREATE_FLAG)); |
| 4560 |
|
| 4561 |
rec = btr_cur_get_rec(cursor); |
| 4562 |
offsets = rec_get_offsets(rec, cursor->index, offsets, ULINT_UNDEFINED, |
| 4563 |
UT_LOCATION_HERE, &heap); |
| 4564 |
|
| 4565 |
auto no_compress_needed = |
| 4566 |
!rec_offs_any_extern(offsets) && |
| 4567 |
btr_cur_can_delete_without_compress(cursor, rec_offs_size(offsets), mtr); |
| 4568 |
|
| 4569 |
if (no_compress_needed) { |
| 4570 |
page_t *page = buf_block_get_frame(block); |
| 4571 |
page_zip_des_t *page_zip = buf_block_get_page_zip(block); |
| 4572 |
|
| 4573 |
lock_update_delete(block, rec); |
| 4574 |
|
| 4575 |
btr_search_update_hash_on_delete(cursor); |
| 4576 |
|
| 4577 |
if (page_zip) { |
| 4578 |
#ifdef UNIV_ZIP_DEBUG |
| 4579 |
ut_a(page_zip_validate(page_zip, page, cursor->index)); |
| 4580 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4581 |
page_cur_delete_rec(btr_cur_get_page_cur(cursor), cursor->index, offsets, |
| 4582 |
mtr); |
| 4583 |
#ifdef UNIV_ZIP_DEBUG |
| 4584 |
ut_a(page_zip_validate(page_zip, page, cursor->index)); |
| 4585 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4586 |
|
| 4587 |
/* On compressed pages, the IBUF_BITMAP_FREE |
| 4588 |
space is not affected by deleting (purging) |
| 4589 |
records, because it is defined as the minimum |
| 4590 |
of space available *without* reorganize, and |
| 4591 |
space available in the modification log. */ |
| 4592 |
} else { |
| 4593 |
const ulint max_ins = page_get_max_insert_size_after_reorganize(page, 1); |
| 4594 |
|
| 4595 |
page_cur_delete_rec(btr_cur_get_page_cur(cursor), cursor->index, offsets, |
| 4596 |
mtr); |
| 4597 |
|
| 4598 |
/* The change buffer does not handle inserts |
| 4599 |
into non-leaf pages, into clustered indexes, |
| 4600 |
or into the change buffer. */ |
| 4601 |
if (!cursor->index->is_clustered() && |
| 4602 |
!cursor->index->table->is_temporary() && |
| 4603 |
!dict_index_is_ibuf(cursor->index)) { |
| 4604 |
ibuf_update_free_bits_low(block, max_ins, mtr); |
| 4605 |
} |
| 4606 |
} |
| 4607 |
} else { |
| 4608 |
/* prefetch siblings of the leaf for the pessimistic |
| 4609 |
operation. */ |
| 4610 |
btr_cur_prefetch_siblings(block); |
| 4611 |
} |
| 4612 |
|
| 4613 |
if (UNIV_LIKELY_NULL(heap)) { |
| 4614 |
mem_heap_free(heap); |
| 4615 |
} |
| 4616 |
|
| 4617 |
return (no_compress_needed); |
| 4618 |
} |
| 4619 |
|
| 4620 |
bool btr_cur_pessimistic_delete(dberr_t *err, bool has_reserved_extents, |
| 4621 |
btr_cur_t *cursor, uint32_t flags, |
| 4622 |
bool rollback, trx_id_t trx_id, |
| 4623 |
undo_no_t undo_no, ulint rec_type, mtr_t *mtr, |
| 4624 |
btr_pcur_t *pcur, purge_node_t *node) { |
| 4625 |
DBUG_TRACE; |
| 4626 |
|
| 4627 |
buf_block_t *block; |
| 4628 |
page_t *page; |
| 4629 |
dict_index_t *index; |
| 4630 |
rec_t *rec; |
| 4631 |
ulint n_reserved = 0; |
| 4632 |
bool success; |
| 4633 |
bool ret = false; |
| 4634 |
ulint level; |
| 4635 |
ulint *offsets; |
| 4636 |
#ifdef UNIV_DEBUG |
| 4637 |
bool parent_latched = false; |
| 4638 |
#endif /* UNIV_DEBUG */ |
| 4639 |
|
| 4640 |
block = btr_cur_get_block(cursor); |
| 4641 |
page = buf_block_get_frame(block); |
| 4642 |
index = cursor->index; |
| 4643 |
|
| 4644 |
ut_ad(flags == 0 || flags == BTR_CREATE_FLAG); |
| 4645 |
ut_ad(!dict_index_is_online_ddl(index) || index->is_clustered() || |
| 4646 |
(flags & BTR_CREATE_FLAG)); |
| 4647 |
ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index), |
| 4648 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) || |
| 4649 |
index->table->is_intrinsic()); |
| 4650 |
ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table)); |
| 4651 |
|
| 4652 |
Scoped_heap heap_ptr(1024, UT_LOCATION_HERE); |
| 4653 |
mem_heap_t *heap = heap_ptr.get(); |
| 4654 |
|
| 4655 |
rec = btr_cur_get_rec(cursor); |
| 4656 |
#ifdef UNIV_ZIP_DEBUG |
| 4657 |
page_zip_des_t *page_zip = buf_block_get_page_zip(block); |
| 4658 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 4659 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4660 |
|
| 4661 |
offsets = rec_get_offsets(rec, index, nullptr, ULINT_UNDEFINED, |
| 4662 |
UT_LOCATION_HERE, &heap); |
| 4663 |
|
| 4664 |
if (rec_offs_any_extern(offsets)) { |
| 4665 |
lob::BtrContext btr_ctx(mtr, pcur, index, rec, offsets, block); |
| 4666 |
|
| 4667 |
/* The following call will restart the btr_mtr, which could change the |
| 4668 |
cursor position. */ |
| 4669 |
btr_ctx.free_externally_stored_fields(trx_id, undo_no, rollback, rec_type, |
| 4670 |
node); |
| 4671 |
|
| 4672 |
/* The cursor position could have changed now. */ |
| 4673 |
if (pcur != nullptr) { |
| 4674 |
cursor = pcur->get_btr_cur(); |
| 4675 |
block = btr_cur_get_block(cursor); |
| 4676 |
page = buf_block_get_frame(block); |
| 4677 |
rec = btr_cur_get_rec(cursor); |
| 4678 |
rec_offs_make_valid(rec, index, offsets); |
| 4679 |
#ifdef UNIV_ZIP_DEBUG |
| 4680 |
page_zip = buf_block_get_page_zip(block); |
| 4681 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4682 |
} |
| 4683 |
|
| 4684 |
#ifdef UNIV_ZIP_DEBUG |
| 4685 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 4686 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4687 |
|
| 4688 |
/* While purging LOBs, there are intermediate mtr commits which releases |
| 4689 |
the latches. In that duration, it is possible that the clust_rec is reused. |
| 4690 |
In such situation, don't proceed further. */ |
| 4691 |
if (!rollback && rec_type != 0 && |
| 4692 |
!rec_get_deleted_flag(rec, rec_offs_comp(offsets))) { |
| 4693 |
/* This is the purge thread. For the purge thread, rec_type will have a |
| 4694 |
valid value. */ |
| 4695 |
*err = DB_SUCCESS; |
| 4696 |
return true; |
| 4697 |
} |
| 4698 |
} |
| 4699 |
|
| 4700 |
if (!has_reserved_extents && !rollback) { |
| 4701 |
/* First reserve enough free space for the file segments |
| 4702 |
of the index tree, so that the node pointer updates will |
| 4703 |
not fail because of lack of space */ |
| 4704 |
|
| 4705 |
ulint n_extents = cursor->tree_height / 32 + 1; |
| 4706 |
|
| 4707 |
success = fsp_reserve_free_extents(&n_reserved, index->space, n_extents, |
| 4708 |
FSP_CLEANING, mtr); |
| 4709 |
if (!success) { |
| 4710 |
*err = DB_OUT_OF_FILE_SPACE; |
| 4711 |
|
| 4712 |
return false; |
| 4713 |
} |
| 4714 |
} |
| 4715 |
|
| 4716 |
if (UNIV_UNLIKELY(page_get_n_recs(page) < 2) && |
| 4717 |
UNIV_UNLIKELY(dict_index_get_page(index) != block->page.id.page_no())) { |
| 4718 |
/* If there is only one record, drop the whole page in |
| 4719 |
btr_discard_page, if this is not the root page */ |
| 4720 |
|
| 4721 |
btr_discard_page(cursor, mtr); |
| 4722 |
|
| 4723 |
ret = true; |
| 4724 |
|
| 4725 |
goto return_after_reservations; |
| 4726 |
} |
| 4727 |
|
| 4728 |
if (flags == 0) { |
| 4729 |
lock_update_delete(block, rec); |
| 4730 |
} |
| 4731 |
|
| 4732 |
level = btr_page_get_level(page); |
| 4733 |
|
| 4734 |
if (level > 0 && |
| 4735 |
UNIV_UNLIKELY(rec == page_rec_get_next(page_get_infimum_rec(page)))) { |
| 4736 |
rec_t *next_rec = page_rec_get_next(rec); |
| 4737 |
|
| 4738 |
if (btr_page_get_prev(page, mtr) == FIL_NULL) { |
| 4739 |
/* If we delete the leftmost node pointer on a |
| 4740 |
non-leaf level, we must mark the new leftmost node |
| 4741 |
pointer as the predefined minimum record */ |
| 4742 |
|
| 4743 |
/* This will make page_zip_validate() fail until |
| 4744 |
page_cur_delete_rec() completes. This is harmless, |
| 4745 |
because everything will take place within a single |
| 4746 |
mini-transaction and because writing to the redo log |
| 4747 |
is an atomic operation (performed by mtr_commit()). */ |
| 4748 |
btr_set_min_rec_mark(next_rec, mtr); |
| 4749 |
} else if (dict_index_is_spatial(index)) { |
| 4750 |
/* For rtree, if delete the leftmost node pointer, |
| 4751 |
we need to update parent page. */ |
| 4752 |
rtr_mbr_t father_mbr; |
| 4753 |
rec_t *father_rec; |
| 4754 |
btr_cur_t father_cursor; |
| 4755 |
ulint *offsets; |
| 4756 |
bool upd_ret; |
| 4757 |
ulint len; |
| 4758 |
|
| 4759 |
rtr_page_get_father_block(nullptr, heap, index, block, mtr, nullptr, |
| 4760 |
&father_cursor); |
| 4761 |
offsets = rec_get_offsets(btr_cur_get_rec(&father_cursor), index, nullptr, |
| 4762 |
ULINT_UNDEFINED, UT_LOCATION_HERE, &heap); |
| 4763 |
|
| 4764 |
father_rec = btr_cur_get_rec(&father_cursor); |
| 4765 |
rtr_read_mbr(rec_get_nth_field(nullptr, father_rec, offsets, 0, &len), |
| 4766 |
&father_mbr); |
| 4767 |
|
| 4768 |
upd_ret = rtr_update_mbr_field(&father_cursor, offsets, nullptr, page, |
| 4769 |
&father_mbr, next_rec, mtr); |
| 4770 |
|
| 4771 |
if (!upd_ret) { |
| 4772 |
*err = DB_ERROR; |
| 4773 |
|
| 4774 |
return false; |
| 4775 |
} |
| 4776 |
|
| 4777 |
ut_d(parent_latched = true); |
| 4778 |
} else { |
| 4779 |
/* Otherwise, if we delete the leftmost node pointer |
| 4780 |
on a page, we have to change the parent node pointer |
| 4781 |
so that it is equal to the new leftmost node pointer |
| 4782 |
on the page */ |
| 4783 |
|
| 4784 |
btr_node_ptr_delete(index, block, mtr); |
| 4785 |
|
| 4786 |
dtuple_t *node_ptr = dict_index_build_node_ptr( |
| 4787 |
index, next_rec, block->page.id.page_no(), heap, level); |
| 4788 |
|
| 4789 |
btr_insert_on_non_leaf_level(flags, index, level + 1, node_ptr, |
| 4790 |
UT_LOCATION_HERE, mtr); |
| 4791 |
|
| 4792 |
ut_d(parent_latched = true); |
| 4793 |
} |
| 4794 |
} |
| 4795 |
|
| 4796 |
btr_search_update_hash_on_delete(cursor); |
| 4797 |
|
| 4798 |
page_cur_delete_rec(btr_cur_get_page_cur(cursor), index, offsets, mtr); |
| 4799 |
#ifdef UNIV_ZIP_DEBUG |
| 4800 |
ut_a(!page_zip || page_zip_validate(page_zip, page, index)); |
| 4801 |
#endif /* UNIV_ZIP_DEBUG */ |
| 4802 |
|
| 4803 |
/* btr_check_node_ptr() needs parent block latched */ |
| 4804 |
|
| 4805 |
ut_ad(!parent_latched || btr_check_node_ptr(index, block, mtr)); |
| 4806 |
|
| 4807 |
return_after_reservations: |
| 4808 |
*err = DB_SUCCESS; |
| 4809 |
|
| 4810 |
if (ret == false) { |
| 4811 |
ret = btr_cur_compress_if_useful(cursor, false, mtr); |
| 4812 |
} |
| 4813 |
|
| 4814 |
if (!srv_read_only_mode && page_is_leaf(page) && |
| 4815 |
!dict_index_is_online_ddl(index)) { |
| 4816 |
mtr_memo_release(mtr, dict_index_get_lock(index), |
| 4817 |
MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK); |
| 4818 |
|
| 4819 |
/* NOTE: We cannot release root block latch here, because it |
| 4820 |
has segment header and already modified in most of cases.*/ |
| 4821 |
} |
| 4822 |
|
| 4823 |
if (n_reserved > 0) { |
| 4824 |
fil_space_release_free_extents(index->space, n_reserved); |
| 4825 |
} |
| 4826 |
|
| 4827 |
ut_ad(heap == heap_ptr.get()); |
| 4828 |
|
| 4829 |
return ret; |
| 4830 |
} |
| 4831 |
|
| 4832 |
static void btr_cur_add_path_info(btr_cur_t *cursor, ulint height, |
| 4833 |
ulint root_height) { |
| 4834 |
ut_a(cursor->path_arr); |
| 4835 |
|
| 4836 |
if (root_height >= BTR_PATH_ARRAY_N_SLOTS - 1) { |
| 4837 |
/* Do nothing; return empty path */ |
| 4838 |
|
| 4839 |
const auto slot = cursor->path_arr; |
| 4840 |
slot->nth_rec = ULINT_UNDEFINED; |
| 4841 |
|
| 4842 |
return; |
| 4843 |
} |
| 4844 |
|
| 4845 |
btr_path_t *slot; |
| 4846 |
|
| 4847 |
if (height == 0) { |
| 4848 |
/* Mark end of slots for path */ |
| 4849 |
slot = cursor->path_arr + root_height + 1; |
| 4850 |
slot->nth_rec = ULINT_UNDEFINED; |
| 4851 |
} |
| 4852 |
|
| 4853 |
const auto rec = btr_cur_get_rec(cursor); |
| 4854 |
|
| 4855 |
slot = cursor->path_arr + (root_height - height); |
| 4856 |
|
| 4857 |
const auto page = page_align(rec); |
| 4858 |
|
| 4859 |
slot->n_recs = page_get_n_recs(page); |
| 4860 |
slot->page_no = page_get_page_no(page); |
| 4861 |
slot->page_level = btr_page_get_level(page); |
| 4862 |
slot->nth_rec = page_rec_get_n_recs_before(rec); |
| 4863 |
} |
| 4864 |
|
| 4865 |
/** Estimate the number of rows between slot1 and slot2 for any level on a |
| 4866 |
B-tree. This function starts from slot1->page and reads a few pages to |
| 4867 |
the right, counting their records. If we reach slot2->page quickly then |
| 4868 |
we know exactly how many records there are between slot1 and slot2 and |
| 4869 |
we set is_n_rows_exact to true. If we cannot reach slot2->page quickly |
| 4870 |
then we calculate the average number of records in the pages scanned |
| 4871 |
so far and assume that all pages that we did not scan up to slot2->page |
| 4872 |
contain the same number of records, then we multiply that average to |
| 4873 |
the number of pages between slot1->page and slot2->page (which is |
| 4874 |
n_rows_on_prev_level). In this case we set is_n_rows_exact to false. |
| 4875 |
@return number of rows, not including the borders (exact or estimated) */ |
| 4876 |
static int64_t btr_estimate_n_rows_in_range_on_level( |
| 4877 |
dict_index_t *index, /*!< in: index */ |
| 4878 |
btr_path_t *slot1, /*!< in: left border */ |
| 4879 |
btr_path_t *slot2, /*!< in: right border */ |
| 4880 |
int64_t n_rows_on_prev_level, /*!< in: number of rows |
| 4881 |
on the previous level for the |
| 4882 |
same descend paths; used to |
| 4883 |
determine the number of pages |
| 4884 |
on this level */ |
| 4885 |
bool *is_n_rows_exact) /*!< out: true if the returned |
| 4886 |
value is exact i.e. not an |
| 4887 |
estimation */ |
| 4888 |
{ |
| 4889 |
int64_t n_rows; |
| 4890 |
ulint n_pages_read; |
| 4891 |
ulint level; |
| 4892 |
|
| 4893 |
n_rows = 0; |
| 4894 |
n_pages_read = 0; |
| 4895 |
|
| 4896 |
/* Assume by default that we will scan all pages between |
| 4897 |
slot1->page_no and slot2->page_no. */ |
| 4898 |
*is_n_rows_exact = true; |
| 4899 |
|
| 4900 |
/* Add records from slot1->page_no which are to the right of |
| 4901 |
the record which serves as a left border of the range, if any |
| 4902 |
(we don't include the record itself in this count). */ |
| 4903 |
if (slot1->nth_rec <= slot1->n_recs) { |
| 4904 |
n_rows += slot1->n_recs - slot1->nth_rec; |
| 4905 |
} |
| 4906 |
|
| 4907 |
/* Add records from slot2->page_no which are to the left of |
| 4908 |
the record which servers as a right border of the range, if any |
| 4909 |
(we don't include the record itself in this count). */ |
| 4910 |
if (slot2->nth_rec > 1) { |
| 4911 |
n_rows += slot2->nth_rec - 1; |
| 4912 |
} |
| 4913 |
|
| 4914 |
/* Count the records in the pages between slot1->page_no and |
| 4915 |
slot2->page_no (non inclusive), if any. */ |
| 4916 |
|
| 4917 |
/* Do not read more than this number of pages in order not to hurt |
| 4918 |
performance with this code which is just an estimation. If we read |
| 4919 |
this many pages before reaching slot2->page_no then we estimate the |
| 4920 |
average from the pages scanned so far. */ |
| 4921 |
|
| 4922 |
constexpr uint32_t N_PAGES_READ_LIMIT = 10; |
| 4923 |
|
| 4924 |
page_id_t page_id(dict_index_get_space(index), slot1->page_no); |
| 4925 |
const fil_space_t *space = fil_space_get(index->space); |
| 4926 |
ut_ad(space); |
| 4927 |
const page_size_t page_size(space->flags); |
| 4928 |
|
| 4929 |
level = slot1->page_level; |
| 4930 |
|
| 4931 |
do { |
| 4932 |
mtr_t mtr; |
| 4933 |
page_t *page; |
| 4934 |
buf_block_t *block; |
| 4935 |
|
| 4936 |
mtr_start(&mtr); |
| 4937 |
|
| 4938 |
/* Fetch the page. Because we are not holding the |
| 4939 |
index->lock, the tree may have changed and we may be |
| 4940 |
attempting to read a page that is no longer part of |
| 4941 |
the B-tree. We pass Page_fetch::POSSIBLY_FREED in order to |
| 4942 |
silence a debug assertion about this. */ |
| 4943 |
block = |
| 4944 |
buf_page_get_gen(page_id, page_size, RW_S_LATCH, nullptr, |
| 4945 |
Page_fetch::POSSIBLY_FREED, UT_LOCATION_HERE, &mtr); |
| 4946 |
|
| 4947 |
page = buf_block_get_frame(block); |
| 4948 |
|
| 4949 |
/* It is possible that the tree has been reorganized in the |
| 4950 |
meantime and this is a different page. If this happens the |
| 4951 |
calculated estimate will be bogus, which is not fatal as |
| 4952 |
this is only an estimate. We are sure that a page with |
| 4953 |
page_no exists because InnoDB never frees pages, only |
| 4954 |
reuses them. */ |
| 4955 |
if (!fil_page_index_page_check(page) || |
| 4956 |
btr_page_get_index_id(page) != index->id || |
| 4957 |
btr_page_get_level(page) != level) { |
| 4958 |
/* The page got reused for something else */ |
| 4959 |
mtr_commit(&mtr); |
| 4960 |
goto inexact; |
| 4961 |
} |
| 4962 |
|
| 4963 |
/* It is possible but highly unlikely that the page was |
| 4964 |
originally written by an old version of InnoDB that did |
| 4965 |
not initialize FIL_PAGE_TYPE on other than B-tree pages. |
| 4966 |
For example, this could be an almost-empty BLOB page |
| 4967 |
that happens to contain the magic values in the fields |
| 4968 |
that we checked above. */ |
| 4969 |
|
| 4970 |
n_pages_read++; |
| 4971 |
|
| 4972 |
if (page_id.page_no() != slot1->page_no) { |
| 4973 |
/* Do not count the records on slot1->page_no, |
| 4974 |
we already counted them before this loop. */ |
| 4975 |
n_rows += page_get_n_recs(page); |
| 4976 |
} |
| 4977 |
|
| 4978 |
page_id.set_page_no(btr_page_get_next(page, &mtr)); |
| 4979 |
|
| 4980 |
mtr_commit(&mtr); |
| 4981 |
|
| 4982 |
if (n_pages_read == N_PAGES_READ_LIMIT || page_id.page_no() == FIL_NULL) { |
| 4983 |
/* Either we read too many pages or |
| 4984 |
we reached the end of the level without passing |
| 4985 |
through slot2->page_no, the tree must have changed |
| 4986 |
in the meantime */ |
| 4987 |
goto inexact; |
| 4988 |
} |
| 4989 |
|
| 4990 |
} while (page_id.page_no() != slot2->page_no); |
| 4991 |
|
| 4992 |
return (n_rows); |
| 4993 |
|
| 4994 |
inexact: |
| 4995 |
|
| 4996 |
*is_n_rows_exact = false; |
| 4997 |
|
| 4998 |
/* We did interrupt before reaching slot2->page */ |
| 4999 |
|
| 5000 |
if (n_pages_read > 0) { |
| 5001 |
/* The number of pages on this level is |
| 5002 |
n_rows_on_prev_level, multiply it by the |
| 5003 |
average number of recs per page so far */ |
| 5004 |
n_rows = n_rows_on_prev_level * n_rows / n_pages_read; |
| 5005 |
} else { |
| 5006 |
/* The tree changed before we could even |
| 5007 |
start with slot1->page_no */ |
| 5008 |
n_rows = 10; |
| 5009 |
} |
| 5010 |
|
| 5011 |
return (n_rows); |
| 5012 |
} |
| 5013 |
|
| 5014 |
/** If the tree gets changed too much between the two dives for the left |
| 5015 |
and right boundary then btr_estimate_n_rows_in_range_low() will retry |
| 5016 |
that many times before giving up and returning the value stored in |
| 5017 |
rows_in_range_arbitrary_ret_val. */ |
| 5018 |
static const unsigned rows_in_range_max_retries = 4; |
| 5019 |
|
| 5020 |
/** We pretend that a range has that many records if the tree keeps changing |
| 5021 |
for rows_in_range_max_retries retries while we try to estimate the records |
| 5022 |
in a given range. */ |
| 5023 |
static const int64_t rows_in_range_arbitrary_ret_val = 10; |
| 5024 |
|
| 5025 |
/** Estimates the number of rows in a given index range. |
| 5026 |
@param[in] index index |
| 5027 |
@param[in] tuple1 range start, may also be empty tuple |
| 5028 |
@param[in] mode1 search mode for range start |
| 5029 |
@param[in] tuple2 range end, may also be empty tuple |
| 5030 |
@param[in] mode2 search mode for range end |
| 5031 |
@param[in] nth_attempt if the tree gets modified too much while |
| 5032 |
we are trying to analyze it, then we will retry (this function will call |
| 5033 |
itself, incrementing this parameter) |
| 5034 |
@return estimated number of rows; if after rows_in_range_max_retries |
| 5035 |
retries the tree keeps changing, then we will just return |
| 5036 |
rows_in_range_arbitrary_ret_val as a result (if |
| 5037 |
nth_attempt >= rows_in_range_max_retries and the tree is modified between |
| 5038 |
the two dives). */ |
| 5039 |
static int64_t btr_estimate_n_rows_in_range_low( |
| 5040 |
dict_index_t *index, const dtuple_t *tuple1, page_cur_mode_t mode1, |
| 5041 |
const dtuple_t *tuple2, page_cur_mode_t mode2, unsigned nth_attempt) { |
| 5042 |
std::array<btr_path_t, BTR_PATH_ARRAY_N_SLOTS> path1; |
| 5043 |
std::array<btr_path_t, BTR_PATH_ARRAY_N_SLOTS> path2; |
| 5044 |
btr_cur_t cursor; |
| 5045 |
ulint divergence_level; |
| 5046 |
int64_t n_rows; |
| 5047 |
ulint i; |
| 5048 |
mtr_t mtr; |
| 5049 |
int64_t table_n_rows; |
| 5050 |
|
| 5051 |
table_n_rows = dict_table_get_n_rows(index->table); |
| 5052 |
|
| 5053 |
/* Below we dive to the two records specified by tuple1 and tuple2 and |
| 5054 |
we remember the entire dive paths from the tree root. The place where |
| 5055 |
the tuple1 path ends on the leaf level we call "left border" of our |
| 5056 |
interval and the place where the tuple2 path ends on the leaf level - |
| 5057 |
"right border". We take care to either include or exclude the interval |
| 5058 |
boundaries depending on whether <, <=, > or >= was specified. For |
| 5059 |
example if "5 < x AND x <= 10" then we should not include the left |
| 5060 |
boundary, but should include the right one. */ |
| 5061 |
|
| 5062 |
mtr_start(&mtr); |
| 5063 |
|
| 5064 |
cursor.path_arr = path1.data(); |
| 5065 |
|
| 5066 |
bool should_count_the_left_border; |
| 5067 |
|
| 5068 |
if (dtuple_get_n_fields(tuple1) > 0) { |
| 5069 |
btr_cur_search_to_nth_level(index, 0, tuple1, mode1, |
| 5070 |
BTR_SEARCH_LEAF | BTR_ESTIMATE, &cursor, 0, |
| 5071 |
__FILE__, __LINE__, &mtr); |
| 5072 |
|
| 5073 |
ut_ad(!page_rec_is_infimum(btr_cur_get_rec(&cursor))); |
| 5074 |
|
| 5075 |
/* We should count the border if there are any records to |
| 5076 |
match the criteria, i.e. if the maximum record on the tree is |
| 5077 |
5 and x > 3 is specified then the cursor will be positioned at |
| 5078 |
5 and we should count the border, but if x > 7 is specified, |
| 5079 |
then the cursor will be positioned at 'sup' on the rightmost |
| 5080 |
leaf page in the tree and we should not count the border. */ |
| 5081 |
should_count_the_left_border = |
| 5082 |
!page_rec_is_supremum(btr_cur_get_rec(&cursor)); |
| 5083 |
} else { |
| 5084 |
btr_cur_open_at_index_side(true, index, BTR_SEARCH_LEAF | BTR_ESTIMATE, |
| 5085 |
&cursor, 0, UT_LOCATION_HERE, &mtr); |
| 5086 |
|
| 5087 |
ut_ad(page_rec_is_infimum(btr_cur_get_rec(&cursor))); |
| 5088 |
|
| 5089 |
/* The range specified is without a left border, just |
| 5090 |
'x < 123' or 'x <= 123' and btr_cur_open_at_index_side() |
| 5091 |
positioned the cursor on the infimum record on the leftmost |
| 5092 |
page, which must not be counted. */ |
| 5093 |
should_count_the_left_border = false; |
| 5094 |
} |
| 5095 |
|
| 5096 |
mtr_commit(&mtr); |
| 5097 |
|
| 5098 |
mtr_start(&mtr); |
| 5099 |
|
| 5100 |
cursor.path_arr = path2.data(); |
| 5101 |
|
| 5102 |
bool should_count_the_right_border; |
| 5103 |
|
| 5104 |
if (dtuple_get_n_fields(tuple2) > 0) { |
| 5105 |
btr_cur_search_to_nth_level(index, 0, tuple2, mode2, |
| 5106 |
BTR_SEARCH_LEAF | BTR_ESTIMATE, &cursor, 0, |
| 5107 |
__FILE__, __LINE__, &mtr); |
| 5108 |
|
| 5109 |
const rec_t *rec = btr_cur_get_rec(&cursor); |
| 5110 |
|
| 5111 |
ut_ad(!(mode2 == PAGE_CUR_L && page_rec_is_supremum(rec))); |
| 5112 |
|
| 5113 |
should_count_the_right_border = |
| 5114 |
(mode2 == PAGE_CUR_LE /* if the range is '<=' */ |
| 5115 |
/* and the record was found */ |
| 5116 |
&& cursor.low_match >= dtuple_get_n_fields(tuple2)) || |
| 5117 |
(mode2 == PAGE_CUR_L /* or if the range is '<' */ |
| 5118 |
/* and there are any records to match the criteria, |
| 5119 |
i.e. if the minimum record on the tree is 5 and |
| 5120 |
x < 7 is specified then the cursor will be |
| 5121 |
positioned at 5 and we should count the border, but |
| 5122 |
if x < 2 is specified, then the cursor will be |
| 5123 |
positioned at 'inf' and we should not count the |
| 5124 |
border */ |
| 5125 |
&& !page_rec_is_infimum(rec)); |
| 5126 |
/* Notice that for "WHERE col <= 'foo'" MySQL passes to |
| 5127 |
ha_innobase::records_in_range(): |
| 5128 |
min_key=NULL (left-unbounded) which is expected |
| 5129 |
max_key='foo' flag=HA_READ_AFTER_KEY (PAGE_CUR_G), which is |
| 5130 |
unexpected - one would expect |
| 5131 |
flag=HA_READ_KEY_OR_PREV (PAGE_CUR_LE). In this case the |
| 5132 |
cursor will be positioned on the first record to the right of |
| 5133 |
the requested one (can also be positioned on the 'sup') and |
| 5134 |
we should not count the right border. */ |
| 5135 |
} else { |
| 5136 |
btr_cur_open_at_index_side(false, index, BTR_SEARCH_LEAF | BTR_ESTIMATE, |
| 5137 |
&cursor, 0, UT_LOCATION_HERE, &mtr); |
| 5138 |
|
| 5139 |
ut_ad(page_rec_is_supremum(btr_cur_get_rec(&cursor))); |
| 5140 |
|
| 5141 |
/* The range specified is without a right border, just |
| 5142 |
'x > 123' or 'x >= 123' and btr_cur_open_at_index_side() |
| 5143 |
positioned the cursor on the supremum record on the rightmost |
| 5144 |
page, which must not be counted. */ |
| 5145 |
should_count_the_right_border = false; |
| 5146 |
} |
| 5147 |
|
| 5148 |
mtr_commit(&mtr); |
| 5149 |
|
| 5150 |
/* We have the path information for the range in path1 and path2 */ |
| 5151 |
|
| 5152 |
n_rows = 0; |
| 5153 |
auto is_n_rows_exact = true; |
| 5154 |
|
| 5155 |
/* This becomes true when the two paths do not pass through the |
| 5156 |
same pages anymore. */ |
| 5157 |
auto diverged = false; |
| 5158 |
|
| 5159 |
/* This becomes true when the paths are not the same or adjacent |
| 5160 |
any more. This means that they pass through the same or |
| 5161 |
neighboring-on-the-same-level pages only. */ |
| 5162 |
auto diverged_lot = false; |
| 5163 |
|
| 5164 |
/* This is the level where paths diverged a lot. */ |
| 5165 |
divergence_level = 1000000; |
| 5166 |
|
| 5167 |
btr_path_t *slot1{}; |
| 5168 |
btr_path_t *slot2{}; |
| 5169 |
|
| 5170 |
ut_a(path1.size() == path2.size()); |
| 5171 |
ut_a(path1.size() == BTR_PATH_ARRAY_N_SLOTS); |
| 5172 |
|
| 5173 |
for (i = 0;; ++i) { |
| 5174 |
ut_ad(i < BTR_PATH_ARRAY_N_SLOTS); |
| 5175 |
|
| 5176 |
slot1 = &path1[i]; |
| 5177 |
slot2 = &path2[i]; |
| 5178 |
|
| 5179 |
if (slot1->nth_rec == ULINT_UNDEFINED || |
| 5180 |
slot2->nth_rec == ULINT_UNDEFINED) { |
| 5181 |
/* Here none of the borders were counted. For example, |
| 5182 |
if on the leaf level we descended to: |
| 5183 |
(inf, a, b, c, d, e, f, sup) |
| 5184 |
^ ^ |
| 5185 |
path1 path2 |
| 5186 |
then n_rows will be 2 (c and d). */ |
| 5187 |
|
| 5188 |
if (is_n_rows_exact) { |
| 5189 |
/* Only fiddle to adjust this off-by-one |
| 5190 |
if the number is exact, otherwise we do |
| 5191 |
much grosser adjustments below. */ |
| 5192 |
|
| 5193 |
auto last1 = &path1[i - 1]; |
| 5194 |
auto last2 = &path2[i - 1]; |
| 5195 |
|
| 5196 |
/* If both paths end up on the same record on |
| 5197 |
the leaf level. */ |
| 5198 |
if (last1->page_no == last2->page_no && |
| 5199 |
last1->nth_rec == last2->nth_rec) { |
| 5200 |
/* n_rows can be > 0 here if the paths |
| 5201 |
were first different and then converged |
| 5202 |
to the same record on the leaf level. |
| 5203 |
For example: |
| 5204 |
SELECT ... LIKE 'wait/synch/rwlock%' |
| 5205 |
mode1=PAGE_CUR_GE, |
| 5206 |
tuple1="wait/synch/rwlock" |
| 5207 |
path1[0]={nth_rec=58, n_recs=58, |
| 5208 |
page_no=3, page_level=1} |
| 5209 |
path1[1]={nth_rec=56, n_recs=55, |
| 5210 |
page_no=119, page_level=0} |
| 5211 |
|
| 5212 |
mode2=PAGE_CUR_G |
| 5213 |
tuple2="wait/synch/rwlock" |
| 5214 |
path2[0]={nth_rec=57, n_recs=57, |
| 5215 |
page_no=3, page_level=1} |
| 5216 |
path2[1]={nth_rec=56, n_recs=55, |
| 5217 |
page_no=119, page_level=0} */ |
| 5218 |
|
| 5219 |
/* If the range is such that we should |
| 5220 |
count both borders, then avoid |
| 5221 |
counting that record twice - once as a |
| 5222 |
left border and once as a right |
| 5223 |
border. */ |
| 5224 |
if (should_count_the_left_border && should_count_the_right_border) { |
| 5225 |
n_rows = 1; |
| 5226 |
} else { |
| 5227 |
/* Some of the borders should |
| 5228 |
not be counted, e.g. [3,3). */ |
| 5229 |
n_rows = 0; |
| 5230 |
} |
| 5231 |
} else { |
| 5232 |
if (should_count_the_left_border) { |
| 5233 |
n_rows++; |
| 5234 |
} |
| 5235 |
|
| 5236 |
if (should_count_the_right_border) { |
| 5237 |
n_rows++; |
| 5238 |
} |
| 5239 |
} |
| 5240 |
} |
| 5241 |
|
| 5242 |
if (i > divergence_level + 1 && !is_n_rows_exact) { |
| 5243 |
/* In trees whose height is > 1 our algorithm |
| 5244 |
tends to underestimate: multiply the estimate |
| 5245 |
by 2: */ |
| 5246 |
|
| 5247 |
n_rows = n_rows * 2; |
| 5248 |
} |
| 5249 |
|
| 5250 |
DBUG_EXECUTE_IF("bug14007649", return (n_rows);); |
| 5251 |
|
| 5252 |
/* Do not estimate the number of rows in the range |
| 5253 |
to over 1 / 2 of the estimated rows in the whole |
| 5254 |
table */ |
| 5255 |
|
| 5256 |
if (n_rows > table_n_rows / 2 && !is_n_rows_exact) { |
| 5257 |
n_rows = table_n_rows / 2; |
| 5258 |
|
| 5259 |
/* If there are just 0 or 1 rows in the table, |
| 5260 |
then we estimate all rows are in the range */ |
| 5261 |
|
| 5262 |
if (n_rows == 0) { |
| 5263 |
n_rows = table_n_rows; |
| 5264 |
} |
| 5265 |
} |
| 5266 |
|
| 5267 |
return (n_rows); |
| 5268 |
} |
| 5269 |
|
| 5270 |
if (!diverged && slot1->nth_rec != slot2->nth_rec) { |
| 5271 |
/* If both slots do not point to the same page, |
| 5272 |
this means that the tree must have changed between |
| 5273 |
the dive for slot1 and the dive for slot2 at the |
| 5274 |
beginning of this function. */ |
| 5275 |
if (slot1->page_no != slot2->page_no || |
| 5276 |
slot1->page_level != slot2->page_level) { |
| 5277 |
/* If the tree keeps changing even after a |
| 5278 |
few attempts, then just return some arbitrary |
| 5279 |
number. */ |
| 5280 |
if (nth_attempt >= rows_in_range_max_retries) { |
| 5281 |
return (rows_in_range_arbitrary_ret_val); |
| 5282 |
} |
| 5283 |
|
| 5284 |
const int64_t ret = btr_estimate_n_rows_in_range_low( |
| 5285 |
index, tuple1, mode1, tuple2, mode2, nth_attempt + 1); |
| 5286 |
|
| 5287 |
return (ret); |
| 5288 |
} |
| 5289 |
|
| 5290 |
diverged = true; |
| 5291 |
|
| 5292 |
if (slot1->nth_rec < slot2->nth_rec) { |
| 5293 |
/* We do not count the borders (nor the left |
| 5294 |
nor the right one), thus "- 1". */ |
| 5295 |
n_rows = slot2->nth_rec - slot1->nth_rec - 1; |
| 5296 |
|
| 5297 |
if (n_rows > 0) { |
| 5298 |
/* There is at least one row between |
| 5299 |
the two borders pointed to by slot1 |
| 5300 |
and slot2, so on the level below the |
| 5301 |
slots will point to non-adjacent |
| 5302 |
pages. */ |
| 5303 |
diverged_lot = true; |
| 5304 |
divergence_level = i; |
| 5305 |
} |
| 5306 |
} else { |
| 5307 |
/* It is possible that |
| 5308 |
slot1->nth_rec >= slot2->nth_rec |
| 5309 |
if, for example, we have a single page |
| 5310 |
tree which contains (inf, 5, 6, supr) |
| 5311 |
and we select where x > 20 and x < 30; |
| 5312 |
in this case slot1->nth_rec will point |
| 5313 |
to the supr record and slot2->nth_rec |
| 5314 |
will point to 6. */ |
| 5315 |
n_rows = 0; |
| 5316 |
should_count_the_left_border = false; |
| 5317 |
should_count_the_right_border = false; |
| 5318 |
} |
| 5319 |
|
| 5320 |
} else if (diverged && !diverged_lot) { |
| 5321 |
if (slot1->nth_rec < slot1->n_recs || slot2->nth_rec > 1) { |
| 5322 |
diverged_lot = true; |
| 5323 |
divergence_level = i; |
| 5324 |
|
| 5325 |
n_rows = 0; |
| 5326 |
|
| 5327 |
if (slot1->nth_rec < slot1->n_recs) { |
| 5328 |
n_rows += slot1->n_recs - slot1->nth_rec; |
| 5329 |
} |
| 5330 |
|
| 5331 |
if (slot2->nth_rec > 1) { |
| 5332 |
n_rows += slot2->nth_rec - 1; |
| 5333 |
} |
| 5334 |
} |
| 5335 |
} else if (diverged_lot) { |
| 5336 |
n_rows = btr_estimate_n_rows_in_range_on_level(index, slot1, slot2, |
| 5337 |
n_rows, &is_n_rows_exact); |
| 5338 |
} |
| 5339 |
} |
| 5340 |
} |
| 5341 |
|
| 5342 |
/** Estimates the number of rows in a given index range. |
| 5343 |
@param[in] index index |
| 5344 |
@param[in] tuple1 range start, may also be empty tuple |
| 5345 |
@param[in] mode1 search mode for range start |
| 5346 |
@param[in] tuple2 range end, may also be empty tuple |
| 5347 |
@param[in] mode2 search mode for range end |
| 5348 |
@return estimated number of rows */ |
| 5349 |
int64_t btr_estimate_n_rows_in_range(dict_index_t *index, |
| 5350 |
const dtuple_t *tuple1, |
| 5351 |
page_cur_mode_t mode1, |
| 5352 |
const dtuple_t *tuple2, |
| 5353 |
page_cur_mode_t mode2) { |
| 5354 |
const int64_t ret = btr_estimate_n_rows_in_range_low( |
| 5355 |
index, tuple1, mode1, tuple2, mode2, 1 /* first attempt */); |
| 5356 |
|
| 5357 |
return (ret); |
| 5358 |
} |
| 5359 |
|
| 5360 |
/** Record the number of non_null key values in a given index for |
| 5361 |
each n-column prefix of the index where 1 <= n <= |
| 5362 |
dict_index_get_n_unique(index). The estimates are eventually stored in the |
| 5363 |
array: index->stat_n_non_null_key_vals[], which is indexed from 0 to n-1. |
| 5364 |
@param[in] index index |
| 5365 |
@param[in] n_unique dict_index_get_n_unique(index), number of columns |
| 5366 |
uniquely determine an index entry |
| 5367 |
@param[in] offsets rec_get_offsets(rec, index), its size could be for |
| 5368 |
all fields or that of "n_unique" |
| 5369 |
@param[in,out] n_not_null array to record number of not null rows for n-column |
| 5370 |
prefix */ |
| 5371 |
static void btr_record_not_null_field_in_rec(const dict_index_t *index, |
| 5372 |
ulint n_unique, |
| 5373 |
const ulint *offsets, |
| 5374 |
uint64_t *n_not_null) { |
| 5375 |
ulint i; |
| 5376 |
|
| 5377 |
ut_ad(rec_offs_n_fields(offsets) >= n_unique); |
| 5378 |
|
| 5379 |
if (n_not_null == nullptr) { |
| 5380 |
return; |
| 5381 |
} |
| 5382 |
|
| 5383 |
for (i = 0; i < n_unique; i++) { |
| 5384 |
if (rec_offs_nth_sql_null(index, offsets, i)) { |
| 5385 |
break; |
| 5386 |
} |
| 5387 |
|
| 5388 |
n_not_null[i]++; |
| 5389 |
} |
| 5390 |
} |
| 5391 |
|
| 5392 |
/** Estimates the number of different key values in a given index, for |
| 5393 |
each n-column prefix of the index where 1 <= n <= |
| 5394 |
dict_index_get_n_unique(index). The estimates are stored in the array |
| 5395 |
index->stat_n_diff_key_vals[] (indexed 0..n_uniq-1) and the number of pages |
| 5396 |
that were sampled is saved in index->stat_n_sample_sizes[]. If |
| 5397 |
innodb_stats_method is nulls_ignored, we also record the number of non-null |
| 5398 |
values for each prefix and stored the estimates in array |
| 5399 |
index->stat_n_non_null_key_vals. |
| 5400 |
@return true if the index is available and we get the estimated numbers, |
| 5401 |
false if the index is unavailable. */ |
| 5402 |
bool btr_estimate_number_of_different_key_vals( |
| 5403 |
dict_index_t *index) /*!< in: index */ |
| 5404 |
{ |
| 5405 |
btr_cur_t cursor; |
| 5406 |
page_t *page; |
| 5407 |
rec_t *rec; |
| 5408 |
ulint n_cols; |
| 5409 |
uint64_t *n_diff; |
| 5410 |
uint64_t *n_not_null; |
| 5411 |
bool stats_null_not_equal; |
| 5412 |
uintmax_t n_sample_pages; /* number of pages to sample */ |
| 5413 |
ulint not_empty_flag = 0; |
| 5414 |
ulint total_external_size = 0; |
| 5415 |
ulint i; |
| 5416 |
ulint j; |
| 5417 |
uintmax_t add_on; |
| 5418 |
mtr_t mtr; |
| 5419 |
mem_heap_t *heap = nullptr; |
| 5420 |
ulint *offsets_rec = nullptr; |
| 5421 |
ulint *offsets_next_rec = nullptr; |
| 5422 |
|
| 5423 |
/* For spatial index, there is no such stats can be |
| 5424 |
fetched. */ |
| 5425 |
if (dict_index_is_spatial(index)) { |
| 5426 |
return (false); |
| 5427 |
} |
| 5428 |
|
| 5429 |
n_cols = dict_index_get_n_unique(index); |
| 5430 |
|
| 5431 |
heap = |
| 5432 |
mem_heap_create((sizeof *n_diff + sizeof *n_not_null) * n_cols + |
| 5433 |
dict_index_get_n_fields(index) * |
| 5434 |
(sizeof *offsets_rec + sizeof *offsets_next_rec), |
| 5435 |
UT_LOCATION_HERE); |
| 5436 |
|
| 5437 |
n_diff = (uint64_t *)mem_heap_zalloc(heap, n_cols * sizeof(n_diff[0])); |
| 5438 |
|
| 5439 |
n_not_null = nullptr; |
| 5440 |
|
| 5441 |
/* Check srv_innodb_stats_method setting, and decide whether we |
| 5442 |
need to record non-null value and also decide if NULL is |
| 5443 |
considered equal (by setting stats_null_not_equal value) */ |
| 5444 |
switch (srv_innodb_stats_method) { |
| 5445 |
case SRV_STATS_NULLS_IGNORED: |
| 5446 |
n_not_null = |
| 5447 |
(uint64_t *)mem_heap_zalloc(heap, n_cols * sizeof *n_not_null); |
| 5448 |
[[fallthrough]]; |
| 5449 |
|
| 5450 |
case SRV_STATS_NULLS_UNEQUAL: |
| 5451 |
/* for both SRV_STATS_NULLS_IGNORED and SRV_STATS_NULLS_UNEQUAL |
| 5452 |
case, we will treat NULLs as unequal value */ |
| 5453 |
stats_null_not_equal = true; |
| 5454 |
break; |
| 5455 |
|
| 5456 |
case SRV_STATS_NULLS_EQUAL: |
| 5457 |
stats_null_not_equal = false; |
| 5458 |
break; |
| 5459 |
|
| 5460 |
default: |
| 5461 |
ut_error; |
| 5462 |
} |
| 5463 |
|
| 5464 |
/* It makes no sense to test more pages than are contained |
| 5465 |
in the index, thus we lower the number if it is too high */ |
| 5466 |
if (srv_stats_transient_sample_pages > index->stat_index_size) { |
| 5467 |
if (index->stat_index_size > 0) { |
| 5468 |
n_sample_pages = index->stat_index_size; |
| 5469 |
} else { |
| 5470 |
n_sample_pages = 1; |
| 5471 |
} |
| 5472 |
} else { |
| 5473 |
n_sample_pages = srv_stats_transient_sample_pages; |
| 5474 |
} |
| 5475 |
|
| 5476 |
/* We sample some pages in the index to get an estimate */ |
| 5477 |
|
| 5478 |
for (i = 0; i < n_sample_pages; i++) { |
| 5479 |
mtr_start(&mtr); |
| 5480 |
|
| 5481 |
bool available; |
| 5482 |
|
| 5483 |
available = btr_cur_open_at_rnd_pos(index, BTR_SEARCH_LEAF, &cursor, |
| 5484 |
__FILE__, __LINE__, &mtr); |
| 5485 |
|
| 5486 |
if (!available) { |
| 5487 |
mtr_commit(&mtr); |
| 5488 |
mem_heap_free(heap); |
| 5489 |
|
| 5490 |
return (false); |
| 5491 |
} |
| 5492 |
|
| 5493 |
/* Count the number of different key values for each prefix of |
| 5494 |
the key on this index page. If the prefix does not determine |
| 5495 |
the index record uniquely in the B-tree, then we subtract one |
| 5496 |
because otherwise our algorithm would give a wrong estimate |
| 5497 |
for an index where there is just one key value. */ |
| 5498 |
|
| 5499 |
page = btr_cur_get_page(&cursor); |
| 5500 |
|
| 5501 |
rec = page_rec_get_next(page_get_infimum_rec(page)); |
| 5502 |
|
| 5503 |
if (!page_rec_is_supremum(rec)) { |
| 5504 |
not_empty_flag = 1; |
| 5505 |
offsets_rec = rec_get_offsets(rec, index, offsets_rec, ULINT_UNDEFINED, |
| 5506 |
UT_LOCATION_HERE, &heap); |
| 5507 |
|
| 5508 |
if (n_not_null != nullptr) { |
| 5509 |
btr_record_not_null_field_in_rec(index, n_cols, offsets_rec, |
| 5510 |
n_not_null); |
| 5511 |
} |
| 5512 |
} |
| 5513 |
|
| 5514 |
while (!page_rec_is_supremum(rec)) { |
| 5515 |
ulint matched_fields; |
| 5516 |
rec_t *next_rec = page_rec_get_next(rec); |
| 5517 |
if (page_rec_is_supremum(next_rec)) { |
| 5518 |
total_external_size += |
| 5519 |
lob::btr_rec_get_externally_stored_len(index, rec, offsets_rec); |
| 5520 |
break; |
| 5521 |
} |
| 5522 |
|
| 5523 |
offsets_next_rec = |
| 5524 |
rec_get_offsets(next_rec, index, offsets_next_rec, ULINT_UNDEFINED, |
| 5525 |
UT_LOCATION_HERE, &heap); |
| 5526 |
|
| 5527 |
cmp_rec_rec_with_match(rec, next_rec, offsets_rec, offsets_next_rec, |
| 5528 |
index, page_is_spatial_non_leaf(next_rec, index), |
| 5529 |
stats_null_not_equal, &matched_fields); |
| 5530 |
|
| 5531 |
for (j = matched_fields; j < n_cols; j++) { |
| 5532 |
/* We add one if this index record has |
| 5533 |
a different prefix from the previous */ |
| 5534 |
|
| 5535 |
n_diff[j]++; |
| 5536 |
} |
| 5537 |
|
| 5538 |
if (n_not_null != nullptr) { |
| 5539 |
btr_record_not_null_field_in_rec(index, n_cols, offsets_next_rec, |
| 5540 |
n_not_null); |
| 5541 |
} |
| 5542 |
|
| 5543 |
total_external_size += |
| 5544 |
lob::btr_rec_get_externally_stored_len(index, rec, offsets_rec); |
| 5545 |
|
| 5546 |
rec = next_rec; |
| 5547 |
/* Initialize offsets_rec for the next round |
| 5548 |
and assign the old offsets_rec buffer to |
| 5549 |
offsets_next_rec. */ |
| 5550 |
{ |
| 5551 |
ulint *offsets_tmp = offsets_rec; |
| 5552 |
offsets_rec = offsets_next_rec; |
| 5553 |
offsets_next_rec = offsets_tmp; |
| 5554 |
} |
| 5555 |
} |
| 5556 |
|
| 5557 |
if (n_cols == dict_index_get_n_unique_in_tree(index)) { |
| 5558 |
/* If there is more than one leaf page in the tree, |
| 5559 |
we add one because we know that the first record |
| 5560 |
on the page certainly had a different prefix than the |
| 5561 |
last record on the previous index page in the |
| 5562 |
alphabetical order. Before this fix, if there was |
| 5563 |
just one big record on each clustered index page, the |
| 5564 |
algorithm grossly underestimated the number of rows |
| 5565 |
in the table. */ |
| 5566 |
|
| 5567 |
if (btr_page_get_prev(page, &mtr) != FIL_NULL || |
| 5568 |
btr_page_get_next(page, &mtr) != FIL_NULL) { |
| 5569 |
n_diff[n_cols - 1]++; |
| 5570 |
} |
| 5571 |
} |
| 5572 |
|
| 5573 |
mtr_commit(&mtr); |
| 5574 |
} |
| 5575 |
|
| 5576 |
/* If we saw k borders between different key values on |
| 5577 |
n_sample_pages leaf pages, we can estimate how many |
| 5578 |
there will be in index->stat_n_leaf_pages */ |
| 5579 |
|
| 5580 |
/* We must take into account that our sample actually represents |
| 5581 |
also the pages used for external storage of fields (those pages are |
| 5582 |
included in index->stat_n_leaf_pages) */ |
| 5583 |
|
| 5584 |
for (j = 0; j < n_cols; j++) { |
| 5585 |
index->stat_n_diff_key_vals[j] = BTR_TABLE_STATS_FROM_SAMPLE( |
| 5586 |
n_diff[j], index, n_sample_pages, total_external_size, not_empty_flag); |
| 5587 |
|
| 5588 |
/* If the tree is small, smaller than |
| 5589 |
10 * n_sample_pages + total_external_size, then |
| 5590 |
the above estimate is ok. For bigger trees it is common that we |
| 5591 |
do not see any borders between key values in the few pages |
| 5592 |
we pick. But still there may be n_sample_pages |
| 5593 |
different key values, or even more. Let us try to approximate |
| 5594 |
that: */ |
| 5595 |
|
| 5596 |
add_on = index->stat_n_leaf_pages / |
| 5597 |
(10 * (n_sample_pages + total_external_size)); |
| 5598 |
|
| 5599 |
if (add_on > n_sample_pages) { |
| 5600 |
add_on = n_sample_pages; |
| 5601 |
} |
| 5602 |
|
| 5603 |
index->stat_n_diff_key_vals[j] += add_on; |
| 5604 |
|
| 5605 |
index->stat_n_sample_sizes[j] = n_sample_pages; |
| 5606 |
|
| 5607 |
/* Update the stat_n_non_null_key_vals[] with our |
| 5608 |
sampled result. stat_n_non_null_key_vals[] is created |
| 5609 |
and initialized to zero in dict_index_add_to_cache(), |
| 5610 |
along with stat_n_diff_key_vals[] array */ |
| 5611 |
if (n_not_null != nullptr) { |
| 5612 |
index->stat_n_non_null_key_vals[j] = |
| 5613 |
BTR_TABLE_STATS_FROM_SAMPLE(n_not_null[j], index, n_sample_pages, |
| 5614 |
total_external_size, not_empty_flag); |
| 5615 |
} |
| 5616 |
} |
| 5617 |
|
| 5618 |
mem_heap_free(heap); |
| 5619 |
|
| 5620 |
return (true); |
| 5621 |
} |
| 5622 |
|
| 5623 |
#endif /* !UNIV_HOTBACKUP */ |