/
btr0cur.cc
6876 lines (5969 loc) · 213 KB
/
btr0cur.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1994, 2019, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2008, Google Inc.
Copyright (c) 2012, Facebook Inc.
Copyright (c) 2015, 2023, MariaDB Corporation.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file btr/btr0cur.cc
The index tree cursor
All changes that row operations make to a B-tree or the records
there must go through this module! Undo log records are written here
of every modify or insert of a clustered index record.
NOTE!!!
To make sure we do not run out of disk space during a pessimistic
insert or update, we have to reserve 2 x the height of the index tree
many pages in the tablespace before we start the operation, because
if leaf splitting has been started, it is difficult to undo, except
by crashing the database and doing a roll-forward.
Created 10/16/1994 Heikki Tuuri
*******************************************************/
#include "btr0cur.h"
#include "row0upd.h"
#include "mtr0log.h"
#include "page0page.h"
#include "page0zip.h"
#include "rem0rec.h"
#include "rem0cmp.h"
#include "buf0lru.h"
#include "btr0btr.h"
#include "btr0sea.h"
#include "row0log.h"
#include "row0purge.h"
#include "row0upd.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "que0que.h"
#include "row0row.h"
#include "srv0srv.h"
#include "ibuf0ibuf.h"
#include "lock0lock.h"
#include "zlib.h"
#include "srv0start.h"
#include "mysql_com.h"
#include "dict0stats.h"
#include "row0ins.h"
#ifdef WITH_WSREP
#include "mysql/service_wsrep.h"
#endif /* WITH_WSREP */
#include "log.h"
/** Buffered B-tree operation types, introduced as part of delete buffering. */
enum btr_op_t {
BTR_NO_OP = 0, /*!< Not buffered */
BTR_INSERT_OP, /*!< Insert, do not ignore UNIQUE */
BTR_INSERT_IGNORE_UNIQUE_OP, /*!< Insert, ignoring UNIQUE */
BTR_DELETE_OP, /*!< Purge a delete-marked record */
BTR_DELMARK_OP /*!< Mark a record for deletion */
};
/** Modification types for the B-tree operation.
Note that the order must be DELETE, BOTH, INSERT !!
*/
enum btr_intention_t {
BTR_INTENTION_DELETE,
BTR_INTENTION_BOTH,
BTR_INTENTION_INSERT
};
/** For the index->lock scalability improvement, only possibility of clear
performance regression observed was caused by grown huge history list length.
That is because the exclusive use of index->lock also worked as reserving
free blocks and read IO bandwidth with priority. To avoid huge glowing history
list as same level with previous implementation, prioritizes pessimistic tree
operations by purge as the previous, when it seems to be growing huge.
Experimentally, the history list length starts to affect to performance
throughput clearly from about 100000. */
#define BTR_CUR_FINE_HISTORY_LENGTH 100000
#ifdef BTR_CUR_HASH_ADAPT
/** Number of searches down the B-tree in btr_cur_t::search_leaf(). */
ib_counter_t<ulint, ib_counter_element_t> btr_cur_n_non_sea;
/** Old value of btr_cur_n_non_sea. Copied by
srv_refresh_innodb_monitor_stats(). Referenced by
srv_printf_innodb_monitor(). */
ulint btr_cur_n_non_sea_old;
/** Number of successful adaptive hash index lookups in
btr_cur_t::search_leaf(). */
ib_counter_t<ulint, ib_counter_element_t> btr_cur_n_sea;
/** Old value of btr_cur_n_sea. Copied by
srv_refresh_innodb_monitor_stats(). Referenced by
srv_printf_innodb_monitor(). */
ulint btr_cur_n_sea_old;
#endif /* BTR_CUR_HASH_ADAPT */
#ifdef UNIV_DEBUG
/* Flag to limit optimistic insert records */
uint btr_cur_limit_optimistic_insert_debug;
#endif /* UNIV_DEBUG */
/** In the optimistic insert, if the insert does not fit, but this much space
can be released by page reorganize, then it is reorganized */
#define BTR_CUR_PAGE_REORGANIZE_LIMIT (srv_page_size / 32)
/** The structure of a BLOB part header */
/* @{ */
/*--------------------------------------*/
#define BTR_BLOB_HDR_PART_LEN 0 /*!< BLOB part len on this
page */
#define BTR_BLOB_HDR_NEXT_PAGE_NO 4 /*!< next BLOB part page no,
FIL_NULL if none */
/*--------------------------------------*/
#define BTR_BLOB_HDR_SIZE 8 /*!< Size of a BLOB
part header, in bytes */
/* @} */
/*******************************************************************//**
Marks all extern fields in a record as owned by the record. This function
should be called if the delete mark of a record is removed: a not delete
marked record always owns all its extern fields. */
static
void
btr_cur_unmark_extern_fields(
/*=========================*/
buf_block_t* block, /*!< in/out: index page */
rec_t* rec, /*!< in/out: record in a clustered index */
dict_index_t* index, /*!< in: index of the page */
const rec_offs* offsets,/*!< in: array returned by rec_get_offsets() */
mtr_t* mtr); /*!< in: mtr, or NULL if not logged */
/***********************************************************//**
Frees the externally stored fields for a record, if the field is mentioned
in the update vector. */
static
void
btr_rec_free_updated_extern_fields(
/*===============================*/
dict_index_t* index, /*!< in: index of rec; the index tree MUST be
X-latched */
rec_t* rec, /*!< in: record */
buf_block_t* block, /*!< in: index page of rec */
const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */
const upd_t* update, /*!< in: update vector */
bool rollback,/*!< in: performing rollback? */
mtr_t* mtr); /*!< in: mini-transaction handle which contains
an X-latch to record page and to the tree */
/***********************************************************//**
Frees the externally stored fields for a record. */
static
void
btr_rec_free_externally_stored_fields(
/*==================================*/
dict_index_t* index, /*!< in: index of the data, the index
tree MUST be X-latched */
rec_t* rec, /*!< in: record */
const rec_offs* offsets,/*!< in: rec_get_offsets(rec, index) */
buf_block_t* block, /*!< in: index page of rec */
bool rollback,/*!< in: performing rollback? */
mtr_t* mtr); /*!< in: mini-transaction handle which contains
an X-latch to record page and to the index
tree */
/*==================== B-TREE SEARCH =========================*/
/** Load the instant ALTER TABLE metadata from the clustered index
when loading a table definition.
@param[in,out] index clustered index definition
@param[in,out] mtr mini-transaction
@return error code
@retval DB_SUCCESS if no error occurred
@retval DB_CORRUPTION if any corruption was noticed */
static dberr_t btr_cur_instant_init_low(dict_index_t* index, mtr_t* mtr)
{
ut_ad(index->is_primary());
ut_ad(index->n_core_null_bytes == dict_index_t::NO_CORE_NULL_BYTES);
ut_ad(index->table->supports_instant());
ut_ad(index->table->is_readable());
dberr_t err;
const fil_space_t* space = index->table->space;
if (!space) {
corrupted:
err = DB_CORRUPTION;
unreadable:
ib::error() << "Table " << index->table->name
<< " has an unreadable root page";
index->table->corrupted = true;
index->table->file_unreadable = true;
return err;
}
buf_block_t* root = btr_root_block_get(index, RW_SX_LATCH, mtr, &err);
if (!root) {
goto unreadable;
}
if (btr_cur_instant_root_init(index, root->page.frame)) {
goto corrupted;
}
ut_ad(index->n_core_null_bytes != dict_index_t::NO_CORE_NULL_BYTES);
if (fil_page_get_type(root->page.frame) == FIL_PAGE_INDEX) {
ut_ad(!index->is_instant());
return DB_SUCCESS;
}
btr_cur_t cur;
/* Relax the assertion in rec_init_offsets(). */
ut_ad(!index->in_instant_init);
ut_d(index->in_instant_init = true);
err = cur.open_leaf(true, index, BTR_SEARCH_LEAF, mtr);
ut_d(index->in_instant_init = false);
if (err != DB_SUCCESS) {
index->table->file_unreadable = true;
index->table->corrupted = true;
return err;
}
ut_ad(page_cur_is_before_first(&cur.page_cur));
ut_ad(page_is_leaf(cur.page_cur.block->page.frame));
const rec_t* rec = page_cur_move_to_next(&cur.page_cur);
const ulint comp = dict_table_is_comp(index->table);
const ulint info_bits = rec ? rec_get_info_bits(rec, comp) : 0;
if (page_rec_is_supremum(rec)
|| !(info_bits & REC_INFO_MIN_REC_FLAG)) {
if (rec && !index->is_instant()) {
/* The FIL_PAGE_TYPE_INSTANT and PAGE_INSTANT may be
assigned even if instant ADD COLUMN was not
committed. Changes to these page header fields are not
undo-logged, but changes to the hidden metadata record
are. If the server is killed and restarted, the page
header fields could remain set even though no metadata
record is present. */
return DB_SUCCESS;
}
ib::error() << "Table " << index->table->name
<< " is missing instant ALTER metadata";
index->table->corrupted = true;
return DB_CORRUPTION;
}
if ((info_bits & ~REC_INFO_DELETED_FLAG) != REC_INFO_MIN_REC_FLAG
|| (comp && rec_get_status(rec) != REC_STATUS_INSTANT)) {
incompatible:
ib::error() << "Table " << index->table->name
<< " contains unrecognizable instant ALTER metadata";
index->table->corrupted = true;
return DB_CORRUPTION;
}
/* Read the metadata. We can get here on server restart
or when the table was evicted from the data dictionary cache
and is now being accessed again.
Here, READ COMMITTED and REPEATABLE READ should be equivalent.
Committing the ADD COLUMN operation would acquire
MDL_EXCLUSIVE and LOCK_X|LOCK_TABLE, which would prevent any
concurrent operations on the table, including table eviction
from the cache. */
if (info_bits & REC_INFO_DELETED_FLAG) {
/* This metadata record includes a BLOB that identifies
any dropped or reordered columns. */
ulint trx_id_offset = index->trx_id_offset;
/* If !index->trx_id_offset, the PRIMARY KEY contains
variable-length columns. For the metadata record,
variable-length columns should be written with zero
length. However, before MDEV-21088 was fixed, for
variable-length encoded PRIMARY KEY column of type
CHAR, we wrote more than zero bytes. That is why we
must determine the actual length of each PRIMARY KEY
column. The DB_TRX_ID will start right after any
PRIMARY KEY columns. */
ut_ad(index->n_uniq);
/* We cannot invoke rec_get_offsets() before
index->table->deserialise_columns(). Therefore,
we must duplicate some logic here. */
if (trx_id_offset) {
} else if (index->table->not_redundant()) {
/* The PRIMARY KEY contains variable-length columns.
For the metadata record, variable-length columns are
always written with zero length. The DB_TRX_ID will
start right after any fixed-length columns. */
/* OK, before MDEV-21088 was fixed, for
variable-length encoded PRIMARY KEY column of
type CHAR, we wrote more than zero bytes. In
order to allow affected tables to be accessed,
it would be nice to determine the actual
length of each PRIMARY KEY column. However, to
be able to do that, we should determine the
size of the null-bit bitmap in the metadata
record. And we cannot know that before reading
the metadata BLOB, whose starting point we are
trying to find here. (Although the PRIMARY KEY
columns cannot be NULL, we would have to know
where the lengths of variable-length PRIMARY KEY
columns start.)
So, unfortunately we cannot help users who
were affected by MDEV-21088 on a ROW_FORMAT=COMPACT
or ROW_FORMAT=DYNAMIC table. */
for (uint i = index->n_uniq; i--; ) {
trx_id_offset += index->fields[i].fixed_len;
}
} else if (rec_get_1byte_offs_flag(rec)) {
trx_id_offset = rec_1_get_field_end_info(
rec, index->n_uniq - 1);
ut_ad(!(trx_id_offset & REC_1BYTE_SQL_NULL_MASK));
trx_id_offset &= ~REC_1BYTE_SQL_NULL_MASK;
} else {
trx_id_offset = rec_2_get_field_end_info(
rec, index->n_uniq - 1);
ut_ad(!(trx_id_offset & REC_2BYTE_SQL_NULL_MASK));
trx_id_offset &= ~REC_2BYTE_SQL_NULL_MASK;
}
const byte* ptr = rec + trx_id_offset
+ (DATA_TRX_ID_LEN + DATA_ROLL_PTR_LEN);
if (mach_read_from_4(ptr + BTR_EXTERN_LEN)) {
goto incompatible;
}
uint len = mach_read_from_4(ptr + BTR_EXTERN_LEN + 4);
if (!len
|| mach_read_from_4(ptr + BTR_EXTERN_OFFSET)
!= FIL_PAGE_DATA
|| mach_read_from_4(ptr + BTR_EXTERN_SPACE_ID)
!= space->id) {
goto incompatible;
}
buf_block_t* block = buf_page_get(
page_id_t(space->id,
mach_read_from_4(ptr + BTR_EXTERN_PAGE_NO)),
0, RW_S_LATCH, mtr);
if (!block) {
goto incompatible;
}
if (fil_page_get_type(block->page.frame) != FIL_PAGE_TYPE_BLOB
|| mach_read_from_4(&block->page.frame
[FIL_PAGE_DATA
+ BTR_BLOB_HDR_NEXT_PAGE_NO])
!= FIL_NULL
|| mach_read_from_4(&block->page.frame
[FIL_PAGE_DATA
+ BTR_BLOB_HDR_PART_LEN])
!= len) {
goto incompatible;
}
/* The unused part of the BLOB page should be zero-filled. */
for (const byte* b = block->page.frame
+ (FIL_PAGE_DATA + BTR_BLOB_HDR_SIZE) + len,
* const end = block->page.frame + srv_page_size
- BTR_EXTERN_LEN;
b < end; ) {
if (*b++) {
goto incompatible;
}
}
if (index->table->deserialise_columns(
&block->page.frame
[FIL_PAGE_DATA + BTR_BLOB_HDR_SIZE], len)) {
goto incompatible;
}
/* Proceed to initialize the default values of
any instantly added columns. */
}
mem_heap_t* heap = NULL;
rec_offs* offsets = rec_get_offsets(rec, index, NULL,
index->n_core_fields,
ULINT_UNDEFINED, &heap);
if (rec_offs_any_default(offsets)) {
inconsistent:
mem_heap_free(heap);
goto incompatible;
}
/* In fact, because we only ever append fields to the metadata
record, it is also OK to perform READ UNCOMMITTED and
then ignore any extra fields, provided that
trx_sys.is_registered(DB_TRX_ID). */
if (rec_offs_n_fields(offsets)
> ulint(index->n_fields) + !!index->table->instant
&& !trx_sys.is_registered(current_trx(),
row_get_rec_trx_id(rec, index,
offsets))) {
goto inconsistent;
}
for (unsigned i = index->n_core_fields; i < index->n_fields; i++) {
dict_col_t* col = index->fields[i].col;
const unsigned o = i + !!index->table->instant;
ulint len;
const byte* data = rec_get_nth_field(rec, offsets, o, &len);
ut_ad(!col->is_added());
ut_ad(!col->def_val.data);
col->def_val.len = len;
switch (len) {
case UNIV_SQL_NULL:
continue;
case 0:
col->def_val.data = field_ref_zero;
continue;
}
ut_ad(len != UNIV_SQL_DEFAULT);
if (!rec_offs_nth_extern(offsets, o)) {
col->def_val.data = mem_heap_dup(
index->table->heap, data, len);
} else if (len < BTR_EXTERN_FIELD_REF_SIZE
|| !memcmp(data + len - BTR_EXTERN_FIELD_REF_SIZE,
field_ref_zero,
BTR_EXTERN_FIELD_REF_SIZE)) {
col->def_val.len = UNIV_SQL_DEFAULT;
goto inconsistent;
} else {
col->def_val.data = btr_copy_externally_stored_field(
&col->def_val.len, data,
cur.page_cur.block->zip_size(),
len, index->table->heap);
}
}
mem_heap_free(heap);
return DB_SUCCESS;
}
/** Load the instant ALTER TABLE metadata from the clustered index
when loading a table definition.
@param[in,out] table table definition from the data dictionary
@return error code
@retval DB_SUCCESS if no error occurred */
dberr_t
btr_cur_instant_init(dict_table_t* table)
{
mtr_t mtr;
dict_index_t* index = dict_table_get_first_index(table);
mtr.start();
dberr_t err = index
? btr_cur_instant_init_low(index, &mtr)
: DB_CORRUPTION;
mtr.commit();
return(err);
}
/** Initialize the n_core_null_bytes on first access to a clustered
index root page.
@param[in] index clustered index that is on its first access
@param[in] page clustered index root page
@return whether the page is corrupted */
bool btr_cur_instant_root_init(dict_index_t* index, const page_t* page)
{
ut_ad(!index->is_dummy);
ut_ad(index->is_primary());
ut_ad(!index->is_instant());
ut_ad(index->table->supports_instant());
if (page_has_siblings(page)) {
return true;
}
/* This is normally executed as part of btr_cur_instant_init()
when dict_load_table_one() is loading a table definition.
Other threads should not access or modify the n_core_null_bytes,
n_core_fields before dict_load_table_one() returns.
This can also be executed during IMPORT TABLESPACE, where the
table definition is exclusively locked. */
switch (fil_page_get_type(page)) {
default:
return true;
case FIL_PAGE_INDEX:
/* The field PAGE_INSTANT is guaranteed 0 on clustered
index root pages of ROW_FORMAT=COMPACT or
ROW_FORMAT=DYNAMIC when instant ADD COLUMN is not used. */
if (page_is_comp(page) && page_get_instant(page)) {
return true;
}
index->n_core_null_bytes = static_cast<uint8_t>(
UT_BITS_IN_BYTES(unsigned(index->n_nullable)));
return false;
case FIL_PAGE_TYPE_INSTANT:
break;
}
const uint16_t n = page_get_instant(page);
if (n < index->n_uniq + DATA_ROLL_PTR) {
/* The PRIMARY KEY (or hidden DB_ROW_ID) and
DB_TRX_ID,DB_ROLL_PTR columns must always be present
as 'core' fields. */
return true;
}
if (n > REC_MAX_N_FIELDS) {
return true;
}
index->n_core_fields = n & dict_index_t::MAX_N_FIELDS;
const rec_t* infimum = page_get_infimum_rec(page);
const rec_t* supremum = page_get_supremum_rec(page);
if (!memcmp(infimum, "infimum", 8)
&& !memcmp(supremum, "supremum", 8)) {
if (n > index->n_fields) {
/* All fields, including those for instantly
added columns, must be present in the
data dictionary. */
return true;
}
ut_ad(!index->is_dummy);
ut_d(index->is_dummy = true);
index->n_core_null_bytes = static_cast<uint8_t>(
UT_BITS_IN_BYTES(index->get_n_nullable(n)));
ut_d(index->is_dummy = false);
return false;
}
if (memcmp(infimum, field_ref_zero, 8)
|| memcmp(supremum, field_ref_zero, 7)) {
/* The infimum and supremum records must either contain
the original strings, or they must be filled with zero
bytes, except for the bytes that we have repurposed. */
return true;
}
index->n_core_null_bytes = supremum[7];
return index->n_core_null_bytes > 128;
}
/**
Gets intention in btr_intention_t from latch_mode, and cleares the intention
at the latch_mode.
@param latch_mode in/out: pointer to latch_mode
@return intention for latching tree */
static
btr_intention_t btr_cur_get_and_clear_intention(btr_latch_mode *latch_mode)
{
btr_intention_t intention;
switch (*latch_mode & (BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE)) {
case BTR_LATCH_FOR_INSERT:
intention = BTR_INTENTION_INSERT;
break;
case BTR_LATCH_FOR_DELETE:
intention = BTR_INTENTION_DELETE;
break;
default:
/* both or unknown */
intention = BTR_INTENTION_BOTH;
}
*latch_mode = btr_latch_mode(
*latch_mode & ~(BTR_LATCH_FOR_INSERT | BTR_LATCH_FOR_DELETE));
return(intention);
}
/** @return whether the distance between two records is at most the
specified value */
static bool
page_rec_distance_is_at_most(const rec_t *left, const rec_t *right, ulint val)
{
do
{
if (left == right)
return true;
left= page_rec_get_next_const(left);
}
while (left && val--);
return false;
}
/** Detects whether the modifying record might need a modifying tree structure.
@param[in] index index
@param[in] page page
@param[in] lock_intention lock intention for the tree operation
@param[in] rec record (current node_ptr)
@param[in] rec_size size of the record or max size of node_ptr
@param[in] zip_size ROW_FORMAT=COMPRESSED page size, or 0
@param[in] mtr mtr
@return true if tree modification is needed */
static
bool
btr_cur_will_modify_tree(
dict_index_t* index,
const page_t* page,
btr_intention_t lock_intention,
const rec_t* rec,
ulint rec_size,
ulint zip_size,
mtr_t* mtr)
{
ut_ad(!page_is_leaf(page));
ut_ad(mtr->memo_contains_flagged(&index->lock, MTR_MEMO_X_LOCK
| MTR_MEMO_SX_LOCK));
/* Pessimistic delete of the first record causes delete & insert
of node_ptr at upper level. And a subsequent page shrink is
possible. It causes delete of node_ptr at the upper level.
So we should pay attention also to 2nd record not only
first record and last record. Because if the "delete & insert" are
done for the different page, the 2nd record become
first record and following compress might delete the record and causes
the uppper level node_ptr modification. */
const ulint n_recs = page_get_n_recs(page);
if (lock_intention <= BTR_INTENTION_BOTH) {
compile_time_assert(BTR_INTENTION_DELETE < BTR_INTENTION_BOTH);
compile_time_assert(BTR_INTENTION_BOTH < BTR_INTENTION_INSERT);
if (!page_has_siblings(page)) {
return true;
}
ulint margin = rec_size;
if (lock_intention == BTR_INTENTION_BOTH) {
ulint level = btr_page_get_level(page);
/* This value is the worst expectation for the node_ptr
records to be deleted from this page. It is used to
expect whether the cursor position can be the left_most
record in this page or not. */
ulint max_nodes_deleted = 0;
/* By modifying tree operations from the under of this
level, logically (2 ^ (level - 1)) opportunities to
deleting records in maximum even unreally rare case. */
if (level > 7) {
/* TODO: adjust this practical limit. */
max_nodes_deleted = 64;
} else if (level > 0) {
max_nodes_deleted = (ulint)1 << (level - 1);
}
/* check delete will cause. (BTR_INTENTION_BOTH
or BTR_INTENTION_DELETE) */
if (n_recs <= max_nodes_deleted * 2
|| page_rec_is_first(rec, page)) {
/* The cursor record can be the left most record
in this page. */
return true;
}
if (page_has_prev(page)
&& page_rec_distance_is_at_most(
page_get_infimum_rec(page), rec,
max_nodes_deleted)) {
return true;
}
if (page_has_next(page)
&& page_rec_distance_is_at_most(
rec, page_get_supremum_rec(page),
max_nodes_deleted)) {
return true;
}
/* Delete at leftmost record in a page causes delete
& insert at its parent page. After that, the delete
might cause btr_compress() and delete record at its
parent page. Thus we should consider max deletes. */
margin *= max_nodes_deleted;
}
/* Safe because we already have SX latch of the index tree */
if (page_get_data_size(page)
< margin + BTR_CUR_PAGE_COMPRESS_LIMIT(index)) {
return(true);
}
}
if (lock_intention >= BTR_INTENTION_BOTH) {
/* check insert will cause. BTR_INTENTION_BOTH
or BTR_INTENTION_INSERT*/
/* Once we invoke the btr_cur_limit_optimistic_insert_debug,
we should check it here in advance, since the max allowable
records in a page is limited. */
LIMIT_OPTIMISTIC_INSERT_DEBUG(n_recs, return true);
/* needs 2 records' space for the case the single split and
insert cannot fit.
page_get_max_insert_size_after_reorganize() includes space
for page directory already */
ulint max_size
= page_get_max_insert_size_after_reorganize(page, 2);
if (max_size < BTR_CUR_PAGE_REORGANIZE_LIMIT + rec_size
|| max_size < rec_size * 2) {
return(true);
}
/* TODO: optimize this condition for ROW_FORMAT=COMPRESSED.
This is based on the worst case, and we could invoke
page_zip_available() on the block->page.zip. */
/* needs 2 records' space also for worst compress rate. */
if (zip_size
&& page_zip_empty_size(index->n_fields, zip_size)
<= rec_size * 2 + page_get_data_size(page)
+ page_dir_calc_reserved_space(n_recs + 2)) {
return(true);
}
}
return(false);
}
/** Detects whether the modifying record might need a opposite modification
to the intention.
@param[in] page page
@param[in] lock_intention lock intention for the tree operation
@param[in] rec record (current node_ptr)
@return true if tree modification is needed */
static
bool
btr_cur_need_opposite_intention(
const page_t* page,
btr_intention_t lock_intention,
const rec_t* rec)
{
switch (lock_intention) {
case BTR_INTENTION_DELETE:
return (page_has_prev(page) && page_rec_is_first(rec, page)) ||
(page_has_next(page) && page_rec_is_last(rec, page));
case BTR_INTENTION_INSERT:
return page_has_next(page) && page_rec_is_last(rec, page);
case BTR_INTENTION_BOTH:
return(false);
}
MY_ASSERT_UNREACHABLE();
return(false);
}
/**
@param[in] index b-tree
@return maximum size of a node pointer record in bytes */
static ulint btr_node_ptr_max_size(const dict_index_t* index)
{
if (dict_index_is_ibuf(index)) {
/* cannot estimate accurately */
/* This is universal index for change buffer.
The max size of the entry is about max key length * 2.
(index key + primary key to be inserted to the index)
(The max key length is UNIV_PAGE_SIZE / 16 * 3 at
ha_innobase::max_supported_key_length(),
considering MAX_KEY_LENGTH = 3072 at MySQL imposes
the 3500 historical InnoDB value for 16K page size case.)
For the universal index, node_ptr contains most of the entry.
And 512 is enough to contain ibuf columns and meta-data */
return srv_page_size / 8 * 3 + 512;
}
/* Each record has page_no, length of page_no and header. */
ulint comp = dict_table_is_comp(index->table);
ulint rec_max_size = comp
? REC_NODE_PTR_SIZE + 1 + REC_N_NEW_EXTRA_BYTES
+ UT_BITS_IN_BYTES(index->n_nullable)
: REC_NODE_PTR_SIZE + 2 + REC_N_OLD_EXTRA_BYTES
+ 2 * index->n_fields;
/* Compute the maximum possible record size. */
for (ulint i = 0; i < dict_index_get_n_unique_in_tree(index); i++) {
const dict_field_t* field
= dict_index_get_nth_field(index, i);
const dict_col_t* col
= dict_field_get_col(field);
ulint field_max_size;
ulint field_ext_max_size;
/* Determine the maximum length of the index field. */
field_max_size = dict_col_get_fixed_size(col, comp);
if (field_max_size) {
/* dict_index_add_col() should guarantee this */
ut_ad(!field->prefix_len
|| field->fixed_len == field->prefix_len);
/* Fixed lengths are not encoded
in ROW_FORMAT=COMPACT. */
rec_max_size += field_max_size;
continue;
}
field_max_size = dict_col_get_max_size(col);
if (UNIV_UNLIKELY(!field_max_size)) {
switch (col->mtype) {
case DATA_VARCHAR:
if (!comp
&& (!strcmp(index->table->name.m_name,
"SYS_FOREIGN")
|| !strcmp(index->table->name.m_name,
"SYS_FOREIGN_COLS"))) {
break;
}
/* fall through */
case DATA_VARMYSQL:
case DATA_CHAR:
case DATA_MYSQL:
/* CHAR(0) and VARCHAR(0) are possible
data type definitions in MariaDB.
The InnoDB internal SQL parser maps
CHAR to DATA_VARCHAR, so DATA_CHAR (or
DATA_MYSQL) is only coming from the
MariaDB SQL layer. */
if (comp) {
/* Add a length byte, because
fixed-length empty field are
encoded as variable-length.
For ROW_FORMAT=REDUNDANT,
these bytes were added to
rec_max_size before this loop. */
rec_max_size++;
}
continue;
}
/* SYS_FOREIGN.ID is defined as CHAR in the
InnoDB internal SQL parser, which translates
into the incorrect VARCHAR(0). InnoDB does
not enforce maximum lengths of columns, so
that is why any data can be inserted in the
first place.
Likewise, SYS_FOREIGN.FOR_NAME,
SYS_FOREIGN.REF_NAME, SYS_FOREIGN_COLS.ID, are
defined as CHAR, and also they are part of a key. */
ut_ad(!strcmp(index->table->name.m_name,
"SYS_FOREIGN")
|| !strcmp(index->table->name.m_name,
"SYS_FOREIGN_COLS"));
ut_ad(!comp);
ut_ad(col->mtype == DATA_VARCHAR);
rec_max_size += (srv_page_size == UNIV_PAGE_SIZE_MAX)
? REDUNDANT_REC_MAX_DATA_SIZE
: page_get_free_space_of_empty(FALSE) / 2;
} else if (field_max_size == NAME_LEN && i == 1
&& (!strcmp(index->table->name.m_name,
TABLE_STATS_NAME)
|| !strcmp(index->table->name.m_name,
INDEX_STATS_NAME))) {
/* Interpret "table_name" as VARCHAR(199) even
if it was incorrectly defined as VARCHAR(64).
While the caller of ha_innobase enforces the
maximum length on any data written, the InnoDB
internal SQL parser will happily write as much
data as is provided. The purpose of this hack
is to avoid InnoDB hangs after persistent
statistics on partitioned tables are
deleted. */
field_max_size = 199 * SYSTEM_CHARSET_MBMAXLEN;
}
field_ext_max_size = field_max_size < 256 ? 1 : 2;
if (field->prefix_len
&& field->prefix_len < field_max_size) {
field_max_size = field->prefix_len;
}
if (comp) {
/* Add the extra size for ROW_FORMAT=COMPACT.
For ROW_FORMAT=REDUNDANT, these bytes were
added to rec_max_size before this loop. */
rec_max_size += field_ext_max_size;
}
rec_max_size += field_max_size;
}
return rec_max_size;
}
/** @return a B-tree search mode suitable for non-leaf pages
@param mode leaf page search mode */
static inline page_cur_mode_t btr_cur_nonleaf_mode(page_cur_mode_t mode)
{
if (mode > PAGE_CUR_GE)
{
ut_ad(mode == PAGE_CUR_L || mode == PAGE_CUR_LE);
return mode;
}
if (mode == PAGE_CUR_GE)
return PAGE_CUR_L;
ut_ad(mode == PAGE_CUR_G);
return PAGE_CUR_LE;
}
dberr_t btr_cur_t::search_leaf(const dtuple_t *tuple, page_cur_mode_t mode,
btr_latch_mode latch_mode, mtr_t *mtr)
{
ut_ad(index()->is_btree() || index()->is_ibuf());
ut_ad(!index()->is_ibuf() || ibuf_inside(mtr));
buf_block_t *guess;
btr_op_t btr_op;
btr_intention_t lock_intention;
bool detected_same_key_root= false;
mem_heap_t* heap = NULL;
rec_offs offsets_[REC_OFFS_NORMAL_SIZE];
rec_offs* offsets = offsets_;
rec_offs offsets2_[REC_OFFS_NORMAL_SIZE];
rec_offs* offsets2 = offsets2_;
rec_offs_init(offsets_);
rec_offs_init(offsets2_);
ut_ad(dict_index_check_search_tuple(index(), tuple));
ut_ad(dtuple_check_typed(tuple));
ut_ad(index()->page != FIL_NULL);
MEM_UNDEFINED(&up_match, sizeof up_match);
MEM_UNDEFINED(&up_bytes, sizeof up_bytes);
MEM_UNDEFINED(&low_match, sizeof low_match);
MEM_UNDEFINED(&low_bytes, sizeof low_bytes);
ut_d(up_match= ULINT_UNDEFINED);
ut_d(low_match= ULINT_UNDEFINED);
ut_ad(!(latch_mode & BTR_ALREADY_S_LATCHED) ||
mtr->memo_contains_flagged(&index()->lock,
MTR_MEMO_S_LOCK | MTR_MEMO_SX_LOCK |
MTR_MEMO_X_LOCK));
/* These flags are mutually exclusive, they are lumped together
with the latch mode for historical reasons. It's possible for
none of the flags to be set. */
switch (UNIV_EXPECT(latch_mode & BTR_DELETE, 0)) {
default:
btr_op= BTR_NO_OP;
break;
case BTR_INSERT:
btr_op= (latch_mode & BTR_IGNORE_SEC_UNIQUE)
? BTR_INSERT_IGNORE_UNIQUE_OP
: BTR_INSERT_OP;
break;
case BTR_DELETE:
btr_op= BTR_DELETE_OP;
ut_a(purge_node);
break;
case BTR_DELETE_MARK:
btr_op= BTR_DELMARK_OP;
break;
}
/* Operations on the insert buffer tree cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !index()->is_ibuf());
/* Operations on the clustered index cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !index()->is_clust());
/* Operations on the temporary table(indexes) cannot be buffered. */
ut_ad(btr_op == BTR_NO_OP || !index()->table->is_temporary());
const bool latch_by_caller= latch_mode & BTR_ALREADY_S_LATCHED;
lock_intention= btr_cur_get_and_clear_intention(&latch_mode);
latch_mode= BTR_LATCH_MODE_WITHOUT_FLAGS(latch_mode);
ut_ad(!latch_by_caller
|| latch_mode == BTR_SEARCH_LEAF
|| latch_mode == BTR_MODIFY_LEAF
|| latch_mode == BTR_MODIFY_TREE