Debian Codegraph @

storage/innobase/dict/dict0load.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file dict/dict0load.cc
29 Loads to the memory cache database object definitions
30 from dictionary tables
31
32 Created 4/24/1996 Heikki Tuuri
33 *******************************************************/
34
35 #include "current_thd.h"
36 #include "ha_prototypes.h"
37
38 #include <set>
39 #include <stack>
40 #include "dict0load.h"
41
42 #include "btr0btr.h"
43 #include "btr0pcur.h"
44 #include "dict0boot.h"
45 #include "dict0crea.h"
46 #include "dict0dd.h"
47 #include "dict0dict.h"
48 #include "dict0load.h"
49 #include "dict0mem.h"
50 #include "dict0priv.h"
51 #include "dict0stats.h"
52 #include "fsp0file.h"
53 #include "fsp0sysspace.h"
54 #include "fts0priv.h"
55 #include "ha_prototypes.h"
56 #include "mach0data.h"
57
58 #include "my_dbug.h"
59
60 #include "fil0fil.h"
61 #include "fts0fts.h"
62 #include "mysql_version.h"
63 #include "page0page.h"
64 #include "rem0cmp.h"
65 #include "srv0srv.h"
66 #include "srv0start.h"
67 #include "ut0math.h"
68
69 /** Following are the InnoDB system tables. The positions in
70 this array are referenced by enum dict_system_table_id. */
71 const char *SYSTEM_TABLE_NAME[] = {
72 "SYS_TABLES", "SYS_INDEXES", "SYS_COLUMNS",
73 "SYS_FIELDS", "SYS_FOREIGN", "SYS_FOREIGN_COLS",
74 "SYS_TABLESPACES", "SYS_DATAFILES", "SYS_VIRTUAL"};
75
76 /** This variant is based on name comparison and is used because
77 system table id array is not built yet.
78 @param[in] name InnoDB table name
79 @return true if table name is InnoDB SYSTEM table */
80 static bool dict_load_is_system_table(const char *name) {
81 ut_ad(name != nullptr);
82 uint32_t size = sizeof(SYSTEM_TABLE_NAME) / sizeof(char *);
83 for (uint32_t i = 0; i < size; i++) {
84 if (strcmp(name, SYSTEM_TABLE_NAME[i]) == 0) {
85 return (true);
86 }
87 }
88 return (false);
89 }
90 /* This is set of tablespaces that are not found in SYS_TABLESPACES.
91 InnoDB tablespaces before 5.6 are not registered in SYS_TABLESPACES.
92 So we maintain a std::set, which is later used to register the
93 tablespaces to dictionary table mysql.tablespaces */
94 missing_sys_tblsp_t missing_spaces;
95
96 /** This bool denotes if we found a Table or Partition with discarded Tablespace
97 during load of SYS_TABLES (in dict_check_sys_tables).
98
99 We use it to stop upgrade from 5.7 to 8.0 if there are discarded Tablespaces. */
100 bool has_discarded_tablespaces = false;
101
102 /** Loads a table definition and also all its index definitions.
103
104 Loads those foreign key constraints whose referenced table is already in
105 dictionary cache. If a foreign key constraint is not loaded, then the
106 referenced table is pushed into the output stack (fk_tables), if it is not
107 NULL. These tables must be subsequently loaded so that all the foreign
108 key constraints are loaded into memory.
109
110 @param[in] name Table name in the db/tablename format
111 @param[in] cached true=add to cache, false=do not
112 @param[in] ignore_err Error to be ignored when loading table
113 and its index definition
114 @param[out] fk_tables Related table names that must also be
115 loaded to ensure that all foreign key
116 constraints are loaded.
117 @param[in] prev_table previous table name. The current table load
118 is happening because of the load of the
119 previous table name. This parameter is used
120 to check for cyclic calls.
121 @return table, NULL if does not exist; if the table is stored in an
122 .ibd file, but the file does not exist, then we set the
123 ibd_file_missing flag true in the table object we return */
124 static dict_table_t *dict_load_table_one(table_name_t &name, bool cached,
125 dict_err_ignore_t ignore_err,
126 dict_names_t &fk_tables,
127 const std::string *prev_table);
128
129 /** Loads a table definition from a SYS_TABLES record to dict_table_t.
130 Does not load any columns or indexes.
131 @param[in] name Table name
132 @param[in] rec SYS_TABLES record
133 @param[out] table Table, or NULL
134 @return error message, or NULL on success */
135 static const char *dict_load_table_low(table_name_t &name, const rec_t *rec,
136 dict_table_t **table);
137
138 /* If this flag is true, then we will load the cluster index's (and tables')
139 metadata even if it is marked as "corrupted". */
140 bool srv_load_corrupted = false;
141
142 #ifdef UNIV_DEBUG
143 /** Compare the name of an index column.
144 @return true if the i'th column of index is 'name'. */
145 static bool name_of_col_is(const dict_table_t *table, /*!< in: table */
146 const dict_index_t *index, /*!< in: index */
147 ulint i, /*!< in: index field offset */
148 const char *name) /*!< in: name to compare to */
149 {
150 ulint tmp = dict_col_get_no(index->get_field(i)->col);
151
152 return (strcmp(name, table->get_col_name(tmp)) == 0);
153 }
154 #endif /* UNIV_DEBUG */
155
156 /** Finds the first table name in the given database.
157 @return own: table name, NULL if does not exist; the caller must free
158 the memory in the string! */
159 char *dict_get_first_table_name_in_db(
160 const char *name) /*!< in: database name which ends in '/' */
161 {
162 dict_table_t *sys_tables;
163 btr_pcur_t pcur;
164 dict_index_t *sys_index;
165 dtuple_t *tuple;
166 mem_heap_t *heap;
167 dfield_t *dfield;
168 const rec_t *rec;
169 const byte *field;
170 ulint len;
171 mtr_t mtr;
172
173 ut_ad(dict_sys_mutex_own());
174
175 heap = mem_heap_create(100, UT_LOCATION_HERE);
176
177 mtr_start(&mtr);
178
179 sys_tables = dict_table_get_low("SYS_TABLES");
180 sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
181 ut_ad(!dict_table_is_comp(sys_tables));
182
183 tuple = dtuple_create(heap, 1);
184 dfield = dtuple_get_nth_field(tuple, 0);
185
186 dfield_set_data(dfield, name, ut_strlen(name));
187 dict_index_copy_types(tuple, sys_index, 1);
188
189 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
190 UT_LOCATION_HERE);
191 loop:
192 rec = pcur.get_rec();
193
194 if (!pcur.is_on_user_rec()) {
195 /* Not found */
196
197 pcur.close();
198 mtr_commit(&mtr);
199 mem_heap_free(heap);
200
201 return (nullptr);
202 }
203
204 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__NAME, &len);
205
206 if (len < strlen(name) || ut_memcmp(name, field, strlen(name)) != 0) {
207 /* Not found */
208
209 pcur.close();
210 mtr_commit(&mtr);
211 mem_heap_free(heap);
212
213 return (nullptr);
214 }
215
216 if (!rec_get_deleted_flag(rec, 0)) {
217 /* We found one */
218
219 char *table_name = mem_strdupl((char *)field, len);
220
221 pcur.close();
222 mtr_commit(&mtr);
223 mem_heap_free(heap);
224
225 return (table_name);
226 }
227
228 pcur.move_to_next_user_rec(&mtr);
229
230 goto loop;
231 }
232
233 /** This function gets the next system table record as it scans the table.
234 @return the next record if found, NULL if end of scan */
235 static const rec_t *dict_getnext_system_low(
236 btr_pcur_t *pcur, /*!< in/out: persistent cursor to the
237 record*/
238 mtr_t *mtr) /*!< in: the mini-transaction */
239 {
240 rec_t *rec = nullptr;
241
242 while (!rec || rec_get_deleted_flag(rec, 0)) {
243 pcur->move_to_next_user_rec(mtr);
244
245 rec = pcur->get_rec();
246
247 if (!pcur->is_on_user_rec()) {
248 /* end of index */
249 pcur->close();
250
251 return (nullptr);
252 }
253 }
254
255 /* Get a record, let's save the position */
256 pcur->store_position(mtr);
257
258 return (rec);
259 }
260
261 /** This function opens a system table, and returns the first record.
262 @return first record of the system table */
263 const rec_t *dict_startscan_system(
264 btr_pcur_t *pcur, /*!< out: persistent cursor to
265 the record */
266 mtr_t *mtr, /*!< in: the mini-transaction */
267 dict_system_id_t system_id) /*!< in: which system table to open */
268 {
269 dict_table_t *system_table;
270 dict_index_t *clust_index;
271 const rec_t *rec;
272
273 ut_a(system_id < SYS_NUM_SYSTEM_TABLES);
274
275 system_table = dict_table_get_low(SYSTEM_TABLE_NAME[system_id]);
276
277 clust_index = UT_LIST_GET_FIRST(system_table->indexes);
278
279 pcur->open_at_side(true, clust_index, BTR_SEARCH_LEAF, true, 0, mtr);
280
281 rec = dict_getnext_system_low(pcur, mtr);
282
283 return (rec);
284 }
285
286 /** This function gets the next system table record as it scans the table.
287 @return the next record if found, NULL if end of scan */
288 const rec_t *dict_getnext_system(btr_pcur_t *pcur, /*!< in/out: persistent
289 cursor to the record */
290 mtr_t *mtr) /*!< in: the mini-transaction */
291 {
292 const rec_t *rec;
293
294 /* Restore the position */
295 pcur->restore_position(BTR_SEARCH_LEAF, mtr, UT_LOCATION_HERE);
296
297 /* Get the next record */
298 rec = dict_getnext_system_low(pcur, mtr);
299
300 return (rec);
301 }
302
303 /** Error message for a delete-marked record in dict_load_index_low() */
304 static const char *dict_load_index_del = "delete-marked record in SYS_INDEXES";
305 /** Error message for table->id mismatch in dict_load_index_low() */
306 static const char *dict_load_index_id_err = "SYS_INDEXES.TABLE_ID mismatch";
307
308 /** Load an index definition from a SYS_INDEXES record to dict_index_t.
309 If allocate=true, we will create a dict_index_t structure and fill it
310 accordingly. If allocated=false, the dict_index_t will be supplied by
311 the caller and filled with information read from the record. @return
312 error message, or NULL on success */
313 static const char *dict_load_index_low(
314 byte *table_id, /*!< in/out: table id (8 bytes),
315 an "in" value if allocate=true
316 and "out" when allocate=false */
317 const char *table_name, /*!< in: table name */
318 mem_heap_t *heap, /*!< in/out: temporary memory heap */
319 const rec_t *rec, /*!< in: SYS_INDEXES record */
320 bool allocate, /*!< in: true=allocate *index,
321 false=fill in a pre-allocated
322 *index */
323 dict_index_t **index) /*!< out,own: index, or NULL */
324 {
325 const byte *field;
326 ulint len;
327 ulint name_len;
328 const char *name_buf;
329 space_index_t id;
330 ulint n_fields;
331 ulint type;
332 ulint space;
333 ulint merge_threshold;
334
335 if (allocate) {
336 /* If allocate=true, no dict_index_t will
337 be supplied. Initialize "*index" to NULL */
338 *index = nullptr;
339 }
340
341 if (rec_get_deleted_flag(rec, 0)) {
342 return (dict_load_index_del);
343 }
344
345 if (rec_get_n_fields_old_raw(rec) == DICT_NUM_FIELDS__SYS_INDEXES) {
346 /* MERGE_THRESHOLD exists */
347 field = rec_get_nth_field_old(nullptr, rec,
348 DICT_FLD__SYS_INDEXES__MERGE_THRESHOLD, &len);
349 switch (len) {
350 case 4:
351 merge_threshold = mach_read_from_4(field);
352 break;
353 case UNIV_SQL_NULL:
354 merge_threshold = DICT_INDEX_MERGE_THRESHOLD_DEFAULT;
355 break;
356 default:
357 return (
358 "incorrect MERGE_THRESHOLD length"
359 " in SYS_INDEXES");
360 }
361 } else if (rec_get_n_fields_old_raw(rec) ==
362 DICT_NUM_FIELDS__SYS_INDEXES - 1) {
363 /* MERGE_THRESHOLD doesn't exist */
364
365 merge_threshold = DICT_INDEX_MERGE_THRESHOLD_DEFAULT;
366 } else {
367 return ("wrong number of columns in SYS_INDEXES record");
368 }
369
370 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__TABLE_ID,
371 &len);
372 if (len != 8) {
373 err_len:
374 return ("incorrect column length in SYS_INDEXES");
375 }
376
377 if (!allocate) {
378 /* We are reading a SYS_INDEXES record. Copy the table_id */
379 memcpy(table_id, (const char *)field, 8);
380 } else if (memcmp(field, table_id, 8)) {
381 /* Caller supplied table_id, verify it is the same
382 id as on the index record */
383 return (dict_load_index_id_err);
384 }
385
386 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__ID, &len);
387 if (len != 8) {
388 goto err_len;
389 }
390
391 id = mach_read_from_8(field);
392
393 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_INDEXES__DB_TRX_ID,
394 &len);
395 if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
396 goto err_len;
397 }
398 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_INDEXES__DB_ROLL_PTR,
399 &len);
400 if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
401 goto err_len;
402 }
403
404 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__NAME,
405 &name_len);
406 if (name_len == UNIV_SQL_NULL) {
407 goto err_len;
408 }
409
410 name_buf = mem_heap_strdupl(heap, (const char *)field, name_len);
411
412 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__N_FIELDS,
413 &len);
414 if (len != 4) {
415 goto err_len;
416 }
417 n_fields = mach_read_from_4(field);
418
419 field =
420 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__TYPE, &len);
421 if (len != 4) {
422 goto err_len;
423 }
424 type = mach_read_from_4(field);
425 if (type & (~0U << DICT_IT_BITS)) {
426 return ("unknown SYS_INDEXES.TYPE bits");
427 }
428
429 field =
430 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__SPACE, &len);
431 if (len != 4) {
432 goto err_len;
433 }
434 space = mach_read_from_4(field);
435
436 field =
437 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__PAGE_NO, &len);
438 if (len != 4) {
439 goto err_len;
440 }
441
442 if (srv_is_upgrade_mode) {
443 if (strcmp(name_buf, innobase_index_reserve_name) == 0) {
444 /* Data dictinory uses PRIMARY instead of GEN_CLUST_INDEX. */
445 name_buf = "PRIMARY";
446 }
447 }
448
449 if (srv_is_upgrade_mode) {
450 fts_aux_table_t fts_table;
451 bool is_fts =
452 fts_is_aux_table_name(&fts_table, table_name, strlen(table_name));
453
454 if (is_fts) {
455 switch (fts_table.type) {
456 case FTS_INDEX_TABLE:
457 name_buf = FTS_INDEX_TABLE_IND_NAME;
458 break;
459 case FTS_COMMON_TABLE:
460 name_buf = FTS_COMMON_TABLE_IND_NAME;
461 break;
462 case FTS_OBSOLETED_TABLE:
463 break;
464 /* do nothing */
465 }
466 }
467 }
468
469 if (allocate) {
470 *index = dict_mem_index_create(table_name, name_buf, space, type, n_fields);
471 } else {
472 ut_a(*index);
473
474 dict_mem_fill_index_struct(*index, nullptr, nullptr, name_buf, space, type,
475 n_fields);
476 }
477
478 (*index)->id = id;
479 (*index)->page = mach_read_from_4(field);
480 ut_ad((*index)->page);
481 (*index)->merge_threshold = merge_threshold;
482
483 return (nullptr);
484 }
485
486 /** Error message for a delete-marked record in dict_load_column_low() */
487 static const char *dict_load_column_del = "delete-marked record in SYS_COLUMN";
488
489 /** Load a table column definition from a SYS_COLUMNS record to
490 dict_table_t.
491 @return error message, or NULL on success */
492 static const char *dict_load_column_low(
493 dict_table_t *table, /*!< in/out: table, could be NULL
494 if we just populate a dict_column_t
495 struct with information from
496 a SYS_COLUMNS record */
497 mem_heap_t *heap, /*!< in/out: memory heap
498 for temporary storage */
499 dict_col_t *column, /*!< out: dict_column_t to fill,
500 or NULL if table != NULL */
501 table_id_t *table_id, /*!< out: table id */
502 const char **col_name, /*!< out: column name */
503 const rec_t *rec, /*!< in: SYS_COLUMNS record */
504 ulint *nth_v_col) /*!< out: if not NULL, this
505 records the "n" of "nth" virtual
506 column */
507 {
508 char *name;
509 const byte *field;
510 ulint len;
511 ulint mtype;
512 ulint prtype;
513 ulint col_len;
514 ulint pos;
515 ulint num_base;
516
517 ut_ad(table || column);
518
519 if (rec_get_deleted_flag(rec, 0)) {
520 return (dict_load_column_del);
521 }
522
523 if (rec_get_n_fields_old_raw(rec) != DICT_NUM_FIELDS__SYS_COLUMNS) {
524 return ("wrong number of columns in SYS_COLUMNS record");
525 }
526
527 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__TABLE_ID,
528 &len);
529 if (len != 8) {
530 err_len:
531 return ("incorrect column length in SYS_COLUMNS");
532 }
533
534 if (table_id) {
535 *table_id = mach_read_from_8(field);
536 } else if (table->id != mach_read_from_8(field)) {
537 return ("SYS_COLUMNS.TABLE_ID mismatch");
538 }
539
540 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__POS, &len);
541 if (len != 4) {
542 goto err_len;
543 }
544
545 pos = mach_read_from_4(field);
546
547 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__DB_TRX_ID,
548 &len);
549 if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
550 goto err_len;
551 }
552 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__DB_ROLL_PTR,
553 &len);
554 if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
555 goto err_len;
556 }
557
558 field =
559 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__NAME, &len);
560 if (len == 0 || len == UNIV_SQL_NULL) {
561 goto err_len;
562 }
563
564 name = mem_heap_strdupl(heap, (const char *)field, len);
565
566 if (col_name) {
567 *col_name = name;
568 }
569
570 field =
571 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__MTYPE, &len);
572 if (len != 4) {
573 goto err_len;
574 }
575
576 mtype = mach_read_from_4(field);
577
578 field =
579 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__PRTYPE, &len);
580 if (len != 4) {
581 goto err_len;
582 }
583 prtype = mach_read_from_4(field);
584
585 if (dtype_get_charset_coll(prtype) == 0 && dtype_is_string_type(mtype)) {
586 /* The table was created with < 4.1.2. */
587
588 if (dtype_is_binary_string_type(mtype, prtype)) {
589 /* Use the binary collation for
590 string columns of binary type. */
591
592 prtype = dtype_form_prtype(prtype, DATA_MYSQL_BINARY_CHARSET_COLL);
593 } else {
594 /* Use the default charset for
595 other than binary columns. */
596
597 prtype = dtype_form_prtype(prtype, data_mysql_default_charset_coll);
598 }
599 }
600
601 if (table && table->n_def != pos && !(prtype & DATA_VIRTUAL)) {
602 return ("SYS_COLUMNS.POS mismatch");
603 }
604
605 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__LEN, &len);
606 if (len != 4) {
607 goto err_len;
608 }
609 col_len = mach_read_from_4(field);
610 field =
611 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_COLUMNS__PREC, &len);
612 if (len != 4) {
613 goto err_len;
614 }
615
616 num_base = mach_read_from_4(field);
617
618 if (column == nullptr) {
619 if (prtype & DATA_VIRTUAL) {
620 #ifdef UNIV_DEBUG
621 dict_v_col_t *vcol =
622 #endif
623 dict_mem_table_add_v_col(table, heap, name, mtype, prtype, col_len,
624 dict_get_v_col_mysql_pos(pos), num_base,
625 true);
626 ut_ad(vcol->v_pos == dict_get_v_col_pos(pos));
627 } else {
628 ut_ad(num_base == 0);
629 dict_mem_table_add_col(table, heap, name, mtype, prtype, col_len, true);
630 }
631 } else {
632 dict_mem_fill_column_struct(column, pos, mtype, prtype, col_len, true,
633 UINT32_UNDEFINED, 0, 0);
634 }
635
636 /* Report the virtual column number */
637 if (prtype & DATA_VIRTUAL && nth_v_col != nullptr) {
638 *nth_v_col = dict_get_v_col_pos(pos);
639 }
640
641 return (nullptr);
642 }
643
644 /** Error message for a delete-marked record in dict_load_virtual_low() */
645 static const char *dict_load_virtual_del =
646 "delete-marked record in SYS_VIRTUAL";
647
648 /** Loads a virtual column "mapping" (to base columns) information
649 from a SYS_VIRTUAL record
650 @param[in,out] table table
651 @param[in,out] column mapped base column's dict_column_t
652 @param[in,out] table_id table id
653 @param[in,out] pos virtual column position
654 @param[in,out] base_pos base column position
655 @param[in] rec SYS_VIRTUAL record
656 @return error message, or NULL on success */
657 static const char *dict_load_virtual_low(dict_table_t *table,
658 dict_col_t **column,
659 table_id_t *table_id, ulint *pos,
660 ulint *base_pos, const rec_t *rec) {
661 const byte *field;
662 ulint len;
663 ulint base;
664
665 if (rec_get_deleted_flag(rec, 0)) {
666 return (dict_load_virtual_del);
667 }
668
669 if (rec_get_n_fields_old_raw(rec) != DICT_NUM_FIELDS__SYS_VIRTUAL) {
670 return ("wrong number of columns in SYS_VIRTUAL record");
671 }
672
673 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_VIRTUAL__TABLE_ID,
674 &len);
675 if (len != 8) {
676 err_len:
677 return ("incorrect column length in SYS_VIRTUAL");
678 }
679
680 if (table_id != nullptr) {
681 *table_id = mach_read_from_8(field);
682 } else if (table->id != mach_read_from_8(field)) {
683 return ("SYS_VIRTUAL.TABLE_ID mismatch");
684 }
685
686 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_VIRTUAL__POS, &len);
687 if (len != 4) {
688 goto err_len;
689 }
690
691 if (pos != nullptr) {
692 *pos = mach_read_from_4(field);
693 }
694
695 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_VIRTUAL__BASE_POS,
696 &len);
697 if (len != 4) {
698 goto err_len;
699 }
700
701 base = mach_read_from_4(field);
702
703 if (base_pos != nullptr) {
704 *base_pos = base;
705 }
706
707 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_VIRTUAL__DB_TRX_ID,
708 &len);
709 if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
710 goto err_len;
711 }
712
713 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_VIRTUAL__DB_ROLL_PTR,
714 &len);
715 if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
716 goto err_len;
717 }
718
719 if (column != nullptr) {
720 *column = table->get_col(base);
721 }
722
723 return (nullptr);
724 }
725
726 /** Loads SYS_VIRTUAL info for one virtual column
727 @param[in,out] table table
728 @param[in] nth_v_col virtual column sequence num
729 @param[in,out] v_col virtual column
730 @param[in,out] heap memory heap
731 */
732 static void dict_load_virtual_one_col(dict_table_t *table, ulint nth_v_col,
733 dict_v_col_t *v_col, mem_heap_t *heap) {
734 dict_table_t *sys_virtual;
735 dict_index_t *sys_virtual_index;
736 btr_pcur_t pcur;
737 dtuple_t *tuple;
738 dfield_t *dfield;
739 const rec_t *rec;
740 byte *buf;
741 ulint i = 0;
742 mtr_t mtr;
743 ulint skipped = 0;
744
745 ut_ad(dict_sys_mutex_own());
746
747 if (v_col->num_base == 0) {
748 return;
749 }
750
751 mtr_start(&mtr);
752
753 sys_virtual = dict_table_get_low("SYS_VIRTUAL");
754 sys_virtual_index = UT_LIST_GET_FIRST(sys_virtual->indexes);
755 ut_ad(!dict_table_is_comp(sys_virtual));
756
757 ut_ad(name_of_col_is(sys_virtual, sys_virtual_index,
758 DICT_FLD__SYS_VIRTUAL__POS, "POS"));
759
760 tuple = dtuple_create(heap, 2);
761
762 /* table ID field */
763 dfield = dtuple_get_nth_field(tuple, 0);
764
765 buf = static_cast<byte *>(mem_heap_alloc(heap, 8));
766 mach_write_to_8(buf, table->id);
767
768 dfield_set_data(dfield, buf, 8);
769
770 /* virtual column pos field */
771 dfield = dtuple_get_nth_field(tuple, 1);
772
773 buf = static_cast<byte *>(mem_heap_alloc(heap, 4));
774 ulint vcol_pos = dict_create_v_col_pos(nth_v_col, v_col->m_col.ind);
775 mach_write_to_4(buf, vcol_pos);
776
777 dfield_set_data(dfield, buf, 4);
778
779 dict_index_copy_types(tuple, sys_virtual_index, 2);
780
781 pcur.open_on_user_rec(sys_virtual_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF,
782 &mtr, UT_LOCATION_HERE);
783
784 for (i = 0; i < v_col->num_base + skipped; i++) {
785 const char *err_msg;
786 ulint pos;
787
788 ut_ad(pcur.is_on_user_rec());
789
790 rec = pcur.get_rec();
791
792 ut_a(pcur.is_on_user_rec());
793
794 err_msg = dict_load_virtual_low(table, &v_col->base_col[i - skipped],
795 nullptr, &pos, nullptr, rec);
796
797 if (err_msg) {
798 if (err_msg != dict_load_virtual_del) {
799 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_187) << err_msg;
800 } else {
801 skipped++;
802 }
803 } else {
804 ut_ad(pos == vcol_pos);
805 }
806
807 pcur.move_to_next_user_rec(&mtr);
808 }
809
810 pcur.close();
811 mtr_commit(&mtr);
812 }
813
814 /** Loads info from SYS_VIRTUAL for virtual columns.
815 @param[in,out] table table
816 @param[in] heap memory heap
817 */
818 static void dict_load_virtual(dict_table_t *table, mem_heap_t *heap) {
819 for (ulint i = 0; i < table->n_v_cols; i++) {
820 dict_v_col_t *v_col = dict_table_get_nth_v_col(table, i);
821
822 dict_load_virtual_one_col(table, i, v_col, heap);
823 }
824 }
825 /** Error message for a delete-marked record in dict_load_field_low() */
826 static const char *dict_load_field_del = "delete-marked record in SYS_FIELDS";
827
828 /** Loads an index field definition from a SYS_FIELDS record to
829 dict_index_t.
830 @return error message
831 @retval NULL on success */
832 static const char *dict_load_field_low(
833 byte *index_id, /*!< in/out: index id (8 bytes)
834 an "in" value if index != NULL
835 and "out" if index == NULL */
836 dict_index_t *index, /*!< in/out: index, could be NULL
837 if we just populate a dict_field_t
838 struct with information from
839 a SYS_FIELDS record */
840 mem_heap_t *heap, /*!< in/out: memory heap
841 for temporary storage */
842 const rec_t *rec) /*!< in: SYS_FIELDS record */
843 {
844 const byte *field;
845 ulint len;
846 ulint pos_and_prefix_len;
847 ulint prefix_len;
848 bool is_ascending;
849 bool first_field;
850
851 /* Index is supplied */
852 ut_a(index);
853
854 if (rec_get_deleted_flag(rec, 0)) {
855 return (dict_load_field_del);
856 }
857
858 if (rec_get_n_fields_old_raw(rec) != DICT_NUM_FIELDS__SYS_FIELDS) {
859 return ("wrong number of columns in SYS_FIELDS record");
860 }
861
862 field =
863 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FIELDS__INDEX_ID, &len);
864 if (len != 8) {
865 err_len:
866 return ("incorrect column length in SYS_FIELDS");
867 }
868
869 first_field = (index->n_def == 0);
870 if (memcmp(field, index_id, 8)) {
871 return ("SYS_FIELDS.INDEX_ID mismatch");
872 }
873
874 /* The next field stores the field position in the index and a
875 possible column prefix length if the index field does not
876 contain the whole column. The storage format is like this: if
877 there is at least one prefix field in the index, then the HIGH
878 2 bytes contain the field number (index->n_def) and the low 2
879 bytes the prefix length for the field. Otherwise the field
880 number (index->n_def) is contained in the 2 LOW bytes. */
881
882 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FIELDS__POS, &len);
883 if (len != 4) {
884 goto err_len;
885 }
886
887 pos_and_prefix_len = mach_read_from_4(field);
888
889 if (index &&
890 UNIV_UNLIKELY((pos_and_prefix_len & 0xFFFFUL) != index->n_def &&
891 (pos_and_prefix_len >> 16 & 0xFFFF) != index->n_def)) {
892 return ("SYS_FIELDS.POS mismatch");
893 }
894
895 if (first_field || pos_and_prefix_len > 0xFFFFUL) {
896 prefix_len = pos_and_prefix_len & 0x7FFFUL;
897 is_ascending = !(pos_and_prefix_len & 0x8000UL);
898 } else {
899 prefix_len = 0;
900 is_ascending = true;
901 }
902
903 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_FIELDS__DB_TRX_ID,
904 &len);
905 if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
906 goto err_len;
907 }
908 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_FIELDS__DB_ROLL_PTR,
909 &len);
910 if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
911 goto err_len;
912 }
913
914 field =
915 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FIELDS__COL_NAME, &len);
916 if (len == 0 || len == UNIV_SQL_NULL) {
917 goto err_len;
918 }
919
920 index->add_field(mem_heap_strdupl(heap, (const char *)field, len), prefix_len,
921 is_ascending);
922
923 return (nullptr);
924 }
925
926 /** This function parses a SYS_TABLESPACES record, extracts necessary
927 information from the record and returns to caller.
928 @return error message, or NULL on success */
929 const char *dict_process_sys_tablespaces(
930 mem_heap_t *heap, /*!< in/out: heap memory */
931 const rec_t *rec, /*!< in: current SYS_TABLESPACES rec */
932 space_id_t *space, /*!< out: space id */
933 const char **name, /*!< out: tablespace name */
934 uint32_t *flags) /*!< out: tablespace flags */
935 {
936 ulint len;
937 const byte *field;
938
939 /* Initialize the output values */
940 *space = SPACE_UNKNOWN;
941 *name = nullptr;
942 *flags = UINT32_UNDEFINED;
943
944 if (rec_get_deleted_flag(rec, 0)) {
945 return ("delete-marked record in SYS_TABLESPACES");
946 }
947
948 if (rec_get_n_fields_old_raw(rec) != DICT_NUM_FIELDS__SYS_TABLESPACES) {
949 return ("wrong number of columns in SYS_TABLESPACES record");
950 }
951
952 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLESPACES__SPACE,
953 &len);
954 if (len != DICT_FLD_LEN_SPACE) {
955 err_len:
956 return ("incorrect column length in SYS_TABLESPACES");
957 }
958 *space = mach_read_from_4(field);
959
960 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLESPACES__DB_TRX_ID,
961 &len);
962 if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
963 goto err_len;
964 }
965
966 rec_get_nth_field_offs_old(nullptr, rec,
967 DICT_FLD__SYS_TABLESPACES__DB_ROLL_PTR, &len);
968 if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
969 goto err_len;
970 }
971
972 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLESPACES__NAME,
973 &len);
974 if (len == 0 || len == UNIV_SQL_NULL) {
975 goto err_len;
976 }
977 *name = mem_heap_strdupl(heap, (char *)field, len);
978
979 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLESPACES__FLAGS,
980 &len);
981 if (len != DICT_FLD_LEN_FLAGS) {
982 goto err_len;
983 }
984 *flags = mach_read_from_4(field);
985
986 return (nullptr);
987 }
988
989 /** Get the first filepath from SYS_DATAFILES for a given space_id.
990 @param[in] space_id Tablespace ID
991 @return First filepath (caller must invoke ut::free() on it)
992 @retval NULL if no SYS_DATAFILES entry was found. */
993 char *dict_get_first_path(ulint space_id) {
994 mtr_t mtr;
995 dict_table_t *sys_datafiles;
996 dict_index_t *sys_index;
997 dtuple_t *tuple;
998 dfield_t *dfield;
999 byte *buf;
1000 btr_pcur_t pcur;
1001 const rec_t *rec;
1002 const byte *field;
1003 ulint len;
1004 char *filepath = nullptr;
1005 mem_heap_t *heap = mem_heap_create(1024, UT_LOCATION_HERE);
1006
1007 ut_ad(dict_sys_mutex_own());
1008
1009 mtr_start(&mtr);
1010
1011 sys_datafiles = dict_table_get_low("SYS_DATAFILES");
1012 sys_index = UT_LIST_GET_FIRST(sys_datafiles->indexes);
1013
1014 ut_ad(!dict_table_is_comp(sys_datafiles));
1015 ut_ad(name_of_col_is(sys_datafiles, sys_index, DICT_FLD__SYS_DATAFILES__SPACE,
1016 "SPACE"));
1017 ut_ad(name_of_col_is(sys_datafiles, sys_index, DICT_FLD__SYS_DATAFILES__PATH,
1018 "PATH"));
1019
1020 tuple = dtuple_create(heap, 1);
1021 dfield = dtuple_get_nth_field(tuple, DICT_FLD__SYS_DATAFILES__SPACE);
1022
1023 buf = static_cast<byte *>(mem_heap_alloc(heap, 4));
1024 mach_write_to_4(buf, space_id);
1025
1026 dfield_set_data(dfield, buf, 4);
1027 dict_index_copy_types(tuple, sys_index, 1);
1028
1029 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
1030 UT_LOCATION_HERE);
1031
1032 rec = pcur.get_rec();
1033
1034 /* Get the filepath from this SYS_DATAFILES record. */
1035 if (pcur.is_on_user_rec()) {
1036 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_DATAFILES__SPACE,
1037 &len);
1038 ut_a(len == 4);
1039
1040 if (space_id == mach_read_from_4(field)) {
1041 /* A record for this space ID was found. */
1042 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_DATAFILES__PATH,
1043 &len);
1044
1045 ut_ad(len > 0);
1046 ut_ad(len < OS_FILE_MAX_PATH);
1047
1048 if (len > 0 && len != UNIV_SQL_NULL) {
1049 filepath = mem_strdupl(reinterpret_cast<const char *>(field), len);
1050 ut_ad(filepath != nullptr);
1051
1052 /* The dictionary may have been written on
1053 another OS. */
1054 Fil_path::normalize(filepath);
1055 }
1056 }
1057 }
1058
1059 pcur.close();
1060 mtr_commit(&mtr);
1061 mem_heap_free(heap);
1062
1063 return (filepath);
1064 }
1065
1066 /** Gets the space name from SYS_TABLESPACES for a given space ID.
1067 @param[in] space_id Tablespace ID
1068 @param[in] callers_heap A heap to allocate from, may be NULL
1069 @return Tablespace name (caller is responsible to free it)
1070 @retval NULL if no dictionary entry was found. */
1071 static char *dict_space_get_name(space_id_t space_id,
1072 mem_heap_t *callers_heap) {
1073 mtr_t mtr;
1074 dict_table_t *sys_tablespaces;
1075 dict_index_t *sys_index;
1076 dtuple_t *tuple;
1077 dfield_t *dfield;
1078 byte *buf;
1079 btr_pcur_t pcur;
1080 const rec_t *rec;
1081 const byte *field;
1082 ulint len;
1083 char *space_name = nullptr;
1084 mem_heap_t *heap = mem_heap_create(1024, UT_LOCATION_HERE);
1085
1086 ut_ad(dict_sys_mutex_own());
1087
1088 sys_tablespaces = dict_table_get_low("SYS_TABLESPACES");
1089 if (sys_tablespaces == nullptr) {
1090 ut_a(!srv_sys_tablespaces_open);
1091 return (nullptr);
1092 }
1093
1094 sys_index = UT_LIST_GET_FIRST(sys_tablespaces->indexes);
1095
1096 ut_ad(!dict_table_is_comp(sys_tablespaces));
1097 ut_ad(name_of_col_is(sys_tablespaces, sys_index,
1098 DICT_FLD__SYS_TABLESPACES__SPACE, "SPACE"));
1099 ut_ad(name_of_col_is(sys_tablespaces, sys_index,
1100 DICT_FLD__SYS_TABLESPACES__NAME, "NAME"));
1101
1102 tuple = dtuple_create(heap, 1);
1103 dfield = dtuple_get_nth_field(tuple, DICT_FLD__SYS_TABLESPACES__SPACE);
1104
1105 buf = static_cast<byte *>(mem_heap_alloc(heap, 4));
1106 mach_write_to_4(buf, space_id);
1107
1108 dfield_set_data(dfield, buf, 4);
1109 dict_index_copy_types(tuple, sys_index, 1);
1110
1111 mtr_start(&mtr);
1112
1113 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
1114 UT_LOCATION_HERE);
1115
1116 rec = pcur.get_rec();
1117
1118 /* Get the tablespace name from this SYS_TABLESPACES record. */
1119 if (pcur.is_on_user_rec()) {
1120 field = rec_get_nth_field_old(nullptr, rec,
1121 DICT_FLD__SYS_TABLESPACES__SPACE, &len);
1122 ut_a(len == 4);
1123
1124 if (space_id == mach_read_from_4(field)) {
1125 /* A record for this space ID was found. */
1126 field = rec_get_nth_field_old(nullptr, rec,
1127 DICT_FLD__SYS_TABLESPACES__NAME, &len);
1128
1129 ut_ad(len > 0);
1130 ut_ad(len < OS_FILE_MAX_PATH);
1131
1132 if (len > 0 && len != UNIV_SQL_NULL) {
1133 /* Found a tablespace name. */
1134 if (callers_heap == nullptr) {
1135 space_name = mem_strdupl(reinterpret_cast<const char *>(field), len);
1136 } else {
1137 space_name = mem_heap_strdupl(
1138 callers_heap, reinterpret_cast<const char *>(field), len);
1139 }
1140 ut_ad(space_name);
1141 }
1142 }
1143 }
1144
1145 pcur.close();
1146 mtr_commit(&mtr);
1147 mem_heap_free(heap);
1148
1149 return (space_name);
1150 }
1151
1152 /** Check the validity of a SYS_TABLES record
1153 Make sure the fields are the right length and that they
1154 do not contain invalid contents.
1155 @param[in] rec SYS_TABLES record
1156 @return error message, or NULL on success */
1157 static const char *dict_sys_tables_rec_check(const rec_t *rec) {
1158 const byte *field;
1159 ulint len;
1160
1161 ut_ad(dict_sys_mutex_own());
1162
1163 if (rec_get_deleted_flag(rec, 0)) {
1164 return ("delete-marked record in SYS_TABLES");
1165 }
1166
1167 if (rec_get_n_fields_old_raw(rec) != DICT_NUM_FIELDS__SYS_TABLES) {
1168 return ("wrong number of columns in SYS_TABLES record");
1169 }
1170
1171 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLES__NAME, &len);
1172 if (len == 0 || len == UNIV_SQL_NULL) {
1173 err_len:
1174 return ("incorrect column length in SYS_TABLES");
1175 }
1176 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLES__DB_TRX_ID,
1177 &len);
1178 if (len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL) {
1179 goto err_len;
1180 }
1181 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLES__DB_ROLL_PTR,
1182 &len);
1183 if (len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL) {
1184 goto err_len;
1185 }
1186
1187 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLES__ID, &len);
1188 if (len != 8) {
1189 goto err_len;
1190 }
1191
1192 field =
1193 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__N_COLS, &len);
1194 if (field == nullptr || len != 4) {
1195 goto err_len;
1196 }
1197
1198 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLES__TYPE, &len);
1199 if (len != 4) {
1200 goto err_len;
1201 }
1202
1203 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLES__MIX_ID, &len);
1204 if (len != 8) {
1205 goto err_len;
1206 }
1207
1208 field =
1209 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__MIX_LEN, &len);
1210 if (field == nullptr || len != 4) {
1211 goto err_len;
1212 }
1213
1214 rec_get_nth_field_offs_old(nullptr, rec, DICT_FLD__SYS_TABLES__CLUSTER_ID,
1215 &len);
1216 if (len != UNIV_SQL_NULL) {
1217 goto err_len;
1218 }
1219
1220 field =
1221 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__SPACE, &len);
1222 if (field == nullptr || len != 4) {
1223 goto err_len;
1224 }
1225
1226 return (nullptr);
1227 }
1228
1229 /** Read and return the contents of a SYS_TABLESPACES record.
1230 @param[in] rec A record of SYS_TABLESPACES
1231 @param[out] id Pointer to the space_id for this table
1232 @param[in,out] name Buffer for Tablespace Name of length NAME_LEN
1233 @param[out] flags Pointer to tablespace flags
1234 @return true if the record was read correctly, false if not. */
1235 static bool dict_sys_tablespaces_rec_read(const rec_t *rec, space_id_t *id,
1236 char *name, uint32_t *flags) {
1237 const byte *field;
1238 ulint len;
1239
1240 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLESPACES__SPACE,
1241 &len);
1242 if (len != DICT_FLD_LEN_SPACE) {
1243 ib::error(ER_IB_MSG_188)
1244 << "Wrong field length in SYS_TABLESPACES.SPACE: " << len;
1245 return (false);
1246 }
1247 *id = mach_read_from_4(field);
1248
1249 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLESPACES__NAME,
1250 &len);
1251 if (len == 0 || len == UNIV_SQL_NULL) {
1252 ib::error(ER_IB_MSG_189)
1253 << "Wrong field length in SYS_TABLESPACES.NAME: " << len;
1254 return (false);
1255 }
1256 strncpy(name, reinterpret_cast<const char *>(field), NAME_LEN);
1257
1258 /* read the 4 byte flags from the TYPE field */
1259 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLESPACES__FLAGS,
1260 &len);
1261 if (len != 4) {
1262 ib::error(ER_IB_MSG_190)
1263 << "Wrong field length in SYS_TABLESPACES.FLAGS: " << len;
1264 return (false);
1265 }
1266 *flags = mach_read_from_4(field);
1267
1268 return (true);
1269 }
1270
1271 /** Load and check each general tablespace mentioned in the SYS_TABLESPACES.
1272 Ignore system and file-per-table tablespaces.
1273 If it is valid, add it to the file_system list.
1274 @param[in] validate true when the previous shutdown was not clean
1275 @return the highest space ID found. */
1276 static inline space_id_t dict_check_sys_tablespaces(bool validate) {
1277 space_id_t max_space_id = 0;
1278 btr_pcur_t pcur;
1279 const rec_t *rec;
1280 mtr_t mtr;
1281
1282 DBUG_TRACE;
1283
1284 ut_ad(dict_sys_mutex_own());
1285
1286 /* Before traversing it, let's make sure we have
1287 SYS_TABLESPACES and SYS_DATAFILES loaded. */
1288 dict_table_get_low("SYS_TABLESPACES");
1289 dict_table_get_low("SYS_DATAFILES");
1290
1291 mtr_start(&mtr);
1292
1293 for (rec = dict_startscan_system(&pcur, &mtr, SYS_TABLESPACES);
1294 rec != nullptr; rec = dict_getnext_system(&pcur, &mtr)) {
1295 char space_name[NAME_LEN + 1];
1296 space_id_t space_id = 0;
1297 uint32_t fsp_flags;
1298
1299 if (!dict_sys_tablespaces_rec_read(rec, &space_id, space_name,
1300 &fsp_flags)) {
1301 continue;
1302 }
1303
1304 /* Ignore system, undo and file-per-table tablespaces,
1305 and tablespaces that already are in the tablespace cache. */
1306 if (fsp_is_system_or_temp_tablespace(space_id) ||
1307 fsp_is_undo_tablespace(space_id) ||
1308 !fsp_is_shared_tablespace(fsp_flags) ||
1309 fil_space_exists_in_mem(space_id, space_name, false, true)) {
1310 continue;
1311 }
1312
1313 /* Set the expected filepath from the data dictionary.
1314 If the file is found elsewhere (from an ISL or the default
1315 location) or this path is the same file but looks different,
1316 fil_ibd_open() will update the dictionary with what is
1317 opened. */
1318 char *filepath = dict_get_first_path(space_id);
1319
1320 /* Check that this ibd is in a known location. If not, allow this
1321 but make some noise. */
1322 if (!fil_path_is_known(filepath)) {
1323 ib::warn(ER_IB_MSG_UNPROTECTED_LOCATION_ALLOWED, filepath, space_name);
1324 }
1325
1326 /* Check that the .ibd file exists. */
1327 dberr_t err = fil_ibd_open(validate, FIL_TYPE_TABLESPACE, space_id,
1328 fsp_flags, space_name, filepath, true, true);
1329
1330 if (err != DB_SUCCESS) {
1331 ib::warn(ER_IB_MSG_191) << "Ignoring tablespace " << id_name_t(space_name)
1332 << " because it could not be opened.";
1333 }
1334
1335 if (!dict_sys_t::is_reserved(space_id)) {
1336 max_space_id = std::max(max_space_id, space_id);
1337 }
1338
1339 ut::free(filepath);
1340 }
1341
1342 mtr_commit(&mtr);
1343
1344 return max_space_id;
1345 }
1346
1347 /** Read and return 5 integer fields from a SYS_TABLES record.
1348 @param[in] rec A record of SYS_TABLES
1349 @param[in] table_name Table Name, the same as SYS_TABLES.NAME
1350 @param[out] table_id Pointer to the table_id for this table
1351 @param[out] space_id Pointer to the space_id for this table
1352 @param[out] n_cols Pointer to number of columns for this table.
1353 @param[out] flags Pointer to table flags
1354 @param[out] flags2 Pointer to table flags2
1355 @return true if the record was read correctly, false if not. */
1356 static bool dict_sys_tables_rec_read(const rec_t *rec,
1357 const table_name_t &table_name,
1358 table_id_t *table_id, space_id_t *space_id,
1359 uint32_t *n_cols, uint32_t *flags,
1360 uint32_t *flags2) {
1361 const byte *field;
1362 ulint len;
1363 uint32_t type;
1364
1365 *flags2 = 0;
1366
1367 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__ID, &len);
1368 ut_ad(len == 8);
1369 *table_id = static_cast<table_id_t>(mach_read_from_8(field));
1370
1371 field =
1372 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__SPACE, &len);
1373 ut_ad(len == 4);
1374 *space_id = mach_read_from_4(field);
1375
1376 /* Read the 4 byte flags from the TYPE field */
1377 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__TYPE, &len);
1378 ut_a(len == 4);
1379 type = mach_read_from_4(field);
1380
1381 /* The low order bit of SYS_TABLES.TYPE is always set to 1. But in
1382 dict_table_t::flags the low order bit is used to determine if the
1383 row format is Redundant (0) or Compact (1) when the format is Antelope.
1384 Read the 4 byte N_COLS field and look at the high order bit. It
1385 should be set for COMPACT and later. It should not be set for
1386 REDUNDANT. */
1387 field =
1388 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__N_COLS, &len);
1389 ut_a(len == 4);
1390 *n_cols = mach_read_from_4(field);
1391
1392 /* This validation function also combines the DICT_N_COLS_COMPACT
1393 flag in n_cols into the type field to effectively make it a
1394 dict_table_t::flags. */
1395
1396 if (UINT32_UNDEFINED == dict_sys_tables_type_validate(type, *n_cols)) {
1397 ib::error(ER_IB_MSG_192) << "Table " << table_name
1398 << " in InnoDB"
1399 " data dictionary contains invalid flags."
1400 " SYS_TABLES.TYPE="
1401 << type << " SYS_TABLES.N_COLS=" << *n_cols;
1402 *flags = UINT32_UNDEFINED;
1403 return (false);
1404 }
1405
1406 *flags = dict_sys_tables_type_to_tf(type, *n_cols);
1407
1408 /* Get flags2 from SYS_TABLES.MIX_LEN */
1409 field =
1410 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__MIX_LEN, &len);
1411 *flags2 = mach_read_from_4(field);
1412
1413 /* DICT_TF2_FTS will be set when indexes are being loaded */
1414 *flags2 &= ~DICT_TF2_FTS;
1415
1416 /* Now that we have used this bit, unset it. */
1417 *n_cols &= ~DICT_N_COLS_COMPACT;
1418
1419 return (true);
1420 }
1421
1422 /** Load and check each non-predefined tablespace mentioned in SYS_TABLES.
1423 Search SYS_TABLES and check each tablespace mentioned that has not
1424 already been added to the fil_system. If it is valid, add it to the
1425 file_system list. Perform extra validation on the table if recovery from
1426 the REDO log occurred.
1427 @param[in] validate Whether to do validation on the table.
1428 @return the highest space ID found. */
1429 static inline space_id_t dict_check_sys_tables(bool validate) {
1430 space_id_t max_space_id = 0;
1431 btr_pcur_t pcur;
1432 const rec_t *rec;
1433 mtr_t mtr;
1434
1435 DBUG_TRACE;
1436
1437 ut_ad(dict_sys_mutex_own());
1438
1439 mtr_start(&mtr);
1440
1441 /* Before traversing SYS_TABLES, let's make sure we have
1442 SYS_TABLESPACES and SYS_DATAFILES loaded. */
1443 dict_table_t *sys_tablespaces;
1444 dict_table_t *sys_datafiles;
1445 sys_tablespaces = dict_table_get_low("SYS_TABLESPACES");
1446 ut_a(sys_tablespaces != nullptr);
1447 sys_datafiles = dict_table_get_low("SYS_DATAFILES");
1448 ut_a(sys_datafiles != nullptr);
1449
1450 for (rec = dict_startscan_system(&pcur, &mtr, SYS_TABLES); rec != nullptr;
1451 rec = dict_getnext_system(&pcur, &mtr)) {
1452 const byte *field;
1453 ulint len;
1454 const char *space_name;
1455 std::string tablespace_name;
1456 table_name_t table_name;
1457 table_id_t table_id;
1458 space_id_t space_id;
1459 uint32_t n_cols;
1460 uint32_t flags;
1461 uint32_t flags2;
1462 const char *tbl_name;
1463 std::string dict_table_name;
1464
1465 /* If a table record is not usable, ignore it and continue
1466 on to the next record. Error messages were logged. */
1467 if (dict_sys_tables_rec_check(rec) != nullptr) {
1468 continue;
1469 }
1470
1471 /* Copy the table name from rec */
1472 field =
1473 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__NAME, &len);
1474 table_name.m_name = mem_strdupl((char *)field, len);
1475 DBUG_PRINT("dict_check_sys_tables",
1476 ("name: %p, '%s'", table_name.m_name, table_name.m_name));
1477
1478 dict_sys_tables_rec_read(rec, table_name, &table_id, &space_id, &n_cols,
1479 &flags, &flags2);
1480 if (flags == UINT32_UNDEFINED ||
1481 fsp_is_system_or_temp_tablespace(space_id)) {
1482 ut_ad(!fsp_is_undo_tablespace(space_id));
1483 ut::free(table_name.m_name);
1484 continue;
1485 }
1486
1487 if (flags2 & DICT_TF2_DISCARDED) {
1488 ib::info(ER_IB_MSG_193)
1489 << "Tablespace " << table_name
1490 << " is set as DISCARDED. Upgrade will stop, please make sure "
1491 "there are no discarded Tables/Partitions before upgrading.";
1492 ut::free(table_name.m_name);
1493 has_discarded_tablespaces = true;
1494 continue;
1495 }
1496
1497 /* If the table is not a predefined tablespace then it must
1498 be in a file-per-table or shared tablespace.
1499 Note that flags2 is not available for REDUNDANT tables and
1500 tables which are upgraded from 5.5 & earlier,
1501 so don't check those. */
1502 bool is_shared_space = DICT_TF_HAS_SHARED_SPACE(flags);
1503
1504 ut_ad(is_shared_space || !DICT_TF_GET_COMPACT(flags) ||
1505 (flags2 == 0 || flags2 & DICT_TF2_USE_FILE_PER_TABLE));
1506
1507 /* Look up the tablespace name in the data dictionary if this
1508 is a shared tablespace. For file-per-table, the table_name
1509 and the tablespace_name are the same.
1510 Some hidden tables like FTS AUX tables may not be found in
1511 the dictionary since they can always be found in the default
1512 location. If so, then dict_space_get_name() will return NULL,
1513 the space name must be the table_name, and the filepath can be
1514 discovered in the default location.*/
1515 char *space_name_from_dict = dict_space_get_name(space_id, nullptr);
1516
1517 if (space_id == dict_sys_t::s_dict_space_id) {
1518 tbl_name = dict_sys_t::s_dd_space_name;
1519 space_name = dict_sys_t::s_dd_space_name;
1520 tablespace_name.assign(space_name);
1521
1522 } else {
1523 tbl_name = (space_name_from_dict != nullptr) ? space_name_from_dict
1524 : table_name.m_name;
1525
1526 /* Convert 5.7 name to 8.0 for partitioned table. Skip for shared
1527 tablespace. */
1528 dict_table_name.assign(tbl_name);
1529 if (!is_shared_space) {
1530 dict_name::rebuild(dict_table_name);
1531 }
1532 tbl_name = dict_table_name.c_str();
1533
1534 /* Convert tablespace name to system cs. Skip for shared tablespace. */
1535 tablespace_name.assign(tbl_name);
1536 if (!is_shared_space) {
1537 dict_name::convert_to_space(tablespace_name);
1538 }
1539 space_name = tablespace_name.c_str();
1540 }
1541
1542 /* Now that we have the proper name for this tablespace,
1543 whether it is a shared tablespace or a single table
1544 tablespace, look to see if it is already in the tablespace
1545 cache. */
1546 if (fil_space_exists_in_mem(space_id, space_name, false, true)) {
1547 ut::free(table_name.m_name);
1548 ut::free(space_name_from_dict);
1549 continue;
1550 }
1551
1552 /* Build FSP flag */
1553 uint32_t fsp_flags = dict_tf_to_fsp_flags(flags);
1554 /* Set tablespace encryption flag */
1555 if (flags2 & DICT_TF2_ENCRYPTION_FILE_PER_TABLE) {
1556 fsp_flags_set_encryption(fsp_flags);
1557 }
1558
1559 /* Set the expected filepath from the data dictionary. */
1560 char *filepath = nullptr;
1561 if (space_id == dict_sys_t::s_dict_space_id) {
1562 filepath = mem_strdup(dict_sys_t::s_dd_space_file_name);
1563 } else {
1564 filepath = dict_get_first_path(space_id);
1565 if (filepath == nullptr) {
1566 /* This record in dd::tablespaces does not have a path in
1567 dd:tablespace_files. This has been shown to occur during
1568 upgrade of some FTS tablespaces created in 5.6.
1569 Build a filepath in the default location from the table name. */
1570 filepath = Fil_path::make_ibd_from_table_name(tbl_name);
1571 } else {
1572 std::string dict_path(filepath);
1573 ut::free(filepath);
1574 /* Convert 5.7 name to 8.0 for partitioned table path. */
1575 fil_update_partition_name(space_id, fsp_flags, true, tablespace_name,
1576 dict_path);
1577 filepath = mem_strdup(dict_path.c_str());
1578 }
1579 }
1580
1581 /* Check that this ibd is in a known location. If not, allow this
1582 but make some noise. */
1583 if (!fil_path_is_known(filepath)) {
1584 ib::warn(ER_IB_MSG_UNPROTECTED_LOCATION_ALLOWED, filepath, space_name);
1585 }
1586
1587 /* Check that the .ibd file exists. */
1588 dberr_t err = fil_ibd_open(validate, FIL_TYPE_TABLESPACE, space_id,
1589 fsp_flags, space_name, filepath, true, true);
1590
1591 if (err != DB_SUCCESS) {
1592 ib::warn(ER_IB_MSG_194) << "Ignoring tablespace " << id_name_t(space_name)
1593 << " because it could not be opened.";
1594 } else {
1595 /* This tablespace is not found in
1596 SYS_TABLESPACES and we are able to
1597 successfully open it. Add it to std::set.
1598 It will be later used for register tablespaces
1599 to mysql.tablespaces */
1600 if (space_name_from_dict == nullptr) {
1601 fil_space_t *space = fil_space_get(space_id);
1602 ut_ad(space != nullptr);
1603 #ifdef UNIV_DEBUG
1604 auto var =
1605 #endif /* UNIV_DEBUG */
1606 missing_spaces.insert(space);
1607 /* duplicate space_ids are not expected */
1608 ut_ad(var.second == true);
1609 }
1610 }
1611
1612 if (!dict_sys_t::is_reserved(space_id)) {
1613 max_space_id = std::max(max_space_id, space_id);
1614 }
1615
1616 ut::free(table_name.m_name);
1617 ut::free(space_name_from_dict);
1618 ut::free(filepath);
1619 }
1620
1621 mtr_commit(&mtr);
1622
1623 return max_space_id;
1624 }
1625
1626 /** Load columns in an innodb cached table object from SYS_COLUMNS table.
1627 @param[in, out] table Table cache object
1628 @param[in, out] heap memory heap for temporary storage */
1629 static void dict_load_columns(dict_table_t *table, mem_heap_t *heap) {
1630 ut_ad(dict_sys_mutex_own());
1631
1632 mtr_t mtr;
1633 mtr_start(&mtr);
1634
1635 dict_table_t *sys_columns = dict_table_get_low("SYS_COLUMNS");
1636 dict_index_t *sys_index = UT_LIST_GET_FIRST(sys_columns->indexes);
1637 ut_ad(!dict_table_is_comp(sys_columns));
1638
1639 ut_ad(name_of_col_is(sys_columns, sys_index, DICT_FLD__SYS_COLUMNS__NAME,
1640 "NAME"));
1641 ut_ad(name_of_col_is(sys_columns, sys_index, DICT_FLD__SYS_COLUMNS__PREC,
1642 "PREC"));
1643
1644 dtuple_t *tuple = dtuple_create(heap, 1);
1645 dfield_t *dfield = dtuple_get_nth_field(tuple, 0);
1646
1647 byte *buf = static_cast<byte *>(mem_heap_alloc(heap, 8));
1648 mach_write_to_8(buf, table->id);
1649
1650 dfield_set_data(dfield, buf, 8);
1651 dict_index_copy_types(tuple, sys_index, 1);
1652
1653 btr_pcur_t pcur;
1654 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
1655 UT_LOCATION_HERE);
1656
1657 ut_ad(table->n_t_cols == static_cast<ulint>(table->n_cols) +
1658 static_cast<ulint>(table->n_v_cols));
1659
1660 size_t non_v_cols = 0;
1661 size_t n_skipped = 0;
1662 for (size_t i = 0; i + DATA_N_SYS_COLS < table->n_t_cols + n_skipped; i++) {
1663 const char *err_msg;
1664 const char *name = nullptr;
1665 ulint nth_v_col = ULINT_UNDEFINED;
1666
1667 const rec_t *rec = pcur.get_rec();
1668
1669 ut_a(pcur.is_on_user_rec());
1670
1671 err_msg = dict_load_column_low(table, heap, nullptr, nullptr, &name, rec,
1672 &nth_v_col);
1673
1674 if (err_msg == dict_load_column_del) {
1675 n_skipped++;
1676 goto next_rec;
1677 } else if (err_msg) {
1678 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_195) << err_msg;
1679 }
1680
1681 if (nth_v_col == ULINT_UNDEFINED) {
1682 /* Not a virtual column */
1683 non_v_cols++;
1684 }
1685
1686 /* Note: Currently we have one DOC_ID column that is
1687 shared by all FTS indexes on a table. And only non-virtual
1688 column can be used for FULLTEXT index */
1689 if (innobase_strcasecmp(name, FTS_DOC_ID_COL_NAME) == 0 &&
1690 nth_v_col == ULINT_UNDEFINED) {
1691 dict_col_t *col;
1692 /* As part of normal loading of tables the
1693 table->flag is not set for tables with FTS
1694 till after the FTS indexes are loaded. So we
1695 create the fts_t instance here if there isn't
1696 one already created.
1697
1698 This case does not arise for table create as
1699 the flag is set before the table is created. */
1700
1701 /* We do not add fts tables to optimize thread
1702 during upgrade because fts tables will be renamed
1703 as part of upgrade. These tables will be added
1704 to fts optimize queue when they are opened. */
1705 if (table->fts == nullptr) {
1706 table->fts = fts_create(table);
1707 if (!srv_is_upgrade_mode) {
1708 fts_optimize_add_table(table);
1709 }
1710 }
1711
1712 ut_a(table->fts->doc_col == ULINT_UNDEFINED);
1713
1714 col = table->get_col(i - n_skipped);
1715
1716 ut_ad(col->len == sizeof(doc_id_t));
1717
1718 if (col->prtype & DATA_FTS_DOC_ID) {
1719 DICT_TF2_FLAG_SET(table, DICT_TF2_FTS_HAS_DOC_ID);
1720 DICT_TF2_FLAG_UNSET(table, DICT_TF2_FTS_ADD_DOC_ID);
1721 }
1722
1723 table->fts->doc_col = i - n_skipped;
1724 }
1725 next_rec:
1726 pcur.move_to_next_user_rec(&mtr);
1727 }
1728
1729 pcur.close();
1730 mtr_commit(&mtr);
1731
1732 /* The table is getting upgraded from 5.7 where there was no row version */
1733 table->initial_col_count = table->current_col_count = table->total_col_count =
1734 non_v_cols;
1735 table->current_row_version = 0;
1736 }
1737
1738 /** Loads definitions for index fields.
1739 @return DB_SUCCESS if ok, DB_CORRUPTION if corruption */
1740 static ulint dict_load_fields(
1741 dict_index_t *index, /*!< in/out: index whose fields to load */
1742 mem_heap_t *heap) /*!< in: memory heap for temporary storage */
1743 {
1744 dict_table_t *sys_fields;
1745 dict_index_t *sys_index;
1746 btr_pcur_t pcur;
1747 dtuple_t *tuple;
1748 dfield_t *dfield;
1749 const rec_t *rec;
1750 byte *buf;
1751 ulint i;
1752 mtr_t mtr;
1753 dberr_t error;
1754
1755 ut_ad(dict_sys_mutex_own());
1756
1757 mtr_start(&mtr);
1758
1759 sys_fields = dict_table_get_low("SYS_FIELDS");
1760 sys_index = UT_LIST_GET_FIRST(sys_fields->indexes);
1761 ut_ad(!dict_table_is_comp(sys_fields));
1762 ut_ad(name_of_col_is(sys_fields, sys_index, DICT_FLD__SYS_FIELDS__COL_NAME,
1763 "COL_NAME"));
1764
1765 tuple = dtuple_create(heap, 1);
1766 dfield = dtuple_get_nth_field(tuple, 0);
1767
1768 buf = static_cast<byte *>(mem_heap_alloc(heap, 8));
1769 mach_write_to_8(buf, index->id);
1770
1771 dfield_set_data(dfield, buf, 8);
1772 dict_index_copy_types(tuple, sys_index, 1);
1773
1774 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
1775 UT_LOCATION_HERE);
1776 for (i = 0; i < index->n_fields; i++) {
1777 const char *err_msg;
1778
1779 rec = pcur.get_rec();
1780
1781 ut_a(pcur.is_on_user_rec());
1782
1783 err_msg = dict_load_field_low(buf, index, heap, rec);
1784
1785 if (err_msg == dict_load_field_del) {
1786 /* There could be delete marked records in
1787 SYS_FIELDS because SYS_FIELDS.INDEX_ID can be
1788 updated by ALTER TABLE ADD INDEX. */
1789
1790 goto next_rec;
1791 } else if (err_msg) {
1792 ib::error(ER_IB_MSG_196) << err_msg;
1793 error = DB_CORRUPTION;
1794 goto func_exit;
1795 }
1796 next_rec:
1797 pcur.move_to_next_user_rec(&mtr);
1798 }
1799
1800 error = DB_SUCCESS;
1801 func_exit:
1802 pcur.close();
1803 mtr_commit(&mtr);
1804 return (error);
1805 }
1806
1807 /** Loads definitions for table indexes. Adds them to the data dictionary
1808 cache.
1809 @return DB_SUCCESS if ok, DB_CORRUPTION if corruption of dictionary
1810 table or DB_UNSUPPORTED if table has unknown index type */
1811 static dberr_t dict_load_indexes(
1812 dict_table_t *table, /*!< in/out: table */
1813 mem_heap_t *heap, /*!< in: memory heap for temporary storage */
1814 dict_err_ignore_t ignore_err)
1815 /*!< in: error to be ignored when
1816 loading the index definition */
1817 {
1818 dict_table_t *sys_indexes;
1819 dict_index_t *sys_index;
1820 btr_pcur_t pcur;
1821 dtuple_t *tuple;
1822 dfield_t *dfield;
1823 const rec_t *rec;
1824 byte *buf;
1825 mtr_t mtr;
1826 dberr_t error = DB_SUCCESS;
1827
1828 ut_ad(dict_sys_mutex_own());
1829
1830 mtr_start(&mtr);
1831
1832 sys_indexes = dict_table_get_low("SYS_INDEXES");
1833 sys_index = UT_LIST_GET_FIRST(sys_indexes->indexes);
1834 ut_ad(!dict_table_is_comp(sys_indexes));
1835 ut_ad(name_of_col_is(sys_indexes, sys_index, DICT_FLD__SYS_INDEXES__NAME,
1836 "NAME"));
1837 ut_ad(name_of_col_is(sys_indexes, sys_index, DICT_FLD__SYS_INDEXES__PAGE_NO,
1838 "PAGE_NO"));
1839
1840 tuple = dtuple_create(heap, 1);
1841 dfield = dtuple_get_nth_field(tuple, 0);
1842
1843 buf = static_cast<byte *>(mem_heap_alloc(heap, 8));
1844 mach_write_to_8(buf, table->id);
1845
1846 dfield_set_data(dfield, buf, 8);
1847 dict_index_copy_types(tuple, sys_index, 1);
1848
1849 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
1850 UT_LOCATION_HERE);
1851 for (;;) {
1852 dict_index_t *index = nullptr;
1853 const char *err_msg;
1854
1855 if (!pcur.is_on_user_rec()) {
1856 /* We should allow the table to open even
1857 without index when DICT_ERR_IGNORE_CORRUPT is set.
1858 DICT_ERR_IGNORE_CORRUPT is currently only set
1859 for drop table */
1860 if (table->first_index() == nullptr &&
1861 !(ignore_err & DICT_ERR_IGNORE_CORRUPT)) {
1862 ib::warn(ER_IB_MSG_197) << "Cannot load table " << table->name
1863 << " because it has no indexes in"
1864 " InnoDB internal data dictionary.";
1865 error = DB_CORRUPTION;
1866 goto func_exit;
1867 }
1868
1869 break;
1870 }
1871
1872 rec = pcur.get_rec();
1873
1874 if ((ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK) &&
1875 (rec_get_n_fields_old_raw(rec) == DICT_NUM_FIELDS__SYS_INDEXES
1876 /* a record for older SYS_INDEXES table
1877 (missing merge_threshold column) is acceptable. */
1878 ||
1879 rec_get_n_fields_old_raw(rec) == DICT_NUM_FIELDS__SYS_INDEXES - 1)) {
1880 const byte *field;
1881 ulint len;
1882 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_INDEXES__NAME,
1883 &len);
1884
1885 if (len != UNIV_SQL_NULL &&
1886 static_cast<char>(*field) ==
1887 static_cast<char>(*TEMP_INDEX_PREFIX_STR)) {
1888 /* Skip indexes whose name starts with
1889 TEMP_INDEX_PREFIX, because they will
1890 be dropped during crash recovery. */
1891 goto next_rec;
1892 }
1893 }
1894
1895 err_msg =
1896 dict_load_index_low(buf, table->name.m_name, heap, rec, true, &index);
1897 ut_ad((index == nullptr && err_msg != nullptr) ||
1898 (index != nullptr && err_msg == nullptr));
1899
1900 if (err_msg == dict_load_index_id_err) {
1901 /* TABLE_ID mismatch means that we have
1902 run out of index definitions for the table. */
1903
1904 if (table->first_index() == nullptr &&
1905 !(ignore_err & DICT_ERR_IGNORE_CORRUPT)) {
1906 ib::warn(ER_IB_MSG_198)
1907 << "Failed to load the"
1908 " clustered index for table "
1909 << table->name << " because of the following error: " << err_msg
1910 << "."
1911 " Refusing to load the rest of the"
1912 " indexes (if any) and the whole table"
1913 " altogether.";
1914 error = DB_CORRUPTION;
1915 goto func_exit;
1916 }
1917
1918 break;
1919 } else if (err_msg == dict_load_index_del) {
1920 /* Skip delete-marked records. */
1921 goto next_rec;
1922 } else if (err_msg) {
1923 ib::error(ER_IB_MSG_199) << err_msg;
1924 if (ignore_err & DICT_ERR_IGNORE_CORRUPT) {
1925 goto next_rec;
1926 }
1927 error = DB_CORRUPTION;
1928 goto func_exit;
1929 }
1930
1931 ut_ad(index);
1932
1933 /* Check whether the index is corrupted */
1934 if (index->is_corrupted()) {
1935 ib::error(ER_IB_MSG_200) << "Index " << index->name << " of table "
1936 << table->name << " is corrupted";
1937
1938 if (!srv_load_corrupted && !(ignore_err & DICT_ERR_IGNORE_CORRUPT) &&
1939 index->is_clustered()) {
1940 dict_mem_index_free(index);
1941
1942 error = DB_INDEX_CORRUPT;
1943 goto func_exit;
1944 } else {
1945 /* We will load the index if
1946 1) srv_load_corrupted is true
1947 2) ignore_err is set with
1948 DICT_ERR_IGNORE_CORRUPT
1949 3) if the index corrupted is a secondary
1950 index */
1951 ib::info(ER_IB_MSG_201) << "Load corrupted index " << index->name
1952 << " of table " << table->name;
1953 }
1954 }
1955
1956 if (index->type & DICT_FTS && !dict_table_has_fts_index(table)) {
1957 /* This should have been created by now. */
1958 ut_a(table->fts != nullptr);
1959 DICT_TF2_FLAG_SET(table, DICT_TF2_FTS);
1960 }
1961
1962 /* We check for unsupported types first, so that the
1963 subsequent checks are relevant for the supported types. */
1964 if (index->type & ~(DICT_CLUSTERED | DICT_UNIQUE | DICT_CORRUPT | DICT_FTS |
1965 DICT_SPATIAL | DICT_VIRTUAL)) {
1966 ib::error(ER_IB_MSG_202) << "Unknown type " << index->type << " of index "
1967 << index->name << " of table " << table->name;
1968
1969 error = DB_UNSUPPORTED;
1970 dict_mem_index_free(index);
1971 goto func_exit;
1972 } else if (!index->is_clustered() && nullptr == table->first_index()) {
1973 ib::error(ER_IB_MSG_203)
1974 << "Trying to load index " << index->name << " for table "
1975 << table->name << ", but the first index is not clustered!";
1976
1977 dict_mem_index_free(index);
1978 error = DB_CORRUPTION;
1979 goto func_exit;
1980 } else if (dict_is_old_sys_table(table->id) &&
1981 (index->is_clustered() || ((table == dict_sys->sys_tables) &&
1982 !strcmp("ID_IND", index->name)))) {
1983 /* The index was created in memory already at booting
1984 of the database server */
1985 dict_mem_index_free(index);
1986 } else {
1987 dict_load_fields(index, heap);
1988
1989 dict_sys_mutex_exit();
1990
1991 error = dict_index_add_to_cache(table, index, index->page, false);
1992
1993 dict_sys_mutex_enter();
1994
1995 /* The data dictionary tables should never contain
1996 invalid index definitions. */
1997 if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
1998 goto func_exit;
1999 }
2000 }
2001 next_rec:
2002 pcur.move_to_next_user_rec(&mtr);
2003 }
2004
2005 ut_ad(table->fts_doc_id_index == nullptr);
2006
2007 if (table->fts != nullptr) {
2008 table->fts_doc_id_index =
2009 dict_table_get_index_on_name(table, FTS_DOC_ID_INDEX_NAME);
2010 }
2011
2012 /* If the table contains FTS indexes, populate table->fts->indexes */
2013 if (dict_table_has_fts_index(table)) {
2014 ut_ad(table->fts_doc_id_index != nullptr);
2015 /* table->fts->indexes should have been created. */
2016 ut_a(table->fts->indexes != nullptr);
2017 dict_table_get_all_fts_indexes(table, table->fts->indexes);
2018 }
2019
2020 func_exit:
2021 pcur.close();
2022 mtr_commit(&mtr);
2023
2024 return (error);
2025 }
2026
2027 /** Loads a table definition from a SYS_TABLES record to dict_table_t.
2028 Does not load any columns or indexes.
2029 @param[in] name Table name
2030 @param[in] rec SYS_TABLES record
2031 @param[out] table Table, or NULL
2032 @return error message, or NULL on success */
2033 static const char *dict_load_table_low(table_name_t &name, const rec_t *rec,
2034 dict_table_t **table) {
2035 table_id_t table_id;
2036 space_id_t space_id;
2037 uint32_t n_cols;
2038 uint32_t t_num;
2039 uint32_t flags;
2040 uint32_t flags2;
2041 uint32_t n_v_col;
2042
2043 const char *error_text = dict_sys_tables_rec_check(rec);
2044 if (error_text != nullptr) {
2045 return (error_text);
2046 }
2047
2048 dict_sys_tables_rec_read(rec, name, &table_id, &space_id, &t_num, &flags,
2049 &flags2);
2050
2051 if (flags == UINT32_UNDEFINED) {
2052 return ("incorrect flags in SYS_TABLES");
2053 }
2054
2055 dict_table_decode_n_col(t_num, &n_cols, &n_v_col);
2056
2057 std::string table_name(name.m_name);
2058 /* Check and convert 5.7 table name. */
2059 if (dict_name::is_partition(table_name)) {
2060 dict_name::rebuild(table_name);
2061 }
2062
2063 *table = dict_mem_table_create(table_name.c_str(), space_id, n_cols + n_v_col,
2064 n_v_col, 0, flags, flags2);
2065
2066 (*table)->id = table_id;
2067 (*table)->ibd_file_missing = false;
2068
2069 return (nullptr);
2070 }
2071
2072 /** Using the table->heap, copy the null-terminated filepath into
2073 table->data_dir_path. The data directory path is derived from the
2074 filepath by stripping the the table->name.m_name component suffix.
2075 If the filepath is not of the correct form (".../db/table.ibd"),
2076 then table->data_dir_path will remain nullptr.
2077 @param[in,out] table table instance
2078 @param[in] filepath filepath of tablespace */
2079 void dict_save_data_dir_path(dict_table_t *table, char *filepath) {
2080 ut_ad(dict_sys_mutex_own());
2081 ut_ad(DICT_TF_HAS_DATA_DIR(table->flags));
2082 ut_ad(table->data_dir_path == nullptr);
2083 ut_a(Fil_path::has_suffix(IBD, filepath));
2084
2085 /* Ensure this filepath is not the default filepath. */
2086 char *default_filepath = Fil_path::make("", table->name.m_name, IBD);
2087
2088 if (default_filepath == nullptr) {
2089 /* Memory allocation problem. */
2090 return;
2091 }
2092
2093 if (strcmp(filepath, default_filepath) != 0) {
2094 size_t pathlen = strlen(filepath);
2095
2096 ut_a(pathlen < OS_FILE_MAX_PATH);
2097 ut_a(Fil_path::has_suffix(IBD, filepath));
2098
2099 char *data_dir_path = mem_heap_strdup(table->heap, filepath);
2100
2101 Fil_path::make_data_dir_path(data_dir_path);
2102
2103 if (strlen(data_dir_path)) {
2104 table->data_dir_path = data_dir_path;
2105 }
2106 }
2107
2108 ut::free(default_filepath);
2109 }
2110
2111 /** Make sure the data_dir_path is saved in dict_table_t if DATA DIRECTORY
2112 was used. Try to read it from the fil_system first, then from SYS_DATAFILES.
2113 @param[in] table Table object
2114 @param[in] dict_mutex_own true if dict_sys->mutex is owned already */
2115 void dict_get_and_save_data_dir_path(dict_table_t *table, bool dict_mutex_own) {
2116 if (!(DICT_TF_HAS_DATA_DIR(table->flags) &&
2117 table->data_dir_path == nullptr)) {
2118 return;
2119 }
2120
2121 char *path = fil_space_get_first_path(table->space);
2122
2123 if (!dict_mutex_own) {
2124 dict_mutex_enter_for_mysql();
2125 }
2126
2127 if (path == nullptr) {
2128 path = dict_get_first_path(table->space);
2129 }
2130
2131 if (path != nullptr) {
2132 dict_save_data_dir_path(table, path);
2133 ut::free(path);
2134 }
2135
2136 ut_ad(table->data_dir_path != nullptr);
2137
2138 if (!dict_mutex_own) {
2139 dict_mutex_exit_for_mysql();
2140 }
2141 }
2142
2143 void dict_get_and_save_space_name(dict_table_t *table) {
2144 /* Do this only for general tablespaces. */
2145 if (!DICT_TF_HAS_SHARED_SPACE(table->flags)) {
2146 return;
2147 }
2148
2149 bool use_cache = true;
2150 if (table->tablespace != nullptr) {
2151 if (srv_sys_tablespaces_open &&
2152 dict_table_has_temp_general_tablespace_name(table->tablespace)) {
2153 /* We previous saved the temporary name,
2154 get the real one now. */
2155 use_cache = false;
2156 } else {
2157 /* Keep and use this name */
2158 return;
2159 }
2160 }
2161
2162 if (use_cache) {
2163 fil_space_t *space = fil_space_acquire_silent(table->space);
2164
2165 if (space != nullptr) {
2166 /* Use this name unless it is a temporary general
2167 tablespace name and we can now replace it. */
2168 if (!srv_sys_tablespaces_open ||
2169 !dict_table_has_temp_general_tablespace_name(space->name)) {
2170 /* Use this tablespace name */
2171 table->tablespace = mem_heap_strdup(table->heap, space->name);
2172
2173 fil_space_release(space);
2174 return;
2175 }
2176 fil_space_release(space);
2177 }
2178 }
2179 }
2180
2181 dict_table_t *dict_load_table(const char *name, bool cached,
2182 dict_err_ignore_t ignore_err,
2183 const std::string *prev_table) {
2184 dict_names_t fk_list;
2185 dict_names_t::iterator i;
2186 table_name_t table_name;
2187 dict_table_t *result;
2188
2189 DBUG_TRACE;
2190 DBUG_PRINT("dict_load_table", ("loading table: '%s'", name));
2191
2192 if (prev_table != nullptr && prev_table->compare(name) == 0) {
2193 return nullptr;
2194 }
2195
2196 const std::string cur_table(name);
2197
2198 ut_ad(dict_sys_mutex_own());
2199
2200 result = dict_table_check_if_in_cache_low(name);
2201
2202 table_name.m_name = const_cast<char *>(name);
2203
2204 if (!result) {
2205 result = dict_load_table_one(table_name, cached, ignore_err, fk_list,
2206 &cur_table);
2207 while (!fk_list.empty()) {
2208 table_name_t fk_table_name;
2209 dict_table_t *fk_table;
2210
2211 fk_table_name.m_name = const_cast<char *>(fk_list.front());
2212 fk_table = dict_table_check_if_in_cache_low(fk_table_name.m_name);
2213 if (!fk_table) {
2214 dict_load_table_one(fk_table_name, cached, ignore_err, fk_list,
2215 &cur_table);
2216 }
2217 fk_list.pop_front();
2218 }
2219 }
2220
2221 return result;
2222 }
2223
2224 void dict_load_tablespace(dict_table_t *table, dict_err_ignore_t ignore_err) {
2225 ut_ad(!table->is_temporary());
2226
2227 /* The system and temporary tablespaces are preloaded and always available. */
2228 if (fsp_is_system_or_temp_tablespace(table->space)) {
2229 return;
2230 }
2231
2232 if (dict_table_is_discarded(table)) {
2233 ib::warn(ER_IB_MSG_204)
2234 << "Tablespace for table " << table->name << " is set as discarded.";
2235 table->ibd_file_missing = true;
2236 return;
2237 }
2238
2239 /* A file-per-table table name is also the tablespace name.
2240 A general tablespace name is not the same as the table name.
2241 Use the general tablespace name if it can be read from the
2242 dictionary, if not use 'innodb_general_##. */
2243 char *shared_space_name = nullptr;
2244 std::string tablespace_name;
2245 const char *space_name;
2246 const char *tbl_name;
2247
2248 if (DICT_TF_HAS_SHARED_SPACE(table->flags)) {
2249 if (table->space == dict_sys_t::s_dict_space_id) {
2250 shared_space_name = mem_strdup(dict_sys_t::s_dd_space_name);
2251 } else if (srv_sys_tablespaces_open) {
2252 shared_space_name = dict_space_get_name(table->space, nullptr);
2253
2254 } else {
2255 /* Make the temporary tablespace name. */
2256 shared_space_name = static_cast<char *>(ut::malloc_withkey(
2257 UT_NEW_THIS_FILE_PSI_KEY, strlen(general_space_name) + 20));
2258
2259 sprintf(shared_space_name, "%s_" ULINTPF, general_space_name,
2260 static_cast<ulint>(table->space));
2261 }
2262 tbl_name = shared_space_name;
2263 space_name = shared_space_name;
2264
2265 } else {
2266 tbl_name = table->name.m_name;
2267
2268 tablespace_name.assign(tbl_name);
2269 dict_name::convert_to_space(tablespace_name);
2270 space_name = tablespace_name.c_str();
2271 }
2272
2273 /* The tablespace may already be open. */
2274 if (fil_space_exists_in_mem(table->space, space_name, false, true)) {
2275 ut::free(shared_space_name);
2276 return;
2277 }
2278
2279 if (!(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK) && !srv_is_upgrade_mode) {
2280 ib::error(ER_IB_MSG_205)
2281 << "Failed to find tablespace for table " << table->name
2282 << " in the cache. Attempting"
2283 " to load the tablespace with space id "
2284 << table->space;
2285 }
2286
2287 /* Use the remote filepath if needed. This parameter is optional
2288 in the call to fil_ibd_open(). If not supplied, it will be built
2289 from the space_name. */
2290 char *filepath = nullptr;
2291 if (DICT_TF_HAS_DATA_DIR(table->flags)) {
2292 /* This will set table->data_dir_path from either
2293 fil_system or SYS_DATAFILES */
2294 dict_get_and_save_data_dir_path(table, true);
2295
2296 if (table->data_dir_path != nullptr) {
2297 filepath = Fil_path::make(table->data_dir_path, table->name.m_name, IBD);
2298 }
2299
2300 } else if (DICT_TF_HAS_SHARED_SPACE(table->flags)) {
2301 /* Set table->tablespace from either
2302 fil_system or SYS_TABLESPACES */
2303 dict_get_and_save_space_name(table);
2304
2305 /* Set the filepath from either
2306 fil_system or SYS_DATAFILES. */
2307 filepath = dict_get_first_path(table->space);
2308 if (filepath == nullptr) {
2309 ib::warn(ER_IB_MSG_206) << "Could not find the filepath"
2310 " for table "
2311 << table->name << ", space ID " << table->space;
2312 }
2313 }
2314
2315 /* Try to open the tablespace. We set the 2nd param (fix_dict) to
2316 false because we do not have an x-lock on dict_operation_lock */
2317 uint32_t fsp_flags = dict_tf_to_fsp_flags(table->flags);
2318 /* Set tablespace encryption flag */
2319 if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_ENCRYPTION_FILE_PER_TABLE)) {
2320 fsp_flags_set_encryption(fsp_flags);
2321 }
2322
2323 /* This dict_load_tablespace() is only used on old 5.7 database during
2324 upgrade */
2325 dberr_t err = fil_ibd_open(true, FIL_TYPE_TABLESPACE, table->space, fsp_flags,
2326 space_name, filepath, true, true);
2327
2328 if (err != DB_SUCCESS) {
2329 /* We failed to find a sensible tablespace file */
2330 table->ibd_file_missing = true;
2331 }
2332
2333 ut::free(shared_space_name);
2334 ut::free(filepath);
2335 }
2336
2337 static dict_table_t *dict_load_table_one(table_name_t &name, bool cached,
2338 dict_err_ignore_t ignore_err,
2339 dict_names_t &fk_tables,
2340 const std::string *prev_table) {
2341 dberr_t err;
2342 dict_table_t *table;
2343 btr_pcur_t pcur;
2344 dict_index_t *sys_index;
2345 dtuple_t *tuple;
2346 mem_heap_t *heap;
2347 dfield_t *dfield;
2348 const rec_t *rec;
2349 const byte *field;
2350 ulint len;
2351 const char *err_msg;
2352 mtr_t mtr;
2353
2354 DBUG_TRACE;
2355 DBUG_PRINT("dict_load_table_one", ("table: %s", name.m_name));
2356
2357 ut_ad(dict_sys_mutex_own());
2358
2359 dict_table_t *sys_tables = dict_table_get_low("SYS_TABLES", prev_table);
2360 if (sys_tables == nullptr) {
2361 return nullptr;
2362 }
2363
2364 heap = mem_heap_create(32000, UT_LOCATION_HERE);
2365
2366 mtr_start(&mtr);
2367 sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
2368 ut_ad(!dict_table_is_comp(sys_tables));
2369 ut_ad(name_of_col_is(sys_tables, sys_index, DICT_FLD__SYS_TABLES__ID, "ID"));
2370 ut_ad(name_of_col_is(sys_tables, sys_index, DICT_FLD__SYS_TABLES__N_COLS,
2371 "N_COLS"));
2372 ut_ad(name_of_col_is(sys_tables, sys_index, DICT_FLD__SYS_TABLES__TYPE,
2373 "TYPE"));
2374 ut_ad(name_of_col_is(sys_tables, sys_index, DICT_FLD__SYS_TABLES__MIX_LEN,
2375 "MIX_LEN"));
2376 ut_ad(name_of_col_is(sys_tables, sys_index, DICT_FLD__SYS_TABLES__SPACE,
2377 "SPACE"));
2378
2379 tuple = dtuple_create(heap, 1);
2380 dfield = dtuple_get_nth_field(tuple, 0);
2381
2382 /* We suffix "_backup57" to 5.7 statistics tables/.ibds. This is
2383 to avoid conflict with 8.0 statistics tables. Since InnoDB dictionary
2384 refers 5.7 stats tables without the sufix, we strip the suffix and
2385 search in dictionary. */
2386 bool is_stats = false;
2387 if (strcmp(name.m_name, "mysql/innodb_index_stats_backup57") == 0 ||
2388 strcmp(name.m_name, "mysql/innodb_table_stats_backup57") == 0) {
2389 is_stats = true;
2390 }
2391
2392 std::string orig_name(name.m_name);
2393
2394 if (is_stats) {
2395 /* To load 5.7 stats tables, we search the table names
2396 with "_backup57" suffix. We now strip the suffix before
2397 searching InnoDB Dictionary */
2398 std::string substr("_backup57");
2399 std::size_t found = orig_name.find(substr);
2400 ut_ad(found != std::string::npos);
2401 orig_name.erase(found, substr.length());
2402
2403 dfield_set_data(dfield, orig_name.c_str(), orig_name.length());
2404 } else {
2405 dfield_set_data(dfield, name.m_name, ut_strlen(name.m_name));
2406 }
2407
2408 dict_index_copy_types(tuple, sys_index, 1);
2409
2410 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
2411 UT_LOCATION_HERE);
2412 rec = pcur.get_rec();
2413
2414 if (!pcur.is_on_user_rec() || rec_get_deleted_flag(rec, 0)) {
2415 /* Not found */
2416 err_exit:
2417 pcur.close();
2418 mtr_commit(&mtr);
2419 mem_heap_free(heap);
2420
2421 return nullptr;
2422 }
2423
2424 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_TABLES__NAME, &len);
2425
2426 /* Check if the table name in record is the searched one */
2427 if (!is_stats && (len != ut_strlen(name.m_name) ||
2428 0 != ut_memcmp(name.m_name, field, len))) {
2429 goto err_exit;
2430 }
2431
2432 err_msg = dict_load_table_low(name, rec, &table);
2433
2434 if (err_msg) {
2435 ib::error(ER_IB_MSG_207) << err_msg;
2436 goto err_exit;
2437 }
2438
2439 pcur.close();
2440 mtr_commit(&mtr);
2441
2442 dict_load_tablespace(table, ignore_err);
2443
2444 dict_load_columns(table, heap);
2445
2446 dict_load_virtual(table, heap);
2447
2448 dict_table_add_system_columns(table, heap);
2449
2450 mem_heap_empty(heap);
2451
2452 /* If there is no tablespace for the table then we only need to
2453 load the index definitions. So that we can IMPORT the tablespace
2454 later. When recovering table locks for resurrected incomplete
2455 transactions, the tablespace should exist, because DDL operations
2456 were not allowed while the table is being locked by a transaction. */
2457 dict_err_ignore_t index_load_err =
2458 !(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK) && table->ibd_file_missing
2459 ? DICT_ERR_IGNORE_ALL
2460 : ignore_err;
2461 err = dict_load_indexes(table, heap, index_load_err);
2462
2463 if (err == DB_SUCCESS) {
2464 if (srv_is_upgrade_mode && !srv_upgrade_old_undo_found &&
2465 !dict_load_is_system_table(table->name.m_name)) {
2466 table->id = table->id + DICT_MAX_DD_TABLES;
2467 }
2468 if (cached) {
2469 dict_table_add_to_cache(table, true);
2470 }
2471 }
2472
2473 if (dict_sys->dynamic_metadata != nullptr) {
2474 dict_table_load_dynamic_metadata(table);
2475 }
2476
2477 /* Re-check like we do in dict_load_indexes() */
2478 if (!srv_load_corrupted && !(index_load_err & DICT_ERR_IGNORE_CORRUPT) &&
2479 table->is_corrupted()) {
2480 err = DB_INDEX_CORRUPT;
2481 }
2482
2483 if (err == DB_INDEX_CORRUPT) {
2484 /* Refuse to load the table if the table has a corrupted
2485 clustered index */
2486 ut_ad(!srv_load_corrupted);
2487
2488 ib::error(ER_IB_MSG_208) << "Load table " << table->name
2489 << " failed, the table contains a"
2490 " corrupted clustered index. Turn on"
2491 " 'innodb_force_load_corrupted' to drop it";
2492 dict_table_remove_from_cache(table);
2493 table = nullptr;
2494 goto func_exit;
2495 }
2496
2497 /* We don't trust the table->flags2(retrieved from SYS_TABLES.MIX_LEN
2498 field) if the datafiles are from 3.23.52 version. To identify this
2499 version, we do the below check and reset the flags. */
2500 if (!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID) &&
2501 table->space == TRX_SYS_SPACE && table->flags == 0) {
2502 table->flags2 = 0;
2503 }
2504
2505 DBUG_EXECUTE_IF(
2506 "ib_table_invalid_flags",
2507 if (strcmp(table->name.m_name, "test/t1") == 0) {
2508 table->flags2 = 255;
2509 table->flags = 255;
2510 });
2511
2512 if (!dict_tf2_is_valid(table->flags, table->flags2)) {
2513 ib::error(ER_IB_MSG_209) << "Table " << table->name
2514 << " in InnoDB"
2515 " data dictionary contains invalid flags."
2516 " SYS_TABLES.MIX_LEN="
2517 << table->flags2;
2518 table->flags2 &= ~(DICT_TF2_TEMPORARY | DICT_TF2_INTRINSIC);
2519 dict_table_remove_from_cache(table);
2520 table = nullptr;
2521 err = DB_FAIL;
2522 goto func_exit;
2523 }
2524
2525 /* Initialize table foreign_child value. Its value could be
2526 changed when dict_load_foreigns() is called below */
2527 table->fk_max_recusive_level = 0;
2528
2529 /* If the force recovery flag is set, we open the table irrespective
2530 of the error condition, since the user may want to dump data from the
2531 clustered index. However we load the foreign key information only if
2532 all indexes were loaded. */
2533 if (!cached || table->ibd_file_missing) {
2534 /* Don't attempt to load the indexes from disk. */
2535 } else if (err == DB_SUCCESS) {
2536 err = dict_load_foreigns(table->name.m_name, nullptr, true, true,
2537 ignore_err, fk_tables);
2538
2539 if (err != DB_SUCCESS) {
2540 ib::warn(ER_IB_MSG_210) << "Load table " << table->name
2541 << " failed, the table has missing"
2542 " foreign key indexes. Turn off"
2543 " 'foreign_key_checks' and try again.";
2544
2545 dict_table_remove_from_cache(table);
2546 table = nullptr;
2547 } else {
2548 dict_mem_table_free_foreign_vcol_set(table);
2549 dict_mem_table_fill_foreign_vcol_set(table);
2550 table->fk_max_recusive_level = 0;
2551 }
2552 } else {
2553 dict_index_t *index;
2554
2555 /* Make sure that at least the clustered index was loaded.
2556 Otherwise refuse to load the table */
2557 index = table->first_index();
2558
2559 if (!srv_force_recovery || !index || !index->is_clustered()) {
2560 dict_table_remove_from_cache(table);
2561 table = nullptr;
2562 }
2563 }
2564
2565 func_exit:
2566 mem_heap_free(heap);
2567
2568 ut_ad(!table || ignore_err != DICT_ERR_IGNORE_NONE ||
2569 table->ibd_file_missing || !table->is_corrupted());
2570
2571 if (table && table->fts) {
2572 /* We do not add fts tables to optimize thread
2573 during upgrade because fts tables will be renamed
2574 as part of upgrade. These tables will be added
2575 to fts optimize queue when they are opened. */
2576
2577 if (!(dict_table_has_fts_index(table) ||
2578 DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_HAS_DOC_ID) ||
2579 DICT_TF2_FLAG_IS_SET(table, DICT_TF2_FTS_ADD_DOC_ID))) {
2580 /* the table->fts could be created in dict_load_column
2581 when a user defined FTS_DOC_ID is present, but no
2582 FTS */
2583 fts_optimize_remove_table(table);
2584 fts_free(table);
2585 } else if (!srv_is_upgrade_mode) {
2586 fts_optimize_add_table(table);
2587 }
2588 }
2589
2590 ut_ad(err != DB_SUCCESS || dict_foreign_set_validate(*table));
2591
2592 return table;
2593 }
2594
2595 /** This function is called when the database is booted. Loads system table
2596 index definitions except for the clustered index which is added to the
2597 dictionary cache at booting before calling this function. */
2598 void dict_load_sys_table(dict_table_t *table) /*!< in: system table */
2599 {
2600 mem_heap_t *heap;
2601
2602 ut_ad(dict_sys_mutex_own());
2603
2604 heap = mem_heap_create(100, UT_LOCATION_HERE);
2605
2606 dict_load_indexes(table, heap, DICT_ERR_IGNORE_NONE);
2607
2608 mem_heap_free(heap);
2609 }
2610
2611 /** Loads foreign key constraint col names (also for the referenced table).
2612 Members that must be set (and valid) in foreign:
2613 foreign->heap
2614 foreign->n_fields
2615 foreign->id ('\0'-terminated)
2616 Members that will be created and set by this function:
2617 foreign->foreign_col_names[i]
2618 foreign->referenced_col_names[i]
2619 (for i=0..foreign->n_fields-1) */
2620 static void dict_load_foreign_cols(
2621 dict_foreign_t *foreign) /*!< in/out: foreign constraint object */
2622 {
2623 dict_table_t *sys_foreign_cols;
2624 dict_index_t *sys_index;
2625 btr_pcur_t pcur;
2626 dtuple_t *tuple;
2627 dfield_t *dfield;
2628 const rec_t *rec;
2629 const byte *field;
2630 ulint len;
2631 ulint i;
2632 mtr_t mtr;
2633 size_t id_len;
2634
2635 ut_ad(dict_sys_mutex_own());
2636
2637 id_len = strlen(foreign->id);
2638
2639 foreign->foreign_col_names = static_cast<const char **>(
2640 mem_heap_alloc(foreign->heap, foreign->n_fields * sizeof(void *)));
2641
2642 foreign->referenced_col_names = static_cast<const char **>(
2643 mem_heap_alloc(foreign->heap, foreign->n_fields * sizeof(void *)));
2644
2645 mtr_start(&mtr);
2646
2647 sys_foreign_cols = dict_table_get_low("SYS_FOREIGN_COLS");
2648
2649 sys_index = UT_LIST_GET_FIRST(sys_foreign_cols->indexes);
2650 ut_ad(!dict_table_is_comp(sys_foreign_cols));
2651
2652 tuple = dtuple_create(foreign->heap, 1);
2653 dfield = dtuple_get_nth_field(tuple, 0);
2654
2655 dfield_set_data(dfield, foreign->id, id_len);
2656 dict_index_copy_types(tuple, sys_index, 1);
2657
2658 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
2659 UT_LOCATION_HERE);
2660 for (i = 0; i < foreign->n_fields; i++) {
2661 rec = pcur.get_rec();
2662
2663 ut_a(pcur.is_on_user_rec());
2664 ut_a(!rec_get_deleted_flag(rec, 0));
2665
2666 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FOREIGN_COLS__ID,
2667 &len);
2668
2669 if (len != id_len || ut_memcmp(foreign->id, field, len) != 0) {
2670 const rec_t *pos;
2671 ulint pos_len;
2672 const rec_t *for_col_name;
2673 ulint for_col_name_len;
2674 const rec_t *ref_col_name;
2675 ulint ref_col_name_len;
2676
2677 pos = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FOREIGN_COLS__POS,
2678 &pos_len);
2679
2680 for_col_name = rec_get_nth_field_old(
2681 nullptr, rec, DICT_FLD__SYS_FOREIGN_COLS__FOR_COL_NAME,
2682 &for_col_name_len);
2683
2684 ref_col_name = rec_get_nth_field_old(
2685 nullptr, rec, DICT_FLD__SYS_FOREIGN_COLS__REF_COL_NAME,
2686 &ref_col_name_len);
2687
2688 ib::fatal sout(UT_LOCATION_HERE);
2689
2690 sout << "Unable to load column names for foreign"
2691 " key '"
2692 << foreign->id
2693 << "' because it was not found in"
2694 " InnoDB internal table SYS_FOREIGN_COLS. The"
2695 " closest entry we found is:"
2696 " (ID='";
2697 sout.write(field, len);
2698 sout << "', POS=" << mach_read_from_4(pos) << ", FOR_COL_NAME='";
2699 sout.write(for_col_name, for_col_name_len);
2700 sout << "', REF_COL_NAME='";
2701 sout.write(ref_col_name, ref_col_name_len);
2702 sout << "')";
2703 }
2704
2705 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FOREIGN_COLS__POS,
2706 &len);
2707 ut_a(len == 4);
2708 ut_a(i == mach_read_from_4(field));
2709
2710 field = rec_get_nth_field_old(
2711 nullptr, rec, DICT_FLD__SYS_FOREIGN_COLS__FOR_COL_NAME, &len);
2712 foreign->foreign_col_names[i] =
2713 mem_heap_strdupl(foreign->heap, (char *)field, len);
2714
2715 field = rec_get_nth_field_old(
2716 nullptr, rec, DICT_FLD__SYS_FOREIGN_COLS__REF_COL_NAME, &len);
2717 foreign->referenced_col_names[i] =
2718 mem_heap_strdupl(foreign->heap, (char *)field, len);
2719
2720 pcur.move_to_next_user_rec(&mtr);
2721 }
2722
2723 pcur.close();
2724 mtr_commit(&mtr);
2725 }
2726
2727 /** Loads a foreign key constraint to the dictionary cache. If the referenced
2728 table is not yet loaded, it is added in the output parameter (fk_tables).
2729 @return DB_SUCCESS or error code */
2730 [[nodiscard]] static dberr_t dict_load_foreign(
2731 const char *id,
2732 /*!< in: foreign constraint id, must be
2733 '\0'-terminated */
2734 const char **col_names,
2735 /*!< in: column names, or NULL
2736 to use foreign->foreign_table->col_names */
2737 bool check_recursive,
2738 /*!< in: whether to record the foreign table
2739 parent count to avoid unlimited recursive
2740 load of chained foreign tables */
2741 bool check_charsets,
2742 /*!< in: whether to check charset
2743 compatibility */
2744 dict_err_ignore_t ignore_err,
2745 /*!< in: error to be ignored */
2746 dict_names_t &fk_tables)
2747 /*!< out: the foreign key constraint is added
2748 to the dictionary cache only if the referenced
2749 table is already in cache. Otherwise, the
2750 foreign key constraint is not added to cache,
2751 and the referenced table is added to this
2752 stack. */
2753 {
2754 dict_foreign_t *foreign;
2755 dict_table_t *sys_foreign;
2756 btr_pcur_t pcur;
2757 dict_index_t *sys_index;
2758 dtuple_t *tuple;
2759 mem_heap_t *heap2;
2760 dfield_t *dfield;
2761 const rec_t *rec;
2762 const byte *field;
2763 ulint len;
2764 ulint n_fields_and_type;
2765 mtr_t mtr;
2766 dict_table_t *for_table;
2767 dict_table_t *ref_table;
2768 size_t id_len;
2769
2770 DBUG_TRACE;
2771 DBUG_PRINT("dict_load_foreign",
2772 ("id: '%s', check_recursive: %d", id, check_recursive));
2773
2774 ut_ad(dict_sys_mutex_own());
2775
2776 id_len = strlen(id);
2777
2778 heap2 = mem_heap_create(100, UT_LOCATION_HERE);
2779
2780 mtr_start(&mtr);
2781
2782 sys_foreign = dict_table_get_low("SYS_FOREIGN");
2783
2784 sys_index = UT_LIST_GET_FIRST(sys_foreign->indexes);
2785 ut_ad(!dict_table_is_comp(sys_foreign));
2786
2787 tuple = dtuple_create(heap2, 1);
2788 dfield = dtuple_get_nth_field(tuple, 0);
2789
2790 dfield_set_data(dfield, id, id_len);
2791 dict_index_copy_types(tuple, sys_index, 1);
2792
2793 pcur.open_on_user_rec(sys_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
2794 UT_LOCATION_HERE);
2795 rec = pcur.get_rec();
2796
2797 if (!pcur.is_on_user_rec() || rec_get_deleted_flag(rec, 0)) {
2798 /* Not found */
2799
2800 ib::error(ER_IB_MSG_211) << "Cannot load foreign constraint " << id
2801 << ": could not find the relevant record in "
2802 << "SYS_FOREIGN";
2803
2804 pcur.close();
2805 mtr_commit(&mtr);
2806 mem_heap_free(heap2);
2807
2808 return DB_ERROR;
2809 }
2810
2811 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FOREIGN__ID, &len);
2812
2813 /* Check if the id in record is the searched one */
2814 if (len != id_len || ut_memcmp(id, field, len) != 0) {
2815 {
2816 ib::error err(ER_IB_MSG_1227);
2817 err << "Cannot load foreign constraint " << id << ": found ";
2818 err.write(field, len);
2819 err << " instead in SYS_FOREIGN";
2820 }
2821
2822 pcur.close();
2823 mtr_commit(&mtr);
2824 mem_heap_free(heap2);
2825
2826 return DB_ERROR;
2827 }
2828
2829 /* Read the table names and the number of columns associated
2830 with the constraint */
2831
2832 mem_heap_free(heap2);
2833
2834 foreign = dict_mem_foreign_create();
2835
2836 n_fields_and_type = mach_read_from_4(
2837 rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FOREIGN__N_COLS, &len));
2838
2839 ut_a(len == 4);
2840
2841 /* We store the type in the bits 24..29 of n_fields_and_type. */
2842
2843 foreign->type = (unsigned int)(n_fields_and_type >> 24);
2844 foreign->n_fields = (unsigned int)(n_fields_and_type & 0x3FFUL);
2845
2846 foreign->id = mem_heap_strdupl(foreign->heap, id, id_len);
2847
2848 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FOREIGN__FOR_NAME,
2849 &len);
2850
2851 foreign->foreign_table_name =
2852 mem_heap_strdupl(foreign->heap, (char *)field, len);
2853 dict_mem_foreign_table_name_lookup_set(foreign, true);
2854
2855 const ulint foreign_table_name_len = len;
2856
2857 field = rec_get_nth_field_old(nullptr, rec, DICT_FLD__SYS_FOREIGN__REF_NAME,
2858 &len);
2859 foreign->referenced_table_name =
2860 mem_heap_strdupl(foreign->heap, (char *)field, len);
2861 dict_mem_referenced_table_name_lookup_set(foreign, true);
2862
2863 pcur.close();
2864 mtr_commit(&mtr);
2865
2866 dict_load_foreign_cols(foreign);
2867
2868 ref_table =
2869 dict_table_check_if_in_cache_low(foreign->referenced_table_name_lookup);
2870 for_table =
2871 dict_table_check_if_in_cache_low(foreign->foreign_table_name_lookup);
2872
2873 if (!for_table) {
2874 /* To avoid recursively loading the tables related through
2875 the foreign key constraints, the child table name is saved
2876 here. The child table will be loaded later, along with its
2877 foreign key constraint. */
2878
2879 lint old_size = mem_heap_get_size(ref_table->heap);
2880
2881 ut_a(ref_table != nullptr);
2882 fk_tables.push_back(mem_heap_strdupl(ref_table->heap,
2883 foreign->foreign_table_name_lookup,
2884 foreign_table_name_len));
2885
2886 lint new_size = mem_heap_get_size(ref_table->heap);
2887 dict_sys->size += new_size - old_size;
2888
2889 dict_foreign_remove_from_cache(foreign);
2890 return DB_SUCCESS;
2891 }
2892
2893 ut_a(for_table || ref_table);
2894
2895 /* Note that there may already be a foreign constraint object in
2896 the dictionary cache for this constraint: then the following
2897 call only sets the pointers in it to point to the appropriate table
2898 and index objects and frees the newly created object foreign.
2899 Adding to the cache should always succeed since we are not creating
2900 a new foreign key constraint but loading one from the data
2901 dictionary. */
2902
2903 return dict_foreign_add_to_cache(foreign, col_names, check_charsets, true,
2904 ignore_err);
2905 }
2906
2907 /** Loads foreign key constraints where the table is either the foreign key
2908 holder or where the table is referenced by a foreign key. Adds these
2909 constraints to the data dictionary.
2910
2911 The foreign key constraint is loaded only if the referenced table is also
2912 in the dictionary cache. If the referenced table is not in dictionary
2913 cache, then it is added to the output parameter (fk_tables).
2914
2915 @return DB_SUCCESS or error code */
2916 dberr_t dict_load_foreigns(
2917 const char *table_name, /*!< in: table name */
2918 const char **col_names, /*!< in: column names, or NULL
2919 to use table->col_names */
2920 bool check_recursive, /*!< in: Whether to check
2921 recursive load of tables
2922 chained by FK */
2923 bool check_charsets, /*!< in: whether to check
2924 charset compatibility */
2925 dict_err_ignore_t ignore_err, /*!< in: error to be ignored */
2926 dict_names_t &fk_tables)
2927 /*!< out: stack of table
2928 names which must be loaded
2929 subsequently to load all the
2930 foreign key constraints. */
2931 {
2932 ulint tuple_buf[ut::div_ceil(DTUPLE_EST_ALLOC(1), sizeof(ulint))];
2933 btr_pcur_t pcur;
2934 dtuple_t *tuple;
2935 dfield_t *dfield;
2936 dict_index_t *sec_index;
2937 dict_table_t *sys_foreign;
2938 const rec_t *rec;
2939 const byte *field;
2940 ulint len;
2941 dberr_t err;
2942 mtr_t mtr;
2943
2944 DBUG_TRACE;
2945
2946 ut_ad(dict_sys_mutex_own());
2947
2948 sys_foreign = dict_table_get_low("SYS_FOREIGN");
2949
2950 if (sys_foreign == nullptr) {
2951 /* No foreign keys defined yet in this database */
2952
2953 ib::info(ER_IB_MSG_212) << "No foreign key system tables in the database";
2954 return DB_ERROR;
2955 }
2956
2957 ut_ad(!dict_table_is_comp(sys_foreign));
2958 mtr_start(&mtr);
2959
2960 /* Get the secondary index based on FOR_NAME from table
2961 SYS_FOREIGN */
2962
2963 sec_index = sys_foreign->first_index()->next();
2964 ut_ad(!sec_index->is_clustered());
2965 start_load:
2966
2967 tuple = dtuple_create_from_mem(tuple_buf, sizeof(tuple_buf), 1, 0);
2968 dfield = dtuple_get_nth_field(tuple, 0);
2969
2970 dfield_set_data(dfield, table_name, ut_strlen(table_name));
2971 dict_index_copy_types(tuple, sec_index, 1);
2972
2973 pcur.open_on_user_rec(sec_index, tuple, PAGE_CUR_GE, BTR_SEARCH_LEAF, &mtr,
2974 UT_LOCATION_HERE);
2975 loop:
2976 rec = pcur.get_rec();
2977
2978 if (!pcur.is_on_user_rec()) {
2979 /* End of index */
2980
2981 goto load_next_index;
2982 }
2983
2984 /* Now we have the record in the secondary index containing a table
2985 name and a foreign constraint ID */
2986
2987 field = rec_get_nth_field_old(nullptr, rec,
2988 DICT_FLD__SYS_FOREIGN_FOR_NAME__NAME, &len);
2989
2990 /* Check if the table name in the record is the one searched for; the
2991 following call does the comparison in the latin1_swedish_ci
2992 charset-collation, in a case-insensitive way. */
2993
2994 if (0 != cmp_data_data(dfield_get_type(dfield)->mtype,
2995 dfield_get_type(dfield)->prtype, true,
2996 static_cast<const byte *>(dfield_get_data(dfield)),
2997 dfield_get_len(dfield), field, len)) {
2998 goto load_next_index;
2999 }
3000
3001 /* Since table names in SYS_FOREIGN are stored in a case-insensitive
3002 order, we have to check that the table name matches also in a binary
3003 string comparison. On Unix, MySQL allows table names that only differ
3004 in character case. If lower_case_table_names=2 then what is stored
3005 may not be the same case, but the previous comparison showed that they
3006 match with no-case. */
3007
3008 if (rec_get_deleted_flag(rec, 0)) {
3009 goto next_rec;
3010 }
3011
3012 if ((innobase_get_lower_case_table_names() != 2) &&
3013 (0 != ut_memcmp(field, table_name, len))) {
3014 goto next_rec;
3015 }
3016
3017 /* Now we get a foreign key constraint id */
3018 field = rec_get_nth_field_old(nullptr, rec,
3019 DICT_FLD__SYS_FOREIGN_FOR_NAME__ID, &len);
3020
3021 /* Copy the string because the page may be modified or evicted
3022 after mtr_commit() below. */
3023 char fk_id[MAX_TABLE_NAME_LEN + 1];
3024
3025 ut_a(len <= MAX_TABLE_NAME_LEN);
3026 memcpy(fk_id, field, len);
3027 fk_id[len] = '\0';
3028
3029 pcur.store_position(&mtr);
3030
3031 mtr_commit(&mtr);
3032
3033 /* Load the foreign constraint definition to the dictionary cache */
3034
3035 err = dict_load_foreign(fk_id, col_names, check_recursive, check_charsets,
3036 ignore_err, fk_tables);
3037
3038 if (err != DB_SUCCESS) {
3039 pcur.close();
3040
3041 return err;
3042 }
3043
3044 mtr_start(&mtr);
3045
3046 pcur.restore_position(BTR_SEARCH_LEAF, &mtr, UT_LOCATION_HERE);
3047 next_rec:
3048 pcur.move_to_next_user_rec(&mtr);
3049 goto loop;
3050
3051 load_next_index:
3052 pcur.close();
3053 mtr_commit(&mtr);
3054
3055 sec_index = sec_index->next();
3056
3057 if (sec_index != nullptr) {
3058 mtr_start(&mtr);
3059
3060 /* Switch to scan index on REF_NAME, fk_max_recusive_level
3061 already been updated when scanning FOR_NAME index, no need to
3062 update again */
3063 check_recursive = false;
3064
3065 goto start_load;
3066 }
3067
3068 return DB_SUCCESS;
3069 }
3070
3071 /** Load all tablespaces during upgrade */
3072 void dict_load_tablespaces_for_upgrade() {
3073 ut_ad(srv_is_upgrade_mode);
3074
3075 dict_sys_mutex_enter();
3076
3077 mtr_t mtr;
3078 mtr_start(&mtr);
3079 space_id_t max_id = mtr_read_ulint(dict_hdr_get(&mtr) + DICT_HDR_MAX_SPACE_ID,
3080 MLOG_4BYTES, &mtr);
3081 mtr_commit(&mtr);
3082 fil_set_max_space_id_if_bigger(max_id);
3083
3084 dict_check_sys_tablespaces(false);
3085 dict_check_sys_tables(false);
3086
3087 dict_sys_mutex_exit();
3088 }