Debian Codegraph @

storage/innobase/dict/dict0inst.cc

raw
1 /*****************************************************************************
2 Copyright (c) 2020, 2026, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License, version 2.0, as published by the
6 Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25 *****************************************************************************/
26
27 /** @file dict/dict0inst.cc
28 Instant DDL interface implementation
29
30 Created 2020-04-24 by Mayank Prasad */
31
32 #include "dict0inst.h"
33 #include "dict0dd.h"
34 #include "dict0dict.h"
35 #include "ha_innodb.h"
36
37 static void populate_to_be_instant_columns_low(
38 const Alter_inplace_info *ha_alter_info, const TABLE *old_table,
39 const TABLE *new_table, Columns &cols_to_add, Columns &cols_to_drop);
40
41 template <typename Table>
42 bool Instant_ddl_impl<Table>::is_instant_add_drop_possible(
43 const Alter_inplace_info *ha_alter_info, const TABLE *table,
44 const TABLE *altered_table, const dict_table_t *dict_table) {
45 Columns cols_to_add;
46 Columns cols_to_drop;
47 populate_to_be_instant_columns_low(ha_alter_info, table, altered_table,
48 cols_to_add, cols_to_drop);
49
50 if (cols_to_add.empty() && cols_to_drop.empty()) {
51 ut_ad(0);
52 return true;
53 }
54
55 const dict_index_t *index = dict_table->first_index();
56
57 /* Get maximum permissible size on page */
58 size_t page_rec_max;
59 size_t page_ptr_max;
60 get_permissible_max_size(dict_table, index, page_rec_max, page_ptr_max);
61
62 /* Get the maximum size of a valid rec in current table */
63 size_t current_max_size;
64 const bool res = dict_index_validate_max_rec_size(
65 dict_table, index, page_rec_max, page_ptr_max, current_max_size);
66
67 if (res) {
68 /* Table is already in a state where possible row size can go beyond
69 permissible size limit. Don't allow INSTANT ADD */
70 ib::error(ER_IB_INSTANT_ADD_DROP_COLUMN_NOT_ALLOWED,
71 dict_table->name.m_name, current_max_size, page_rec_max);
72 return false;
73 }
74
75 for (auto field : cols_to_add) {
76 if (innobase_is_v_fld(field)) {
77 continue;
78 }
79
80 /* Get the maximum possible size needed for this field */
81 size_t field_max_size = 0;
82 {
83 unsigned col_len;
84 ulint mtype;
85 ulint prtype;
86 get_field_types(nullptr, dict_table, field, col_len, mtype, prtype);
87
88 /* Create a dummy dict_cot_t and dict_field_t just to calculate size */
89 dict_col_t dummy_col;
90 dummy_col.mtype = (unsigned int)mtype;
91 dummy_col.prtype = (unsigned int)prtype;
92 dummy_col.len = (unsigned int)col_len;
93 ulint mbminlen;
94 ulint mbmaxlen;
95 dtype_get_mblen(mtype, prtype, &mbminlen, &mbmaxlen);
96 dummy_col.set_mbminmaxlen(mbminlen, mbmaxlen);
97 dict_field_t dummy_field;
98 dummy_field.col = &dummy_col;
99 get_field_max_size(dict_table, index, &dummy_field, field_max_size);
100 }
101
102 current_max_size += field_max_size;
103 if (current_max_size > page_rec_max) {
104 /* Don't allow INSTANT ADD */
105 ib::error(ER_IB_INSTANT_ADD_NOT_POSSIBLE, cols_to_add.size(),
106 dict_table->name.m_name, current_max_size, page_rec_max);
107 return false;
108 }
109 }
110
111 return true;
112 }
113
114 template bool Instant_ddl_impl<dd::Table>::is_instant_add_drop_possible(
115 const Alter_inplace_info *ha_alter_info, const TABLE *table,
116 const TABLE *altered_table, const dict_table_t *dict_table);
117
118 template bool Instant_ddl_impl<dd::Partition>::is_instant_add_drop_possible(
119 const Alter_inplace_info *ha_alter_info, const TABLE *table,
120 const TABLE *altered_table, const dict_table_t *dict_table);
121
122 template <typename Table>
123 bool Instant_ddl_impl<Table>::commit_instant_add_col_low() {
124 ut_ad(!m_dict_table->is_temporary());
125
126 ut_a(is_instant_add_drop_possible(m_ha_alter_info, m_old_table,
127 m_altered_table, m_dict_table));
128
129 /* To remember old default values if exist */
130 dd_copy_table_columns(m_ha_alter_info, m_new_dd_tab->table(),
131 m_old_dd_tab->table(), m_dict_table);
132
133 /* Then add all new default values */
134 if (dd_add_instant_columns(&m_old_dd_tab->table(), &m_new_dd_tab->table(),
135 m_dict_table, m_cols_to_add))
136 return true;
137
138 /* Keep the metadata for newly added virtual columns if exist */
139 dd_update_v_cols(&m_new_dd_tab->table(), m_dict_table->id);
140
141 return false;
142 }
143
144 template bool Instant_ddl_impl<dd::Table>::commit_instant_add_col_low();
145 template bool Instant_ddl_impl<dd::Partition>::commit_instant_add_col_low();
146
147 template <>
148 bool Instant_ddl_impl<dd::Table>::commit_instant_add_col() {
149 return commit_instant_add_col_low();
150 }
151
152 template <>
153 bool Instant_ddl_impl<dd::Partition>::commit_instant_add_col() {
154 if (dd_part_is_first(m_new_dd_tab)) {
155 if (commit_instant_add_col_low()) return true;
156 }
157 return false;
158 }
159
160 template <typename Table>
161 bool Instant_ddl_impl<Table>::commit_instant_drop_col_low() {
162 ut_ad(!m_dict_table->is_temporary());
163
164 ut_a(is_instant_add_drop_possible(m_ha_alter_info, m_old_table,
165 m_altered_table, m_dict_table));
166
167 /* Copy columns metadata */
168 dd_copy_table_columns(m_ha_alter_info, m_new_dd_tab->table(),
169 m_old_dd_tab->table(), m_dict_table);
170
171 /* Update metadata of columns to be dropped */
172 return dd_drop_instant_columns(&m_old_dd_tab->table(), &m_new_dd_tab->table(),
173 m_dict_table, m_cols_to_drop
174 #ifdef UNIV_DEBUG
175 ,
176 m_cols_to_add, m_ha_alter_info
177 #endif
178 );
179 }
180
181 template bool Instant_ddl_impl<dd::Table>::commit_instant_drop_col_low();
182 template bool Instant_ddl_impl<dd::Partition>::commit_instant_drop_col_low();
183
184 template <>
185 bool Instant_ddl_impl<dd::Table>::commit_instant_drop_col() {
186 return commit_instant_drop_col_low();
187 }
188
189 template <>
190 bool Instant_ddl_impl<dd::Partition>::commit_instant_drop_col() {
191 if (dd_part_is_first(m_new_dd_tab)) {
192 if (commit_instant_drop_col_low()) return true;
193 }
194 return false;
195 }
196
197 template <typename Table>
198 bool Instant_ddl_impl<Table>::commit_instant_ddl() {
199 Instant_Type type =
200 static_cast<Instant_Type>(m_ha_alter_info->handler_trivial_ctx);
201
202 switch (type) {
203 case Instant_Type::INSTANT_NO_CHANGE:
204 dd_commit_inplace_no_change(false);
205 break;
206 case Instant_Type::INSTANT_COLUMN_RENAME:
207 dd_commit_inplace_no_change(false);
208
209 if (!dd_table_is_partitioned(m_new_dd_tab->table()) ||
210 dd_part_is_first(reinterpret_cast<dd::Partition *>(m_new_dd_tab))) {
211 dd_update_v_cols(&m_new_dd_tab->table(), m_dict_table->id);
212 }
213
214 row_mysql_lock_data_dictionary(m_trx, UT_LOCATION_HERE);
215 innobase_discard_table(m_thd, m_dict_table);
216 row_mysql_unlock_data_dictionary(m_trx);
217 break;
218 case Instant_Type::INSTANT_VIRTUAL_ONLY:
219 if (dd_find_column(&m_old_dd_tab->table(), FTS_DOC_ID_COL_NAME) &&
220 !dd_find_column(&m_new_dd_tab->table(), FTS_DOC_ID_COL_NAME)) {
221 dd::Column *col = dd_add_hidden_column(
222 &m_new_dd_tab->table(), FTS_DOC_ID_COL_NAME, FTS_DOC_ID_LEN,
223 dd::enum_column_types::LONGLONG);
224 dd_set_hidden_unique_index(m_new_dd_tab->table().add_index(),
225 FTS_DOC_ID_INDEX_NAME, col);
226 }
227 dd_commit_inplace_no_change(true);
228
229 if (!dd_table_is_partitioned(m_new_dd_tab->table()) ||
230 dd_part_is_first(reinterpret_cast<dd::Partition *>(m_new_dd_tab))) {
231 dd_update_v_cols(&m_new_dd_tab->table(), m_dict_table->id);
232 }
233
234 row_mysql_lock_data_dictionary(m_trx, UT_LOCATION_HERE);
235 innobase_discard_table(m_thd, m_dict_table);
236 row_mysql_unlock_data_dictionary(m_trx);
237 break;
238 case Instant_Type::INSTANT_ADD_DROP_COLUMN:
239 trx_start_if_not_started(m_trx, true, UT_LOCATION_HERE);
240 dd_copy_private(*m_new_dd_tab, *m_old_dd_tab);
241
242 /* Fetch the columns which are to be added or dropped */
243 populate_to_be_instant_columns();
244
245 ut_ad(!m_cols_to_add.empty() || !m_cols_to_drop.empty());
246
247 if (!m_cols_to_drop.empty()) {
248 /* INSTANT DROP */
249 if (commit_instant_drop_col()) return true;
250 }
251
252 if (!m_cols_to_add.empty()) {
253 /* INSTANT ADD */
254 if (commit_instant_add_col()) return true;
255 }
256
257 /* Update the current row version in dictionary cache */
258 m_dict_table->current_row_version++;
259
260 ut_ad(dd_table_has_instant_cols(m_new_dd_tab->table()));
261
262 for (auto dd_index : *m_new_dd_tab->indexes()) {
263 dd::Properties &p = dd_index->se_private_data();
264 p.set(dd_index_key_strings[DD_INDEX_TRX_ID], m_trx->id);
265 }
266
267 row_mysql_lock_data_dictionary(m_trx, UT_LOCATION_HERE);
268 innobase_discard_table(m_thd, m_dict_table);
269 row_mysql_unlock_data_dictionary(m_trx);
270
271 break;
272 case Instant_Type::INSTANT_IMPOSSIBLE:
273 default:
274 ut_ad(0);
275 }
276
277 if (m_autoinc != nullptr) {
278 ut_ad(m_altered_table->found_next_number_field != nullptr);
279 if (!dd_table_is_partitioned(m_new_dd_tab->table()) ||
280 dd_part_is_first(reinterpret_cast<dd::Partition *>(m_new_dd_tab))) {
281 dd_set_autoinc(m_new_dd_tab->table().se_private_data(), *m_autoinc);
282 }
283 }
284 return false;
285 }
286
287 template bool Instant_ddl_impl<dd::Table>::commit_instant_ddl();
288 template bool Instant_ddl_impl<dd::Partition>::commit_instant_ddl();
289
290 static void populate_to_be_instant_columns_low(
291 const Alter_inplace_info *ha_alter_info, const TABLE *old_table,
292 const TABLE *altered_table, Columns &cols_to_add, Columns &cols_to_drop) {
293 /* Collect all renamed columns */
294 using renamed_fields_t = std::pair<std::string, std::string>;
295 std::vector<renamed_fields_t> renamed_fields;
296 for (size_t i = 0; i < old_table->s->fields; i++) {
297 const char *field_name = old_table->field[i]->field_name;
298 std::string new_name;
299 if (is_renamed(ha_alter_info, field_name, new_name)) {
300 ut_a(!new_name.empty());
301 renamed_fields.push_back(std::pair(field_name, new_name));
302 }
303 }
304
305 for (size_t i = 0; i < old_table->s->fields; i++) {
306 Field *old_table_field = old_table->field[i];
307 const char *old_field_name = old_table_field->field_name;
308
309 /* Skip virtual column from old table */
310 if (innobase_is_v_fld(old_table_field)) {
311 continue;
312 }
313
314 /* Skip if this column is being renamed */
315 auto it = std::find_if(
316 renamed_fields.begin(), renamed_fields.end(),
317 [&old_field_name](renamed_fields_t &element) {
318 return (strcmp(element.first.c_str(), old_field_name) == 0);
319 });
320 if (it != renamed_fields.end()) {
321 continue;
322 }
323
324 /* Look for this column in new table */
325 bool found = false;
326 for (size_t j = 0; j < altered_table->s->fields; j++) {
327 Field *new_table_field = altered_table->field[j];
328
329 /* Skip virtual column from altered table */
330 if (innobase_is_v_fld(new_table_field)) {
331 continue;
332 }
333
334 const char *new_field_name = new_table_field->field_name;
335
336 if (strcmp(old_field_name, new_field_name) == 0) {
337 /* This column is present in both the tables. Stop iteration. */
338
339 /* Check if this column is in drop list of alter_info. */
340 if (is_dropped(ha_alter_info, old_field_name)) {
341 /* This column is being dropped */
342 cols_to_drop.push_back(old_table_field);
343
344 /* But this column is present in new table as well which is possible
345 if a new column with same name is being added or an existing column
346 is being renamed to this name. */
347 auto it = std::find_if(
348 renamed_fields.begin(), renamed_fields.end(),
349 [&new_field_name](renamed_fields_t &element) {
350 return (strcmp(element.second.c_str(), new_field_name) == 0);
351 });
352 if (it == renamed_fields.end()) {
353 /* Not renamed, so must be being added */
354 cols_to_add.push_back(new_table_field);
355 }
356 }
357
358 found = true;
359 break;
360 }
361 }
362
363 /* Could not find this column in new table. So it is being dropped. */
364 if (!found) {
365 cols_to_drop.push_back(old_table_field);
366 }
367 } /* For */
368
369 for (size_t i = 0; i < altered_table->s->fields; i++) {
370 Field *new_table_field = altered_table->field[i];
371
372 /* Skip virtual column from altered table */
373 if (innobase_is_v_fld(new_table_field)) {
374 continue;
375 }
376
377 const char *new_field_name = new_table_field->field_name;
378 /* Skip if it is renamed field */
379 auto it = std::find_if(
380 renamed_fields.begin(), renamed_fields.end(),
381 [&new_field_name](renamed_fields_t &element) {
382 return (strcmp(element.second.c_str(), new_field_name) == 0);
383 });
384 if (it != renamed_fields.end()) {
385 continue;
386 }
387
388 /* Look for this column in old table */
389 bool found = false;
390 for (size_t j = 0; j < old_table->s->fields; j++) {
391 Field *old_table_field = old_table->field[j];
392
393 /* Skip virtual column from old table */
394 if (innobase_is_v_fld(old_table_field)) {
395 continue;
396 }
397
398 const char *old_field_name = old_table_field->field_name;
399
400 /* This column is present in both the tables. Stop iteration. */
401 if (strcmp(old_field_name, new_field_name) == 0) {
402 /* If the old column is renamed, then this new column is being added */
403 auto it = std::find_if(
404 renamed_fields.begin(), renamed_fields.end(),
405 [&old_field_name](renamed_fields_t &element) {
406 return (strcmp(element.first.c_str(), old_field_name) == 0);
407 });
408 if (it != renamed_fields.end()) {
409 cols_to_add.push_back(new_table_field);
410 }
411
412 found = true;
413 break;
414 }
415 }
416
417 /* Could not find this column in old table. So it is being added. */
418 if (!found) {
419 cols_to_add.push_back(new_table_field);
420 }
421 } /* FOR */
422
423 renamed_fields.clear();
424 }
425
426 template <typename Table>
427 void Instant_ddl_impl<Table>::populate_to_be_instant_columns() {
428 populate_to_be_instant_columns_low(m_ha_alter_info, m_old_table,
429 m_altered_table, m_cols_to_add,
430 m_cols_to_drop);
431 }
432
433 template <typename Table>
434 void Instant_ddl_impl<Table>::dd_commit_inplace_no_change(bool ignore_fts) {
435 if (dd_table_has_instant_drop_cols(m_old_dd_tab->table())) {
436 /* Copy dropped columns from old table to new table */
437 copy_dropped_columns(&m_old_dd_tab->table(), &m_new_dd_tab->table(),
438 UINT32_UNDEFINED);
439 }
440
441 if (!ignore_fts) {
442 dd_add_fts_doc_id_index(m_new_dd_tab->table(), m_old_dd_tab->table());
443 }
444
445 dd_copy_private(*m_new_dd_tab, *m_old_dd_tab);
446
447 if (!dd_table_is_partitioned(m_new_dd_tab->table()) ||
448 dd_part_is_first(reinterpret_cast<dd::Partition *>(m_new_dd_tab))) {
449 dd_copy_table(m_ha_alter_info, m_new_dd_tab->table(),
450 m_old_dd_tab->table());
451 }
452 }