| 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/dict0mem.h |
| 30 |
Data dictionary memory object creation |
| 31 |
|
| 32 |
Created 1/8/1996 Heikki Tuuri |
| 33 |
*******************************************************/ |
| 34 |
|
| 35 |
#ifndef dict0mem_h |
| 36 |
#define dict0mem_h |
| 37 |
|
| 38 |
#include "sql/dd/object_id.h" |
| 39 |
#include "sql/dd/types/column.h" |
| 40 |
#include "univ.i" |
| 41 |
#if defined UNIV_COMPILE_TEST_FUNCS || defined UNIV_HOTBACKUP |
| 42 |
#include "sql/dd/types/spatial_reference_system.h" |
| 43 |
#endif /* UNIV_COMPILE_TEST_FUNCS || UNIV_HOTBACKUP */ |
| 44 |
#include "btr0types.h" |
| 45 |
#include "data0type.h" |
| 46 |
#include "dict0types.h" |
| 47 |
#include "mem0mem.h" |
| 48 |
#include "rem0types.h" |
| 49 |
#include "row0types.h" |
| 50 |
#ifndef UNIV_HOTBACKUP |
| 51 |
#include "lock0types.h" |
| 52 |
#include "que0types.h" |
| 53 |
#endif /* !UNIV_HOTBACKUP */ |
| 54 |
#include "hash0hash.h" |
| 55 |
#include "sync0rw.h" |
| 56 |
#include "trx0types.h" |
| 57 |
#include "ut0byte.h" |
| 58 |
#include "ut0mem.h" |
| 59 |
#include "ut0rnd.h" |
| 60 |
#ifndef UNIV_HOTBACKUP |
| 61 |
#include "fts0fts.h" |
| 62 |
#endif /* !UNIV_HOTBACKUP */ |
| 63 |
#include "buf0buf.h" |
| 64 |
#include "gis0type.h" |
| 65 |
#ifndef UNIV_HOTBACKUP |
| 66 |
#include "os0once.h" |
| 67 |
#endif /* !UNIV_HOTBACKUP */ |
| 68 |
#include "dict/mem.h" |
| 69 |
#include "ut0new.h" |
| 70 |
|
| 71 |
#include "sql/sql_const.h" /* MAX_KEY_LENGTH */ |
| 72 |
#include "sql/table.h" |
| 73 |
|
| 74 |
#include <algorithm> |
| 75 |
#include <iterator> |
| 76 |
#include <memory> /* std::unique_ptr */ |
| 77 |
#include <set> |
| 78 |
#include <string> |
| 79 |
#include <vector> |
| 80 |
|
| 81 |
/* Forward declaration. */ |
| 82 |
struct ib_rbt_t; |
| 83 |
|
| 84 |
/** Format of INSTANTLY DROPPED column names. */ |
| 85 |
constexpr char INSTANT_DROP_SUFFIX_8_0_29[] = "_dropped_v"; |
| 86 |
constexpr char INSTANT_DROP_PREFIX_8_0_32[] = "!hidden!_dropped_"; |
| 87 |
|
| 88 |
/** index/table name used while applying REDO logs during recovery */ |
| 89 |
constexpr char RECOVERY_INDEX_TABLE_NAME[] = "LOG_DUMMY"; |
| 90 |
|
| 91 |
/** Type flags of an index: OR'ing of the flags is allowed to define a |
| 92 |
combination of types */ |
| 93 |
/** @{ */ |
| 94 |
/** clustered index; for other than auto-generated clustered indexes, also |
| 95 |
DICT_UNIQUE will be set */ |
| 96 |
constexpr uint32_t DICT_CLUSTERED = 1; |
| 97 |
/** unique index */ |
| 98 |
constexpr uint32_t DICT_UNIQUE = 2; |
| 99 |
/** insert buffer tree */ |
| 100 |
constexpr uint32_t DICT_IBUF = 8; |
| 101 |
/** bit to store the corrupted flag in SYS_INDEXES.TYPE */ |
| 102 |
constexpr uint32_t DICT_CORRUPT = 16; |
| 103 |
/** FTS index; can't be combined with the other flags */ |
| 104 |
constexpr uint32_t DICT_FTS = 32; |
| 105 |
/** SPATIAL index; can't be combined with the other flags */ |
| 106 |
constexpr uint32_t DICT_SPATIAL = 64; |
| 107 |
/** Index on Virtual column */ |
| 108 |
constexpr uint32_t DICT_VIRTUAL = 128; |
| 109 |
/* Tablespace dictionary Index. Set only in in-memory index structure. */ |
| 110 |
constexpr uint32_t DICT_SDI = 256; |
| 111 |
/** Multi-value index */ |
| 112 |
constexpr uint32_t DICT_MULTI_VALUE = 512; |
| 113 |
|
| 114 |
/** number of bits used for SYS_INDEXES.TYPE */ |
| 115 |
constexpr uint32_t DICT_IT_BITS = 10; |
| 116 |
/** @} */ |
| 117 |
|
| 118 |
#if 0 /* not implemented, retained for history */ |
| 119 |
/** Types for a table object */ |
| 120 |
#define DICT_TABLE_ORDINARY 1 /*!< ordinary table */ |
| 121 |
#define DICT_TABLE_CLUSTER_MEMBER 2 |
| 122 |
#define DICT_TABLE_CLUSTER \ |
| 123 |
3 /* this means that the table is \ |
| 124 |
really a cluster definition */ |
| 125 |
#endif |
| 126 |
|
| 127 |
/* Table and tablespace flags are generally not used for the Antelope file |
| 128 |
format except for the low order bit, which is used differently depending on |
| 129 |
where the flags are stored. |
| 130 |
|
| 131 |
==================== Low order flags bit ========================= |
| 132 |
| REDUNDANT | COMPACT | COMPRESSED and DYNAMIC |
| 133 |
SYS_TABLES.TYPE | 1 | 1 | 1 |
| 134 |
dict_table_t::flags | 0 | 1 | 1 |
| 135 |
FSP_SPACE_FLAGS | 0 | 0 | 1 |
| 136 |
fil_space_t::flags | 0 | 0 | 1 |
| 137 |
|
| 138 |
Before the 5.1 plugin, SYS_TABLES.TYPE was always DICT_TABLE_ORDINARY (1) |
| 139 |
and the tablespace flags field was always 0. In the 5.1 plugin, these fields |
| 140 |
were repurposed to identify compressed and dynamic row formats. |
| 141 |
|
| 142 |
The following types and constants describe the flags found in dict_table_t |
| 143 |
and SYS_TABLES.TYPE. Similar flags found in fil_space_t and FSP_SPACE_FLAGS |
| 144 |
are described in fsp0fsp.h. */ |
| 145 |
|
| 146 |
/** @{ */ |
| 147 |
/** dict_table_t::flags bit 0 is equal to 0 if the row format = Redundant */ |
| 148 |
/** Redundant row format. */ |
| 149 |
constexpr uint32_t DICT_TF_REDUNDANT = 0; |
| 150 |
/** dict_table_t::flags bit 0 is equal to 1 if the row format = Compact */ |
| 151 |
/** Compact row format. */ |
| 152 |
constexpr uint32_t DICT_TF_COMPACT = 1; |
| 153 |
|
| 154 |
/** This bitmask is used in SYS_TABLES.N_COLS to set and test whether |
| 155 |
the Compact page format is used, i.e ROW_FORMAT != REDUNDANT */ |
| 156 |
constexpr uint32_t DICT_N_COLS_COMPACT = 0x80000000UL; |
| 157 |
|
| 158 |
/** Width of the COMPACT flag */ |
| 159 |
constexpr uint32_t DICT_TF_WIDTH_COMPACT = 1; |
| 160 |
|
| 161 |
/** Width of the ZIP_SSIZE flag */ |
| 162 |
constexpr uint32_t DICT_TF_WIDTH_ZIP_SSIZE = 4; |
| 163 |
|
| 164 |
/** Width of the ATOMIC_BLOBS flag. The ROW_FORMAT=REDUNDANT and |
| 165 |
ROW_FORMAT=COMPACT broke up BLOB and TEXT fields, storing the first 768 bytes |
| 166 |
in the clustered index. ROW_FORMAT=DYNAMIC and ROW_FORMAT=COMPRESSED |
| 167 |
store the whole blob or text field off-page atomically. |
| 168 |
Secondary indexes are created from this external data using row_ext_t |
| 169 |
to cache the BLOB prefixes. */ |
| 170 |
constexpr uint32_t DICT_TF_WIDTH_ATOMIC_BLOBS = 1; |
| 171 |
|
| 172 |
/** If a table is created with the MYSQL option DATA DIRECTORY and |
| 173 |
innodb-file-per-table, an older engine will not be able to find that table. |
| 174 |
This flag prevents older engines from attempting to open the table and |
| 175 |
allows InnoDB to update_create_info() accordingly. */ |
| 176 |
constexpr uint32_t DICT_TF_WIDTH_DATA_DIR = 1; |
| 177 |
|
| 178 |
/** Width of the SHARED tablespace flag. |
| 179 |
It is used to identify tables that exist inside a shared general tablespace. |
| 180 |
If a table is created with the TABLESPACE=tsname option, an older engine will |
| 181 |
not be able to find that table. This flag prevents older engines from attempting |
| 182 |
to open the table and allows InnoDB to quickly find the tablespace. */ |
| 183 |
|
| 184 |
constexpr uint32_t DICT_TF_WIDTH_SHARED_SPACE = 1; |
| 185 |
|
| 186 |
/** Width of all the currently known table flags */ |
| 187 |
constexpr uint32_t DICT_TF_BITS = |
| 188 |
DICT_TF_WIDTH_COMPACT + DICT_TF_WIDTH_ZIP_SSIZE + |
| 189 |
DICT_TF_WIDTH_ATOMIC_BLOBS + DICT_TF_WIDTH_DATA_DIR + |
| 190 |
DICT_TF_WIDTH_SHARED_SPACE; |
| 191 |
|
| 192 |
/** A mask of all the known/used bits in table flags */ |
| 193 |
constexpr uint32_t DICT_TF_BIT_MASK = ~(~0U << DICT_TF_BITS); |
| 194 |
|
| 195 |
/** Zero relative shift position of the COMPACT field */ |
| 196 |
constexpr uint32_t DICT_TF_POS_COMPACT = 0; |
| 197 |
/** Zero relative shift position of the ZIP_SSIZE field */ |
| 198 |
constexpr uint32_t DICT_TF_POS_ZIP_SSIZE = |
| 199 |
DICT_TF_POS_COMPACT + DICT_TF_WIDTH_COMPACT; |
| 200 |
/** Zero relative shift position of the ATOMIC_BLOBS field */ |
| 201 |
constexpr uint32_t DICT_TF_POS_ATOMIC_BLOBS = |
| 202 |
DICT_TF_POS_ZIP_SSIZE + DICT_TF_WIDTH_ZIP_SSIZE; |
| 203 |
/** Zero relative shift position of the DATA_DIR field */ |
| 204 |
constexpr uint32_t DICT_TF_POS_DATA_DIR = |
| 205 |
DICT_TF_POS_ATOMIC_BLOBS + DICT_TF_WIDTH_ATOMIC_BLOBS; |
| 206 |
/** Zero relative shift position of the SHARED TABLESPACE field */ |
| 207 |
constexpr uint32_t DICT_TF_POS_SHARED_SPACE = |
| 208 |
DICT_TF_POS_DATA_DIR + DICT_TF_WIDTH_DATA_DIR; |
| 209 |
/** Zero relative shift position of the start of the UNUSED bits */ |
| 210 |
constexpr uint32_t DICT_TF_POS_UNUSED = |
| 211 |
DICT_TF_POS_SHARED_SPACE + DICT_TF_WIDTH_SHARED_SPACE; |
| 212 |
|
| 213 |
/** Bit mask of the COMPACT field */ |
| 214 |
constexpr uint32_t DICT_TF_MASK_COMPACT = (~(~0U << DICT_TF_WIDTH_COMPACT)) |
| 215 |
<< DICT_TF_POS_COMPACT; |
| 216 |
/** Bit mask of the ZIP_SSIZE field */ |
| 217 |
constexpr uint32_t DICT_TF_MASK_ZIP_SSIZE = (~(~0U << DICT_TF_WIDTH_ZIP_SSIZE)) |
| 218 |
<< DICT_TF_POS_ZIP_SSIZE; |
| 219 |
/** Bit mask of the ATOMIC_BLOBS field */ |
| 220 |
constexpr uint32_t DICT_TF_MASK_ATOMIC_BLOBS = |
| 221 |
(~(~0U << DICT_TF_WIDTH_ATOMIC_BLOBS)) << DICT_TF_POS_ATOMIC_BLOBS; |
| 222 |
/** Bit mask of the DATA_DIR field */ |
| 223 |
constexpr uint32_t DICT_TF_MASK_DATA_DIR = (~(~0U << DICT_TF_WIDTH_DATA_DIR)) |
| 224 |
<< DICT_TF_POS_DATA_DIR; |
| 225 |
/** Bit mask of the SHARED_SPACE field */ |
| 226 |
constexpr uint32_t DICT_TF_MASK_SHARED_SPACE = |
| 227 |
(~(~0U << DICT_TF_WIDTH_SHARED_SPACE)) << DICT_TF_POS_SHARED_SPACE; |
| 228 |
|
| 229 |
/** Return the value of the COMPACT field */ |
| 230 |
inline uint32_t DICT_TF_GET_COMPACT(uint32_t flags) { |
| 231 |
return (flags & DICT_TF_MASK_COMPACT) >> DICT_TF_POS_COMPACT; |
| 232 |
} |
| 233 |
/** Return the value of the ZIP_SSIZE field */ |
| 234 |
inline uint32_t DICT_TF_GET_ZIP_SSIZE(uint32_t flags) { |
| 235 |
return (flags & DICT_TF_MASK_ZIP_SSIZE) >> DICT_TF_POS_ZIP_SSIZE; |
| 236 |
} |
| 237 |
/** Return the value of the ATOMIC_BLOBS field */ |
| 238 |
inline uint32_t DICT_TF_HAS_ATOMIC_BLOBS(uint32_t flags) { |
| 239 |
return (flags & DICT_TF_MASK_ATOMIC_BLOBS) >> DICT_TF_POS_ATOMIC_BLOBS; |
| 240 |
} |
| 241 |
/** Return the value of the DATA_DIR field */ |
| 242 |
inline uint32_t DICT_TF_HAS_DATA_DIR(uint32_t flags) { |
| 243 |
return (flags & DICT_TF_MASK_DATA_DIR) >> DICT_TF_POS_DATA_DIR; |
| 244 |
} |
| 245 |
/** Return the value of the SHARED_SPACE field */ |
| 246 |
inline uint32_t DICT_TF_HAS_SHARED_SPACE(uint32_t flags) { |
| 247 |
return (flags & DICT_TF_MASK_SHARED_SPACE) >> DICT_TF_POS_SHARED_SPACE; |
| 248 |
} |
| 249 |
/** Return the contents of the UNUSED bits */ |
| 250 |
inline uint32_t DICT_TF_GET_UNUSED(uint32_t flags) { |
| 251 |
return flags >> DICT_TF_POS_UNUSED; |
| 252 |
} |
| 253 |
/** @} */ |
| 254 |
|
| 255 |
/** @brief Table Flags set number 2. |
| 256 |
|
| 257 |
These flags will be stored in SYS_TABLES.MIX_LEN. All unused flags |
| 258 |
will be written as 0. The column may contain garbage for tables |
| 259 |
created with old versions of InnoDB that only implemented |
| 260 |
ROW_FORMAT=REDUNDANT. InnoDB engines do not check these flags |
| 261 |
for unknown bits in order to protect backward incompatibility. */ |
| 262 |
/** @{ */ |
| 263 |
/** Total number of bits in table->flags2. */ |
| 264 |
constexpr uint32_t DICT_TF2_BITS = 11; |
| 265 |
constexpr uint32_t DICT_TF2_UNUSED_BIT_MASK = ~0U << DICT_TF2_BITS; |
| 266 |
constexpr uint32_t DICT_TF2_BIT_MASK = ~DICT_TF2_UNUSED_BIT_MASK; |
| 267 |
|
| 268 |
/** TEMPORARY; true for tables from CREATE TEMPORARY TABLE. */ |
| 269 |
constexpr uint32_t DICT_TF2_TEMPORARY = 1; |
| 270 |
|
| 271 |
/** The table has an internal defined DOC ID column */ |
| 272 |
constexpr uint32_t DICT_TF2_FTS_HAS_DOC_ID = 2; |
| 273 |
|
| 274 |
/** The table has an FTS index */ |
| 275 |
constexpr uint32_t DICT_TF2_FTS = 4; |
| 276 |
|
| 277 |
/** Need to add Doc ID column for FTS index build. |
| 278 |
This is a transient bit for index build */ |
| 279 |
constexpr uint32_t DICT_TF2_FTS_ADD_DOC_ID = 8; |
| 280 |
|
| 281 |
/** This bit is used during table creation to indicate that it will |
| 282 |
use its own tablespace instead of the system tablespace. */ |
| 283 |
constexpr uint32_t DICT_TF2_USE_FILE_PER_TABLE = 16; |
| 284 |
|
| 285 |
/** Set when we discard/detach the tablespace */ |
| 286 |
constexpr uint32_t DICT_TF2_DISCARDED = 32; |
| 287 |
|
| 288 |
/** Intrinsic table bit |
| 289 |
Intrinsic table is table created internally by MySQL modules viz. Optimizer, |
| 290 |
FTS, etc.... Intrinsic table has all the properties of the normal table except |
| 291 |
it is not created by user and so not visible to end-user. */ |
| 292 |
constexpr uint32_t DICT_TF2_INTRINSIC = 128; |
| 293 |
|
| 294 |
/** Encryption table bit for innodb_file-per-table only. */ |
| 295 |
constexpr uint32_t DICT_TF2_ENCRYPTION_FILE_PER_TABLE = 256; |
| 296 |
|
| 297 |
/** FTS AUX hidden table bit. */ |
| 298 |
constexpr uint32_t DICT_TF2_AUX = 512; |
| 299 |
|
| 300 |
/** Table is opened by resurrected trx during crash recovery. */ |
| 301 |
constexpr uint32_t DICT_TF2_RESURRECT_PREPARED = 1024; |
| 302 |
/** @} */ |
| 303 |
|
| 304 |
/** Tables could be chained together with Foreign key constraint. When |
| 305 |
first load the parent table, we would load all of its descedents. |
| 306 |
This could result in rescursive calls and out of stack error eventually. |
| 307 |
DICT_FK_MAX_RECURSIVE_LOAD defines the maximum number of recursive loads, |
| 308 |
when exceeded, the child table will not be loaded. It will be loaded when |
| 309 |
the foreign constraint check needs to be run. */ |
| 310 |
constexpr uint32_t DICT_FK_MAX_RECURSIVE_LOAD = 20; |
| 311 |
|
| 312 |
/** Similarly, when tables are chained together with foreign key constraints |
| 313 |
with on cascading delete/update clause, delete from parent table could |
| 314 |
result in recursive cascading calls. This defines the maximum number of |
| 315 |
such cascading deletes/updates allowed. When exceeded, the delete from |
| 316 |
parent table will fail, and user has to drop excessive foreign constraint |
| 317 |
before proceeds. */ |
| 318 |
constexpr uint32_t FK_MAX_CASCADE_DEL = 15; |
| 319 |
|
| 320 |
/** Maximum number of rows version allowed when columns are added/dropped |
| 321 |
INSTANTly. After this limit is reached, any attempt to do ADD/DROP INSTANT |
| 322 |
column will result in error. */ |
| 323 |
const uint8_t MAX_ROW_VERSION = 64; |
| 324 |
|
| 325 |
/** Adds a virtual column definition to a table. |
| 326 |
@param[in,out] table table |
| 327 |
@param[in] heap temporary memory heap, or NULL. It is |
| 328 |
used to store name when we have not finished |
| 329 |
adding all columns. When all columns are |
| 330 |
added, the whole name will copy to memory from |
| 331 |
table->heap |
| 332 |
@param[in] name column name |
| 333 |
@param[in] mtype main datatype |
| 334 |
@param[in] prtype precise type |
| 335 |
@param[in] len length |
| 336 |
@param[in] pos position in a table |
| 337 |
@param[in] num_base number of base columns |
| 338 |
@param[in] is_visible True if virtual column is visible to user |
| 339 |
@return the virtual column definition */ |
| 340 |
dict_v_col_t *dict_mem_table_add_v_col(dict_table_t *table, mem_heap_t *heap, |
| 341 |
const char *name, ulint mtype, |
| 342 |
ulint prtype, ulint len, ulint pos, |
| 343 |
ulint num_base, bool is_visible); |
| 344 |
|
| 345 |
/** Adds a stored column definition to a table. |
| 346 |
@param[in,out] table table |
| 347 |
@param[in] num_base number of base columns. */ |
| 348 |
void dict_mem_table_add_s_col(dict_table_t *table, ulint num_base); |
| 349 |
|
| 350 |
/** Renames a column of a table in the data dictionary cache. |
| 351 |
@param[in,out] table Table |
| 352 |
@param[in] nth_col Column index |
| 353 |
@param[in] from Old column name |
| 354 |
@param[in] to New column name |
| 355 |
@param[in] is_virtual If this is a virtual column */ |
| 356 |
void dict_mem_table_col_rename(dict_table_t *table, ulint nth_col, |
| 357 |
const char *from, const char *to, |
| 358 |
bool is_virtual); |
| 359 |
|
| 360 |
/** This function poplulates a dict_index_t index memory structure with |
| 361 |
supplied information. |
| 362 |
@param[out] index index to be filled |
| 363 |
@param[in] heap memory heap |
| 364 |
@param[in] table_name table name |
| 365 |
@param[in] index_name index name |
| 366 |
@param[in] space space where the index tree is placed, the |
| 367 |
clustered type ignored if the index is of |
| 368 |
the clustered type |
| 369 |
@param[in] type DICT_UNIQUE, DICT_CLUSTERED, ... ORed |
| 370 |
@param[in] n_fields number of fields */ |
| 371 |
static inline void dict_mem_fill_index_struct( |
| 372 |
dict_index_t *index, mem_heap_t *heap, const char *table_name, |
| 373 |
const char *index_name, ulint space, ulint type, ulint n_fields); |
| 374 |
|
| 375 |
/** Frees an index memory object. */ |
| 376 |
void dict_mem_index_free(dict_index_t *index); /*!< in: index */ |
| 377 |
/** Creates and initializes a foreign constraint memory object. |
| 378 |
@return own: foreign constraint struct */ |
| 379 |
dict_foreign_t *dict_mem_foreign_create(void); |
| 380 |
|
| 381 |
/** Sets the foreign_table_name_lookup pointer based on the value of |
| 382 |
lower_case_table_names. If that is 0 or 1, foreign_table_name_lookup |
| 383 |
will point to foreign_table_name. If 2, then another string is |
| 384 |
allocated from the heap and set to lower case. */ |
| 385 |
void dict_mem_foreign_table_name_lookup_set( |
| 386 |
dict_foreign_t *foreign, /*!< in/out: foreign struct */ |
| 387 |
bool do_alloc); /*!< in: is an alloc needed */ |
| 388 |
|
| 389 |
/** Sets the referenced_table_name_lookup pointer based on the value of |
| 390 |
lower_case_table_names. If that is 0 or 1, referenced_table_name_lookup |
| 391 |
will point to referenced_table_name. If 2, then another string is |
| 392 |
allocated from the heap and set to lower case. */ |
| 393 |
void dict_mem_referenced_table_name_lookup_set( |
| 394 |
dict_foreign_t *foreign, /*!< in/out: foreign struct */ |
| 395 |
bool do_alloc); /*!< in: is an alloc needed */ |
| 396 |
|
| 397 |
/** Fills the dependent virtual columns in a set. |
| 398 |
Reason for being dependent are |
| 399 |
1) FK can be present on base column of virtual columns |
| 400 |
2) FK can be present on column which is a part of virtual index |
| 401 |
@param[in,out] foreign foreign key information. */ |
| 402 |
void dict_mem_foreign_fill_vcol_set(dict_foreign_t *foreign); |
| 403 |
|
| 404 |
/** Fill virtual columns set in each fk constraint present in the table. |
| 405 |
@param[in,out] table innodb table object. */ |
| 406 |
void dict_mem_table_fill_foreign_vcol_set(dict_table_t *table); |
| 407 |
|
| 408 |
/** Free the vcol_set from all foreign key constraint on the table. |
| 409 |
@param[in,out] table innodb table object. */ |
| 410 |
void dict_mem_table_free_foreign_vcol_set(dict_table_t *table); |
| 411 |
|
| 412 |
/** Create a temporary tablename like "#sql-ibtid-inc" where |
| 413 |
tid = the Table ID |
| 414 |
inc = a randomly initialized number that is incremented for each file |
| 415 |
The table ID is a 64 bit integer, can use up to 20 digits, and is |
| 416 |
initialized at bootstrap. The second number is 32 bits, can use up to 10 |
| 417 |
digits, and is initialized at startup to a randomly distributed number. |
| 418 |
It is hoped that the combination of these two numbers will provide a |
| 419 |
reasonably unique temporary file name. |
| 420 |
@param[in] heap A memory heap |
| 421 |
@param[in] dbtab Table name in the form database/table name |
| 422 |
@param[in] id Table id |
| 423 |
@return A unique temporary tablename suitable for InnoDB use */ |
| 424 |
char *dict_mem_create_temporary_tablename(mem_heap_t *heap, const char *dbtab, |
| 425 |
table_id_t id); |
| 426 |
|
| 427 |
static inline bool is_valid_row_version(const uint8_t version) { |
| 428 |
/* NOTE : 0 is also a valid row versions for rows which are inserted after |
| 429 |
upgrading from earlier INSTANT implemenation */ |
| 430 |
if (version <= MAX_ROW_VERSION) { |
| 431 |
return true; |
| 432 |
} |
| 433 |
|
| 434 |
return false; |
| 435 |
} |
| 436 |
|
| 437 |
/** Initialize dict memory variables */ |
| 438 |
void dict_mem_init(void); |
| 439 |
|
| 440 |
/** SQL identifier name wrapper for pretty-printing */ |
| 441 |
class id_name_t { |
| 442 |
public: |
| 443 |
/** Default constructor */ |
| 444 |
id_name_t() : m_name() {} |
| 445 |
/** Constructor |
| 446 |
@param[in] name identifier to assign */ |
| 447 |
explicit id_name_t(const char *name) : m_name(name) {} |
| 448 |
|
| 449 |
/** Assignment operator |
| 450 |
@param[in] name identifier to assign */ |
| 451 |
id_name_t &operator=(const char *name) { |
| 452 |
m_name = name; |
| 453 |
return (*this); |
| 454 |
} |
| 455 |
|
| 456 |
/** Implicit type conversion |
| 457 |
@return the name */ |
| 458 |
operator const char *() const { return (m_name); } |
| 459 |
|
| 460 |
/** Explicit type conversion |
| 461 |
@return the name */ |
| 462 |
const char *operator()() const { return (m_name); } |
| 463 |
|
| 464 |
private: |
| 465 |
/** The name in internal representation */ |
| 466 |
const char *m_name; |
| 467 |
}; |
| 468 |
|
| 469 |
/** Table name wrapper for pretty-printing */ |
| 470 |
struct table_name_t { |
| 471 |
/** The name in internal representation */ |
| 472 |
char *m_name; |
| 473 |
}; |
| 474 |
|
| 475 |
/** Data structure for default value of a column in a table */ |
| 476 |
struct dict_col_default_t { |
| 477 |
/** Pointer to the column itself */ |
| 478 |
dict_col_t *col; |
| 479 |
/** Default value in bytes */ |
| 480 |
byte *value; |
| 481 |
/** Length of default value */ |
| 482 |
size_t len; |
| 483 |
|
| 484 |
bool operator==(const dict_col_default_t &other); |
| 485 |
bool operator!=(const dict_col_default_t &other); |
| 486 |
}; |
| 487 |
|
| 488 |
/** Data structure for a column in a table */ |
| 489 |
struct dict_col_t { |
| 490 |
/*----------------------*/ |
| 491 |
/** The following are copied from dtype_t, |
| 492 |
so that all bit-fields can be packed tightly. */ |
| 493 |
/** @{ */ |
| 494 |
|
| 495 |
/** Default value when this column was added instantly. |
| 496 |
If this is not a instantly added column then this is nullptr. */ |
| 497 |
dict_col_default_t *instant_default{nullptr}; |
| 498 |
|
| 499 |
unsigned prtype : 32; /*!< precise type; MySQL data |
| 500 |
type, charset code, flags to |
| 501 |
indicate nullability, |
| 502 |
signedness, whether this is a |
| 503 |
binary string, whether this is |
| 504 |
a true VARCHAR where MySQL |
| 505 |
uses 2 bytes to store the length */ |
| 506 |
unsigned mtype : 8; /*!< main data type */ |
| 507 |
|
| 508 |
/* the remaining fields do not affect alphabetical ordering: */ |
| 509 |
|
| 510 |
unsigned len : 16; /*!< length; for MySQL data this |
| 511 |
is field->pack_length(), |
| 512 |
except that for a >= 5.0.3 |
| 513 |
type true VARCHAR this is the |
| 514 |
maximum byte length of the |
| 515 |
string data (in addition to |
| 516 |
the string, MySQL uses 1 or 2 |
| 517 |
bytes to store the string length) */ |
| 518 |
|
| 519 |
unsigned mbminmaxlen : 5; /*!< minimum and maximum length of a |
| 520 |
character, in bytes; |
| 521 |
DATA_MBMINMAXLEN(mbminlen,mbmaxlen); |
| 522 |
mbminlen=DATA_MBMINLEN(mbminmaxlen); |
| 523 |
mbmaxlen=DATA_MBMINLEN(mbminmaxlen) */ |
| 524 |
/*----------------------*/ |
| 525 |
/* End of definitions copied from dtype_t */ |
| 526 |
/** @} */ |
| 527 |
|
| 528 |
unsigned ind : 10; /*!< table column position |
| 529 |
(starting from 0) */ |
| 530 |
unsigned ord_part : 1; /*!< nonzero if this column |
| 531 |
appears in the ordering fields |
| 532 |
of an index */ |
| 533 |
unsigned max_prefix : 12; /*!< maximum index prefix length on |
| 534 |
this column. Our current max limit is |
| 535 |
3072 (REC_VERSION_56_MAX_INDEX_COL_LEN) |
| 536 |
bytes. */ |
| 537 |
|
| 538 |
/* True, if the column is visible */ |
| 539 |
bool is_visible; |
| 540 |
|
| 541 |
private: |
| 542 |
/* Position of column on physical row. |
| 543 |
If column prefix is part of PK, it appears twice on row. First 2 bytes are |
| 544 |
for prefix position and next 2 bytes are for column position on row. */ |
| 545 |
uint32_t phy_pos{UINT32_UNDEFINED}; |
| 546 |
|
| 547 |
/* Row version in which this column was added INSTANTly to the table */ |
| 548 |
uint8_t version_added{UINT8_UNDEFINED}; |
| 549 |
|
| 550 |
/* Row version in which this column was dropped INSTANTly from the table */ |
| 551 |
uint8_t version_dropped{UINT8_UNDEFINED}; |
| 552 |
|
| 553 |
public: |
| 554 |
/* If column prefix is there on row. */ |
| 555 |
bool has_prefix_phy_pos() const { return (phy_pos & 0x8000); } |
| 556 |
|
| 557 |
/* Get the physical position of column prefix on row. */ |
| 558 |
uint16_t get_prefix_phy_pos() const { |
| 559 |
ut_ad(has_prefix_phy_pos()); |
| 560 |
return ((uint16_t)(phy_pos >> 16)); |
| 561 |
} |
| 562 |
|
| 563 |
/* Set the physical position of column prefix on row. */ |
| 564 |
void set_prefix_phy_pos(uint16_t prefix_pos) { |
| 565 |
phy_pos = prefix_pos; |
| 566 |
phy_pos = phy_pos << 16; |
| 567 |
phy_pos |= 0x8000; |
| 568 |
} |
| 569 |
|
| 570 |
/* Get the physical position of column on row. */ |
| 571 |
uint16_t get_col_phy_pos() const { return ((phy_pos & ~0x8000) & 0xFFFF); } |
| 572 |
|
| 573 |
/* Set the physical position of column on row. */ |
| 574 |
void set_col_phy_pos(uint16_t pos) { |
| 575 |
ut_ad(has_prefix_phy_pos()); |
| 576 |
phy_pos |= pos; |
| 577 |
} |
| 578 |
|
| 579 |
/* Set the physical position metadata of column. */ |
| 580 |
uint32_t get_phy_pos() const { return phy_pos; } |
| 581 |
|
| 582 |
/* Get the physical position metadata of column. */ |
| 583 |
void set_phy_pos(uint32_t pos) { phy_pos = pos; } |
| 584 |
|
| 585 |
bool is_instant_added() const { |
| 586 |
if (version_added != UINT8_UNDEFINED && version_added > 0) { |
| 587 |
return true; |
| 588 |
} |
| 589 |
return false; |
| 590 |
} |
| 591 |
|
| 592 |
uint8_t get_version_added() const { |
| 593 |
ut_ad(is_instant_added()); |
| 594 |
return version_added; |
| 595 |
} |
| 596 |
|
| 597 |
void set_version_added(uint8_t version) { |
| 598 |
ut_ad(version == UINT8_UNDEFINED || is_valid_row_version(version)); |
| 599 |
version_added = version; |
| 600 |
} |
| 601 |
|
| 602 |
bool is_version_added_match(const dict_col_t *col) const { |
| 603 |
if (is_instant_added() != col->is_instant_added()) { |
| 604 |
return false; |
| 605 |
} |
| 606 |
|
| 607 |
if (is_instant_added()) { |
| 608 |
return (get_version_added() == col->get_version_added()); |
| 609 |
} |
| 610 |
|
| 611 |
return true; |
| 612 |
} |
| 613 |
|
| 614 |
bool is_instant_dropped() const { |
| 615 |
if (version_dropped != UINT8_UNDEFINED && version_dropped > 0) { |
| 616 |
return true; |
| 617 |
} |
| 618 |
return false; |
| 619 |
} |
| 620 |
|
| 621 |
uint8_t get_version_dropped() const { |
| 622 |
ut_ad(is_instant_dropped()); |
| 623 |
return version_dropped; |
| 624 |
} |
| 625 |
|
| 626 |
void set_version_dropped(uint8_t version) { |
| 627 |
ut_ad(version == UINT8_UNDEFINED || is_valid_row_version(version)); |
| 628 |
version_dropped = version; |
| 629 |
} |
| 630 |
|
| 631 |
bool is_version_dropped_match(const dict_col_t *col) const { |
| 632 |
if (is_instant_dropped() != col->is_instant_dropped()) { |
| 633 |
return false; |
| 634 |
} |
| 635 |
|
| 636 |
if (is_instant_dropped()) { |
| 637 |
return (get_version_dropped() == col->get_version_dropped()); |
| 638 |
} |
| 639 |
|
| 640 |
return true; |
| 641 |
} |
| 642 |
|
| 643 |
/** Returns the minimum size of the column. |
| 644 |
@return minimum size */ |
| 645 |
ulint get_min_size() const { |
| 646 |
return (dtype_get_min_size_low(mtype, prtype, len, mbminmaxlen)); |
| 647 |
} |
| 648 |
|
| 649 |
/** Returns the maximum size of the column. |
| 650 |
@return maximum size */ |
| 651 |
ulint get_max_size() const { return (dtype_get_max_size_low(mtype, len)); } |
| 652 |
|
| 653 |
/** Check if a column is a virtual column |
| 654 |
@return true if it is a virtual column, false otherwise */ |
| 655 |
bool is_virtual() const { return (prtype & DATA_VIRTUAL); } |
| 656 |
|
| 657 |
/** Check if a column is a multi-value virtual column |
| 658 |
@return true if it is a multi-value virtual column, false otherwise */ |
| 659 |
bool is_multi_value() const { return ((prtype & DATA_MULTI_VALUE) != 0); } |
| 660 |
|
| 661 |
/** Check if a column is nullable |
| 662 |
@return true if it is nullable, otherwise false */ |
| 663 |
bool is_nullable() const { return ((prtype & DATA_NOT_NULL) == 0); } |
| 664 |
|
| 665 |
/** Gets the column data type. |
| 666 |
@param[out] type data type */ |
| 667 |
void copy_type(dtype_t *type) const { |
| 668 |
ut_ad(type != nullptr); |
| 669 |
|
| 670 |
type->mtype = mtype; |
| 671 |
type->prtype = prtype; |
| 672 |
type->len = len; |
| 673 |
type->mbminmaxlen = mbminmaxlen; |
| 674 |
} |
| 675 |
|
| 676 |
/** Gets the minimum number of bytes per character. |
| 677 |
@return minimum multi-byte char size, in bytes */ |
| 678 |
ulint get_mbminlen() const { return (DATA_MBMINLEN(mbminmaxlen)); } |
| 679 |
|
| 680 |
/** Gets the maximum number of bytes per character. |
| 681 |
@return maximum multi-byte char size, in bytes */ |
| 682 |
ulint get_mbmaxlen() const { return (DATA_MBMAXLEN(mbminmaxlen)); } |
| 683 |
|
| 684 |
/** Sets the minimum and maximum number of bytes per character. |
| 685 |
@param[in] mbminlen minimum multi byte character size, in bytes |
| 686 |
@param[in] mbmaxlen mAXimum multi-byte character size, in bytes */ |
| 687 |
void set_mbminmaxlen(ulint mbminlen, ulint mbmaxlen) { |
| 688 |
ut_ad(mbminlen < DATA_MBMAX); |
| 689 |
ut_ad(mbmaxlen < DATA_MBMAX); |
| 690 |
ut_ad(mbminlen <= mbmaxlen); |
| 691 |
|
| 692 |
mbminmaxlen = DATA_MBMINMAXLEN(mbminlen, mbmaxlen); |
| 693 |
} |
| 694 |
|
| 695 |
/** Returns the size of a fixed size column, 0 if not a fixed size column. |
| 696 |
@param[in] comp nonzero=ROW_FORMAT=COMPACT |
| 697 |
@return fixed size, or 0 */ |
| 698 |
ulint get_fixed_size(ulint comp) const { |
| 699 |
return (dtype_get_fixed_size_low(mtype, prtype, len, mbminmaxlen, comp)); |
| 700 |
} |
| 701 |
|
| 702 |
/** Returns the ROW_FORMAT=REDUNDANT stored SQL NULL size of a column. |
| 703 |
For fixed length types it is the fixed length of the type, otherwise 0. |
| 704 |
@param[in] comp nonzero=ROW_FORMAT=COMPACT |
| 705 |
@return SQL null storage size in ROW_FORMAT=REDUNDANT */ |
| 706 |
ulint get_null_size(ulint comp) const { return (get_fixed_size(comp)); } |
| 707 |
|
| 708 |
/** Check whether the col is used in spatial index or regular index. |
| 709 |
@return spatial status */ |
| 710 |
spatial_status_t get_spatial_status() const { |
| 711 |
spatial_status_t spatial_status = SPATIAL_NONE; |
| 712 |
|
| 713 |
/* Column is not a part of any index. */ |
| 714 |
if (!ord_part) { |
| 715 |
return (spatial_status); |
| 716 |
} |
| 717 |
|
| 718 |
if (DATA_GEOMETRY_MTYPE(mtype)) { |
| 719 |
if (max_prefix == 0) { |
| 720 |
spatial_status = SPATIAL_ONLY; |
| 721 |
} else { |
| 722 |
/* Any regular index on a geometry column |
| 723 |
should have a prefix. */ |
| 724 |
spatial_status = SPATIAL_MIXED; |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
return (spatial_status); |
| 729 |
} |
| 730 |
|
| 731 |
/** Set default value |
| 732 |
@param[in] value Default value |
| 733 |
@param[in] length Default value length |
| 734 |
@param[in,out] heap Heap to allocate memory */ |
| 735 |
void set_default(const byte *value, size_t length, mem_heap_t *heap); |
| 736 |
|
| 737 |
/** Check if column is dropped before the given version. |
| 738 |
@param[in] version row version |
| 739 |
@return true if the column is dropped before or in the version. */ |
| 740 |
bool is_dropped_in_or_before(uint8_t version) const { |
| 741 |
ut_ad(is_valid_row_version(version)); |
| 742 |
|
| 743 |
if (!is_instant_dropped()) { |
| 744 |
return false; |
| 745 |
} |
| 746 |
|
| 747 |
return (get_version_dropped() <= version); |
| 748 |
} |
| 749 |
|
| 750 |
/** Check if column is added after the current version. |
| 751 |
@param[in] version row version |
| 752 |
@return true if column is added after the current row version. */ |
| 753 |
bool is_added_after(uint8_t version) const { |
| 754 |
ut_ad(is_valid_row_version(version)); |
| 755 |
|
| 756 |
if (!is_instant_added()) { |
| 757 |
return false; |
| 758 |
} |
| 759 |
|
| 760 |
return (get_version_added() > version); |
| 761 |
} |
| 762 |
|
| 763 |
/** Check if a column is visible in given version. |
| 764 |
@param[in] version row version |
| 765 |
return true if column is visible in version. */ |
| 766 |
bool is_visible_in_version(uint8_t version) const { |
| 767 |
ut_ad(is_valid_row_version(version)); |
| 768 |
return (!is_added_after(version) && !is_dropped_in_or_before(version)); |
| 769 |
} |
| 770 |
|
| 771 |
#ifdef UNIV_DEBUG |
| 772 |
/** Assert that a column and a data type match. |
| 773 |
param[in] type data type |
| 774 |
@return true */ |
| 775 |
bool assert_equal(const dtype_t *type) const { |
| 776 |
ut_ad(type); |
| 777 |
|
| 778 |
ut_ad(mtype == type->mtype); |
| 779 |
ut_ad((prtype | DATA_VIRTUAL) == (type->prtype | DATA_VIRTUAL)); |
| 780 |
// ut_ad(col->len == type->len); |
| 781 |
#ifndef UNIV_HOTBACKUP |
| 782 |
ut_ad(mbminmaxlen == type->mbminmaxlen); |
| 783 |
#endif /* !UNIV_HOTBACKUP */ |
| 784 |
|
| 785 |
return true; |
| 786 |
} |
| 787 |
|
| 788 |
/** Check if a column name resembles format for dropped column. |
| 789 |
param[in] type column name |
| 790 |
@return true if column name resembles dropped column. */ |
| 791 |
static bool is_instant_dropped_name(const std::string col_name) { |
| 792 |
if (col_name.find(INSTANT_DROP_SUFFIX_8_0_29) != std::string::npos || |
| 793 |
col_name.find(INSTANT_DROP_PREFIX_8_0_32) != std::string::npos) |
| 794 |
return true; |
| 795 |
return false; |
| 796 |
} |
| 797 |
#endif /* UNIV_DEBUG */ |
| 798 |
}; |
| 799 |
|
| 800 |
/** Index information put in a list of virtual column structure. Index |
| 801 |
id and virtual column position in the index will be logged. |
| 802 |
There can be multiple entries for a given index, with a different position. */ |
| 803 |
struct dict_v_idx_t { |
| 804 |
/** active index on the column */ |
| 805 |
dict_index_t *index; |
| 806 |
|
| 807 |
/** position in this index */ |
| 808 |
ulint nth_field; |
| 809 |
}; |
| 810 |
|
| 811 |
/** Index list to put in dict_v_col_t */ |
| 812 |
typedef std::list<dict_v_idx_t, ut::allocator<dict_v_idx_t>> dict_v_idx_list; |
| 813 |
|
| 814 |
/** Data structure for a virtual column in a table */ |
| 815 |
struct dict_v_col_t { |
| 816 |
/** column structure */ |
| 817 |
dict_col_t m_col; |
| 818 |
|
| 819 |
/** array of base column ptr */ |
| 820 |
dict_col_t **base_col; |
| 821 |
|
| 822 |
/** number of base columns */ |
| 823 |
ulint num_base; |
| 824 |
|
| 825 |
/** column pos in table */ |
| 826 |
ulint v_pos; |
| 827 |
|
| 828 |
/** Virtual index list, and column position in the index, |
| 829 |
the allocated memory is not from table->heap, nor it is |
| 830 |
tracked by dict_sys->size */ |
| 831 |
dict_v_idx_list *v_indexes; |
| 832 |
}; |
| 833 |
|
| 834 |
/** Data structure for newly added virtual column in a table */ |
| 835 |
struct dict_add_v_col_t { |
| 836 |
/** number of new virtual column */ |
| 837 |
ulint n_v_col; |
| 838 |
|
| 839 |
/** column structures */ |
| 840 |
const dict_v_col_t *v_col; |
| 841 |
|
| 842 |
/** new col names */ |
| 843 |
const char **v_col_name; |
| 844 |
}; |
| 845 |
|
| 846 |
/** Data structure for a stored column in a table. */ |
| 847 |
struct dict_s_col_t { |
| 848 |
/** Stored column ptr */ |
| 849 |
dict_col_t *m_col; |
| 850 |
/** array of base col ptr */ |
| 851 |
dict_col_t **base_col; |
| 852 |
/** number of base columns */ |
| 853 |
ulint num_base; |
| 854 |
/** column pos in table */ |
| 855 |
ulint s_pos; |
| 856 |
}; |
| 857 |
|
| 858 |
/** list to put stored column for dict_table_t */ |
| 859 |
typedef std::list<dict_s_col_t, ut::allocator<dict_s_col_t>> dict_s_col_list; |
| 860 |
|
| 861 |
/** @brief DICT_ANTELOPE_MAX_INDEX_COL_LEN is measured in bytes and |
| 862 |
is the maximum indexed column length (or indexed prefix length) in |
| 863 |
ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT. Also, in any format, |
| 864 |
any fixed-length field that is longer than this will be encoded as |
| 865 |
a variable-length field. |
| 866 |
|
| 867 |
It is set to 3*256, so that one can create a column prefix index on |
| 868 |
256 characters of a TEXT or VARCHAR column also in the UTF-8 |
| 869 |
charset. In that charset, a character may take at most 3 bytes. This |
| 870 |
constant MUST NOT BE CHANGED, or the compatibility of InnoDB data |
| 871 |
files would be at risk! */ |
| 872 |
constexpr uint32_t DICT_ANTELOPE_MAX_INDEX_COL_LEN = |
| 873 |
REC_ANTELOPE_MAX_INDEX_COL_LEN; |
| 874 |
|
| 875 |
/** Find out maximum indexed column length by its table format. |
| 876 |
For ROW_FORMAT=REDUNDANT and ROW_FORMAT=COMPACT, the maximum |
| 877 |
field length is REC_ANTELOPE_MAX_INDEX_COL_LEN - 1 (767). For |
| 878 |
ROW_FORMAT=COMPRESSED and ROW_FORMAT=DYNAMIC, the length could |
| 879 |
be REC_VERSION_56_MAX_INDEX_COL_LEN (3072) bytes */ |
| 880 |
#define DICT_MAX_FIELD_LEN_BY_FORMAT(table) \ |
| 881 |
(dict_table_has_atomic_blobs(table) ? REC_VERSION_56_MAX_INDEX_COL_LEN \ |
| 882 |
: REC_ANTELOPE_MAX_INDEX_COL_LEN - 1) |
| 883 |
|
| 884 |
static inline uint32_t DICT_MAX_FIELD_LEN_BY_FORMAT_FLAG(uint32_t flags) { |
| 885 |
if (DICT_TF_HAS_ATOMIC_BLOBS(flags)) { |
| 886 |
return REC_VERSION_56_MAX_INDEX_COL_LEN; |
| 887 |
} |
| 888 |
return REC_ANTELOPE_MAX_INDEX_COL_LEN - 1; |
| 889 |
} |
| 890 |
|
| 891 |
/** Defines the maximum fixed length column size */ |
| 892 |
constexpr uint32_t DICT_MAX_FIXED_COL_LEN = DICT_ANTELOPE_MAX_INDEX_COL_LEN; |
| 893 |
|
| 894 |
/** Data structure for a field in an index */ |
| 895 |
struct dict_field_t { |
| 896 |
dict_field_t() : col(nullptr), prefix_len(0), fixed_len(0), is_ascending(0) {} |
| 897 |
|
| 898 |
dict_col_t *col; /*!< pointer to the table column */ |
| 899 |
id_name_t name; /*!< name of the column */ |
| 900 |
unsigned prefix_len : 12; /*!< 0 or the length of the column |
| 901 |
prefix in bytes in a MySQL index of |
| 902 |
type, e.g., INDEX (textcol(25)); |
| 903 |
must be smaller than |
| 904 |
DICT_MAX_FIELD_LEN_BY_FORMAT; |
| 905 |
NOTE that in the UTF-8 charset, MySQL |
| 906 |
sets this to (mbmaxlen * the prefix len) |
| 907 |
in UTF-8 chars */ |
| 908 |
unsigned fixed_len : 10; /*!< 0 or the fixed length of the |
| 909 |
column if smaller than |
| 910 |
DICT_ANTELOPE_MAX_INDEX_COL_LEN */ |
| 911 |
unsigned is_ascending : 1; /*!< 0=DESC, 1=ASC */ |
| 912 |
|
| 913 |
uint16_t get_phy_pos() const { |
| 914 |
if (prefix_len != 0) { |
| 915 |
return col->get_prefix_phy_pos(); |
| 916 |
} |
| 917 |
|
| 918 |
return col->get_col_phy_pos(); |
| 919 |
} |
| 920 |
}; |
| 921 |
|
| 922 |
/** PADDING HEURISTIC BASED ON LINEAR INCREASE OF PADDING TO AVOID |
| 923 |
COMPRESSION FAILURES |
| 924 |
(Note: this is relevant only for compressed indexes) |
| 925 |
GOAL: Avoid compression failures by maintaining information about the |
| 926 |
compressibility of data. If data is not very compressible then leave |
| 927 |
some extra space 'padding' in the uncompressed page making it more |
| 928 |
likely that compression of less than fully packed uncompressed page will |
| 929 |
succeed. |
| 930 |
|
| 931 |
This padding heuristic works by increasing the pad linearly until the |
| 932 |
desired failure rate is reached. A "round" is a fixed number of |
| 933 |
compression operations. |
| 934 |
After each round, the compression failure rate for that round is |
| 935 |
computed. If the failure rate is too high, then padding is incremented |
| 936 |
by a fixed value, otherwise it's left intact. |
| 937 |
If the compression failure is lower than the desired rate for a fixed |
| 938 |
number of consecutive rounds, then the padding is decreased by a fixed |
| 939 |
value. This is done to prevent overshooting the padding value, |
| 940 |
and to accommodate the possible change in data compressibility. */ |
| 941 |
|
| 942 |
/** Number of zip ops in one round. */ |
| 943 |
constexpr uint32_t ZIP_PAD_ROUND_LEN = 128; |
| 944 |
|
| 945 |
/** Number of successful rounds after which the padding is decreased */ |
| 946 |
constexpr uint32_t ZIP_PAD_SUCCESSFUL_ROUND_LIMIT = 5; |
| 947 |
|
| 948 |
/** Amount by which padding is increased. */ |
| 949 |
constexpr uint32_t ZIP_PAD_INCR = 128; |
| 950 |
|
| 951 |
/** Percentage of compression failures that are allowed in a single |
| 952 |
round */ |
| 953 |
extern ulong zip_failure_threshold_pct; |
| 954 |
|
| 955 |
/** Maximum percentage of a page that can be allowed as a pad to avoid |
| 956 |
compression failures */ |
| 957 |
extern ulong zip_pad_max; |
| 958 |
|
| 959 |
/** Data structure to hold information about about how much space in |
| 960 |
an uncompressed page should be left as padding to avoid compression |
| 961 |
failures. This estimate is based on a self-adapting heuristic. */ |
| 962 |
struct zip_pad_info_t { |
| 963 |
SysMutex *mutex; /*!< mutex protecting the info */ |
| 964 |
std::atomic<ulint> pad; /*!< number of bytes used as pad */ |
| 965 |
ulint success; /*!< successful compression ops during |
| 966 |
current round */ |
| 967 |
ulint failure; /*!< failed compression ops during |
| 968 |
current round */ |
| 969 |
ulint n_rounds; /*!< number of currently successful |
| 970 |
rounds */ |
| 971 |
#ifndef UNIV_HOTBACKUP |
| 972 |
std::atomic<os_once::state_t> mutex_created; |
| 973 |
/*!< Creation state of mutex member */ |
| 974 |
#endif /* !UNIV_HOTBACKUP */ |
| 975 |
}; |
| 976 |
|
| 977 |
/** If key is fixed length key then cache the record offsets on first |
| 978 |
computation. This will help save computation cycle that generate same |
| 979 |
redundant data. */ |
| 980 |
struct rec_cache_t { |
| 981 |
/** Holds reference to cached offsets for record. */ |
| 982 |
const ulint *offsets{nullptr}; |
| 983 |
|
| 984 |
/** Number of NULLable columns among those for which offsets are cached */ |
| 985 |
size_t nullable_cols{0}; |
| 986 |
}; |
| 987 |
|
| 988 |
/** Cache position of last inserted or selected record by caching record |
| 989 |
and holding reference to the block where record resides. |
| 990 |
Note: We don't commit mtr and hold it beyond a transaction lifetime as this is |
| 991 |
a special case (intrinsic table) that are not shared across connection. */ |
| 992 |
class last_ops_cur_t { |
| 993 |
public: |
| 994 |
/** Constructor */ |
| 995 |
last_ops_cur_t() : rec(), block(), mtr(), disable_caching(), invalid() { |
| 996 |
/* Do Nothing. */ |
| 997 |
} |
| 998 |
|
| 999 |
/* Commit mtr and re-initialize cache record and block to NULL. */ |
| 1000 |
void release() { |
| 1001 |
if (mtr.is_active()) { |
| 1002 |
mtr_commit(&mtr); |
| 1003 |
} |
| 1004 |
rec = nullptr; |
| 1005 |
block = nullptr; |
| 1006 |
invalid = false; |
| 1007 |
} |
| 1008 |
|
| 1009 |
public: |
| 1010 |
/** last inserted/selected record. */ |
| 1011 |
rec_t *rec; |
| 1012 |
|
| 1013 |
/** block where record reside. */ |
| 1014 |
buf_block_t *block; |
| 1015 |
|
| 1016 |
/** active mtr that will be re-used for next insert/select. */ |
| 1017 |
mtr_t mtr; |
| 1018 |
|
| 1019 |
/** disable caching. (disabled when table involves blob/text.) */ |
| 1020 |
bool disable_caching; |
| 1021 |
|
| 1022 |
/** If index structure is undergoing structural change viz. |
| 1023 |
split then invalidate the cached position as it would be no more |
| 1024 |
remain valid. Will be re-cached on post-split insert. */ |
| 1025 |
bool invalid; |
| 1026 |
}; |
| 1027 |
|
| 1028 |
/** "GEN_CLUST_INDEX" is the name reserved for InnoDB default |
| 1029 |
system clustered index when there is no primary key. */ |
| 1030 |
const char innobase_index_reserve_name[] = "GEN_CLUST_INDEX"; |
| 1031 |
|
| 1032 |
namespace dd { |
| 1033 |
class Spatial_reference_system; |
| 1034 |
} |
| 1035 |
|
| 1036 |
#ifdef UNIV_DEBUG |
| 1037 |
/** Value of dict_index_t::magic_n */ |
| 1038 |
constexpr uint32_t DICT_INDEX_MAGIC_N = 76789786; |
| 1039 |
#endif |
| 1040 |
|
| 1041 |
constexpr uint32_t DICT_INDEX_MERGE_THRESHOLD_DEFAULT = 50; |
| 1042 |
constexpr uint32_t MAX_KEY_LENGTH_BITS = 12; |
| 1043 |
|
| 1044 |
/** Data structure for an index. Most fields will be |
| 1045 |
initialized to 0, NULL or false in dict_mem_index_create(). */ |
| 1046 |
struct dict_index_t { |
| 1047 |
/** id of the index */ |
| 1048 |
space_index_t id; |
| 1049 |
|
| 1050 |
/** memory heap */ |
| 1051 |
mem_heap_t *heap; |
| 1052 |
|
| 1053 |
/** index name */ |
| 1054 |
id_name_t name; |
| 1055 |
|
| 1056 |
/** table name */ |
| 1057 |
const char *table_name; |
| 1058 |
|
| 1059 |
/** back pointer to table */ |
| 1060 |
dict_table_t *table; |
| 1061 |
|
| 1062 |
/** space where the index tree is placed */ |
| 1063 |
unsigned space : 32; |
| 1064 |
|
| 1065 |
/** index tree root page number */ |
| 1066 |
unsigned page : 32; |
| 1067 |
|
| 1068 |
/** In the pessimistic delete, if the page data size drops below this limit |
| 1069 |
in percent, merging it to a neighbor is tried */ |
| 1070 |
unsigned merge_threshold : 6; |
| 1071 |
|
| 1072 |
/** index type (DICT_CLUSTERED, DICT_UNIQUE, DICT_IBUF, DICT_CORRUPT) */ |
| 1073 |
unsigned type : DICT_IT_BITS; |
| 1074 |
|
| 1075 |
/** position of the trx id column in a clustered index record, if the fields |
| 1076 |
before it are known to be of a fixed size, 0 otherwise */ |
| 1077 |
unsigned trx_id_offset : MAX_KEY_LENGTH_BITS; |
| 1078 |
|
| 1079 |
static_assert(1 << MAX_KEY_LENGTH_BITS >= MAX_KEY_LENGTH, |
| 1080 |
"1<<MAX_KEY_LENGTH_BITS) < MAX_KEY_LENGTH"); |
| 1081 |
|
| 1082 |
/** number of columns the user defined to be in the index: in the internal |
| 1083 |
representation we add more columns */ |
| 1084 |
unsigned n_user_defined_cols : 10; |
| 1085 |
|
| 1086 |
/** if true, allow duplicate values even if index is created with unique |
| 1087 |
constraint */ |
| 1088 |
unsigned allow_duplicates : 1; |
| 1089 |
|
| 1090 |
/** if true, SQL NULL == SQL NULL */ |
| 1091 |
unsigned nulls_equal : 1; |
| 1092 |
|
| 1093 |
/** if true, then disable AHI. Currently limited to intrinsic temporary table |
| 1094 |
and SDI table as index id is not unique for such table which is one of the |
| 1095 |
validation criterion for ahi. */ |
| 1096 |
unsigned disable_ahi : 1; |
| 1097 |
|
| 1098 |
/** number of fields from the beginning which are enough to determine an index |
| 1099 |
entry uniquely */ |
| 1100 |
unsigned n_uniq : 10; |
| 1101 |
|
| 1102 |
/** number of fields defined so far */ |
| 1103 |
unsigned n_def : 10; |
| 1104 |
|
| 1105 |
/** number of fields in the index */ |
| 1106 |
unsigned n_fields : 10; |
| 1107 |
|
| 1108 |
/** number of total fields in the index (including INSTANT dropped fields) */ |
| 1109 |
unsigned n_total_fields : 10; |
| 1110 |
|
| 1111 |
/** number of nullable fields */ |
| 1112 |
unsigned n_nullable : 10; |
| 1113 |
|
| 1114 |
/** number of nullable fields before first instant ADD COLUMN applied to this |
| 1115 |
table. This is valid only when has_instant_cols() is true */ |
| 1116 |
unsigned n_instant_nullable : 10; |
| 1117 |
|
| 1118 |
/** true if the index object is in the dictionary cache */ |
| 1119 |
unsigned cached : 1; |
| 1120 |
|
| 1121 |
/** true if the index is to be dropped; protected by dict_operation_lock */ |
| 1122 |
unsigned to_be_dropped : 1; |
| 1123 |
|
| 1124 |
/** enum online_index_status. Transitions from ONLINE_INDEX_COMPLETE (to |
| 1125 |
ONLINE_INDEX_CREATION) are protected by dict_operation_lock and |
| 1126 |
dict_sys->mutex. Other changes are protected by index->lock. */ |
| 1127 |
unsigned online_status : 2; |
| 1128 |
|
| 1129 |
/** a flag that is set for secondary indexes that have not been committed to |
| 1130 |
the data dictionary yet */ |
| 1131 |
unsigned uncommitted : 1; |
| 1132 |
|
| 1133 |
/** true if the index is clustered index and it has some instant columns */ |
| 1134 |
unsigned instant_cols : 1; |
| 1135 |
|
| 1136 |
/** true if the index is clustered index and table has row versions */ |
| 1137 |
unsigned row_versions : 1; |
| 1138 |
|
| 1139 |
/** spatial reference id */ |
| 1140 |
uint32_t srid; |
| 1141 |
|
| 1142 |
/** says whether SRID is valid - it cane be undefined */ |
| 1143 |
bool srid_is_valid; |
| 1144 |
|
| 1145 |
/** Cached spatial reference system dictionary entry used by R-tree indexes. |
| 1146 |
*/ |
| 1147 |
std::unique_ptr<dd::Spatial_reference_system> rtr_srs; |
| 1148 |
|
| 1149 |
#ifdef UNIV_DEBUG |
| 1150 |
uint32_t magic_n; /*!< magic number */ |
| 1151 |
#endif |
| 1152 |
|
| 1153 |
/** array of field descriptions */ |
| 1154 |
dict_field_t *fields; |
| 1155 |
|
| 1156 |
/** Array of field pos sorted as per their physical pos in record. Only |
| 1157 |
needed for clustered index having INSTANT ADD/DROP columns. */ |
| 1158 |
std::vector<uint16_t> fields_array; |
| 1159 |
|
| 1160 |
/** Number of nullable columns in each version. Only needed for clustered |
| 1161 |
index having INSTANT ADD/DROP columns. */ |
| 1162 |
uint32_t nullables[MAX_ROW_VERSION + 1] = {0}; |
| 1163 |
|
| 1164 |
#ifndef UNIV_HOTBACKUP |
| 1165 |
/** fulltext parser plugin */ |
| 1166 |
st_mysql_ftparser *parser; |
| 1167 |
|
| 1168 |
/** true if it's ngram parser */ |
| 1169 |
bool is_ngram; |
| 1170 |
|
| 1171 |
/** whether it has a newly added virtual column in ALTER */ |
| 1172 |
bool has_new_v_col; |
| 1173 |
|
| 1174 |
/** if the index is an hidden index */ |
| 1175 |
bool hidden; |
| 1176 |
#endif /* !UNIV_HOTBACKUP */ |
| 1177 |
|
| 1178 |
/** list of indexes of the table */ |
| 1179 |
UT_LIST_NODE_T(dict_index_t) indexes; |
| 1180 |
|
| 1181 |
/** info used in optimistic searches */ |
| 1182 |
btr_search_t *search_info; |
| 1183 |
|
| 1184 |
#ifndef UNIV_HOTBACKUP |
| 1185 |
/** the log of modifications during online index creation; |
| 1186 |
valid when online_status is ONLINE_INDEX_CREATION */ |
| 1187 |
row_log_t *online_log; |
| 1188 |
|
| 1189 |
/*----------------------*/ |
| 1190 |
/** Statistics for query optimization */ |
| 1191 |
/** @{ */ |
| 1192 |
/** approximate number of different key values for this index, for each |
| 1193 |
n-column prefix where 1 <= n <= dict_get_n_unique(index) (the array is |
| 1194 |
indexed from 0 to n_uniq-1); we periodically calculate new estimates */ |
| 1195 |
uint64_t *stat_n_diff_key_vals; |
| 1196 |
|
| 1197 |
/** number of pages that were sampled to calculate each of |
| 1198 |
stat_n_diff_key_vals[], e.g. stat_n_sample_sizes[3] pages were sampled to get |
| 1199 |
the number stat_n_diff_key_vals[3]. */ |
| 1200 |
uint64_t *stat_n_sample_sizes; |
| 1201 |
|
| 1202 |
/* approximate number of non-null key values for this index, for each column |
| 1203 |
where 1 <= n <= dict_get_n_unique(index) (the array is indexed from 0 to |
| 1204 |
n_uniq-1); This is used when innodb_stats_method is "nulls_ignored". */ |
| 1205 |
uint64_t *stat_n_non_null_key_vals; |
| 1206 |
|
| 1207 |
/** approximate index size in database pages */ |
| 1208 |
ulint stat_index_size; |
| 1209 |
#endif /* !UNIV_HOTBACKUP */ |
| 1210 |
/** approximate number of leaf pages in the index tree */ |
| 1211 |
ulint stat_n_leaf_pages; |
| 1212 |
/** @} */ |
| 1213 |
|
| 1214 |
/** cache the last insert position. Currently limited to auto-generated |
| 1215 |
clustered index on intrinsic table only. */ |
| 1216 |
last_ops_cur_t *last_ins_cur; |
| 1217 |
|
| 1218 |
/** cache the last selected position. Currently limited to intrinsic table |
| 1219 |
only. */ |
| 1220 |
last_ops_cur_t *last_sel_cur; |
| 1221 |
|
| 1222 |
/** cache the field that needs to be re-computed on each insert. Limited to |
| 1223 |
intrinsic table as this is common share and can't be used without protection |
| 1224 |
if table is accessible to multiple-threads. */ |
| 1225 |
rec_cache_t rec_cache; |
| 1226 |
|
| 1227 |
/** Node sequence number for RTree */ |
| 1228 |
rtr_ssn_t rtr_ssn; |
| 1229 |
|
| 1230 |
/** tracking all R-Tree search cursors */ |
| 1231 |
rtr_info_track_t *rtr_track; |
| 1232 |
|
| 1233 |
/** id of the transaction that created this index, or 0 if the index existed |
| 1234 |
when InnoDB was started up */ |
| 1235 |
trx_id_t trx_id; |
| 1236 |
|
| 1237 |
/** Information about state of compression failures and successes */ |
| 1238 |
zip_pad_info_t zip_pad; |
| 1239 |
|
| 1240 |
/** read-write lock protecting the upper levels of the index tree */ |
| 1241 |
rw_lock_t lock; |
| 1242 |
|
| 1243 |
/** Flag whether need to fill dd tables when it's a fulltext index. */ |
| 1244 |
bool fill_dd; |
| 1245 |
|
| 1246 |
/** Set instant nullable |
| 1247 |
@param[in] n nullable fields before first INSTANT ADD */ |
| 1248 |
void set_instant_nullable(uint16_t n) { n_instant_nullable = n; } |
| 1249 |
|
| 1250 |
/** Get instant nullable. |
| 1251 |
@return number of nullable fields before first INSTANT ADD */ |
| 1252 |
uint16_t get_instant_nullable() const { return n_instant_nullable; } |
| 1253 |
|
| 1254 |
/** Get the nullable fields before any INSTANT ADD/DROP |
| 1255 |
@return number of nullable fields */ |
| 1256 |
uint16_t get_nullable_before_instant_add_drop() const { |
| 1257 |
if (has_instant_cols()) { |
| 1258 |
return get_instant_nullable(); |
| 1259 |
} |
| 1260 |
|
| 1261 |
if (has_row_versions()) { |
| 1262 |
return get_nullable_in_version(0); |
| 1263 |
} |
| 1264 |
|
| 1265 |
return n_nullable; |
| 1266 |
} |
| 1267 |
|
| 1268 |
/** Determine if the index has been committed to the |
| 1269 |
data dictionary. |
| 1270 |
@return whether the index definition has been committed */ |
| 1271 |
bool is_committed() const { |
| 1272 |
ut_ad(!uncommitted || !(type & DICT_CLUSTERED)); |
| 1273 |
return (UNIV_LIKELY(!uncommitted)); |
| 1274 |
} |
| 1275 |
|
| 1276 |
/** Flag an index committed or uncommitted. |
| 1277 |
@param[in] committed whether the index is committed */ |
| 1278 |
void set_committed(bool committed) { |
| 1279 |
ut_ad(!to_be_dropped); |
| 1280 |
ut_ad(committed || !(type & DICT_CLUSTERED)); |
| 1281 |
uncommitted = !committed; |
| 1282 |
} |
| 1283 |
|
| 1284 |
/** Get the next index. |
| 1285 |
@return next index |
| 1286 |
@retval NULL if this was the last index */ |
| 1287 |
const dict_index_t *next() const { |
| 1288 |
const dict_index_t *next = UT_LIST_GET_NEXT(indexes, this); |
| 1289 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 1290 |
return (next); |
| 1291 |
} |
| 1292 |
/** Get the next index. |
| 1293 |
@return next index |
| 1294 |
@retval NULL if this was the last index */ |
| 1295 |
dict_index_t *next() { |
| 1296 |
return (const_cast<dict_index_t *>( |
| 1297 |
const_cast<const dict_index_t *>(this)->next())); |
| 1298 |
} |
| 1299 |
|
| 1300 |
/** Check whether the index is corrupted. |
| 1301 |
@return true if index is corrupted, otherwise false */ |
| 1302 |
bool is_corrupted() const { |
| 1303 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 1304 |
|
| 1305 |
return (type & DICT_CORRUPT); |
| 1306 |
} |
| 1307 |
|
| 1308 |
/* Check whether the index is the clustered index |
| 1309 |
@return nonzero for clustered index, zero for other indexes */ |
| 1310 |
|
| 1311 |
bool is_clustered() const { |
| 1312 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 1313 |
|
| 1314 |
return (type & DICT_CLUSTERED); |
| 1315 |
} |
| 1316 |
|
| 1317 |
/** Check whether the index is the multi-value index |
| 1318 |
@return nonzero for multi-value index, zero for other indexes */ |
| 1319 |
bool is_multi_value() const { |
| 1320 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 1321 |
|
| 1322 |
return (type & DICT_MULTI_VALUE); |
| 1323 |
} |
| 1324 |
|
| 1325 |
/** Returns the minimum data size of an index record. |
| 1326 |
@return minimum data size in bytes */ |
| 1327 |
ulint get_min_size() const { |
| 1328 |
ulint size = 0; |
| 1329 |
|
| 1330 |
for (unsigned i = 0; i < n_fields; i++) { |
| 1331 |
size += get_col(i)->get_min_size(); |
| 1332 |
} |
| 1333 |
|
| 1334 |
return (size); |
| 1335 |
} |
| 1336 |
|
| 1337 |
/** Check whether index can be used by transaction |
| 1338 |
@param[in] trx transaction*/ |
| 1339 |
bool is_usable(const trx_t *trx) const; |
| 1340 |
|
| 1341 |
/** Check whether index has any instantly added columns. |
| 1342 |
Possible only if table has INSTANT ADD columns and is upgraded. |
| 1343 |
@return true if this is instant affected, otherwise false */ |
| 1344 |
bool has_instant_cols() const { return (instant_cols); } |
| 1345 |
|
| 1346 |
/** Check whether index belongs to a table having row versions |
| 1347 |
@return true if table has row versions, otherwise false */ |
| 1348 |
bool has_row_versions() const { return (row_versions); } |
| 1349 |
|
| 1350 |
/** check if either instant or versioned. |
| 1351 |
@return true if table has row versions or instant cols, otherwise false */ |
| 1352 |
bool has_instant_cols_or_row_versions() const { |
| 1353 |
if (!is_clustered()) { |
| 1354 |
ut_ad(!has_row_versions() && !has_instant_cols()); |
| 1355 |
return false; |
| 1356 |
} |
| 1357 |
|
| 1358 |
return (has_row_versions() || has_instant_cols()); |
| 1359 |
} |
| 1360 |
|
| 1361 |
/** Check if tuple is having instant format. |
| 1362 |
@param[in] n_fields_in_tuple number of fields in tuple |
| 1363 |
@return true if yes, false otherwise. */ |
| 1364 |
bool is_tuple_instant_format(const uint16_t n_fields_in_tuple) const; |
| 1365 |
|
| 1366 |
/** Returns the number of nullable fields before specified nth field |
| 1367 |
@param[in] nth nth field to check */ |
| 1368 |
uint32_t get_n_nullable_before(uint32_t nth) const { |
| 1369 |
uint32_t nullable = 0; |
| 1370 |
ut_ad(nth <= n_total_fields); |
| 1371 |
|
| 1372 |
for (size_t i = 0; i < nth; ++i) { |
| 1373 |
dict_col_t *col = get_field(i)->col; |
| 1374 |
|
| 1375 |
ut_ad(!col->is_instant_dropped()); |
| 1376 |
|
| 1377 |
if (col->is_nullable()) { |
| 1378 |
nullable++; |
| 1379 |
} |
| 1380 |
} |
| 1381 |
|
| 1382 |
return (nullable); |
| 1383 |
} |
| 1384 |
|
| 1385 |
/** Returns total fields including INSTANT DROP fields. */ |
| 1386 |
uint32_t get_n_total_fields() const { |
| 1387 |
ut_ad(is_clustered()); |
| 1388 |
return n_total_fields; |
| 1389 |
} |
| 1390 |
|
| 1391 |
/** Returns the number of fields before first instant ADD COLUMN. This is |
| 1392 |
needed only for V1 INSTANT ADD. */ |
| 1393 |
uint32_t get_instant_fields() const; |
| 1394 |
|
| 1395 |
size_t calculate_n_instant_nullable(size_t _n_fields) const { |
| 1396 |
if (!has_row_versions()) { |
| 1397 |
ut_ad(has_instant_cols()); |
| 1398 |
return get_n_nullable_before(_n_fields); |
| 1399 |
} |
| 1400 |
|
| 1401 |
size_t n_drop_nullable_cols = 0; |
| 1402 |
size_t new_n_nullable = 0; |
| 1403 |
for (size_t i = 0; i < n_def; i++) { |
| 1404 |
const dict_field_t *field = &fields[i]; |
| 1405 |
const dict_col_t *col = field->col; |
| 1406 |
|
| 1407 |
if (col->is_instant_added()) { |
| 1408 |
continue; |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (col->is_instant_dropped()) { |
| 1412 |
if (col->get_col_phy_pos() < _n_fields && col->is_nullable()) { |
| 1413 |
n_drop_nullable_cols++; |
| 1414 |
} |
| 1415 |
continue; |
| 1416 |
} |
| 1417 |
|
| 1418 |
/* This is regular column */ |
| 1419 |
if (col->get_col_phy_pos() < _n_fields) { |
| 1420 |
if (col->is_nullable()) { |
| 1421 |
new_n_nullable++; |
| 1422 |
} |
| 1423 |
} |
| 1424 |
} |
| 1425 |
|
| 1426 |
new_n_nullable += n_drop_nullable_cols; |
| 1427 |
|
| 1428 |
return new_n_nullable; |
| 1429 |
} |
| 1430 |
|
| 1431 |
/** Create nullables array. |
| 1432 |
@param[in] current_row_version current row version of table */ |
| 1433 |
void create_nullables(uint32_t current_row_version); |
| 1434 |
|
| 1435 |
/** Return nullable in a specific row version */ |
| 1436 |
uint32_t get_nullable_in_version(uint8_t version) const { |
| 1437 |
ut_ad(is_valid_row_version(version)); |
| 1438 |
|
| 1439 |
return nullables[version]; |
| 1440 |
} |
| 1441 |
|
| 1442 |
/** Create fields array sorted by phy_pos of field in row */ |
| 1443 |
void create_fields_array() { |
| 1444 |
fields_array.resize(n_def); |
| 1445 |
for (uint32_t i = 0; i < n_def; i++) { |
| 1446 |
dict_field_t *field = get_field(i); |
| 1447 |
ut_ad(field != nullptr && field->col != nullptr); |
| 1448 |
|
| 1449 |
size_t pos = field->get_phy_pos(); |
| 1450 |
|
| 1451 |
fields_array[pos] = i; |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
void destroy_fields_array() { |
| 1456 |
/* The dict_index_t destructor is never called. The object is "destructed" |
| 1457 |
manually in dict_mem_index_free() and then the memory is just freed. This |
| 1458 |
method is called from the mentioned dict_mem_index_free(). Please note that |
| 1459 |
this vector is never constructed either - we just zero the memory and start |
| 1460 |
using it after calling a "constructor" dict_mem_fill_index_struct(). */ |
| 1461 |
fields_array.~vector<uint16_t>(); |
| 1462 |
} |
| 1463 |
|
| 1464 |
/** Adds a field definition to an index. NOTE: does not take a copy |
| 1465 |
of the column name if the field is a column. The memory occupied |
| 1466 |
by the column name may be released only after publishing the index. |
| 1467 |
@param[in] name_arg column name |
| 1468 |
@param[in] prefix_len 0 or the column prefix length in a MySQL index |
| 1469 |
like INDEX (textcol(25)) |
| 1470 |
@param[in] is_ascending true=ASC, false=DESC */ |
| 1471 |
void add_field(const char *name_arg, ulint prefix_len, bool is_ascending) { |
| 1472 |
dict_field_t *field; |
| 1473 |
|
| 1474 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 1475 |
|
| 1476 |
n_def++; |
| 1477 |
|
| 1478 |
field = get_field(n_def - 1); |
| 1479 |
|
| 1480 |
field->name = name_arg; |
| 1481 |
field->prefix_len = (unsigned int)prefix_len; |
| 1482 |
field->is_ascending = is_ascending; |
| 1483 |
} |
| 1484 |
|
| 1485 |
/** Gets the nth physical pos field. |
| 1486 |
@param[in] pos physical position of the field |
| 1487 |
@return pointer to the field object. */ |
| 1488 |
dict_field_t *get_physical_field(size_t pos) const { |
| 1489 |
ut_ad(pos < n_def); |
| 1490 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 1491 |
|
| 1492 |
if (has_row_versions()) { |
| 1493 |
return get_field(fields_array[pos]); |
| 1494 |
} |
| 1495 |
|
| 1496 |
return get_field(pos); |
| 1497 |
} |
| 1498 |
|
| 1499 |
/** Gets the nth field of an index. |
| 1500 |
@param[in] pos position of field |
| 1501 |
@return pointer to field object */ |
| 1502 |
dict_field_t *get_field(ulint pos) const { |
| 1503 |
ut_ad(pos < n_def); |
| 1504 |
ut_ad(magic_n == DICT_INDEX_MAGIC_N); |
| 1505 |
|
| 1506 |
return (fields + pos); |
| 1507 |
} |
| 1508 |
|
| 1509 |
/** Given the physical position, find the logical position of field. |
| 1510 |
@param[in] phy_pos physical position of field |
| 1511 |
@return logical position of field */ |
| 1512 |
uint16_t get_logical_pos(uint16_t phy_pos) const { |
| 1513 |
for (size_t i = 0; i < n_def; i++) { |
| 1514 |
if (get_field(i)->get_phy_pos() == phy_pos) { |
| 1515 |
return i; |
| 1516 |
} |
| 1517 |
} |
| 1518 |
ut_ad(false); |
| 1519 |
return UINT16_UNDEFINED; |
| 1520 |
} |
| 1521 |
|
| 1522 |
/** Get the physical position of a field on a row. For table having INSTANT |
| 1523 |
column, it might differ from field index (pos). |
| 1524 |
@param[in] pos field index |
| 1525 |
@return physical position on row */ |
| 1526 |
uint16_t get_field_off_pos(ulint pos) const { |
| 1527 |
return get_field(pos)->get_phy_pos(); |
| 1528 |
} |
| 1529 |
|
| 1530 |
uint16_t get_field_phy_pos(ulint pos, uint8_t version) const { |
| 1531 |
uint16_t phy_pos = get_field(pos)->get_phy_pos(); |
| 1532 |
if (version == UINT8_UNDEFINED) { |
| 1533 |
return phy_pos; |
| 1534 |
} |
| 1535 |
|
| 1536 |
uint16_t res = phy_pos; |
| 1537 |
for (size_t i = 0; i < phy_pos; i++) { |
| 1538 |
if (get_field(fields_array[i])->col->is_dropped_in_or_before(version)) { |
| 1539 |
res--; |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
return res; |
| 1544 |
} |
| 1545 |
|
| 1546 |
/** Gets pointer to the nth column in an index. |
| 1547 |
@param[in] pos position of the field |
| 1548 |
@return column */ |
| 1549 |
const dict_col_t *get_col(ulint pos) const { return (get_field(pos)->col); } |
| 1550 |
|
| 1551 |
/** Gets the column number the nth field in an index. |
| 1552 |
@param[in] pos position of the field |
| 1553 |
@return column number */ |
| 1554 |
ulint get_col_no(ulint pos) const; |
| 1555 |
|
| 1556 |
/** Returns the position of a system column in an index. |
| 1557 |
@param[in] type DATA_ROW_ID, ... |
| 1558 |
@return position, ULINT_UNDEFINED if not contained */ |
| 1559 |
ulint get_sys_col_pos(ulint type) const; |
| 1560 |
|
| 1561 |
/** Looks for column n in an index. |
| 1562 |
@param[in] n column number |
| 1563 |
@param[in] inc_prefix true=consider column prefixes too |
| 1564 |
@param[in] is_virtual true==virtual column |
| 1565 |
@return position in internal representation of the index; |
| 1566 |
ULINT_UNDEFINED if not contained */ |
| 1567 |
ulint get_col_pos(ulint n, bool inc_prefix = false, |
| 1568 |
bool is_virtual = false) const; |
| 1569 |
|
| 1570 |
/** Get the default value of nth field and its length if exists. |
| 1571 |
If not exists, both the return value is nullptr and length is 0. |
| 1572 |
@param[in] nth nth field to get |
| 1573 |
@param[in,out] length length of the default value |
| 1574 |
@return the default value data of nth field */ |
| 1575 |
const byte *get_nth_default(ulint nth, ulint *length) const { |
| 1576 |
ut_ad(nth < get_n_total_fields()); |
| 1577 |
|
| 1578 |
const dict_col_t *col = get_physical_field(nth)->col; |
| 1579 |
if (col->instant_default == nullptr) { |
| 1580 |
*length = 0; |
| 1581 |
return (nullptr); |
| 1582 |
} |
| 1583 |
|
| 1584 |
*length = col->instant_default->len; |
| 1585 |
ut_ad(*length == 0 || *length == UNIV_SQL_NULL || |
| 1586 |
col->instant_default->value != nullptr); |
| 1587 |
return (col->instant_default->value); |
| 1588 |
} |
| 1589 |
|
| 1590 |
/** Sets srid and srid_is_valid values |
| 1591 |
@param[in] srid_value value of SRID, may be garbage |
| 1592 |
if srid_is_valid_value = false |
| 1593 |
@param[in] srid_is_valid_value value of srid_is_valid */ |
| 1594 |
void fill_srid_value(uint32_t srid_value, bool srid_is_valid_value) { |
| 1595 |
srid_is_valid = srid_is_valid_value; |
| 1596 |
srid = srid_value; |
| 1597 |
} |
| 1598 |
|
| 1599 |
/** Check if the underlying table is compressed. |
| 1600 |
@return true if compressed, false otherwise. */ |
| 1601 |
bool is_compressed() const; |
| 1602 |
|
| 1603 |
/** Check if a multi-value index is built on specified multi-value |
| 1604 |
virtual column. Please note that there could be only one multi-value |
| 1605 |
virtual column on the multi-value index, but not necessary the first |
| 1606 |
field of the index. |
| 1607 |
@param[in] mv_col multi-value virtual column |
| 1608 |
@return non-zero means the column is on the index and this is the |
| 1609 |
nth position of the column, zero means it's not on the index */ |
| 1610 |
uint32_t has_multi_value_col(const dict_v_col_t *mv_col) const { |
| 1611 |
ut_ad(is_multi_value()); |
| 1612 |
for (uint32_t i = 0; i < n_fields; ++i) { |
| 1613 |
const dict_col_t *col = get_col(i); |
| 1614 |
if (mv_col->m_col.ind == col->ind) { |
| 1615 |
return (i + 1); |
| 1616 |
} |
| 1617 |
|
| 1618 |
/* Only one multi-value field, if not match then no match. */ |
| 1619 |
if (col->is_multi_value()) { |
| 1620 |
break; |
| 1621 |
} |
| 1622 |
} |
| 1623 |
|
| 1624 |
return (0); |
| 1625 |
} |
| 1626 |
|
| 1627 |
public: |
| 1628 |
/** Get the page size of the tablespace to which this index belongs. |
| 1629 |
@return the page size. */ |
| 1630 |
page_size_t get_page_size() const; |
| 1631 |
|
| 1632 |
/** Get the space id of the tablespace to which this index belongs. |
| 1633 |
@return the space id. */ |
| 1634 |
space_id_t space_id() const { return space; } |
| 1635 |
}; |
| 1636 |
|
| 1637 |
/** The status of online index creation */ |
| 1638 |
enum online_index_status { |
| 1639 |
/** the index is complete and ready for access */ |
| 1640 |
ONLINE_INDEX_COMPLETE = 0, |
| 1641 |
/** the index is being created, online |
| 1642 |
(allowing concurrent modifications) */ |
| 1643 |
ONLINE_INDEX_CREATION, |
| 1644 |
/** secondary index creation was aborted and the index |
| 1645 |
should be dropped as soon as index->table->n_ref_count reaches 0, |
| 1646 |
or online table rebuild was aborted and the clustered index |
| 1647 |
of the original table should soon be restored to |
| 1648 |
ONLINE_INDEX_COMPLETE */ |
| 1649 |
ONLINE_INDEX_ABORTED, |
| 1650 |
/** the online index creation was aborted, the index was |
| 1651 |
dropped from the data dictionary and the tablespace, and it |
| 1652 |
should be dropped from the data dictionary cache as soon as |
| 1653 |
index->table->n_ref_count reaches 0. */ |
| 1654 |
ONLINE_INDEX_ABORTED_DROPPED |
| 1655 |
}; |
| 1656 |
|
| 1657 |
/** Set to store the virtual columns which are affected by Foreign |
| 1658 |
key constraint. */ |
| 1659 |
typedef std::set<dict_v_col_t *, std::less<dict_v_col_t *>, |
| 1660 |
ut::allocator<dict_v_col_t *>> |
| 1661 |
dict_vcol_set; |
| 1662 |
|
| 1663 |
/** Data structure for a foreign key constraint; an example: |
| 1664 |
FOREIGN KEY (A, B) REFERENCES TABLE2 (C, D). Most fields will be |
| 1665 |
initialized to 0, NULL or false in dict_mem_foreign_create(). */ |
| 1666 |
struct dict_foreign_t { |
| 1667 |
mem_heap_t *heap; /*!< this object is allocated from |
| 1668 |
this memory heap */ |
| 1669 |
char *id; /*!< id of the constraint as a |
| 1670 |
null-terminated string */ |
| 1671 |
unsigned n_fields : 10; /*!< number of indexes' first fields |
| 1672 |
for which the foreign key |
| 1673 |
constraint is defined: we allow the |
| 1674 |
indexes to contain more fields than |
| 1675 |
mentioned in the constraint, as long |
| 1676 |
as the first fields are as mentioned */ |
| 1677 |
unsigned type : 6; /*!< 0 or DICT_FOREIGN_ON_DELETE_CASCADE |
| 1678 |
or DICT_FOREIGN_ON_DELETE_SET_NULL */ |
| 1679 |
char *foreign_table_name; /*!< foreign table name */ |
| 1680 |
char *foreign_table_name_lookup; |
| 1681 |
/*!< foreign table name used for dict lookup */ |
| 1682 |
dict_table_t *foreign_table; /*!< table where the foreign key is */ |
| 1683 |
const char **foreign_col_names; /*!< names of the columns in the |
| 1684 |
foreign key */ |
| 1685 |
char *referenced_table_name; /*!< referenced table name */ |
| 1686 |
char *referenced_table_name_lookup; |
| 1687 |
/*!< referenced table name for dict lookup*/ |
| 1688 |
dict_table_t *referenced_table; /*!< table where the referenced key |
| 1689 |
is */ |
| 1690 |
const char **referenced_col_names; /*!< names of the referenced |
| 1691 |
columns in the referenced table */ |
| 1692 |
dict_index_t *foreign_index; /*!< foreign index; we require that |
| 1693 |
both tables contain explicitly defined |
| 1694 |
indexes for the constraint: InnoDB |
| 1695 |
does not generate new indexes |
| 1696 |
implicitly */ |
| 1697 |
dict_index_t *referenced_index; /*!< referenced index */ |
| 1698 |
|
| 1699 |
dict_vcol_set *v_cols; /*!< set of virtual columns affected |
| 1700 |
by foreign key constraint. */ |
| 1701 |
|
| 1702 |
/** Check whether foreign key constraint contains a column with a full |
| 1703 |
index on it. The function is used in the context of cascading DML |
| 1704 |
operations. |
| 1705 |
@retval true if the column has FTS index on it. |
| 1706 |
@retval false if the FK table has no FTS index or has self referential |
| 1707 |
relationship |
| 1708 |
*/ |
| 1709 |
bool is_fts_col_affected() const; |
| 1710 |
}; |
| 1711 |
|
| 1712 |
std::ostream &operator<<(std::ostream &out, const dict_foreign_t &foreign); |
| 1713 |
|
| 1714 |
struct dict_foreign_print { |
| 1715 |
dict_foreign_print(std::ostream &out) : m_out(out) {} |
| 1716 |
|
| 1717 |
void operator()(const dict_foreign_t *foreign) { m_out << *foreign; } |
| 1718 |
|
| 1719 |
private: |
| 1720 |
std::ostream &m_out; |
| 1721 |
}; |
| 1722 |
|
| 1723 |
/** Compare two dict_foreign_t objects using their ids. Used in the ordering |
| 1724 |
of dict_table_t::foreign_set and dict_table_t::referenced_set. It returns |
| 1725 |
true if the first argument is considered to go before the second in the |
| 1726 |
strict weak ordering it defines, and false otherwise. */ |
| 1727 |
struct dict_foreign_compare { |
| 1728 |
bool operator()(const dict_foreign_t *lhs, const dict_foreign_t *rhs) const { |
| 1729 |
return (ut_strcmp(lhs->id, rhs->id) < 0); |
| 1730 |
} |
| 1731 |
}; |
| 1732 |
|
| 1733 |
/** A function object to find a foreign key with the given index as the |
| 1734 |
referenced index. Return the foreign key with matching criteria or NULL */ |
| 1735 |
struct dict_foreign_with_index { |
| 1736 |
dict_foreign_with_index(const dict_index_t *index) : m_index(index) {} |
| 1737 |
|
| 1738 |
bool operator()(const dict_foreign_t *foreign) const { |
| 1739 |
return (foreign->referenced_index == m_index); |
| 1740 |
} |
| 1741 |
|
| 1742 |
const dict_index_t *m_index; |
| 1743 |
}; |
| 1744 |
|
| 1745 |
/* A function object to check if the foreign constraint is between different |
| 1746 |
tables. Returns true if foreign key constraint is between different tables, |
| 1747 |
false otherwise. */ |
| 1748 |
struct dict_foreign_different_tables { |
| 1749 |
bool operator()(const dict_foreign_t *foreign) const { |
| 1750 |
return (foreign->foreign_table != foreign->referenced_table); |
| 1751 |
} |
| 1752 |
}; |
| 1753 |
|
| 1754 |
typedef std::set<dict_foreign_t *, dict_foreign_compare, |
| 1755 |
ut::allocator<dict_foreign_t *>> |
| 1756 |
dict_foreign_set; |
| 1757 |
|
| 1758 |
std::ostream &operator<<(std::ostream &out, const dict_foreign_set &fk_set); |
| 1759 |
|
| 1760 |
/** Function object to check if a foreign key object is there |
| 1761 |
in the given foreign key set or not. It returns true if the |
| 1762 |
foreign key is not found, false otherwise */ |
| 1763 |
struct dict_foreign_not_exists { |
| 1764 |
dict_foreign_not_exists(const dict_foreign_set &obj_) : m_foreigns(obj_) {} |
| 1765 |
|
| 1766 |
/* Return true if the given foreign key is not found */ |
| 1767 |
bool operator()(dict_foreign_t *const &foreign) const { |
| 1768 |
return (m_foreigns.find(foreign) == m_foreigns.end()); |
| 1769 |
} |
| 1770 |
|
| 1771 |
private: |
| 1772 |
const dict_foreign_set &m_foreigns; |
| 1773 |
}; |
| 1774 |
|
| 1775 |
/** Validate the search order in the foreign key set. |
| 1776 |
@param[in] fk_set the foreign key set to be validated |
| 1777 |
@return true if search order is fine in the set, false otherwise. */ |
| 1778 |
bool dict_foreign_set_validate(const dict_foreign_set &fk_set); |
| 1779 |
|
| 1780 |
/** Validate the search order in the foreign key sets of the table |
| 1781 |
(foreign_set and referenced_set). |
| 1782 |
@param[in] table table whose foreign key sets are to be validated |
| 1783 |
@return true if foreign key sets are fine, false otherwise. */ |
| 1784 |
bool dict_foreign_set_validate(const dict_table_t &table); |
| 1785 |
|
| 1786 |
/** Frees a foreign key struct. */ |
| 1787 |
inline void dict_foreign_free( |
| 1788 |
dict_foreign_t *foreign) /*!< in, own: foreign key struct */ |
| 1789 |
{ |
| 1790 |
if (foreign->v_cols != nullptr) { |
| 1791 |
ut::delete_(foreign->v_cols); |
| 1792 |
} |
| 1793 |
|
| 1794 |
mem_heap_free(foreign->heap); |
| 1795 |
} |
| 1796 |
|
| 1797 |
/** The destructor will free all the foreign key constraints in the set |
| 1798 |
by calling dict_foreign_free() on each of the foreign key constraints. |
| 1799 |
This is used to free the allocated memory when a local set goes out |
| 1800 |
of scope. */ |
| 1801 |
struct dict_foreign_set_free { |
| 1802 |
dict_foreign_set_free(const dict_foreign_set &foreign_set) |
| 1803 |
: m_foreign_set(foreign_set) {} |
| 1804 |
|
| 1805 |
~dict_foreign_set_free() { |
| 1806 |
std::for_each(m_foreign_set.begin(), m_foreign_set.end(), |
| 1807 |
dict_foreign_free); |
| 1808 |
} |
| 1809 |
|
| 1810 |
const dict_foreign_set &m_foreign_set; |
| 1811 |
}; |
| 1812 |
|
| 1813 |
/** The flags for ON_UPDATE and ON_DELETE can be ORed; the default is that |
| 1814 |
a foreign key constraint is enforced, therefore RESTRICT just means no flag */ |
| 1815 |
/** @{ */ |
| 1816 |
/** ON DELETE CASCADE */ |
| 1817 |
constexpr uint32_t DICT_FOREIGN_ON_DELETE_CASCADE = 1; |
| 1818 |
/** ON DELETE SET NULL */ |
| 1819 |
constexpr uint32_t DICT_FOREIGN_ON_DELETE_SET_NULL = 2; |
| 1820 |
/** ON UPDATE CASCADE */ |
| 1821 |
constexpr uint32_t DICT_FOREIGN_ON_UPDATE_CASCADE = 4; |
| 1822 |
/** ON UPDATE SET NULL */ |
| 1823 |
constexpr uint32_t DICT_FOREIGN_ON_UPDATE_SET_NULL = 8; |
| 1824 |
/** ON DELETE NO ACTION */ |
| 1825 |
constexpr uint32_t DICT_FOREIGN_ON_DELETE_NO_ACTION = 16; |
| 1826 |
/** ON UPDATE NO ACTION */ |
| 1827 |
constexpr uint32_t DICT_FOREIGN_ON_UPDATE_NO_ACTION = 32; |
| 1828 |
/** @} */ |
| 1829 |
|
| 1830 |
/** Display an identifier. |
| 1831 |
@param[in,out] s output stream |
| 1832 |
@param[in] id_name SQL identifier (other than table name) |
| 1833 |
@return the output stream */ |
| 1834 |
std::ostream &operator<<(std::ostream &s, const id_name_t &id_name); |
| 1835 |
|
| 1836 |
/** Display a table name. |
| 1837 |
@param[in,out] s output stream |
| 1838 |
@param[in] table_name table name |
| 1839 |
@return the output stream */ |
| 1840 |
std::ostream &operator<<(std::ostream &s, const table_name_t &table_name); |
| 1841 |
|
| 1842 |
#ifndef UNIV_HOTBACKUP |
| 1843 |
/** List of locks that different transactions have acquired on a table. This |
| 1844 |
list has a list node that is embedded in a nested union/structure. We have to |
| 1845 |
generate a specific template for it. */ |
| 1846 |
struct TableLockGetNode; |
| 1847 |
typedef ut_list_base<lock_t, TableLockGetNode> table_lock_list_t; |
| 1848 |
#endif /* !UNIV_HOTBACKUP */ |
| 1849 |
|
| 1850 |
/** mysql template structure defined in row0mysql.cc */ |
| 1851 |
struct mysql_row_templ_t; |
| 1852 |
|
| 1853 |
/** Structure defines template related to virtual columns and |
| 1854 |
their base columns */ |
| 1855 |
struct dict_vcol_templ_t { |
| 1856 |
/** number of regular columns */ |
| 1857 |
ulint n_col; |
| 1858 |
|
| 1859 |
/** number of virtual columns */ |
| 1860 |
ulint n_v_col; |
| 1861 |
|
| 1862 |
/** array of templates for virtual col and their base columns */ |
| 1863 |
mysql_row_templ_t **vtempl; |
| 1864 |
|
| 1865 |
/** table's database name */ |
| 1866 |
std::string db_name; |
| 1867 |
|
| 1868 |
/** table name */ |
| 1869 |
std::string tb_name; |
| 1870 |
|
| 1871 |
/** share->table_name */ |
| 1872 |
std::string share_name; |
| 1873 |
|
| 1874 |
/** MySQL record length */ |
| 1875 |
ulint rec_len; |
| 1876 |
|
| 1877 |
/** default column value if any */ |
| 1878 |
byte *default_rec; |
| 1879 |
}; |
| 1880 |
|
| 1881 |
/** The dirty status of tables, used to indicate if a table has some |
| 1882 |
dynamic metadata changed to be written back */ |
| 1883 |
enum table_dirty_status { |
| 1884 |
/** Some persistent metadata is now dirty in memory, need to be |
| 1885 |
written back to DDTableBuffer table and(or directly to) DD table. |
| 1886 |
There could be some exceptions, when it's marked as dirty, but |
| 1887 |
the metadata has already been written back to DDTableBuffer. |
| 1888 |
For example, if a corrupted index is found and marked as corrupted, |
| 1889 |
then it gets dropped. At this time, the dirty_status is still of |
| 1890 |
this dirty value. Also a concurrent checkpoint make this bit |
| 1891 |
out-of-date for other working threads, which still think the |
| 1892 |
status is dirty and write-back is necessary. |
| 1893 |
There could be either one row or no row for this table in |
| 1894 |
DDTableBuffer table */ |
| 1895 |
METADATA_DIRTY = 0, |
| 1896 |
/** Some persistent metadata is buffered in DDTableBuffer table, |
| 1897 |
need to be written back to DD table. There is must be one row in |
| 1898 |
DDTableBuffer table for this table */ |
| 1899 |
METADATA_BUFFERED, |
| 1900 |
/** All persistent metadata are up to date. There is no row |
| 1901 |
for this table in DDTableBuffer table */ |
| 1902 |
METADATA_CLEAN |
| 1903 |
}; |
| 1904 |
|
| 1905 |
#ifndef UNIV_HOTBACKUP |
| 1906 |
/** A vector to collect prebuilt from different readers working on the same |
| 1907 |
temp table */ |
| 1908 |
typedef std::vector<row_prebuilt_t *> temp_prebuilt_vec; |
| 1909 |
#endif /* !UNIV_HOTBACKUP */ |
| 1910 |
|
| 1911 |
#ifdef UNIV_DEBUG |
| 1912 |
/** Value of 'magic_n'. */ |
| 1913 |
constexpr uint32_t DICT_TABLE_MAGIC_N = 76333786; |
| 1914 |
#endif |
| 1915 |
|
| 1916 |
/** Data structure for a database table. Most fields will be |
| 1917 |
initialized to 0, NULL or false in dict_mem_table_create(). */ |
| 1918 |
struct dict_table_t { |
| 1919 |
/** Check if the table is compressed. |
| 1920 |
@return true if compressed, false otherwise. */ |
| 1921 |
bool is_compressed() const { return (DICT_TF_GET_ZIP_SSIZE(flags) != 0); } |
| 1922 |
|
| 1923 |
/** Get reference count. |
| 1924 |
@return current value of n_ref_count */ |
| 1925 |
inline uint64_t get_ref_count() const; |
| 1926 |
|
| 1927 |
/** Acquire the table handle. */ |
| 1928 |
inline void acquire(); |
| 1929 |
|
| 1930 |
/** Acquire the table handle, with lock() and unlock() the table. |
| 1931 |
This function needs to be called for opening table when the table |
| 1932 |
is in memory and later the stats information would be initialized */ |
| 1933 |
inline void acquire_with_lock(); |
| 1934 |
|
| 1935 |
/** Release the table handle. */ |
| 1936 |
inline void release(); |
| 1937 |
|
| 1938 |
/** Lock the table handle. */ |
| 1939 |
inline void lock(); |
| 1940 |
|
| 1941 |
/** Unlock the table handle. */ |
| 1942 |
inline void unlock(); |
| 1943 |
|
| 1944 |
#ifndef UNIV_HOTBACKUP |
| 1945 |
/** Get schema and table name in system character set. |
| 1946 |
@param[out] schema schema name |
| 1947 |
@param[out] table table name */ |
| 1948 |
void get_table_name(std::string &schema, std::string &table) const; |
| 1949 |
|
| 1950 |
bool is_system_schema() const { |
| 1951 |
std::string schema_name; |
| 1952 |
std::string table_name; |
| 1953 |
|
| 1954 |
get_table_name(schema_name, table_name); |
| 1955 |
|
| 1956 |
if (0 == strcmp(schema_name.c_str(), MYSQL_SCHEMA_NAME.str) || |
| 1957 |
0 == strcmp(schema_name.c_str(), "sys") || |
| 1958 |
0 == strcmp(schema_name.c_str(), PERFORMANCE_SCHEMA_DB_NAME.str) || |
| 1959 |
0 == strcmp(schema_name.c_str(), INFORMATION_SCHEMA_NAME.str)) { |
| 1960 |
return (true); |
| 1961 |
} |
| 1962 |
return (false); |
| 1963 |
} |
| 1964 |
|
| 1965 |
/** Mutex of the table for concurrency access. */ |
| 1966 |
ib_mutex_t *mutex; |
| 1967 |
|
| 1968 |
/** Creation state of mutex. */ |
| 1969 |
std::atomic<os_once::state_t> mutex_created; |
| 1970 |
#endif /* !UNIV_HOTBACKUP */ |
| 1971 |
|
| 1972 |
/** Id of the table. */ |
| 1973 |
table_id_t id; |
| 1974 |
|
| 1975 |
/** Memory heap. If you allocate from this heap after the table has |
| 1976 |
been created then be sure to account the allocation into |
| 1977 |
dict_sys->size. When closing the table we do something like |
| 1978 |
dict_sys->size -= mem_heap_get_size(table->heap) and if that is going |
| 1979 |
to become negative then we would assert. Something like this should do: |
| 1980 |
old_size = mem_heap_get_size() |
| 1981 |
mem_heap_alloc() |
| 1982 |
new_size = mem_heap_get_size() |
| 1983 |
dict_sys->size += new_size - old_size. */ |
| 1984 |
mem_heap_t *heap; |
| 1985 |
|
| 1986 |
/** Table name. */ |
| 1987 |
table_name_t name; |
| 1988 |
|
| 1989 |
/** Truncate name. */ |
| 1990 |
table_name_t trunc_name; |
| 1991 |
|
| 1992 |
/** NULL or the directory path specified by DATA DIRECTORY. */ |
| 1993 |
char *data_dir_path; |
| 1994 |
|
| 1995 |
/** NULL or the tablespace name that this table is assigned to, |
| 1996 |
specified by the TABLESPACE option.*/ |
| 1997 |
id_name_t tablespace; |
| 1998 |
|
| 1999 |
/** Space where the clustered index of the table is placed. */ |
| 2000 |
space_id_t space; |
| 2001 |
|
| 2002 |
/** dd::Tablespace::id of the table */ |
| 2003 |
dd::Object_id dd_space_id; |
| 2004 |
|
| 2005 |
/** Stores information about: |
| 2006 |
1 row format (redundant or compact), |
| 2007 |
2 compressed page size (zip shift size), |
| 2008 |
3 whether using atomic blobs, |
| 2009 |
4 whether the table has been created with the option DATA DIRECTORY. |
| 2010 |
Use DICT_TF_GET_COMPACT(), DICT_TF_GET_ZIP_SSIZE(), |
| 2011 |
DICT_TF_HAS_ATOMIC_BLOBS() and DICT_TF_HAS_DATA_DIR() to parse this |
| 2012 |
flag. */ |
| 2013 |
unsigned flags : DICT_TF_BITS; |
| 2014 |
|
| 2015 |
/** Stores information about: |
| 2016 |
1 whether the table has been created using CREATE TEMPORARY TABLE, |
| 2017 |
2 whether the table has an internally defined DOC ID column, |
| 2018 |
3 whether the table has a FTS index, |
| 2019 |
4 whether DOC ID column need to be added to the FTS index, |
| 2020 |
5 whether the table is being created its own tablespace, |
| 2021 |
6 whether the table has been DISCARDed, |
| 2022 |
7 whether the aux FTS tables names are in hex. |
| 2023 |
8 whether the table is instinc table. |
| 2024 |
9 whether the table has encryption setting. |
| 2025 |
Use DICT_TF2_FLAG_IS_SET() to parse this flag. */ |
| 2026 |
unsigned flags2 : DICT_TF2_BITS; |
| 2027 |
|
| 2028 |
/** true if the table is an intermediate table during copy alter |
| 2029 |
operation or a partition/subpartition which is required for copying |
| 2030 |
data and skip the undo log for insertion of row in the table. |
| 2031 |
This variable will be set and unset during extra(), or during the |
| 2032 |
process of altering partitions */ |
| 2033 |
unsigned skip_alter_undo : 1; |
| 2034 |
|
| 2035 |
/** true if this is in a single-table tablespace and the .ibd file is |
| 2036 |
missing. Then we must return in ha_innodb.cc an error if the user |
| 2037 |
tries to query such an orphaned table. */ |
| 2038 |
unsigned ibd_file_missing : 1; |
| 2039 |
|
| 2040 |
/** true if the table object has been added to the dictionary cache. */ |
| 2041 |
unsigned cached : 1; |
| 2042 |
|
| 2043 |
/** true if the table is to be dropped, but not yet actually dropped |
| 2044 |
(could in the background drop list). It is turned on at the beginning |
| 2045 |
of row_drop_table_for_mysql() and turned off just before we start to |
| 2046 |
update system tables for the drop. It is protected by |
| 2047 |
dict_operation_lock. */ |
| 2048 |
unsigned to_be_dropped : 1; |
| 2049 |
|
| 2050 |
/** Number of non-virtual columns defined so far. */ |
| 2051 |
unsigned n_def : 10; |
| 2052 |
|
| 2053 |
/** Number of non-virtual columns. */ |
| 2054 |
unsigned n_cols : 10; |
| 2055 |
|
| 2056 |
/** Number of non-virtual columns before first instant ADD COLUMN, |
| 2057 |
including the system columns like n_cols. This is used only when table has |
| 2058 |
instant ADD clumns in V1. */ |
| 2059 |
unsigned n_instant_cols : 10; |
| 2060 |
|
| 2061 |
/** Number of total columns (include virtual and non-virtual) */ |
| 2062 |
unsigned n_t_cols : 10; |
| 2063 |
|
| 2064 |
/** Number of total columns defined so far. */ |
| 2065 |
unsigned n_t_def : 10; |
| 2066 |
|
| 2067 |
/** Number of virtual columns defined so far. */ |
| 2068 |
unsigned n_v_def : 10; |
| 2069 |
|
| 2070 |
/** Number of virtual columns. */ |
| 2071 |
unsigned n_v_cols : 10; |
| 2072 |
|
| 2073 |
/** Number of multi-value virtual columns. */ |
| 2074 |
unsigned n_m_v_cols : 10; |
| 2075 |
|
| 2076 |
/** true if this table is expected to be kept in memory. This table |
| 2077 |
could be a table that has FK relationships or is undergoing DDL */ |
| 2078 |
bool can_be_evicted : 1; |
| 2079 |
|
| 2080 |
/** true if this table is not evictable(can_be_evicted) and this is |
| 2081 |
because of DDL operation */ |
| 2082 |
unsigned ddl_not_evictable : 1; |
| 2083 |
|
| 2084 |
/** true if some indexes should be dropped after ONLINE_INDEX_ABORTED |
| 2085 |
or ONLINE_INDEX_ABORTED_DROPPED. */ |
| 2086 |
unsigned drop_aborted : 1; |
| 2087 |
|
| 2088 |
/** Array of column descriptions. */ |
| 2089 |
dict_col_t *cols; |
| 2090 |
|
| 2091 |
/** Array of virtual column descriptions. */ |
| 2092 |
dict_v_col_t *v_cols; |
| 2093 |
|
| 2094 |
/** List of stored column descriptions. It is used only for foreign key |
| 2095 |
check during create table and copy alter operations. |
| 2096 |
During copy alter, s_cols list is filled during create table operation |
| 2097 |
and need to preserve till rename table operation. That is the |
| 2098 |
reason s_cols is a part of dict_table_t */ |
| 2099 |
dict_s_col_list *s_cols; |
| 2100 |
|
| 2101 |
/** Column names packed in a character string |
| 2102 |
"name1\0name2\0...nameN\0". Until the string contains n_cols, it will |
| 2103 |
be allocated from a temporary heap. The final string will be allocated |
| 2104 |
from table->heap. */ |
| 2105 |
const char *col_names; |
| 2106 |
|
| 2107 |
/** Virtual column names */ |
| 2108 |
const char *v_col_names; |
| 2109 |
|
| 2110 |
/** True if the table belongs to a system database (mysql, information_schema |
| 2111 |
or performance_schema) */ |
| 2112 |
bool is_system_table; |
| 2113 |
|
| 2114 |
/** Hash chain node. */ |
| 2115 |
hash_node_t name_hash; |
| 2116 |
|
| 2117 |
/** Hash chain node. */ |
| 2118 |
hash_node_t id_hash; |
| 2119 |
|
| 2120 |
/** The FTS_DOC_ID_INDEX, or NULL if no fulltext indexes exist */ |
| 2121 |
dict_index_t *fts_doc_id_index; |
| 2122 |
|
| 2123 |
/** List of indexes of the table. */ |
| 2124 |
UT_LIST_BASE_NODE_T(dict_index_t, indexes) indexes; |
| 2125 |
|
| 2126 |
/** Node of the LRU list of tables. */ |
| 2127 |
UT_LIST_NODE_T(dict_table_t) table_LRU; |
| 2128 |
|
| 2129 |
/** metadata version number of dd::Table::se_private_data() */ |
| 2130 |
uint64_t version; |
| 2131 |
|
| 2132 |
/** Current row version in case columns are added/dropped INSTANTly */ |
| 2133 |
uint32_t current_row_version{0}; |
| 2134 |
|
| 2135 |
/** Initial non-virtual column count */ |
| 2136 |
uint32_t initial_col_count{0}; |
| 2137 |
|
| 2138 |
/** Current non-virtual column count */ |
| 2139 |
uint32_t current_col_count{0}; |
| 2140 |
|
| 2141 |
/** Total non-virtual column count */ |
| 2142 |
uint32_t total_col_count{0}; |
| 2143 |
|
| 2144 |
/** Set if table is upgraded instant table */ |
| 2145 |
bool m_upgraded_instant{false}; |
| 2146 |
|
| 2147 |
/** table dynamic metadata status, protected by dict_persist->mutex */ |
| 2148 |
std::atomic<table_dirty_status> dirty_status; |
| 2149 |
|
| 2150 |
#ifndef UNIV_HOTBACKUP |
| 2151 |
/** Node of the dirty table list of tables, which is protected |
| 2152 |
by dict_persist->mutex */ |
| 2153 |
UT_LIST_NODE_T(dict_table_t) dirty_dict_tables; |
| 2154 |
#endif /* !UNIV_HOTBACKUP */ |
| 2155 |
|
| 2156 |
#ifdef UNIV_DEBUG |
| 2157 |
/** This field is used to mark if a table is in the |
| 2158 |
dirty_dict_tables_list. if the dirty_status is not of |
| 2159 |
METADATA_CLEAN, the table should be in the list, otherwise not. |
| 2160 |
This field should be protected by dict_persist->mutex too. */ |
| 2161 |
bool in_dirty_dict_tables_list; |
| 2162 |
#endif /* UNIV_DEBUG */ |
| 2163 |
|
| 2164 |
/** Maximum recursive level we support when loading tables chained |
| 2165 |
together with FK constraints. If exceeds this level, we will stop |
| 2166 |
loading child table into memory along with its parent table. */ |
| 2167 |
unsigned fk_max_recusive_level : 8; |
| 2168 |
|
| 2169 |
/** Count of how many foreign key check operations are currently being |
| 2170 |
performed on the table. We cannot drop the table while there are |
| 2171 |
foreign key checks running on it. */ |
| 2172 |
std::atomic<ulint> n_foreign_key_checks_running; |
| 2173 |
|
| 2174 |
/** Transaction id that last touched the table definition. Either when |
| 2175 |
loading the definition or CREATE TABLE, or ALTER TABLE (prepare, |
| 2176 |
commit, and rollback phases). */ |
| 2177 |
trx_id_t def_trx_id; |
| 2178 |
|
| 2179 |
/*!< set of foreign key constraints in the table; these refer to |
| 2180 |
columns in other tables */ |
| 2181 |
dict_foreign_set foreign_set; |
| 2182 |
|
| 2183 |
/*!< set of foreign key constraints which refer to this table */ |
| 2184 |
dict_foreign_set referenced_set; |
| 2185 |
|
| 2186 |
#ifdef UNIV_DEBUG |
| 2187 |
/** This field is used to specify in simulations tables which are so |
| 2188 |
big that disk should be accessed. Disk access is simulated by putting |
| 2189 |
the thread to sleep for a while. NOTE that this flag is not stored to |
| 2190 |
the data dictionary on disk, and the database will forget about value |
| 2191 |
true if it has to reload the table definition from disk. */ |
| 2192 |
bool does_not_fit_in_memory; |
| 2193 |
#endif /* UNIV_DEBUG */ |
| 2194 |
|
| 2195 |
/** true if the maximum length of a single row exceeds BIG_ROW_SIZE. |
| 2196 |
Initialized in dict_table_add_to_cache(). */ |
| 2197 |
unsigned big_rows : 1; |
| 2198 |
|
| 2199 |
#ifndef UNIV_HOTBACKUP |
| 2200 |
/** Statistics for query optimization. @{ */ |
| 2201 |
|
| 2202 |
/** Creation state of 'stats_latch'. */ |
| 2203 |
std::atomic<os_once::state_t> stats_latch_created; |
| 2204 |
|
| 2205 |
/** This latch protects: |
| 2206 |
"dict_table_t::stat_initialized", |
| 2207 |
"dict_table_t::stat_n_rows (*)", |
| 2208 |
"dict_table_t::stat_clustered_index_size", |
| 2209 |
"dict_table_t::stat_sum_of_other_index_sizes", |
| 2210 |
"dict_table_t::stat_modified_counter (*)", |
| 2211 |
"dict_table_t::indexes*::stat_n_diff_key_vals[]", |
| 2212 |
"dict_table_t::indexes*::stat_index_size", |
| 2213 |
"dict_table_t::indexes*::stat_n_leaf_pages". |
| 2214 |
(*) Those are not always protected for |
| 2215 |
performance reasons. */ |
| 2216 |
rw_lock_t *stats_latch; |
| 2217 |
|
| 2218 |
/** true if statistics have been calculated the first time after |
| 2219 |
database startup or table creation. */ |
| 2220 |
unsigned stat_initialized : 1; |
| 2221 |
|
| 2222 |
/** Timestamp of last recalc of the stats. */ |
| 2223 |
std::chrono::steady_clock::time_point stats_last_recalc; |
| 2224 |
|
| 2225 |
/** The two bits below are set in the 'stat_persistent' member. They |
| 2226 |
have the following meaning: |
| 2227 |
1. _ON=0, _OFF=0, no explicit persistent stats setting for this table, |
| 2228 |
the value of the global srv_stats_persistent is used to determine |
| 2229 |
whether the table has persistent stats enabled or not |
| 2230 |
2. _ON=0, _OFF=1, persistent stats are explicitly disabled for this |
| 2231 |
table, regardless of the value of the global srv_stats_persistent |
| 2232 |
3. _ON=1, _OFF=0, persistent stats are explicitly enabled for this |
| 2233 |
table, regardless of the value of the global srv_stats_persistent |
| 2234 |
4. _ON=1, _OFF=1, not allowed, we assert if this ever happens. */ |
| 2235 |
#define DICT_STATS_PERSISTENT_ON (1 << 1) |
| 2236 |
#define DICT_STATS_PERSISTENT_OFF (1 << 2) |
| 2237 |
|
| 2238 |
/** Indicates whether the table uses persistent stats or not. See |
| 2239 |
DICT_STATS_PERSISTENT_ON and DICT_STATS_PERSISTENT_OFF. */ |
| 2240 |
uint32_t stat_persistent; |
| 2241 |
|
| 2242 |
/** The two bits below are set in the 'stats_auto_recalc' member. They |
| 2243 |
have the following meaning: |
| 2244 |
1. _ON=0, _OFF=0, no explicit auto recalc setting for this table, the |
| 2245 |
value of the global srv_stats_persistent_auto_recalc is used to |
| 2246 |
determine whether the table has auto recalc enabled or not |
| 2247 |
2. _ON=0, _OFF=1, auto recalc is explicitly disabled for this table, |
| 2248 |
regardless of the value of the global srv_stats_persistent_auto_recalc |
| 2249 |
3. _ON=1, _OFF=0, auto recalc is explicitly enabled for this table, |
| 2250 |
regardless of the value of the global srv_stats_persistent_auto_recalc |
| 2251 |
4. _ON=1, _OFF=1, not allowed, we assert if this ever happens. */ |
| 2252 |
#define DICT_STATS_AUTO_RECALC_ON (1 << 1) |
| 2253 |
#define DICT_STATS_AUTO_RECALC_OFF (1 << 2) |
| 2254 |
|
| 2255 |
/** Indicates whether the table uses automatic recalc for persistent |
| 2256 |
stats or not. See DICT_STATS_AUTO_RECALC_ON and |
| 2257 |
DICT_STATS_AUTO_RECALC_OFF. */ |
| 2258 |
uint32_t stats_auto_recalc; |
| 2259 |
|
| 2260 |
/** The number of pages to sample for this table during persistent |
| 2261 |
stats estimation. If this is 0, then the value of the global |
| 2262 |
srv_stats_persistent_sample_pages will be used instead. */ |
| 2263 |
ulint stats_sample_pages; |
| 2264 |
|
| 2265 |
/** Approximate number of rows in the table. We periodically calculate |
| 2266 |
new estimates. */ |
| 2267 |
uint64_t stat_n_rows; |
| 2268 |
|
| 2269 |
/** Approximate clustered index size in database pages. */ |
| 2270 |
ulint stat_clustered_index_size; |
| 2271 |
|
| 2272 |
/** Approximate size of other indexes in database pages. */ |
| 2273 |
ulint stat_sum_of_other_index_sizes; |
| 2274 |
|
| 2275 |
/** If FTS AUX table, parent table id */ |
| 2276 |
table_id_t parent_id; |
| 2277 |
|
| 2278 |
/** How many rows are modified since last stats recalc. When a row is |
| 2279 |
inserted, updated, or deleted, we add 1 to this number; we calculate |
| 2280 |
new estimates for the table and the indexes if the table has changed |
| 2281 |
too much, see row_update_statistics_if_needed(). The counter is reset |
| 2282 |
to zero at statistics calculation. This counter is not protected by |
| 2283 |
any latch, because this is only used for heuristics. */ |
| 2284 |
uint64_t stat_modified_counter; |
| 2285 |
|
| 2286 |
/** Background stats thread is not working on this table. */ |
| 2287 |
#define BG_STAT_NONE 0 |
| 2288 |
|
| 2289 |
/** Set in 'stats_bg_flag' when the background stats code is working |
| 2290 |
on this table. The DROP TABLE code waits for this to be cleared before |
| 2291 |
proceeding. */ |
| 2292 |
#define BG_STAT_IN_PROGRESS (1 << 0) |
| 2293 |
|
| 2294 |
/** Set in 'stats_bg_flag' when DROP TABLE starts waiting on |
| 2295 |
BG_STAT_IN_PROGRESS to be cleared. The background stats thread will |
| 2296 |
detect this and will eventually quit sooner. */ |
| 2297 |
#define BG_STAT_SHOULD_QUIT (1 << 1) |
| 2298 |
|
| 2299 |
/** The state of the background stats thread wrt this table. |
| 2300 |
See BG_STAT_NONE, BG_STAT_IN_PROGRESS and BG_STAT_SHOULD_QUIT. |
| 2301 |
Writes are covered by dict_sys->mutex. Dirty reads are possible. */ |
| 2302 |
byte stats_bg_flag; |
| 2303 |
|
| 2304 |
/** @} */ |
| 2305 |
#endif /* !UNIV_HOTBACKUP */ |
| 2306 |
|
| 2307 |
/** AUTOINC related members. @{ */ |
| 2308 |
|
| 2309 |
/* The actual collection of tables locked during AUTOINC read/write is |
| 2310 |
kept in trx_t. In order to quickly determine whether a transaction has |
| 2311 |
locked the AUTOINC lock we keep a pointer to the transaction here in |
| 2312 |
the 'autoinc_trx' member. This is to avoid acquiring lock_sys latches and |
| 2313 |
scanning the vector in trx_t. |
| 2314 |
When an AUTOINC lock has to wait, the corresponding lock instance is |
| 2315 |
created on the trx lock heap rather than use the pre-allocated instance |
| 2316 |
in autoinc_lock below. */ |
| 2317 |
|
| 2318 |
/** A buffer for an AUTOINC lock for this table. We allocate the |
| 2319 |
memory here so that individual transactions can get it and release it |
| 2320 |
without a need to allocate space from the lock heap of the trx: |
| 2321 |
otherwise the lock heap would grow rapidly if we do a large insert |
| 2322 |
from a select. */ |
| 2323 |
#ifndef UNIV_HOTBACKUP |
| 2324 |
lock_t *autoinc_lock; |
| 2325 |
|
| 2326 |
/** Creation state of autoinc_mutex member */ |
| 2327 |
std::atomic<os_once::state_t> autoinc_mutex_created; |
| 2328 |
#endif /* !UNIV_HOTBACKUP */ |
| 2329 |
|
| 2330 |
/** Mutex protecting the autoincrement counter. */ |
| 2331 |
ib_mutex_t *autoinc_mutex; |
| 2332 |
|
| 2333 |
/** Autoinc counter value to give to the next inserted row. */ |
| 2334 |
uint64_t autoinc; |
| 2335 |
|
| 2336 |
/** Mutex protecting the persisted autoincrement counter. */ |
| 2337 |
ib_mutex_t *autoinc_persisted_mutex; |
| 2338 |
|
| 2339 |
/** Autoinc counter value that has been persisted in redo logs or |
| 2340 |
DDTableBuffer. It's mainly used when we want to write counter back |
| 2341 |
to DDTableBuffer. |
| 2342 |
This is different from the 'autoinc' above, which could be bigger |
| 2343 |
than this one, because 'autoinc' will get updated right after |
| 2344 |
some counters are allocated, but we will write the counter to redo |
| 2345 |
logs and update this counter later. Once all allocated counters |
| 2346 |
have been written to redo logs, 'autoinc' should be exact the next |
| 2347 |
counter of this persisted one. |
| 2348 |
We want this counter because when we need to write the counter back |
| 2349 |
to DDTableBuffer, we had better keep it consistency with the counter |
| 2350 |
that has been written to redo logs. Besides, we can't read the 'autoinc' |
| 2351 |
directly easily, because the autoinc_lock is required and there could |
| 2352 |
be a deadlock. |
| 2353 |
This variable is protected by autoinc_persisted_mutex. */ |
| 2354 |
uint64_t autoinc_persisted; |
| 2355 |
|
| 2356 |
/** The position of autoinc counter field in clustered index. This would |
| 2357 |
be set when CREATE/ALTER/OPEN TABLE and IMPORT TABLESPACE, and used in |
| 2358 |
modifications to clustered index, such as INSERT/UPDATE. There should |
| 2359 |
be no conflict to access it, so no protection is needed. */ |
| 2360 |
ulint autoinc_field_no; |
| 2361 |
|
| 2362 |
/** The transaction that currently holds the the AUTOINC lock on this table. |
| 2363 |
Protected by lock_sys table shard latch. To "peek" the current value one |
| 2364 |
can read it without any latch, understanding that in general it may change. |
| 2365 |
Such access pattern is correct if trx thread wants to check if it has the lock |
| 2366 |
granted, as the field can only change to other value when lock is released, |
| 2367 |
which can not happen concurrently to thread executing the trx. */ |
| 2368 |
std::atomic<const trx_t *> autoinc_trx; |
| 2369 |
|
| 2370 |
/** @} */ |
| 2371 |
|
| 2372 |
#ifndef UNIV_HOTBACKUP |
| 2373 |
/** FTS specific state variables. */ |
| 2374 |
fts_t *fts; |
| 2375 |
#endif /* !UNIV_HOTBACKUP */ |
| 2376 |
|
| 2377 |
/** Quiescing states, protected by the dict_index_t::lock. ie. we can |
| 2378 |
only change the state if we acquire all the latches (dict_index_t::lock) |
| 2379 |
in X mode of this table's indexes. */ |
| 2380 |
ib_quiesce_t quiesce; |
| 2381 |
|
| 2382 |
/** Count of the number of record locks on this table. We use this to |
| 2383 |
determine whether we can evict the table from the dictionary cache. |
| 2384 |
Writes (atomic increments and decrements) are performed when holding a shared |
| 2385 |
latch on lock_sys. (Note that this the table's shard latch is NOT required, |
| 2386 |
as this is field counts *record* locks, so a page shard is latched instead) |
| 2387 |
Reads should be performed when holding exclusive lock_sys latch, however: |
| 2388 |
- Some places assert this field is zero without holding any latch. |
| 2389 |
- Some places assert this field is positive holding only shared latch. */ |
| 2390 |
std::atomic<size_t> n_rec_locks; |
| 2391 |
|
| 2392 |
#ifndef UNIV_DEBUG |
| 2393 |
private: |
| 2394 |
#endif |
| 2395 |
/** Count of how many handles are opened to this table. Dropping of the |
| 2396 |
table is NOT allowed until this count gets to zero. MySQL does NOT |
| 2397 |
itself check the number of open handles at DROP. */ |
| 2398 |
std::atomic<uint64_t> n_ref_count; |
| 2399 |
|
| 2400 |
public: |
| 2401 |
#ifndef UNIV_HOTBACKUP |
| 2402 |
/** List of locks on the table. Protected by lock_sys shard latch. */ |
| 2403 |
table_lock_list_t locks; |
| 2404 |
/** count_by_mode[M] = number of locks in this->locks with |
| 2405 |
lock->type_mode&LOCK_MODE_MASK == M. |
| 2406 |
Used to quickly verify that there are no LOCK_S or LOCK_X, which are the only |
| 2407 |
modes incompatible with LOCK_IS and LOCK_IX, to avoid costly iteration over |
| 2408 |
this->locks when adding LOCK_IS or LOCK_IX. |
| 2409 |
We use count_by_mode[LOCK_AUTO_INC] to track the number of granted and pending |
| 2410 |
autoinc locks on this table. This value is set after acquiring the lock_sys |
| 2411 |
table shard latch, but we peek the contents to determine whether other |
| 2412 |
transactions have acquired the AUTOINC lock or not. Of course only one |
| 2413 |
transaction can be granted the lock but there can be multiple |
| 2414 |
waiters. |
| 2415 |
Protected by lock_sys table shard latch. */ |
| 2416 |
ulong count_by_mode[LOCK_NUM]; |
| 2417 |
#endif /* !UNIV_HOTBACKUP */ |
| 2418 |
|
| 2419 |
/** Timestamp of the last modification of this table. */ |
| 2420 |
std::atomic<std::chrono::system_clock::time_point> update_time; |
| 2421 |
static_assert(decltype(update_time)::is_always_lock_free); |
| 2422 |
|
| 2423 |
/** row-id counter for use by intrinsic table for getting row-id. |
| 2424 |
Given intrinsic table semantics, row-id can be locally maintained |
| 2425 |
instead of getting it from central generator which involves mutex |
| 2426 |
locking. */ |
| 2427 |
uint64_t sess_row_id; |
| 2428 |
|
| 2429 |
/** trx_id counter for use by intrinsic table for getting trx-id. |
| 2430 |
Intrinsic table are not shared so don't need a central trx-id |
| 2431 |
but just need a increased counter to track consistent view while |
| 2432 |
proceeding SELECT as part of UPDATE. */ |
| 2433 |
uint64_t sess_trx_id; |
| 2434 |
|
| 2435 |
#ifdef UNIV_DEBUG |
| 2436 |
/** Magic number. */ |
| 2437 |
ulint magic_n; |
| 2438 |
#endif /* UNIV_DEBUG */ |
| 2439 |
/** mysql_row_templ_t for base columns used for compute the virtual |
| 2440 |
columns */ |
| 2441 |
dict_vcol_templ_t *vc_templ; |
| 2442 |
|
| 2443 |
/** remove the dict_table_t from cache after DDL operation */ |
| 2444 |
bool discard_after_ddl; |
| 2445 |
|
| 2446 |
/** refresh/reload FK info */ |
| 2447 |
bool refresh_fk; |
| 2448 |
|
| 2449 |
#ifndef UNIV_HOTBACKUP |
| 2450 |
/** multiple cursors can be active on this temporary table */ |
| 2451 |
temp_prebuilt_vec *temp_prebuilt; |
| 2452 |
#endif /* !UNIV_HOTBACKUP */ |
| 2453 |
|
| 2454 |
/** true only for dictionary tables like mysql/tables, |
| 2455 |
mysql/columns, mysql/tablespaces, etc. This flag is used |
| 2456 |
to do non-locking reads on DD tables. */ |
| 2457 |
bool is_dd_table; |
| 2458 |
|
| 2459 |
/** true if this table is explicitly put to non-LRU list |
| 2460 |
during table creation */ |
| 2461 |
bool explicitly_non_lru; |
| 2462 |
|
| 2463 |
/** @return the clustered index */ |
| 2464 |
const dict_index_t *first_index() const { |
| 2465 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2466 |
const dict_index_t *first = UT_LIST_GET_FIRST(indexes); |
| 2467 |
return (first); |
| 2468 |
} |
| 2469 |
/** @return the clustered index */ |
| 2470 |
dict_index_t *first_index() { |
| 2471 |
return (const_cast<dict_index_t *>( |
| 2472 |
const_cast<const dict_table_t *>(this)->first_index())); |
| 2473 |
} |
| 2474 |
|
| 2475 |
/** @returns true if the table has row versions.. */ |
| 2476 |
bool has_row_versions() const { |
| 2477 |
if (current_row_version > 0) { |
| 2478 |
ut_ad(has_instant_add_cols() || has_instant_drop_cols()); |
| 2479 |
return (true); |
| 2480 |
} |
| 2481 |
|
| 2482 |
return false; |
| 2483 |
} |
| 2484 |
|
| 2485 |
/** @return if there was any instantly added column. |
| 2486 |
This will be true after one or more instant ADD COLUMN, however, |
| 2487 |
it would become false after ALTER TABLE which rebuilds or copies |
| 2488 |
the old table. |
| 2489 |
If this is true, all instantly added columns should have default |
| 2490 |
values, and records in the table may have REC_INFO_INSTANT_FLAG set. */ |
| 2491 |
bool has_instant_cols() const { |
| 2492 |
if (is_upgraded_instant() || (n_instant_cols < n_cols)) { |
| 2493 |
/* Instant add col V1 */ |
| 2494 |
return (true); |
| 2495 |
} |
| 2496 |
|
| 2497 |
return false; |
| 2498 |
} |
| 2499 |
|
| 2500 |
/** Set the number of columns when the first instant ADD COLUMN happens. |
| 2501 |
@param[in] n_inst_cols number of fields when first instant |
| 2502 |
ADD COLUMN happens, without system columns */ |
| 2503 |
void set_instant_cols(uint16_t n_inst_cols) { |
| 2504 |
n_instant_cols = static_cast<unsigned>(n_inst_cols) + get_n_sys_cols(); |
| 2505 |
} |
| 2506 |
|
| 2507 |
/** Get the number of user columns when the first instant ADD COLUMN |
| 2508 |
happens. |
| 2509 |
@return the number of user columns as described above */ |
| 2510 |
uint16_t get_instant_cols() const { |
| 2511 |
return static_cast<uint16_t>(n_instant_cols - get_n_sys_cols()); |
| 2512 |
} |
| 2513 |
|
| 2514 |
size_t get_n_instant_added_col_v1() const { |
| 2515 |
size_t n_cols_dropped = get_n_instant_drop_cols(); |
| 2516 |
size_t n_cols_added = get_n_instant_add_cols(); |
| 2517 |
size_t n_instant_added_cols = |
| 2518 |
n_cols + n_cols_dropped - n_cols_added - n_instant_cols; |
| 2519 |
|
| 2520 |
return (n_instant_added_cols); |
| 2521 |
} |
| 2522 |
|
| 2523 |
/** Get number of columns added instantly */ |
| 2524 |
uint32_t get_n_instant_add_cols() const { |
| 2525 |
ut_ad(total_col_count >= initial_col_count); |
| 2526 |
return total_col_count - initial_col_count; |
| 2527 |
} |
| 2528 |
|
| 2529 |
/** Get number of columns dropped instantly */ |
| 2530 |
uint32_t get_n_instant_drop_cols() const { |
| 2531 |
ut_ad(total_col_count >= current_col_count); |
| 2532 |
return total_col_count - current_col_count; |
| 2533 |
} |
| 2534 |
|
| 2535 |
/** check if table has INSTANT ADD columns. |
| 2536 |
@return true if the table has INSTANT ADD columns, otherwise false */ |
| 2537 |
bool has_instant_add_cols() const { return (get_n_instant_add_cols() > 0); } |
| 2538 |
|
| 2539 |
/** check if table has INSTANT DROP columns. |
| 2540 |
@return true if the table has INSTANT DROP columns, otherwise false */ |
| 2541 |
bool has_instant_drop_cols() const { return (get_n_instant_drop_cols() > 0); } |
| 2542 |
|
| 2543 |
/** Set table to be upgraded table with INSTANT ADD columns in V1. */ |
| 2544 |
void set_upgraded_instant() { m_upgraded_instant = true; } |
| 2545 |
|
| 2546 |
/** Checks if table is upgraded table with INSTANT ADD columns in V1. |
| 2547 |
@return true if it is, false otherwise */ |
| 2548 |
bool is_upgraded_instant() const { return m_upgraded_instant; } |
| 2549 |
|
| 2550 |
/** Check whether the table is corrupted. |
| 2551 |
@return true if the table is corrupted, otherwise false */ |
| 2552 |
bool is_corrupted() const { |
| 2553 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2554 |
|
| 2555 |
const dict_index_t *index = first_index(); |
| 2556 |
|
| 2557 |
/* It is possible that this table is only half created, in which case |
| 2558 |
the clustered index may be NULL. If the clustered index is corrupted, |
| 2559 |
the table is corrupt. We do not consider the table corrupt if only |
| 2560 |
a secondary index is corrupt. */ |
| 2561 |
ut_ad(index == nullptr || index->is_clustered()); |
| 2562 |
|
| 2563 |
return (index != nullptr && index->type & DICT_CORRUPT); |
| 2564 |
} |
| 2565 |
|
| 2566 |
/** Returns a column's name. |
| 2567 |
@param[in] col_nr column number |
| 2568 |
@return column name. NOTE: not guaranteed to stay valid if table is |
| 2569 |
modified in any way (columns added, etc.). */ |
| 2570 |
const char *get_col_name(ulint col_nr) const { |
| 2571 |
ut_ad(col_nr < n_def); |
| 2572 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2573 |
|
| 2574 |
const char *s = col_names; |
| 2575 |
if (s) { |
| 2576 |
for (ulint i = 0; i < col_nr; i++) { |
| 2577 |
s += strlen(s) + 1; |
| 2578 |
} |
| 2579 |
} |
| 2580 |
|
| 2581 |
return (s); |
| 2582 |
} |
| 2583 |
|
| 2584 |
/** Gets the nth column of a table. |
| 2585 |
@param[in] pos position of column |
| 2586 |
@return pointer to column object */ |
| 2587 |
dict_col_t *get_col(uint pos) const { |
| 2588 |
ut_ad(pos < n_def); |
| 2589 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2590 |
|
| 2591 |
return (cols + pos); |
| 2592 |
} |
| 2593 |
|
| 2594 |
/** Get column by name |
| 2595 |
@param[in] name column name |
| 2596 |
@return column name if found, null otherwise */ |
| 2597 |
dict_col_t *get_col_by_name(const char *name) const { |
| 2598 |
ut_ad(name != nullptr); |
| 2599 |
|
| 2600 |
dict_col_t *ret = nullptr; |
| 2601 |
|
| 2602 |
const char *s = col_names; |
| 2603 |
for (ulint i = 0; i < n_def; i++) { |
| 2604 |
if (strcmp(s, name) == 0) { |
| 2605 |
ret = get_col(i); |
| 2606 |
} |
| 2607 |
s += strlen(s) + 1; |
| 2608 |
} |
| 2609 |
|
| 2610 |
return ret; |
| 2611 |
} |
| 2612 |
|
| 2613 |
/** Gets the number of user-defined non-virtual columns in a table |
| 2614 |
in the dictionary cache. |
| 2615 |
@return number of user-defined (e.g., not ROW_ID) non-virtual columns |
| 2616 |
of a table */ |
| 2617 |
uint16_t get_n_user_cols() const { |
| 2618 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2619 |
|
| 2620 |
return (static_cast<uint16_t>(n_cols) - get_n_sys_cols()); |
| 2621 |
} |
| 2622 |
|
| 2623 |
/** Gets the number of system columns in a table. |
| 2624 |
For intrinsic table on ROW_ID column is added for all other |
| 2625 |
tables TRX_ID and ROLL_PTR are all also appended. |
| 2626 |
@return number of system (e.g., ROW_ID) columns of a table */ |
| 2627 |
uint16_t get_n_sys_cols() const { |
| 2628 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2629 |
|
| 2630 |
return (is_intrinsic() ? DATA_ITT_N_SYS_COLS : DATA_N_SYS_COLS); |
| 2631 |
} |
| 2632 |
|
| 2633 |
/** Gets the number of all non-virtual columns (also system) in a table |
| 2634 |
in the dictionary cache. |
| 2635 |
@return number of non-virtual columns of a table */ |
| 2636 |
ulint get_n_cols() const { |
| 2637 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2638 |
|
| 2639 |
return (n_cols); |
| 2640 |
} |
| 2641 |
|
| 2642 |
/** Gets the number of all non-virtual columns in a table including columns |
| 2643 |
dropped INSTANTly. |
| 2644 |
@returns number of non-virtual columns of a table */ |
| 2645 |
ulint get_total_cols() const { |
| 2646 |
if (!has_row_versions()) { |
| 2647 |
return n_cols; |
| 2648 |
} |
| 2649 |
|
| 2650 |
ut_ad((total_col_count + get_n_sys_cols()) == |
| 2651 |
(n_cols + get_n_instant_drop_cols())); |
| 2652 |
return n_cols + get_n_instant_drop_cols(); |
| 2653 |
} |
| 2654 |
|
| 2655 |
/** Gets the given system column of a table. |
| 2656 |
@param[in] sys DATA_ROW_ID, ... |
| 2657 |
@return pointer to column object */ |
| 2658 |
dict_col_t *get_sys_col(ulint sys) const { |
| 2659 |
dict_col_t *col; |
| 2660 |
|
| 2661 |
ut_ad(sys < get_n_sys_cols()); |
| 2662 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2663 |
|
| 2664 |
col = get_col(n_cols - get_n_sys_cols() + sys); |
| 2665 |
ut_ad(col->mtype == DATA_SYS); |
| 2666 |
ut_ad(col->prtype == (sys | DATA_NOT_NULL)); |
| 2667 |
|
| 2668 |
return (col); |
| 2669 |
} |
| 2670 |
|
| 2671 |
/** Determine if this is a temporary table. */ |
| 2672 |
bool is_temporary() const { |
| 2673 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2674 |
return (flags2 & DICT_TF2_TEMPORARY); |
| 2675 |
} |
| 2676 |
|
| 2677 |
/** Determine if this is a FTS AUX table. */ |
| 2678 |
bool is_fts_aux() const { |
| 2679 |
ut_ad(magic_n == DICT_TABLE_MAGIC_N); |
| 2680 |
return (flags2 & DICT_TF2_AUX); |
| 2681 |
} |
| 2682 |
|
| 2683 |
/** Determine whether the table is intrinsic. |
| 2684 |
An intrinsic table is a special kind of temporary table that |
| 2685 |
is invisible to the end user. It can be created internally by InnoDB, |
| 2686 |
the MySQL server layer or other modules connected to InnoDB in order |
| 2687 |
to gather and use data as part of a larger task. Since access to it |
| 2688 |
must be as fast as possible, it does not need UNDO semantics, system |
| 2689 |
fields DB_TRX_ID & DB_ROLL_PTR, doublewrite, checksum, insert buffer, |
| 2690 |
use of the shared data dictionary, locking, or even a transaction. |
| 2691 |
In short, these are not ACID tables at all, just temporary data stored |
| 2692 |
and manipulated during a larger process.*/ |
| 2693 |
bool is_intrinsic() const { |
| 2694 |
if (flags2 & DICT_TF2_INTRINSIC) { |
| 2695 |
ut_ad(is_temporary()); |
| 2696 |
return (true); |
| 2697 |
} |
| 2698 |
|
| 2699 |
return (false); |
| 2700 |
} |
| 2701 |
|
| 2702 |
/* GAP locks are skipped for DD tables and SDI tables |
| 2703 |
@return true if table is DD table or SDI table, else false */ |
| 2704 |
inline bool skip_gap_locks() const; |
| 2705 |
|
| 2706 |
/** Determine if the table can support instant ADD/DROP COLUMN */ |
| 2707 |
inline bool support_instant_add_drop() const; |
| 2708 |
}; |
| 2709 |
|
| 2710 |
static inline void DICT_TF2_FLAG_SET(dict_table_t *table, uint32_t flag) { |
| 2711 |
table->flags2 |= flag; |
| 2712 |
} |
| 2713 |
|
| 2714 |
static inline bool DICT_TF2_FLAG_IS_SET(const dict_table_t *table, |
| 2715 |
uint32_t flag) { |
| 2716 |
return table->flags2 & flag; |
| 2717 |
} |
| 2718 |
|
| 2719 |
static inline void DICT_TF2_FLAG_UNSET(dict_table_t *table, uint32_t flag) { |
| 2720 |
table->flags2 &= ~flag; |
| 2721 |
} |
| 2722 |
|
| 2723 |
inline bool dict_index_t::is_compressed() const { |
| 2724 |
return (table->is_compressed()); |
| 2725 |
} |
| 2726 |
|
| 2727 |
/** Persistent dynamic metadata type, there should be 1 to 1 |
| 2728 |
relationship between the metadata and the type. Please keep them in order |
| 2729 |
so that we can iterate over it */ |
| 2730 |
enum persistent_type_t { |
| 2731 |
/** The smallest type, which should be 1 less than the first |
| 2732 |
true type */ |
| 2733 |
PM_SMALLEST_TYPE = 0, |
| 2734 |
|
| 2735 |
/** Persistent Metadata type for corrupted indexes */ |
| 2736 |
PM_INDEX_CORRUPTED = 1, |
| 2737 |
|
| 2738 |
/** Persistent Metadata type for autoinc counter */ |
| 2739 |
PM_TABLE_AUTO_INC = 2, |
| 2740 |
|
| 2741 |
/* TODO: Will add following types |
| 2742 |
PM_TABLE_UPDATE_TIME = 3, |
| 2743 |
Maybe something tablespace related |
| 2744 |
PM_TABLESPACE_SIZE = 4, |
| 2745 |
PM_TABLESPACE_MAX_TRX_ID = 5, */ |
| 2746 |
|
| 2747 |
/** The biggest type, which should be 1 bigger than the last |
| 2748 |
true type */ |
| 2749 |
PM_BIGGEST_TYPE = 3 |
| 2750 |
}; |
| 2751 |
|
| 2752 |
typedef std::vector<index_id_t, ut::allocator<index_id_t>> corrupted_ids_t; |
| 2753 |
|
| 2754 |
/** Persistent dynamic metadata for a table */ |
| 2755 |
class PersistentTableMetadata { |
| 2756 |
public: |
| 2757 |
/** Constructor |
| 2758 |
@param[in] id table id |
| 2759 |
@param[in] version table dynamic metadata version */ |
| 2760 |
PersistentTableMetadata(table_id_t id, uint64_t version) |
| 2761 |
: m_id(id), m_version(version), m_corrupted_ids(), m_autoinc(0) {} |
| 2762 |
|
| 2763 |
/** Get the corrupted indexes' IDs |
| 2764 |
@return the vector of indexes' IDs */ |
| 2765 |
const corrupted_ids_t &get_corrupted_indexes() const { |
| 2766 |
return (m_corrupted_ids); |
| 2767 |
} |
| 2768 |
|
| 2769 |
/** Add a corrupted index id and space id |
| 2770 |
@param[in] id corrupted index id */ |
| 2771 |
void add_corrupted_index(const index_id_t id) { |
| 2772 |
m_corrupted_ids.push_back(id); |
| 2773 |
} |
| 2774 |
|
| 2775 |
/** Set the dynamic metadata version. |
| 2776 |
@param[in] version dynamic metadata version */ |
| 2777 |
void set_version(uint64_t version) { m_version = version; } |
| 2778 |
|
| 2779 |
/** Get the dynamic metadata version */ |
| 2780 |
uint64_t get_version() const { return (m_version); } |
| 2781 |
|
| 2782 |
/** Get the table id of the metadata |
| 2783 |
@return table id */ |
| 2784 |
table_id_t get_table_id() const { return (m_id); } |
| 2785 |
|
| 2786 |
/** Set the autoinc counter of the table if it's bigger |
| 2787 |
@param[in] autoinc autoinc counter */ |
| 2788 |
void set_autoinc_if_bigger(uint64_t autoinc) { |
| 2789 |
/* We only set the biggest autoinc counter. Callers don't |
| 2790 |
guarantee passing a bigger number in. */ |
| 2791 |
if (autoinc > m_autoinc) { |
| 2792 |
m_autoinc = autoinc; |
| 2793 |
} |
| 2794 |
} |
| 2795 |
|
| 2796 |
/** Set the autoinc counter of the table |
| 2797 |
@param[in] autoinc autoinc counter */ |
| 2798 |
void set_autoinc(uint64_t autoinc) { m_autoinc = autoinc; } |
| 2799 |
|
| 2800 |
/** Get the autoinc counter of the table |
| 2801 |
@return the autoinc counter */ |
| 2802 |
uint64_t get_autoinc() const { return (m_autoinc); } |
| 2803 |
|
| 2804 |
private: |
| 2805 |
/** Table ID which this metadata belongs to */ |
| 2806 |
table_id_t m_id; |
| 2807 |
|
| 2808 |
/** Table dynamic metadata version of the change */ |
| 2809 |
uint64_t m_version; |
| 2810 |
|
| 2811 |
/** Storing the corrupted indexes' ID if exist, or else empty */ |
| 2812 |
corrupted_ids_t m_corrupted_ids; |
| 2813 |
|
| 2814 |
/** Autoinc counter of the table */ |
| 2815 |
uint64_t m_autoinc; |
| 2816 |
|
| 2817 |
/* TODO: We will add update_time, etc. here and APIs accordingly */ |
| 2818 |
}; |
| 2819 |
|
| 2820 |
/** Interface for persistent dynamic table metadata. */ |
| 2821 |
class Persister { |
| 2822 |
public: |
| 2823 |
/** Virtual destructor */ |
| 2824 |
virtual ~Persister() = default; |
| 2825 |
|
| 2826 |
/** Write the dynamic metadata of a table, we can pre-calculate |
| 2827 |
the size by calling get_write_size() |
| 2828 |
@param[in] metadata persistent data |
| 2829 |
@param[out] buffer write buffer |
| 2830 |
@param[in] size size of write buffer, should be |
| 2831 |
at least get_write_size() |
| 2832 |
@return the length of bytes written */ |
| 2833 |
virtual ulint write(const PersistentTableMetadata &metadata, byte *buffer, |
| 2834 |
ulint size) const = 0; |
| 2835 |
|
| 2836 |
/** Pre-calculate the size of metadata to be written |
| 2837 |
@param[in] metadata metadata to be written |
| 2838 |
@return the size of metadata */ |
| 2839 |
virtual ulint get_write_size( |
| 2840 |
const PersistentTableMetadata &metadata) const = 0; |
| 2841 |
|
| 2842 |
/** Read the dynamic metadata from buffer, and store them to |
| 2843 |
metadata object |
| 2844 |
@param[out] metadata metadata where we store the read data |
| 2845 |
@param[in] buffer buffer to read |
| 2846 |
@param[in] size size of buffer |
| 2847 |
@param[out] corrupt true if we found something wrong in |
| 2848 |
the buffer except incomplete buffer, |
| 2849 |
otherwise false |
| 2850 |
@return the bytes we read from the buffer if the buffer data |
| 2851 |
is complete and we get everything, 0 if the buffer is incompleted */ |
| 2852 |
virtual ulint read(PersistentTableMetadata &metadata, const byte *buffer, |
| 2853 |
ulint size, bool *corrupt) const = 0; |
| 2854 |
|
| 2855 |
/** Aggregate metadata entries into a single metadata instance, considering |
| 2856 |
version numbers |
| 2857 |
@param[in,out] metadata metadata object to be modified |
| 2858 |
@param[in] new_entry metadata entry from logs */ |
| 2859 |
virtual void aggregate(PersistentTableMetadata &metadata, |
| 2860 |
const PersistentTableMetadata &new_entry) const = 0; |
| 2861 |
|
| 2862 |
/** Write MLOG_TABLE_DYNAMIC_META for persistent dynamic |
| 2863 |
metadata of table |
| 2864 |
@param[in] id Table id |
| 2865 |
@param[in] metadata Metadata used to write the log |
| 2866 |
@param[in,out] mtr Mini-transaction */ |
| 2867 |
void write_log(table_id_t id, const PersistentTableMetadata &metadata, |
| 2868 |
mtr_t *mtr) const; |
| 2869 |
}; |
| 2870 |
|
| 2871 |
/** Persister used for corrupted indexes */ |
| 2872 |
class CorruptedIndexPersister : public Persister { |
| 2873 |
public: |
| 2874 |
/** Write the corrupted indexes of a table, we can pre-calculate the size |
| 2875 |
by calling get_write_size() |
| 2876 |
@param[in] metadata persistent data |
| 2877 |
@param[out] buffer write buffer |
| 2878 |
@param[in] size size of write buffer, should be at least |
| 2879 |
get_write_size() |
| 2880 |
@return the length of bytes written */ |
| 2881 |
ulint write(const PersistentTableMetadata &metadata, byte *buffer, |
| 2882 |
ulint size) const override; |
| 2883 |
|
| 2884 |
/** Pre-calculate the size of metadata to be written |
| 2885 |
@param[in] metadata metadata to be written |
| 2886 |
@return the size of metadata */ |
| 2887 |
ulint get_write_size(const PersistentTableMetadata &metadata) const override; |
| 2888 |
|
| 2889 |
/** Read the corrupted indexes from buffer, and store them to |
| 2890 |
metadata object |
| 2891 |
@param[out] metadata metadata where we store the read data |
| 2892 |
@param[in] buffer buffer to read |
| 2893 |
@param[in] size size of buffer |
| 2894 |
@param[out] corrupt true if we found something wrong in |
| 2895 |
the buffer except incomplete buffer, |
| 2896 |
otherwise false |
| 2897 |
@return the bytes we read from the buffer if the buffer data |
| 2898 |
is complete and we get everything, 0 if the buffer is incompleted */ |
| 2899 |
ulint read(PersistentTableMetadata &metadata, const byte *buffer, ulint size, |
| 2900 |
bool *corrupt) const override; |
| 2901 |
|
| 2902 |
void aggregate(PersistentTableMetadata &metadata, |
| 2903 |
const PersistentTableMetadata &new_entry) const override; |
| 2904 |
|
| 2905 |
private: |
| 2906 |
/** The length of index_id_t we will write */ |
| 2907 |
static const size_t INDEX_ID_LENGTH = 12; |
| 2908 |
}; |
| 2909 |
|
| 2910 |
/** Persister used for autoinc counters */ |
| 2911 |
class AutoIncPersister : public Persister { |
| 2912 |
public: |
| 2913 |
/** Write the autoinc counter of a table, we can pre-calculate |
| 2914 |
the size by calling get_write_size() |
| 2915 |
@param[in] metadata persistent metadata |
| 2916 |
@param[out] buffer write buffer |
| 2917 |
@param[in] size size of write buffer, should be |
| 2918 |
at least get_write_size() |
| 2919 |
@return the length of bytes written */ |
| 2920 |
ulint write(const PersistentTableMetadata &metadata, byte *buffer, |
| 2921 |
ulint size) const override; |
| 2922 |
|
| 2923 |
/** Pre-calculate the size of metadata to be written |
| 2924 |
@param[in] metadata metadata to be written |
| 2925 |
@return the size of metadata */ |
| 2926 |
inline ulint get_write_size(const PersistentTableMetadata &metadata |
| 2927 |
[[maybe_unused]]) const override { |
| 2928 |
/* We just return the max possible size that would be used |
| 2929 |
if the counter exists, so we don't calculate every time. |
| 2930 |
Here we need 1 byte for dynamic metadata type and 11 bytes |
| 2931 |
for the max possible size of counter. */ |
| 2932 |
return (12); |
| 2933 |
} |
| 2934 |
|
| 2935 |
/** Read the autoinc counter from buffer, and store them to |
| 2936 |
metadata object |
| 2937 |
@param[out] metadata metadata where we store the read data |
| 2938 |
@param[in] buffer buffer to read |
| 2939 |
@param[in] size size of buffer |
| 2940 |
@param[out] corrupt true if we found something wrong in |
| 2941 |
the buffer except incomplete buffer, |
| 2942 |
otherwise false |
| 2943 |
@return the bytes we read from the buffer if the buffer data |
| 2944 |
is complete and we get everything, 0 if the buffer is incomplete */ |
| 2945 |
ulint read(PersistentTableMetadata &metadata, const byte *buffer, ulint size, |
| 2946 |
bool *corrupt) const override; |
| 2947 |
|
| 2948 |
void aggregate(PersistentTableMetadata &metadata, |
| 2949 |
const PersistentTableMetadata &new_entry) const override; |
| 2950 |
}; |
| 2951 |
|
| 2952 |
/** Container of persisters used in the system. Currently we don't need |
| 2953 |
to protect this object since we only initialize it at very beginning and |
| 2954 |
destroy it in the end. During the server running, we only get the persisters */ |
| 2955 |
class Persisters { |
| 2956 |
typedef std::map< |
| 2957 |
persistent_type_t, Persister *, std::less<persistent_type_t>, |
| 2958 |
ut::allocator<std::pair<const persistent_type_t, Persister *>>> |
| 2959 |
persisters_t; |
| 2960 |
|
| 2961 |
public: |
| 2962 |
/** Constructor */ |
| 2963 |
Persisters() : m_persisters() {} |
| 2964 |
|
| 2965 |
/** Destructor */ |
| 2966 |
~Persisters(); |
| 2967 |
|
| 2968 |
/** Get the persister object with specified type |
| 2969 |
@param[in] type persister type |
| 2970 |
@return Persister object required or NULL if not found */ |
| 2971 |
Persister *get(persistent_type_t type) const; |
| 2972 |
|
| 2973 |
/** Add a specified persister of type, we will allocate the Persister |
| 2974 |
if there is no such persister exist, otherwise do nothing and return |
| 2975 |
the existing one |
| 2976 |
@param[in] type persister type |
| 2977 |
@return the persister of type */ |
| 2978 |
Persister *add(persistent_type_t type); |
| 2979 |
|
| 2980 |
/** Remove a specified persister of type, we will free the Persister |
| 2981 |
@param[in] type persister type */ |
| 2982 |
void remove(persistent_type_t type); |
| 2983 |
|
| 2984 |
/** Serialize the metadata to a buffer |
| 2985 |
@param[in] metadata metadata to serialize |
| 2986 |
@param[out] buffer buffer to store the serialized metadata |
| 2987 |
@return the length of serialized metadata */ |
| 2988 |
size_t write(PersistentTableMetadata &metadata, byte *buffer); |
| 2989 |
|
| 2990 |
private: |
| 2991 |
/** A map to store all persisters needed */ |
| 2992 |
persisters_t m_persisters; |
| 2993 |
}; |
| 2994 |
|
| 2995 |
#ifndef UNIV_HOTBACKUP |
| 2996 |
|
| 2997 |
/** A function object to add the foreign key constraint to the referenced set |
| 2998 |
of the referenced table, if it exists in the dictionary cache. */ |
| 2999 |
struct dict_foreign_add_to_referenced_table { |
| 3000 |
void operator()(dict_foreign_t *foreign) const { |
| 3001 |
if (dict_table_t *table = foreign->referenced_table) { |
| 3002 |
std::pair<dict_foreign_set::iterator, bool> ret = |
| 3003 |
table->referenced_set.insert(foreign); |
| 3004 |
ut_a(ret.second); |
| 3005 |
} |
| 3006 |
} |
| 3007 |
}; |
| 3008 |
|
| 3009 |
/** Request for lazy creation of the mutex of a given table. |
| 3010 |
This function is only called from either single threaded environment |
| 3011 |
or from a thread that has not shared the table object with other threads. |
| 3012 |
@param[in,out] table table whose mutex is to be created */ |
| 3013 |
inline void dict_table_mutex_create_lazy(dict_table_t *table) { |
| 3014 |
table->mutex = nullptr; |
| 3015 |
table->mutex_created = os_once::NEVER_DONE; |
| 3016 |
} |
| 3017 |
|
| 3018 |
/** Destroy the mutex of a given table. |
| 3019 |
This function is only called from either single threaded environment |
| 3020 |
or from a thread that has not shared the table object with other threads. |
| 3021 |
@param[in,out] table table whose mutex is to be created */ |
| 3022 |
inline void dict_table_mutex_destroy(dict_table_t *table) { |
| 3023 |
if (table->mutex_created == os_once::DONE) { |
| 3024 |
if (table->mutex != nullptr) { |
| 3025 |
mutex_free(table->mutex); |
| 3026 |
ut::delete_(table->mutex); |
| 3027 |
} |
| 3028 |
} |
| 3029 |
} |
| 3030 |
|
| 3031 |
/** Destroy the autoinc latch of the given table. |
| 3032 |
This function is only called from either single threaded environment |
| 3033 |
or from a thread that has not shared the table object with other threads. |
| 3034 |
@param[in,out] table table whose stats latch to destroy */ |
| 3035 |
inline void dict_table_autoinc_destroy(dict_table_t *table) { |
| 3036 |
if (table->autoinc_mutex_created == os_once::DONE) { |
| 3037 |
if (table->autoinc_mutex != nullptr) { |
| 3038 |
mutex_free(table->autoinc_mutex); |
| 3039 |
ut::delete_(table->autoinc_mutex); |
| 3040 |
} |
| 3041 |
|
| 3042 |
if (table->autoinc_persisted_mutex != nullptr) { |
| 3043 |
mutex_free(table->autoinc_persisted_mutex); |
| 3044 |
ut::delete_(table->autoinc_persisted_mutex); |
| 3045 |
} |
| 3046 |
} |
| 3047 |
} |
| 3048 |
|
| 3049 |
/** Request for lazy creation of the autoinc latch of a given table. |
| 3050 |
This function is only called from either single threaded environment |
| 3051 |
or from a thread that has not shared the table object with other threads. |
| 3052 |
@param[in,out] table table whose autoinc latch is to be created. */ |
| 3053 |
inline void dict_table_autoinc_create_lazy(dict_table_t *table) { |
| 3054 |
table->autoinc_mutex = nullptr; |
| 3055 |
table->autoinc_persisted_mutex = nullptr; |
| 3056 |
table->autoinc_mutex_created = os_once::NEVER_DONE; |
| 3057 |
} |
| 3058 |
|
| 3059 |
/** Request a lazy creation of dict_index_t::zip_pad::mutex. |
| 3060 |
This function is only called from either single threaded environment |
| 3061 |
or from a thread that has not shared the table object with other threads. |
| 3062 |
@param[in,out] index index whose zip_pad mutex is to be created */ |
| 3063 |
inline void dict_index_zip_pad_mutex_create_lazy(dict_index_t *index) { |
| 3064 |
index->zip_pad.mutex = nullptr; |
| 3065 |
index->zip_pad.mutex_created = os_once::NEVER_DONE; |
| 3066 |
} |
| 3067 |
|
| 3068 |
/** Destroy the zip_pad_mutex of the given index. |
| 3069 |
This function is only called from either single threaded environment |
| 3070 |
or from a thread that has not shared the table object with other threads. |
| 3071 |
@param[in,out] index index whose stats latch to destroy */ |
| 3072 |
inline void dict_index_zip_pad_mutex_destroy(dict_index_t *index) { |
| 3073 |
if (index->zip_pad.mutex_created == os_once::DONE && |
| 3074 |
index->zip_pad.mutex != nullptr) { |
| 3075 |
mutex_free(index->zip_pad.mutex); |
| 3076 |
ut::delete_(index->zip_pad.mutex); |
| 3077 |
} |
| 3078 |
} |
| 3079 |
#endif /* !UNIV_HOTBACKUP */ |
| 3080 |
|
| 3081 |
/** Release the zip_pad_mutex of a given index. |
| 3082 |
@param[in,out] index index whose zip_pad_mutex is to be released */ |
| 3083 |
inline void dict_index_zip_pad_unlock(dict_index_t *index) { |
| 3084 |
#ifndef UNIV_HOTBACKUP |
| 3085 |
mutex_exit(index->zip_pad.mutex); |
| 3086 |
#endif /* !UNIV_HOTBACKUP */ |
| 3087 |
} |
| 3088 |
|
| 3089 |
#ifdef UNIV_DEBUG |
| 3090 |
/** Check if the current thread owns the autoinc_mutex of a given table. |
| 3091 |
@param[in] table the autoinc_mutex belongs to this table |
| 3092 |
@return true, if the current thread owns the autoinc_mutex, false otherwise.*/ |
| 3093 |
inline bool dict_table_autoinc_own(const dict_table_t *table) { |
| 3094 |
return (mutex_own(table->autoinc_mutex)); |
| 3095 |
} |
| 3096 |
#endif /* UNIV_DEBUG */ |
| 3097 |
|
| 3098 |
#include "dict0mem.ic" |
| 3099 |
|
| 3100 |
#endif /* dict0mem_h */ |