Debian Codegraph @

storage/innobase/ddl/ddl0ddl.cc

raw
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 ddl/ddl0ddl.cc
29 DDL implementation misc functions.
30 Created 2020-11-01 by Sunny Bains. */
31
32 #include "btr0load.h"
33 #include "ddl0fts.h"
34 #include "ddl0impl-cursor.h"
35 #include "ddl0impl-merge.h"
36 #include "dict0dd.h"
37 #include "handler0alter.h"
38 #include "lock0lock.h"
39 #include "row0log.h"
40
41 /* Ignore posix_fadvise() on those platforms where it does not exist */
42 #if defined _WIN32
43 #define posix_fadvise(fd, offset, len, advice) /* nothing */
44 #endif /* _WIN32 */
45
46 /** Whether to disable file system cache */
47 bool srv_disable_sort_file_cache;
48
49 namespace ddl {
50
51 /** Note that an index build has failed.
52 @param[in,out] index Index that failed to build. */
53 static void index_build_failed(dict_index_t *index) noexcept;
54
55 doc_id_t Fetch_sequence::fetch(const dtuple_t *dtuple) noexcept {
56 const auto doc_field =
57 dtuple_get_nth_field(dtuple, m_index->table->fts->doc_col);
58
59 const auto doc_id = static_cast<doc_id_t>(
60 mach_read_from_8(static_cast<byte *>(dfield_get_data(doc_field))));
61
62 if (doc_id > m_max_doc_id) {
63 m_max_doc_id = doc_id;
64 }
65
66 return doc_id;
67 }
68
69 void Dup::report(const mrec_t *mrec, const ulint *offsets) noexcept {
70 ++m_n_dup;
71
72 if (m_n_dup == 1) {
73 auto heap = mem_heap_create(1024, UT_LOCATION_HERE);
74 auto dtuple = row_rec_to_index_entry_low(mrec, m_index, offsets, heap);
75
76 /* Report the first duplicate record, but count all duplicate records. */
77 innobase_fields_to_mysql(m_table, m_index, dtuple->fields);
78
79 mem_heap_free(heap);
80 }
81 }
82
83 void Dup::report(const dfield_t *dfield) noexcept {
84 ++m_n_dup;
85
86 if (m_n_dup == 1) {
87 /* Report the first duplicate record, but count all duplicate records. */
88 innobase_fields_to_mysql(m_table, m_index, dfield);
89 }
90 }
91
92 dberr_t pread(os_fd_t fd, void *ptr, size_t len, os_offset_t offset) noexcept {
93 IF_ENABLED("ddl_read_failure", return DB_IO_ERROR;)
94
95 IORequest request;
96
97 /* Merge sort pages are never compressed. */
98 request.disable_compression();
99
100 auto err = os_file_read_no_error_handling_int_fd(request, "(ddl)", fd, ptr,
101 offset, len, nullptr);
102
103 #ifdef POSIX_FADV_DONTNEED
104 /* Each block is read exactly once. Free up the file cache. */
105 posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
106 #endif /* POSIX_FADV_DONTNEED */
107
108 return err;
109 }
110
111 dberr_t pwrite(os_fd_t fd, void *ptr, size_t len, os_offset_t offset) noexcept {
112 IF_ENABLED("ddl_write_failure", return DB_IO_ERROR;)
113
114 IORequest request(IORequest::WRITE);
115
116 request.disable_compression();
117
118 auto err = os_file_write_int_fd(request, "(ddl)", fd, ptr, offset, len);
119
120 #ifdef POSIX_FADV_DONTNEED
121 /* The block will be needed on the next merge pass,
122 but it can be evicted from the file cache meanwhile. */
123 posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
124 #endif /* POSIX_FADV_DONTNEED */
125
126 return err;
127 }
128
129 Unique_os_file_descriptor file_create_low(const char *path) noexcept {
130 if (path == nullptr) {
131 path = innobase_mysql_tmpdir();
132 }
133 #ifdef UNIV_PFS_IO
134 /* This temp file open does not go through normal file APIs, add
135 instrumentation to register with performance schema */
136 Datafile df;
137
138 df.make_filepath(path, "Innodb Merge Temp File", NO_EXT);
139
140 PSI_file_locker *locker{};
141 PSI_file_locker_state state;
142
143 locker = PSI_FILE_CALL(get_thread_file_name_locker)(
144 &state, innodb_temp_file_key.m_value, PSI_FILE_OPEN, df.filepath(),
145 &locker);
146
147 if (locker != nullptr) {
148 PSI_FILE_CALL(start_file_open_wait)(locker, __FILE__, __LINE__);
149 }
150
151 #endif /* UNIV_PFS_IO */
152
153 auto fd = innobase_mysql_tmpfile(path);
154
155 #ifdef UNIV_PFS_IO
156 if (locker != nullptr) {
157 PSI_FILE_CALL(end_file_open_wait_and_bind_to_descriptor)(locker, fd);
158 }
159 #endif /* UNIV_PFS_IO */
160
161 if (fd < 0) {
162 ib::error(ER_IB_MSG_967) << "Cannot create temporary merge file";
163 return Unique_os_file_descriptor{};
164 }
165
166 return Unique_os_file_descriptor{fd};
167 }
168
169 bool file_create(ddl::file_t *file, const char *path) noexcept {
170 file->m_size = 0;
171 file->m_n_recs = 0;
172 file->m_file = ddl::file_create_low(path);
173
174 if (file->m_file.is_open()) {
175 if (srv_disable_sort_file_cache) {
176 os_file_set_nocache(file->m_file.get(), "ddl0ddl.cc", "sort");
177 }
178 return true;
179 }
180
181 return false;
182 }
183
184 dict_index_t *create_index(trx_t *trx, dict_table_t *table,
185 const Index_defn *index_def,
186 const dict_add_v_col_t *add_v) noexcept {
187 const size_t n_fields = index_def->m_n_fields;
188
189 ut_ad(!srv_read_only_mode);
190
191 /* Create the index prototype, using the passed in def, this is not
192 a persistent operation. We pass 0 as the space id, and determine at
193 a lower level the space id where to row the table. */
194
195 auto index = dict_mem_index_create(table->name.m_name, index_def->m_name, 0,
196 index_def->m_ind_type, n_fields);
197
198 ut_a(index);
199
200 index->set_committed(index_def->m_rebuild);
201
202 bool has_new_v_col{};
203
204 for (size_t i = 0; i < n_fields; i++) {
205 const char *name;
206 auto ifield = &index_def->m_fields[i];
207
208 if (ifield->m_is_v_col) {
209 if (ifield->m_col_no >= table->n_v_def) {
210 ut_ad(ifield->m_col_no < table->n_v_def + add_v->n_v_col);
211 ut_ad(ifield->m_col_no >= table->n_v_def);
212 name = add_v->v_col_name[ifield->m_col_no - table->n_v_def];
213
214 has_new_v_col = true;
215 } else {
216 name = dict_table_get_v_col_name(table, ifield->m_col_no);
217 }
218 } else {
219 name = table->get_col_name(ifield->m_col_no);
220 }
221
222 index->add_field(name, ifield->m_prefix_len, ifield->m_is_ascending);
223 }
224
225 /* Create B-tree */
226 dict_sys_mutex_exit();
227
228 dict_build_index_def(table, index, trx);
229
230 auto err = dict_index_add_to_cache_w_vcol(table, index, add_v, index->page,
231 trx_is_strict(trx));
232
233 if (err != DB_SUCCESS) {
234 trx->error_state = err;
235 dict_sys_mutex_enter();
236 return nullptr;
237 }
238
239 index = dict_table_get_index_on_name(table, index_def->m_name,
240 index_def->m_rebuild);
241 ut_ad(index != nullptr);
242
243 err = dict_create_index_tree_in_mem(index, trx);
244
245 dict_sys_mutex_enter();
246
247 if (err != DB_SUCCESS) {
248 if ((index->type & DICT_FTS) && table->fts) {
249 fts_cache_index_cache_remove(table, index);
250 }
251
252 trx->error_state = err;
253 return nullptr;
254 }
255
256 if (dict_index_is_spatial(index)) {
257 index->fill_srid_value(index_def->m_srid, index_def->m_srid_is_valid);
258 }
259
260 /* Adjust field name for newly added virtual columns. */
261 for (size_t i = 0; i < n_fields; i++) {
262 auto ifield = &index_def->m_fields[i];
263
264 if (ifield->m_is_v_col && ifield->m_col_no >= table->n_v_def) {
265 ut_ad(ifield->m_col_no < table->n_v_def + add_v->n_v_col);
266 ut_ad(ifield->m_col_no >= table->n_v_def);
267 dict_field_t *field = index->get_field(i);
268 field->name = add_v->v_col_name[ifield->m_col_no - table->n_v_def];
269 }
270 }
271
272 if (dict_index_is_spatial(index)) {
273 index->fill_srid_value(index_def->m_srid, index_def->m_srid_is_valid);
274 index->rtr_srs.reset(fetch_srs(index->srid));
275 }
276
277 index->parser = index_def->m_parser;
278 index->is_ngram = index_def->m_is_ngram;
279 index->has_new_v_col = has_new_v_col;
280
281 /* Note the id of the transaction that created this
282 index, we use it to restrict readers from accessing
283 this index, to ensure read consistency. */
284 ut_ad(index->trx_id == trx->id);
285
286 index->table->def_trx_id = trx->id;
287
288 return index;
289 }
290
291 dberr_t drop_table(trx_t *trx, dict_table_t *table) noexcept {
292 ut_ad(!srv_read_only_mode);
293
294 /* There must be no open transactions on the table. */
295 ut_a(table->get_ref_count() == 0);
296
297 return row_drop_table_for_mysql(table->name.m_name, trx, false, nullptr);
298 }
299
300 dberr_t lock_table(trx_t *trx, dict_table_t *table,
301 enum lock_mode mode) noexcept {
302 ut_ad(!srv_read_only_mode);
303 ut_ad(mode == LOCK_X || mode == LOCK_S);
304
305 trx->op_info = "setting table lock for creating or dropping index";
306 /* Trx for DDL should not be forced to rollback for now */
307 trx->in_innodb |= TRX_FORCE_ROLLBACK_DISABLE;
308
309 return lock_table_for_trx(table, trx, mode);
310 }
311
312 static void index_build_failed(dict_index_t *index) noexcept {
313 DEBUG_SYNC_C("merge_drop_index_after_abort");
314
315 rw_lock_x_lock(dict_index_get_lock(index), UT_LOCATION_HERE);
316
317 dict_index_set_online_status(index, ONLINE_INDEX_ABORTED_DROPPED);
318
319 rw_lock_x_unlock(dict_index_get_lock(index));
320
321 index->table->drop_aborted = true;
322
323 if (dict_index_has_virtual(index)) {
324 /* If multi-value index, a virtual column would be created too.
325 This is same for a virtual column along with an index on it case.
326 In this rollback case, the metadata for this index would be
327 inconsistent with metadata of table, because table metadata
328 doesn't have the virtual column for this index yet. So set
329 this index to be corrupted and any further use of this index
330 would be prevented. */
331 index->type |= DICT_CORRUPT;
332 }
333 }
334
335 /** We will have to drop the secondary indexes later, when the table is
336 in use, unless the the DDL has already been externalized. Mark the indexes
337 as incomplete and corrupted, so that other threads will stop using them.
338 Let dict_table_close() or crash recovery or the next invocation of
339 prepare_inplace_alter_table() take care of dropping the indexes.
340 @param[in,out] trx Transaction
341 @param[in] table Table that owns the indexes. */
342 static void mark_secondary_indexes(trx_t *trx, dict_table_t *table) noexcept {
343 auto index = table->first_index();
344
345 while ((index = index->next()) != nullptr) {
346 ut_ad(!index->is_clustered());
347
348 switch (dict_index_get_online_status(index)) {
349 case ONLINE_INDEX_ABORTED_DROPPED:
350 continue;
351
352 case ONLINE_INDEX_COMPLETE:
353 if (index->is_committed()) {
354 /* Do nothing to already published indexes. */
355 } else if (index->type & DICT_FTS) {
356 /* Drop a completed FULLTEXT index, due to a timeout during
357 MDL upgrade for commit_inplace_alter_table(). Because only
358 concurrent reads are allowed (and they are not seeing this
359 index yet) we are safe to drop the index. */
360 auto prev = UT_LIST_GET_PREV(indexes, index);
361
362 /* At least there should be the clustered index before this one. */
363 ut_ad(prev != nullptr);
364 ut_a(table->fts);
365
366 fts_drop_index(table, index, trx, nullptr);
367
368 /* Since INNOBASE_SHARE::idx_trans_tbl is shared between all open
369 ha_innobase handles to this table, no thread should be accessing
370 this dict_index_t object. Also, we should be holding LOCK=SHARED
371 MDL on the table even after the MDL upgrade timeout. */
372
373 /* We can remove a DICT_FTS index from the cache, because we do
374 not allow ADD FULLTEXT INDEX with LOCK=NONE. If we allowed that,
375 we should exclude FTS entries from prebuilt->ins_node->entry_list
376 in ins_node_create_entry_list(). */
377 dict_index_remove_from_cache(table, index);
378
379 index = prev;
380
381 } else {
382 rw_lock_x_lock(dict_index_get_lock(index), UT_LOCATION_HERE);
383
384 dict_index_set_online_status(index, ONLINE_INDEX_ABORTED);
385
386 index->type |= DICT_CORRUPT;
387
388 table->drop_aborted = true;
389
390 rw_lock_x_unlock(dict_index_get_lock(index));
391
392 ut_a(table == index->table);
393
394 ddl::index_build_failed(index);
395 }
396 break;
397
398 case ONLINE_INDEX_CREATION:
399 rw_lock_x_lock(dict_index_get_lock(index), UT_LOCATION_HERE);
400 ut_ad(!index->is_committed());
401 row_log_abort_sec(index);
402 rw_lock_x_unlock(dict_index_get_lock(index));
403
404 [[fallthrough]];
405
406 case ONLINE_INDEX_ABORTED:
407 ut_a(table == index->table);
408 ddl::index_build_failed(index);
409 break;
410
411 default:
412 ut_error;
413 }
414 }
415 }
416
417 /** Invalidate all row_prebuilt_t::ins_graph that are referring
418 to this table. That is, force row_get_prebuilt_insert_row() to
419 rebuild prebuilt->ins_node->entry_list).
420 @param[in,out] trx Transaction
421 @param[in] table Table that owns the indexes. */
422 static void drop_secondary_indexes(trx_t *trx, dict_table_t *table) noexcept {
423 ut_ad(table->def_trx_id <= trx->id);
424
425 table->def_trx_id = trx->id;
426
427 auto index = table->first_index();
428 auto next_index = index->next();
429
430 while ((index = next_index) != nullptr) {
431 /* read the next pointer before freeing the index */
432 next_index = index->next();
433
434 ut_ad(!index->is_clustered());
435
436 if (!index->is_committed()) {
437 /* For FTS index, drop from table->fts along with its aux tables */
438 if (index->type & DICT_FTS) {
439 ut_a(table->fts);
440 fts_drop_index(table, index, trx, nullptr);
441 }
442
443 switch (dict_index_get_online_status(index)) {
444 case ONLINE_INDEX_CREATION:
445 /* This state should only be possible when
446 prepare_inplace_alter_table() fails after invoking
447 ddl::create_index(). In inplace_alter_table(),
448 ddl::Context::cleanup() should never leave the index in
449 this state. It would invoke row_log_abort_sec() on failure. */
450 case ONLINE_INDEX_COMPLETE:
451 /* In these cases, we are able to drop the index
452 straight. The DROP INDEX was never deferred. */
453 break;
454 case ONLINE_INDEX_ABORTED:
455 case ONLINE_INDEX_ABORTED_DROPPED:
456 break;
457 }
458
459 dict_index_remove_from_cache(table, index);
460 }
461 }
462
463 table->drop_aborted = false;
464
465 ut_d(dict_table_check_for_dup_indexes(table, CHECK_ALL_COMPLETE));
466 }
467
468 void drop_indexes(trx_t *trx, dict_table_t *table, bool locked) noexcept {
469 ut_ad(!srv_read_only_mode);
470 ut_ad(dict_sys_mutex_own());
471 ut_ad(trx->dict_operation_lock_mode == RW_X_LATCH);
472 ut_ad(rw_lock_own(dict_operation_lock, RW_LOCK_X));
473
474 ut_d(auto index = table->first_index());
475 ut_ad(index->is_clustered());
476 ut_ad(dict_index_get_online_status(index) == ONLINE_INDEX_COMPLETE);
477
478 /* the caller should have an open handle to the table */
479 ut_ad(table->get_ref_count() >= 1);
480
481 /* It is possible that table->n_ref_count > 1 when
482 locked=true. In this case, all code that should have an open
483 handle to the table be waiting for the next statement to execute,
484 or waiting for a meta-data lock.
485
486 A concurrent purge will be prevented by dict_operation_lock. */
487
488 if (!locked && table->get_ref_count() > 1) {
489 /* Mark the secondary indexes as aborted/offline. */
490 mark_secondary_indexes(trx, table);
491 } else {
492 drop_secondary_indexes(trx, table);
493 }
494 }
495
496 dberr_t Row::build(ddl::Context &ctx, dict_index_t *index, mem_heap_t *heap,
497 size_t type) noexcept {
498 ut_ad(rec_offs_any_null_extern(index, m_rec, m_offsets) == nullptr);
499
500 /* Build a row based on the clustered index. */
501
502 m_ptr = row_build_w_add_vcol(type, index, m_rec, m_offsets, ctx.m_new_table,
503 m_add_cols, ctx.m_add_v, ctx.m_col_map, &m_ext,
504 heap);
505
506 if (!ctx.check_null_constraints(m_ptr)) {
507 ctx.m_trx->error_key_num = SERVER_CLUSTER_INDEX_ID;
508 return DB_INVALID_NULL;
509 }
510
511 auto &fts = ctx.m_fts;
512
513 if (fts.m_doc_id != nullptr && fts.m_doc_id->is_generated()) {
514 fts.m_doc_id->increment();
515 }
516
517 if (ctx.m_add_autoinc != ULINT_UNDEFINED) {
518 auto err = ctx.handle_autoinc(m_ptr, heap);
519
520 if (err != DB_SUCCESS) {
521 return err;
522 }
523 }
524
525 return DB_SUCCESS;
526 }
527
528 dberr_t Cursor::finish(dberr_t err) noexcept {
529 if (m_ctx.m_fts.m_ptr != nullptr) {
530 /* Wait for the FTS parser threads to complete and prepare to insert. */
531 return m_ctx.m_fts.m_ptr->scan_finished(err);
532 } else {
533 return err;
534 }
535 }
536
537 } // namespace ddl