Debian Codegraph @

storage/innobase/page/page0page.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 page/page0page.cc
30 Index page routines
31
32 Created 2/2/1994 Heikki Tuuri
33 *******************************************************/
34
35 #include "my_dbug.h"
36
37 #include "btr0btr.h"
38 #include "buf0buf.h"
39 #include "ibuf0ibuf.h"
40 #include "page0cur.h"
41 #include "page0page.h"
42 #include "page0zip.h"
43 #ifndef UNIV_HOTBACKUP
44 #include "btr0sea.h"
45 #include "fut0lst.h"
46 #include "lock0lock.h"
47 #include "srv0srv.h"
48 #endif /* !UNIV_HOTBACKUP */
49
50 /* THE INDEX PAGE
51 ==============
52
53 The index page consists of a page header which contains the page's
54 id and other information. On top of it are the index records
55 in a heap linked into a one way linear list according to alphabetic order.
56
57 Just below page end is an array of pointers which we call page directory,
58 to about every sixth record in the list. The pointers are placed in
59 the directory in the alphabetical order of the records pointed to,
60 enabling us to make binary search using the array. Each slot n:o I
61 in the directory points to a record, where a 4-bit field contains a count
62 of those records which are in the linear list between pointer I and
63 the pointer I - 1 in the directory, including the record
64 pointed to by pointer I and not including the record pointed to by I - 1.
65 We say that the record pointed to by slot I, or that slot I, owns
66 these records. The count is always kept in the range 4 to 8, with
67 the exception that it is 1 for the first slot, and 1--8 for the second slot.
68
69 An essentially binary search can be performed in the list of index
70 records, like we could do if we had pointer to every record in the
71 page directory. The data structure is, however, more efficient when
72 we are doing inserts, because most inserts are just pushed on a heap.
73 Only every 8th insert requires block move in the directory pointer
74 table, which itself is quite small. A record is deleted from the page
75 by just taking it off the linear list and updating the number of owned
76 records-field of the record which owns it, and updating the page directory,
77 if necessary. A special case is the one when the record owns itself.
78 Because the overhead of inserts is so small, we may also increase the
79 page size from the projected default of 8 kB to 64 kB without too
80 much loss of efficiency in inserts. Bigger page becomes actual
81 when the disk transfer rate compared to seek and latency time rises.
82 On the present system, the page size is set so that the page transfer
83 time (3 ms) is 20 % of the disk random access time (15 ms).
84
85 When the page is split, merged, or becomes full but contains deleted
86 records, we have to reorganize the page.
87
88 Assuming a page size of 8 kB, a typical index page of a secondary
89 index contains 300 index entries, and the size of the page directory
90 is 50 x 4 bytes = 200 bytes. */
91
92 /** Looks for the directory slot which owns the given record.
93 @return the directory slot number */
94 ulint page_dir_find_owner_slot(const rec_t *rec) /*!< in: the physical record */
95 {
96 const page_t *page;
97 uint16 rec_offs_bytes;
98 const page_dir_slot_t *slot;
99 const page_dir_slot_t *first_slot;
100 const rec_t *r = rec;
101
102 ut_ad(page_rec_check(rec));
103
104 page = page_align(rec);
105 first_slot = page_dir_get_nth_slot(page, 0);
106 slot = page_dir_get_nth_slot(page, page_dir_get_n_slots(page) - 1);
107
108 if (page_is_comp(page)) {
109 while (rec_get_n_owned_new(r) == 0) {
110 r = rec_get_next_ptr_const(r, true);
111 ut_ad(r >= page + PAGE_NEW_SUPREMUM);
112 ut_ad(r < page + (UNIV_PAGE_SIZE - PAGE_DIR));
113 }
114 } else {
115 while (rec_get_n_owned_old(r) == 0) {
116 r = rec_get_next_ptr_const(r, false);
117 ut_ad(r >= page + PAGE_OLD_SUPREMUM);
118 ut_ad(r < page + (UNIV_PAGE_SIZE - PAGE_DIR));
119 }
120 }
121
122 rec_offs_bytes = mach_encode_2(r - page);
123
124 while (UNIV_LIKELY(*(uint16 *)slot != rec_offs_bytes)) {
125 if (UNIV_UNLIKELY(slot == first_slot)) {
126 ib::error(ER_IB_MSG_860)
127 << "Probable data corruption on page " << page_get_page_no(page)
128 << ". Original record on that page;";
129
130 if (page_is_comp(page)) {
131 fputs("(compact record)", stderr);
132 } else {
133 rec_print_old(stderr, rec);
134 }
135
136 ib::error(ER_IB_MSG_861) << "Cannot find the dir slot for this"
137 " record on that page;";
138
139 if (page_is_comp(page)) {
140 fputs("(compact record)", stderr);
141 } else {
142 rec_print_old(stderr, page + mach_decode_2(rec_offs_bytes));
143 }
144
145 ut_error;
146 }
147
148 slot += PAGE_DIR_SLOT_SIZE;
149 }
150
151 return (((ulint)(first_slot - slot)) / PAGE_DIR_SLOT_SIZE);
152 }
153
154 /** Used to check the consistency of a directory slot.
155 @return true if succeed */
156 static bool page_dir_slot_check(const page_dir_slot_t *slot) /*!< in: slot */
157 {
158 const page_t *page;
159 ulint n_slots;
160 ulint n_owned;
161
162 ut_a(slot);
163
164 page = page_align(slot);
165
166 n_slots = page_dir_get_n_slots(page);
167
168 ut_a(slot <= page_dir_get_nth_slot(page, 0));
169 ut_a(slot >= page_dir_get_nth_slot(page, n_slots - 1));
170
171 ut_a(page_rec_check(page_dir_slot_get_rec(slot)));
172
173 if (page_is_comp(page)) {
174 n_owned = rec_get_n_owned_new(page_dir_slot_get_rec(slot));
175 } else {
176 n_owned = rec_get_n_owned_old(page_dir_slot_get_rec(slot));
177 }
178
179 if (slot == page_dir_get_nth_slot(page, 0)) {
180 ut_a(n_owned == 1);
181 } else if (slot == page_dir_get_nth_slot(page, n_slots - 1)) {
182 ut_a(n_owned >= 1);
183 ut_a(n_owned <= PAGE_DIR_SLOT_MAX_N_OWNED);
184 } else {
185 ut_a(n_owned >= PAGE_DIR_SLOT_MIN_N_OWNED);
186 ut_a(n_owned <= PAGE_DIR_SLOT_MAX_N_OWNED);
187 }
188
189 return true;
190 }
191
192 /** Sets the max trx id field value.
193 @param[in,out] block Page
194 @param[in,out] page_zip Compressed page, or NULL
195 @param[in] trx_id Transaction id
196 @param[in,out] mtr Mini-transaction, or NULL */
197 void page_set_max_trx_id(buf_block_t *block, page_zip_des_t *page_zip,
198 trx_id_t trx_id, mtr_t *mtr) {
199 page_t *page = buf_block_get_frame(block);
200 #ifndef UNIV_HOTBACKUP
201 ut_ad(!mtr || mtr_memo_contains(mtr, block, MTR_MEMO_PAGE_X_FIX));
202 #endif /* !UNIV_HOTBACKUP */
203
204 /* It is not necessary to write this change to the redo log, as
205 during a database recovery we assume that the max trx id of every
206 page is the maximum trx id assigned before the crash. */
207
208 if (page_zip) {
209 mach_write_to_8(page + (PAGE_HEADER + PAGE_MAX_TRX_ID), trx_id);
210 page_zip_write_header(page_zip, page + (PAGE_HEADER + PAGE_MAX_TRX_ID), 8,
211 mtr);
212 #ifndef UNIV_HOTBACKUP
213 } else if (mtr) {
214 mlog_write_ull(page + (PAGE_HEADER + PAGE_MAX_TRX_ID), trx_id, mtr);
215 #endif /* !UNIV_HOTBACKUP */
216 } else {
217 mach_write_to_8(page + (PAGE_HEADER + PAGE_MAX_TRX_ID), trx_id);
218 }
219 }
220
221 /** Allocates a block of memory from the heap of an index page.
222 @return pointer to start of allocated buffer, or NULL if allocation fails */
223 byte *page_mem_alloc_heap(
224 page_t *page, /*!< in/out: index page */
225 page_zip_des_t *page_zip, /*!< in/out: compressed page with enough
226 space available for inserting the record,
227 or NULL */
228 ulint need, /*!< in: total number of bytes needed */
229 ulint *heap_no) /*!< out: this contains the heap number
230 of the allocated record
231 if allocation succeeds */
232 {
233 byte *block;
234 ulint avl_space;
235
236 ut_ad(page && heap_no);
237
238 avl_space = page_get_max_insert_size(page, 1);
239
240 if (avl_space >= need) {
241 block = page_header_get_ptr(page, PAGE_HEAP_TOP);
242
243 page_header_set_ptr(page, page_zip, PAGE_HEAP_TOP, block + need);
244 *heap_no = page_dir_get_n_heap(page);
245
246 page_dir_set_n_heap(page, page_zip, 1 + *heap_no);
247
248 return (block);
249 }
250
251 return (nullptr);
252 }
253
254 #ifndef UNIV_HOTBACKUP
255 /** Writes a log record of page creation
256 @param[in] frame A buffer frame where the page is created
257 @param[in] mtr Mini-transaction handle
258 @param[in] comp true=compact page format
259 @param[in] page_type Page type */
260 static inline void page_create_write_log(buf_frame_t *frame, mtr_t *mtr,
261 bool comp, page_type_t page_type) {
262 mlog_id_t type;
263
264 switch (page_type) {
265 case FIL_PAGE_INDEX:
266 type = comp ? MLOG_COMP_PAGE_CREATE : MLOG_PAGE_CREATE;
267 break;
268 case FIL_PAGE_RTREE:
269 type = comp ? MLOG_COMP_PAGE_CREATE_RTREE : MLOG_PAGE_CREATE_RTREE;
270 break;
271 case FIL_PAGE_SDI:
272 type = comp ? MLOG_COMP_PAGE_CREATE_SDI : MLOG_PAGE_CREATE_SDI;
273 break;
274 default:
275 ut_error;
276 }
277
278 mlog_write_initial_log_record(frame, type, mtr);
279 }
280 #else /* !UNIV_HOTBACKUP */
281 #define page_create_write_log(frame, mtr, comp, type) ((void)0)
282 #endif /* !UNIV_HOTBACKUP */
283
284 /** The page infimum and supremum of an empty page in ROW_FORMAT=REDUNDANT */
285 static const byte infimum_supremum_redundant[] = {
286 /* the infimum record */
287 0x08 /*end offset*/, 0x01 /*n_owned*/, 0x00, 0x00 /*heap_no=0*/,
288 0x03 /*n_fields=1, 1-byte offsets*/, 0x00, 0x74 /* pointer to supremum */,
289 'i', 'n', 'f', 'i', 'm', 'u', 'm', 0,
290 /* the supremum record */
291 0x09 /*end offset*/, 0x01 /*n_owned*/, 0x00, 0x08 /*heap_no=1*/,
292 0x03 /*n_fields=1, 1-byte offsets*/, 0x00, 0x00 /* end of record list */,
293 's', 'u', 'p', 'r', 'e', 'm', 'u', 'm', 0};
294
295 /** The page infimum and supremum of an empty page in ROW_FORMAT=COMPACT */
296 static const byte infimum_supremum_compact[] = {
297 /* the infimum record */
298 0x01 /*n_owned=1*/, 0x00, 0x02 /* heap_no=0, REC_STATUS_INFIMUM */, 0x00,
299 0x0d /* pointer to supremum */, 'i', 'n', 'f', 'i', 'm', 'u', 'm', 0,
300 /* the supremum record */
301 0x01 /*n_owned=1*/, 0x00, 0x0b /* heap_no=1, REC_STATUS_SUPREMUM */, 0x00,
302 0x00 /* end of record list */, 's', 'u', 'p', 'r', 'e', 'm', 'u', 'm'};
303
304 /** The index page creation function.
305 @param[in,out] block a buffer block where the page is created
306 @param[in] comp nonzero=compact page format
307 @param[in] page_type page type
308 @return pointer to the page */
309 static page_t *page_create_low(buf_block_t *block, ulint comp,
310 page_type_t page_type) {
311 page_t *page;
312
313 static_assert(PAGE_BTR_IBUF_FREE_LIST + FLST_BASE_NODE_SIZE <= PAGE_DATA,
314 "PAGE_BTR_IBUF_FREE_LIST + FLST_BASE_NODE_SIZE > PAGE_DATA");
315
316 static_assert(PAGE_BTR_IBUF_FREE_LIST_NODE + FLST_NODE_SIZE <= PAGE_DATA,
317 "PAGE_BTR_IBUF_FREE_LIST_NODE + FLST_NODE_SIZE > PAGE_DATA");
318
319 buf_block_modify_clock_inc(block);
320
321 page = buf_block_get_frame(block);
322
323 ut_ad(page_type == FIL_PAGE_INDEX || page_type == FIL_PAGE_RTREE ||
324 page_type == FIL_PAGE_SDI);
325
326 fil_page_set_type(page, page_type);
327
328 memset(page + PAGE_HEADER, 0, PAGE_HEADER_PRIV_END);
329 page[PAGE_HEADER + PAGE_N_DIR_SLOTS + 1] = 2;
330 page[PAGE_HEADER + PAGE_DIRECTION + 1] = PAGE_NO_DIRECTION;
331
332 if (comp) {
333 page[PAGE_HEADER + PAGE_N_HEAP] = 0x80; /*page_is_comp()*/
334 page[PAGE_HEADER + PAGE_N_HEAP + 1] = PAGE_HEAP_NO_USER_LOW;
335 page[PAGE_HEADER + PAGE_HEAP_TOP + 1] = PAGE_NEW_SUPREMUM_END;
336 memcpy(page + PAGE_DATA, infimum_supremum_compact,
337 sizeof infimum_supremum_compact);
338 memset(page + PAGE_NEW_SUPREMUM_END, 0,
339 UNIV_PAGE_SIZE - PAGE_DIR - PAGE_NEW_SUPREMUM_END);
340 page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE * 2 + 1] =
341 PAGE_NEW_SUPREMUM;
342 page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE + 1] = PAGE_NEW_INFIMUM;
343 } else {
344 page[PAGE_HEADER + PAGE_N_HEAP + 1] = PAGE_HEAP_NO_USER_LOW;
345 page[PAGE_HEADER + PAGE_HEAP_TOP + 1] = PAGE_OLD_SUPREMUM_END;
346 memcpy(page + PAGE_DATA, infimum_supremum_redundant,
347 sizeof infimum_supremum_redundant);
348 memset(page + PAGE_OLD_SUPREMUM_END, 0,
349 UNIV_PAGE_SIZE - PAGE_DIR - PAGE_OLD_SUPREMUM_END);
350 page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE * 2 + 1] =
351 PAGE_OLD_SUPREMUM;
352 page[UNIV_PAGE_SIZE - PAGE_DIR - PAGE_DIR_SLOT_SIZE + 1] = PAGE_OLD_INFIMUM;
353 }
354
355 return (page);
356 }
357
358 /** Parses a redo log record of creating a page.
359 @param[in,out] block buffer block, or NULL
360 @param[in] comp nonzero=compact page format
361 @param[in] page_type page type (FIL_PAGE_INDEX, FIL_PAGE_RTREE
362 or FIL_PAGE_SDI) */
363 void page_parse_create(buf_block_t *block, ulint comp, page_type_t page_type) {
364 if (block != nullptr) {
365 page_create_low(block, comp, page_type);
366 }
367 }
368
369 /** Create an uncompressed B-tree or R-tree or SDI index page.
370 @param[in] block A buffer block where the page is created
371 @param[in] mtr Mini-transaction handle
372 @param[in] comp nonzero=compact page format
373 @param[in] page_type Page type
374 @return pointer to the page */
375 page_t *page_create(buf_block_t *block, mtr_t *mtr, ulint comp,
376 page_type_t page_type) {
377 page_create_write_log(buf_block_get_frame(block), mtr, comp, page_type);
378 return (page_create_low(block, comp, page_type));
379 }
380
381 /** Create a compressed B-tree index page.
382 @param[in,out] block Buffer frame where the page is created
383 @param[in] index Index of the page, or NULL when applying
384 TRUNCATE log record during recovery
385 @param[in] level The B-tree level of the page
386 @param[in] max_trx_id PAGE_MAX_TRX_ID
387 @param[in] mtr Mini-transaction handle
388 @param[in] page_type Page type to be created. Only FIL_PAGE_INDEX,
389 FIL_PAGE_RTREE, FIL_PAGE_SDI allowed
390 @return pointer to the page */
391 page_t *page_create_zip(buf_block_t *block, dict_index_t *index, ulint level,
392 trx_id_t max_trx_id, mtr_t *mtr,
393 page_type_t page_type) {
394 page_t *page;
395 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
396
397 ut_ad(block);
398 ut_ad(page_zip);
399 ut_ad(dict_table_is_comp(index->table));
400
401 #ifdef UNIV_DEBUG
402 switch (page_type) {
403 case FIL_PAGE_INDEX:
404 case FIL_PAGE_RTREE:
405 case FIL_PAGE_SDI:
406 break;
407 default:
408 ut_d(ut_error);
409 }
410 #endif /* UNIV_DEBUG */
411
412 page = page_create_low(block, true, page_type);
413
414 mach_write_to_2(PAGE_HEADER + PAGE_LEVEL + page, level);
415 mach_write_to_8(PAGE_HEADER + PAGE_MAX_TRX_ID + page, max_trx_id);
416
417 if (!page_zip_compress(page_zip, page, index, page_zip_level, mtr)) {
418 /* The compression of a newly created
419 page should always succeed. */
420 ut_error;
421 }
422
423 return (page);
424 }
425
426 /** Empty a previously created B-tree index page.
427 @param[in,out] block B-tree block
428 @param[in] index The index of the page
429 @param[in,out] mtr Mini-transaction */
430 void page_create_empty(buf_block_t *block, dict_index_t *index, mtr_t *mtr) {
431 trx_id_t max_trx_id = 0;
432 page_t *page = buf_block_get_frame(block);
433 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
434
435 ut_ad(fil_page_index_page_check(page));
436
437 /* Multiple transactions cannot simultaneously operate on the
438 same temp-table in parallel.
439 max_trx_id is ignored for temp tables because it not required
440 for MVCC. */
441 if (dict_index_is_sec_or_ibuf(index) && !index->table->is_temporary() &&
442 page_is_leaf(page)) {
443 max_trx_id = page_get_max_trx_id(page);
444 ut_ad(max_trx_id);
445 }
446
447 if (page_zip) {
448 ut_ad(!index->table->is_temporary());
449 page_create_zip(block, index, page_header_get_field(page, PAGE_LEVEL),
450 max_trx_id, mtr, fil_page_get_type(page));
451 } else {
452 page_create(block, mtr, page_is_comp(page), fil_page_get_type(page));
453
454 if (max_trx_id) {
455 page_update_max_trx_id(block, page_zip, max_trx_id, mtr);
456 }
457 }
458 }
459
460 /** Differs from page_copy_rec_list_end, because this function does not
461 touch the lock table and max trx id on page or compress the page.
462
463 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
464 if new_block is a compressed leaf page in a secondary index.
465 This has to be done either within the same mini-transaction,
466 or by invoking ibuf_reset_free_bits() before mtr_commit(). */
467 void page_copy_rec_list_end_no_locks(
468 buf_block_t *new_block, /*!< in: index page to copy to */
469 buf_block_t *block, /*!< in: index page of rec */
470 rec_t *rec, /*!< in: record on page */
471 dict_index_t *index, /*!< in: record descriptor */
472 mtr_t *mtr) /*!< in: mtr */
473 {
474 page_t *new_page = buf_block_get_frame(new_block);
475 page_cur_t cur1;
476 rec_t *cur2;
477 mem_heap_t *heap = nullptr;
478 ulint offsets_[REC_OFFS_NORMAL_SIZE];
479 ulint *offsets = offsets_;
480 rec_offs_init(offsets_);
481
482 page_cur_position(rec, block, &cur1);
483
484 if (page_cur_is_before_first(&cur1)) {
485 page_cur_move_to_next(&cur1);
486 }
487
488 btr_assert_not_corrupted(new_block, index);
489 ut_a(page_is_comp(new_page) == page_rec_is_comp(rec));
490 ut_a(mach_read_from_2(new_page + UNIV_PAGE_SIZE - 10) ==
491 (ulint)(page_is_comp(new_page) ? PAGE_NEW_INFIMUM : PAGE_OLD_INFIMUM));
492
493 cur2 = page_get_infimum_rec(buf_block_get_frame(new_block));
494
495 /* Copy records from the original page to the new page */
496
497 while (!page_cur_is_after_last(&cur1)) {
498 rec_t *cur1_rec = page_cur_get_rec(&cur1);
499 rec_t *ins_rec;
500 offsets = rec_get_offsets(cur1_rec, index, offsets, ULINT_UNDEFINED,
501 UT_LOCATION_HERE, &heap);
502 ins_rec = page_cur_insert_rec_low(cur2, index, cur1_rec, offsets, mtr);
503 if (UNIV_UNLIKELY(!ins_rec)) {
504 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_862)
505 << "Rec offset " << page_offset(rec) << ", cur1 offset "
506 << page_offset(page_cur_get_rec(&cur1)) << ", cur2 offset "
507 << page_offset(cur2);
508 }
509
510 page_cur_move_to_next(&cur1);
511 cur2 = ins_rec;
512 }
513
514 if (UNIV_LIKELY_NULL(heap)) {
515 mem_heap_free(heap);
516 }
517 }
518
519 #ifndef UNIV_HOTBACKUP
520 /** Copies records from page to new_page, from a given record onward,
521 including that record. Infimum and supremum records are not copied.
522 The records are copied to the start of the record list on new_page.
523
524 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
525 if new_block is a compressed leaf page in a secondary index.
526 This has to be done either within the same mini-transaction,
527 or by invoking ibuf_reset_free_bits() before mtr_commit().
528
529 @return pointer to the original successor of the infimum record on
530 new_page, or NULL on zip overflow (new_block will be decompressed) */
531 rec_t *page_copy_rec_list_end(
532 buf_block_t *new_block, /*!< in/out: index page to copy to */
533 buf_block_t *block, /*!< in: index page containing rec */
534 rec_t *rec, /*!< in: record on page */
535 dict_index_t *index, /*!< in: record descriptor */
536 mtr_t *mtr) /*!< in: mtr */
537 {
538 page_t *new_page = buf_block_get_frame(new_block);
539 page_zip_des_t *new_page_zip = buf_block_get_page_zip(new_block);
540 page_t *page = page_align(rec);
541 rec_t *ret = page_rec_get_next(page_get_infimum_rec(new_page));
542 ulint num_moved = 0;
543 rtr_rec_move_t *rec_move = nullptr;
544 mem_heap_t *heap = nullptr;
545
546 #ifdef UNIV_ZIP_DEBUG
547 if (new_page_zip) {
548 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
549 ut_a(page_zip);
550
551 /* Strict page_zip_validate() may fail here.
552 Furthermore, btr_compress() may set FIL_PAGE_PREV to
553 FIL_NULL on new_page while leaving it intact on
554 new_page_zip. So, we cannot validate new_page_zip. */
555 ut_a(page_zip_validate_low(page_zip, page, index, true));
556 }
557 #endif /* UNIV_ZIP_DEBUG */
558 ut_ad(buf_block_get_frame(block) == page);
559 ut_ad(page_is_leaf(page) == page_is_leaf(new_page));
560 ut_ad(page_is_comp(page) == page_is_comp(new_page));
561 /* Here, "ret" may be pointing to a user record or the
562 predefined supremum record. */
563
564 mtr_log_t log_mode = MTR_LOG_NONE;
565
566 if (new_page_zip) {
567 log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
568 }
569
570 if (page_dir_get_n_heap(new_page) == PAGE_HEAP_NO_USER_LOW) {
571 page_copy_rec_list_end_to_created_page(new_page, rec, index, mtr);
572 } else {
573 if (dict_index_is_spatial(index)) {
574 ulint max_to_move = page_get_n_recs(buf_block_get_frame(block));
575 heap = mem_heap_create(256, UT_LOCATION_HERE);
576
577 rec_move = static_cast<rtr_rec_move_t *>(
578 mem_heap_alloc(heap, sizeof(*rec_move) * max_to_move));
579
580 /* For spatial index, we need to insert recs one by one
581 to keep recs ordered. */
582 rtr_page_copy_rec_list_end_no_locks(new_block, block, rec, index, heap,
583 rec_move, max_to_move, &num_moved,
584 mtr);
585 } else {
586 page_copy_rec_list_end_no_locks(new_block, block, rec, index, mtr);
587 }
588 }
589
590 /* Update PAGE_MAX_TRX_ID on the uncompressed page.
591 Modifications will be redo logged and copied to the compressed
592 page in page_zip_compress() or page_zip_reorganize() below.
593 Multiple transactions cannot simultaneously operate on the
594 same temp-table in parallel.
595 max_trx_id is ignored for temp tables because it not required
596 for MVCC. */
597 if (dict_index_is_sec_or_ibuf(index) && page_is_leaf(page) &&
598 !index->table->is_temporary()) {
599 page_update_max_trx_id(new_block, nullptr, page_get_max_trx_id(page), mtr);
600 }
601
602 if (new_page_zip) {
603 mtr_set_log_mode(mtr, log_mode);
604
605 if (!page_zip_compress(new_page_zip, new_page, index, page_zip_level,
606 mtr)) {
607 /* Before trying to reorganize the page,
608 store the number of preceding records on the page. */
609 ulint ret_pos = page_rec_get_n_recs_before(ret);
610 /* Before copying, "ret" was the successor of
611 the predefined infimum record. It must still
612 have at least one predecessor (the predefined
613 infimum record, or a freshly copied record
614 that is smaller than "ret"). */
615 ut_a(ret_pos > 0);
616
617 if (!page_zip_reorganize(new_block, index, mtr)) {
618 if (!page_zip_decompress(new_page_zip, new_page, false)) {
619 ut_error;
620 }
621 ut_ad(page_validate(new_page, index));
622
623 if (heap) {
624 mem_heap_free(heap);
625 }
626
627 return (nullptr);
628 } else {
629 /* The page was reorganized:
630 Seek to ret_pos. */
631 ret = new_page + PAGE_NEW_INFIMUM;
632
633 do {
634 ret = rec_get_next_ptr(ret, true);
635 } while (--ret_pos);
636 }
637 }
638 }
639
640 /* Update the lock table and possible hash index */
641
642 if (dict_index_is_spatial(index) && rec_move) {
643 lock_rtr_move_rec_list(new_block, block, rec_move, num_moved);
644 } else if (!dict_table_is_locking_disabled(index->table)) {
645 lock_move_rec_list_end(new_block, block, rec);
646 }
647
648 if (heap) {
649 mem_heap_free(heap);
650 }
651
652 btr_search_update_hash_on_move(new_block, block, index);
653
654 return (ret);
655 }
656
657 /** Copies records from page to new_page, up to the given record,
658 NOT including that record. Infimum and supremum records are not copied.
659 The records are copied to the end of the record list on new_page.
660
661 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
662 if new_block is a compressed leaf page in a secondary index.
663 This has to be done either within the same mini-transaction,
664 or by invoking ibuf_reset_free_bits() before mtr_commit().
665
666 @return pointer to the original predecessor of the supremum record on
667 new_page, or NULL on zip overflow (new_block will be decompressed) */
668 rec_t *page_copy_rec_list_start(
669 buf_block_t *new_block, /*!< in/out: index page to copy to */
670 buf_block_t *block, /*!< in: index page containing rec */
671 rec_t *rec, /*!< in: record on page */
672 dict_index_t *index, /*!< in: record descriptor */
673 mtr_t *mtr) /*!< in: mtr */
674 {
675 page_t *new_page = buf_block_get_frame(new_block);
676 page_zip_des_t *new_page_zip = buf_block_get_page_zip(new_block);
677 page_cur_t cur1;
678 rec_t *cur2;
679 mem_heap_t *heap = nullptr;
680 ulint num_moved = 0;
681 rtr_rec_move_t *rec_move = nullptr;
682 rec_t *ret = page_rec_get_prev(page_get_supremum_rec(new_page));
683 ulint offsets_[REC_OFFS_NORMAL_SIZE];
684 ulint *offsets = offsets_;
685 rec_offs_init(offsets_);
686
687 /* Here, "ret" may be pointing to a user record or the
688 predefined infimum record. */
689
690 if (page_rec_is_infimum(rec)) {
691 return (ret);
692 }
693
694 mtr_log_t log_mode = MTR_LOG_NONE;
695
696 if (new_page_zip) {
697 log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
698 }
699
700 page_cur_set_before_first(block, &cur1);
701 page_cur_move_to_next(&cur1);
702
703 cur2 = ret;
704
705 /* Copy records from the original page to the new page */
706 if (dict_index_is_spatial(index)) {
707 ulint max_to_move = page_get_n_recs(buf_block_get_frame(block));
708 heap = mem_heap_create(256, UT_LOCATION_HERE);
709
710 rec_move = static_cast<rtr_rec_move_t *>(
711 mem_heap_alloc(heap, sizeof(*rec_move) * max_to_move));
712
713 /* For spatial index, we need to insert recs one by one
714 to keep recs ordered. */
715 rtr_page_copy_rec_list_start_no_locks(new_block, block, rec, index, heap,
716 rec_move, max_to_move, &num_moved,
717 mtr);
718 } else {
719 while (page_cur_get_rec(&cur1) != rec) {
720 rec_t *cur1_rec = page_cur_get_rec(&cur1);
721 offsets = rec_get_offsets(cur1_rec, index, offsets, ULINT_UNDEFINED,
722 UT_LOCATION_HERE, &heap);
723 cur2 = page_cur_insert_rec_low(cur2, index, cur1_rec, offsets, mtr);
724 ut_a(cur2);
725
726 page_cur_move_to_next(&cur1);
727 }
728 }
729
730 /* Update PAGE_MAX_TRX_ID on the uncompressed page.
731 Modifications will be redo logged and copied to the compressed
732 page in page_zip_compress() or page_zip_reorganize() below.
733 Multiple transactions cannot simultaneously operate on the
734 same temp-table in parallel.
735 max_trx_id is ignored for temp tables because it not required
736 for MVCC. */
737 if (dict_index_is_sec_or_ibuf(index) && page_is_leaf(page_align(rec)) &&
738 !index->table->is_temporary()) {
739 page_update_max_trx_id(new_block, nullptr,
740 page_get_max_trx_id(page_align(rec)), mtr);
741 }
742
743 if (new_page_zip) {
744 mtr_set_log_mode(mtr, log_mode);
745
746 DBUG_EXECUTE_IF("page_copy_rec_list_start_compress_fail",
747 goto zip_reorganize;);
748
749 if (!page_zip_compress(new_page_zip, new_page, index, page_zip_level,
750 mtr)) {
751 ulint ret_pos;
752 #ifdef UNIV_DEBUG
753 zip_reorganize:
754 #endif /* UNIV_DEBUG */
755 /* Before trying to reorganize the page,
756 store the number of preceding records on the page. */
757 ret_pos = page_rec_get_n_recs_before(ret);
758 /* Before copying, "ret" was the predecessor
759 of the predefined supremum record. If it was
760 the predefined infimum record, then it would
761 still be the infimum, and we would have
762 ret_pos == 0. */
763
764 if (UNIV_UNLIKELY(!page_zip_reorganize(new_block, index, mtr))) {
765 if (UNIV_UNLIKELY(
766 !page_zip_decompress(new_page_zip, new_page, false))) {
767 ut_error;
768 }
769 ut_ad(page_validate(new_page, index));
770
771 if (UNIV_LIKELY_NULL(heap)) {
772 mem_heap_free(heap);
773 }
774
775 return (nullptr);
776 }
777
778 /* The page was reorganized: Seek to ret_pos. */
779 ret = page_rec_get_nth(new_page, ret_pos);
780 }
781 }
782
783 /* Update the lock table and possible hash index */
784
785 if (dict_index_is_spatial(index)) {
786 lock_rtr_move_rec_list(new_block, block, rec_move, num_moved);
787 } else if (!dict_table_is_locking_disabled(index->table)) {
788 lock_move_rec_list_start(new_block, block, rec, ret);
789 }
790
791 if (heap) {
792 mem_heap_free(heap);
793 }
794
795 btr_search_update_hash_on_move(new_block, block, index);
796
797 return (ret);
798 }
799
800 /** Writes a log record of a record list end or start deletion. */
801 static inline void page_delete_rec_list_write_log(
802 rec_t *rec, /*!< in: record on page */
803 dict_index_t *index, /*!< in: record descriptor */
804 mlog_id_t type, /*!< in: operation type:
805 MLOG_LIST_END_DELETE, ... */
806 mtr_t *mtr) /*!< in: mtr */
807 {
808 byte *log_ptr = nullptr;
809 ut_ad(type == MLOG_LIST_END_DELETE || type == MLOG_LIST_START_DELETE);
810
811 if (!mlog_open_and_write_index(mtr, rec, index, type, 2, log_ptr)) {
812 return;
813 }
814 /* Write the parameter as a 2-byte ulint */
815 mach_write_to_2(log_ptr, page_offset(rec));
816 mlog_close(mtr, log_ptr + 2);
817 }
818 #else /* !UNIV_HOTBACKUP */
819 #define page_delete_rec_list_write_log(rec, index, type, mtr) ((void)0)
820 #endif /* !UNIV_HOTBACKUP */
821
822 /** Parses a log record of a record list end or start deletion.
823 @return end of log record or NULL */
824 byte *page_parse_delete_rec_list(
825 mlog_id_t type, /*!< in: MLOG_LIST_END_DELETE,
826 MLOG_LIST_START_DELETE,
827 MLOG_COMP_LIST_END_DELETE or
828 MLOG_COMP_LIST_START_DELETE */
829 byte *ptr, /*!< in: buffer */
830 byte *end_ptr, /*!< in: buffer end */
831 buf_block_t *block, /*!< in/out: buffer block or NULL */
832 dict_index_t *index, /*!< in: record descriptor */
833 mtr_t *mtr) /*!< in: mtr or NULL */
834 {
835 page_t *page;
836 ulint offset;
837
838 ut_ad(type == MLOG_LIST_END_DELETE_8027 ||
839 type == MLOG_LIST_START_DELETE_8027 ||
840 type == MLOG_COMP_LIST_END_DELETE_8027 ||
841 type == MLOG_COMP_LIST_START_DELETE_8027 ||
842 type == MLOG_LIST_END_DELETE || type == MLOG_LIST_START_DELETE);
843
844 /* Read the record offset as a 2-byte ulint */
845
846 if (end_ptr < ptr + 2) {
847 return (nullptr);
848 }
849
850 offset = mach_read_from_2(ptr);
851 ptr += 2;
852
853 if (!block) {
854 return (ptr);
855 }
856
857 page = buf_block_get_frame(block);
858
859 ut_ad(page_is_comp(page) == dict_table_is_comp(index->table));
860
861 if (type == MLOG_LIST_END_DELETE || type == MLOG_COMP_LIST_END_DELETE_8027 ||
862 type == MLOG_LIST_END_DELETE_8027) {
863 page_delete_rec_list_end(page + offset, block, index, ULINT_UNDEFINED,
864 ULINT_UNDEFINED, mtr);
865 } else {
866 page_delete_rec_list_start(page + offset, block, index, mtr);
867 }
868
869 return (ptr);
870 }
871
872 /** Deletes records from a page from a given record onward, including that
873 record. The infimum and supremum records are not deleted. */
874 void page_delete_rec_list_end(
875 rec_t *rec, /*!< in: pointer to record on page */
876 buf_block_t *block, /*!< in: buffer block of the page */
877 dict_index_t *index, /*!< in: record descriptor */
878 ulint n_recs, /*!< in: number of records to delete,
879 or ULINT_UNDEFINED if not known */
880 ulint size, /*!< in: the sum of the sizes of the
881 records in the end of the chain to
882 delete, or ULINT_UNDEFINED if not known */
883 mtr_t *mtr) /*!< in: mtr */
884 {
885 page_dir_slot_t *slot;
886 ulint slot_index;
887 rec_t *last_rec;
888 rec_t *prev_rec;
889 ulint n_owned;
890 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
891 page_t *page = page_align(rec);
892 mem_heap_t *heap = nullptr;
893 ulint offsets_[REC_OFFS_NORMAL_SIZE];
894 ulint *offsets = offsets_;
895 rec_offs_init(offsets_);
896
897 ut_ad(size == ULINT_UNDEFINED || size < UNIV_PAGE_SIZE);
898 ut_ad(!page_zip || page_rec_is_comp(rec));
899 #ifdef UNIV_ZIP_DEBUG
900 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
901 #endif /* UNIV_ZIP_DEBUG */
902
903 if (page_rec_is_supremum(rec)) {
904 ut_ad(n_recs == 0 || n_recs == ULINT_UNDEFINED);
905 /* Nothing to do, there are no records bigger than the
906 page supremum. */
907 return;
908 }
909
910 if (recv_recovery_is_on()) {
911 /* If we are replaying a redo log record, we must
912 replay it exactly. Since MySQL 5.6.11, we should be
913 generating a redo log record for page creation if
914 the page would become empty. Thus, this branch should
915 only be executed when applying redo log that was
916 generated by an older version of MySQL. */
917 } else if (page_rec_is_infimum(rec) || n_recs == page_get_n_recs(page)) {
918 delete_all:
919 /* We are deleting all records. */
920 page_create_empty(block, index, mtr);
921 return;
922 } else if (page_is_comp(page)) {
923 if (page_rec_get_next_low(page + PAGE_NEW_INFIMUM, 1) == rec) {
924 /* We are deleting everything from the first
925 user record onwards. */
926 goto delete_all;
927 }
928 } else {
929 if (page_rec_get_next_low(page + PAGE_OLD_INFIMUM, 0) == rec) {
930 /* We are deleting everything from the first
931 user record onwards. */
932 goto delete_all;
933 }
934 }
935
936 /* Reset the last insert info in the page header and increment
937 the modify clock for the frame */
938
939 page_header_set_ptr(page, page_zip, PAGE_LAST_INSERT, nullptr);
940
941 /* The page gets invalid for optimistic searches: increment the
942 frame modify clock */
943
944 buf_block_modify_clock_inc(block);
945
946 page_delete_rec_list_write_log(rec, index, MLOG_LIST_END_DELETE, mtr);
947
948 if (page_zip) {
949 mtr_log_t log_mode;
950
951 ut_a(page_is_comp(page));
952 /* Individual deletes are not logged */
953
954 log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
955
956 do {
957 page_cur_t cur;
958 page_cur_position(rec, block, &cur);
959
960 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
961 UT_LOCATION_HERE, &heap);
962 rec = rec_get_next_ptr(rec, true);
963 #ifdef UNIV_ZIP_DEBUG
964 ut_a(page_zip_validate(page_zip, page, index));
965 #endif /* UNIV_ZIP_DEBUG */
966 page_cur_delete_rec(&cur, index, offsets, mtr);
967 } while (page_offset(rec) != PAGE_NEW_SUPREMUM);
968
969 if (UNIV_LIKELY_NULL(heap)) {
970 mem_heap_free(heap);
971 }
972
973 /* Restore log mode */
974
975 mtr_set_log_mode(mtr, log_mode);
976 return;
977 }
978
979 prev_rec = page_rec_get_prev(rec);
980
981 last_rec = page_rec_get_prev(page_get_supremum_rec(page));
982
983 if ((size == ULINT_UNDEFINED) || (n_recs == ULINT_UNDEFINED)) {
984 rec_t *rec2 = rec;
985 /* Calculate the sum of sizes and the number of records */
986 size = 0;
987 n_recs = 0;
988
989 do {
990 ulint s;
991 offsets = rec_get_offsets(rec2, index, offsets, ULINT_UNDEFINED,
992 UT_LOCATION_HERE, &heap);
993 s = rec_offs_size(offsets);
994 ut_ad(rec2 - page + s - rec_offs_extra_size(offsets) < UNIV_PAGE_SIZE);
995 ut_ad(size + s < UNIV_PAGE_SIZE);
996 size += s;
997 n_recs++;
998
999 rec2 = page_rec_get_next(rec2);
1000 } while (!page_rec_is_supremum(rec2));
1001
1002 if (UNIV_LIKELY_NULL(heap)) {
1003 mem_heap_free(heap);
1004 }
1005 }
1006
1007 ut_ad(size < UNIV_PAGE_SIZE);
1008
1009 /* Update the page directory; there is no need to balance the number
1010 of the records owned by the supremum record, as it is allowed to be
1011 less than PAGE_DIR_SLOT_MIN_N_OWNED */
1012
1013 if (page_is_comp(page)) {
1014 rec_t *rec2 = rec;
1015 ulint count = 0;
1016
1017 while (rec_get_n_owned_new(rec2) == 0) {
1018 count++;
1019
1020 rec2 = rec_get_next_ptr(rec2, true);
1021 }
1022
1023 ut_ad(rec_get_n_owned_new(rec2) > count);
1024
1025 n_owned = rec_get_n_owned_new(rec2) - count;
1026 slot_index = page_dir_find_owner_slot(rec2);
1027 ut_ad(slot_index > 0);
1028 slot = page_dir_get_nth_slot(page, slot_index);
1029 } else {
1030 rec_t *rec2 = rec;
1031 ulint count = 0;
1032
1033 while (rec_get_n_owned_old(rec2) == 0) {
1034 count++;
1035
1036 rec2 = rec_get_next_ptr(rec2, false);
1037 }
1038
1039 ut_ad(rec_get_n_owned_old(rec2) > count);
1040
1041 n_owned = rec_get_n_owned_old(rec2) - count;
1042 slot_index = page_dir_find_owner_slot(rec2);
1043 ut_ad(slot_index > 0);
1044 slot = page_dir_get_nth_slot(page, slot_index);
1045 }
1046
1047 page_dir_slot_set_rec(slot, page_get_supremum_rec(page));
1048 page_dir_slot_set_n_owned(slot, nullptr, n_owned);
1049
1050 page_dir_set_n_slots(page, nullptr, slot_index + 1);
1051
1052 /* Remove the record chain segment from the record chain */
1053 page_rec_set_next(prev_rec, page_get_supremum_rec(page));
1054
1055 /* Catenate the deleted chain segment to the page free list */
1056
1057 page_rec_set_next(last_rec, page_header_get_ptr(page, PAGE_FREE));
1058 page_header_set_ptr(page, nullptr, PAGE_FREE, rec);
1059
1060 page_header_set_field(page, nullptr, PAGE_GARBAGE,
1061 size + page_header_get_field(page, PAGE_GARBAGE));
1062
1063 page_header_set_field(page, nullptr, PAGE_N_RECS,
1064 (ulint)(page_get_n_recs(page) - n_recs));
1065 }
1066
1067 /** Deletes records from page, up to the given record, NOT including
1068 that record. Infimum and supremum records are not deleted. */
1069 void page_delete_rec_list_start(
1070 rec_t *rec, /*!< in: record on page */
1071 buf_block_t *block, /*!< in: buffer block of the page */
1072 dict_index_t *index, /*!< in: record descriptor */
1073 mtr_t *mtr) /*!< in: mtr */
1074 {
1075 page_cur_t cur1;
1076 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1077 ulint *offsets = offsets_;
1078 mem_heap_t *heap = nullptr;
1079
1080 rec_offs_init(offsets_);
1081
1082 ut_ad(page_rec_is_comp(rec) == dict_table_is_comp(index->table));
1083 #ifdef UNIV_ZIP_DEBUG
1084 {
1085 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
1086 page_t *page = buf_block_get_frame(block);
1087
1088 /* page_zip_validate() would detect a min_rec_mark mismatch
1089 in btr_page_split_and_insert()
1090 between btr_attach_half_pages() and insert_page = ...
1091 when btr_page_get_split_rec_to_left() holds
1092 (direction == FSP_DOWN). */
1093 ut_a(!page_zip || page_zip_validate_low(page_zip, page, index, true));
1094 }
1095 #endif /* UNIV_ZIP_DEBUG */
1096
1097 if (page_rec_is_infimum(rec)) {
1098 return;
1099 }
1100
1101 if (page_rec_is_supremum(rec)) {
1102 /* We are deleting all records. */
1103 page_create_empty(block, index, mtr);
1104 return;
1105 }
1106
1107 #ifndef UNIV_HOTBACKUP
1108 page_delete_rec_list_write_log(rec, index, MLOG_LIST_START_DELETE, mtr);
1109 #endif /* !UNIV_HOTBACKUP */
1110
1111 page_cur_set_before_first(block, &cur1);
1112 page_cur_move_to_next(&cur1);
1113
1114 /* Individual deletes are not logged */
1115
1116 mtr_log_t log_mode = mtr_set_log_mode(mtr, MTR_LOG_NONE);
1117
1118 while (page_cur_get_rec(&cur1) != rec) {
1119 offsets = rec_get_offsets(page_cur_get_rec(&cur1), index, offsets,
1120 ULINT_UNDEFINED, UT_LOCATION_HERE, &heap);
1121 page_cur_delete_rec(&cur1, index, offsets, mtr);
1122 }
1123
1124 if (UNIV_LIKELY_NULL(heap)) {
1125 mem_heap_free(heap);
1126 }
1127
1128 /* Restore log mode */
1129
1130 mtr_set_log_mode(mtr, log_mode);
1131 }
1132
1133 #ifndef UNIV_HOTBACKUP
1134 /** Moves record list end to another page. Moved records include
1135 split_rec.
1136
1137 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
1138 if new_block is a compressed leaf page in a secondary index.
1139 This has to be done either within the same mini-transaction,
1140 or by invoking ibuf_reset_free_bits() before mtr_commit().
1141
1142 @return true on success; false on compression failure (new_block will
1143 be decompressed) */
1144 bool page_move_rec_list_end(
1145 buf_block_t *new_block, /*!< in/out: index page where to move */
1146 buf_block_t *block, /*!< in: index page from where to move */
1147 rec_t *split_rec, /*!< in: first record to move */
1148 dict_index_t *index, /*!< in: record descriptor */
1149 mtr_t *mtr) /*!< in: mtr */
1150 {
1151 page_t *new_page = buf_block_get_frame(new_block);
1152 ulint old_data_size;
1153 ulint new_data_size;
1154 ulint old_n_recs;
1155 ulint new_n_recs;
1156
1157 ut_ad(!dict_index_is_spatial(index));
1158
1159 old_data_size = page_get_data_size(new_page);
1160 old_n_recs = page_get_n_recs(new_page);
1161 #ifdef UNIV_ZIP_DEBUG
1162 {
1163 page_zip_des_t *new_page_zip = buf_block_get_page_zip(new_block);
1164 page_zip_des_t *page_zip = buf_block_get_page_zip(block);
1165 ut_a(!new_page_zip == !page_zip);
1166 ut_a(!new_page_zip || page_zip_validate(new_page_zip, new_page, index));
1167 ut_a(!page_zip ||
1168 page_zip_validate(page_zip, page_align(split_rec), index));
1169 }
1170 #endif /* UNIV_ZIP_DEBUG */
1171
1172 if (UNIV_UNLIKELY(
1173 !page_copy_rec_list_end(new_block, block, split_rec, index, mtr))) {
1174 return false;
1175 }
1176
1177 new_data_size = page_get_data_size(new_page);
1178 new_n_recs = page_get_n_recs(new_page);
1179
1180 ut_ad(new_data_size >= old_data_size);
1181
1182 page_delete_rec_list_end(split_rec, block, index, new_n_recs - old_n_recs,
1183 new_data_size - old_data_size, mtr);
1184
1185 return true;
1186 }
1187
1188 /** Moves record list start to another page. Moved records do not include
1189 split_rec.
1190
1191 IMPORTANT: The caller will have to update IBUF_BITMAP_FREE
1192 if new_block is a compressed leaf page in a secondary index.
1193 This has to be done either within the same mini-transaction,
1194 or by invoking ibuf_reset_free_bits() before mtr_commit().
1195
1196 @return true on success; false on compression failure */
1197 bool page_move_rec_list_start(
1198 buf_block_t *new_block, /*!< in/out: index page where to move */
1199 buf_block_t *block, /*!< in/out: page containing split_rec */
1200 rec_t *split_rec, /*!< in: first record not to move */
1201 dict_index_t *index, /*!< in: record descriptor */
1202 mtr_t *mtr) /*!< in: mtr */
1203 {
1204 if (UNIV_UNLIKELY(
1205 !page_copy_rec_list_start(new_block, block, split_rec, index, mtr))) {
1206 return false;
1207 }
1208
1209 page_delete_rec_list_start(split_rec, block, index, mtr);
1210
1211 return true;
1212 }
1213 #endif /* !UNIV_HOTBACKUP */
1214
1215 /** Used to delete n slots from the directory. This function updates
1216 also n_owned fields in the records, so that the first slot after
1217 the deleted ones inherits the records of the deleted slots. */
1218 static inline void page_dir_delete_slot(
1219 page_t *page, /*!< in/out: the index page */
1220 page_zip_des_t *page_zip, /*!< in/out: compressed page, or NULL */
1221 ulint slot_no) /*!< in: slot to be deleted */
1222 {
1223 page_dir_slot_t *slot;
1224 ulint n_owned;
1225 ulint i;
1226 ulint n_slots;
1227
1228 ut_ad(!page_zip || page_is_comp(page));
1229 ut_ad(slot_no > 0);
1230 ut_ad(slot_no + 1 < page_dir_get_n_slots(page));
1231
1232 n_slots = page_dir_get_n_slots(page);
1233
1234 /* 1. Reset the n_owned fields of the slots to be
1235 deleted */
1236 slot = page_dir_get_nth_slot(page, slot_no);
1237 n_owned = page_dir_slot_get_n_owned(slot);
1238 page_dir_slot_set_n_owned(slot, page_zip, 0);
1239
1240 /* 2. Update the n_owned value of the first non-deleted slot */
1241
1242 slot = page_dir_get_nth_slot(page, slot_no + 1);
1243 page_dir_slot_set_n_owned(slot, page_zip,
1244 n_owned + page_dir_slot_get_n_owned(slot));
1245
1246 /* 3. Destroy the slot by copying slots */
1247 for (i = slot_no + 1; i < n_slots; i++) {
1248 rec_t *rec = (rec_t *)page_dir_slot_get_rec(page_dir_get_nth_slot(page, i));
1249 page_dir_slot_set_rec(page_dir_get_nth_slot(page, i - 1), rec);
1250 }
1251
1252 /* 4. Zero out the last slot, which will be removed */
1253 mach_write_to_2(page_dir_get_nth_slot(page, n_slots - 1), 0);
1254
1255 /* 5. Update the page header */
1256 page_header_set_field(page, page_zip, PAGE_N_DIR_SLOTS, n_slots - 1);
1257 }
1258
1259 /** Used to add n slots to the directory. Does not set the record pointers
1260 in the added slots or update n_owned values: this is the responsibility
1261 of the caller. */
1262 static inline void page_dir_add_slot(
1263 page_t *page, /*!< in/out: the index page */
1264 page_zip_des_t *page_zip, /*!< in/out: comprssed page, or NULL */
1265 ulint start) /*!< in: the slot above which the new slots
1266 are added */
1267 {
1268 page_dir_slot_t *slot;
1269 ulint n_slots;
1270
1271 n_slots = page_dir_get_n_slots(page);
1272
1273 ut_ad(start < n_slots - 1);
1274
1275 /* Update the page header */
1276 page_dir_set_n_slots(page, page_zip, n_slots + 1);
1277
1278 /* Move slots up */
1279 slot = page_dir_get_nth_slot(page, n_slots);
1280 memmove(slot, slot + PAGE_DIR_SLOT_SIZE,
1281 (n_slots - 1 - start) * PAGE_DIR_SLOT_SIZE);
1282 }
1283
1284 /** Splits a directory slot which owns too many records.
1285 @param[in,out] page Index page
1286 @param[in,out] page_zip Compressed page whose uncompressed part will be written,
1287 or null
1288 @param[in] slot_no The directory slot */
1289 void page_dir_split_slot(page_t *page, page_zip_des_t *page_zip,
1290 ulint slot_no) {
1291 rec_t *rec;
1292 page_dir_slot_t *new_slot;
1293 page_dir_slot_t *prev_slot;
1294 page_dir_slot_t *slot;
1295 ulint i;
1296 ulint n_owned;
1297
1298 ut_ad(page);
1299 ut_ad(!page_zip || page_is_comp(page));
1300 ut_ad(slot_no > 0);
1301
1302 slot = page_dir_get_nth_slot(page, slot_no);
1303
1304 n_owned = page_dir_slot_get_n_owned(slot);
1305 ut_ad(n_owned == PAGE_DIR_SLOT_MAX_N_OWNED + 1);
1306
1307 /* 1. We loop to find a record approximately in the middle of the
1308 records owned by the slot. */
1309
1310 prev_slot = page_dir_get_nth_slot(page, slot_no - 1);
1311 rec = (rec_t *)page_dir_slot_get_rec(prev_slot);
1312
1313 for (i = 0; i < n_owned / 2; i++) {
1314 rec = page_rec_get_next(rec);
1315 }
1316
1317 ut_ad(n_owned / 2 >= PAGE_DIR_SLOT_MIN_N_OWNED);
1318
1319 /* 2. We add one directory slot immediately below the slot to be
1320 split. */
1321
1322 page_dir_add_slot(page, page_zip, slot_no - 1);
1323
1324 /* The added slot is now number slot_no, and the old slot is
1325 now number slot_no + 1 */
1326
1327 new_slot = page_dir_get_nth_slot(page, slot_no);
1328 slot = page_dir_get_nth_slot(page, slot_no + 1);
1329
1330 /* 3. We store the appropriate values to the new slot. */
1331
1332 page_dir_slot_set_rec(new_slot, rec);
1333 page_dir_slot_set_n_owned(new_slot, page_zip, n_owned / 2);
1334
1335 /* 4. Finally, we update the number of records field of the
1336 original slot */
1337
1338 page_dir_slot_set_n_owned(slot, page_zip, n_owned - (n_owned / 2));
1339 }
1340
1341 /** Tries to balance the given directory slot with too few records with the
1342 upper neighbor, so that there are at least the minimum number of records owned
1343 by the slot; this may result in the merging of two slots.
1344 @param[in,out] page Index page
1345 @param[in,out] page_zip Compressed page, or null
1346 @param[in] slot_no The directory slot */
1347 void page_dir_balance_slot(page_t *page, page_zip_des_t *page_zip,
1348 ulint slot_no) {
1349 page_dir_slot_t *slot;
1350 page_dir_slot_t *up_slot;
1351 ulint n_owned;
1352 ulint up_n_owned;
1353 rec_t *old_rec;
1354 rec_t *new_rec;
1355
1356 ut_ad(page);
1357 ut_ad(!page_zip || page_is_comp(page));
1358 ut_ad(slot_no > 0);
1359
1360 slot = page_dir_get_nth_slot(page, slot_no);
1361
1362 /* The last directory slot cannot be balanced with the upper
1363 neighbor, as there is none. */
1364
1365 if (UNIV_UNLIKELY(slot_no == page_dir_get_n_slots(page) - 1)) {
1366 return;
1367 }
1368
1369 up_slot = page_dir_get_nth_slot(page, slot_no + 1);
1370
1371 n_owned = page_dir_slot_get_n_owned(slot);
1372 up_n_owned = page_dir_slot_get_n_owned(up_slot);
1373
1374 ut_ad(n_owned == PAGE_DIR_SLOT_MIN_N_OWNED - 1);
1375
1376 /* If the upper slot has the minimum value of n_owned, we will merge
1377 the two slots, therefore we assert: */
1378 static_assert(2 * PAGE_DIR_SLOT_MIN_N_OWNED - 1 <= PAGE_DIR_SLOT_MAX_N_OWNED);
1379
1380 if (up_n_owned > PAGE_DIR_SLOT_MIN_N_OWNED) {
1381 /* In this case we can just transfer one record owned
1382 by the upper slot to the property of the lower slot */
1383 old_rec = (rec_t *)page_dir_slot_get_rec(slot);
1384
1385 if (page_is_comp(page)) {
1386 new_rec = rec_get_next_ptr(old_rec, true);
1387
1388 rec_set_n_owned_new(old_rec, page_zip, 0);
1389 rec_set_n_owned_new(new_rec, page_zip, n_owned + 1);
1390 } else {
1391 new_rec = rec_get_next_ptr(old_rec, false);
1392
1393 rec_set_n_owned_old(old_rec, 0);
1394 rec_set_n_owned_old(new_rec, n_owned + 1);
1395 }
1396
1397 page_dir_slot_set_rec(slot, new_rec);
1398
1399 page_dir_slot_set_n_owned(up_slot, page_zip, up_n_owned - 1);
1400 } else {
1401 /* In this case we may merge the two slots */
1402 page_dir_delete_slot(page, page_zip, slot_no);
1403 }
1404 }
1405
1406 /** Returns the nth record of the record list.
1407 This is the inverse function of page_rec_get_n_recs_before().
1408 @return nth record */
1409 const rec_t *page_rec_get_nth_const(const page_t *page, /*!< in: page */
1410 ulint nth) /*!< in: nth record */
1411 {
1412 const page_dir_slot_t *slot;
1413 ulint i;
1414 ulint n_owned;
1415 const rec_t *rec;
1416
1417 if (nth == 0) {
1418 return (page_get_infimum_rec(page));
1419 }
1420
1421 ut_ad(nth < UNIV_PAGE_SIZE / (REC_N_NEW_EXTRA_BYTES + 1));
1422
1423 for (i = 0;; i++) {
1424 slot = page_dir_get_nth_slot(page, i);
1425 n_owned = page_dir_slot_get_n_owned(slot);
1426
1427 if (n_owned > nth) {
1428 break;
1429 } else {
1430 nth -= n_owned;
1431 }
1432 }
1433
1434 ut_ad(i > 0);
1435 slot = page_dir_get_nth_slot(page, i - 1);
1436 rec = page_dir_slot_get_rec(slot);
1437
1438 if (page_is_comp(page)) {
1439 do {
1440 rec = page_rec_get_next_low(rec, true);
1441 ut_ad(rec);
1442 } while (nth--);
1443 } else {
1444 do {
1445 rec = page_rec_get_next_low(rec, false);
1446 ut_ad(rec);
1447 } while (nth--);
1448 }
1449
1450 return (rec);
1451 }
1452
1453 /** Returns the number of records before the given record in chain.
1454 The number includes infimum and supremum records.
1455 @return number of records */
1456 ulint page_rec_get_n_recs_before(
1457 const rec_t *rec) /*!< in: the physical record */
1458 {
1459 const page_dir_slot_t *slot;
1460 const rec_t *slot_rec;
1461 const page_t *page;
1462 ulint i;
1463 lint n = 0;
1464
1465 ut_ad(page_rec_check(rec));
1466
1467 page = page_align(rec);
1468 if (page_is_comp(page)) {
1469 while (rec_get_n_owned_new(rec) == 0) {
1470 rec = rec_get_next_ptr_const(rec, true);
1471 n--;
1472 }
1473
1474 for (i = 0;; i++) {
1475 slot = page_dir_get_nth_slot(page, i);
1476 slot_rec = page_dir_slot_get_rec(slot);
1477
1478 n += rec_get_n_owned_new(slot_rec);
1479
1480 if (rec == slot_rec) {
1481 break;
1482 }
1483 }
1484 } else {
1485 while (rec_get_n_owned_old(rec) == 0) {
1486 rec = rec_get_next_ptr_const(rec, false);
1487 n--;
1488 }
1489
1490 for (i = 0;; i++) {
1491 slot = page_dir_get_nth_slot(page, i);
1492 slot_rec = page_dir_slot_get_rec(slot);
1493
1494 n += rec_get_n_owned_old(slot_rec);
1495
1496 if (rec == slot_rec) {
1497 break;
1498 }
1499 }
1500 }
1501
1502 n--;
1503
1504 ut_ad(n >= 0);
1505 ut_ad((ulong)n < UNIV_PAGE_SIZE / (REC_N_NEW_EXTRA_BYTES + 1));
1506
1507 return ((ulint)n);
1508 }
1509
1510 #ifndef UNIV_HOTBACKUP
1511 /** Prints record contents including the data relevant only in
1512 the index page context.
1513 @param[in] rec Physical record
1514 @param[in] offsets Record descriptor */
1515 void page_rec_print(const rec_t *rec, const ulint *offsets) {
1516 ut_a(!page_rec_is_comp(rec) == !rec_offs_comp(offsets));
1517 rec_print_new(stderr, rec, offsets);
1518 if (page_rec_is_comp(rec)) {
1519 ib::info(ER_IB_MSG_863) << "n_owned: " << rec_get_n_owned_new(rec)
1520 << "; heap_no: " << rec_get_heap_no_new(rec)
1521 << "; next rec: " << rec_get_next_offs(rec, true);
1522 } else {
1523 ib::info(ER_IB_MSG_864) << "n_owned: " << rec_get_n_owned_old(rec)
1524 << "; heap_no: " << rec_get_heap_no_old(rec)
1525 << "; next rec: " << rec_get_next_offs(rec, false);
1526 }
1527
1528 page_rec_check(rec);
1529 rec_validate(rec, offsets);
1530 }
1531
1532 #ifdef UNIV_BTR_PRINT
1533 /** This is used to print the contents of the directory for
1534 debugging purposes. */
1535 void page_dir_print(page_t *page, /*!< in: index page */
1536 ulint pr_n) /*!< in: print n first and n last entries */
1537 {
1538 ulint n;
1539 ulint i;
1540 page_dir_slot_t *slot;
1541
1542 n = page_dir_get_n_slots(page);
1543
1544 fprintf(stderr,
1545 "--------------------------------\n"
1546 "PAGE DIRECTORY\n"
1547 "Page address %p\n"
1548 "Directory stack top at offs: %lu; number of slots: %lu\n",
1549 page, (ulong)page_offset(page_dir_get_nth_slot(page, n - 1)),
1550 (ulong)n);
1551 for (i = 0; i < n; i++) {
1552 slot = page_dir_get_nth_slot(page, i);
1553 if ((i == pr_n) && (i < n - pr_n)) {
1554 fputs(" ... \n", stderr);
1555 }
1556 if ((i < pr_n) || (i >= n - pr_n)) {
1557 fprintf(stderr,
1558 "Contents of slot: %lu: n_owned: %lu,"
1559 " rec offs: %lu\n",
1560 (ulong)i, (ulong)page_dir_slot_get_n_owned(slot),
1561 (ulong)page_offset(page_dir_slot_get_rec(slot)));
1562 }
1563 }
1564 fprintf(stderr,
1565 "Total of %lu records\n"
1566 "--------------------------------\n",
1567 (ulong)(PAGE_HEAP_NO_USER_LOW + page_get_n_recs(page)));
1568 }
1569
1570 /** This is used to print the contents of the page record list for
1571 debugging purposes. */
1572 void page_print_list(
1573 buf_block_t *block, /*!< in: index page */
1574 dict_index_t *index, /*!< in: dictionary index of the page */
1575 ulint pr_n) /*!< in: print n first and n last entries */
1576 {
1577 page_t *page = block->frame;
1578 page_cur_t cur;
1579 ulint count;
1580 ulint n_recs;
1581 mem_heap_t *heap = NULL;
1582 ulint offsets_[REC_OFFS_NORMAL_SIZE];
1583 ulint *offsets = offsets_;
1584 rec_offs_init(offsets_);
1585
1586 ut_a(page_is_comp(page) == dict_table_is_comp(index->table));
1587
1588 fprint(stderr,
1589 "--------------------------------\n"
1590 "PAGE RECORD LIST\n"
1591 "Page address %p\n",
1592 page);
1593
1594 n_recs = page_get_n_recs(page);
1595
1596 page_cur_set_before_first(block, &cur);
1597 count = 0;
1598 for (;;) {
1599 offsets = rec_get_offsets(cur.rec, index, offsets, ULINT_UNDEFINED, &heap);
1600 page_rec_print(cur.rec, offsets);
1601
1602 if (count == pr_n) {
1603 break;
1604 }
1605 if (page_cur_is_after_last(&cur)) {
1606 break;
1607 }
1608 page_cur_move_to_next(&cur);
1609 count++;
1610 }
1611
1612 if (n_recs > 2 * pr_n) {
1613 fputs(" ... \n", stderr);
1614 }
1615
1616 while (!page_cur_is_after_last(&cur)) {
1617 page_cur_move_to_next(&cur);
1618
1619 if (count + pr_n >= n_recs) {
1620 offsets =
1621 rec_get_offsets(cur.rec, index, offsets, ULINT_UNDEFINED, &heap);
1622 page_rec_print(cur.rec, offsets);
1623 }
1624 count++;
1625 }
1626
1627 fprintf(stderr,
1628 "Total of %lu records \n"
1629 "--------------------------------\n",
1630 (ulong)(count + 1));
1631
1632 if (UNIV_LIKELY_NULL(heap)) {
1633 mem_heap_free(heap);
1634 }
1635 }
1636
1637 /** Prints the info in a page header. */
1638 void page_header_print(const page_t *page) {
1639 fprintf(stderr,
1640 "--------------------------------\n"
1641 "PAGE HEADER INFO\n"
1642 "Page address %p, n records %lu (%s)\n"
1643 "n dir slots %lu, heap top %lu\n"
1644 "Page n heap %lu, free %lu, garbage %lu\n"
1645 "Page last insert %lu, direction %lu, n direction %lu\n",
1646 page, (ulong)page_header_get_field(page, PAGE_N_RECS),
1647 page_is_comp(page) ? "compact format" : "original format",
1648 (ulong)page_header_get_field(page, PAGE_N_DIR_SLOTS),
1649 (ulong)page_header_get_field(page, PAGE_HEAP_TOP),
1650 (ulong)page_dir_get_n_heap(page),
1651 (ulong)page_header_get_field(page, PAGE_FREE),
1652 (ulong)page_header_get_field(page, PAGE_GARBAGE),
1653 (ulong)page_header_get_field(page, PAGE_LAST_INSERT),
1654 (ulong)page_header_get_field(page, PAGE_DIRECTION),
1655 (ulong)page_header_get_field(page, PAGE_N_DIRECTION));
1656 }
1657
1658 /** This is used to print the contents of the page for
1659 debugging purposes. */
1660 void page_print(buf_block_t *block, /*!< in: index page */
1661 dict_index_t *index, /*!< in: dictionary index of the page */
1662 ulint dn, /*!< in: print dn first and last entries
1663 in directory */
1664 ulint rn) /*!< in: print rn first and last records
1665 in directory */
1666 {
1667 page_t *page = block->frame;
1668
1669 page_header_print(page);
1670 page_dir_print(page, dn);
1671 page_print_list(block, index, rn);
1672 }
1673 #endif /* UNIV_BTR_PRINT */
1674 #endif /* !UNIV_HOTBACKUP */
1675
1676 /** The following is used to validate a record on a page. This function
1677 differs from rec_validate as it can also check the n_owned field and
1678 the heap_no field.
1679 @return true if ok */
1680 bool page_rec_validate(
1681 const rec_t *rec, /*!< in: physical record */
1682 const ulint *offsets) /*!< in: array returned by rec_get_offsets() */
1683 {
1684 ulint n_owned;
1685 ulint heap_no;
1686 const page_t *page;
1687
1688 page = page_align(rec);
1689 ut_a(!page_is_comp(page) == !rec_offs_comp(offsets));
1690
1691 page_rec_check(rec);
1692 rec_validate(rec, offsets);
1693
1694 if (page_rec_is_comp(rec)) {
1695 n_owned = rec_get_n_owned_new(rec);
1696 heap_no = rec_get_heap_no_new(rec);
1697
1698 /* Only one of the bit (INSTANT or VERSION) could be set */
1699 if (rec_get_instant_flag_new(rec) && rec_new_is_versioned(rec)) {
1700 ib::error(ER_CHECK_TABLE_INSTANT_VERSION_BIT_SET);
1701 return false;
1702 }
1703 } else {
1704 n_owned = rec_get_n_owned_old(rec);
1705 heap_no = rec_get_heap_no_old(rec);
1706 }
1707
1708 if (UNIV_UNLIKELY(!(n_owned <= PAGE_DIR_SLOT_MAX_N_OWNED))) {
1709 ib::warn(ER_IB_MSG_865) << "Dir slot of rec " << page_offset(rec)
1710 << ", n owned too big " << n_owned;
1711 return false;
1712 }
1713
1714 if (UNIV_UNLIKELY(!(heap_no < page_dir_get_n_heap(page)))) {
1715 ib::warn(ER_IB_MSG_866)
1716 << "Heap no of rec " << page_offset(rec) << " too big " << heap_no
1717 << " " << page_dir_get_n_heap(page);
1718 return false;
1719 }
1720
1721 return true;
1722 }
1723
1724 #ifndef UNIV_HOTBACKUP
1725 #ifdef UNIV_DEBUG
1726 /** Checks that the first directory slot points to the infimum record and
1727 the last to the supremum. This function is intended to track if the
1728 bug fixed in 4.0.14 has caused corruption to users' databases. */
1729 void page_check_dir(const page_t *page) /*!< in: index page */
1730 {
1731 ulint n_slots;
1732 ulint infimum_offs;
1733 ulint supremum_offs;
1734
1735 n_slots = page_dir_get_n_slots(page);
1736 infimum_offs = mach_read_from_2(page_dir_get_nth_slot(page, 0));
1737 supremum_offs = mach_read_from_2(page_dir_get_nth_slot(page, n_slots - 1));
1738
1739 if (UNIV_UNLIKELY(!page_rec_is_infimum_low(infimum_offs))) {
1740 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_867)
1741 << "Page directory corruption: infimum not"
1742 " pointed to";
1743 }
1744
1745 if (UNIV_UNLIKELY(!page_rec_is_supremum_low(supremum_offs))) {
1746 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_868)
1747 << "Page directory corruption: supremum not"
1748 " pointed to";
1749 }
1750 }
1751 #endif /* UNIV_DEBUG */
1752 #endif /* !UNIV_HOTBACKUP */
1753
1754 /** This function checks the consistency of an index page when we do not
1755 know the index. This is also resilient so that this should never crash
1756 even if the page is total garbage.
1757 @return true if ok */
1758 bool page_simple_validate_old(
1759 const page_t *page) /*!< in: index page in ROW_FORMAT=REDUNDANT */
1760 {
1761 const page_dir_slot_t *slot;
1762 ulint slot_no;
1763 ulint n_slots;
1764 const rec_t *rec;
1765 const byte *rec_heap_top;
1766 ulint count;
1767 ulint own_count;
1768 bool ret = false;
1769
1770 ut_a(!page_is_comp(page));
1771
1772 /* Check first that the record heap and the directory do not
1773 overlap. */
1774
1775 n_slots = page_dir_get_n_slots(page);
1776
1777 if (UNIV_UNLIKELY(n_slots > UNIV_PAGE_SIZE / 4)) {
1778 ib::error(ER_IB_MSG_869)
1779 << "Nonsensical number " << n_slots << " of page dir slots";
1780
1781 goto func_exit;
1782 }
1783
1784 rec_heap_top = page_header_get_ptr(page, PAGE_HEAP_TOP);
1785
1786 if (UNIV_UNLIKELY(rec_heap_top > page_dir_get_nth_slot(page, n_slots - 1))) {
1787 ib::error(ER_IB_MSG_870)
1788 << "Record heap and dir overlap on a page, heap top "
1789 << page_header_get_field(page, PAGE_HEAP_TOP) << ", dir "
1790 << page_offset(page_dir_get_nth_slot(page, n_slots - 1));
1791
1792 goto func_exit;
1793 }
1794
1795 /* Validate the record list in a loop checking also that it is
1796 consistent with the page record directory. */
1797
1798 count = 0;
1799 own_count = 1;
1800 slot_no = 0;
1801 slot = page_dir_get_nth_slot(page, slot_no);
1802
1803 rec = page_get_infimum_rec(page);
1804
1805 for (;;) {
1806 if (UNIV_UNLIKELY(rec > rec_heap_top)) {
1807 ib::error(ER_IB_MSG_871)
1808 << "Record " << (rec - page) << " is above rec heap top "
1809 << (rec_heap_top - page);
1810
1811 goto func_exit;
1812 }
1813
1814 if (UNIV_UNLIKELY(rec_get_n_owned_old(rec))) {
1815 /* This is a record pointed to by a dir slot */
1816 if (UNIV_UNLIKELY(rec_get_n_owned_old(rec) != own_count)) {
1817 ib::error(ER_IB_MSG_872)
1818 << "Wrong owned count " << rec_get_n_owned_old(rec) << ", "
1819 << own_count << ", rec " << (rec - page);
1820
1821 goto func_exit;
1822 }
1823
1824 if (UNIV_UNLIKELY(page_dir_slot_get_rec(slot) != rec)) {
1825 ib::error(ER_IB_MSG_873) << "Dir slot does not point"
1826 " to right rec "
1827 << (rec - page);
1828
1829 goto func_exit;
1830 }
1831
1832 own_count = 0;
1833
1834 if (!page_rec_is_supremum(rec)) {
1835 slot_no++;
1836 slot = page_dir_get_nth_slot(page, slot_no);
1837 }
1838 }
1839
1840 if (page_rec_is_supremum(rec)) {
1841 break;
1842 }
1843
1844 if (UNIV_UNLIKELY(rec_get_next_offs(rec, false) < FIL_PAGE_DATA ||
1845 rec_get_next_offs(rec, false) >= UNIV_PAGE_SIZE)) {
1846 ib::error(ER_IB_MSG_874)
1847 << "Next record offset nonsensical " << rec_get_next_offs(rec, false)
1848 << " for rec " << (rec - page);
1849
1850 goto func_exit;
1851 }
1852
1853 count++;
1854
1855 if (UNIV_UNLIKELY(count > UNIV_PAGE_SIZE)) {
1856 ib::error(ER_IB_MSG_875) << "Page record list appears"
1857 " to be circular "
1858 << count;
1859 goto func_exit;
1860 }
1861
1862 rec = page_rec_get_next_const(rec);
1863 own_count++;
1864 }
1865
1866 if (UNIV_UNLIKELY(rec_get_n_owned_old(rec) == 0)) {
1867 ib::error(ER_IB_MSG_876) << "n owned is zero in a supremum rec";
1868
1869 goto func_exit;
1870 }
1871
1872 if (UNIV_UNLIKELY(slot_no != n_slots - 1)) {
1873 ib::error(ER_IB_MSG_877)
1874 << "n slots wrong " << slot_no << ", " << (n_slots - 1);
1875 goto func_exit;
1876 }
1877
1878 if (UNIV_UNLIKELY(page_header_get_field(page, PAGE_N_RECS) +
1879 PAGE_HEAP_NO_USER_LOW !=
1880 count + 1)) {
1881 ib::error(ER_IB_MSG_878)
1882 << "n recs wrong "
1883 << page_header_get_field(page, PAGE_N_RECS) + PAGE_HEAP_NO_USER_LOW
1884 << " " << (count + 1);
1885
1886 goto func_exit;
1887 }
1888
1889 /* Check then the free list */
1890 rec = page_header_get_ptr(page, PAGE_FREE);
1891
1892 while (rec != nullptr) {
1893 if (UNIV_UNLIKELY(rec < page + FIL_PAGE_DATA ||
1894 rec >= page + UNIV_PAGE_SIZE)) {
1895 ib::error(ER_IB_MSG_879) << "Free list record has"
1896 " a nonsensical offset "
1897 << (rec - page);
1898
1899 goto func_exit;
1900 }
1901
1902 if (UNIV_UNLIKELY(rec > rec_heap_top)) {
1903 ib::error(ER_IB_MSG_880)
1904 << "Free list record " << (rec - page) << " is above rec heap top "
1905 << (rec_heap_top - page);
1906
1907 goto func_exit;
1908 }
1909
1910 count++;
1911
1912 if (UNIV_UNLIKELY(count > UNIV_PAGE_SIZE)) {
1913 ib::error(ER_IB_MSG_881) << "Page free list appears"
1914 " to be circular "
1915 << count;
1916 goto func_exit;
1917 }
1918
1919 rec = page_rec_get_next_const(rec);
1920 }
1921
1922 if (UNIV_UNLIKELY(page_dir_get_n_heap(page) != count + 1)) {
1923 ib::error(ER_IB_MSG_882) << "N heap is wrong " << page_dir_get_n_heap(page)
1924 << ", " << (count + 1);
1925
1926 goto func_exit;
1927 }
1928
1929 ret = true;
1930
1931 func_exit:
1932 return (ret);
1933 }
1934
1935 /** This function checks the consistency of an index page when we do not
1936 know the index. This is also resilient so that this should never crash
1937 even if the page is total garbage.
1938 @return true if ok */
1939 bool page_simple_validate_new(
1940 const page_t *page) /*!< in: index page in ROW_FORMAT!=REDUNDANT */
1941 {
1942 const page_dir_slot_t *slot;
1943 ulint slot_no;
1944 ulint n_slots;
1945 const rec_t *rec;
1946 const byte *rec_heap_top;
1947 ulint count;
1948 ulint own_count;
1949 bool ret = false;
1950
1951 ut_a(page_is_comp(page));
1952
1953 /* Check first that the record heap and the directory do not
1954 overlap. */
1955
1956 n_slots = page_dir_get_n_slots(page);
1957
1958 if (UNIV_UNLIKELY(n_slots > UNIV_PAGE_SIZE / 4)) {
1959 ib::error(ER_IB_MSG_883)
1960 << "Nonsensical number " << n_slots << " of page dir slots";
1961
1962 goto func_exit;
1963 }
1964
1965 rec_heap_top = page_header_get_ptr(page, PAGE_HEAP_TOP);
1966
1967 if (UNIV_UNLIKELY(rec_heap_top > page_dir_get_nth_slot(page, n_slots - 1))) {
1968 ib::error(ER_IB_MSG_884)
1969 << "Record heap and dir overlap on a page,"
1970 " heap top "
1971 << page_header_get_field(page, PAGE_HEAP_TOP) << ", dir "
1972 << page_offset(page_dir_get_nth_slot(page, n_slots - 1));
1973
1974 goto func_exit;
1975 }
1976
1977 /* Validate the record list in a loop checking also that it is
1978 consistent with the page record directory. */
1979
1980 count = 0;
1981 own_count = 1;
1982 slot_no = 0;
1983 slot = page_dir_get_nth_slot(page, slot_no);
1984
1985 rec = page_get_infimum_rec(page);
1986
1987 for (;;) {
1988 if (UNIV_UNLIKELY(rec > rec_heap_top)) {
1989 ib::error(ER_IB_MSG_885)
1990 << "Record " << page_offset(rec) << " is above rec heap top "
1991 << page_offset(rec_heap_top);
1992
1993 goto func_exit;
1994 }
1995
1996 if (UNIV_UNLIKELY(rec_get_n_owned_new(rec))) {
1997 /* This is a record pointed to by a dir slot */
1998 if (UNIV_UNLIKELY(rec_get_n_owned_new(rec) != own_count)) {
1999 ib::error(ER_IB_MSG_886)
2000 << "Wrong owned count " << rec_get_n_owned_new(rec) << ", "
2001 << own_count << ", rec " << page_offset(rec);
2002
2003 goto func_exit;
2004 }
2005
2006 if (UNIV_UNLIKELY(page_dir_slot_get_rec(slot) != rec)) {
2007 ib::error(ER_IB_MSG_887) << "Dir slot does not point"
2008 " to right rec "
2009 << page_offset(rec);
2010
2011 goto func_exit;
2012 }
2013
2014 own_count = 0;
2015
2016 if (!page_rec_is_supremum(rec)) {
2017 slot_no++;
2018 slot = page_dir_get_nth_slot(page, slot_no);
2019 }
2020 }
2021
2022 if (page_rec_is_supremum(rec)) {
2023 break;
2024 }
2025
2026 if (UNIV_UNLIKELY(rec_get_next_offs(rec, true) < FIL_PAGE_DATA ||
2027 rec_get_next_offs(rec, true) >= UNIV_PAGE_SIZE)) {
2028 ib::error(ER_IB_MSG_888)
2029 << "Next record offset nonsensical " << rec_get_next_offs(rec, true)
2030 << " for rec " << page_offset(rec);
2031
2032 goto func_exit;
2033 }
2034
2035 count++;
2036
2037 if (UNIV_UNLIKELY(count > UNIV_PAGE_SIZE)) {
2038 ib::error(ER_IB_MSG_889) << "Page record list appears to be"
2039 " circular "
2040 << count;
2041 goto func_exit;
2042 }
2043
2044 rec = page_rec_get_next_const(rec);
2045 own_count++;
2046 }
2047
2048 if (UNIV_UNLIKELY(rec_get_n_owned_new(rec) == 0)) {
2049 ib::error(ER_IB_MSG_890) << "n owned is zero in a supremum rec";
2050
2051 goto func_exit;
2052 }
2053
2054 if (UNIV_UNLIKELY(slot_no != n_slots - 1)) {
2055 ib::error(ER_IB_MSG_891)
2056 << "n slots wrong " << slot_no << ", " << (n_slots - 1);
2057 goto func_exit;
2058 }
2059
2060 if (UNIV_UNLIKELY(page_header_get_field(page, PAGE_N_RECS) +
2061 PAGE_HEAP_NO_USER_LOW !=
2062 count + 1)) {
2063 ib::error(ER_IB_MSG_892)
2064 << "n recs wrong "
2065 << page_header_get_field(page, PAGE_N_RECS) + PAGE_HEAP_NO_USER_LOW
2066 << " " << (count + 1);
2067
2068 goto func_exit;
2069 }
2070
2071 /* Check then the free list */
2072 rec = page_header_get_ptr(page, PAGE_FREE);
2073
2074 while (rec != nullptr) {
2075 if (UNIV_UNLIKELY(rec < page + FIL_PAGE_DATA ||
2076 rec >= page + UNIV_PAGE_SIZE)) {
2077 ib::error(ER_IB_MSG_893) << "Free list record has"
2078 " a nonsensical offset "
2079 << page_offset(rec);
2080
2081 goto func_exit;
2082 }
2083
2084 if (UNIV_UNLIKELY(rec > rec_heap_top)) {
2085 ib::error(ER_IB_MSG_894)
2086 << "Free list record " << page_offset(rec)
2087 << " is above rec heap top " << page_offset(rec_heap_top);
2088
2089 goto func_exit;
2090 }
2091
2092 count++;
2093
2094 if (UNIV_UNLIKELY(count > UNIV_PAGE_SIZE)) {
2095 ib::error(ER_IB_MSG_895) << "Page free list appears to be"
2096 " circular "
2097 << count;
2098 goto func_exit;
2099 }
2100
2101 rec = page_rec_get_next_const(rec);
2102 }
2103
2104 if (UNIV_UNLIKELY(page_dir_get_n_heap(page) != count + 1)) {
2105 ib::error(ER_IB_MSG_896) << "N heap is wrong " << page_dir_get_n_heap(page)
2106 << ", " << (count + 1);
2107
2108 goto func_exit;
2109 }
2110
2111 ret = true;
2112
2113 func_exit:
2114 return (ret);
2115 }
2116
2117 /** This function checks if the page in which record is present is a
2118 non-leaf node of a spatial index.
2119 param[in] rec Btree record
2120 param[in] index index
2121 @return true if ok */
2122 bool page_is_spatial_non_leaf(const rec_t *rec, dict_index_t *index) {
2123 return (dict_index_is_spatial(index) && !page_is_leaf(page_align(rec)));
2124 }
2125
2126 bool page_validate(const page_t *page, dict_index_t *index) {
2127 const page_dir_slot_t *slot;
2128 mem_heap_t *heap;
2129 byte *buf;
2130 ulint count;
2131 ulint own_count;
2132 ulint rec_own_count;
2133 ulint slot_no;
2134 ulint data_size;
2135 const rec_t *rec;
2136 const rec_t *old_rec = nullptr;
2137 ulint offs;
2138 ulint n_slots;
2139 bool ret = false;
2140 ulint i;
2141 ulint *offsets = nullptr;
2142 ulint *old_offsets = nullptr;
2143 const page_no_t page_no = page_get_page_no(page);
2144 bool is_first_non_leaf_page = false;
2145
2146 #ifdef UNIV_GIS_DEBUG
2147 if (dict_index_is_spatial(index)) {
2148 fprintf(stderr, "Page no: %lu\n", page_no);
2149 }
2150 #endif /* UNIV_DEBUG */
2151
2152 if (UNIV_UNLIKELY(page_is_comp(page) != dict_table_is_comp(index->table))) {
2153 ib::error(ER_IB_MSG_897) << "'compact format' flag mismatch";
2154 goto func_exit2;
2155 }
2156 if (page_is_comp(page)) {
2157 if (UNIV_UNLIKELY(!page_simple_validate_new(page))) {
2158 goto func_exit2;
2159 }
2160 } else {
2161 if (UNIV_UNLIKELY(!page_simple_validate_old(page))) {
2162 goto func_exit2;
2163 }
2164 }
2165
2166 /* Multiple transactions cannot simultaneously operate on the
2167 same temp-table in parallel.
2168 max_trx_id is ignored for temp tables because it not required
2169 for MVCC. */
2170 #ifndef UNIV_HOTBACKUP
2171 if (dict_index_is_sec_or_ibuf(index) && !index->table->is_temporary() &&
2172 page_is_leaf(page) && !page_is_empty(page)) {
2173 trx_id_t max_trx_id = page_get_max_trx_id(page);
2174 /* This will be 0 during recv_apply_hashed_log_recs(),
2175 because the transaction system has not been initialized yet */
2176 trx_id_t sys_next_trx_id_or_no = trx_sys_get_next_trx_id_or_no();
2177
2178 if (max_trx_id == 0 ||
2179 (sys_next_trx_id_or_no != 0 && max_trx_id >= sys_next_trx_id_or_no)) {
2180 ib::error(ER_IB_MSG_898)
2181 << "PAGE_MAX_TRX_ID out of bounds: " << max_trx_id << ", "
2182 << sys_next_trx_id_or_no;
2183 goto func_exit2;
2184 }
2185 }
2186 #endif /* !UNIV_HOTBACKUP */
2187
2188 heap = mem_heap_create(UNIV_PAGE_SIZE + 200, UT_LOCATION_HERE);
2189
2190 /* The following buffer is used to check that the
2191 records in the page record heap do not overlap */
2192
2193 buf = static_cast<byte *>(mem_heap_zalloc(heap, UNIV_PAGE_SIZE));
2194
2195 /* Check first that the record heap and the directory do not
2196 overlap. */
2197
2198 n_slots = page_dir_get_n_slots(page);
2199
2200 if (UNIV_UNLIKELY(!(page_header_get_ptr(page, PAGE_HEAP_TOP) <=
2201 page_dir_get_nth_slot(page, n_slots - 1)))) {
2202 ib::warn(ER_IB_MSG_899)
2203 << "Record heap and dir overlap on space " << page_get_space_id(page)
2204 << " page " << page_no << " index " << index->name << ", "
2205 << page_header_get_ptr(page, PAGE_HEAP_TOP) << ", "
2206 << page_dir_get_nth_slot(page, n_slots - 1);
2207
2208 goto func_exit;
2209 }
2210
2211 /* Validate the record list in a loop checking also that
2212 it is consistent with the directory. */
2213 count = 0;
2214 data_size = 0;
2215 own_count = 1;
2216 slot_no = 0;
2217 slot = page_dir_get_nth_slot(page, slot_no);
2218 is_first_non_leaf_page =
2219 !page_is_leaf(page) && btr_is_first_page_on_level(page);
2220
2221 rec = page_get_infimum_rec(page);
2222
2223 for (;;) {
2224 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
2225 UT_LOCATION_HERE, &heap);
2226
2227 if (page_is_comp(page) && page_rec_is_user_rec(rec) &&
2228 UNIV_UNLIKELY(rec_get_node_ptr_flag(rec) == page_is_leaf(page))) {
2229 ib::error(ER_IB_MSG_900) << "'node_ptr' flag mismatch";
2230 goto func_exit;
2231 }
2232
2233 if (UNIV_UNLIKELY(!page_rec_validate(rec, offsets))) {
2234 goto func_exit;
2235 }
2236
2237 DBUG_EXECUTE_IF(
2238 "check_table_set_wrong_min_bit",
2239 if (page_rec_is_user_rec(rec) &&
2240 !(is_first_non_leaf_page && page_rec_is_first(rec, page))) {
2241 ut_ad(!rec_is_min_rec_flag_set(rec, page_is_comp(page)));
2242 ut_ad(page_is_comp(page));
2243 rec_set_info_bits_new(
2244 const_cast<rec_t *>(rec),
2245 rec_get_info_bits(rec, true) | REC_INFO_MIN_REC_FLAG);
2246 ib::info() << "Setting wrong min bit to " << count << "nd record";
2247 });
2248
2249 DBUG_EXECUTE_IF(
2250 "check_table_reset_correct_min_bit",
2251 if (page_rec_is_user_rec(rec) &&
2252 (is_first_non_leaf_page && page_rec_is_first(rec, page))) {
2253 ut_ad(rec_is_min_rec_flag_set(rec, page_is_comp(page)));
2254 ut_ad(page_is_comp(page));
2255 rec_set_info_bits_new(
2256 const_cast<rec_t *>(rec),
2257 rec_get_info_bits(rec, true) & ~REC_INFO_MIN_REC_FLAG);
2258 ib::info() << "Resetting correct min bit to " << count << "st record";
2259 });
2260
2261 /* REC_INFO_MIN_REC_FLAG must be set only for first record on first non-leaf
2262 page on a level. */
2263 if (rec_is_min_rec_flag_set(rec, page_is_comp(page))) {
2264 if (!page_rec_is_user_rec(rec) ||
2265 !(is_first_non_leaf_page && page_rec_is_first(rec, page))) {
2266 ib::error(ER_CHECK_TABLE_MIN_REC_FLAG_SET, (unsigned long int)page_no,
2267 (unsigned long int)btr_page_get_level(page), index->name(),
2268 index->table_name);
2269 DBUG_EXECUTE_IF(
2270 "check_table_set_wrong_min_bit", ut_ad(page_is_comp(page));
2271 rec_set_info_bits_new(
2272 const_cast<rec_t *>(rec),
2273 rec_get_info_bits(rec, true) & ~REC_INFO_MIN_REC_FLAG););
2274 goto func_exit;
2275 }
2276 } else {
2277 if (is_first_non_leaf_page && page_rec_is_user_rec(rec) &&
2278 page_rec_is_first(rec, page)) {
2279 ib::error(ER_CHECK_TABLE_MIN_REC_FLAG_NOT_SET,
2280 (unsigned long int)page_no,
2281 (unsigned long int)btr_page_get_level(page), index->name(),
2282 index->table_name);
2283 DBUG_EXECUTE_IF(
2284 "check_table_reset_correct_min_bit", ut_ad(page_is_comp(page));
2285 rec_set_info_bits_new(
2286 const_cast<rec_t *>(rec),
2287 rec_get_info_bits(rec, true) | REC_INFO_MIN_REC_FLAG););
2288 goto func_exit;
2289 }
2290 }
2291
2292 #ifndef UNIV_HOTBACKUP
2293 /* Check that the records are in the ascending order */
2294 if (count >= PAGE_HEAP_NO_USER_LOW && !page_rec_is_supremum(rec)) {
2295 int ret = cmp_rec_rec(rec, old_rec, offsets, old_offsets, index,
2296 page_is_spatial_non_leaf(rec, index));
2297
2298 /* For spatial index, on nonleaf leavel, we
2299 allow recs to be equal. */
2300 bool rtr_equal_nodeptrs =
2301 (ret == 0 && dict_index_is_spatial(index) && !page_is_leaf(page));
2302
2303 /* For multi-value index, we allow recs to be equal. */
2304 bool multi_equal_nodeptrs = (ret == 0 && index->is_multi_value());
2305
2306 /* For multi-value index record buffered in change buffer,
2307 we allow recs to be equal. This is for the case that a single multi-value
2308 data may have values like "abc", "abc " and "abc ", etc. These are
2309 treated as unequal for user search, however, in change buffer,
2310 they are treated as binary and comparison will confirm they are equal */
2311 bool multi_value_on_ibuf = false;
2312 if (ret == 0 && dict_index_is_ibuf(index) &&
2313 ibuf_rec_has_multi_value(rec)) {
2314 ut_ad(ibuf_rec_has_multi_value(old_rec));
2315 /* Ideally, it would be better to double check that only the
2316 multi-value fields are different by trailing spaces, other fields
2317 should be the same. */
2318 multi_value_on_ibuf = true;
2319 }
2320
2321 if (ret <= 0 && !rtr_equal_nodeptrs && !multi_equal_nodeptrs &&
2322 !multi_value_on_ibuf) {
2323 ib::error(ER_IB_MSG_901) << "Records in wrong order on"
2324 " space "
2325 << page_get_space_id(page) << " page "
2326 << page_no << " index " << index->name;
2327
2328 fputs("\nInnoDB: previous record ", stderr);
2329 /* For spatial index, print the mbr info.*/
2330 if (index->type & DICT_SPATIAL) {
2331 putc('\n', stderr);
2332 rec_print_mbr_rec(stderr, old_rec, old_offsets);
2333 fputs("\nInnoDB: record ", stderr);
2334 putc('\n', stderr);
2335 rec_print_mbr_rec(stderr, rec, offsets);
2336 putc('\n', stderr);
2337 putc('\n', stderr);
2338
2339 } else {
2340 rec_print_new(stderr, old_rec, old_offsets);
2341 fputs("\nInnoDB: record ", stderr);
2342 rec_print_new(stderr, rec, offsets);
2343 putc('\n', stderr);
2344 }
2345
2346 goto func_exit;
2347 }
2348 }
2349 #else /* !UNIV_HOTBACKUP */
2350 UT_NOT_USED(old_rec);
2351 #endif /* !UNIV_HOTBACKUP */
2352
2353 if (page_rec_is_user_rec(rec)) {
2354 data_size += rec_offs_size(offsets);
2355
2356 #ifdef UNIV_GIS_DEBUG
2357 /* For spatial index, print the mbr info.*/
2358 if (index->type & DICT_SPATIAL) {
2359 rec_print_mbr_rec(stderr, rec, offsets);
2360 putc('\n', stderr);
2361 }
2362 #endif /* UNIV_GIS_DEBUG */
2363 }
2364
2365 offs = page_offset(rec_get_start(rec, offsets));
2366 i = rec_offs_size(offsets);
2367
2368 if (UNIV_UNLIKELY(offs + i >= UNIV_PAGE_SIZE)) {
2369 ib::error(ER_IB_MSG_902) << "Record offset out of bounds";
2370 goto func_exit;
2371 }
2372
2373 while (i--) {
2374 if (UNIV_UNLIKELY(buf[offs + i])) {
2375 /* No other record may overlap this */
2376 ib::error(ER_IB_MSG_903) << "Record overlaps another";
2377 goto func_exit;
2378 }
2379
2380 buf[offs + i] = 1;
2381 }
2382
2383 if (page_is_comp(page)) {
2384 rec_own_count = rec_get_n_owned_new(rec);
2385 } else {
2386 rec_own_count = rec_get_n_owned_old(rec);
2387 }
2388
2389 if (UNIV_UNLIKELY(rec_own_count)) {
2390 /* This is a record pointed to by a dir slot */
2391 if (UNIV_UNLIKELY(rec_own_count != own_count)) {
2392 ib::error(ER_IB_MSG_904)
2393 << "Wrong owned count " << rec_own_count << ", " << own_count;
2394 goto func_exit;
2395 }
2396
2397 if (page_dir_slot_get_rec(slot) != rec) {
2398 ib::error(ER_IB_MSG_905) << "Dir slot does not"
2399 " point to right rec";
2400 goto func_exit;
2401 }
2402
2403 page_dir_slot_check(slot);
2404
2405 own_count = 0;
2406 if (!page_rec_is_supremum(rec)) {
2407 slot_no++;
2408 slot = page_dir_get_nth_slot(page, slot_no);
2409 }
2410 }
2411
2412 if (page_rec_is_supremum(rec)) {
2413 break;
2414 }
2415
2416 count++;
2417 own_count++;
2418 old_rec = rec;
2419 rec = page_rec_get_next_const(rec);
2420
2421 /* set old_offsets to offsets; recycle offsets */
2422 {
2423 ulint *offs = old_offsets;
2424 old_offsets = offsets;
2425 offsets = offs;
2426 }
2427 }
2428
2429 if (page_is_comp(page)) {
2430 if (UNIV_UNLIKELY(rec_get_n_owned_new(rec) == 0)) {
2431 goto n_owned_zero;
2432 }
2433 } else if (UNIV_UNLIKELY(rec_get_n_owned_old(rec) == 0)) {
2434 n_owned_zero:
2435 ib::error(ER_IB_MSG_906) << "n owned is zero";
2436 goto func_exit;
2437 }
2438
2439 if (UNIV_UNLIKELY(slot_no != n_slots - 1)) {
2440 ib::error(ER_IB_MSG_907)
2441 << "n slots wrong " << slot_no << " " << (n_slots - 1);
2442 goto func_exit;
2443 }
2444
2445 if (UNIV_UNLIKELY(page_header_get_field(page, PAGE_N_RECS) +
2446 PAGE_HEAP_NO_USER_LOW !=
2447 count + 1)) {
2448 ib::error(ER_IB_MSG_908)
2449 << "n recs wrong "
2450 << page_header_get_field(page, PAGE_N_RECS) + PAGE_HEAP_NO_USER_LOW
2451 << " " << (count + 1);
2452 goto func_exit;
2453 }
2454
2455 if (UNIV_UNLIKELY(data_size != page_get_data_size(page))) {
2456 ib::error(ER_IB_MSG_909)
2457 << "Summed data size " << data_size << ", returned by func "
2458 << page_get_data_size(page);
2459 goto func_exit;
2460 }
2461
2462 /* Check then the free list */
2463 rec = page_header_get_ptr(page, PAGE_FREE);
2464
2465 while (rec != nullptr) {
2466 offsets = rec_get_offsets(rec, index, offsets, ULINT_UNDEFINED,
2467 UT_LOCATION_HERE, &heap);
2468 if (UNIV_UNLIKELY(!page_rec_validate(rec, offsets))) {
2469 goto func_exit;
2470 }
2471
2472 count++;
2473 offs = page_offset(rec_get_start(rec, offsets));
2474 i = rec_offs_size(offsets);
2475 if (UNIV_UNLIKELY(offs + i >= UNIV_PAGE_SIZE)) {
2476 ib::error(ER_IB_MSG_910) << "Record offset out of bounds";
2477 goto func_exit;
2478 }
2479
2480 while (i--) {
2481 if (UNIV_UNLIKELY(buf[offs + i])) {
2482 ib::error(ER_IB_MSG_911) << "Record overlaps another"
2483 " in free list";
2484 goto func_exit;
2485 }
2486
2487 buf[offs + i] = 1;
2488 }
2489
2490 rec = page_rec_get_next_const(rec);
2491 }
2492
2493 if (UNIV_UNLIKELY(page_dir_get_n_heap(page) != count + 1)) {
2494 ib::error(ER_IB_MSG_912)
2495 << "N heap is wrong " << page_dir_get_n_heap(page) << " " << count + 1;
2496 goto func_exit;
2497 }
2498
2499 ret = true;
2500
2501 func_exit:
2502 mem_heap_free(heap);
2503
2504 if (UNIV_UNLIKELY(ret == false)) {
2505 func_exit2:
2506 ib::error(ER_IB_MSG_913)
2507 << "Apparent corruption in space " << page_get_space_id(page)
2508 << " page " << page_no << " index " << index->name;
2509 }
2510
2511 return (ret);
2512 }
2513
2514 #ifndef UNIV_HOTBACKUP
2515 /** Looks in the page record list for a record with the given heap number.
2516 @return record, NULL if not found */
2517 const rec_t *page_find_rec_with_heap_no(
2518 const page_t *page, /*!< in: index page */
2519 ulint heap_no) /*!< in: heap number */
2520 {
2521 const rec_t *rec;
2522
2523 if (page_is_comp(page)) {
2524 rec = page + PAGE_NEW_INFIMUM;
2525
2526 for (;;) {
2527 ulint rec_heap_no = rec_get_heap_no_new(rec);
2528
2529 if (rec_heap_no == heap_no) {
2530 return (rec);
2531 } else if (rec_heap_no == PAGE_HEAP_NO_SUPREMUM) {
2532 return (nullptr);
2533 }
2534
2535 rec = page + rec_get_next_offs(rec, true);
2536 }
2537 } else {
2538 rec = page + PAGE_OLD_INFIMUM;
2539
2540 for (;;) {
2541 ulint rec_heap_no = rec_get_heap_no_old(rec);
2542
2543 if (rec_heap_no == heap_no) {
2544 return (rec);
2545 } else if (rec_heap_no == PAGE_HEAP_NO_SUPREMUM) {
2546 return (nullptr);
2547 }
2548
2549 rec = page + rec_get_next_offs(rec, false);
2550 }
2551 }
2552 }
2553
2554 bool page_delete_rec(const dict_index_t *index, page_cur_t *pcur,
2555 const ulint *offsets) {
2556 bool no_compress_needed;
2557 buf_block_t *block = pcur->block;
2558 page_t *page = buf_block_get_frame(block);
2559
2560 ut_ad(page_is_leaf(page));
2561
2562 if (!rec_offs_any_extern(offsets) &&
2563 ((page_get_data_size(page) - rec_offs_size(offsets) <
2564 BTR_CUR_PAGE_COMPRESS_LIMIT(index)) ||
2565 (mach_read_from_4(page + FIL_PAGE_NEXT) == FIL_NULL &&
2566 mach_read_from_4(page + FIL_PAGE_PREV) == FIL_NULL) ||
2567 (page_get_n_recs(page) < 2))) {
2568 page_no_t root_page_no = dict_index_get_page(index);
2569
2570 /* The page fillfactor will drop below a predefined
2571 minimum value, OR the level in the B-tree contains just
2572 one page, OR the page will become empty: we recommend
2573 compression if this is not the root page. */
2574
2575 no_compress_needed = page_get_page_no(page) == root_page_no;
2576 } else {
2577 no_compress_needed = true;
2578 }
2579
2580 if (no_compress_needed) {
2581 #ifdef UNIV_ZIP_DEBUG
2582 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
2583 #endif /* UNIV_ZIP_DEBUG */
2584
2585 page_cur_delete_rec(pcur, index, offsets, nullptr);
2586
2587 #ifdef UNIV_ZIP_DEBUG
2588 ut_a(!page_zip || page_zip_validate(page_zip, page, index));
2589 #endif /* UNIV_ZIP_DEBUG */
2590 }
2591
2592 return (no_compress_needed);
2593 }
2594 #endif /* !UNIV_HOTBACKUP */
2595
2596 /** Get the last non-delete-marked record on a page.
2597 @param[in] page index tree leaf page
2598 @return the last record, not delete-marked
2599 @retval infimum record if all records are delete-marked */
2600 const rec_t *page_find_rec_last_not_deleted(const page_t *page) {
2601 const rec_t *rec = page_get_infimum_rec(page);
2602 const rec_t *prev_rec = nullptr; // remove warning
2603
2604 /* Because the page infimum is never delete-marked,
2605 prev_rec will always be assigned to it first. */
2606 ut_ad(!rec_get_deleted_flag(rec, page_rec_is_comp(rec)));
2607 if (page_is_comp(page)) {
2608 do {
2609 if (!rec_get_deleted_flag(rec, true)) {
2610 prev_rec = rec;
2611 }
2612 rec = page_rec_get_next_low(rec, true);
2613 } while (rec != page + PAGE_NEW_SUPREMUM);
2614 } else {
2615 do {
2616 if (!rec_get_deleted_flag(rec, false)) {
2617 prev_rec = rec;
2618 }
2619 rec = page_rec_get_next_low(rec, false);
2620 } while (rec != page + PAGE_OLD_SUPREMUM);
2621 }
2622 return (prev_rec);
2623 }
2624
2625 /** Issue a warning when the checksum that is stored in the page is valid,
2626 but different than the global setting innodb_checksum_algorithm.
2627 @param[in] curr_algo current checksum algorithm
2628 @param[in] page_checksum page valid checksum
2629 @param[in] page_id page identifier */
2630 void page_warn_strict_checksum(srv_checksum_algorithm_t curr_algo,
2631 srv_checksum_algorithm_t page_checksum,
2632 const page_id_t &page_id) {
2633 srv_checksum_algorithm_t curr_algo_nonstrict;
2634 switch (curr_algo) {
2635 case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
2636 curr_algo_nonstrict = SRV_CHECKSUM_ALGORITHM_CRC32;
2637 break;
2638 case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
2639 curr_algo_nonstrict = SRV_CHECKSUM_ALGORITHM_INNODB;
2640 break;
2641 case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
2642 curr_algo_nonstrict = SRV_CHECKSUM_ALGORITHM_NONE;
2643 break;
2644 default:
2645 ut_error;
2646 }
2647
2648 ib::warn(ER_IB_MSG_914)
2649 << "innodb_checksum_algorithm is set to \""
2650 << buf_checksum_algorithm_name(curr_algo) << "\""
2651 << " but the page " << page_id << " contains a valid checksum \""
2652 << buf_checksum_algorithm_name(page_checksum) << "\". "
2653 << " Accepting the page as valid. Change"
2654 << " innodb_checksum_algorithm to \""
2655 << buf_checksum_algorithm_name(curr_algo_nonstrict)
2656 << "\" to silently accept such pages or rewrite all pages"
2657 << " so that they contain \""
2658 << buf_checksum_algorithm_name(curr_algo_nonstrict) << "\" checksum.";
2659 }