| 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 include/dict0dict.h |
| 30 |
Data dictionary system |
| 31 |
|
| 32 |
Created 1/8/1996 Heikki Tuuri |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef dict0dict_h |
| 36 |
#define dict0dict_h |
| 37 |
|
| 38 |
#include <set> |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
#include <deque> |
| 42 |
#include "data0data.h" |
| 43 |
#include "data0type.h" |
| 44 |
#include "dict/dict.h" |
| 45 |
#include "dict0mem.h" |
| 46 |
#include "dict0types.h" |
| 47 |
#include "fsp0fsp.h" |
| 48 |
#include "fsp0sysspace.h" |
| 49 |
#include "hash0hash.h" |
| 50 |
#include "mem0mem.h" |
| 51 |
#include "rem0types.h" |
| 52 |
#include "row0types.h" |
| 53 |
#include "sql/dd/object_id.h" |
| 54 |
#include "srv0mon.h" /* for dict0dict.ic */ |
| 55 |
#include "sync0rw.h" |
| 56 |
#include "trx0types.h" |
| 57 |
#include "univ.i" |
| 58 |
#include "ut0byte.h" |
| 59 |
#include "ut0mem.h" |
| 60 |
#include "ut0new.h" |
| 61 |
#include "ut0rnd.h" |
| 62 |
|
| 63 |
#define dict_sys_mutex_enter() mutex_enter(&dict_sys->mutex) |
| 64 |
|
| 65 |
#define dict_sys_mutex_exit() mutex_exit(&dict_sys->mutex) |
| 66 |
|
| 67 |
#define dict_sys_mutex_own() mutex_own(&dict_sys->mutex) |
| 68 |
|
| 69 |
#define dict_sys_mutex_free() mutex_free(&dict_sys->mutex) |
| 70 |
/** initial memory heap size when creating a table or index object */ |
| 71 |
constexpr uint32_t DICT_HEAP_SIZE = 100; |
| 72 |
|
| 73 |
/** SDI version. Written on Page 1 & 2 at FIL_PAGE_FILE_FLUSH_LSN offset. */ |
| 74 |
const uint32_t SDI_VERSION = 1; |
| 75 |
|
| 76 |
/** Space id of system tablespace */ |
| 77 |
const space_id_t SYSTEM_TABLE_SPACE = TRX_SYS_SPACE; |
| 78 |
|
| 79 |
/** Get the database name length in a table name. |
| 80 |
@return database name length */ |
| 81 |
[[nodiscard]] ulint dict_get_db_name_len( |
| 82 |
const char *name); /*!< in: table name in the form |
| 83 |
dbname '/' tablename */ |
| 84 |
#ifndef UNIV_HOTBACKUP |
| 85 |
/** Frees a foreign key struct. */ |
| 86 |
void dict_foreign_free( |
| 87 |
dict_foreign_t *foreign); /*!< in, own: foreign key struct */ |
| 88 |
/** Finds the highest [number] for foreign key constraints of the table. Looks |
| 89 |
only at the >= 4.0.18-format id's, which are of the form |
| 90 |
databasename/tablename_ibfk_[number]. |
| 91 |
@return highest number, 0 if table has no new format foreign key constraints */ |
| 92 |
ulint dict_table_get_highest_foreign_id( |
| 93 |
dict_table_t *table); /*!< in: table in the dictionary |
| 94 |
memory cache */ |
| 95 |
#endif /* !UNIV_HOTBACKUP */ |
| 96 |
/** Return the end of table name where we have removed dbname and '/'. |
| 97 |
@return table name */ |
| 98 |
[[nodiscard]] const char *dict_remove_db_name( |
| 99 |
const char *name); /*!< in: table name in the |
| 100 |
form dbname '/' tablename */ |
| 101 |
|
| 102 |
/** Operation to perform when opening a table */ |
| 103 |
enum dict_table_op_t { |
| 104 |
/** Expect the tablespace to exist. */ |
| 105 |
DICT_TABLE_OP_NORMAL = 0, |
| 106 |
/** Drop any orphan indexes after an aborted online index creation */ |
| 107 |
DICT_TABLE_OP_DROP_ORPHAN, |
| 108 |
/** Silently load the tablespace if it does not exist, |
| 109 |
and do not load the definitions of incomplete indexes. */ |
| 110 |
DICT_TABLE_OP_LOAD_TABLESPACE |
| 111 |
}; |
| 112 |
|
| 113 |
/** Decrements the count of open handles to a table. |
| 114 |
@param[in,out] table Table |
| 115 |
@param[in] dict_locked True=data dictionary locked |
| 116 |
@param[in] try_drop True=try to drop any orphan indexes after an aborted online |
| 117 |
index creation */ |
| 118 |
void dict_table_close(dict_table_t *table, bool dict_locked, bool try_drop); |
| 119 |
|
| 120 |
/** Closes the only open handle to a table and drops a table while assuring |
| 121 |
that dict_sys->mutex is held the whole time. This assures that the table |
| 122 |
is not evicted after the close when the count of open handles goes to zero. |
| 123 |
Because dict_sys->mutex is held, we do not need to call |
| 124 |
dict_table_prevent_eviction(). */ |
| 125 |
void dict_table_close_and_drop( |
| 126 |
trx_t *trx, /*!< in: data dictionary transaction */ |
| 127 |
dict_table_t *table); /*!< in/out: table */ |
| 128 |
/** Inits the data dictionary module. */ |
| 129 |
void dict_init(void); |
| 130 |
|
| 131 |
/** Closes the data dictionary module. */ |
| 132 |
void dict_close(void); |
| 133 |
|
| 134 |
/** Inits the structure for persisting dynamic metadata */ |
| 135 |
void dict_persist_init(void); |
| 136 |
|
| 137 |
/** Clear the structure */ |
| 138 |
void dict_persist_close(void); |
| 139 |
|
| 140 |
#ifndef UNIV_HOTBACKUP |
| 141 |
/** Write back the dirty persistent dynamic metadata of the table |
| 142 |
to DDTableBuffer. |
| 143 |
@param[in,out] table table object */ |
| 144 |
void dict_table_persist_to_dd_table_buffer(dict_table_t *table); |
| 145 |
|
| 146 |
/** Read persistent dynamic metadata stored in a buffer |
| 147 |
@param[in] buffer buffer to read |
| 148 |
@param[in] size size of data in buffer |
| 149 |
@param[in] metadata where we store the metadata from buffer */ |
| 150 |
void dict_table_read_dynamic_metadata(const byte *buffer, ulint size, |
| 151 |
PersistentTableMetadata *metadata); |
| 152 |
|
| 153 |
/** Determine bytes of column prefix to be stored in the undo log. Please |
| 154 |
note that if !dict_table_has_atomic_blobs(table), no prefix |
| 155 |
needs to be stored in the undo log. |
| 156 |
@param[in] table Table. |
| 157 |
@param[in] col Column which index prefix is based on. |
| 158 |
@return bytes of column prefix to be stored in the undo log */ |
| 159 |
[[nodiscard]] static inline ulint dict_max_field_len_store_undo( |
| 160 |
dict_table_t *table, const dict_col_t *col); |
| 161 |
|
| 162 |
/** Determine maximum bytes of a virtual column need to be stored |
| 163 |
in the undo log. |
| 164 |
@param[in] table dict_table_t for the table |
| 165 |
@param[in] col_no virtual column number |
| 166 |
@return maximum bytes of virtual column to be stored in the undo log */ |
| 167 |
static inline ulint dict_max_v_field_len_store_undo(dict_table_t *table, |
| 168 |
ulint col_no); |
| 169 |
|
| 170 |
#endif /* !UNIV_HOTBACKUP */ |
| 171 |
/** Gets the column number. |
| 172 |
@return col->ind, table column position (starting from 0) */ |
| 173 |
[[nodiscard]] static inline ulint dict_col_get_no( |
| 174 |
const dict_col_t *col); /*!< in: column */ |
| 175 |
/** Gets the column position in the clustered index. |
| 176 |
@param[in] col Table column. |
| 177 |
@param[in] clust_index Clustered index. */ |
| 178 |
[[nodiscard]] static inline ulint dict_col_get_clust_pos( |
| 179 |
const dict_col_t *col, const dict_index_t *clust_index); |
| 180 |
|
| 181 |
#ifndef UNIV_HOTBACKUP |
| 182 |
/** Gets the column position in the given index. |
| 183 |
@param[in] col table column |
| 184 |
@param[in] index index to be searched for column |
| 185 |
@return position of column in the given index. */ |
| 186 |
[[nodiscard]] static inline ulint dict_col_get_index_pos( |
| 187 |
const dict_col_t *col, const dict_index_t *index) MY_ATTRIBUTE((nonnull)); |
| 188 |
|
| 189 |
/** If the given column name is reserved for InnoDB system columns, return |
| 190 |
true. |
| 191 |
@return true if name is reserved */ |
| 192 |
[[nodiscard]] bool dict_col_name_is_reserved( |
| 193 |
const char *name); /*!< in: column name */ |
| 194 |
/** Acquire the autoinc lock. */ |
| 195 |
void dict_table_autoinc_lock(dict_table_t *table); /*!< in/out: table */ |
| 196 |
|
| 197 |
/** Unconditionally set the autoinc counter. |
| 198 |
@param[in,out] table Table |
| 199 |
@param[in] value Next value to assign to a row */ |
| 200 |
void dict_table_autoinc_initialize(dict_table_t *table, uint64_t value); |
| 201 |
|
| 202 |
/** Reads the next autoinc value (== autoinc counter value), 0 if not yet |
| 203 |
initialized. |
| 204 |
@return value for a new row, or 0 */ |
| 205 |
[[nodiscard]] uint64_t dict_table_autoinc_read( |
| 206 |
const dict_table_t *table); /*!< in: table */ |
| 207 |
|
| 208 |
/** Updates the autoinc counter if the value supplied is greater than the |
| 209 |
current value. |
| 210 |
@param[in,out] table Table |
| 211 |
@param[in] value Value which was assigned to a row */ |
| 212 |
void dict_table_autoinc_update_if_greater(dict_table_t *table, uint64_t value); |
| 213 |
|
| 214 |
/** Release the autoinc lock. */ |
| 215 |
void dict_table_autoinc_unlock(dict_table_t *table); /*!< in/out: table */ |
| 216 |
|
| 217 |
/** Update the persisted autoinc counter to specified one, we should hold |
| 218 |
autoinc_persisted_mutex. |
| 219 |
@param[in,out] table table |
| 220 |
@param[in] autoinc set autoinc_persisted to this value */ |
| 221 |
static inline void dict_table_autoinc_persisted_update(dict_table_t *table, |
| 222 |
uint64_t autoinc); |
| 223 |
|
| 224 |
/** Set the column position of autoinc column in clustered index for a table. |
| 225 |
@param[in] table table |
| 226 |
@param[in] pos column position in table definition */ |
| 227 |
static inline void dict_table_autoinc_set_col_pos(dict_table_t *table, |
| 228 |
ulint pos); |
| 229 |
|
| 230 |
/** Write redo logs for autoinc counter that is to be inserted, or to |
| 231 |
update some existing smaller one to bigger. |
| 232 |
@param[in,out] table InnoDB table object |
| 233 |
@param[in] value AUTOINC counter to log |
| 234 |
@param[in,out] mtr Mini-transaction |
| 235 |
@return true if auto increment needs to be persisted to DD table buffer. */ |
| 236 |
bool dict_table_autoinc_log(dict_table_t *table, uint64_t value, mtr_t *mtr); |
| 237 |
|
| 238 |
/** Check if a table has an autoinc counter column. |
| 239 |
@param[in] table table |
| 240 |
@return true if there is an autoinc column in the table, otherwise false. */ |
| 241 |
static inline bool dict_table_has_autoinc_col(const dict_table_t *table); |
| 242 |
|
| 243 |
#endif /* !UNIV_HOTBACKUP */ |
| 244 |
|
| 245 |
/** Adds system columns to a table object. |
| 246 |
@param[in,out] table Table |
| 247 |
@param[in] heap Temporary heap */ |
| 248 |
void dict_table_add_system_columns(dict_table_t *table, mem_heap_t *heap); |
| 249 |
|
| 250 |
#ifndef UNIV_HOTBACKUP |
| 251 |
/** Mark if table has big rows. |
| 252 |
@param[in,out] table table handler */ |
| 253 |
void dict_table_set_big_rows(dict_table_t *table) MY_ATTRIBUTE((nonnull)); |
| 254 |
|
| 255 |
/** Adds a table object to the dictionary cache. |
| 256 |
@param[in,out] table table |
| 257 |
@param[in] can_be_evicted true if can be evicted */ |
| 258 |
void dict_table_add_to_cache(dict_table_t *table, bool can_be_evicted); |
| 259 |
|
| 260 |
/** Removes a table object from the dictionary cache. */ |
| 261 |
void dict_table_remove_from_cache(dict_table_t *table); /*!< in, own: table */ |
| 262 |
|
| 263 |
/** Try to invalidate an entry from the dict cache, for a partitioned table, |
| 264 |
if any table found. |
| 265 |
@param[in] name Table name */ |
| 266 |
void dict_partitioned_table_remove_from_cache(const char *name); |
| 267 |
|
| 268 |
#ifdef UNIV_DEBUG |
| 269 |
/** Removes a table object from the dictionary cache, for debug purpose |
| 270 |
@param[in,out] table table object |
| 271 |
@param[in] lru_evict true if table being evicted to make room |
| 272 |
in the table LRU list */ |
| 273 |
void dict_table_remove_from_cache_debug(dict_table_t *table, bool lru_evict); |
| 274 |
#endif /* UNIV_DEBUG */ |
| 275 |
|
| 276 |
/** Renames a table object. |
| 277 |
@return true if success */ |
| 278 |
[[nodiscard]] dberr_t dict_table_rename_in_cache( |
| 279 |
dict_table_t *table, /*!< in/out: table */ |
| 280 |
const char *new_name, /*!< in: new name */ |
| 281 |
bool rename_also_foreigns); |
| 282 |
/*!< in: in ALTER TABLE we want |
| 283 |
to preserve the original table name |
| 284 |
in constraints which reference it */ |
| 285 |
|
| 286 |
/** Removes an index from the dictionary cache. |
| 287 |
@param[in,out] table table whose index to remove |
| 288 |
@param[in,out] index index to remove, this object is destroyed and must not |
| 289 |
be accessed by the caller afterwards */ |
| 290 |
void dict_index_remove_from_cache(dict_table_t *table, dict_index_t *index); |
| 291 |
|
| 292 |
/** Gathers ids of all tables in cache at the moment. |
| 293 |
@return ids of all tables */ |
| 294 |
std::vector<table_id_t> dict_get_all_table_ids(); |
| 295 |
|
| 296 |
/** Change the id of a table object in the dictionary cache. This is used in |
| 297 |
DISCARD TABLESPACE. */ |
| 298 |
void dict_table_change_id_in_cache( |
| 299 |
dict_table_t *table, /*!< in/out: table object already in cache */ |
| 300 |
table_id_t new_id); /*!< in: new id to set */ |
| 301 |
/** Removes a foreign constraint struct from the dictionary cache. */ |
| 302 |
void dict_foreign_remove_from_cache( |
| 303 |
dict_foreign_t *foreign); /*!< in, own: foreign constraint */ |
| 304 |
/** Adds a foreign key constraint object to the dictionary cache. May free |
| 305 |
the object if there already is an object with the same identifier in. |
| 306 |
At least one of foreign table or referenced table must already be in |
| 307 |
the dictionary cache! |
| 308 |
@return DB_SUCCESS or error code */ |
| 309 |
[[nodiscard]] dberr_t dict_foreign_add_to_cache( |
| 310 |
dict_foreign_t *foreign, |
| 311 |
/*!< in, own: foreign key constraint */ |
| 312 |
const char **col_names, |
| 313 |
/*!< in: column names, or NULL to use |
| 314 |
foreign->foreign_table->col_names */ |
| 315 |
bool check_charsets, |
| 316 |
/*!< in: whether to check charset |
| 317 |
compatibility */ |
| 318 |
bool can_free_fk, |
| 319 |
/*!< in: whether free existing FK */ |
| 320 |
dict_err_ignore_t ignore_err); |
| 321 |
/*!< in: error to be ignored */ |
| 322 |
/** Checks if a table is referenced by foreign keys. |
| 323 |
@return true if table is referenced by a foreign key */ |
| 324 |
[[nodiscard]] bool dict_table_is_referenced_by_foreign_key( |
| 325 |
const dict_table_t *table); /*!< in: InnoDB table */ |
| 326 |
/** Replace the index passed in with another equivalent index in the |
| 327 |
foreign key lists of the table. |
| 328 |
@return whether all replacements were found */ |
| 329 |
[[nodiscard]] bool dict_foreign_replace_index( |
| 330 |
dict_table_t *table, /*!< in/out: table */ |
| 331 |
const char **col_names, |
| 332 |
/*!< in: column names, or NULL |
| 333 |
to use table->col_names */ |
| 334 |
const dict_index_t *index); /*!< in: index to be replaced */ |
| 335 |
#endif /* !UNIV_HOTBACKUP */ |
| 336 |
/** Returns a table object and increments its open handle count. |
| 337 |
NOTE! This is a high-level function to be used mainly from outside the |
| 338 |
'dict' directory. Inside this directory dict_table_get_low |
| 339 |
is usually the appropriate function. |
| 340 |
@param[in] table_name Table name |
| 341 |
@param[in] dict_locked true=data dictionary locked |
| 342 |
@param[in] try_drop true=try to drop any orphan indexes after |
| 343 |
an aborted online index creation |
| 344 |
@param[in] ignore_err error to be ignored when loading the table |
| 345 |
@return table, NULL if does not exist */ |
| 346 |
[[nodiscard]] dict_table_t *dict_table_open_on_name( |
| 347 |
const char *table_name, bool dict_locked, bool try_drop, |
| 348 |
dict_err_ignore_t ignore_err); |
| 349 |
|
| 350 |
/** Tries to find an index whose first fields are the columns in the array, |
| 351 |
in the same order and is not marked for deletion and is not the same |
| 352 |
as types_idx. |
| 353 |
@return matching index, NULL if not found */ |
| 354 |
[[nodiscard]] dict_index_t *dict_foreign_find_index( |
| 355 |
const dict_table_t *table, /*!< in: table */ |
| 356 |
const char **col_names, |
| 357 |
/*!< in: column names, or NULL |
| 358 |
to use table->col_names */ |
| 359 |
const char **columns, /*!< in: array of column names */ |
| 360 |
ulint n_cols, /*!< in: number of columns */ |
| 361 |
const dict_index_t *types_idx, |
| 362 |
/*!< in: NULL or an index |
| 363 |
whose types the column types |
| 364 |
must match */ |
| 365 |
bool check_charsets, |
| 366 |
/*!< in: whether to check |
| 367 |
charsets. only has an effect |
| 368 |
if types_idx != NULL */ |
| 369 |
ulint check_null); |
| 370 |
/*!< in: nonzero if none of |
| 371 |
the columns must be declared |
| 372 |
NOT NULL */ |
| 373 |
|
| 374 |
/** Returns a virtual column's name. |
| 375 |
@param[in] table target table |
| 376 |
@param[in] col_nr virtual column number (nth virtual column) |
| 377 |
@return column name or NULL if column number out of range. */ |
| 378 |
const char *dict_table_get_v_col_name(const dict_table_t *table, ulint col_nr); |
| 379 |
|
| 380 |
/** Check if the table has a given (non_virtual) column. |
| 381 |
@param[in] table table object |
| 382 |
@param[in] col_name column name |
| 383 |
@param[in] col_nr column number guessed, 0 as default |
| 384 |
@return column number if the table has the specified column, |
| 385 |
otherwise table->n_def */ |
| 386 |
ulint dict_table_has_column(const dict_table_t *table, const char *col_name, |
| 387 |
ulint col_nr = 0); |
| 388 |
|
| 389 |
/** Outputs info on a foreign key of a table in a format suitable for |
| 390 |
CREATE TABLE. |
| 391 |
@param[in] file File where to print |
| 392 |
@param[in] trx Transaction |
| 393 |
@param[in] foreign Foreign key constraint |
| 394 |
@param[in] add_newline Whether to add a newline */ |
| 395 |
void dict_print_info_on_foreign_key_in_create_format(FILE *file, trx_t *trx, |
| 396 |
dict_foreign_t *foreign, |
| 397 |
bool add_newline); |
| 398 |
|
| 399 |
/** Tries to find an index whose first fields are the columns in the array, |
| 400 |
in the same order and is not marked for deletion and is not the same |
| 401 |
as types_idx. |
| 402 |
@return matching index, NULL if not found */ |
| 403 |
[[nodiscard]] bool dict_foreign_qualify_index( |
| 404 |
const dict_table_t *table, /*!< in: table */ |
| 405 |
const char **col_names, |
| 406 |
/*!< in: column names, or NULL |
| 407 |
to use table->col_names */ |
| 408 |
const char **columns, /*!< in: array of column names */ |
| 409 |
ulint n_cols, /*!< in: number of columns */ |
| 410 |
const dict_index_t *index, /*!< in: index to check */ |
| 411 |
const dict_index_t *types_idx, |
| 412 |
/*!< in: NULL or an index |
| 413 |
whose types the column types |
| 414 |
must match */ |
| 415 |
bool check_charsets, |
| 416 |
/*!< in: whether to check |
| 417 |
charsets. only has an effect |
| 418 |
if types_idx != NULL */ |
| 419 |
ulint check_null); |
| 420 |
/*!< in: nonzero if none of |
| 421 |
the columns must be declared |
| 422 |
NOT NULL */ |
| 423 |
|
| 424 |
/* Skip corrupted index */ |
| 425 |
static inline void dict_table_skip_corrupt_index(dict_index_t *&index) { |
| 426 |
while (index && index->is_corrupted()) { |
| 427 |
index = index->next(); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
/* Get the next non-corrupt index */ |
| 432 |
static inline void dict_table_next_uncorrupted_index(dict_index_t *&index) { |
| 433 |
index = index->next(); |
| 434 |
dict_table_skip_corrupt_index(index); |
| 435 |
} |
| 436 |
|
| 437 |
/** Check if index is auto-generated clustered index. |
| 438 |
@param[in] index index |
| 439 |
|
| 440 |
@return true if index is auto-generated clustered index. */ |
| 441 |
static inline bool dict_index_is_auto_gen_clust(const dict_index_t *index); |
| 442 |
|
| 443 |
/** Check whether the index is unique. |
| 444 |
@return nonzero for unique index, zero for other indexes */ |
| 445 |
[[nodiscard]] static inline ulint dict_index_is_unique( |
| 446 |
const dict_index_t *index); /*!< in: index */ |
| 447 |
/** Check whether the index is a Spatial Index. |
| 448 |
@return nonzero for Spatial Index, zero for other indexes */ |
| 449 |
[[nodiscard]] static inline ulint dict_index_is_spatial( |
| 450 |
const dict_index_t *index); /*!< in: index */ |
| 451 |
/** Check whether the index contains a virtual column. |
| 452 |
@param[in] index index |
| 453 |
@return nonzero for index on virtual column, zero for other indexes */ |
| 454 |
static inline ulint dict_index_has_virtual(const dict_index_t *index); |
| 455 |
/** Check whether the index is the insert buffer tree. |
| 456 |
@return nonzero for insert buffer, zero for other indexes */ |
| 457 |
[[nodiscard]] static inline ulint dict_index_is_ibuf( |
| 458 |
const dict_index_t *index); /*!< in: index */ |
| 459 |
|
| 460 |
/** Check whether the index consists of descending columns only. |
| 461 |
@param[in] index index tree |
| 462 |
@retval true if index has any descending column |
| 463 |
@retval false if index has only ascending columns */ |
| 464 |
[[nodiscard]] static inline bool dict_index_has_desc(const dict_index_t *index); |
| 465 |
/** Check whether the index is a secondary index or the insert buffer tree. |
| 466 |
@return nonzero for insert buffer, zero for other indexes */ |
| 467 |
[[nodiscard]] static inline ulint dict_index_is_sec_or_ibuf( |
| 468 |
const dict_index_t *index); /*!< in: index */ |
| 469 |
|
| 470 |
/** Get all the FTS indexes on a table. |
| 471 |
@param[in] table table |
| 472 |
@param[out] indexes all FTS indexes on this table |
| 473 |
@return number of FTS indexes */ |
| 474 |
ulint dict_table_get_all_fts_indexes(dict_table_t *table, ib_vector_t *indexes); |
| 475 |
|
| 476 |
static inline ulint dict_table_get_n_tot_u_cols(const dict_table_t *table); |
| 477 |
|
| 478 |
/** Gets the number of virtual columns in a table in the dictionary cache. |
| 479 |
@param[in] table the table to check |
| 480 |
@return number of virtual columns of a table */ |
| 481 |
static inline ulint dict_table_get_n_v_cols(const dict_table_t *table); |
| 482 |
|
| 483 |
/** Check if a table has indexed virtual columns |
| 484 |
@param[in] table the table to check |
| 485 |
@return true is the table has indexed virtual columns */ |
| 486 |
static inline bool dict_table_has_indexed_v_cols(const dict_table_t *table); |
| 487 |
|
| 488 |
#ifndef UNIV_HOTBACKUP |
| 489 |
/** Gets the approximately estimated number of rows in the table. |
| 490 |
@return estimated number of rows */ |
| 491 |
[[nodiscard]] static inline uint64_t dict_table_get_n_rows( |
| 492 |
const dict_table_t *table); /*!< in: table */ |
| 493 |
/** Increment the number of rows in the table by one. |
| 494 |
Notice that this operation is not protected by any latch, the number is |
| 495 |
approximate. */ |
| 496 |
static inline void dict_table_n_rows_inc( |
| 497 |
dict_table_t *table); /*!< in/out: table */ |
| 498 |
/** Decrement the number of rows in the table by one. |
| 499 |
Notice that this operation is not protected by any latch, the number is |
| 500 |
approximate. */ |
| 501 |
static inline void dict_table_n_rows_dec( |
| 502 |
dict_table_t *table); /*!< in/out: table */ |
| 503 |
#endif /* !UNIV_HOTBACKUP */ |
| 504 |
|
| 505 |
/** Get nth virtual column according to its original MySQL table position |
| 506 |
@param[in] table target table |
| 507 |
@param[in] col_nr column number in MySQL Table definition |
| 508 |
@return dict_v_col_t ptr */ |
| 509 |
dict_v_col_t *dict_table_get_nth_v_col_mysql(const dict_table_t *table, |
| 510 |
ulint col_nr); |
| 511 |
|
| 512 |
#ifdef UNIV_DEBUG |
| 513 |
/** Gets the nth virtual column of a table. |
| 514 |
@param[in] table table |
| 515 |
@param[in] pos position of virtual column |
| 516 |
@return pointer to virtual column object */ |
| 517 |
static inline dict_v_col_t *dict_table_get_nth_v_col(const dict_table_t *table, |
| 518 |
ulint pos); |
| 519 |
|
| 520 |
#else /* UNIV_DEBUG */ |
| 521 |
/* Get nth virtual columns */ |
| 522 |
#define dict_table_get_nth_v_col(table, pos) ((table)->v_cols + (pos)) |
| 523 |
#endif /* UNIV_DEBUG */ |
| 524 |
/** Gets the given system column number of a table. |
| 525 |
@param[in] table Table. |
| 526 |
@param[in] sys DATA_ROW_ID, ... |
| 527 |
@return column number */ |
| 528 |
[[nodiscard]] static inline ulint dict_table_get_sys_col_no( |
| 529 |
const dict_table_t *table, ulint sys); |
| 530 |
/** Check whether the table uses the compact page format. |
| 531 |
@return true if table uses the compact page format */ |
| 532 |
[[nodiscard]] static inline bool dict_table_is_comp( |
| 533 |
const dict_table_t *table); /*!< in: table */ |
| 534 |
|
| 535 |
/** Determine if a table uses atomic BLOBs (no locally stored prefix). |
| 536 |
@param[in] table InnoDB table |
| 537 |
@return whether BLOBs are atomic */ |
| 538 |
[[nodiscard]] static inline bool dict_table_has_atomic_blobs( |
| 539 |
const dict_table_t *table); |
| 540 |
|
| 541 |
#ifndef UNIV_HOTBACKUP |
| 542 |
/** Set the various values in a dict_table_t::flags pointer. |
| 543 |
@param[in,out] flags Pointer to a 4 byte Table Flags |
| 544 |
@param[in] format File Format |
| 545 |
@param[in] zip_ssize Zip Shift Size |
| 546 |
@param[in] use_data_dir Table uses DATA DIRECTORY |
| 547 |
@param[in] shared_space Table uses a General Shared Tablespace */ |
| 548 |
static inline void dict_tf_set(uint32_t *flags, rec_format_t format, |
| 549 |
uint32_t zip_ssize, bool use_data_dir, |
| 550 |
bool shared_space); |
| 551 |
|
| 552 |
/** Initialize a dict_table_t::flags pointer. |
| 553 |
@param[in] compact Table uses Compact or greater |
| 554 |
@param[in] zip_ssize Zip Shift Size (log 2 minus 9) |
| 555 |
@param[in] atomic_blobs Table uses Compressed or Dynamic |
| 556 |
@param[in] data_dir Table uses DATA DIRECTORY |
| 557 |
@param[in] shared_space Table uses a General Shared Tablespace */ |
| 558 |
static inline uint32_t dict_tf_init(bool compact, ulint zip_ssize, |
| 559 |
bool atomic_blobs, bool data_dir, |
| 560 |
bool shared_space); |
| 561 |
|
| 562 |
/** Convert a 32 bit integer table flags to the 32 bit FSP Flags. |
| 563 |
Fsp Flags are written into the tablespace header at the offset |
| 564 |
FSP_SPACE_FLAGS and are also stored in the fil_space_t::flags field. |
| 565 |
The following chart shows the translation of the low order bit. |
| 566 |
Other bits are the same. |
| 567 |
========================= Low order bit ========================== |
| 568 |
| REDUNDANT | COMPACT | COMPRESSED | DYNAMIC |
| 569 |
dict_table_t::flags | 0 | 1 | 1 | 1 |
| 570 |
fil_space_t::flags | 0 | 0 | 1 | 1 |
| 571 |
================================================================== |
| 572 |
@param[in] table_flags dict_table_t::flags |
| 573 |
@return tablespace flags (fil_space_t::flags) */ |
| 574 |
uint32_t dict_tf_to_fsp_flags(uint32_t table_flags); |
| 575 |
|
| 576 |
/** Extract the page size info from table flags. |
| 577 |
@param[in] flags flags |
| 578 |
@return a structure containing the compressed and uncompressed |
| 579 |
page sizes and a boolean indicating if the page is compressed. */ |
| 580 |
static inline const page_size_t dict_tf_get_page_size(uint32_t flags); |
| 581 |
|
| 582 |
#endif /* !UNIV_HOTBACKUP */ |
| 583 |
|
| 584 |
/** Determine the extent size (in pages) for the given table |
| 585 |
@param[in] table the table whose extent size is being |
| 586 |
calculated. |
| 587 |
@return extent size in pages (256, 128 or 64) */ |
| 588 |
page_no_t dict_table_extent_size(const dict_table_t *table); |
| 589 |
|
| 590 |
/** Get the table page size. |
| 591 |
@param[in] table table |
| 592 |
@return compressed page size, or 0 if not compressed */ |
| 593 |
[[nodiscard]] static inline const page_size_t dict_table_page_size( |
| 594 |
const dict_table_t *table); |
| 595 |
|
| 596 |
#ifndef UNIV_HOTBACKUP |
| 597 |
/** Obtain exclusive locks on all index trees of the table. This is to prevent |
| 598 |
accessing index trees while InnoDB is updating internal metadata for |
| 599 |
operations such as FLUSH TABLES. */ |
| 600 |
static inline void dict_table_x_lock_indexes( |
| 601 |
dict_table_t *table); /*!< in: table */ |
| 602 |
/** Release the exclusive locks on all index tree. */ |
| 603 |
static inline void dict_table_x_unlock_indexes( |
| 604 |
dict_table_t *table); /*!< in: table */ |
| 605 |
/** Check if the table has an FTS index. |
| 606 |
@return true if table has an FTS index */ |
| 607 |
[[nodiscard]] static inline bool dict_table_has_fts_index( |
| 608 |
dict_table_t *table); /*!< in: table */ |
| 609 |
#ifdef UNIV_DEBUG |
| 610 |
/** Validate no active background threads to cause purge or rollback |
| 611 |
operations. */ |
| 612 |
void dict_validate_no_purge_rollback_threads(); |
| 613 |
#endif /* UNIV_DEBUG */ |
| 614 |
#endif /* !UNIV_HOTBACKUP */ |
| 615 |
/** Checks if a column is in the ordering columns of the clustered index of a |
| 616 |
table. Column prefixes are treated like whole columns. |
| 617 |
@return true if the column, or its prefix, is in the clustered key */ |
| 618 |
[[nodiscard]] bool dict_table_col_in_clustered_key( |
| 619 |
const dict_table_t *table, /*!< in: table */ |
| 620 |
ulint n); /*!< in: column number */ |
| 621 |
/** Copies types of virtual columns contained in table to tuple and sets all |
| 622 |
fields of the tuple to the SQL NULL value. This function should |
| 623 |
be called right after dtuple_create(). |
| 624 |
@param[in,out] tuple data tuple |
| 625 |
@param[in] table table */ |
| 626 |
void dict_table_copy_v_types(dtuple_t *tuple, const dict_table_t *table); |
| 627 |
/** Copies types of columns contained in table to tuple and sets all |
| 628 |
fields of the tuple to the SQL NULL value. This function should |
| 629 |
be called right after dtuple_create(). */ |
| 630 |
void dict_table_copy_types(dtuple_t *tuple, /*!< in/out: data tuple */ |
| 631 |
const dict_table_t *table); /*!< in: table */ |
| 632 |
#ifndef UNIV_HOTBACKUP |
| 633 |
/******************************************************************** |
| 634 |
Wait until all the background threads of the given table have exited, i.e., |
| 635 |
bg_threads == 0. Note: bg_threads_mutex must be reserved when |
| 636 |
calling this. */ |
| 637 |
void dict_table_wait_for_bg_threads_to_exit( |
| 638 |
dict_table_t *table, /* in: table */ |
| 639 |
std::chrono::microseconds delay); /* in: time to wait between |
| 640 |
checks of bg_threads. */ |
| 641 |
|
| 642 |
/** Look up an index among already opened tables. Does not attempt to open |
| 643 |
tables that are not available in the dictionary cache. This behaviour is fine |
| 644 |
for information schema's scenarios - use dd_table_open_on_id() if you need |
| 645 |
to access index meta-data reliably. |
| 646 |
@param[in] id index identifier |
| 647 |
@return index or NULL if not found */ |
| 648 |
[[nodiscard]] const dict_index_t *dict_index_find(const index_id_t &id); |
| 649 |
|
| 650 |
/** Make room in the table cache by evicting an unused table. The unused table |
| 651 |
should not be part of FK relationship and currently not used in any user |
| 652 |
transaction. There is no guarantee that it will remove a table. |
| 653 |
@return number of tables evicted. */ |
| 654 |
ulint dict_make_room_in_cache( |
| 655 |
ulint max_tables, /*!< in: max tables allowed in cache */ |
| 656 |
ulint pct_check); /*!< in: max percent to check */ |
| 657 |
|
| 658 |
constexpr uint32_t BIG_ROW_SIZE = 1024; |
| 659 |
|
| 660 |
/** Adds an index to the dictionary cache. |
| 661 |
@param[in,out] table table on which the index is |
| 662 |
@param[in,out] index index; NOTE! The index memory |
| 663 |
object is freed in this function! |
| 664 |
@param[in] page_no root page number of the index |
| 665 |
@param[in] strict true=refuse to create the index |
| 666 |
if records could be too big to fit in |
| 667 |
an B-tree page |
| 668 |
@return DB_SUCCESS, DB_TOO_BIG_RECORD, or DB_CORRUPTION */ |
| 669 |
[[nodiscard]] dberr_t dict_index_add_to_cache(dict_table_t *table, |
| 670 |
dict_index_t *index, |
| 671 |
page_no_t page_no, bool strict); |
| 672 |
|
| 673 |
/** Clears the virtual column's index list before index is being freed. |
| 674 |
@param[in] index Index being freed */ |
| 675 |
void dict_index_remove_from_v_col_list(dict_index_t *index); |
| 676 |
|
| 677 |
/** Adds an index to the dictionary cache, with possible indexing newly |
| 678 |
added column. |
| 679 |
@param[in,out] table table on which the index is |
| 680 |
@param[in,out] index index; NOTE! The index memory |
| 681 |
object is freed in this function! |
| 682 |
@param[in] add_v new virtual column that being added along with |
| 683 |
an add index call |
| 684 |
@param[in] page_no root page number of the index |
| 685 |
@param[in] strict true=refuse to create the index |
| 686 |
if records could be too big to fit in |
| 687 |
an B-tree page |
| 688 |
@return DB_SUCCESS, DB_TOO_BIG_RECORD, or DB_CORRUPTION */ |
| 689 |
[[nodiscard]] dberr_t dict_index_add_to_cache_w_vcol( |
| 690 |
dict_table_t *table, dict_index_t *index, const dict_add_v_col_t *add_v, |
| 691 |
page_no_t page_no, bool strict); |
| 692 |
#endif /* !UNIV_HOTBACKUP */ |
| 693 |
/** Gets the number of fields in the internal representation of an index, |
| 694 |
including fields added by the dictionary system. |
| 695 |
@return number of fields */ |
| 696 |
[[nodiscard]] inline uint16_t dict_index_get_n_fields( |
| 697 |
const dict_index_t *index); /*!< in: an internal |
| 698 |
representation of index (in |
| 699 |
the dictionary cache) */ |
| 700 |
/** Gets the number of fields in the internal representation of an index |
| 701 |
that uniquely determine the position of an index entry in the index, if |
| 702 |
we do not take multiversioning into account: in the B-tree use the value |
| 703 |
returned by dict_index_get_n_unique_in_tree. |
| 704 |
@return number of fields */ |
| 705 |
[[nodiscard]] inline uint16_t dict_index_get_n_unique( |
| 706 |
const dict_index_t *index); /*!< in: an internal representation |
| 707 |
of index (in the dictionary cache) */ |
| 708 |
/** Gets the number of fields in the internal representation of an index |
| 709 |
which uniquely determine the position of an index entry in the index, if |
| 710 |
we also take multiversioning into account. |
| 711 |
@return number of fields */ |
| 712 |
[[nodiscard]] inline uint16_t dict_index_get_n_unique_in_tree( |
| 713 |
const dict_index_t *index); /*!< in: an internal representation |
| 714 |
of index (in the dictionary cache) */ |
| 715 |
|
| 716 |
/** The number of fields in the nonleaf page of spatial index, except |
| 717 |
the page no field. */ |
| 718 |
constexpr uint32_t DICT_INDEX_SPATIAL_NODEPTR_SIZE = 1; |
| 719 |
/** |
| 720 |
Gets the number of fields on nonleaf page level in the internal representation |
| 721 |
of an index which uniquely determine the position of an index entry in the |
| 722 |
index, if we also take multiversioning into account. Note, it doesn't |
| 723 |
include page no field. |
| 724 |
@param[in] index index |
| 725 |
@return number of fields */ |
| 726 |
[[nodiscard]] inline uint16_t dict_index_get_n_unique_in_tree_nonleaf( |
| 727 |
const dict_index_t *index); |
| 728 |
/** Gets the number of user-defined ordering fields in the index. In the |
| 729 |
internal representation we add the row id to the ordering fields to make all |
| 730 |
indexes unique, but this function returns the number of fields the user defined |
| 731 |
in the index as ordering fields. |
| 732 |
@return number of fields */ |
| 733 |
[[nodiscard]] inline ulint dict_index_get_n_ordering_defined_by_user( |
| 734 |
const dict_index_t *index); /*!< in: an internal representation |
| 735 |
of index (in the dictionary cache) */ |
| 736 |
/** Returns true if the index contains a column or a prefix of that column. |
| 737 |
@param[in] index index |
| 738 |
@param[in] n column number |
| 739 |
@param[in] is_virtual whether it is a virtual col |
| 740 |
@return true if contains the column or its prefix */ |
| 741 |
[[nodiscard]] bool dict_index_contains_col_or_prefix(const dict_index_t *index, |
| 742 |
ulint n, bool is_virtual); |
| 743 |
/** Looks for a matching field in an index. The column has to be the same. The |
| 744 |
column in index must be complete, or must contain a prefix longer than the |
| 745 |
column in index2. That is, we must be able to construct the prefix in index2 |
| 746 |
from the prefix in index. |
| 747 |
@return position in internal representation of the index; |
| 748 |
ULINT_UNDEFINED if not contained */ |
| 749 |
[[nodiscard]] ulint dict_index_get_nth_field_pos( |
| 750 |
const dict_index_t *index, /*!< in: index from which to search */ |
| 751 |
const dict_index_t *index2, /*!< in: index */ |
| 752 |
ulint n); /*!< in: field number in index2 */ |
| 753 |
/** Looks for non-virtual column n position in the clustered index. |
| 754 |
@return position in internal representation of the clustered index */ |
| 755 |
[[nodiscard]] ulint dict_table_get_nth_col_pos( |
| 756 |
const dict_table_t *table, /*!< in: table */ |
| 757 |
ulint n); /*!< in: column number */ |
| 758 |
|
| 759 |
/** Get the innodb column position for a non-virtual column according to |
| 760 |
its original MySQL table position n |
| 761 |
@param[in] table table |
| 762 |
@param[in] n MySQL column position |
| 763 |
@return column position in InnoDB */ |
| 764 |
ulint dict_table_mysql_pos_to_innodb(const dict_table_t *table, ulint n); |
| 765 |
|
| 766 |
/** Copies types of fields contained in index to tuple. |
| 767 |
@param[in,out] tuple Data tuple |
| 768 |
@param[in] index Index |
| 769 |
@param[in] n_fields Number of field types to copy */ |
| 770 |
void dict_index_copy_types(dtuple_t *tuple, const dict_index_t *index, |
| 771 |
ulint n_fields); |
| 772 |
|
| 773 |
#ifdef UNIV_DEBUG |
| 774 |
/** Checks that a tuple has n_fields_cmp value in a sensible range, so that |
| 775 |
no comparison can occur with the page number field in a node pointer. |
| 776 |
@return true if ok */ |
| 777 |
[[nodiscard]] bool dict_index_check_search_tuple( |
| 778 |
const dict_index_t *index, /*!< in: index tree */ |
| 779 |
const dtuple_t *tuple); /*!< in: tuple used in a search */ |
| 780 |
/** Whether and when to allow temporary index names */ |
| 781 |
enum check_name { |
| 782 |
/** Require all indexes to be complete. */ |
| 783 |
CHECK_ALL_COMPLETE, |
| 784 |
/** Allow aborted online index creation. */ |
| 785 |
CHECK_ABORTED_OK, |
| 786 |
/** Allow partial indexes to exist. */ |
| 787 |
CHECK_PARTIAL_OK |
| 788 |
}; |
| 789 |
|
| 790 |
/** Check for duplicate index entries in a table [using the index name] |
| 791 |
@param[in] table Check for dup indexes in this table |
| 792 |
@param[in] check Whether and when to allow temporary index names */ |
| 793 |
void dict_table_check_for_dup_indexes(const dict_table_t *table, |
| 794 |
enum check_name check); |
| 795 |
|
| 796 |
/** Check if a table is a temporary table with compressed row format, |
| 797 |
we should always expect false. |
| 798 |
@param[in] table table |
| 799 |
@return true if it's a compressed temporary table, false otherwise */ |
| 800 |
inline bool dict_table_is_compressed_temporary(const dict_table_t *table); |
| 801 |
#endif /* UNIV_DEBUG */ |
| 802 |
/** Builds a node pointer out of a physical record and a page number. |
| 803 |
@return own: node pointer */ |
| 804 |
[[nodiscard]] dtuple_t *dict_index_build_node_ptr( |
| 805 |
const dict_index_t *index, /*!< in: index */ |
| 806 |
const rec_t *rec, /*!< in: record for which |
| 807 |
to build node pointer */ |
| 808 |
page_no_t page_no, /*!< in: page number to |
| 809 |
put in node pointer */ |
| 810 |
mem_heap_t *heap, /*!< in: memory heap where |
| 811 |
pointer created */ |
| 812 |
ulint level); /*!< in: level of rec in tree: |
| 813 |
0 means leaf level */ |
| 814 |
/** Copies an initial segment of a physical record, long enough to specify an |
| 815 |
index entry uniquely. In case buf is nullptr or the buf_size is not enough to |
| 816 |
store the prefix, the new buffer will be allocated. |
| 817 |
@param[in] index index |
| 818 |
@param[in] rec record for which to copy prefix |
| 819 |
@param[out] n_fields number of fields copied |
| 820 |
@param[in,out] buf memory buffer for the copied prefix, or nullptr |
| 821 |
@param[in,out] buf_size buffer size, size of allocated buffer |
| 822 |
@return pointer to the prefix record */ |
| 823 |
[[nodiscard]] rec_t *dict_index_copy_rec_order_prefix(const dict_index_t *index, |
| 824 |
const rec_t *rec, |
| 825 |
ulint *n_fields, |
| 826 |
byte **buf, |
| 827 |
size_t *buf_size); |
| 828 |
/** Builds a typed data tuple out of a physical record. |
| 829 |
@return own: data tuple */ |
| 830 |
[[nodiscard]] dtuple_t *dict_index_build_data_tuple( |
| 831 |
dict_index_t *index, /*!< in: index */ |
| 832 |
rec_t *rec, /*!< in: record for which to build data tuple */ |
| 833 |
ulint n_fields, /*!< in: number of data fields */ |
| 834 |
mem_heap_t *heap); /*!< in: memory heap where tuple created */ |
| 835 |
/** Gets the space id of the root of the index tree. |
| 836 |
@return space id */ |
| 837 |
[[nodiscard]] static inline space_id_t dict_index_get_space( |
| 838 |
const dict_index_t *index); /*!< in: index */ |
| 839 |
|
| 840 |
/** Sets the space id of the root of the index tree. |
| 841 |
@param[in,out] index index |
| 842 |
@param[in] space space id */ |
| 843 |
static inline void dict_index_set_space(dict_index_t *index, space_id_t space); |
| 844 |
|
| 845 |
/** Gets the page number of the root of the index tree. |
| 846 |
@return page number */ |
| 847 |
[[nodiscard]] static inline page_no_t dict_index_get_page( |
| 848 |
const dict_index_t *tree); /*!< in: index */ |
| 849 |
/** Gets the read-write lock of the index tree. |
| 850 |
@return read-write lock */ |
| 851 |
[[nodiscard]] static inline rw_lock_t *dict_index_get_lock( |
| 852 |
dict_index_t *index); /*!< in: index */ |
| 853 |
/** Returns free space reserved for future updates of records. This is |
| 854 |
relevant only in the case of many consecutive inserts, as updates |
| 855 |
which make the records bigger might fragment the index. |
| 856 |
@return number of free bytes on page, reserved for updates */ |
| 857 |
static inline ulint dict_index_get_space_reserve(void); |
| 858 |
|
| 859 |
/* Online index creation @{ */ |
| 860 |
/** Gets the status of online index creation. |
| 861 |
@return the status */ |
| 862 |
[[nodiscard]] static inline enum online_index_status |
| 863 |
dict_index_get_online_status( |
| 864 |
const dict_index_t *index); /*!< in: secondary index */ |
| 865 |
|
| 866 |
/** Sets the status of online index creation. |
| 867 |
@param[in,out] index index |
| 868 |
@param[in] status status */ |
| 869 |
static inline void dict_index_set_online_status( |
| 870 |
dict_index_t *index, enum online_index_status status); |
| 871 |
|
| 872 |
/** Determines if a secondary index is being or has been created online, |
| 873 |
or if the table is being rebuilt online, allowing concurrent modifications |
| 874 |
to the table. |
| 875 |
@retval true if the index is being or has been built online, or |
| 876 |
if this is a clustered index and the table is being or has been rebuilt online |
| 877 |
@retval false if the index has been created or the table has been |
| 878 |
rebuilt completely */ |
| 879 |
[[nodiscard]] static inline bool dict_index_is_online_ddl( |
| 880 |
const dict_index_t *index); /*!< in: index */ |
| 881 |
/** Calculates the minimum record length in an index. */ |
| 882 |
[[nodiscard]] ulint dict_index_calc_min_rec_len( |
| 883 |
const dict_index_t *index); /*!< in: index */ |
| 884 |
/** Reserves the dictionary system mutex for MySQL. */ |
| 885 |
void dict_mutex_enter_for_mysql(void); |
| 886 |
/** Releases the dictionary system mutex for MySQL. */ |
| 887 |
void dict_mutex_exit_for_mysql(void); |
| 888 |
|
| 889 |
#ifndef UNIV_HOTBACKUP |
| 890 |
/** Create a dict_table_t's stats latch or delay for lazy creation. |
| 891 |
This function is only called from either single threaded environment |
| 892 |
or from a thread that has not shared the table object with other threads. |
| 893 |
@param[in,out] table table whose stats latch to create |
| 894 |
@param[in] enabled if false then the latch is disabled |
| 895 |
and dict_table_stats_lock()/unlock() become noop on this table. */ |
| 896 |
void dict_table_stats_latch_create(dict_table_t *table, bool enabled); |
| 897 |
|
| 898 |
/** Destroy a dict_table_t's stats latch. |
| 899 |
This function is only called from either single threaded environment |
| 900 |
or from a thread that has not shared the table object with other threads. |
| 901 |
@param[in,out] table table whose stats latch to destroy */ |
| 902 |
void dict_table_stats_latch_destroy(dict_table_t *table); |
| 903 |
|
| 904 |
/** Lock the appropriate latch to protect a given table's statistics. |
| 905 |
@param[in] table table whose stats to lock |
| 906 |
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */ |
| 907 |
void dict_table_stats_lock(dict_table_t *table, ulint latch_mode); |
| 908 |
|
| 909 |
/** Unlock the latch that has been locked by dict_table_stats_lock(). |
| 910 |
@param[in] table table whose stats to unlock |
| 911 |
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */ |
| 912 |
void dict_table_stats_unlock(dict_table_t *table, ulint latch_mode); |
| 913 |
|
| 914 |
/** Checks if the database name in two table names is the same. |
| 915 |
@return true if same db name */ |
| 916 |
[[nodiscard]] bool dict_tables_have_same_db( |
| 917 |
const char *name1, /*!< in: table name in the |
| 918 |
form dbname '/' tablename */ |
| 919 |
const char *name2); /*!< in: table name in the |
| 920 |
form dbname '/' tablename */ |
| 921 |
/** Get an index by name. |
| 922 |
@param[in] table the table where to look for the index |
| 923 |
@param[in] name the index name to look for |
| 924 |
@param[in] committed true=search for committed, |
| 925 |
false=search for uncommitted |
| 926 |
@return index, NULL if does not exist */ |
| 927 |
[[nodiscard]] dict_index_t *dict_table_get_index_on_name(dict_table_t *table, |
| 928 |
const char *name, |
| 929 |
bool committed = true); |
| 930 |
/** Get an index by name. |
| 931 |
@param[in] table the table where to look for the index |
| 932 |
@param[in] name the index name to look for |
| 933 |
@param[in] committed true=search for committed, |
| 934 |
false=search for uncommitted |
| 935 |
@return index, NULL if does not exist */ |
| 936 |
inline const dict_index_t *dict_table_get_index_on_name( |
| 937 |
const dict_table_t *table, const char *name, bool committed = true) { |
| 938 |
return (dict_table_get_index_on_name(const_cast<dict_table_t *>(table), name, |
| 939 |
committed)); |
| 940 |
} |
| 941 |
|
| 942 |
/** Check whether a column exists in an FTS index. |
| 943 |
@param[in] indexes Vector containing only FTS indexes |
| 944 |
@param[in] col_no Column number to search for. |
| 945 |
@param[in] is_virtual Whether it is a virtual column. |
| 946 |
@return ULINT_UNDEFINED if no match else the offset within the vector. */ |
| 947 |
[[nodiscard]] static inline ulint dict_table_is_fts_column(ib_vector_t *indexes, |
| 948 |
ulint col_no, |
| 949 |
bool is_virtual); |
| 950 |
/** Prevent table eviction by moving a table to the non-LRU list from the |
| 951 |
LRU list if it is not already there. */ |
| 952 |
static inline void dict_table_prevent_eviction( |
| 953 |
dict_table_t *table); /*!< in: table to prevent eviction */ |
| 954 |
|
| 955 |
/** Allow the table to be evicted by moving a table to the LRU list from |
| 956 |
the non-LRU list if it is not already there. |
| 957 |
@param[in] table InnoDB table object can be evicted */ |
| 958 |
static inline void dict_table_allow_eviction(dict_table_t *table); |
| 959 |
|
| 960 |
/** Move this table to non-LRU list for DDL operations if it's |
| 961 |
currently not there. This also prevents later opening table via DD objects, |
| 962 |
when the table name in InnoDB doesn't match with DD object. |
| 963 |
@param[in,out] table Table to put in non-LRU list */ |
| 964 |
static inline void dict_table_ddl_acquire(dict_table_t *table); |
| 965 |
|
| 966 |
/** Move this table to LRU list after DDL operations if it was moved |
| 967 |
to non-LRU list |
| 968 |
@param[in,out] table Table to put in LRU list */ |
| 969 |
static inline void dict_table_ddl_release(dict_table_t *table); |
| 970 |
|
| 971 |
/** Move a table to the non LRU end of the LRU list. */ |
| 972 |
void dict_table_move_from_lru_to_non_lru( |
| 973 |
dict_table_t *table); /*!< in: table to move from LRU to non-LRU */ |
| 974 |
|
| 975 |
/** Move a table to the LRU end from the non LRU list. |
| 976 |
@param[in] table InnoDB table object */ |
| 977 |
void dict_table_move_from_non_lru_to_lru(dict_table_t *table); |
| 978 |
|
| 979 |
/** Move to the most recently used segment of the LRU list. */ |
| 980 |
void dict_move_to_mru(dict_table_t *table); /*!< in: table to move to MRU */ |
| 981 |
|
| 982 |
/** Maximum number of columns in a foreign key constraint. Please Note MySQL |
| 983 |
has a much lower limit on the number of columns allowed in a foreign key |
| 984 |
constraint */ |
| 985 |
constexpr uint32_t MAX_NUM_FK_COLUMNS = 500; |
| 986 |
|
| 987 |
/* Buffers for storing detailed information about the latest foreign key |
| 988 |
and unique key errors */ |
| 989 |
extern FILE *dict_foreign_err_file; |
| 990 |
extern ib_mutex_t dict_foreign_err_mutex; /* mutex protecting the |
| 991 |
foreign key error messages */ |
| 992 |
#endif /* !UNIV_HOTBACKUP */ |
| 993 |
|
| 994 |
/** the dictionary system */ |
| 995 |
extern dict_sys_t *dict_sys; |
| 996 |
#ifndef UNIV_HOTBACKUP |
| 997 |
/** the data dictionary rw-latch protecting dict_sys */ |
| 998 |
extern rw_lock_t *dict_operation_lock; |
| 999 |
|
| 1000 |
/** Forward declaration */ |
| 1001 |
class DDTableBuffer; |
| 1002 |
#endif /* !UNIV_HOTBACKUP */ |
| 1003 |
struct dict_persist_t; |
| 1004 |
|
| 1005 |
/** the dictionary persisting structure */ |
| 1006 |
extern dict_persist_t *dict_persist; |
| 1007 |
|
| 1008 |
/* Dictionary system struct */ |
| 1009 |
struct dict_sys_t { |
| 1010 |
#ifndef UNIV_HOTBACKUP |
| 1011 |
DictSysMutex mutex; /*!< mutex protecting the data |
| 1012 |
dictionary; protects also the |
| 1013 |
disk-based dictionary system tables; |
| 1014 |
this mutex serializes CREATE TABLE |
| 1015 |
and DROP TABLE, as well as reading |
| 1016 |
the dictionary data for a table from |
| 1017 |
system tables */ |
| 1018 |
#endif /* !UNIV_HOTBACKUP */ |
| 1019 |
row_id_t row_id; /*!< the next row id to assign; |
| 1020 |
NOTE that at a checkpoint this |
| 1021 |
must be written to the dict system |
| 1022 |
header and flushed to a file; in |
| 1023 |
recovery this must be derived from |
| 1024 |
the log records */ |
| 1025 |
hash_table_t *table_hash; /*!< hash table of the tables, based |
| 1026 |
on name */ |
| 1027 |
hash_table_t *table_id_hash; /*!< hash table of the tables, based |
| 1028 |
on id */ |
| 1029 |
size_t size; /*!< varying space in bytes occupied |
| 1030 |
by the data dictionary table and |
| 1031 |
index objects */ |
| 1032 |
/** Handler to sys_* tables, they're only for upgrade */ |
| 1033 |
dict_table_t *sys_tables; /*!< SYS_TABLES table */ |
| 1034 |
dict_table_t *sys_columns; /*!< SYS_COLUMNS table */ |
| 1035 |
dict_table_t *sys_indexes; /*!< SYS_INDEXES table */ |
| 1036 |
dict_table_t *sys_fields; /*!< SYS_FIELDS table */ |
| 1037 |
dict_table_t *sys_virtual; /*!< SYS_VIRTUAL table */ |
| 1038 |
|
| 1039 |
/** Permanent handle to mysql.innodb_table_stats */ |
| 1040 |
dict_table_t *table_stats; |
| 1041 |
/** Permanent handle to mysql.innodb_index_stats */ |
| 1042 |
dict_table_t *index_stats; |
| 1043 |
/** Permanent handle to mysql.innodb_ddl_log */ |
| 1044 |
dict_table_t *ddl_log; |
| 1045 |
/** Permanent handle to mysql.innodb_dynamic_metadata */ |
| 1046 |
dict_table_t *dynamic_metadata; |
| 1047 |
using Table_LRU_list_base = UT_LIST_BASE_NODE_T(dict_table_t, table_LRU); |
| 1048 |
|
| 1049 |
/** List of tables that can be evicted from the cache */ |
| 1050 |
Table_LRU_list_base table_LRU; |
| 1051 |
/** List of tables that can't be evicted from the cache */ |
| 1052 |
Table_LRU_list_base table_non_LRU; |
| 1053 |
|
| 1054 |
/** Iterate each table. |
| 1055 |
@tparam Functor visitor |
| 1056 |
@param[in,out] functor to be invoked on each table */ |
| 1057 |
template <typename Functor> |
| 1058 |
void for_each_table(Functor &functor) { |
| 1059 |
mutex_enter(&mutex); |
| 1060 |
|
| 1061 |
hash_table_t *hash = table_id_hash; |
| 1062 |
const auto n_cells = hash->get_n_cells(); |
| 1063 |
for (ulint i = 0; i < n_cells; i++) { |
| 1064 |
for (dict_table_t *table = |
| 1065 |
static_cast<dict_table_t *>(hash_get_first(hash, i)); |
| 1066 |
table; |
| 1067 |
table = static_cast<dict_table_t *>(HASH_GET_NEXT(id_hash, table))) { |
| 1068 |
functor(table); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
mutex_exit(&mutex); |
| 1073 |
} |
| 1074 |
|
| 1075 |
/** Check if a tablespace id is a reserved tablespace ID |
| 1076 |
@param[in] space tablespace id to check |
| 1077 |
@return true if a reserved tablespace id, otherwise false */ |
| 1078 |
static bool is_reserved(space_id_t space) { |
| 1079 |
return (space >= dict_sys_t::s_reserved_space_id); |
| 1080 |
} |
| 1081 |
|
| 1082 |
/** Set of ids of DD tables */ |
| 1083 |
static std::set<dd::Object_id> s_dd_table_ids; |
| 1084 |
|
| 1085 |
/** Check if a table is hardcoded. it only includes the dd tables |
| 1086 |
@param[in] id table ID |
| 1087 |
@retval true if the table is a persistent hard-coded table |
| 1088 |
(dict_table_t::is_temporary() will not hold) |
| 1089 |
@retval false if the table is not hard-coded |
| 1090 |
(it can be persistent or temporary) */ |
| 1091 |
static bool is_dd_table_id(table_id_t id) { |
| 1092 |
return (s_dd_table_ids.find(id) != s_dd_table_ids.end()); |
| 1093 |
} |
| 1094 |
|
| 1095 |
/** The first ID of the redo log pseudo-tablespace */ |
| 1096 |
static constexpr space_id_t s_log_space_id = 0xFFFFFFF0UL; |
| 1097 |
|
| 1098 |
/** Use maximum UINT value to indicate invalid space ID. */ |
| 1099 |
static constexpr space_id_t s_invalid_space_id = 0xFFFFFFFF; |
| 1100 |
|
| 1101 |
/** The data dictionary tablespace ID. */ |
| 1102 |
static constexpr space_id_t s_dict_space_id = 0xFFFFFFFE; |
| 1103 |
|
| 1104 |
/** The innodb_temporary tablespace ID. */ |
| 1105 |
static constexpr space_id_t s_temp_space_id = 0xFFFFFFFD; |
| 1106 |
|
| 1107 |
/** The number of space IDs dedicated to each undo tablespace */ |
| 1108 |
static constexpr space_id_t s_undo_space_id_range = 400000; |
| 1109 |
|
| 1110 |
/** The lowest undo tablespace ID. */ |
| 1111 |
static constexpr space_id_t s_min_undo_space_id = |
| 1112 |
s_log_space_id - (FSP_MAX_UNDO_TABLESPACES * s_undo_space_id_range); |
| 1113 |
|
| 1114 |
/** The highest undo tablespace ID. */ |
| 1115 |
static constexpr space_id_t s_max_undo_space_id = s_log_space_id - 1; |
| 1116 |
|
| 1117 |
/** Start space_ids for temporary tablespaces. */ |
| 1118 |
static constexpr space_id_t s_max_temp_space_id = s_min_undo_space_id - 1; |
| 1119 |
|
| 1120 |
/** The number of space IDs dedicated to temporary tablespaces */ |
| 1121 |
static constexpr space_id_t s_temp_space_id_range = 400000; |
| 1122 |
|
| 1123 |
/** Lowest temporary general space id */ |
| 1124 |
static constexpr space_id_t s_min_temp_space_id = |
| 1125 |
s_min_undo_space_id - s_temp_space_id_range; |
| 1126 |
|
| 1127 |
/** The first reserved tablespace ID */ |
| 1128 |
static constexpr space_id_t s_reserved_space_id = s_min_temp_space_id; |
| 1129 |
|
| 1130 |
/** The dd::Tablespace::id of the dictionary tablespace. */ |
| 1131 |
static constexpr dd::Object_id s_dd_dict_space_id = 1; |
| 1132 |
|
| 1133 |
/** The dd::Tablespace::id of innodb_system. */ |
| 1134 |
static constexpr dd::Object_id s_dd_sys_space_id = 2; |
| 1135 |
|
| 1136 |
/** The dd::Tablespace::id of innodb_temporary. */ |
| 1137 |
static constexpr dd::Object_id s_dd_temp_space_id = 3; |
| 1138 |
|
| 1139 |
/** The name of the data dictionary tablespace. */ |
| 1140 |
static const char *s_dd_space_name; |
| 1141 |
|
| 1142 |
/** The file name of the data dictionary tablespace. */ |
| 1143 |
static const char *s_dd_space_file_name; |
| 1144 |
|
| 1145 |
/** The name of the hard-coded system tablespace. */ |
| 1146 |
static const char *s_sys_space_name; |
| 1147 |
|
| 1148 |
/** The name of the predefined temporary tablespace. */ |
| 1149 |
static const char *s_temp_space_name; |
| 1150 |
|
| 1151 |
/** The file name of the predefined temporary tablespace. */ |
| 1152 |
static const char *s_temp_space_file_name; |
| 1153 |
|
| 1154 |
/** The hard-coded tablespace name innodb_file_per_table. */ |
| 1155 |
static const char *s_file_per_table_name; |
| 1156 |
|
| 1157 |
/** These two undo tablespaces cannot be dropped. */ |
| 1158 |
static const char *s_default_undo_space_name_1; |
| 1159 |
static const char *s_default_undo_space_name_2; |
| 1160 |
|
| 1161 |
/** The table ID of mysql.innodb_dynamic_metadata */ |
| 1162 |
static constexpr table_id_t s_dynamic_meta_table_id = 2; |
| 1163 |
|
| 1164 |
/** The clustered index ID of mysql.innodb_dynamic_metadata */ |
| 1165 |
static constexpr space_index_t s_dynamic_meta_index_id = 2; |
| 1166 |
}; |
| 1167 |
|
| 1168 |
/** Structure for persisting dynamic metadata of data dictionary */ |
| 1169 |
struct dict_persist_t { |
| 1170 |
#ifndef UNIV_HOTBACKUP |
| 1171 |
/** Write dynamic metadata to DD buffer table immediately when such data is |
| 1172 |
generated. By default, the metadata is first written to redo log and then to |
| 1173 |
the DD buffer table during checkpoint. This is going to hurt auto increment |
| 1174 |
performance and Currently enabled by clone for short time to eliminate |
| 1175 |
dependency with dynamic metadata recovered from redo log. */ |
| 1176 |
class Enable_immediate { |
| 1177 |
public: |
| 1178 |
/** Constructor to immediate persisting mode. |
| 1179 |
@param[in,out] persister dictionary persister */ |
| 1180 |
Enable_immediate(dict_persist_t *persister); |
| 1181 |
|
| 1182 |
/** Destructor to switch back to default mode. */ |
| 1183 |
~Enable_immediate(); |
| 1184 |
|
| 1185 |
/** Disable copy construction */ |
| 1186 |
Enable_immediate(Enable_immediate const &) = delete; |
| 1187 |
|
| 1188 |
/** Disable assignment */ |
| 1189 |
Enable_immediate &operator=(Enable_immediate const &) = delete; |
| 1190 |
|
| 1191 |
private: |
| 1192 |
/** Dictionary persister */ |
| 1193 |
dict_persist_t *m_persister; |
| 1194 |
}; |
| 1195 |
|
| 1196 |
/** @return true if need to write dynamic metadata to DD buffer table |
| 1197 |
immediately after logging. */ |
| 1198 |
bool check_persist_immediately() const { |
| 1199 |
return m_persist_immediately.load(); |
| 1200 |
} |
| 1201 |
|
| 1202 |
/** List of tables whose dirty_status are marked as METADATA_DIRTY, |
| 1203 |
or METADATA_BUFFERED. It's protected by the mutex */ |
| 1204 |
UT_LIST_BASE_NODE_T(dict_table_t, dirty_dict_tables) |
| 1205 |
dirty_dict_tables; |
| 1206 |
#endif |
| 1207 |
|
| 1208 |
/** Mutex to protect data in this structure, also the |
| 1209 |
dict_table_t::dirty_status and |
| 1210 |
dict_table_t::in_dirty_dict_tables_list |
| 1211 |
This mutex should be low-level one so that it can be used widely |
| 1212 |
when necessary, so its level had to be above SYNC_LOG. However, |
| 1213 |
after this mutex, persister may have to access B-tree and require |
| 1214 |
tree latch, the latch level of this mutex then has to be right |
| 1215 |
before the SYNC_INDEX_TREE. */ |
| 1216 |
ib_mutex_t mutex; |
| 1217 |
|
| 1218 |
/** Number of the tables which are of status METADATA_DIRTY. |
| 1219 |
It's protected by the mutex */ |
| 1220 |
std::atomic<uint32_t> num_dirty_tables; |
| 1221 |
|
| 1222 |
/** If set, dynamic metadata is saved to DD buffer table immediately. |
| 1223 |
Currently we consider only auto increment PM_TABLE_AUTO_INC. Corrupt |
| 1224 |
index PM_INDEX_CORRUPTED is not needed to be saved immediately. */ |
| 1225 |
std::atomic<bool> m_persist_immediately; |
| 1226 |
|
| 1227 |
#ifndef UNIV_HOTBACKUP |
| 1228 |
/** DDTableBuffer table for persistent dynamic metadata */ |
| 1229 |
DDTableBuffer *table_buffer; |
| 1230 |
#endif /* !UNIV_HOTBACKUP */ |
| 1231 |
|
| 1232 |
/** Collection of instances to persist dynamic metadata */ |
| 1233 |
Persisters *persisters; |
| 1234 |
}; |
| 1235 |
|
| 1236 |
#ifndef UNIV_HOTBACKUP |
| 1237 |
/** dummy index for ROW_FORMAT=REDUNDANT supremum and infimum records */ |
| 1238 |
extern dict_index_t *dict_ind_redundant; |
| 1239 |
|
| 1240 |
/** Inits dict_ind_redundant. */ |
| 1241 |
void dict_ind_init(void); |
| 1242 |
|
| 1243 |
/** Converts a database and table name from filesystem encoding (e.g. |
| 1244 |
"@code d@i1b/a@q1b@1Kc @endcode", same format as used in dict_table_t::name) |
| 1245 |
in two strings in UTF8MB3 encoding (e.g. dцb and aюbØc). The output buffers must |
| 1246 |
be at least MAX_DB_UTF8MB3_LEN and MAX_TABLE_UTF8MB3_LEN bytes. |
| 1247 |
@param[in] db_and_table database and table names, |
| 1248 |
e.g. "@code d@i1b/a@q1b@1Kc @endcode" |
| 1249 |
@param[out] db_utf8mb3 database name, e.g. dцb |
| 1250 |
@param[in] db_utf8mb3_size db_utf8mb3 size |
| 1251 |
@param[out] table_utf8mb3 table name, e.g. aюbØc |
| 1252 |
@param[in] table_utf8mb3_size table_utf8mb3 size */ |
| 1253 |
void dict_fs2utf8(const char *db_and_table, char *db_utf8mb3, |
| 1254 |
size_t db_utf8mb3_size, char *table_utf8mb3, |
| 1255 |
size_t table_utf8mb3_size); |
| 1256 |
|
| 1257 |
/** Resize the hash tables based on the current buffer pool size. */ |
| 1258 |
void dict_resize(); |
| 1259 |
|
| 1260 |
/** Wrapper for the mysql.innodb_dynamic_metadata used to buffer the persistent |
| 1261 |
dynamic metadata. |
| 1262 |
This should be a table with only clustered index, no delete-marked records, |
| 1263 |
no locking, no undo logging, no purge, no adaptive hash index. |
| 1264 |
We should always use low level btr functions to access and modify the table. |
| 1265 |
Accessing this table should be protected by dict_sys->mutex */ |
| 1266 |
class DDTableBuffer { |
| 1267 |
public: |
| 1268 |
/** Default constructor */ |
| 1269 |
DDTableBuffer(); |
| 1270 |
|
| 1271 |
/** Destructor */ |
| 1272 |
~DDTableBuffer(); |
| 1273 |
|
| 1274 |
/** Replace the dynamic metadata for a specific table |
| 1275 |
@param[in] id table id |
| 1276 |
@param[in] version table dynamic metadata version |
| 1277 |
@param[in] metadata the metadata we want to replace |
| 1278 |
@param[in] len the metadata length |
| 1279 |
@return DB_SUCCESS or error code */ |
| 1280 |
dberr_t replace(table_id_t id, uint64_t version, const byte *metadata, |
| 1281 |
size_t len); |
| 1282 |
|
| 1283 |
/** Remove the whole row for a specific table |
| 1284 |
@param[in] id table id |
| 1285 |
@return DB_SUCCESS or error code */ |
| 1286 |
dberr_t remove(table_id_t id); |
| 1287 |
|
| 1288 |
/** Truncate the table. We can call it after all the dynamic |
| 1289 |
metadata has been written back to DD table */ |
| 1290 |
void truncate(void); |
| 1291 |
|
| 1292 |
/** Get the buffered metadata for a specific table, the caller |
| 1293 |
has to delete the returned std::string object by ut::delete_ |
| 1294 |
@param[in] id table id |
| 1295 |
@param[out] version table dynamic metadata version |
| 1296 |
@return the metadata saved in a vector object, if nothing, the |
| 1297 |
vector would be empty */ |
| 1298 |
std::vector<byte> get(table_id_t id, uint64_t *version); |
| 1299 |
|
| 1300 |
private: |
| 1301 |
/** Initialize m_index, the in-memory clustered index of the table |
| 1302 |
and two tuples used in this class */ |
| 1303 |
void init(); |
| 1304 |
|
| 1305 |
/** Open the mysql.innodb_dynamic_metadata when DD is not fully up */ |
| 1306 |
void open(); |
| 1307 |
|
| 1308 |
/** Create the search and replace tuples */ |
| 1309 |
void create_tuples(); |
| 1310 |
|
| 1311 |
/** Initialize the id field of tuple |
| 1312 |
@param[out] tuple the tuple to be initialized |
| 1313 |
@param[in] id table id */ |
| 1314 |
void init_tuple_with_id(dtuple_t *tuple, table_id_t id); |
| 1315 |
|
| 1316 |
/** Free the things initialized in init() */ |
| 1317 |
void close(); |
| 1318 |
|
| 1319 |
/** Prepare for a update on METADATA field |
| 1320 |
@param[in] entry clustered index entry to replace rec |
| 1321 |
@param[in] rec clustered index record |
| 1322 |
@return update vector of differing fields without system columns, |
| 1323 |
or NULL if there isn't any different field */ |
| 1324 |
upd_t *update_set_metadata(const dtuple_t *entry, const rec_t *rec); |
| 1325 |
|
| 1326 |
private: |
| 1327 |
/** The clustered index of this system table */ |
| 1328 |
dict_index_t *m_index; |
| 1329 |
|
| 1330 |
/** The heap used for dynamic allocations, which should always |
| 1331 |
be freed before return */ |
| 1332 |
mem_heap_t *m_dynamic_heap; |
| 1333 |
|
| 1334 |
/** The heap used during replace() operation, which should always |
| 1335 |
be freed before return */ |
| 1336 |
mem_heap_t *m_replace_heap; |
| 1337 |
|
| 1338 |
/** The heap used to create the search tuple and replace tuple */ |
| 1339 |
mem_heap_t *m_heap; |
| 1340 |
|
| 1341 |
/** The tuple used to search for specified table, it's protected |
| 1342 |
by dict_persist->mutex */ |
| 1343 |
dtuple_t *m_search_tuple; |
| 1344 |
|
| 1345 |
/** The tuple used to replace for specified table, it's protected |
| 1346 |
by dict_persist->mutex */ |
| 1347 |
dtuple_t *m_replace_tuple; |
| 1348 |
|
| 1349 |
private: |
| 1350 |
/** Column number of mysql.innodb_dynamic_metadata.table_id */ |
| 1351 |
static constexpr unsigned TABLE_ID_COL_NO = 0; |
| 1352 |
|
| 1353 |
/** Column number of mysql.innodb_dynamic_metadata.version */ |
| 1354 |
static constexpr unsigned VERSION_COL_NO = 1; |
| 1355 |
|
| 1356 |
/** Column number of mysql.innodb_dynamic_metadata.metadata */ |
| 1357 |
static constexpr unsigned METADATA_COL_NO = 2; |
| 1358 |
|
| 1359 |
/** Number of user columns */ |
| 1360 |
static constexpr unsigned N_USER_COLS = METADATA_COL_NO + 1; |
| 1361 |
|
| 1362 |
/** Number of columns */ |
| 1363 |
static constexpr unsigned N_COLS = N_USER_COLS + DATA_N_SYS_COLS; |
| 1364 |
|
| 1365 |
/** Clustered index field number of |
| 1366 |
mysql.innodb_dynamic_metadata.table_id */ |
| 1367 |
static constexpr unsigned TABLE_ID_FIELD_NO = TABLE_ID_COL_NO; |
| 1368 |
|
| 1369 |
/** Clustered index field number of |
| 1370 |
mysql.innodb_dynamic_metadata.version */ |
| 1371 |
static constexpr unsigned VERSION_FIELD_NO = VERSION_COL_NO + 2; |
| 1372 |
|
| 1373 |
/** Clustered index field number of |
| 1374 |
mysql.innodb_dynamic_metadata.metadata |
| 1375 |
Plusing 2 here skips the DATA_TRX_ID and DATA_ROLL_PTR fields */ |
| 1376 |
static constexpr unsigned METADATA_FIELD_NO = METADATA_COL_NO + 2; |
| 1377 |
|
| 1378 |
/** Number of fields in the clustered index */ |
| 1379 |
static constexpr unsigned N_FIELDS = METADATA_FIELD_NO + 1; |
| 1380 |
}; |
| 1381 |
|
| 1382 |
/** Mark the dirty_status of a table as METADATA_DIRTY, and add it to the |
| 1383 |
dirty_dict_tables list if necessary. |
| 1384 |
@param[in,out] table table */ |
| 1385 |
void dict_table_mark_dirty(dict_table_t *table); |
| 1386 |
#endif /* !UNIV_HOTBACKUP */ |
| 1387 |
|
| 1388 |
/** Flags an index corrupted in the data dictionary cache only. This |
| 1389 |
is used to mark a corrupted index when index's own dictionary |
| 1390 |
is corrupted, and we would force to load such index for repair purpose. |
| 1391 |
Besides, we have to write a redo log. |
| 1392 |
We don't want to hold dict_sys->mutex here, so that we can set index as |
| 1393 |
corrupted in some low-level functions. We would only set the flags from |
| 1394 |
not corrupted to corrupted when server is running, so it should be safe |
| 1395 |
to set it directly. |
| 1396 |
@param[in,out] index index, must not be NULL */ |
| 1397 |
void dict_set_corrupted(dict_index_t *index) UNIV_COLD; |
| 1398 |
|
| 1399 |
#ifndef UNIV_HOTBACKUP |
| 1400 |
|
| 1401 |
/** Check if there is any latest persistent dynamic metadata recorded |
| 1402 |
in DDTableBuffer table of the specific table. If so, read the metadata and |
| 1403 |
update the table object accordingly. It's used when loading table. |
| 1404 |
@param[in] table table object */ |
| 1405 |
void dict_table_load_dynamic_metadata(dict_table_t *table); |
| 1406 |
|
| 1407 |
/** Check if any table has any dirty persistent data, if so |
| 1408 |
write dirty persistent data of table to mysql.innodb_dynamic_metadata |
| 1409 |
accordingly. */ |
| 1410 |
void dict_persist_to_dd_table_buffer(); |
| 1411 |
|
| 1412 |
/** Sets merge_threshold in the SYS_INDEXES |
| 1413 |
@param[in,out] index index |
| 1414 |
@param[in] merge_threshold value to set */ |
| 1415 |
void dict_index_set_merge_threshold(dict_index_t *index, ulint merge_threshold); |
| 1416 |
|
| 1417 |
#ifdef UNIV_DEBUG |
| 1418 |
/** Sets merge_threshold for all indexes in dictionary cache for debug. |
| 1419 |
@param[in] merge_threshold_all value to set for all indexes */ |
| 1420 |
void dict_set_merge_threshold_all_debug(uint merge_threshold_all); |
| 1421 |
#endif /* UNIV_DEBUG */ |
| 1422 |
|
| 1423 |
/** Validate the table flags. |
| 1424 |
@param[in] flags Table flags |
| 1425 |
@return true if valid. */ |
| 1426 |
static inline bool dict_tf_is_valid(uint32_t flags); |
| 1427 |
|
| 1428 |
/** Validate both table flags and table flags2 and make sure they |
| 1429 |
are compatible. |
| 1430 |
@param[in] flags Table flags |
| 1431 |
@param[in] flags2 Table flags2 |
| 1432 |
@return true if valid. */ |
| 1433 |
static inline bool dict_tf2_is_valid(uint32_t flags, uint32_t flags2); |
| 1434 |
|
| 1435 |
/** Check if the tablespace for the table has been discarded. |
| 1436 |
@return true if the tablespace has been discarded. */ |
| 1437 |
[[nodiscard]] static inline bool dict_table_is_discarded( |
| 1438 |
const dict_table_t *table); /*!< in: table to check */ |
| 1439 |
|
| 1440 |
/** Check whether the table is DDTableBuffer. See class DDTableBuffer |
| 1441 |
@param[in] table table to check |
| 1442 |
@return true if this is a DDTableBuffer table. */ |
| 1443 |
static inline bool dict_table_is_table_buffer(const dict_table_t *table); |
| 1444 |
|
| 1445 |
/** Check if the table is in a shared tablespace (System or General). |
| 1446 |
@param[in] table table to check |
| 1447 |
@return true if table is a shared tablespace, false if not. */ |
| 1448 |
[[nodiscard]] static inline bool dict_table_in_shared_tablespace( |
| 1449 |
const dict_table_t *table); |
| 1450 |
|
| 1451 |
/** Check whether locking is disabled for this table. |
| 1452 |
Currently this is done for intrinsic table as their visibility is limited |
| 1453 |
to the connection and the DDTableBuffer as it's protected by |
| 1454 |
dict_persist->mutex. |
| 1455 |
|
| 1456 |
@param[in] table table to check |
| 1457 |
@return true if locking is disabled. */ |
| 1458 |
[[nodiscard]] static inline bool dict_table_is_locking_disabled( |
| 1459 |
const dict_table_t *table); |
| 1460 |
|
| 1461 |
/** Turn-off redo-logging if temporary table. |
| 1462 |
@param[in] table Table to check |
| 1463 |
@param[out] mtr Mini-transaction */ |
| 1464 |
static inline void dict_disable_redo_if_temporary(const dict_table_t *table, |
| 1465 |
mtr_t *mtr); |
| 1466 |
|
| 1467 |
/** Get table session row-id and increment the row-id counter for next use. |
| 1468 |
@param[in,out] table table handler |
| 1469 |
@return next table local row-id. */ |
| 1470 |
static inline row_id_t dict_table_get_next_table_sess_row_id( |
| 1471 |
dict_table_t *table); |
| 1472 |
|
| 1473 |
/** Get table session trx-id and increment the trx-id counter for next use. |
| 1474 |
@param[in,out] table table handler |
| 1475 |
@return next table local trx-id. */ |
| 1476 |
static inline trx_id_t dict_table_get_next_table_sess_trx_id( |
| 1477 |
dict_table_t *table); |
| 1478 |
|
| 1479 |
/** Get current session trx-id. |
| 1480 |
@param[in] table table handler |
| 1481 |
@return table local trx-id. */ |
| 1482 |
static inline trx_id_t dict_table_get_curr_table_sess_trx_id( |
| 1483 |
const dict_table_t *table); |
| 1484 |
|
| 1485 |
/** This function should be called whenever a page is successfully |
| 1486 |
compressed. Updates the compression padding information. */ |
| 1487 |
void dict_index_zip_success( |
| 1488 |
dict_index_t *index); /*!< in/out: index to be updated. */ |
| 1489 |
/** This function should be called whenever a page compression attempt |
| 1490 |
fails. Updates the compression padding information. */ |
| 1491 |
void dict_index_zip_failure( |
| 1492 |
dict_index_t *index); /*!< in/out: index to be updated. */ |
| 1493 |
/** Return the optimal page size, for which page will likely compress. |
| 1494 |
@return page size beyond which page may not compress*/ |
| 1495 |
[[nodiscard]] ulint dict_index_zip_pad_optimal_page_size( |
| 1496 |
dict_index_t *index); /*!< in: index for which page size |
| 1497 |
is requested */ |
| 1498 |
/** Convert table flag to row format string. |
| 1499 |
@return row format name */ |
| 1500 |
const char *dict_tf_to_row_format_string( |
| 1501 |
uint32_t table_flag); /*!< in: row format setting */ |
| 1502 |
/** Return maximum size of the node pointer record. |
| 1503 |
@return maximum size of the record in bytes */ |
| 1504 |
[[nodiscard]] ulint dict_index_node_ptr_max_size( |
| 1505 |
const dict_index_t *index); /*!< in: index */ |
| 1506 |
|
| 1507 |
/** Get index by first field of the index. |
| 1508 |
@param[in] table table |
| 1509 |
@param[in] col_index position of column in table |
| 1510 |
@return index which is having first field matches with the field present in |
| 1511 |
field_index position of table */ |
| 1512 |
static inline dict_index_t *dict_table_get_index_on_first_col( |
| 1513 |
dict_table_t *table, ulint col_index); |
| 1514 |
#endif /* !UNIV_HOTBACKUP */ |
| 1515 |
|
| 1516 |
/** encode number of columns and number of virtual columns in one |
| 1517 |
4 bytes value. We could do this because the number of columns in |
| 1518 |
InnoDB is limited to 1017 |
| 1519 |
@param[in] n_col number of non-virtual column |
| 1520 |
@param[in] n_v_col number of virtual column |
| 1521 |
@return encoded value */ |
| 1522 |
static inline ulint dict_table_encode_n_col(ulint n_col, ulint n_v_col); |
| 1523 |
|
| 1524 |
/** Decode number of virtual and non-virtual columns in one 4 bytes value. |
| 1525 |
@param[in] encoded encoded value |
| 1526 |
@param[in,out] n_col number of non-virtual column |
| 1527 |
@param[in,out] n_v_col number of virtual column */ |
| 1528 |
static inline void dict_table_decode_n_col(uint32_t encoded, uint32_t *n_col, |
| 1529 |
uint32_t *n_v_col); |
| 1530 |
|
| 1531 |
/** Free the virtual column template |
| 1532 |
@param[in,out] vc_templ virtual column template */ |
| 1533 |
static inline void dict_free_vc_templ(dict_vcol_templ_t *vc_templ); |
| 1534 |
|
| 1535 |
/** Returns a virtual column's name according to its original |
| 1536 |
MySQL table position. |
| 1537 |
@param[in] table target table |
| 1538 |
@param[in] col_nr column number (nth column in the table) |
| 1539 |
@return column name. */ |
| 1540 |
const char *dict_table_get_v_col_name_mysql(const dict_table_t *table, |
| 1541 |
ulint col_nr); |
| 1542 |
|
| 1543 |
/** Check whether the table have virtual index. |
| 1544 |
@param[in] table InnoDB table |
| 1545 |
@return true if the table have virtual index, false otherwise. */ |
| 1546 |
static inline bool dict_table_have_virtual_index(dict_table_t *table); |
| 1547 |
|
| 1548 |
/** Retrieve in-memory index for SDI table. |
| 1549 |
@param[in] tablespace_id innodb tablespace ID |
| 1550 |
@return dict_index_t structure or NULL*/ |
| 1551 |
dict_index_t *dict_sdi_get_index(space_id_t tablespace_id); |
| 1552 |
|
| 1553 |
/** Retrieve in-memory table object for SDI table. |
| 1554 |
@param[in] tablespace_id innodb tablespace ID |
| 1555 |
@param[in] dict_locked true if dict_sys mutex is acquired |
| 1556 |
@param[in] is_create true when creating SDI Index |
| 1557 |
@return dict_table_t structure */ |
| 1558 |
dict_table_t *dict_sdi_get_table(space_id_t tablespace_id, bool dict_locked, |
| 1559 |
bool is_create); |
| 1560 |
|
| 1561 |
/** Remove the SDI table from table cache. |
| 1562 |
@param[in] space_id InnoDB tablespace ID |
| 1563 |
@param[in] sdi_table SDI table |
| 1564 |
@param[in] dict_locked true if dict_sys mutex acquired */ |
| 1565 |
void dict_sdi_remove_from_cache(space_id_t space_id, dict_table_t *sdi_table, |
| 1566 |
bool dict_locked); |
| 1567 |
|
| 1568 |
/** Check if the index is SDI index |
| 1569 |
@param[in] index in-memory index structure |
| 1570 |
@return true if index is SDI index else false */ |
| 1571 |
static inline bool dict_index_is_sdi(const dict_index_t *index); |
| 1572 |
|
| 1573 |
/** Check if an table id belongs SDI table |
| 1574 |
@param[in] table_id dict_table_t id |
| 1575 |
@return true if table_id is SDI table_id else false */ |
| 1576 |
static inline bool dict_table_is_sdi(uint64_t table_id); |
| 1577 |
|
| 1578 |
/** Close SDI table. |
| 1579 |
@param[in] table the in-memory SDI table object */ |
| 1580 |
void dict_sdi_close_table(dict_table_t *table); |
| 1581 |
|
| 1582 |
/** Acquire exclusive MDL on SDI tables. This is acquired to |
| 1583 |
prevent concurrent DROP table/tablespace when there is purge |
| 1584 |
happening on SDI table records. Purge will acquire shared |
| 1585 |
MDL on SDI table. |
| 1586 |
|
| 1587 |
Exclusive MDL is transactional(released on trx commit). So |
| 1588 |
for successful acquisition, there should be a valid thd associated |
| 1589 |
with a trx. |
| 1590 |
|
| 1591 |
Acquisition order of SDI MDL and SDI table has to be in the same |
| 1592 |
order: |
| 1593 |
|
| 1594 |
1. dd_sdi_acquire_exclusive_mdl |
| 1595 |
2. row_drop_table_from_cache()/innodb_drop_tablespace() |
| 1596 |
->dict_sdi_remove_from_cache()->dd_table_open_on_id() |
| 1597 |
|
| 1598 |
In purge: |
| 1599 |
|
| 1600 |
1. dd_sdi_acquire_shared_mdl |
| 1601 |
2. dd_table_open_on_id() |
| 1602 |
|
| 1603 |
@param[in] thd server thread instance |
| 1604 |
@param[in] space_id InnoDB tablespace id |
| 1605 |
@param[in,out] sdi_mdl MDL ticket on SDI table |
| 1606 |
@retval DB_SUCESS on success |
| 1607 |
@retval DB_LOCK_WAIT_TIMEOUT on error */ |
| 1608 |
dberr_t dd_sdi_acquire_exclusive_mdl(THD *thd, space_id_t space_id, |
| 1609 |
MDL_ticket **sdi_mdl); |
| 1610 |
|
| 1611 |
/** Acquire shared MDL on SDI tables. This is acquired by purge to |
| 1612 |
prevent concurrent DROP table/tablespace. |
| 1613 |
DROP table/tablespace will acquire exclusive MDL on SDI table |
| 1614 |
|
| 1615 |
Acquisition order of SDI MDL and SDI table has to be in same |
| 1616 |
order: |
| 1617 |
|
| 1618 |
1. dd_sdi_acquire_exclusive_mdl |
| 1619 |
2. row_drop_table_from_cache()/innodb_drop_tablespace() |
| 1620 |
->dict_sdi_remove_from_cache()->dd_table_open_on_id() |
| 1621 |
|
| 1622 |
In purge: |
| 1623 |
|
| 1624 |
1. dd_sdi_acquire_shared_mdl |
| 1625 |
2. dd_table_open_on_id() |
| 1626 |
|
| 1627 |
MDL should be released by caller |
| 1628 |
@param[in] thd server thread instance |
| 1629 |
@param[in] space_id InnoDB tablespace id |
| 1630 |
@param[in,out] sdi_mdl MDL ticket on SDI table |
| 1631 |
@retval DB_SUCESS on success |
| 1632 |
@retval DB_LOCK_WAIT_TIMEOUT on error */ |
| 1633 |
dberr_t dd_sdi_acquire_shared_mdl(THD *thd, space_id_t space_id, |
| 1634 |
MDL_ticket **sdi_mdl); |
| 1635 |
|
| 1636 |
/** Check whether the dict_table_t is a partition. |
| 1637 |
A partitioned table on the SQL level is composed of InnoDB tables, |
| 1638 |
where each InnoDB table is a [sub]partition including its secondary indexes |
| 1639 |
which belongs to the partition. |
| 1640 |
@param[in] table Table to check. |
| 1641 |
@return true if the dict_table_t is a partition else false. */ |
| 1642 |
static inline bool dict_table_is_partition(const dict_table_t *table); |
| 1643 |
|
| 1644 |
/** Allocate memory for intrinsic cache elements in the index |
| 1645 |
@param[in] index index object */ |
| 1646 |
static inline void dict_allocate_mem_intrinsic_cache(dict_index_t *index); |
| 1647 |
|
| 1648 |
/** Evict all tables that are loaded for applying purge. |
| 1649 |
Since we move the offset of all table ids during upgrade, |
| 1650 |
these tables cannot exist in cache. Also change table_ids |
| 1651 |
of SYS_* tables if they are upgraded from earlier versions */ |
| 1652 |
void dict_upgrade_evict_tables_cache(); |
| 1653 |
|
| 1654 |
/** @return true if table is InnoDB SYS_* table |
| 1655 |
@param[in] table_id table id */ |
| 1656 |
bool dict_table_is_system(table_id_t table_id); |
| 1657 |
|
| 1658 |
/** Build the table_id array of SYS_* tables. This |
| 1659 |
array is used to determine if a table is InnoDB SYSTEM |
| 1660 |
table or not. |
| 1661 |
@return true if successful, false otherwise */ |
| 1662 |
bool dict_sys_table_id_build(); |
| 1663 |
|
| 1664 |
/** Change the table_id of SYS_* tables if they have been created after |
| 1665 |
an earlier upgrade. This will update the table_id by adding DICT_MAX_DD_TABLES |
| 1666 |
*/ |
| 1667 |
void dict_table_change_id_sys_tables(); |
| 1668 |
|
| 1669 |
/** Get the tablespace data directory if set, otherwise empty string. |
| 1670 |
@return the data directory */ |
| 1671 |
[[nodiscard]] std::string dict_table_get_datadir(const dict_table_t *table); |
| 1672 |
|
| 1673 |
/** Set the compression type for the tablespace of a table |
| 1674 |
@param[in] table The table that should be compressed |
| 1675 |
@param[in] algorithm Text representation of the algorithm |
| 1676 |
@param[in] is_import_op True if it's being set during import operation |
| 1677 |
@return DB_SUCCESS or error code */ |
| 1678 |
[[nodiscard]] dberr_t dict_set_compression(dict_table_t *table, |
| 1679 |
const char *algorithm, |
| 1680 |
bool is_import_op); |
| 1681 |
|
| 1682 |
/** @return true if all base column of virtual column is foreign key column |
| 1683 |
@param[in] vcol in-memory virtual column |
| 1684 |
@param[in] foreign in-memory Foreign key constraint */ |
| 1685 |
uint32_t dict_vcol_base_is_foreign_key(dict_v_col_t *vcol, |
| 1686 |
dict_foreign_t *foreign); |
| 1687 |
|
| 1688 |
/** Get maximum possible size needed for a field. |
| 1689 |
@param[in] table innodb table definition cache |
| 1690 |
@param[in] index index |
| 1691 |
@param[in] field field |
| 1692 |
@param[in,out] rec_max_size max record size calculated till now. It |
| 1693 |
will be increased with the length needed |
| 1694 |
for this field */ |
| 1695 |
void get_field_max_size(const dict_table_t *table, const dict_index_t *index, |
| 1696 |
const dict_field_t *field, size_t &rec_max_size); |
| 1697 |
|
| 1698 |
/** Get the maximum size of a record permissible on an index page. |
| 1699 |
@param[in] table innodb table definition cache |
| 1700 |
@param[in] index index |
| 1701 |
@param[out] page_rec_max maximum size of record on a leaf page |
| 1702 |
@param[out] page_ptr_max maximum size of record on non-leaf page */ |
| 1703 |
void get_permissible_max_size(const dict_table_t *table, |
| 1704 |
const dict_index_t *index, size_t &page_rec_max, |
| 1705 |
size_t &page_ptr_max); |
| 1706 |
|
| 1707 |
/** validate that maximum possible size of a row is within permissible limit. |
| 1708 |
@param[in] table innodb table definition cache |
| 1709 |
@param[in] index index |
| 1710 |
@param[in] page_rec_max maximum size of possible record on leaf page |
| 1711 |
@param[in] page_ptr_max maximum size of possible record on non-leaf page |
| 1712 |
@param[out] rec_max_size maximum size of record on page |
| 1713 |
@return true if max record size is within limit, false otherwise. */ |
| 1714 |
bool dict_index_validate_max_rec_size(const dict_table_t *table, |
| 1715 |
const dict_index_t *index, |
| 1716 |
const size_t page_rec_max, |
| 1717 |
const size_t page_ptr_max, |
| 1718 |
size_t &rec_max_size); |
| 1719 |
|
| 1720 |
#include "dict0dict.ic" |
| 1721 |
|
| 1722 |
#endif |