Debian Codegraph @

storage/innobase/dict/dict0boot.cc

raw
1 /*****************************************************************************
2
3 Copyright (c) 1996, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file dict/dict0boot.cc
29 Data dictionary creation and booting
30
31 Created 4/18/1996 Heikki Tuuri
32 *******************************************************/
33
34 #include "dict0boot.h"
35 #include "btr0btr.h"
36 #include "buf0flu.h"
37 #include "dict0crea.h"
38 #include "dict0load.h"
39 #include "ha_prototypes.h"
40 #include "ibuf0ibuf.h"
41 #include "log0recv.h"
42
43 #include "dict0dd.h"
44 #include "os0file.h"
45 #include "srv0srv.h"
46 #include "trx0trx.h"
47
48 /** Gets a pointer to the dictionary header and x-latches its page.
49 @return pointer to the dictionary header, page x-latched */
50 dict_hdr_t *dict_hdr_get(mtr_t *mtr) /*!< in: mtr */
51 {
52 buf_block_t *block;
53 dict_hdr_t *header;
54
55 block = buf_page_get(page_id_t(DICT_HDR_SPACE, DICT_HDR_PAGE_NO),
56 univ_page_size, RW_X_LATCH, UT_LOCATION_HERE, mtr);
57 header = DICT_HDR + buf_block_get_frame(block);
58
59 buf_block_dbg_add_level(block, SYNC_DICT_HEADER);
60
61 return (header);
62 }
63
64 /** Returns a new table, index, or space id.
65 @param[out] table_id Table id (not assigned if null)
66 @param[out] index_id Index id (not assigned if null)
67 @param[out] space_id Space id (not assigned if null)
68 @param[in] table Table
69 @param[in] disable_redo If true and table object is null then disable-redo */
70 void dict_hdr_get_new_id(table_id_t *table_id, space_index_t *index_id,
71 space_id_t *space_id, const dict_table_t *table,
72 bool disable_redo) {
73 dict_hdr_t *dict_hdr;
74 ib_id_t id;
75 mtr_t mtr;
76
77 mtr_start(&mtr);
78
79 if (table) {
80 dict_disable_redo_if_temporary(table, &mtr);
81 } else if (disable_redo) {
82 /* In non-read-only mode we need to ensure that space-id header
83 page is written to disk else if page is removed from buffer
84 cache and re-loaded it would assign temporary tablespace id
85 to another tablespace.
86 This is not a case with read-only mode as there is no new object
87 that is created except temporary tablespace. */
88 mtr_set_log_mode(&mtr,
89 (srv_read_only_mode ? MTR_LOG_NONE : MTR_LOG_NO_REDO));
90 }
91
92 /* Server started and let's say space-id = x
93 - table created with file-per-table
94 - space-id = x + 1
95 - crash
96 Case 1: If it was redo logged then we know that it will be
97 restored to x + 1
98 Case 2: if not redo-logged
99 Header will have the old space-id = x
100 This is OK because on restart there is no object with
101 space id = x + 1
102 Case 3:
103 space-id = x (on start)
104 space-id = x+1 (temp-table allocation) - no redo logging
105 space-id = x+2 (non-temp-table allocation), this gets
106 redo logged.
107 If there is a crash there will be only 2 entries
108 x (original) and x+2 (new) and disk hdr will be updated
109 to reflect x + 2 entry.
110 We cannot allocate the same space id to different objects. */
111 dict_hdr = dict_hdr_get(&mtr);
112
113 if (table_id) {
114 id = mach_read_from_8(dict_hdr + DICT_HDR_TABLE_ID);
115 id++;
116
117 /* This means we are running out of table_ids and
118 entering into reserved range of table_ids for SDI
119 tables */
120 if (id >= dict_sdi_get_table_id(0)) {
121 ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_160)
122 << "InnoDB is running out of table_ids"
123 << " Please dump and reload the database";
124 }
125
126 mlog_write_ull(dict_hdr + DICT_HDR_TABLE_ID, id, &mtr);
127 *table_id = id;
128 }
129
130 if (index_id) {
131 id = mach_read_from_8(dict_hdr + DICT_HDR_INDEX_ID);
132 id++;
133 DBUG_EXECUTE_IF(
134 "simulate_index_id_exceed_uint32",
135 constexpr ib_id_t max_uint32 = 0xFFFFFFFF;
136 if (id < max_uint32) { id = max_uint32; });
137 mlog_write_ull(dict_hdr + DICT_HDR_INDEX_ID, id, &mtr);
138 *index_id = id;
139 }
140
141 if (space_id) {
142 *space_id =
143 mtr_read_ulint(dict_hdr + DICT_HDR_MAX_SPACE_ID, MLOG_4BYTES, &mtr);
144 if (fil_assign_new_space_id(space_id)) {
145 mlog_write_ulint(dict_hdr + DICT_HDR_MAX_SPACE_ID, *space_id, MLOG_4BYTES,
146 &mtr);
147 }
148 }
149
150 mtr_commit(&mtr);
151 }
152
153 /** Writes the current value of the row id counter to the dictionary header file
154 page. */
155 void dict_hdr_flush_row_id(void) {
156 dict_hdr_t *dict_hdr;
157 row_id_t id;
158 mtr_t mtr;
159
160 ut_ad(dict_sys_mutex_own());
161
162 id = dict_sys->row_id;
163
164 mtr_start(&mtr);
165
166 dict_hdr = dict_hdr_get(&mtr);
167
168 mlog_write_ull(dict_hdr + DICT_HDR_ROW_ID, id, &mtr);
169
170 mtr_commit(&mtr);
171 }
172
173 /** Creates the file page for the dictionary header. This function is
174 called only at the database creation.
175 @return true if succeed */
176 static bool dict_hdr_create(mtr_t *mtr) /*!< in: mtr */
177 {
178 buf_block_t *block;
179 dict_hdr_t *dict_header;
180
181 ut_ad(mtr);
182
183 /* Create the dictionary header file block in a new, allocated file
184 segment in the system tablespace */
185 block = fseg_create(DICT_HDR_SPACE, 0, DICT_HDR + DICT_HDR_FSEG_HEADER, mtr);
186
187 ut_a(DICT_HDR_PAGE_NO == block->page.id.page_no());
188
189 dict_header = dict_hdr_get(mtr);
190
191 /* Start counting row, table, index, and tree ids from 0 */
192 mlog_write_ull(dict_header + DICT_HDR_ROW_ID, 0, mtr);
193
194 mlog_write_ull(dict_header + DICT_HDR_TABLE_ID, DICT_MAX_DD_TABLES, mtr);
195
196 mlog_write_ull(dict_header + DICT_HDR_INDEX_ID, 0, mtr);
197
198 mlog_write_ulint(dict_header + DICT_HDR_MAX_SPACE_ID, 0, MLOG_4BYTES, mtr);
199
200 /* Obsolete, but we must initialize it anyway. */
201 mlog_write_ulint(dict_header + DICT_HDR_MIX_ID_LOW, 0, MLOG_4BYTES, mtr);
202
203 return true;
204 }
205
206 /** Initializes the data dictionary memory structures when the database is
207 started. This function is also called when the data dictionary is created.
208 @return DB_SUCCESS or error code. */
209 dberr_t dict_boot(void) {
210 dict_hdr_t *dict_hdr;
211 mtr_t mtr;
212 dberr_t err = DB_SUCCESS;
213
214 mtr_start(&mtr);
215
216 /* Create the hash tables etc. */
217 dict_init();
218
219 /* Get the dictionary header */
220 dict_hdr = dict_hdr_get(&mtr);
221
222 /* Because we only write new row ids to disk-based data structure
223 (dictionary header) when it is divisible by
224 DICT_HDR_ROW_ID_WRITE_MARGIN, in recovery we will not recover
225 the latest value of the row id counter. Therefore we advance
226 the counter at the database startup to avoid overlapping values.
227 Note that when a user after database startup first time asks for
228 a new row id, then because the counter is now divisible by
229 ..._MARGIN, it will immediately be updated to the disk-based
230 header. */
231
232 dict_sys->row_id =
233 DICT_HDR_ROW_ID_WRITE_MARGIN +
234 ut_uint64_align_up(mach_read_from_8(dict_hdr + DICT_HDR_ROW_ID),
235 DICT_HDR_ROW_ID_WRITE_MARGIN);
236
237 /* For upgrading, we need to load the old InnoDB internal SYS_*
238 tables. */
239 if (srv_is_upgrade_mode) {
240 dict_table_t *table;
241 dict_index_t *index;
242 mem_heap_t *heap;
243
244 /* Be sure these constants do not ever change. To avoid bloat,
245 only check the *NUM_FIELDS* in each table */
246 static_assert(DICT_NUM_COLS__SYS_TABLES == 8);
247 static_assert(DICT_NUM_FIELDS__SYS_TABLES == 10);
248 static_assert(DICT_NUM_FIELDS__SYS_TABLE_IDS == 2);
249 static_assert(DICT_NUM_COLS__SYS_COLUMNS == 7);
250 static_assert(DICT_NUM_FIELDS__SYS_COLUMNS == 9);
251 static_assert(DICT_NUM_COLS__SYS_INDEXES == 8);
252 static_assert(DICT_NUM_FIELDS__SYS_INDEXES == 10);
253 static_assert(DICT_NUM_COLS__SYS_FIELDS == 3);
254 static_assert(DICT_NUM_FIELDS__SYS_FIELDS == 5);
255 static_assert(DICT_NUM_COLS__SYS_FOREIGN == 4);
256 static_assert(DICT_NUM_FIELDS__SYS_FOREIGN == 6);
257 static_assert(DICT_NUM_FIELDS__SYS_FOREIGN_FOR_NAME == 2);
258 static_assert(DICT_NUM_COLS__SYS_FOREIGN_COLS == 4);
259 static_assert(DICT_NUM_FIELDS__SYS_FOREIGN_COLS == 6);
260
261 heap = mem_heap_create(450, UT_LOCATION_HERE);
262
263 /* Insert into the dictionary cache the descriptions of the basic
264 system tables */
265 table = dict_mem_table_create("SYS_TABLES", DICT_HDR_SPACE, 8, 0, 0, 0, 0);
266
267 dict_mem_table_add_col(table, heap, "NAME", DATA_BINARY, 0,
268 MAX_FULL_NAME_LEN, true);
269 dict_mem_table_add_col(table, heap, "ID", DATA_BINARY, 0, 8, true);
270 /* ROW_FORMAT = (N_COLS >> 31) ? COMPACT : REDUNDANT */
271 dict_mem_table_add_col(table, heap, "N_COLS", DATA_INT, 0, 4, true);
272 /* The low order bit of TYPE is always set to 1. If ROW_FORMAT
273 is not REDUNDANT or COMPACT, this field matches table->flags. */
274 dict_mem_table_add_col(table, heap, "TYPE", DATA_INT, 0, 4, true);
275 dict_mem_table_add_col(table, heap, "MIX_ID", DATA_BINARY, 0, 0, true);
276 /* MIX_LEN may contain additional table flags when
277 ROW_FORMAT!=REDUNDANT. Currently, these flags include
278 DICT_TF2_TEMPORARY. */
279 dict_mem_table_add_col(table, heap, "MIX_LEN", DATA_INT, 0, 4, true);
280 dict_mem_table_add_col(table, heap, "CLUSTER_NAME", DATA_BINARY, 0, 0,
281 true);
282 dict_mem_table_add_col(table, heap, "SPACE", DATA_INT, 0, 4, true);
283
284 table->id = DICT_TABLES_ID;
285
286 dict_table_add_system_columns(table, heap);
287 dict_sys_mutex_enter();
288 dict_table_add_to_cache(table, false);
289 dict_sys_mutex_exit();
290 dict_sys->sys_tables = table;
291 mem_heap_empty(heap);
292
293 index = dict_mem_index_create("SYS_TABLES", "CLUST_IND", DICT_HDR_SPACE,
294 DICT_UNIQUE | DICT_CLUSTERED, 1);
295
296 index->add_field("NAME", 0, true);
297
298 index->id = DICT_TABLES_ID;
299
300 err = dict_index_add_to_cache(
301 table, index,
302 mtr_read_ulint(dict_hdr + DICT_HDR_TABLES, MLOG_4BYTES, &mtr), false);
303 ut_a(err == DB_SUCCESS);
304
305 /*-------------------------*/
306 index = dict_mem_index_create("SYS_TABLES", "ID_IND", DICT_HDR_SPACE,
307 DICT_UNIQUE, 1);
308 index->add_field("ID", 0, true);
309
310 index->id = DICT_TABLE_IDS_ID;
311
312 err = dict_index_add_to_cache(
313 table, index,
314 mtr_read_ulint(dict_hdr + DICT_HDR_TABLE_IDS, MLOG_4BYTES, &mtr),
315 false);
316 ut_a(err == DB_SUCCESS);
317
318 /*-------------------------*/
319 table = dict_mem_table_create("SYS_COLUMNS", DICT_HDR_SPACE, 7, 0, 0, 0, 0);
320
321 dict_mem_table_add_col(table, heap, "TABLE_ID", DATA_BINARY, 0, 8, true);
322 dict_mem_table_add_col(table, heap, "POS", DATA_INT, 0, 4, true);
323 dict_mem_table_add_col(table, heap, "NAME", DATA_BINARY, 0, 0, true);
324 dict_mem_table_add_col(table, heap, "MTYPE", DATA_INT, 0, 4, true);
325 dict_mem_table_add_col(table, heap, "PRTYPE", DATA_INT, 0, 4, true);
326 dict_mem_table_add_col(table, heap, "LEN", DATA_INT, 0, 4, true);
327 dict_mem_table_add_col(table, heap, "PREC", DATA_INT, 0, 4, true);
328
329 table->id = DICT_COLUMNS_ID;
330
331 dict_table_add_system_columns(table, heap);
332 dict_sys_mutex_enter();
333 dict_table_add_to_cache(table, false);
334 dict_sys_mutex_exit();
335 dict_sys->sys_columns = table;
336 mem_heap_empty(heap);
337
338 index = dict_mem_index_create("SYS_COLUMNS", "CLUST_IND", DICT_HDR_SPACE,
339 DICT_UNIQUE | DICT_CLUSTERED, 2);
340
341 index->add_field("TABLE_ID", 0, true);
342 index->add_field("POS", 0, true);
343
344 index->id = DICT_COLUMNS_ID;
345
346 err = dict_index_add_to_cache(
347 table, index,
348 mtr_read_ulint(dict_hdr + DICT_HDR_COLUMNS, MLOG_4BYTES, &mtr), false);
349 ut_a(err == DB_SUCCESS);
350
351 /*-------------------------*/
352 table = dict_mem_table_create("SYS_INDEXES", DICT_HDR_SPACE,
353 DICT_NUM_COLS__SYS_INDEXES, 0, 0, 0, 0);
354
355 dict_mem_table_add_col(table, heap, "TABLE_ID", DATA_BINARY, 0, 8, true);
356 dict_mem_table_add_col(table, heap, "ID", DATA_BINARY, 0, 8, true);
357 dict_mem_table_add_col(table, heap, "NAME", DATA_BINARY, 0, 0, true);
358 dict_mem_table_add_col(table, heap, "N_FIELDS", DATA_INT, 0, 4, true);
359 dict_mem_table_add_col(table, heap, "TYPE", DATA_INT, 0, 4, true);
360 dict_mem_table_add_col(table, heap, "SPACE", DATA_INT, 0, 4, true);
361 dict_mem_table_add_col(table, heap, "PAGE_NO", DATA_INT, 0, 4, true);
362 dict_mem_table_add_col(table, heap, "MERGE_THRESHOLD", DATA_INT, 0, 4,
363 true);
364
365 table->id = DICT_INDEXES_ID;
366
367 dict_table_add_system_columns(table, heap);
368 dict_sys_mutex_enter();
369 dict_table_add_to_cache(table, false);
370 dict_sys_mutex_exit();
371 dict_sys->sys_indexes = table;
372 mem_heap_empty(heap);
373
374 index = dict_mem_index_create("SYS_INDEXES", "CLUST_IND", DICT_HDR_SPACE,
375 DICT_UNIQUE | DICT_CLUSTERED, 2);
376
377 index->add_field("TABLE_ID", 0, true);
378 index->add_field("ID", 0, true);
379
380 index->id = DICT_INDEXES_ID;
381
382 err = dict_index_add_to_cache(
383 table, index,
384 mtr_read_ulint(dict_hdr + DICT_HDR_INDEXES, MLOG_4BYTES, &mtr), false);
385 ut_a(err == DB_SUCCESS);
386
387 /*-------------------------*/
388 table = dict_mem_table_create("SYS_FIELDS", DICT_HDR_SPACE, 3, 0, 0, 0, 0);
389
390 dict_mem_table_add_col(table, heap, "INDEX_ID", DATA_BINARY, 0, 8, true);
391 dict_mem_table_add_col(table, heap, "POS", DATA_INT, 0, 4, true);
392 dict_mem_table_add_col(table, heap, "COL_NAME", DATA_BINARY, 0, 0, true);
393
394 table->id = DICT_FIELDS_ID;
395
396 dict_table_add_system_columns(table, heap);
397 dict_sys_mutex_enter();
398 dict_table_add_to_cache(table, false);
399 dict_sys_mutex_exit();
400 dict_sys->sys_fields = table;
401 mem_heap_free(heap);
402
403 index = dict_mem_index_create("SYS_FIELDS", "CLUST_IND", DICT_HDR_SPACE,
404 DICT_UNIQUE | DICT_CLUSTERED, 2);
405
406 index->add_field("INDEX_ID", 0, true);
407 index->add_field("POS", 0, true);
408
409 index->id = DICT_FIELDS_ID;
410
411 err = dict_index_add_to_cache(
412 table, index,
413 mtr_read_ulint(dict_hdr + DICT_HDR_FIELDS, MLOG_4BYTES, &mtr), false);
414 ut_a(err == DB_SUCCESS);
415
416 dict_sys_mutex_enter();
417 dict_load_sys_table(dict_sys->sys_tables);
418 dict_load_sys_table(dict_sys->sys_columns);
419 dict_load_sys_table(dict_sys->sys_indexes);
420 dict_load_sys_table(dict_sys->sys_fields);
421 dict_sys_mutex_exit();
422 }
423
424 mtr_commit(&mtr);
425
426 /*-------------------------*/
427
428 /* Initialize the insert buffer table, table buffer and indexes */
429
430 ibuf_init_at_db_start();
431
432 if (srv_force_recovery != SRV_FORCE_NO_LOG_REDO && srv_read_only_mode &&
433 !ibuf_is_empty()) {
434 ib::error(ER_IB_MSG_161) << "Change buffer must be empty when"
435 " --innodb-read-only is set!";
436
437 err = DB_ERROR;
438 }
439
440 return (err);
441 }
442
443 /** Creates and initializes the data dictionary at the server bootstrap.
444 @return DB_SUCCESS or error code. */
445 dberr_t dict_create(void) {
446 mtr_t mtr;
447
448 mtr_start(&mtr);
449
450 dict_hdr_create(&mtr);
451
452 mtr_commit(&mtr);
453
454 dberr_t err = dict_boot();
455
456 return (err);
457 }