| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1996, 2026, Oracle and/or its affiliates. |
| 4 |
Copyright (c) 2012, Facebook Inc. |
| 5 |
|
| 6 |
This program is free software; you can redistribute it and/or modify it under |
| 7 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 8 |
Free Software Foundation. |
| 9 |
|
| 10 |
This program is designed to work with certain software (including |
| 11 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 12 |
as designated in a particular file or component or in included license |
| 13 |
documentation. The authors of MySQL hereby grant you an additional |
| 14 |
permission to link the program and your derivative works with the |
| 15 |
separately licensed software that they have either included with |
| 16 |
the program or referenced in the documentation. |
| 17 |
|
| 18 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 19 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 20 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 21 |
for more details. |
| 22 |
|
| 23 |
You should have received a copy of the GNU General Public License along with |
| 24 |
this program; if not, write to the Free Software Foundation, Inc., |
| 25 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 26 |
|
| 27 |
*****************************************************************************/ |
| 28 |
|
| 29 |
/** @file dict/dict0dict.cc |
| 30 |
Data dictionary system |
| 31 |
|
| 32 |
Created 1/8/1996 Heikki Tuuri |
| 33 |
***********************************************************************/ |
| 34 |
|
| 35 |
#include "debug_sync.h" |
| 36 |
#include "my_config.h" |
| 37 |
|
| 38 |
#include <stdlib.h> |
| 39 |
#include <strfunc.h> |
| 40 |
#include <sys/types.h> |
| 41 |
#include <algorithm> |
| 42 |
#include <string> |
| 43 |
|
| 44 |
#ifndef UNIV_HOTBACKUP |
| 45 |
#include "current_thd.h" |
| 46 |
#endif /* !UNIV_HOTBACKUP */ |
| 47 |
#include "dict0dict.h" |
| 48 |
#include "fil0fil.h" |
| 49 |
#ifndef UNIV_HOTBACKUP |
| 50 |
#include "fts0fts.h" |
| 51 |
#endif /* !UNIV_HOTBACKUP */ |
| 52 |
#include "ha_prototypes.h" |
| 53 |
#include "log0chkp.h" |
| 54 |
#include "log0write.h" |
| 55 |
#include "my_dbug.h" |
| 56 |
|
| 57 |
#ifndef UNIV_HOTBACKUP |
| 58 |
#include "clone0api.h" |
| 59 |
#include "mysqld.h" // system_charset_info |
| 60 |
#include "que0types.h" |
| 61 |
#include "row0sel.h" |
| 62 |
#endif /* !UNIV_HOTBACKUP */ |
| 63 |
|
| 64 |
#if defined UNIV_HOTBACKUP && defined UNIV_DEBUG |
| 65 |
static inline bool dict_non_lru_find_table(const dict_table_t *x) { |
| 66 |
return true; |
| 67 |
} |
| 68 |
#endif /* UNIV_HOTBACKUP && UNIV_DEBUG */ |
| 69 |
|
| 70 |
/** dummy index for ROW_FORMAT=REDUNDANT supremum and infimum records */ |
| 71 |
dict_index_t *dict_ind_redundant; |
| 72 |
|
| 73 |
#if defined UNIV_DEBUG || defined UNIV_IBUF_DEBUG |
| 74 |
/** Flag to control insert buffer debugging. */ |
| 75 |
extern uint ibuf_debug; |
| 76 |
#endif /* UNIV_DEBUG || UNIV_IBUF_DEBUG */ |
| 77 |
|
| 78 |
#include <algorithm> |
| 79 |
#include <vector> |
| 80 |
|
| 81 |
#include "btr0btr.h" |
| 82 |
#include "btr0cur.h" |
| 83 |
#include "btr0sea.h" |
| 84 |
#include "buf0buf.h" |
| 85 |
#include "data0type.h" |
| 86 |
#include "dict0boot.h" |
| 87 |
#include "dict0crea.h" |
| 88 |
#ifndef UNIV_HOTBACKUP |
| 89 |
#include "dict0dd.h" |
| 90 |
#endif /* !UNIV_HOTBACKUP */ |
| 91 |
#include "dict0mem.h" |
| 92 |
#include "dict0priv.h" |
| 93 |
#ifndef UNIV_HOTBACKUP |
| 94 |
#include "dict0stats.h" |
| 95 |
#endif /* !UNIV_HOTBACKUP */ |
| 96 |
#include "fsp0sysspace.h" |
| 97 |
#ifndef UNIV_HOTBACKUP |
| 98 |
#include "fts0fts.h" |
| 99 |
#include "fts0types.h" |
| 100 |
#include "lock0lock.h" |
| 101 |
#endif /* !UNIV_HOTBACKUP */ |
| 102 |
#include "mach0data.h" |
| 103 |
#include "mem0mem.h" |
| 104 |
#include "os0once.h" |
| 105 |
#include "page0page.h" |
| 106 |
#include "page0zip.h" |
| 107 |
#ifndef UNIV_HOTBACKUP |
| 108 |
#include "pars0pars.h" |
| 109 |
#include "pars0sym.h" |
| 110 |
#include "que0que.h" |
| 111 |
#endif /* !UNIV_HOTBACKUP */ |
| 112 |
#include "rem0cmp.h" |
| 113 |
#include "row0ins.h" |
| 114 |
#include "row0log.h" |
| 115 |
#ifndef UNIV_HOTBACKUP |
| 116 |
#include "ddl0ddl.h" |
| 117 |
#include "row0mysql.h" |
| 118 |
#endif /* !UNIV_HOTBACKUP */ |
| 119 |
#include "row0upd.h" |
| 120 |
#ifndef UNIV_HOTBACKUP |
| 121 |
#include "ha_innodb.h" |
| 122 |
#include "srv0mon.h" |
| 123 |
#include "srv0start.h" |
| 124 |
#include "sync0sync.h" |
| 125 |
#include "trx0undo.h" |
| 126 |
#include "ut0new.h" |
| 127 |
#endif /* !UNIV_HOTBACKUP */ |
| 128 |
|
| 129 |
static_assert(DATA_ROW_ID == 0, "DATA_ROW_ID != 0"); |
| 130 |
static_assert(DATA_TRX_ID == 1, "DATA_TRX_ID != 1"); |
| 131 |
static_assert(DATA_ROLL_PTR == 2, "DATA_ROLL_PTR != 2"); |
| 132 |
static_assert(DATA_N_SYS_COLS == 3, "DATA_N_SYS_COLS != 3"); |
| 133 |
static_assert(DATA_TRX_ID_LEN == 6, "DATA_TRX_ID_LEN != 6"); |
| 134 |
static_assert(DATA_ITT_N_SYS_COLS == 2, "DATA_ITT_N_SYS_COLS != 2"); |
| 135 |
|
| 136 |
/** the dictionary system */ |
| 137 |
dict_sys_t *dict_sys = nullptr; |
| 138 |
|
| 139 |
/** The set of SE private IDs of DD tables. Used to tell whether a table is |
| 140 |
a DD table. Since the DD tables can be rebuilt with new SE private IDs, |
| 141 |
this set replaces checks based on ranges of IDs. */ |
| 142 |
std::set<dd::Object_id> dict_sys_t::s_dd_table_ids = {}; |
| 143 |
|
| 144 |
/** The name of the data dictionary tablespace. */ |
| 145 |
const char *dict_sys_t::s_dd_space_name = "mysql"; |
| 146 |
|
| 147 |
/** The file name of the data dictionary tablespace */ |
| 148 |
const char *dict_sys_t::s_dd_space_file_name = "mysql.ibd"; |
| 149 |
|
| 150 |
/** The name of the hard-coded system tablespace. */ |
| 151 |
const char *dict_sys_t::s_sys_space_name = "innodb_system"; |
| 152 |
|
| 153 |
/** The name of the predefined temporary tablespace. */ |
| 154 |
const char *dict_sys_t::s_temp_space_name = "innodb_temporary"; |
| 155 |
|
| 156 |
/** The file name of the predefined temporary tablespace */ |
| 157 |
const char *dict_sys_t::s_temp_space_file_name = "ibtmp1"; |
| 158 |
|
| 159 |
/** The hard-coded tablespace name innodb_file_per_table. */ |
| 160 |
const char *dict_sys_t::s_file_per_table_name = "innodb_file_per_table"; |
| 161 |
|
| 162 |
/** These two undo tablespaces cannot be dropped. */ |
| 163 |
const char *dict_sys_t::s_default_undo_space_name_1 = "innodb_undo_001"; |
| 164 |
const char *dict_sys_t::s_default_undo_space_name_2 = "innodb_undo_002"; |
| 165 |
|
| 166 |
/** the dictionary persisting structure */ |
| 167 |
dict_persist_t *dict_persist = nullptr; |
| 168 |
|
| 169 |
/** @brief the data dictionary rw-latch protecting dict_sys |
| 170 |
|
| 171 |
table create, drop, etc. reserve this in X-mode; implicit or |
| 172 |
background operations purge, rollback, foreign key checks reserve this |
| 173 |
in S-mode; we cannot trust that MySQL protects implicit or background |
| 174 |
operations a table drop since MySQL does not know of them; therefore |
| 175 |
we need this; NOTE: a transaction which reserves this must keep book |
| 176 |
on the mode in trx_t::dict_operation_lock_mode */ |
| 177 |
rw_lock_t *dict_operation_lock; |
| 178 |
|
| 179 |
/** Percentage of compression failures that are allowed in a single |
| 180 |
round */ |
| 181 |
ulong zip_failure_threshold_pct = 5; |
| 182 |
|
| 183 |
/** Maximum percentage of a page that can be allowed as a pad to avoid |
| 184 |
compression failures */ |
| 185 |
ulong zip_pad_max = 50; |
| 186 |
|
| 187 |
/** buffer pool max size per table hash table fixed size in bytes */ |
| 188 |
constexpr uint32_t DICT_POOL_PER_TABLE_HASH = 512; |
| 189 |
|
| 190 |
#ifndef UNIV_HOTBACKUP |
| 191 |
/** Identifies generated InnoDB foreign key names */ |
| 192 |
static char dict_ibfk[] = "_ibfk_"; |
| 193 |
|
| 194 |
/** Array to store table_ids of INNODB_SYS_* TABLES */ |
| 195 |
static table_id_t dict_sys_table_id[SYS_NUM_SYSTEM_TABLES]; |
| 196 |
|
| 197 |
/** Tries to find column names for the index and sets the col field of the |
| 198 |
index. |
| 199 |
@param[in] table table |
| 200 |
@param[in,out] index index |
| 201 |
@param[in] add_v new virtual columns added along with an add index call |
| 202 |
@return true if the column names were found */ |
| 203 |
static bool dict_index_find_and_set_cols(const dict_table_t *table, |
| 204 |
dict_index_t *index, |
| 205 |
const dict_add_v_col_t *add_v); |
| 206 |
/** Builds the internal dictionary cache representation for a clustered |
| 207 |
index, containing also system fields not defined by the user. |
| 208 |
@return own: the internal representation of the clustered index */ |
| 209 |
static dict_index_t *dict_index_build_internal_clust( |
| 210 |
const dict_table_t *table, /*!< in: table */ |
| 211 |
dict_index_t *index); /*!< in: user representation of |
| 212 |
a clustered index */ |
| 213 |
/** Builds the internal dictionary cache representation for a non-clustered |
| 214 |
index, containing also system fields not defined by the user. |
| 215 |
@return own: the internal representation of the non-clustered index */ |
| 216 |
static dict_index_t *dict_index_build_internal_non_clust( |
| 217 |
const dict_table_t *table, /*!< in: table */ |
| 218 |
dict_index_t *index); /*!< in: user representation of |
| 219 |
a non-clustered index */ |
| 220 |
/** Builds the internal dictionary cache representation for an FTS index. |
| 221 |
@return own: the internal representation of the FTS index */ |
| 222 |
static dict_index_t *dict_index_build_internal_fts( |
| 223 |
dict_table_t *table, /*!< in: table */ |
| 224 |
dict_index_t *index); /*!< in: user representation of an FTS index */ |
| 225 |
|
| 226 |
/** Removes an index from the dictionary cache. */ |
| 227 |
static void dict_index_remove_from_cache_low( |
| 228 |
dict_table_t *table, /*!< in/out: table */ |
| 229 |
dict_index_t *index, /*!< in, own: index */ |
| 230 |
bool lru_evict); /*!< in: true if page being evicted |
| 231 |
to make room in the table LRU list */ |
| 232 |
|
| 233 |
/** Calculate and update the redo log margin for current tables which |
| 234 |
have some changed dynamic metadata in memory and have not been written |
| 235 |
back to mysql.innodb_dynamic_metadata. Update LSN limit, which is used |
| 236 |
to stop user threads when redo log is running out of space and they |
| 237 |
do not hold latches (log.free_check_limit_lsn). */ |
| 238 |
static void dict_persist_update_log_margin(void); |
| 239 |
|
| 240 |
/** Removes a table object from the dictionary cache. */ |
| 241 |
static void dict_table_remove_from_cache_low( |
| 242 |
dict_table_t *table, /*!< in, own: table */ |
| 243 |
bool lru_evict); /*!< in: true if evicting from LRU */ |
| 244 |
|
| 245 |
#ifdef UNIV_DEBUG |
| 246 |
/** Validate the dictionary table LRU list. |
| 247 |
@return true if validate OK */ |
| 248 |
static bool dict_lru_validate(); |
| 249 |
/** Check if table is in the dictionary table LRU list. |
| 250 |
@param[in] find_table table to find |
| 251 |
@return true if table found in LRU list. */ |
| 252 |
static bool dict_lru_find_table(const dict_table_t *find_table); |
| 253 |
/** Check if a table exists in the dict table non-LRU list. |
| 254 |
@param[in] find_table table to find |
| 255 |
@return true if table found in non-LRU list */ |
| 256 |
static bool dict_non_lru_find_table(const dict_table_t *find_table); |
| 257 |
#endif /* UNIV_DEBUG */ |
| 258 |
|
| 259 |
/* Stream for storing detailed information about the latest foreign key |
| 260 |
and unique key errors. Only created if !srv_read_only_mode */ |
| 261 |
FILE *dict_foreign_err_file = nullptr; |
| 262 |
/* mutex protecting the foreign and unique error buffers */ |
| 263 |
ib_mutex_t dict_foreign_err_mutex; |
| 264 |
|
| 265 |
/** Checks if the database name in two table names is the same. |
| 266 |
@return true if same db name */ |
| 267 |
bool dict_tables_have_same_db(const char *name1, /*!< in: table name in the |
| 268 |
form dbname '/' tablename */ |
| 269 |
const char *name2) /*!< in: table name in the |
| 270 |
form dbname '/' tablename */ |
| 271 |
{ |
| 272 |
for (; *name1 == *name2; name1++, name2++) { |
| 273 |
if (*name1 == '/') { |
| 274 |
return true; |
| 275 |
} |
| 276 |
ut_a(*name1); /* the names must contain '/' */ |
| 277 |
} |
| 278 |
return false; |
| 279 |
} |
| 280 |
|
| 281 |
/** Return the end of table name where we have removed dbname and '/'. |
| 282 |
@return table name */ |
| 283 |
const char *dict_remove_db_name(const char *name) /*!< in: table name in the |
| 284 |
form dbname '/' tablename */ |
| 285 |
{ |
| 286 |
const char *s = strchr(name, '/'); |
| 287 |
ut_a(s); |
| 288 |
|
| 289 |
return (s + 1); |
| 290 |
} |
| 291 |
#endif /* !UNIV_HOTBACKUP */ |
| 292 |
|
| 293 |
/** Get the database name length in a table name. |
| 294 |
@return database name length */ |
| 295 |
ulint dict_get_db_name_len(const char *name) /*!< in: table name in the form |
| 296 |
dbname '/' tablename */ |
| 297 |
{ |
| 298 |
const char *s; |
| 299 |
s = strchr(name, '/'); |
| 300 |
if (s == nullptr) { |
| 301 |
return (0); |
| 302 |
} |
| 303 |
return (s - name); |
| 304 |
} |
| 305 |
|
| 306 |
#ifndef UNIV_HOTBACKUP |
| 307 |
/** Reserves the dictionary system mutex for MySQL. */ |
| 308 |
void dict_mutex_enter_for_mysql(void) { dict_sys_mutex_enter(); } |
| 309 |
|
| 310 |
/** Releases the dictionary system mutex for MySQL. */ |
| 311 |
void dict_mutex_exit_for_mysql(void) { dict_sys_mutex_exit(); } |
| 312 |
|
| 313 |
/** Allocate and init a dict_table_t's stats latch. |
| 314 |
This function must not be called concurrently on the same table object. |
| 315 |
@param[in,out] table_void table whose stats latch to create */ |
| 316 |
static void dict_table_stats_latch_alloc(void *table_void) { |
| 317 |
dict_table_t *table = static_cast<dict_table_t *>(table_void); |
| 318 |
|
| 319 |
/* Note: rw_lock_create() will call the constructor */ |
| 320 |
|
| 321 |
table->stats_latch = static_cast<rw_lock_t *>( |
| 322 |
ut::malloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(rw_lock_t))); |
| 323 |
|
| 324 |
ut_a(table->stats_latch != nullptr); |
| 325 |
|
| 326 |
rw_lock_create(dict_table_stats_key, table->stats_latch, |
| 327 |
LATCH_ID_DICT_TABLE_STATS); |
| 328 |
} |
| 329 |
|
| 330 |
/** Deinit and free a dict_table_t's stats latch. |
| 331 |
This function must not be called concurrently on the same table object. |
| 332 |
@param[in,out] table table whose stats latch to free */ |
| 333 |
static void dict_table_stats_latch_free(dict_table_t *table) { |
| 334 |
rw_lock_free(table->stats_latch); |
| 335 |
ut::free(table->stats_latch); |
| 336 |
} |
| 337 |
|
| 338 |
/** Create a dict_table_t's stats latch or delay for lazy creation. |
| 339 |
This function is only called from either single threaded environment |
| 340 |
or from a thread that has not shared the table object with other threads. |
| 341 |
@param[in,out] table table whose stats latch to create |
| 342 |
@param[in] enabled if false then the latch is disabled |
| 343 |
and dict_table_stats_lock()/unlock() become noop on this table. */ |
| 344 |
void dict_table_stats_latch_create(dict_table_t *table, bool enabled) { |
| 345 |
if (!enabled) { |
| 346 |
table->stats_latch = nullptr; |
| 347 |
table->stats_latch_created = os_once::DONE; |
| 348 |
return; |
| 349 |
} |
| 350 |
|
| 351 |
/* We create this lazily the first time it is used. */ |
| 352 |
table->stats_latch = nullptr; |
| 353 |
table->stats_latch_created = os_once::NEVER_DONE; |
| 354 |
} |
| 355 |
|
| 356 |
/** Destroy a dict_table_t's stats latch. |
| 357 |
This function is only called from either single threaded environment |
| 358 |
or from a thread that has not shared the table object with other threads. |
| 359 |
@param[in,out] table table whose stats latch to destroy */ |
| 360 |
void dict_table_stats_latch_destroy(dict_table_t *table) { |
| 361 |
if (table->stats_latch_created == os_once::DONE && |
| 362 |
table->stats_latch != nullptr) { |
| 363 |
dict_table_stats_latch_free(table); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/** Lock the appropriate latch to protect a given table's statistics. |
| 368 |
@param[in] table table whose stats to lock |
| 369 |
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */ |
| 370 |
void dict_table_stats_lock(dict_table_t *table, ulint latch_mode) { |
| 371 |
ut_ad(table != nullptr); |
| 372 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 373 |
|
| 374 |
os_once::do_or_wait_for_done(&table->stats_latch_created, |
| 375 |
dict_table_stats_latch_alloc, table); |
| 376 |
|
| 377 |
if (table->stats_latch == nullptr) { |
| 378 |
/* This is a dummy table object that is private in the current |
| 379 |
thread and is not shared between multiple threads, thus we |
| 380 |
skip any locking. */ |
| 381 |
return; |
| 382 |
} |
| 383 |
|
| 384 |
switch (latch_mode) { |
| 385 |
case RW_S_LATCH: |
| 386 |
rw_lock_s_lock(table->stats_latch, UT_LOCATION_HERE); |
| 387 |
break; |
| 388 |
case RW_X_LATCH: |
| 389 |
rw_lock_x_lock(table->stats_latch, UT_LOCATION_HERE); |
| 390 |
break; |
| 391 |
case RW_NO_LATCH: |
| 392 |
[[fallthrough]]; |
| 393 |
default: |
| 394 |
ut_error; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
/** Unlock the latch that has been locked by dict_table_stats_lock(). |
| 399 |
@param[in] table table whose stats to unlock |
| 400 |
@param[in] latch_mode RW_S_LATCH or RW_X_LATCH */ |
| 401 |
void dict_table_stats_unlock(dict_table_t *table, ulint latch_mode) { |
| 402 |
ut_ad(table != nullptr); |
| 403 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 404 |
|
| 405 |
if (table->stats_latch == nullptr) { |
| 406 |
/* This is a dummy table object that is private in the current |
| 407 |
thread and is not shared between multiple threads, thus we |
| 408 |
skip any locking. */ |
| 409 |
return; |
| 410 |
} |
| 411 |
|
| 412 |
switch (latch_mode) { |
| 413 |
case RW_S_LATCH: |
| 414 |
rw_lock_s_unlock(table->stats_latch); |
| 415 |
break; |
| 416 |
case RW_X_LATCH: |
| 417 |
rw_lock_x_unlock(table->stats_latch); |
| 418 |
break; |
| 419 |
case RW_NO_LATCH: |
| 420 |
[[fallthrough]]; |
| 421 |
default: |
| 422 |
ut_error; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/** Try to drop any indexes after an aborted index creation. |
| 427 |
This can also be after a server kill during DROP INDEX. */ |
| 428 |
static void dict_table_try_drop_aborted( |
| 429 |
dict_table_t *table, /*!< in: table, or NULL if it |
| 430 |
needs to be looked up again */ |
| 431 |
table_id_t table_id, /*!< in: table identifier */ |
| 432 |
ulint ref_count) /*!< in: expected table->n_ref_count */ |
| 433 |
{ |
| 434 |
trx_t *trx; |
| 435 |
|
| 436 |
trx = trx_allocate_for_background(); |
| 437 |
trx->op_info = "try to drop any indexes after an aborted index creation"; |
| 438 |
row_mysql_lock_data_dictionary(trx, UT_LOCATION_HERE); |
| 439 |
trx_set_dict_operation(trx, TRX_DICT_OP_INDEX); |
| 440 |
|
| 441 |
if (table == nullptr) { |
| 442 |
table = dd_table_open_on_id(table_id, nullptr, nullptr, true, true); |
| 443 |
|
| 444 |
/* Decrement the ref count. The table is MDL locked, so should |
| 445 |
not be dropped */ |
| 446 |
if (table) { |
| 447 |
dd_table_close(table, nullptr, nullptr, true); |
| 448 |
} |
| 449 |
} else { |
| 450 |
ut_ad(table->id == table_id); |
| 451 |
} |
| 452 |
|
| 453 |
if (table && table->get_ref_count() == ref_count && table->drop_aborted) { |
| 454 |
/* Silence a debug assertion in ddl::drop_indexes(). */ |
| 455 |
ut_d(table->acquire()); |
| 456 |
ddl::drop_indexes(trx, table, true); |
| 457 |
ut_d(table->release()); |
| 458 |
ut_ad(table->get_ref_count() == ref_count); |
| 459 |
trx_commit_for_mysql(trx); |
| 460 |
} |
| 461 |
|
| 462 |
row_mysql_unlock_data_dictionary(trx); |
| 463 |
trx_free_for_background(trx); |
| 464 |
} |
| 465 |
|
| 466 |
/** When opening a table, |
| 467 |
try to drop any indexes after an aborted index creation. |
| 468 |
Release the dict_sys->mutex. */ |
| 469 |
static void dict_table_try_drop_aborted_and_mutex_exit( |
| 470 |
dict_table_t *table, /*!< in: table (may be NULL) */ |
| 471 |
bool try_drop) /*!< in: false if should try to |
| 472 |
drop indexes whose online creation |
| 473 |
was aborted */ |
| 474 |
{ |
| 475 |
if (try_drop && table != nullptr && table->drop_aborted && |
| 476 |
table->get_ref_count() == 1 && table->first_index()) { |
| 477 |
/* Attempt to drop the indexes whose online creation |
| 478 |
was aborted. */ |
| 479 |
table_id_t table_id = table->id; |
| 480 |
|
| 481 |
dict_sys_mutex_exit(); |
| 482 |
|
| 483 |
dict_table_try_drop_aborted(table, table_id, 1); |
| 484 |
} else { |
| 485 |
dict_sys_mutex_exit(); |
| 486 |
} |
| 487 |
} |
| 488 |
#endif /* !UNIV_HOTBACKUP */ |
| 489 |
|
| 490 |
/** Decrements the count of open handles to a table. |
| 491 |
@param[in,out] table Table |
| 492 |
@param[in] dict_locked True=data dictionary locked |
| 493 |
@param[in] try_drop True=try to drop any orphan indexes after an aborted online |
| 494 |
index creation */ |
| 495 |
void dict_table_close(dict_table_t *table, bool dict_locked, bool try_drop) { |
| 496 |
ut_a(table->get_ref_count() > 0); |
| 497 |
|
| 498 |
#ifndef UNIV_HOTBACKUP |
| 499 |
#ifdef UNIV_DEBUG |
| 500 |
if (!table->is_intrinsic()) { |
| 501 |
/* This is now only for validation in debug mode */ |
| 502 |
if (!dict_locked) { |
| 503 |
dict_sys_mutex_enter(); |
| 504 |
} |
| 505 |
|
| 506 |
ut_ad(dict_lru_validate()); |
| 507 |
|
| 508 |
if (table->can_be_evicted) { |
| 509 |
ut_ad(dict_lru_find_table(table)); |
| 510 |
} else { |
| 511 |
ut_ad(dict_non_lru_find_table(table)); |
| 512 |
} |
| 513 |
|
| 514 |
if (!dict_locked) { |
| 515 |
dict_sys_mutex_exit(); |
| 516 |
} |
| 517 |
} |
| 518 |
#endif /* UNIV_DEBUG */ |
| 519 |
#endif /* !UNIV_HOTBACKUP */ |
| 520 |
|
| 521 |
if (!table->is_intrinsic()) { |
| 522 |
/* Ask for lock to prevent concurrent table open, |
| 523 |
in case the race of n_ref_count and stat_initialized in |
| 524 |
dict_stats_deinit(). See dict_table_t::acquire_with_lock() too. |
| 525 |
We don't actually need dict_sys mutex any more here. */ |
| 526 |
table->lock(); |
| 527 |
} |
| 528 |
|
| 529 |
auto drop_aborted = try_drop && table->drop_aborted && |
| 530 |
table->get_ref_count() == 1 && table->first_index(); |
| 531 |
|
| 532 |
table->release(); |
| 533 |
|
| 534 |
#ifndef UNIV_HOTBACKUP |
| 535 |
/* Intrinsic table is not added to dictionary cache so skip other |
| 536 |
cache specific actions. */ |
| 537 |
if (table->is_intrinsic()) { |
| 538 |
return; |
| 539 |
} |
| 540 |
|
| 541 |
/* Force persistent stats re-read upon next open of the table |
| 542 |
so that FLUSH TABLE can be used to forcibly fetch stats from disk |
| 543 |
if they have been manually modified. We reset table->stat_initialized |
| 544 |
only if table reference count is 0 because we do not want too frequent |
| 545 |
stats re-reads (e.g. in other cases than FLUSH TABLE). */ |
| 546 |
if (strchr(table->name.m_name, '/') != nullptr && |
| 547 |
table->get_ref_count() == 0 && dict_stats_is_persistent_enabled(table)) { |
| 548 |
DEBUG_SYNC(current_thd, "innodb.before_stats_deinit"); |
| 549 |
dict_stats_deinit(table); |
| 550 |
} |
| 551 |
|
| 552 |
if (!dict_locked) { |
| 553 |
if (drop_aborted) { |
| 554 |
ut_d(ut_error); |
| 555 |
ut_o(dict_table_try_drop_aborted(nullptr, table->id, 0)); |
| 556 |
} |
| 557 |
} |
| 558 |
#endif /* !UNIV_HOTBACKUP */ |
| 559 |
|
| 560 |
if (!table->is_intrinsic()) { |
| 561 |
table->unlock(); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
#ifndef UNIV_HOTBACKUP |
| 566 |
/** Closes the only open handle to a table and drops a table while assuring |
| 567 |
that dict_sys->mutex is held the whole time. This assures that the table |
| 568 |
is not evicted after the close when the count of open handles goes to zero. |
| 569 |
Because dict_sys->mutex is held, we do not need to call |
| 570 |
dict_table_prevent_eviction(). */ |
| 571 |
void dict_table_close_and_drop( |
| 572 |
trx_t *trx, /*!< in: data dictionary transaction */ |
| 573 |
dict_table_t *table) /*!< in/out: table */ |
| 574 |
{ |
| 575 |
ut_ad(dict_sys_mutex_own()); |
| 576 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 577 |
ut_ad(trx_state_eq(trx, TRX_STATE_ACTIVE)); |
| 578 |
|
| 579 |
dict_table_close(table, true, false); |
| 580 |
|
| 581 |
#if defined UNIV_DEBUG || defined UNIV_DDL_DEBUG |
| 582 |
/* Nobody should have initialized the stats of the newly created |
| 583 |
table when this is called. So we know that it has not been added |
| 584 |
for background stats gathering. */ |
| 585 |
ut_a(!table->stat_initialized); |
| 586 |
#endif /* UNIV_DEBUG || UNIV_DDL_DEBUG */ |
| 587 |
|
| 588 |
ddl::drop_table(trx, table); |
| 589 |
} |
| 590 |
|
| 591 |
/** Check if the table has a given (non_virtual) column. |
| 592 |
@param[in] table table object |
| 593 |
@param[in] col_name column name |
| 594 |
@param[in] col_nr column number guessed, 0 as default |
| 595 |
@return column number if the table has the specified column, |
| 596 |
otherwise table->n_def */ |
| 597 |
ulint dict_table_has_column(const dict_table_t *table, const char *col_name, |
| 598 |
ulint col_nr) { |
| 599 |
ulint col_max = table->n_def; |
| 600 |
|
| 601 |
ut_ad(table); |
| 602 |
ut_ad(col_name); |
| 603 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 604 |
|
| 605 |
if (col_nr < col_max && |
| 606 |
innobase_strcasecmp(col_name, table->get_col_name(col_nr)) == 0) { |
| 607 |
return (col_nr); |
| 608 |
} |
| 609 |
|
| 610 |
/** The order of column may changed, check it with other columns */ |
| 611 |
for (ulint i = 0; i < col_max; i++) { |
| 612 |
if (i != col_nr && |
| 613 |
innobase_strcasecmp(col_name, table->get_col_name(i)) == 0) { |
| 614 |
return (i); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
return (col_max); |
| 619 |
} |
| 620 |
|
| 621 |
/** Returns a virtual column's name. |
| 622 |
@param[in] table target table |
| 623 |
@param[in] col_nr virtual column number (nth virtual column) |
| 624 |
@return column name or NULL if column number out of range. */ |
| 625 |
const char *dict_table_get_v_col_name(const dict_table_t *table, ulint col_nr) { |
| 626 |
const char *s; |
| 627 |
|
| 628 |
ut_ad(table); |
| 629 |
ut_ad(col_nr < table->n_v_def); |
| 630 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 631 |
|
| 632 |
if (col_nr >= table->n_v_def) { |
| 633 |
return (nullptr); |
| 634 |
} |
| 635 |
|
| 636 |
s = table->v_col_names; |
| 637 |
|
| 638 |
if (s != nullptr) { |
| 639 |
for (ulint i = 0; i < col_nr; i++) { |
| 640 |
s += strlen(s) + 1; |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
return (s); |
| 645 |
} |
| 646 |
|
| 647 |
/** Search virtual column's position in InnoDB according to its position |
| 648 |
in original table's position |
| 649 |
@param[in] table target table |
| 650 |
@param[in] col_nr column number (nth column in the MySQL table) |
| 651 |
@return virtual column's position in InnoDB, ULINT_UNDEFINED if not find */ |
| 652 |
static ulint dict_table_get_v_col_pos_for_mysql(const dict_table_t *table, |
| 653 |
ulint col_nr) { |
| 654 |
ulint i; |
| 655 |
|
| 656 |
ut_ad(table); |
| 657 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 658 |
|
| 659 |
for (i = 0; i < table->n_v_def; i++) { |
| 660 |
if (col_nr == dict_get_v_col_mysql_pos(table->v_cols[i].m_col.ind)) { |
| 661 |
break; |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
if (i == table->n_v_def) { |
| 666 |
return (ULINT_UNDEFINED); |
| 667 |
} |
| 668 |
|
| 669 |
ut_ad(col_nr < static_cast<ulint>(table->n_t_def)); |
| 670 |
|
| 671 |
return (i); |
| 672 |
} |
| 673 |
|
| 674 |
/** Returns a virtual column's name according to its original |
| 675 |
MySQL table position. |
| 676 |
@param[in] table target table |
| 677 |
@param[in] col_nr column number (nth column in the table) |
| 678 |
@return column name. */ |
| 679 |
const char *dict_table_get_v_col_name_mysql(const dict_table_t *table, |
| 680 |
ulint col_nr) { |
| 681 |
ulint i = dict_table_get_v_col_pos_for_mysql(table, col_nr); |
| 682 |
|
| 683 |
if (i == ULINT_UNDEFINED) { |
| 684 |
return (nullptr); |
| 685 |
} |
| 686 |
|
| 687 |
return (dict_table_get_v_col_name(table, i)); |
| 688 |
} |
| 689 |
|
| 690 |
/** Get nth virtual column according to its original MySQL table position |
| 691 |
@param[in] table target table |
| 692 |
@param[in] col_nr column number in MySQL Table definition |
| 693 |
@return dict_v_col_t ptr */ |
| 694 |
dict_v_col_t *dict_table_get_nth_v_col_mysql(const dict_table_t *table, |
| 695 |
ulint col_nr) { |
| 696 |
ulint i = dict_table_get_v_col_pos_for_mysql(table, col_nr); |
| 697 |
|
| 698 |
if (i == ULINT_UNDEFINED) { |
| 699 |
return (nullptr); |
| 700 |
} |
| 701 |
|
| 702 |
return (dict_table_get_nth_v_col(table, i)); |
| 703 |
} |
| 704 |
|
| 705 |
/** Allocate and init the autoinc latch of a given table. |
| 706 |
This function must not be called concurrently on the same table object. |
| 707 |
@param[in,out] table_void table whose autoinc latch to create */ |
| 708 |
static void dict_table_autoinc_alloc(void *table_void) { |
| 709 |
dict_table_t *table = static_cast<dict_table_t *>(table_void); |
| 710 |
|
| 711 |
table->autoinc_mutex = ut::new_withkey<ib_mutex_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 712 |
ut_a(table->autoinc_mutex != nullptr); |
| 713 |
mutex_create(LATCH_ID_AUTOINC, table->autoinc_mutex); |
| 714 |
|
| 715 |
table->autoinc_persisted_mutex = |
| 716 |
ut::new_withkey<ib_mutex_t>(UT_NEW_THIS_FILE_PSI_KEY); |
| 717 |
ut_a(table->autoinc_persisted_mutex != nullptr); |
| 718 |
mutex_create(LATCH_ID_PERSIST_AUTOINC, table->autoinc_persisted_mutex); |
| 719 |
} |
| 720 |
|
| 721 |
/** Allocate and init the zip_pad_mutex of a given index. |
| 722 |
This function must not be called concurrently on the same index object. |
| 723 |
@param[in,out] index_void index whose zip_pad_mutex to create */ |
| 724 |
static void dict_index_zip_pad_alloc(void *index_void) { |
| 725 |
dict_index_t *index = static_cast<dict_index_t *>(index_void); |
| 726 |
index->zip_pad.mutex = ut::new_withkey<SysMutex>(UT_NEW_THIS_FILE_PSI_KEY); |
| 727 |
ut_a(index->zip_pad.mutex != nullptr); |
| 728 |
mutex_create(LATCH_ID_ZIP_PAD_MUTEX, index->zip_pad.mutex); |
| 729 |
} |
| 730 |
|
| 731 |
/** Acquire the autoinc lock. */ |
| 732 |
void dict_table_autoinc_lock(dict_table_t *table) /*!< in/out: table */ |
| 733 |
{ |
| 734 |
os_once::do_or_wait_for_done(&table->autoinc_mutex_created, |
| 735 |
dict_table_autoinc_alloc, table); |
| 736 |
|
| 737 |
mutex_enter(table->autoinc_mutex); |
| 738 |
} |
| 739 |
|
| 740 |
/** Acquire the zip_pad_mutex latch. |
| 741 |
@param[in,out] index the index whose zip_pad_mutex to acquire.*/ |
| 742 |
static void dict_index_zip_pad_lock(dict_index_t *index) { |
| 743 |
os_once::do_or_wait_for_done(&index->zip_pad.mutex_created, |
| 744 |
dict_index_zip_pad_alloc, index); |
| 745 |
|
| 746 |
mutex_enter(index->zip_pad.mutex); |
| 747 |
} |
| 748 |
|
| 749 |
/** Unconditionally set the autoinc counter. |
| 750 |
@param[in,out] table Table |
| 751 |
@param[in] value Next value to assign to a row */ |
| 752 |
void dict_table_autoinc_initialize(dict_table_t *table, uint64_t value) { |
| 753 |
ut_ad(dict_table_autoinc_own(table)); |
| 754 |
|
| 755 |
table->autoinc = value; |
| 756 |
} |
| 757 |
|
| 758 |
bool dict_table_autoinc_log(dict_table_t *table, uint64_t value, mtr_t *mtr) { |
| 759 |
bool log = false; |
| 760 |
|
| 761 |
mutex_enter(table->autoinc_persisted_mutex); |
| 762 |
|
| 763 |
if (table->autoinc_persisted < value) { |
| 764 |
dict_table_autoinc_persisted_update(table, value); |
| 765 |
|
| 766 |
/* The only concern here is some concurrent thread may |
| 767 |
change the dirty_status to METADATA_BUFFERED. And the |
| 768 |
only function is dict_table_persist_to_dd_table_buffer_low(), |
| 769 |
which could be called by checkpoint and will first set the |
| 770 |
dirty_status to METADATA_BUFFERED, and then write back |
| 771 |
the latest changes to DDTableBuffer, all of which are under |
| 772 |
protection of dict_persist->mutex. |
| 773 |
|
| 774 |
If that function sets the dirty_status to METADATA_BUFFERED |
| 775 |
first, below checking will force current thread to wait on |
| 776 |
dict_persist->mutex. Above update to AUTOINC would be either |
| 777 |
written back to DDTableBuffer or not. But the redo logs for |
| 778 |
current change won't be counted into current checkpoint. |
| 779 |
See how log_sys->dict_max_allowed_checkpoint_lsn is set. |
| 780 |
So even a crash after below redo log flushed, no change lost. |
| 781 |
|
| 782 |
If that function sets the dirty_status after below checking, |
| 783 |
which means current change would be written back to |
| 784 |
DDTableBuffer. It's also safe. */ |
| 785 |
if (table->dirty_status.load() == METADATA_DIRTY) { |
| 786 |
ut_ad(table->in_dirty_dict_tables_list); |
| 787 |
} else { |
| 788 |
dict_table_mark_dirty(table); |
| 789 |
} |
| 790 |
|
| 791 |
log = true; |
| 792 |
} |
| 793 |
|
| 794 |
mutex_exit(table->autoinc_persisted_mutex); |
| 795 |
|
| 796 |
if (log) { |
| 797 |
PersistentTableMetadata metadata(table->id, table->version); |
| 798 |
metadata.set_autoinc(value); |
| 799 |
|
| 800 |
Persister *persister = dict_persist->persisters->get(PM_TABLE_AUTO_INC); |
| 801 |
persister->write_log(table->id, metadata, mtr); |
| 802 |
/* No need to flush due to performance reason */ |
| 803 |
} |
| 804 |
|
| 805 |
/* Check and return if auto increment is to be persisted. */ |
| 806 |
return (log && dict_persist->check_persist_immediately()); |
| 807 |
} |
| 808 |
|
| 809 |
/** Get all the FTS indexes on a table. |
| 810 |
@param[in] table table |
| 811 |
@param[out] indexes all FTS indexes on this table |
| 812 |
@return number of FTS indexes */ |
| 813 |
ulint dict_table_get_all_fts_indexes(dict_table_t *table, |
| 814 |
ib_vector_t *indexes) { |
| 815 |
dict_index_t *index; |
| 816 |
|
| 817 |
ut_a(ib_vector_size(indexes) == 0); |
| 818 |
|
| 819 |
for (index = table->first_index(); index; index = index->next()) { |
| 820 |
if (index->type == DICT_FTS) { |
| 821 |
ib_vector_push(indexes, &index); |
| 822 |
} |
| 823 |
} |
| 824 |
|
| 825 |
return (ib_vector_size(indexes)); |
| 826 |
} |
| 827 |
|
| 828 |
/** Reads the next autoinc value (== autoinc counter value), 0 if not yet |
| 829 |
initialized. |
| 830 |
@return value for a new row, or 0 */ |
| 831 |
uint64_t dict_table_autoinc_read(const dict_table_t *table) /*!< in: table */ |
| 832 |
{ |
| 833 |
ut_ad(dict_table_autoinc_own(table)); |
| 834 |
|
| 835 |
return (table->autoinc); |
| 836 |
} |
| 837 |
|
| 838 |
/** Updates the autoinc counter if the value supplied is greater than the |
| 839 |
current value. |
| 840 |
@param[in,out] table Table |
| 841 |
@param[in] value Value which was assigned to a row */ |
| 842 |
void dict_table_autoinc_update_if_greater(dict_table_t *table, uint64_t value) { |
| 843 |
ut_ad(dict_table_autoinc_own(table)); |
| 844 |
|
| 845 |
if (value > table->autoinc) { |
| 846 |
table->autoinc = value; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
/** Release the autoinc lock. */ |
| 851 |
void dict_table_autoinc_unlock(dict_table_t *table) /*!< in/out: table */ |
| 852 |
{ |
| 853 |
mutex_exit(table->autoinc_mutex); |
| 854 |
} |
| 855 |
|
| 856 |
bool dict_index_contains_col_or_prefix(const dict_index_t *index, ulint n, |
| 857 |
bool is_virtual) { |
| 858 |
const dict_field_t *field; |
| 859 |
const dict_col_t *col; |
| 860 |
ulint pos; |
| 861 |
ulint n_fields; |
| 862 |
|
| 863 |
ut_ad(index); |
| 864 |
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N); |
| 865 |
|
| 866 |
if (index->is_clustered()) { |
| 867 |
return true; |
| 868 |
} |
| 869 |
|
| 870 |
if (is_virtual) { |
| 871 |
col = &dict_table_get_nth_v_col(index->table, n)->m_col; |
| 872 |
} else { |
| 873 |
col = index->table->get_col(n); |
| 874 |
} |
| 875 |
|
| 876 |
n_fields = dict_index_get_n_fields(index); |
| 877 |
|
| 878 |
for (pos = 0; pos < n_fields; pos++) { |
| 879 |
field = index->get_field(pos); |
| 880 |
|
| 881 |
if (col == field->col) { |
| 882 |
return true; |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
return false; |
| 887 |
} |
| 888 |
|
| 889 |
/** Looks for a matching field in an index. The column has to be the same. The |
| 890 |
column in index must be complete, or must contain a prefix longer than the |
| 891 |
column in index2. That is, we must be able to construct the prefix in index2 |
| 892 |
from the prefix in index. |
| 893 |
@return position in internal representation of the index; |
| 894 |
ULINT_UNDEFINED if not contained */ |
| 895 |
ulint dict_index_get_nth_field_pos( |
| 896 |
const dict_index_t *index, /*!< in: index from which to search */ |
| 897 |
const dict_index_t *index2, /*!< in: index */ |
| 898 |
ulint n) /*!< in: field number in index2 */ |
| 899 |
{ |
| 900 |
const dict_field_t *field; |
| 901 |
const dict_field_t *field2; |
| 902 |
ulint n_fields; |
| 903 |
ulint pos; |
| 904 |
|
| 905 |
ut_ad(index); |
| 906 |
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N); |
| 907 |
|
| 908 |
field2 = index2->get_field(n); |
| 909 |
|
| 910 |
n_fields = dict_index_get_n_fields(index); |
| 911 |
|
| 912 |
/* Are we looking for a MBR (Minimum Bound Box) field of |
| 913 |
a spatial index */ |
| 914 |
bool is_mbr_fld = (n == 0 && dict_index_is_spatial(index2)); |
| 915 |
|
| 916 |
for (pos = 0; pos < n_fields; pos++) { |
| 917 |
field = index->get_field(pos); |
| 918 |
|
| 919 |
/* The first field of a spatial index is a transformed |
| 920 |
MBR (Minimum Bound Box) field made out of original column, |
| 921 |
so its field->col still points to original cluster index |
| 922 |
col, but the actual content is different. So we cannot |
| 923 |
consider them equal if neither of them is MBR field */ |
| 924 |
if (pos == 0 && dict_index_is_spatial(index) && !is_mbr_fld) { |
| 925 |
continue; |
| 926 |
} |
| 927 |
|
| 928 |
if (field->col == field2->col && |
| 929 |
(field->prefix_len == 0 || (field->prefix_len >= field2->prefix_len && |
| 930 |
field2->prefix_len != 0))) { |
| 931 |
return (pos); |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
return (ULINT_UNDEFINED); |
| 936 |
} |
| 937 |
|
| 938 |
/** Looks for non-virtual column n position in the clustered index. |
| 939 |
@return position in internal representation of the clustered index */ |
| 940 |
ulint dict_table_get_nth_col_pos(const dict_table_t *table, /*!< in: table */ |
| 941 |
ulint n) /*!< in: column number */ |
| 942 |
{ |
| 943 |
return (table->first_index()->get_col_pos(n)); |
| 944 |
} |
| 945 |
|
| 946 |
/** Get the innodb column position for a non-virtual column according to |
| 947 |
its original MySQL table position n |
| 948 |
@param[in] table table |
| 949 |
@param[in] n MySQL column position |
| 950 |
@return column position in InnoDB */ |
| 951 |
ulint dict_table_mysql_pos_to_innodb(const dict_table_t *table, ulint n) { |
| 952 |
ut_ad(n < table->n_t_cols); |
| 953 |
|
| 954 |
if (table->n_v_def == 0) { |
| 955 |
/* No virtual columns, the MySQL position is the same |
| 956 |
as InnoDB position */ |
| 957 |
return (n); |
| 958 |
} |
| 959 |
|
| 960 |
/* Find out how many virtual columns are stored in front of 'n' */ |
| 961 |
ulint v_before = 0; |
| 962 |
for (ulint i = 0; i < table->n_v_def; ++i) { |
| 963 |
if (table->v_cols[i].m_col.ind > n) { |
| 964 |
break; |
| 965 |
} |
| 966 |
|
| 967 |
++v_before; |
| 968 |
} |
| 969 |
|
| 970 |
ut_ad(n >= v_before); |
| 971 |
|
| 972 |
return (n - v_before); |
| 973 |
} |
| 974 |
|
| 975 |
/** Checks if a column is in the ordering columns of the clustered index of a |
| 976 |
table. Column prefixes are treated like whole columns. |
| 977 |
@return true if the column, or its prefix, is in the clustered key */ |
| 978 |
bool dict_table_col_in_clustered_key( |
| 979 |
const dict_table_t *table, /*!< in: table */ |
| 980 |
ulint n) /*!< in: column number */ |
| 981 |
{ |
| 982 |
const dict_index_t *index; |
| 983 |
const dict_field_t *field; |
| 984 |
const dict_col_t *col; |
| 985 |
ulint pos; |
| 986 |
ulint n_fields; |
| 987 |
|
| 988 |
ut_ad(table); |
| 989 |
|
| 990 |
col = table->get_col(n); |
| 991 |
|
| 992 |
index = table->first_index(); |
| 993 |
|
| 994 |
n_fields = dict_index_get_n_unique(index); |
| 995 |
|
| 996 |
for (pos = 0; pos < n_fields; pos++) { |
| 997 |
field = index->get_field(pos); |
| 998 |
|
| 999 |
if (col == field->col) { |
| 1000 |
return true; |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
return false; |
| 1005 |
} |
| 1006 |
#endif /* !UNIV_HOTBACKUP */ |
| 1007 |
|
| 1008 |
/** Inits the data dictionary module. */ |
| 1009 |
void dict_init(void) { |
| 1010 |
dict_operation_lock = static_cast<rw_lock_t *>(ut::zalloc_withkey( |
| 1011 |
UT_NEW_THIS_FILE_PSI_KEY, sizeof(*dict_operation_lock))); |
| 1012 |
|
| 1013 |
dict_sys = static_cast<dict_sys_t *>( |
| 1014 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*dict_sys))); |
| 1015 |
|
| 1016 |
UT_LIST_INIT(dict_sys->table_LRU); |
| 1017 |
UT_LIST_INIT(dict_sys->table_non_LRU); |
| 1018 |
|
| 1019 |
mutex_create(LATCH_ID_DICT_SYS, &dict_sys->mutex); |
| 1020 |
|
| 1021 |
dict_sys->table_hash = ut::new_<hash_table_t>( |
| 1022 |
buf_pool_get_curr_size() / (DICT_POOL_PER_TABLE_HASH * UNIV_WORD_SIZE)); |
| 1023 |
|
| 1024 |
dict_sys->table_id_hash = ut::new_<hash_table_t>( |
| 1025 |
buf_pool_get_curr_size() / (DICT_POOL_PER_TABLE_HASH * UNIV_WORD_SIZE)); |
| 1026 |
|
| 1027 |
rw_lock_create(dict_operation_lock_key, dict_operation_lock, |
| 1028 |
LATCH_ID_DICT_OPERATION); |
| 1029 |
|
| 1030 |
#ifndef UNIV_HOTBACKUP |
| 1031 |
if (!srv_read_only_mode) { |
| 1032 |
dict_foreign_err_file = os_file_create_tmpfile(); |
| 1033 |
ut_a(dict_foreign_err_file); |
| 1034 |
} |
| 1035 |
#endif /* !UNIV_HOTBACKUP */ |
| 1036 |
|
| 1037 |
mutex_create(LATCH_ID_DICT_FOREIGN_ERR, &dict_foreign_err_mutex); |
| 1038 |
} |
| 1039 |
|
| 1040 |
#ifndef UNIV_HOTBACKUP |
| 1041 |
/** Move to the most recently used segment of the LRU list. */ |
| 1042 |
void dict_move_to_mru(dict_table_t *table) /*!< in: table to move to MRU */ |
| 1043 |
{ |
| 1044 |
ut_ad(dict_sys_mutex_own()); |
| 1045 |
ut_ad(dict_lru_validate()); |
| 1046 |
ut_ad(dict_lru_find_table(table)); |
| 1047 |
|
| 1048 |
ut_a(table->can_be_evicted); |
| 1049 |
|
| 1050 |
UT_LIST_REMOVE(dict_sys->table_LRU, table); |
| 1051 |
|
| 1052 |
UT_LIST_ADD_FIRST(dict_sys->table_LRU, table); |
| 1053 |
|
| 1054 |
ut_ad(dict_lru_validate()); |
| 1055 |
} |
| 1056 |
|
| 1057 |
/** Returns a table object and increment its open handle count. |
| 1058 |
NOTE! This is a high-level function to be used mainly from outside the |
| 1059 |
'dict' module. Inside this directory dict_table_get_low |
| 1060 |
is usually the appropriate function. |
| 1061 |
@return table, NULL if does not exist */ |
| 1062 |
dict_table_t *dict_table_open_on_name( |
| 1063 |
const char *table_name, /*!< in: table name */ |
| 1064 |
bool dict_locked, /*!< in: true=data dictionary locked */ |
| 1065 |
bool try_drop, /*!< in: true=try to drop any orphan |
| 1066 |
indexes after an aborted online |
| 1067 |
index creation */ |
| 1068 |
dict_err_ignore_t ignore_err) /*!< in: error to be ignored when |
| 1069 |
loading a table definition */ |
| 1070 |
{ |
| 1071 |
dict_table_t *table; |
| 1072 |
DBUG_TRACE; |
| 1073 |
DBUG_PRINT("dict_table_open_on_name", ("table: '%s'", table_name)); |
| 1074 |
|
| 1075 |
if (!dict_locked) { |
| 1076 |
dict_sys_mutex_enter(); |
| 1077 |
} |
| 1078 |
|
| 1079 |
ut_ad(table_name); |
| 1080 |
ut_ad(dict_sys_mutex_own()); |
| 1081 |
|
| 1082 |
std::string table_str(table_name); |
| 1083 |
/* Check and convert 5.7 table name. We always keep 8.0 format name in cache |
| 1084 |
during upgrade. */ |
| 1085 |
if (dict_name::is_partition(table_name)) { |
| 1086 |
dict_name::rebuild(table_str); |
| 1087 |
} |
| 1088 |
table = dict_table_check_if_in_cache_low(table_str.c_str()); |
| 1089 |
|
| 1090 |
if (table == nullptr) { |
| 1091 |
table = dict_load_table(table_name, true, ignore_err); |
| 1092 |
} |
| 1093 |
|
| 1094 |
ut_ad(!table || table->cached); |
| 1095 |
|
| 1096 |
if (table != nullptr) { |
| 1097 |
if (ignore_err == DICT_ERR_IGNORE_NONE && table->is_corrupted()) { |
| 1098 |
/* Make life easy for drop table. */ |
| 1099 |
dict_table_prevent_eviction(table); |
| 1100 |
|
| 1101 |
if (!dict_locked) { |
| 1102 |
dict_sys_mutex_exit(); |
| 1103 |
} |
| 1104 |
|
| 1105 |
ib::info(ER_IB_MSG_175) << "Table " << table->name |
| 1106 |
<< " is corrupted. Please drop the table" |
| 1107 |
" and recreate it"; |
| 1108 |
return nullptr; |
| 1109 |
} |
| 1110 |
|
| 1111 |
if (table->can_be_evicted) { |
| 1112 |
dict_move_to_mru(table); |
| 1113 |
} |
| 1114 |
|
| 1115 |
table->acquire(); |
| 1116 |
} |
| 1117 |
|
| 1118 |
ut_ad(dict_lru_validate()); |
| 1119 |
|
| 1120 |
if (!dict_locked) { |
| 1121 |
dict_table_try_drop_aborted_and_mutex_exit(table, try_drop); |
| 1122 |
} |
| 1123 |
|
| 1124 |
return table; |
| 1125 |
} |
| 1126 |
#endif /* !UNIV_HOTBACKUP */ |
| 1127 |
|
| 1128 |
/** Adds system columns to a table object. |
| 1129 |
@param[in,out] table Table |
| 1130 |
@param[in] heap Temporary heap */ |
| 1131 |
void dict_table_add_system_columns(dict_table_t *table, mem_heap_t *heap) { |
| 1132 |
ut_ad(table); |
| 1133 |
/* INSTANT DROP columns are added after system columns, so no need to |
| 1134 |
consider drop columns count in following assert. */ |
| 1135 |
ut_ad(table->n_def == (table->n_cols - table->get_n_sys_cols())); |
| 1136 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 1137 |
ut_ad(!table->cached); |
| 1138 |
|
| 1139 |
/* NOTE: the system columns MUST be added in the following order |
| 1140 |
(so that they can be indexed by the numerical value of DATA_ROW_ID, |
| 1141 |
etc.) and as the last columns of the table memory object. |
| 1142 |
The clustered index will not always physically contain all system |
| 1143 |
columns. |
| 1144 |
Intrinsic table don't need DB_ROLL_PTR as UNDO logging is turned off |
| 1145 |
for these tables. */ |
| 1146 |
|
| 1147 |
const uint32_t phy_pos = UINT32_UNDEFINED; |
| 1148 |
const uint8_t v_added = 0; |
| 1149 |
const uint8_t v_dropped = 0; |
| 1150 |
|
| 1151 |
dict_mem_table_add_col(table, heap, "DB_ROW_ID", DATA_SYS, |
| 1152 |
DATA_ROW_ID | DATA_NOT_NULL, DATA_ROW_ID_LEN, false, |
| 1153 |
phy_pos, v_added, v_dropped); |
| 1154 |
|
| 1155 |
dict_mem_table_add_col(table, heap, "DB_TRX_ID", DATA_SYS, |
| 1156 |
DATA_TRX_ID | DATA_NOT_NULL, DATA_TRX_ID_LEN, false, |
| 1157 |
phy_pos, v_added, v_dropped); |
| 1158 |
|
| 1159 |
if (!table->is_intrinsic()) { |
| 1160 |
dict_mem_table_add_col(table, heap, "DB_ROLL_PTR", DATA_SYS, |
| 1161 |
DATA_ROLL_PTR | DATA_NOT_NULL, DATA_ROLL_PTR_LEN, |
| 1162 |
false, phy_pos, v_added, v_dropped); |
| 1163 |
|
| 1164 |
/* This check reminds that if a new system column is added to |
| 1165 |
the program, it should be dealt with here */ |
| 1166 |
} |
| 1167 |
} |
| 1168 |
|
| 1169 |
#ifndef UNIV_HOTBACKUP |
| 1170 |
/** Mark if table has big rows. |
| 1171 |
@param[in,out] table table handler */ |
| 1172 |
void dict_table_set_big_rows(dict_table_t *table) { |
| 1173 |
ulint row_len = 0; |
| 1174 |
for (ulint i = 0; i < table->n_def; i++) { |
| 1175 |
ulint col_len = table->get_col(i)->get_max_size(); |
| 1176 |
|
| 1177 |
row_len += col_len; |
| 1178 |
|
| 1179 |
/* If we have a single unbounded field, or several gigantic |
| 1180 |
fields, mark the maximum row size as BIG_ROW_SIZE. */ |
| 1181 |
if (row_len >= BIG_ROW_SIZE || col_len >= BIG_ROW_SIZE) { |
| 1182 |
row_len = BIG_ROW_SIZE; |
| 1183 |
|
| 1184 |
break; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
table->big_rows = (row_len >= BIG_ROW_SIZE) ? true : false; |
| 1189 |
} |
| 1190 |
|
| 1191 |
void dict_table_add_to_cache(dict_table_t *table, bool can_be_evicted) { |
| 1192 |
ut_ad(dict_lru_validate()); |
| 1193 |
ut_ad(dict_sys_mutex_own()); |
| 1194 |
|
| 1195 |
table->cached = true; |
| 1196 |
|
| 1197 |
const auto name_hash_value = ut::hash_string(table->name.m_name); |
| 1198 |
const auto index_id_hash_value = ut::hash_uint64(table->id); |
| 1199 |
|
| 1200 |
dict_table_set_big_rows(table); |
| 1201 |
|
| 1202 |
/* Look for a table with the same name: error if such exists */ |
| 1203 |
{ |
| 1204 |
dict_table_t *table2; |
| 1205 |
HASH_SEARCH(name_hash, dict_sys->table_hash, name_hash_value, |
| 1206 |
dict_table_t *, table2, ut_ad(table2->cached), |
| 1207 |
!strcmp(table2->name.m_name, table->name.m_name)); |
| 1208 |
ut_a(table2 == nullptr); |
| 1209 |
|
| 1210 |
#ifdef UNIV_DEBUG |
| 1211 |
/* Look for the same table pointer with a different name */ |
| 1212 |
HASH_SEARCH_ALL(name_hash, dict_sys->table_hash, dict_table_t *, table2, |
| 1213 |
ut_ad(table2->cached), table2 == table); |
| 1214 |
ut_ad(table2 == nullptr); |
| 1215 |
#endif /* UNIV_DEBUG */ |
| 1216 |
} |
| 1217 |
|
| 1218 |
/* Look for a table with the same id: error if such exists */ |
| 1219 |
{ |
| 1220 |
dict_table_t *table2; |
| 1221 |
HASH_SEARCH(id_hash, dict_sys->table_id_hash, index_id_hash_value, |
| 1222 |
dict_table_t *, table2, ut_ad(table2->cached), |
| 1223 |
table2->id == table->id); |
| 1224 |
ut_a(table2 == nullptr); |
| 1225 |
|
| 1226 |
#ifdef UNIV_DEBUG |
| 1227 |
/* Look for the same table pointer with a different id */ |
| 1228 |
HASH_SEARCH_ALL(id_hash, dict_sys->table_id_hash, dict_table_t *, table2, |
| 1229 |
ut_ad(table2->cached), table2 == table); |
| 1230 |
ut_ad(table2 == nullptr); |
| 1231 |
#endif /* UNIV_DEBUG */ |
| 1232 |
} |
| 1233 |
|
| 1234 |
/* Add table to hash table of tables */ |
| 1235 |
HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, name_hash_value, |
| 1236 |
table); |
| 1237 |
|
| 1238 |
/* Add table to hash table of tables based on table id */ |
| 1239 |
HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash, |
| 1240 |
index_id_hash_value, table); |
| 1241 |
|
| 1242 |
table->can_be_evicted = can_be_evicted; |
| 1243 |
|
| 1244 |
if (table->can_be_evicted) { |
| 1245 |
UT_LIST_ADD_FIRST(dict_sys->table_LRU, table); |
| 1246 |
} else { |
| 1247 |
UT_LIST_ADD_FIRST(dict_sys->table_non_LRU, table); |
| 1248 |
} |
| 1249 |
|
| 1250 |
ut_ad(dict_lru_validate()); |
| 1251 |
|
| 1252 |
table->dirty_status.store(METADATA_CLEAN); |
| 1253 |
|
| 1254 |
dict_sys->size += |
| 1255 |
mem_heap_get_size(table->heap) + strlen(table->name.m_name) + 1; |
| 1256 |
DBUG_EXECUTE_IF( |
| 1257 |
"dd_upgrade", if (srv_is_upgrade_mode && srv_upgrade_old_undo_found) { |
| 1258 |
ib::info(ER_IB_MSG_176) << "Adding table to cache: " << table->name; |
| 1259 |
}); |
| 1260 |
} |
| 1261 |
|
| 1262 |
/** Test whether a table can be evicted from the LRU cache. |
| 1263 |
@return true if table can be evicted. */ |
| 1264 |
static bool dict_table_can_be_evicted( |
| 1265 |
dict_table_t *table) /*!< in: table to test */ |
| 1266 |
{ |
| 1267 |
ut_ad(dict_sys_mutex_own()); |
| 1268 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 1269 |
|
| 1270 |
ut_a(table->can_be_evicted); |
| 1271 |
ut_a(table->foreign_set.empty()); |
| 1272 |
ut_a(table->referenced_set.empty()); |
| 1273 |
|
| 1274 |
if (table->get_ref_count() == 0) { |
| 1275 |
const dict_index_t *index; |
| 1276 |
|
| 1277 |
/* The transaction commit and rollback are called from |
| 1278 |
outside the handler interface. This means that there is |
| 1279 |
a window where the table->n_ref_count can be zero but |
| 1280 |
the table instance is in "use". */ |
| 1281 |
|
| 1282 |
if (lock_table_has_locks(table)) { |
| 1283 |
return false; |
| 1284 |
} |
| 1285 |
|
| 1286 |
for (index = table->first_index(); index != nullptr; |
| 1287 |
index = index->next()) { |
| 1288 |
/* We are not allowed to free the in-memory index |
| 1289 |
struct dict_index_t until all entries in the adaptive |
| 1290 |
hash index that point to any of the page belonging to |
| 1291 |
his b-tree index are dropped. This is so because |
| 1292 |
dropping of these entries require access to |
| 1293 |
dict_index_t struct. To avoid such scenario we keep |
| 1294 |
a count of number of such pages in the search_info and |
| 1295 |
only free the dict_index_t struct when this count |
| 1296 |
drops to zero. |
| 1297 |
|
| 1298 |
See also: dict_index_remove_from_cache_low() */ |
| 1299 |
|
| 1300 |
if (index->search_info->ref_count > 0) { |
| 1301 |
return false; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
|
| 1305 |
return true; |
| 1306 |
} |
| 1307 |
|
| 1308 |
return false; |
| 1309 |
} |
| 1310 |
|
| 1311 |
/** Make room in the table cache by evicting an unused table. The unused table |
| 1312 |
should not be part of FK relationship and currently not used in any user |
| 1313 |
transaction. There is no guarantee that it will remove a table. |
| 1314 |
@return number of tables evicted. If the number of tables in the dict_LRU |
| 1315 |
is less than max_tables it will not do anything. */ |
| 1316 |
ulint dict_make_room_in_cache( |
| 1317 |
ulint max_tables, /*!< in: max tables allowed in cache */ |
| 1318 |
ulint pct_check) /*!< in: max percent to check */ |
| 1319 |
{ |
| 1320 |
ulint i; |
| 1321 |
ulint len; |
| 1322 |
dict_table_t *table; |
| 1323 |
ulint check_up_to; |
| 1324 |
ulint n_evicted = 0; |
| 1325 |
|
| 1326 |
ut_a(pct_check > 0); |
| 1327 |
ut_a(pct_check <= 100); |
| 1328 |
ut_ad(dict_sys_mutex_own()); |
| 1329 |
ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X)); |
| 1330 |
ut_ad(dict_lru_validate()); |
| 1331 |
|
| 1332 |
i = len = UT_LIST_GET_LEN(dict_sys->table_LRU); |
| 1333 |
|
| 1334 |
if (len < max_tables) { |
| 1335 |
return (0); |
| 1336 |
} |
| 1337 |
|
| 1338 |
check_up_to = len - ((len * pct_check) / 100); |
| 1339 |
|
| 1340 |
/* Check for overflow */ |
| 1341 |
ut_a(i == 0 || check_up_to <= i); |
| 1342 |
|
| 1343 |
/* Find a suitable candidate to evict from the cache. Don't scan the |
| 1344 |
entire LRU list. Only scan pct_check list entries. */ |
| 1345 |
|
| 1346 |
for (table = UT_LIST_GET_LAST(dict_sys->table_LRU); |
| 1347 |
table != nullptr && i > check_up_to && (len - n_evicted) > max_tables; |
| 1348 |
--i) { |
| 1349 |
dict_table_t *prev_table; |
| 1350 |
|
| 1351 |
prev_table = UT_LIST_GET_PREV(table_LRU, table); |
| 1352 |
|
| 1353 |
table->lock(); |
| 1354 |
|
| 1355 |
if (dict_table_can_be_evicted(table)) { |
| 1356 |
table->unlock(); |
| 1357 |
DBUG_EXECUTE_IF("crash_if_fts_table_is_evicted", { |
| 1358 |
if (table->fts && dict_table_has_fts_index(table)) { |
| 1359 |
ut_d(ut_error); |
| 1360 |
} |
| 1361 |
};); |
| 1362 |
dict_table_remove_from_cache_low(table, true); |
| 1363 |
|
| 1364 |
++n_evicted; |
| 1365 |
} else { |
| 1366 |
table->unlock(); |
| 1367 |
} |
| 1368 |
|
| 1369 |
table = prev_table; |
| 1370 |
} |
| 1371 |
|
| 1372 |
return (n_evicted); |
| 1373 |
} |
| 1374 |
|
| 1375 |
/** Move a table to the non-LRU list from the LRU list. */ |
| 1376 |
void dict_table_move_from_lru_to_non_lru( |
| 1377 |
dict_table_t *table) /*!< in: table to move from LRU to non-LRU */ |
| 1378 |
{ |
| 1379 |
ut_ad(dict_sys_mutex_own()); |
| 1380 |
ut_ad(dict_lru_find_table(table)); |
| 1381 |
|
| 1382 |
ut_a(table->can_be_evicted); |
| 1383 |
|
| 1384 |
UT_LIST_REMOVE(dict_sys->table_LRU, table); |
| 1385 |
|
| 1386 |
UT_LIST_ADD_LAST(dict_sys->table_non_LRU, table); |
| 1387 |
|
| 1388 |
table->can_be_evicted = false; |
| 1389 |
} |
| 1390 |
#endif /* !UNIV_HOTBACKUP */ |
| 1391 |
|
| 1392 |
/** Move a table to the LRU end from the non LRU list. |
| 1393 |
@param[in] table InnoDB table object */ |
| 1394 |
void dict_table_move_from_non_lru_to_lru(dict_table_t *table) { |
| 1395 |
ut_ad(dict_sys_mutex_own()); |
| 1396 |
ut_ad(dict_non_lru_find_table(table)); |
| 1397 |
|
| 1398 |
ut_a(!table->can_be_evicted); |
| 1399 |
|
| 1400 |
UT_LIST_REMOVE(dict_sys->table_non_LRU, table); |
| 1401 |
|
| 1402 |
UT_LIST_ADD_LAST(dict_sys->table_LRU, table); |
| 1403 |
|
| 1404 |
table->can_be_evicted = true; |
| 1405 |
} |
| 1406 |
|
| 1407 |
#ifndef UNIV_HOTBACKUP |
| 1408 |
|
| 1409 |
/** Look up an index in a table. |
| 1410 |
@param[in] table table |
| 1411 |
@param[in] id index identifier |
| 1412 |
@return index |
| 1413 |
@retval NULL if not found */ |
| 1414 |
static const dict_index_t *dict_table_find_index_on_id( |
| 1415 |
const dict_table_t *table, const index_id_t &id) { |
| 1416 |
for (const dict_index_t *index = table->first_index(); index != nullptr; |
| 1417 |
index = index->next()) { |
| 1418 |
if (index->space == id.m_space_id && index->id == id.m_index_id) { |
| 1419 |
return (index); |
| 1420 |
} |
| 1421 |
} |
| 1422 |
|
| 1423 |
return (nullptr); |
| 1424 |
} |
| 1425 |
|
| 1426 |
const dict_index_t *dict_index_find(const index_id_t &id) { |
| 1427 |
ut_ad(dict_sys_mutex_own()); |
| 1428 |
|
| 1429 |
for (auto table : dict_sys->table_LRU) { |
| 1430 |
const dict_index_t *index = dict_table_find_index_on_id(table, id); |
| 1431 |
if (index != nullptr) { |
| 1432 |
return (index); |
| 1433 |
} |
| 1434 |
} |
| 1435 |
|
| 1436 |
for (auto table : dict_sys->table_non_LRU) { |
| 1437 |
const dict_index_t *index = dict_table_find_index_on_id(table, id); |
| 1438 |
if (index != nullptr) { |
| 1439 |
return (index); |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
return (nullptr); |
| 1444 |
} |
| 1445 |
|
| 1446 |
/** Function object to remove a foreign key constraint from the |
| 1447 |
referenced_set of the referenced table. The foreign key object is |
| 1448 |
also removed from the dictionary cache. The foreign key constraint |
| 1449 |
is not removed from the foreign_set of the table containing the |
| 1450 |
constraint. */ |
| 1451 |
struct dict_foreign_remove_partial { |
| 1452 |
void operator()(dict_foreign_t *foreign) { |
| 1453 |
dict_table_t *table = foreign->referenced_table; |
| 1454 |
if (table != nullptr) { |
| 1455 |
table->referenced_set.erase(foreign); |
| 1456 |
} |
| 1457 |
dict_foreign_free(foreign); |
| 1458 |
} |
| 1459 |
}; |
| 1460 |
|
| 1461 |
/** Renames a table object. |
| 1462 |
@return true if success */ |
| 1463 |
dberr_t dict_table_rename_in_cache( |
| 1464 |
dict_table_t *table, /*!< in/out: table */ |
| 1465 |
const char *new_name, /*!< in: new name */ |
| 1466 |
bool rename_also_foreigns) /*!< in: in ALTER TABLE we want |
| 1467 |
to preserve the original table name |
| 1468 |
in constraints which reference it */ |
| 1469 |
{ |
| 1470 |
dberr_t err; |
| 1471 |
dict_foreign_t *foreign; |
| 1472 |
dict_index_t *index; |
| 1473 |
char old_name[MAX_FULL_NAME_LEN + 1]; |
| 1474 |
|
| 1475 |
ut_ad(dict_sys_mutex_own()); |
| 1476 |
|
| 1477 |
/* store the old/current name to an automatic variable */ |
| 1478 |
if (strlen(table->name.m_name) + 1 <= sizeof(old_name)) { |
| 1479 |
strcpy(old_name, table->name.m_name); |
| 1480 |
} else { |
| 1481 |
ib::fatal(UT_LOCATION_HERE, ER_IB_MSG_177) |
| 1482 |
<< "Too long table name: " << table->name << ", max length is " |
| 1483 |
<< MAX_FULL_NAME_LEN; |
| 1484 |
} |
| 1485 |
|
| 1486 |
const auto hash_value = ut::hash_string(new_name); |
| 1487 |
|
| 1488 |
/* Look for a table with the same name: error if such exists */ |
| 1489 |
dict_table_t *table2; |
| 1490 |
HASH_SEARCH(name_hash, dict_sys->table_hash, hash_value, dict_table_t *, |
| 1491 |
table2, ut_ad(table2->cached), |
| 1492 |
(ut_strcmp(table2->name.m_name, new_name) == 0)); |
| 1493 |
|
| 1494 |
DBUG_EXECUTE_IF( |
| 1495 |
"dict_table_rename_in_cache_failure", |
| 1496 |
if (table2 == nullptr) { table2 = (dict_table_t *)-1; }); |
| 1497 |
|
| 1498 |
if (table2 != nullptr) { |
| 1499 |
ib::error(ER_IB_MSG_178) |
| 1500 |
<< "Cannot rename table '" << old_name << "' to '" << new_name |
| 1501 |
<< "' since the" |
| 1502 |
" dictionary cache already contains '" |
| 1503 |
<< new_name << "'."; |
| 1504 |
|
| 1505 |
return (DB_ERROR); |
| 1506 |
} |
| 1507 |
|
| 1508 |
/* If the table is stored in a single-table tablespace, |
| 1509 |
rename the tablespace file. */ |
| 1510 |
|
| 1511 |
if (dict_table_is_discarded(table)) { |
| 1512 |
char *filepath; |
| 1513 |
|
| 1514 |
ut_ad(dict_table_is_file_per_table(table)); |
| 1515 |
ut_ad(!table->is_temporary()); |
| 1516 |
|
| 1517 |
/* Make sure the data_dir_path is set. */ |
| 1518 |
dd_get_and_save_data_dir_path<dd::Table>(table, nullptr, true); |
| 1519 |
|
| 1520 |
std::string path = dict_table_get_datadir(table); |
| 1521 |
|
| 1522 |
filepath = Fil_path::make(path, table->name.m_name, IBD, true); |
| 1523 |
|
| 1524 |
if (filepath == nullptr) { |
| 1525 |
return (DB_OUT_OF_MEMORY); |
| 1526 |
} |
| 1527 |
|
| 1528 |
err = fil_delete_tablespace(table->space, BUF_REMOVE_NONE); |
| 1529 |
|
| 1530 |
ut_a(err == DB_SUCCESS || err == DB_TABLESPACE_NOT_FOUND || |
| 1531 |
err == DB_IO_ERROR); |
| 1532 |
|
| 1533 |
if (err == DB_IO_ERROR) { |
| 1534 |
ib::info(ER_IB_MSG_179) << "IO error while deleting: " << table->space |
| 1535 |
<< " during rename of '" << old_name << "' to" |
| 1536 |
<< " '" << new_name << "'"; |
| 1537 |
} |
| 1538 |
|
| 1539 |
/* Delete any temp file hanging around. */ |
| 1540 |
os_file_type_t ftype; |
| 1541 |
bool exists; |
| 1542 |
if (os_file_status(filepath, &exists, &ftype) && exists && |
| 1543 |
!os_file_delete_if_exists(innodb_temp_file_key, filepath, nullptr)) { |
| 1544 |
ib::info(ER_IB_MSG_180) << "Delete of " << filepath << " failed."; |
| 1545 |
} |
| 1546 |
|
| 1547 |
ut::free(filepath); |
| 1548 |
|
| 1549 |
} else if (dict_table_is_file_per_table(table)) { |
| 1550 |
char *new_path = nullptr; |
| 1551 |
char *old_path = fil_space_get_first_path(table->space); |
| 1552 |
|
| 1553 |
ut_ad(!table->is_temporary()); |
| 1554 |
|
| 1555 |
/* In case of explicit data dir setting or |
| 1556 |
table recreation(ALTER TABLE .. FORCE/ALTER TABLE .. ENGINE) |
| 1557 |
from a non-default path, the new table path should be same |
| 1558 |
as the source/old path. */ |
| 1559 |
if (DICT_TF_HAS_DATA_DIR(table->flags)) { |
| 1560 |
ut_ad(old_path != nullptr); |
| 1561 |
std::string new_ibd; |
| 1562 |
|
| 1563 |
new_ibd = Fil_path::make_new_path(old_path, new_name, IBD); |
| 1564 |
|
| 1565 |
new_path = mem_strdup(new_ibd.c_str()); |
| 1566 |
|
| 1567 |
/* InnoDB adds the db directory to the data directory. |
| 1568 |
If the RENAME changes database, then it is possible that |
| 1569 |
the a directory named for the new db does not exist |
| 1570 |
in this remote location. */ |
| 1571 |
err = os_file_create_subdirs_if_needed(new_path); |
| 1572 |
if (err != DB_SUCCESS) { |
| 1573 |
ut::free(old_path); |
| 1574 |
ut::free(new_path); |
| 1575 |
return (err); |
| 1576 |
} |
| 1577 |
} else { |
| 1578 |
new_path = Fil_path::make_ibd_from_table_name(new_name); |
| 1579 |
} |
| 1580 |
|
| 1581 |
/* New filepath must not exist. */ |
| 1582 |
err = fil_rename_tablespace_check(table->space, old_path, new_path, false); |
| 1583 |
if (err != DB_SUCCESS) { |
| 1584 |
ut::free(old_path); |
| 1585 |
ut::free(new_path); |
| 1586 |
return (err); |
| 1587 |
} |
| 1588 |
|
| 1589 |
std::string new_tablespace_name(new_name); |
| 1590 |
dict_name::convert_to_space(new_tablespace_name); |
| 1591 |
|
| 1592 |
dberr_t err = fil_rename_tablespace(table->space, old_path, |
| 1593 |
new_tablespace_name.c_str(), new_path); |
| 1594 |
|
| 1595 |
ut::free(old_path); |
| 1596 |
ut::free(new_path); |
| 1597 |
|
| 1598 |
if (err != DB_SUCCESS) { |
| 1599 |
return (err); |
| 1600 |
} |
| 1601 |
} |
| 1602 |
|
| 1603 |
err = log_ddl->write_rename_table_log(table, new_name, table->name.m_name); |
| 1604 |
if (err != DB_SUCCESS) { |
| 1605 |
return (err); |
| 1606 |
} |
| 1607 |
|
| 1608 |
/* Remove table from the hash tables of tables */ |
| 1609 |
HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash, |
| 1610 |
ut::hash_string(old_name), table); |
| 1611 |
|
| 1612 |
if (strlen(new_name) > strlen(table->name.m_name)) { |
| 1613 |
/* We allocate MAX_FULL_NAME_LEN + 1 bytes here to avoid |
| 1614 |
memory fragmentation, we assume a repeated calls of |
| 1615 |
ut_realloc() with the same size do not cause fragmentation */ |
| 1616 |
ut_a(strlen(new_name) <= MAX_FULL_NAME_LEN); |
| 1617 |
|
| 1618 |
table->name.m_name = static_cast<char *>(ut::realloc_withkey( |
| 1619 |
UT_NEW_THIS_FILE_PSI_KEY, table->name.m_name, MAX_FULL_NAME_LEN + 1)); |
| 1620 |
} |
| 1621 |
strcpy(table->name.m_name, new_name); |
| 1622 |
|
| 1623 |
/* Add table to hash table of tables */ |
| 1624 |
HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, hash_value, table); |
| 1625 |
|
| 1626 |
dict_sys->size += strlen(new_name) - strlen(old_name); |
| 1627 |
ut_a(dict_sys->size > 0); |
| 1628 |
|
| 1629 |
/* Update the table_name field in indexes */ |
| 1630 |
for (index = table->first_index(); index != nullptr; index = index->next()) { |
| 1631 |
index->table_name = table->name.m_name; |
| 1632 |
} |
| 1633 |
|
| 1634 |
if (!rename_also_foreigns) { |
| 1635 |
/* In ALTER TABLE we think of the rename table operation |
| 1636 |
in the direction table -> temporary table (#sql...) |
| 1637 |
as dropping the table with the old name and creating |
| 1638 |
a new with the new name. Thus we kind of drop the |
| 1639 |
constraints from the dictionary cache here. The foreign key |
| 1640 |
constraints will be inherited to the new table from the |
| 1641 |
system tables through a call of dict_load_foreigns. */ |
| 1642 |
|
| 1643 |
/* Remove the foreign constraints from the cache */ |
| 1644 |
std::for_each(table->foreign_set.begin(), table->foreign_set.end(), |
| 1645 |
dict_foreign_remove_partial()); |
| 1646 |
table->foreign_set.clear(); |
| 1647 |
|
| 1648 |
/* Reset table field in referencing constraints */ |
| 1649 |
for (dict_foreign_set::iterator it = table->referenced_set.begin(); |
| 1650 |
it != table->referenced_set.end(); ++it) { |
| 1651 |
foreign = *it; |
| 1652 |
foreign->referenced_table = nullptr; |
| 1653 |
foreign->referenced_index = nullptr; |
| 1654 |
} |
| 1655 |
|
| 1656 |
/* Make the set of referencing constraints empty */ |
| 1657 |
table->referenced_set.clear(); |
| 1658 |
|
| 1659 |
return (DB_SUCCESS); |
| 1660 |
} |
| 1661 |
|
| 1662 |
/* Update the table name fields in foreign constraints, and update also |
| 1663 |
the constraint id of new format >= 4.0.18 constraints. Note that at |
| 1664 |
this point we have already changed table->name to the new name. */ |
| 1665 |
|
| 1666 |
dict_foreign_set fk_set; |
| 1667 |
|
| 1668 |
for (;;) { |
| 1669 |
dict_foreign_set::iterator it = table->foreign_set.begin(); |
| 1670 |
|
| 1671 |
if (it == table->foreign_set.end()) { |
| 1672 |
break; |
| 1673 |
} |
| 1674 |
|
| 1675 |
foreign = *it; |
| 1676 |
|
| 1677 |
if (foreign->referenced_table) { |
| 1678 |
foreign->referenced_table->referenced_set.erase(foreign); |
| 1679 |
} |
| 1680 |
|
| 1681 |
if (ut_strlen(foreign->foreign_table_name) < |
| 1682 |
ut_strlen(table->name.m_name)) { |
| 1683 |
/* Allocate a longer name buffer; |
| 1684 |
TODO: store buf len to save memory */ |
| 1685 |
|
| 1686 |
foreign->foreign_table_name = |
| 1687 |
mem_heap_strdup(foreign->heap, table->name.m_name); |
| 1688 |
dict_mem_foreign_table_name_lookup_set(foreign, true); |
| 1689 |
} else { |
| 1690 |
strcpy(foreign->foreign_table_name, table->name.m_name); |
| 1691 |
dict_mem_foreign_table_name_lookup_set(foreign, false); |
| 1692 |
} |
| 1693 |
if (strchr(foreign->id, '/')) { |
| 1694 |
/* This is a >= 4.0.18 format id */ |
| 1695 |
|
| 1696 |
ulint db_len; |
| 1697 |
char *old_id; |
| 1698 |
char old_name_cs_filename[MAX_FULL_NAME_LEN + 1]; |
| 1699 |
uint errors = 0; |
| 1700 |
|
| 1701 |
/* All table names are internally stored in charset |
| 1702 |
my_charset_filename (except the temp tables and the |
| 1703 |
partition identifier suffix in partition tables). The |
| 1704 |
foreign key constraint names are internally stored |
| 1705 |
in UTF-8 charset. The variable fkid here is used |
| 1706 |
to store foreign key constraint name in charset |
| 1707 |
my_charset_filename for comparison further below. */ |
| 1708 |
char fkid[MAX_TABLE_NAME_LEN + 20]; |
| 1709 |
bool on_tmp = false; |
| 1710 |
|
| 1711 |
/* The old table name in my_charset_filename is stored |
| 1712 |
in old_name_cs_filename */ |
| 1713 |
|
| 1714 |
strncpy(old_name_cs_filename, old_name, sizeof(old_name_cs_filename)); |
| 1715 |
if (strstr(old_name, TEMP_TABLE_PATH_PREFIX) == nullptr) { |
| 1716 |
innobase_convert_to_system_charset( |
| 1717 |
strchr(old_name_cs_filename, '/') + 1, strchr(old_name, '/') + 1, |
| 1718 |
MAX_TABLE_NAME_LEN, &errors); |
| 1719 |
|
| 1720 |
if (errors) { |
| 1721 |
/* There has been an error to convert |
| 1722 |
old table into UTF-8. This probably |
| 1723 |
means that the old table name is |
| 1724 |
actually in UTF-8. */ |
| 1725 |
innobase_convert_to_filename_charset( |
| 1726 |
strchr(old_name_cs_filename, '/') + 1, strchr(old_name, '/') + 1, |
| 1727 |
MAX_TABLE_NAME_LEN); |
| 1728 |
} else { |
| 1729 |
/* Old name already in |
| 1730 |
my_charset_filename */ |
| 1731 |
strncpy(old_name_cs_filename, old_name, sizeof(old_name_cs_filename)); |
| 1732 |
} |
| 1733 |
} |
| 1734 |
|
| 1735 |
strncpy(fkid, foreign->id, MAX_TABLE_NAME_LEN); |
| 1736 |
|
| 1737 |
if (strstr(fkid, TEMP_TABLE_PATH_PREFIX) == nullptr) { |
| 1738 |
innobase_convert_to_filename_charset(strchr(fkid, '/') + 1, |
| 1739 |
strchr(foreign->id, '/') + 1, |
| 1740 |
MAX_TABLE_NAME_LEN + 20); |
| 1741 |
} else { |
| 1742 |
on_tmp = true; |
| 1743 |
} |
| 1744 |
|
| 1745 |
old_id = mem_strdup(foreign->id); |
| 1746 |
|
| 1747 |
if (ut_strlen(fkid) > |
| 1748 |
ut_strlen(old_name_cs_filename) + ((sizeof dict_ibfk) - 1) && |
| 1749 |
!memcmp(fkid, old_name_cs_filename, |
| 1750 |
ut_strlen(old_name_cs_filename)) && |
| 1751 |
!memcmp(fkid + ut_strlen(old_name_cs_filename), dict_ibfk, |
| 1752 |
(sizeof dict_ibfk) - 1)) { |
| 1753 |
/* This is a generated >= 4.0.18 format id */ |
| 1754 |
|
| 1755 |
char table_name[MAX_TABLE_NAME_LEN + 1] = ""; |
| 1756 |
uint errors = 0; |
| 1757 |
|
| 1758 |
if (strlen(table->name.m_name) > strlen(old_name)) { |
| 1759 |
foreign->id = static_cast<char *>(mem_heap_alloc( |
| 1760 |
foreign->heap, strlen(table->name.m_name) + strlen(old_id) + 1)); |
| 1761 |
} |
| 1762 |
|
| 1763 |
/* Convert the table name to UTF-8 */ |
| 1764 |
strncpy(table_name, table->name.m_name, MAX_TABLE_NAME_LEN); |
| 1765 |
innobase_convert_to_system_charset(strchr(table_name, '/') + 1, |
| 1766 |
strchr(table->name.m_name, '/') + 1, |
| 1767 |
MAX_TABLE_NAME_LEN, &errors); |
| 1768 |
|
| 1769 |
if (errors) { |
| 1770 |
/* Table name could not be converted |
| 1771 |
from charset my_charset_filename to |
| 1772 |
UTF-8. This means that the table name |
| 1773 |
is already in UTF-8 (#mysql#50). */ |
| 1774 |
strncpy(table_name, table->name.m_name, MAX_TABLE_NAME_LEN); |
| 1775 |
} |
| 1776 |
|
| 1777 |
/* Replace the prefix 'databasename/tablename' |
| 1778 |
with the new names */ |
| 1779 |
strcpy(foreign->id, table_name); |
| 1780 |
if (on_tmp) { |
| 1781 |
strcat(foreign->id, old_id + ut_strlen(old_name)); |
| 1782 |
} else { |
| 1783 |
sprintf(strchr(foreign->id, '/') + 1, "%s%s", |
| 1784 |
strchr(table_name, '/') + 1, strstr(old_id, "_ibfk_")); |
| 1785 |
} |
| 1786 |
|
| 1787 |
} else { |
| 1788 |
/* This is a >= 4.0.18 format id where the user |
| 1789 |
gave the id name */ |
| 1790 |
db_len = dict_get_db_name_len(table->name.m_name) + 1; |
| 1791 |
|
| 1792 |
if (db_len - 1 > dict_get_db_name_len(foreign->id)) { |
| 1793 |
foreign->id = static_cast<char *>( |
| 1794 |
mem_heap_alloc(foreign->heap, db_len + strlen(old_id) + 1)); |
| 1795 |
} |
| 1796 |
|
| 1797 |
/* Replace the database prefix in id with the |
| 1798 |
one from table->name */ |
| 1799 |
|
| 1800 |
ut_memcpy(foreign->id, table->name.m_name, db_len); |
| 1801 |
|
| 1802 |
strcpy(foreign->id + db_len, dict_remove_db_name(old_id)); |
| 1803 |
} |
| 1804 |
|
| 1805 |
ut::free(old_id); |
| 1806 |
} |
| 1807 |
|
| 1808 |
table->foreign_set.erase(it); |
| 1809 |
fk_set.insert(foreign); |
| 1810 |
|
| 1811 |
if (foreign->referenced_table) { |
| 1812 |
foreign->referenced_table->referenced_set.insert(foreign); |
| 1813 |
} |
| 1814 |
} |
| 1815 |
|
| 1816 |
ut_a(table->foreign_set.empty()); |
| 1817 |
table->foreign_set.swap(fk_set); |
| 1818 |
|
| 1819 |
for (dict_foreign_set::iterator it = table->referenced_set.begin(); |
| 1820 |
it != table->referenced_set.end(); ++it) { |
| 1821 |
foreign = *it; |
| 1822 |
|
| 1823 |
if (ut_strlen(foreign->referenced_table_name) < |
| 1824 |
ut_strlen(table->name.m_name)) { |
| 1825 |
/* Allocate a longer name buffer; |
| 1826 |
TODO: store buf len to save memory */ |
| 1827 |
|
| 1828 |
foreign->referenced_table_name = |
| 1829 |
mem_heap_strdup(foreign->heap, table->name.m_name); |
| 1830 |
|
| 1831 |
dict_mem_referenced_table_name_lookup_set(foreign, true); |
| 1832 |
} else { |
| 1833 |
/* Use the same buffer */ |
| 1834 |
strcpy(foreign->referenced_table_name, table->name.m_name); |
| 1835 |
|
| 1836 |
dict_mem_referenced_table_name_lookup_set(foreign, false); |
| 1837 |
} |
| 1838 |
} |
| 1839 |
|
| 1840 |
return (DB_SUCCESS); |
| 1841 |
} |
| 1842 |
|
| 1843 |
/** Change the id of a table object in the dictionary cache. This is used in |
| 1844 |
DISCARD TABLESPACE. */ |
| 1845 |
void dict_table_change_id_in_cache( |
| 1846 |
dict_table_t *table, /*!< in/out: table object already in cache */ |
| 1847 |
table_id_t new_id) /*!< in: new id to set */ |
| 1848 |
{ |
| 1849 |
ut_ad(table); |
| 1850 |
ut_ad(dict_sys_mutex_own()); |
| 1851 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 1852 |
|
| 1853 |
#ifdef UNIV_DEBUG |
| 1854 |
|
| 1855 |
auto id_fold = ut::hash_uint64(table->id); |
| 1856 |
/* Table with the same id should not exist in cache */ |
| 1857 |
{ |
| 1858 |
dict_table_t *table2; |
| 1859 |
HASH_SEARCH(id_hash, dict_sys->table_id_hash, id_fold, dict_table_t *, |
| 1860 |
table2, ut_ad(table2->cached), table2->id == new_id); |
| 1861 |
ut_ad(table2 == nullptr); |
| 1862 |
|
| 1863 |
if (table2 != nullptr) { |
| 1864 |
return; |
| 1865 |
} |
| 1866 |
} |
| 1867 |
#endif /*UNIV_DEBUG*/ |
| 1868 |
|
| 1869 |
/* Remove the table from the hash table of id's */ |
| 1870 |
|
| 1871 |
HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash, |
| 1872 |
ut::hash_uint64(table->id), table); |
| 1873 |
|
| 1874 |
table->id = new_id; |
| 1875 |
|
| 1876 |
/* Add the table back to the hash table */ |
| 1877 |
HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash, |
| 1878 |
ut::hash_uint64(table->id), table); |
| 1879 |
} |
| 1880 |
|
| 1881 |
/** Removes a table object from the dictionary cache. */ |
| 1882 |
static void dict_table_remove_from_cache_low( |
| 1883 |
dict_table_t *table, /*!< in, own: table */ |
| 1884 |
bool lru_evict) /*!< in: true if table being evicted |
| 1885 |
to make room in the table LRU list */ |
| 1886 |
{ |
| 1887 |
dict_foreign_t *foreign; |
| 1888 |
dict_index_t *index; |
| 1889 |
|
| 1890 |
ut_ad(table); |
| 1891 |
ut_ad(dict_lru_validate()); |
| 1892 |
ut_a(table->get_ref_count() == 0); |
| 1893 |
ut_a(table->n_rec_locks.load() == 0); |
| 1894 |
ut_ad(dict_sys_mutex_own()); |
| 1895 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 1896 |
|
| 1897 |
/* We first dirty read the status which could be changed from |
| 1898 |
METADATA_DIRTY to METADATA_BUFFERED by checkpoint, and check again |
| 1899 |
when persistence is necessary */ |
| 1900 |
switch (table->dirty_status.load()) { |
| 1901 |
case METADATA_DIRTY: |
| 1902 |
/* Write back the dirty metadata to DDTableBuffer */ |
| 1903 |
dict_table_persist_to_dd_table_buffer(table); |
| 1904 |
ut_ad(table->dirty_status.load() != METADATA_DIRTY); |
| 1905 |
[[fallthrough]]; |
| 1906 |
case METADATA_BUFFERED: |
| 1907 |
/* We have to remove it away here, since it's evicted. |
| 1908 |
And we will add it again once it's re-loaded if possible */ |
| 1909 |
mutex_enter(&dict_persist->mutex); |
| 1910 |
ut_ad(table->in_dirty_dict_tables_list); |
| 1911 |
UT_LIST_REMOVE(dict_persist->dirty_dict_tables, table); |
| 1912 |
mutex_exit(&dict_persist->mutex); |
| 1913 |
break; |
| 1914 |
case METADATA_CLEAN: |
| 1915 |
break; |
| 1916 |
} |
| 1917 |
|
| 1918 |
/* Remove the foreign constraints from the cache */ |
| 1919 |
std::for_each(table->foreign_set.begin(), table->foreign_set.end(), |
| 1920 |
dict_foreign_remove_partial()); |
| 1921 |
table->foreign_set.clear(); |
| 1922 |
|
| 1923 |
/* Reset table field in referencing constraints */ |
| 1924 |
for (dict_foreign_set::iterator it = table->referenced_set.begin(); |
| 1925 |
it != table->referenced_set.end(); ++it) { |
| 1926 |
foreign = *it; |
| 1927 |
foreign->referenced_table = nullptr; |
| 1928 |
foreign->referenced_index = nullptr; |
| 1929 |
} |
| 1930 |
|
| 1931 |
/* Remove the indexes from the cache */ |
| 1932 |
|
| 1933 |
for (index = UT_LIST_GET_LAST(table->indexes); index != nullptr; |
| 1934 |
index = UT_LIST_GET_LAST(table->indexes)) { |
| 1935 |
dict_index_remove_from_cache_low(table, index, lru_evict); |
| 1936 |
} |
| 1937 |
|
| 1938 |
/* Remove table from the hash tables of tables */ |
| 1939 |
|
| 1940 |
HASH_DELETE(dict_table_t, name_hash, dict_sys->table_hash, |
| 1941 |
ut::hash_string(table->name.m_name), table); |
| 1942 |
|
| 1943 |
HASH_DELETE(dict_table_t, id_hash, dict_sys->table_id_hash, |
| 1944 |
ut::hash_uint64(table->id), table); |
| 1945 |
|
| 1946 |
/* Remove table from LRU or non-LRU list. */ |
| 1947 |
if (table->can_be_evicted) { |
| 1948 |
ut_ad(dict_lru_find_table(table)); |
| 1949 |
UT_LIST_REMOVE(dict_sys->table_LRU, table); |
| 1950 |
} else { |
| 1951 |
ut_ad(dict_non_lru_find_table(table)); |
| 1952 |
UT_LIST_REMOVE(dict_sys->table_non_LRU, table); |
| 1953 |
} |
| 1954 |
|
| 1955 |
ut_ad(dict_lru_validate()); |
| 1956 |
|
| 1957 |
/* Free virtual column template if any */ |
| 1958 |
if (table->vc_templ != nullptr) { |
| 1959 |
dict_free_vc_templ(table->vc_templ); |
| 1960 |
ut::delete_(table->vc_templ); |
| 1961 |
} |
| 1962 |
|
| 1963 |
const auto size = |
| 1964 |
mem_heap_get_size(table->heap) + strlen(table->name.m_name) + 1; |
| 1965 |
|
| 1966 |
ut_ad(dict_sys->size >= size); |
| 1967 |
|
| 1968 |
dict_sys->size -= size; |
| 1969 |
|
| 1970 |
dict_mem_table_free(table); |
| 1971 |
} |
| 1972 |
|
| 1973 |
/** Removes a table object from the dictionary cache. */ |
| 1974 |
void dict_table_remove_from_cache(dict_table_t *table) /*!< in, own: table */ |
| 1975 |
{ |
| 1976 |
dict_table_remove_from_cache_low(table, false); |
| 1977 |
} |
| 1978 |
|
| 1979 |
/** Try to invalidate an entry from the dict cache, for a partitioned table, |
| 1980 |
if any table found. |
| 1981 |
@param[in] name Table name */ |
| 1982 |
void dict_partitioned_table_remove_from_cache(const char *name) { |
| 1983 |
ut_ad(dict_sys_mutex_own()); |
| 1984 |
|
| 1985 |
size_t name_len = strlen(name); |
| 1986 |
const auto name_with_separator = |
| 1987 |
std::string{name, name_len} + dict_name::PART_SEPARATOR; |
| 1988 |
|
| 1989 |
for (uint32_t i = 0; i < hash_get_n_cells(dict_sys->table_id_hash); ++i) { |
| 1990 |
dict_table_t *table; |
| 1991 |
|
| 1992 |
table = |
| 1993 |
static_cast<dict_table_t *>(hash_get_first(dict_sys->table_hash, i)); |
| 1994 |
|
| 1995 |
while (table != nullptr) { |
| 1996 |
dict_table_t *prev_table = table; |
| 1997 |
|
| 1998 |
table = static_cast<dict_table_t *>(HASH_GET_NEXT(name_hash, prev_table)); |
| 1999 |
ut_ad(prev_table->magic_n == DICT_TABLE_MAGIC_N); |
| 2000 |
|
| 2001 |
if (prev_table->is_dd_table) { |
| 2002 |
continue; |
| 2003 |
} |
| 2004 |
|
| 2005 |
/* Find all the partitions or subpartitions of table with name */ |
| 2006 |
if (!strncmp(name_with_separator.data(), prev_table->name.m_name, |
| 2007 |
name_with_separator.size())) { |
| 2008 |
ut_a(dict_table_is_partition(prev_table)); |
| 2009 |
btr_drop_ahi_for_table(prev_table); |
| 2010 |
dict_table_remove_from_cache(prev_table); |
| 2011 |
} |
| 2012 |
} |
| 2013 |
} |
| 2014 |
} |
| 2015 |
|
| 2016 |
#ifdef UNIV_DEBUG |
| 2017 |
/** Removes a table object from the dictionary cache, for debug purpose |
| 2018 |
@param[in,out] table table object |
| 2019 |
@param[in] lru_evict true if table being evicted to make room |
| 2020 |
in the table LRU list */ |
| 2021 |
void dict_table_remove_from_cache_debug(dict_table_t *table, bool lru_evict) { |
| 2022 |
dict_table_remove_from_cache_low(table, lru_evict); |
| 2023 |
} |
| 2024 |
#endif /* UNIV_DEBUG */ |
| 2025 |
|
| 2026 |
/** If the given column name is reserved for InnoDB system columns, return |
| 2027 |
true. |
| 2028 |
@return true if name is reserved */ |
| 2029 |
bool dict_col_name_is_reserved(const char *name) /*!< in: column name */ |
| 2030 |
{ |
| 2031 |
/* This check reminds that if a new system column is added to |
| 2032 |
the program, it should be dealt with here. */ |
| 2033 |
static_assert(DATA_N_SYS_COLS == 3, "DATA_N_SYS_COLS != 3"); |
| 2034 |
|
| 2035 |
static const char *reserved_names[] = {"DB_ROW_ID", "DB_TRX_ID", |
| 2036 |
"DB_ROLL_PTR"}; |
| 2037 |
|
| 2038 |
ulint i; |
| 2039 |
|
| 2040 |
for (i = 0; i < UT_ARR_SIZE(reserved_names); i++) { |
| 2041 |
if (innobase_strcasecmp(name, reserved_names[i]) == 0) { |
| 2042 |
return true; |
| 2043 |
} |
| 2044 |
} |
| 2045 |
|
| 2046 |
return false; |
| 2047 |
} |
| 2048 |
|
| 2049 |
/** Return maximum size of the node pointer record. |
| 2050 |
@return maximum size of the record in bytes */ |
| 2051 |
ulint dict_index_node_ptr_max_size(const dict_index_t *index) /*!< in: index */ |
| 2052 |
{ |
| 2053 |
ulint comp; |
| 2054 |
ulint i; |
| 2055 |
/* maximum possible storage size of a record */ |
| 2056 |
size_t rec_max_size; |
| 2057 |
|
| 2058 |
if (dict_index_is_ibuf(index)) { |
| 2059 |
/* cannot estimate accurately */ |
| 2060 |
/* This is universal index for change buffer. |
| 2061 |
The max size of the entry is about max key length * 2. |
| 2062 |
(index key + primary key to be inserted to the index) |
| 2063 |
(The max key length is UNIV_PAGE_SIZE / 16 * 3 at |
| 2064 |
ha_innobase::max_supported_key_length(), |
| 2065 |
considering MAX_KEY_LENGTH = 3072 at MySQL imposes |
| 2066 |
the 3500 historical InnoDB value for 16K page size case.) |
| 2067 |
For the universal index, node_ptr contains most of the entry. |
| 2068 |
And 512 is enough to contain ibuf columns and meta-data */ |
| 2069 |
return (UNIV_PAGE_SIZE / 8 * 3 + 512); |
| 2070 |
} |
| 2071 |
|
| 2072 |
comp = dict_table_is_comp(index->table); |
| 2073 |
|
| 2074 |
/* Each record has page_no, length of page_no and header. */ |
| 2075 |
rec_max_size = comp ? REC_NODE_PTR_SIZE + 1 + REC_N_NEW_EXTRA_BYTES |
| 2076 |
: REC_NODE_PTR_SIZE + 2 + REC_N_OLD_EXTRA_BYTES; |
| 2077 |
|
| 2078 |
if (comp) { |
| 2079 |
/* Include the "null" flags in the |
| 2080 |
maximum possible record size. */ |
| 2081 |
rec_max_size += UT_BITS_IN_BYTES(index->n_nullable); |
| 2082 |
} else { |
| 2083 |
/* For each column, include a 2-byte offset and a |
| 2084 |
"null" flag. */ |
| 2085 |
rec_max_size += 2 * index->n_fields; |
| 2086 |
} |
| 2087 |
|
| 2088 |
/* Compute the maximum possible record size. */ |
| 2089 |
for (i = 0; i < dict_index_get_n_unique_in_tree(index); i++) { |
| 2090 |
const dict_field_t *field = index->get_field(i); |
| 2091 |
get_field_max_size(index->table, index, field, rec_max_size); |
| 2092 |
} |
| 2093 |
|
| 2094 |
return (rec_max_size); |
| 2095 |
} |
| 2096 |
|
| 2097 |
void get_permissible_max_size(const dict_table_t *table, |
| 2098 |
const dict_index_t *index, size_t &page_rec_max, |
| 2099 |
size_t &page_ptr_max) { |
| 2100 |
const bool comp = dict_table_is_comp(table); |
| 2101 |
const page_size_t page_size(dict_table_page_size(table)); |
| 2102 |
|
| 2103 |
if (page_size.is_compressed() && |
| 2104 |
page_size.physical() < univ_page_size.physical()) { |
| 2105 |
/* On a compressed page, two records must fit in the |
| 2106 |
uncompressed page modification log. On compressed pages |
| 2107 |
with size.physical() == univ_page_size.physical(), |
| 2108 |
this limit will never be reached. */ |
| 2109 |
ut_ad(comp); |
| 2110 |
/* The maximum allowed record size is the size of |
| 2111 |
an empty page, minus a byte for recoding the heap |
| 2112 |
number in the page modification log. The maximum |
| 2113 |
allowed node pointer size is half that. */ |
| 2114 |
page_rec_max = page_zip_empty_size(index->n_fields, page_size.physical()); |
| 2115 |
if (page_rec_max) { |
| 2116 |
page_rec_max--; |
| 2117 |
} |
| 2118 |
page_ptr_max = page_rec_max / 2; |
| 2119 |
} else { |
| 2120 |
/* The maximum allowed record size is half a B-tree |
| 2121 |
page(16k for 64k page size). No additional sparse |
| 2122 |
page directory entry will be generated for the first |
| 2123 |
few user records. */ |
| 2124 |
page_rec_max = srv_page_size == UNIV_PAGE_SIZE_MAX |
| 2125 |
? REC_MAX_DATA_SIZE - 1 |
| 2126 |
: page_get_free_space_of_empty(comp) / 2; |
| 2127 |
page_ptr_max = page_rec_max; |
| 2128 |
} |
| 2129 |
} |
| 2130 |
|
| 2131 |
void get_field_max_size(const dict_table_t *table, const dict_index_t *index, |
| 2132 |
const dict_field_t *field, size_t &rec_max_size) { |
| 2133 |
/* Determine the maximum length of the index field. In case ROW_FORMAT is |
| 2134 |
not REDUNDANT, this function should take into account the extra bytes needed |
| 2135 |
to encode the variable length of the field, computed as the worst case in |
| 2136 |
rec_get_converted_size_comp() for REC_STATUS_ORDINARY records. */ |
| 2137 |
|
| 2138 |
const bool comp = dict_table_is_comp(table); |
| 2139 |
const dict_col_t *col = field->col; |
| 2140 |
|
| 2141 |
if (field->fixed_len != 0) { |
| 2142 |
/* dict_index_add_col() should guarantee this */ |
| 2143 |
ut_ad(!field->prefix_len || field->fixed_len == field->prefix_len); |
| 2144 |
/* Fixed lengths are not encoded in ROW_FORMAT=COMPACT. */ |
| 2145 |
rec_max_size += field->fixed_len; |
| 2146 |
return; |
| 2147 |
} |
| 2148 |
/* For historical reasons, a spatial index on DATA_GEOMETRY has fixed_len==0, |
| 2149 |
even though in reality it always uses DATA_MBR_LEN bytes for the MBR. We can't |
| 2150 |
change the on-disc format - the extra byte for length is indeed always |
| 2151 |
there. But, we can handle this special case here, explicitly, instead of using |
| 2152 |
the generic code below, which would see field_max_size=ULINT_MAX, add 2, and |
| 2153 |
cause overflow. |
| 2154 |
The first column of spatial index is always one of the geometry types like |
| 2155 |
DATA_POINT, DATA_VAR_POINT or DATA_GEOMETRY. The next field(s) are the PK |
| 2156 |
columns, none of which can be DATA_GEOMETRY as PK can't have unbounded |
| 2157 |
length. This is why we don't need to check if the field is the first field of |
| 2158 |
the spatial index once we see it is DATA_GEOMETRY. We can't check this anyway, |
| 2159 |
because the field might be a dummy object not yet added to the index. */ |
| 2160 |
if (dict_index_is_spatial(index) && col->mtype == DATA_GEOMETRY) { |
| 2161 |
rec_max_size += DATA_MBR_LEN + 1; |
| 2162 |
return; |
| 2163 |
} |
| 2164 |
|
| 2165 |
size_t field_max_size = col->get_max_size(); |
| 2166 |
size_t field_ext_max_size = DATA_BIG_COL(col) ? 2 : 1; |
| 2167 |
|
| 2168 |
/* The first dict_index_get_n_unique_in_tree(index) fields are used for |
| 2169 |
navigating the B-tree, so are always inlined for efficient access. |
| 2170 |
This function may be called with a dummy field which doesn't even belong to |
| 2171 |
the index yet. In such case, it is not part of the unique prefix anyway.*/ |
| 2172 |
const bool always_inlined = std::any_of( |
| 2173 |
index->fields, index->fields + dict_index_get_n_unique_in_tree(index), |
| 2174 |
[&](const dict_field_t &pref_field) { return field == &pref_field; }); |
| 2175 |
|
| 2176 |
/* A spatial index on DATA_GEOMETRY should be the only case where we have |
| 2177 |
prefix_len=0 AND unbounded col->get_max_size() AND the field is |
| 2178 |
always_inlined. Otherwise fields used in B-tree navigation could be |
| 2179 |
arbitrarily long, which is impossible to handle efficiently. */ |
| 2180 |
ut_ad(!(always_inlined && field_max_size == ULINT_MAX && |
| 2181 |
field->prefix_len == 0)); |
| 2182 |
|
| 2183 |
if (field->prefix_len) { |
| 2184 |
ut_ad(always_inlined); |
| 2185 |
if (field->prefix_len < field_max_size) { |
| 2186 |
field_max_size = field->prefix_len; |
| 2187 |
} |
| 2188 |
} else if (!always_inlined && |
| 2189 |
field_max_size > BTR_EXTERN_LOCAL_STORED_MAX_SIZE && |
| 2190 |
DATA_BIG_COL(col) && index->is_clustered()) { |
| 2191 |
/* In dtuple_convert_big_rec(), a "big" variable-length column that is |
| 2192 |
longer than BTR_EXTERN_LOCAL_STORED_MAX_SIZE and is not used for navigation |
| 2193 |
may be chosen for external storage. */ |
| 2194 |
if (dict_table_has_atomic_blobs(table)) { |
| 2195 |
/* In the worst case, we have a locally stored column of |
| 2196 |
BTR_EXTERN_LOCAL_STORED_MAX_SIZE bytes. The length can be stored in one |
| 2197 |
byte. If the column were stored externally, the lengths in the clustered |
| 2198 |
index page would be BTR_EXTERN_FIELD_REF_SIZE and 2. */ |
| 2199 |
field_max_size = BTR_EXTERN_LOCAL_STORED_MAX_SIZE; |
| 2200 |
field_ext_max_size = 1; |
| 2201 |
} else { |
| 2202 |
/* In this case, externally stored fields would have 768 byte prefix |
| 2203 |
stored in-line followed by fields reference. */ |
| 2204 |
size_t local_stored_length = |
| 2205 |
BTR_EXTERN_FIELD_REF_SIZE + DICT_ANTELOPE_MAX_INDEX_COL_LEN; |
| 2206 |
if (field_max_size > local_stored_length) { |
| 2207 |
field_max_size = local_stored_length; |
| 2208 |
/* Prefix length will be stored in 2 bytes. */ |
| 2209 |
field_ext_max_size = 2; |
| 2210 |
} |
| 2211 |
} |
| 2212 |
} |
| 2213 |
|
| 2214 |
/* Sanity check, preventing overflow. Note that col->get_max_size()==ULINT_MAX |
| 2215 |
implies DATA_BIG_COL(col), and we don't have secondary indexes on BLOBs |
| 2216 |
without prefix_len, so if you combine all the logical conditions leading to |
| 2217 |
this line, you should see why it must hold. */ |
| 2218 |
ut_ad(field_max_size != ULINT_MAX); |
| 2219 |
|
| 2220 |
if (comp) { |
| 2221 |
/* Add the extra size for ROW_FORMAT=COMPACT. |
| 2222 |
For ROW_FORMAT=REDUNDANT, these bytes were added to rec_max_size before the |
| 2223 |
call. */ |
| 2224 |
rec_max_size += field_ext_max_size; |
| 2225 |
} |
| 2226 |
|
| 2227 |
rec_max_size += field_max_size; |
| 2228 |
} |
| 2229 |
|
| 2230 |
/** A B-tree page should accommodate at least two records. This function finds |
| 2231 |
out if this is violated for records of maximum possible length of this index. |
| 2232 |
@param[in] table table |
| 2233 |
@param[in] new_index index |
| 2234 |
@return true if the index record could become too big */ |
| 2235 |
static bool dict_index_too_big_for_tree(const dict_table_t *table, |
| 2236 |
const dict_index_t *new_index) { |
| 2237 |
/* FTS index consists of auxiliary tables, they shall be excluded from index |
| 2238 |
row size check */ |
| 2239 |
if (new_index->type & DICT_FTS) { |
| 2240 |
return (false); |
| 2241 |
} |
| 2242 |
|
| 2243 |
DBUG_EXECUTE_IF("ib_force_create_table", return (false);); |
| 2244 |
|
| 2245 |
/* maximum allowed size of a record on a leaf page */ |
| 2246 |
size_t page_rec_max; |
| 2247 |
/* maximum allowed size of a node pointer record */ |
| 2248 |
size_t page_ptr_max; |
| 2249 |
get_permissible_max_size(table, new_index, page_rec_max, page_ptr_max); |
| 2250 |
|
| 2251 |
size_t rec_max_size; |
| 2252 |
bool res = dict_index_validate_max_rec_size(table, new_index, page_rec_max, |
| 2253 |
page_ptr_max, rec_max_size); |
| 2254 |
return (res); |
| 2255 |
} |
| 2256 |
|
| 2257 |
bool dict_index_validate_max_rec_size(const dict_table_t *table, |
| 2258 |
const dict_index_t *index, |
| 2259 |
const size_t page_rec_max, |
| 2260 |
const size_t page_ptr_max, |
| 2261 |
size_t &rec_max_size) { |
| 2262 |
const bool comp = dict_table_is_comp(table); |
| 2263 |
|
| 2264 |
const page_size_t page_size(dict_table_page_size(table)); |
| 2265 |
if (page_size.is_compressed() && |
| 2266 |
page_size.physical() < univ_page_size.physical()) { |
| 2267 |
/* On a compressed page, there is a two-byte entry in the dense page |
| 2268 |
directory for every record. But there is no record header. */ |
| 2269 |
rec_max_size = 2; |
| 2270 |
} else { |
| 2271 |
/* Each record has a header. */ |
| 2272 |
rec_max_size = comp ? REC_N_NEW_EXTRA_BYTES : REC_N_OLD_EXTRA_BYTES; |
| 2273 |
} |
| 2274 |
|
| 2275 |
if (comp) { |
| 2276 |
/* Include the "null" flags in the maximum possible record size. */ |
| 2277 |
rec_max_size += UT_BITS_IN_BYTES(index->n_nullable); |
| 2278 |
} else { |
| 2279 |
/* For each column, include a 2-byte offset and a "null" flag. The 1-byte |
| 2280 |
format is only used in short records that do not contain externally stored |
| 2281 |
columns. Such records could never exceed the page limit, even when using |
| 2282 |
the 2-byte format. */ |
| 2283 |
rec_max_size += 2 * index->n_fields; |
| 2284 |
} |
| 2285 |
|
| 2286 |
/* Each record would have version stored in 1 byte */ |
| 2287 |
if (index->has_row_versions()) { |
| 2288 |
rec_max_size += 1; |
| 2289 |
} |
| 2290 |
|
| 2291 |
/* Compute the maximum possible record size. */ |
| 2292 |
for (size_t i = 0; i < index->n_fields; i++) { |
| 2293 |
const dict_field_t *field = index->get_field(i); |
| 2294 |
get_field_max_size(table, index, field, rec_max_size); |
| 2295 |
|
| 2296 |
/* Check the size limit on leaf pages. */ |
| 2297 |
if (rec_max_size >= page_rec_max) { |
| 2298 |
return true; |
| 2299 |
} |
| 2300 |
|
| 2301 |
/* Check the size limit on non-leaf pages. Records stored in non-leaf |
| 2302 |
B-tree pages consist of the unique columns of the record (the key columns |
| 2303 |
of the B-tree) and a node pointer field. When we have processed the unique |
| 2304 |
columns, rec_max_size equals the size of the node pointer record minus the |
| 2305 |
node pointer column. */ |
| 2306 |
if (i + 1 == dict_index_get_n_unique_in_tree(index) && |
| 2307 |
rec_max_size + REC_NODE_PTR_SIZE >= page_ptr_max) { |
| 2308 |
return (true); |
| 2309 |
} |
| 2310 |
} |
| 2311 |
|
| 2312 |
return (false); |
| 2313 |
} |
| 2314 |
|
| 2315 |
/** Adds an index to the dictionary cache. |
| 2316 |
@param[in,out] table table on which the index is |
| 2317 |
@param[in,out] index index; NOTE! The index memory |
| 2318 |
object is freed in this function! |
| 2319 |
@param[in] page_no root page number of the index |
| 2320 |
@param[in] strict true=refuse to create the index |
| 2321 |
if records could be too big to fit in |
| 2322 |
an B-tree page |
| 2323 |
@return DB_SUCCESS, DB_TOO_BIG_RECORD, or DB_CORRUPTION */ |
| 2324 |
dberr_t dict_index_add_to_cache(dict_table_t *table, dict_index_t *index, |
| 2325 |
page_no_t page_no, bool strict) { |
| 2326 |
ut_ad(!dict_sys_mutex_own()); |
| 2327 |
return ( |
| 2328 |
dict_index_add_to_cache_w_vcol(table, index, nullptr, page_no, strict)); |
| 2329 |
} |
| 2330 |
|
| 2331 |
/** Clears the virtual column's index list before index is being freed. |
| 2332 |
@param[in] index Index being freed */ |
| 2333 |
void dict_index_remove_from_v_col_list(dict_index_t *index) { |
| 2334 |
/* Index is not completely formed */ |
| 2335 |
if (!index->cached) { |
| 2336 |
return; |
| 2337 |
} |
| 2338 |
if (dict_index_has_virtual(index)) { |
| 2339 |
const dict_col_t *col; |
| 2340 |
const dict_v_col_t *vcol; |
| 2341 |
|
| 2342 |
for (ulint i = 0; i < dict_index_get_n_fields(index); i++) { |
| 2343 |
col = index->get_col(i); |
| 2344 |
if (col->is_virtual()) { |
| 2345 |
vcol = reinterpret_cast<const dict_v_col_t *>(col); |
| 2346 |
/* This could be NULL, when we do add |
| 2347 |
virtual column, add index together. We do not |
| 2348 |
need to track this virtual column's index */ |
| 2349 |
if (vcol->v_indexes == nullptr) { |
| 2350 |
continue; |
| 2351 |
} |
| 2352 |
dict_v_idx_list::iterator it; |
| 2353 |
for (it = vcol->v_indexes->begin(); it != vcol->v_indexes->end(); |
| 2354 |
++it) { |
| 2355 |
dict_v_idx_t v_index = *it; |
| 2356 |
if (v_index.index == index) { |
| 2357 |
vcol->v_indexes->erase(it); |
| 2358 |
break; |
| 2359 |
} |
| 2360 |
} |
| 2361 |
} |
| 2362 |
} |
| 2363 |
} |
| 2364 |
} |
| 2365 |
/** Check if key part of the index has fixed len and thus rec offsets can be |
| 2366 |
cached. If so, it initializes rec_cache.offsets and rec_cache.nullable_cols, so |
| 2367 |
that we can avoid calling the costly rec_get_offsets() in many cases. That is: |
| 2368 |
- offsets will contain offsets of the first n columns within a rec, assuming |
| 2369 |
that none of them contains NULL, where n is chosen to be sufficient for the |
| 2370 |
most common use case, which is a binary search within a B-tree page, |
| 2371 |
- nullable_cols will be the number of columns among those first n columns, which |
| 2372 |
are NULLable at all, so that before using the rec_cache.offset we know how |
| 2373 |
many initial bits of the rec's NULL mask need to be zero, to ensure that the |
| 2374 |
rec doesn't have NULLs in the n first columns and thus rec_cache.offsets is |
| 2375 |
indeed corresponding to its layout and can be used for this rec. |
| 2376 |
@param[in] index The index instance for which rec_cache should be computed |
| 2377 |
*/ |
| 2378 |
static void dict_index_try_cache_rec_offsets(dict_index_t *index) { |
| 2379 |
ut_ad(index->rec_cache.offsets == nullptr); |
| 2380 |
ut_ad(index->rec_cache.nullable_cols == 0); |
| 2381 |
|
| 2382 |
const auto n_unique_in_tree = dict_index_get_n_unique_in_tree(index); |
| 2383 |
ut_a(0 < n_unique_in_tree); |
| 2384 |
#ifdef UNIV_DEBUG |
| 2385 |
/* Check for backward compatibility - in the past we used cache only for |
| 2386 |
intrinsic tables, and assumed that it is sufficient to check only if the |
| 2387 |
first n_uniq fields are fixed length. We'd like to ensure that the new code |
| 2388 |
uses cache for intrinsic tables at least in these cases it used to before. |
| 2389 |
But the loop below now checks n_unique_in_tree columns, not just n_unique. |
| 2390 |
Following assert makes sure that when n_unique < n_unique_in_tree, we still |
| 2391 |
use cache, because it only happens when n_unique_in_tree has one more column |
| 2392 |
which is the primary key reference and has a fixed length, so should not |
| 2393 |
affect the decision of the loop. |
| 2394 |
Note: in general primary keys can have more than one column, don't have to be |
| 2395 |
fixed length, etc. It just never happens for intrinsic tables in our testing. |
| 2396 |
This is not an assert crucial for correctness. It's just to show that there's |
| 2397 |
no obvious regression w.r.t intrinsic tables. */ |
| 2398 |
if (index->table->is_intrinsic() && index->n_uniq != n_unique_in_tree) { |
| 2399 |
ut_a(index->n_uniq == n_unique_in_tree - 1U); |
| 2400 |
ut_a(!index->is_clustered()); |
| 2401 |
ut_a(index->get_field(n_unique_in_tree - 1U)->fixed_len); |
| 2402 |
} |
| 2403 |
#endif |
| 2404 |
for (size_t i = 0; i < n_unique_in_tree; i++) { |
| 2405 |
if (!index->get_field(i)->fixed_len) { |
| 2406 |
return; |
| 2407 |
} |
| 2408 |
} |
| 2409 |
/* inlined rec_get_offsets() and rec_init_offsets()'s fast-path without |
| 2410 |
referencing rec follows: */ |
| 2411 |
|
| 2412 |
const auto offsets_len = n_unique_in_tree + (1 + REC_OFFS_HEADER_SIZE); |
| 2413 |
auto *const offsets = static_cast<ulint *>( |
| 2414 |
mem_heap_alloc(index->heap, sizeof(ulint) * offsets_len)); |
| 2415 |
|
| 2416 |
index->rec_cache.offsets = offsets; |
| 2417 |
rec_offs_set_n_alloc(offsets, offsets_len); |
| 2418 |
rec_offs_set_n_fields(offsets, n_unique_in_tree); |
| 2419 |
rec_init_fixed_offsets(index, offsets); |
| 2420 |
|
| 2421 |
for (size_t i = 0; i < n_unique_in_tree; i++) { |
| 2422 |
if (!(index->get_field(i)->col->prtype & DATA_NOT_NULL)) { |
| 2423 |
index->rec_cache.nullable_cols++; |
| 2424 |
} |
| 2425 |
} |
| 2426 |
ut_a(index->rec_cache.nullable_cols <= index->n_nullable); |
| 2427 |
} |
| 2428 |
|
| 2429 |
/** Adds an index to the dictionary cache, with possible indexing newly |
| 2430 |
added column. |
| 2431 |
@param[in,out] table table on which the index is |
| 2432 |
@param[in,out] index index; NOTE! The index memory |
| 2433 |
object is freed in this function! |
| 2434 |
@param[in] add_v new virtual column that being added along with |
| 2435 |
an add index call |
| 2436 |
@param[in] page_no root page number of the index |
| 2437 |
@param[in] strict true=refuse to create the index |
| 2438 |
if records could be too big to fit in |
| 2439 |
an B-tree page |
| 2440 |
@return DB_SUCCESS, DB_TOO_BIG_RECORD, or DB_CORRUPTION */ |
| 2441 |
dberr_t dict_index_add_to_cache_w_vcol(dict_table_t *table, dict_index_t *index, |
| 2442 |
const dict_add_v_col_t *add_v, |
| 2443 |
page_no_t page_no, bool strict) { |
| 2444 |
dict_index_t *new_index; |
| 2445 |
ulint n_ord; |
| 2446 |
ulint i; |
| 2447 |
|
| 2448 |
ut_ad(index); |
| 2449 |
ut_ad(!dict_sys_mutex_own()); |
| 2450 |
ut_ad(index->n_def == index->n_fields); |
| 2451 |
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N); |
| 2452 |
ut_ad(!dict_index_is_online_ddl(index)); |
| 2453 |
ut_ad(!dict_index_is_ibuf(index)); |
| 2454 |
|
| 2455 |
ut_d(mem_heap_validate(index->heap)); |
| 2456 |
ut_a(!index->is_clustered() || UT_LIST_GET_LEN(table->indexes) == 0); |
| 2457 |
|
| 2458 |
if (!dict_index_find_and_set_cols(table, index, add_v)) { |
| 2459 |
dict_mem_index_free(index); |
| 2460 |
return (DB_CORRUPTION); |
| 2461 |
} |
| 2462 |
|
| 2463 |
/* Build the cache internal representation of the index, |
| 2464 |
containing also the added system fields */ |
| 2465 |
|
| 2466 |
if (index->type == DICT_FTS) { |
| 2467 |
new_index = dict_index_build_internal_fts(table, index); |
| 2468 |
} else if (index->is_clustered()) { |
| 2469 |
new_index = dict_index_build_internal_clust(table, index); |
| 2470 |
} else { |
| 2471 |
new_index = dict_index_build_internal_non_clust(table, index); |
| 2472 |
} |
| 2473 |
|
| 2474 |
/* Set the n_fields value in new_index to the actual defined |
| 2475 |
number of fields in the cache internal representation */ |
| 2476 |
|
| 2477 |
new_index->n_fields = new_index->n_def; |
| 2478 |
|
| 2479 |
if (index->is_clustered() && table->has_row_versions()) { |
| 2480 |
new_index->n_fields = new_index->n_def - table->get_n_instant_drop_cols(); |
| 2481 |
} |
| 2482 |
new_index->n_total_fields = new_index->n_def; |
| 2483 |
new_index->trx_id = index->trx_id; |
| 2484 |
new_index->set_committed(index->is_committed()); |
| 2485 |
new_index->allow_duplicates = index->allow_duplicates; |
| 2486 |
new_index->nulls_equal = index->nulls_equal; |
| 2487 |
new_index->disable_ahi = index->disable_ahi; |
| 2488 |
new_index->srid_is_valid = index->srid_is_valid; |
| 2489 |
new_index->srid = index->srid; |
| 2490 |
|
| 2491 |
new_index->srid = index->srid; |
| 2492 |
new_index->srid_is_valid = index->srid_is_valid; |
| 2493 |
if (index->rtr_srs.get() != nullptr) |
| 2494 |
new_index->rtr_srs.reset(index->rtr_srs->clone()); |
| 2495 |
|
| 2496 |
if (dict_index_too_big_for_tree(table, new_index)) { |
| 2497 |
if (strict) { |
| 2498 |
dict_mem_index_free(new_index); |
| 2499 |
dict_mem_index_free(index); |
| 2500 |
return (DB_TOO_BIG_RECORD); |
| 2501 |
} else if (current_thd != nullptr) { |
| 2502 |
/* Avoid the warning to be printed |
| 2503 |
during recovery. */ |
| 2504 |
ib_warn_row_too_big(table); |
| 2505 |
} |
| 2506 |
} |
| 2507 |
|
| 2508 |
n_ord = new_index->n_uniq; |
| 2509 |
|
| 2510 |
/* Flag the ordering columns and also set column max_prefix */ |
| 2511 |
|
| 2512 |
for (i = 0; i < n_ord; i++) { |
| 2513 |
const dict_field_t *field = new_index->get_field(i); |
| 2514 |
|
| 2515 |
/* Check the column being added in the index for |
| 2516 |
the first time and flag the ordering column. */ |
| 2517 |
if (field->col->ord_part == 0) { |
| 2518 |
field->col->max_prefix = field->prefix_len; |
| 2519 |
field->col->ord_part = 1; |
| 2520 |
} else if (field->prefix_len == 0) { |
| 2521 |
/* Set the max_prefix for a column to 0 if |
| 2522 |
its prefix length is 0 (for this index) |
| 2523 |
even if it was a part of any other index |
| 2524 |
with some prefix length. */ |
| 2525 |
field->col->max_prefix = 0; |
| 2526 |
} else if (field->col->max_prefix != 0 && |
| 2527 |
field->prefix_len > field->col->max_prefix) { |
| 2528 |
/* Set the max_prefix value based on the |
| 2529 |
prefix_len. */ |
| 2530 |
field->col->max_prefix = field->prefix_len; |
| 2531 |
} |
| 2532 |
ut_ad(field->col->ord_part == 1); |
| 2533 |
} |
| 2534 |
|
| 2535 |
new_index->stat_n_diff_key_vals = static_cast<uint64_t *>(mem_heap_zalloc( |
| 2536 |
new_index->heap, dict_index_get_n_unique(new_index) * |
| 2537 |
sizeof(*new_index->stat_n_diff_key_vals))); |
| 2538 |
|
| 2539 |
new_index->stat_n_sample_sizes = static_cast<uint64_t *>(mem_heap_zalloc( |
| 2540 |
new_index->heap, dict_index_get_n_unique(new_index) * |
| 2541 |
sizeof(*new_index->stat_n_sample_sizes))); |
| 2542 |
|
| 2543 |
new_index->stat_n_non_null_key_vals = static_cast<uint64_t *>(mem_heap_zalloc( |
| 2544 |
new_index->heap, dict_index_get_n_unique(new_index) * |
| 2545 |
sizeof(*new_index->stat_n_non_null_key_vals))); |
| 2546 |
|
| 2547 |
new_index->stat_index_size = 1; |
| 2548 |
new_index->stat_n_leaf_pages = 1; |
| 2549 |
|
| 2550 |
new_index->table = table; |
| 2551 |
new_index->table_name = table->name.m_name; |
| 2552 |
new_index->search_info = btr_search_info_create(new_index->heap); |
| 2553 |
|
| 2554 |
new_index->page = page_no; |
| 2555 |
rw_lock_create(index_tree_rw_lock_key, &new_index->lock, LATCH_ID_INDEX_TREE); |
| 2556 |
|
| 2557 |
/* The conditions listed here correspond to the simplest call-path through |
| 2558 |
rec_get_offsets(). If they are met now, we can cache rec offsets and use the |
| 2559 |
cache as long they are met. We assert before using the rec_cache that they are |
| 2560 |
still met, and clear the rec_cache.offsets when they change. */ |
| 2561 |
if (dict_table_is_comp(table) && !dict_index_has_virtual(index) && |
| 2562 |
(!table->has_instant_cols() && !table->has_row_versions()) && |
| 2563 |
!dict_index_is_spatial(index)) { |
| 2564 |
dict_index_try_cache_rec_offsets(new_index); |
| 2565 |
} else { |
| 2566 |
/* The rules should not prevent caching for intrinsic tables */ |
| 2567 |
ut_ad(!table->is_intrinsic()); |
| 2568 |
} |
| 2569 |
|
| 2570 |
dict_sys_mutex_enter(); |
| 2571 |
|
| 2572 |
/* Add the new index as the last index for the table */ |
| 2573 |
UT_LIST_ADD_LAST(table->indexes, new_index); |
| 2574 |
|
| 2575 |
/* Intrinsic table are not added to dictionary cache instead are |
| 2576 |
cached to session specific thread cache. */ |
| 2577 |
if (!table->is_intrinsic()) { |
| 2578 |
dict_sys->size += mem_heap_get_size(new_index->heap); |
| 2579 |
} |
| 2580 |
|
| 2581 |
dict_sys_mutex_exit(); |
| 2582 |
|
| 2583 |
if (dict_index_has_virtual(index)) { |
| 2584 |
const dict_col_t *col; |
| 2585 |
const dict_v_col_t *vcol; |
| 2586 |
|
| 2587 |
for (ulint i = 0; i < dict_index_get_n_fields(index); i++) { |
| 2588 |
col = index->get_col(i); |
| 2589 |
if (col->is_virtual()) { |
| 2590 |
vcol = reinterpret_cast<const dict_v_col_t *>(col); |
| 2591 |
|
| 2592 |
/* This could be NULL, when we do add virtual |
| 2593 |
column, add index together. We do not need to |
| 2594 |
track this virtual column's index */ |
| 2595 |
if (vcol->v_indexes == nullptr) { |
| 2596 |
continue; |
| 2597 |
} |
| 2598 |
|
| 2599 |
dict_v_idx_list::iterator it; |
| 2600 |
|
| 2601 |
for (it = vcol->v_indexes->begin(); it != vcol->v_indexes->end();) { |
| 2602 |
dict_v_idx_t v_index = *it; |
| 2603 |
if (v_index.index == index) { |
| 2604 |
vcol->v_indexes->erase(it++); |
| 2605 |
} else { |
| 2606 |
it++; |
| 2607 |
} |
| 2608 |
} |
| 2609 |
} |
| 2610 |
} |
| 2611 |
} |
| 2612 |
|
| 2613 |
new_index->instant_cols = false; |
| 2614 |
new_index->row_versions = false; |
| 2615 |
new_index->set_instant_nullable(new_index->n_nullable); |
| 2616 |
|
| 2617 |
if (new_index->is_clustered()) { |
| 2618 |
if (new_index->table->has_row_versions()) { |
| 2619 |
new_index->row_versions = true; |
| 2620 |
} |
| 2621 |
|
| 2622 |
if (new_index->table->has_instant_cols()) { |
| 2623 |
ut_ad(new_index->table->is_upgraded_instant()); |
| 2624 |
new_index->instant_cols = true; |
| 2625 |
const size_t n_instant_fields = new_index->get_instant_fields(); |
| 2626 |
size_t new_n_nullable = |
| 2627 |
new_index->calculate_n_instant_nullable(n_instant_fields); |
| 2628 |
new_index->set_instant_nullable(new_n_nullable); |
| 2629 |
} |
| 2630 |
} |
| 2631 |
|
| 2632 |
dict_mem_index_free(index); |
| 2633 |
|
| 2634 |
return (DB_SUCCESS); |
| 2635 |
} |
| 2636 |
|
| 2637 |
/** Removes an index from the dictionary cache. */ |
| 2638 |
static void dict_index_remove_from_cache_low( |
| 2639 |
dict_table_t *table, /*!< in/out: table */ |
| 2640 |
dict_index_t *index, /*!< in, own: index */ |
| 2641 |
bool lru_evict) /*!< in: true if index being evicted |
| 2642 |
to make room in the table LRU list */ |
| 2643 |
{ |
| 2644 |
ut_ad(table && index); |
| 2645 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 2646 |
ut_ad(index->magic_n == DICT_INDEX_MAGIC_N); |
| 2647 |
ut_ad(dict_sys_mutex_own()); |
| 2648 |
|
| 2649 |
/* No need to acquire the dict_index_t::lock here because |
| 2650 |
there can't be any active operations on this index (or table). */ |
| 2651 |
|
| 2652 |
if (index->online_log) { |
| 2653 |
ut_ad(index->online_status == ONLINE_INDEX_CREATION); |
| 2654 |
row_log_free(index->online_log); |
| 2655 |
} |
| 2656 |
|
| 2657 |
btr_search_await_no_reference(table, index, !lru_evict); |
| 2658 |
|
| 2659 |
rw_lock_free(&index->lock); |
| 2660 |
|
| 2661 |
/* The index is being dropped, remove any compression stats for it. */ |
| 2662 |
if (!lru_evict && DICT_TF_GET_ZIP_SSIZE(index->table->flags) && |
| 2663 |
!index->table->discard_after_ddl) { |
| 2664 |
index_id_t id(index->space, index->id); |
| 2665 |
mutex_enter(&page_zip_stat_per_index_mutex); |
| 2666 |
page_zip_stat_per_index.erase(id); |
| 2667 |
mutex_exit(&page_zip_stat_per_index_mutex); |
| 2668 |
} |
| 2669 |
|
| 2670 |
/* Remove the index from the list of indexes of the table */ |
| 2671 |
UT_LIST_REMOVE(table->indexes, index); |
| 2672 |
|
| 2673 |
/* Remove the index from affected virtual column index list */ |
| 2674 |
if (dict_index_has_virtual(index)) { |
| 2675 |
const dict_col_t *col; |
| 2676 |
const dict_v_col_t *vcol; |
| 2677 |
|
| 2678 |
for (ulint i = 0; i < dict_index_get_n_fields(index); i++) { |
| 2679 |
col = index->get_col(i); |
| 2680 |
if (col->is_virtual()) { |
| 2681 |
vcol = reinterpret_cast<const dict_v_col_t *>(col); |
| 2682 |
|
| 2683 |
/* This could be NULL, when we do add virtual |
| 2684 |
column, add index together. We do not need to |
| 2685 |
track this virtual column's index */ |
| 2686 |
if (vcol->v_indexes == nullptr) { |
| 2687 |
continue; |
| 2688 |
} |
| 2689 |
|
| 2690 |
dict_v_idx_list::iterator it; |
| 2691 |
|
| 2692 |
for (it = vcol->v_indexes->begin(); it != vcol->v_indexes->end(); |
| 2693 |
++it) { |
| 2694 |
dict_v_idx_t v_index = *it; |
| 2695 |
if (v_index.index == index) { |
| 2696 |
vcol->v_indexes->erase(it); |
| 2697 |
break; |
| 2698 |
} |
| 2699 |
} |
| 2700 |
} |
| 2701 |
} |
| 2702 |
} |
| 2703 |
|
| 2704 |
const auto size = mem_heap_get_size(index->heap); |
| 2705 |
|
| 2706 |
ut_ad(!table->is_intrinsic()); |
| 2707 |
ut_ad(dict_sys->size >= size); |
| 2708 |
|
| 2709 |
dict_sys->size -= size; |
| 2710 |
|
| 2711 |
dict_mem_index_free(index); |
| 2712 |
} |
| 2713 |
|
| 2714 |
/** Removes an index from the dictionary cache. */ |
| 2715 |
void dict_index_remove_from_cache(dict_table_t *table, /*!< in/out: table */ |
| 2716 |
dict_index_t *index) /*!< in, own: index */ |
| 2717 |
{ |
| 2718 |
dict_index_remove_from_cache_low(table, index, false); |
| 2719 |
} |
| 2720 |
|
| 2721 |
std::vector<table_id_t> dict_get_all_table_ids() { |
| 2722 |
std::vector<table_id_t> ids; |
| 2723 |
mutex_enter(&dict_sys->mutex); |
| 2724 |
ids.reserve(dict_sys->table_LRU.get_length() + |
| 2725 |
dict_sys->table_non_LRU.get_length()); |
| 2726 |
for (dict_table_t *table : dict_sys->table_LRU) { |
| 2727 |
ids.push_back(table->id); |
| 2728 |
} |
| 2729 |
for (dict_table_t *table : dict_sys->table_non_LRU) { |
| 2730 |
ids.push_back(table->id); |
| 2731 |
} |
| 2732 |
mutex_exit(&dict_sys->mutex); |
| 2733 |
return ids; |
| 2734 |
} |
| 2735 |
|
| 2736 |
/** Duplicate a virtual column information |
| 2737 |
@param[in] v_col virtual column information to duplicate |
| 2738 |
@param[in,out] heap memory heap |
| 2739 |
@return the duplicated virtual column */ |
| 2740 |
static dict_v_col_t *dict_duplicate_v_col(const dict_v_col_t *v_col, |
| 2741 |
mem_heap_t *heap) { |
| 2742 |
dict_v_col_t *new_v_col = |
| 2743 |
static_cast<dict_v_col_t *>(mem_heap_zalloc(heap, sizeof(*v_col))); |
| 2744 |
|
| 2745 |
ut_ad(v_col->v_indexes == nullptr); |
| 2746 |
|
| 2747 |
/* Currently, only m_col and v_indexes would be cared in future use, |
| 2748 |
and v_indexes is always nullptr. So the memcpy can work for it */ |
| 2749 |
memcpy(new_v_col, v_col, sizeof(*v_col)); |
| 2750 |
|
| 2751 |
return (new_v_col); |
| 2752 |
} |
| 2753 |
|
| 2754 |
/** Tries to find column names for the index and sets the col field of the |
| 2755 |
index. |
| 2756 |
@param[in] table table |
| 2757 |
@param[in,out] index index |
| 2758 |
@param[in] add_v new virtual columns added along with an add index call |
| 2759 |
@return true if the column names were found */ |
| 2760 |
static bool dict_index_find_and_set_cols(const dict_table_t *table, |
| 2761 |
dict_index_t *index, |
| 2762 |
const dict_add_v_col_t *add_v) { |
| 2763 |
std::vector<ulint, ut::allocator<ulint>> col_added; |
| 2764 |
std::vector<ulint, ut::allocator<ulint>> v_col_added; |
| 2765 |
|
| 2766 |
ut_ad(table != nullptr && index != nullptr); |
| 2767 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 2768 |
ut_ad(!dict_sys_mutex_own()); |
| 2769 |
|
| 2770 |
for (ulint i = 0; i < index->n_fields; i++) { |
| 2771 |
ulint j; |
| 2772 |
dict_field_t *field = index->get_field(i); |
| 2773 |
|
| 2774 |
for (j = 0; j < table->n_cols; j++) { |
| 2775 |
if (!strcmp(table->get_col_name(j), field->name)) { |
| 2776 |
/* Check if same column is being assigned again |
| 2777 |
which suggest that column has duplicate name. */ |
| 2778 |
bool exists = |
| 2779 |
std::find(col_added.begin(), col_added.end(), j) != col_added.end(); |
| 2780 |
|
| 2781 |
if (exists) { |
| 2782 |
/* Duplicate column found. */ |
| 2783 |
goto dup_err; |
| 2784 |
} |
| 2785 |
|
| 2786 |
field->col = table->get_col(j); |
| 2787 |
|
| 2788 |
col_added.push_back(j); |
| 2789 |
|
| 2790 |
goto found; |
| 2791 |
} |
| 2792 |
} |
| 2793 |
|
| 2794 |
/* Let's check if it is a virtual column */ |
| 2795 |
for (j = 0; j < table->n_v_cols; j++) { |
| 2796 |
if (!strcmp(dict_table_get_v_col_name(table, j), field->name)) { |
| 2797 |
/* Check if same column is being assigned again |
| 2798 |
which suggest that column has duplicate name. */ |
| 2799 |
bool exists = std::find(v_col_added.begin(), v_col_added.end(), j) != |
| 2800 |
v_col_added.end(); |
| 2801 |
|
| 2802 |
if (exists) { |
| 2803 |
/* Duplicate column found. */ |
| 2804 |
break; |
| 2805 |
} |
| 2806 |
|
| 2807 |
field->col = |
| 2808 |
reinterpret_cast<dict_col_t *>(dict_table_get_nth_v_col(table, j)); |
| 2809 |
|
| 2810 |
v_col_added.push_back(j); |
| 2811 |
|
| 2812 |
goto found; |
| 2813 |
} |
| 2814 |
} |
| 2815 |
|
| 2816 |
if (add_v) { |
| 2817 |
for (j = 0; j < add_v->n_v_col; j++) { |
| 2818 |
if (!strcmp(add_v->v_col_name[j], field->name)) { |
| 2819 |
/* Once add_v is not nullptr, it comes from ALTER TABLE. |
| 2820 |
To make sure the index can work after ALTER TABLE path, |
| 2821 |
which may happen when the ALTER TABLE gets rolled back, |
| 2822 |
it is a must to duplicate the virtual column information, |
| 2823 |
in case the passed in object would be freed after ALTER TABLE. */ |
| 2824 |
|
| 2825 |
dict_sys_mutex_enter(); |
| 2826 |
uint64_t old_size = mem_heap_get_size(table->heap); |
| 2827 |
dict_v_col_t *vcol = |
| 2828 |
dict_duplicate_v_col(&add_v->v_col[j], table->heap); |
| 2829 |
field->col = &vcol->m_col; |
| 2830 |
dict_sys->size += mem_heap_get_size(table->heap) - old_size; |
| 2831 |
dict_sys_mutex_exit(); |
| 2832 |
|
| 2833 |
goto found; |
| 2834 |
} |
| 2835 |
} |
| 2836 |
} |
| 2837 |
|
| 2838 |
dup_err: |
| 2839 |
#ifdef UNIV_DEBUG |
| 2840 |
/* It is an error not to find a matching column. */ |
| 2841 |
ib::error(ER_IB_MSG_182) |
| 2842 |
<< "No matching column for " << field->name << " in index " |
| 2843 |
<< index->name << " of table " << table->name; |
| 2844 |
#endif /* UNIV_DEBUG */ |
| 2845 |
return false; |
| 2846 |
|
| 2847 |
found:; |
| 2848 |
} |
| 2849 |
|
| 2850 |
return true; |
| 2851 |
} |
| 2852 |
|
| 2853 |
/** Copies fields contained in index2 to index1. */ |
| 2854 |
static void dict_index_copy(dict_index_t *index1, /*!< in: index to copy to */ |
| 2855 |
dict_index_t *index2, /*!< in: index to copy from */ |
| 2856 |
const dict_table_t *table, /*!< in: table */ |
| 2857 |
ulint start, /*!< in: first position to copy */ |
| 2858 |
ulint end) /*!< in: last position to copy */ |
| 2859 |
{ |
| 2860 |
dict_field_t *field; |
| 2861 |
ulint i; |
| 2862 |
|
| 2863 |
/* Copy fields contained in index2 */ |
| 2864 |
|
| 2865 |
for (i = start; i < end; i++) { |
| 2866 |
field = index2->get_field(i); |
| 2867 |
|
| 2868 |
dict_index_add_col(index1, table, field->col, field->prefix_len, |
| 2869 |
field->is_ascending); |
| 2870 |
} |
| 2871 |
} |
| 2872 |
|
| 2873 |
/** Copies types of fields contained in index to tuple. |
| 2874 |
@param[in,out] tuple Data tuple |
| 2875 |
@param[in] index Index |
| 2876 |
@param[in] n_fields Number of field types to copy */ |
| 2877 |
void dict_index_copy_types(dtuple_t *tuple, const dict_index_t *index, |
| 2878 |
ulint n_fields) { |
| 2879 |
ulint i; |
| 2880 |
|
| 2881 |
if (dict_index_is_ibuf(index)) { |
| 2882 |
/* For IBUF index set field types explicitly. */ |
| 2883 |
for (ulint i = 0; i < n_fields; i++) { |
| 2884 |
dtype_t *dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i)); |
| 2885 |
dtype_set(dfield_type, DATA_BINARY, 0, 0); |
| 2886 |
} |
| 2887 |
|
| 2888 |
return; |
| 2889 |
} |
| 2890 |
|
| 2891 |
for (i = 0; i < n_fields; i++) { |
| 2892 |
const dict_field_t *ifield; |
| 2893 |
dtype_t *dfield_type; |
| 2894 |
|
| 2895 |
ifield = index->get_field(i); |
| 2896 |
dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i)); |
| 2897 |
ut_ad(!ifield->col->is_virtual() || !index->is_clustered()); |
| 2898 |
ifield->col->copy_type(dfield_type); |
| 2899 |
/* Field for materialized virtual column is not itself virtual. */ |
| 2900 |
dfield_type->prtype &= ~DATA_VIRTUAL; |
| 2901 |
if (dict_index_is_spatial(index) && |
| 2902 |
DATA_GEOMETRY_MTYPE(dfield_type->mtype)) { |
| 2903 |
dfield_type->prtype |= DATA_GIS_MBR; |
| 2904 |
} |
| 2905 |
} |
| 2906 |
} |
| 2907 |
|
| 2908 |
/** Copies types of virtual columns contained in table to tuple and sets all |
| 2909 |
fields of the tuple to the SQL NULL value. This function should |
| 2910 |
be called right after dtuple_create(). |
| 2911 |
@param[in,out] tuple data tuple |
| 2912 |
@param[in] table table |
| 2913 |
*/ |
| 2914 |
void dict_table_copy_v_types(dtuple_t *tuple, const dict_table_t *table) { |
| 2915 |
/* tuple could have more virtual columns than existing table, |
| 2916 |
if we are calling this for creating index along with adding |
| 2917 |
virtual columns */ |
| 2918 |
ulint n_fields = std::min(dtuple_get_n_v_fields(tuple), |
| 2919 |
static_cast<ulint>(table->n_v_def)); |
| 2920 |
|
| 2921 |
for (ulint i = 0; i < n_fields; i++) { |
| 2922 |
dfield_t *dfield = dtuple_get_nth_v_field(tuple, i); |
| 2923 |
dtype_t *dtype = dfield_get_type(dfield); |
| 2924 |
|
| 2925 |
dfield_set_null(dfield); |
| 2926 |
dict_table_get_nth_v_col(table, i)->m_col.copy_type(dtype); |
| 2927 |
} |
| 2928 |
} |
| 2929 |
/** Copies types of columns contained in table to tuple and sets all |
| 2930 |
fields of the tuple to the SQL NULL value. This function should |
| 2931 |
be called right after dtuple_create(). */ |
| 2932 |
void dict_table_copy_types(dtuple_t *tuple, /*!< in/out: data tuple */ |
| 2933 |
const dict_table_t *table) /*!< in: table */ |
| 2934 |
{ |
| 2935 |
ulint i; |
| 2936 |
|
| 2937 |
for (i = 0; i < dtuple_get_n_fields(tuple); i++) { |
| 2938 |
dfield_t *dfield = dtuple_get_nth_field(tuple, i); |
| 2939 |
dtype_t *dtype = dfield_get_type(dfield); |
| 2940 |
|
| 2941 |
dfield_set_null(dfield); |
| 2942 |
table->get_col(i)->copy_type(dtype); |
| 2943 |
} |
| 2944 |
|
| 2945 |
dict_table_copy_v_types(tuple, table); |
| 2946 |
} |
| 2947 |
|
| 2948 |
void dict_table_wait_for_bg_threads_to_exit(dict_table_t *table, |
| 2949 |
std::chrono::microseconds delay) { |
| 2950 |
fts_t *fts = table->fts; |
| 2951 |
|
| 2952 |
ut_ad(mutex_own(&fts->bg_threads_mutex)); |
| 2953 |
|
| 2954 |
while (fts->bg_threads > 0) { |
| 2955 |
mutex_exit(&fts->bg_threads_mutex); |
| 2956 |
|
| 2957 |
std::this_thread::sleep_for(delay); |
| 2958 |
|
| 2959 |
mutex_enter(&fts->bg_threads_mutex); |
| 2960 |
} |
| 2961 |
} |
| 2962 |
|
| 2963 |
/** Builds the internal dictionary cache representation for a clustered |
| 2964 |
index, containing also system fields not defined by the user. |
| 2965 |
@return own: the internal representation of the clustered index */ |
| 2966 |
static dict_index_t *dict_index_build_internal_clust( |
| 2967 |
const dict_table_t *table, /*!< in: table */ |
| 2968 |
dict_index_t *index) /*!< in: user representation of |
| 2969 |
a clustered index */ |
| 2970 |
{ |
| 2971 |
ut_ad(table && index); |
| 2972 |
ut_ad(index->is_clustered()); |
| 2973 |
ut_ad(!dict_index_is_ibuf(index)); |
| 2974 |
|
| 2975 |
ut_ad(!dict_sys_mutex_own()); |
| 2976 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 2977 |
|
| 2978 |
uint32_t total_cols_in_table = table->get_total_cols(); |
| 2979 |
|
| 2980 |
/* Create a new index object with certainly enough fields */ |
| 2981 |
dict_index_t *new_index = |
| 2982 |
dict_mem_index_create(table->name.m_name, index->name, table->space, |
| 2983 |
index->type, index->n_fields + total_cols_in_table); |
| 2984 |
|
| 2985 |
/* Copy other relevant data from the old index struct to the new |
| 2986 |
struct: it inherits the values */ |
| 2987 |
|
| 2988 |
new_index->n_user_defined_cols = index->n_fields; |
| 2989 |
|
| 2990 |
new_index->id = index->id; |
| 2991 |
|
| 2992 |
/* Copy the fields of index */ |
| 2993 |
dict_index_copy(new_index, index, table, 0, index->n_fields); |
| 2994 |
|
| 2995 |
if (dict_index_is_unique(index)) { |
| 2996 |
/* Only the fields defined so far are needed to identify |
| 2997 |
the index entry uniquely */ |
| 2998 |
|
| 2999 |
new_index->n_uniq = new_index->n_def; |
| 3000 |
} else { |
| 3001 |
/* Also the row id is needed to identify the entry */ |
| 3002 |
new_index->n_uniq = 1 + new_index->n_def; |
| 3003 |
} |
| 3004 |
|
| 3005 |
new_index->trx_id_offset = 0; |
| 3006 |
|
| 3007 |
uint16_t n_fields_processed = 0; |
| 3008 |
|
| 3009 |
/* Add phy_pos for fields defined so far */ |
| 3010 |
while (n_fields_processed < new_index->n_def) { |
| 3011 |
dict_field_t *field = new_index->get_field(n_fields_processed); |
| 3012 |
dict_col_t *col = field->col; |
| 3013 |
if (!table->has_row_versions()) { |
| 3014 |
if (field->prefix_len != 0) { |
| 3015 |
/* This column prefix is used in PK */ |
| 3016 |
col->set_prefix_phy_pos(n_fields_processed); |
| 3017 |
} else { |
| 3018 |
col->set_phy_pos(n_fields_processed); |
| 3019 |
} |
| 3020 |
} else { |
| 3021 |
ut_ad(col->get_phy_pos() != UINT32_UNDEFINED); |
| 3022 |
} |
| 3023 |
n_fields_processed++; |
| 3024 |
} |
| 3025 |
|
| 3026 |
auto set_phy_pos = [&](const dict_col_t *cmp_col) { |
| 3027 |
dict_col_t *col = |
| 3028 |
const_cast<dict_col_t *>(new_index->get_col(n_fields_processed)); |
| 3029 |
ut_a(col == cmp_col); |
| 3030 |
|
| 3031 |
if (!table->has_row_versions()) { |
| 3032 |
if (col->get_phy_pos() != UINT32_UNDEFINED && col->has_prefix_phy_pos()) { |
| 3033 |
/* This column prefix is used in PK */ |
| 3034 |
ut_ad(new_index->get_col(col->get_prefix_phy_pos()) == col); |
| 3035 |
col->set_col_phy_pos(n_fields_processed); |
| 3036 |
} else { |
| 3037 |
col->set_phy_pos(n_fields_processed); |
| 3038 |
} |
| 3039 |
} else { |
| 3040 |
ut_ad(col->get_phy_pos() != UINT32_UNDEFINED); |
| 3041 |
} |
| 3042 |
|
| 3043 |
n_fields_processed++; |
| 3044 |
}; |
| 3045 |
|
| 3046 |
/* Add system columns */ |
| 3047 |
|
| 3048 |
size_t trx_id_pos = new_index->n_def; |
| 3049 |
|
| 3050 |
/* Add ROW ID */ |
| 3051 |
if (!dict_index_is_unique(index)) { |
| 3052 |
dict_index_add_col(new_index, table, table->get_sys_col(DATA_ROW_ID), 0, |
| 3053 |
true); |
| 3054 |
set_phy_pos(table->get_sys_col(DATA_ROW_ID)); |
| 3055 |
trx_id_pos++; |
| 3056 |
} |
| 3057 |
|
| 3058 |
/* Add TRX ID */ |
| 3059 |
dict_index_add_col(new_index, table, table->get_sys_col(DATA_TRX_ID), 0, |
| 3060 |
true); |
| 3061 |
set_phy_pos(table->get_sys_col(DATA_TRX_ID)); |
| 3062 |
|
| 3063 |
for (size_t i = 0; i < trx_id_pos; i++) { |
| 3064 |
ulint fixed_size = |
| 3065 |
new_index->get_col(i)->get_fixed_size(dict_table_is_comp(table)); |
| 3066 |
|
| 3067 |
if (fixed_size == 0) { |
| 3068 |
new_index->trx_id_offset = 0; |
| 3069 |
|
| 3070 |
break; |
| 3071 |
} |
| 3072 |
|
| 3073 |
dict_field_t *field = new_index->get_field(i); |
| 3074 |
if (field->prefix_len > 0) { |
| 3075 |
new_index->trx_id_offset = 0; |
| 3076 |
|
| 3077 |
break; |
| 3078 |
} |
| 3079 |
|
| 3080 |
/* Add fixed_size to new_index->trx_id_offset. |
| 3081 |
Because the latter is a bit-field, an overflow |
| 3082 |
can theoretically occur. Check for it. */ |
| 3083 |
fixed_size += new_index->trx_id_offset; |
| 3084 |
|
| 3085 |
new_index->trx_id_offset = fixed_size; |
| 3086 |
|
| 3087 |
if (new_index->trx_id_offset != fixed_size) { |
| 3088 |
/* Overflow. Pretend that this is a |
| 3089 |
variable-length PRIMARY KEY. */ |
| 3090 |
ut_d(ut_error); |
| 3091 |
ut_o(new_index->trx_id_offset = 0); |
| 3092 |
ut_o(break); |
| 3093 |
} |
| 3094 |
} |
| 3095 |
|
| 3096 |
/* Add ROLL PTR. UNDO logging is turned-off for intrinsic table and so |
| 3097 |
DATA_ROLL_PTR system columns are not added as default system columns to such |
| 3098 |
tables. */ |
| 3099 |
if (!table->is_intrinsic()) { |
| 3100 |
dict_index_add_col(new_index, table, table->get_sys_col(DATA_ROLL_PTR), 0, |
| 3101 |
true); |
| 3102 |
set_phy_pos(table->get_sys_col(DATA_ROLL_PTR)); |
| 3103 |
} |
| 3104 |
|
| 3105 |
/* Remember the table columns already contained in new_index */ |
| 3106 |
bool *indexed = static_cast<bool *>(ut::zalloc_withkey( |
| 3107 |
UT_NEW_THIS_FILE_PSI_KEY, total_cols_in_table * sizeof *indexed)); |
| 3108 |
|
| 3109 |
/* Mark the table columns already contained in new_index */ |
| 3110 |
for (size_t i = 0; i < new_index->n_def; i++) { |
| 3111 |
dict_field_t *field = new_index->get_field(i); |
| 3112 |
|
| 3113 |
/* If there is only a prefix of the column in the index |
| 3114 |
field, do not mark the column as contained in the index */ |
| 3115 |
|
| 3116 |
if (field->prefix_len == 0) { |
| 3117 |
indexed[field->col->ind] = true; |
| 3118 |
} |
| 3119 |
} |
| 3120 |
|
| 3121 |
/* Add to new_index non-system columns of table not yet included there */ |
| 3122 |
for (size_t i = 0; i < table->get_n_user_cols(); i++) { |
| 3123 |
dict_col_t *col = table->get_col(i); |
| 3124 |
ut_ad(col->mtype != DATA_SYS); |
| 3125 |
|
| 3126 |
if (indexed[col->ind]) { |
| 3127 |
continue; |
| 3128 |
} |
| 3129 |
|
| 3130 |
dict_index_add_col(new_index, table, col, 0, true); |
| 3131 |
set_phy_pos(new_index->get_col(n_fields_processed)); |
| 3132 |
} |
| 3133 |
|
| 3134 |
/* Add INSTANT DROP column */ |
| 3135 |
for (size_t i = table->n_cols; i < total_cols_in_table; i++) { |
| 3136 |
dict_col_t *col = table->get_col(i); |
| 3137 |
ut_ad(col->mtype != DATA_SYS); |
| 3138 |
|
| 3139 |
ut_ad(!indexed[col->ind]); |
| 3140 |
dict_index_add_col(new_index, table, col, 0, true); |
| 3141 |
/* Physical position must have already been set */ |
| 3142 |
ut_ad(col->get_phy_pos() != UINT32_UNDEFINED); |
| 3143 |
n_fields_processed++; |
| 3144 |
} |
| 3145 |
|
| 3146 |
ut_ad(n_fields_processed == new_index->n_def); |
| 3147 |
|
| 3148 |
ut::free(indexed); |
| 3149 |
|
| 3150 |
new_index->create_nullables(table->current_row_version); |
| 3151 |
|
| 3152 |
if (table->has_row_versions()) { |
| 3153 |
new_index->create_fields_array(); |
| 3154 |
} else { |
| 3155 |
/* Table with no row version are considered of version 0 */ |
| 3156 |
ut_a(new_index->get_nullable_in_version(0) == new_index->n_nullable); |
| 3157 |
} |
| 3158 |
|
| 3159 |
ut_ad(UT_LIST_GET_LEN(table->indexes) == 0); |
| 3160 |
|
| 3161 |
new_index->cached = true; |
| 3162 |
|
| 3163 |
return (new_index); |
| 3164 |
} |
| 3165 |
|
| 3166 |
/** Builds the internal dictionary cache representation for a non-clustered |
| 3167 |
index, containing also system fields not defined by the user. |
| 3168 |
@return own: the internal representation of the non-clustered index */ |
| 3169 |
static dict_index_t *dict_index_build_internal_non_clust( |
| 3170 |
const dict_table_t *table, /*!< in: table */ |
| 3171 |
dict_index_t *index) /*!< in: user representation of |
| 3172 |
a non-clustered index */ |
| 3173 |
{ |
| 3174 |
dict_field_t *field; |
| 3175 |
dict_index_t *new_index; |
| 3176 |
dict_index_t *clust_index; |
| 3177 |
ulint i; |
| 3178 |
bool *indexed; |
| 3179 |
|
| 3180 |
ut_ad(table && index); |
| 3181 |
ut_ad(!index->is_clustered()); |
| 3182 |
ut_ad(!dict_index_is_ibuf(index)); |
| 3183 |
ut_ad(!dict_sys_mutex_own()); |
| 3184 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 3185 |
|
| 3186 |
/* The clustered index should be the first in the list of indexes */ |
| 3187 |
clust_index = UT_LIST_GET_FIRST(table->indexes); |
| 3188 |
|
| 3189 |
ut_ad(clust_index); |
| 3190 |
ut_ad(clust_index->is_clustered()); |
| 3191 |
ut_ad(!dict_index_is_ibuf(clust_index)); |
| 3192 |
|
| 3193 |
/* Create a new index */ |
| 3194 |
new_index = dict_mem_index_create(table->name.m_name, index->name, |
| 3195 |
index->space, index->type, |
| 3196 |
index->n_fields + 1 + clust_index->n_uniq); |
| 3197 |
|
| 3198 |
/* Copy other relevant data from the old index |
| 3199 |
struct to the new struct: it inherits the values */ |
| 3200 |
|
| 3201 |
new_index->n_user_defined_cols = index->n_fields; |
| 3202 |
|
| 3203 |
new_index->id = index->id; |
| 3204 |
|
| 3205 |
/* Copy fields from index to new_index */ |
| 3206 |
dict_index_copy(new_index, index, table, 0, index->n_fields); |
| 3207 |
|
| 3208 |
/* Remember the table columns already contained in new_index */ |
| 3209 |
indexed = static_cast<bool *>(ut::zalloc_withkey( |
| 3210 |
UT_NEW_THIS_FILE_PSI_KEY, table->n_cols * sizeof *indexed)); |
| 3211 |
|
| 3212 |
/* Mark the table columns already contained in new_index */ |
| 3213 |
for (i = 0; i < new_index->n_def; i++) { |
| 3214 |
field = new_index->get_field(i); |
| 3215 |
|
| 3216 |
if (field->col->is_virtual()) { |
| 3217 |
continue; |
| 3218 |
} |
| 3219 |
|
| 3220 |
/* If there is only a prefix of the column in the index |
| 3221 |
field, do not mark the column as contained in the index */ |
| 3222 |
|
| 3223 |
if (field->prefix_len == 0) { |
| 3224 |
indexed[field->col->ind] = true; |
| 3225 |
} |
| 3226 |
} |
| 3227 |
|
| 3228 |
/* Add to new_index the columns necessary to determine the clustered |
| 3229 |
index entry uniquely */ |
| 3230 |
|
| 3231 |
for (i = 0; i < clust_index->n_uniq; i++) { |
| 3232 |
field = clust_index->get_field(i); |
| 3233 |
|
| 3234 |
if (!indexed[field->col->ind]) { |
| 3235 |
dict_index_add_col(new_index, table, field->col, field->prefix_len, |
| 3236 |
field->is_ascending); |
| 3237 |
} else if (dict_index_is_spatial(index)) { |
| 3238 |
/*For spatial index, we still need to add the |
| 3239 |
field to index. */ |
| 3240 |
dict_index_add_col(new_index, table, field->col, field->prefix_len, |
| 3241 |
field->is_ascending); |
| 3242 |
} |
| 3243 |
} |
| 3244 |
|
| 3245 |
ut::free(indexed); |
| 3246 |
|
| 3247 |
if (dict_index_is_unique(index)) { |
| 3248 |
new_index->n_uniq = index->n_fields; |
| 3249 |
} else { |
| 3250 |
new_index->n_uniq = new_index->n_def; |
| 3251 |
} |
| 3252 |
|
| 3253 |
/* Set the n_fields value in new_index to the actual defined |
| 3254 |
number of fields */ |
| 3255 |
|
| 3256 |
new_index->n_fields = new_index->n_def; |
| 3257 |
|
| 3258 |
new_index->cached = true; |
| 3259 |
|
| 3260 |
return (new_index); |
| 3261 |
} |
| 3262 |
|
| 3263 |
/*********************************************************************** |
| 3264 |
Builds the internal dictionary cache representation for an FTS index. |
| 3265 |
@return own: the internal representation of the FTS index */ |
| 3266 |
static dict_index_t *dict_index_build_internal_fts( |
| 3267 |
dict_table_t *table, /*!< in: table */ |
| 3268 |
dict_index_t *index) /*!< in: user representation of an FTS index */ |
| 3269 |
{ |
| 3270 |
dict_index_t *new_index; |
| 3271 |
|
| 3272 |
ut_ad(table && index); |
| 3273 |
ut_ad(index->type == DICT_FTS); |
| 3274 |
ut_ad(!dict_sys_mutex_own()); |
| 3275 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 3276 |
|
| 3277 |
/* Create a new index */ |
| 3278 |
new_index = dict_mem_index_create(table->name.m_name, index->name, |
| 3279 |
index->space, index->type, index->n_fields); |
| 3280 |
|
| 3281 |
/* Copy other relevant data from the old index struct to the new |
| 3282 |
struct: it inherits the values */ |
| 3283 |
|
| 3284 |
new_index->n_user_defined_cols = index->n_fields; |
| 3285 |
|
| 3286 |
new_index->id = index->id; |
| 3287 |
|
| 3288 |
/* Copy fields from index to new_index */ |
| 3289 |
dict_index_copy(new_index, index, table, 0, index->n_fields); |
| 3290 |
|
| 3291 |
new_index->n_uniq = 0; |
| 3292 |
new_index->cached = true; |
| 3293 |
|
| 3294 |
if (table->fts->cache == nullptr) { |
| 3295 |
table->fts->cache = fts_cache_create(table); |
| 3296 |
} |
| 3297 |
|
| 3298 |
rw_lock_x_lock(&table->fts->cache->init_lock, UT_LOCATION_HERE); |
| 3299 |
/* Notify the FTS cache about this index. */ |
| 3300 |
fts_cache_index_cache_create(table, new_index); |
| 3301 |
rw_lock_x_unlock(&table->fts->cache->init_lock); |
| 3302 |
|
| 3303 |
return (new_index); |
| 3304 |
} |
| 3305 |
/*====================== FOREIGN KEY PROCESSING ========================*/ |
| 3306 |
|
| 3307 |
/** Checks if a table is referenced by foreign keys. |
| 3308 |
@return true if table is referenced by a foreign key */ |
| 3309 |
bool dict_table_is_referenced_by_foreign_key( |
| 3310 |
const dict_table_t *table) /*!< in: InnoDB table */ |
| 3311 |
{ |
| 3312 |
return (!table->referenced_set.empty()); |
| 3313 |
} |
| 3314 |
|
| 3315 |
/** Removes a foreign constraint struct from the dictionary cache. */ |
| 3316 |
void dict_foreign_remove_from_cache( |
| 3317 |
dict_foreign_t *foreign) /*!< in, own: foreign constraint */ |
| 3318 |
{ |
| 3319 |
ut_ad(dict_sys_mutex_own()); |
| 3320 |
ut_a(foreign); |
| 3321 |
|
| 3322 |
if (foreign->referenced_table != nullptr) { |
| 3323 |
foreign->referenced_table->referenced_set.erase(foreign); |
| 3324 |
} |
| 3325 |
|
| 3326 |
if (foreign->foreign_table != nullptr) { |
| 3327 |
foreign->foreign_table->foreign_set.erase(foreign); |
| 3328 |
} |
| 3329 |
|
| 3330 |
dict_foreign_free(foreign); |
| 3331 |
} |
| 3332 |
|
| 3333 |
/** Looks for the foreign constraint from the foreign and referenced lists |
| 3334 |
of a table. |
| 3335 |
@return foreign constraint */ |
| 3336 |
static dict_foreign_t *dict_foreign_find( |
| 3337 |
dict_table_t *table, /*!< in: table object */ |
| 3338 |
dict_foreign_t *foreign) /*!< in: foreign constraint */ |
| 3339 |
{ |
| 3340 |
ut_ad(dict_sys_mutex_own()); |
| 3341 |
|
| 3342 |
ut_ad(dict_foreign_set_validate(table->foreign_set)); |
| 3343 |
ut_ad(dict_foreign_set_validate(table->referenced_set)); |
| 3344 |
|
| 3345 |
dict_foreign_set::iterator it = table->foreign_set.find(foreign); |
| 3346 |
|
| 3347 |
if (it != table->foreign_set.end()) { |
| 3348 |
return (*it); |
| 3349 |
} |
| 3350 |
|
| 3351 |
it = table->referenced_set.find(foreign); |
| 3352 |
|
| 3353 |
if (it != table->referenced_set.end()) { |
| 3354 |
return (*it); |
| 3355 |
} |
| 3356 |
|
| 3357 |
return (nullptr); |
| 3358 |
} |
| 3359 |
|
| 3360 |
/** Tries to find an index whose first fields are the columns in the array, |
| 3361 |
in the same order and is not marked for deletion and is not the same |
| 3362 |
as types_idx. |
| 3363 |
@return matching index, NULL if not found */ |
| 3364 |
dict_index_t *dict_foreign_find_index( |
| 3365 |
const dict_table_t *table, /*!< in: table */ |
| 3366 |
const char **col_names, |
| 3367 |
/*!< in: column names, or NULL |
| 3368 |
to use table->col_names */ |
| 3369 |
const char **columns, /*!< in: array of column names */ |
| 3370 |
ulint n_cols, /*!< in: number of columns */ |
| 3371 |
const dict_index_t *types_idx, |
| 3372 |
/*!< in: NULL or an index |
| 3373 |
whose types the column types |
| 3374 |
must match */ |
| 3375 |
bool check_charsets, |
| 3376 |
/*!< in: whether to check |
| 3377 |
charsets. only has an effect |
| 3378 |
if types_idx != NULL */ |
| 3379 |
ulint check_null) |
| 3380 |
/*!< in: nonzero if none of |
| 3381 |
the columns must be declared |
| 3382 |
NOT NULL */ |
| 3383 |
{ |
| 3384 |
const dict_index_t *index; |
| 3385 |
|
| 3386 |
ut_ad(dict_sys_mutex_own()); |
| 3387 |
|
| 3388 |
index = table->first_index(); |
| 3389 |
|
| 3390 |
while (index != nullptr) { |
| 3391 |
if (types_idx != index && !(index->type & DICT_FTS) && |
| 3392 |
!dict_index_is_spatial(index) && !index->to_be_dropped && |
| 3393 |
(!(index->uncommitted && |
| 3394 |
((index->online_status == ONLINE_INDEX_ABORTED_DROPPED) || |
| 3395 |
(index->online_status == ONLINE_INDEX_ABORTED)))) && |
| 3396 |
dict_foreign_qualify_index(table, col_names, columns, n_cols, index, |
| 3397 |
types_idx, check_charsets, check_null)) { |
| 3398 |
return const_cast<dict_index_t *>(index); |
| 3399 |
} |
| 3400 |
|
| 3401 |
index = index->next(); |
| 3402 |
} |
| 3403 |
|
| 3404 |
return (nullptr); |
| 3405 |
} |
| 3406 |
|
| 3407 |
/** Report an error in a foreign key definition. */ |
| 3408 |
static void dict_foreign_error_report_low( |
| 3409 |
FILE *file, /*!< in: output stream */ |
| 3410 |
const char *name) /*!< in: table name */ |
| 3411 |
{ |
| 3412 |
rewind(file); |
| 3413 |
ut_print_timestamp(file); |
| 3414 |
fprintf(file, " Error in foreign key constraint of table %s:\n", name); |
| 3415 |
} |
| 3416 |
|
| 3417 |
/** Report an error in a foreign key definition. */ |
| 3418 |
static void dict_foreign_error_report( |
| 3419 |
FILE *file, /*!< in: output stream */ |
| 3420 |
dict_foreign_t *fk, /*!< in: foreign key constraint */ |
| 3421 |
const char *msg) /*!< in: the error message */ |
| 3422 |
{ |
| 3423 |
mutex_enter(&dict_foreign_err_mutex); |
| 3424 |
dict_foreign_error_report_low(file, fk->foreign_table_name); |
| 3425 |
fputs(msg, file); |
| 3426 |
fputs(" Constraint:\n", file); |
| 3427 |
dict_print_info_on_foreign_key_in_create_format(file, nullptr, fk, true); |
| 3428 |
putc('\n', file); |
| 3429 |
if (fk->foreign_index) { |
| 3430 |
fprintf(file, |
| 3431 |
"The index in the foreign key in table is" |
| 3432 |
" %s\n%s\n", |
| 3433 |
fk->foreign_index->name(), FOREIGN_KEY_CONSTRAINTS_MSG); |
| 3434 |
} |
| 3435 |
mutex_exit(&dict_foreign_err_mutex); |
| 3436 |
} |
| 3437 |
|
| 3438 |
/** Adds a foreign key constraint object to the dictionary cache. May free |
| 3439 |
the object if there already is an object with the same identifier in. |
| 3440 |
At least one of the foreign table and the referenced table must already |
| 3441 |
be in the dictionary cache! |
| 3442 |
@return DB_SUCCESS or error code */ |
| 3443 |
dberr_t dict_foreign_add_to_cache(dict_foreign_t *foreign, |
| 3444 |
/*!< in, own: foreign key constraint */ |
| 3445 |
const char **col_names, |
| 3446 |
/*!< in: column names, or NULL to use |
| 3447 |
foreign->foreign_table->col_names */ |
| 3448 |
bool check_charsets, |
| 3449 |
/*!< in: whether to check charset |
| 3450 |
compatibility */ |
| 3451 |
bool can_free_fk, |
| 3452 |
/*!< in: whether free existing FK */ |
| 3453 |
dict_err_ignore_t ignore_err) |
| 3454 |
/*!< in: error to be ignored */ |
| 3455 |
{ |
| 3456 |
dict_table_t *for_table; |
| 3457 |
dict_table_t *ref_table; |
| 3458 |
dict_foreign_t *for_in_cache = nullptr; |
| 3459 |
dict_index_t *index; |
| 3460 |
bool added_to_referenced_list = false; |
| 3461 |
FILE *ef = dict_foreign_err_file; |
| 3462 |
|
| 3463 |
DBUG_TRACE; |
| 3464 |
DBUG_PRINT("dict_foreign_add_to_cache", ("id: %s", foreign->id)); |
| 3465 |
|
| 3466 |
ut_ad(dict_sys_mutex_own()); |
| 3467 |
|
| 3468 |
for_table = |
| 3469 |
dict_table_check_if_in_cache_low(foreign->foreign_table_name_lookup); |
| 3470 |
|
| 3471 |
ref_table = |
| 3472 |
dict_table_check_if_in_cache_low(foreign->referenced_table_name_lookup); |
| 3473 |
ut_a(for_table || ref_table); |
| 3474 |
|
| 3475 |
if (for_table) { |
| 3476 |
for_in_cache = dict_foreign_find(for_table, foreign); |
| 3477 |
} |
| 3478 |
|
| 3479 |
if (!for_in_cache && ref_table) { |
| 3480 |
for_in_cache = dict_foreign_find(ref_table, foreign); |
| 3481 |
} |
| 3482 |
|
| 3483 |
if (for_in_cache && for_in_cache != foreign) { |
| 3484 |
/* Free the foreign object */ |
| 3485 |
dict_foreign_free(foreign); |
| 3486 |
} else { |
| 3487 |
for_in_cache = foreign; |
| 3488 |
} |
| 3489 |
|
| 3490 |
if (ref_table && !for_in_cache->referenced_table) { |
| 3491 |
index = dict_foreign_find_index( |
| 3492 |
ref_table, nullptr, for_in_cache->referenced_col_names, |
| 3493 |
for_in_cache->n_fields, for_in_cache->foreign_index, check_charsets, |
| 3494 |
false); |
| 3495 |
|
| 3496 |
if (index == nullptr && !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) { |
| 3497 |
dict_foreign_error_report(ef, for_in_cache, |
| 3498 |
"there is no index in referenced table" |
| 3499 |
" which would contain\n" |
| 3500 |
"the columns as the first columns," |
| 3501 |
" or the data types in the\n" |
| 3502 |
"referenced table do not match" |
| 3503 |
" the ones in table."); |
| 3504 |
|
| 3505 |
if (for_in_cache == foreign && can_free_fk) { |
| 3506 |
mem_heap_free(foreign->heap); |
| 3507 |
} |
| 3508 |
|
| 3509 |
return DB_CANNOT_ADD_CONSTRAINT; |
| 3510 |
} |
| 3511 |
|
| 3512 |
for_in_cache->referenced_table = ref_table; |
| 3513 |
for_in_cache->referenced_index = index; |
| 3514 |
|
| 3515 |
std::pair<dict_foreign_set::iterator, bool> ret = |
| 3516 |
ref_table->referenced_set.insert(for_in_cache); |
| 3517 |
|
| 3518 |
ut_a(ret.second); /* second is true if the insertion |
| 3519 |
took place */ |
| 3520 |
added_to_referenced_list = true; |
| 3521 |
} |
| 3522 |
|
| 3523 |
if (for_table && !for_in_cache->foreign_table) { |
| 3524 |
index = dict_foreign_find_index( |
| 3525 |
for_table, col_names, for_in_cache->foreign_col_names, |
| 3526 |
for_in_cache->n_fields, for_in_cache->referenced_index, check_charsets, |
| 3527 |
for_in_cache->type & (DICT_FOREIGN_ON_DELETE_SET_NULL | |
| 3528 |
DICT_FOREIGN_ON_UPDATE_SET_NULL)); |
| 3529 |
|
| 3530 |
if (index == nullptr && !(ignore_err & DICT_ERR_IGNORE_FK_NOKEY)) { |
| 3531 |
dict_foreign_error_report(ef, for_in_cache, |
| 3532 |
"there is no index in the table" |
| 3533 |
" which would contain\n" |
| 3534 |
"the columns as the first columns," |
| 3535 |
" or the data types in the\n" |
| 3536 |
"table do not match" |
| 3537 |
" the ones in the referenced table\n" |
| 3538 |
"or one of the ON ... SET NULL columns" |
| 3539 |
" is declared NOT NULL."); |
| 3540 |
|
| 3541 |
if (for_in_cache == foreign) { |
| 3542 |
if (added_to_referenced_list) { |
| 3543 |
const dict_foreign_set::size_type n = |
| 3544 |
ref_table->referenced_set.erase(for_in_cache); |
| 3545 |
|
| 3546 |
ut_a(n == 1); /* the number of |
| 3547 |
elements removed must |
| 3548 |
be one */ |
| 3549 |
} |
| 3550 |
mem_heap_free(foreign->heap); |
| 3551 |
} |
| 3552 |
|
| 3553 |
return DB_CANNOT_ADD_CONSTRAINT; |
| 3554 |
} |
| 3555 |
|
| 3556 |
for_in_cache->foreign_table = for_table; |
| 3557 |
for_in_cache->foreign_index = index; |
| 3558 |
|
| 3559 |
std::pair<dict_foreign_set::iterator, bool> ret = |
| 3560 |
for_table->foreign_set.insert(for_in_cache); |
| 3561 |
|
| 3562 |
ut_a(ret.second); /* second is true if the insertion |
| 3563 |
took place */ |
| 3564 |
} |
| 3565 |
|
| 3566 |
/* We need to move the table to the non-LRU end of the table LRU |
| 3567 |
list. Otherwise it will be evicted from the cache. */ |
| 3568 |
|
| 3569 |
if (ref_table != nullptr) { |
| 3570 |
dict_table_prevent_eviction(ref_table); |
| 3571 |
} |
| 3572 |
|
| 3573 |
if (for_table != nullptr) { |
| 3574 |
dict_table_prevent_eviction(for_table); |
| 3575 |
} |
| 3576 |
|
| 3577 |
ut_ad(dict_lru_validate()); |
| 3578 |
return DB_SUCCESS; |
| 3579 |
} |
| 3580 |
|
| 3581 |
/** Finds the highest [number] for foreign key constraints of the table. Looks |
| 3582 |
only at the >= 4.0.18-format id's, which are of the form |
| 3583 |
databasename/tablename_ibfk_[number]. |
| 3584 |
TODO: Remove this function once in-place ALTER TABLE code is |
| 3585 |
updated to avoid its use. |
| 3586 |
@return highest number, 0 if table has no new format foreign key constraints */ |
| 3587 |
ulint dict_table_get_highest_foreign_id( |
| 3588 |
dict_table_t *table) /*!< in: table in the dictionary memory cache */ |
| 3589 |
{ |
| 3590 |
dict_foreign_t *foreign; |
| 3591 |
char *endp; |
| 3592 |
ulint biggest_id = 0; |
| 3593 |
ulint id; |
| 3594 |
ulint len; |
| 3595 |
|
| 3596 |
DBUG_TRACE; |
| 3597 |
|
| 3598 |
ut_a(table); |
| 3599 |
|
| 3600 |
len = ut_strlen(table->name.m_name); |
| 3601 |
|
| 3602 |
for (dict_foreign_set::iterator it = table->foreign_set.begin(); |
| 3603 |
it != table->foreign_set.end(); ++it) { |
| 3604 |
foreign = *it; |
| 3605 |
|
| 3606 |
if (ut_strlen(foreign->id) > ((sizeof dict_ibfk) - 1) + len && |
| 3607 |
0 == ut_memcmp(foreign->id, table->name.m_name, len) && |
| 3608 |
0 == ut_memcmp(foreign->id + len, dict_ibfk, (sizeof dict_ibfk) - 1) && |
| 3609 |
foreign->id[len + ((sizeof dict_ibfk) - 1)] != '0') { |
| 3610 |
/* It is of the >= 4.0.18 format */ |
| 3611 |
|
| 3612 |
id = strtoul(foreign->id + len + ((sizeof dict_ibfk) - 1), &endp, 10); |
| 3613 |
if (*endp == '\0') { |
| 3614 |
ut_a(id != biggest_id); |
| 3615 |
|
| 3616 |
if (id > biggest_id) { |
| 3617 |
biggest_id = id; |
| 3618 |
} |
| 3619 |
} |
| 3620 |
} |
| 3621 |
} |
| 3622 |
|
| 3623 |
ulint size = table->foreign_set.size(); |
| 3624 |
|
| 3625 |
biggest_id = (size > biggest_id) ? size : biggest_id; |
| 3626 |
|
| 3627 |
DBUG_PRINT("dict_table_get_highest_foreign_id", ("id: %lu", biggest_id)); |
| 3628 |
|
| 3629 |
return biggest_id; |
| 3630 |
} |
| 3631 |
|
| 3632 |
/*==================== END OF FOREIGN KEY PROCESSING ====================*/ |
| 3633 |
|
| 3634 |
#ifdef UNIV_DEBUG |
| 3635 |
/** Checks that a tuple has n_fields_cmp value in a sensible range, so that |
| 3636 |
no comparison can occur with the page number field in a node pointer. |
| 3637 |
@return true if ok */ |
| 3638 |
bool dict_index_check_search_tuple( |
| 3639 |
const dict_index_t *index, /*!< in: index tree */ |
| 3640 |
const dtuple_t *tuple) /*!< in: tuple used in a search */ |
| 3641 |
{ |
| 3642 |
ut_a(index); |
| 3643 |
ut_a(dtuple_get_n_fields_cmp(tuple) <= |
| 3644 |
dict_index_get_n_unique_in_tree(index)); |
| 3645 |
ut_ad(index->page != FIL_NULL); |
| 3646 |
ut_ad(index->page >= FSP_FIRST_INODE_PAGE_NO); |
| 3647 |
ut_ad(dtuple_check_typed(tuple)); |
| 3648 |
ut_ad(!(index->type & DICT_FTS)); |
| 3649 |
return true; |
| 3650 |
} |
| 3651 |
#endif /* UNIV_DEBUG */ |
| 3652 |
|
| 3653 |
/** Builds a node pointer out of a physical record and a page number. |
| 3654 |
@return own: node pointer */ |
| 3655 |
dtuple_t *dict_index_build_node_ptr(const dict_index_t *index, /*!< in: index */ |
| 3656 |
const rec_t *rec, /*!< in: record for which |
| 3657 |
to build node pointer */ |
| 3658 |
page_no_t page_no, /*!< in: page number to |
| 3659 |
put in node pointer */ |
| 3660 |
mem_heap_t *heap, /*!< in: memory heap where |
| 3661 |
pointer created */ |
| 3662 |
ulint level) /*!< in: level of rec in tree: |
| 3663 |
0 means leaf level */ |
| 3664 |
{ |
| 3665 |
dtuple_t *tuple; |
| 3666 |
dfield_t *field; |
| 3667 |
byte *buf; |
| 3668 |
ulint n_unique; |
| 3669 |
|
| 3670 |
if (dict_index_is_ibuf(index)) { |
| 3671 |
/* In a universal index tree, we take the whole record as |
| 3672 |
the node pointer if the record is on the leaf level, |
| 3673 |
on non-leaf levels we remove the last field, which |
| 3674 |
contains the page number of the child page */ |
| 3675 |
|
| 3676 |
ut_a(!dict_table_is_comp(index->table)); |
| 3677 |
n_unique = rec_get_n_fields_old_raw(rec); |
| 3678 |
|
| 3679 |
if (level > 0) { |
| 3680 |
ut_a(n_unique > 1); |
| 3681 |
n_unique--; |
| 3682 |
} |
| 3683 |
} else { |
| 3684 |
n_unique = dict_index_get_n_unique_in_tree_nonleaf(index); |
| 3685 |
} |
| 3686 |
|
| 3687 |
tuple = dtuple_create(heap, n_unique + 1); |
| 3688 |
|
| 3689 |
/* When searching in the tree for the node pointer, we must not do |
| 3690 |
comparison on the last field, the page number field, as on upper |
| 3691 |
levels in the tree there may be identical node pointers with a |
| 3692 |
different page number; therefore, we set the n_fields_cmp to one |
| 3693 |
less: */ |
| 3694 |
|
| 3695 |
dtuple_set_n_fields_cmp(tuple, n_unique); |
| 3696 |
|
| 3697 |
dict_index_copy_types(tuple, index, n_unique); |
| 3698 |
|
| 3699 |
buf = static_cast<byte *>(mem_heap_alloc(heap, 4)); |
| 3700 |
|
| 3701 |
mach_write_to_4(buf, page_no); |
| 3702 |
|
| 3703 |
field = dtuple_get_nth_field(tuple, n_unique); |
| 3704 |
dfield_set_data(field, buf, 4); |
| 3705 |
|
| 3706 |
dtype_set(dfield_get_type(field), DATA_SYS_CHILD, DATA_NOT_NULL, 4); |
| 3707 |
|
| 3708 |
rec_copy_prefix_to_dtuple(tuple, rec, index, n_unique, heap); |
| 3709 |
dtuple_set_info_bits(tuple, |
| 3710 |
dtuple_get_info_bits(tuple) | REC_STATUS_NODE_PTR); |
| 3711 |
|
| 3712 |
ut_ad(dtuple_check_typed(tuple)); |
| 3713 |
|
| 3714 |
return (tuple); |
| 3715 |
} |
| 3716 |
|
| 3717 |
rec_t *dict_index_copy_rec_order_prefix(const dict_index_t *index, |
| 3718 |
const rec_t *rec, ulint *n_fields, |
| 3719 |
|
| 3720 |
byte **buf, size_t *buf_size) { |
| 3721 |
ulint n; |
| 3722 |
|
| 3723 |
UNIV_PREFETCH_R(rec); |
| 3724 |
|
| 3725 |
if (dict_index_is_ibuf(index)) { |
| 3726 |
ut_a(!dict_table_is_comp(index->table)); |
| 3727 |
n = rec_get_n_fields_old_raw(rec); |
| 3728 |
} else { |
| 3729 |
if (page_is_leaf(page_align(rec))) { |
| 3730 |
n = dict_index_get_n_unique_in_tree(index); |
| 3731 |
} else { |
| 3732 |
n = dict_index_get_n_unique_in_tree_nonleaf(index); |
| 3733 |
/* For internal node of R-tree, since we need to |
| 3734 |
compare the page no field, so, we need to copy this |
| 3735 |
field as well. */ |
| 3736 |
if (dict_index_is_spatial(index)) { |
| 3737 |
n++; |
| 3738 |
} |
| 3739 |
} |
| 3740 |
} |
| 3741 |
|
| 3742 |
*n_fields = n; |
| 3743 |
return (rec_copy_prefix_to_buf(rec, index, n, buf, buf_size)); |
| 3744 |
} |
| 3745 |
|
| 3746 |
/** Builds a typed data tuple out of a physical record. |
| 3747 |
@return own: data tuple */ |
| 3748 |
dtuple_t *dict_index_build_data_tuple( |
| 3749 |
dict_index_t *index, /*!< in: index tree */ |
| 3750 |
rec_t *rec, /*!< in: record for which to build data tuple */ |
| 3751 |
ulint n_fields, /*!< in: number of data fields */ |
| 3752 |
mem_heap_t *heap) /*!< in: memory heap where tuple created */ |
| 3753 |
{ |
| 3754 |
dtuple_t *tuple; |
| 3755 |
|
| 3756 |
ut_ad(dict_table_is_comp(index->table) || |
| 3757 |
n_fields <= rec_get_n_fields_old(rec, index)); |
| 3758 |
|
| 3759 |
tuple = dtuple_create(heap, n_fields); |
| 3760 |
|
| 3761 |
dict_index_copy_types(tuple, index, n_fields); |
| 3762 |
|
| 3763 |
rec_copy_prefix_to_dtuple(tuple, rec, index, n_fields, heap); |
| 3764 |
|
| 3765 |
ut_ad(dtuple_check_typed(tuple)); |
| 3766 |
|
| 3767 |
return (tuple); |
| 3768 |
} |
| 3769 |
|
| 3770 |
/** Calculates the minimum record length in an index. */ |
| 3771 |
ulint dict_index_calc_min_rec_len(const dict_index_t *index) /*!< in: index */ |
| 3772 |
{ |
| 3773 |
ulint sum = 0; |
| 3774 |
ulint i; |
| 3775 |
ulint comp = dict_table_is_comp(index->table); |
| 3776 |
|
| 3777 |
if (comp) { |
| 3778 |
ulint nullable = 0; |
| 3779 |
sum = REC_N_NEW_EXTRA_BYTES; |
| 3780 |
for (i = 0; i < dict_index_get_n_fields(index); i++) { |
| 3781 |
const dict_col_t *col = index->get_col(i); |
| 3782 |
ulint size = col->get_fixed_size(comp); |
| 3783 |
sum += size; |
| 3784 |
if (!size) { |
| 3785 |
size = col->len; |
| 3786 |
sum += size < 128 ? 1 : 2; |
| 3787 |
} |
| 3788 |
if (!(col->prtype & DATA_NOT_NULL)) { |
| 3789 |
nullable++; |
| 3790 |
} |
| 3791 |
} |
| 3792 |
|
| 3793 |
/* round the NULL flags up to full bytes */ |
| 3794 |
sum += UT_BITS_IN_BYTES(nullable); |
| 3795 |
|
| 3796 |
return (sum); |
| 3797 |
} |
| 3798 |
|
| 3799 |
for (i = 0; i < dict_index_get_n_fields(index); i++) { |
| 3800 |
sum += index->get_col(i)->get_fixed_size(comp); |
| 3801 |
} |
| 3802 |
|
| 3803 |
if (sum > 127) { |
| 3804 |
sum += 2 * dict_index_get_n_fields(index); |
| 3805 |
} else { |
| 3806 |
sum += dict_index_get_n_fields(index); |
| 3807 |
} |
| 3808 |
|
| 3809 |
sum += REC_N_OLD_EXTRA_BYTES; |
| 3810 |
|
| 3811 |
return (sum); |
| 3812 |
} |
| 3813 |
|
| 3814 |
/** Outputs info on a foreign key of a table in a format suitable for |
| 3815 |
CREATE TABLE. |
| 3816 |
@param[in] file File where to print |
| 3817 |
@param[in] trx Transaction |
| 3818 |
@param[in] foreign Foreign key constraint |
| 3819 |
@param[in] add_newline Whether to add a newline */ |
| 3820 |
void dict_print_info_on_foreign_key_in_create_format(FILE *file, trx_t *trx, |
| 3821 |
dict_foreign_t *foreign, |
| 3822 |
bool add_newline) { |
| 3823 |
const char *stripped_id; |
| 3824 |
ulint i; |
| 3825 |
|
| 3826 |
if (strchr(foreign->id, '/')) { |
| 3827 |
/* Strip the preceding database name from the constraint id */ |
| 3828 |
stripped_id = foreign->id + 1 + dict_get_db_name_len(foreign->id); |
| 3829 |
} else { |
| 3830 |
stripped_id = foreign->id; |
| 3831 |
} |
| 3832 |
|
| 3833 |
putc(',', file); |
| 3834 |
|
| 3835 |
if (add_newline) { |
| 3836 |
/* SHOW CREATE TABLE wants constraints each printed nicely |
| 3837 |
on its own line, while error messages want no newlines |
| 3838 |
inserted. */ |
| 3839 |
fputs("\n ", file); |
| 3840 |
} |
| 3841 |
|
| 3842 |
fputs(" CONSTRAINT ", file); |
| 3843 |
innobase_quote_identifier(file, trx, stripped_id); |
| 3844 |
fputs(" FOREIGN KEY (", file); |
| 3845 |
|
| 3846 |
for (i = 0;;) { |
| 3847 |
innobase_quote_identifier(file, trx, foreign->foreign_col_names[i]); |
| 3848 |
if (++i < foreign->n_fields) { |
| 3849 |
fputs(", ", file); |
| 3850 |
} else { |
| 3851 |
break; |
| 3852 |
} |
| 3853 |
} |
| 3854 |
|
| 3855 |
fputs(") REFERENCES ", file); |
| 3856 |
|
| 3857 |
if (dict_tables_have_same_db(foreign->foreign_table_name_lookup, |
| 3858 |
foreign->referenced_table_name_lookup)) { |
| 3859 |
/* Do not print the database name of the referenced table */ |
| 3860 |
ut_print_name(file, trx, |
| 3861 |
dict_remove_db_name(foreign->referenced_table_name)); |
| 3862 |
} else { |
| 3863 |
ut_print_name(file, trx, foreign->referenced_table_name); |
| 3864 |
} |
| 3865 |
|
| 3866 |
putc(' ', file); |
| 3867 |
putc('(', file); |
| 3868 |
|
| 3869 |
for (i = 0;;) { |
| 3870 |
innobase_quote_identifier(file, trx, foreign->referenced_col_names[i]); |
| 3871 |
if (++i < foreign->n_fields) { |
| 3872 |
fputs(", ", file); |
| 3873 |
} else { |
| 3874 |
break; |
| 3875 |
} |
| 3876 |
} |
| 3877 |
|
| 3878 |
putc(')', file); |
| 3879 |
|
| 3880 |
if (foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE) { |
| 3881 |
fputs(" ON DELETE CASCADE", file); |
| 3882 |
} |
| 3883 |
|
| 3884 |
if (foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL) { |
| 3885 |
fputs(" ON DELETE SET NULL", file); |
| 3886 |
} |
| 3887 |
|
| 3888 |
if (!(foreign->type & DICT_FOREIGN_ON_DELETE_NO_ACTION) && |
| 3889 |
!(foreign->type & DICT_FOREIGN_ON_DELETE_CASCADE) && |
| 3890 |
!(foreign->type & DICT_FOREIGN_ON_DELETE_SET_NULL)) { |
| 3891 |
fputs(" ON DELETE RESTRICT", file); |
| 3892 |
} |
| 3893 |
|
| 3894 |
if (foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE) { |
| 3895 |
fputs(" ON UPDATE CASCADE", file); |
| 3896 |
} |
| 3897 |
|
| 3898 |
if (foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL) { |
| 3899 |
fputs(" ON UPDATE SET NULL", file); |
| 3900 |
} |
| 3901 |
|
| 3902 |
if (!(foreign->type & DICT_FOREIGN_ON_UPDATE_NO_ACTION) && |
| 3903 |
!(foreign->type & DICT_FOREIGN_ON_UPDATE_CASCADE) && |
| 3904 |
!(foreign->type & DICT_FOREIGN_ON_UPDATE_SET_NULL)) { |
| 3905 |
fputs(" ON UPDATE RESTRICT", file); |
| 3906 |
} |
| 3907 |
} |
| 3908 |
#endif /* !UNIV_HOTBACKUP */ |
| 3909 |
|
| 3910 |
/** Inits the structure for persisting dynamic metadata */ |
| 3911 |
void dict_persist_init(void) { |
| 3912 |
dict_persist = static_cast<dict_persist_t *>( |
| 3913 |
ut::zalloc_withkey(UT_NEW_THIS_FILE_PSI_KEY, sizeof(*dict_persist))); |
| 3914 |
|
| 3915 |
mutex_create(LATCH_ID_DICT_PERSIST_DIRTY_TABLES, &dict_persist->mutex); |
| 3916 |
|
| 3917 |
#ifndef UNIV_HOTBACKUP |
| 3918 |
UT_LIST_INIT(dict_persist->dirty_dict_tables); |
| 3919 |
#endif /* !UNIV_HOTBACKUP */ |
| 3920 |
|
| 3921 |
dict_persist->num_dirty_tables = 0; |
| 3922 |
|
| 3923 |
dict_persist->m_persist_immediately.store(false); |
| 3924 |
|
| 3925 |
dict_persist->persisters = |
| 3926 |
ut::new_withkey<Persisters>(UT_NEW_THIS_FILE_PSI_KEY); |
| 3927 |
dict_persist->persisters->add(PM_INDEX_CORRUPTED); |
| 3928 |
dict_persist->persisters->add(PM_TABLE_AUTO_INC); |
| 3929 |
|
| 3930 |
#ifndef UNIV_HOTBACKUP |
| 3931 |
dict_persist_update_log_margin(); |
| 3932 |
#endif /* !UNIV_HOTBACKUP */ |
| 3933 |
} |
| 3934 |
|
| 3935 |
/** Clear the structure */ |
| 3936 |
void dict_persist_close(void) { |
| 3937 |
ut::delete_(dict_persist->persisters); |
| 3938 |
|
| 3939 |
#ifndef UNIV_HOTBACKUP |
| 3940 |
ut::delete_(dict_persist->table_buffer); |
| 3941 |
#endif /* !UNIV_HOTBACKUP */ |
| 3942 |
|
| 3943 |
mutex_free(&dict_persist->mutex); |
| 3944 |
|
| 3945 |
ut::free(dict_persist); |
| 3946 |
} |
| 3947 |
|
| 3948 |
#ifndef UNIV_HOTBACKUP |
| 3949 |
/** Initialize the dynamic metadata according to the table object |
| 3950 |
@param[in] table table object |
| 3951 |
@param[in,out] metadata metadata to be initialized */ |
| 3952 |
static void dict_init_dynamic_metadata(dict_table_t *table, |
| 3953 |
PersistentTableMetadata *metadata) { |
| 3954 |
ut_ad(mutex_own(&dict_persist->mutex)); |
| 3955 |
|
| 3956 |
ut_ad(metadata->get_table_id() == table->id); |
| 3957 |
|
| 3958 |
for (const dict_index_t *index = table->first_index(); index != nullptr; |
| 3959 |
index = index->next()) { |
| 3960 |
if (index->is_corrupted()) { |
| 3961 |
metadata->add_corrupted_index(index_id_t(index->space, index->id)); |
| 3962 |
} |
| 3963 |
} |
| 3964 |
|
| 3965 |
if (table->autoinc_persisted != 0) { |
| 3966 |
metadata->set_autoinc(table->autoinc_persisted); |
| 3967 |
} |
| 3968 |
|
| 3969 |
/* Will initialize other metadata here */ |
| 3970 |
} |
| 3971 |
|
| 3972 |
/** Apply the persistent dynamic metadata read from redo logs or |
| 3973 |
DDTableBuffer to corresponding table during recovery. |
| 3974 |
@param[in,out] table table |
| 3975 |
@param[in] metadata structure of persistent metadata |
| 3976 |
@return true if we do apply something to the in-memory table object, |
| 3977 |
otherwise false */ |
| 3978 |
static bool dict_table_apply_dynamic_metadata( |
| 3979 |
dict_table_t *table, const PersistentTableMetadata *metadata) { |
| 3980 |
bool get_dirty = false; |
| 3981 |
|
| 3982 |
ut_ad(dict_sys_mutex_own()); |
| 3983 |
|
| 3984 |
/* Apply corrupted index ids first */ |
| 3985 |
const corrupted_ids_t corrupted_ids = metadata->get_corrupted_indexes(); |
| 3986 |
|
| 3987 |
for (corrupted_ids_t::const_iterator iter = corrupted_ids.begin(); |
| 3988 |
iter != corrupted_ids.end(); ++iter) { |
| 3989 |
const index_id_t index_id = *iter; |
| 3990 |
dict_index_t *index; |
| 3991 |
|
| 3992 |
index = const_cast<dict_index_t *>( |
| 3993 |
dict_table_find_index_on_id(table, index_id)); |
| 3994 |
|
| 3995 |
if (index != nullptr) { |
| 3996 |
ut_ad(index->space == index_id.m_space_id); |
| 3997 |
|
| 3998 |
if (!index->is_corrupted()) { |
| 3999 |
index->type |= DICT_CORRUPT; |
| 4000 |
get_dirty = true; |
| 4001 |
} |
| 4002 |
|
| 4003 |
} else { |
| 4004 |
/* In some cases, we could only load some indexes |
| 4005 |
of a table but not all(See dict_load_indexes()). |
| 4006 |
So we might not find it here */ |
| 4007 |
ib::info(ER_IB_MSG_184) |
| 4008 |
<< "Failed to find the index: " << index_id.m_index_id |
| 4009 |
<< " in space: " << index_id.m_space_id |
| 4010 |
<< " of table: " << table->name << "(table id: " << table->id |
| 4011 |
<< "). The index should have been dropped" |
| 4012 |
<< " or couldn't be loaded."; |
| 4013 |
} |
| 4014 |
} |
| 4015 |
|
| 4016 |
/* FIXME: Move this to the beginning of this function once corrupted |
| 4017 |
index IDs are also written back to dd::Table::se_private_data. */ |
| 4018 |
/* Here is how version play role. Basically, version would be increased |
| 4019 |
by one during every DDL. So applying metadata here should only be |
| 4020 |
done when the versions match. One reason for this version is that |
| 4021 |
autoinc counter may not be applied if it's bigger if the version is |
| 4022 |
older. |
| 4023 |
If the version of metadata is older than current table, |
| 4024 |
then table already has the latest metadata, the old one should be |
| 4025 |
discarded. |
| 4026 |
If the metadata version is bigger than the one in table. |
| 4027 |
it could be that an ALTER TABLE has been rolled back, so metadata |
| 4028 |
in new version should be ignored too. */ |
| 4029 |
if (table->version != metadata->get_version()) { |
| 4030 |
return (get_dirty); |
| 4031 |
} |
| 4032 |
|
| 4033 |
uint64_t autoinc = metadata->get_autoinc(); |
| 4034 |
|
| 4035 |
/* This happens during recovery, so no locks are needed. */ |
| 4036 |
if (autoinc > table->autoinc_persisted) { |
| 4037 |
table->autoinc = autoinc; |
| 4038 |
table->autoinc_persisted = autoinc; |
| 4039 |
|
| 4040 |
get_dirty = true; |
| 4041 |
} |
| 4042 |
|
| 4043 |
/* Will apply other persistent metadata here */ |
| 4044 |
|
| 4045 |
return (get_dirty); |
| 4046 |
} |
| 4047 |
|
| 4048 |
/** Read persistent dynamic metadata stored in a buffer |
| 4049 |
@param[in] buffer buffer to read |
| 4050 |
@param[in] size size of data in buffer |
| 4051 |
@param[in] metadata where we store the metadata from buffer */ |
| 4052 |
void dict_table_read_dynamic_metadata(const byte *buffer, ulint size, |
| 4053 |
PersistentTableMetadata *metadata) { |
| 4054 |
const byte *pos = buffer; |
| 4055 |
persistent_type_t type; |
| 4056 |
Persister *persister; |
| 4057 |
ulint consumed; |
| 4058 |
bool corrupt; |
| 4059 |
|
| 4060 |
while (size > 0) { |
| 4061 |
type = static_cast<persistent_type_t>(pos[0]); |
| 4062 |
ut_ad(type > PM_SMALLEST_TYPE && type < PM_BIGGEST_TYPE); |
| 4063 |
|
| 4064 |
persister = dict_persist->persisters->get(type); |
| 4065 |
ut_ad(persister != nullptr); |
| 4066 |
pos++; |
| 4067 |
size--; |
| 4068 |
|
| 4069 |
consumed = persister->read(*metadata, pos, size, &corrupt); |
| 4070 |
ut_ad(consumed != 0); |
| 4071 |
ut_ad(size >= consumed); |
| 4072 |
ut_ad(!corrupt); |
| 4073 |
|
| 4074 |
size -= consumed; |
| 4075 |
pos += consumed; |
| 4076 |
} |
| 4077 |
|
| 4078 |
ut_ad(size == 0); |
| 4079 |
} |
| 4080 |
|
| 4081 |
/** Check if there is any latest persistent dynamic metadata recorded |
| 4082 |
in DDTableBuffer table of the specific table. If so, read the metadata and |
| 4083 |
update the table object accordingly. It's used when loading table. |
| 4084 |
@param[in] table table object */ |
| 4085 |
void dict_table_load_dynamic_metadata(dict_table_t *table) { |
| 4086 |
DDTableBuffer *table_buffer; |
| 4087 |
|
| 4088 |
ut_ad(dict_sys != nullptr); |
| 4089 |
ut_ad(dict_sys_mutex_own()); |
| 4090 |
ut_ad(!table->is_temporary()); |
| 4091 |
|
| 4092 |
table_buffer = dict_persist->table_buffer; |
| 4093 |
|
| 4094 |
mutex_enter(&dict_persist->mutex); |
| 4095 |
|
| 4096 |
uint64_t version; |
| 4097 |
const auto readmeta = table_buffer->get(table->id, &version); |
| 4098 |
|
| 4099 |
if (!readmeta.empty()) { |
| 4100 |
/* Persistent dynamic metadata of this table have changed |
| 4101 |
recently, we need to update them to in-memory table */ |
| 4102 |
PersistentTableMetadata metadata(table->id, version); |
| 4103 |
|
| 4104 |
dict_table_read_dynamic_metadata(readmeta.data(), readmeta.size(), |
| 4105 |
&metadata); |
| 4106 |
|
| 4107 |
bool is_dirty = dict_table_apply_dynamic_metadata(table, &metadata); |
| 4108 |
|
| 4109 |
/* If !is_dirty, it could be either: |
| 4110 |
1. It's first time to load this table, and the corrupted |
| 4111 |
index marked has been dropped. Current dirty_status should |
| 4112 |
be METADATA_CLEAN. |
| 4113 |
2. It's the second time to apply dynamic metadata to this |
| 4114 |
table, current in-memory dynamic metadata is up-to-date. |
| 4115 |
Current dirty_status should be METADATA_BUFFERED. |
| 4116 |
In both cases, we don't have to change the dirty_status */ |
| 4117 |
if (is_dirty) { |
| 4118 |
UT_LIST_ADD_LAST(dict_persist->dirty_dict_tables, table); |
| 4119 |
table->dirty_status.store(METADATA_BUFFERED); |
| 4120 |
ut_d(table->in_dirty_dict_tables_list = true); |
| 4121 |
} |
| 4122 |
} |
| 4123 |
|
| 4124 |
mutex_exit(&dict_persist->mutex); |
| 4125 |
} |
| 4126 |
|
| 4127 |
/** Mark the dirty_status of a table as METADATA_DIRTY, and add it to the |
| 4128 |
dirty_dict_tables list if necessary. |
| 4129 |
@param[in,out] table table */ |
| 4130 |
void dict_table_mark_dirty(dict_table_t *table) { |
| 4131 |
ut_ad(!table->is_temporary()); |
| 4132 |
|
| 4133 |
/* We should not adding dynamic metadata so late in shutdown phase and |
| 4134 |
this data would only be retrieved during recovery. */ |
| 4135 |
ut_ad(srv_shutdown_state.load() < SRV_SHUTDOWN_FLUSH_PHASE); |
| 4136 |
|
| 4137 |
mutex_enter(&dict_persist->mutex); |
| 4138 |
|
| 4139 |
switch (table->dirty_status.load()) { |
| 4140 |
case METADATA_DIRTY: |
| 4141 |
break; |
| 4142 |
case METADATA_CLEAN: |
| 4143 |
/* Not in dirty_tables list, add it now */ |
| 4144 |
UT_LIST_ADD_LAST(dict_persist->dirty_dict_tables, table); |
| 4145 |
ut_d(table->in_dirty_dict_tables_list = true); |
| 4146 |
[[fallthrough]]; |
| 4147 |
case METADATA_BUFFERED: |
| 4148 |
table->dirty_status.store(METADATA_DIRTY); |
| 4149 |
++dict_persist->num_dirty_tables; |
| 4150 |
|
| 4151 |
dict_persist_update_log_margin(); |
| 4152 |
} |
| 4153 |
|
| 4154 |
ut_ad(table->in_dirty_dict_tables_list); |
| 4155 |
|
| 4156 |
mutex_exit(&dict_persist->mutex); |
| 4157 |
} |
| 4158 |
|
| 4159 |
/** Flags an index corrupted in the data dictionary cache only. This |
| 4160 |
is used to mark a corrupted index when index's own dictionary |
| 4161 |
is corrupted, and we would force to load such index for repair purpose. |
| 4162 |
Besides, we have to write a redo log. |
| 4163 |
We don't want to hold dict_sys->mutex here, so that we can set index as |
| 4164 |
corrupted in some low-level functions. We would only set the flags from |
| 4165 |
not corrupted to corrupted when server is running, so it should be safe |
| 4166 |
to set it directly. |
| 4167 |
@param[in,out] index index, must not be NULL */ |
| 4168 |
void dict_set_corrupted(dict_index_t *index) { |
| 4169 |
dict_table_t *table = index->table; |
| 4170 |
|
| 4171 |
if (index->type & DICT_CORRUPT) { |
| 4172 |
return; |
| 4173 |
} |
| 4174 |
|
| 4175 |
index->type |= DICT_CORRUPT; |
| 4176 |
|
| 4177 |
if (!srv_read_only_mode && !table->is_temporary()) { |
| 4178 |
PersistentTableMetadata metadata(table->id, table->version); |
| 4179 |
metadata.add_corrupted_index(index_id_t(index->space, index->id)); |
| 4180 |
|
| 4181 |
Persister *persister = dict_persist->persisters->get(PM_INDEX_CORRUPTED); |
| 4182 |
ut_ad(persister != nullptr); |
| 4183 |
|
| 4184 |
mtr_t mtr; |
| 4185 |
|
| 4186 |
mtr.start(); |
| 4187 |
persister->write_log(table->id, metadata, &mtr); |
| 4188 |
mtr.commit(); |
| 4189 |
|
| 4190 |
/* Make sure the corruption bit won't be lost */ |
| 4191 |
log_write_up_to(*log_sys, mtr.commit_lsn(), true); |
| 4192 |
|
| 4193 |
DBUG_INJECT_CRASH("log_corruption_crash", 1); |
| 4194 |
|
| 4195 |
dict_table_mark_dirty(table); |
| 4196 |
} |
| 4197 |
} |
| 4198 |
|
| 4199 |
/** Write the dirty persistent dynamic metadata for a table to |
| 4200 |
DD TABLE BUFFER table. This is the low level function to write back. |
| 4201 |
@param[in,out] table table to write */ |
| 4202 |
static void dict_table_persist_to_dd_table_buffer_low(dict_table_t *table) { |
| 4203 |
ut_ad(dict_sys != nullptr); |
| 4204 |
ut_ad(mutex_own(&dict_persist->mutex)); |
| 4205 |
ut_ad(table->dirty_status.load() == METADATA_DIRTY); |
| 4206 |
ut_ad(table->in_dirty_dict_tables_list); |
| 4207 |
ut_ad(!table->is_temporary()); |
| 4208 |
|
| 4209 |
DDTableBuffer *table_buffer = dict_persist->table_buffer; |
| 4210 |
PersistentTableMetadata metadata(table->id, table->version); |
| 4211 |
byte buffer[REC_MAX_DATA_SIZE]; |
| 4212 |
ulint size; |
| 4213 |
|
| 4214 |
/* Here the status gets changed first, to make concurrent |
| 4215 |
update to this table to wait on dict_persist_t::mutex. |
| 4216 |
See dict_table_autoinc_log(), etc. */ |
| 4217 |
table->dirty_status.store(METADATA_BUFFERED); |
| 4218 |
|
| 4219 |
dict_init_dynamic_metadata(table, &metadata); |
| 4220 |
|
| 4221 |
size = dict_persist->persisters->write(metadata, buffer); |
| 4222 |
|
| 4223 |
dberr_t error = |
| 4224 |
table_buffer->replace(table->id, table->version, buffer, size); |
| 4225 |
ut_a(error == DB_SUCCESS); |
| 4226 |
|
| 4227 |
ut_ad(dict_persist->num_dirty_tables > 0); |
| 4228 |
--dict_persist->num_dirty_tables; |
| 4229 |
dict_persist_update_log_margin(); |
| 4230 |
} |
| 4231 |
|
| 4232 |
/** Write back the dirty persistent dynamic metadata of the table |
| 4233 |
to DDTableBuffer |
| 4234 |
@param[in,out] table table object */ |
| 4235 |
void dict_table_persist_to_dd_table_buffer(dict_table_t *table) { |
| 4236 |
mutex_enter(&dict_persist->mutex); |
| 4237 |
|
| 4238 |
if (table->dirty_status.load() != METADATA_DIRTY) { |
| 4239 |
/* Double check the status, since a concurrent checkpoint |
| 4240 |
may have already changed the status to not dirty */ |
| 4241 |
mutex_exit(&dict_persist->mutex); |
| 4242 |
return; |
| 4243 |
} |
| 4244 |
|
| 4245 |
ut_ad(table->in_dirty_dict_tables_list); |
| 4246 |
|
| 4247 |
dict_table_persist_to_dd_table_buffer_low(table); |
| 4248 |
|
| 4249 |
mutex_exit(&dict_persist->mutex); |
| 4250 |
} |
| 4251 |
|
| 4252 |
/** Check if any table has any dirty persistent data, if so |
| 4253 |
write dirty persistent data of table to mysql.innodb_dynamic_metadata |
| 4254 |
accordingly. */ |
| 4255 |
void dict_persist_to_dd_table_buffer() { |
| 4256 |
bool persisted = false; |
| 4257 |
|
| 4258 |
if (dict_sys == nullptr) { |
| 4259 |
log_set_dict_max_allowed_checkpoint_lsn(*log_sys, 0); |
| 4260 |
return; |
| 4261 |
} |
| 4262 |
|
| 4263 |
mutex_enter(&dict_persist->mutex); |
| 4264 |
|
| 4265 |
for (dict_table_t *table : dict_persist->dirty_dict_tables.removable()) { |
| 4266 |
ut_ad(table->magic_n == DICT_TABLE_MAGIC_N); |
| 4267 |
ut_ad(table->dirty_status.load() == METADATA_DIRTY || |
| 4268 |
table->dirty_status.load() == METADATA_BUFFERED); |
| 4269 |
|
| 4270 |
if (table->dirty_status.load() == METADATA_DIRTY) { |
| 4271 |
/* We should not attempt to write to data pages while shutting down |
| 4272 |
page cleaners. */ |
| 4273 |
if (srv_shutdown_state.load() >= SRV_SHUTDOWN_FLUSH_PHASE) { |
| 4274 |
ut_d(ut_error); |
| 4275 |
} else { |
| 4276 |
dict_table_persist_to_dd_table_buffer_low(table); |
| 4277 |
persisted = true; |
| 4278 |
} |
| 4279 |
} |
| 4280 |
} |
| 4281 |
|
| 4282 |
ut_ad(dict_persist->num_dirty_tables == 0); |
| 4283 |
|
| 4284 |
/* Get this lsn with dict_persist->mutex held, |
| 4285 |
so no other concurrent dynamic metadata change logs |
| 4286 |
would be before this lsn. */ |
| 4287 |
const lsn_t persisted_lsn = log_get_lsn(*log_sys); |
| 4288 |
|
| 4289 |
/* As soon as we release the dict_persist->mutex, new dynamic |
| 4290 |
metadata changes could happen. They would be not persisted |
| 4291 |
until next call to dict_persist_to_dd_table_buffer. |
| 4292 |
We must not remove redo which could allow to deduce them. |
| 4293 |
Therefore the maximum allowed lsn for checkpoint is the |
| 4294 |
current lsn. */ |
| 4295 |
log_set_dict_max_allowed_checkpoint_lsn(*log_sys, persisted_lsn); |
| 4296 |
|
| 4297 |
mutex_exit(&dict_persist->mutex); |
| 4298 |
|
| 4299 |
if (persisted) { |
| 4300 |
log_write_up_to(*log_sys, persisted_lsn, true); |
| 4301 |
} |
| 4302 |
} |
| 4303 |
|
| 4304 |
/** Calculate and update the redo log margin for current tables which |
| 4305 |
have some changed dynamic metadata in memory and have not been written |
| 4306 |
back to mysql.innodb_dynamic_metadata. Update LSN limit, which is used |
| 4307 |
to stop user threads when redo log is running out of space and they |
| 4308 |
do not hold latches (log.free_check_limit_lsn). */ |
| 4309 |
static void dict_persist_update_log_margin() { |
| 4310 |
/* Below variables basically considers only the AUTO_INCREMENT counter |
| 4311 |
and a small margin for corrupted indexes. */ |
| 4312 |
|
| 4313 |
/* Every table will generate less than 80 bytes without |
| 4314 |
considering page split */ |
| 4315 |
static constexpr uint32_t log_margin_per_table_no_split = 80; |
| 4316 |
|
| 4317 |
/* Every table metadata log may roughly consume such many bytes. */ |
| 4318 |
static constexpr uint32_t record_size_per_table = 50; |
| 4319 |
|
| 4320 |
/* How many tables may generate one page split */ |
| 4321 |
static const uint32_t tables_per_split = |
| 4322 |
(univ_page_size.physical() - PAGE_NEW_SUPREMUM_END) / |
| 4323 |
record_size_per_table / 2; |
| 4324 |
|
| 4325 |
/* Every page split needs at most this log margin, if not root split. */ |
| 4326 |
static const uint32_t log_margin_per_split_no_root = 500; |
| 4327 |
|
| 4328 |
/* Extra margin for root split, we always leave this margin, |
| 4329 |
since we don't know exactly it will split root or not */ |
| 4330 |
static const uint32_t log_margin_per_split_root = |
| 4331 |
univ_page_size.physical() / 2 * 3; /* Add 50% margin. */ |
| 4332 |
|
| 4333 |
/* Read without holding the dict_persist_t::mutex */ |
| 4334 |
uint32_t num_dirty_tables = dict_persist->num_dirty_tables; |
| 4335 |
uint32_t total_splits = 0; |
| 4336 |
uint32_t num_tables = num_dirty_tables; |
| 4337 |
|
| 4338 |
while (num_tables > 0) { |
| 4339 |
total_splits += num_tables / tables_per_split + 1; |
| 4340 |
num_tables = num_tables / tables_per_split; |
| 4341 |
} |
| 4342 |
|
| 4343 |
const auto margin = (num_dirty_tables * log_margin_per_table_no_split + |
| 4344 |
total_splits * log_margin_per_split_no_root + |
| 4345 |
(num_dirty_tables == 0 ? 0 : log_margin_per_split_root)); |
| 4346 |
|
| 4347 |
if (log_sys != nullptr) { |
| 4348 |
/* Update margin for redo log */ |
| 4349 |
log_set_dict_persist_margin(*log_sys, margin); |
| 4350 |
} |
| 4351 |
} |
| 4352 |
|
| 4353 |
#ifdef UNIV_DEBUG |
| 4354 |
/** Sets merge_threshold for all indexes in the list of tables |
| 4355 |
@param[in] list pointer to the list of tables |
| 4356 |
@param[in] merge_threshold_all value to set for all indexes */ |
| 4357 |
inline void dict_set_merge_threshold_list_debug( |
| 4358 |
dict_sys_t::Table_LRU_list_base *list, uint merge_threshold_all) { |
| 4359 |
for (auto table : *list) { |
| 4360 |
for (auto index : table->indexes) { |
| 4361 |
rw_lock_x_lock(dict_index_get_lock(index), UT_LOCATION_HERE); |
| 4362 |
index->merge_threshold = merge_threshold_all; |
| 4363 |
rw_lock_x_unlock(dict_index_get_lock(index)); |
| 4364 |
} |
| 4365 |
} |
| 4366 |
} |
| 4367 |
|
| 4368 |
/** Sets merge_threshold for all indexes in dictionary cache for debug. |
| 4369 |
@param[in] merge_threshold_all value to set for all indexes */ |
| 4370 |
void dict_set_merge_threshold_all_debug(uint merge_threshold_all) { |
| 4371 |
dict_sys_mutex_enter(); |
| 4372 |
|
| 4373 |
dict_set_merge_threshold_list_debug(&dict_sys->table_LRU, |
| 4374 |
merge_threshold_all); |
| 4375 |
dict_set_merge_threshold_list_debug(&dict_sys->table_non_LRU, |
| 4376 |
merge_threshold_all); |
| 4377 |
|
| 4378 |
dict_sys_mutex_exit(); |
| 4379 |
} |
| 4380 |
#endif /* UNIV_DEBUG */ |
| 4381 |
|
| 4382 |
/** Inits dict_ind_redundant. */ |
| 4383 |
void dict_ind_init(void) { |
| 4384 |
dict_table_t *table; |
| 4385 |
|
| 4386 |
/* create dummy table and index for REDUNDANT infimum and supremum */ |
| 4387 |
table = dict_mem_table_create("SYS_DUMMY1", DICT_HDR_SPACE, 1, 0, 0, 0, 0); |
| 4388 |
dict_mem_table_add_col(table, nullptr, nullptr, DATA_CHAR, |
| 4389 |
DATA_ENGLISH | DATA_NOT_NULL, 8, true); |
| 4390 |
|
| 4391 |
dict_ind_redundant = |
| 4392 |
dict_mem_index_create("SYS_DUMMY1", "SYS_DUMMY1", DICT_HDR_SPACE, 0, 1); |
| 4393 |
dict_index_add_col(dict_ind_redundant, table, table->get_col(0), 0, true); |
| 4394 |
dict_ind_redundant->table = table; |
| 4395 |
/* avoid ut_ad(index->cached) in dict_index_get_n_unique_in_tree */ |
| 4396 |
dict_ind_redundant->cached = true; |
| 4397 |
} |
| 4398 |
|
| 4399 |
/** Frees dict_ind_redundant. */ |
| 4400 |
void dict_ind_free(void) { |
| 4401 |
dict_table_t *table; |
| 4402 |
|
| 4403 |
table = dict_ind_redundant->table; |
| 4404 |
dict_mem_index_free(dict_ind_redundant); |
| 4405 |
dict_ind_redundant = nullptr; |
| 4406 |
dict_mem_table_free(table); |
| 4407 |
} |
| 4408 |
|
| 4409 |
/** Get an index by name. |
| 4410 |
@param[in] table the table where to look for the index |
| 4411 |
@param[in] name the index name to look for |
| 4412 |
@param[in] committed true=search for committed, |
| 4413 |
false=search for uncommitted |
| 4414 |
@return index, NULL if does not exist */ |
| 4415 |
dict_index_t *dict_table_get_index_on_name(dict_table_t *table, |
| 4416 |
const char *name, bool committed) { |
| 4417 |
dict_index_t *index; |
| 4418 |
|
| 4419 |
index = table->first_index(); |
| 4420 |
|
| 4421 |
while (index != nullptr) { |
| 4422 |
if (index->is_committed() == committed && |
| 4423 |
innobase_strcasecmp(index->name, name) == 0) { |
| 4424 |
return (index); |
| 4425 |
} |
| 4426 |
|
| 4427 |
index = index->next(); |
| 4428 |
} |
| 4429 |
|
| 4430 |
return (nullptr); |
| 4431 |
} |
| 4432 |
|
| 4433 |
/** Replace the index passed in with another equivalent index in the |
| 4434 |
foreign key lists of the table. |
| 4435 |
@return whether all replacements were found */ |
| 4436 |
bool dict_foreign_replace_index( |
| 4437 |
dict_table_t *table, /*!< in/out: table */ |
| 4438 |
const char **col_names, |
| 4439 |
/*!< in: column names, or NULL |
| 4440 |
to use table->col_names */ |
| 4441 |
const dict_index_t *index) /*!< in: index to be replaced */ |
| 4442 |
{ |
| 4443 |
bool found = true; |
| 4444 |
dict_foreign_t *foreign; |
| 4445 |
|
| 4446 |
ut_ad(index->to_be_dropped); |
| 4447 |
ut_ad(index->table == table); |
| 4448 |
|
| 4449 |
for (dict_foreign_set::iterator it = table->foreign_set.begin(); |
| 4450 |
it != table->foreign_set.end(); ++it) { |
| 4451 |
foreign = *it; |
| 4452 |
if (foreign->foreign_index == index) { |
| 4453 |
ut_ad(foreign->foreign_table == index->table); |
| 4454 |
|
| 4455 |
dict_index_t *new_index = dict_foreign_find_index( |
| 4456 |
foreign->foreign_table, col_names, foreign->foreign_col_names, |
| 4457 |
foreign->n_fields, index, |
| 4458 |
/*check_charsets=*/true, /*check_null=*/false); |
| 4459 |
if (new_index) { |
| 4460 |
ut_ad(new_index->table == index->table); |
| 4461 |
ut_ad(!new_index->to_be_dropped); |
| 4462 |
} else { |
| 4463 |
found = false; |
| 4464 |
} |
| 4465 |
|
| 4466 |
foreign->foreign_index = new_index; |
| 4467 |
} |
| 4468 |
} |
| 4469 |
|
| 4470 |
for (dict_foreign_set::iterator it = table->referenced_set.begin(); |
| 4471 |
it != table->referenced_set.end(); ++it) { |
| 4472 |
foreign = *it; |
| 4473 |
if (foreign->referenced_index == index) { |
| 4474 |
ut_ad(foreign->referenced_table == index->table); |
| 4475 |
|
| 4476 |
dict_index_t *new_index = dict_foreign_find_index( |
| 4477 |
foreign->referenced_table, nullptr, foreign->referenced_col_names, |
| 4478 |
foreign->n_fields, index, |
| 4479 |
/*check_charsets=*/true, /*check_null=*/false); |
| 4480 |
/* There must exist an alternative index, |
| 4481 |
since this must have been checked earlier. */ |
| 4482 |
if (new_index) { |
| 4483 |
ut_ad(new_index->table == index->table); |
| 4484 |
ut_ad(!new_index->to_be_dropped); |
| 4485 |
} else { |
| 4486 |
found = false; |
| 4487 |
} |
| 4488 |
|
| 4489 |
foreign->referenced_index = new_index; |
| 4490 |
} |
| 4491 |
} |
| 4492 |
|
| 4493 |
return (found); |
| 4494 |
} |
| 4495 |
|
| 4496 |
#ifdef UNIV_DEBUG |
| 4497 |
/** Check for duplicate index entries in a table [using the index name] |
| 4498 |
@param[in] table Check for dup indexes in this table |
| 4499 |
@param[in] check Whether and when to allow temporary index names */ |
| 4500 |
void dict_table_check_for_dup_indexes(const dict_table_t *table, |
| 4501 |
enum check_name check) { |
| 4502 |
/* Check for duplicates, ignoring indexes that are marked |
| 4503 |
as to be dropped */ |
| 4504 |
|
| 4505 |
const dict_index_t *index2; |
| 4506 |
|
| 4507 |
ut_ad(dict_sys_mutex_own()); |
| 4508 |
|
| 4509 |
/* The primary index _must_ exist */ |
| 4510 |
ut_a(UT_LIST_GET_LEN(table->indexes) > 0); |
| 4511 |
|
| 4512 |
for (auto index1 : table->indexes) { |
| 4513 |
if (!index1->is_committed()) { |
| 4514 |
ut_a(!index1->is_clustered()); |
| 4515 |
|
| 4516 |
switch (check) { |
| 4517 |
case CHECK_ALL_COMPLETE: |
| 4518 |
ut_error; |
| 4519 |
case CHECK_ABORTED_OK: |
| 4520 |
switch (dict_index_get_online_status(index1)) { |
| 4521 |
case ONLINE_INDEX_COMPLETE: |
| 4522 |
case ONLINE_INDEX_CREATION: |
| 4523 |
ut_error; |
| 4524 |
break; |
| 4525 |
case ONLINE_INDEX_ABORTED: |
| 4526 |
case ONLINE_INDEX_ABORTED_DROPPED: |
| 4527 |
break; |
| 4528 |
} |
| 4529 |
[[fallthrough]]; |
| 4530 |
case CHECK_PARTIAL_OK: |
| 4531 |
break; |
| 4532 |
} |
| 4533 |
} |
| 4534 |
|
| 4535 |
for (index2 = UT_LIST_GET_NEXT(indexes, index1); index2 != nullptr; |
| 4536 |
index2 = UT_LIST_GET_NEXT(indexes, index2)) { |
| 4537 |
ut_ad(index1->is_committed() != index2->is_committed() || |
| 4538 |
strcmp(index1->name, index2->name) != 0); |
| 4539 |
} |
| 4540 |
} |
| 4541 |
} |
| 4542 |
#endif /* UNIV_DEBUG */ |
| 4543 |
|
| 4544 |
/** Converts a database and table name from filesystem encoding (e.g. |
| 4545 |
"@code d@i1b/a@q1b@1Kc @endcode", same format as used in dict_table_t::name) |
| 4546 |
in two strings in UTF8MB3 encoding (e.g. dцb and aюbØc). The output buffers must |
| 4547 |
be at least MAX_DB_UTF8MB3_LEN and MAX_TABLE_UTF8MB3_LEN bytes. |
| 4548 |
@param[in] db_and_table database and table names, |
| 4549 |
e.g. "@code d@i1b/a@q1b@1Kc @endcode" |
| 4550 |
@param[out] db_utf8mb3 database name, e.g. dцb |
| 4551 |
@param[in] db_utf8mb3_size db_utf8mb3 size |
| 4552 |
@param[out] table_utf8mb3 table name, e.g. aюbØc |
| 4553 |
@param[in] table_utf8mb3_size table_utf8mb3 size */ |
| 4554 |
void dict_fs2utf8(const char *db_and_table, char *db_utf8mb3, |
| 4555 |
size_t db_utf8mb3_size, char *table_utf8mb3, |
| 4556 |
size_t table_utf8mb3_size) { |
| 4557 |
char db[MAX_DATABASE_NAME_LEN + 1]; |
| 4558 |
ulint db_len; |
| 4559 |
uint errors; |
| 4560 |
|
| 4561 |
db_len = dict_get_db_name_len(db_and_table); |
| 4562 |
|
| 4563 |
ut_a(db_len <= sizeof(db)); |
| 4564 |
|
| 4565 |
memcpy(db, db_and_table, db_len); |
| 4566 |
db[db_len] = '\0'; |
| 4567 |
|
| 4568 |
strconvert(&my_charset_filename, db, system_charset_info, db_utf8mb3, |
| 4569 |
db_utf8mb3_size, &errors); |
| 4570 |
|
| 4571 |
/* convert each # to @0023 in table name and store the result in buf */ |
| 4572 |
const char *table = dict_remove_db_name(db_and_table); |
| 4573 |
const char *table_p; |
| 4574 |
char buf[MAX_TABLE_NAME_LEN * 5 + 1]; |
| 4575 |
char *buf_p; |
| 4576 |
for (table_p = table, buf_p = buf; table_p[0] != '\0'; table_p++) { |
| 4577 |
if (table_p[0] != '#') { |
| 4578 |
buf_p[0] = table_p[0]; |
| 4579 |
buf_p++; |
| 4580 |
} else { |
| 4581 |
buf_p[0] = '@'; |
| 4582 |
buf_p[1] = '0'; |
| 4583 |
buf_p[2] = '0'; |
| 4584 |
buf_p[3] = '2'; |
| 4585 |
buf_p[4] = '3'; |
| 4586 |
buf_p += 5; |
| 4587 |
} |
| 4588 |
ut_a((size_t)(buf_p - buf) < sizeof(buf)); |
| 4589 |
} |
| 4590 |
buf_p[0] = '\0'; |
| 4591 |
|
| 4592 |
errors = 0; |
| 4593 |
strconvert(&my_charset_filename, buf, system_charset_info, table_utf8mb3, |
| 4594 |
table_utf8mb3_size, &errors); |
| 4595 |
|
| 4596 |
if (errors != 0) { |
| 4597 |
snprintf(table_utf8mb3, table_utf8mb3_size, "%s", table); |
| 4598 |
} |
| 4599 |
} |
| 4600 |
|
| 4601 |
/** Resize the hash tables based on the current buffer pool size. */ |
| 4602 |
void dict_resize() { |
| 4603 |
dict_sys_mutex_enter(); |
| 4604 |
|
| 4605 |
/* all table entries are in table_LRU and table_non_LRU lists */ |
| 4606 |
ut::delete_(dict_sys->table_hash); |
| 4607 |
ut::delete_(dict_sys->table_id_hash); |
| 4608 |
|
| 4609 |
dict_sys->table_hash = ut::new_<hash_table_t>( |
| 4610 |
buf_pool_get_curr_size() / (DICT_POOL_PER_TABLE_HASH * UNIV_WORD_SIZE)); |
| 4611 |
|
| 4612 |
dict_sys->table_id_hash = ut::new_<hash_table_t>( |
| 4613 |
buf_pool_get_curr_size() / (DICT_POOL_PER_TABLE_HASH * UNIV_WORD_SIZE)); |
| 4614 |
|
| 4615 |
for (auto table : dict_sys->table_LRU) { |
| 4616 |
const auto name_hash_value = ut::hash_string(table->name.m_name); |
| 4617 |
const auto index_id_hash_value = ut::hash_uint64(table->id); |
| 4618 |
|
| 4619 |
HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, name_hash_value, |
| 4620 |
table); |
| 4621 |
|
| 4622 |
HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash, |
| 4623 |
index_id_hash_value, table); |
| 4624 |
} |
| 4625 |
|
| 4626 |
for (auto table : dict_sys->table_non_LRU) { |
| 4627 |
const auto name_hash_value = ut::hash_string(table->name.m_name); |
| 4628 |
const auto index_id_hash_value = ut::hash_uint64(table->id); |
| 4629 |
|
| 4630 |
HASH_INSERT(dict_table_t, name_hash, dict_sys->table_hash, name_hash_value, |
| 4631 |
table); |
| 4632 |
|
| 4633 |
HASH_INSERT(dict_table_t, id_hash, dict_sys->table_id_hash, |
| 4634 |
index_id_hash_value, table); |
| 4635 |
} |
| 4636 |
|
| 4637 |
dict_sys_mutex_exit(); |
| 4638 |
} |
| 4639 |
#else |
| 4640 |
/* Dummy implementation for satisfying compiler for MEB. This can be removed |
| 4641 |
once the callers in dict0dd.cc is factored out from MEB compilation. */ |
| 4642 |
/* purecov: begin deadcode */ |
| 4643 |
void dict_set_corrupted(dict_index_t *index [[maybe_unused]]) {} |
| 4644 |
/* purecov: end */ |
| 4645 |
#endif /* !UNIV_HOTBACKUP */ |
| 4646 |
|
| 4647 |
/** Closes the data dictionary module. */ |
| 4648 |
void dict_close(void) { |
| 4649 |
if (dict_sys == nullptr) { |
| 4650 |
/* This should only happen if a failure occurred |
| 4651 |
during redo log processing. */ |
| 4652 |
return; |
| 4653 |
} |
| 4654 |
|
| 4655 |
/* Acquire only because it's a pre-condition. */ |
| 4656 |
dict_sys_mutex_enter(); |
| 4657 |
|
| 4658 |
if (dict_sys->table_stats != nullptr) { |
| 4659 |
dict_table_close(dict_sys->table_stats, true, false); |
| 4660 |
} |
| 4661 |
if (dict_sys->index_stats != nullptr) { |
| 4662 |
dict_table_close(dict_sys->index_stats, true, false); |
| 4663 |
} |
| 4664 |
if (dict_sys->dynamic_metadata != nullptr) { |
| 4665 |
dict_table_close(dict_sys->dynamic_metadata, true, false); |
| 4666 |
} |
| 4667 |
if (dict_sys->ddl_log) { |
| 4668 |
dict_table_close(dict_sys->ddl_log, true, false); |
| 4669 |
} |
| 4670 |
|
| 4671 |
#ifndef UNIV_HOTBACKUP |
| 4672 |
/* Free the hash elements. We don't remove them from the table |
| 4673 |
because we are going to destroy the table anyway. */ |
| 4674 |
for (ulint i = 0; i < hash_get_n_cells(dict_sys->table_id_hash); i++) { |
| 4675 |
dict_table_t *table; |
| 4676 |
|
| 4677 |
table = |
| 4678 |
static_cast<dict_table_t *>(hash_get_first(dict_sys->table_hash, i)); |
| 4679 |
|
| 4680 |
while (table) { |
| 4681 |
dict_table_t *prev_table = table; |
| 4682 |
|
| 4683 |
table = static_cast<dict_table_t *>(HASH_GET_NEXT(name_hash, prev_table)); |
| 4684 |
ut_ad(prev_table->magic_n == DICT_TABLE_MAGIC_N); |
| 4685 |
dict_table_remove_from_cache(prev_table); |
| 4686 |
} |
| 4687 |
} |
| 4688 |
#endif /* !UNIV_HOTBACKUP */ |
| 4689 |
|
| 4690 |
ut::delete_(dict_sys->table_hash); |
| 4691 |
|
| 4692 |
/* The elements are the same instance as in dict_sys->table_hash, |
| 4693 |
therefore we don't delete the individual elements. */ |
| 4694 |
ut::delete_(dict_sys->table_id_hash); |
| 4695 |
|
| 4696 |
#ifndef UNIV_HOTBACKUP |
| 4697 |
dict_ind_free(); |
| 4698 |
#endif /* !UNIV_HOTBACKUP */ |
| 4699 |
|
| 4700 |
dict_sys_mutex_exit(); |
| 4701 |
dict_sys_mutex_free(); |
| 4702 |
|
| 4703 |
rw_lock_free(dict_operation_lock); |
| 4704 |
|
| 4705 |
ut::free(dict_operation_lock); |
| 4706 |
dict_operation_lock = nullptr; |
| 4707 |
|
| 4708 |
mutex_free(&dict_foreign_err_mutex); |
| 4709 |
|
| 4710 |
#ifndef UNIV_HOTBACKUP |
| 4711 |
if (dict_foreign_err_file != nullptr) { |
| 4712 |
fclose(dict_foreign_err_file); |
| 4713 |
dict_foreign_err_file = nullptr; |
| 4714 |
} |
| 4715 |
#endif /* !UNIV_HOTBACKUP */ |
| 4716 |
|
| 4717 |
ut_ad(dict_sys->size == 0); |
| 4718 |
|
| 4719 |
ut::free(dict_sys); |
| 4720 |
dict_sys = nullptr; |
| 4721 |
} |
| 4722 |
|
| 4723 |
#ifndef UNIV_HOTBACKUP |
| 4724 |
#ifdef UNIV_DEBUG |
| 4725 |
/** Validate the dictionary table LRU list. |
| 4726 |
@return true if valid */ |
| 4727 |
static bool dict_lru_validate() { |
| 4728 |
ut_ad(dict_sys_mutex_own()); |
| 4729 |
|
| 4730 |
for (auto table : dict_sys->table_LRU) { |
| 4731 |
ut_a(table->can_be_evicted); |
| 4732 |
} |
| 4733 |
|
| 4734 |
for (auto table : dict_sys->table_non_LRU) { |
| 4735 |
ut_a(!table->can_be_evicted); |
| 4736 |
} |
| 4737 |
|
| 4738 |
return true; |
| 4739 |
} |
| 4740 |
|
| 4741 |
static bool dict_lru_find_table(const dict_table_t *find_table) { |
| 4742 |
ut_ad(find_table != nullptr); |
| 4743 |
ut_ad(dict_sys_mutex_own()); |
| 4744 |
|
| 4745 |
for (auto table : dict_sys->table_LRU) { |
| 4746 |
ut_a(table->can_be_evicted); |
| 4747 |
|
| 4748 |
if (table == find_table) { |
| 4749 |
return true; |
| 4750 |
} |
| 4751 |
} |
| 4752 |
|
| 4753 |
return false; |
| 4754 |
} |
| 4755 |
|
| 4756 |
static bool dict_non_lru_find_table(const dict_table_t *find_table) { |
| 4757 |
ut_ad(find_table != nullptr); |
| 4758 |
ut_ad(dict_sys_mutex_own()); |
| 4759 |
|
| 4760 |
for (auto table : dict_sys->table_non_LRU) { |
| 4761 |
ut_a(!table->can_be_evicted); |
| 4762 |
|
| 4763 |
if (table == find_table) { |
| 4764 |
return true; |
| 4765 |
} |
| 4766 |
} |
| 4767 |
|
| 4768 |
return false; |
| 4769 |
} |
| 4770 |
#endif /* UNIV_DEBUG */ |
| 4771 |
/** Check an index to see whether its first fields are the columns in the array, |
| 4772 |
in the same order and is not marked for deletion and is not the same |
| 4773 |
as types_idx. |
| 4774 |
@return true if the index qualifies, otherwise false */ |
| 4775 |
bool dict_foreign_qualify_index( |
| 4776 |
const dict_table_t *table, /*!< in: table */ |
| 4777 |
const char **col_names, |
| 4778 |
/*!< in: column names, or NULL |
| 4779 |
to use table->col_names */ |
| 4780 |
const char **columns, /*!< in: array of column names */ |
| 4781 |
ulint n_cols, /*!< in: number of columns */ |
| 4782 |
const dict_index_t *index, /*!< in: index to check */ |
| 4783 |
const dict_index_t *types_idx, |
| 4784 |
/*!< in: NULL or an index |
| 4785 |
whose types the column types |
| 4786 |
must match */ |
| 4787 |
bool check_charsets, |
| 4788 |
/*!< in: whether to check |
| 4789 |
charsets. only has an effect |
| 4790 |
if types_idx != NULL */ |
| 4791 |
ulint check_null) |
| 4792 |
/*!< in: nonzero if none of |
| 4793 |
the columns must be declared |
| 4794 |
NOT NULL */ |
| 4795 |
{ |
| 4796 |
if (dict_index_get_n_fields(index) < n_cols) { |
| 4797 |
return (false); |
| 4798 |
} |
| 4799 |
|
| 4800 |
for (ulint i = 0; i < n_cols; i++) { |
| 4801 |
dict_field_t *field; |
| 4802 |
const char *col_name; |
| 4803 |
ulint col_no; |
| 4804 |
|
| 4805 |
field = index->get_field(i); |
| 4806 |
col_no = dict_col_get_no(field->col); |
| 4807 |
|
| 4808 |
if (field->prefix_len != 0) { |
| 4809 |
/* We do not accept column prefix |
| 4810 |
indexes here */ |
| 4811 |
return (false); |
| 4812 |
} |
| 4813 |
|
| 4814 |
if (check_null && (field->col->prtype & DATA_NOT_NULL)) { |
| 4815 |
return (false); |
| 4816 |
} |
| 4817 |
|
| 4818 |
col_name = col_names ? col_names[col_no] |
| 4819 |
: (field->col->is_virtual() |
| 4820 |
? dict_table_get_v_col_name_mysql(table, col_no) |
| 4821 |
: table->get_col_name(col_no)); |
| 4822 |
|
| 4823 |
if (0 != innobase_strcasecmp(columns[i], col_name)) { |
| 4824 |
return (false); |
| 4825 |
} |
| 4826 |
|
| 4827 |
if (types_idx && |
| 4828 |
!cmp_cols_are_equal(index->get_col(i), types_idx->get_col(i), |
| 4829 |
check_charsets)) { |
| 4830 |
return (false); |
| 4831 |
} |
| 4832 |
} |
| 4833 |
|
| 4834 |
return (true); |
| 4835 |
} |
| 4836 |
|
| 4837 |
/** Update the state of compression failure padding heuristics. This is |
| 4838 |
called whenever a compression operation succeeds or fails. |
| 4839 |
The caller must be holding info->mutex */ |
| 4840 |
static void dict_index_zip_pad_update( |
| 4841 |
zip_pad_info_t *info, /*!< in/out: info to be updated */ |
| 4842 |
ulint zip_threshold) /*!< in: zip threshold value */ |
| 4843 |
{ |
| 4844 |
ulint total; |
| 4845 |
ulint fail_pct; |
| 4846 |
|
| 4847 |
ut_ad(info); |
| 4848 |
|
| 4849 |
total = info->success + info->failure; |
| 4850 |
|
| 4851 |
ut_ad(total > 0); |
| 4852 |
|
| 4853 |
if (zip_threshold == 0) { |
| 4854 |
/* User has just disabled the padding. */ |
| 4855 |
return; |
| 4856 |
} |
| 4857 |
|
| 4858 |
if (total < ZIP_PAD_ROUND_LEN) { |
| 4859 |
/* We are in middle of a round. Do nothing. */ |
| 4860 |
return; |
| 4861 |
} |
| 4862 |
|
| 4863 |
/* We are at a 'round' boundary. Reset the values but first |
| 4864 |
calculate fail rate for our heuristic. */ |
| 4865 |
fail_pct = (info->failure * 100) / total; |
| 4866 |
info->failure = 0; |
| 4867 |
info->success = 0; |
| 4868 |
|
| 4869 |
if (fail_pct > zip_threshold) { |
| 4870 |
/* Compression failures are more then user defined |
| 4871 |
threshold. Increase the pad size to reduce chances of |
| 4872 |
compression failures. */ |
| 4873 |
ut_ad(info->pad % ZIP_PAD_INCR == 0); |
| 4874 |
|
| 4875 |
/* Only do increment if it won't increase padding |
| 4876 |
beyond max pad size. */ |
| 4877 |
if (info->pad + ZIP_PAD_INCR < (UNIV_PAGE_SIZE * zip_pad_max) / 100) { |
| 4878 |
/* Use atomics even though we have the mutex. |
| 4879 |
This is to ensure that we are able to read |
| 4880 |
info->pad atomically. */ |
| 4881 |
info->pad.fetch_add(ZIP_PAD_INCR); |
| 4882 |
|
| 4883 |
MONITOR_INC(MONITOR_PAD_INCREMENTS); |
| 4884 |
} |
| 4885 |
|
| 4886 |
info->n_rounds = 0; |
| 4887 |
|
| 4888 |
} else { |
| 4889 |
/* Failure rate was OK. Another successful round |
| 4890 |
completed. */ |
| 4891 |
++info->n_rounds; |
| 4892 |
|
| 4893 |
/* If enough successful rounds are completed with |
| 4894 |
compression failure rate in control, decrease the |
| 4895 |
padding. */ |
| 4896 |
if (info->n_rounds >= ZIP_PAD_SUCCESSFUL_ROUND_LIMIT && info->pad > 0) { |
| 4897 |
ut_ad(info->pad % ZIP_PAD_INCR == 0); |
| 4898 |
/* Use atomics even though we have the mutex. |
| 4899 |
This is to ensure that we are able to read |
| 4900 |
info->pad atomically. */ |
| 4901 |
info->pad.fetch_sub(ZIP_PAD_INCR); |
| 4902 |
|
| 4903 |
info->n_rounds = 0; |
| 4904 |
|
| 4905 |
MONITOR_INC(MONITOR_PAD_DECREMENTS); |
| 4906 |
} |
| 4907 |
} |
| 4908 |
} |
| 4909 |
|
| 4910 |
/** This function should be called whenever a page is successfully |
| 4911 |
compressed. Updates the compression padding information. */ |
| 4912 |
void dict_index_zip_success( |
| 4913 |
dict_index_t *index) /*!< in/out: index to be updated. */ |
| 4914 |
{ |
| 4915 |
ut_ad(index); |
| 4916 |
|
| 4917 |
ulint zip_threshold = zip_failure_threshold_pct; |
| 4918 |
if (!zip_threshold) { |
| 4919 |
/* Disabled by user. */ |
| 4920 |
return; |
| 4921 |
} |
| 4922 |
|
| 4923 |
dict_index_zip_pad_lock(index); |
| 4924 |
++index->zip_pad.success; |
| 4925 |
dict_index_zip_pad_update(&index->zip_pad, zip_threshold); |
| 4926 |
dict_index_zip_pad_unlock(index); |
| 4927 |
} |
| 4928 |
|
| 4929 |
/** This function should be called whenever a page compression attempt |
| 4930 |
fails. Updates the compression padding information. */ |
| 4931 |
void dict_index_zip_failure( |
| 4932 |
dict_index_t *index) /*!< in/out: index to be updated. */ |
| 4933 |
{ |
| 4934 |
ut_ad(index); |
| 4935 |
|
| 4936 |
ulint zip_threshold = zip_failure_threshold_pct; |
| 4937 |
if (!zip_threshold) { |
| 4938 |
/* Disabled by user. */ |
| 4939 |
return; |
| 4940 |
} |
| 4941 |
|
| 4942 |
dict_index_zip_pad_lock(index); |
| 4943 |
++index->zip_pad.failure; |
| 4944 |
dict_index_zip_pad_update(&index->zip_pad, zip_threshold); |
| 4945 |
dict_index_zip_pad_unlock(index); |
| 4946 |
} |
| 4947 |
|
| 4948 |
/** Return the optimal page size, for which page will likely compress. |
| 4949 |
@return page size beyond which page might not compress */ |
| 4950 |
ulint dict_index_zip_pad_optimal_page_size( |
| 4951 |
dict_index_t *index) /*!< in: index for which page size |
| 4952 |
is requested */ |
| 4953 |
{ |
| 4954 |
ulint pad; |
| 4955 |
ulint min_sz; |
| 4956 |
ulint sz; |
| 4957 |
|
| 4958 |
ut_ad(index); |
| 4959 |
|
| 4960 |
if (!zip_failure_threshold_pct) { |
| 4961 |
/* Disabled by user. */ |
| 4962 |
return (UNIV_PAGE_SIZE); |
| 4963 |
} |
| 4964 |
|
| 4965 |
/* We use atomics to read index->zip_pad.pad. Here we use zero |
| 4966 |
as increment as are not changing the value of the 'pad'. */ |
| 4967 |
|
| 4968 |
pad = index->zip_pad.pad.load(); |
| 4969 |
|
| 4970 |
ut_ad(pad < UNIV_PAGE_SIZE); |
| 4971 |
sz = UNIV_PAGE_SIZE - pad; |
| 4972 |
|
| 4973 |
/* Min size allowed by user. */ |
| 4974 |
ut_ad(zip_pad_max < 100); |
| 4975 |
min_sz = (UNIV_PAGE_SIZE * (100 - zip_pad_max)) / 100; |
| 4976 |
|
| 4977 |
return (std::max(sz, min_sz)); |
| 4978 |
} |
| 4979 |
|
| 4980 |
/** Convert a 32 bit integer table flags to the 32 bit FSP Flags. |
| 4981 |
Fsp Flags are written into the tablespace header at the offset |
| 4982 |
FSP_SPACE_FLAGS and are also stored in the fil_space_t::flags field. |
| 4983 |
The following chart shows the translation of the low order bit. |
| 4984 |
Other bits are the same. |
| 4985 |
========================= Low order bit ========================== |
| 4986 |
| REDUNDANT | COMPACT | COMPRESSED | DYNAMIC |
| 4987 |
dict_table_t::flags | 0 | 1 | 1 | 1 |
| 4988 |
fil_space_t::flags | 0 | 0 | 1 | 1 |
| 4989 |
================================================================== |
| 4990 |
@param[in] table_flags dict_table_t::flags |
| 4991 |
@return tablespace flags (fil_space_t::flags) */ |
| 4992 |
uint32_t dict_tf_to_fsp_flags(uint32_t table_flags) { |
| 4993 |
DBUG_EXECUTE_IF("dict_tf_to_fsp_flags_failure", return (UINT32_UNDEFINED);); |
| 4994 |
|
| 4995 |
bool has_atomic_blobs = DICT_TF_HAS_ATOMIC_BLOBS(table_flags); |
| 4996 |
page_size_t page_size = dict_tf_get_page_size(table_flags); |
| 4997 |
bool has_data_dir = DICT_TF_HAS_DATA_DIR(table_flags); |
| 4998 |
bool is_shared = DICT_TF_HAS_SHARED_SPACE(table_flags); |
| 4999 |
|
| 5000 |
ut_ad(!page_size.is_compressed() || has_atomic_blobs); |
| 5001 |
|
| 5002 |
/* General tablespaces that are not compressed do not get the |
| 5003 |
flags for dynamic row format (ATOMIC_BLOBS) */ |
| 5004 |
if (is_shared && !page_size.is_compressed()) { |
| 5005 |
has_atomic_blobs = false; |
| 5006 |
} |
| 5007 |
|
| 5008 |
uint32_t fsp_flags = fsp_flags_init(page_size, has_atomic_blobs, has_data_dir, |
| 5009 |
is_shared, false); |
| 5010 |
|
| 5011 |
return (fsp_flags); |
| 5012 |
} |
| 5013 |
|
| 5014 |
/** Convert table flag to row format string. |
| 5015 |
@return row format name. */ |
| 5016 |
const char *dict_tf_to_row_format_string( |
| 5017 |
uint32_t table_flag) /*!< in: row format setting */ |
| 5018 |
{ |
| 5019 |
switch (dict_tf_get_rec_format(table_flag)) { |
| 5020 |
case REC_FORMAT_REDUNDANT: |
| 5021 |
return ("ROW_TYPE_REDUNDANT"); |
| 5022 |
case REC_FORMAT_COMPACT: |
| 5023 |
return ("ROW_TYPE_COMPACT"); |
| 5024 |
case REC_FORMAT_COMPRESSED: |
| 5025 |
return ("ROW_TYPE_COMPRESSED"); |
| 5026 |
case REC_FORMAT_DYNAMIC: |
| 5027 |
return ("ROW_TYPE_DYNAMIC"); |
| 5028 |
} |
| 5029 |
|
| 5030 |
ut_error; |
| 5031 |
} |
| 5032 |
|
| 5033 |
/** Determine the extent size (in pages) for the given table |
| 5034 |
@param[in] table the table whose extent size is being |
| 5035 |
calculated. |
| 5036 |
@return extent size in pages (256, 128 or 64) */ |
| 5037 |
page_no_t dict_table_extent_size(const dict_table_t *table) { |
| 5038 |
const ulint mb_1 = 1024 * 1024; |
| 5039 |
const ulint mb_2 = 2 * mb_1; |
| 5040 |
const ulint mb_4 = 4 * mb_1; |
| 5041 |
|
| 5042 |
page_size_t page_size = dict_table_page_size(table); |
| 5043 |
page_no_t pages_in_extent = FSP_EXTENT_SIZE; |
| 5044 |
|
| 5045 |
if (page_size.is_compressed()) { |
| 5046 |
ulint disk_page_size = page_size.physical(); |
| 5047 |
|
| 5048 |
switch (disk_page_size) { |
| 5049 |
case 1024: |
| 5050 |
pages_in_extent = mb_1 / 1024; |
| 5051 |
break; |
| 5052 |
case 2048: |
| 5053 |
pages_in_extent = mb_1 / 2048; |
| 5054 |
break; |
| 5055 |
case 4096: |
| 5056 |
pages_in_extent = mb_1 / 4096; |
| 5057 |
break; |
| 5058 |
case 8192: |
| 5059 |
pages_in_extent = mb_1 / 8192; |
| 5060 |
break; |
| 5061 |
case 16384: |
| 5062 |
pages_in_extent = mb_1 / 16384; |
| 5063 |
break; |
| 5064 |
case 32768: |
| 5065 |
pages_in_extent = mb_2 / 32768; |
| 5066 |
break; |
| 5067 |
case 65536: |
| 5068 |
pages_in_extent = mb_4 / 65536; |
| 5069 |
break; |
| 5070 |
default: |
| 5071 |
ut_d(ut_error); |
| 5072 |
} |
| 5073 |
} |
| 5074 |
|
| 5075 |
return (pages_in_extent); |
| 5076 |
} |
| 5077 |
|
| 5078 |
/** Default constructor */ |
| 5079 |
DDTableBuffer::DDTableBuffer() { |
| 5080 |
init(); |
| 5081 |
|
| 5082 |
/* Check if we need to recover it, in case of crash */ |
| 5083 |
btr_truncate_recover(m_index); |
| 5084 |
} |
| 5085 |
|
| 5086 |
/** Destructor */ |
| 5087 |
DDTableBuffer::~DDTableBuffer() { close(); } |
| 5088 |
|
| 5089 |
/* Create the search and replace tuples */ |
| 5090 |
void DDTableBuffer::create_tuples() { |
| 5091 |
const dict_col_t *col; |
| 5092 |
dfield_t *dfield; |
| 5093 |
byte *sys_buf; |
| 5094 |
byte *id_buf; |
| 5095 |
|
| 5096 |
id_buf = static_cast<byte *>(mem_heap_alloc(m_heap, 8)); |
| 5097 |
memset(id_buf, 0, sizeof *id_buf); |
| 5098 |
|
| 5099 |
m_search_tuple = dtuple_create(m_heap, 1); |
| 5100 |
dict_index_copy_types(m_search_tuple, m_index, 1); |
| 5101 |
|
| 5102 |
dfield = dtuple_get_nth_field(m_search_tuple, 0); |
| 5103 |
dfield_set_data(dfield, id_buf, 8); |
| 5104 |
|
| 5105 |
/* Allocate another memory for this tuple */ |
| 5106 |
id_buf = static_cast<byte *>(mem_heap_alloc(m_heap, 8)); |
| 5107 |
memset(id_buf, 0, sizeof *id_buf); |
| 5108 |
|
| 5109 |
m_replace_tuple = dtuple_create(m_heap, N_COLS); |
| 5110 |
dict_table_copy_types(m_replace_tuple, m_index->table); |
| 5111 |
|
| 5112 |
dfield = dtuple_get_nth_field(m_replace_tuple, TABLE_ID_FIELD_NO); |
| 5113 |
dfield_set_data(dfield, id_buf, 8); |
| 5114 |
|
| 5115 |
/* Initialize system fields, we always write fake value. */ |
| 5116 |
sys_buf = static_cast<byte *>(mem_heap_alloc(m_heap, 8)); |
| 5117 |
memset(sys_buf, 0xFF, 8); |
| 5118 |
|
| 5119 |
col = m_index->table->get_sys_col(DATA_ROW_ID); |
| 5120 |
dfield = dtuple_get_nth_field(m_replace_tuple, dict_col_get_no(col)); |
| 5121 |
dfield_set_data(dfield, sys_buf, DATA_ROW_ID_LEN); |
| 5122 |
|
| 5123 |
col = m_index->table->get_sys_col(DATA_TRX_ID); |
| 5124 |
dfield = dtuple_get_nth_field(m_replace_tuple, dict_col_get_no(col)); |
| 5125 |
dfield_set_data(dfield, sys_buf, DATA_TRX_ID_LEN); |
| 5126 |
|
| 5127 |
col = m_index->table->get_sys_col(DATA_ROLL_PTR); |
| 5128 |
dfield = dtuple_get_nth_field(m_replace_tuple, dict_col_get_no(col)); |
| 5129 |
dfield_set_data(dfield, sys_buf, DATA_ROLL_PTR_LEN); |
| 5130 |
} |
| 5131 |
|
| 5132 |
/** Initialize the in-memory index */ |
| 5133 |
void DDTableBuffer::init() { |
| 5134 |
if (dict_sys->dynamic_metadata != nullptr) { |
| 5135 |
ut_ad(dict_table_is_comp(dict_sys->dynamic_metadata)); |
| 5136 |
m_index = dict_sys->dynamic_metadata->first_index(); |
| 5137 |
} else { |
| 5138 |
open(); |
| 5139 |
dict_sys->dynamic_metadata = m_index->table; |
| 5140 |
} |
| 5141 |
|
| 5142 |
ut_ad(m_index->next() == nullptr); |
| 5143 |
ut_ad(m_index->n_uniq == 1); |
| 5144 |
ut_ad(N_FIELDS == m_index->n_fields); |
| 5145 |
ut_ad(m_index->table->n_cols == N_COLS); |
| 5146 |
|
| 5147 |
/* We don't need AHI for this table */ |
| 5148 |
m_index->disable_ahi = true; |
| 5149 |
m_index->cached = true; |
| 5150 |
|
| 5151 |
m_heap = mem_heap_create(500, UT_LOCATION_HERE); |
| 5152 |
m_dynamic_heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 5153 |
m_replace_heap = mem_heap_create(100, UT_LOCATION_HERE); |
| 5154 |
|
| 5155 |
create_tuples(); |
| 5156 |
} |
| 5157 |
|
| 5158 |
/** Open the mysql.innodb_dynamic_metadata when DD is not fully up */ |
| 5159 |
void DDTableBuffer::open() { |
| 5160 |
ut_ad(dict_sys->dynamic_metadata == nullptr); |
| 5161 |
|
| 5162 |
dict_table_t *table = nullptr; |
| 5163 |
/* Keep it the same with definition of mysql/innodb_dynamic_metadata */ |
| 5164 |
const char *table_name = "mysql/innodb_dynamic_metadata"; |
| 5165 |
const char *table_id_name = "table_id"; |
| 5166 |
const char *version_name = "version"; |
| 5167 |
const char *metadata_name = "metadata"; |
| 5168 |
ulint prtype = 0; |
| 5169 |
mem_heap_t *heap = mem_heap_create(256, UT_LOCATION_HERE); |
| 5170 |
|
| 5171 |
/* Get the root page number according to index id, this is |
| 5172 |
same with what we do in ha_innobsae::get_se_private_data() */ |
| 5173 |
page_no_t root = 4; |
| 5174 |
space_index_t index_id = 0; |
| 5175 |
while (true) { |
| 5176 |
if (fsp_is_inode_page(root)) { |
| 5177 |
++root; |
| 5178 |
ut_ad(!fsp_is_inode_page(root)); |
| 5179 |
} |
| 5180 |
|
| 5181 |
if (++index_id == dict_sys_t::s_dynamic_meta_index_id) { |
| 5182 |
break; |
| 5183 |
} |
| 5184 |
|
| 5185 |
++root; |
| 5186 |
} |
| 5187 |
|
| 5188 |
table = dict_mem_table_create(table_name, dict_sys_t::s_dict_space_id, |
| 5189 |
N_USER_COLS, 0, 0, 0, 0); |
| 5190 |
|
| 5191 |
table->id = dict_sys_t::s_dynamic_meta_table_id; |
| 5192 |
table->is_dd_table = true; |
| 5193 |
table->dd_space_id = dict_sys_t::s_dd_dict_space_id; |
| 5194 |
table->flags |= DICT_TF_COMPACT | (1 << DICT_TF_POS_SHARED_SPACE) | |
| 5195 |
(1 << DICT_TF_POS_ATOMIC_BLOBS); |
| 5196 |
|
| 5197 |
prtype = dtype_form_prtype( |
| 5198 |
MYSQL_TYPE_LONGLONG | DATA_NOT_NULL | DATA_UNSIGNED | DATA_BINARY_TYPE, |
| 5199 |
0); |
| 5200 |
|
| 5201 |
dict_mem_table_add_col(table, heap, table_id_name, DATA_INT, prtype, 8, true); |
| 5202 |
dict_mem_table_add_col(table, heap, version_name, DATA_INT, prtype, 8, true); |
| 5203 |
|
| 5204 |
prtype = |
| 5205 |
dtype_form_prtype(MYSQL_TYPE_BLOB | DATA_NOT_NULL | DATA_BINARY_TYPE, 63); |
| 5206 |
|
| 5207 |
dict_mem_table_add_col(table, heap, metadata_name, DATA_BLOB, prtype, 10, |
| 5208 |
true); |
| 5209 |
|
| 5210 |
dict_table_add_system_columns(table, heap); |
| 5211 |
|
| 5212 |
m_index = |
| 5213 |
dict_mem_index_create(table_name, "PRIMARY", dict_sys_t::s_dict_space_id, |
| 5214 |
DICT_CLUSTERED | DICT_UNIQUE, 1); |
| 5215 |
|
| 5216 |
dict_index_add_col(m_index, table, &table->cols[0], 0, true); |
| 5217 |
|
| 5218 |
m_index->id = dict_sys_t::s_dynamic_meta_index_id; |
| 5219 |
m_index->n_uniq = 1; |
| 5220 |
|
| 5221 |
dberr_t err = dict_index_add_to_cache(table, m_index, root, false); |
| 5222 |
if (err != DB_SUCCESS) { |
| 5223 |
ut_d(ut_error); |
| 5224 |
} |
| 5225 |
|
| 5226 |
m_index = table->first_index(); |
| 5227 |
|
| 5228 |
dict_sys_mutex_enter(); |
| 5229 |
|
| 5230 |
dict_table_add_to_cache(table, true); |
| 5231 |
|
| 5232 |
table->acquire(); |
| 5233 |
|
| 5234 |
dict_sys_mutex_exit(); |
| 5235 |
|
| 5236 |
mem_heap_free(heap); |
| 5237 |
} |
| 5238 |
|
| 5239 |
/** Initialize the id field of tuple |
| 5240 |
@param[out] tuple the tuple to be initialized |
| 5241 |
@param[in] id table id */ |
| 5242 |
void DDTableBuffer::init_tuple_with_id(dtuple_t *tuple, table_id_t id) { |
| 5243 |
dfield_t *dfield = dtuple_get_nth_field(tuple, TABLE_ID_FIELD_NO); |
| 5244 |
void *data = dfield->data; |
| 5245 |
|
| 5246 |
mach_write_to_8(data, id); |
| 5247 |
dfield_set_data(dfield, data, 8); |
| 5248 |
} |
| 5249 |
|
| 5250 |
/** Free the things initialized in init() */ |
| 5251 |
void DDTableBuffer::close() { |
| 5252 |
mem_heap_free(m_heap); |
| 5253 |
mem_heap_free(m_dynamic_heap); |
| 5254 |
mem_heap_free(m_replace_heap); |
| 5255 |
|
| 5256 |
m_search_tuple = nullptr; |
| 5257 |
m_replace_tuple = nullptr; |
| 5258 |
} |
| 5259 |
|
| 5260 |
/** Prepare for a update on METADATA field |
| 5261 |
@param[in] entry clustered index entry to replace rec |
| 5262 |
@param[in] rec clustered index record |
| 5263 |
@return update vector of differing fields without system columns, |
| 5264 |
or NULL if there isn't any different field */ |
| 5265 |
upd_t *DDTableBuffer::update_set_metadata(const dtuple_t *entry, |
| 5266 |
const rec_t *rec) { |
| 5267 |
ulint offsets[N_FIELDS + 1 + REC_OFFS_HEADER_SIZE]; |
| 5268 |
upd_field_t *upd_field; |
| 5269 |
const dfield_t *version_field; |
| 5270 |
const dfield_t *metadata_dfield; |
| 5271 |
const byte *metadata; |
| 5272 |
const byte *version; |
| 5273 |
ulint len; |
| 5274 |
upd_t *update; |
| 5275 |
|
| 5276 |
rec_offs_init(offsets); |
| 5277 |
rec_offs_set_n_fields(offsets, N_FIELDS); |
| 5278 |
rec_init_offsets_comp_ordinary(rec, false, m_index, offsets); |
| 5279 |
ut_ad(!rec_get_deleted_flag(rec, 1)); |
| 5280 |
|
| 5281 |
version = rec_get_nth_field(nullptr, rec, offsets, VERSION_FIELD_NO, &len); |
| 5282 |
ut_ad(len == 8); |
| 5283 |
version_field = dtuple_get_nth_field(entry, VERSION_FIELD_NO); |
| 5284 |
|
| 5285 |
metadata = rec_get_nth_field(nullptr, rec, offsets, METADATA_FIELD_NO, &len); |
| 5286 |
metadata_dfield = dtuple_get_nth_field(entry, METADATA_FIELD_NO); |
| 5287 |
|
| 5288 |
if (dfield_data_is_binary_equal(version_field, 8, version) && |
| 5289 |
dfield_data_is_binary_equal(metadata_dfield, len, metadata)) { |
| 5290 |
return (nullptr); |
| 5291 |
} |
| 5292 |
|
| 5293 |
update = upd_create(2, m_replace_heap); |
| 5294 |
|
| 5295 |
upd_field = upd_get_nth_field(update, 0); |
| 5296 |
dfield_copy(&upd_field->new_val, version_field); |
| 5297 |
upd_field_set_field_no(upd_field, VERSION_FIELD_NO, m_index); |
| 5298 |
|
| 5299 |
upd_field = upd_get_nth_field(update, 1); |
| 5300 |
dfield_copy(&upd_field->new_val, metadata_dfield); |
| 5301 |
upd_field_set_field_no(upd_field, METADATA_FIELD_NO, m_index); |
| 5302 |
|
| 5303 |
ut_d(update->validate()); |
| 5304 |
|
| 5305 |
return (update); |
| 5306 |
} |
| 5307 |
|
| 5308 |
/** Replace the dynamic metadata for a specific table |
| 5309 |
@param[in] id table id |
| 5310 |
@param[in] version table dynamic metadata version |
| 5311 |
@param[in] metadata the metadata we want to replace |
| 5312 |
@param[in] len the metadata length |
| 5313 |
@return DB_SUCCESS or error code */ |
| 5314 |
dberr_t DDTableBuffer::replace(table_id_t id, uint64_t version, |
| 5315 |
const byte *metadata, size_t len) { |
| 5316 |
dtuple_t *entry; |
| 5317 |
dfield_t *dfield; |
| 5318 |
btr_pcur_t pcur; |
| 5319 |
mtr_t mtr; |
| 5320 |
byte ver[8]; |
| 5321 |
dberr_t error; |
| 5322 |
|
| 5323 |
ut_ad(mutex_own(&dict_persist->mutex)); |
| 5324 |
|
| 5325 |
init_tuple_with_id(m_search_tuple, id); |
| 5326 |
|
| 5327 |
init_tuple_with_id(m_replace_tuple, id); |
| 5328 |
mach_write_to_8(ver, version); |
| 5329 |
dfield = dtuple_get_nth_field(m_replace_tuple, VERSION_COL_NO); |
| 5330 |
dfield_set_data(dfield, ver, sizeof ver); |
| 5331 |
dfield = dtuple_get_nth_field(m_replace_tuple, METADATA_COL_NO); |
| 5332 |
dfield_set_data(dfield, metadata, len); |
| 5333 |
/* Other system fields have been initialized */ |
| 5334 |
|
| 5335 |
entry = |
| 5336 |
row_build_index_entry(m_replace_tuple, nullptr, m_index, m_replace_heap); |
| 5337 |
|
| 5338 |
/* Start to search for the to-be-replaced tuple */ |
| 5339 |
mtr.start(); |
| 5340 |
|
| 5341 |
pcur.open(m_index, 0, m_search_tuple, PAGE_CUR_LE, BTR_MODIFY_TREE, &mtr, |
| 5342 |
UT_LOCATION_HERE); |
| 5343 |
|
| 5344 |
if (page_rec_is_infimum(pcur.get_rec()) || |
| 5345 |
pcur.get_low_match() < m_index->n_uniq) { |
| 5346 |
/* The record was not found, so it's the first time we |
| 5347 |
add the row for this table of id, we need to insert it */ |
| 5348 |
static const ulint flags = (BTR_CREATE_FLAG | BTR_NO_LOCKING_FLAG | |
| 5349 |
BTR_NO_UNDO_LOG_FLAG | BTR_KEEP_SYS_FLAG); |
| 5350 |
|
| 5351 |
pcur.close(); |
| 5352 |
mtr.commit(); |
| 5353 |
|
| 5354 |
error = |
| 5355 |
row_ins_clust_index_entry_low(flags, BTR_MODIFY_TREE, m_index, |
| 5356 |
m_index->n_uniq, entry, nullptr, false); |
| 5357 |
ut_a(error == DB_SUCCESS); |
| 5358 |
|
| 5359 |
mem_heap_empty(m_dynamic_heap); |
| 5360 |
mem_heap_empty(m_replace_heap); |
| 5361 |
|
| 5362 |
return (DB_SUCCESS); |
| 5363 |
} |
| 5364 |
|
| 5365 |
ut_ad(!rec_get_deleted_flag(pcur.get_rec(), true)); |
| 5366 |
|
| 5367 |
/* Prepare to update the record. */ |
| 5368 |
upd_t *update = update_set_metadata(entry, pcur.get_rec()); |
| 5369 |
|
| 5370 |
if (update != nullptr) { |
| 5371 |
ulint *cur_offsets = nullptr; |
| 5372 |
big_rec_t *big_rec; |
| 5373 |
static const ulint flags = |
| 5374 |
(BTR_CREATE_FLAG | BTR_NO_LOCKING_FLAG | BTR_NO_UNDO_LOG_FLAG | |
| 5375 |
BTR_KEEP_POS_FLAG | BTR_KEEP_SYS_FLAG); |
| 5376 |
|
| 5377 |
error = btr_cur_pessimistic_update( |
| 5378 |
flags, pcur.get_btr_cur(), &cur_offsets, &m_dynamic_heap, |
| 5379 |
m_replace_heap, &big_rec, update, 0, nullptr, 0, 0, &mtr); |
| 5380 |
ut_a(error == DB_SUCCESS); |
| 5381 |
/* We don't have big rec in this table */ |
| 5382 |
ut_ad(!big_rec); |
| 5383 |
} |
| 5384 |
|
| 5385 |
pcur.close(); |
| 5386 |
mtr.commit(); |
| 5387 |
mem_heap_empty(m_dynamic_heap); |
| 5388 |
mem_heap_empty(m_replace_heap); |
| 5389 |
|
| 5390 |
return (DB_SUCCESS); |
| 5391 |
} |
| 5392 |
|
| 5393 |
/** Remove the whole row for a specific table |
| 5394 |
@param[in] id table id |
| 5395 |
@return DB_SUCCESS or error code */ |
| 5396 |
dberr_t DDTableBuffer::remove(table_id_t id) { |
| 5397 |
btr_pcur_t pcur; |
| 5398 |
mtr_t mtr; |
| 5399 |
dberr_t error; |
| 5400 |
|
| 5401 |
ut_ad(mutex_own(&dict_persist->mutex)); |
| 5402 |
|
| 5403 |
init_tuple_with_id(m_search_tuple, id); |
| 5404 |
|
| 5405 |
mtr.start(); |
| 5406 |
|
| 5407 |
pcur.open(m_index, 0, m_search_tuple, PAGE_CUR_LE, |
| 5408 |
BTR_MODIFY_TREE | BTR_LATCH_FOR_DELETE, &mtr, UT_LOCATION_HERE); |
| 5409 |
|
| 5410 |
if (!page_rec_is_infimum(pcur.get_rec()) && |
| 5411 |
pcur.get_low_match() == m_index->n_uniq) { |
| 5412 |
DEBUG_SYNC_C("delete_metadata_before"); |
| 5413 |
|
| 5414 |
btr_cur_pessimistic_delete(&error, false, pcur.get_btr_cur(), |
| 5415 |
BTR_CREATE_FLAG, false, 0, 0, 0, &mtr, nullptr, |
| 5416 |
nullptr); |
| 5417 |
ut_ad(error == DB_SUCCESS); |
| 5418 |
} |
| 5419 |
|
| 5420 |
pcur.close(); |
| 5421 |
mtr.commit(); |
| 5422 |
|
| 5423 |
return (DB_SUCCESS); |
| 5424 |
} |
| 5425 |
|
| 5426 |
/** Truncate the table. We can call it after all the dynamic metadata |
| 5427 |
has been written back to DD table */ |
| 5428 |
void DDTableBuffer::truncate() { |
| 5429 |
ut_ad(mutex_own(&dict_persist->mutex)); |
| 5430 |
|
| 5431 |
btr_truncate(m_index); |
| 5432 |
} |
| 5433 |
|
| 5434 |
/** Get the buffered metadata for a specific table, the caller |
| 5435 |
has to delete the returned std::string object by ut::delete_ |
| 5436 |
@param[in] id table id |
| 5437 |
@param[out] version table dynamic metadata version |
| 5438 |
@return the metadata saved in a vector object, if nothing, the |
| 5439 |
vector would be empty */ |
| 5440 |
std::vector<byte> DDTableBuffer::get(table_id_t id, uint64_t *version) { |
| 5441 |
btr_cur_t cursor; |
| 5442 |
mtr_t mtr; |
| 5443 |
ulint len; |
| 5444 |
const byte *field = nullptr; |
| 5445 |
|
| 5446 |
ut_ad(mutex_own(&dict_persist->mutex)); |
| 5447 |
|
| 5448 |
init_tuple_with_id(m_search_tuple, id); |
| 5449 |
|
| 5450 |
mtr.start(); |
| 5451 |
|
| 5452 |
btr_cur_search_to_nth_level(m_index, 0, m_search_tuple, PAGE_CUR_LE, |
| 5453 |
BTR_SEARCH_LEAF, &cursor, 0, __FILE__, __LINE__, |
| 5454 |
&mtr); |
| 5455 |
|
| 5456 |
if (cursor.low_match == dtuple_get_n_fields(m_search_tuple)) { |
| 5457 |
ulint offsets[N_FIELDS + 1 + REC_OFFS_HEADER_SIZE]; |
| 5458 |
rec_offs_init(offsets); |
| 5459 |
rec_offs_set_n_fields(offsets, N_FIELDS); |
| 5460 |
rec_t *rec = btr_cur_get_rec(&cursor); |
| 5461 |
rec_init_offsets_comp_ordinary(rec, false, m_index, offsets); |
| 5462 |
ut_ad(!rec_get_deleted_flag(rec, true)); |
| 5463 |
|
| 5464 |
const byte *rec_version = |
| 5465 |
rec_get_nth_field(nullptr, rec, offsets, VERSION_FIELD_NO, &len); |
| 5466 |
ut_ad(len == 8); |
| 5467 |
*version = mach_read_from_8(rec_version); |
| 5468 |
|
| 5469 |
field = rec_get_nth_field(nullptr, rec, offsets, METADATA_FIELD_NO, &len); |
| 5470 |
|
| 5471 |
ut_ad(len != UNIV_SQL_NULL); |
| 5472 |
} else { |
| 5473 |
len = 0; |
| 5474 |
*version = 0; |
| 5475 |
} |
| 5476 |
|
| 5477 |
std::vector<byte> metadata{field, field + len}; |
| 5478 |
|
| 5479 |
mtr.commit(); |
| 5480 |
|
| 5481 |
return (metadata); |
| 5482 |
} |
| 5483 |
|
| 5484 |
/** Write MLOG_TABLE_DYNAMIC_META for persistent dynamic metadata of table |
| 5485 |
@param[in] id Table id |
| 5486 |
@param[in] metadata Metadata used to write the log |
| 5487 |
@param[in,out] mtr Mini-transaction */ |
| 5488 |
void Persister::write_log(table_id_t id, |
| 5489 |
const PersistentTableMetadata &metadata, |
| 5490 |
mtr_t *mtr) const { |
| 5491 |
byte *log_ptr; |
| 5492 |
ulint size = get_write_size(metadata); |
| 5493 |
/* Both table id and version would be written in a compressed format, |
| 5494 |
each of which would cost 1..11 bytes, and MLOG_TABLE_DYNAMIC_META costs |
| 5495 |
1 byte. Refer to mlog_write_initial_dict_log_record() as well */ |
| 5496 |
static constexpr uint8_t metadata_log_header_size = 23; |
| 5497 |
|
| 5498 |
ut_ad(size > 0); |
| 5499 |
|
| 5500 |
if (!mlog_open_metadata(mtr, metadata_log_header_size + size, log_ptr)) { |
| 5501 |
/* Currently possible only when global redo logging is not enabled. */ |
| 5502 |
ut_ad(!mtr_t::s_logging.is_enabled()); |
| 5503 |
return; |
| 5504 |
} |
| 5505 |
|
| 5506 |
log_ptr = mlog_write_initial_dict_log_record( |
| 5507 |
MLOG_TABLE_DYNAMIC_META, id, metadata.get_version(), log_ptr, mtr); |
| 5508 |
|
| 5509 |
ulint consumed = write(metadata, log_ptr, size); |
| 5510 |
log_ptr += consumed; |
| 5511 |
|
| 5512 |
mlog_close(mtr, log_ptr); |
| 5513 |
} |
| 5514 |
#endif /* !UNIV_HOTBACKUP */ |
| 5515 |
|
| 5516 |
/** Write the corrupted indexes of a table, we can pre-calculate the size |
| 5517 |
by calling get_write_size() |
| 5518 |
@param[in] metadata persistent data |
| 5519 |
@param[out] buffer write buffer |
| 5520 |
@param[in] size size of write buffer, should be at least |
| 5521 |
get_write_size() |
| 5522 |
@return the length of bytes written */ |
| 5523 |
ulint CorruptedIndexPersister::write(const PersistentTableMetadata &metadata, |
| 5524 |
byte *buffer, ulint size) const { |
| 5525 |
ulint length = 0; |
| 5526 |
corrupted_ids_t corrupted_ids = metadata.get_corrupted_indexes(); |
| 5527 |
ulint num = corrupted_ids.size(); |
| 5528 |
|
| 5529 |
ut_ad(num < MAX_INDEXES); |
| 5530 |
|
| 5531 |
if (corrupted_ids.empty()) { |
| 5532 |
return (0); |
| 5533 |
} |
| 5534 |
|
| 5535 |
/* Write the PM_INDEX_CORRUPTED mark first */ |
| 5536 |
mach_write_to_1(buffer, static_cast<byte>(PM_INDEX_CORRUPTED)); |
| 5537 |
++length; |
| 5538 |
++buffer; |
| 5539 |
|
| 5540 |
mach_write_to_1(buffer, num); |
| 5541 |
DBUG_EXECUTE_IF("log_corruption_1", mach_write_to_1(buffer, 100);); |
| 5542 |
++length; |
| 5543 |
++buffer; |
| 5544 |
|
| 5545 |
for (ulint i = 0; i < num; ++i) { |
| 5546 |
mach_write_to_4(buffer, corrupted_ids[i].m_space_id); |
| 5547 |
mach_write_to_8(buffer + 4, corrupted_ids[i].m_index_id); |
| 5548 |
length += INDEX_ID_LENGTH; |
| 5549 |
buffer += INDEX_ID_LENGTH; |
| 5550 |
ut_ad(length <= size); |
| 5551 |
} |
| 5552 |
|
| 5553 |
return (length); |
| 5554 |
} |
| 5555 |
|
| 5556 |
/** Pre-calculate the size of metadata to be written |
| 5557 |
@param[in] metadata metadata to be written |
| 5558 |
@return the size of metadata */ |
| 5559 |
ulint CorruptedIndexPersister::get_write_size( |
| 5560 |
const PersistentTableMetadata &metadata) const { |
| 5561 |
ulint length = 0; |
| 5562 |
corrupted_ids_t corrupted_ids = metadata.get_corrupted_indexes(); |
| 5563 |
|
| 5564 |
ut_ad(corrupted_ids.size() < MAX_INDEXES); |
| 5565 |
|
| 5566 |
if (corrupted_ids.empty()) { |
| 5567 |
return (0); |
| 5568 |
} |
| 5569 |
|
| 5570 |
/* PM_INDEX_CORRUPTED mark and number of corrupted indexes' ids */ |
| 5571 |
length += 1 + 1; |
| 5572 |
length += corrupted_ids.size() * INDEX_ID_LENGTH; |
| 5573 |
|
| 5574 |
return (length); |
| 5575 |
} |
| 5576 |
|
| 5577 |
/** Read the corrupted indexes from buffer, and store them to |
| 5578 |
metadata object |
| 5579 |
@param[out] metadata metadata where we store the read data |
| 5580 |
@param[in] buffer buffer to read |
| 5581 |
@param[in] size size of buffer |
| 5582 |
@param[out] corrupt true if we found something wrong in |
| 5583 |
the buffer except incomplete buffer, |
| 5584 |
otherwise false |
| 5585 |
@return the bytes we read from the buffer if the buffer data |
| 5586 |
is complete and we get everything, 0 if the buffer is incompleted */ |
| 5587 |
ulint CorruptedIndexPersister::read(PersistentTableMetadata &metadata, |
| 5588 |
const byte *buffer, ulint size, |
| 5589 |
bool *corrupt) const { |
| 5590 |
const byte *end = buffer + size; |
| 5591 |
ulint consumed = 0; |
| 5592 |
ulint num; |
| 5593 |
|
| 5594 |
*corrupt = false; |
| 5595 |
|
| 5596 |
/* It should contain a number at least */ |
| 5597 |
if (size <= 1) { |
| 5598 |
return (0); |
| 5599 |
} |
| 5600 |
|
| 5601 |
num = mach_read_from_1(buffer); |
| 5602 |
++consumed; |
| 5603 |
++buffer; |
| 5604 |
|
| 5605 |
if (num == 0 || num > MAX_INDEXES) { |
| 5606 |
*corrupt = true; |
| 5607 |
return (consumed); |
| 5608 |
} |
| 5609 |
|
| 5610 |
if (buffer + num * INDEX_ID_LENGTH > end) { |
| 5611 |
return (0); |
| 5612 |
} |
| 5613 |
|
| 5614 |
for (ulint i = 0; i < num; ++i) { |
| 5615 |
space_id_t space_id = mach_read_from_4(buffer); |
| 5616 |
space_index_t index_id = mach_read_from_8(buffer + 4); |
| 5617 |
metadata.add_corrupted_index(index_id_t(space_id, index_id)); |
| 5618 |
|
| 5619 |
buffer += INDEX_ID_LENGTH; |
| 5620 |
consumed += INDEX_ID_LENGTH; |
| 5621 |
} |
| 5622 |
|
| 5623 |
return (consumed); |
| 5624 |
} |
| 5625 |
|
| 5626 |
void CorruptedIndexPersister::aggregate( |
| 5627 |
PersistentTableMetadata &metadata, |
| 5628 |
const PersistentTableMetadata &new_entry) const { |
| 5629 |
for (auto id : new_entry.get_corrupted_indexes()) { |
| 5630 |
metadata.add_corrupted_index(id); |
| 5631 |
} |
| 5632 |
} |
| 5633 |
|
| 5634 |
/** Write the autoinc counter of a table, we can pre-calculate |
| 5635 |
the size by calling get_write_size() |
| 5636 |
@param[in] metadata persistent metadata |
| 5637 |
@param[out] buffer write buffer |
| 5638 |
@param[in] size size of write buffer, should be |
| 5639 |
at least get_write_size() |
| 5640 |
@return the length of bytes written */ |
| 5641 |
ulint AutoIncPersister::write(const PersistentTableMetadata &metadata, |
| 5642 |
byte *buffer, ulint size) const { |
| 5643 |
ulint length = 0; |
| 5644 |
uint64_t autoinc = metadata.get_autoinc(); |
| 5645 |
|
| 5646 |
mach_write_to_1(buffer, static_cast<byte>(PM_TABLE_AUTO_INC)); |
| 5647 |
++length; |
| 5648 |
++buffer; |
| 5649 |
|
| 5650 |
ulint len = mach_u64_write_much_compressed(buffer, autoinc); |
| 5651 |
length += len; |
| 5652 |
buffer += len; |
| 5653 |
|
| 5654 |
ut_ad(length <= size); |
| 5655 |
return (length); |
| 5656 |
} |
| 5657 |
|
| 5658 |
/** Read the autoinc counter from buffer, and store them to |
| 5659 |
a metadata object |
| 5660 |
@param[out] metadata metadata where we store the read data |
| 5661 |
@param[in] buffer buffer to read |
| 5662 |
@param[in] size size of buffer |
| 5663 |
@param[out] corrupt true if we found something wrong in |
| 5664 |
the buffer except incomplete buffer, |
| 5665 |
otherwise false |
| 5666 |
@return the bytes we read from the buffer if the buffer data |
| 5667 |
is complete and we get everything, 0 if the buffer is incomplete */ |
| 5668 |
ulint AutoIncPersister::read(PersistentTableMetadata &metadata, |
| 5669 |
const byte *buffer, ulint size, |
| 5670 |
bool *corrupt) const { |
| 5671 |
*corrupt = false; |
| 5672 |
|
| 5673 |
const byte *start = buffer; |
| 5674 |
const auto autoinc = mach_parse_u64_much_compressed(&start, buffer + size); |
| 5675 |
|
| 5676 |
if (start == nullptr) { |
| 5677 |
/* Just incomplete data, not corrupted */ |
| 5678 |
return (0); |
| 5679 |
} |
| 5680 |
|
| 5681 |
metadata.set_autoinc(autoinc); |
| 5682 |
|
| 5683 |
const ulint consumed = start - buffer; |
| 5684 |
ut_ad(consumed <= size); |
| 5685 |
return (consumed); |
| 5686 |
} |
| 5687 |
|
| 5688 |
void AutoIncPersister::aggregate( |
| 5689 |
PersistentTableMetadata &metadata, |
| 5690 |
const PersistentTableMetadata &new_entry) const { |
| 5691 |
if (new_entry.get_version() > metadata.get_version()) { |
| 5692 |
metadata.set_autoinc(new_entry.get_autoinc()); |
| 5693 |
metadata.set_version(new_entry.get_version()); |
| 5694 |
} else if (new_entry.get_version() == metadata.get_version()) { |
| 5695 |
metadata.set_autoinc_if_bigger(new_entry.get_autoinc()); |
| 5696 |
} |
| 5697 |
} |
| 5698 |
|
| 5699 |
/** Destructor */ |
| 5700 |
Persisters::~Persisters() { |
| 5701 |
persisters_t::iterator iter; |
| 5702 |
for (iter = m_persisters.begin(); iter != m_persisters.end(); ++iter) { |
| 5703 |
ut::delete_(iter->second); |
| 5704 |
} |
| 5705 |
} |
| 5706 |
|
| 5707 |
/** Get the persister object with specified type |
| 5708 |
@param[in] type persister type |
| 5709 |
@return Persister object required or NULL if not found */ |
| 5710 |
Persister *Persisters::get(persistent_type_t type) const { |
| 5711 |
ut_ad(type > PM_SMALLEST_TYPE); |
| 5712 |
ut_ad(type < PM_BIGGEST_TYPE); |
| 5713 |
|
| 5714 |
persisters_t::const_iterator iter = m_persisters.find(type); |
| 5715 |
|
| 5716 |
return (iter == m_persisters.end() ? NULL : iter->second); |
| 5717 |
} |
| 5718 |
|
| 5719 |
/** Add a specified persister of type, we will allocate the Persister |
| 5720 |
if there is no such persister exist, otherwise do nothing and return |
| 5721 |
the existing one |
| 5722 |
@param[in] type persister type |
| 5723 |
@return the persister of type */ |
| 5724 |
Persister *Persisters::add(persistent_type_t type) { |
| 5725 |
ut_ad(type > PM_SMALLEST_TYPE); |
| 5726 |
ut_ad(type < PM_BIGGEST_TYPE); |
| 5727 |
|
| 5728 |
Persister *persister = get(type); |
| 5729 |
|
| 5730 |
if (persister != nullptr) { |
| 5731 |
return (persister); |
| 5732 |
} |
| 5733 |
|
| 5734 |
switch (type) { |
| 5735 |
case PM_INDEX_CORRUPTED: |
| 5736 |
persister = |
| 5737 |
ut::new_withkey<CorruptedIndexPersister>(UT_NEW_THIS_FILE_PSI_KEY); |
| 5738 |
break; |
| 5739 |
case PM_TABLE_AUTO_INC: |
| 5740 |
persister = ut::new_withkey<AutoIncPersister>(UT_NEW_THIS_FILE_PSI_KEY); |
| 5741 |
break; |
| 5742 |
default: |
| 5743 |
ut_d(ut_error); |
| 5744 |
ut_o(break); |
| 5745 |
} |
| 5746 |
|
| 5747 |
m_persisters.insert(std::make_pair(type, persister)); |
| 5748 |
|
| 5749 |
return (persister); |
| 5750 |
} |
| 5751 |
|
| 5752 |
/** Remove a specified persister of type, we will free the Persister |
| 5753 |
@param[in] type persister type */ |
| 5754 |
void Persisters::remove(persistent_type_t type) { |
| 5755 |
persisters_t::iterator iter = m_persisters.find(type); |
| 5756 |
|
| 5757 |
if (iter != m_persisters.end()) { |
| 5758 |
ut::delete_(iter->second); |
| 5759 |
m_persisters.erase(iter); |
| 5760 |
} |
| 5761 |
} |
| 5762 |
|
| 5763 |
#ifndef UNIV_HOTBACKUP |
| 5764 |
/** Serialize the metadata to a buffer |
| 5765 |
@param[in] metadata metadata to serialize |
| 5766 |
@param[out] buffer buffer to store the serialized metadata |
| 5767 |
@return the length of serialized metadata */ |
| 5768 |
size_t Persisters::write(PersistentTableMetadata &metadata, byte *buffer) { |
| 5769 |
size_t size = 0; |
| 5770 |
byte *pos = buffer; |
| 5771 |
persistent_type_t type; |
| 5772 |
|
| 5773 |
for (type = static_cast<persistent_type_t>(PM_SMALLEST_TYPE + 1); |
| 5774 |
type < PM_BIGGEST_TYPE; |
| 5775 |
type = static_cast<persistent_type_t>(type + 1)) { |
| 5776 |
ut_ad(size <= REC_MAX_DATA_SIZE); |
| 5777 |
|
| 5778 |
Persister *persister = get(type); |
| 5779 |
ulint consumed = persister->write(metadata, pos, REC_MAX_DATA_SIZE - size); |
| 5780 |
|
| 5781 |
pos += consumed; |
| 5782 |
size += consumed; |
| 5783 |
} |
| 5784 |
|
| 5785 |
return (size); |
| 5786 |
} |
| 5787 |
|
| 5788 |
void dict_sdi_close_table(dict_table_t *table) { |
| 5789 |
ut_ad(dict_table_is_sdi(table->id)); |
| 5790 |
dict_table_close(table, true, false); |
| 5791 |
} |
| 5792 |
|
| 5793 |
/** Retrieve in-memory index for SDI table. |
| 5794 |
@param[in] tablespace_id innodb tablespace ID |
| 5795 |
@return dict_index_t structure or NULL*/ |
| 5796 |
dict_index_t *dict_sdi_get_index(space_id_t tablespace_id) { |
| 5797 |
dict_table_t *table = dd_table_open_on_id( |
| 5798 |
dict_sdi_get_table_id(tablespace_id), nullptr, nullptr, true, true); |
| 5799 |
|
| 5800 |
if (table != nullptr) { |
| 5801 |
dict_sdi_close_table(table); |
| 5802 |
return (table->first_index()); |
| 5803 |
} |
| 5804 |
return (nullptr); |
| 5805 |
} |
| 5806 |
|
| 5807 |
/** Retrieve in-memory table object for SDI table. |
| 5808 |
@param[in] tablespace_id innodb tablespace ID |
| 5809 |
@param[in] dict_locked true if dict_sys mutex is acquired |
| 5810 |
@param[in] is_create true when creating SDI Index |
| 5811 |
@return dict_table_t structure */ |
| 5812 |
dict_table_t *dict_sdi_get_table(space_id_t tablespace_id, bool dict_locked, |
| 5813 |
bool is_create) { |
| 5814 |
if (is_create) { |
| 5815 |
if (!dict_locked) { |
| 5816 |
dict_sys_mutex_enter(); |
| 5817 |
} |
| 5818 |
|
| 5819 |
dict_sdi_create_idx_in_mem(tablespace_id, false, 0, true); |
| 5820 |
|
| 5821 |
if (!dict_locked) { |
| 5822 |
dict_sys_mutex_exit(); |
| 5823 |
} |
| 5824 |
} |
| 5825 |
dict_table_t *table = |
| 5826 |
dd_table_open_on_id(dict_sdi_get_table_id(tablespace_id), nullptr, |
| 5827 |
nullptr, dict_locked, true); |
| 5828 |
|
| 5829 |
return (table); |
| 5830 |
} |
| 5831 |
|
| 5832 |
/** Remove the SDI table from table cache. |
| 5833 |
@param[in] space_id InnoDB tablespace ID |
| 5834 |
@param[in] sdi_table SDI table |
| 5835 |
@param[in] dict_locked true if dict_sys mutex acquired */ |
| 5836 |
void dict_sdi_remove_from_cache(space_id_t space_id, dict_table_t *sdi_table, |
| 5837 |
bool dict_locked) { |
| 5838 |
if (sdi_table == nullptr) { |
| 5839 |
/* Remove SDI table from table cache */ |
| 5840 |
/* We already have MDL protection on tablespace as well |
| 5841 |
as MDL on SDI table */ |
| 5842 |
sdi_table = dd_table_open_on_id_in_mem(dict_sdi_get_table_id(space_id), |
| 5843 |
dict_locked); |
| 5844 |
if (sdi_table) { |
| 5845 |
dd_table_close(sdi_table, nullptr, nullptr, dict_locked); |
| 5846 |
} |
| 5847 |
} else { |
| 5848 |
dd_table_close(sdi_table, nullptr, nullptr, dict_locked); |
| 5849 |
} |
| 5850 |
|
| 5851 |
if (sdi_table) { |
| 5852 |
if (!dict_locked) { |
| 5853 |
dict_sys_mutex_enter(); |
| 5854 |
} |
| 5855 |
|
| 5856 |
dict_table_remove_from_cache(sdi_table); |
| 5857 |
|
| 5858 |
if (!dict_locked) { |
| 5859 |
dict_sys_mutex_exit(); |
| 5860 |
} |
| 5861 |
} |
| 5862 |
} |
| 5863 |
|
| 5864 |
/** Change the table_id of SYS_* tables if they have been created after |
| 5865 |
an earlier upgrade. This will update the table_id by adding DICT_MAX_DD_TABLES |
| 5866 |
*/ |
| 5867 |
void dict_table_change_id_sys_tables() { |
| 5868 |
ut_ad(dict_sys_mutex_own()); |
| 5869 |
|
| 5870 |
/* On upgrading from 5.6 to 5.7, new system table SYS_VIRTUAL is given table |
| 5871 |
id after the last created user table. So, if last user table was created |
| 5872 |
with table_id as 1027, SYS_VIRTUAL would get id 1028. On upgrade to 8.0, |
| 5873 |
all these tables are shifted by 1024. On 5.7, the SYS_FIELDS has table id |
| 5874 |
4, which gets updated to 1028 on upgrade. This would later assert when we |
| 5875 |
try to open the SYS_VIRTUAL table (having id 1028) for upgrade. Hence, we |
| 5876 |
need to upgrade system tables in reverse order to avoid that. |
| 5877 |
These tables are created on boot. And hence this issue can only be caused by |
| 5878 |
a new table being added at a later stage - the SYS_VIRTUAL table being added |
| 5879 |
on upgrading to 5.7. */ |
| 5880 |
|
| 5881 |
for (int i = SYS_NUM_SYSTEM_TABLES - 1; i >= 0; i--) { |
| 5882 |
dict_table_t *system_table = dict_table_get_low(SYSTEM_TABLE_NAME[i]); |
| 5883 |
|
| 5884 |
ut_a(system_table != nullptr); |
| 5885 |
ut_ad(dict_sys_table_id[i] == system_table->id); |
| 5886 |
|
| 5887 |
/* During upgrade, table_id of user tables is also |
| 5888 |
moved by DICT_MAX_DD_TABLES. See dict_load_table_one()*/ |
| 5889 |
table_id_t new_table_id = system_table->id + DICT_MAX_DD_TABLES; |
| 5890 |
|
| 5891 |
dict_table_change_id_in_cache(system_table, new_table_id); |
| 5892 |
|
| 5893 |
dict_sys_table_id[i] = system_table->id; |
| 5894 |
|
| 5895 |
dict_table_prevent_eviction(system_table); |
| 5896 |
} |
| 5897 |
} |
| 5898 |
|
| 5899 |
/** Evict all tables that are loaded for applying purge. |
| 5900 |
Since we move the offset of all table ids during upgrade, |
| 5901 |
these tables cannot exist in cache. Also change table_ids |
| 5902 |
of SYS_* tables if they are upgraded from earlier versions */ |
| 5903 |
void dict_upgrade_evict_tables_cache() { |
| 5904 |
dict_table_t *table; |
| 5905 |
|
| 5906 |
rw_lock_x_lock(dict_operation_lock, UT_LOCATION_HERE); |
| 5907 |
dict_sys_mutex_enter(); |
| 5908 |
|
| 5909 |
ut_ad(dict_lru_validate()); |
| 5910 |
ut_ad(srv_is_upgrade_mode); |
| 5911 |
|
| 5912 |
/* Move all tables from non-LRU to LRU */ |
| 5913 |
for (table = UT_LIST_GET_LAST(dict_sys->table_non_LRU); table != nullptr;) { |
| 5914 |
dict_table_t *prev_table; |
| 5915 |
|
| 5916 |
prev_table = UT_LIST_GET_PREV(table_LRU, table); |
| 5917 |
|
| 5918 |
if (!dict_table_is_system(table->id)) { |
| 5919 |
DBUG_EXECUTE_IF("dd_upgrade", ib::info(ER_IB_MSG_185) |
| 5920 |
<< "Moving table " << table->name |
| 5921 |
<< " from non-LRU to LRU";); |
| 5922 |
|
| 5923 |
dict_table_move_from_non_lru_to_lru(table); |
| 5924 |
} |
| 5925 |
|
| 5926 |
table = prev_table; |
| 5927 |
} |
| 5928 |
|
| 5929 |
for (table = UT_LIST_GET_LAST(dict_sys->table_LRU); table != nullptr;) { |
| 5930 |
dict_table_t *prev_table; |
| 5931 |
|
| 5932 |
prev_table = UT_LIST_GET_PREV(table_LRU, table); |
| 5933 |
|
| 5934 |
ut_ad(dict_table_can_be_evicted(table)); |
| 5935 |
|
| 5936 |
DBUG_EXECUTE_IF("dd_upgrade", ib::info(ER_IB_MSG_186) |
| 5937 |
<< "Evicting table: LRU: " |
| 5938 |
<< table->name;); |
| 5939 |
|
| 5940 |
dict_table_remove_from_cache_low(table, true); |
| 5941 |
|
| 5942 |
table = prev_table; |
| 5943 |
} |
| 5944 |
|
| 5945 |
dict_table_change_id_sys_tables(); |
| 5946 |
|
| 5947 |
dict_sys_mutex_exit(); |
| 5948 |
rw_lock_x_unlock(dict_operation_lock); |
| 5949 |
} |
| 5950 |
|
| 5951 |
/** Build the table_id array of SYS_* tables. This |
| 5952 |
array is used to determine if a table is InnoDB SYSTEM |
| 5953 |
table or not. |
| 5954 |
@return true if successful, false otherwise */ |
| 5955 |
bool dict_sys_table_id_build() { |
| 5956 |
dict_sys_mutex_enter(); |
| 5957 |
for (uint32_t i = 0; i < SYS_NUM_SYSTEM_TABLES; i++) { |
| 5958 |
dict_table_t *system_table = dict_table_get_low(SYSTEM_TABLE_NAME[i]); |
| 5959 |
|
| 5960 |
if (system_table == nullptr) { |
| 5961 |
/* Cannot find a system table, this happens only if user trying |
| 5962 |
to boot server earlier than 5.7 */ |
| 5963 |
dict_sys_mutex_exit(); |
| 5964 |
LogErr(ERROR_LEVEL, ER_IB_MSG_1271); |
| 5965 |
return (false); |
| 5966 |
} |
| 5967 |
dict_sys_table_id[i] = system_table->id; |
| 5968 |
} |
| 5969 |
dict_sys_mutex_exit(); |
| 5970 |
return (true); |
| 5971 |
} |
| 5972 |
|
| 5973 |
/** @return true if table is InnoDB SYS_* table |
| 5974 |
@param[in] table_id table id */ |
| 5975 |
bool dict_table_is_system(table_id_t table_id) { |
| 5976 |
for (uint32_t i = 0; i < SYS_NUM_SYSTEM_TABLES; i++) { |
| 5977 |
if (table_id == dict_sys_table_id[i]) { |
| 5978 |
return (true); |
| 5979 |
} |
| 5980 |
} |
| 5981 |
return (false); |
| 5982 |
} |
| 5983 |
|
| 5984 |
dberr_t dd_sdi_acquire_exclusive_mdl(THD *thd, space_id_t space_id, |
| 5985 |
MDL_ticket **sdi_mdl) { |
| 5986 |
/* Exclusive MDL always need trx context and is |
| 5987 |
released on trx commit. So check if thd & trx |
| 5988 |
exists */ |
| 5989 |
ut_ad(thd != nullptr); |
| 5990 |
ut_ad(check_trx_exists(current_thd) != nullptr); |
| 5991 |
ut_ad(sdi_mdl != nullptr); |
| 5992 |
ut_ad(!dict_sys_mutex_own()); |
| 5993 |
|
| 5994 |
char tbl_buf[NAME_LEN + 1]; |
| 5995 |
const char *db_buf = "dummy_sdi_db"; |
| 5996 |
|
| 5997 |
snprintf(tbl_buf, sizeof(tbl_buf), "SDI_" SPACE_ID_PF, space_id); |
| 5998 |
|
| 5999 |
/* Submit a higher than default lock wait timeout, the timeout accepted by |
| 6000 |
dd::acquire_exclusive_table_mdl is in seconds. */ |
| 6001 |
std::chrono::seconds lock_wait_timeout = thd_lock_wait_timeout(thd); |
| 6002 |
if (lock_wait_timeout < std::chrono::hours(27)) { |
| 6003 |
lock_wait_timeout += std::chrono::hours(27); |
| 6004 |
} |
| 6005 |
if (dd::acquire_exclusive_table_mdl( |
| 6006 |
thd, db_buf, tbl_buf, (unsigned long int)lock_wait_timeout.count(), |
| 6007 |
sdi_mdl)) { |
| 6008 |
/* MDL failure can happen with lower timeout |
| 6009 |
values chosen by user */ |
| 6010 |
return (DB_LOCK_WAIT_TIMEOUT); |
| 6011 |
} |
| 6012 |
|
| 6013 |
/* MDL creation failed */ |
| 6014 |
if (*sdi_mdl == nullptr) { |
| 6015 |
ut_d(ut_error); |
| 6016 |
ut_o(return (DB_LOCK_WAIT_TIMEOUT)); |
| 6017 |
} |
| 6018 |
|
| 6019 |
return (DB_SUCCESS); |
| 6020 |
} |
| 6021 |
|
| 6022 |
dberr_t dd_sdi_acquire_shared_mdl(THD *thd, space_id_t space_id, |
| 6023 |
MDL_ticket **sdi_mdl) { |
| 6024 |
ut_ad(sdi_mdl != nullptr); |
| 6025 |
ut_ad(!dict_sys_mutex_own()); |
| 6026 |
|
| 6027 |
char tbl_buf[NAME_LEN + 1]; |
| 6028 |
const char *db_buf = "dummy_sdi_db"; |
| 6029 |
|
| 6030 |
snprintf(tbl_buf, sizeof(tbl_buf), "SDI_" SPACE_ID_PF, space_id); |
| 6031 |
|
| 6032 |
if (dd::acquire_shared_table_mdl(thd, db_buf, tbl_buf, false, sdi_mdl)) { |
| 6033 |
/* MDL failure can happen with lower timeout |
| 6034 |
values chosen by user */ |
| 6035 |
return (DB_LOCK_WAIT_TIMEOUT); |
| 6036 |
} |
| 6037 |
|
| 6038 |
/* MDL creation failed */ |
| 6039 |
if (*sdi_mdl == nullptr) { |
| 6040 |
ut_d(ut_error); |
| 6041 |
ut_o(return (DB_LOCK_WAIT_TIMEOUT)); |
| 6042 |
} |
| 6043 |
|
| 6044 |
return (DB_SUCCESS); |
| 6045 |
} |
| 6046 |
|
| 6047 |
/** Get the tablespace data directory if set, otherwise empty string. |
| 6048 |
@return the data directory */ |
| 6049 |
std::string dict_table_get_datadir(const dict_table_t *table) { |
| 6050 |
std::string path; |
| 6051 |
|
| 6052 |
if (DICT_TF_HAS_DATA_DIR(table->flags) && table->data_dir_path != nullptr) { |
| 6053 |
path.assign(table->data_dir_path); |
| 6054 |
} |
| 6055 |
|
| 6056 |
return (path); |
| 6057 |
} |
| 6058 |
|
| 6059 |
dberr_t dict_set_compression(dict_table_t *table, const char *algorithm, |
| 6060 |
bool is_import_op) { |
| 6061 |
ut_ad(table != nullptr); |
| 6062 |
|
| 6063 |
/* We don't support Page Compression for the system tablespace, the temporary |
| 6064 |
tablespace, or any general tablespace because COMPRESSION is set by TABLE |
| 6065 |
DDL, not TABLESPACE DDL (there is no other technical reason). And neither do |
| 6066 |
we support it for tables with compressed row_format. |
| 6067 |
|
| 6068 |
Also, do not use it for missing tables, unless it's an import operation as in |
| 6069 |
the case of import we would still be importing the tablespace at this stage |
| 6070 |
and would need to set the compression option for further processing. */ |
| 6071 |
if ((table->ibd_file_missing && !is_import_op) || |
| 6072 |
!DICT_TF2_FLAG_IS_SET(table, DICT_TF2_USE_FILE_PER_TABLE) || |
| 6073 |
DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY) || |
| 6074 |
page_size_t(table->flags).is_compressed()) { |
| 6075 |
return (DB_IO_NO_PUNCH_HOLE_TABLESPACE); |
| 6076 |
} |
| 6077 |
|
| 6078 |
auto err = fil_set_compression(table->space, algorithm); |
| 6079 |
|
| 6080 |
return err; |
| 6081 |
} |
| 6082 |
|
| 6083 |
dict_persist_t::Enable_immediate::Enable_immediate(dict_persist_t *persister) |
| 6084 |
: m_persister(persister) { |
| 6085 |
/* 1. Make sure all new auto increment operations save metadata to |
| 6086 |
DD buffer table immediately. */ |
| 6087 |
m_persister->m_persist_immediately.store(true); |
| 6088 |
|
| 6089 |
/* 2. Save all existing metadata to DD buffer table. */ |
| 6090 |
dict_persist_to_dd_table_buffer(); |
| 6091 |
} |
| 6092 |
|
| 6093 |
dict_persist_t::Enable_immediate::~Enable_immediate() { |
| 6094 |
m_persister->m_persist_immediately.store(false); |
| 6095 |
m_persister = nullptr; |
| 6096 |
} |
| 6097 |
|
| 6098 |
uint32_t dict_vcol_base_is_foreign_key(dict_v_col_t *vcol, |
| 6099 |
dict_foreign_t *foreign) { |
| 6100 |
const dict_table_t *table = foreign->foreign_table; |
| 6101 |
uint32_t foreign_col_count = 0; |
| 6102 |
|
| 6103 |
for (uint32_t i = 0; i < foreign->n_fields; i++) { |
| 6104 |
const char *foreign_col_name = foreign->foreign_col_names[i]; |
| 6105 |
for (uint32_t j = 0; j < vcol->num_base; j++) { |
| 6106 |
if (strcmp(foreign_col_name, |
| 6107 |
table->get_col_name(vcol->base_col[j]->ind)) == 0) { |
| 6108 |
foreign_col_count++; |
| 6109 |
} |
| 6110 |
} |
| 6111 |
} |
| 6112 |
return foreign_col_count; |
| 6113 |
} |
| 6114 |
|
| 6115 |
#ifdef UNIV_DEBUG |
| 6116 |
/** Validate no active background threads to cause purge or rollback |
| 6117 |
operations. */ |
| 6118 |
void dict_validate_no_purge_rollback_threads() { |
| 6119 |
/* No concurrent background threads to access to the table */ |
| 6120 |
ut_ad(trx_purge_state() == PURGE_STATE_STOP || |
| 6121 |
trx_purge_state() == PURGE_STATE_DISABLED); |
| 6122 |
ut_ad(!srv_thread_is_active(srv_threads.m_trx_recovery_rollback)); |
| 6123 |
} |
| 6124 |
#endif /* UNIV_DEBUG */ |
| 6125 |
#endif /* !UNIV_HOTBACKUP */ |