-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Expand file tree
/
Copy pathdict0dict.cc
More file actions
6059 lines (4916 loc) · 193 KB
/
Copy pathdict0dict.cc
File metadata and controls
6059 lines (4916 loc) · 193 KB
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) 1996, 2025, Oracle and/or its affiliates.
Copyright (c) 2012, Facebook Inc.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0,
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/** @file dict/dict0dict.cc
Data dictionary system
Created 1/8/1996 Heikki Tuuri
***********************************************************************/
#include "debug_sync.h"
#include "my_config.h"
#include <stdlib.h>
#include <strfunc.h>
#include <sys/types.h>
#include <algorithm>
#include <string>
#ifndef UNIV_HOTBACKUP
#include "current_thd.h"
#endif /* !UNIV_HOTBACKUP */
#include "dict0dict.h"
#include "fil0fil.h"
#ifndef UNIV_HOTBACKUP
#include "fts0fts.h"
#endif /* !UNIV_HOTBACKUP */
#include "ha_prototypes.h"
#include "log0chkp.h"
#include "log0write.h"
#include "my_dbug.h"
#ifndef UNIV_HOTBACKUP
#include "clone0api.h"
#include "mysqld.h" // system_charset_info
#include "que0types.h"
#include "row0sel.h"
#endif /* !UNIV_HOTBACKUP */
#if defined UNIV_HOTBACKUP && defined UNIV_DEBUG
static inline bool dict_non_lru_find_table(const dict_table_t *x) {
return true;
}
#endif /* UNIV_HOTBACKUP && UNIV_DEBUG */
/** dummy index for ROW_FORMAT=REDUNDANT supremum and infimum records */
dict_index_t *dict_ind_redundant;
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG
/** Flag to control insert buffer debugging. */
extern uint ibuf_debug;
#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */
#include <algorithm>
#include <vector>
#include "btr0btr.h"
#include "btr0cur.h"
#include "btr0sea.h"
#include "buf0buf.h"
#include "data0type.h"
#include "dict0boot.h"
#include "dict0crea.h"
#ifndef UNIV_HOTBACKUP
#include "dict0dd.h"
#endif /* !UNIV_HOTBACKUP */
#include "dict0mem.h"
#include "dict0priv.h"
#ifndef UNIV_HOTBACKUP
#include "dict0stats.h"
#endif /* !UNIV_HOTBACKUP */
#include "fsp0sysspace.h"
#ifndef UNIV_HOTBACKUP
#include "fts0fts.h"
#include "fts0types.h"
#include "lock0lock.h"
#endif /* !UNIV_HOTBACKUP */
#include "mach0data.h"
#include "mem0mem.h"
#include "os0once.h"
#include "page0page.h"
#include "page0zip.h"
#ifndef UNIV_HOTBACKUP
#include "pars0pars.h"
#include "pars0sym.h"
#include "que0que.h"
#endif /* !UNIV_HOTBACKUP */
#include "rem0cmp.h"
#include "row0ins.h"
#include "row0log.h"
#ifndef UNIV_HOTBACKUP
#include "ddl0ddl.h"
#include "row0mysql.h"
#endif /* !UNIV_HOTBACKUP */
#include "row0upd.h"
#ifndef UNIV_HOTBACKUP
#include "ha_innodb.h"
#include "srv0mon.h"
#include "srv0start.h"
#include "sync0sync.h"
#include "trx0undo.h"
#include "ut0new.h"
#endif /* !UNIV_HOTBACKUP */
static_assert(DATA_ROW_ID == 0, "DATA_ROW_ID != 0");
static_assert(DATA_TRX_ID == 1, "DATA_TRX_ID != 1");
static_assert(DATA_ROLL_PTR == 2, "DATA_ROLL_PTR != 2");
static_assert(DATA_N_SYS_COLS == 3, "DATA_N_SYS_COLS != 3");
static_assert(DATA_TRX_ID_LEN == 6, "DATA_TRX_ID_LEN != 6");
static_assert(DATA_ITT_N_SYS_COLS == 2, "DATA_ITT_N_SYS_COLS != 2");
/** the dictionary system */
dict_sys_t *dict_sys = nullptr;
/** The set of SE private IDs of DD tables. Used to tell whether a table is
a DD table. Since the DD tables can be rebuilt with new SE private IDs,
this set replaces checks based on ranges of IDs. */
std::set<dd::Object_id> dict_sys_t::s_dd_table_ids = {};
/** The name of the data dictionary tablespace. */
const char *dict_sys_t::s_dd_space_name = "mysql";
/** The file name of the data dictionary tablespace */
const char *dict_sys_t::s_dd_space_file_name = "mysql.ibd";
/** The name of the hard-coded system tablespace. */
const char *dict_sys_t::s_sys_space_name = "innodb_system";
/** The name of the predefined temporary tablespace. */
const char *dict_sys_t::s_temp_space_name = "innodb_temporary";
/** The file name of the predefined temporary tablespace */
const char *dict_sys_t::s_temp_space_file_name = "ibtmp1";
/** The hard-coded tablespace name innodb_file_per_table. */
const char *dict_sys_t::s_file_per_table_name = "innodb_file_per_table";
/** These two undo tablespaces cannot be dropped. */
const char *dict_sys_t::s_default_undo_space_name_1 = "innodb_undo_001";
const char *dict_sys_t::s_default_undo_space_name_2 = "innodb_undo_002";
/** the dictionary persisting structure */
dict_persist_t *dict_persist = nullptr;
/** @brief the data dictionary rw-latch protecting dict_sys
table create, drop, etc. reserve this in X-mode; implicit or
background operations purge, rollback, foreign key checks reserve this
in S-mode; we cannot trust that MySQL protects implicit or background
operations a table drop since MySQL does not know of them; therefore
we need this; NOTE: a transaction which reserves this must keep book
on the mode in trx_t::dict_operation_lock_mode */
rw_lock_t *dict_operation_lock;
/** Percentage of compression failures that are allowed in a single
round */
ulong zip_failure_threshold_pct = 5;
/** Maximum percentage of a page that can be allowed as a pad to avoid
compression failures */
ulong zip_pad_max = 50;
/** buffer pool max size per table hash table fixed size in bytes */
constexpr uint32_t DICT_POOL_PER_TABLE_HASH = 512;
#ifndef UNIV_HOTBACKUP
/** Identifies generated InnoDB foreign key names */
static char dict_ibfk[] = "_ibfk_";
/** Array to store table_ids of INNODB_SYS_* TABLES */
static table_id_t dict_sys_table_id[SYS_NUM_SYSTEM_TABLES];
/** Tries to find column names for the index and sets the col field of the
index.
@param[in] table table
@param[in,out] index index
@param[in] add_v new virtual columns added along with an add index call
@return true if the column names were found */
static bool dict_index_find_and_set_cols(const dict_table_t *table,
dict_index_t *index,
const dict_add_v_col_t *add_v);
/** Builds the internal dictionary cache representation for a clustered
index, containing also system fields not defined by the user.
@return own: the internal representation of the clustered index */
static dict_index_t *dict_index_build_internal_clust(
const dict_table_t *table, /*!< in: table */
dict_index_t *index); /*!< in: user representation of
a clustered index */
/** Builds the internal dictionary cache representation for a non-clustered
index, containing also system fields not defined by the user.
@return own: the internal representation of the non-clustered index */
static dict_index_t *dict_index_build_internal_non_clust(
const dict_table_t *table, /*!< in: table */
dict_index_t *index); /*!< in: user representation of
a non-clustered index */
/** Builds the internal dictionary cache representation for an FTS index.
@return own: the internal representation of the FTS index */
static dict_index_t *dict_index_build_internal_fts(
dict_table_t *table, /*!< in: table */
dict_index_t *index); /*!< in: user representation of an FTS index */
/** Removes an index from the dictionary cache. */
static void dict_index_remove_from_cache_low(
dict_table_t *table, /*!< in/out: table */
dict_index_t *index, /*!< in, own: index */
bool lru_evict); /*!< in: true if page being evicted
to make room in the table LRU list */
/** Calculate and update the redo log margin for current tables which
have some changed dynamic metadata in memory and have not been written
back to mysql.innodb_dynamic_metadata. Update LSN limit, which is used
to stop user threads when redo log is running out of space and they
do not hold latches (log.free_check_limit_lsn). */
static void dict_persist_update_log_margin(void);
/** Removes a table object from the dictionary cache. */
static void dict_table_remove_from_cache_low(
dict_table_t *table, /*!< in, own: table */
bool lru_evict); /*!< in: true if evicting from LRU */
#ifdef UNIV_DEBUG
/** Validate the dictionary table LRU list.
@return true if validate OK */
static bool dict_lru_validate();
/** Check if table is in the dictionary table LRU list.
@param[in] find_table table to find
@return true if table found in LRU list. */
static bool dict_lru_find_table(const dict_table_t *find_table);
/** Check if a table exists in the dict table non-LRU list.
@param[in] find_table table to find
@return true if table found in non-LRU list */
static bool dict_non_lru_find_table(const dict_table_t *find_table);
#endif /* UNIV_DEBUG */
/* Stream for storing detailed information about the latest foreign key
and unique key errors. Only created if !srv_read_only_mode */
FILE *dict_foreign_err_file = nullptr;
/* mutex protecting the foreign and unique error buffers */
ib_mutex_t dict_foreign_err_mutex;
/** Checks if the database name in two table names is the same.
@return true if same db name */
bool dict_tables_have_same_db(const char *name1, /*!< in: table name in the
form dbname '/' tablename */
const char *name2) /*!< in: table name in the
form dbname '/' tablename */
{
for (; *name1 == *name2; name1++, name2++) {
if (*name1 == '/') {
return true;
}
ut_a(*name1); /* the names must contain '/' */
}
return false;
}
/** Return the end of table name where we have removed dbname and '/'.
@return table name */
const char *dict_remove_db_name(const char *name) /*!< in: table name in the
form dbname '/' tablename */
{
const char *s = strchr(name, '/');
ut_a(s);
return (s + 1);
}
#endif /* !UNIV_HOTBACKUP */
/** Get the database name length in a table name.
@return database name length */
ulint dict_get_db_name_len(const char *name) /*!< in: table name in the form
dbname '/' tablename */
{
const char *s;
s = strchr(name, '/');
if (s == nullptr) {
return (0);
}
return (s - name);
}
#ifndef UNIV_HOTBACKUP
/** Reserves the dictionary system mutex for MySQL. */
void dict_mutex_enter_for_mysql(void) { dict_sys_mutex_enter(); }
/** Releases the dictionary system mutex for MySQL. */
void dict_mutex_exit_for_mysql(void) { dict_sys_mutex_exit(); }
/** Allocate and init a dict_table_t's stats latch.
This function must not be called concurrently on the same table object.
@param[in,out] table_void table whose stats latch to create */
static void dict_table_stats_latch_alloc(void *table_void) {
dict_table_t *table = static_cast<dict_table_t *>(table_void);
/* Note: rw_lock_create() will call the constructor */
table->stats_latch = static_cast<rw_lock_t *>(
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(rw_lock_t)));
ut_a(table->stats_latch != nullptr);
rw_lock_create(dict_table_stats_key, table->stats_latch,
LATCH_ID_DICT_TABLE_STATS);
}
/** Deinit and free a dict_table_t's stats latch.
This function must not be called concurrently on the same table object.
@param[in,out] table table whose stats latch to free */
static void dict_table_stats_latch_free(dict_table_t *table) {
rw_lock_free(table->stats_latch);
ut::free(table->stats_latch);
}
/** Create a dict_table_t's stats latch or delay for lazy creation.
This function is only called from either single threaded environment
or from a thread that has not shared the table object with other threads.
@param[in,out] table table whose stats latch to create
@param[in] enabled if false then the latch is disabled
and dict_table_stats_lock()/unlock() become noop on this table. */
void dict_table_stats_latch_create(dict_table_t *table, bool enabled) {
if (!enabled) {
table->stats_latch = nullptr;
table->stats_latch_created = os_once::DONE;
return;
}
/* We create this lazily the first time it is used. */
table->stats_latch = nullptr;
table->stats_latch_created = os_once::NEVER_DONE;
}
/** Destroy a dict_table_t's stats latch.
This function is only called from either single threaded environment
or from a thread that has not shared the table object with other threads.
@param[in,out] table table whose stats latch to destroy */
void dict_table_stats_latch_destroy(dict_table_t *table) {
if (table->stats_latch_created == os_once::DONE &&
table->stats_latch != nullptr) {
dict_table_stats_latch_free(table);
}
}
/** Lock the appropriate latch to protect a given table's statistics.
@param[in] table table whose stats to lock
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */
void dict_table_stats_lock(dict_table_t *table, ulint latch_mode) {
ut_ad(table != nullptr);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
os_once::do_or_wait_for_done(&table->stats_latch_created,
dict_table_stats_latch_alloc, table);
if (table->stats_latch == nullptr) {
/* This is a dummy table object that is private in the current
thread and is not shared between multiple threads, thus we
skip any locking. */
return;
}
switch (latch_mode) {
case RW_S_LATCH:
rw_lock_s_lock(table->stats_latch, UT_LOCATION_HERE);
break;
case RW_X_LATCH:
rw_lock_x_lock(table->stats_latch, UT_LOCATION_HERE);
break;
case RW_NO_LATCH:
[[fallthrough]];
default:
ut_error;
}
}
/** Unlock the latch that has been locked by dict_table_stats_lock().
@param[in] table table whose stats to unlock
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */
void dict_table_stats_unlock(dict_table_t *table, ulint latch_mode) {
ut_ad(table != nullptr);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
if (table->stats_latch == nullptr) {
/* This is a dummy table object that is private in the current
thread and is not shared between multiple threads, thus we
skip any locking. */
return;
}
switch (latch_mode) {
case RW_S_LATCH:
rw_lock_s_unlock(table->stats_latch);
break;
case RW_X_LATCH:
rw_lock_x_unlock(table->stats_latch);
break;
case RW_NO_LATCH:
[[fallthrough]];
default:
ut_error;
}
}
/** Try to drop any indexes after an aborted index creation.
This can also be after a server kill during DROP INDEX. */
static void dict_table_try_drop_aborted(
dict_table_t *table, /*!< in: table, or NULL if it
needs to be looked up again */
table_id_t table_id, /*!< in: table identifier */
ulint ref_count) /*!< in: expected table->n_ref_count */
{
trx_t *trx;
trx = trx_allocate_for_background();
trx->op_info = "try to drop any indexes after an aborted index creation";
row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE);
trx_set_dict_operation(trx, TRX_DICT_OP_INDEX);
if (table == nullptr) {
table = dd_table_open_on_id(table_id, nullptr, nullptr, true, true);
/* Decrement the ref count. The table is MDL locked, so should
not be dropped */
if (table) {
dd_table_close(table, nullptr, nullptr, true);
}
} else {
ut_ad(table->id == table_id);
}
if (table && table->get_ref_count() == ref_count && table->drop_aborted) {
/* Silence a debug assertion in ddl::drop_indexes(). */
ut_d(table->acquire());
ddl::drop_indexes(trx, table, true);
ut_d(table->release());
ut_ad(table->get_ref_count() == ref_count);
trx_commit_for_mysql(trx);
}
row_mysql_unlock_data_dictionary(trx);
trx_free_for_background(trx);
}
/** When opening a table,
try to drop any indexes after an aborted index creation.
Release the dict_sys->mutex. */
static void dict_table_try_drop_aborted_and_mutex_exit(
dict_table_t *table, /*!< in: table (may be NULL) */
bool try_drop) /*!< in: false if should try to
drop indexes whose online creation
was aborted */
{
if (try_drop && table != nullptr && table->drop_aborted &&
table->get_ref_count() == 1 && table->first_index()) {
/* Attempt to drop the indexes whose online creation
was aborted. */
table_id_t table_id = table->id;
dict_sys_mutex_exit();
dict_table_try_drop_aborted(table, table_id, 1);
} else {
dict_sys_mutex_exit();
}
}
#endif /* !UNIV_HOTBACKUP */
/** Decrements the count of open handles to a table.
@param[in,out] table Table
@param[in] dict_locked True=data dictionary locked
@param[in] try_drop True=try to drop any orphan indexes after an aborted online
index creation */
void dict_table_close(dict_table_t *table, bool dict_locked, bool try_drop) {
ut_a(table->get_ref_count() > 0);
#ifndef UNIV_HOTBACKUP
#ifdef UNIV_DEBUG
if (!table->is_intrinsic()) {
/* This is now only for validation in debug mode */
if (!dict_locked) {
dict_sys_mutex_enter();
}
ut_ad(dict_lru_validate());
if (table->can_be_evicted) {
ut_ad(dict_lru_find_table(table));
} else {
ut_ad(dict_non_lru_find_table(table));
}
if (!dict_locked) {
dict_sys_mutex_exit();
}
}
#endif /* UNIV_DEBUG */
#endif /* !UNIV_HOTBACKUP */
if (!table->is_intrinsic()) {
/* Ask for lock to prevent concurrent table open,
in case the race of n_ref_count and stat_initialized in
dict_stats_deinit(). See dict_table_t::acquire_with_lock() too.
We don't actually need dict_sys mutex any more here. */
table->lock();
}
auto drop_aborted = try_drop && table->drop_aborted &&
table->get_ref_count() == 1 && table->first_index();
table->release();
#ifndef UNIV_HOTBACKUP
/* Intrinsic table is not added to dictionary cache so skip other
cache specific actions. */
if (table->is_intrinsic()) {
return;
}
/* Force persistent stats re-read upon next open of the table
so that FLUSH TABLE can be used to forcibly fetch stats from disk
if they have been manually modified. We reset table->stat_initialized
only if table reference count is 0 because we do not want too frequent
stats re-reads (e.g. in other cases than FLUSH TABLE). */
if (strchr(table->name.m_name, '/') != nullptr &&
table->get_ref_count() == 0 && dict_stats_is_persistent_enabled(table)) {
DEBUG_SYNC(current_thd, "innodb.before_stats_deinit");
dict_stats_deinit(table);
}
if (!dict_locked) {
if (drop_aborted) {
ut_d(ut_error);
ut_o(dict_table_try_drop_aborted(nullptr, table->id, 0));
}
}
#endif /* !UNIV_HOTBACKUP */
if (!table->is_intrinsic()) {
table->unlock();
}
}
#ifndef UNIV_HOTBACKUP
/** Closes the only open handle to a table and drops a table while assuring
that dict_sys->mutex is held the whole time. This assures that the table
is not evicted after the close when the count of open handles goes to zero.
Because dict_sys->mutex is held, we do not need to call
dict_table_prevent_eviction(). */
void dict_table_close_and_drop(
trx_t *trx, /*!< in: data dictionary transaction */
dict_table_t *table) /*!< in/out: table */
{
ut_ad(dict_sys_mutex_own());
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X));
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE));
dict_table_close(table, true, false);
#if defined UNIV_DEBUG || defined UNIV_DDL_DEBUG
/* Nobody should have initialized the stats of the newly created
table when this is called. So we know that it has not been added
for background stats gathering. */
ut_a(!table->stat_initialized);
#endif /* UNIV_DEBUG || UNIV_DDL_DEBUG */
ddl::drop_table(trx, table);
}
/** Check if the table has a given (non_virtual) column.
@param[in] table table object
@param[in] col_name column name
@param[in] col_nr column number guessed, 0 as default
@return column number if the table has the specified column,
otherwise table->n_def */
ulint dict_table_has_column(const dict_table_t *table, const char *col_name,
ulint col_nr) {
ulint col_max = table->n_def;
ut_ad(table);
ut_ad(col_name);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
if (col_nr < col_max &&
innobase_strcasecmp(col_name, table->get_col_name(col_nr)) == 0) {
return (col_nr);
}
/** The order of column may changed, check it with other columns */
for (ulint i = 0; i < col_max; i++) {
if (i != col_nr &&
innobase_strcasecmp(col_name, table->get_col_name(i)) == 0) {
return (i);
}
}
return (col_max);
}
/** Returns a virtual column's name.
@param[in] table target table
@param[in] col_nr virtual column number (nth virtual column)
@return column name or NULL if column number out of range. */
const char *dict_table_get_v_col_name(const dict_table_t *table, ulint col_nr) {
const char *s;
ut_ad(table);
ut_ad(col_nr < table->n_v_def);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
if (col_nr >= table->n_v_def) {
return (nullptr);
}
s = table->v_col_names;
if (s != nullptr) {
for (ulint i = 0; i < col_nr; i++) {
s += strlen(s) + 1;
}
}
return (s);
}
/** Search virtual column's position in InnoDB according to its position
in original table's position
@param[in] table target table
@param[in] col_nr column number (nth column in the MySQL table)
@return virtual column's position in InnoDB, ULINT_UNDEFINED if not find */
static ulint dict_table_get_v_col_pos_for_mysql(const dict_table_t *table,
ulint col_nr) {
ulint i;
ut_ad(table);
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N);
for (i = 0; i < table->n_v_def; i++) {
if (col_nr == dict_get_v_col_mysql_pos(table->v_cols[i].m_col.ind)) {
break;
}
}
if (i == table->n_v_def) {
return (ULINT_UNDEFINED);
}
ut_ad(col_nr < static_cast<ulint>(table->n_t_def));
return (i);
}
/** Returns a virtual column's name according to its original
MySQL table position.
@param[in] table target table
@param[in] col_nr column number (nth column in the table)
@return column name. */
const char *dict_table_get_v_col_name_mysql(const dict_table_t *table,
ulint col_nr) {
ulint i = dict_table_get_v_col_pos_for_mysql(table, col_nr);
if (i == ULINT_UNDEFINED) {
return (nullptr);
}
return (dict_table_get_v_col_name(table, i));
}
/** Get nth virtual column according to its original MySQL table position
@param[in] table target table
@param[in] col_nr column number in MySQL Table definition
@return dict_v_col_t ptr */
dict_v_col_t *dict_table_get_nth_v_col_mysql(const dict_table_t *table,
ulint col_nr) {
ulint i = dict_table_get_v_col_pos_for_mysql(table, col_nr);
if (i == ULINT_UNDEFINED) {
return (nullptr);
}
return (dict_table_get_nth_v_col(table, i));
}
/** Allocate and init the autoinc latch of a given table.
This function must not be called concurrently on the same table object.
@param[in,out] table_void table whose autoinc latch to create */
static void dict_table_autoinc_alloc(void *table_void) {
dict_table_t *table = static_cast<dict_table_t *>(table_void);
table->autoinc_mutex = ut::new_withkey<ib_mutex_t>(UT_NEW_THIS_FILE_PSI_KEY);
ut_a(table->autoinc_mutex != nullptr);
mutex_create(LATCH_ID_AUTOINC, table->autoinc_mutex);
table->autoinc_persisted_mutex =
ut::new_withkey<ib_mutex_t>(UT_NEW_THIS_FILE_PSI_KEY);
ut_a(table->autoinc_persisted_mutex != nullptr);
mutex_create(LATCH_ID_PERSIST_AUTOINC, table->autoinc_persisted_mutex);
}
/** Allocate and init the zip_pad_mutex of a given index.
This function must not be called concurrently on the same index object.
@param[in,out] index_void index whose zip_pad_mutex to create */
static void dict_index_zip_pad_alloc(void *index_void) {
dict_index_t *index = static_cast<dict_index_t *>(index_void);
index->zip_pad.mutex = ut::new_withkey<SysMutex>(UT_NEW_THIS_FILE_PSI_KEY);
ut_a(index->zip_pad.mutex != nullptr);
mutex_create(LATCH_ID_ZIP_PAD_MUTEX, index->zip_pad.mutex);
}
/** Acquire the autoinc lock. */
void dict_table_autoinc_lock(dict_table_t *table) /*!< in/out: table */
{
os_once::do_or_wait_for_done(&table->autoinc_mutex_created,
dict_table_autoinc_alloc, table);
mutex_enter(table->autoinc_mutex);
}
/** Acquire the zip_pad_mutex latch.
@param[in,out] index the index whose zip_pad_mutex to acquire.*/
static void dict_index_zip_pad_lock(dict_index_t *index) {
os_once::do_or_wait_for_done(&index->zip_pad.mutex_created,
dict_index_zip_pad_alloc, index);
mutex_enter(index->zip_pad.mutex);
}
/** Unconditionally set the autoinc counter.
@param[in,out] table Table
@param[in] value Next value to assign to a row */
void dict_table_autoinc_initialize(dict_table_t *table, uint64_t value) {
ut_ad(dict_table_autoinc_own(table));
table->autoinc = value;
}
bool dict_table_autoinc_log(dict_table_t *table, uint64_t value, mtr_t *mtr) {
bool log = false;
mutex_enter(table->autoinc_persisted_mutex);
if (table->autoinc_persisted < value) {
dict_table_autoinc_persisted_update(table, value);
/* The only concern here is some concurrent thread may
change the dirty_status to METADATA_BUFFERED. And the
only function is dict_table_persist_to_dd_table_buffer_low(),
which could be called by checkpoint and will first set the
dirty_status to METADATA_BUFFERED, and then write back
the latest changes to DDTableBuffer, all of which are under
protection of dict_persist->mutex.
If that function sets the dirty_status to METADATA_BUFFERED
first, below checking will force current thread to wait on
dict_persist->mutex. Above update to AUTOINC would be either
written back to DDTableBuffer or not. But the redo logs for
current change won't be counted into current checkpoint.
See how log_sys->dict_max_allowed_checkpoint_lsn is set.
So even a crash after below redo log flushed, no change lost.
If that function sets the dirty_status after below checking,
which means current change would be written back to
DDTableBuffer. It's also safe. */
if (table->dirty_status.load() == METADATA_DIRTY) {
ut_ad(table->in_dirty_dict_tables_list);
} else {
dict_table_mark_dirty(table);
}
log = true;
}
mutex_exit(table->autoinc_persisted_mutex);
if (log) {
PersistentTableMetadata metadata(table->id, table->version);
metadata.set_autoinc(value);
Persister *persister = dict_persist->persisters->get(PM_TABLE_AUTO_INC);
persister->write_log(table->id, metadata, mtr);
/* No need to flush due to performance reason */
}
/* Check and return if auto increment is to be persisted. */
return (log && dict_persist->check_persist_immediately());
}
/** Get all the FTS indexes on a table.
@param[in] table table
@param[out] indexes all FTS indexes on this table
@return number of FTS indexes */
ulint dict_table_get_all_fts_indexes(dict_table_t *table,
ib_vector_t *indexes) {
dict_index_t *index;
ut_a(ib_vector_size(indexes) == 0);
for (index = table->first_index(); index; index = index->next()) {
if (index->type == DICT_FTS) {
ib_vector_push(indexes, &index);
}
}
return (ib_vector_size(indexes));
}
/** Reads the next autoinc value (== autoinc counter value), 0 if not yet
initialized.
@return value for a new row, or 0 */
uint64_t dict_table_autoinc_read(const dict_table_t *table) /*!< in: table */
{
ut_ad(dict_table_autoinc_own(table));
return (table->autoinc);
}
/** Updates the autoinc counter if the value supplied is greater than the
current value.
@param[in,out] table Table
@param[in] value Value which was assigned to a row */
void dict_table_autoinc_update_if_greater(dict_table_t *table, uint64_t value) {
ut_ad(dict_table_autoinc_own(table));
if (value > table->autoinc) {
table->autoinc = value;
}
}
/** Release the autoinc lock. */
void dict_table_autoinc_unlock(dict_table_t *table) /*!< in/out: table */
{
mutex_exit(table->autoinc_mutex);
}
bool dict_index_contains_col_or_prefix(const dict_index_t *index, ulint n,
bool is_virtual) {
const dict_field_t *field;
const dict_col_t *col;
ulint pos;
ulint n_fields;
ut_ad(index);
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
if (index->is_clustered()) {
return true;
}
if (is_virtual) {
col = &dict_table_get_nth_v_col(index->table, n)->m_col;
} else {
col = index->table->get_col(n);
}
n_fields = dict_index_get_n_fields(index);
for (pos = 0; pos < n_fields; pos++) {
field = index->get_field(pos);
if (col == field->col) {
return true;
}
}
return false;
}
/** Looks for a matching field in an index. The column has to be the same. The
column in index must be complete, or must contain a prefix longer than the
column in index2. That is, we must be able to construct the prefix in index2
from the prefix in index.
@return position in internal representation of the index;
ULINT_UNDEFINED if not contained */
ulint dict_index_get_nth_field_pos(
const dict_index_t *index, /*!< in: index from which to search */
const dict_index_t *index2, /*!< in: index */
ulint n) /*!< in: field number in index2 */
{
const dict_field_t *field;
const dict_field_t *field2;
ulint n_fields;
ulint pos;
ut_ad(index);
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N);
field2 = index2->get_field(n);
n_fields = dict_index_get_n_fields(index);
/* Are we looking for a MBR (Minimum Bound Box) field of
a spatial index */
bool is_mbr_fld = (n == 0 && dict_index_is_spatial(index2));
for (pos = 0; pos < n_fields; pos++) {
field = index->get_field(pos);
/* The first field of a spatial index is a transformed
MBR (Minimum Bound Box) field made out of original column,
so its field->col still points to original cluster index
col, but the actual content is different. So we cannot
consider them equal if neither of them is MBR field */
if (pos == 0 && dict_index_is_spatial(index) && !is_mbr_fld) {
continue;
}
if (field->col == field2->col &&
(field->prefix_len == 0 || (field->prefix_len >= field2->prefix_len &&
field2->prefix_len != 0))) {
return (pos);
}
}
return (ULINT_UNDEFINED);
}
/** Looks for non-virtual column n position in the clustered index.
@return position in internal representation of the clustered index */
ulint dict_table_get_nth_col_pos(const dict_table_t *table, /*!< in: table */
ulint n) /*!< in: column number */
{
return (table->first_index()->get_col_pos(n));
}
/** Get the innodb column position for a non-virtual column according to
its original MySQL table position n
@param[in] table table
@param[in] n MySQL column position
@return column position in InnoDB */
ulint dict_table_mysql_pos_to_innodb(const dict_table_t *table, ulint n) {
ut_ad(n < table->n_t_cols);
if (table->n_v_def == 0) {
/* No virtual columns, the MySQL position is the same
as InnoDB position */
return (n);
}
/* Find out how many virtual columns are stored in front of 'n' */
ulint v_before = 0;
for (ulint i = 0; i < table->n_v_def; ++i) {
if (table->v_cols[i].m_col.ind > n) {
break;
}
++v_before;
}
ut_ad(n >= v_before);
return (n - v_before);
}
/** Checks if a column is in the ordering columns of the clustered index of a
table. Column prefixes are treated like whole columns.
@return true if the column, or its prefix, is in the clustered key */
bool dict_table_col_in_clustered_key(
const dict_table_t *table, /*!< in: table */
ulint n) /*!< in: column number */
{
const dict_index_t *index;
const dict_field_t *field;
const dict_col_t *col;
ulint pos;
ulint n_fields;
ut_ad(table);
col = table->get_col(n);
index = table->first_index();
n_fields = dict_index_get_n_unique(index);
for (pos = 0; pos < n_fields; pos++) {
field = index->get_field(pos);
if (col == field->col) {
return true;