| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 2005, 2026, Oracle and/or its affiliates. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify it under |
| 6 |
the terms of the GNU General Public License, version 2.0, as published by the |
| 7 |
Free Software Foundation. |
| 8 |
|
| 9 |
This program is designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/ddl0ddl.h |
| 29 |
DDL context */ |
| 30 |
|
| 31 |
#ifndef ddl0ddl_h |
| 32 |
#define ddl0ddl_h |
| 33 |
|
| 34 |
#include "fts0fts.h" |
| 35 |
#include "lock0types.h" |
| 36 |
#include "os0file.h" |
| 37 |
#include "ut0class_life_cycle.h" |
| 38 |
|
| 39 |
// Forward declaration |
| 40 |
class Flush_observer; |
| 41 |
class Alter_stage; |
| 42 |
|
| 43 |
namespace ddl { |
| 44 |
// Forward declaration |
| 45 |
struct Dup; |
| 46 |
struct Row; |
| 47 |
struct FTS; |
| 48 |
class Loader; |
| 49 |
struct Cursor; |
| 50 |
struct Context; |
| 51 |
struct Builder; |
| 52 |
struct Sequence; |
| 53 |
struct Merge_file_sort; |
| 54 |
struct Load_cursor; |
| 55 |
struct Btree_cursor; |
| 56 |
struct Parallel_cursor; |
| 57 |
|
| 58 |
/** Innodb B-tree index fill factor for bulk load. */ |
| 59 |
extern long fill_factor; |
| 60 |
|
| 61 |
/** Variable specifying the number of FTS parser threads to use. */ |
| 62 |
extern ulong fts_parser_threads; |
| 63 |
|
| 64 |
/** Minimum IO buffer size. */ |
| 65 |
constexpr size_t IO_BLOCK_SIZE = 4 * 1024; |
| 66 |
|
| 67 |
/** @brief Secondary buffer for I/O operations of merge records. |
| 68 |
|
| 69 |
This buffer is used for writing or reading a record that spans two |
| 70 |
Aligned_buffer. Thus, it must be able to hold one merge record, |
| 71 |
whose maximum size is the same as the minimum size of Aligned_buffer. */ |
| 72 |
using mrec_buf_t = byte[UNIV_PAGE_SIZE_MAX]; |
| 73 |
|
| 74 |
/** @brief Merge record in Aligned_buffer. |
| 75 |
|
| 76 |
The format is the same as a record in ROW_FORMAT=COMPACT with the |
| 77 |
exception that the REC_N_NEW_EXTRA_BYTES are omitted. */ |
| 78 |
using mrec_t = byte; |
| 79 |
|
| 80 |
/** Index field definition */ |
| 81 |
struct Index_field { |
| 82 |
/** Column offset */ |
| 83 |
size_t m_col_no{}; |
| 84 |
|
| 85 |
/** Column prefix length, or 0 if indexing the whole column */ |
| 86 |
size_t m_prefix_len{}; |
| 87 |
|
| 88 |
/** Whether this is a virtual column */ |
| 89 |
bool m_is_v_col{}; |
| 90 |
|
| 91 |
/** Whether it has multi-value */ |
| 92 |
bool m_is_multi_value{}; |
| 93 |
|
| 94 |
/** true=ASC, false=DESC */ |
| 95 |
bool m_is_ascending{}; |
| 96 |
}; |
| 97 |
|
| 98 |
/** Definition of an index being created */ |
| 99 |
struct Index_defn { |
| 100 |
/** Index name */ |
| 101 |
const char *m_name{}; |
| 102 |
|
| 103 |
/** Whether the table is rebuilt */ |
| 104 |
bool m_rebuild{}; |
| 105 |
|
| 106 |
/** 0, DICT_UNIQUE, or DICT_CLUSTERED */ |
| 107 |
size_t m_ind_type{}; |
| 108 |
|
| 109 |
/** MySQL key number, or ULINT_UNDEFINED if none */ |
| 110 |
size_t m_key_number{ULINT_UNDEFINED}; |
| 111 |
|
| 112 |
/** Number of fields in index */ |
| 113 |
size_t m_n_fields{}; |
| 114 |
|
| 115 |
/** Field definitions */ |
| 116 |
Index_field *m_fields{}; |
| 117 |
|
| 118 |
/** Fulltext parser plugin */ |
| 119 |
st_mysql_ftparser *m_parser{}; |
| 120 |
|
| 121 |
/** true if it's ngram parser */ |
| 122 |
bool m_is_ngram{}; |
| 123 |
|
| 124 |
/** true if we want to check SRID while inserting to index */ |
| 125 |
bool m_srid_is_valid{}; |
| 126 |
|
| 127 |
/** SRID obtained from dd column */ |
| 128 |
uint32_t m_srid{}; |
| 129 |
}; |
| 130 |
|
| 131 |
/** Structure for reporting duplicate records. */ |
| 132 |
struct Dup { |
| 133 |
/** Report a duplicate key. |
| 134 |
@param[in] entry For reporting duplicate key. */ |
| 135 |
void report(const dfield_t *entry) noexcept; |
| 136 |
|
| 137 |
/** Report a duplicate key. |
| 138 |
@param[in] entry For reporting duplicate key. |
| 139 |
@param[in] offsets Row offsets */ |
| 140 |
void report(const mrec_t *entry, const ulint *offsets) noexcept; |
| 141 |
|
| 142 |
/** @return true if no duplicates reported yet. */ |
| 143 |
[[nodiscard]] bool empty() const noexcept { return m_n_dup == 0; } |
| 144 |
|
| 145 |
/** Index being sorted */ |
| 146 |
dict_index_t *m_index{}; |
| 147 |
|
| 148 |
/** MySQL table object */ |
| 149 |
TABLE *m_table{}; |
| 150 |
|
| 151 |
/** Mapping of column numbers in table to the rebuilt table |
| 152 |
(index->table), or NULL if not rebuilding table */ |
| 153 |
const ulint *m_col_map{}; |
| 154 |
|
| 155 |
/** Number of duplicates */ |
| 156 |
size_t m_n_dup{}; |
| 157 |
}; |
| 158 |
|
| 159 |
/** Captures ownership and manages lifetime of an already opened OS file |
| 160 |
descriptor. Closes the file on object destruction. */ |
| 161 |
class Unique_os_file_descriptor : private ut::Non_copyable { |
| 162 |
public: |
| 163 |
/** Default constructor, does not hold any file, does not close any on |
| 164 |
destruction. */ |
| 165 |
Unique_os_file_descriptor() = default; |
| 166 |
/** Main constructor capturing an already opened OS file descriptor. */ |
| 167 |
Unique_os_file_descriptor(os_fd_t fd) : m_fd(fd) {} |
| 168 |
|
| 169 |
Unique_os_file_descriptor(Unique_os_file_descriptor &&other) { swap(other); } |
| 170 |
|
| 171 |
~Unique_os_file_descriptor() { close(); } |
| 172 |
|
| 173 |
/** Returns the managed OS file descriptor for use with OS functions that |
| 174 |
operate on file. Do not close this file. */ |
| 175 |
os_fd_t get() const { |
| 176 |
ut_a(is_open()); |
| 177 |
return m_fd; |
| 178 |
} |
| 179 |
bool is_open() const { return m_fd != OS_FD_CLOSED; } |
| 180 |
|
| 181 |
Unique_os_file_descriptor &operator=(Unique_os_file_descriptor &&other) { |
| 182 |
close(); |
| 183 |
swap(other); |
| 184 |
return *this; |
| 185 |
} |
| 186 |
|
| 187 |
/** Swaps the underlying managed file descriptors between two instances of |
| 188 |
Unique_os_file_descriptor. No files are closed. */ |
| 189 |
void swap(Unique_os_file_descriptor &other) { std::swap(m_fd, other.m_fd); } |
| 190 |
|
| 191 |
/** Closes the managed file. Leaves the instance in the same state as default |
| 192 |
constructed instance. */ |
| 193 |
void close() { |
| 194 |
#ifdef UNIV_PFS_IO |
| 195 |
struct PSI_file_locker *locker = nullptr; |
| 196 |
PSI_file_locker_state state; |
| 197 |
locker = PSI_FILE_CALL(get_thread_file_descriptor_locker)(&state, m_fd, |
| 198 |
PSI_FILE_CLOSE); |
| 199 |
if (locker != nullptr) { |
| 200 |
PSI_FILE_CALL(start_file_wait) |
| 201 |
(locker, 0, __FILE__, __LINE__); |
| 202 |
} |
| 203 |
#endif /* UNIV_PFS_IO */ |
| 204 |
if (m_fd != OS_FD_CLOSED) { |
| 205 |
::close(m_fd); |
| 206 |
m_fd = OS_FD_CLOSED; |
| 207 |
} |
| 208 |
#ifdef UNIV_PFS_IO |
| 209 |
if (locker != nullptr) { |
| 210 |
PSI_FILE_CALL(end_file_wait)(locker, 0); |
| 211 |
} |
| 212 |
#endif /* UNIV_PFS_IO */ |
| 213 |
} |
| 214 |
|
| 215 |
private: |
| 216 |
os_fd_t m_fd{OS_FD_CLOSED}; |
| 217 |
}; |
| 218 |
|
| 219 |
/** Sets an exclusive lock on a table, for the duration of creating indexes. |
| 220 |
@param[in,out] trx Transaction |
| 221 |
@param[in] table Table to lock. |
| 222 |
@param[in] mode Lock mode LOCK_X or LOCK_S |
| 223 |
@return error code or DB_SUCCESS */ |
| 224 |
[[nodiscard]] dberr_t lock_table(trx_t *trx, dict_table_t *table, |
| 225 |
lock_mode mode) noexcept; |
| 226 |
|
| 227 |
/** Drop those indexes which were created before an error occurred. |
| 228 |
The data dictionary must have been locked exclusively by the caller, |
| 229 |
because the transaction will not be committed. |
| 230 |
@param[in,out] trx Transaction |
| 231 |
@param[in] table Table to lock. |
| 232 |
@param[in] locked true=table locked, false=may need to do a lazy |
| 233 |
drop */ |
| 234 |
void drop_indexes(trx_t *trx, dict_table_t *table, bool locked) noexcept; |
| 235 |
|
| 236 |
/**Create temporary merge files in the given parameter path, and if |
| 237 |
UNIV_PFS_IO defined, register the file descriptor with Performance Schema. |
| 238 |
@param[in] path Location for creating temporary merge files. |
| 239 |
@return File descriptor */ |
| 240 |
[[nodiscard]] Unique_os_file_descriptor file_create_low( |
| 241 |
const char *path) noexcept; |
| 242 |
|
| 243 |
/** Create the index and load in to the dictionary. |
| 244 |
@param[in,out] trx Trx (sets error_state) |
| 245 |
@param[in,out] table The index is on this table |
| 246 |
@param[in] index_def The index definition |
| 247 |
@param[in] add_v New virtual columns added along with add |
| 248 |
index call |
| 249 |
@return index, or nullptr on error */ |
| 250 |
[[nodiscard]] dict_index_t *create_index( |
| 251 |
trx_t *trx, dict_table_t *table, const Index_defn *index_def, |
| 252 |
const dict_add_v_col_t *add_v) noexcept; |
| 253 |
|
| 254 |
/** Drop a table. The caller must have ensured that the background stats |
| 255 |
thread is not processing the table. This can be done by calling |
| 256 |
dict_stats_wait_bg_to_stop_using_table() after locking the dictionary and |
| 257 |
before calling this function. |
| 258 |
@param[in,out] trx Transaction |
| 259 |
@param[in,out] table Table to drop. |
| 260 |
@return DB_SUCCESS or error code */ |
| 261 |
dberr_t drop_table(trx_t *trx, dict_table_t *table) noexcept; |
| 262 |
|
| 263 |
/** Generate the next autoinc based on a snapshot of the session |
| 264 |
auto_increment_increment and auto_increment_offset variables. |
| 265 |
Assignment operator would be used during the inplace_alter_table() |
| 266 |
phase only **/ |
| 267 |
struct Sequence { |
| 268 |
/** Constructor. |
| 269 |
@param[in,out] thd The session |
| 270 |
@param[in] start_value The lower bound |
| 271 |
@param[in] max_value The upper bound (inclusive) */ |
| 272 |
Sequence(THD *thd, ulonglong start_value, ulonglong max_value) noexcept; |
| 273 |
|
| 274 |
/** Destructor. */ |
| 275 |
~Sequence() = default; |
| 276 |
|
| 277 |
/** Postfix increment |
| 278 |
@return the value to insert */ |
| 279 |
ulonglong operator++(int) noexcept; |
| 280 |
|
| 281 |
/** Check if the autoinc "sequence" is exhausted. |
| 282 |
@return true if the sequence is exhausted */ |
| 283 |
bool eof() const noexcept { return m_eof; } |
| 284 |
|
| 285 |
/** Assignment operator to copy the sequence values |
| 286 |
@param[in] rhs Sequence to copy from */ |
| 287 |
ddl::Sequence &operator=(const ddl::Sequence &rhs) noexcept { |
| 288 |
ut_ad(rhs.m_next_value > 0); |
| 289 |
ut_ad(rhs.m_max_value == m_max_value); |
| 290 |
m_next_value = rhs.m_next_value; |
| 291 |
m_increment = rhs.m_increment; |
| 292 |
m_offset = rhs.m_offset; |
| 293 |
m_eof = rhs.m_eof; |
| 294 |
return *this; |
| 295 |
} |
| 296 |
|
| 297 |
/** @return the next value in the sequence */ |
| 298 |
ulonglong last() const noexcept { |
| 299 |
ut_ad(m_next_value > 0); |
| 300 |
return m_next_value; |
| 301 |
} |
| 302 |
|
| 303 |
/** Maximum column value if adding an AUTOINC column else 0. Once |
| 304 |
we reach the end of the sequence it will be set to ~0. */ |
| 305 |
const ulonglong m_max_value{}; |
| 306 |
|
| 307 |
/** Value of auto_increment_increment */ |
| 308 |
ulong m_increment{}; |
| 309 |
|
| 310 |
/** Value of auto_increment_offset */ |
| 311 |
ulong m_offset{}; |
| 312 |
|
| 313 |
/** Next value in the sequence */ |
| 314 |
ulonglong m_next_value{}; |
| 315 |
|
| 316 |
/** true if no more values left in the sequence */ |
| 317 |
bool m_eof{}; |
| 318 |
}; |
| 319 |
|
| 320 |
/** DDL context/configuration. */ |
| 321 |
struct Context { |
| 322 |
/** Full text search context information and state. */ |
| 323 |
struct FTS { |
| 324 |
/** Document ID sequence */ |
| 325 |
struct Sequence { |
| 326 |
/** Destructor. */ |
| 327 |
virtual ~Sequence() noexcept; |
| 328 |
|
| 329 |
/** Get the next document ID. |
| 330 |
@param[in] dtuple Row from which to fetch ID. |
| 331 |
@return the next document ID. */ |
| 332 |
virtual doc_id_t fetch(const dtuple_t *dtuple = nullptr) noexcept = 0; |
| 333 |
|
| 334 |
/** Get the current document ID. |
| 335 |
@return the current document ID. */ |
| 336 |
virtual doc_id_t current() noexcept = 0; |
| 337 |
|
| 338 |
/** @return the number of document IDs generated. */ |
| 339 |
virtual doc_id_t generated_count() const noexcept = 0; |
| 340 |
|
| 341 |
/** @return the maximum document ID seen so far. */ |
| 342 |
virtual doc_id_t max_doc_id() const noexcept = 0; |
| 343 |
|
| 344 |
/** @return true if the document ID is generated, instead of fetched |
| 345 |
from a column from the row. */ |
| 346 |
virtual bool is_generated() const noexcept = 0; |
| 347 |
|
| 348 |
/** Advance the document ID. */ |
| 349 |
virtual void increment() noexcept = 0; |
| 350 |
|
| 351 |
/** Current document ID. */ |
| 352 |
doc_id_t m_doc_id{}; |
| 353 |
}; |
| 354 |
|
| 355 |
/** Constructor. |
| 356 |
@param[in] n_parser_threads Number of FTS parser threads. */ |
| 357 |
explicit FTS(size_t n_parser_threads) noexcept |
| 358 |
: m_n_parser_threads(n_parser_threads) {} |
| 359 |
|
| 360 |
/** Destructor. */ |
| 361 |
~FTS() noexcept { ut::delete_(m_doc_id); } |
| 362 |
|
| 363 |
/** FTS index. */ |
| 364 |
dict_index_t *m_index{}; |
| 365 |
|
| 366 |
/** Maximum number of FTS parser and sort threads to use. */ |
| 367 |
const size_t m_n_parser_threads{}; |
| 368 |
|
| 369 |
/** Document ID sequence generator. */ |
| 370 |
Sequence *m_doc_id{}; |
| 371 |
|
| 372 |
/** FTS instance. */ |
| 373 |
ddl::FTS *m_ptr{}; |
| 374 |
}; |
| 375 |
|
| 376 |
/** Scan sort and IO buffer size. */ |
| 377 |
using Scan_buffer_size = std::pair<size_t, size_t>; |
| 378 |
|
| 379 |
/** Build indexes on a table by reading a clustered index, creating a |
| 380 |
temporary file containing index entries, merge sorting these index entries and |
| 381 |
inserting sorted index entries to indexes. |
| 382 |
@param[in] trx Transaction. |
| 383 |
@param[in] old_table Table where rows are read from |
| 384 |
@param[in] new_table Table where indexes are created; identical to |
| 385 |
old_table unless creating a PRIMARY KEY |
| 386 |
@param[in] online True if creating indexes online |
| 387 |
@param[in] indexes Indexes to be created |
| 388 |
@param[in] key_numbers MySQL key numbers |
| 389 |
@param[in] n_indexes Size of indexes[] |
| 390 |
@param[in,out] table MySQL table, for reporting erroneous key |
| 391 |
value if applicable |
| 392 |
@param[in] add_cols Default values of added columns, or NULL |
| 393 |
@param[in] col_map Mapping of old column numbers to new |
| 394 |
ones, or nullptr if old_table == new_table |
| 395 |
@param[in] add_autoinc Number of added AUTO_INCREMENT columns, or |
| 396 |
ULINT_UNDEFINED if none is added |
| 397 |
@param[in,out] sequence Autoinc sequence |
| 398 |
@param[in] skip_pk_sort Whether the new PRIMARY KEY will follow |
| 399 |
existing order |
| 400 |
@param[in,out] stage Performance schema accounting object, |
| 401 |
used by ALTER TABLE. |
| 402 |
stage->begin_phase_read_pk() will be called |
| 403 |
at the beginning of this function and it will |
| 404 |
be passed to other functions for further |
| 405 |
accounting. |
| 406 |
@param[in] add_v New virtual columns added along with indexes |
| 407 |
@param[in] eval_table MySQL table used to evaluate virtual column |
| 408 |
value, see innobase_get_computed_value(). |
| 409 |
@param[in] max_buffer_size Memory use upper limit. |
| 410 |
@param[in] max_threads true if DDL should use multiple threads. */ |
| 411 |
Context(trx_t *trx, dict_table_t *old_table, dict_table_t *new_table, |
| 412 |
bool online, dict_index_t **indexes, const ulint *key_numbers, |
| 413 |
size_t n_indexes, TABLE *table, const dtuple_t *add_cols, |
| 414 |
const ulint *col_map, size_t add_autoinc, ddl::Sequence &sequence, |
| 415 |
bool skip_pk_sort, Alter_stage *stage, const dict_add_v_col_t *add_v, |
| 416 |
TABLE *eval_table, size_t max_buffer_size, |
| 417 |
size_t max_threads) noexcept; |
| 418 |
|
| 419 |
/** Destructor. */ |
| 420 |
~Context() noexcept; |
| 421 |
|
| 422 |
/** @return the DDL error status. */ |
| 423 |
dberr_t get_error() const noexcept { return m_err; } |
| 424 |
|
| 425 |
/** Set the error code, when it's not specific to an index. |
| 426 |
@param[in] err Error code. */ |
| 427 |
void set_error(dberr_t err) noexcept { |
| 428 |
ut_a(err != DB_SUCCESS && err != DB_END_OF_INDEX); |
| 429 |
|
| 430 |
/* This should only be settable by the the thread that encounters the |
| 431 |
first error, therefore try only once. */ |
| 432 |
|
| 433 |
dberr_t expected{DB_SUCCESS}; |
| 434 |
m_err.compare_exchange_strong(expected, err); |
| 435 |
} |
| 436 |
|
| 437 |
/** Set the error code and index number where the error occurred. |
| 438 |
@param[in] err Error code. |
| 439 |
@param[in] id Index ordinal value where error occurred. */ |
| 440 |
void set_error(dberr_t err, size_t id) noexcept { |
| 441 |
/* This should only be settable by the the thread that encounters the |
| 442 |
first error, therefore try only once. */ |
| 443 |
|
| 444 |
ut_a(err != DB_SUCCESS); |
| 445 |
|
| 446 |
dberr_t expected{DB_SUCCESS}; |
| 447 |
|
| 448 |
if (m_err.compare_exchange_strong(expected, err)) { |
| 449 |
ut_ad(m_err_key_number == std::numeric_limits<size_t>::max()); |
| 450 |
m_err_key_number = m_key_numbers[id]; |
| 451 |
} |
| 452 |
} |
| 453 |
|
| 454 |
/** Build the indexes. |
| 455 |
@return DB_SUCCESS or error code. */ |
| 456 |
[[nodiscard]] dberr_t build() noexcept; |
| 457 |
|
| 458 |
/** @return the flush observer to use for flushing. */ |
| 459 |
[[nodiscard]] Flush_observer *flush_observer() noexcept; |
| 460 |
|
| 461 |
/** @return the old table. */ |
| 462 |
[[nodiscard]] dict_table_t *old_table() noexcept { return m_old_table; } |
| 463 |
|
| 464 |
/** @return the new table. */ |
| 465 |
[[nodiscard]] dict_table_t *new_table() noexcept { return m_new_table; } |
| 466 |
|
| 467 |
/** Calculate the sort and buffer size per thread. |
| 468 |
@param[in] n_threads Total number of threads used for scanning. |
| 469 |
@return the sort and IO buffer size per thread. */ |
| 470 |
[[nodiscard]] Scan_buffer_size scan_buffer_size( |
| 471 |
size_t n_threads) const noexcept; |
| 472 |
|
| 473 |
/** Calculate the io buffer size per file for the sort phase. |
| 474 |
@param[in] n_buffers Total number of buffers to use for the merge. |
| 475 |
@return the sort buffer size for one instance. */ |
| 476 |
[[nodiscard]] size_t merge_io_buffer_size(size_t n_buffers) const noexcept; |
| 477 |
|
| 478 |
/** Calculate the io buffer size per file for the load phase. |
| 479 |
@param[in] n_buffers Total number of buffers to use for the loading. |
| 480 |
@return the per thread io buffer size. */ |
| 481 |
[[nodiscard]] size_t load_io_buffer_size(size_t n_buffers) const noexcept; |
| 482 |
|
| 483 |
/** Request number of bytes for a buffer. |
| 484 |
@param[in] n Number of bytes requested. |
| 485 |
@return the number of bytes available. */ |
| 486 |
[[nodiscard]] size_t allocate(size_t n) const; |
| 487 |
|
| 488 |
/** @return the server session/connection context. */ |
| 489 |
[[nodiscard]] THD *thd() noexcept; |
| 490 |
|
| 491 |
/** Copy the added columns dtuples so that we don't use the same |
| 492 |
column data buffer for the added column across multiple threads. |
| 493 |
@return new instance or nullptr if out of memory. */ |
| 494 |
[[nodiscard]] dtuple_t *create_add_cols() noexcept; |
| 495 |
|
| 496 |
private: |
| 497 |
/** @return the cluster index read cursor. */ |
| 498 |
[[nodiscard]] Cursor *cursor() noexcept { return m_cursor; } |
| 499 |
|
| 500 |
/** @return the original table cluster index. */ |
| 501 |
[[nodiscard]] const dict_index_t *index() const noexcept; |
| 502 |
|
| 503 |
/** Initialize the context for a cluster index scan. |
| 504 |
@param[in,out] cursor Cursor used for the cluster index read. */ |
| 505 |
[[nodiscard]] dberr_t read_init(Cursor *cursor) noexcept; |
| 506 |
|
| 507 |
/** Initialize the FTS build infrastructure. |
| 508 |
@param[in,out] index Index prototype to build. |
| 509 |
@return DB_SUCCESS or error code. */ |
| 510 |
dberr_t fts_create(dict_index_t *index) noexcept; |
| 511 |
|
| 512 |
/** Setup the FTS index build data structures. |
| 513 |
@return DB_SUCCESS or error code. */ |
| 514 |
[[nodiscard]] dberr_t setup_fts_build() noexcept; |
| 515 |
|
| 516 |
/** Get the next Doc ID and increment the current value. |
| 517 |
@return a document ID. */ |
| 518 |
[[nodiscard]] doc_id_t next_doc_id() noexcept; |
| 519 |
|
| 520 |
/** Update the FTS document ID. */ |
| 521 |
void update_fts_doc_id() noexcept; |
| 522 |
|
| 523 |
/** Check the state of the online build log for the index. |
| 524 |
@return DB_SUCCESS or error code. */ |
| 525 |
[[nodiscard]] dberr_t check_state_of_online_build_log() noexcept; |
| 526 |
|
| 527 |
/** Track the highest TxID that modified this index when the scan |
| 528 |
was completed. We prevent older readers from accessing this index, to |
| 529 |
ensure read consistency. |
| 530 |
@param[in,out] index Index to track. */ |
| 531 |
void note_max_trx_id(dict_index_t *index) noexcept; |
| 532 |
|
| 533 |
/** Setup the primary key sort. |
| 534 |
@param[in,out] cursor Setup the primary key data structures. |
| 535 |
@return DB_SUCCESS or error code. */ |
| 536 |
[[nodiscard]] dberr_t setup_pk_sort(Cursor *cursor) noexcept; |
| 537 |
|
| 538 |
/** Init the non-null column constraints checks (if required). */ |
| 539 |
void setup_nonnull() noexcept; |
| 540 |
|
| 541 |
/** Check if the nonnull columns satisfy the constraint. |
| 542 |
@param[in] row Row to check. |
| 543 |
@return true on success. */ |
| 544 |
[[nodiscard]] bool check_null_constraints(const dtuple_t *row) const noexcept; |
| 545 |
|
| 546 |
/** Clean up the data structures at the end of the DDL. |
| 547 |
@param[in] err Status of the DDL. |
| 548 |
@return DB_SUCCESS or error code. */ |
| 549 |
[[nodiscard]] dberr_t cleanup(dberr_t err) noexcept; |
| 550 |
|
| 551 |
/** Handle auto increment. |
| 552 |
@param[in] row Row with autoinc column. |
| 553 |
@param[in] heap Heap to use for allocation of autoinc column. |
| 554 |
@return DB_SUCCESS or error code. */ |
| 555 |
[[nodiscard]] dberr_t handle_autoinc(const dtuple_t *row, |
| 556 |
mem_heap_t *heap) noexcept; |
| 557 |
|
| 558 |
/** @return true if any virtual columns are involved. */ |
| 559 |
[[nodiscard]] bool has_virtual_columns() const noexcept; |
| 560 |
|
| 561 |
/** @return true if any FTS indexes are involved. */ |
| 562 |
[[nodiscard]] bool has_fts_indexes() const noexcept; |
| 563 |
|
| 564 |
/** @return true if the DDL was interrupted. */ |
| 565 |
[[nodiscard]] bool is_interrupted() noexcept; |
| 566 |
|
| 567 |
private: |
| 568 |
using Key_numbers = std::vector<size_t, ut::allocator<size_t>>; |
| 569 |
using Indexes = std::vector<dict_index_t *, ut::allocator<dict_index_t *>>; |
| 570 |
|
| 571 |
/** Common error code for all index builders running in parallel. */ |
| 572 |
std::atomic<dberr_t> m_err{DB_SUCCESS}; |
| 573 |
|
| 574 |
/** Index where the error occurred. */ |
| 575 |
size_t m_err_key_number{std::numeric_limits<size_t>::max()}; |
| 576 |
|
| 577 |
/** Transaction covering the index build. */ |
| 578 |
trx_t *m_trx{}; |
| 579 |
|
| 580 |
/** The FTS builder. There is one FTS per table. */ |
| 581 |
FTS m_fts; |
| 582 |
|
| 583 |
/** Source table, read rows from this table. */ |
| 584 |
dict_table_t *m_old_table{}; |
| 585 |
|
| 586 |
/** Table where indexes are created; identical to old_table unless creating |
| 587 |
a PRIMARY KEY. */ |
| 588 |
dict_table_t *m_new_table{}; |
| 589 |
|
| 590 |
/** True if creating index online. Non-online implies that we have an |
| 591 |
S latch on the table, therefore there can't be concurrent updates to |
| 592 |
the table while we are executing the DDL. We don't log the changes to |
| 593 |
the row log. */ |
| 594 |
bool m_online{}; |
| 595 |
|
| 596 |
/** Indexes to be created. */ |
| 597 |
Indexes m_indexes{}; |
| 598 |
|
| 599 |
/** MySQL key numbers. */ |
| 600 |
Key_numbers m_key_numbers{}; |
| 601 |
|
| 602 |
/** MySQL table for reporting errors/warnings. */ |
| 603 |
TABLE *m_table{}; |
| 604 |
|
| 605 |
/** Default value for added columns or null. */ |
| 606 |
const dtuple_t *m_add_cols{}; |
| 607 |
|
| 608 |
/** Mapping of old column numbers to new ones, or nullptr if none |
| 609 |
were added. */ |
| 610 |
const ulint *m_col_map{}; |
| 611 |
|
| 612 |
/** Number of added AUTO_INCREMENT columns, or ULINT_UNDEFINED if |
| 613 |
none added. */ |
| 614 |
size_t m_add_autoinc{ULINT_UNDEFINED}; |
| 615 |
|
| 616 |
/** Autoinc sequence. */ |
| 617 |
ddl::Sequence &m_sequence; |
| 618 |
|
| 619 |
/** Performance schema accounting object, used by ALTER TABLE. |
| 620 |
stage->begin_phase_read_pk() will be called at the beginning of |
| 621 |
this function and it will be passed to other functions for further |
| 622 |
accounting. */ |
| 623 |
Alter_stage *m_stage{}; |
| 624 |
|
| 625 |
/** New virtual columns added along with indexes */ |
| 626 |
const dict_add_v_col_t *m_add_v{}; |
| 627 |
|
| 628 |
/** MySQL table used to evaluate virtual column value, see |
| 629 |
innobase_get_computed_value(). */ |
| 630 |
TABLE *m_eval_table{}; |
| 631 |
|
| 632 |
/** Skip the sorting phase if true. */ |
| 633 |
bool m_skip_pk_sort{}; |
| 634 |
|
| 635 |
/** Non null columns. */ |
| 636 |
std::vector<size_t, ut::allocator<size_t>> m_nonnull{}; |
| 637 |
|
| 638 |
/** Number of unique columns in the key. */ |
| 639 |
size_t m_n_uniq{}; |
| 640 |
|
| 641 |
/** true if need flush observer. */ |
| 642 |
bool m_need_observer{}; |
| 643 |
|
| 644 |
/** Cursor for reading the cluster index. */ |
| 645 |
Cursor *m_cursor{}; |
| 646 |
|
| 647 |
/** Number of bytes used. */ |
| 648 |
size_t m_n_allocated{}; |
| 649 |
|
| 650 |
/** Maximum number of bytes to use. */ |
| 651 |
const size_t m_max_buffer_size{}; |
| 652 |
|
| 653 |
/** Maximum number of threads to use. We don't do a parallel scan of the |
| 654 |
clustered index when FTS and/or virtual columns are involved. The build |
| 655 |
phase is parallel though. */ |
| 656 |
const size_t m_max_threads{}; |
| 657 |
|
| 658 |
/** For parallel access to the autoincrement generator. */ |
| 659 |
ib_mutex_t m_autoinc_mutex; |
| 660 |
|
| 661 |
/** Heap for copies of m_add_cols. */ |
| 662 |
mem_heap_t *m_dtuple_heap{}; |
| 663 |
|
| 664 |
friend struct Row; |
| 665 |
friend class Loader; |
| 666 |
friend struct Cursor; |
| 667 |
friend struct Builder; |
| 668 |
friend struct ddl::FTS; |
| 669 |
friend struct Load_cursor; |
| 670 |
friend struct Btree_cursor; |
| 671 |
friend struct Merge_file_sort; |
| 672 |
friend struct Parallel_cursor; |
| 673 |
}; |
| 674 |
|
| 675 |
} // namespace ddl |
| 676 |
|
| 677 |
#endif /* ddl0ddl_h */ |