Debian Codegraph @

storage/innobase/btr/btr0btr.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1994, 2026, Oracle and/or its affiliates.
4 Copyright (c) 2012, Facebook Inc.
5
6 This program is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License, version 2.0, as published by the
8 Free Software Foundation.
9
10 This program is designed to work with certain software (including
11 but not limited to OpenSSL) that is licensed under separate terms,
12 as designated in a particular file or component or in included license
13 documentation. The authors of MySQL hereby grant you an additional
14 permission to link the program and your derivative works with the
15 separately licensed software that they have either included with
16 the program or referenced in the documentation.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
20 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
21 for more details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc.,
25 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26
27 *****************************************************************************/
28
29 /** @file btr/btr0btr.cc
30 The B-tree
31
32 Created 6/2/1994 Heikki Tuuri
33 *******************************************************/
34
35 #include "btr0btr.h"
36
37 #include <sys/types.h>
38
39 #ifndef UNIV_HOTBACKUP
40 #include "btr0cur.h"
41 #include "btr0pcur.h"
42 #include "btr0sea.h"
43 #include "buf0stats.h"
44 #include "dict0boot.h"
45 #include "fsp0sysspace.h"
46 #include "gis0geo.h"
47 #include "gis0rtree.h"
48 #include "ibuf0ibuf.h"
49 #include "lock0lock.h"
50 #include "my_dbug.h"
51 #include "page0page.h"
52 #include "page0zip.h"
53 #include "rem0cmp.h"
54 #include "srv0mon.h"
55 #include "trx0trx.h"
56 #include "ut0new.h"
57 #endif /* !UNIV_HOTBACKUP */
58
59 #ifndef UNIV_HOTBACKUP
60 /** Checks if the page in the cursor can be merged with given page.
61 If necessary, re-organize the merge_page.
62 @return true if possible to merge. */
63 static bool btr_can_merge_with_page(
64 btr_cur_t *cursor, /*!< in: cursor on the page to merge */
65 page_no_t page_no, /*!< in: a sibling page */
66 buf_block_t **merge_block, /*!< out: the merge block */
67 mtr_t *mtr); /*!< in: mini-transaction */
68 #endif /* !UNIV_HOTBACKUP */
69
70 /** Report that an index page is corrupted. */
71 void btr_corruption_report(const buf_block_t *block, /*!< in: corrupted block */
72 const dict_index_t *index) /*!< in: index tree */
73 {
74 ib::error(ER_IB_MSG_27) << "Flag mismatch in page " << block->page.id
75 << " index " << index->name << " of table "
76 << index->table->name;
77 }
78
79 #ifndef UNIV_HOTBACKUP
80 /*
81 Latching strategy of the InnoDB B-tree
82 --------------------------------------
83 A tree latch protects all non-leaf nodes of the tree. Each node of a tree
84 also has a latch of its own.
85
86 A B-tree operation normally first acquires an S-latch on the tree. It
87 searches down the tree and releases the tree latch when it has the
88 leaf node latch. To save CPU time we do not acquire any latch on
89 non-leaf nodes of the tree during a search, those pages are only bufferfixed.
90
91 If an operation needs to restructure the tree, it acquires an X-latch on
92 the tree before searching to a leaf node. If it needs, for example, to
93 split a leaf,
94 (1) InnoDB decides the split point in the leaf,
95 (2) allocates a new page,
96 (3) inserts the appropriate node pointer to the first non-leaf level,
97 (4) releases the tree X-latch,
98 (5) and then moves records from the leaf to the new allocated page.
99
100 Node pointers
101 -------------
102 Leaf pages of a B-tree contain the index records stored in the
103 tree. On levels n > 0 we store 'node pointers' to pages on level
104 n - 1. For each page there is exactly one node pointer stored:
105 thus the our tree is an ordinary B-tree, not a B-link tree.
106
107 A node pointer contains a prefix P of an index record. The prefix
108 is long enough so that it determines an index record uniquely.
109 The file page number of the child page is added as the last
110 field. To the child page we can store node pointers or index records
111 which are >= P in the alphabetical order, but < P1 if there is
112 a next node pointer on the level, and P1 is its prefix.
113
114 If a node pointer with a prefix P points to a non-leaf child,
115 then the leftmost record in the child must have the same
116 prefix P. If it points to a leaf node, the child is not required
117 to contain any record with a prefix equal to P. The leaf case
118 is decided this way to allow arbitrary deletions in a leaf node
119 without touching upper levels of the tree.
120
121 We have predefined a special minimum record which we
122 define as the smallest record in any alphabetical order.
123 A minimum record is denoted by setting a bit in the record
124 header. A minimum record acts as the prefix of a node pointer
125 which points to a leftmost node on any level of the tree.
126
127 File page allocation
128 --------------------
129 In the root node of a B-tree there are two file segment headers.
130 The leaf pages of a tree are allocated from one file segment, to
131 make them consecutive on disk if possible. From the other file segment
132 we allocate pages for the non-leaf levels of the tree.
133 */
134
135 #ifdef UNIV_BTR_DEBUG
136 /** Checks a file segment header within a B-tree root page.
137 @return true if valid */
138 static bool btr_root_fseg_validate(
139 const fseg_header_t *seg_header, /*!< in: segment header */
140 space_id_t space) /*!< in: tablespace identifier */
141 {
142 ulint offset = mach_read_from_2(seg_header + FSEG_HDR_OFFSET);
143
144 ut_a(mach_read_from_4(seg_header + FSEG_HDR_SPACE) == space);
145 ut_a(offset >= FIL_PAGE_DATA);
146 ut_a(offset <= UNIV_PAGE_SIZE - FIL_PAGE_DATA_END);
147 return true;
148 }
149 #endif /* UNIV_BTR_DEBUG */
150
151 /** Gets the root node of a tree and x- or s-latches it.
152 @return root page, x- or s-latched */
153 buf_block_t *btr_root_block_get(
154 const dict_index_t *index, /*!< in: index tree */
155 ulint mode, /*!< in: either RW_S_LATCH
156 or RW_X_LATCH */
157 mtr_t *mtr) /*!< in: mtr */
158 {
159 const space_id_t space_id = dict_index_get_space(index);
160 const page_id_t page_id(space_id, dict_index_get_page(index));
161 const page_size_t page_size(dict_table_page_size(index->table));
162
163 buf_block_t *block =
164 btr_block_get(page_id, page_size, mode, UT_LOCATION_HERE, index, mtr);
165
166 btr_assert_not_corrupted(block, index);
167 #ifdef UNIV_BTR_DEBUG
168 if (!dict_index_is_ibuf(index)) {
169 const page_t *root = buf_block_get_frame(block);
170
171 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF + root,
172 space_id));
173 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP + root,
174 space_id));
175 }
176 #endif /* UNIV_BTR_DEBUG */
177
178 return (block);
179 }
180
181 /** Gets the root node of a tree and sx-latches it for segment access.
182 @return root page, sx-latched */
183 page_t *btr_root_get(const dict_index_t *index, /*!< in: index tree */
184 mtr_t *mtr) /*!< in: mtr */
185 {
186 /* Intended to be used for segment list access.
187 SX lock doesn't block reading user data by other threads.
188 And block the segment list access by others.*/
189 return (buf_block_get_frame(btr_root_block_get(index, RW_SX_LATCH, mtr)));
190 }
191
192 /** Gets the height of the B-tree (the level of the root, when the leaf
193 level is assumed to be 0). The caller must hold an S or X latch on
194 the index.
195 @return tree height (level of the root) */
196 ulint btr_height_get(dict_index_t *index, /*!< in: index tree */
197 mtr_t *mtr) /*!< in/out: mini-transaction */
198 {
199 ulint height;
200 buf_block_t *root_block;
201
202 ut_ad(srv_read_only_mode ||
203 mtr_memo_contains_flagged(
204 mtr, dict_index_get_lock(index),
205 MTR_MEMO_S_LOCK | MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
206 index->table->is_intrinsic());
207
208 /* S latches the page */
209 root_block = btr_root_block_get(index, RW_S_LATCH, mtr);
210
211 height = btr_page_get_level(buf_block_get_frame(root_block));
212
213 /* Release the S latch on the root page. */
214 mtr->memo_release(root_block, MTR_MEMO_PAGE_S_FIX);
215
216 ut_d(sync_check_unlock(&root_block->lock));
217
218 return (height);
219 }
220
221 /** Checks a file segment header within a B-tree root page and updates
222 the segment header space id.
223 @return true if valid */
224 static bool btr_root_fseg_adjust_on_import(
225 fseg_header_t *seg_header, /*!< in/out: segment header */
226 page_zip_des_t *page_zip, /*!< in/out: compressed page,
227 or NULL */
228 space_id_t space, /*!< in: tablespace identifier */
229 mtr_t *mtr) /*!< in/out: mini-transaction */
230 {
231 page_no_t offset = mach_read_from_2(seg_header + FSEG_HDR_OFFSET);
232
233 if (offset < FIL_PAGE_DATA || offset > UNIV_PAGE_SIZE - FIL_PAGE_DATA_END) {
234 return false;
235
236 } else if (page_zip) {
237 mach_write_to_4(seg_header + FSEG_HDR_SPACE, space);
238 page_zip_write_header(page_zip, seg_header + FSEG_HDR_SPACE, 4, mtr);
239 } else {
240 mlog_write_ulint(seg_header + FSEG_HDR_SPACE, space, MLOG_4BYTES, mtr);
241 }
242
243 return true;
244 }
245
246 /** Checks and adjusts the root node of a tree during IMPORT TABLESPACE.
247 @return error code, or DB_SUCCESS */
248 dberr_t btr_root_adjust_on_import(
249 const dict_index_t *index) /*!< in: index tree */
250 {
251 dberr_t err;
252 mtr_t mtr;
253 page_t *page;
254 buf_block_t *block;
255 page_zip_des_t *page_zip;
256 dict_table_t *table = index->table;
257 const space_id_t space_id = dict_index_get_space(index);
258 const page_id_t page_id(space_id, dict_index_get_page(index));
259 const page_size_t page_size(dict_table_page_size(table));
260
261 DBUG_EXECUTE_IF("ib_import_trigger_corruption_3", return (DB_CORRUPTION););
262
263 mtr_start(&mtr);
264
265 mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO);
266
267 block = btr_block_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, index,
268 &mtr);
269
270 page = buf_block_get_frame(block);
271 page_zip = buf_block_get_page_zip(block);
272
273 if (!page_is_root(page)) {
274 err = DB_CORRUPTION;
275
276 } else if (index->is_clustered()) {
277 auto page_is_compact_format = page_is_comp(page);
278
279 /* Check if the page format and table format agree. */
280 if (page_is_compact_format != dict_table_is_comp(table)) {
281 err = DB_CORRUPTION;
282 } else {
283 /* Check that the table flags and the tablespace
284 flags match. */
285 uint32_t flags = dict_tf_to_fsp_flags(table->flags);
286 uint32_t fsp_flags = fil_space_get_flags(table->space);
287
288 /* We remove SDI flag from space flags temporarily for
289 comparison because the space flags derived from table
290 flags will not have SDI flag */
291 fsp_flags_unset_sdi(fsp_flags);
292
293 /* As encryption is not a table property, we don't keep
294 any encryption property related flag in table. Thus
295 exclude encryption flag as well. */
296 fsp_flags_unset_encryption(fsp_flags);
297
298 err = fsp_flags_are_equal(flags, fsp_flags) ? DB_SUCCESS : DB_CORRUPTION;
299 }
300 } else {
301 err = DB_SUCCESS;
302 }
303
304 /* Check and adjust the file segment headers, if all OK so far. */
305 if (err == DB_SUCCESS &&
306 (!btr_root_fseg_adjust_on_import(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF + page,
307 page_zip, space_id, &mtr) ||
308 !btr_root_fseg_adjust_on_import(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP + page,
309 page_zip, space_id, &mtr))) {
310 err = DB_CORRUPTION;
311 }
312
313 mtr_commit(&mtr);
314
315 return (err);
316 }
317
318 /** Creates a new index page (not the root, and also not
319 used in page reorganization). @see btr_page_empty(). */
320 void btr_page_create(
321 buf_block_t *block, /*!< in/out: page to be created */
322 page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */
323 dict_index_t *index, /*!< in: index */
324 ulint level, /*!< in: the B-tree level of the page */
325 mtr_t *mtr) /*!< in: mtr */
326 {
327 page_t *page = buf_block_get_frame(block);
328
329 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
330
331 uint16_t page_create_type;
332 if (dict_index_is_spatial(index)) {
333 page_create_type = FIL_PAGE_RTREE;
334 } else if (dict_index_is_sdi(index)) {
335 page_create_type = FIL_PAGE_SDI;
336 } else {
337 page_create_type = FIL_PAGE_INDEX;
338 }
339
340 if (page_zip) {
341 page_create_zip(block, index, level, 0, mtr, page_create_type);
342 } else {
343 page_create(block, mtr, dict_table_is_comp(index->table), page_create_type);
344 /* Set the level of the new index page */
345 btr_page_set_level(page, nullptr, level, mtr);
346 }
347
348 /* For Spatial Index, initialize the Split Sequence Number */
349 if (dict_index_is_spatial(index)) {
350 page_set_ssn_id(block, page_zip, 0, mtr);
351 }
352
353 btr_page_set_index_id(page, page_zip, index->id, mtr);
354
355 if (level == 0) {
356 buf_stat_per_index->inc(index_id_t(index->space, index->id));
357 }
358 }
359
360 /** Allocates a new file page to be used in an ibuf tree. Takes the page from
361 the free list of the tree, which must contain pages!
362 @return new allocated block, x-latched */
363 static buf_block_t *btr_page_alloc_for_ibuf(
364 dict_index_t *index, /*!< in: index tree */
365 mtr_t *mtr) /*!< in: mtr */
366 {
367 fil_addr_t node_addr;
368 page_t *root;
369 page_t *new_page;
370 buf_block_t *new_block;
371
372 root = btr_root_get(index, mtr);
373
374 node_addr = flst_get_first(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr);
375 ut_a(node_addr.page != FIL_NULL);
376
377 new_block = buf_page_get(
378 page_id_t(dict_index_get_space(index), node_addr.page),
379 dict_table_page_size(index->table), RW_X_LATCH, UT_LOCATION_HERE, mtr);
380
381 new_page = buf_block_get_frame(new_block);
382 buf_block_dbg_add_level(new_block, SYNC_IBUF_TREE_NODE_NEW);
383
384 flst_remove(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
385 new_page + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE, mtr);
386 ut_d(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr));
387
388 return (new_block);
389 }
390
391 /** Allocates a new file page to be used in an index tree. NOTE: we assume
392 that the caller has made the reservation for free extents!
393 @retval NULL if no page could be allocated
394 @retval block, rw_lock_x_lock_count(&block->lock) == 1 if allocation succeeded
395 (init_mtr == mtr, or the page was not previously freed in mtr),
396 returned block is not allocated nor initialized otherwise */
397 [[nodiscard]] static buf_block_t *btr_page_alloc_low(
398 dict_index_t *index, /*!< in: index */
399 page_no_t hint_page_no, /*!< in: hint of a good page */
400 byte file_direction, /*!< in: direction where a possible
401 page split is made */
402 ulint level, /*!< in: level where the page is placed
403 in the tree */
404 mtr_t *mtr, /*!< in/out: mini-transaction
405 for the allocation */
406 mtr_t *init_mtr) /*!< in/out: mtr or another
407 mini-transaction in which the
408 page should be initialized.
409 If init_mtr!=mtr, but the page
410 is already X-latched in mtr, do
411 not initialize the page. */
412 {
413 fseg_header_t *seg_header;
414 page_t *root;
415
416 root = btr_root_get(index, mtr);
417
418 if (level == 0) {
419 seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
420 } else {
421 seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
422 }
423
424 /* Parameter true below states that the caller has made the
425 reservation for free extents, and thus we know that a page can
426 be allocated. However, for the cases where the file size is
427 either smaller than an extent or same as the initial size,
428 the fsp_reserve_free_extents returns reserved extents as 0.
429 This can be true even if the initial allocation is about
430 to be completely filled up. The function below is a point
431 of no return and expects that enough space is already allocated.
432 However, the callers of this function rely on the success/failure
433 return by fsp_reserve_free_extents, which does not really guarantee
434 that a whole extent has been reserved.
435 The function fseg_alloc_free_page_general should attempt to ensure
436 enough space is reserved before proceeding ahead. */
437 uint64_t reserved_ext = fil_space_get_n_reserved_extents(
438 page_get_space_id(page_align(seg_header)));
439
440 return (fseg_alloc_free_page_general(seg_header, hint_page_no, file_direction,
441 reserved_ext > 0, mtr, init_mtr));
442 }
443
444 buf_block_t *btr_page_alloc_priv(
445 dict_index_t *index, page_no_t hint_page_no, byte file_direction,
446 ulint level, mtr_t *mtr,
447 mtr_t *init_mtr IF_DEBUG(, const ut::Location &loc [[maybe_unused]])) {
448 buf_block_t *new_block;
449
450 if (dict_index_is_ibuf(index)) {
451 return (btr_page_alloc_for_ibuf(index, mtr));
452 }
453
454 new_block = btr_page_alloc_low(index, hint_page_no, file_direction, level,
455 mtr, init_mtr);
456
457 if (new_block) {
458 buf_block_dbg_add_level(new_block, SYNC_TREE_NODE_NEW);
459 }
460
461 return (new_block);
462 }
463
464 /** Gets the number of pages in a B-tree.
465 @return number of pages, or ULINT_UNDEFINED if the index is unavailable */
466 ulint btr_get_size(dict_index_t *index, /*!< in: index */
467 ulint flag, /*!< in: BTR_N_LEAF_PAGES or BTR_TOTAL_SIZE */
468 mtr_t *mtr) /*!< in/out: mini-transaction where index
469 is s-latched */
470 {
471 fseg_header_t *seg_header;
472 page_t *root;
473 ulint n;
474 ulint dummy;
475
476 ut_ad(srv_read_only_mode ||
477 mtr_memo_contains(mtr, dict_index_get_lock(index), MTR_MEMO_S_LOCK) ||
478 index->table->is_intrinsic());
479 ut_ad(index->page >= FSP_FIRST_INODE_PAGE_NO);
480
481 if (index->page == FIL_NULL || dict_index_is_online_ddl(index) ||
482 !index->is_committed()) {
483 return (ULINT_UNDEFINED);
484 }
485
486 root = btr_root_get(index, mtr);
487
488 if (flag == BTR_N_LEAF_PAGES) {
489 seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
490
491 fseg_n_reserved_pages(seg_header, &n, mtr);
492
493 } else if (flag == BTR_TOTAL_SIZE) {
494 seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
495
496 n = fseg_n_reserved_pages(seg_header, &dummy, mtr);
497
498 seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
499
500 n += fseg_n_reserved_pages(seg_header, &dummy, mtr);
501 } else {
502 ut_error;
503 }
504
505 return (n);
506 }
507
508 /** Frees a page used in an ibuf tree. Puts the page to the free list of the
509 ibuf tree. */
510 static void btr_page_free_for_ibuf(
511 dict_index_t *index, /*!< in: index tree */
512 buf_block_t *block, /*!< in: block to be freed, x-latched */
513 mtr_t *mtr) /*!< in: mtr */
514 {
515 page_t *root;
516
517 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
518 root = btr_root_get(index, mtr);
519
520 flst_add_first(
521 root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST,
522 buf_block_get_frame(block) + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST_NODE,
523 mtr);
524
525 ut_d(flst_validate(root + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr));
526 }
527
528 void btr_page_free_low(dict_index_t *index, buf_block_t *block, ulint level,
529 mtr_t *mtr) {
530 fseg_header_t *seg_header;
531 page_t *root;
532
533 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
534 /* The page gets invalid for optimistic searches: increment the frame
535 modify clock */
536
537 buf_block_modify_clock_inc(block);
538
539 if (dict_index_is_ibuf(index)) {
540 btr_page_free_for_ibuf(index, block, mtr);
541
542 return;
543 }
544
545 root = btr_root_get(index, mtr);
546
547 if (level == 0 || level == ULINT_UNDEFINED) {
548 seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
549 } else {
550 seg_header = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
551 }
552
553 #ifdef UNIV_GIS_DEBUG
554 if (dict_index_is_spatial(index)) {
555 fprintf(stderr, "GIS_DIAG: Freed %ld\n", (long)block->page.id.page_no());
556 }
557 #endif
558
559 fseg_free_page(seg_header, block->page.id.space(), block->page.id.page_no(),
560 level != ULINT_UNDEFINED, mtr);
561
562 /* The page was marked free in the allocation bitmap, but it
563 should remain buffer-fixed until mtr_commit(mtr) or until it
564 is explicitly freed from the mini-transaction. */
565 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
566 /* TODO: Discard any operations on the page from the redo log
567 and remove the block from the flush list and the buffer pool.
568 This would free up buffer pool earlier and reduce writes to
569 both the tablespace and the redo log. */
570 }
571
572 /** Frees a file page used in an index tree. NOTE: cannot free field external
573 storage pages because the page must contain info on its level. */
574 void btr_page_free(dict_index_t *index, /*!< in: index tree */
575 buf_block_t *block, /*!< in: block to be freed, x-latched */
576 mtr_t *mtr) /*!< in: mtr */
577 {
578 const page_t *page = buf_block_get_frame(block);
579 ulint level = btr_page_get_level(page);
580
581 ut_ad(fil_page_index_page_check(block->frame));
582 ut_ad(level != ULINT_UNDEFINED);
583 btr_page_free_low(index, block, level, mtr);
584 }
585
586 /** Sets the child node file address in a node pointer. */
587 static inline void btr_node_ptr_set_child_page_no(
588 rec_t *rec, /*!< in: node pointer record */
589 page_zip_des_t *page_zip, /*!< in/out: compressed page whose uncompressed
590 part will be updated, or NULL */
591 const ulint *offsets, /*!< in: array returned by rec_get_offsets() */
592 page_no_t page_no, /*!< in: child node address */
593 mtr_t *mtr) /*!< in: mtr */
594 {
595 byte *field;
596 ulint len;
597
598 ut_ad(rec_offs_validate(rec, nullptr, offsets));
599 ut_ad(!page_is_leaf(page_align(rec)));
600 ut_ad(!rec_offs_comp(offsets) || rec_get_node_ptr_flag(rec));
601
602 /* The child address is in the last field */
603 field = rec_get_nth_field(nullptr, rec, offsets,
604 rec_offs_n_fields(offsets) - 1, &len);
605
606 ut_ad(len == REC_NODE_PTR_SIZE);
607
608 if (page_zip) {
609 page_zip_write_node_ptr(page_zip, rec, rec_offs_data_size(offsets), page_no,
610 mtr);
611 } else {
612 mlog_write_ulint(field, page_no, MLOG_4BYTES, mtr);
613 }
614 }
615
616 buf_block_t *btr_node_ptr_get_child(const rec_t *node_ptr, dict_index_t *index,
617 const ulint *offsets, mtr_t *mtr,
618 rw_lock_type_t type) {
619 ut_ad(rec_offs_validate(node_ptr, index, offsets));
620
621 const page_id_t page_id(page_get_space_id(page_align(node_ptr)),
622 btr_node_ptr_get_child_page_no(node_ptr, offsets));
623
624 return (btr_block_get(page_id, dict_table_page_size(index->table), type,
625 UT_LOCATION_HERE, index, mtr));
626 }
627
628 /** Returns the upper level node pointer to a page. It is assumed that mtr holds
629 an sx-latch on the tree.
630 @param[in] offsets work area for the return value
631 @param[in] heap memory heap to use
632 @param[in,out] cursor in: cursor pointing to user record, out: cursor on node
633 pointer record, its page x-latched
634 @param[in] latch_mode BTR_CONT_MODIFY_TREE or BTR_CONT_SEARCH_TREE
635 @param[in] location location where called
636 @param[in] mtr mtr
637 @return rec_get_offsets() of the node pointer record */
638 static ulint *btr_page_get_father_node_ptr_func(
639 ulint *offsets, mem_heap_t *heap, btr_cur_t *cursor, ulint latch_mode,
640 ut::Location location, mtr_t *mtr) {
641 dtuple_t *tuple;
642 rec_t *user_rec;
643 rec_t *node_ptr;
644 ulint level;
645 page_no_t page_no;
646 dict_index_t *index;
647
648 ut_ad(latch_mode == BTR_CONT_MODIFY_TREE ||
649 latch_mode == BTR_CONT_SEARCH_TREE);
650
651 page_no = btr_cur_get_block(cursor)->page.id.page_no();
652 index = cursor->index;
653 ut_ad(!dict_index_is_spatial(index));
654
655 ut_ad(srv_read_only_mode ||
656 mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
657 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
658 index->table->is_intrinsic());
659
660 ut_ad(dict_index_get_page(index) != page_no);
661
662 level = btr_page_get_level(btr_cur_get_page(cursor));
663
664 user_rec = btr_cur_get_rec(cursor);
665 ut_a(page_rec_is_user_rec(user_rec));
666
667 tuple = dict_index_build_node_ptr(index, user_rec, 0, heap, level);
668 if (index->table->is_intrinsic()) {
669 btr_cur_search_to_nth_level_with_no_latch(
670 index, level + 1, tuple, PAGE_CUR_LE, cursor, location.filename,
671 location.line, mtr);
672 } else {
673 btr_cur_search_to_nth_level(index, level + 1, tuple, PAGE_CUR_LE,
674 latch_mode, cursor, 0, location.filename,
675 location.line, mtr);
676 }
677
678 node_ptr = btr_cur_get_rec(cursor);
679 ut_ad(!page_rec_is_comp(node_ptr) ||
680 rec_get_status(node_ptr) == REC_STATUS_NODE_PTR);
681 offsets = rec_get_offsets(node_ptr, index, offsets, ULINT_UNDEFINED,
682 UT_LOCATION_HERE, &heap);
683
684 if (btr_node_ptr_get_child_page_no(node_ptr, offsets) != page_no) {
685 rec_t *print_rec;
686
687 ib::error(ER_IB_MSG_28)
688 << "Corruption of an index tree: table " << index->table->name
689 << " index " << index->name << ", father ptr page no "
690 << btr_node_ptr_get_child_page_no(node_ptr, offsets)
691 << ", child page no " << page_no;
692
693 print_rec = page_rec_get_next(page_get_infimum_rec(page_align(user_rec)));
694 offsets = rec_get_offsets(print_rec, index, offsets, ULINT_UNDEFINED,
695 UT_LOCATION_HERE, &heap);
696 page_rec_print(print_rec, offsets);
697 offsets = rec_get_offsets(node_ptr, index, offsets, ULINT_UNDEFINED,
698 UT_LOCATION_HERE, &heap);
699 page_rec_print(node_ptr, offsets);
700
701 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_29)
702 << "You should dump + drop + reimport the table to"
703 << " fix the corruption. If the crash happens at"
704 << " database startup. " << FORCE_RECOVERY_MSG
705 << " Then dump + drop + reimport.";
706 }
707
708 return (offsets);
709 }
710
711 inline ulint *btr_page_get_father_node_ptr(ulint *offsets, mem_heap_t *heap,
712 btr_cur_t *cursor,
713 ut::Location location, mtr_t *mtr) {
714 return btr_page_get_father_node_ptr_func(offsets, heap, cursor,
715 BTR_CONT_MODIFY_TREE, location, mtr);
716 }
717
718 inline ulint *btr_page_get_father_node_ptr_for_validate(ulint *offsets,
719 mem_heap_t *heap,
720 btr_cur_t *cursor,
721 ut::Location location,
722 mtr_t *mtr) {
723 return btr_page_get_father_node_ptr_func(offsets, heap, cursor,
724 BTR_CONT_SEARCH_TREE, location, mtr);
725 }
726
727 /** Returns the upper level node pointer to a page. It is assumed that mtr holds
728 an x-latch on the tree.
729 @return rec_get_offsets() of the node pointer record */
730 static ulint *btr_page_get_father_block(
731 ulint *offsets, /*!< in: work area for the return value */
732 mem_heap_t *heap, /*!< in: memory heap to use */
733 dict_index_t *index, /*!< in: b-tree index */
734 buf_block_t *block, /*!< in: child page in the index */
735 mtr_t *mtr, /*!< in: mtr */
736 btr_cur_t *cursor) /*!< out: cursor on node pointer record,
737 its page x-latched */
738 {
739 rec_t *rec =
740 page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(block)));
741 btr_cur_position(index, rec, block, cursor);
742 return btr_page_get_father_node_ptr(offsets, heap, cursor, UT_LOCATION_HERE,
743 mtr);
744 }
745
746 /** Seeks to the upper level node pointer to a page.
747 It is assumed that mtr holds an x-latch on the tree. */
748 static void btr_page_get_father(
749 dict_index_t *index, /*!< in: b-tree index */
750 buf_block_t *block, /*!< in: child page in the index */
751 mtr_t *mtr, /*!< in: mtr */
752 btr_cur_t *cursor) /*!< out: cursor on node pointer record,
753 its page x-latched */
754 {
755 mem_heap_t *heap;
756 rec_t *rec =
757 page_rec_get_next(page_get_infimum_rec(buf_block_get_frame(block)));
758 btr_cur_position(index, rec, block, cursor);
759
760 heap = mem_heap_create(100, UT_LOCATION_HERE);
761 btr_page_get_father_node_ptr(nullptr, heap, cursor, UT_LOCATION_HERE, mtr);
762 mem_heap_free(heap);
763 }
764
765 /** Free a B-tree root page. btr_free_but_not_root() must already
766 have been called.
767 In a persistent tablespace, the caller must invoke fsp_init_file_page()
768 before mtr.commit().
769 @param[in,out] block Index root page
770 @param[in,out] mtr Mini-transaction */
771 static void btr_free_root(buf_block_t *block, mtr_t *mtr) {
772 fseg_header_t *header;
773
774 ut_ad(mtr_memo_contains_flagged(mtr, block, MTR_MEMO_PAGE_X_FIX));
775
776 btr_search_drop_page_hash_index(block);
777
778 header = buf_block_get_frame(block) + PAGE_HEADER + PAGE_BTR_SEG_TOP;
779 #ifdef UNIV_BTR_DEBUG
780 ut_a(btr_root_fseg_validate(header, block->page.id.space()));
781 #endif /* UNIV_BTR_DEBUG */
782
783 while (!fseg_free_step(header, true, mtr)) {
784 /* Free the entire segment in small steps. */
785 }
786 }
787
788 /** PAGE_INDEX_ID value for freed index B-trees */
789 static const space_index_t BTR_FREED_INDEX_ID = 0;
790
791 /** Invalidate an index root page so that btr_free_root_check()
792 will not find it.
793 @param[in,out] block Index root page
794 @param[in,out] mtr Mini-transaction */
795 static void btr_free_root_invalidate(buf_block_t *block, mtr_t *mtr) {
796 ut_ad(page_is_root(block->frame));
797
798 btr_page_set_index_id(buf_block_get_frame(block),
799 buf_block_get_page_zip(block), BTR_FREED_INDEX_ID, mtr);
800 }
801
802 /** Prepare to free a B-tree.
803 @param[in] page_id Page id
804 @param[in] page_size Page size
805 @param[in] index_id PAGE_INDEX_ID contents
806 @param[in,out] mtr Mini-transaction
807 @return root block, to invoke btr_free_but_not_root() and btr_free_root()
808 @retval NULL if the page is no longer a matching B-tree page */
809 [[nodiscard]] static buf_block_t *btr_free_root_check(
810 const page_id_t &page_id, const page_size_t &page_size,
811 space_index_t index_id, mtr_t *mtr) {
812 ut_ad(!fsp_is_system_temporary(page_id.space()));
813 ut_ad(index_id != BTR_FREED_INDEX_ID);
814
815 buf_block_t *block =
816 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr);
817 buf_block_dbg_add_level(block, SYNC_TREE_NODE);
818
819 if (fil_page_index_page_check(block->frame) &&
820 index_id == btr_page_get_index_id(block->frame)) {
821 /* This should be a root page.
822 It should not be possible to reassign the same
823 index_id for some other index in the tablespace. */
824 ut_ad(page_is_root(block->frame));
825 } else {
826 block = nullptr;
827 }
828
829 return (block);
830 }
831
832 ulint btr_create(ulint type, space_id_t space, space_index_t index_id,
833 dict_index_t *index, mtr_t *mtr) {
834 page_no_t page_no;
835 buf_block_t *block;
836 buf_frame_t *frame;
837 page_t *page;
838 page_zip_des_t *page_zip;
839
840 ut_ad(index_id != BTR_FREED_INDEX_ID);
841
842 /* Create the two new segments (one, in the case of an ibuf tree) for
843 the index tree; the segment headers are put on the allocated root page
844 (for an ibuf tree, not in the root, but on a separate ibuf header
845 page) */
846
847 if (type & DICT_IBUF) {
848 /* Allocate first the ibuf header page */
849 buf_block_t *ibuf_hdr_block =
850 fseg_create(space, 0, IBUF_HEADER + IBUF_TREE_SEG_HEADER, mtr);
851
852 if (ibuf_hdr_block == nullptr) {
853 return (FIL_NULL);
854 }
855
856 buf_block_dbg_add_level(ibuf_hdr_block, SYNC_IBUF_TREE_NODE_NEW);
857
858 ut_ad(ibuf_hdr_block->page.id.page_no() == IBUF_HEADER_PAGE_NO);
859 /* Allocate then the next page to the segment: it will be the
860 tree root page */
861
862 block = fseg_alloc_free_page(buf_block_get_frame(ibuf_hdr_block) +
863 IBUF_HEADER + IBUF_TREE_SEG_HEADER,
864 IBUF_TREE_ROOT_PAGE_NO, FSP_UP, mtr);
865 ut_ad(block->page.id.page_no() == IBUF_TREE_ROOT_PAGE_NO);
866 } else {
867 block = fseg_create(space, 0, PAGE_HEADER + PAGE_BTR_SEG_TOP, mtr);
868 }
869
870 if (block == nullptr) {
871 return (FIL_NULL);
872 }
873
874 page_no = block->page.id.page_no();
875 frame = buf_block_get_frame(block);
876
877 if (type & DICT_IBUF) {
878 /* It is an insert buffer tree: initialize the free list */
879 buf_block_dbg_add_level(block, SYNC_IBUF_TREE_NODE_NEW);
880
881 ut_ad(page_no == IBUF_TREE_ROOT_PAGE_NO);
882
883 flst_init(frame + PAGE_HEADER + PAGE_BTR_IBUF_FREE_LIST, mtr);
884 } else {
885 /* It is a non-ibuf tree: create a file segment for leaf
886 pages */
887 buf_block_dbg_add_level(block, SYNC_TREE_NODE_NEW);
888
889 if (!fseg_create(space, page_no, PAGE_HEADER + PAGE_BTR_SEG_LEAF, mtr)) {
890 /* Not enough space for new segment, free root
891 segment before return. */
892 btr_free_root(block, mtr);
893 if (!index->table->is_temporary()) {
894 btr_free_root_invalidate(block, mtr);
895 }
896
897 return (FIL_NULL);
898 }
899
900 /* The fseg create acquires a second latch on the page,
901 therefore we must declare it: */
902 buf_block_dbg_add_level(block, SYNC_TREE_NODE_NEW);
903 }
904
905 uint16_t page_create_type;
906 if (dict_index_is_spatial(index)) {
907 page_create_type = FIL_PAGE_RTREE;
908 } else if (dict_index_is_sdi(index)) {
909 page_create_type = FIL_PAGE_SDI;
910 } else {
911 page_create_type = FIL_PAGE_INDEX;
912 }
913
914 /* Create a new index page on the allocated segment page */
915 page_zip = buf_block_get_page_zip(block);
916
917 if (page_zip) {
918 page = page_create_zip(block, index, 0, 0, mtr, page_create_type);
919 } else {
920 page = page_create(block, mtr, dict_table_is_comp(index->table),
921 page_create_type);
922 /* Set the level of the new index page */
923 btr_page_set_level(page, nullptr, 0, mtr);
924 }
925
926 /* Set the index id of the page */
927 btr_page_set_index_id(page, page_zip, index_id, mtr);
928
929 /* Set the next node and previous node fields */
930 btr_page_set_next(page, page_zip, FIL_NULL, mtr);
931 btr_page_set_prev(page, page_zip, FIL_NULL, mtr);
932
933 /* We reset the free bits for the page to allow creation of several
934 trees in the same mtr, otherwise the latch on a bitmap page would
935 prevent it because of the latching order.
936
937 Note: Insert Buffering is disabled for temporary tables given that
938 most temporary tables are smaller in size and short-lived. */
939 if (!(type & DICT_CLUSTERED) && !index->table->is_temporary()) {
940 ibuf_reset_free_bits(block);
941 }
942
943 /* In the following assertion we test that two records of maximum
944 allowed size fit on the root page: this fact is needed to ensure
945 correctness of split algorithms */
946
947 ut_ad(page_get_max_insert_size(page, 2) > 2 * BTR_PAGE_MAX_REC_SIZE);
948
949 buf_stat_per_index->inc(index_id_t(space, index_id));
950
951 return (page_no);
952 }
953
954 /** Free a B-tree except the root page. The root page MUST be freed after
955 this by calling btr_free_root.
956 @param[in,out] block root page
957 @param[in] log_mode mtr logging mode */
958 static void btr_free_but_not_root(buf_block_t *block, mtr_log_t log_mode) {
959 bool finished;
960 mtr_t mtr;
961
962 ut_ad(page_is_root(block->frame));
963 leaf_loop:
964 mtr_start(&mtr);
965 mtr_set_log_mode(&mtr, log_mode);
966
967 page_t *root = block->frame;
968
969 #ifdef UNIV_BTR_DEBUG
970 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF + root,
971 block->page.id.space()));
972 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP + root,
973 block->page.id.space()));
974 #endif /* UNIV_BTR_DEBUG */
975
976 /* NOTE: page hash indexes are dropped when a page is freed inside
977 fsp0fsp. */
978
979 finished = fseg_free_step(root + PAGE_HEADER + PAGE_BTR_SEG_LEAF, true, &mtr);
980 mtr_commit(&mtr);
981
982 if (!finished) {
983 goto leaf_loop;
984 }
985 top_loop:
986 mtr_start(&mtr);
987 mtr_set_log_mode(&mtr, log_mode);
988
989 root = block->frame;
990
991 #ifdef UNIV_BTR_DEBUG
992 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP + root,
993 block->page.id.space()));
994 #endif /* UNIV_BTR_DEBUG */
995
996 finished = fseg_free_step_not_header(root + PAGE_HEADER + PAGE_BTR_SEG_TOP,
997 true, &mtr);
998 mtr_commit(&mtr);
999
1000 if (!finished) {
1001 goto top_loop;
1002 }
1003 }
1004
1005 /** Free a persistent index tree if it exists.
1006 @param[in] page_id Root page id
1007 @param[in] page_size Page size
1008 @param[in] index_id PAGE_INDEX_ID contents
1009 @param[in,out] mtr Mini-transaction */
1010 void btr_free_if_exists(const page_id_t &page_id, const page_size_t &page_size,
1011 space_index_t index_id, mtr_t *mtr) {
1012 buf_block_t *root = btr_free_root_check(page_id, page_size, index_id, mtr);
1013
1014 if (root == nullptr) {
1015 return;
1016 }
1017
1018 btr_free_but_not_root(root, mtr->get_log_mode());
1019 btr_free_root(root, mtr);
1020 btr_free_root_invalidate(root, mtr);
1021 }
1022
1023 /** Free an index tree in a temporary tablespace.
1024 @param[in] page_id root page id
1025 @param[in] page_size page size */
1026 void btr_free(const page_id_t &page_id, const page_size_t &page_size) {
1027 mtr_t mtr;
1028 mtr.start();
1029 mtr.set_log_mode(MTR_LOG_NO_REDO);
1030
1031 buf_block_t *block =
1032 buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, &mtr);
1033
1034 ut_ad(page_is_root(block->frame));
1035
1036 btr_free_but_not_root(block, MTR_LOG_NO_REDO);
1037 btr_free_root(block, &mtr);
1038 mtr.commit();
1039 }
1040
1041 /** Truncate an index tree. We just free all except the root.
1042 Currently, this function is only specific for clustered indexes and the only
1043 caller is DDTableBuffer which manages a table with only a clustered index.
1044 It is up to the caller to ensure atomicity and to ensure correct recovery by
1045 calling btr_truncate_recover().
1046 @param[in] index clustered index */
1047 void btr_truncate(const dict_index_t *index) {
1048 ut_ad(index->is_clustered());
1049 ut_ad(index->next() == nullptr);
1050
1051 page_no_t root_page_no = index->page;
1052 space_id_t space_id = index->space;
1053 fil_space_t *space = fil_space_acquire_silent(space_id);
1054
1055 if (space == nullptr) {
1056 return;
1057 }
1058
1059 page_size_t page_size(space->flags);
1060 const page_id_t page_id(space_id, root_page_no);
1061 mtr_t mtr;
1062 buf_block_t *block;
1063
1064 mtr.start();
1065
1066 mtr_x_lock(&space->latch, &mtr, UT_LOCATION_HERE);
1067
1068 block = buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, &mtr);
1069
1070 page_t *page = buf_block_get_frame(block);
1071 ut_ad(page_is_root(page));
1072
1073 /* Mark that we are going to truncate the index tree
1074 We use PAGE_MAX_TRX_ID as it should be always 0 for clustered
1075 index. */
1076 mlog_write_ull(page + (PAGE_HEADER + PAGE_MAX_TRX_ID), IB_ID_MAX, &mtr);
1077
1078 mtr.commit();
1079
1080 mtr.start();
1081
1082 block = buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, &mtr);
1083
1084 /* Free all except the root, we don't want to change it. */
1085 btr_free_but_not_root(block, MTR_LOG_ALL);
1086
1087 /* Reset the mark saying that we have finished the truncate.
1088 The PAGE_MAX_TRX_ID would be reset here. */
1089 page_create(block, &mtr, dict_table_is_comp(index->table), false);
1090 ut_ad(buf_block_get_frame(block) + (PAGE_HEADER + PAGE_MAX_TRX_ID) ==
1091 nullptr);
1092
1093 mtr.commit();
1094
1095 rw_lock_x_unlock(&space->latch);
1096
1097 fil_space_release(space);
1098 }
1099
1100 /** Recovery function for btr_truncate. We will check if there is a
1101 crash during btr_truncate, if so, do recover it, if not, do nothing.
1102 @param[in] index clustered index */
1103 void btr_truncate_recover(const dict_index_t *index) {
1104 ut_ad(index->is_clustered());
1105 ut_ad(index->next() == nullptr);
1106
1107 page_no_t root_page_no = index->page;
1108 space_id_t space_id = index->space;
1109 fil_space_t *space = fil_space_acquire_silent(space_id);
1110
1111 if (space == nullptr) {
1112 return;
1113 }
1114
1115 page_size_t page_size(space->flags);
1116 const page_id_t page_id(space_id, root_page_no);
1117 mtr_t mtr;
1118 buf_block_t *block;
1119
1120 mtr.start();
1121
1122 block = buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, &mtr);
1123
1124 page_t *page = buf_block_get_frame(block);
1125
1126 trx_id_t trx_id = page_get_max_trx_id(page);
1127 ut_ad(trx_id == 0 || trx_id == IB_ID_MAX);
1128
1129 mtr.commit();
1130
1131 fil_space_release(space);
1132
1133 if (trx_id == IB_ID_MAX) {
1134 /* -1 means there is a half-done btr_truncate,
1135 we can simple redo it again. */
1136 btr_truncate(index);
1137 }
1138 }
1139 #endif /* !UNIV_HOTBACKUP */
1140
1141 bool btr_page_reorganize_low(bool recovery, ulint z_level, page_cur_t *cursor,
1142 dict_index_t *index, mtr_t *mtr) {
1143 buf_block_t *block = page_cur_get_block(cursor);
1144 #ifndef UNIV_HOTBACKUP
1145 buf_pool_t *buf_pool = buf_pool_from_bpage(&block->page);
1146 #endif /* !UNIV_HOTBACKUP */
1147 page_t *page = buf_block_get_frame(block);
1148 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
1149 buf_block_t *temp_block;
1150 page_t *temp_page;
1151 ulint data_size1;
1152 ulint data_size2;
1153 ulint max_ins_size1;
1154 ulint max_ins_size2;
1155 bool success = false;
1156 ulint pos;
1157 bool log_compressed;
1158
1159 #ifndef UNIV_HOTBACKUP
1160 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
1161 #endif /* !UNIV_HOTBACKUP */
1162 btr_assert_not_corrupted(block, index);
1163 #ifdef UNIV_ZIP_DEBUG
1164 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
1165 #endif /* UNIV_ZIP_DEBUG */
1166 data_size1 = page_get_data_size(page);
1167 max_ins_size1 = page_get_max_insert_size_after_reorganize(page, 1);
1168
1169 /* Turn logging off */
1170 mtr_log_t log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
1171
1172 #ifndef UNIV_HOTBACKUP
1173 temp_block = buf_block_alloc(buf_pool);
1174 #else /* !UNIV_HOTBACKUP */
1175 temp_block = back_block2;
1176 #endif /* !UNIV_HOTBACKUP */
1177 temp_page = temp_block->frame;
1178
1179 #ifndef UNIV_HOTBACKUP
1180 MONITOR_INC(MONITOR_INDEX_REORG_ATTEMPTS);
1181 #endif /* !UNIV_HOTBACKUP */
1182
1183 /* Copy the old page to temporary space */
1184 buf_frame_copy(temp_page, page);
1185
1186 #ifndef UNIV_HOTBACKUP
1187 if (!recovery) {
1188 btr_search_drop_page_hash_index(block);
1189 }
1190 #endif /* !UNIV_HOTBACKUP */
1191
1192 /* Save the cursor position. */
1193 pos = page_rec_get_n_recs_before(page_cur_get_rec(cursor));
1194
1195 /* Recreate the page: note that global data on page (possible
1196 segment headers, next page-field, etc.) is preserved intact */
1197
1198 page_create(block, mtr, dict_table_is_comp(index->table),
1199 fil_page_get_type(page));
1200
1201 /* Copy the records from the temporary space to the recreated page;
1202 do not copy the lock bits yet */
1203
1204 page_copy_rec_list_end_no_locks(block, temp_block,
1205 page_get_infimum_rec(temp_page), index, mtr);
1206
1207 /* Multiple transactions cannot simultaneously operate on the
1208 same temp-table in parallel.
1209 max_trx_id is ignored for temp tables because it not required
1210 for MVCC. */
1211 if (dict_index_is_sec_or_ibuf(index) && page_is_leaf(page) &&
1212 !index->table->is_temporary()) {
1213 /* Copy max trx id to recreated page */
1214 trx_id_t max_trx_id = page_get_max_trx_id(temp_page);
1215 page_set_max_trx_id(block, nullptr, max_trx_id, mtr);
1216 /* In crash recovery, dict_index_is_sec_or_ibuf() always
1217 holds, even for clustered indexes. max_trx_id is
1218 unused in clustered index pages. */
1219 ut_ad(max_trx_id != 0 || recovery);
1220 }
1221
1222 /* If innodb_log_compressed_pages is ON, page reorganize should log the
1223 compressed page image.*/
1224 log_compressed = page_zip && page_zip_log_pages;
1225
1226 if (log_compressed) {
1227 mtr_set_log_mode(mtr, log_mode);
1228 }
1229
1230 if (page_zip && !page_zip_compress(page_zip, page, index, z_level, mtr)) {
1231 /* Restore the old page and exit. */
1232 #if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
1233 /* Check that the bytes that we skip are identical. */
1234 ut_a(!memcmp(page, temp_page, PAGE_HEADER));
1235 ut_a(!memcmp(PAGE_HEADER + PAGE_N_RECS + page,
1236 PAGE_HEADER + PAGE_N_RECS + temp_page,
1237 PAGE_DATA - (PAGE_HEADER + PAGE_N_RECS)));
1238 ut_a(!memcmp(UNIV_PAGE_SIZE - FIL_PAGE_DATA_END + page,
1239 UNIV_PAGE_SIZE - FIL_PAGE_DATA_END + temp_page,
1240 FIL_PAGE_DATA_END));
1241 #endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
1242
1243 memcpy(PAGE_HEADER + page, PAGE_HEADER + temp_page,
1244 PAGE_N_RECS - PAGE_N_DIR_SLOTS);
1245 memcpy(PAGE_DATA + page, PAGE_DATA + temp_page,
1246 UNIV_PAGE_SIZE - PAGE_DATA - FIL_PAGE_DATA_END);
1247
1248 #if defined UNIV_DEBUG || defined UNIV_ZIP_DEBUG
1249 ut_a(!memcmp(page, temp_page, UNIV_PAGE_SIZE));
1250 #endif /* UNIV_DEBUG || UNIV_ZIP_DEBUG */
1251
1252 goto func_exit;
1253 }
1254
1255 #ifndef UNIV_HOTBACKUP
1256 /* No locks are acquried for intrinsic tables. */
1257 if (!recovery && !dict_table_is_locking_disabled(index->table)) {
1258 /* Update the record lock bitmaps */
1259 lock_move_reorganize_page(block, temp_block);
1260 }
1261 #endif /* !UNIV_HOTBACKUP */
1262
1263 data_size2 = page_get_data_size(page);
1264 max_ins_size2 = page_get_max_insert_size_after_reorganize(page, 1);
1265
1266 if (data_size1 != data_size2 || max_ins_size1 != max_ins_size2) {
1267 ib::error(ER_IB_MSG_30)
1268 << "Page old data size " << data_size1 << " new data size "
1269 << data_size2 << ", page old max ins size " << max_ins_size1
1270 << " new max ins size " << max_ins_size2;
1271
1272 ib::error(ER_IB_MSG_31) << BUG_REPORT_MSG;
1273 ut_d(ut_error);
1274 } else {
1275 success = true;
1276 }
1277
1278 /* Restore the cursor position. */
1279 if (pos > 0) {
1280 cursor->rec = page_rec_get_nth(page, pos);
1281 } else {
1282 ut_ad(cursor->rec == page_get_infimum_rec(page));
1283 }
1284
1285 func_exit:
1286 #ifdef UNIV_ZIP_DEBUG
1287 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
1288 #endif /* UNIV_ZIP_DEBUG */
1289 #ifndef UNIV_HOTBACKUP
1290 buf_block_free(temp_block);
1291 #endif /* !UNIV_HOTBACKUP */
1292
1293 /* Restore logging mode */
1294 mtr_set_log_mode(mtr, log_mode);
1295
1296 #ifndef UNIV_HOTBACKUP
1297 if (success) {
1298 mlog_id_t type;
1299 byte *log_ptr = nullptr;
1300
1301 /* Write the log record */
1302 if (page_zip) {
1303 ut_ad(page_is_comp(page));
1304 type = MLOG_ZIP_PAGE_REORGANIZE;
1305 } else {
1306 type = MLOG_PAGE_REORGANIZE;
1307 }
1308
1309 bool opened = false;
1310
1311 if (!log_compressed) {
1312 opened = mlog_open_and_write_index(mtr, page, index, type,
1313 page_zip ? 1 : 0, log_ptr);
1314 }
1315
1316 /* For compressed pages write the compression level. */
1317 if (opened && page_zip) {
1318 mach_write_to_1(log_ptr, z_level);
1319 mlog_close(mtr, log_ptr + 1);
1320 }
1321
1322 MONITOR_INC(MONITOR_INDEX_REORG_SUCCESSFUL);
1323 }
1324 #endif /* !UNIV_HOTBACKUP */
1325
1326 return (success);
1327 }
1328
1329 /** Reorganizes an index page.
1330
1331 IMPORTANT: On success, the caller will have to update IBUF_BITMAP_FREE
1332 if this is a compressed leaf page in a secondary index. This has to
1333 be done either within the same mini-transaction, or by invoking
1334 ibuf_reset_free_bits() before mtr_commit(). On uncompressed pages,
1335 IBUF_BITMAP_FREE is unaffected by reorganization.
1336
1337 @retval true if the operation was successful
1338 @retval false if it is a compressed page, and recompression failed */
1339 static bool btr_page_reorganize_block(
1340 bool recovery, /*!< in: true if called in recovery:
1341 locks should not be updated, i.e.,
1342 there cannot exist locks on the
1343 page, and a hash index should not be
1344 dropped: it cannot exist */
1345 ulint z_level, /*!< in: compression level to be used
1346 if dealing with compressed page */
1347 buf_block_t *block, /*!< in/out: B-tree page */
1348 dict_index_t *index, /*!< in: the index tree of the page */
1349 mtr_t *mtr) /*!< in/out: mini-transaction */
1350 {
1351 page_cur_t cur;
1352 page_cur_set_before_first(block, &cur);
1353
1354 return (btr_page_reorganize_low(recovery, z_level, &cur, index, mtr));
1355 }
1356
1357 /** Reorganizes an index page.
1358
1359 IMPORTANT: On success, the caller will have to update IBUF_BITMAP_FREE
1360 if this is a compressed leaf page in a secondary index. This has to
1361 be done either within the same mini-transaction, or by invoking
1362 ibuf_reset_free_bits() before mtr_commit(). On uncompressed pages,
1363 IBUF_BITMAP_FREE is unaffected by reorganization.
1364
1365 @param[in,out] cursor Page cursor
1366 @param[in] index The index tree of the page
1367 @param[in,out] mtr Mini-transaction
1368 @retval true if the operation was successful
1369 @retval false if it is a compressed page, and recompression failed */
1370 bool btr_page_reorganize(page_cur_t *cursor, dict_index_t *index, mtr_t *mtr) {
1371 return (btr_page_reorganize_low(false, page_zip_level, cursor, index, mtr));
1372 }
1373
1374 /** Parses a redo log record of reorganizing a page.
1375 @return end of log record or NULL */
1376 byte *btr_parse_page_reorganize(
1377 byte *ptr, /*!< in: buffer */
1378 byte *end_ptr, /*!< in: buffer end */
1379 dict_index_t *index, /*!< in: record descriptor */
1380 bool compressed, /*!< in: true if compressed page */
1381 buf_block_t *block, /*!< in: page to be reorganized, or NULL */
1382 mtr_t *mtr) /*!< in: mtr or NULL */
1383 {
1384 ulint level;
1385
1386 ut_ad(index != nullptr);
1387
1388 /* If dealing with a compressed page the record has the
1389 compression level used during original compression written in
1390 one byte. Otherwise record is empty. */
1391 if (compressed) {
1392 if (ptr == end_ptr) {
1393 return (nullptr);
1394 }
1395
1396 level = mach_read_from_1(ptr);
1397
1398 ut_a(level <= 9);
1399 ++ptr;
1400 } else {
1401 level = page_zip_level;
1402 }
1403
1404 if (block != nullptr) {
1405 btr_page_reorganize_block(true, level, block, index, mtr);
1406 }
1407
1408 return (ptr);
1409 }
1410
1411 #ifndef UNIV_HOTBACKUP
1412 /** Empties an index page. @see btr_page_create(). */
1413 static void btr_page_empty(
1414 buf_block_t *block, /*!< in: page to be emptied */
1415 page_zip_des_t *page_zip, /*!< out: compressed page, or NULL */
1416 dict_index_t *index, /*!< in: index of the page */
1417 ulint level, /*!< in: the B-tree level of the page */
1418 mtr_t *mtr) /*!< in: mtr */
1419 {
1420 page_t *page = buf_block_get_frame(block);
1421
1422 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
1423 ut_ad(page_zip == buf_block_get_page_zip(block));
1424 #ifdef UNIV_ZIP_DEBUG
1425 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
1426 #endif /* UNIV_ZIP_DEBUG */
1427
1428 btr_search_drop_page_hash_index(block);
1429
1430 uint16_t page_type;
1431 if (dict_index_is_spatial(index)) {
1432 page_type = FIL_PAGE_RTREE;
1433 } else if (dict_index_is_sdi(index)) {
1434 page_type = FIL_PAGE_SDI;
1435 } else {
1436 page_type = FIL_PAGE_INDEX;
1437 }
1438
1439 /* Recreate the page: note that global data on page (possible
1440 segment headers, next page-field, etc.) is preserved intact */
1441
1442 if (page_zip) {
1443 page_create_zip(block, index, level, 0, mtr, page_type);
1444 } else {
1445 page_create(block, mtr, dict_table_is_comp(index->table), page_type);
1446 btr_page_set_level(page, nullptr, level, mtr);
1447 }
1448 }
1449
1450 /** Makes tree one level higher by splitting the root, and inserts
1451 the tuple. It is assumed that mtr contains an x-latch on the tree.
1452 NOTE that the operation of this function must always succeed,
1453 we cannot reverse it: therefore enough free disk space must be
1454 guaranteed to be available before this function is called.
1455 @return inserted record */
1456 rec_t *btr_root_raise_and_insert(
1457 uint32_t flags, /*!< in: undo logging and locking flags */
1458 btr_cur_t *cursor, /*!< in: cursor at which to insert: must be
1459 on the root page; when the function returns,
1460 the cursor is positioned on the predecessor
1461 of the inserted record */
1462 ulint **offsets, /*!< out: offsets on inserted record */
1463 mem_heap_t **heap, /*!< in/out: pointer to memory heap, or NULL */
1464 const dtuple_t *tuple, /*!< in: tuple to insert */
1465 mtr_t *mtr) /*!< in: mtr */
1466 {
1467 dict_index_t *index;
1468 page_t *root;
1469 page_t *new_page;
1470 page_no_t new_page_no;
1471 rec_t *rec;
1472 dtuple_t *node_ptr;
1473 ulint level;
1474 rec_t *node_ptr_rec;
1475 page_cur_t *page_cursor;
1476 page_zip_des_t *root_page_zip;
1477 page_zip_des_t *new_page_zip;
1478 buf_block_t *root_block;
1479 buf_block_t *new_block;
1480
1481 root = btr_cur_get_page(cursor);
1482 root_block = btr_cur_get_block(cursor);
1483 root_page_zip = buf_block_get_page_zip(root_block);
1484 ut_ad(!page_is_empty(root));
1485 index = cursor->index;
1486 #ifdef UNIV_ZIP_DEBUG
1487 ut_a(!root_page_zip || page_zip_validate(root_page_zip, root, index));
1488 #endif /* UNIV_ZIP_DEBUG */
1489 #ifdef UNIV_BTR_DEBUG
1490 if (!dict_index_is_ibuf(index)) {
1491 space_id_t space_id = dict_index_get_space(index);
1492
1493 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF + root,
1494 space_id));
1495 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP + root,
1496 space_id));
1497 }
1498
1499 ut_a(dict_index_get_page(index) == page_get_page_no(root));
1500 #endif /* UNIV_BTR_DEBUG */
1501 ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
1502 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
1503 index->table->is_intrinsic());
1504 ut_ad(mtr_is_block_fix(mtr, root_block, MTR_MEMO_PAGE_X_FIX, index->table));
1505
1506 /* Allocate a new page to the tree. Root splitting is done by first
1507 moving the root records to the new page, emptying the root, putting
1508 a node pointer to the new page, and then splitting the new page. */
1509
1510 level = btr_page_get_level(root);
1511
1512 new_block = btr_page_alloc(index, 0, FSP_NO_DIR, level, mtr, mtr);
1513
1514 /* New page could not be allocated */
1515 if (!new_block) {
1516 return nullptr;
1517 }
1518
1519 new_page = buf_block_get_frame(new_block);
1520 new_page_zip = buf_block_get_page_zip(new_block);
1521 ut_a(!new_page_zip == !root_page_zip);
1522 ut_a(!new_page_zip ||
1523 page_zip_get_size(new_page_zip) == page_zip_get_size(root_page_zip));
1524
1525 btr_page_create(new_block, new_page_zip, index, level, mtr);
1526
1527 /* Set the next node and previous node fields of new page */
1528 btr_page_set_next(new_page, new_page_zip, FIL_NULL, mtr);
1529 btr_page_set_prev(new_page, new_page_zip, FIL_NULL, mtr);
1530
1531 /* Copy the records from root to the new page one by one. */
1532
1533 if (false
1534 #ifdef UNIV_ZIP_COPY
1535 || new_page_zip
1536 #endif /* UNIV_ZIP_COPY */
1537 || !page_copy_rec_list_end(new_block, root_block,
1538 page_get_infimum_rec(root), index, mtr)) {
1539 ut_a(new_page_zip);
1540
1541 /* Copy the page byte for byte. */
1542 page_zip_copy_recs(new_page_zip, new_page, root_page_zip, root, index, mtr);
1543
1544 /* Update the lock table and possible hash index. */
1545
1546 if (!dict_table_is_locking_disabled(index->table)) {
1547 lock_move_rec_list_end(new_block, root_block, page_get_infimum_rec(root));
1548 }
1549
1550 /* Move any existing predicate locks */
1551 if (dict_index_is_spatial(index)) {
1552 lock_prdt_rec_move(new_block, root_block);
1553 }
1554
1555 btr_search_update_hash_on_move(new_block, root_block, index);
1556 }
1557
1558 /* If this is a pessimistic insert which is actually done to
1559 perform a pessimistic update then we have stored the lock
1560 information of the record to be inserted on the infimum of the
1561 root page: we cannot discard the lock structs on the root page */
1562
1563 if (!dict_table_is_locking_disabled(index->table)) {
1564 lock_update_root_raise(new_block, root_block);
1565 }
1566
1567 /* Create a memory heap where the node pointer is stored */
1568 if (!*heap) {
1569 *heap = mem_heap_create(100, UT_LOCATION_HERE);
1570 }
1571
1572 rec = page_rec_get_next(page_get_infimum_rec(new_page));
1573 new_page_no = new_block->page.id.page_no();
1574
1575 /* Build the node pointer (= node key and page address) for the
1576 child */
1577 if (dict_index_is_spatial(index)) {
1578 rtr_mbr_t new_mbr;
1579
1580 rtr_page_cal_mbr(index, new_block, &new_mbr, *heap);
1581 node_ptr =
1582 rtr_index_build_node_ptr(index, &new_mbr, rec, new_page_no, *heap);
1583 } else {
1584 node_ptr = dict_index_build_node_ptr(index, rec, new_page_no, *heap, level);
1585 }
1586 /* The node pointer must be marked as the predefined minimum record,
1587 as there is no lower alphabetical limit to records in the leftmost
1588 node of a level: */
1589 dtuple_set_info_bits(node_ptr,
1590 dtuple_get_info_bits(node_ptr) | REC_INFO_MIN_REC_FLAG);
1591
1592 /* Rebuild the root page to get free space */
1593 btr_page_empty(root_block, root_page_zip, index, level + 1, mtr);
1594
1595 /* Set the next node and previous node fields, although
1596 they should already have been set. The previous node field
1597 must be FIL_NULL if root_page_zip != NULL, because the
1598 REC_INFO_MIN_REC_FLAG (of the first user record) will be
1599 set if and only if btr_page_get_prev() == FIL_NULL. */
1600 btr_page_set_next(root, root_page_zip, FIL_NULL, mtr);
1601 btr_page_set_prev(root, root_page_zip, FIL_NULL, mtr);
1602
1603 page_cursor = btr_cur_get_page_cur(cursor);
1604
1605 /* Insert node pointer to the root */
1606
1607 page_cur_set_before_first(root_block, page_cursor);
1608
1609 node_ptr_rec =
1610 page_cur_tuple_insert(page_cursor, node_ptr, index, offsets, heap, mtr);
1611
1612 /* The root page should only contain the node pointer
1613 to new_page at this point. Thus, the data should fit. */
1614 ut_a(node_ptr_rec);
1615
1616 /* We play safe and reset the free bits for the new page */
1617
1618 if (!index->is_clustered() && !index->table->is_temporary()) {
1619 ibuf_reset_free_bits(new_block);
1620 }
1621
1622 /* Reposition the cursor to the child node */
1623 page_cur_search(new_block, index, tuple, page_cursor);
1624
1625 /* Split the child and insert tuple */
1626 if (dict_index_is_spatial(index)) {
1627 /* Split rtree page and insert tuple */
1628 return (
1629 rtr_page_split_and_insert(flags, cursor, offsets, heap, tuple, mtr));
1630 } else {
1631 return (
1632 btr_page_split_and_insert(flags, cursor, offsets, heap, tuple, mtr));
1633 }
1634 }
1635
1636 /** Decides if the page should be split at the convergence point of inserts
1637 converging to the left.
1638 @return true if split recommended */
1639 bool btr_page_get_split_rec_to_left(
1640 btr_cur_t *cursor, /*!< in: cursor at which to insert */
1641 rec_t **split_rec) /*!< out: if split recommended,
1642 the first record on upper half page,
1643 or NULL if tuple to be inserted should
1644 be first */
1645 {
1646 page_t *page;
1647 rec_t *insert_point;
1648 rec_t *infimum;
1649
1650 page = btr_cur_get_page(cursor);
1651 insert_point = btr_cur_get_rec(cursor);
1652
1653 if (page_header_get_ptr(page, PAGE_LAST_INSERT) ==
1654 page_rec_get_next(insert_point)) {
1655 infimum = page_get_infimum_rec(page);
1656
1657 /* If the convergence is in the middle of a page, include also
1658 the record immediately before the new insert to the upper
1659 page. Otherwise, we could repeatedly move from page to page
1660 lots of records smaller than the convergence point. */
1661
1662 if (infimum != insert_point && page_rec_get_next(infimum) != insert_point) {
1663 *split_rec = insert_point;
1664 } else {
1665 *split_rec = page_rec_get_next(insert_point);
1666 }
1667
1668 return true;
1669 }
1670
1671 return false;
1672 }
1673
1674 /** Decides if the page should be split at the convergence point of inserts
1675 converging to the right.
1676 @return true if split recommended */
1677 bool btr_page_get_split_rec_to_right(
1678 btr_cur_t *cursor, /*!< in: cursor at which to insert */
1679 rec_t **split_rec) /*!< out: if split recommended,
1680 the first record on upper half page,
1681 or NULL if tuple to be inserted should
1682 be first */
1683 {
1684 page_t *page;
1685 rec_t *insert_point;
1686
1687 page = btr_cur_get_page(cursor);
1688 insert_point = btr_cur_get_rec(cursor);
1689
1690 /* We use eager heuristics: if the new insert would be right after
1691 the previous insert on the same page, we assume that there is a
1692 pattern of sequential inserts here. */
1693
1694 if (page_header_get_ptr(page, PAGE_LAST_INSERT) == insert_point) {
1695 rec_t *next_rec;
1696
1697 next_rec = page_rec_get_next(insert_point);
1698
1699 if (page_rec_is_supremum(next_rec)) {
1700 split_at_new:
1701 /* Split at the new record to insert */
1702 *split_rec = nullptr;
1703 } else {
1704 rec_t *next_next_rec = page_rec_get_next(next_rec);
1705 if (page_rec_is_supremum(next_next_rec)) {
1706 goto split_at_new;
1707 }
1708
1709 /* If there are >= 2 user records up from the insert
1710 point, split all but 1 off. We want to keep one because
1711 then sequential inserts can use the adaptive hash
1712 index, as they can do the necessary checks of the right
1713 search position just by looking at the records on this
1714 page. */
1715
1716 *split_rec = next_next_rec;
1717 }
1718
1719 return true;
1720 }
1721
1722 return false;
1723 }
1724
1725 /** Calculates a split record such that the tuple will certainly fit on
1726 its half-page when the split is performed. We assume in this function
1727 only that the cursor page has at least one user record.
1728 @return split record, or NULL if tuple will be the first record on
1729 the lower or upper half-page (determined by btr_page_tuple_smaller()) */
1730 static rec_t *btr_page_get_split_rec(
1731 btr_cur_t *cursor, /*!< in: cursor at which insert should be made */
1732 const dtuple_t *tuple) /*!< in: tuple to insert */
1733 {
1734 page_t *page;
1735 page_zip_des_t *page_zip;
1736 ulint insert_size;
1737 ulint free_space;
1738 ulint total_data;
1739 ulint total_n_recs;
1740 ulint total_space;
1741 ulint incl_data;
1742 rec_t *ins_rec;
1743 rec_t *rec;
1744 rec_t *next_rec;
1745 ulint n;
1746 mem_heap_t *heap;
1747 ulint *offsets;
1748
1749 page = btr_cur_get_page(cursor);
1750
1751 insert_size = rec_get_converted_size(cursor->index, tuple);
1752 free_space = page_get_free_space_of_empty(page_is_comp(page));
1753
1754 page_zip = btr_cur_get_page_zip(cursor);
1755 if (page_zip) {
1756 /* Estimate the free space of an empty compressed page. */
1757 ulint free_space_zip = page_zip_empty_size(cursor->index->n_fields,
1758 page_zip_get_size(page_zip));
1759
1760 if (free_space > (ulint)free_space_zip) {
1761 free_space = (ulint)free_space_zip;
1762 }
1763 }
1764
1765 /* free_space is now the free space of a created new page */
1766
1767 total_data = page_get_data_size(page) + insert_size;
1768 total_n_recs = page_get_n_recs(page) + 1;
1769 ut_ad(total_n_recs >= 2);
1770 total_space = total_data + page_dir_calc_reserved_space(total_n_recs);
1771
1772 n = 0;
1773 incl_data = 0;
1774 ins_rec = btr_cur_get_rec(cursor);
1775 rec = page_get_infimum_rec(page);
1776
1777 heap = nullptr;
1778 offsets = nullptr;
1779
1780 /* We start to include records to the left half, and when the
1781 space reserved by them exceeds half of total_space, then if
1782 the included records fit on the left page, they will be put there
1783 if something was left over also for the right page,
1784 otherwise the last included record will be the first on the right
1785 half page */
1786
1787 do {
1788 /* Decide the next record to include */
1789 if (rec == ins_rec) {
1790 rec = nullptr; /* NULL denotes that tuple is
1791 now included */
1792 } else if (rec == nullptr) {
1793 rec = page_rec_get_next(ins_rec);
1794 } else {
1795 rec = page_rec_get_next(rec);
1796 }
1797
1798 if (rec == nullptr) {
1799 /* Include tuple */
1800 incl_data += insert_size;
1801 } else {
1802 offsets = rec_get_offsets(rec, cursor->index, offsets, ULINT_UNDEFINED,
1803 UT_LOCATION_HERE, &heap);
1804 incl_data += rec_offs_size(offsets);
1805 }
1806
1807 n++;
1808 } while (incl_data + page_dir_calc_reserved_space(n) < total_space / 2);
1809
1810 if (incl_data + page_dir_calc_reserved_space(n) <= free_space) {
1811 /* The next record will be the first on
1812 the right half page if it is not the
1813 supremum record of page */
1814
1815 if (rec == ins_rec) {
1816 rec = nullptr;
1817
1818 goto func_exit;
1819 } else if (rec == nullptr) {
1820 next_rec = page_rec_get_next(ins_rec);
1821 } else {
1822 next_rec = page_rec_get_next(rec);
1823 }
1824 ut_ad(next_rec);
1825 if (!page_rec_is_supremum(next_rec)) {
1826 rec = next_rec;
1827 }
1828 }
1829
1830 func_exit:
1831 if (heap) {
1832 mem_heap_free(heap);
1833 }
1834 return (rec);
1835 }
1836
1837 /** Returns true if the insert fits on the appropriate half-page with the
1838 chosen split_rec.
1839 @return true if fits */
1840 [[nodiscard]] static bool btr_page_insert_fits(
1841 btr_cur_t *cursor, /*!< in: cursor at which insert
1842 should be made */
1843 const rec_t *split_rec, /*!< in: suggestion for first record
1844 on upper half-page, or NULL if
1845 tuple to be inserted should be first */
1846 ulint **offsets, /*!< in: rec_get_offsets(
1847 split_rec, cursor->index); out: garbage */
1848 const dtuple_t *tuple, /*!< in: tuple to insert */
1849 mem_heap_t **heap) /*!< in: temporary memory heap */
1850 {
1851 page_t *page;
1852 ulint insert_size;
1853 ulint free_space;
1854 ulint total_data;
1855 ulint total_n_recs;
1856 const rec_t *rec;
1857 const rec_t *end_rec;
1858
1859 page = btr_cur_get_page(cursor);
1860
1861 ut_ad(!split_rec || !page_is_comp(page) == !rec_offs_comp(*offsets));
1862 ut_ad(!split_rec || rec_offs_validate(split_rec, cursor->index, *offsets));
1863
1864 insert_size = rec_get_converted_size(cursor->index, tuple);
1865 free_space = page_get_free_space_of_empty(page_is_comp(page));
1866
1867 /* free_space is now the free space of a created new page */
1868
1869 total_data = page_get_data_size(page) + insert_size;
1870 total_n_recs = page_get_n_recs(page) + 1;
1871
1872 /* We determine which records (from rec to end_rec, not including
1873 end_rec) will end up on the other half page from tuple when it is
1874 inserted. */
1875
1876 if (split_rec == nullptr) {
1877 rec = page_rec_get_next(page_get_infimum_rec(page));
1878 end_rec = page_rec_get_next(btr_cur_get_rec(cursor));
1879
1880 } else if (cmp_dtuple_rec(tuple, split_rec, cursor->index, *offsets) >= 0) {
1881 rec = page_rec_get_next(page_get_infimum_rec(page));
1882 end_rec = split_rec;
1883 } else {
1884 rec = split_rec;
1885 end_rec = page_get_supremum_rec(page);
1886 }
1887
1888 if (total_data + page_dir_calc_reserved_space(total_n_recs) <= free_space) {
1889 /* Ok, there will be enough available space on the
1890 half page where the tuple is inserted */
1891
1892 return (true);
1893 }
1894
1895 while (rec != end_rec) {
1896 /* In this loop we calculate the amount of reserved
1897 space after rec is removed from page. */
1898
1899 *offsets = rec_get_offsets(rec, cursor->index, *offsets, ULINT_UNDEFINED,
1900 UT_LOCATION_HERE, heap);
1901
1902 total_data -= rec_offs_size(*offsets);
1903 total_n_recs--;
1904
1905 if (total_data + page_dir_calc_reserved_space(total_n_recs) <= free_space) {
1906 /* Ok, there will be enough available space on the
1907 half page where the tuple is inserted */
1908
1909 return (true);
1910 }
1911
1912 rec = page_rec_get_next_const(rec);
1913 }
1914
1915 return (false);
1916 }
1917
1918 /** Inserts a data tuple to a tree on a non-leaf level. It is assumed
1919 that mtr holds an x-latch on the tree.
1920 @param[in] flags undo logging and locking flags
1921 @param[in] index index
1922 @param[in] level level, must be > 0
1923 @param[in] tuple the record to be inserted
1924 @param[in] location location where called
1925 @param[in] mtr mtr */
1926 void btr_insert_on_non_leaf_level(uint32_t flags, dict_index_t *index,
1927 ulint level, dtuple_t *tuple,
1928 ut::Location location, mtr_t *mtr) {
1929 big_rec_t *dummy_big_rec;
1930 btr_cur_t cursor;
1931 dberr_t err;
1932 rec_t *rec;
1933 mem_heap_t *heap = nullptr;
1934 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1935 ulint *offsets = offsets_;
1936 rec_offs_init(offsets_);
1937 rtr_info_t rtr_info;
1938
1939 ut_ad(level > 0);
1940
1941 if (!dict_index_is_spatial(index)) {
1942 if (index->table->is_intrinsic()) {
1943 btr_cur_search_to_nth_level_with_no_latch(
1944 index, level, tuple, PAGE_CUR_LE, &cursor, __FILE__, __LINE__, mtr);
1945 } else {
1946 btr_cur_search_to_nth_level(index, level, tuple, PAGE_CUR_LE,
1947 BTR_CONT_MODIFY_TREE, &cursor, 0,
1948 location.filename, location.line, mtr);
1949 }
1950 } else {
1951 /* For spatial index, initialize structures to track
1952 its parents etc. */
1953 rtr_init_rtr_info(&rtr_info, false, &cursor, index, false);
1954
1955 rtr_info_update_btr(&cursor, &rtr_info);
1956
1957 btr_cur_search_to_nth_level(index, level, tuple, PAGE_CUR_RTREE_INSERT,
1958 BTR_CONT_MODIFY_TREE, &cursor, 0,
1959 location.filename, location.line, mtr);
1960 }
1961
1962 ut_ad(cursor.flag == BTR_CUR_BINARY);
1963
1964 err = btr_cur_optimistic_insert(
1965 flags | BTR_NO_LOCKING_FLAG | BTR_KEEP_SYS_FLAG | BTR_NO_UNDO_LOG_FLAG,
1966 &cursor, &offsets, &heap, tuple, &rec, &dummy_big_rec, nullptr, mtr);
1967
1968 if (err == DB_FAIL) {
1969 err = btr_cur_pessimistic_insert(
1970 flags | BTR_NO_LOCKING_FLAG | BTR_KEEP_SYS_FLAG | BTR_NO_UNDO_LOG_FLAG,
1971 &cursor, &offsets, &heap, tuple, &rec, &dummy_big_rec, nullptr, mtr);
1972 ut_a(err == DB_SUCCESS);
1973 }
1974
1975 if (heap != nullptr) {
1976 mem_heap_free(heap);
1977 }
1978
1979 if (dict_index_is_spatial(index)) {
1980 ut_ad(cursor.rtr_info);
1981
1982 rtr_clean_rtr_info(&rtr_info, true);
1983 }
1984 }
1985
1986 /** Attaches the halves of an index page on the appropriate level in an
1987 index tree. */
1988 static void btr_attach_half_pages(
1989 uint32_t flags, /*!< in: undo logging and
1990 locking flags */
1991 dict_index_t *index, /*!< in: the index tree */
1992 buf_block_t *block, /*!< in/out: page to be split */
1993 const rec_t *split_rec, /*!< in: first record on upper
1994 half page */
1995 buf_block_t *new_block, /*!< in/out: the new half page */
1996 ulint direction, /*!< in: FSP_UP or FSP_DOWN */
1997 mtr_t *mtr) /*!< in: mtr */
1998 {
1999 page_no_t prev_page_no;
2000 page_no_t next_page_no;
2001 ulint level;
2002 page_t *page = buf_block_get_frame(block);
2003 page_t *lower_page;
2004 page_t *upper_page;
2005 page_no_t lower_page_no;
2006 page_no_t upper_page_no;
2007 page_zip_des_t *lower_page_zip;
2008 page_zip_des_t *upper_page_zip;
2009 dtuple_t *node_ptr_upper;
2010 mem_heap_t *heap;
2011 buf_block_t *prev_block = nullptr;
2012 buf_block_t *next_block = nullptr;
2013
2014 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
2015 ut_ad(mtr_is_block_fix(mtr, new_block, MTR_MEMO_PAGE_X_FIX, index->table));
2016
2017 /* Create a memory heap where the data tuple is stored */
2018 heap = mem_heap_create(1024, UT_LOCATION_HERE);
2019
2020 /* Based on split direction, decide upper and lower pages */
2021 if (direction == FSP_DOWN) {
2022 btr_cur_t cursor;
2023 ulint *offsets;
2024
2025 lower_page = buf_block_get_frame(new_block);
2026 lower_page_no = new_block->page.id.page_no();
2027 lower_page_zip = buf_block_get_page_zip(new_block);
2028 upper_page = buf_block_get_frame(block);
2029 upper_page_no = block->page.id.page_no();
2030 upper_page_zip = buf_block_get_page_zip(block);
2031
2032 /* Look up the index for the node pointer to page */
2033 offsets =
2034 btr_page_get_father_block(nullptr, heap, index, block, mtr, &cursor);
2035
2036 /* Replace the address of the old child node (= page) with the
2037 address of the new lower half */
2038
2039 btr_node_ptr_set_child_page_no(btr_cur_get_rec(&cursor),
2040 btr_cur_get_page_zip(&cursor), offsets,
2041 lower_page_no, mtr);
2042 mem_heap_empty(heap);
2043 } else {
2044 lower_page = buf_block_get_frame(block);
2045 lower_page_no = block->page.id.page_no();
2046 lower_page_zip = buf_block_get_page_zip(block);
2047 upper_page = buf_block_get_frame(new_block);
2048 upper_page_no = new_block->page.id.page_no();
2049 upper_page_zip = buf_block_get_page_zip(new_block);
2050 }
2051
2052 /* Get the previous and next pages of page */
2053 prev_page_no = btr_page_get_prev(page, mtr);
2054 next_page_no = btr_page_get_next(page, mtr);
2055
2056 const space_id_t space = block->page.id.space();
2057
2058 /* for consistency, both blocks should be locked, before change */
2059 if (prev_page_no != FIL_NULL && direction == FSP_DOWN) {
2060 DEBUG_SYNC_C("btr_attach_half_pages_prev_block");
2061 prev_block = btr_block_get(page_id_t(space, prev_page_no), block->page.size,
2062 RW_X_LATCH, UT_LOCATION_HERE, index, mtr);
2063 }
2064 if (next_page_no != FIL_NULL && direction != FSP_DOWN) {
2065 next_block = btr_block_get(page_id_t(space, next_page_no), block->page.size,
2066 RW_X_LATCH, UT_LOCATION_HERE, index, mtr);
2067 }
2068
2069 /* Get the level of the split pages */
2070 level = btr_page_get_level(buf_block_get_frame(block));
2071 ut_ad(level == btr_page_get_level(buf_block_get_frame(new_block)));
2072
2073 /* Build the node pointer (= node key and page address) for the upper
2074 half */
2075
2076 node_ptr_upper =
2077 dict_index_build_node_ptr(index, split_rec, upper_page_no, heap, level);
2078
2079 /* Insert it next to the pointer to the lower half. Note that this
2080 may generate recursion leading to a split on the higher level. */
2081
2082 btr_insert_on_non_leaf_level(flags, index, level + 1, node_ptr_upper,
2083 UT_LOCATION_HERE, mtr);
2084
2085 /* Free the memory heap */
2086 mem_heap_free(heap);
2087
2088 /* Update page links of the level */
2089
2090 if (prev_block) {
2091 #ifdef UNIV_BTR_DEBUG
2092 ut_a(page_is_comp(prev_block->frame) == page_is_comp(page));
2093 ut_a(btr_page_get_next(prev_block->frame, mtr) == block->page.id.page_no());
2094 #endif /* UNIV_BTR_DEBUG */
2095
2096 btr_page_set_next(buf_block_get_frame(prev_block),
2097 buf_block_get_page_zip(prev_block), lower_page_no, mtr);
2098 }
2099
2100 if (next_block) {
2101 #ifdef UNIV_BTR_DEBUG
2102 ut_a(page_is_comp(next_block->frame) == page_is_comp(page));
2103 ut_a(btr_page_get_prev(next_block->frame, mtr) == page_get_page_no(page));
2104 #endif /* UNIV_BTR_DEBUG */
2105
2106 btr_page_set_prev(buf_block_get_frame(next_block),
2107 buf_block_get_page_zip(next_block), upper_page_no, mtr);
2108 }
2109
2110 if (direction == FSP_DOWN) {
2111 /* lower_page is new */
2112 btr_page_set_prev(lower_page, lower_page_zip, prev_page_no, mtr);
2113 } else {
2114 ut_ad(btr_page_get_prev(lower_page, mtr) == prev_page_no);
2115 }
2116
2117 btr_page_set_next(lower_page, lower_page_zip, upper_page_no, mtr);
2118 btr_page_set_prev(upper_page, upper_page_zip, lower_page_no, mtr);
2119
2120 if (direction != FSP_DOWN) {
2121 /* upper_page is new */
2122 btr_page_set_next(upper_page, upper_page_zip, next_page_no, mtr);
2123 } else {
2124 ut_ad(btr_page_get_next(upper_page, mtr) == next_page_no);
2125 }
2126 }
2127
2128 /** Determine if a tuple is smaller than any record on the page.
2129 @return true if smaller */
2130 [[nodiscard]] static bool btr_page_tuple_smaller(
2131 btr_cur_t *cursor, /*!< in: b-tree cursor */
2132 const dtuple_t *tuple, /*!< in: tuple to consider */
2133 ulint **offsets, /*!< in/out: temporary storage */
2134 ulint n_uniq, /*!< in: number of unique fields
2135 in the index page records */
2136 mem_heap_t **heap) /*!< in/out: heap for offsets */
2137 {
2138 buf_block_t *block;
2139 const rec_t *first_rec;
2140 page_cur_t pcur;
2141
2142 /* Read the first user record in the page. */
2143 block = btr_cur_get_block(cursor);
2144 page_cur_set_before_first(block, &pcur);
2145 page_cur_move_to_next(&pcur);
2146 first_rec = page_cur_get_rec(&pcur);
2147
2148 *offsets = rec_get_offsets(first_rec, cursor->index, *offsets, n_uniq,
2149 UT_LOCATION_HERE, heap);
2150
2151 return (cmp_dtuple_rec(tuple, first_rec, cursor->index, *offsets) < 0);
2152 }
2153
2154 /** Insert the tuple into the right sibling page, if the cursor is at the end
2155 of a page.
2156 @param[in] flags Undo logging and locking flags
2157 @param[in,out] cursor Cursor at which to insert; when the function succeeds,
2158 the cursor is positioned before the insert point.
2159 @param[out] offsets Offsets on inserted record
2160 @param[in,out] heap Memory heap for allocating offsets
2161 @param[in] tuple Tuple to insert
2162 @param[in,out] mtr Mini-transaction
2163 @return inserted record (first record on the right sibling page);
2164 the cursor will be positioned on the page infimum
2165 @retval NULL if the operation was not performed */
2166 static rec_t *btr_insert_into_right_sibling(uint32_t flags, btr_cur_t *cursor,
2167 ulint **offsets, mem_heap_t *heap,
2168 const dtuple_t *tuple, mtr_t *mtr) {
2169 buf_block_t *block = btr_cur_get_block(cursor);
2170 page_t *page = buf_block_get_frame(block);
2171 page_no_t next_page_no = btr_page_get_next(page, mtr);
2172
2173 ut_ad(cursor->index->table->is_intrinsic() ||
2174 mtr_memo_contains_flagged(mtr, dict_index_get_lock(cursor->index),
2175 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK));
2176 ut_ad(
2177 mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, cursor->index->table));
2178 ut_ad(heap);
2179
2180 if (next_page_no == FIL_NULL ||
2181 !page_rec_is_supremum(page_rec_get_next(btr_cur_get_rec(cursor)))) {
2182 return (nullptr);
2183 }
2184
2185 page_cur_t next_page_cursor;
2186 buf_block_t *next_block;
2187 page_t *next_page;
2188 btr_cur_t next_father_cursor;
2189 rec_t *rec = nullptr;
2190 ulint max_size;
2191
2192 const space_id_t space = block->page.id.space();
2193
2194 next_block = btr_block_get(page_id_t(space, next_page_no), block->page.size,
2195 RW_X_LATCH, UT_LOCATION_HERE, cursor->index, mtr);
2196 next_page = buf_block_get_frame(next_block);
2197
2198 bool is_leaf = page_is_leaf(next_page);
2199
2200 btr_page_get_father(cursor->index, next_block, mtr, &next_father_cursor);
2201
2202 page_cur_search(next_block, cursor->index, tuple, PAGE_CUR_LE,
2203 &next_page_cursor);
2204
2205 max_size = page_get_max_insert_size_after_reorganize(next_page, 1);
2206
2207 /* Extends gap lock for the next page */
2208 if (!dict_table_is_locking_disabled(cursor->index->table)) {
2209 lock_update_split_point(next_block, block);
2210 }
2211
2212 rec = page_cur_tuple_insert(&next_page_cursor, tuple, cursor->index, offsets,
2213 &heap, mtr);
2214
2215 if (rec == nullptr) {
2216 if (is_leaf && next_block->page.size.is_compressed() &&
2217 !cursor->index->is_clustered() &&
2218 !cursor->index->table->is_temporary()) {
2219 /* Reset the IBUF_BITMAP_FREE bits, because
2220 page_cur_tuple_insert() will have attempted page
2221 reorganize before failing. */
2222 ibuf_reset_free_bits(next_block);
2223 }
2224 return (nullptr);
2225 }
2226
2227 dberr_t err;
2228 ulint level = btr_page_get_level(next_page);
2229
2230 /* adjust cursor position */
2231 *btr_cur_get_page_cur(cursor) = next_page_cursor;
2232
2233 ut_ad(btr_cur_get_rec(cursor) == page_get_infimum_rec(next_page));
2234 ut_ad(page_rec_get_next(page_get_infimum_rec(next_page)) == rec);
2235
2236 /* We have to change the parent node pointer */
2237
2238 auto compressed = btr_cur_pessimistic_delete(&err, true, &next_father_cursor,
2239 BTR_CREATE_FLAG, false, 0, 0, 0,
2240 mtr, nullptr, nullptr);
2241
2242 ut_a(err == DB_SUCCESS);
2243
2244 if (!compressed) {
2245 btr_cur_compress_if_useful(&next_father_cursor, false, mtr);
2246 }
2247
2248 dtuple_t *node_ptr = dict_index_build_node_ptr(
2249 cursor->index, rec, next_block->page.id.page_no(), heap, level);
2250
2251 btr_insert_on_non_leaf_level(flags, cursor->index, level + 1, node_ptr,
2252 UT_LOCATION_HERE, mtr);
2253
2254 ut_ad(rec_offs_validate(rec, cursor->index, *offsets));
2255
2256 if (is_leaf && !cursor->index->is_clustered() &&
2257 !cursor->index->table->is_temporary()) {
2258 /* Update the free bits of the B-tree page in the
2259 insert buffer bitmap. */
2260
2261 if (next_block->page.size.is_compressed()) {
2262 ibuf_update_free_bits_zip(next_block, mtr);
2263 } else {
2264 ibuf_update_free_bits_if_full(
2265 next_block, max_size, rec_offs_size(*offsets) + PAGE_DIR_SLOT_SIZE);
2266 }
2267 }
2268
2269 return (rec);
2270 }
2271
2272 /** Splits an index page to halves and inserts the tuple. It is assumed
2273 that mtr holds an x-latch to the index tree. NOTE: the tree x-latch is
2274 released within this function! NOTE that the operation of this
2275 function must always succeed, we cannot reverse it: therefore enough
2276 free disk space (2 pages) must be guaranteed to be available before
2277 this function is called.
2278 @return inserted record */
2279 rec_t *btr_page_split_and_insert(
2280 uint32_t flags, /*!< in: undo logging and locking flags */
2281 btr_cur_t *cursor, /*!< in: cursor at which to insert; when the
2282 function returns, the cursor is positioned
2283 on the predecessor of the inserted record */
2284 ulint **offsets, /*!< out: offsets on inserted record */
2285 mem_heap_t **heap, /*!< in/out: pointer to memory heap, or NULL */
2286 const dtuple_t *tuple, /*!< in: tuple to insert */
2287 mtr_t *mtr) /*!< in: mtr */
2288 {
2289 buf_block_t *block;
2290 page_t *page;
2291 page_zip_des_t *page_zip;
2292 page_no_t page_no;
2293 byte direction;
2294 page_no_t hint_page_no;
2295 buf_block_t *new_block;
2296 page_t *new_page;
2297 page_zip_des_t *new_page_zip;
2298 rec_t *split_rec;
2299 buf_block_t *left_block;
2300 buf_block_t *right_block;
2301 buf_block_t *insert_block;
2302 page_cur_t *page_cursor;
2303 rec_t *first_rec;
2304 byte *buf = nullptr; /* remove warning */
2305 rec_t *move_limit;
2306 bool insert_will_fit;
2307 bool insert_left;
2308 ulint n_iterations = 0;
2309 rec_t *rec;
2310 ulint n_uniq;
2311 dict_index_t *index;
2312
2313 index = cursor->index;
2314
2315 if (dict_index_is_spatial(index)) {
2316 /* Split rtree page and update parent */
2317 return (
2318 rtr_page_split_and_insert(flags, cursor, offsets, heap, tuple, mtr));
2319 }
2320
2321 if (!*heap) {
2322 *heap = mem_heap_create(1024, UT_LOCATION_HERE);
2323 }
2324 n_uniq = dict_index_get_n_unique_in_tree(cursor->index);
2325 func_start:
2326 ut_ad(tuple->m_heap != *heap);
2327 mem_heap_empty(*heap);
2328 *offsets = nullptr;
2329
2330 ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(cursor->index),
2331 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
2332 cursor->index->table->is_intrinsic());
2333 ut_ad(!dict_index_is_online_ddl(cursor->index) || (flags & BTR_CREATE_FLAG) ||
2334 cursor->index->is_clustered());
2335 ut_ad(rw_lock_own_flagged(dict_index_get_lock(cursor->index),
2336 RW_LOCK_FLAG_X | RW_LOCK_FLAG_SX) ||
2337 cursor->index->table->is_intrinsic());
2338
2339 block = btr_cur_get_block(cursor);
2340 page = buf_block_get_frame(block);
2341 page_zip = buf_block_get_page_zip(block);
2342
2343 ut_ad(
2344 mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, cursor->index->table));
2345 ut_ad(!page_is_empty(page));
2346
2347 /* try to insert to the next page if possible before split */
2348 rec =
2349 btr_insert_into_right_sibling(flags, cursor, offsets, *heap, tuple, mtr);
2350
2351 if (rec != nullptr) {
2352 return (rec);
2353 }
2354
2355 page_no = block->page.id.page_no();
2356
2357 /* 1. Decide the split record; split_rec == NULL means that the
2358 tuple to be inserted should be the first record on the upper
2359 half-page */
2360 insert_left = false;
2361
2362 if (n_iterations > 0) {
2363 direction = FSP_UP;
2364 hint_page_no = page_no + 1;
2365 split_rec = btr_page_get_split_rec(cursor, tuple);
2366
2367 if (split_rec == nullptr) {
2368 insert_left =
2369 btr_page_tuple_smaller(cursor, tuple, offsets, n_uniq, heap);
2370 }
2371 } else if (btr_page_get_split_rec_to_right(cursor, &split_rec)) {
2372 direction = FSP_UP;
2373 hint_page_no = page_no + 1;
2374
2375 } else if (btr_page_get_split_rec_to_left(cursor, &split_rec)) {
2376 direction = FSP_DOWN;
2377 hint_page_no = page_no - 1;
2378 ut_ad(split_rec);
2379 } else {
2380 direction = FSP_UP;
2381 hint_page_no = page_no + 1;
2382
2383 /* If there is only one record in the index page, we
2384 can't split the node in the middle by default. We need
2385 to determine whether the new record will be inserted
2386 to the left or right. */
2387
2388 if (page_get_n_recs(page) > 1) {
2389 split_rec = page_get_middle_rec(page);
2390 } else if (btr_page_tuple_smaller(cursor, tuple, offsets, n_uniq, heap)) {
2391 split_rec = page_rec_get_next(page_get_infimum_rec(page));
2392 } else {
2393 split_rec = nullptr;
2394 }
2395 }
2396
2397 /* 2. Allocate a new page to the index */
2398 new_block = btr_page_alloc(cursor->index, hint_page_no, direction,
2399 btr_page_get_level(page), mtr, mtr);
2400
2401 /* New page could not be allocated */
2402 if (!new_block) {
2403 return nullptr;
2404 }
2405
2406 new_page = buf_block_get_frame(new_block);
2407 new_page_zip = buf_block_get_page_zip(new_block);
2408 btr_page_create(new_block, new_page_zip, cursor->index,
2409 btr_page_get_level(page), mtr);
2410
2411 /* 3. Calculate the first record on the upper half-page, and the
2412 first record (move_limit) on original page which ends up on the
2413 upper half */
2414
2415 if (split_rec) {
2416 first_rec = move_limit = split_rec;
2417
2418 *offsets = rec_get_offsets(split_rec, cursor->index, *offsets, n_uniq,
2419 UT_LOCATION_HERE, heap);
2420
2421 insert_left = cmp_dtuple_rec(tuple, split_rec, cursor->index, *offsets) < 0;
2422
2423 if (!insert_left && new_page_zip && n_iterations > 0) {
2424 /* If a compressed page has already been split,
2425 avoid further splits by inserting the record
2426 to an empty page. */
2427 split_rec = nullptr;
2428 goto insert_empty;
2429 }
2430 } else if (insert_left) {
2431 ut_a(n_iterations > 0);
2432 first_rec = page_rec_get_next(page_get_infimum_rec(page));
2433 move_limit = page_rec_get_next(btr_cur_get_rec(cursor));
2434 } else {
2435 insert_empty:
2436 ut_ad(!split_rec);
2437 ut_ad(!insert_left);
2438 buf = ut::new_arr_withkey<byte>(
2439 UT_NEW_THIS_FILE_PSI_KEY,
2440 ut::Count{rec_get_converted_size(cursor->index, tuple)});
2441
2442 first_rec = rec_convert_dtuple_to_rec(buf, cursor->index, tuple);
2443 move_limit = page_rec_get_next(btr_cur_get_rec(cursor));
2444 }
2445
2446 /* 4. Do first the modifications in the tree structure */
2447
2448 btr_attach_half_pages(flags, cursor->index, block, first_rec, new_block,
2449 direction, mtr);
2450
2451 /* If the split is made on the leaf level and the insert will fit
2452 on the appropriate half-page, we may release the tree x-latch.
2453 We can then move the records after releasing the tree latch,
2454 thus reducing the tree latch contention. */
2455
2456 if (split_rec) {
2457 insert_will_fit =
2458 !new_page_zip &&
2459 btr_page_insert_fits(cursor, split_rec, offsets, tuple, heap);
2460 } else {
2461 if (!insert_left) {
2462 ut::delete_arr(buf);
2463 buf = nullptr;
2464 }
2465
2466 insert_will_fit =
2467 !new_page_zip &&
2468 btr_page_insert_fits(cursor, nullptr, offsets, tuple, heap);
2469 }
2470
2471 if (!srv_read_only_mode && !cursor->index->table->is_intrinsic() &&
2472 insert_will_fit && page_is_leaf(page) &&
2473 !dict_index_is_online_ddl(cursor->index)) {
2474 mtr->memo_release(dict_index_get_lock(cursor->index),
2475 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK);
2476
2477 /* NOTE: We cannot release root block latch here, because it
2478 has segment header and already modified in most of cases.*/
2479 }
2480
2481 /* 5. Move then the records to the new page */
2482 if (direction == FSP_DOWN) {
2483 /* fputs("Split left\n", stderr); */
2484
2485 if (false
2486 #ifdef UNIV_ZIP_COPY
2487 || page_zip
2488 #endif /* UNIV_ZIP_COPY */
2489 || !page_move_rec_list_start(new_block, block, move_limit,
2490 cursor->index, mtr)) {
2491 /* For some reason, compressing new_page failed,
2492 even though it should contain fewer records than
2493 the original page. Copy the page byte for byte
2494 and then delete the records from both pages
2495 as appropriate. Deleting will always succeed. */
2496 ut_a(new_page_zip);
2497
2498 page_zip_copy_recs(new_page_zip, new_page, page_zip, page, cursor->index,
2499 mtr);
2500 page_delete_rec_list_end(move_limit - page + new_page, new_block,
2501 cursor->index, ULINT_UNDEFINED, ULINT_UNDEFINED,
2502 mtr);
2503
2504 /* Update the lock table and possible hash index. */
2505
2506 if (!dict_table_is_locking_disabled(cursor->index->table)) {
2507 lock_move_rec_list_start(new_block, block, move_limit,
2508 new_page + PAGE_NEW_INFIMUM);
2509 }
2510
2511 btr_search_update_hash_on_move(new_block, block, cursor->index);
2512
2513 /* Delete the records from the source page. */
2514
2515 page_delete_rec_list_start(move_limit, block, cursor->index, mtr);
2516 }
2517
2518 left_block = new_block;
2519 right_block = block;
2520
2521 if (!dict_table_is_locking_disabled(cursor->index->table)) {
2522 lock_update_split_left(right_block, left_block);
2523 }
2524 } else {
2525 /* fputs("Split right\n", stderr); */
2526
2527 if (false
2528 #ifdef UNIV_ZIP_COPY
2529 || page_zip
2530 #endif /* UNIV_ZIP_COPY */
2531 || !page_move_rec_list_end(new_block, block, move_limit, cursor->index,
2532 mtr)) {
2533 /* For some reason, compressing new_page failed,
2534 even though it should contain fewer records than
2535 the original page. Copy the page byte for byte
2536 and then delete the records from both pages
2537 as appropriate. Deleting will always succeed. */
2538 ut_a(new_page_zip);
2539
2540 page_zip_copy_recs(new_page_zip, new_page, page_zip, page, cursor->index,
2541 mtr);
2542 page_delete_rec_list_start(move_limit - page + new_page, new_block,
2543 cursor->index, mtr);
2544
2545 /* Update the lock table and possible hash index. */
2546 if (!dict_table_is_locking_disabled(cursor->index->table)) {
2547 lock_move_rec_list_end(new_block, block, move_limit);
2548 }
2549
2550 ut_ad(!dict_index_is_spatial(index));
2551
2552 btr_search_update_hash_on_move(new_block, block, cursor->index);
2553
2554 /* Delete the records from the source page. */
2555
2556 page_delete_rec_list_end(move_limit, block, cursor->index,
2557 ULINT_UNDEFINED, ULINT_UNDEFINED, mtr);
2558 }
2559
2560 left_block = block;
2561 right_block = new_block;
2562
2563 if (!dict_table_is_locking_disabled(cursor->index->table)) {
2564 lock_update_split_right(right_block, left_block);
2565 }
2566 }
2567
2568 #ifdef UNIV_ZIP_DEBUG
2569 if (page_zip) {
2570 ut_a(page_zip_validate(page_zip, page, cursor->index));
2571 ut_a(page_zip_validate(new_page_zip, new_page, cursor->index));
2572 }
2573 #endif /* UNIV_ZIP_DEBUG */
2574
2575 /* At this point, split_rec, move_limit and first_rec may point
2576 to garbage on the old page. */
2577
2578 /* 6. The split and the tree modification is now completed. Decide the
2579 page where the tuple should be inserted */
2580
2581 if (insert_left) {
2582 insert_block = left_block;
2583 } else {
2584 insert_block = right_block;
2585 }
2586
2587 /* 7. Reposition the cursor for insert and try insertion */
2588 page_cursor = btr_cur_get_page_cur(cursor);
2589
2590 page_cur_search(insert_block, cursor->index, tuple, page_cursor);
2591
2592 rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, offsets, heap,
2593 mtr);
2594
2595 #ifdef UNIV_ZIP_DEBUG
2596 {
2597 page_t *insert_page = buf_block_get_frame(insert_block);
2598
2599 page_zip_des_t *insert_page_zip = buf_block_get_page_zip(insert_block);
2600
2601 ut_a(!insert_page_zip ||
2602 page_zip_validate(insert_page_zip, insert_page, cursor->index));
2603 }
2604 #endif /* UNIV_ZIP_DEBUG */
2605
2606 if (rec != nullptr) {
2607 goto func_exit;
2608 }
2609
2610 /* 8. If insert did not fit, try page reorganization.
2611 For compressed pages, page_cur_tuple_insert() will have
2612 attempted this already. */
2613
2614 if (page_cur_get_page_zip(page_cursor) ||
2615 !btr_page_reorganize(page_cursor, cursor->index, mtr)) {
2616 goto insert_failed;
2617 }
2618
2619 rec = page_cur_tuple_insert(page_cursor, tuple, cursor->index, offsets, heap,
2620 mtr);
2621
2622 if (rec == nullptr) {
2623 /* The insert did not fit on the page: loop back to the
2624 start of the function for a new split */
2625 insert_failed:
2626 /* We play safe and reset the free bits for new_page */
2627 if (!cursor->index->is_clustered() &&
2628 !cursor->index->table->is_temporary()) {
2629 ibuf_reset_free_bits(new_block);
2630 ibuf_reset_free_bits(block);
2631 }
2632
2633 n_iterations++;
2634 ut_ad(n_iterations < 2 || buf_block_get_page_zip(insert_block));
2635 ut_ad(!insert_will_fit);
2636
2637 goto func_start;
2638 }
2639
2640 func_exit:
2641 /* Insert fit on the page: update the free bits for the
2642 left and right pages in the same mtr */
2643
2644 if (!cursor->index->is_clustered() && !cursor->index->table->is_temporary() &&
2645 page_is_leaf(page)) {
2646 ibuf_update_free_bits_for_two_pages_low(left_block, right_block, mtr);
2647 }
2648
2649 MONITOR_INC(MONITOR_INDEX_SPLIT);
2650
2651 ut_ad(page_validate(buf_block_get_frame(left_block), cursor->index));
2652 ut_ad(page_validate(buf_block_get_frame(right_block), cursor->index));
2653
2654 ut_ad(!rec || rec_offs_validate(rec, cursor->index, *offsets));
2655 return (rec);
2656 }
2657
2658 /** Removes a page from the level list of pages.
2659 @param[in] space Space where removed
2660 @param[in] page_size Page size
2661 @param[in,out] page Page to remove
2662 @param[in] index Index tree
2663 @param[in,out] mtr Mini-transaction */
2664 static void btr_level_list_remove(space_id_t space,
2665 const page_size_t &page_size, page_t *page,
2666 const dict_index_t *index, mtr_t *mtr) {
2667 ut_ad(page != nullptr);
2668 ut_ad(mtr != nullptr);
2669 ut_ad(mtr_is_page_fix(mtr, page, MTR_MEMO_PAGE_X_FIX, index->table));
2670 ut_ad(space == page_get_space_id(page));
2671 /* Get the previous and next page numbers of page */
2672
2673 const page_no_t prev_page_no = btr_page_get_prev(page, mtr);
2674 const page_no_t next_page_no = btr_page_get_next(page, mtr);
2675
2676 /* Update page links of the level */
2677
2678 if (prev_page_no != FIL_NULL) {
2679 buf_block_t *prev_block =
2680 btr_block_get(page_id_t(space, prev_page_no), page_size, RW_X_LATCH,
2681 UT_LOCATION_HERE, index, mtr);
2682
2683 page_t *prev_page = buf_block_get_frame(prev_block);
2684 #ifdef UNIV_BTR_DEBUG
2685 ut_a(page_is_comp(prev_page) == page_is_comp(page));
2686 ut_a(btr_page_get_next(prev_page, mtr) == page_get_page_no(page));
2687 #endif /* UNIV_BTR_DEBUG */
2688
2689 btr_page_set_next(prev_page, buf_block_get_page_zip(prev_block),
2690 next_page_no, mtr);
2691 }
2692
2693 if (next_page_no != FIL_NULL) {
2694 buf_block_t *next_block =
2695 btr_block_get(page_id_t(space, next_page_no), page_size, RW_X_LATCH,
2696 UT_LOCATION_HERE, index, mtr);
2697
2698 page_t *next_page = buf_block_get_frame(next_block);
2699 #ifdef UNIV_BTR_DEBUG
2700 ut_a(page_is_comp(next_page) == page_is_comp(page));
2701 ut_a(btr_page_get_prev(next_page, mtr) == page_get_page_no(page));
2702 #endif /* UNIV_BTR_DEBUG */
2703
2704 btr_page_set_prev(next_page, buf_block_get_page_zip(next_block),
2705 prev_page_no, mtr);
2706 }
2707 }
2708
2709 /** Writes the redo log record for setting an index record as the predefined
2710 minimum record. */
2711 static inline void btr_set_min_rec_mark_log(
2712 rec_t *rec, /*!< in: record */
2713 mlog_id_t type, /*!< in: MLOG_COMP_REC_MIN_MARK or
2714 MLOG_REC_MIN_MARK */
2715 mtr_t *mtr) /*!< in: mtr */
2716 {
2717 mlog_write_initial_log_record(rec, type, mtr);
2718
2719 /* Write rec offset as a 2-byte ulint */
2720 mlog_catenate_ulint(mtr, page_offset(rec), MLOG_2BYTES);
2721 }
2722 #else /* !UNIV_HOTBACKUP */
2723 static inline void btr_set_min_rec_mark_log(rec_t *rec, mlog_id_t type,
2724 mtr_t *mtr) {}
2725 #endif /* !UNIV_HOTBACKUP */
2726
2727 /** Parses the redo log record for setting an index record as the predefined
2728 minimum record.
2729 @return end of log record or NULL */
2730 byte *btr_parse_set_min_rec_mark(
2731 byte *ptr, /*!< in: buffer */
2732 byte *end_ptr, /*!< in: buffer end */
2733 ulint comp, /*!< in: nonzero=compact page format */
2734 page_t *page, /*!< in: page or NULL */
2735 mtr_t *mtr) /*!< in: mtr or NULL */
2736 {
2737 rec_t *rec;
2738
2739 if (end_ptr < ptr + 2) {
2740 return (nullptr);
2741 }
2742
2743 if (page) {
2744 ut_a(!page_is_comp(page) == !comp);
2745
2746 rec = page + mach_read_from_2(ptr);
2747
2748 btr_set_min_rec_mark(rec, mtr);
2749 }
2750
2751 return (ptr + 2);
2752 }
2753
2754 /** Sets a record as the predefined minimum record.
2755 @param[in,out] rec Record
2756 @param[in] mtr Mini-transaction
2757 */
2758 void btr_set_min_rec_mark(rec_t *rec, mtr_t *mtr) {
2759 ulint info_bits;
2760
2761 if (page_rec_is_comp(rec)) {
2762 info_bits = rec_get_info_bits(rec, true);
2763
2764 rec_set_info_bits_new(rec, info_bits | REC_INFO_MIN_REC_FLAG);
2765
2766 btr_set_min_rec_mark_log(rec, MLOG_COMP_REC_MIN_MARK, mtr);
2767 } else {
2768 info_bits = rec_get_info_bits(rec, false);
2769
2770 rec_set_info_bits_old(rec, info_bits | REC_INFO_MIN_REC_FLAG);
2771
2772 btr_set_min_rec_mark_log(rec, MLOG_REC_MIN_MARK, mtr);
2773 }
2774 }
2775
2776 #ifndef UNIV_HOTBACKUP
2777 bool btr_is_index_empty(const dict_index_t *index) {
2778 mtr_t mtr;
2779 mtr.start();
2780 buf_block_t *block = btr_root_block_get(index, RW_X_LATCH, &mtr);
2781 const bool is_empty =
2782 block->is_index_page() && block->is_leaf() && block->is_empty();
2783 mtr.commit();
2784 return is_empty;
2785 }
2786
2787 /** Deletes on the upper level the node pointer to a page.
2788 @param[in] index Index tree
2789 @param[in] block Page whose node pointer is deleted
2790 @param[in] mtr Mini-transaction
2791 */
2792 void btr_node_ptr_delete(dict_index_t *index, buf_block_t *block, mtr_t *mtr) {
2793 btr_cur_t cursor;
2794 dberr_t err;
2795
2796 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
2797
2798 /* Delete node pointer on father page */
2799 btr_page_get_father(index, block, mtr, &cursor);
2800
2801 bool compressed =
2802 btr_cur_pessimistic_delete(&err, true, &cursor, BTR_CREATE_FLAG, false, 0,
2803 0, 0, mtr, nullptr, nullptr);
2804 ut_a(err == DB_SUCCESS);
2805
2806 if (!compressed) {
2807 btr_cur_compress_if_useful(&cursor, false, mtr);
2808 }
2809 }
2810
2811 /** If page is the only on its level, this function moves its records to the
2812 father page, thus reducing the tree height.
2813 @return father block */
2814 static buf_block_t *btr_lift_page_up(
2815 dict_index_t *index, /*!< in: index tree */
2816 buf_block_t *block, /*!< in: page which is the only on its level;
2817 must not be empty: use
2818 btr_discard_only_page_on_level if the last
2819 record from the page should be removed */
2820 mtr_t *mtr) /*!< in: mtr */
2821 {
2822 buf_block_t *father_block;
2823 page_t *father_page;
2824 ulint page_level;
2825 page_zip_des_t *father_page_zip;
2826 page_t *page = buf_block_get_frame(block);
2827 page_no_t root_page_no;
2828 buf_block_t *blocks[BTR_MAX_LEVELS];
2829 ulint n_blocks; /*!< last used index in blocks[] */
2830 ulint i;
2831 bool lift_father_up;
2832 buf_block_t *block_orig = block;
2833
2834 ut_ad(btr_page_get_prev(page, mtr) == FIL_NULL);
2835 ut_ad(btr_page_get_next(page, mtr) == FIL_NULL);
2836 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
2837
2838 page_level = btr_page_get_level(page);
2839 root_page_no = dict_index_get_page(index);
2840
2841 {
2842 btr_cur_t cursor;
2843 ulint *offsets = nullptr;
2844 mem_heap_t *heap = mem_heap_create(
2845 sizeof(*offsets) * (REC_OFFS_HEADER_SIZE + 1 + 1 + index->n_fields),
2846 UT_LOCATION_HERE);
2847 buf_block_t *b;
2848
2849 if (dict_index_is_spatial(index)) {
2850 offsets = rtr_page_get_father_block(nullptr, heap, index, block, mtr,
2851 nullptr, &cursor);
2852 } else {
2853 offsets =
2854 btr_page_get_father_block(offsets, heap, index, block, mtr, &cursor);
2855 }
2856 father_block = btr_cur_get_block(&cursor);
2857 father_page_zip = buf_block_get_page_zip(father_block);
2858 father_page = buf_block_get_frame(father_block);
2859
2860 n_blocks = 0;
2861
2862 /* Store all ancestor pages so we can reset their
2863 levels later on. We have to do all the searches on
2864 the tree now because later on, after we've replaced
2865 the first level, the tree is in an inconsistent state
2866 and can not be searched. */
2867 for (b = father_block; b->page.id.page_no() != root_page_no;) {
2868 ut_a(n_blocks < BTR_MAX_LEVELS);
2869
2870 if (dict_index_is_spatial(index)) {
2871 offsets = rtr_page_get_father_block(nullptr, heap, index, b, mtr,
2872 nullptr, &cursor);
2873 } else {
2874 offsets =
2875 btr_page_get_father_block(offsets, heap, index, b, mtr, &cursor);
2876 }
2877
2878 blocks[n_blocks++] = b = btr_cur_get_block(&cursor);
2879 }
2880
2881 lift_father_up = (n_blocks && page_level == 0);
2882 if (lift_father_up) {
2883 /* The father page also should be the only on its level (not
2884 root). We should lift up the father page at first.
2885 Because the leaf page should be lifted up only for root page.
2886 The freeing page is based on page_level (==0 or !=0)
2887 to choose segment. If the page_level is changed ==0 from !=0,
2888 later freeing of the page doesn't find the page allocation
2889 to be freed.*/
2890
2891 block = father_block;
2892 page = buf_block_get_frame(block);
2893 page_level = btr_page_get_level(page);
2894
2895 ut_ad(btr_page_get_prev(page, mtr) == FIL_NULL);
2896 ut_ad(btr_page_get_next(page, mtr) == FIL_NULL);
2897 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
2898
2899 father_block = blocks[0];
2900 father_page_zip = buf_block_get_page_zip(father_block);
2901 father_page = buf_block_get_frame(father_block);
2902 }
2903
2904 mem_heap_free(heap);
2905 }
2906
2907 btr_search_drop_page_hash_index(block);
2908
2909 /* Make the father empty */
2910 btr_page_empty(father_block, father_page_zip, index, page_level, mtr);
2911 page_level++;
2912
2913 /* Copy the records to the father page one by one. */
2914 if (false
2915 #ifdef UNIV_ZIP_COPY
2916 || father_page_zip
2917 #endif /* UNIV_ZIP_COPY */
2918 || !page_copy_rec_list_end(father_block, block,
2919 page_get_infimum_rec(page), index, mtr)) {
2920 const page_zip_des_t *page_zip = buf_block_get_page_zip(block);
2921 ut_a(father_page_zip);
2922 ut_a(page_zip);
2923
2924 /* Copy the page byte for byte. */
2925 page_zip_copy_recs(father_page_zip, father_page, page_zip, page, index,
2926 mtr);
2927
2928 /* Update the lock table and possible hash index. */
2929
2930 if (!dict_table_is_locking_disabled(index->table)) {
2931 lock_move_rec_list_end(father_block, block, page_get_infimum_rec(page));
2932 }
2933
2934 /* Also update the predicate locks */
2935 if (dict_index_is_spatial(index)) {
2936 lock_prdt_rec_move(father_block, block);
2937 }
2938
2939 btr_search_update_hash_on_move(father_block, block, index);
2940 }
2941
2942 if (!dict_table_is_locking_disabled(index->table)) {
2943 /* Free predicate page locks on the block */
2944 if (dict_index_is_spatial(index)) {
2945 locksys::Shard_latch_guard guard{UT_LOCATION_HERE, block->get_page_id()};
2946 lock_prdt_page_free_from_discard(block, lock_sys->prdt_page_hash);
2947 }
2948 lock_update_copy_and_discard(father_block, block);
2949 }
2950
2951 /* Go upward to root page, decrementing levels by one. */
2952 for (i = lift_father_up ? 1 : 0; i < n_blocks; i++, page_level++) {
2953 page_t *page = buf_block_get_frame(blocks[i]);
2954 page_zip_des_t *page_zip = buf_block_get_page_zip(blocks[i]);
2955
2956 ut_ad(btr_page_get_level(page) == page_level + 1);
2957
2958 btr_page_set_level(page, page_zip, page_level, mtr);
2959 #ifdef UNIV_ZIP_DEBUG
2960 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
2961 #endif /* UNIV_ZIP_DEBUG */
2962 }
2963
2964 if (dict_index_is_spatial(index)) {
2965 rtr_check_discard_page(index, nullptr, block);
2966 }
2967
2968 /* Free the file page */
2969 btr_page_free(index, block, mtr);
2970
2971 /* We play it safe and reset the free bits for the father */
2972 if (!index->is_clustered() && !index->table->is_temporary()) {
2973 ibuf_reset_free_bits(father_block);
2974 }
2975 ut_ad(page_validate(father_page, index));
2976 ut_d(btr_check_node_ptr(index, father_block, mtr));
2977
2978 return (lift_father_up ? block_orig : father_block);
2979 }
2980
2981 bool btr_compress(btr_cur_t *cursor, bool adjust, mtr_t *mtr) {
2982 dict_index_t *index;
2983 space_id_t space;
2984 page_no_t left_page_no;
2985 page_no_t right_page_no;
2986 buf_block_t *merge_block;
2987 page_t *merge_page = nullptr;
2988 page_zip_des_t *merge_page_zip;
2989 bool is_left;
2990 buf_block_t *block;
2991 page_t *page;
2992 btr_cur_t father_cursor;
2993 mem_heap_t *heap;
2994 ulint *offsets;
2995 ulint nth_rec = 0; /* remove bogus warning */
2996 bool mbr_changed = false;
2997 #ifdef UNIV_DEBUG
2998 bool leftmost_child;
2999 #endif
3000 DBUG_TRACE;
3001
3002 block = btr_cur_get_block(cursor);
3003 page = btr_cur_get_page(cursor);
3004 index = cursor->index;
3005
3006 btr_assert_not_corrupted(block, index);
3007
3008 #ifdef UNIV_DEBUG
3009 if (dict_index_is_spatial(index)) {
3010 ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
3011 MTR_MEMO_X_LOCK));
3012 } else {
3013 ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
3014 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
3015 index->table->is_intrinsic());
3016 }
3017 #endif /* UNIV_DEBUG */
3018
3019 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
3020 space = dict_index_get_space(index);
3021
3022 const page_size_t page_size(dict_table_page_size(index->table));
3023
3024 MONITOR_INC(MONITOR_INDEX_MERGE_ATTEMPTS);
3025
3026 left_page_no = btr_page_get_prev(page, mtr);
3027 right_page_no = btr_page_get_next(page, mtr);
3028
3029 #ifdef UNIV_DEBUG
3030 if (!page_is_leaf(page) && left_page_no == FIL_NULL) {
3031 ut_a(REC_INFO_MIN_REC_FLAG &
3032 rec_get_info_bits(page_rec_get_next(page_get_infimum_rec(page)),
3033 page_is_comp(page)));
3034 }
3035 #endif /* UNIV_DEBUG */
3036
3037 heap = mem_heap_create(100, UT_LOCATION_HERE);
3038
3039 if (dict_index_is_spatial(index)) {
3040 offsets = rtr_page_get_father_block(nullptr, heap, index, block, mtr,
3041 cursor, &father_cursor);
3042 ut_ad(cursor->page_cur.block->page.id.page_no() ==
3043 block->page.id.page_no());
3044 rec_t *my_rec = father_cursor.page_cur.rec;
3045
3046 page_no_t page_no = btr_node_ptr_get_child_page_no(my_rec, offsets);
3047
3048 if (page_no != block->page.id.page_no()) {
3049 ib::info(ER_IB_MSG_32) << "father positioned on page " << page_no
3050 << "instead of " << block->page.id.page_no();
3051 offsets = btr_page_get_father_block(nullptr, heap, index, block, mtr,
3052 &father_cursor);
3053 }
3054 } else {
3055 offsets = btr_page_get_father_block(nullptr, heap, index, block, mtr,
3056 &father_cursor);
3057 }
3058
3059 if (adjust) {
3060 nth_rec = page_rec_get_n_recs_before(btr_cur_get_rec(cursor));
3061 ut_ad(nth_rec > 0);
3062 }
3063
3064 if (left_page_no == FIL_NULL && right_page_no == FIL_NULL) {
3065 /* The page is the only one on the level, lift the records
3066 to the father */
3067
3068 merge_block = btr_lift_page_up(index, block, mtr);
3069 goto func_exit;
3070 }
3071
3072 ut_d(leftmost_child =
3073 left_page_no != FIL_NULL &&
3074 (page_rec_get_next(page_get_infimum_rec(btr_cur_get_page(
3075 &father_cursor))) == btr_cur_get_rec(&father_cursor)));
3076
3077 /* Decide the page to which we try to merge and which will inherit
3078 the locks */
3079
3080 is_left = btr_can_merge_with_page(cursor, left_page_no, &merge_block, mtr);
3081
3082 DBUG_EXECUTE_IF("ib_always_merge_right", is_left = false;);
3083 retry:
3084 if (!is_left &&
3085 !btr_can_merge_with_page(cursor, right_page_no, &merge_block, mtr)) {
3086 if (!merge_block) {
3087 merge_page = nullptr;
3088 }
3089 goto err_exit;
3090 }
3091
3092 merge_page = buf_block_get_frame(merge_block);
3093
3094 #ifdef UNIV_BTR_DEBUG
3095 if (is_left) {
3096 ut_a(btr_page_get_next(merge_page, mtr) == block->page.id.page_no());
3097 } else {
3098 ut_a(btr_page_get_prev(merge_page, mtr) == block->page.id.page_no());
3099 }
3100 #endif /* UNIV_BTR_DEBUG */
3101
3102 #ifdef UNIV_GIS_DEBUG
3103 if (dict_index_is_spatial(index)) {
3104 if (is_left) {
3105 fprintf(stderr, "GIS_DIAG: merge left %ld to %ld \n",
3106 (long)block->page.id.page_no(), left_page_no);
3107 } else {
3108 fprintf(stderr, "GIS_DIAG: merge right %ld to %ld\n",
3109 (long)block->page.id.page_no(), right_page_no);
3110 }
3111 }
3112 #endif /* UNIV_GIS_DEBUG */
3113
3114 ut_ad(page_validate(merge_page, index));
3115
3116 merge_page_zip = buf_block_get_page_zip(merge_block);
3117 #ifdef UNIV_ZIP_DEBUG
3118 if (merge_page_zip) {
3119 const page_zip_des_t *page_zip = buf_block_get_page_zip(block);
3120 ut_a(page_zip);
3121 ut_a(page_zip_validate(merge_page_zip, merge_page, index));
3122 ut_a(page_zip_validate(page_zip, page, index));
3123 }
3124 #endif /* UNIV_ZIP_DEBUG */
3125
3126 /* Move records to the merge page */
3127 if (is_left) {
3128 btr_cur_t cursor2;
3129 rtr_mbr_t new_mbr;
3130 ulint *offsets2 = nullptr;
3131
3132 /* For rtree, we need to update father's mbr. */
3133 if (dict_index_is_spatial(index)) {
3134 /* We only support merge pages with the same parent
3135 page */
3136 if (!rtr_check_same_block(index, &cursor2,
3137 btr_cur_get_block(&father_cursor), merge_block,
3138 heap)) {
3139 is_left = false;
3140 goto retry;
3141 }
3142
3143 /* Set rtr_info for cursor2, since it is
3144 necessary in recursive page merge. */
3145 cursor2.rtr_info = cursor->rtr_info;
3146 cursor2.tree_height = cursor->tree_height;
3147
3148 offsets2 = rec_get_offsets(btr_cur_get_rec(&cursor2), index, nullptr,
3149 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
3150
3151 /* Check if parent entry needs to be updated */
3152 mbr_changed = rtr_merge_mbr_changed(&cursor2, &father_cursor, offsets2,
3153 offsets, &new_mbr);
3154 }
3155
3156 rec_t *orig_pred = page_copy_rec_list_start(
3157 merge_block, block, page_get_supremum_rec(page), index, mtr);
3158
3159 if (!orig_pred) {
3160 goto err_exit;
3161 }
3162
3163 btr_search_drop_page_hash_index(block);
3164
3165 /* Remove the page from the level list */
3166 btr_level_list_remove(space, page_size, page, index, mtr);
3167
3168 if (dict_index_is_spatial(index)) {
3169 rec_t *my_rec = father_cursor.page_cur.rec;
3170
3171 page_no_t page_no = btr_node_ptr_get_child_page_no(my_rec, offsets);
3172
3173 if (page_no != block->page.id.page_no()) {
3174 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_33)
3175 << "father positioned on " << page_no << " instead of "
3176 << block->page.id.page_no();
3177
3178 ut_d(ut_error);
3179 }
3180
3181 if (mbr_changed) {
3182 #ifdef UNIV_DEBUG
3183 bool success = rtr_update_mbr_field(&cursor2, offsets2, &father_cursor,
3184 merge_page, &new_mbr, nullptr, mtr);
3185
3186 ut_ad(success);
3187 #else
3188 rtr_update_mbr_field(&cursor2, offsets2, &father_cursor, merge_page,
3189 &new_mbr, nullptr, mtr);
3190 #endif
3191 } else {
3192 rtr_node_ptr_delete(&father_cursor, mtr);
3193 }
3194
3195 /* No GAP lock needs to be worrying about */
3196 locksys::Shard_latch_guard guard{UT_LOCATION_HERE, block->get_page_id()};
3197 lock_prdt_page_free_from_discard(block, lock_sys->prdt_page_hash);
3198 lock_rec_free_all_from_discard_page(block);
3199 } else {
3200 btr_node_ptr_delete(index, block, mtr);
3201 if (!dict_table_is_locking_disabled(index->table)) {
3202 lock_update_merge_left(merge_block, orig_pred, block);
3203 }
3204 }
3205
3206 if (adjust) {
3207 nth_rec += page_rec_get_n_recs_before(orig_pred);
3208 }
3209 } else {
3210 rec_t *orig_succ;
3211 bool compressed;
3212 dberr_t err;
3213 btr_cur_t cursor2;
3214 /* father cursor pointing to node ptr
3215 of the right sibling */
3216 #ifdef UNIV_BTR_DEBUG
3217 byte fil_page_prev[4];
3218 #endif /* UNIV_BTR_DEBUG */
3219
3220 if (dict_index_is_spatial(index)) {
3221 cursor2.rtr_info = nullptr;
3222
3223 /* For spatial index, we disallow merge of blocks
3224 with different parents, since the merge would need
3225 to update entry (for MBR and Primary key) in the
3226 parent of block being merged */
3227 if (!rtr_check_same_block(index, &cursor2,
3228 btr_cur_get_block(&father_cursor), merge_block,
3229 heap)) {
3230 goto err_exit;
3231 }
3232
3233 /* Set rtr_info for cursor2, since it is
3234 necessary in recursive page merge. */
3235 cursor2.rtr_info = cursor->rtr_info;
3236 cursor2.tree_height = cursor->tree_height;
3237 } else {
3238 btr_page_get_father(index, merge_block, mtr, &cursor2);
3239 }
3240
3241 if (merge_page_zip && left_page_no == FIL_NULL) {
3242 /* The function page_zip_compress(), which will be
3243 invoked by page_copy_rec_list_end() below,
3244 requires that FIL_PAGE_PREV be FIL_NULL.
3245 Clear the field, but prepare to restore it. */
3246 #ifdef UNIV_BTR_DEBUG
3247 memcpy(fil_page_prev, merge_page + FIL_PAGE_PREV, 4);
3248 #endif /* UNIV_BTR_DEBUG */
3249 static_assert(FIL_NULL == 0xffffffff, "FIL_NULL != 0xffffffff");
3250 memset(merge_page + FIL_PAGE_PREV, 0xff, 4);
3251 }
3252
3253 orig_succ = page_copy_rec_list_end(
3254 merge_block, block, page_get_infimum_rec(page), cursor->index, mtr);
3255
3256 if (!orig_succ) {
3257 ut_a(merge_page_zip);
3258 #ifdef UNIV_BTR_DEBUG
3259 if (left_page_no == FIL_NULL) {
3260 /* FIL_PAGE_PREV was restored from
3261 merge_page_zip. */
3262 ut_a(!memcmp(fil_page_prev, merge_page + FIL_PAGE_PREV, 4));
3263 }
3264 #endif /* UNIV_BTR_DEBUG */
3265 goto err_exit;
3266 }
3267
3268 btr_search_drop_page_hash_index(block);
3269
3270 #ifdef UNIV_BTR_DEBUG
3271 if (merge_page_zip && left_page_no == FIL_NULL) {
3272 /* Restore FIL_PAGE_PREV in order to avoid an assertion
3273 failure in btr_level_list_remove(), which will set
3274 the field again to FIL_NULL. Even though this makes
3275 merge_page and merge_page_zip inconsistent for a
3276 split second, it is harmless, because the pages
3277 are X-latched. */
3278 memcpy(merge_page + FIL_PAGE_PREV, fil_page_prev, 4);
3279 }
3280 #endif /* UNIV_BTR_DEBUG */
3281
3282 /* Remove the page from the level list */
3283 btr_level_list_remove(space, page_size, page, index, mtr);
3284
3285 ut_ad(btr_node_ptr_get_child_page_no(btr_cur_get_rec(&father_cursor),
3286 offsets) == block->page.id.page_no());
3287
3288 /* Replace the address of the old child node (= page) with the
3289 address of the merge page to the right */
3290 btr_node_ptr_set_child_page_no(btr_cur_get_rec(&father_cursor),
3291 btr_cur_get_page_zip(&father_cursor),
3292 offsets, right_page_no, mtr);
3293
3294 #ifdef UNIV_DEBUG
3295 if (!page_is_leaf(page) && left_page_no == FIL_NULL) {
3296 ut_ad(REC_INFO_MIN_REC_FLAG &
3297 rec_get_info_bits(page_rec_get_next(page_get_infimum_rec(
3298 buf_block_get_frame(merge_block))),
3299 page_is_comp(page)));
3300 }
3301 #endif /* UNIV_DEBUG */
3302
3303 /* For rtree, we need to update father's mbr. */
3304 if (dict_index_is_spatial(index)) {
3305 ulint *offsets2;
3306 ulint rec_info;
3307
3308 offsets2 = rec_get_offsets(btr_cur_get_rec(&cursor2), index, nullptr,
3309 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
3310
3311 ut_ad(btr_node_ptr_get_child_page_no(btr_cur_get_rec(&cursor2),
3312 offsets2) == right_page_no);
3313
3314 rec_info = rec_get_info_bits(btr_cur_get_rec(&father_cursor),
3315 rec_offs_comp(offsets));
3316 if (rec_info & REC_INFO_MIN_REC_FLAG) {
3317 /* When the father node ptr is minimal rec,
3318 we will keep it and delete the node ptr of
3319 merge page. */
3320 rtr_merge_and_update_mbr(&father_cursor, &cursor2, offsets, offsets2,
3321 merge_page, mtr);
3322 } else {
3323 /* Otherwise, we will keep the node ptr of
3324 merge page and delete the father node ptr.
3325 This is for keeping the rec order in upper
3326 level. */
3327 rtr_merge_and_update_mbr(&cursor2, &father_cursor, offsets2, offsets,
3328 merge_page, mtr);
3329 }
3330 locksys::Shard_latch_guard guard{UT_LOCATION_HERE, block->get_page_id()};
3331 lock_prdt_page_free_from_discard(block, lock_sys->prdt_page_hash);
3332 lock_rec_free_all_from_discard_page(block);
3333 } else {
3334 compressed =
3335 btr_cur_pessimistic_delete(&err, true, &cursor2, BTR_CREATE_FLAG,
3336 false, 0, 0, 0, mtr, nullptr, nullptr);
3337 ut_a(err == DB_SUCCESS);
3338
3339 if (!compressed) {
3340 btr_cur_compress_if_useful(&cursor2, false, mtr);
3341 }
3342
3343 if (!dict_table_is_locking_disabled(index->table)) {
3344 lock_update_merge_right(merge_block, orig_succ, block);
3345 }
3346 }
3347 }
3348
3349 if (!index->is_clustered() && !index->table->is_temporary() &&
3350 page_is_leaf(merge_page)) {
3351 /* Update the free bits of the B-tree page in the
3352 insert buffer bitmap. This has to be done in a
3353 separate mini-transaction that is committed before the
3354 main mini-transaction. We cannot update the insert
3355 buffer bitmap in this mini-transaction, because
3356 btr_compress() can be invoked recursively without
3357 committing the mini-transaction in between. Since
3358 insert buffer bitmap pages have a lower rank than
3359 B-tree pages, we must not access other pages in the
3360 same mini-transaction after accessing an insert buffer
3361 bitmap page. */
3362
3363 /* The free bits in the insert buffer bitmap must
3364 never exceed the free space on a page. It is safe to
3365 decrement or reset the bits in the bitmap in a
3366 mini-transaction that is committed before the
3367 mini-transaction that affects the free space. */
3368
3369 /* It is unsafe to increment the bits in a separately
3370 committed mini-transaction, because in crash recovery,
3371 the free bits could momentarily be set too high. */
3372
3373 if (page_size.is_compressed()) {
3374 /* Because the free bits may be incremented
3375 and we cannot update the insert buffer bitmap
3376 in the same mini-transaction, the only safe
3377 thing we can do here is the pessimistic
3378 approach: reset the free bits. */
3379 ibuf_reset_free_bits(merge_block);
3380 } else {
3381 /* On uncompressed pages, the free bits will
3382 never increase here. Thus, it is safe to
3383 write the bits accurately in a separate
3384 mini-transaction. */
3385 ibuf_update_free_bits_if_full(merge_block, UNIV_PAGE_SIZE,
3386 ULINT_UNDEFINED);
3387 }
3388 }
3389
3390 ut_ad(page_validate(merge_page, index));
3391 #ifdef UNIV_ZIP_DEBUG
3392 ut_a(!merge_page_zip || page_zip_validate(merge_page_zip, merge_page, index));
3393 #endif /* UNIV_ZIP_DEBUG */
3394
3395 if (dict_index_is_spatial(index)) {
3396 #ifdef UNIV_GIS_DEBUG
3397 fprintf(stderr, "GIS_DIAG: compressed away %ld\n",
3398 (long)block->page.id.page_no());
3399 fprintf(stderr, "GIS_DIAG: merged to %ld\n",
3400 (long)merge_block->page.id.page_no());
3401 #endif
3402
3403 rtr_check_discard_page(index, nullptr, block);
3404 }
3405
3406 /* Free the file page */
3407 btr_page_free(index, block, mtr);
3408
3409 /* btr_check_node_ptr() needs parent block latched.
3410 If the merge_block's parent block is not same,
3411 we cannot use btr_check_node_ptr() */
3412
3413 ut_ad(leftmost_child || btr_check_node_ptr(index, merge_block, mtr));
3414
3415 func_exit:
3416 mem_heap_free(heap);
3417
3418 if (adjust) {
3419 ut_ad(nth_rec > 0);
3420 btr_cur_position(index, page_rec_get_nth(merge_block->frame, nth_rec),
3421 merge_block, cursor);
3422 }
3423
3424 MONITOR_INC(MONITOR_INDEX_MERGE_SUCCESSFUL);
3425
3426 return true;
3427
3428 err_exit:
3429 /* We play it safe and reset the free bits. */
3430 if (page_size.is_compressed() && merge_page && page_is_leaf(merge_page) &&
3431 !index->is_clustered()) {
3432 ibuf_reset_free_bits(merge_block);
3433 }
3434
3435 mem_heap_free(heap);
3436 return false;
3437 }
3438
3439 /** Discards a page that is the only page on its level. This will empty
3440 the whole B-tree, leaving just an empty root page. This function
3441 should never be reached, because btr_compress(), which is invoked in
3442 delete operations, calls btr_lift_page_up() to flatten the B-tree. */
3443 static void btr_discard_only_page_on_level(
3444 dict_index_t *index, /*!< in: index tree */
3445 buf_block_t *block, /*!< in: page which is the only on its level */
3446 mtr_t *mtr) /*!< in: mtr */
3447 {
3448 ulint page_level = 0;
3449 trx_id_t max_trx_id;
3450
3451 /* Save the PAGE_MAX_TRX_ID from the leaf page. */
3452 max_trx_id = page_get_max_trx_id(buf_block_get_frame(block));
3453
3454 while (block->page.id.page_no() != dict_index_get_page(index)) {
3455 btr_cur_t cursor;
3456 buf_block_t *father;
3457 const page_t *page = buf_block_get_frame(block);
3458
3459 ut_a(page_get_n_recs(page) == 1);
3460 ut_a(page_level == btr_page_get_level(page));
3461 ut_a(btr_page_get_prev(page, mtr) == FIL_NULL);
3462 ut_a(btr_page_get_next(page, mtr) == FIL_NULL);
3463
3464 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
3465 btr_search_drop_page_hash_index(block);
3466
3467 if (dict_index_is_spatial(index)) {
3468 /* Check any concurrent search having this page */
3469 rtr_check_discard_page(index, nullptr, block);
3470 rtr_page_get_father(index, block, mtr, nullptr, &cursor);
3471 } else {
3472 btr_page_get_father(index, block, mtr, &cursor);
3473 }
3474 father = btr_cur_get_block(&cursor);
3475
3476 if (!dict_table_is_locking_disabled(index->table)) {
3477 lock_update_discard(father, PAGE_HEAP_NO_SUPREMUM, block);
3478 }
3479
3480 /* Free the file page */
3481 btr_page_free(index, block, mtr);
3482
3483 block = father;
3484 page_level++;
3485 }
3486
3487 /* block is the root page, which must be empty, except
3488 for the node pointer to the (now discarded) block(s). */
3489
3490 #ifdef UNIV_BTR_DEBUG
3491 if (!dict_index_is_ibuf(index)) {
3492 const page_t *root = buf_block_get_frame(block);
3493 const space_id_t space_id = dict_index_get_space(index);
3494 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_LEAF + root,
3495 space_id));
3496 ut_a(btr_root_fseg_validate(FIL_PAGE_DATA + PAGE_BTR_SEG_TOP + root,
3497 space_id));
3498 }
3499 #endif /* UNIV_BTR_DEBUG */
3500
3501 btr_page_empty(block, buf_block_get_page_zip(block), index, 0, mtr);
3502 ut_ad(page_is_leaf(buf_block_get_frame(block)));
3503
3504 if (!index->is_clustered() && !index->table->is_temporary()) {
3505 /* We play it safe and reset the free bits for the root */
3506 ibuf_reset_free_bits(block);
3507
3508 ut_a(max_trx_id);
3509 page_set_max_trx_id(block, buf_block_get_page_zip(block), max_trx_id, mtr);
3510 }
3511 }
3512
3513 /** Discards a page from a B-tree. This is used to remove the last record from
3514 a B-tree page: the whole page must be removed at the same time. This cannot
3515 be used for the root page, which is allowed to be empty. */
3516 void btr_discard_page(btr_cur_t *cursor, /*!< in: cursor on the page to discard:
3517 not on the root page */
3518 mtr_t *mtr) /*!< in: mtr */
3519 {
3520 dict_index_t *index;
3521 page_no_t left_page_no;
3522 page_no_t right_page_no;
3523 buf_block_t *merge_block;
3524 page_t *merge_page;
3525 buf_block_t *block;
3526 page_t *page;
3527 rec_t *node_ptr;
3528 #ifdef UNIV_DEBUG
3529 btr_cur_t parent_cursor;
3530 bool parent_is_different = false;
3531 #endif
3532
3533 block = btr_cur_get_block(cursor);
3534 index = cursor->index;
3535
3536 ut_ad(dict_index_get_page(index) != block->page.id.page_no());
3537
3538 ut_ad(mtr_memo_contains_flagged(mtr, dict_index_get_lock(index),
3539 MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
3540 index->table->is_intrinsic());
3541
3542 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
3543
3544 const space_id_t space = dict_index_get_space(index);
3545
3546 MONITOR_INC(MONITOR_INDEX_DISCARD);
3547
3548 #ifdef UNIV_DEBUG
3549 if (dict_index_is_spatial(index)) {
3550 rtr_page_get_father(index, block, mtr, cursor, &parent_cursor);
3551 } else {
3552 btr_page_get_father(index, block, mtr, &parent_cursor);
3553 }
3554 #endif
3555
3556 /* Decide the page which will inherit the locks */
3557
3558 left_page_no = btr_page_get_prev(buf_block_get_frame(block), mtr);
3559 right_page_no = btr_page_get_next(buf_block_get_frame(block), mtr);
3560
3561 const page_size_t page_size(dict_table_page_size(index->table));
3562
3563 if (left_page_no != FIL_NULL) {
3564 merge_block = btr_block_get(page_id_t(space, left_page_no), page_size,
3565 RW_X_LATCH, UT_LOCATION_HERE, index, mtr);
3566
3567 merge_page = buf_block_get_frame(merge_block);
3568 #ifdef UNIV_BTR_DEBUG
3569 ut_a(btr_page_get_next(merge_page, mtr) == block->page.id.page_no());
3570 #endif /* UNIV_BTR_DEBUG */
3571 ut_d(parent_is_different =
3572 (page_rec_get_next(page_get_infimum_rec(btr_cur_get_page(
3573 &parent_cursor))) == btr_cur_get_rec(&parent_cursor)));
3574 } else if (right_page_no != FIL_NULL) {
3575 merge_block = btr_block_get(page_id_t(space, right_page_no), page_size,
3576 RW_X_LATCH, UT_LOCATION_HERE, index, mtr);
3577
3578 merge_page = buf_block_get_frame(merge_block);
3579 #ifdef UNIV_BTR_DEBUG
3580 ut_a(btr_page_get_prev(merge_page, mtr) == block->page.id.page_no());
3581 #endif /* UNIV_BTR_DEBUG */
3582 ut_d(parent_is_different = page_rec_is_supremum(
3583 page_rec_get_next(btr_cur_get_rec(&parent_cursor))));
3584 } else {
3585 btr_discard_only_page_on_level(index, block, mtr);
3586
3587 return;
3588 }
3589
3590 page = buf_block_get_frame(block);
3591 ut_a(page_is_comp(merge_page) == page_is_comp(page));
3592 btr_search_drop_page_hash_index(block);
3593
3594 if (left_page_no == FIL_NULL && !page_is_leaf(page)) {
3595 /* We have to mark the leftmost node pointer on the right
3596 side page as the predefined minimum record */
3597 node_ptr = page_rec_get_next(page_get_infimum_rec(merge_page));
3598
3599 ut_ad(page_rec_is_user_rec(node_ptr));
3600
3601 /* This will make page_zip_validate() fail on merge_page
3602 until btr_level_list_remove() completes. This is harmless,
3603 because everything will take place within a single
3604 mini-transaction and because writing to the redo log
3605 is an atomic operation (performed by mtr_commit()). */
3606 btr_set_min_rec_mark(node_ptr, mtr);
3607 }
3608
3609 if (dict_index_is_spatial(index)) {
3610 btr_cur_t father_cursor;
3611
3612 /* Since rtr_node_ptr_delete doesn't contain get father
3613 node ptr, so, we need to get father node ptr first and then
3614 delete it. */
3615 rtr_page_get_father(index, block, mtr, cursor, &father_cursor);
3616 rtr_node_ptr_delete(&father_cursor, mtr);
3617 } else {
3618 btr_node_ptr_delete(index, block, mtr);
3619 }
3620
3621 /* Remove the page from the level list */
3622 btr_level_list_remove(space, page_size, page, index, mtr);
3623 #ifdef UNIV_ZIP_DEBUG
3624 {
3625 page_zip_des_t *merge_page_zip = buf_block_get_page_zip(merge_block);
3626 ut_a(!merge_page_zip ||
3627 page_zip_validate(merge_page_zip, merge_page, index));
3628 }
3629 #endif /* UNIV_ZIP_DEBUG */
3630
3631 if (!dict_table_is_locking_disabled(index->table)) {
3632 if (left_page_no != FIL_NULL) {
3633 lock_update_discard(merge_block, PAGE_HEAP_NO_SUPREMUM, block);
3634 } else {
3635 lock_update_discard(merge_block, lock_get_min_heap_no(merge_block),
3636 block);
3637 }
3638 }
3639
3640 if (dict_index_is_spatial(index)) {
3641 rtr_check_discard_page(index, cursor, block);
3642 }
3643
3644 /* Free the file page */
3645 btr_page_free(index, block, mtr);
3646
3647 /* btr_check_node_ptr() needs parent block latched.
3648 If the merge_block's parent block is not same,
3649 we cannot use btr_check_node_ptr() */
3650
3651 ut_ad(parent_is_different || btr_check_node_ptr(index, merge_block, mtr));
3652 }
3653
3654 #ifdef UNIV_BTR_PRINT
3655 /** Prints size info of a B-tree. */
3656 void btr_print_size(dict_index_t *index) /*!< in: index tree */
3657 {
3658 page_t *root;
3659 fseg_header_t *seg;
3660 mtr_t mtr;
3661
3662 if (dict_index_is_ibuf(index)) {
3663 fputs(
3664 "Sorry, cannot print info of an ibuf tree:"
3665 " use ibuf functions\n",
3666 stderr);
3667
3668 return;
3669 }
3670
3671 mtr_start(&mtr);
3672
3673 root = btr_root_get(index, &mtr);
3674
3675 seg = root + PAGE_HEADER + PAGE_BTR_SEG_TOP;
3676
3677 fputs("INFO OF THE NON-LEAF PAGE SEGMENT\n", stderr);
3678 fseg_print(seg, &mtr);
3679
3680 if (!dict_index_is_ibuf(index)) {
3681 seg = root + PAGE_HEADER + PAGE_BTR_SEG_LEAF;
3682
3683 fputs("INFO OF THE LEAF PAGE SEGMENT\n", stderr);
3684 fseg_print(seg, &mtr);
3685 }
3686
3687 mtr_commit(&mtr);
3688 }
3689
3690 /** Prints recursively index tree pages. */
3691 static void btr_print_recursive(
3692 dict_index_t *index, /*!< in: index tree */
3693 buf_block_t *block, /*!< in: index page */
3694 ulint width, /*!< in: print this many entries from start
3695 and end */
3696 mem_heap_t **heap, /*!< in/out: heap for rec_get_offsets() */
3697 ulint **offsets, /*!< in/out: buffer for rec_get_offsets() */
3698 mtr_t *mtr) /*!< in: mtr */
3699 {
3700 const page_t *page = buf_block_get_frame(block);
3701 page_cur_t cursor;
3702 ulint n_recs;
3703 ulint i = 0;
3704 mtr_t mtr2;
3705
3706 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_SX_FIX, index->table));
3707
3708 ib::info(ER_IB_MSG_34) << "NODE ON LEVEL " << btr_page_get_level(page)
3709 << " page " << block->page.id;
3710
3711 page_print(block, index, width, width);
3712
3713 n_recs = page_get_n_recs(page);
3714
3715 page_cur_set_before_first(block, &cursor);
3716 page_cur_move_to_next(&cursor);
3717
3718 while (!page_cur_is_after_last(&cursor)) {
3719 if (page_is_leaf(page)) {
3720 /* If this is the leaf level, do nothing */
3721
3722 } else if ((i <= width) || (i >= n_recs - width)) {
3723 const rec_t *node_ptr;
3724
3725 mtr_start(&mtr2);
3726
3727 node_ptr = page_cur_get_rec(&cursor);
3728
3729 *offsets =
3730 rec_get_offsets(node_ptr, index, *offsets, ULINT_UNDEFINED, heap);
3731 btr_print_recursive(
3732 index, btr_node_ptr_get_child(node_ptr, index, *offsets, &mtr2),
3733 width, heap, offsets, &mtr2);
3734 mtr_commit(&mtr2);
3735 }
3736
3737 page_cur_move_to_next(&cursor);
3738 i++;
3739 }
3740 }
3741
3742 /** Prints directories and other info of all nodes in the tree. */
3743 void btr_print_index(dict_index_t *index, /*!< in: index */
3744 ulint width) /*!< in: print this many entries from start
3745 and end */
3746 {
3747 mtr_t mtr;
3748 buf_block_t *root;
3749 mem_heap_t *heap = NULL;
3750 ulint offsets_[REC_OFFS_NORMAL_SIZE];
3751 ulint *offsets = offsets_;
3752 rec_offs_init(offsets_);
3753
3754 fputs(
3755 "--------------------------\n"
3756 "INDEX TREE PRINT\n",
3757 stderr);
3758
3759 mtr_start(&mtr);
3760
3761 root = btr_root_block_get(index, RW_SX_LATCH, &mtr);
3762
3763 btr_print_recursive(index, root, width, &heap, &offsets, &mtr);
3764 if (heap) {
3765 mem_heap_free(heap);
3766 }
3767
3768 mtr_commit(&mtr);
3769
3770 ut_ad(btr_validate_index(index, 0, false));
3771 }
3772 #endif /* UNIV_BTR_PRINT */
3773
3774 #ifdef UNIV_DEBUG
3775 bool btr_check_node_ptr(dict_index_t *index, buf_block_t *block, mtr_t *mtr) {
3776 mem_heap_t *heap;
3777 dtuple_t *tuple;
3778 ulint *offsets;
3779 btr_cur_t cursor;
3780 page_t *page = buf_block_get_frame(block);
3781
3782 ut_ad(mtr_is_block_fix(mtr, block, MTR_MEMO_PAGE_X_FIX, index->table));
3783
3784 if (dict_index_get_page(index) == block->page.id.page_no()) {
3785 return true;
3786 }
3787
3788 heap = mem_heap_create(256, UT_LOCATION_HERE);
3789
3790 if (dict_index_is_spatial(index)) {
3791 offsets = rtr_page_get_father_block(nullptr, heap, index, block, mtr,
3792 nullptr, &cursor);
3793 } else {
3794 offsets =
3795 btr_page_get_father_block(nullptr, heap, index, block, mtr, &cursor);
3796 }
3797
3798 if (page_is_leaf(page)) {
3799 goto func_exit;
3800 }
3801
3802 tuple = dict_index_build_node_ptr(
3803 index, page_rec_get_next(page_get_infimum_rec(page)), 0, heap,
3804 btr_page_get_level(page));
3805
3806 /* For spatial index, the MBR in the parent rec could be different
3807 with that of first rec of child, their relationship should be
3808 "WITHIN" relationship */
3809 if (dict_index_is_spatial(index)) {
3810 ut_a(!cmp_dtuple_rec_with_gis(tuple, btr_cur_get_rec(&cursor), offsets,
3811 PAGE_CUR_WITHIN, index->rtr_srs.get()));
3812 } else {
3813 ut_a(!cmp_dtuple_rec(tuple, btr_cur_get_rec(&cursor), index, offsets));
3814 }
3815 func_exit:
3816 mem_heap_free(heap);
3817 return true;
3818 }
3819 #endif /* UNIV_DEBUG */
3820
3821 /** Display identification information for a record. */
3822 static void btr_index_rec_validate_report(
3823 const page_t *page, /*!< in: index page */
3824 const rec_t *rec, /*!< in: index record */
3825 const dict_index_t *index) /*!< in: index */
3826 {
3827 ib::info(ER_IB_MSG_35) << "Record in index " << index->name << " of table "
3828 << index->table->name << ", page "
3829 << page_id_t(page_get_space_id(page),
3830 page_get_page_no(page))
3831 << ", at offset " << page_offset(rec);
3832 }
3833
3834 /** Checks the size and number of fields in a record based on the definition of
3835 the index.
3836 @return true if ok */
3837 bool btr_index_rec_validate(const rec_t *rec, /*!< in: index record */
3838 const dict_index_t *index, /*!< in: index */
3839 bool dump_on_error) /*!< in: true if the function
3840 should print hex dump of
3841 record and page on error */
3842 {
3843 ulint len;
3844 ulint n;
3845 ulint i;
3846 const page_t *page;
3847 mem_heap_t *heap = nullptr;
3848 ulint offsets_[REC_OFFS_NORMAL_SIZE];
3849 ulint *offsets = offsets_;
3850 rec_offs_init(offsets_);
3851
3852 page = page_align(rec);
3853
3854 if (dict_index_is_ibuf(index)) {
3855 /* The insert buffer index tree can contain records from any
3856 other index: we cannot check the number of fields or
3857 their length */
3858
3859 return true;
3860 }
3861
3862 #ifdef VIRTUAL_INDEX_DEBUG
3863 if (dict_index_has_virtual(index) || index->is_clustered()) {
3864 fprintf(stderr, "index name is %s\n", index->name());
3865 }
3866 #endif
3867 if (page_is_comp(page) != dict_table_is_comp(index->table)) {
3868 btr_index_rec_validate_report(page, rec, index);
3869
3870 ib::error(ER_IB_MSG_36)
3871 << "Compact flag=" << page_is_comp(page) << ", should be "
3872 << dict_table_is_comp(index->table);
3873
3874 return false;
3875 }
3876
3877 n = dict_index_get_n_fields(index);
3878
3879 if (!page_is_comp(page) &&
3880 (rec_get_n_fields_old(rec, index) != n
3881 /* a record for older SYS_INDEXES table
3882 (missing merge_threshold column) is acceptable. */
3883 && !(index->id == DICT_INDEXES_ID &&
3884 rec_get_n_fields_old(rec, index) == n - 1))) {
3885 btr_index_rec_validate_report(page, rec, index);
3886
3887 ib::error(ER_IB_MSG_37) << "Has " << rec_get_n_fields_old(rec, index)
3888 << " fields, should have " << n;
3889
3890 if (dump_on_error) {
3891 fputs("InnoDB: corrupt record ", stderr);
3892 rec_print_old(stderr, rec);
3893 putc('\n', stderr);
3894 }
3895 return false;
3896 }
3897
3898 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
3899 UT_LOCATION_HERE, &heap);
3900
3901 for (i = 0; i < n; i++) {
3902 dict_field_t *field = nullptr;
3903 field = index->get_physical_field(i);
3904 const dict_col_t *col = field->col;
3905 ulint fixed_size = col->get_fixed_size(page_is_comp(page));
3906
3907 /* nullptr for index as i is physical here */
3908 rec_get_nth_field_offs(nullptr, offsets, i, &len);
3909
3910 /* Note that if fixed_size != 0, it equals the
3911 length of a fixed-size column in the clustered index,
3912 except the DATA_POINT, whose length would be MBR_LEN
3913 when it's indexed in a R-TREE. We should adjust it here.
3914 A prefix index of the column is of fixed, but different
3915 length. When fixed_size == 0, prefix_len is the maximum
3916 length of the prefix index column. */
3917
3918 if (col->mtype == DATA_POINT) {
3919 ut_ad(fixed_size == DATA_POINT_LEN);
3920 if (dict_index_is_spatial(index)) {
3921 /* For DATA_POINT data, when it has R-tree
3922 index, the fixed_len is the MBR of the point.
3923 But if it's a primary key and on R-TREE
3924 as the PK pointer, the length shall be
3925 DATA_POINT_LEN as well. */
3926 ut_ad((field->fixed_len == DATA_MBR_LEN && i == 0) ||
3927 (field->fixed_len == DATA_POINT_LEN && i != 0));
3928 fixed_size = field->fixed_len;
3929 }
3930 }
3931
3932 if ((field->prefix_len == 0 && rec_field_not_null_not_add_col_def(len) &&
3933 fixed_size && len != fixed_size) ||
3934 (field->prefix_len > 0 && rec_field_not_null_not_add_col_def(len) &&
3935 len > field->prefix_len)) {
3936 btr_index_rec_validate_report(page, rec, index);
3937
3938 ib::error error(ER_IB_MSG_1224);
3939
3940 error << "Field " << i << " len is " << len << ", should be "
3941 << fixed_size;
3942
3943 if (dump_on_error) {
3944 error << "; ";
3945 rec_print(error.m_oss, rec,
3946 rec_get_info_bits(rec, rec_offs_comp(offsets)), offsets);
3947 }
3948 if (heap) {
3949 mem_heap_free(heap);
3950 }
3951 return false;
3952 }
3953 }
3954
3955 #ifdef VIRTUAL_INDEX_DEBUG
3956 if (dict_index_has_virtual(index) || index->is_clustered()) {
3957 rec_print_new(stderr, rec, offsets);
3958 }
3959 #endif
3960
3961 if (heap) {
3962 mem_heap_free(heap);
3963 }
3964 return true;
3965 }
3966
3967 /** Checks the size and number of fields in records based on the definition of
3968 the index.
3969 @return true if ok */
3970 static bool btr_index_page_validate(buf_block_t *block, /*!< in: index page */
3971 dict_index_t *index) /*!< in: index */
3972 {
3973 page_cur_t cur;
3974 bool ret = true;
3975 #ifdef UNIV_DEBUG
3976 ulint nth = 1;
3977 #endif /* UNIV_DEBUG */
3978
3979 page_cur_set_before_first(block, &cur);
3980
3981 /* Directory slot 0 should only contain the infimum record. */
3982 DBUG_EXECUTE_IF(
3983 "check_table_rec_next",
3984 ut_a(page_rec_get_nth_const(page_cur_get_page(&cur), 0) == cur.rec);
3985 ut_a(page_dir_slot_get_n_owned(
3986 page_dir_get_nth_slot(page_cur_get_page(&cur), 0)) == 1););
3987
3988 page_cur_move_to_next(&cur);
3989
3990 for (;;) {
3991 if (page_cur_is_after_last(&cur)) {
3992 break;
3993 }
3994
3995 if (!btr_index_rec_validate(cur.rec, index, true)) {
3996 return false;
3997 }
3998
3999 /* Verify that page_rec_get_nth_const() is correctly
4000 retrieving each record. */
4001 DBUG_EXECUTE_IF("check_table_rec_next",
4002 ut_a(cur.rec == page_rec_get_nth_const(
4003 page_cur_get_page(&cur),
4004 page_rec_get_n_recs_before(cur.rec)));
4005 ut_a(nth++ == page_rec_get_n_recs_before(cur.rec)););
4006
4007 page_cur_move_to_next(&cur);
4008 }
4009
4010 return (ret);
4011 }
4012
4013 /** Report an error on one page of an index tree. */
4014 static void btr_validate_report1(
4015 dict_index_t *index, /*!< in: index */
4016 ulint level, /*!< in: B-tree level */
4017 const buf_block_t *block) /*!< in: index page */
4018 {
4019 ib::error error(ER_IB_MSG_1225);
4020 error << "In page " << block->page.id.page_no() << " of index " << index->name
4021 << " of table " << index->table->name;
4022
4023 if (level > 0) {
4024 error << ", index tree level " << level;
4025 }
4026 }
4027
4028 /** Report an error on two pages of an index tree. */
4029 static void btr_validate_report2(
4030 const dict_index_t *index, /*!< in: index */
4031 ulint level, /*!< in: B-tree level */
4032 const buf_block_t *block1, /*!< in: first index page */
4033 const buf_block_t *block2) /*!< in: second index page */
4034 {
4035 ib::error error(ER_IB_MSG_1226);
4036 error << "In pages " << block1->page.id << " and " << block2->page.id
4037 << " of index " << index->name << " of table " << index->table->name;
4038
4039 if (level > 0) {
4040 error << ", index tree level " << level;
4041 }
4042 }
4043
4044 /** Validates index tree level.
4045 @return true if ok */
4046 static bool btr_validate_level(
4047 dict_index_t *index, /*!< in: index tree */
4048 const trx_t *trx, /*!< in: transaction or NULL */
4049 ulint level, /*!< in: level number */
4050 bool lockout) /*!< in: true if X-latch index is intended */
4051 {
4052 buf_block_t *block;
4053 page_t *page;
4054 buf_block_t *right_block = nullptr; /* remove warning */
4055 page_t *right_page = nullptr; /* remove warning */
4056 page_t *father_page;
4057 btr_cur_t node_cur;
4058 btr_cur_t right_node_cur;
4059 rec_t *rec;
4060 page_no_t right_page_no;
4061 page_no_t left_page_no;
4062 page_cur_t cursor;
4063 dtuple_t *node_ptr_tuple;
4064 bool ret = true;
4065 mtr_t mtr;
4066 mem_heap_t *heap = mem_heap_create(256, UT_LOCATION_HERE);
4067 fseg_header_t *seg;
4068 ulint *offsets = nullptr;
4069 ulint *offsets2 = nullptr;
4070 #ifdef UNIV_ZIP_DEBUG
4071 page_zip_des_t *page_zip;
4072 #endif /* UNIV_ZIP_DEBUG */
4073 ulint savepoint = 0;
4074 ulint savepoint2 = 0;
4075 page_no_t parent_page_no = FIL_NULL;
4076 page_no_t parent_right_page_no = FIL_NULL;
4077 bool rightmost_child = false;
4078
4079 mtr_start(&mtr);
4080
4081 if (!srv_read_only_mode) {
4082 if (lockout) {
4083 mtr_x_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
4084 } else {
4085 mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
4086 }
4087 }
4088
4089 block = btr_root_block_get(index, RW_SX_LATCH, &mtr);
4090 page = buf_block_get_frame(block);
4091 seg = page + PAGE_HEADER + PAGE_BTR_SEG_TOP;
4092
4093 #ifdef UNIV_RTR_DEBUG
4094 if (dict_index_is_spatial(index)) {
4095 fprintf(stderr, "Root page no: %lu\n", (ulong)page_get_page_no(page));
4096 }
4097 #endif
4098
4099 const fil_space_t *space = fil_space_get(index->space);
4100 const page_size_t table_page_size(dict_table_page_size(index->table));
4101 const page_size_t space_page_size(space->flags);
4102
4103 if (!table_page_size.equals_to(space_page_size)) {
4104 ib::warn(ER_IB_MSG_38) << "Flags mismatch: table=" << index->table->flags
4105 << ", tablespace=" << space->flags;
4106
4107 mtr_commit(&mtr);
4108
4109 return (false);
4110 }
4111
4112 while (level != btr_page_get_level(page)) {
4113 const rec_t *node_ptr;
4114
4115 if (fseg_page_is_free(seg, block->page.id.space(),
4116 block->page.id.page_no())) {
4117 btr_validate_report1(index, level, block);
4118
4119 ib::warn(ER_IB_MSG_39) << "Page is free";
4120
4121 ret = false;
4122 }
4123
4124 ut_a(index->space == block->page.id.space());
4125 ut_a(index->space == page_get_space_id(page));
4126 #ifdef UNIV_ZIP_DEBUG
4127 page_zip = buf_block_get_page_zip(block);
4128 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
4129 #endif /* UNIV_ZIP_DEBUG */
4130 ut_a(!page_is_leaf(page));
4131
4132 page_cur_set_before_first(block, &cursor);
4133 page_cur_move_to_next(&cursor);
4134
4135 node_ptr = page_cur_get_rec(&cursor);
4136 offsets = rec_get_offsets(node_ptr, index, offsets, ULINT_UNDEFINED,
4137 UT_LOCATION_HERE, &heap);
4138
4139 savepoint2 = mtr_set_savepoint(&mtr);
4140 block = btr_node_ptr_get_child(node_ptr, index, offsets, &mtr);
4141 page = buf_block_get_frame(block);
4142
4143 /* For R-Tree, since record order might not be the same as
4144 linked index page in the lower level, we need to traverse
4145 backwards to get the first page rec in this level.
4146 This is only used for index validation. Spatial index
4147 does not use such scan for any of its DML or query
4148 operations */
4149 if (dict_index_is_spatial(index)) {
4150 left_page_no = btr_page_get_prev(page, &mtr);
4151
4152 while (left_page_no != FIL_NULL) {
4153 page_id_t left_page_id(index->space, left_page_no);
4154 /* To obey latch order of tree blocks,
4155 we should release the right_block once to
4156 obtain lock of the uncle block. */
4157 mtr_release_block_at_savepoint(&mtr, savepoint2, block);
4158
4159 savepoint2 = mtr_set_savepoint(&mtr);
4160 block = btr_block_get(left_page_id, table_page_size, RW_SX_LATCH,
4161 UT_LOCATION_HERE, index, &mtr);
4162 page = buf_block_get_frame(block);
4163 left_page_no = btr_page_get_prev(page, &mtr);
4164 }
4165 }
4166 }
4167
4168 /* Now we are on the desired level. Loop through the pages on that
4169 level. */
4170
4171 if (level == 0) {
4172 /* Leaf pages are managed in their own file segment. */
4173 seg -= PAGE_BTR_SEG_TOP - PAGE_BTR_SEG_LEAF;
4174 }
4175
4176 loop:
4177 mem_heap_empty(heap);
4178 offsets = offsets2 = nullptr;
4179 if (!srv_read_only_mode) {
4180 if (lockout) {
4181 mtr_x_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
4182 } else {
4183 mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
4184 }
4185 }
4186
4187 #ifdef UNIV_ZIP_DEBUG
4188 page_zip = buf_block_get_page_zip(block);
4189 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
4190 #endif /* UNIV_ZIP_DEBUG */
4191
4192 ut_a(block->page.id.space() == index->space);
4193
4194 if (fseg_page_is_free(seg, block->page.id.space(),
4195 block->page.id.page_no())) {
4196 btr_validate_report1(index, level, block);
4197
4198 ib::warn(ER_IB_MSG_40) << "Page is marked as free";
4199 ret = false;
4200
4201 } else if (btr_page_get_index_id(page) != index->id) {
4202 ib::error(ER_IB_MSG_41) << "Page index id " << btr_page_get_index_id(page)
4203 << " != data dictionary index id " << index->id;
4204
4205 ret = false;
4206
4207 } else if (!page_validate(page, index)) {
4208 btr_validate_report1(index, level, block);
4209 ret = false;
4210
4211 } else if (level == 0 && !btr_index_page_validate(block, index)) {
4212 /* We are on level 0. Check that the records have the right
4213 number of fields, and field lengths are right. */
4214
4215 ret = false;
4216 }
4217
4218 ut_a(btr_page_get_level(page) == level);
4219
4220 right_page_no = btr_page_get_next(page, &mtr);
4221 left_page_no = btr_page_get_prev(page, &mtr);
4222
4223 ut_a(!page_is_empty(page) ||
4224 (level == 0 && page_get_page_no(page) == dict_index_get_page(index)));
4225
4226 if (right_page_no != FIL_NULL) {
4227 const rec_t *right_rec;
4228 savepoint = mtr_set_savepoint(&mtr);
4229
4230 right_block =
4231 btr_block_get(page_id_t(index->space, right_page_no), table_page_size,
4232 RW_SX_LATCH, UT_LOCATION_HERE, index, &mtr);
4233
4234 right_page = buf_block_get_frame(right_block);
4235
4236 if (btr_page_get_prev(right_page, &mtr) != page_get_page_no(page)) {
4237 btr_validate_report2(index, level, block, right_block);
4238 fputs(
4239 "InnoDB: broken FIL_PAGE_NEXT"
4240 " or FIL_PAGE_PREV links\n",
4241 stderr);
4242
4243 ret = false;
4244 }
4245
4246 if (page_is_comp(right_page) != page_is_comp(page)) {
4247 btr_validate_report2(index, level, block, right_block);
4248 fputs("InnoDB: 'compact' flag mismatch\n", stderr);
4249
4250 ret = false;
4251
4252 goto node_ptr_fails;
4253 }
4254
4255 rec = page_rec_get_prev(page_get_supremum_rec(page));
4256 right_rec = page_rec_get_next(page_get_infimum_rec(right_page));
4257 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
4258 UT_LOCATION_HERE, &heap);
4259 offsets2 = rec_get_offsets(right_rec, index, offsets2, ULINT_UNDEFINED,
4260 UT_LOCATION_HERE, &heap);
4261
4262 /* For spatial index, we cannot guarantee the key ordering
4263 across pages, so skip the record compare verification for
4264 now. Will enhanced in special R-Tree index validation scheme */
4265 if (!dict_index_is_spatial(index) &&
4266 cmp_rec_rec(rec, right_rec, offsets, offsets2, index, false) >= 0) {
4267 btr_validate_report2(index, level, block, right_block);
4268
4269 fputs(
4270 "InnoDB: records in wrong order"
4271 " on adjacent pages\n",
4272 stderr);
4273
4274 fputs("InnoDB: record ", stderr);
4275 rec = page_rec_get_prev(page_get_supremum_rec(page));
4276 rec_print(stderr, rec, index);
4277 putc('\n', stderr);
4278 fputs("InnoDB: record ", stderr);
4279 rec = page_rec_get_next(page_get_infimum_rec(right_page));
4280 rec_print(stderr, rec, index);
4281 putc('\n', stderr);
4282
4283 ret = false;
4284 }
4285 }
4286
4287 if (level > 0 && left_page_no == FIL_NULL) {
4288 ut_a(REC_INFO_MIN_REC_FLAG &
4289 rec_get_info_bits(page_rec_get_next(page_get_infimum_rec(page)),
4290 page_is_comp(page)));
4291 }
4292
4293 /* Similarly skip the father node check for spatial index for now,
4294 for a couple of reasons:
4295 1) As mentioned, there is no ordering relationship between records
4296 in parent level and linked pages in the child level.
4297 2) Search parent from root is very costly for R-tree.
4298 We will add special validation mechanism for R-tree later (WL #7520) */
4299 if (!dict_index_is_spatial(index) &&
4300 block->page.id.page_no() != dict_index_get_page(index)) {
4301 /* Check father node pointers */
4302 rec_t *node_ptr;
4303
4304 btr_cur_position(index, page_rec_get_next(page_get_infimum_rec(page)),
4305 block, &node_cur);
4306 offsets = btr_page_get_father_node_ptr_for_validate(
4307 offsets, heap, &node_cur, UT_LOCATION_HERE, &mtr);
4308
4309 father_page = btr_cur_get_page(&node_cur);
4310 node_ptr = btr_cur_get_rec(&node_cur);
4311
4312 parent_page_no = page_get_page_no(father_page);
4313 parent_right_page_no = btr_page_get_next(father_page, &mtr);
4314 rightmost_child = page_rec_is_supremum(page_rec_get_next(node_ptr));
4315
4316 btr_cur_position(index, page_rec_get_prev(page_get_supremum_rec(page)),
4317 block, &node_cur);
4318
4319 offsets = btr_page_get_father_node_ptr_for_validate(
4320 offsets, heap, &node_cur, UT_LOCATION_HERE, &mtr);
4321
4322 if (node_ptr != btr_cur_get_rec(&node_cur) ||
4323 btr_node_ptr_get_child_page_no(node_ptr, offsets) !=
4324 block->page.id.page_no()) {
4325 btr_validate_report1(index, level, block);
4326
4327 fputs("InnoDB: node pointer to the page is wrong\n", stderr);
4328
4329 fputs("InnoDB: node ptr ", stderr);
4330 rec_print(stderr, node_ptr, index);
4331
4332 rec = btr_cur_get_rec(&node_cur);
4333 fprintf(stderr,
4334 "\n"
4335 "InnoDB: node ptr child page n:o %lu\n",
4336 (ulong)btr_node_ptr_get_child_page_no(rec, offsets));
4337
4338 fputs("InnoDB: record on page ", stderr);
4339 rec_print_new(stderr, rec, offsets);
4340 putc('\n', stderr);
4341 ret = false;
4342
4343 goto node_ptr_fails;
4344 }
4345
4346 if (!page_is_leaf(page)) {
4347 node_ptr_tuple = dict_index_build_node_ptr(
4348 index, page_rec_get_next(page_get_infimum_rec(page)), 0, heap,
4349 btr_page_get_level(page));
4350
4351 if (cmp_dtuple_rec(node_ptr_tuple, node_ptr, index, offsets)) {
4352 const rec_t *first_rec = page_rec_get_next(page_get_infimum_rec(page));
4353
4354 btr_validate_report1(index, level, block);
4355
4356 ib::error(ER_IB_MSG_42) << "Node ptrs differ on levels > 0";
4357
4358 fputs("InnoDB: node ptr ", stderr);
4359 rec_print_new(stderr, node_ptr, offsets);
4360 fputs("InnoDB: first rec ", stderr);
4361 rec_print(stderr, first_rec, index);
4362 putc('\n', stderr);
4363 ret = false;
4364
4365 goto node_ptr_fails;
4366 }
4367 }
4368
4369 if (left_page_no == FIL_NULL) {
4370 ut_a(node_ptr == page_rec_get_next(page_get_infimum_rec(father_page)));
4371 ut_a(btr_page_get_prev(father_page, &mtr) == FIL_NULL);
4372 }
4373
4374 if (right_page_no == FIL_NULL) {
4375 ut_a(node_ptr == page_rec_get_prev(page_get_supremum_rec(father_page)));
4376 ut_a(btr_page_get_next(father_page, &mtr) == FIL_NULL);
4377 } else {
4378 const rec_t *right_node_ptr;
4379
4380 right_node_ptr = page_rec_get_next(node_ptr);
4381
4382 if (!lockout && rightmost_child) {
4383 /* To obey latch order of tree blocks,
4384 we should release the right_block once to
4385 obtain lock of the uncle block. */
4386 mtr_release_block_at_savepoint(&mtr, savepoint, right_block);
4387
4388 btr_block_get(page_id_t(index->space, parent_right_page_no),
4389 table_page_size, RW_SX_LATCH, UT_LOCATION_HERE, index,
4390 &mtr);
4391
4392 right_block = btr_block_get(page_id_t(index->space, right_page_no),
4393 table_page_size, RW_SX_LATCH,
4394 UT_LOCATION_HERE, index, &mtr);
4395 }
4396
4397 btr_cur_position(index,
4398 page_rec_get_next(page_get_infimum_rec(
4399 buf_block_get_frame(right_block))),
4400 right_block, &right_node_cur);
4401
4402 offsets = btr_page_get_father_node_ptr_for_validate(
4403 offsets, heap, &right_node_cur, UT_LOCATION_HERE, &mtr);
4404
4405 if (right_node_ptr != page_get_supremum_rec(father_page)) {
4406 if (btr_cur_get_rec(&right_node_cur) != right_node_ptr) {
4407 ret = false;
4408 fputs(
4409 "InnoDB: node pointer to"
4410 " the right page is wrong\n",
4411 stderr);
4412
4413 btr_validate_report1(index, level, block);
4414 }
4415 } else {
4416 page_t *right_father_page = btr_cur_get_page(&right_node_cur);
4417
4418 if (btr_cur_get_rec(&right_node_cur) !=
4419 page_rec_get_next(page_get_infimum_rec(right_father_page))) {
4420 ret = false;
4421 fputs(
4422 "InnoDB: node pointer 2 to"
4423 " the right page is wrong\n",
4424 stderr);
4425
4426 btr_validate_report1(index, level, block);
4427 }
4428
4429 if (page_get_page_no(right_father_page) !=
4430 btr_page_get_next(father_page, &mtr)) {
4431 ret = false;
4432 fputs(
4433 "InnoDB: node pointer 3 to"
4434 " the right page is wrong\n",
4435 stderr);
4436
4437 btr_validate_report1(index, level, block);
4438 }
4439 }
4440 }
4441 }
4442
4443 node_ptr_fails:
4444 /* Commit the mini-transaction to release the latch on 'page'.
4445 Re-acquire the latch on right_page, which will become 'page'
4446 on the next loop. The page has already been checked. */
4447 mtr_commit(&mtr);
4448
4449 if (trx_is_interrupted(trx)) {
4450 /* On interrupt, return the current status. */
4451 } else if (right_page_no != FIL_NULL) {
4452 mtr_start(&mtr);
4453
4454 if (!lockout) {
4455 if (rightmost_child) {
4456 if (parent_right_page_no != FIL_NULL) {
4457 btr_block_get(page_id_t(index->space, parent_right_page_no),
4458 table_page_size, RW_SX_LATCH, UT_LOCATION_HERE, index,
4459 &mtr);
4460 }
4461 } else if (parent_page_no != FIL_NULL) {
4462 btr_block_get(page_id_t(index->space, parent_page_no), table_page_size,
4463 RW_SX_LATCH, UT_LOCATION_HERE, index, &mtr);
4464 }
4465 }
4466
4467 block =
4468 btr_block_get(page_id_t(index->space, right_page_no), table_page_size,
4469 RW_SX_LATCH, UT_LOCATION_HERE, index, &mtr);
4470
4471 page = buf_block_get_frame(block);
4472
4473 goto loop;
4474 }
4475
4476 mem_heap_free(heap);
4477
4478 return (ret);
4479 }
4480
4481 /** Do an index level validation of spaital index tree.
4482 @return true if no error found */
4483 static bool btr_validate_spatial_index(
4484 dict_index_t *index, /*!< in: index */
4485 const trx_t *trx) /*!< in: transaction or NULL */
4486 {
4487 mtr_t mtr;
4488 bool ok = true;
4489
4490 mtr_start(&mtr);
4491
4492 mtr_x_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
4493
4494 page_t *root = btr_root_get(index, &mtr);
4495 ulint n = btr_page_get_level(root);
4496
4497 #ifdef UNIV_RTR_DEBUG
4498 fprintf(stderr, "R-tree level is %lu\n", n);
4499 #endif /* UNIV_RTR_DEBUG */
4500
4501 for (ulint i = 0; i <= n; ++i) {
4502 #ifdef UNIV_RTR_DEBUG
4503 fprintf(stderr, "Level %lu:\n", n - i);
4504 #endif /* UNIV_RTR_DEBUG */
4505
4506 if (!btr_validate_level(index, trx, n - i, true)) {
4507 ok = false;
4508 break;
4509 }
4510 }
4511
4512 mtr_commit(&mtr);
4513
4514 return (ok);
4515 }
4516
4517 /** Checks the consistency of an index tree.
4518 @return true if ok */
4519 bool btr_validate_index(
4520 dict_index_t *index, /*!< in: index */
4521 const trx_t *trx, /*!< in: transaction or NULL */
4522 bool lockout) /*!< in: true if X-latch index is intended */
4523 {
4524 /* Full Text index are implemented by auxiliary tables,
4525 not the B-tree */
4526 if (dict_index_is_online_ddl(index) || (index->type & DICT_FTS)) {
4527 return (true);
4528 }
4529
4530 if (dict_index_is_spatial(index)) {
4531 return (btr_validate_spatial_index(index, trx));
4532 }
4533
4534 mtr_t mtr;
4535
4536 #ifdef UNIV_DEBUG
4537 /* Check the FSEG_NOT_FULL_N_USED field stored in the segment inode. */
4538 {
4539 const space_id_t space_id = dict_index_get_space(index);
4540
4541 fil_space_t *space = fil_space_acquire(space_id);
4542
4543 if (space == nullptr) {
4544 return true;
4545 }
4546
4547 mtr_start(&mtr);
4548 const page_t *const root = btr_root_get(index, &mtr);
4549 mtr_x_lock(&space->latch, &mtr, UT_LOCATION_HERE);
4550 const page_size_t page_size = dict_table_page_size(index->table);
4551
4552 for (auto offset : {PAGE_BTR_SEG_LEAF, PAGE_BTR_SEG_TOP}) {
4553 const fseg_header_t *const seg_header = root + PAGE_HEADER + offset;
4554 fseg_inode_t *inode =
4555 fseg_inode_get(seg_header, space_id, page_size, &mtr);
4556 File_segment_inode fsi(space_id, page_size, inode, &mtr);
4557 ut_ad(fsi.verify_not_full_n_used());
4558 }
4559
4560 mtr_commit(&mtr);
4561
4562 fil_space_release(space);
4563 }
4564 #endif /* UNIV_DEBUG */
4565
4566 mtr_start(&mtr);
4567
4568 if (!srv_read_only_mode) {
4569 if (lockout) {
4570 mtr_x_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
4571 } else {
4572 mtr_sx_lock(dict_index_get_lock(index), &mtr, UT_LOCATION_HERE);
4573 }
4574 }
4575
4576 bool ok = true;
4577 page_t *root = btr_root_get(index, &mtr);
4578 ulint n = btr_page_get_level(root);
4579
4580 for (ulint i = 0; i <= n; ++i) {
4581 if (!btr_validate_level(index, trx, n - i, lockout)) {
4582 ok = false;
4583 break;
4584 }
4585 }
4586
4587 mtr_commit(&mtr);
4588
4589 return (ok);
4590 }
4591
4592 /** Checks if the page in the cursor can be merged with given page.
4593 If necessary, re-organize the merge_page.
4594 @return true if possible to merge. */
4595 static bool btr_can_merge_with_page(
4596 btr_cur_t *cursor, /*!< in: cursor on the page to merge */
4597 page_no_t page_no, /*!< in: a sibling page */
4598 buf_block_t **merge_block, /*!< out: the merge block */
4599 mtr_t *mtr) /*!< in: mini-transaction */
4600 {
4601 dict_index_t *index;
4602 page_t *page;
4603 ulint n_recs;
4604 ulint data_size;
4605 ulint max_ins_size_reorg;
4606 ulint max_ins_size;
4607 buf_block_t *mblock;
4608 page_t *mpage;
4609 DBUG_TRACE;
4610
4611 if (page_no == FIL_NULL) {
4612 *merge_block = nullptr;
4613 return false;
4614 }
4615
4616 index = cursor->index;
4617 page = btr_cur_get_page(cursor);
4618
4619 const page_id_t page_id(dict_index_get_space(index), page_no);
4620 const page_size_t page_size(dict_table_page_size(index->table));
4621
4622 mblock = btr_block_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE,
4623 index, mtr);
4624 mpage = buf_block_get_frame(mblock);
4625
4626 n_recs = page_get_n_recs(page);
4627 data_size = page_get_data_size(page);
4628
4629 max_ins_size_reorg = page_get_max_insert_size_after_reorganize(mpage, n_recs);
4630
4631 if (data_size > max_ins_size_reorg) {
4632 goto error;
4633 }
4634
4635 /* If compression padding tells us that merging will result in
4636 too packed up page i.e.: which is likely to cause compression
4637 failure then don't merge the pages. */
4638 if (page_size.is_compressed() && page_is_leaf(mpage) &&
4639 (page_get_data_size(mpage) + data_size >=
4640 dict_index_zip_pad_optimal_page_size(index))) {
4641 goto error;
4642 }
4643
4644 max_ins_size = page_get_max_insert_size(mpage, n_recs);
4645
4646 if (data_size > max_ins_size) {
4647 /* We have to reorganize mpage */
4648
4649 if (!btr_page_reorganize_block(false, page_zip_level, mblock, index, mtr)) {
4650 goto error;
4651 }
4652
4653 max_ins_size = page_get_max_insert_size(mpage, n_recs);
4654
4655 ut_ad(page_validate(mpage, index));
4656 ut_ad(max_ins_size == max_ins_size_reorg);
4657
4658 if (data_size > max_ins_size) {
4659 /* Add fault tolerance, though this should
4660 never happen */
4661
4662 goto error;
4663 }
4664 }
4665
4666 *merge_block = mblock;
4667 return true;
4668
4669 error:
4670 *merge_block = nullptr;
4671 return false;
4672 }
4673
4674 /** Create an SDI Index
4675 @param[in] space_id Tablespace id
4676 @param[in,out] mtr Mini-transaction
4677 @param[in,out] table SDI table
4678 @return root page number of the SDI index created or FIL_NULL on failure */
4679 static page_no_t btr_sdi_create(space_id_t space_id, mtr_t *mtr,
4680 dict_table_t *table) {
4681 dict_index_t *index = table->first_index();
4682 ut_ad(index != nullptr);
4683 ut_ad(UT_LIST_GET_LEN(table->indexes) == 1);
4684
4685 index->page = btr_create(DICT_CLUSTERED | DICT_UNIQUE | DICT_SDI, space_id,
4686 index->id, index, mtr);
4687
4688 return (index->page);
4689 }
4690
4691 /** Creates SDI index and stores the root page numbers in page 1 & 2
4692 @param[in] space_id tablespace id
4693 @param[in] dict_locked true if dict_sys mutex is acquired
4694 @return DB_SUCCESS on success, else DB_ERROR on failure */
4695 dberr_t btr_sdi_create_index(space_id_t space_id, bool dict_locked) {
4696 fil_space_t *space = fil_space_acquire(space_id);
4697 if (space == nullptr) {
4698 ut_d(ut_error);
4699 ut_o(return (DB_ERROR));
4700 }
4701
4702 dict_table_t *sdi_table;
4703 page_no_t sdi_root_page_num;
4704
4705 sdi_table = dict_sdi_get_table(space_id, dict_locked, true);
4706 ut_ad(sdi_table != nullptr);
4707
4708 mtr_t mtr;
4709 mtr.start();
4710
4711 const page_size_t page_size = page_size_t(space->flags);
4712
4713 /* Create B-Tree root page for SDI Indexes */
4714
4715 sdi_root_page_num = btr_sdi_create(space_id, &mtr, sdi_table);
4716
4717 if (sdi_root_page_num == FIL_NULL) {
4718 ib::error(ER_IB_MSG_43) << "Unable to create root index page"
4719 " for SDI table "
4720 << " in tablespace " << space_id;
4721 mtr.commit();
4722 dict_sdi_remove_from_cache(space_id, sdi_table, dict_locked);
4723 fil_space_release(space);
4724 return (DB_ERROR);
4725 } else {
4726 dict_index_t *index = sdi_table->first_index();
4727 index->page = sdi_root_page_num;
4728 }
4729
4730 buf_block_t *block = buf_page_get(page_id_t(space_id, 0), page_size,
4731 RW_SX_LATCH, UT_LOCATION_HERE, &mtr);
4732
4733 buf_block_dbg_add_level(block, SYNC_FSP_PAGE);
4734
4735 page_t *page = buf_block_get_frame(block);
4736
4737 /* Write SDI Index root page numbers to Page 0 */
4738 fsp_sdi_write_root_to_page(page, page_size, sdi_root_page_num, &mtr);
4739
4740 /* Space flags from memory */
4741 uint32_t fsp_flags = space->flags;
4742
4743 ut_ad(fsp_header_get_field(page, FSP_SPACE_FLAGS) == fsp_flags);
4744
4745 fsp_flags_set_sdi(fsp_flags);
4746 mlog_write_ulint(FSP_HEADER_OFFSET + FSP_SPACE_FLAGS + page, fsp_flags,
4747 MLOG_4BYTES, &mtr);
4748
4749 mtr.commit();
4750
4751 fil_space_set_flags(space, fsp_flags);
4752
4753 dict_table_close(sdi_table, dict_locked, false);
4754
4755 fil_space_release(space);
4756 return (DB_SUCCESS);
4757 }
4758 #endif /* !UNIV_HOTBACKUP */