You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2559 lines
69 KiB

17 years ago
17 years ago
17 years ago
16 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
16 years ago
16 years ago
16 years ago
16 years ago
15 years ago
15 years ago
15 years ago
15 years ago
16 years ago
16 years ago
15 years ago
15 years ago
  1. /*****************************************************************************
  2. Copyright (c) 1996, 2013, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc.,
  11. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  12. *****************************************************************************/
  13. /**************************************************//**
  14. @file dict/dict0load.c
  15. Loads to the memory cache database object definitions
  16. from dictionary tables
  17. Created 4/24/1996 Heikki Tuuri
  18. *******************************************************/
  19. #include "dict0load.h"
  20. #include "mysql_version.h"
  21. #ifdef UNIV_NONINL
  22. #include "dict0load.ic"
  23. #endif
  24. #include "btr0pcur.h"
  25. #include "btr0btr.h"
  26. #include "btr0sea.h"
  27. #include "page0page.h"
  28. #include "mach0data.h"
  29. #include "dict0dict.h"
  30. #include "dict0boot.h"
  31. #include "rem0cmp.h"
  32. #include "srv0start.h"
  33. #include "srv0srv.h"
  34. #include "ha_prototypes.h" /* innobase_casedn_str() */
  35. #include "trx0sys.h"
  36. /** Following are six InnoDB system tables */
  37. static const char* SYSTEM_TABLE_NAME[] = {
  38. "SYS_TABLES",
  39. "SYS_INDEXES",
  40. "SYS_COLUMNS",
  41. "SYS_FIELDS",
  42. "SYS_FOREIGN",
  43. "SYS_FOREIGN_COLS",
  44. "SYS_STATS"
  45. };
  46. /* If this flag is TRUE, then we will load the cluster index's (and tables')
  47. metadata even if it is marked as "corrupted". */
  48. UNIV_INTERN my_bool srv_load_corrupted = FALSE;
  49. /****************************************************************//**
  50. Compare the name of an index column.
  51. @return TRUE if the i'th column of index is 'name'. */
  52. static
  53. ibool
  54. name_of_col_is(
  55. /*===========*/
  56. const dict_table_t* table, /*!< in: table */
  57. const dict_index_t* index, /*!< in: index */
  58. ulint i, /*!< in: index field offset */
  59. const char* name) /*!< in: name to compare to */
  60. {
  61. ulint tmp = dict_col_get_no(dict_field_get_col(
  62. dict_index_get_nth_field(
  63. index, i)));
  64. return(strcmp(name, dict_table_get_col_name(table, tmp)) == 0);
  65. }
  66. /********************************************************************//**
  67. Finds the first table name in the given database.
  68. @return own: table name, NULL if does not exist; the caller must free
  69. the memory in the string! */
  70. UNIV_INTERN
  71. char*
  72. dict_get_first_table_name_in_db(
  73. /*============================*/
  74. const char* name) /*!< in: database name which ends in '/' */
  75. {
  76. dict_table_t* sys_tables;
  77. btr_pcur_t pcur;
  78. dict_index_t* sys_index;
  79. dtuple_t* tuple;
  80. mem_heap_t* heap;
  81. dfield_t* dfield;
  82. const rec_t* rec;
  83. const byte* field;
  84. ulint len;
  85. mtr_t mtr;
  86. ut_ad(mutex_own(&(dict_sys->mutex)));
  87. heap = mem_heap_create(1000);
  88. mtr_start(&mtr);
  89. sys_tables = dict_table_get_low("SYS_TABLES", DICT_ERR_IGNORE_NONE);
  90. sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
  91. ut_a(!dict_table_is_comp(sys_tables));
  92. tuple = dtuple_create(heap, 1);
  93. dfield = dtuple_get_nth_field(tuple, 0);
  94. dfield_set_data(dfield, name, ut_strlen(name));
  95. dict_index_copy_types(tuple, sys_index, 1);
  96. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  97. BTR_SEARCH_LEAF, &pcur, &mtr);
  98. loop:
  99. rec = btr_pcur_get_rec(&pcur);
  100. if (!btr_pcur_is_on_user_rec(&pcur)) {
  101. /* Not found */
  102. btr_pcur_close(&pcur);
  103. mtr_commit(&mtr);
  104. mem_heap_free(heap);
  105. return(NULL);
  106. }
  107. field = rec_get_nth_field_old(rec, 0, &len);
  108. if (len < strlen(name)
  109. || ut_memcmp(name, field, strlen(name)) != 0) {
  110. /* Not found */
  111. btr_pcur_close(&pcur);
  112. mtr_commit(&mtr);
  113. mem_heap_free(heap);
  114. return(NULL);
  115. }
  116. if (!rec_get_deleted_flag(rec, 0)) {
  117. /* We found one */
  118. char* table_name = mem_strdupl((char*) field, len);
  119. btr_pcur_close(&pcur);
  120. mtr_commit(&mtr);
  121. mem_heap_free(heap);
  122. return(table_name);
  123. }
  124. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  125. goto loop;
  126. }
  127. /********************************************************************//**
  128. Prints to the standard output information on all tables found in the data
  129. dictionary system table. */
  130. UNIV_INTERN
  131. void
  132. dict_print(void)
  133. /*============*/
  134. {
  135. dict_table_t* table;
  136. btr_pcur_t pcur;
  137. const rec_t* rec;
  138. mem_heap_t* heap;
  139. mtr_t mtr;
  140. /* Enlarge the fatal semaphore wait timeout during the InnoDB table
  141. monitor printout */
  142. mutex_enter(&kernel_mutex);
  143. srv_fatal_semaphore_wait_threshold += SRV_SEMAPHORE_WAIT_EXTENSION;
  144. mutex_exit(&kernel_mutex);
  145. heap = mem_heap_create(1000);
  146. mutex_enter(&(dict_sys->mutex));
  147. mtr_start(&mtr);
  148. rec = dict_startscan_system(&pcur, &mtr, SYS_TABLES);
  149. while (rec) {
  150. const char* err_msg;
  151. err_msg = dict_process_sys_tables_rec(
  152. heap, rec, &table, DICT_TABLE_LOAD_FROM_CACHE
  153. | DICT_TABLE_UPDATE_STATS);
  154. mtr_commit(&mtr);
  155. if (!err_msg) {
  156. dict_table_print_low(table);
  157. } else {
  158. ut_print_timestamp(stderr);
  159. fprintf(stderr, " InnoDB: %s\n", err_msg);
  160. }
  161. mem_heap_empty(heap);
  162. mtr_start(&mtr);
  163. rec = dict_getnext_system(&pcur, &mtr);
  164. }
  165. mtr_commit(&mtr);
  166. mutex_exit(&(dict_sys->mutex));
  167. mem_heap_free(heap);
  168. /* Restore the fatal semaphore wait timeout */
  169. mutex_enter(&kernel_mutex);
  170. srv_fatal_semaphore_wait_threshold -= SRV_SEMAPHORE_WAIT_EXTENSION;
  171. mutex_exit(&kernel_mutex);
  172. }
  173. /********************************************************************//**
  174. This function gets the next system table record as it scans the table.
  175. @return the next record if found, NULL if end of scan */
  176. static
  177. const rec_t*
  178. dict_getnext_system_low(
  179. /*====================*/
  180. btr_pcur_t* pcur, /*!< in/out: persistent cursor to the
  181. record*/
  182. mtr_t* mtr) /*!< in: the mini-transaction */
  183. {
  184. rec_t* rec = NULL;
  185. while (!rec || rec_get_deleted_flag(rec, 0)) {
  186. btr_pcur_move_to_next_user_rec(pcur, mtr);
  187. rec = btr_pcur_get_rec(pcur);
  188. if (!btr_pcur_is_on_user_rec(pcur)) {
  189. /* end of index */
  190. btr_pcur_close(pcur);
  191. return(NULL);
  192. }
  193. }
  194. /* Get a record, let's save the position */
  195. btr_pcur_store_position(pcur, mtr);
  196. return(rec);
  197. }
  198. /********************************************************************//**
  199. This function opens a system table, and return the first record.
  200. @return first record of the system table */
  201. UNIV_INTERN
  202. const rec_t*
  203. dict_startscan_system(
  204. /*==================*/
  205. btr_pcur_t* pcur, /*!< out: persistent cursor to
  206. the record */
  207. mtr_t* mtr, /*!< in: the mini-transaction */
  208. dict_system_id_t system_id) /*!< in: which system table to open */
  209. {
  210. dict_table_t* system_table;
  211. dict_index_t* clust_index;
  212. const rec_t* rec;
  213. ut_a(system_id < SYS_NUM_SYSTEM_TABLES);
  214. system_table = dict_table_get_low(SYSTEM_TABLE_NAME[system_id],
  215. DICT_ERR_IGNORE_NONE);
  216. clust_index = UT_LIST_GET_FIRST(system_table->indexes);
  217. btr_pcur_open_at_index_side(TRUE, clust_index, BTR_SEARCH_LEAF, pcur,
  218. TRUE, mtr);
  219. rec = dict_getnext_system_low(pcur, mtr);
  220. return(rec);
  221. }
  222. /********************************************************************//**
  223. This function gets the next system table record as it scans the table.
  224. @return the next record if found, NULL if end of scan */
  225. UNIV_INTERN
  226. const rec_t*
  227. dict_getnext_system(
  228. /*================*/
  229. btr_pcur_t* pcur, /*!< in/out: persistent cursor
  230. to the record */
  231. mtr_t* mtr) /*!< in: the mini-transaction */
  232. {
  233. const rec_t* rec;
  234. /* Restore the position */
  235. btr_pcur_restore_position(BTR_SEARCH_LEAF, pcur, mtr);
  236. /* Get the next record */
  237. rec = dict_getnext_system_low(pcur, mtr);
  238. return(rec);
  239. }
  240. /********************************************************************//**
  241. This function processes one SYS_TABLES record and populate the dict_table_t
  242. struct for the table. Extracted out of dict_print() to be used by
  243. both monitor table output and information schema innodb_sys_tables output.
  244. @return error message, or NULL on success */
  245. UNIV_INTERN
  246. const char*
  247. dict_process_sys_tables_rec(
  248. /*========================*/
  249. mem_heap_t* heap, /*!< in/out: temporary memory heap */
  250. const rec_t* rec, /*!< in: SYS_TABLES record */
  251. dict_table_t** table, /*!< out: dict_table_t to fill */
  252. dict_table_info_t status) /*!< in: status bit controls
  253. options such as whether we shall
  254. look for dict_table_t from cache
  255. first */
  256. {
  257. ulint len;
  258. const char* field;
  259. const char* err_msg = NULL;
  260. char* table_name;
  261. field = (const char*) rec_get_nth_field_old(rec, 0, &len);
  262. ut_a(!rec_get_deleted_flag(rec, 0));
  263. /* Get the table name */
  264. table_name = mem_heap_strdupl(heap, field, len);
  265. /* If DICT_TABLE_LOAD_FROM_CACHE is set, first check
  266. whether there is cached dict_table_t struct first */
  267. if (status & DICT_TABLE_LOAD_FROM_CACHE) {
  268. *table = dict_table_get_low(table_name, DICT_ERR_IGNORE_NONE);
  269. if (!(*table)) {
  270. err_msg = "Table not found in cache";
  271. }
  272. } else {
  273. err_msg = dict_load_table_low(table_name, rec, table);
  274. }
  275. if (err_msg) {
  276. return(err_msg);
  277. }
  278. if ((status & DICT_TABLE_UPDATE_STATS)
  279. && srv_stats_auto_update
  280. && dict_table_get_first_index(*table)) {
  281. /* Update statistics if DICT_TABLE_UPDATE_STATS
  282. is set */
  283. dict_update_statistics(
  284. *table,
  285. FALSE, /* update even if initialized */
  286. FALSE,
  287. FALSE /* update even if not changed too much */);
  288. }
  289. return(NULL);
  290. }
  291. /********************************************************************//**
  292. This function parses a SYS_INDEXES record and populate a dict_index_t
  293. structure with the information from the record. For detail information
  294. about SYS_INDEXES fields, please refer to dict_boot() function.
  295. @return error message, or NULL on success */
  296. UNIV_INTERN
  297. const char*
  298. dict_process_sys_indexes_rec(
  299. /*=========================*/
  300. mem_heap_t* heap, /*!< in/out: heap memory */
  301. const rec_t* rec, /*!< in: current SYS_INDEXES rec */
  302. dict_index_t* index, /*!< out: index to be filled */
  303. table_id_t* table_id) /*!< out: index table id */
  304. {
  305. const char* err_msg;
  306. byte* buf;
  307. buf = mem_heap_alloc(heap, 8);
  308. /* Parse the record, and get "dict_index_t" struct filled */
  309. err_msg = dict_load_index_low(buf, NULL,
  310. heap, rec, FALSE, &index);
  311. *table_id = mach_read_from_8(buf);
  312. return(err_msg);
  313. }
  314. /********************************************************************//**
  315. This function parses a SYS_COLUMNS record and populate a dict_column_t
  316. structure with the information from the record.
  317. @return error message, or NULL on success */
  318. UNIV_INTERN
  319. const char*
  320. dict_process_sys_columns_rec(
  321. /*=========================*/
  322. mem_heap_t* heap, /*!< in/out: heap memory */
  323. const rec_t* rec, /*!< in: current SYS_COLUMNS rec */
  324. dict_col_t* column, /*!< out: dict_col_t to be filled */
  325. table_id_t* table_id, /*!< out: table id */
  326. const char** col_name) /*!< out: column name */
  327. {
  328. const char* err_msg;
  329. /* Parse the record, and get "dict_col_t" struct filled */
  330. err_msg = dict_load_column_low(NULL, heap, column,
  331. table_id, col_name, rec);
  332. return(err_msg);
  333. }
  334. /********************************************************************//**
  335. This function parses a SYS_FIELDS record and populates a dict_field_t
  336. structure with the information from the record.
  337. @return error message, or NULL on success */
  338. UNIV_INTERN
  339. const char*
  340. dict_process_sys_fields_rec(
  341. /*========================*/
  342. mem_heap_t* heap, /*!< in/out: heap memory */
  343. const rec_t* rec, /*!< in: current SYS_FIELDS rec */
  344. dict_field_t* sys_field, /*!< out: dict_field_t to be
  345. filled */
  346. ulint* pos, /*!< out: Field position */
  347. index_id_t* index_id, /*!< out: current index id */
  348. index_id_t last_id) /*!< in: previous index id */
  349. {
  350. byte* buf;
  351. byte* last_index_id;
  352. const char* err_msg;
  353. buf = mem_heap_alloc(heap, 8);
  354. last_index_id = mem_heap_alloc(heap, 8);
  355. mach_write_to_8(last_index_id, last_id);
  356. err_msg = dict_load_field_low(buf, NULL, sys_field,
  357. pos, last_index_id, heap, rec, NULL, 0);
  358. *index_id = mach_read_from_8(buf);
  359. return(err_msg);
  360. }
  361. //#ifdef FOREIGN_NOT_USED
  362. /********************************************************************//**
  363. This function parses a SYS_FOREIGN record and populate a dict_foreign_t
  364. structure with the information from the record. For detail information
  365. about SYS_FOREIGN fields, please refer to dict_load_foreign() function.
  366. @return error message, or NULL on success */
  367. UNIV_INTERN
  368. const char*
  369. dict_process_sys_foreign_rec(
  370. /*=========================*/
  371. mem_heap_t* heap, /*!< in/out: heap memory */
  372. const rec_t* rec, /*!< in: current SYS_FOREIGN rec */
  373. dict_foreign_t* foreign) /*!< out: dict_foreign_t struct
  374. to be filled */
  375. {
  376. ulint len;
  377. const byte* field;
  378. ulint n_fields_and_type;
  379. if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
  380. return("delete-marked record in SYS_FOREIGN");
  381. }
  382. if (UNIV_UNLIKELY(rec_get_n_fields_old(rec) != 6)) {
  383. return("wrong number of columns in SYS_FOREIGN record");
  384. }
  385. field = rec_get_nth_field_old(rec, 0/*ID*/, &len);
  386. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  387. err_len:
  388. return("incorrect column length in SYS_FOREIGN");
  389. }
  390. /* This recieves a dict_foreign_t* that points to a stack variable.
  391. So mem_heap_free(foreign->heap) is not used as elsewhere.
  392. Since the heap used here is freed elsewhere, foreign->heap
  393. is not assigned. */
  394. foreign->id = mem_heap_strdupl(heap, (const char*) field, len);
  395. rec_get_nth_field_offs_old(rec, 1/*DB_TRX_ID*/, &len);
  396. if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
  397. goto err_len;
  398. }
  399. rec_get_nth_field_offs_old(rec, 2/*DB_ROLL_PTR*/, &len);
  400. if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
  401. goto err_len;
  402. }
  403. /* The _lookup versions of the referenced and foreign table names
  404. are not assigned since they are not used in this dict_foreign_t */
  405. field = rec_get_nth_field_old(rec, 3/*FOR_NAME*/, &len);
  406. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  407. goto err_len;
  408. }
  409. foreign->foreign_table_name = mem_heap_strdupl(
  410. heap, (const char*) field, len);
  411. field = rec_get_nth_field_old(rec, 4/*REF_NAME*/, &len);
  412. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  413. goto err_len;
  414. }
  415. foreign->referenced_table_name = mem_heap_strdupl(
  416. heap, (const char*) field, len);
  417. field = rec_get_nth_field_old(rec, 5/*N_COLS*/, &len);
  418. if (UNIV_UNLIKELY(len != 4)) {
  419. goto err_len;
  420. }
  421. n_fields_and_type = mach_read_from_4(field);
  422. foreign->type = (unsigned int) (n_fields_and_type >> 24);
  423. foreign->n_fields = (unsigned int) (n_fields_and_type & 0x3FFUL);
  424. return(NULL);
  425. }
  426. //#endif /* FOREIGN_NOT_USED */
  427. //#ifdef FOREIGN_NOT_USED
  428. /********************************************************************//**
  429. This function parses a SYS_FOREIGN_COLS record and extract necessary
  430. information from the record and return to caller.
  431. @return error message, or NULL on success */
  432. UNIV_INTERN
  433. const char*
  434. dict_process_sys_foreign_col_rec(
  435. /*=============================*/
  436. mem_heap_t* heap, /*!< in/out: heap memory */
  437. const rec_t* rec, /*!< in: current SYS_FOREIGN_COLS rec */
  438. const char** name, /*!< out: foreign key constraint name */
  439. const char** for_col_name, /*!< out: referencing column name */
  440. const char** ref_col_name, /*!< out: referenced column name
  441. in referenced table */
  442. ulint* pos) /*!< out: column position */
  443. {
  444. ulint len;
  445. const byte* field;
  446. if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
  447. return("delete-marked record in SYS_FOREIGN_COLS");
  448. }
  449. if (UNIV_UNLIKELY(rec_get_n_fields_old(rec) != 6)) {
  450. return("wrong number of columns in SYS_FOREIGN_COLS record");
  451. }
  452. field = rec_get_nth_field_old(rec, 0/*ID*/, &len);
  453. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  454. err_len:
  455. return("incorrect column length in SYS_FOREIGN_COLS");
  456. }
  457. *name = mem_heap_strdupl(heap, (char*) field, len);
  458. field = rec_get_nth_field_old(rec, 1/*POS*/, &len);
  459. if (UNIV_UNLIKELY(len != 4)) {
  460. goto err_len;
  461. }
  462. *pos = mach_read_from_4(field);
  463. rec_get_nth_field_offs_old(rec, 2/*DB_TRX_ID*/, &len);
  464. if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
  465. goto err_len;
  466. }
  467. rec_get_nth_field_offs_old(rec, 3/*DB_ROLL_PTR*/, &len);
  468. if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
  469. goto err_len;
  470. }
  471. field = rec_get_nth_field_old(rec, 4/*FOR_COL_NAME*/, &len);
  472. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  473. goto err_len;
  474. }
  475. *for_col_name = mem_heap_strdupl(heap, (char*) field, len);
  476. field = rec_get_nth_field_old(rec, 5/*REF_COL_NAME*/, &len);
  477. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  478. goto err_len;
  479. }
  480. *ref_col_name = mem_heap_strdupl(heap, (char*) field, len);
  481. return(NULL);
  482. }
  483. //#endif /* FOREIGN_NOT_USED */
  484. /********************************************************************//**
  485. This function parses a SYS_STATS record and extract necessary
  486. information from the record and return to caller.
  487. @return error message, or NULL on success */
  488. UNIV_INTERN
  489. const char*
  490. dict_process_sys_stats_rec(
  491. /*=============================*/
  492. mem_heap_t* heap __attribute__((unused)), /*!< in/out: heap memory */
  493. const rec_t* rec, /*!< in: current SYS_STATS rec */
  494. index_id_t* index_id, /*!< out: INDEX_ID */
  495. ulint* key_cols, /*!< out: KEY_COLS */
  496. ib_uint64_t* diff_vals, /*!< out: DIFF_VALS */
  497. ib_uint64_t* non_null_vals) /*!< out: NON_NULL_VALS */
  498. {
  499. ulint len;
  500. const byte* field;
  501. ulint n_fields;
  502. if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
  503. return("delete-marked record in SYS_STATS");
  504. }
  505. n_fields = rec_get_n_fields_old(rec);
  506. if (UNIV_UNLIKELY(n_fields < 5)) {
  507. return("wrong number of columns in SYS_STATS record");
  508. }
  509. field = rec_get_nth_field_old(rec, 0/*INDEX_ID*/, &len);
  510. if (UNIV_UNLIKELY(len != 8)) {
  511. err_len:
  512. return("incorrect column length in SYS_STATS");
  513. }
  514. *index_id = mach_read_from_8(field);
  515. field = rec_get_nth_field_old(rec, 1/*KEY_COLS*/, &len);
  516. if (UNIV_UNLIKELY(len != 4)) {
  517. goto err_len;
  518. }
  519. *key_cols = mach_read_from_4(field);
  520. rec_get_nth_field_offs_old(rec, 2/*DB_TRX_ID*/, &len);
  521. if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
  522. goto err_len;
  523. }
  524. rec_get_nth_field_offs_old(rec, 3/*DB_ROLL_PTR*/, &len);
  525. if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
  526. goto err_len;
  527. }
  528. field = rec_get_nth_field_old(rec, 4/*DIFF_VALS*/, &len);
  529. if (UNIV_UNLIKELY(len != 8)) {
  530. goto err_len;
  531. }
  532. *diff_vals = mach_read_from_8(field);
  533. if (n_fields < 6) {
  534. *non_null_vals = ((ib_uint64_t)(-1));
  535. } else {
  536. field = rec_get_nth_field_old(rec, 5/*NON_NULL_VALS*/, &len);
  537. if (UNIV_UNLIKELY(len != 8)) {
  538. goto err_len;
  539. }
  540. *non_null_vals = mach_read_from_8(field);
  541. }
  542. return(NULL);
  543. }
  544. /********************************************************************//**
  545. Determine the flags of a table described in SYS_TABLES.
  546. @return compressed page size in kilobytes; or 0 if the tablespace is
  547. uncompressed, ULINT_UNDEFINED on error */
  548. static
  549. ulint
  550. dict_sys_tables_get_flags(
  551. /*======================*/
  552. const rec_t* rec) /*!< in: a record of SYS_TABLES */
  553. {
  554. const byte* field;
  555. ulint len;
  556. ulint n_cols;
  557. ulint flags;
  558. field = rec_get_nth_field_old(rec, 5, &len);
  559. ut_a(len == 4);
  560. flags = mach_read_from_4(field);
  561. if (UNIV_LIKELY(flags == DICT_TABLE_ORDINARY)) {
  562. return(0);
  563. }
  564. field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
  565. n_cols = mach_read_from_4(field);
  566. if (UNIV_UNLIKELY(!(n_cols & 0x80000000UL))) {
  567. /* New file formats require ROW_FORMAT=COMPACT. */
  568. return(ULINT_UNDEFINED);
  569. }
  570. switch (flags & (DICT_TF_FORMAT_MASK | DICT_TF_COMPACT)) {
  571. default:
  572. case DICT_TF_FORMAT_51 << DICT_TF_FORMAT_SHIFT:
  573. case DICT_TF_FORMAT_51 << DICT_TF_FORMAT_SHIFT | DICT_TF_COMPACT:
  574. /* flags should be DICT_TABLE_ORDINARY,
  575. or DICT_TF_FORMAT_MASK should be nonzero. */
  576. return(ULINT_UNDEFINED);
  577. case DICT_TF_FORMAT_ZIP << DICT_TF_FORMAT_SHIFT | DICT_TF_COMPACT:
  578. #if DICT_TF_FORMAT_MAX > DICT_TF_FORMAT_ZIP
  579. # error "missing case labels for DICT_TF_FORMAT_ZIP .. DICT_TF_FORMAT_MAX"
  580. #endif
  581. /* We support this format. */
  582. break;
  583. }
  584. if (UNIV_UNLIKELY((flags & DICT_TF_ZSSIZE_MASK)
  585. > (DICT_TF_ZSSIZE_MAX << DICT_TF_ZSSIZE_SHIFT))) {
  586. /* Unsupported compressed page size. */
  587. return(ULINT_UNDEFINED);
  588. }
  589. if (UNIV_UNLIKELY(flags & (~0 << DICT_TF_BITS))) {
  590. /* Some unused bits are set. */
  591. return(ULINT_UNDEFINED);
  592. }
  593. return(flags);
  594. }
  595. /********************************************************************//**
  596. In a crash recovery we already have all the tablespace objects created.
  597. This function compares the space id information in the InnoDB data dictionary
  598. to what we already read with fil_load_single_table_tablespaces().
  599. In a normal startup, we create the tablespace objects for every table in
  600. InnoDB's data dictionary, if the corresponding .ibd file exists.
  601. We also scan the biggest space id, and store it to fil_system. */
  602. UNIV_INTERN
  603. void
  604. dict_check_tablespaces_and_store_max_id(
  605. /*====================================*/
  606. ibool in_crash_recovery) /*!< in: are we doing a crash recovery */
  607. {
  608. dict_table_t* sys_tables;
  609. dict_index_t* sys_index;
  610. btr_pcur_t pcur;
  611. const rec_t* rec;
  612. ulint max_space_id;
  613. mtr_t mtr;
  614. mutex_enter(&(dict_sys->mutex));
  615. mtr_start(&mtr);
  616. sys_tables = dict_table_get_low("SYS_TABLES", DICT_ERR_IGNORE_NONE);
  617. sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
  618. ut_a(!dict_table_is_comp(sys_tables));
  619. max_space_id = mtr_read_ulint(dict_hdr_get(&mtr)
  620. + DICT_HDR_MAX_SPACE_ID,
  621. MLOG_4BYTES, &mtr);
  622. fil_set_max_space_id_if_bigger(max_space_id);
  623. btr_pcur_open_at_index_side(TRUE, sys_index, BTR_SEARCH_LEAF, &pcur,
  624. TRUE, &mtr);
  625. loop:
  626. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  627. rec = btr_pcur_get_rec(&pcur);
  628. if (!btr_pcur_is_on_user_rec(&pcur)) {
  629. /* end of index */
  630. btr_pcur_close(&pcur);
  631. mtr_commit(&mtr);
  632. /* We must make the tablespace cache aware of the biggest
  633. known space id */
  634. /* printf("Biggest space id in data dictionary %lu\n",
  635. max_space_id); */
  636. fil_set_max_space_id_if_bigger(max_space_id);
  637. mutex_exit(&(dict_sys->mutex));
  638. return;
  639. }
  640. if (!rec_get_deleted_flag(rec, 0)) {
  641. /* We found one */
  642. const byte* field;
  643. ulint len;
  644. ulint space_id;
  645. ulint flags;
  646. char* name;
  647. field = rec_get_nth_field_old(rec, 0, &len);
  648. name = mem_strdupl((char*) field, len);
  649. flags = dict_sys_tables_get_flags(rec);
  650. if (UNIV_UNLIKELY(flags == ULINT_UNDEFINED)) {
  651. field = rec_get_nth_field_old(rec, 5, &len);
  652. flags = mach_read_from_4(field);
  653. ut_print_timestamp(stderr);
  654. fputs(" InnoDB: Error: table ", stderr);
  655. ut_print_filename(stderr, name);
  656. fprintf(stderr, "\n"
  657. "InnoDB: in InnoDB data dictionary"
  658. " has unknown type %lx.\n",
  659. (ulong) flags);
  660. goto loop;
  661. }
  662. field = rec_get_nth_field_old(rec, 9, &len);
  663. ut_a(len == 4);
  664. space_id = mach_read_from_4(field);
  665. btr_pcur_store_position(&pcur, &mtr);
  666. mtr_commit(&mtr);
  667. if (trx_sys_sys_space(space_id)) {
  668. /* The system tablespace always exists. */
  669. } else if (in_crash_recovery) {
  670. /* Check that the tablespace (the .ibd file) really
  671. exists; print a warning to the .err log if not.
  672. Do not print warnings for temporary tables. */
  673. ibool is_temp;
  674. field = rec_get_nth_field_old(rec, 4, &len);
  675. if (0x80000000UL & mach_read_from_4(field)) {
  676. /* ROW_FORMAT=COMPACT: read the is_temp
  677. flag from SYS_TABLES.MIX_LEN. */
  678. field = rec_get_nth_field_old(rec, 7, &len);
  679. is_temp = mach_read_from_4(field)
  680. & DICT_TF2_TEMPORARY;
  681. } else {
  682. /* For tables created with old versions
  683. of InnoDB, SYS_TABLES.MIX_LEN may contain
  684. garbage. Such tables would always be
  685. in ROW_FORMAT=REDUNDANT. Pretend that
  686. all such tables are non-temporary. That is,
  687. do not suppress error printouts about
  688. temporary tables not being found. */
  689. is_temp = FALSE;
  690. }
  691. fil_space_for_table_exists_in_mem(
  692. space_id, name, is_temp, TRUE, !is_temp);
  693. } else {
  694. /* It is a normal database startup: create the space
  695. object and check that the .ibd file exists. */
  696. fil_open_single_table_tablespace(FALSE, space_id,
  697. flags, name, NULL);
  698. }
  699. mem_free(name);
  700. if (space_id > max_space_id) {
  701. max_space_id = space_id;
  702. }
  703. mtr_start(&mtr);
  704. btr_pcur_restore_position(BTR_SEARCH_LEAF, &pcur, &mtr);
  705. }
  706. goto loop;
  707. }
  708. /********************************************************************//**
  709. Loads a table column definition from a SYS_COLUMNS record to
  710. dict_table_t.
  711. @return error message, or NULL on success */
  712. UNIV_INTERN
  713. const char*
  714. dict_load_column_low(
  715. /*=================*/
  716. dict_table_t* table, /*!< in/out: table, could be NULL
  717. if we just populate a dict_column_t
  718. struct with information from
  719. a SYS_COLUMNS record */
  720. mem_heap_t* heap, /*!< in/out: memory heap
  721. for temporary storage */
  722. dict_col_t* column, /*!< out: dict_column_t to fill,
  723. or NULL if table != NULL */
  724. table_id_t* table_id, /*!< out: table id */
  725. const char** col_name, /*!< out: column name */
  726. const rec_t* rec) /*!< in: SYS_COLUMNS record */
  727. {
  728. char* name;
  729. const byte* field;
  730. ulint len;
  731. ulint mtype;
  732. ulint prtype;
  733. ulint col_len;
  734. ulint pos;
  735. ut_ad(table || column);
  736. if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
  737. return("delete-marked record in SYS_COLUMNS");
  738. }
  739. if (UNIV_UNLIKELY(rec_get_n_fields_old(rec) != 9)) {
  740. return("wrong number of columns in SYS_COLUMNS record");
  741. }
  742. field = rec_get_nth_field_old(rec, 0/*TABLE_ID*/, &len);
  743. if (UNIV_UNLIKELY(len != 8)) {
  744. err_len:
  745. return("incorrect column length in SYS_COLUMNS");
  746. }
  747. if (table_id) {
  748. *table_id = mach_read_from_8(field);
  749. } else if (UNIV_UNLIKELY(table->id != mach_read_from_8(field))) {
  750. return("SYS_COLUMNS.TABLE_ID mismatch");
  751. }
  752. field = rec_get_nth_field_old(rec, 1/*POS*/, &len);
  753. if (UNIV_UNLIKELY(len != 4)) {
  754. goto err_len;
  755. }
  756. pos = mach_read_from_4(field);
  757. if (UNIV_UNLIKELY(table && table->n_def != pos)) {
  758. return("SYS_COLUMNS.POS mismatch");
  759. }
  760. rec_get_nth_field_offs_old(rec, 2/*DB_TRX_ID*/, &len);
  761. if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
  762. goto err_len;
  763. }
  764. rec_get_nth_field_offs_old(rec, 3/*DB_ROLL_PTR*/, &len);
  765. if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
  766. goto err_len;
  767. }
  768. field = rec_get_nth_field_old(rec, 4/*NAME*/, &len);
  769. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  770. goto err_len;
  771. }
  772. name = mem_heap_strdupl(heap, (const char*) field, len);
  773. if (col_name) {
  774. *col_name = name;
  775. }
  776. field = rec_get_nth_field_old(rec, 5/*MTYPE*/, &len);
  777. if (UNIV_UNLIKELY(len != 4)) {
  778. goto err_len;
  779. }
  780. mtype = mach_read_from_4(field);
  781. field = rec_get_nth_field_old(rec, 6/*PRTYPE*/, &len);
  782. if (UNIV_UNLIKELY(len != 4)) {
  783. goto err_len;
  784. }
  785. prtype = mach_read_from_4(field);
  786. if (dtype_get_charset_coll(prtype) == 0
  787. && dtype_is_string_type(mtype)) {
  788. /* The table was created with < 4.1.2. */
  789. if (dtype_is_binary_string_type(mtype, prtype)) {
  790. /* Use the binary collation for
  791. string columns of binary type. */
  792. prtype = dtype_form_prtype(
  793. prtype,
  794. DATA_MYSQL_BINARY_CHARSET_COLL);
  795. } else {
  796. /* Use the default charset for
  797. other than binary columns. */
  798. prtype = dtype_form_prtype(
  799. prtype,
  800. data_mysql_default_charset_coll);
  801. }
  802. }
  803. field = rec_get_nth_field_old(rec, 7/*LEN*/, &len);
  804. if (UNIV_UNLIKELY(len != 4)) {
  805. goto err_len;
  806. }
  807. col_len = mach_read_from_4(field);
  808. field = rec_get_nth_field_old(rec, 8/*PREC*/, &len);
  809. if (UNIV_UNLIKELY(len != 4)) {
  810. goto err_len;
  811. }
  812. if (!column) {
  813. dict_mem_table_add_col(table, heap, name, mtype,
  814. prtype, col_len);
  815. } else {
  816. dict_mem_fill_column_struct(column, pos, mtype,
  817. prtype, col_len);
  818. }
  819. return(NULL);
  820. }
  821. /********************************************************************//**
  822. Loads definitions for table columns. */
  823. static
  824. void
  825. dict_load_columns(
  826. /*==============*/
  827. dict_table_t* table, /*!< in/out: table */
  828. mem_heap_t* heap) /*!< in/out: memory heap
  829. for temporary storage */
  830. {
  831. dict_table_t* sys_columns;
  832. dict_index_t* sys_index;
  833. btr_pcur_t pcur;
  834. dtuple_t* tuple;
  835. dfield_t* dfield;
  836. const rec_t* rec;
  837. byte* buf;
  838. ulint i;
  839. mtr_t mtr;
  840. ut_ad(mutex_own(&(dict_sys->mutex)));
  841. mtr_start(&mtr);
  842. sys_columns = dict_table_get_low("SYS_COLUMNS", DICT_ERR_IGNORE_NONE);
  843. sys_index = UT_LIST_GET_FIRST(sys_columns->indexes);
  844. ut_a(!dict_table_is_comp(sys_columns));
  845. ut_a(name_of_col_is(sys_columns, sys_index, 4, "NAME"));
  846. ut_a(name_of_col_is(sys_columns, sys_index, 8, "PREC"));
  847. tuple = dtuple_create(heap, 1);
  848. dfield = dtuple_get_nth_field(tuple, 0);
  849. buf = mem_heap_alloc(heap, 8);
  850. mach_write_to_8(buf, table->id);
  851. dfield_set_data(dfield, buf, 8);
  852. dict_index_copy_types(tuple, sys_index, 1);
  853. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  854. BTR_SEARCH_LEAF, &pcur, &mtr);
  855. for (i = 0; i + DATA_N_SYS_COLS < (ulint) table->n_cols; i++) {
  856. const char* err_msg;
  857. rec = btr_pcur_get_rec(&pcur);
  858. ut_a(btr_pcur_is_on_user_rec(&pcur));
  859. err_msg = dict_load_column_low(table, heap, NULL, NULL,
  860. NULL, rec);
  861. if (err_msg) {
  862. fprintf(stderr, "InnoDB: %s\n", err_msg);
  863. ut_error;
  864. }
  865. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  866. }
  867. btr_pcur_close(&pcur);
  868. mtr_commit(&mtr);
  869. }
  870. /** Error message for a delete-marked record in dict_load_field_low() */
  871. static const char* dict_load_field_del = "delete-marked record in SYS_FIELDS";
  872. static const char* dict_load_field_too_big = "column prefix exceeds maximum"
  873. " limit";
  874. /********************************************************************//**
  875. Loads an index field definition from a SYS_FIELDS record to
  876. dict_index_t.
  877. @return error message, or NULL on success */
  878. UNIV_INTERN
  879. const char*
  880. dict_load_field_low(
  881. /*================*/
  882. byte* index_id, /*!< in/out: index id (8 bytes)
  883. an "in" value if index != NULL
  884. and "out" if index == NULL */
  885. dict_index_t* index, /*!< in/out: index, could be NULL
  886. if we just populate a dict_field_t
  887. struct with information from
  888. a SYS_FIELDSS record */
  889. dict_field_t* sys_field, /*!< out: dict_field_t to be
  890. filled */
  891. ulint* pos, /*!< out: Field position */
  892. byte* last_index_id, /*!< in: last index id */
  893. mem_heap_t* heap, /*!< in/out: memory heap
  894. for temporary storage */
  895. const rec_t* rec, /*!< in: SYS_FIELDS record */
  896. char* addition_err_str,/*!< out: additional error message
  897. that requires information to be
  898. filled, or NULL */
  899. ulint err_str_len) /*!< in: length of addition_err_str
  900. in bytes */
  901. {
  902. const byte* field;
  903. ulint len;
  904. ulint pos_and_prefix_len;
  905. ulint prefix_len;
  906. ibool first_field;
  907. ulint position;
  908. /* Either index or sys_field is supplied, not both */
  909. ut_a((!index) || (!sys_field));
  910. if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
  911. return(dict_load_field_del);
  912. }
  913. if (UNIV_UNLIKELY(rec_get_n_fields_old(rec) != 5)) {
  914. return("wrong number of columns in SYS_FIELDS record");
  915. }
  916. field = rec_get_nth_field_old(rec, 0/*INDEX_ID*/, &len);
  917. if (UNIV_UNLIKELY(len != 8)) {
  918. err_len:
  919. return("incorrect column length in SYS_FIELDS");
  920. }
  921. if (!index) {
  922. ut_a(last_index_id);
  923. memcpy(index_id, (const char*)field, 8);
  924. first_field = memcmp(index_id, last_index_id, 8);
  925. } else {
  926. first_field = (index->n_def == 0);
  927. if (memcmp(field, index_id, 8)) {
  928. return("SYS_FIELDS.INDEX_ID mismatch");
  929. }
  930. }
  931. field = rec_get_nth_field_old(rec, 1/*POS*/, &len);
  932. if (UNIV_UNLIKELY(len != 4)) {
  933. goto err_len;
  934. }
  935. rec_get_nth_field_offs_old(rec, 2/*DB_TRX_ID*/, &len);
  936. if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
  937. goto err_len;
  938. }
  939. rec_get_nth_field_offs_old(rec, 3/*DB_ROLL_PTR*/, &len);
  940. if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
  941. goto err_len;
  942. }
  943. /* The next field stores the field position in the index and a
  944. possible column prefix length if the index field does not
  945. contain the whole column. The storage format is like this: if
  946. there is at least one prefix field in the index, then the HIGH
  947. 2 bytes contain the field number (index->n_def) and the low 2
  948. bytes the prefix length for the field. Otherwise the field
  949. number (index->n_def) is contained in the 2 LOW bytes. */
  950. pos_and_prefix_len = mach_read_from_4(field);
  951. if (index && UNIV_UNLIKELY
  952. ((pos_and_prefix_len & 0xFFFFUL) != index->n_def
  953. && (pos_and_prefix_len >> 16 & 0xFFFF) != index->n_def)) {
  954. return("SYS_FIELDS.POS mismatch");
  955. }
  956. if (first_field || pos_and_prefix_len > 0xFFFFUL) {
  957. prefix_len = pos_and_prefix_len & 0xFFFFUL;
  958. position = (pos_and_prefix_len & 0xFFFF0000UL) >> 16;
  959. } else {
  960. prefix_len = 0;
  961. position = pos_and_prefix_len & 0xFFFFUL;
  962. }
  963. field = rec_get_nth_field_old(rec, 4, &len);
  964. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  965. goto err_len;
  966. }
  967. if (prefix_len > REC_VERSION_56_MAX_INDEX_COL_LEN) {
  968. if (addition_err_str) {
  969. ut_snprintf(addition_err_str, err_str_len,
  970. "index field '%s' has a prefix length"
  971. " of %lu bytes",
  972. mem_heap_strdupl(
  973. heap, (const char*) field, len),
  974. (ulong) prefix_len);
  975. }
  976. return(dict_load_field_too_big);
  977. }
  978. if (index) {
  979. dict_mem_index_add_field(
  980. index, mem_heap_strdupl(heap, (const char*) field, len),
  981. prefix_len);
  982. } else {
  983. ut_a(sys_field);
  984. ut_a(pos);
  985. sys_field->name = mem_heap_strdupl(
  986. heap, (const char*) field, len);
  987. sys_field->prefix_len = prefix_len;
  988. *pos = position;
  989. }
  990. return(NULL);
  991. }
  992. /********************************************************************//**
  993. Loads definitions for index fields.
  994. @return DB_SUCCESS if ok, DB_CORRUPTION if corruption */
  995. static
  996. ulint
  997. dict_load_fields(
  998. /*=============*/
  999. dict_index_t* index, /*!< in/out: index whose fields to load */
  1000. mem_heap_t* heap) /*!< in: memory heap for temporary storage */
  1001. {
  1002. dict_table_t* sys_fields;
  1003. dict_index_t* sys_index;
  1004. btr_pcur_t pcur;
  1005. dtuple_t* tuple;
  1006. dfield_t* dfield;
  1007. const rec_t* rec;
  1008. byte* buf;
  1009. ulint i;
  1010. mtr_t mtr;
  1011. ulint error;
  1012. ut_ad(mutex_own(&(dict_sys->mutex)));
  1013. mtr_start(&mtr);
  1014. sys_fields = dict_table_get_low("SYS_FIELDS", DICT_ERR_IGNORE_NONE);
  1015. sys_index = UT_LIST_GET_FIRST(sys_fields->indexes);
  1016. ut_a(!dict_table_is_comp(sys_fields));
  1017. ut_a(name_of_col_is(sys_fields, sys_index, 4, "COL_NAME"));
  1018. tuple = dtuple_create(heap, 1);
  1019. dfield = dtuple_get_nth_field(tuple, 0);
  1020. buf = mem_heap_alloc(heap, 8);
  1021. mach_write_to_8(buf, index->id);
  1022. dfield_set_data(dfield, buf, 8);
  1023. dict_index_copy_types(tuple, sys_index, 1);
  1024. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1025. BTR_SEARCH_LEAF, &pcur, &mtr);
  1026. for (i = 0; i < index->n_fields; i++) {
  1027. const char* err_msg;
  1028. char addition_err_str[1024];
  1029. rec = btr_pcur_get_rec(&pcur);
  1030. ut_a(btr_pcur_is_on_user_rec(&pcur));
  1031. err_msg = dict_load_field_low(buf, index, NULL, NULL, NULL,
  1032. heap, rec, addition_err_str,
  1033. sizeof(addition_err_str));
  1034. if (err_msg == dict_load_field_del) {
  1035. /* There could be delete marked records in
  1036. SYS_FIELDS because SYS_FIELDS.INDEX_ID can be
  1037. updated by ALTER TABLE ADD INDEX. */
  1038. goto next_rec;
  1039. } else if (err_msg) {
  1040. if (err_msg == dict_load_field_too_big) {
  1041. fprintf(stderr, "InnoDB: Error: load index"
  1042. " '%s' failed.\n"
  1043. "InnoDB: %s,\n"
  1044. "InnoDB: which exceeds the"
  1045. " maximum limit of %lu bytes.\n"
  1046. "InnoDB: Please use server that"
  1047. " supports long index prefix\n"
  1048. "InnoDB: or turn on"
  1049. " innodb_force_recovery to load"
  1050. " the table\n",
  1051. index->name, addition_err_str,
  1052. (ulong) (REC_VERSION_56_MAX_INDEX_COL_LEN));
  1053. } else {
  1054. fprintf(stderr, "InnoDB: %s\n", err_msg);
  1055. }
  1056. error = DB_CORRUPTION;
  1057. goto func_exit;
  1058. }
  1059. next_rec:
  1060. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  1061. }
  1062. error = DB_SUCCESS;
  1063. func_exit:
  1064. btr_pcur_close(&pcur);
  1065. mtr_commit(&mtr);
  1066. return(error);
  1067. }
  1068. /** Error message for a delete-marked record in dict_load_index_low() */
  1069. static const char* dict_load_index_del = "delete-marked record in SYS_INDEXES";
  1070. /** Error message for table->id mismatch in dict_load_index_low() */
  1071. static const char* dict_load_index_id_err = "SYS_INDEXES.TABLE_ID mismatch";
  1072. /********************************************************************//**
  1073. Loads an index definition from a SYS_INDEXES record to dict_index_t.
  1074. If allocate=TRUE, we will create a dict_index_t structure and fill it
  1075. accordingly. If allocated=FALSE, the dict_index_t will be supplied by
  1076. the caller and filled with information read from the record. @return
  1077. error message, or NULL on success */
  1078. UNIV_INTERN
  1079. const char*
  1080. dict_load_index_low(
  1081. /*================*/
  1082. byte* table_id, /*!< in/out: table id (8 bytes),
  1083. an "in" value if allocate=TRUE
  1084. and "out" when allocate=FALSE */
  1085. const char* table_name, /*!< in: table name */
  1086. mem_heap_t* heap, /*!< in/out: temporary memory heap */
  1087. const rec_t* rec, /*!< in: SYS_INDEXES record */
  1088. ibool allocate, /*!< in: TRUE=allocate *index,
  1089. FALSE=fill in a pre-allocated
  1090. *index */
  1091. dict_index_t** index) /*!< out,own: index, or NULL */
  1092. {
  1093. const byte* field;
  1094. ulint len;
  1095. ulint name_len;
  1096. char* name_buf;
  1097. index_id_t id;
  1098. ulint n_fields;
  1099. ulint type;
  1100. ulint space;
  1101. if (allocate) {
  1102. /* If allocate=TRUE, no dict_index_t will
  1103. be supplied. Initialize "*index" to NULL */
  1104. *index = NULL;
  1105. }
  1106. if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
  1107. return(dict_load_index_del);
  1108. }
  1109. if (UNIV_UNLIKELY(rec_get_n_fields_old(rec) != 9)) {
  1110. return("wrong number of columns in SYS_INDEXES record");
  1111. }
  1112. field = rec_get_nth_field_old(rec, 0/*TABLE_ID*/, &len);
  1113. if (UNIV_UNLIKELY(len != 8)) {
  1114. err_len:
  1115. return("incorrect column length in SYS_INDEXES");
  1116. }
  1117. if (!allocate) {
  1118. /* We are reading a SYS_INDEXES record. Copy the table_id */
  1119. memcpy(table_id, (const char*)field, 8);
  1120. } else if (memcmp(field, table_id, 8)) {
  1121. /* Caller supplied table_id, verify it is the same
  1122. id as on the index record */
  1123. return(dict_load_index_id_err);
  1124. }
  1125. field = rec_get_nth_field_old(rec, 1/*ID*/, &len);
  1126. if (UNIV_UNLIKELY(len != 8)) {
  1127. goto err_len;
  1128. }
  1129. id = mach_read_from_8(field);
  1130. rec_get_nth_field_offs_old(rec, 2/*DB_TRX_ID*/, &len);
  1131. if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
  1132. goto err_len;
  1133. }
  1134. rec_get_nth_field_offs_old(rec, 3/*DB_ROLL_PTR*/, &len);
  1135. if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
  1136. goto err_len;
  1137. }
  1138. field = rec_get_nth_field_old(rec, 4/*NAME*/, &name_len);
  1139. if (UNIV_UNLIKELY(name_len == UNIV_SQL_NULL)) {
  1140. goto err_len;
  1141. }
  1142. name_buf = mem_heap_strdupl(heap, (const char*) field,
  1143. name_len);
  1144. field = rec_get_nth_field_old(rec, 5/*N_FIELDS*/, &len);
  1145. if (UNIV_UNLIKELY(len != 4)) {
  1146. goto err_len;
  1147. }
  1148. n_fields = mach_read_from_4(field);
  1149. field = rec_get_nth_field_old(rec, 6/*TYPE*/, &len);
  1150. if (UNIV_UNLIKELY(len != 4)) {
  1151. goto err_len;
  1152. }
  1153. type = mach_read_from_4(field);
  1154. if (UNIV_UNLIKELY(type & (~0 << DICT_IT_BITS))) {
  1155. return("unknown SYS_INDEXES.TYPE bits");
  1156. }
  1157. field = rec_get_nth_field_old(rec, 7/*SPACE*/, &len);
  1158. if (UNIV_UNLIKELY(len != 4)) {
  1159. goto err_len;
  1160. }
  1161. space = mach_read_from_4(field);
  1162. field = rec_get_nth_field_old(rec, 8/*PAGE_NO*/, &len);
  1163. if (UNIV_UNLIKELY(len != 4)) {
  1164. goto err_len;
  1165. }
  1166. if (allocate) {
  1167. *index = dict_mem_index_create(table_name, name_buf,
  1168. space, type, n_fields);
  1169. } else {
  1170. ut_a(*index);
  1171. dict_mem_fill_index_struct(*index, NULL, NULL, name_buf,
  1172. space, type, n_fields);
  1173. }
  1174. (*index)->id = id;
  1175. (*index)->page = mach_read_from_4(field);
  1176. btr_search_index_init(*index);
  1177. ut_ad((*index)->page);
  1178. return(NULL);
  1179. }
  1180. /********************************************************************//**
  1181. Loads definitions for table indexes. Adds them to the data dictionary
  1182. cache.
  1183. @return DB_SUCCESS if ok, DB_CORRUPTION if corruption of dictionary
  1184. table or DB_UNSUPPORTED if table has unknown index type */
  1185. static
  1186. ulint
  1187. dict_load_indexes(
  1188. /*==============*/
  1189. dict_table_t* table, /*!< in/out: table */
  1190. mem_heap_t* heap, /*!< in: memory heap for temporary storage */
  1191. dict_err_ignore_t ignore_err)
  1192. /*!< in: error to be ignored when
  1193. loading the index definition */
  1194. {
  1195. dict_table_t* sys_indexes;
  1196. dict_index_t* sys_index;
  1197. btr_pcur_t pcur;
  1198. dtuple_t* tuple;
  1199. dfield_t* dfield;
  1200. const rec_t* rec;
  1201. byte* buf;
  1202. mtr_t mtr;
  1203. ulint error = DB_SUCCESS;
  1204. ut_ad(mutex_own(&(dict_sys->mutex)));
  1205. mtr_start(&mtr);
  1206. sys_indexes = dict_table_get_low("SYS_INDEXES", DICT_ERR_IGNORE_NONE);
  1207. sys_index = UT_LIST_GET_FIRST(sys_indexes->indexes);
  1208. ut_a(!dict_table_is_comp(sys_indexes));
  1209. ut_a(name_of_col_is(sys_indexes, sys_index, 4, "NAME"));
  1210. ut_a(name_of_col_is(sys_indexes, sys_index, 8, "PAGE_NO"));
  1211. tuple = dtuple_create(heap, 1);
  1212. dfield = dtuple_get_nth_field(tuple, 0);
  1213. buf = mem_heap_alloc(heap, 8);
  1214. mach_write_to_8(buf, table->id);
  1215. dfield_set_data(dfield, buf, 8);
  1216. dict_index_copy_types(tuple, sys_index, 1);
  1217. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1218. BTR_SEARCH_LEAF, &pcur, &mtr);
  1219. for (;;) {
  1220. dict_index_t* index = NULL;
  1221. const char* err_msg;
  1222. if (!btr_pcur_is_on_user_rec(&pcur)) {
  1223. break;
  1224. }
  1225. rec = btr_pcur_get_rec(&pcur);
  1226. err_msg = dict_load_index_low(buf, table->name, heap, rec,
  1227. TRUE, &index);
  1228. ut_ad((index == NULL) == (err_msg != NULL));
  1229. if (err_msg == dict_load_index_id_err) {
  1230. /* TABLE_ID mismatch means that we have
  1231. run out of index definitions for the table. */
  1232. break;
  1233. } else if (err_msg == dict_load_index_del) {
  1234. /* Skip delete-marked records. */
  1235. goto next_rec;
  1236. } else if (err_msg) {
  1237. fprintf(stderr, "InnoDB: %s\n", err_msg);
  1238. if (ignore_err & DICT_ERR_IGNORE_CORRUPT) {
  1239. goto next_rec;
  1240. }
  1241. error = DB_CORRUPTION;
  1242. goto func_exit;
  1243. }
  1244. ut_ad(index);
  1245. /* Check whether the index is corrupted */
  1246. if (dict_index_is_corrupted(index)) {
  1247. ut_print_timestamp(stderr);
  1248. fputs(" InnoDB: ", stderr);
  1249. dict_index_name_print(stderr, NULL, index);
  1250. fputs(" is corrupted\n", stderr);
  1251. if (!srv_load_corrupted
  1252. && !(ignore_err & DICT_ERR_IGNORE_CORRUPT)
  1253. && dict_index_is_clust(index)) {
  1254. dict_mem_index_free(index);
  1255. error = DB_INDEX_CORRUPT;
  1256. goto func_exit;
  1257. } else {
  1258. /* We will load the index if
  1259. 1) srv_load_corrupted is TRUE
  1260. 2) ignore_err is set with
  1261. DICT_ERR_IGNORE_CORRUPT
  1262. 3) if the index corrupted is a secondary
  1263. index */
  1264. ut_print_timestamp(stderr);
  1265. fputs(" InnoDB: load corrupted index ", stderr);
  1266. dict_index_name_print(stderr, NULL, index);
  1267. putc('\n', stderr);
  1268. }
  1269. }
  1270. /* We check for unsupported types first, so that the
  1271. subsequent checks are relevant for the supported types. */
  1272. if (index->type & ~(DICT_CLUSTERED | DICT_UNIQUE
  1273. | DICT_CORRUPT)) {
  1274. fprintf(stderr,
  1275. "InnoDB: Error: unknown type %lu"
  1276. " of index %s of table %s\n",
  1277. (ulong) index->type, index->name, table->name);
  1278. error = DB_UNSUPPORTED;
  1279. dict_mem_index_free(index);
  1280. goto func_exit;
  1281. } else if (index->page == FIL_NULL) {
  1282. fprintf(stderr,
  1283. "InnoDB: Error: trying to load index %s"
  1284. " for table %s\n"
  1285. "InnoDB: but the index tree has been freed!\n",
  1286. index->name, table->name);
  1287. if (ignore_err & DICT_ERR_IGNORE_INDEX_ROOT) {
  1288. /* If caller can tolerate this error,
  1289. we will continue to load the index and
  1290. let caller deal with this error. However
  1291. mark the index and table corrupted. We
  1292. only need to mark such in the index
  1293. dictionary cache for such metadata corruption,
  1294. since we would always be able to set it
  1295. when loading the dictionary cache */
  1296. dict_set_corrupted_index_cache_only(
  1297. index, table);
  1298. fprintf(stderr,
  1299. "InnoDB: Index is corrupt but forcing"
  1300. " load into data dictionary\n");
  1301. } else {
  1302. corrupted:
  1303. dict_mem_index_free(index);
  1304. error = DB_CORRUPTION;
  1305. goto func_exit;
  1306. }
  1307. } else if (!dict_index_is_clust(index)
  1308. && NULL == dict_table_get_first_index(table)) {
  1309. fputs("InnoDB: Error: trying to load index ",
  1310. stderr);
  1311. ut_print_name(stderr, NULL, FALSE, index->name);
  1312. fputs(" for table ", stderr);
  1313. ut_print_name(stderr, NULL, TRUE, table->name);
  1314. fputs("\nInnoDB: but the first index"
  1315. " is not clustered!\n", stderr);
  1316. goto corrupted;
  1317. } else if (table->id < DICT_HDR_FIRST_ID
  1318. && (dict_index_is_clust(index)
  1319. || ((table == dict_sys->sys_tables)
  1320. && !strcmp("ID_IND", index->name)))) {
  1321. /* The index was created in memory already at booting
  1322. of the database server */
  1323. dict_mem_index_free(index);
  1324. } else {
  1325. error = dict_load_fields(index, heap);
  1326. if (error != DB_SUCCESS) {
  1327. fprintf(stderr, "InnoDB: Error: load index '%s'"
  1328. " for table '%s' failed\n",
  1329. index->name, table->name);
  1330. /* If the force recovery flag is set, and
  1331. if the failed index is not the clustered index,
  1332. we will continue and open other indexes */
  1333. if ((srv_force_recovery
  1334. || srv_load_corrupted)
  1335. && !dict_index_is_clust(index)) {
  1336. error = DB_SUCCESS;
  1337. goto next_rec;
  1338. } else {
  1339. goto func_exit;
  1340. }
  1341. }
  1342. error = dict_index_add_to_cache(table, index,
  1343. index->page, FALSE);
  1344. /* The data dictionary tables should never contain
  1345. invalid index definitions. If we ignored this error
  1346. and simply did not load this index definition, the
  1347. .frm file would disagree with the index definitions
  1348. inside InnoDB. */
  1349. if (UNIV_UNLIKELY(error != DB_SUCCESS)) {
  1350. goto func_exit;
  1351. }
  1352. }
  1353. next_rec:
  1354. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  1355. }
  1356. func_exit:
  1357. btr_pcur_close(&pcur);
  1358. mtr_commit(&mtr);
  1359. return(error);
  1360. }
  1361. /********************************************************************//**
  1362. Loads a table definition from a SYS_TABLES record to dict_table_t.
  1363. Does not load any columns or indexes.
  1364. @return error message, or NULL on success */
  1365. UNIV_INTERN
  1366. const char*
  1367. dict_load_table_low(
  1368. /*================*/
  1369. const char* name, /*!< in: table name */
  1370. const rec_t* rec, /*!< in: SYS_TABLES record */
  1371. dict_table_t** table) /*!< out,own: table, or NULL */
  1372. {
  1373. const byte* field;
  1374. ulint len;
  1375. ulint space;
  1376. ulint n_cols;
  1377. ulint flags;
  1378. if (UNIV_UNLIKELY(rec_get_deleted_flag(rec, 0))) {
  1379. return("delete-marked record in SYS_TABLES");
  1380. }
  1381. if (UNIV_UNLIKELY(rec_get_n_fields_old(rec) != 10)) {
  1382. return("wrong number of columns in SYS_TABLES record");
  1383. }
  1384. rec_get_nth_field_offs_old(rec, 0/*NAME*/, &len);
  1385. if (UNIV_UNLIKELY(len < 1 || len == UNIV_SQL_NULL)) {
  1386. err_len:
  1387. return("incorrect column length in SYS_TABLES");
  1388. }
  1389. rec_get_nth_field_offs_old(rec, 1/*DB_TRX_ID*/, &len);
  1390. if (UNIV_UNLIKELY(len != DATA_TRX_ID_LEN && len != UNIV_SQL_NULL)) {
  1391. goto err_len;
  1392. }
  1393. rec_get_nth_field_offs_old(rec, 2/*DB_ROLL_PTR*/, &len);
  1394. if (UNIV_UNLIKELY(len != DATA_ROLL_PTR_LEN && len != UNIV_SQL_NULL)) {
  1395. goto err_len;
  1396. }
  1397. rec_get_nth_field_offs_old(rec, 3/*ID*/, &len);
  1398. if (UNIV_UNLIKELY(len != 8)) {
  1399. goto err_len;
  1400. }
  1401. field = rec_get_nth_field_old(rec, 4/*N_COLS*/, &len);
  1402. if (UNIV_UNLIKELY(len != 4)) {
  1403. goto err_len;
  1404. }
  1405. n_cols = mach_read_from_4(field);
  1406. rec_get_nth_field_offs_old(rec, 5/*TYPE*/, &len);
  1407. if (UNIV_UNLIKELY(len != 4)) {
  1408. goto err_len;
  1409. }
  1410. rec_get_nth_field_offs_old(rec, 6/*MIX_ID*/, &len);
  1411. if (UNIV_UNLIKELY(len != 8)) {
  1412. goto err_len;
  1413. }
  1414. rec_get_nth_field_offs_old(rec, 7/*MIX_LEN*/, &len);
  1415. if (UNIV_UNLIKELY(len != 4)) {
  1416. goto err_len;
  1417. }
  1418. rec_get_nth_field_offs_old(rec, 8/*CLUSTER_ID*/, &len);
  1419. if (UNIV_UNLIKELY(len != UNIV_SQL_NULL)) {
  1420. goto err_len;
  1421. }
  1422. field = rec_get_nth_field_old(rec, 9/*SPACE*/, &len);
  1423. if (UNIV_UNLIKELY(len != 4)) {
  1424. goto err_len;
  1425. }
  1426. space = mach_read_from_4(field);
  1427. /* Check if the tablespace exists and has the right name */
  1428. if (!trx_sys_sys_space(space)) {
  1429. flags = dict_sys_tables_get_flags(rec);
  1430. if (UNIV_UNLIKELY(flags == ULINT_UNDEFINED)) {
  1431. field = rec_get_nth_field_old(rec, 5/*TYPE*/, &len);
  1432. ut_ad(len == 4); /* this was checked earlier */
  1433. flags = mach_read_from_4(field);
  1434. ut_print_timestamp(stderr);
  1435. fputs(" InnoDB: Error: table ", stderr);
  1436. ut_print_filename(stderr, name);
  1437. fprintf(stderr, "\n"
  1438. "InnoDB: in InnoDB data dictionary"
  1439. " has unknown type %lx.\n",
  1440. (ulong) flags);
  1441. return("incorrect flags in SYS_TABLES");
  1442. }
  1443. } else {
  1444. flags = 0;
  1445. }
  1446. /* The high-order bit of N_COLS is the "compact format" flag.
  1447. For tables in that format, MIX_LEN may hold additional flags. */
  1448. if (n_cols & 0x80000000UL) {
  1449. ulint flags2;
  1450. flags |= DICT_TF_COMPACT;
  1451. field = rec_get_nth_field_old(rec, 7, &len);
  1452. if (UNIV_UNLIKELY(len != 4)) {
  1453. goto err_len;
  1454. }
  1455. flags2 = mach_read_from_4(field);
  1456. if (flags2 & (~0 << (DICT_TF2_BITS - DICT_TF2_SHIFT))) {
  1457. ut_print_timestamp(stderr);
  1458. fputs(" InnoDB: Warning: table ", stderr);
  1459. ut_print_filename(stderr, name);
  1460. fprintf(stderr, "\n"
  1461. "InnoDB: in InnoDB data dictionary"
  1462. " has unknown flags %lx.\n",
  1463. (ulong) flags2);
  1464. flags2 &= ~(~0 << (DICT_TF2_BITS - DICT_TF2_SHIFT));
  1465. }
  1466. flags |= flags2 << DICT_TF2_SHIFT;
  1467. }
  1468. /* See if the tablespace is available. */
  1469. *table = dict_mem_table_create(name, space, n_cols & ~0x80000000UL,
  1470. flags);
  1471. field = rec_get_nth_field_old(rec, 3/*ID*/, &len);
  1472. ut_ad(len == 8); /* this was checked earlier */
  1473. (*table)->id = mach_read_from_8(field);
  1474. (*table)->ibd_file_missing = FALSE;
  1475. return(NULL);
  1476. }
  1477. /********************************************************************//**
  1478. Loads a table definition and also all its index definitions, and also
  1479. the cluster definition if the table is a member in a cluster. Also loads
  1480. all foreign key constraints where the foreign key is in the table or where
  1481. a foreign key references columns in this table. Adds all these to the data
  1482. dictionary cache.
  1483. @return table, NULL if does not exist; if the table is stored in an
  1484. .ibd file, but the file does not exist, then we set the
  1485. ibd_file_missing flag TRUE in the table object we return */
  1486. UNIV_INTERN
  1487. dict_table_t*
  1488. dict_load_table(
  1489. /*============*/
  1490. const char* name, /*!< in: table name in the
  1491. databasename/tablename format */
  1492. ibool cached, /*!< in: TRUE=add to cache, FALSE=do not */
  1493. dict_err_ignore_t ignore_err)
  1494. /*!< in: error to be ignored when loading
  1495. table and its indexes' definition */
  1496. {
  1497. dict_table_t* table;
  1498. dict_table_t* sys_tables;
  1499. btr_pcur_t pcur;
  1500. dict_index_t* sys_index;
  1501. dtuple_t* tuple;
  1502. mem_heap_t* heap;
  1503. dfield_t* dfield;
  1504. const rec_t* rec;
  1505. const byte* field;
  1506. ulint len;
  1507. ulint err;
  1508. const char* err_msg;
  1509. mtr_t mtr;
  1510. ut_ad(mutex_own(&(dict_sys->mutex)));
  1511. heap = mem_heap_create(32000);
  1512. mtr_start(&mtr);
  1513. sys_tables = dict_table_get_low("SYS_TABLES", DICT_ERR_IGNORE_NONE);
  1514. sys_index = UT_LIST_GET_FIRST(sys_tables->indexes);
  1515. ut_a(!dict_table_is_comp(sys_tables));
  1516. ut_a(name_of_col_is(sys_tables, sys_index, 3, "ID"));
  1517. ut_a(name_of_col_is(sys_tables, sys_index, 4, "N_COLS"));
  1518. ut_a(name_of_col_is(sys_tables, sys_index, 5, "TYPE"));
  1519. ut_a(name_of_col_is(sys_tables, sys_index, 7, "MIX_LEN"));
  1520. ut_a(name_of_col_is(sys_tables, sys_index, 9, "SPACE"));
  1521. tuple = dtuple_create(heap, 1);
  1522. dfield = dtuple_get_nth_field(tuple, 0);
  1523. dfield_set_data(dfield, name, ut_strlen(name));
  1524. dict_index_copy_types(tuple, sys_index, 1);
  1525. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1526. BTR_SEARCH_LEAF, &pcur, &mtr);
  1527. rec = btr_pcur_get_rec(&pcur);
  1528. if (!btr_pcur_is_on_user_rec(&pcur)
  1529. || rec_get_deleted_flag(rec, 0)) {
  1530. /* Not found */
  1531. err_exit:
  1532. btr_pcur_close(&pcur);
  1533. mtr_commit(&mtr);
  1534. mem_heap_free(heap);
  1535. return(NULL);
  1536. }
  1537. field = rec_get_nth_field_old(rec, 0, &len);
  1538. /* Check if the table name in record is the searched one */
  1539. if (len != ut_strlen(name) || ut_memcmp(name, field, len) != 0) {
  1540. goto err_exit;
  1541. }
  1542. err_msg = dict_load_table_low(name, rec, &table);
  1543. if (err_msg) {
  1544. ut_print_timestamp(stderr);
  1545. fprintf(stderr, " InnoDB: %s\n", err_msg);
  1546. goto err_exit;
  1547. }
  1548. if (trx_sys_sys_space(table->space)) {
  1549. /* The system tablespace is always available. */
  1550. } else if (!fil_space_for_table_exists_in_mem(
  1551. table->space, name,
  1552. (table->flags >> DICT_TF2_SHIFT)
  1553. & DICT_TF2_TEMPORARY,
  1554. FALSE, FALSE)) {
  1555. if (table->flags & (DICT_TF2_TEMPORARY << DICT_TF2_SHIFT)) {
  1556. /* Do not bother to retry opening temporary tables. */
  1557. table->ibd_file_missing = TRUE;
  1558. } else {
  1559. ut_print_timestamp(stderr);
  1560. fprintf(stderr,
  1561. " InnoDB: error: space object of table ");
  1562. ut_print_filename(stderr, name);
  1563. fprintf(stderr, ",\n"
  1564. "InnoDB: space id %lu did not exist in memory."
  1565. " Retrying an open.\n",
  1566. (ulong) table->space);
  1567. /* Try to open the tablespace */
  1568. if (!fil_open_single_table_tablespace(
  1569. TRUE, table->space,
  1570. table->flags == DICT_TF_COMPACT ? 0 :
  1571. table->flags & ~(~0 << DICT_TF_BITS), name, NULL)) {
  1572. /* We failed to find a sensible
  1573. tablespace file */
  1574. table->ibd_file_missing = TRUE;
  1575. }
  1576. }
  1577. }
  1578. btr_pcur_close(&pcur);
  1579. mtr_commit(&mtr);
  1580. dict_load_columns(table, heap);
  1581. if (cached) {
  1582. dict_table_add_to_cache(table, heap);
  1583. } else {
  1584. dict_table_add_system_columns(table, heap);
  1585. }
  1586. mem_heap_empty(heap);
  1587. err = dict_load_indexes(table, heap, ignore_err);
  1588. if (err == DB_INDEX_CORRUPT) {
  1589. /* Refuse to load the table if the table has a corrupted
  1590. cluster index */
  1591. if (!srv_load_corrupted) {
  1592. fprintf(stderr, "InnoDB: Error: Load table ");
  1593. ut_print_name(stderr, NULL, TRUE, table->name);
  1594. fprintf(stderr, " failed, the table has corrupted"
  1595. " clustered indexes. Turn on"
  1596. " 'innodb_force_load_corrupted'"
  1597. " to drop it\n");
  1598. dict_table_remove_from_cache(table);
  1599. table = NULL;
  1600. goto func_exit;
  1601. } else {
  1602. dict_index_t* clust_index;
  1603. clust_index = dict_table_get_first_index(table);
  1604. if (dict_index_is_corrupted(clust_index)) {
  1605. table->corrupted = TRUE;
  1606. }
  1607. }
  1608. }
  1609. /* Initialize table foreign_child value. Its value could be
  1610. changed when dict_load_foreigns() is called below */
  1611. table->fk_max_recusive_level = 0;
  1612. /* If the force recovery flag is set, we open the table irrespective
  1613. of the error condition, since the user may want to dump data from the
  1614. clustered index. However we load the foreign key information only if
  1615. all indexes were loaded. */
  1616. if (!cached) {
  1617. } else if (err == DB_SUCCESS) {
  1618. err = dict_load_foreigns(table->name, TRUE, TRUE,
  1619. ignore_err);
  1620. if (err != DB_SUCCESS) {
  1621. fprintf(stderr,
  1622. "InnoDB: Load table '%s' failed, the table "
  1623. "has missing foreign key indexes. Turn off "
  1624. "'foreign_key_checks' and try again.",
  1625. table->name);
  1626. dict_table_remove_from_cache(table);
  1627. table = NULL;
  1628. } else {
  1629. table->fk_max_recusive_level = 0;
  1630. }
  1631. } else {
  1632. dict_index_t* index;
  1633. /* Make sure that at least the clustered index was loaded.
  1634. Otherwise refuse to load the table */
  1635. index = dict_table_get_first_index(table);
  1636. if (!srv_force_recovery || !index
  1637. || !dict_index_is_clust(index)) {
  1638. dict_table_remove_from_cache(table);
  1639. table = NULL;
  1640. } else if (dict_index_is_corrupted(index)) {
  1641. /* It is possible we force to load a corrupted
  1642. clustered index if srv_load_corrupted is set.
  1643. Mark the table as corrupted in this case */
  1644. table->corrupted = TRUE;
  1645. }
  1646. }
  1647. #if 0
  1648. if (err != DB_SUCCESS && table != NULL) {
  1649. mutex_enter(&dict_foreign_err_mutex);
  1650. ut_print_timestamp(stderr);
  1651. fprintf(stderr,
  1652. " InnoDB: Error: could not make a foreign key"
  1653. " definition to match\n"
  1654. "InnoDB: the foreign key table"
  1655. " or the referenced table!\n"
  1656. "InnoDB: The data dictionary of InnoDB is corrupt."
  1657. " You may need to drop\n"
  1658. "InnoDB: and recreate the foreign key table"
  1659. " or the referenced table.\n"
  1660. "InnoDB: Submit a detailed bug report"
  1661. " to http://bugs.mysql.com\n"
  1662. "InnoDB: Latest foreign key error printout:\n%s\n",
  1663. dict_foreign_err_buf);
  1664. mutex_exit(&dict_foreign_err_mutex);
  1665. }
  1666. #endif /* 0 */
  1667. func_exit:
  1668. mem_heap_free(heap);
  1669. return(table);
  1670. }
  1671. /***********************************************************************//**
  1672. Loads a table object based on the table id.
  1673. @return table; NULL if table does not exist */
  1674. UNIV_INTERN
  1675. dict_table_t*
  1676. dict_load_table_on_id(
  1677. /*==================*/
  1678. table_id_t table_id) /*!< in: table id */
  1679. {
  1680. byte id_buf[8];
  1681. btr_pcur_t pcur;
  1682. mem_heap_t* heap;
  1683. dtuple_t* tuple;
  1684. dfield_t* dfield;
  1685. dict_index_t* sys_table_ids;
  1686. dict_table_t* sys_tables;
  1687. const rec_t* rec;
  1688. const byte* field;
  1689. ulint len;
  1690. dict_table_t* table;
  1691. mtr_t mtr;
  1692. ut_ad(mutex_own(&(dict_sys->mutex)));
  1693. table = NULL;
  1694. /* NOTE that the operation of this function is protected by
  1695. the dictionary mutex, and therefore no deadlocks can occur
  1696. with other dictionary operations. */
  1697. mtr_start(&mtr);
  1698. /*---------------------------------------------------*/
  1699. /* Get the secondary index based on ID for table SYS_TABLES */
  1700. sys_tables = dict_sys->sys_tables;
  1701. sys_table_ids = dict_table_get_next_index(
  1702. dict_table_get_first_index(sys_tables));
  1703. ut_a(!dict_table_is_comp(sys_tables));
  1704. heap = mem_heap_create(256);
  1705. tuple = dtuple_create(heap, 1);
  1706. dfield = dtuple_get_nth_field(tuple, 0);
  1707. /* Write the table id in byte format to id_buf */
  1708. mach_write_to_8(id_buf, table_id);
  1709. dfield_set_data(dfield, id_buf, 8);
  1710. dict_index_copy_types(tuple, sys_table_ids, 1);
  1711. btr_pcur_open_on_user_rec(sys_table_ids, tuple, PAGE_CUR_GE,
  1712. BTR_SEARCH_LEAF, &pcur, &mtr);
  1713. rec = btr_pcur_get_rec(&pcur);
  1714. if (!btr_pcur_is_on_user_rec(&pcur)) {
  1715. /* Not found */
  1716. goto func_exit;
  1717. }
  1718. /* Find the first record that is not delete marked */
  1719. while (rec_get_deleted_flag(rec, 0)) {
  1720. if (!btr_pcur_move_to_next_user_rec(&pcur, &mtr)) {
  1721. goto func_exit;
  1722. }
  1723. rec = btr_pcur_get_rec(&pcur);
  1724. }
  1725. /*---------------------------------------------------*/
  1726. /* Now we have the record in the secondary index containing the
  1727. table ID and NAME */
  1728. rec = btr_pcur_get_rec(&pcur);
  1729. field = rec_get_nth_field_old(rec, 0, &len);
  1730. ut_ad(len == 8);
  1731. /* Check if the table id in record is the one searched for */
  1732. if (table_id != mach_read_from_8(field)) {
  1733. goto func_exit;
  1734. }
  1735. /* Now we get the table name from the record */
  1736. field = rec_get_nth_field_old(rec, 1, &len);
  1737. /* Load the table definition to memory */
  1738. table = dict_load_table(mem_heap_strdupl(heap, (char*) field, len),
  1739. TRUE, DICT_ERR_IGNORE_NONE);
  1740. func_exit:
  1741. btr_pcur_close(&pcur);
  1742. mtr_commit(&mtr);
  1743. mem_heap_free(heap);
  1744. return(table);
  1745. }
  1746. /********************************************************************//**
  1747. This function is called when the database is booted. Loads system table
  1748. index definitions except for the clustered index which is added to the
  1749. dictionary cache at booting before calling this function. */
  1750. UNIV_INTERN
  1751. void
  1752. dict_load_sys_table(
  1753. /*================*/
  1754. dict_table_t* table) /*!< in: system table */
  1755. {
  1756. mem_heap_t* heap;
  1757. ut_ad(mutex_own(&(dict_sys->mutex)));
  1758. heap = mem_heap_create(1000);
  1759. dict_load_indexes(table, heap, DICT_ERR_IGNORE_NONE);
  1760. mem_heap_free(heap);
  1761. }
  1762. /********************************************************************//**
  1763. Loads foreign key constraint col names (also for the referenced table). */
  1764. static
  1765. void
  1766. dict_load_foreign_cols(
  1767. /*===================*/
  1768. const char* id, /*!< in: foreign constraint id, not
  1769. necessary '\0'-terminated */
  1770. ulint id_len, /*!< in: id length */
  1771. dict_foreign_t* foreign)/*!< in: foreign constraint object */
  1772. {
  1773. dict_table_t* sys_foreign_cols;
  1774. dict_index_t* sys_index;
  1775. btr_pcur_t pcur;
  1776. dtuple_t* tuple;
  1777. dfield_t* dfield;
  1778. const rec_t* rec;
  1779. const byte* field;
  1780. ulint len;
  1781. ulint i;
  1782. mtr_t mtr;
  1783. ut_ad(mutex_own(&(dict_sys->mutex)));
  1784. foreign->foreign_col_names = mem_heap_alloc(
  1785. foreign->heap, foreign->n_fields * sizeof(void*));
  1786. foreign->referenced_col_names = mem_heap_alloc(
  1787. foreign->heap, foreign->n_fields * sizeof(void*));
  1788. mtr_start(&mtr);
  1789. sys_foreign_cols = dict_table_get_low("SYS_FOREIGN_COLS",
  1790. DICT_ERR_IGNORE_NONE);
  1791. sys_index = UT_LIST_GET_FIRST(sys_foreign_cols->indexes);
  1792. ut_a(!dict_table_is_comp(sys_foreign_cols));
  1793. tuple = dtuple_create(foreign->heap, 1);
  1794. dfield = dtuple_get_nth_field(tuple, 0);
  1795. dfield_set_data(dfield, id, id_len);
  1796. dict_index_copy_types(tuple, sys_index, 1);
  1797. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1798. BTR_SEARCH_LEAF, &pcur, &mtr);
  1799. for (i = 0; i < foreign->n_fields; i++) {
  1800. rec = btr_pcur_get_rec(&pcur);
  1801. ut_a(btr_pcur_is_on_user_rec(&pcur));
  1802. ut_a(!rec_get_deleted_flag(rec, 0));
  1803. field = rec_get_nth_field_old(rec, 0, &len);
  1804. ut_a(len == id_len);
  1805. ut_a(ut_memcmp(id, field, len) == 0);
  1806. field = rec_get_nth_field_old(rec, 1, &len);
  1807. ut_a(len == 4);
  1808. ut_a(i == mach_read_from_4(field));
  1809. field = rec_get_nth_field_old(rec, 4, &len);
  1810. foreign->foreign_col_names[i] = mem_heap_strdupl(
  1811. foreign->heap, (char*) field, len);
  1812. field = rec_get_nth_field_old(rec, 5, &len);
  1813. foreign->referenced_col_names[i] = mem_heap_strdupl(
  1814. foreign->heap, (char*) field, len);
  1815. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  1816. }
  1817. btr_pcur_close(&pcur);
  1818. mtr_commit(&mtr);
  1819. }
  1820. /***********************************************************************//**
  1821. Loads a foreign key constraint to the dictionary cache.
  1822. @return DB_SUCCESS or error code */
  1823. static
  1824. ulint
  1825. dict_load_foreign(
  1826. /*==============*/
  1827. const char* id,
  1828. /*!< in: foreign constraint id, not
  1829. necessary '\0'-terminated */
  1830. ulint id_len,
  1831. /*!< in: id length */
  1832. ibool check_charsets,
  1833. /*!< in: TRUE=check charset compatibility */
  1834. ibool check_recursive,
  1835. /*!< in: Whether to record the foreign table
  1836. parent count to avoid unlimited recursive
  1837. load of chained foreign tables */
  1838. dict_err_ignore_t ignore_err)
  1839. /*!< in: error to be ignored */
  1840. {
  1841. dict_foreign_t* foreign;
  1842. dict_table_t* sys_foreign;
  1843. btr_pcur_t pcur;
  1844. dict_index_t* sys_index;
  1845. dtuple_t* tuple;
  1846. mem_heap_t* heap2;
  1847. dfield_t* dfield;
  1848. const rec_t* rec;
  1849. const byte* field;
  1850. ulint len;
  1851. ulint n_fields_and_type;
  1852. mtr_t mtr;
  1853. dict_table_t* for_table;
  1854. dict_table_t* ref_table;
  1855. ut_ad(mutex_own(&(dict_sys->mutex)));
  1856. heap2 = mem_heap_create(1000);
  1857. mtr_start(&mtr);
  1858. sys_foreign = dict_table_get_low("SYS_FOREIGN", DICT_ERR_IGNORE_NONE);
  1859. sys_index = UT_LIST_GET_FIRST(sys_foreign->indexes);
  1860. ut_a(!dict_table_is_comp(sys_foreign));
  1861. tuple = dtuple_create(heap2, 1);
  1862. dfield = dtuple_get_nth_field(tuple, 0);
  1863. dfield_set_data(dfield, id, id_len);
  1864. dict_index_copy_types(tuple, sys_index, 1);
  1865. btr_pcur_open_on_user_rec(sys_index, tuple, PAGE_CUR_GE,
  1866. BTR_SEARCH_LEAF, &pcur, &mtr);
  1867. rec = btr_pcur_get_rec(&pcur);
  1868. if (!btr_pcur_is_on_user_rec(&pcur)
  1869. || rec_get_deleted_flag(rec, 0)) {
  1870. /* Not found */
  1871. fprintf(stderr,
  1872. "InnoDB: Error A: cannot load foreign constraint "
  1873. "%.*s\n", (int) id_len, id);
  1874. btr_pcur_close(&pcur);
  1875. mtr_commit(&mtr);
  1876. mem_heap_free(heap2);
  1877. return(DB_ERROR);
  1878. }
  1879. field = rec_get_nth_field_old(rec, 0, &len);
  1880. /* Check if the id in record is the searched one */
  1881. if (len != id_len || ut_memcmp(id, field, len) != 0) {
  1882. fprintf(stderr,
  1883. "InnoDB: Error B: cannot load foreign constraint "
  1884. "%.*s\n", (int) id_len, id);
  1885. btr_pcur_close(&pcur);
  1886. mtr_commit(&mtr);
  1887. mem_heap_free(heap2);
  1888. return(DB_ERROR);
  1889. }
  1890. /* Read the table names and the number of columns associated
  1891. with the constraint */
  1892. mem_heap_free(heap2);
  1893. foreign = dict_mem_foreign_create();
  1894. n_fields_and_type = mach_read_from_4(
  1895. rec_get_nth_field_old(rec, 5, &len));
  1896. ut_a(len == 4);
  1897. /* We store the type in the bits 24..29 of n_fields_and_type. */
  1898. foreign->type = (unsigned int) (n_fields_and_type >> 24);
  1899. foreign->n_fields = (unsigned int) (n_fields_and_type & 0x3FFUL);
  1900. foreign->id = mem_heap_strdupl(foreign->heap, id, id_len);
  1901. field = rec_get_nth_field_old(rec, 3, &len);
  1902. foreign->foreign_table_name = mem_heap_strdupl(
  1903. foreign->heap, (char*) field, len);
  1904. dict_mem_foreign_table_name_lookup_set(foreign, TRUE);
  1905. field = rec_get_nth_field_old(rec, 4, &len);
  1906. foreign->referenced_table_name = mem_heap_strdupl(
  1907. foreign->heap, (char*) field, len);
  1908. dict_mem_referenced_table_name_lookup_set(foreign, TRUE);
  1909. btr_pcur_close(&pcur);
  1910. mtr_commit(&mtr);
  1911. dict_load_foreign_cols(id, id_len, foreign);
  1912. ref_table = dict_table_check_if_in_cache_low(
  1913. foreign->referenced_table_name_lookup);
  1914. /* We could possibly wind up in a deep recursive calls if
  1915. we call dict_table_get_low() again here if there
  1916. is a chain of tables concatenated together with
  1917. foreign constraints. In such case, each table is
  1918. both a parent and child of the other tables, and
  1919. act as a "link" in such table chains.
  1920. To avoid such scenario, we would need to check the
  1921. number of ancesters the current table has. If that
  1922. exceeds DICT_FK_MAX_CHAIN_LEN, we will stop loading
  1923. the child table.
  1924. Foreign constraints are loaded in a Breath First fashion,
  1925. that is, the index on FOR_NAME is scanned first, and then
  1926. index on REF_NAME. So foreign constrains in which
  1927. current table is a child (foreign table) are loaded first,
  1928. and then those constraints where current table is a
  1929. parent (referenced) table.
  1930. Thus we could check the parent (ref_table) table's
  1931. reference count (fk_max_recusive_level) to know how deep the
  1932. recursive call is. If the parent table (ref_table) is already
  1933. loaded, and its fk_max_recusive_level is larger than
  1934. DICT_FK_MAX_CHAIN_LEN, we will stop the recursive loading
  1935. by skipping loading the child table. It will not affect foreign
  1936. constraint check for DMLs since child table will be loaded
  1937. at that time for the constraint check. */
  1938. if (!ref_table
  1939. || ref_table->fk_max_recusive_level < DICT_FK_MAX_RECURSIVE_LOAD) {
  1940. /* If the foreign table is not yet in the dictionary cache, we
  1941. have to load it so that we are able to make type comparisons
  1942. in the next function call. */
  1943. for_table = dict_table_get_low(
  1944. foreign->foreign_table_name_lookup,
  1945. DICT_ERR_IGNORE_NONE);
  1946. if (for_table && ref_table && check_recursive) {
  1947. /* This is to record the longest chain of ancesters
  1948. this table has, if the parent has more ancesters
  1949. than this table has, record it after add 1 (for this
  1950. parent */
  1951. if (ref_table->fk_max_recusive_level
  1952. >= for_table->fk_max_recusive_level) {
  1953. for_table->fk_max_recusive_level =
  1954. ref_table->fk_max_recusive_level + 1;
  1955. }
  1956. }
  1957. }
  1958. /* Note that there may already be a foreign constraint object in
  1959. the dictionary cache for this constraint: then the following
  1960. call only sets the pointers in it to point to the appropriate table
  1961. and index objects and frees the newly created object foreign.
  1962. Adding to the cache should always succeed since we are not creating
  1963. a new foreign key constraint but loading one from the data
  1964. dictionary. */
  1965. return(dict_foreign_add_to_cache(foreign, check_charsets, ignore_err));
  1966. }
  1967. /***********************************************************************//**
  1968. Loads foreign key constraints where the table is either the foreign key
  1969. holder or where the table is referenced by a foreign key. Adds these
  1970. constraints to the data dictionary. Note that we know that the dictionary
  1971. cache already contains all constraints where the other relevant table is
  1972. already in the dictionary cache.
  1973. @return DB_SUCCESS or error code */
  1974. UNIV_INTERN
  1975. ulint
  1976. dict_load_foreigns(
  1977. /*===============*/
  1978. const char* table_name, /*!< in: table name */
  1979. ibool check_recursive,/*!< in: Whether to check
  1980. recursive load of tables
  1981. chained by FK */
  1982. ibool check_charsets, /*!< in: TRUE=check charset
  1983. compatibility */
  1984. dict_err_ignore_t ignore_err) /*!< in: error to be ignored */
  1985. {
  1986. ulint tuple_buf[(DTUPLE_EST_ALLOC(1) + sizeof(ulint) - 1)
  1987. / sizeof(ulint)];
  1988. btr_pcur_t pcur;
  1989. dtuple_t* tuple;
  1990. dfield_t* dfield;
  1991. dict_index_t* sec_index;
  1992. dict_table_t* sys_foreign;
  1993. const rec_t* rec;
  1994. const byte* field;
  1995. ulint len;
  1996. ulint err;
  1997. mtr_t mtr;
  1998. ut_ad(mutex_own(&(dict_sys->mutex)));
  1999. sys_foreign = dict_table_get_low("SYS_FOREIGN", DICT_ERR_IGNORE_NONE);
  2000. if (sys_foreign == NULL) {
  2001. /* No foreign keys defined yet in this database */
  2002. fprintf(stderr,
  2003. "InnoDB: Error: no foreign key system tables"
  2004. " in the database\n");
  2005. return(DB_ERROR);
  2006. }
  2007. ut_a(!dict_table_is_comp(sys_foreign));
  2008. mtr_start(&mtr);
  2009. /* Get the secondary index based on FOR_NAME from table
  2010. SYS_FOREIGN */
  2011. sec_index = dict_table_get_next_index(
  2012. dict_table_get_first_index(sys_foreign));
  2013. start_load:
  2014. tuple = dtuple_create_from_mem(tuple_buf, sizeof(tuple_buf), 1);
  2015. dfield = dtuple_get_nth_field(tuple, 0);
  2016. dfield_set_data(dfield, table_name, ut_strlen(table_name));
  2017. dict_index_copy_types(tuple, sec_index, 1);
  2018. btr_pcur_open_on_user_rec(sec_index, tuple, PAGE_CUR_GE,
  2019. BTR_SEARCH_LEAF, &pcur, &mtr);
  2020. loop:
  2021. rec = btr_pcur_get_rec(&pcur);
  2022. if (!btr_pcur_is_on_user_rec(&pcur)) {
  2023. /* End of index */
  2024. goto load_next_index;
  2025. }
  2026. /* Now we have the record in the secondary index containing a table
  2027. name and a foreign constraint ID */
  2028. rec = btr_pcur_get_rec(&pcur);
  2029. field = rec_get_nth_field_old(rec, 0, &len);
  2030. /* Check if the table name in the record is the one searched for; the
  2031. following call does the comparison in the latin1_swedish_ci
  2032. charset-collation, in a case-insensitive way. */
  2033. if (0 != cmp_data_data(dfield_get_type(dfield)->mtype,
  2034. dfield_get_type(dfield)->prtype,
  2035. dfield_get_data(dfield), dfield_get_len(dfield),
  2036. field, len)) {
  2037. goto load_next_index;
  2038. }
  2039. /* Since table names in SYS_FOREIGN are stored in a case-insensitive
  2040. order, we have to check that the table name matches also in a binary
  2041. string comparison. On Unix, MySQL allows table names that only differ
  2042. in character case. If lower_case_table_names=2 then what is stored
  2043. may not be the same case, but the previous comparison showed that they
  2044. match with no-case. */
  2045. if ((innobase_get_lower_case_table_names() != 2)
  2046. && (0 != ut_memcmp(field, table_name, len))) {
  2047. goto next_rec;
  2048. }
  2049. if (rec_get_deleted_flag(rec, 0)) {
  2050. goto next_rec;
  2051. }
  2052. /* Now we get a foreign key constraint id */
  2053. field = rec_get_nth_field_old(rec, 1, &len);
  2054. btr_pcur_store_position(&pcur, &mtr);
  2055. mtr_commit(&mtr);
  2056. /* Load the foreign constraint definition to the dictionary cache */
  2057. err = dict_load_foreign((char*) field, len, check_charsets,
  2058. check_recursive, ignore_err);
  2059. if (err != DB_SUCCESS) {
  2060. btr_pcur_close(&pcur);
  2061. return(err);
  2062. }
  2063. mtr_start(&mtr);
  2064. btr_pcur_restore_position(BTR_SEARCH_LEAF, &pcur, &mtr);
  2065. next_rec:
  2066. btr_pcur_move_to_next_user_rec(&pcur, &mtr);
  2067. goto loop;
  2068. load_next_index:
  2069. btr_pcur_close(&pcur);
  2070. mtr_commit(&mtr);
  2071. sec_index = dict_table_get_next_index(sec_index);
  2072. if (sec_index != NULL) {
  2073. mtr_start(&mtr);
  2074. /* Switch to scan index on REF_NAME, fk_max_recusive_level
  2075. already been updated when scanning FOR_NAME index, no need to
  2076. update again */
  2077. check_recursive = FALSE;
  2078. goto start_load;
  2079. }
  2080. return(DB_SUCCESS);
  2081. }