| 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/dict0crea.cc |
| 29 |
Database object creation |
| 30 |
|
| 31 |
Created 1/8/1996 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#include "dict0crea.h" |
| 35 |
#include "btr0btr.h" |
| 36 |
#include "btr0pcur.h" |
| 37 |
#include "dict0boot.h" |
| 38 |
#include "dict0dd.h" |
| 39 |
#include "dict0dict.h" |
| 40 |
#include "dict0priv.h" |
| 41 |
#include "dict0stats.h" |
| 42 |
#include "fsp0space.h" |
| 43 |
#include "fsp0sysspace.h" |
| 44 |
#include "fts0priv.h" |
| 45 |
#include "ha_prototypes.h" |
| 46 |
#include "log0write.h" |
| 47 |
#include "mach0data.h" |
| 48 |
|
| 49 |
#include "my_dbug.h" |
| 50 |
|
| 51 |
#include "dict0upgrade.h" |
| 52 |
#include "page0page.h" |
| 53 |
#include "pars0pars.h" |
| 54 |
#include "que0que.h" |
| 55 |
#include "row0ins.h" |
| 56 |
#include "row0mysql.h" |
| 57 |
#include "srv0start.h" |
| 58 |
#include "trx0roll.h" |
| 59 |
#include "usr0sess.h" |
| 60 |
#include "ut0vec.h" |
| 61 |
|
| 62 |
dberr_t dict_build_table_def(dict_table_t *table, |
| 63 |
const HA_CREATE_INFO *create_info, trx_t *trx) { |
| 64 |
std::string db_name; |
| 65 |
std::string tbl_name; |
| 66 |
dict_name::get_table(table->name.m_name, db_name, tbl_name); |
| 67 |
|
| 68 |
bool is_dd_table = |
| 69 |
dd::get_dictionary()->is_dd_table_name(db_name.c_str(), tbl_name.c_str()); |
| 70 |
|
| 71 |
/** In-memory counter used for assigning table_id |
| 72 |
of data dictionary table. This counter is only used |
| 73 |
during bootstrap or upgrade */ |
| 74 |
static uint32_t dd_table_id = 1; |
| 75 |
|
| 76 |
if (is_dd_table) { |
| 77 |
table->id = dd_table_id++; |
| 78 |
table->is_dd_table = true; |
| 79 |
|
| 80 |
ut_ad(strcmp(tbl_name.c_str(), innodb_dd_table[table->id - 1].name) == 0); |
| 81 |
|
| 82 |
} else { |
| 83 |
dict_table_assign_new_id(table); |
| 84 |
} |
| 85 |
|
| 86 |
dberr_t err = dict_build_tablespace_for_table(table, create_info, trx); |
| 87 |
|
| 88 |
return (err); |
| 89 |
} |
| 90 |
|
| 91 |
/** Builds a tablespace to store various objects. |
| 92 |
@param[in,out] trx DD transaction |
| 93 |
@param[in,out] tablespace Tablespace object describing what to build. |
| 94 |
@return DB_SUCCESS or error code. */ |
| 95 |
dberr_t dict_build_tablespace(trx_t *trx, Tablespace *tablespace) { |
| 96 |
dberr_t err = DB_SUCCESS; |
| 97 |
mtr_t mtr; |
| 98 |
space_id_t space = 0; |
| 99 |
ut_d(static uint32_t crash_injection_after_create_counter = 1;); |
| 100 |
|
| 101 |
ut_ad(dict_sys_mutex_own()); |
| 102 |
ut_ad(tablespace); |
| 103 |
|
| 104 |
DBUG_EXECUTE_IF("out_of_tablespace_disk", return (DB_OUT_OF_FILE_SPACE);); |
| 105 |
/* Get a new space id. */ |
| 106 |
dict_hdr_get_new_id(nullptr, nullptr, &space, nullptr, false); |
| 107 |
if (space == SPACE_UNKNOWN) { |
| 108 |
return (DB_ERROR); |
| 109 |
} |
| 110 |
tablespace->set_space_id(space); |
| 111 |
|
| 112 |
Datafile *datafile = tablespace->first_datafile(); |
| 113 |
|
| 114 |
/* If file already exists we cannot write delete space to ddl log. */ |
| 115 |
os_file_type_t type; |
| 116 |
bool exists; |
| 117 |
if (os_file_status(datafile->filepath(), &exists, &type)) { |
| 118 |
if (exists) { |
| 119 |
return DB_TABLESPACE_EXISTS; |
| 120 |
} |
| 121 |
} else { |
| 122 |
return DB_IO_ERROR; |
| 123 |
} |
| 124 |
|
| 125 |
err = log_ddl->write_delete_space_log(trx, nullptr, space, |
| 126 |
datafile->filepath(), false, true); |
| 127 |
if (err != DB_SUCCESS) { |
| 128 |
return err; |
| 129 |
} |
| 130 |
|
| 131 |
/* We create a new generic empty tablespace. |
| 132 |
We initially let it be 4 pages: |
| 133 |
- page 0 is the fsp header and an extent descriptor page, |
| 134 |
- page 1 is an ibuf bitmap page, |
| 135 |
- page 2 is the first inode page, |
| 136 |
- page 3 will contain the root of the clustered index of the |
| 137 |
first table we create here. */ |
| 138 |
|
| 139 |
/* Set the initial size of the file being created. */ |
| 140 |
page_no_t size{}; |
| 141 |
|
| 142 |
size = tablespace->get_autoextend_size() > 0 |
| 143 |
? (tablespace->get_autoextend_size() / srv_page_size) |
| 144 |
: FIL_IBD_FILE_INITIAL_SIZE; |
| 145 |
|
| 146 |
err = fil_ibd_create(space, tablespace->name(), datafile->filepath(), |
| 147 |
tablespace->flags(), size); |
| 148 |
|
| 149 |
DBUG_INJECT_CRASH("ddl_crash_after_create_tablespace", |
| 150 |
crash_injection_after_create_counter++); |
| 151 |
|
| 152 |
if (err != DB_SUCCESS) { |
| 153 |
return (err); |
| 154 |
} |
| 155 |
|
| 156 |
DBUG_EXECUTE_IF("innodb_fail_to_update_tablespace_dict", |
| 157 |
return (DB_INTERRUPTED);); |
| 158 |
|
| 159 |
mtr_start(&mtr); |
| 160 |
|
| 161 |
/* Once we allow temporary general tablespaces, we must do this; |
| 162 |
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO); */ |
| 163 |
ut_a(!FSP_FLAGS_GET_TEMPORARY(tablespace->flags())); |
| 164 |
|
| 165 |
bool ret = fsp_header_init(space, size, &mtr); |
| 166 |
mtr_commit(&mtr); |
| 167 |
|
| 168 |
DBUG_EXECUTE_IF("fil_ibd_create_log", |
| 169 |
log_write_up_to(*log_sys, mtr.commit_lsn(), true); |
| 170 |
DBUG_SUICIDE();); |
| 171 |
|
| 172 |
if (!ret) { |
| 173 |
return (DB_ERROR); |
| 174 |
} |
| 175 |
|
| 176 |
return (err); |
| 177 |
} |
| 178 |
|
| 179 |
dberr_t dict_build_tablespace_for_table(dict_table_t *table, |
| 180 |
const HA_CREATE_INFO *create_info, |
| 181 |
trx_t *trx) { |
| 182 |
dberr_t err = DB_SUCCESS; |
| 183 |
ut_d(static uint32_t crash_injection_after_create_counter = 1;); |
| 184 |
|
| 185 |
ut_ad(!dict_sys_mutex_own()); |
| 186 |
|
| 187 |
const auto needs_file_per_table = |
| 188 |
DICT_TF2_FLAG_IS_SET(table, DICT_TF2_USE_FILE_PER_TABLE); |
| 189 |
|
| 190 |
if (needs_file_per_table) { |
| 191 |
/* Temporary table would always reside in the same |
| 192 |
shared temp tablespace. */ |
| 193 |
ut_ad(!table->is_temporary()); |
| 194 |
/* This table will need a new tablespace. */ |
| 195 |
|
| 196 |
ut_ad(DICT_TF_GET_ZIP_SSIZE(table->flags) == 0 || |
| 197 |
dict_table_has_atomic_blobs(table)); |
| 198 |
|
| 199 |
/* Get a new tablespace ID */ |
| 200 |
space_id_t space_id = 0; |
| 201 |
dict_hdr_get_new_id(nullptr, nullptr, &space_id, table, false); |
| 202 |
|
| 203 |
DBUG_EXECUTE_IF("ib_create_table_fail_out_of_space_ids", |
| 204 |
space_id = SPACE_UNKNOWN;); |
| 205 |
|
| 206 |
if (space_id == SPACE_UNKNOWN) { |
| 207 |
return (DB_ERROR); |
| 208 |
} |
| 209 |
table->space = space_id; |
| 210 |
|
| 211 |
/* Determine the tablespace flags. */ |
| 212 |
uint32_t fsp_flags = dict_tf_to_fsp_flags(table->flags); |
| 213 |
|
| 214 |
/* For file-per-table tablespace, set encryption flag */ |
| 215 |
if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_ENCRYPTION_FILE_PER_TABLE)) { |
| 216 |
fsp_flags_set_encryption(fsp_flags); |
| 217 |
} |
| 218 |
|
| 219 |
char *filepath; |
| 220 |
if (DICT_TF_HAS_DATA_DIR(table->flags)) { |
| 221 |
std::string path; |
| 222 |
|
| 223 |
path = dict_table_get_datadir(table); |
| 224 |
|
| 225 |
filepath = Fil_path::make(path, table->name.m_name, IBD, true); |
| 226 |
} else { |
| 227 |
/* Table resides in datadir */ |
| 228 |
filepath = Fil_path::make_ibd_from_table_name(table->name.m_name); |
| 229 |
} |
| 230 |
|
| 231 |
/* If file already exists we cannot write delete space to ddl log. */ |
| 232 |
os_file_type_t type; |
| 233 |
bool exists; |
| 234 |
if (os_file_status(filepath, &exists, &type)) { |
| 235 |
if (exists) { |
| 236 |
ut::free(filepath); |
| 237 |
return DB_TABLESPACE_EXISTS; |
| 238 |
} |
| 239 |
} else { |
| 240 |
ut::free(filepath); |
| 241 |
return DB_IO_ERROR; |
| 242 |
} |
| 243 |
|
| 244 |
err = log_ddl->write_delete_space_log(trx, table, space_id, filepath, false, |
| 245 |
false); |
| 246 |
if (err != DB_SUCCESS) { |
| 247 |
ut::free(filepath); |
| 248 |
return err; |
| 249 |
} |
| 250 |
|
| 251 |
/* We create a new single-table tablespace for the table. |
| 252 |
We initially let it be 4 pages: |
| 253 |
- page 0 is the fsp header and an extent descriptor page, |
| 254 |
- page 1 is an ibuf bitmap page, |
| 255 |
- page 2 is the first inode page, |
| 256 |
- page 3 will contain the root of the clustered index of |
| 257 |
the table we create here. */ |
| 258 |
|
| 259 |
std::string tablespace_name(table->name.m_name); |
| 260 |
dict_name::convert_to_space(tablespace_name); |
| 261 |
|
| 262 |
page_no_t size{}; |
| 263 |
size = create_info && create_info->m_implicit_tablespace_autoextend_size > 0 |
| 264 |
? (create_info->m_implicit_tablespace_autoextend_size / |
| 265 |
srv_page_size) |
| 266 |
: FIL_IBD_FILE_INITIAL_SIZE; |
| 267 |
err = fil_ibd_create(space_id, tablespace_name.c_str(), filepath, fsp_flags, |
| 268 |
size); |
| 269 |
|
| 270 |
ut::free(filepath); |
| 271 |
|
| 272 |
DBUG_INJECT_CRASH("ddl_crash_after_create_tablespace", |
| 273 |
crash_injection_after_create_counter++); |
| 274 |
|
| 275 |
if (err != DB_SUCCESS) { |
| 276 |
return (err); |
| 277 |
} |
| 278 |
|
| 279 |
mtr_t mtr; |
| 280 |
mtr_start(&mtr); |
| 281 |
|
| 282 |
bool ret = fsp_header_init(table->space, size, &mtr); |
| 283 |
|
| 284 |
if (ret) { |
| 285 |
fil_set_autoextend_size( |
| 286 |
table->space, |
| 287 |
(create_info ? create_info->m_implicit_tablespace_autoextend_size |
| 288 |
: 0)); |
| 289 |
} |
| 290 |
|
| 291 |
mtr_commit(&mtr); |
| 292 |
|
| 293 |
DBUG_EXECUTE_IF("fil_ibd_create_log", |
| 294 |
log_write_up_to(*log_sys, mtr.commit_lsn(), true); |
| 295 |
DBUG_SUICIDE();); |
| 296 |
|
| 297 |
if (!ret) { |
| 298 |
return (DB_ERROR); |
| 299 |
} |
| 300 |
|
| 301 |
err = btr_sdi_create_index(table->space, false); |
| 302 |
return (err); |
| 303 |
|
| 304 |
} else { |
| 305 |
/* We do not need to build a tablespace for this table. It |
| 306 |
is already built. Just find the correct tablespace ID. */ |
| 307 |
|
| 308 |
if (DICT_TF_HAS_SHARED_SPACE(table->flags)) { |
| 309 |
ut_ad(table->tablespace != nullptr); |
| 310 |
|
| 311 |
ut_ad(table->space == fil_space_get_id_by_name(table->tablespace())); |
| 312 |
} else if (table->is_temporary()) { |
| 313 |
/* Use the shared temporary tablespace. |
| 314 |
Note: The temp tablespace supports all non-Compressed |
| 315 |
row formats whereas the system tablespace only |
| 316 |
supports Redundant and Compact */ |
| 317 |
ut_ad(dict_tf_get_rec_format(table->flags) != REC_FORMAT_COMPRESSED); |
| 318 |
|
| 319 |
innodb_session_t *innodb_session = thd_to_innodb_session(trx->mysql_thd); |
| 320 |
ibt::Tablespace *tblsp = nullptr; |
| 321 |
|
| 322 |
bool is_slave_thd = thd_is_replication_slave_thread(trx->mysql_thd); |
| 323 |
if (is_slave_thd) { |
| 324 |
tblsp = ibt::get_rpl_slave_tblsp(); |
| 325 |
} else if (table->is_intrinsic()) { |
| 326 |
tblsp = innodb_session->get_instrinsic_temp_tblsp(); |
| 327 |
} else { |
| 328 |
tblsp = innodb_session->get_usr_temp_tblsp(); |
| 329 |
} |
| 330 |
|
| 331 |
/* Session temporary tablespace couldn't be allocated. This means, |
| 332 |
we have run out of disk space */ |
| 333 |
if (tblsp == nullptr) { |
| 334 |
return (DB_NO_SESSION_TEMP); |
| 335 |
} |
| 336 |
|
| 337 |
table->space = tblsp->space_id(); |
| 338 |
|
| 339 |
} else { |
| 340 |
/* Create in the system tablespace. */ |
| 341 |
ut_ad(table->space == TRX_SYS_SPACE); |
| 342 |
} |
| 343 |
|
| 344 |
DBUG_EXECUTE_IF("ib_ddl_crash_during_tablespace_alloc", DBUG_SUICIDE();); |
| 345 |
} |
| 346 |
|
| 347 |
return (DB_SUCCESS); |
| 348 |
} |
| 349 |
|
| 350 |
/** Builds an index definition */ |
| 351 |
void dict_build_index_def(const dict_table_t *table, /*!< in: table */ |
| 352 |
dict_index_t *index, /*!< in/out: index */ |
| 353 |
trx_t *trx) /*!< in/out: InnoDB transaction handle */ |
| 354 |
{ |
| 355 |
ut_ad(!dict_sys_mutex_own()); |
| 356 |
ut_ad((UT_LIST_GET_LEN(table->indexes) > 0) || index->is_clustered()); |
| 357 |
|
| 358 |
if (!table->is_intrinsic()) { |
| 359 |
if (srv_is_upgrade_mode) { |
| 360 |
index->id = dd_upgrade_indexes_num++; |
| 361 |
ut_ad(index->id <= dd_get_total_indexes_num()); |
| 362 |
} else { |
| 363 |
dict_hdr_get_new_id(nullptr, &index->id, nullptr, table, false); |
| 364 |
} |
| 365 |
|
| 366 |
} else { |
| 367 |
/* Index are re-loaded in process of creation using id. |
| 368 |
If same-id is used for all indexes only first index will always |
| 369 |
be retrieved when expected is iterative return of all indexes*/ |
| 370 |
if (UT_LIST_GET_LEN(table->indexes) > 0) { |
| 371 |
index->id = UT_LIST_GET_LAST(table->indexes)->id + 1; |
| 372 |
} else { |
| 373 |
index->id = 1; |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
/* Inherit the space id from the table; we store all indexes of a |
| 378 |
table in the same tablespace */ |
| 379 |
|
| 380 |
index->space = table->space; |
| 381 |
|
| 382 |
/* Note that the index was created by this transaction. */ |
| 383 |
index->trx_id = trx->id; |
| 384 |
} |
| 385 |
|
| 386 |
/** Creates an index tree for the index if it is not a member of a cluster. |
| 387 |
@param[in,out] index InnoDB index object |
| 388 |
@param[in,out] trx transaction |
| 389 |
@return DB_SUCCESS or DB_OUT_OF_FILE_SPACE */ |
| 390 |
dberr_t dict_create_index_tree_in_mem(dict_index_t *index, trx_t *trx) { |
| 391 |
mtr_t mtr; |
| 392 |
ulint page_no = FIL_NULL; |
| 393 |
|
| 394 |
ut_ad(!dict_sys_mutex_own()); |
| 395 |
|
| 396 |
DBUG_EXECUTE_IF("ib_dict_create_index_tree_fail", return (DB_OUT_OF_MEMORY);); |
| 397 |
|
| 398 |
if (index->type == DICT_FTS) { |
| 399 |
/* FTS index does not need an index tree */ |
| 400 |
return (DB_SUCCESS); |
| 401 |
} |
| 402 |
|
| 403 |
const bool missing = |
| 404 |
index->table->ibd_file_missing || dict_table_is_discarded(index->table); |
| 405 |
|
| 406 |
if (missing) { |
| 407 |
index->page = FIL_NULL; |
| 408 |
index->trx_id = trx->id; |
| 409 |
|
| 410 |
return (DB_SUCCESS); |
| 411 |
} |
| 412 |
|
| 413 |
mtr_start(&mtr); |
| 414 |
|
| 415 |
if (index->table->is_temporary()) { |
| 416 |
mtr_set_log_mode(&mtr, MTR_LOG_NO_REDO); |
| 417 |
} |
| 418 |
|
| 419 |
dberr_t err = DB_SUCCESS; |
| 420 |
|
| 421 |
page_no = btr_create(index->type, index->space, index->id, index, &mtr); |
| 422 |
|
| 423 |
index->page = page_no; |
| 424 |
index->trx_id = trx->id; |
| 425 |
|
| 426 |
mtr_commit(&mtr); |
| 427 |
if (page_no == FIL_NULL) { |
| 428 |
err = DB_OUT_OF_FILE_SPACE; |
| 429 |
} else { |
| 430 |
/* FIXME: Now writing ddl log after the index has been created, |
| 431 |
so if server crashes before the redo log gets persisted, |
| 432 |
there is no way to find the resources(two segments, etc.) |
| 433 |
allocated to this index. Since this is a rare case, living |
| 434 |
with it is acceptable */ |
| 435 |
/* FIXME: if it's part of CREATE TABLE, and file_per_table is |
| 436 |
true, skip ddl log, because during rollback, the whole |
| 437 |
tablespace would be dropped */ |
| 438 |
|
| 439 |
/* During upgrade, etc., the log_ddl may haven't been |
| 440 |
initialized and we don't need to write DDL logs too. |
| 441 |
This can only happen for CREATE TABLE. */ |
| 442 |
if (log_ddl != nullptr) { |
| 443 |
err = log_ddl->write_free_tree_log(trx, index, false); |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
return (err); |
| 448 |
} |
| 449 |
|
| 450 |
/** Drop an index tree belonging to a temporary table. |
| 451 |
@param[in] index index in a temporary table |
| 452 |
@param[in] root_page_no index root page number */ |
| 453 |
void dict_drop_temporary_table_index(const dict_index_t *index, |
| 454 |
page_no_t root_page_no) { |
| 455 |
ut_ad(dict_sys_mutex_own() || index->table->is_intrinsic()); |
| 456 |
ut_ad(index->table->is_temporary()); |
| 457 |
ut_ad(index->page == FIL_NULL); |
| 458 |
|
| 459 |
space_id_t space = index->space; |
| 460 |
bool found; |
| 461 |
const page_size_t page_size(fil_space_get_page_size(space, &found)); |
| 462 |
|
| 463 |
/* If tree has already been freed or it is a single table |
| 464 |
tablespace and the .ibd file is missing do nothing, |
| 465 |
else free the all the pages */ |
| 466 |
if (root_page_no != FIL_NULL && found) { |
| 467 |
btr_free(page_id_t(space, root_page_no), page_size); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
/** Check whether a column is in an index by the column name |
| 472 |
@param[in] col_name column name for the column to be checked |
| 473 |
@param[in] index the index to be searched |
| 474 |
@return true if this column is in the index, otherwise, false */ |
| 475 |
static bool dict_index_has_col_by_name(const char *col_name, |
| 476 |
const dict_index_t *index) { |
| 477 |
for (ulint i = 0; i < index->n_fields; i++) { |
| 478 |
dict_field_t *field = index->get_field(i); |
| 479 |
|
| 480 |
if (strcmp(field->name, col_name) == 0) { |
| 481 |
return (true); |
| 482 |
} |
| 483 |
} |
| 484 |
return (false); |
| 485 |
} |
| 486 |
|
| 487 |
/** Check whether the foreign constraint could be on a column that is |
| 488 |
part of a virtual index (index contains virtual column) in the table |
| 489 |
@param[in] fk_col_name FK column name to be checked |
| 490 |
@param[in] table the table |
| 491 |
@return true if this column is indexed with other virtual columns */ |
| 492 |
bool dict_foreign_has_col_in_v_index(const char *fk_col_name, |
| 493 |
const dict_table_t *table) { |
| 494 |
/* virtual column can't be Primary Key, so start with secondary index */ |
| 495 |
for (const dict_index_t *index = table->first_index()->next(); index; |
| 496 |
index = index->next()) { |
| 497 |
if (dict_index_has_virtual(index)) { |
| 498 |
if (dict_index_has_col_by_name(fk_col_name, index)) { |
| 499 |
return (true); |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
return (false); |
| 505 |
} |
| 506 |
|
| 507 |
/** Check whether the foreign constraint could be on a column that is |
| 508 |
a base column of some indexed virtual columns. |
| 509 |
@param[in] col_name column name for the column to be checked |
| 510 |
@param[in] table the table |
| 511 |
@return true if this column is a base column, otherwise, false */ |
| 512 |
bool dict_foreign_has_col_as_base_col(const char *col_name, |
| 513 |
const dict_table_t *table) { |
| 514 |
/* Loop through each virtual column and check if its base column has |
| 515 |
the same name as the column name being checked */ |
| 516 |
for (ulint i = 0; i < table->n_v_cols; i++) { |
| 517 |
dict_v_col_t *v_col = dict_table_get_nth_v_col(table, i); |
| 518 |
|
| 519 |
/* Only check if the virtual column is indexed */ |
| 520 |
if (!v_col->m_col.ord_part) { |
| 521 |
continue; |
| 522 |
} |
| 523 |
|
| 524 |
for (ulint j = 0; j < v_col->num_base; j++) { |
| 525 |
if (strcmp(col_name, table->get_col_name(v_col->base_col[j]->ind)) == 0) { |
| 526 |
return (true); |
| 527 |
} |
| 528 |
} |
| 529 |
} |
| 530 |
|
| 531 |
return (false); |
| 532 |
} |
| 533 |
|
| 534 |
/** Check if a foreign constraint is on the given column name. |
| 535 |
@param[in] col_name column name to be searched for fk constraint |
| 536 |
@param[in] table table to which foreign key constraint belongs |
| 537 |
@return true if fk constraint is present on the table, false otherwise. */ |
| 538 |
static bool dict_foreign_base_for_stored(const char *col_name, |
| 539 |
const dict_table_t *table) { |
| 540 |
/* Loop through each stored column and check if its base column has |
| 541 |
the same name as the column name being checked */ |
| 542 |
dict_s_col_list::const_iterator it; |
| 543 |
for (it = table->s_cols->begin(); it != table->s_cols->end(); ++it) { |
| 544 |
dict_s_col_t s_col = *it; |
| 545 |
|
| 546 |
for (ulint j = 0; j < s_col.num_base; j++) { |
| 547 |
/** If the stored column can refer to virtual column |
| 548 |
or stored column then it can points to NULL. */ |
| 549 |
|
| 550 |
if (s_col.base_col[j] == nullptr) { |
| 551 |
continue; |
| 552 |
} |
| 553 |
|
| 554 |
if (strcmp(col_name, table->get_col_name(s_col.base_col[j]->ind)) == 0) { |
| 555 |
return (true); |
| 556 |
} |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
return (false); |
| 561 |
} |
| 562 |
|
| 563 |
/** Check if a foreign constraint is on columns served as base columns |
| 564 |
of any stored column. This is to prevent creating SET NULL or CASCADE |
| 565 |
constraint on such columns |
| 566 |
@param[in] local_fk_set set of foreign key objects, to be added to |
| 567 |
the dictionary tables |
| 568 |
@param[in] table table to which the foreign key objects in |
| 569 |
local_fk_set belong to |
| 570 |
@return true if yes, otherwise, false */ |
| 571 |
bool dict_foreigns_has_s_base_col(const dict_foreign_set &local_fk_set, |
| 572 |
const dict_table_t *table) { |
| 573 |
dict_foreign_t *foreign; |
| 574 |
|
| 575 |
if (table->s_cols == nullptr) { |
| 576 |
return (false); |
| 577 |
} |
| 578 |
|
| 579 |
for (dict_foreign_set::const_iterator it = local_fk_set.begin(); |
| 580 |
it != local_fk_set.end(); ++it) { |
| 581 |
foreign = *it; |
| 582 |
ulint type = foreign->type; |
| 583 |
|
| 584 |
type &= |
| 585 |
~(DICT_FOREIGN_ON_DELETE_NO_ACTION | DICT_FOREIGN_ON_UPDATE_NO_ACTION); |
| 586 |
|
| 587 |
if (type == 0) { |
| 588 |
continue; |
| 589 |
} |
| 590 |
|
| 591 |
for (ulint i = 0; i < foreign->n_fields; i++) { |
| 592 |
/* Check if the constraint is on a column that |
| 593 |
is a base column of any stored column */ |
| 594 |
if (dict_foreign_base_for_stored(foreign->foreign_col_names[i], table)) { |
| 595 |
return (true); |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
return (false); |
| 601 |
} |
| 602 |
|
| 603 |
/** Check if a column is in foreign constraint with CASCADE properties or |
| 604 |
SET NULL |
| 605 |
@param[in] table table |
| 606 |
@param[in] col_name name for the column to be checked |
| 607 |
@return true if the column is in foreign constraint, otherwise, false */ |
| 608 |
bool dict_foreigns_has_this_col(const dict_table_t *table, |
| 609 |
const char *col_name) { |
| 610 |
dict_foreign_t *foreign; |
| 611 |
const dict_foreign_set *local_fk_set = &table->foreign_set; |
| 612 |
|
| 613 |
for (dict_foreign_set::const_iterator it = local_fk_set->begin(); |
| 614 |
it != local_fk_set->end(); ++it) { |
| 615 |
foreign = *it; |
| 616 |
ut_ad(foreign->id != nullptr); |
| 617 |
ulint type = foreign->type; |
| 618 |
|
| 619 |
type &= |
| 620 |
~(DICT_FOREIGN_ON_DELETE_NO_ACTION | DICT_FOREIGN_ON_UPDATE_NO_ACTION); |
| 621 |
|
| 622 |
if (type == 0) { |
| 623 |
continue; |
| 624 |
} |
| 625 |
|
| 626 |
for (ulint i = 0; i < foreign->n_fields; i++) { |
| 627 |
if (strcmp(foreign->foreign_col_names[i], col_name) == 0) { |
| 628 |
return (true); |
| 629 |
} |
| 630 |
} |
| 631 |
} |
| 632 |
return (false); |
| 633 |
} |
| 634 |
|
| 635 |
void dict_table_assign_new_id(dict_table_t *table) { |
| 636 |
if (table->is_intrinsic()) { |
| 637 |
/* There is no significance of this table->id (if table is |
| 638 |
intrinsic) so assign it default instead of something meaningful |
| 639 |
to avoid confusion.*/ |
| 640 |
table->id = ULINT_UNDEFINED; |
| 641 |
} else { |
| 642 |
dict_hdr_get_new_id(&table->id, nullptr, nullptr, table, false); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
/** Create in-memory tablespace dictionary index & table |
| 647 |
@param[in] space tablespace id |
| 648 |
@param[in] space_discarded true if space is discarded |
| 649 |
@param[in] in_flags space flags to use when space_discarded is true |
| 650 |
@param[in] is_create true when creating SDI index |
| 651 |
@return in-memory index structure for tablespace dictionary or NULL */ |
| 652 |
dict_index_t *dict_sdi_create_idx_in_mem(space_id_t space, bool space_discarded, |
| 653 |
uint32_t in_flags, bool is_create) { |
| 654 |
uint32_t flags = space_discarded ? in_flags : fil_space_get_flags(space); |
| 655 |
|
| 656 |
/* This means the tablespace is evicted from cache */ |
| 657 |
if (flags == UINT32_UNDEFINED) { |
| 658 |
return (nullptr); |
| 659 |
} |
| 660 |
|
| 661 |
ut_ad(fsp_flags_is_valid(flags)); |
| 662 |
|
| 663 |
dict_sys_mutex_exit(); |
| 664 |
|
| 665 |
rec_format_t rec_format; |
| 666 |
|
| 667 |
uint32_t zip_ssize = FSP_FLAGS_GET_ZIP_SSIZE(flags); |
| 668 |
ulint atomic_blobs = FSP_FLAGS_HAS_ATOMIC_BLOBS(flags); |
| 669 |
bool has_data_dir = FSP_FLAGS_HAS_DATA_DIR(flags); |
| 670 |
bool has_shared_space = FSP_FLAGS_GET_SHARED(flags); |
| 671 |
|
| 672 |
if (zip_ssize > 0) { |
| 673 |
rec_format = REC_FORMAT_COMPRESSED; |
| 674 |
} else if (atomic_blobs) { |
| 675 |
rec_format = REC_FORMAT_DYNAMIC; |
| 676 |
} else { |
| 677 |
rec_format = REC_FORMAT_COMPACT; |
| 678 |
} |
| 679 |
|
| 680 |
uint32_t table_flags = 0; |
| 681 |
dict_tf_set(&table_flags, rec_format, zip_ssize, has_data_dir, |
| 682 |
has_shared_space); |
| 683 |
|
| 684 |
/* 18 = strlen(SDI) + Max digits of 4 byte spaceid (10) + 1 */ |
| 685 |
char table_name[18]; |
| 686 |
mem_heap_t *heap = mem_heap_create(DICT_HEAP_SIZE, UT_LOCATION_HERE); |
| 687 |
snprintf(table_name, sizeof(table_name), "SDI_" SPACE_ID_PF, space); |
| 688 |
|
| 689 |
constexpr size_t n_cols_sdi = 5; |
| 690 |
dict_table_t *table = dict_mem_table_create(table_name, space, n_cols_sdi, 0, |
| 691 |
0, table_flags, 0); |
| 692 |
|
| 693 |
dict_mem_table_add_col(table, heap, "type", DATA_INT, |
| 694 |
DATA_NOT_NULL | DATA_UNSIGNED, 4, true); |
| 695 |
dict_mem_table_add_col(table, heap, "id", DATA_INT, |
| 696 |
DATA_NOT_NULL | DATA_UNSIGNED, 8, true); |
| 697 |
dict_mem_table_add_col(table, heap, "uncompressed_len", DATA_INT, |
| 698 |
DATA_NOT_NULL | DATA_UNSIGNED, 4, true); |
| 699 |
dict_mem_table_add_col(table, heap, "compressed_len", DATA_INT, |
| 700 |
DATA_NOT_NULL | DATA_UNSIGNED, 4, true); |
| 701 |
dict_mem_table_add_col(table, heap, "data", DATA_BLOB, DATA_NOT_NULL, 0, |
| 702 |
true); |
| 703 |
|
| 704 |
/* Initialize row version and column counts for new table */ |
| 705 |
table->current_row_version = 0; |
| 706 |
table->initial_col_count = n_cols_sdi; |
| 707 |
table->current_col_count = table->initial_col_count; |
| 708 |
table->total_col_count = table->initial_col_count; |
| 709 |
|
| 710 |
table->id = dict_sdi_get_table_id(space); |
| 711 |
|
| 712 |
/* Disable persistent statistics on the table */ |
| 713 |
dict_stats_set_persistent(table, false, true); |
| 714 |
|
| 715 |
dict_table_add_system_columns(table, heap); |
| 716 |
|
| 717 |
const char *index_name = "CLUST_IND_SDI"; |
| 718 |
|
| 719 |
dict_index_t *temp_index = |
| 720 |
dict_mem_index_create(table_name, index_name, space, |
| 721 |
DICT_CLUSTERED | DICT_UNIQUE | DICT_SDI, 2); |
| 722 |
ut_ad(temp_index); |
| 723 |
|
| 724 |
temp_index->add_field("type", 0, true); |
| 725 |
temp_index->add_field("id", 0, true); |
| 726 |
|
| 727 |
temp_index->table = table; |
| 728 |
|
| 729 |
/* Disable AHI on SDI tables */ |
| 730 |
temp_index->disable_ahi = true; |
| 731 |
|
| 732 |
page_no_t index_root_page_num; |
| 733 |
|
| 734 |
/* When we do DISCARD TABLESPACE, there will be no fil_space_t |
| 735 |
for the tablespace. In this case, we should not use fil_space_*() |
| 736 |
methods */ |
| 737 |
if (!space_discarded && !is_create) { |
| 738 |
mtr_t mtr; |
| 739 |
mtr.start(); |
| 740 |
|
| 741 |
index_root_page_num = |
| 742 |
fsp_sdi_get_root_page_num(space, page_size_t(flags), &mtr); |
| 743 |
|
| 744 |
mtr_commit(&mtr); |
| 745 |
|
| 746 |
} else { |
| 747 |
index_root_page_num = FIL_NULL; |
| 748 |
} |
| 749 |
|
| 750 |
temp_index->id = dict_sdi_get_index_id(); |
| 751 |
|
| 752 |
dberr_t error = |
| 753 |
dict_index_add_to_cache(table, temp_index, index_root_page_num, false); |
| 754 |
ut_a(error == DB_SUCCESS); |
| 755 |
|
| 756 |
dict_sys_mutex_enter(); |
| 757 |
|
| 758 |
/* After re-acquiring dict_sys mutex, check if there is already |
| 759 |
a table created by other threads. Just keep one copy in memory */ |
| 760 |
dict_table_t *exist = dict_table_check_if_in_cache_low(table->name.m_name); |
| 761 |
if (exist != nullptr) { |
| 762 |
dict_index_remove_from_cache(table, table->first_index()); |
| 763 |
dict_mem_table_free(table); |
| 764 |
table = exist; |
| 765 |
} else { |
| 766 |
dict_table_add_to_cache(table, true); |
| 767 |
} |
| 768 |
|
| 769 |
mem_heap_free(heap); |
| 770 |
return (table->first_index()); |
| 771 |
} |