29 #include "dict0crea.ic"
55 dict_create_sys_tables_tuple(
78 dfield = dtuple_get_nth_field(
79 entry, DICT_COL__SYS_TABLES__NAME);
86 dfield = dtuple_get_nth_field(
87 entry, DICT_COL__SYS_TABLES__ID);
95 dfield = dtuple_get_nth_field(
96 entry, DICT_COL__SYS_TABLES__N_COLS);
104 dfield = dtuple_get_nth_field(
105 entry, DICT_COL__SYS_TABLES__TYPE);
112 type = dict_tf_to_sys_tables_type(table->
flags);
118 dfield = dtuple_get_nth_field(
119 entry, DICT_COL__SYS_TABLES__MIX_ID);
126 dfield = dtuple_get_nth_field(
127 entry, DICT_COL__SYS_TABLES__MIX_LEN);
137 dfield = dtuple_get_nth_field(
138 entry, DICT_COL__SYS_TABLES__CLUSTER_ID);
142 dfield = dtuple_get_nth_field(
143 entry, DICT_COL__SYS_TABLES__SPACE);
160 dict_create_sys_columns_tuple(
173 const char* col_name;
178 column = dict_table_get_nth_col(table, i);
187 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_COLUMNS__TABLE_ID);
195 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_COLUMNS__POS);
205 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_COLUMNS__NAME);
211 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_COLUMNS__MTYPE);
219 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_COLUMNS__PRTYPE);
227 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_COLUMNS__LEN);
235 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_COLUMNS__PREC);
249 static __attribute__((nonnull, warn_unused_result))
251 dict_build_table_def_step(
273 if (use_tablespace) {
279 "ib_create_table_fail_out_of_space_ids",
280 space = ULINT_UNDEFINED;
283 if (UNIV_UNLIKELY(space == ULINT_UNDEFINED)) {
303 space, table->
name, path,
308 table->
space = (
unsigned int) space;
310 if (error != DB_SUCCESS) {
327 row = dict_create_sys_tables_tuple(table, node->
heap);
338 dict_build_col_def_step(
344 row = dict_create_sys_columns_tuple(node->
table, node->
col_no,
355 dict_create_sys_indexes_tuple(
381 dfield = dtuple_get_nth_field(
382 entry, DICT_COL__SYS_INDEXES__TABLE_ID);
390 dfield = dtuple_get_nth_field(
391 entry, DICT_COL__SYS_INDEXES__ID);
401 dfield = dtuple_get_nth_field(
402 entry, DICT_COL__SYS_INDEXES__NAME);
407 dfield = dtuple_get_nth_field(
408 entry, DICT_COL__SYS_INDEXES__N_FIELDS);
416 dfield = dtuple_get_nth_field(
417 entry, DICT_COL__SYS_INDEXES__TYPE);
426 dfield = dtuple_get_nth_field(
427 entry, DICT_COL__SYS_INDEXES__SPACE);
436 dfield = dtuple_get_nth_field(
437 entry, DICT_COL__SYS_INDEXES__PAGE_NO);
455 dict_create_sys_fields_tuple(
468 ibool index_contains_column_prefix_field = FALSE;
474 for (j = 0; j < index->
n_fields; j++) {
475 if (dict_index_get_nth_field(index, j)->prefix_len > 0) {
476 index_contains_column_prefix_field = TRUE;
481 field = dict_index_get_nth_field(index, fld_no);
490 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_FIELDS__INDEX_ID);
499 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_FIELDS__POS);
503 if (index_contains_column_prefix_field) {
522 dfield = dtuple_get_nth_field(entry, DICT_COL__SYS_FIELDS__COL_NAME);
537 dict_create_search_tuple(
548 ut_ad(tuple && heap);
552 field1 = dtuple_get_nth_field(tuple, 0);
553 field2 = dtuple_get_nth_field(search_tuple, 0);
557 field1 = dtuple_get_nth_field(tuple, 1);
558 field2 = dtuple_get_nth_field(search_tuple, 1);
562 ut_ad(dtuple_validate(search_tuple));
564 return(search_tuple);
570 static __attribute__((nonnull, warn_unused_result))
572 dict_build_index_def_step(
591 return(DB_TABLE_NOT_FOUND);
611 row = dict_create_sys_indexes_tuple(index, node->
heap);
628 dict_build_field_def_step(
637 row = dict_create_sys_fields_tuple(index, node->field_no, node->
heap);
645 static __attribute__((nonnull, warn_unused_result))
647 dict_create_index_tree_step(
663 if (index->
type == DICT_FTS) {
674 search_tuple = dict_create_search_tuple(node->ind_row, node->heap);
686 if (node->index->table->ibd_file_missing
693 index->
id, index, &mtr);
696 err = DB_OUT_OF_FILE_SPACE;
699 DBUG_EXECUTE_IF(
"ib_import_create_index_failure_1",
701 err = DB_OUT_OF_FILE_SPACE; );
705 btr_pcur_get_rec(&pcur), DICT_FLD__SYS_INDEXES__PAGE_NO,
706 node->page_no, &mtr);
733 ptr = rec_get_nth_field_old(
734 rec, DICT_FLD__SYS_INDEXES__PAGE_NO, &len);
746 ptr = rec_get_nth_field_old(
747 rec, DICT_FLD__SYS_INDEXES__SPACE, &len);
754 if (UNIV_UNLIKELY(zip_size == ULINT_UNDEFINED)) {
809 rec = btr_pcur_get_rec(pcur);
810 ptr = rec_get_nth_field_old(
811 rec, DICT_FLD__SYS_INDEXES__PAGE_NO, &len);
817 if (drop && root_page_no ==
FIL_NULL) {
821 fprintf(stderr,
" InnoDB: Trying to TRUNCATE"
822 " a missing index of table %s!\n", table->
name);
826 ptr = rec_get_nth_field_old(
827 rec, DICT_FLD__SYS_INDEXES__SPACE, &len);
837 if (UNIV_UNLIKELY(zip_size == ULINT_UNDEFINED)) {
842 fprintf(stderr,
" InnoDB: Trying to TRUNCATE"
843 " a missing .ibd file of table %s!\n", table->
name);
847 ptr = rec_get_nth_field_old(
848 rec, DICT_FLD__SYS_INDEXES__TYPE, &len);
852 ptr = rec_get_nth_field_old(rec, DICT_FLD__SYS_INDEXES__ID, &len);
871 btr_block_get(space, zip_size, root_page_no, RW_X_LATCH, NULL, mtr);
895 if (index->
id == index_id) {
896 if (index->
type & DICT_FTS) {
899 root_page_no =
btr_create(type, space, zip_size,
900 index_id, index, mtr);
901 index->
page = (
unsigned int) root_page_no;
902 return(root_page_no);
909 " InnoDB: Index %llu of table %s is missing\n"
910 "InnoDB: from the data dictionary during TRUNCATE!\n",
939 node->
state = TABLE_BUILD_TABLE_DEF;
954 node->commit_node = 0;
982 node->
state = INDEX_BUILD_INDEX_DEF;
998 node->commit_node = 0;
1027 node->
state = TABLE_BUILD_TABLE_DEF;
1030 if (node->
state == TABLE_BUILD_TABLE_DEF) {
1034 err = dict_build_table_def_step(thr, node);
1036 if (err != DB_SUCCESS) {
1041 node->
state = TABLE_BUILD_COL_DEF;
1049 if (node->
state == TABLE_BUILD_COL_DEF) {
1053 dict_build_col_def_step(node);
1061 node->
state = TABLE_COMMIT_WORK;
1065 if (node->
state == TABLE_COMMIT_WORK) {
1071 node->
state = TABLE_ADD_TO_CACHE;
1078 if (node->
state == TABLE_ADD_TO_CACHE) {
1088 if (err == DB_SUCCESS) {
1091 }
else if (err == DB_LOCK_WAIT) {
1129 node->
state = INDEX_BUILD_INDEX_DEF;
1132 if (node->
state == INDEX_BUILD_INDEX_DEF) {
1134 err = dict_build_index_def_step(thr, node);
1136 if (err != DB_SUCCESS) {
1141 node->
state = INDEX_BUILD_FIELD_DEF;
1149 if (node->
state == INDEX_BUILD_FIELD_DEF) {
1151 if (node->field_no < (node->
index)->n_fields) {
1153 dict_build_field_def_step(node);
1161 node->
state = INDEX_ADD_TO_CACHE;
1165 if (node->
state == INDEX_ADD_TO_CACHE) {
1167 index_id_t index_id = node->
index->
id;
1176 ut_a(!node->
index == (err != DB_SUCCESS));
1178 if (err != DB_SUCCESS) {
1183 node->
state = INDEX_CREATE_INDEX_TREE;
1186 if (node->
state == INDEX_CREATE_INDEX_TREE) {
1188 err = dict_create_index_tree_step(node);
1190 DBUG_EXECUTE_IF(
"ib_dict_create_index_tree_fail",
1191 err = DB_OUT_OF_MEMORY;);
1193 if (err != DB_SUCCESS) {
1197 && node->
table->fts) {
1208 if (index_cache->
words) {
1210 index_cache->
words = 0;
1215 *reinterpret_cast<void**>(index_cache));
1233 node->
state = INDEX_COMMIT_WORK;
1236 if (node->
state == INDEX_COMMIT_WORK) {
1242 node->
state = INDEX_CREATE_INDEX_TREE;
1252 if (err == DB_SUCCESS) {
1255 }
else if (err == DB_LOCK_WAIT) {
1279 dict_check_if_system_table_exists(
1281 const char* tablename,
1294 if (sys_table == NULL) {
1295 error = DB_TABLE_NOT_FOUND;
1298 || sys_table->
n_cols != num_fields) {
1324 my_bool srv_file_per_table_backup;
1334 sys_foreign_err = dict_check_if_system_table_exists(
1335 "SYS_FOREIGN", DICT_NUM_FIELDS__SYS_FOREIGN + 1, 3);
1336 sys_foreign_cols_err = dict_check_if_system_table_exists(
1337 "SYS_FOREIGN_COLS", DICT_NUM_FIELDS__SYS_FOREIGN_COLS + 1, 1);
1339 if (sys_foreign_err == DB_SUCCESS
1340 && sys_foreign_cols_err == DB_SUCCESS) {
1348 trx->
op_info =
"creating foreign key sys tables";
1350 row_mysql_lock_data_dictionary(trx);
1356 "Dropping incompletely created "
1357 "SYS_FOREIGN table.");
1363 "Dropping incompletely created "
1364 "SYS_FOREIGN_COLS table.");
1370 "Creating foreign key constraint system tables.");
1391 "PROCEDURE CREATE_FOREIGN_SYS_TABLES_PROC () IS\n"
1394 "SYS_FOREIGN(ID CHAR, FOR_NAME CHAR,"
1395 " REF_NAME CHAR, N_COLS INT);\n"
1396 "CREATE UNIQUE CLUSTERED INDEX ID_IND"
1397 " ON SYS_FOREIGN (ID);\n"
1398 "CREATE INDEX FOR_IND"
1399 " ON SYS_FOREIGN (FOR_NAME);\n"
1400 "CREATE INDEX REF_IND"
1401 " ON SYS_FOREIGN (REF_NAME);\n"
1403 "SYS_FOREIGN_COLS(ID CHAR, POS INT,"
1404 " FOR_COL_NAME CHAR, REF_COL_NAME CHAR);\n"
1405 "CREATE UNIQUE CLUSTERED INDEX ID_IND"
1406 " ON SYS_FOREIGN_COLS (ID, POS);\n"
1410 if (err != DB_SUCCESS) {
1412 "Creation of SYS_FOREIGN and SYS_FOREIGN_COLS "
1413 "has failed with error %lu. Tablespace is full. "
1414 "Dropping incompletely created tables.",
1417 ut_ad(err == DB_OUT_OF_FILE_SPACE
1423 if (err == DB_OUT_OF_FILE_SPACE) {
1436 if (err == DB_SUCCESS) {
1438 "Foreign key constraint system tables created");
1443 sys_foreign_err = dict_check_if_system_table_exists(
1444 "SYS_FOREIGN", DICT_NUM_FIELDS__SYS_FOREIGN + 1, 3);
1445 ut_a(sys_foreign_err == DB_SUCCESS);
1447 sys_foreign_cols_err = dict_check_if_system_table_exists(
1448 "SYS_FOREIGN_COLS", DICT_NUM_FIELDS__SYS_FOREIGN_COLS + 1, 1);
1449 ut_a(sys_foreign_cols_err == DB_SUCCESS);
1457 static __attribute__((nonnull, warn_unused_result))
1459 dict_foreign_eval_sql(
1468 FILE* ef = dict_foreign_err_file;
1472 if (error == DB_DUPLICATE_KEY) {
1473 mutex_enter(&dict_foreign_err_mutex);
1476 fputs(
" Error in foreign key constraint creation for table ",
1479 fputs(
".\nA foreign key constraint of name ", ef);
1481 fputs(
"\nalready exists."
1482 " (Note that internally InnoDB adds 'databasename'\n"
1483 "in front of the user-defined constraint name.)\n"
1484 "Note that InnoDB's FOREIGN KEY system tables store\n"
1485 "constraint names as case-insensitive, with the\n"
1486 "MySQL standard latin1_swedish_ci collation. If you\n"
1487 "create tables or databases whose names differ only in\n"
1488 "the character case, then collisions in constraint\n"
1489 "names can occur. Workaround: name your constraints\n"
1490 "explicitly with unique names.\n",
1493 mutex_exit(&dict_foreign_err_mutex);
1498 if (error != DB_SUCCESS) {
1500 "InnoDB: Foreign key constraint creation failed:\n"
1501 "InnoDB: internal error number %lu\n", (ulong) error);
1503 mutex_enter(&dict_foreign_err_mutex);
1505 fputs(
" Internal error in foreign key constraint creation"
1509 "See the MySQL .err log in the datadir"
1510 " for more information.\n", ef);
1511 mutex_exit(&dict_foreign_err_mutex);
1523 static __attribute__((nonnull, warn_unused_result))
1525 dict_create_add_foreign_field_to_dictionary(
1539 foreign->foreign_col_names[field_nr]);
1542 foreign->referenced_col_names[field_nr]);
1544 return(dict_foreign_eval_sql(
1546 "PROCEDURE P () IS\n"
1548 "INSERT INTO SYS_FOREIGN_COLS VALUES"
1549 "(:id, :pos, :for_col_name, :ref_col_name);\n"
1551 table_name, foreign->id, trx));
1578 error = dict_foreign_eval_sql(info,
1579 "PROCEDURE P () IS\n"
1581 "INSERT INTO SYS_FOREIGN VALUES"
1582 "(:id, :for_name, :ref_name, :n_cols);\n"
1584 , name, foreign->
id, trx);
1586 if (error != DB_SUCCESS) {
1591 for (ulint i = 0; i < foreign->
n_fields; i++) {
1592 error = dict_create_add_foreign_field_to_dictionary(
1593 i, name, foreign, trx);
1595 if (error != DB_SUCCESS) {
1623 ulint number = start_id + 1;
1630 "InnoDB: table SYS_FOREIGN not found"
1631 " in internal data dictionary\n");
1643 if (error != DB_SUCCESS) {
1651 if (error != DB_SUCCESS) {
1657 trx->
op_info =
"committing foreign key definitions";
1677 my_bool srv_file_per_table_backup;
1686 sys_tablespaces_err = dict_check_if_system_table_exists(
1687 "SYS_TABLESPACES", DICT_NUM_FIELDS__SYS_TABLESPACES + 1, 1);
1688 sys_datafiles_err = dict_check_if_system_table_exists(
1689 "SYS_DATAFILES", DICT_NUM_FIELDS__SYS_DATAFILES + 1, 1);
1691 if (sys_tablespaces_err == DB_SUCCESS
1692 && sys_datafiles_err == DB_SUCCESS) {
1700 trx->
op_info =
"creating tablepace and datafile sys tables";
1702 row_mysql_lock_data_dictionary(trx);
1708 "Dropping incompletely created "
1709 "SYS_TABLESPACES table.");
1715 "Dropping incompletely created "
1716 "SYS_DATAFILES table.");
1722 "Creating tablespace and datafile system tables.");
1731 "PROCEDURE CREATE_SYS_TABLESPACE_PROC () IS\n"
1733 "CREATE TABLE SYS_TABLESPACES(\n"
1734 " SPACE INT, NAME CHAR, FLAGS INT);\n"
1735 "CREATE UNIQUE CLUSTERED INDEX SYS_TABLESPACES_SPACE"
1736 " ON SYS_TABLESPACES (SPACE);\n"
1737 "CREATE TABLE SYS_DATAFILES(\n"
1738 " SPACE INT, PATH CHAR);\n"
1739 "CREATE UNIQUE CLUSTERED INDEX SYS_DATAFILES_SPACE"
1740 " ON SYS_DATAFILES (SPACE);\n"
1744 if (err != DB_SUCCESS) {
1746 "Creation of SYS_TABLESPACES and SYS_DATAFILES "
1747 "has failed with error %lu. Tablespace is full. "
1748 "Dropping incompletely created tables.",
1751 ut_a(err == DB_OUT_OF_FILE_SPACE
1757 if (err == DB_OUT_OF_FILE_SPACE) {
1770 if (err == DB_SUCCESS) {
1772 "Tablespace and datafile system tables created.");
1778 sys_tablespaces_err = dict_check_if_system_table_exists(
1779 "SYS_TABLESPACES", DICT_NUM_FIELDS__SYS_TABLESPACES + 1, 1);
1780 ut_a(sys_tablespaces_err == DB_SUCCESS);
1782 sys_datafiles_err = dict_check_if_system_table_exists(
1783 "SYS_DATAFILES", DICT_NUM_FIELDS__SYS_DATAFILES + 1, 1);
1784 ut_a(sys_datafiles_err == DB_SUCCESS);
1809 ut_a(space > TRX_SYS_SPACE);
1820 "PROCEDURE P () IS\n"
1822 "INSERT INTO SYS_TABLESPACES VALUES"
1823 "(:space, :name, :flags);\n"
1824 "INSERT INTO SYS_DATAFILES VALUES"
1825 "(:space, :path);\n"
1829 if (error != DB_SUCCESS) {
1834 trx->
op_info =
"committing tablespace and datafile definition";