| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2012, Facebook Inc. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 8 |
Free Software Foundation. |
| 9 |
|
| 10 |
This program is designed to work with certain software (including |
| 11 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 12 |
as designated in a particular file or component or in included license |
| 13 |
documentation. The authors of MySQL hereby grant you an additional |
| 14 |
permission to link the program and your derivative works with the |
| 15 |
separately licensed software that they have either included with |
| 16 |
the program or referenced in the documentation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 21 |
for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License along with |
| 24 |
this program; if not, write to the Free Software Foundation, Inc., |
| 25 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
|
| 27 |
*****************************************************************************/ |
| 28 |
|
| 29 |
/** @file dict/dict0mem.cc |
| 30 |
Data dictionary memory object creation |
| 31 |
|
| 32 |
Created 1/8/1996 Heikki Tuuri |
| 33 |
***********************************************************************/ |
| 34 |
|
| 35 |
#include <atomic> |
| 36 |
#ifndef UNIV_HOTBACKUP |
| 37 |
#include <mysql_com.h> |
| 38 |
|
| 39 |
#include "ha_prototypes.h" |
| 40 |
|
| 41 |
#include "data0type.h" |
| 42 |
#endif /* !UNIV_HOTBACKUP */ |
| 43 |
#include "dict0dd.h" |
| 44 |
#include "dict0dict.h" |
| 45 |
#include "dict0mem.h" |
| 46 |
#ifndef UNIV_HOTBACKUP |
| 47 |
#include "fts0priv.h" |
| 48 |
#include "mach0data.h" |
| 49 |
#include "my_dbug.h" |
| 50 |
#include "rem0rec.h" |
| 51 |
#include "ut0crc32.h" |
| 52 |
#endif /* !UNIV_HOTBACKUP */ |
| 53 |
|
| 54 |
#include <iostream> |
| 55 |
|
| 56 |
#include "sync0sync.h" |
| 57 |
|
| 58 |
/** An integer randomly initialized at startup used to make a temporary |
| 59 |
table name as unuique as possible. */ |
| 60 |
static std::atomic<uint32_t> dict_temp_file_num; |
| 61 |
|
| 62 |
/** Display an identifier. |
| 63 |
@param[in,out] s output stream |
| 64 |
@param[in] id_name SQL identifier (other than table name) |
| 65 |
@return the output stream */ |
| 66 |
std::ostream &operator<<(std::ostream &s, const id_name_t &id_name) { |
| 67 |
const char q = '`'; |
| 68 |
const char *c = id_name; |
| 69 |
s << q; |
| 70 |
for (; *c != 0; c++) { |
| 71 |
if (*c == q) { |
| 72 |
s << *c; |
| 73 |
} |
| 74 |
s << *c; |
| 75 |
} |
| 76 |
s << q; |
| 77 |
return (s); |
| 78 |
} |
| 79 |
|
| 80 |
/** Display a table name. |
| 81 |
@param[in,out] s output stream |
| 82 |
@param[in] table_name table name |
| 83 |
@return the output stream */ |
| 84 |
std::ostream &operator<<(std::ostream &s, const table_name_t &table_name) { |
| 85 |
#ifndef UNIV_HOTBACKUP |
| 86 |
return (s << ut_get_name(nullptr, table_name.m_name)); |
| 87 |
#else /* !UNIV_HOTBACKUP */ |
| 88 |
return (s << table_name.m_name); |
| 89 |
#endif /* !UNIV_HOTBACKUP */ |
| 90 |
} |
| 91 |
|
| 92 |
#ifndef UNIV_HOTBACKUP |
| 93 |
dict_v_col_t *dict_mem_table_add_v_col(dict_table_t *table, mem_heap_t *heap, |
| 94 |
const char *name, ulint mtype, |
| 95 |
ulint prtype, ulint len, ulint pos, |
| 96 |
ulint num_base, bool is_visible) { |
| 97 |
dict_v_col_t *v_col; |
| 98 |
ulint i; |
| 99 |
|
| 100 |
ut_ad(table); |
| 101 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 102 |
ut_ad(!heap == !name); |
| 103 |
|
| 104 |
ut_ad(prtype & DATA_VIRTUAL); |
| 105 |
|
| 106 |
i = table->n_v_def++; |
| 107 |
|
| 108 |
table->n_t_def++; |
| 109 |
|
| 110 |
if (name != nullptr) { |
| 111 |
if (table->n_v_def == table->n_v_cols) { |
| 112 |
heap = table->heap; |
| 113 |
} |
| 114 |
|
| 115 |
if (i && !table->v_col_names) { |
| 116 |
/* All preceding column names are empty. */ |
| 117 |
char *s = static_cast<char *>(mem_heap_zalloc(heap, table->n_v_def)); |
| 118 |
|
| 119 |
table->v_col_names = s; |
| 120 |
} |
| 121 |
|
| 122 |
table->v_col_names = dict_add_col_name(table->v_col_names, i, name, heap); |
| 123 |
} |
| 124 |
|
| 125 |
v_col = dict_table_get_nth_v_col(table, i); |
| 126 |
|
| 127 |
dict_mem_fill_column_struct(&v_col->m_col, pos, mtype, prtype, len, true, |
| 128 |
UINT32_UNDEFINED, 0, 0); |
| 129 |
v_col->v_pos = i; |
| 130 |
|
| 131 |
if (num_base != 0) { |
| 132 |
v_col->base_col = static_cast<dict_col_t **>( |
| 133 |
mem_heap_zalloc(table->heap, num_base * sizeof(*v_col->base_col))); |
| 134 |
} else { |
| 135 |
v_col->base_col = nullptr; |
| 136 |
} |
| 137 |
|
| 138 |
v_col->num_base = num_base; |
| 139 |
|
| 140 |
/* Initialize the index list for virtual columns */ |
| 141 |
v_col->v_indexes = ut::new_withkey<dict_v_idx_list>(UT_NEW_THIS_FILE_PSI_KEY); |
| 142 |
|
| 143 |
v_col->m_col.is_visible = is_visible; |
| 144 |
return (v_col); |
| 145 |
} |
| 146 |
|
| 147 |
/** Adds a stored column definition to a table. |
| 148 |
@param[in,out] table table |
| 149 |
@param[in] num_base number of base columns. */ |
| 150 |
void dict_mem_table_add_s_col(dict_table_t *table, ulint num_base) { |
| 151 |
ulint i = table->n_def - 1; |
| 152 |
dict_col_t *col = table->get_col(i); |
| 153 |
dict_s_col_t s_col; |
| 154 |
|
| 155 |
ut_ad(col != nullptr); |
| 156 |
|
| 157 |
if (table->s_cols == nullptr) { |
| 158 |
table->s_cols = ut::new_withkey<dict_s_col_list>(UT_NEW_THIS_FILE_PSI_KEY); |
| 159 |
} |
| 160 |
|
| 161 |
s_col.m_col = col; |
| 162 |
s_col.s_pos = i + table->n_v_def; |
| 163 |
|
| 164 |
if (num_base != 0) { |
| 165 |
s_col.base_col = static_cast<dict_col_t **>( |
| 166 |
mem_heap_zalloc(table->heap, num_base * sizeof(dict_col_t *))); |
| 167 |
} else { |
| 168 |
s_col.base_col = nullptr; |
| 169 |
} |
| 170 |
|
| 171 |
s_col.num_base = num_base; |
| 172 |
table->s_cols->push_back(s_col); |
| 173 |
} |
| 174 |
|
| 175 |
/** Renames a column of a table in the data dictionary cache. */ |
| 176 |
static void dict_mem_table_col_rename_low( |
| 177 |
dict_table_t *table, /*!< in/out: table */ |
| 178 |
unsigned i, /*!< in: column offset corresponding to s */ |
| 179 |
const char *to, /*!< in: new column name */ |
| 180 |
const char *s, /*!< in: pointer to table->col_names */ |
| 181 |
bool is_virtual) |
| 182 |
/*!< in: if this is a virtual column */ |
| 183 |
{ |
| 184 |
char *t_col_names = |
| 185 |
const_cast<char *>(is_virtual ? table->v_col_names : table->col_names); |
| 186 |
ulint n_col = is_virtual ? table->n_v_def : table->n_def; |
| 187 |
|
| 188 |
size_t from_len = strlen(s), to_len = strlen(to); |
| 189 |
|
| 190 |
ut_ad(i < table->n_def || is_virtual); |
| 191 |
ut_ad(i < table->n_v_def || !is_virtual); |
| 192 |
|
| 193 |
ut_ad(from_len <= NAME_LEN); |
| 194 |
ut_ad(to_len <= NAME_LEN); |
| 195 |
|
| 196 |
if (from_len == to_len) { |
| 197 |
/* The easy case: simply replace the column name in |
| 198 |
table->col_names. */ |
| 199 |
strcpy(const_cast<char *>(s), to); |
| 200 |
} else { |
| 201 |
/* We need to adjust all affected index->field |
| 202 |
pointers, as in dict_index_add_col(). First, copy |
| 203 |
table->col_names. */ |
| 204 |
ulint prefix_len = s - t_col_names; |
| 205 |
|
| 206 |
for (; i < n_col; i++) { |
| 207 |
s += strlen(s) + 1; |
| 208 |
} |
| 209 |
|
| 210 |
ulint full_len = s - t_col_names; |
| 211 |
char *col_names; |
| 212 |
|
| 213 |
if (to_len > from_len) { |
| 214 |
ulint table_size_before_rename_col = mem_heap_get_size(table->heap); |
| 215 |
col_names = static_cast<char *>( |
| 216 |
mem_heap_alloc(table->heap, full_len + to_len - from_len)); |
| 217 |
ulint table_size_after_rename_col = mem_heap_get_size(table->heap); |
| 218 |
if (table_size_before_rename_col != table_size_after_rename_col) { |
| 219 |
dict_sys->size += |
| 220 |
table_size_after_rename_col - table_size_before_rename_col; |
| 221 |
} |
| 222 |
memcpy(col_names, t_col_names, prefix_len); |
| 223 |
} else { |
| 224 |
col_names = const_cast<char *>(t_col_names); |
| 225 |
} |
| 226 |
|
| 227 |
memcpy(col_names + prefix_len, to, to_len); |
| 228 |
memmove(col_names + prefix_len + to_len, |
| 229 |
t_col_names + (prefix_len + from_len), |
| 230 |
full_len - (prefix_len + from_len)); |
| 231 |
|
| 232 |
/* Replace the field names in every index. */ |
| 233 |
for (dict_index_t *index = table->first_index(); index != nullptr; |
| 234 |
index = index->next()) { |
| 235 |
ulint n_fields = dict_index_get_n_fields(index); |
| 236 |
|
| 237 |
for (ulint i = 0; i < n_fields; i++) { |
| 238 |
dict_field_t *field = index->get_field(i); |
| 239 |
|
| 240 |
/* if is_virtual and that in field->col does |
| 241 |
not match, continue */ |
| 242 |
if ((!is_virtual) != (!field->col->is_virtual())) { |
| 243 |
continue; |
| 244 |
} |
| 245 |
|
| 246 |
ulint name_ofs = field->name - t_col_names; |
| 247 |
if (name_ofs <= prefix_len) { |
| 248 |
field->name = col_names + name_ofs; |
| 249 |
} else { |
| 250 |
ut_a(name_ofs < full_len); |
| 251 |
field->name = col_names + name_ofs + to_len - from_len; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if (is_virtual) { |
| 257 |
table->v_col_names = col_names; |
| 258 |
} else { |
| 259 |
table->col_names = col_names; |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
/* Virtual columns are not allowed for foreign key */ |
| 264 |
if (is_virtual) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
dict_foreign_t *foreign; |
| 269 |
|
| 270 |
/* Replace the field names in every foreign key constraint. */ |
| 271 |
for (dict_foreign_set::iterator it = table->foreign_set.begin(); |
| 272 |
it != table->foreign_set.end(); ++it) { |
| 273 |
foreign = *it; |
| 274 |
|
| 275 |
for (unsigned f = 0; f < foreign->n_fields; f++) { |
| 276 |
/* These can point straight to |
| 277 |
table->col_names, because the foreign key |
| 278 |
constraints will be freed at the same time |
| 279 |
when the table object is freed. */ |
| 280 |
foreign->foreign_col_names[f] = |
| 281 |
foreign->foreign_index->get_field(f)->name; |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
for (dict_foreign_set::iterator it = table->referenced_set.begin(); |
| 286 |
it != table->referenced_set.end(); ++it) { |
| 287 |
foreign = *it; |
| 288 |
|
| 289 |
for (unsigned f = 0; f < foreign->n_fields; f++) { |
| 290 |
/* foreign->referenced_col_names[] need to be |
| 291 |
copies, because the constraint may become |
| 292 |
orphan when foreign_key_checks=0 and the |
| 293 |
parent table is dropped. */ |
| 294 |
|
| 295 |
const char *col_name = foreign->referenced_index->get_field(f)->name; |
| 296 |
|
| 297 |
if (strcmp(foreign->referenced_col_names[f], col_name)) { |
| 298 |
char **rc = const_cast<char **>(foreign->referenced_col_names + f); |
| 299 |
size_t col_name_len_1 = strlen(col_name) + 1; |
| 300 |
|
| 301 |
if (col_name_len_1 <= strlen(*rc) + 1) { |
| 302 |
memcpy(*rc, col_name, col_name_len_1); |
| 303 |
} else { |
| 304 |
*rc = static_cast<char *>( |
| 305 |
mem_heap_dup(foreign->heap, col_name, col_name_len_1)); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
/** Renames a column of a table in the data dictionary cache. |
| 313 |
@param[in,out] table Table |
| 314 |
@param[in] nth_col Column index |
| 315 |
@param[in] from Old column name |
| 316 |
@param[in] to New column name |
| 317 |
@param[in] is_virtual If this is a virtual column */ |
| 318 |
void dict_mem_table_col_rename(dict_table_t *table, ulint nth_col, |
| 319 |
const char *from, const char *to, |
| 320 |
bool is_virtual) { |
| 321 |
const char *s = is_virtual ? table->v_col_names : table->col_names; |
| 322 |
|
| 323 |
ut_ad((!is_virtual && nth_col < table->n_def) || |
| 324 |
(is_virtual && nth_col < table->n_v_def)); |
| 325 |
|
| 326 |
for (ulint i = 0; i < nth_col; i++) { |
| 327 |
size_t len = strlen(s); |
| 328 |
ut_ad(len > 0); |
| 329 |
s += len + 1; |
| 330 |
} |
| 331 |
|
| 332 |
/* This could fail if the data dictionaries are out of sync. |
| 333 |
Proceed with the renaming anyway. */ |
| 334 |
ut_ad(!strcmp(from, s)); |
| 335 |
|
| 336 |
dict_mem_table_col_rename_low(table, static_cast<unsigned>(nth_col), to, s, |
| 337 |
is_virtual); |
| 338 |
} |
| 339 |
|
| 340 |
/** Creates and initializes a foreign constraint memory object. |
| 341 |
@return own: foreign constraint struct */ |
| 342 |
dict_foreign_t *dict_mem_foreign_create(void) { |
| 343 |
dict_foreign_t *foreign; |
| 344 |
mem_heap_t *heap; |
| 345 |
DBUG_TRACE; |
| 346 |
|
| 347 |
heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 348 |
|
| 349 |
foreign = static_cast<dict_foreign_t *>( |
| 350 |
mem_heap_zalloc(heap, sizeof(dict_foreign_t))); |
| 351 |
|
| 352 |
foreign->heap = heap; |
| 353 |
|
| 354 |
foreign->v_cols = nullptr; |
| 355 |
|
| 356 |
DBUG_PRINT("dict_mem_foreign_create", ("heap: %p", heap)); |
| 357 |
|
| 358 |
return foreign; |
| 359 |
} |
| 360 |
|
| 361 |
/** Sets the foreign_table_name_lookup pointer based on the value of |
| 362 |
lower_case_table_names. If that is 0 or 1, foreign_table_name_lookup |
| 363 |
will point to foreign_table_name. If 2, then another string is |
| 364 |
allocated from foreign->heap and set to lower case. */ |
| 365 |
void dict_mem_foreign_table_name_lookup_set( |
| 366 |
dict_foreign_t *foreign, /*!< in/out: foreign struct */ |
| 367 |
bool do_alloc) /*!< in: is an alloc needed */ |
| 368 |
{ |
| 369 |
if (innobase_get_lower_case_table_names() == 2) { |
| 370 |
if (do_alloc) { |
| 371 |
ulint len; |
| 372 |
|
| 373 |
len = strlen(foreign->foreign_table_name) + 1; |
| 374 |
|
| 375 |
foreign->foreign_table_name_lookup = |
| 376 |
static_cast<char *>(mem_heap_alloc(foreign->heap, len)); |
| 377 |
} |
| 378 |
strcpy(foreign->foreign_table_name_lookup, foreign->foreign_table_name); |
| 379 |
innobase_casedn_str(foreign->foreign_table_name_lookup); |
| 380 |
} else { |
| 381 |
foreign->foreign_table_name_lookup = foreign->foreign_table_name; |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
/** Sets the referenced_table_name_lookup pointer based on the value of |
| 386 |
lower_case_table_names. If that is 0 or 1, referenced_table_name_lookup |
| 387 |
will point to referenced_table_name. If 2, then another string is |
| 388 |
allocated from foreign->heap and set to lower case. */ |
| 389 |
void dict_mem_referenced_table_name_lookup_set( |
| 390 |
dict_foreign_t *foreign, /*!< in/out: foreign struct */ |
| 391 |
bool do_alloc) /*!< in: is an alloc needed */ |
| 392 |
{ |
| 393 |
if (innobase_get_lower_case_table_names() == 2) { |
| 394 |
if (do_alloc) { |
| 395 |
ulint len; |
| 396 |
|
| 397 |
len = strlen(foreign->referenced_table_name) + 1; |
| 398 |
|
| 399 |
foreign->referenced_table_name_lookup = |
| 400 |
static_cast<char *>(mem_heap_alloc(foreign->heap, len)); |
| 401 |
} |
| 402 |
strcpy(foreign->referenced_table_name_lookup, |
| 403 |
foreign->referenced_table_name); |
| 404 |
innobase_casedn_str(foreign->referenced_table_name_lookup); |
| 405 |
} else { |
| 406 |
foreign->referenced_table_name_lookup = foreign->referenced_table_name; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
/** Fill the virtual column set with virtual column information |
| 411 |
present in the given virtual index. |
| 412 |
@param[in] index virtual index |
| 413 |
@param[out] v_cols virtual column set. */ |
| 414 |
static void dict_mem_fill_vcol_has_index(const dict_index_t *index, |
| 415 |
dict_vcol_set **v_cols) { |
| 416 |
for (ulint i = 0; i < index->table->n_v_cols; i++) { |
| 417 |
dict_v_col_t *v_col = dict_table_get_nth_v_col(index->table, i); |
| 418 |
if (!v_col->m_col.ord_part) { |
| 419 |
continue; |
| 420 |
} |
| 421 |
|
| 422 |
for (auto it = v_col->v_indexes->begin(); it != v_col->v_indexes->end(); |
| 423 |
++it) { |
| 424 |
dict_v_idx_t v_idx = *it; |
| 425 |
|
| 426 |
if (v_idx.index == index) { |
| 427 |
if (*v_cols == nullptr) { |
| 428 |
*v_cols = ut::new_withkey<dict_vcol_set>(UT_NEW_THIS_FILE_PSI_KEY); |
| 429 |
} |
| 430 |
|
| 431 |
(*v_cols)->insert(v_col); |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
/** Fill the virtual column set with virtual column of the index |
| 438 |
if the index contains the given column name. |
| 439 |
@param[in] col_name column name |
| 440 |
@param[in] table innodb table object |
| 441 |
@param[out] v_cols set of virtual column information. */ |
| 442 |
static void dict_mem_fill_vcol_from_v_indexes(const char *col_name, |
| 443 |
const dict_table_t *table, |
| 444 |
dict_vcol_set **v_cols) { |
| 445 |
/* virtual column can't be Primary Key, so start with secondary index */ |
| 446 |
for (const dict_index_t *index = table->first_index()->next(); |
| 447 |
index != nullptr; index = index->next()) { |
| 448 |
/* Skip if the index have newly added |
| 449 |
virtual column because field name is NULL. |
| 450 |
Later virtual column set will be |
| 451 |
refreshed during loading of table. */ |
| 452 |
if (!dict_index_has_virtual(index) || index->has_new_v_col) { |
| 453 |
continue; |
| 454 |
} |
| 455 |
|
| 456 |
for (ulint i = 0; i < index->n_fields; i++) { |
| 457 |
dict_field_t *field = index->get_field(i); |
| 458 |
|
| 459 |
if (strcmp(field->name, col_name) == 0) { |
| 460 |
dict_mem_fill_vcol_has_index(index, v_cols); |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
/** Fill the virtual column set with virtual columns which have base columns |
| 467 |
as the given col_name |
| 468 |
@param[in] col_name column name |
| 469 |
@param[in] table table object |
| 470 |
@param[out] v_cols set of virtual columns. */ |
| 471 |
static void dict_mem_fill_vcol_set_for_base_col(const char *col_name, |
| 472 |
const dict_table_t *table, |
| 473 |
dict_vcol_set **v_cols) { |
| 474 |
for (ulint i = 0; i < table->n_v_cols; i++) { |
| 475 |
dict_v_col_t *v_col = dict_table_get_nth_v_col(table, i); |
| 476 |
|
| 477 |
if (!v_col->m_col.ord_part) { |
| 478 |
continue; |
| 479 |
} |
| 480 |
|
| 481 |
for (ulint j = 0; j < v_col->num_base; j++) { |
| 482 |
if (strcmp(col_name, table->get_col_name(v_col->base_col[j]->ind)) == 0) { |
| 483 |
if (*v_cols == nullptr) { |
| 484 |
*v_cols = ut::new_withkey<dict_vcol_set>(UT_NEW_THIS_FILE_PSI_KEY); |
| 485 |
} |
| 486 |
|
| 487 |
(*v_cols)->insert(v_col); |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
} |
| 492 |
|
| 493 |
/** Fills the dependent virtual columns in a set. |
| 494 |
Reason for being dependent are |
| 495 |
1) FK can be present on base column of virtual columns |
| 496 |
2) FK can be present on column which is a part of virtual index |
| 497 |
@param[in,out] foreign foreign key information. */ |
| 498 |
void dict_mem_foreign_fill_vcol_set(dict_foreign_t *foreign) { |
| 499 |
ulint type = foreign->type; |
| 500 |
|
| 501 |
if (type == 0) { |
| 502 |
return; |
| 503 |
} |
| 504 |
|
| 505 |
for (ulint i = 0; i < foreign->n_fields; i++) { |
| 506 |
/** FK can be present on base columns |
| 507 |
of virtual columns. */ |
| 508 |
dict_mem_fill_vcol_set_for_base_col(foreign->foreign_col_names[i], |
| 509 |
foreign->foreign_table, |
| 510 |
&foreign->v_cols); |
| 511 |
|
| 512 |
/** FK can be present on the columns |
| 513 |
which can be a part of virtual index. */ |
| 514 |
dict_mem_fill_vcol_from_v_indexes(foreign->foreign_col_names[i], |
| 515 |
foreign->foreign_table, &foreign->v_cols); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
/** Fill virtual columns set in each fk constraint present in the table. |
| 520 |
@param[in,out] table innodb table object. */ |
| 521 |
void dict_mem_table_fill_foreign_vcol_set(dict_table_t *table) { |
| 522 |
dict_foreign_set fk_set = table->foreign_set; |
| 523 |
dict_foreign_t *foreign; |
| 524 |
|
| 525 |
for (auto it = fk_set.begin(); it != fk_set.end(); ++it) { |
| 526 |
foreign = *it; |
| 527 |
|
| 528 |
dict_mem_foreign_fill_vcol_set(foreign); |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
/** Free the vcol_set from all foreign key constraint on the table. |
| 533 |
@param[in,out] table innodb table object. */ |
| 534 |
void dict_mem_table_free_foreign_vcol_set(dict_table_t *table) { |
| 535 |
dict_foreign_set fk_set = table->foreign_set; |
| 536 |
dict_foreign_t *foreign; |
| 537 |
|
| 538 |
for (auto it = fk_set.begin(); it != fk_set.end(); ++it) { |
| 539 |
foreign = *it; |
| 540 |
|
| 541 |
if (foreign->v_cols != nullptr) { |
| 542 |
ut::delete_(foreign->v_cols); |
| 543 |
foreign->v_cols = nullptr; |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/** Set default value |
| 549 |
@param[in] value Default value |
| 550 |
@param[in] length Default value length |
| 551 |
@param[in,out] heap Heap to allocate memory */ |
| 552 |
void dict_col_t::set_default(const byte *value, size_t length, |
| 553 |
mem_heap_t *heap) { |
| 554 |
ut_ad(instant_default == nullptr); |
| 555 |
ut_ad(length == 0 || length == UNIV_SQL_NULL || value != nullptr); |
| 556 |
|
| 557 |
instant_default = static_cast<dict_col_default_t *>( |
| 558 |
mem_heap_alloc(heap, sizeof(dict_col_default_t))); |
| 559 |
|
| 560 |
instant_default->col = this; |
| 561 |
|
| 562 |
if (length != UNIV_SQL_NULL) { |
| 563 |
const char *val = |
| 564 |
(value == nullptr ? "\0" : reinterpret_cast<const char *>(value)); |
| 565 |
|
| 566 |
instant_default->value = |
| 567 |
reinterpret_cast<byte *>(mem_heap_strdupl(heap, val, length)); |
| 568 |
} else { |
| 569 |
ut_ad(!(prtype & DATA_NOT_NULL)); |
| 570 |
instant_default->value = nullptr; |
| 571 |
} |
| 572 |
|
| 573 |
instant_default->len = length; |
| 574 |
} |
| 575 |
|
| 576 |
bool dict_col_default_t::operator==(const dict_col_default_t &other) { |
| 577 |
/* If the lengths are different, trivially the default values are not |
| 578 |
the same, return false immediately */ |
| 579 |
if (len != other.len) { |
| 580 |
return false; |
| 581 |
} |
| 582 |
/* If the lengths are null or 0, the values are empty and equal. |
| 583 |
No need to check both lengths since we only reach this point |
| 584 |
if len == other.len */ |
| 585 |
if (len == UNIV_SQL_NULL || len == 0) { |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
auto length = len; |
| 590 |
while (length-- > 0) { |
| 591 |
if (value[length] != other.value[length]) { |
| 592 |
return false; |
| 593 |
} |
| 594 |
} |
| 595 |
return true; |
| 596 |
} |
| 597 |
|
| 598 |
bool dict_col_default_t::operator!=(const dict_col_default_t &other) { |
| 599 |
return !(*this == other); |
| 600 |
} |
| 601 |
|
| 602 |
/** Check whether index can be used by transaction |
| 603 |
@param[in] trx transaction*/ |
| 604 |
bool dict_index_t::is_usable(const trx_t *trx) const { |
| 605 |
/* Indexes that are being created are not usable. */ |
| 606 |
if (!is_clustered() && dict_index_is_online_ddl(this)) { |
| 607 |
return false; |
| 608 |
} |
| 609 |
|
| 610 |
/* Cannot use a corrupted index. */ |
| 611 |
if (is_corrupted()) { |
| 612 |
return false; |
| 613 |
} |
| 614 |
|
| 615 |
/* Check if the specified transaction can see this index. */ |
| 616 |
return (table->is_temporary() || trx_id == 0 || |
| 617 |
!MVCC::is_view_active(trx->read_view) || |
| 618 |
trx->read_view->changes_visible(trx_id, table->name)); |
| 619 |
} |
| 620 |
#endif /* !UNIV_HOTBACKUP */ |
| 621 |
|
| 622 |
void dict_index_t::create_nullables(uint32_t current_row_version) { |
| 623 |
ut_ad(is_clustered()); |
| 624 |
ut_ad(is_valid_row_version(current_row_version)); |
| 625 |
|
| 626 |
memset(nullables, 0, (MAX_ROW_VERSION + 1) * sizeof(nullables[0])); |
| 627 |
|
| 628 |
auto update_nullable = [&](size_t start_version, bool is_increment) { |
| 629 |
ut_ad(is_valid_row_version(start_version)); |
| 630 |
for (size_t i = start_version; i <= current_row_version; i++) { |
| 631 |
ut_ad(is_increment || nullables[i] > 0); |
| 632 |
|
| 633 |
if (is_increment) { |
| 634 |
++nullables[i]; |
| 635 |
} else { |
| 636 |
--nullables[i]; |
| 637 |
} |
| 638 |
} |
| 639 |
}; |
| 640 |
|
| 641 |
for (uint32_t i = 0; i < n_def; i++) { |
| 642 |
dict_field_t *field = get_field(i); |
| 643 |
|
| 644 |
/* In case of redo recovery, names are not populated */ |
| 645 |
ut_ad(field->name != nullptr || |
| 646 |
strcmp(name, RECOVERY_INDEX_TABLE_NAME) == 0); |
| 647 |
|
| 648 |
if (field->name != nullptr && (strcmp(field->name, "DB_ROW_ID") == 0 || |
| 649 |
strcmp(field->name, "DB_TRX_ID") == 0 || |
| 650 |
strcmp(field->name, "DB_ROLL_PTR") == 0)) { |
| 651 |
continue; |
| 652 |
} |
| 653 |
|
| 654 |
if (field->col->prtype & DATA_NOT_NULL) { |
| 655 |
continue; |
| 656 |
} |
| 657 |
|
| 658 |
/* For each version increment by 1 starting from field->col->v_added */ |
| 659 |
size_t start_from = 0; |
| 660 |
if (field->col->is_instant_added()) { |
| 661 |
start_from = field->col->get_version_added(); |
| 662 |
} |
| 663 |
update_nullable(start_from, true); |
| 664 |
|
| 665 |
/* For each version decrement by 1 starting from field->col->v_dropped */ |
| 666 |
if (field->col->is_instant_dropped()) { |
| 667 |
update_nullable(field->col->get_version_dropped(), false); |
| 668 |
} |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
bool dict_index_t::is_tuple_instant_format( |
| 673 |
const uint16_t n_fields_in_tuple) const { |
| 674 |
if (!has_instant_cols_or_row_versions()) { |
| 675 |
return false; |
| 676 |
} |
| 677 |
|
| 678 |
ut_ad(n_fields_in_tuple <= n_total_fields); |
| 679 |
|
| 680 |
/* In versioned rows, always materialize INSTANT cols even in from ROLLBACK */ |
| 681 |
if (has_row_versions()) { |
| 682 |
return true; |
| 683 |
} |
| 684 |
|
| 685 |
/* For instant index, if the tuple comes from UPDATE, its fields could be less |
| 686 |
than index definition. Because, we restore the table row as it was before |
| 687 |
UPDATE (i.e. we get rid of INSTANT ADD columns which weren't part of row) */ |
| 688 |
if (n_fields_in_tuple < n_fields) { |
| 689 |
/* If PK is not specified, DB_ROW_ID will be part of tuple */ |
| 690 |
uint16_t sys_fields_in_tuple = 0; |
| 691 |
if (innobase_strcasecmp(name, innobase_index_reserve_name) == 0) { |
| 692 |
sys_fields_in_tuple = table->get_n_sys_cols(); |
| 693 |
} else { |
| 694 |
sys_fields_in_tuple = table->get_n_sys_cols() - 1; |
| 695 |
} |
| 696 |
|
| 697 |
uint16_t fields_in_tuple = n_fields_in_tuple - sys_fields_in_tuple; |
| 698 |
if (fields_in_tuple == table->get_instant_cols()) { |
| 699 |
return false; |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
return true; |
| 704 |
} |
| 705 |
|
| 706 |
/** Gets the column number the nth field in an index. |
| 707 |
@param[in] pos position of the field |
| 708 |
@return column number */ |
| 709 |
ulint dict_index_t::get_col_no(ulint pos) const { |
| 710 |
return (dict_col_get_no(get_col(pos))); |
| 711 |
} |
| 712 |
|
| 713 |
/** Returns the position of a system column in an index. |
| 714 |
@param[in] type DATA_ROW_ID, ... |
| 715 |
@return position, ULINT_UNDEFINED if not contained */ |
| 716 |
ulint dict_index_t::get_sys_col_pos(ulint type) const { |
| 717 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 718 |
ut_ad(!dict_index_is_ibuf(this)); |
| 719 |
|
| 720 |
if (is_clustered()) { |
| 721 |
return (dict_col_get_clust_pos(table->get_sys_col(type), this)); |
| 722 |
} |
| 723 |
|
| 724 |
return (get_col_pos(dict_table_get_sys_col_no(table, type))); |
| 725 |
} |
| 726 |
|
| 727 |
/** Looks for column n in an index. |
| 728 |
@param[in] n column number |
| 729 |
@param[in] inc_prefix true=consider column prefixes too |
| 730 |
@param[in] is_virtual true==virtual column |
| 731 |
@return position in internal representation of the index; |
| 732 |
ULINT_UNDEFINED if not contained */ |
| 733 |
ulint dict_index_t::get_col_pos(ulint n, bool inc_prefix, |
| 734 |
bool is_virtual) const { |
| 735 |
const dict_field_t *field; |
| 736 |
const dict_col_t *col; |
| 737 |
ulint pos; |
| 738 |
ulint n_fields; |
| 739 |
|
| 740 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 741 |
|
| 742 |
if (is_virtual) { |
| 743 |
col = &(dict_table_get_nth_v_col(table, n)->m_col); |
| 744 |
} else { |
| 745 |
col = table->get_col(n); |
| 746 |
} |
| 747 |
|
| 748 |
if (is_clustered()) { |
| 749 |
return (dict_col_get_clust_pos(col, this)); |
| 750 |
} |
| 751 |
|
| 752 |
n_fields = dict_index_get_n_fields(this); |
| 753 |
|
| 754 |
for (pos = 0; pos < n_fields; pos++) { |
| 755 |
field = get_field(pos); |
| 756 |
|
| 757 |
if (col == field->col && (inc_prefix || field->prefix_len == 0)) { |
| 758 |
return (pos); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
return (ULINT_UNDEFINED); |
| 763 |
} |
| 764 |
|
| 765 |
/** Frees an index memory object. */ |
| 766 |
void dict_mem_index_free(dict_index_t *index) /*!< in: index */ |
| 767 |
{ |
| 768 |
ut_ad(index); |
| 769 |
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N); |
| 770 |
|
| 771 |
index->destroy_fields_array(); |
| 772 |
|
| 773 |
#ifndef UNIV_HOTBACKUP |
| 774 |
dict_index_zip_pad_mutex_destroy(index); |
| 775 |
|
| 776 |
if (dict_index_is_spatial(index)) { |
| 777 |
rtr_info_active::iterator it; |
| 778 |
rtr_info_t *rtr_info; |
| 779 |
|
| 780 |
for (it = index->rtr_track->rtr_active->begin(); |
| 781 |
it != index->rtr_track->rtr_active->end(); ++it) { |
| 782 |
rtr_info = *it; |
| 783 |
|
| 784 |
rtr_info->index = nullptr; |
| 785 |
} |
| 786 |
|
| 787 |
mutex_destroy(&index->rtr_ssn.mutex); |
| 788 |
mutex_destroy(&index->rtr_track->rtr_active_mutex); |
| 789 |
ut::delete_(index->rtr_track->rtr_active); |
| 790 |
} |
| 791 |
dict_index_remove_from_v_col_list(index); |
| 792 |
#endif /* !UNIV_HOTBACKUP */ |
| 793 |
|
| 794 |
index->rtr_srs.reset(); |
| 795 |
|
| 796 |
mem_heap_free(index->heap); |
| 797 |
} |
| 798 |
#ifndef UNIV_HOTBACKUP |
| 799 |
|
| 800 |
/** Create a temporary tablename like "#sql-ibtid-inc" where |
| 801 |
tid = the Table ID |
| 802 |
inc = a randomly initialized number that is incremented for each file |
| 803 |
The table ID is a 64 bit integer, can use up to 20 digits, and is initialized |
| 804 |
at bootstrap. The second number is 32 bits, can use up to 10 digits, and is |
| 805 |
initialized at startup to a randomly distributed number. It is hoped that the |
| 806 |
combination of these two numbers will provide a reasonably unique temporary |
| 807 |
file name. |
| 808 |
@param[in] heap A memory heap |
| 809 |
@param[in] dbtab Table name in the form database/table name |
| 810 |
@param[in] id Table id |
| 811 |
@return A unique temporary tablename suitable for InnoDB use */ |
| 812 |
char *dict_mem_create_temporary_tablename(mem_heap_t *heap, const char *dbtab, |
| 813 |
table_id_t id) { |
| 814 |
size_t size; |
| 815 |
char *name; |
| 816 |
const char *dbend = strchr(dbtab, '/'); |
| 817 |
ut_ad(dbend); |
| 818 |
size_t dblen = dbend - dbtab + 1; |
| 819 |
|
| 820 |
/* Increment a randomly initialized number for each temp file. */ |
| 821 |
auto file_num = |
| 822 |
dict_temp_file_num.fetch_add(1, std::memory_order_relaxed) + 1; |
| 823 |
|
| 824 |
size = dblen + (sizeof(TEMP_FILE_PREFIX) + 3 + 20 + 1 + 10); |
| 825 |
name = static_cast<char *>(mem_heap_alloc(heap, size)); |
| 826 |
memcpy(name, dbtab, dblen); |
| 827 |
snprintf(name + dblen, size - dblen, |
| 828 |
TEMP_FILE_PREFIX_INNODB UINT64PF "-" UINT32PF, id, file_num); |
| 829 |
|
| 830 |
return (name); |
| 831 |
} |
| 832 |
|
| 833 |
/** Initialize dict memory variables */ |
| 834 |
void dict_mem_init(void) { |
| 835 |
/* Initialize a randomly distributed temporary file number */ |
| 836 |
uint32_t now = static_cast<uint32_t>( |
| 837 |
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now())); |
| 838 |
|
| 839 |
const byte *buf = reinterpret_cast<const byte *>(&now); |
| 840 |
auto file_num = ut_crc32(buf, sizeof(now)); |
| 841 |
dict_temp_file_num.store(file_num, std::memory_order_relaxed); |
| 842 |
|
| 843 |
DBUG_PRINT("dict_mem_init", |
| 844 |
("Starting Temporary file number is " UINT32PF, file_num)); |
| 845 |
} |
| 846 |
|
| 847 |
/** Validate the search order in the foreign key set. |
| 848 |
@param[in] fk_set the foreign key set to be validated |
| 849 |
@return true if search order is fine in the set, false otherwise. */ |
| 850 |
bool dict_foreign_set_validate(const dict_foreign_set &fk_set) { |
| 851 |
dict_foreign_not_exists not_exists(fk_set); |
| 852 |
|
| 853 |
dict_foreign_set::iterator it = |
| 854 |
std::find_if(fk_set.begin(), fk_set.end(), not_exists); |
| 855 |
|
| 856 |
if (it == fk_set.end()) { |
| 857 |
return (true); |
| 858 |
} |
| 859 |
|
| 860 |
dict_foreign_t *foreign = *it; |
| 861 |
std::cerr << "Foreign key lookup failed: " << *foreign; |
| 862 |
std::cerr << fk_set; |
| 863 |
ut_d(ut_error); |
| 864 |
ut_o(return (false)); |
| 865 |
} |
| 866 |
|
| 867 |
/** Validate the search order in the foreign key sets of the table |
| 868 |
(foreign_set and referenced_set). |
| 869 |
@param[in] table table whose foreign key sets are to be validated |
| 870 |
@return true if foreign key sets are fine, false otherwise. */ |
| 871 |
bool dict_foreign_set_validate(const dict_table_t &table) { |
| 872 |
return (dict_foreign_set_validate(table.foreign_set) && |
| 873 |
dict_foreign_set_validate(table.referenced_set)); |
| 874 |
} |
| 875 |
|
| 876 |
bool dict_foreign_t::is_fts_col_affected() const { |
| 877 |
/* The check is skipped: |
| 878 |
- if the table has no full text index defined. |
| 879 |
- if it is a self referential foreign constaint. This is because |
| 880 |
in the context of cascading DML operation, only the referenced |
| 881 |
table is relevant for the validation even if the current table |
| 882 |
has FTS index.*/ |
| 883 |
if (!foreign_table->fts || foreign_table == referenced_table) { |
| 884 |
return false; |
| 885 |
} |
| 886 |
|
| 887 |
for (ulint i = 0; i < n_fields; i++) { |
| 888 |
const dict_col_t *col = foreign_index->get_col(i); |
| 889 |
if (dict_table_is_fts_column(foreign_table->fts->indexes, |
| 890 |
dict_col_get_no(col), |
| 891 |
col->is_virtual()) != ULINT_UNDEFINED) { |
| 892 |
return true; |
| 893 |
} |
| 894 |
} |
| 895 |
return false; |
| 896 |
} |
| 897 |
#endif /* !UNIV_HOTBACKUP */ |
| 898 |
|
| 899 |
std::ostream &operator<<(std::ostream &out, const dict_foreign_t &foreign) { |
| 900 |
out << "[dict_foreign_t: id='" << foreign.id << "'"; |
| 901 |
|
| 902 |
if (foreign.foreign_table_name != nullptr) { |
| 903 |
out << ",for: '" << foreign.foreign_table_name << "'"; |
| 904 |
} |
| 905 |
|
| 906 |
out << "]"; |
| 907 |
return (out); |
| 908 |
} |
| 909 |
|
| 910 |
std::ostream &operator<<(std::ostream &out, const dict_foreign_set &fk_set) { |
| 911 |
out << "[dict_foreign_set:"; |
| 912 |
std::for_each(fk_set.begin(), fk_set.end(), dict_foreign_print(out)); |
| 913 |
out << "]" << std::endl; |
| 914 |
return (out); |
| 915 |
} |
| 916 |
|
| 917 |
page_size_t dict_index_t::get_page_size() const { |
| 918 |
return (dict_table_page_size(table)); |
| 919 |
} |