forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler0alter.cc
9461 lines (8015 loc) · 256 KB
/
handler0alter.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 2005, 2023, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file handler/handler0alter.cc
Smart ALTER TABLE
*******************************************************/
/* Include necessary SQL headers */
#include "ha_prototypes.h"
#include <debug_sync.h>
#include <log.h>
#include <sql_lex.h>
#include <sql_class.h>
#include <sql_table.h>
#include <mysql/plugin.h>
/* Include necessary InnoDB headers */
#include "btr0sea.h"
#include "dict0crea.h"
#include "dict0dict.h"
#include "dict0priv.h"
#include "dict0stats.h"
#include "dict0stats_bg.h"
#include "fsp0sysspace.h"
#include "log0log.h"
#include "rem0types.h"
#include "row0log.h"
#include "row0merge.h"
#include "trx0trx.h"
#include "trx0roll.h"
#include "handler0alter.h"
#include "srv0mon.h"
#include "fts0priv.h"
#include "fts0plugin.h"
#include "pars0pars.h"
#include "row0sel.h"
#include "ha_innodb.h"
#include "ut0new.h"
#include "ut0stage.h"
/* For supporting Native InnoDB Partitioning. */
#include "partition_info.h"
#include "ha_innopart.h"
/** Operations for creating secondary indexes (no rebuild needed) */
static const Alter_inplace_info::HA_ALTER_FLAGS INNOBASE_ONLINE_CREATE
= Alter_inplace_info::ADD_INDEX
| Alter_inplace_info::ADD_UNIQUE_INDEX
| Alter_inplace_info::ADD_SPATIAL_INDEX;
/** Operations for rebuilding a table in place */
static const Alter_inplace_info::HA_ALTER_FLAGS INNOBASE_ALTER_REBUILD
= Alter_inplace_info::ADD_PK_INDEX
| Alter_inplace_info::DROP_PK_INDEX
| Alter_inplace_info::CHANGE_CREATE_OPTION
/* CHANGE_CREATE_OPTION needs to check innobase_need_rebuild() */
| Alter_inplace_info::ALTER_COLUMN_NULLABLE
| Alter_inplace_info::ALTER_COLUMN_NOT_NULLABLE
| Alter_inplace_info::ALTER_STORED_COLUMN_ORDER
| Alter_inplace_info::DROP_STORED_COLUMN
| Alter_inplace_info::ADD_STORED_BASE_COLUMN
| Alter_inplace_info::RECREATE_TABLE
/*
| Alter_inplace_info::ALTER_STORED_COLUMN_TYPE
*/
;
/** Operations that require changes to data */
static const Alter_inplace_info::HA_ALTER_FLAGS INNOBASE_ALTER_DATA
= INNOBASE_ONLINE_CREATE | INNOBASE_ALTER_REBUILD;
/** Operations for altering a table that InnoDB does not care about */
static const Alter_inplace_info::HA_ALTER_FLAGS INNOBASE_INPLACE_IGNORE
= Alter_inplace_info::ALTER_COLUMN_DEFAULT
| Alter_inplace_info::ALTER_COLUMN_COLUMN_FORMAT
| Alter_inplace_info::ALTER_COLUMN_STORAGE_TYPE
| Alter_inplace_info::ALTER_VIRTUAL_GCOL_EXPR
| Alter_inplace_info::ALTER_RENAME;
/** Operations on foreign key definitions (changing the schema only) */
static const Alter_inplace_info::HA_ALTER_FLAGS INNOBASE_FOREIGN_OPERATIONS
= Alter_inplace_info::DROP_FOREIGN_KEY
| Alter_inplace_info::ADD_FOREIGN_KEY;
/** Operations that InnoDB cares about and can perform without rebuild */
static const Alter_inplace_info::HA_ALTER_FLAGS INNOBASE_ALTER_NOREBUILD
= INNOBASE_ONLINE_CREATE
| INNOBASE_FOREIGN_OPERATIONS
| Alter_inplace_info::DROP_INDEX
| Alter_inplace_info::DROP_UNIQUE_INDEX
| Alter_inplace_info::RENAME_INDEX
| Alter_inplace_info::ALTER_COLUMN_NAME
| Alter_inplace_info::ALTER_COLUMN_EQUAL_PACK_LENGTH
| Alter_inplace_info::ALTER_INDEX_COMMENT
| Alter_inplace_info::ADD_VIRTUAL_COLUMN
| Alter_inplace_info::DROP_VIRTUAL_COLUMN
| Alter_inplace_info::ALTER_VIRTUAL_COLUMN_ORDER
| Alter_inplace_info::ALTER_COLUMN_INDEX_LENGTH;
/* | Alter_inplace_info::ALTER_VIRTUAL_COLUMN_TYPE; */
struct ha_innobase_inplace_ctx : public inplace_alter_handler_ctx
{
/** Dummy query graph */
que_thr_t* thr;
/** The prebuilt struct of the creating instance */
row_prebuilt_t*& prebuilt;
/** InnoDB indexes being created */
dict_index_t** add_index;
/** MySQL key numbers for the InnoDB indexes that are being created */
const ulint* add_key_numbers;
/** number of InnoDB indexes being created */
ulint num_to_add_index;
/** InnoDB indexes being dropped */
dict_index_t** drop_index;
/** number of InnoDB indexes being dropped */
const ulint num_to_drop_index;
/** InnoDB indexes being renamed */
dict_index_t** rename;
/** number of InnoDB indexes being renamed */
const ulint num_to_rename;
/** InnoDB foreign key constraints being dropped */
dict_foreign_t** drop_fk;
/** number of InnoDB foreign key constraints being dropped */
const ulint num_to_drop_fk;
/** InnoDB foreign key constraints being added */
dict_foreign_t** add_fk;
/** number of InnoDB foreign key constraints being dropped */
const ulint num_to_add_fk;
/** whether to create the indexes online */
bool online;
/** memory heap */
mem_heap_t* heap;
/** dictionary transaction */
trx_t* trx;
/** original table (if rebuilt, differs from indexed_table) */
dict_table_t* old_table;
/** table where the indexes are being created or dropped */
dict_table_t* new_table;
/** mapping of old column numbers to new ones, or NULL */
const ulint* col_map;
/** new column names, or NULL if nothing was renamed */
const char** col_names;
/** added AUTO_INCREMENT column position, or ULINT_UNDEFINED */
const ulint add_autoinc;
/** default values of ADD COLUMN, or NULL */
const dtuple_t* add_cols;
/** autoinc sequence to use */
ib_sequence_t sequence;
/** maximum auto-increment value */
ulonglong max_autoinc;
/** temporary table name to use for old table when renaming tables */
const char* tmp_name;
/** whether the order of the clustered index is unchanged */
bool skip_pk_sort;
/** number of virtual columns to be added */
ulint num_to_add_vcol;
/** virtual columns to be added */
dict_v_col_t* add_vcol;
const char** add_vcol_name;
/** number of virtual columns to be dropped */
ulint num_to_drop_vcol;
/** virtual columns to be dropped */
dict_v_col_t* drop_vcol;
const char** drop_vcol_name;
/** ALTER TABLE stage progress recorder */
ut_stage_alter_t* m_stage;
ha_innobase_inplace_ctx(row_prebuilt_t*& prebuilt_arg,
dict_index_t** drop_arg,
ulint num_to_drop_arg,
dict_index_t** rename_arg,
ulint num_to_rename_arg,
dict_foreign_t** drop_fk_arg,
ulint num_to_drop_fk_arg,
dict_foreign_t** add_fk_arg,
ulint num_to_add_fk_arg,
bool online_arg,
mem_heap_t* heap_arg,
dict_table_t* new_table_arg,
const char** col_names_arg,
ulint add_autoinc_arg,
ulonglong autoinc_col_min_value_arg,
ulonglong autoinc_col_max_value_arg,
ulint num_to_drop_vcol_arg) :
inplace_alter_handler_ctx(),
prebuilt (prebuilt_arg),
add_index (0), add_key_numbers (0), num_to_add_index (0),
drop_index (drop_arg), num_to_drop_index (num_to_drop_arg),
rename (rename_arg), num_to_rename (num_to_rename_arg),
drop_fk (drop_fk_arg), num_to_drop_fk (num_to_drop_fk_arg),
add_fk (add_fk_arg), num_to_add_fk (num_to_add_fk_arg),
online (online_arg), heap (heap_arg), trx (0),
old_table (prebuilt_arg->table),
new_table (new_table_arg),
col_map (0), col_names (col_names_arg),
add_autoinc (add_autoinc_arg),
add_cols (0),
sequence(prebuilt->trx->mysql_thd,
autoinc_col_min_value_arg, autoinc_col_max_value_arg),
max_autoinc (0),
tmp_name (0),
skip_pk_sort(false),
num_to_add_vcol(0),
add_vcol(0),
add_vcol_name(0),
num_to_drop_vcol(0),
drop_vcol(0),
drop_vcol_name(0),
m_stage(NULL)
{
#ifdef UNIV_DEBUG
for (ulint i = 0; i < num_to_add_index; i++) {
ut_ad(!add_index[i]->to_be_dropped);
}
for (ulint i = 0; i < num_to_drop_index; i++) {
ut_ad(drop_index[i]->to_be_dropped);
}
#endif /* UNIV_DEBUG */
thr = pars_complete_graph_for_exec(NULL, prebuilt->trx, heap,
prebuilt);
}
~ha_innobase_inplace_ctx()
{
UT_DELETE(m_stage);
mem_heap_free(heap);
}
/** Determine if the table will be rebuilt.
@return whether the table will be rebuilt */
bool need_rebuild () const { return(old_table != new_table); }
/** Set shared data between the passed in handler context
and current context.
@param[in] inplace_alter_handler_ctx handler context */
void set_shared_data(
const inplace_alter_handler_ctx *ctx)
{
ut_ad(ctx != NULL);
if (this->add_autoinc == ULINT_UNDEFINED) {
return;
}
const ha_innobase_inplace_ctx* ha_ctx =
static_cast<const ha_innobase_inplace_ctx*> (ctx);
/* In InnoDB table, if it's adding AUTOINC column,
the sequence value should be shared among contexts.*/
ut_ad(ha_ctx->add_autoinc != ULINT_UNDEFINED);
this->sequence = ha_ctx->sequence;
}
private:
// Disable copying
ha_innobase_inplace_ctx(const ha_innobase_inplace_ctx&);
ha_innobase_inplace_ctx& operator=(const ha_innobase_inplace_ctx&);
};
/* Report an InnoDB error to the client by invoking my_error(). */
static UNIV_COLD
void
my_error_innodb(
/*============*/
dberr_t error, /*!< in: InnoDB error code */
const char* table, /*!< in: table name */
ulint flags) /*!< in: table flags */
{
switch (error) {
case DB_MISSING_HISTORY:
my_error(ER_TABLE_DEF_CHANGED, MYF(0));
break;
case DB_RECORD_NOT_FOUND:
my_error(ER_KEY_NOT_FOUND, MYF(0), table);
break;
case DB_DEADLOCK:
my_error(ER_LOCK_DEADLOCK, MYF(0));
break;
case DB_LOCK_WAIT_TIMEOUT:
my_error(ER_LOCK_WAIT_TIMEOUT, MYF(0));
break;
case DB_INTERRUPTED:
my_error(ER_QUERY_INTERRUPTED, MYF(0));
break;
case DB_OUT_OF_MEMORY:
my_error(ER_OUT_OF_RESOURCES, MYF(0));
break;
case DB_OUT_OF_FILE_SPACE:
my_error(ER_RECORD_FILE_FULL, MYF(0), table);
break;
case DB_TEMP_FILE_WRITE_FAIL:
my_error(ER_TEMP_FILE_WRITE_FAILURE, MYF(0));
break;
case DB_TOO_BIG_INDEX_COL:
my_error(ER_INDEX_COLUMN_TOO_LONG, MYF(0),
DICT_MAX_FIELD_LEN_BY_FORMAT_FLAG(flags));
break;
case DB_TOO_MANY_CONCURRENT_TRXS:
my_error(ER_TOO_MANY_CONCURRENT_TRXS, MYF(0));
break;
case DB_LOCK_TABLE_FULL:
my_error(ER_LOCK_TABLE_FULL, MYF(0));
break;
case DB_UNDO_RECORD_TOO_BIG:
my_error(ER_UNDO_RECORD_TOO_BIG, MYF(0));
break;
case DB_CORRUPTION:
my_error(ER_NOT_KEYFILE, MYF(0), table);
break;
case DB_TOO_BIG_RECORD:
/* We limit max record size to 16k for 64k page size. */
my_error(ER_TOO_BIG_ROWSIZE, MYF(0),
srv_page_size == UNIV_PAGE_SIZE_MAX
? REC_MAX_DATA_SIZE - 1
: page_get_free_space_of_empty(
flags & DICT_TF_COMPACT) / 2);
break;
case DB_INVALID_NULL:
/* TODO: report the row, as we do for DB_DUPLICATE_KEY */
my_error(ER_INVALID_USE_OF_NULL, MYF(0));
break;
case DB_CANT_CREATE_GEOMETRY_OBJECT:
my_error(ER_CANT_CREATE_GEOMETRY_OBJECT, MYF(0));
break;
case DB_TABLESPACE_EXISTS:
my_error(ER_TABLESPACE_EXISTS, MYF(0), table);
break;
#ifdef UNIV_DEBUG
case DB_SUCCESS:
case DB_DUPLICATE_KEY:
case DB_ONLINE_LOG_TOO_BIG:
/* These codes should not be passed here. */
ut_error;
#endif /* UNIV_DEBUG */
default:
my_error(ER_GET_ERRNO, MYF(0), error);
break;
}
}
/** Determine if fulltext indexes exist in a given table.
@param table MySQL table
@return whether fulltext indexes exist on the table */
static
bool
innobase_fulltext_exist(
/*====================*/
const TABLE* table)
{
for (uint i = 0; i < table->s->keys; i++) {
if (table->key_info[i].flags & HA_FULLTEXT) {
return(true);
}
}
return(false);
}
/** Determine if spatial indexes exist in a given table.
@param table MySQL table
@return whether spatial indexes exist on the table */
static
bool
innobase_spatial_exist(
/*===================*/
const TABLE* table)
{
for (uint i = 0; i < table->s->keys; i++) {
if (table->key_info[i].flags & HA_SPATIAL) {
return(true);
}
}
return(false);
}
/*******************************************************************//**
Determine if ALTER TABLE needs to rebuild the table.
@param ha_alter_info the DDL operation
@return whether it is necessary to rebuild the table */
static MY_ATTRIBUTE((warn_unused_result))
bool
innobase_need_rebuild(
/*==================*/
const Alter_inplace_info* ha_alter_info)
{
Alter_inplace_info::HA_ALTER_FLAGS alter_inplace_flags =
ha_alter_info->handler_flags & ~(INNOBASE_INPLACE_IGNORE);
if (alter_inplace_flags
== Alter_inplace_info::CHANGE_CREATE_OPTION
&& !(ha_alter_info->create_info->used_fields
& (HA_CREATE_USED_ROW_FORMAT
| HA_CREATE_USED_KEY_BLOCK_SIZE
| HA_CREATE_USED_TABLESPACE))) {
/* Any other CHANGE_CREATE_OPTION than changing
ROW_FORMAT, KEY_BLOCK_SIZE or TABLESPACE can be done
without rebuilding the table. */
return(false);
}
return(!!(ha_alter_info->handler_flags & INNOBASE_ALTER_REBUILD));
}
/** Check if virtual column in old and new table are in order, excluding
those dropped column. This is needed because when we drop a virtual column,
ALTER_VIRTUAL_COLUMN_ORDER is also turned on, so we can't decide if this
is a real ORDER change or just DROP COLUMN
@param[in] table old TABLE
@param[in] altered_table new TABLE
@param[in] ha_alter_info Structure describing changes to be done
by ALTER TABLE and holding data used during in-place alter.
@return true is all columns in order, false otherwise. */
static
bool
check_v_col_in_order(
const TABLE* table,
const TABLE* altered_table,
Alter_inplace_info* ha_alter_info)
{
ulint j = 0;
/* We don't support any adding new virtual column before
existed virtual column. */
if (ha_alter_info->handler_flags
& Alter_inplace_info::ADD_VIRTUAL_COLUMN) {
bool has_new = false;
List_iterator_fast<Create_field> cf_it(
ha_alter_info->alter_info->create_list);
cf_it.rewind();
while (const Create_field* new_field = cf_it++) {
if (!new_field->is_virtual_gcol()) {
continue;
}
/* Found a new added virtual column. */
if (!new_field->field) {
has_new = true;
continue;
}
/* If there's any old virtual column
after the new added virtual column,
order must be changed. */
if (has_new) {
return(false);
}
}
}
/* directly return true if ALTER_VIRTUAL_COLUMN_ORDER is not on */
if (!(ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_VIRTUAL_COLUMN_ORDER)) {
return(true);
}
for (ulint i = 0; i < table->s->fields; i++) {
Field* field = table->s->field[i];
bool dropped = false;
Alter_drop* drop;
if (field->stored_in_db) {
continue;
}
ut_ad(innobase_is_v_fld(field));
/* Check if this column is in drop list */
List_iterator_fast<Alter_drop> cf_it(
ha_alter_info->alter_info->drop_list);
while ((drop = (cf_it++)) != NULL) {
if (my_strcasecmp(system_charset_info,
field->field_name, drop->name) == 0) {
dropped = true;
break;
}
}
if (dropped) {
continue;
}
/* Now check if the next virtual column in altered table
matches this column */
while (j < altered_table->s->fields) {
Field* new_field = altered_table->s->field[j];
if (new_field->stored_in_db) {
j++;
continue;
}
if (my_strcasecmp(system_charset_info,
field->field_name,
new_field->field_name) != 0) {
/* different column */
return(false);
} else {
j++;
break;
}
}
if (j > altered_table->s->fields) {
/* there should not be less column in new table
without them being in drop list */
ut_ad(0);
return(false);
}
}
return(true);
}
/** Check if InnoDB supports a particular alter table in-place
@param altered_table TABLE object for new version of table.
@param ha_alter_info Structure describing changes to be done
by ALTER TABLE and holding data used during in-place alter.
@retval HA_ALTER_INPLACE_NOT_SUPPORTED Not supported
@retval HA_ALTER_INPLACE_NO_LOCK Supported
@retval HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE Supported, but requires
lock during main phase and exclusive lock during prepare phase.
@retval HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE Supported, prepare phase
requires exclusive lock (any transactions that have accessed the table
must commit or roll back first, and no transactions can access the table
while prepare_inplace_alter_table() is executing)
*/
enum_alter_inplace_result
ha_innobase::check_if_supported_inplace_alter(
/*==========================================*/
TABLE* altered_table,
Alter_inplace_info* ha_alter_info)
{
DBUG_ENTER("check_if_supported_inplace_alter");
if (high_level_read_only
|| srv_sys_space.created_new_raw()
|| srv_force_recovery) {
ha_alter_info->unsupported_reason = (srv_force_recovery)?
innobase_get_err_msg(ER_INNODB_FORCED_RECOVERY):
innobase_get_err_msg(ER_READ_ONLY_MODE);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
if (altered_table->s->fields > REC_MAX_N_USER_FIELDS) {
/* Deny the inplace ALTER TABLE. MySQL will try to
re-create the table and ha_innobase::create() will
return an error too. This is how we effectively
deny adding too many columns to a table. */
ha_alter_info->unsupported_reason =
innobase_get_err_msg(ER_TOO_MANY_FIELDS);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
/* We don't support change encryption attribute with
inplace algorithm. */
char* old_encryption = this->table->s->encrypt_type.str;
char* new_encryption = altered_table->s->encrypt_type.str;
if (Encryption::is_none(old_encryption)
!= Encryption::is_none(new_encryption)) {
ha_alter_info->unsupported_reason =
innobase_get_err_msg(
ER_UNSUPPORTED_ALTER_ENCRYPTION_INPLACE);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
update_thd();
trx_search_latch_release_if_reserved(m_prebuilt->trx);
if (ha_alter_info->handler_flags
& ~(INNOBASE_INPLACE_IGNORE
| INNOBASE_ALTER_NOREBUILD
| INNOBASE_ALTER_REBUILD)) {
if (ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_STORED_COLUMN_TYPE) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_COLUMN_TYPE);
}
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
/* Only support online add foreign key constraint when
check_foreigns is turned off */
if ((ha_alter_info->handler_flags & Alter_inplace_info::ADD_FOREIGN_KEY)
&& m_prebuilt->trx->check_foreigns) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_FK_CHECK);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
if (altered_table->file->ht != ht) {
/* Non-native partitioning table engine. No longer supported,
due to implementation of native InnoDB partitioning. */
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
if (!(ha_alter_info->handler_flags & ~INNOBASE_INPLACE_IGNORE)) {
DBUG_RETURN(HA_ALTER_INPLACE_NO_LOCK);
}
/* Only support NULL -> NOT NULL change if strict table sql_mode
is set. Fall back to COPY for conversion if not strict tables.
In-Place will fail with an error when trying to convert
NULL to a NOT NULL value. */
if ((ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_COLUMN_NOT_NULLABLE)
&& !thd_is_strict_mode(m_user_thd)) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_NOT_NULL);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
/* DROP PRIMARY KEY is only allowed in combination with ADD
PRIMARY KEY. */
if ((ha_alter_info->handler_flags
& (Alter_inplace_info::ADD_PK_INDEX
| Alter_inplace_info::DROP_PK_INDEX))
== Alter_inplace_info::DROP_PK_INDEX) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_NOPK);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
/* If a column change from NOT NULL to NULL,
and there's a implict pk on this column. the
table should be rebuild. The change should
only go through the "Copy" method. */
if ((ha_alter_info->handler_flags
& Alter_inplace_info::ALTER_COLUMN_NULLABLE)) {
const uint my_primary_key = altered_table->s->primary_key;
/* See if MYSQL table has no pk but we do. */
if (UNIV_UNLIKELY(my_primary_key >= MAX_KEY)
&& !row_table_got_default_clust_index(m_prebuilt->table)) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_PRIMARY_CANT_HAVE_NULL);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
}
bool add_drop_v_cols = false;
/* If there is add or drop virtual columns, we will support operations
with these 2 options alone with inplace interface for now */
if (ha_alter_info->handler_flags
& (Alter_inplace_info::ADD_VIRTUAL_COLUMN
| Alter_inplace_info::DROP_VIRTUAL_COLUMN
| Alter_inplace_info::ALTER_VIRTUAL_COLUMN_ORDER)) {
ulonglong flags = ha_alter_info->handler_flags;
/* TODO: uncomment the flags below, once we start to
support them */
flags &= ~(Alter_inplace_info::ADD_VIRTUAL_COLUMN
| Alter_inplace_info::DROP_VIRTUAL_COLUMN
| Alter_inplace_info::ALTER_VIRTUAL_COLUMN_ORDER
| Alter_inplace_info::ALTER_VIRTUAL_GCOL_EXPR
/*
| Alter_inplace_info::ALTER_STORED_COLUMN_ORDER
| Alter_inplace_info::ADD_STORED_BASE_COLUMN
| Alter_inplace_info::DROP_STORED_COLUMN
| Alter_inplace_info::ALTER_STORED_COLUMN_ORDER
| Alter_inplace_info::ADD_UNIQUE_INDEX
*/
| Alter_inplace_info::ADD_INDEX
| Alter_inplace_info::DROP_INDEX);
if (flags != 0
|| (altered_table->s->partition_info_str
&& altered_table->s->partition_info_str_len)
|| (!check_v_col_in_order(
this->table, altered_table, ha_alter_info))) {
ha_alter_info->unsupported_reason =
innobase_get_err_msg(
ER_UNSUPPORTED_ALTER_INPLACE_ON_VIRTUAL_COLUMN);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
add_drop_v_cols = true;
}
/* We should be able to do the operation in-place.
See if we can do it online (LOCK=NONE). */
bool online = true;
List_iterator_fast<Create_field> cf_it(
ha_alter_info->alter_info->create_list);
/* Fix the key parts. */
for (KEY* new_key = ha_alter_info->key_info_buffer;
new_key < ha_alter_info->key_info_buffer
+ ha_alter_info->key_count;
new_key++) {
/* Do not support adding/droping a vritual column, while
there is a table rebuild caused by adding a new FTS_DOC_ID */
if ((new_key->flags & HA_FULLTEXT) && add_drop_v_cols
&& !DICT_TF2_FLAG_IS_SET(m_prebuilt->table,
DICT_TF2_FTS_HAS_DOC_ID)) {
ha_alter_info->unsupported_reason =
innobase_get_err_msg(
ER_UNSUPPORTED_ALTER_INPLACE_ON_VIRTUAL_COLUMN);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
for (KEY_PART_INFO* key_part = new_key->key_part;
key_part < new_key->key_part + new_key->user_defined_key_parts;
key_part++) {
const Create_field* new_field;
assert(key_part->fieldnr
< altered_table->s->fields);
cf_it.rewind();
for (uint fieldnr = 0; (new_field = cf_it++);
fieldnr++) {
if (fieldnr == key_part->fieldnr) {
break;
}
}
assert(new_field);
key_part->field = altered_table->field[
key_part->fieldnr];
/* In some special cases InnoDB emits "false"
duplicate key errors with NULL key values. Let
us play safe and ensure that we can correctly
print key values even in such cases. */
key_part->null_offset = key_part->field->null_offset();
key_part->null_bit = key_part->field->null_bit;
if (new_field->field) {
/* This is an existing column. */
continue;
}
/* This is an added column. */
assert(ha_alter_info->handler_flags
& Alter_inplace_info::ADD_COLUMN);
/* We cannot replace a hidden FTS_DOC_ID
with a user-visible FTS_DOC_ID. */
if (m_prebuilt->table->fts
&& innobase_fulltext_exist(altered_table)
&& !my_strcasecmp(
system_charset_info,
key_part->field->field_name,
FTS_DOC_ID_COL_NAME)) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_HIDDEN_FTS);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
assert((MTYP_TYPENR(key_part->field->unireg_check)
== Field::NEXT_NUMBER)
== !!(key_part->field->flags
& AUTO_INCREMENT_FLAG));
if (key_part->field->flags & AUTO_INCREMENT_FLAG) {
/* We cannot assign an AUTO_INCREMENT
column values during online ALTER. */
assert(key_part->field == altered_table
-> found_next_number_field);
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_AUTOINC);
online = false;
}
if (key_part->field->is_virtual_gcol()) {
/* Do not support adding index on newly added
virtual column, while there is also a drop
virtual column in the same clause */
if (ha_alter_info->handler_flags
& Alter_inplace_info::DROP_VIRTUAL_COLUMN) {
ha_alter_info->unsupported_reason =
innobase_get_err_msg(
ER_UNSUPPORTED_ALTER_INPLACE_ON_VIRTUAL_COLUMN);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
ha_alter_info->unsupported_reason =
innobase_get_err_msg(
ER_UNSUPPORTED_ALTER_ONLINE_ON_VIRTUAL_COLUMN);
online = false;
}
}
}
assert(!m_prebuilt->table->fts || m_prebuilt->table->fts->doc_col
<= table->s->fields);
assert(!m_prebuilt->table->fts || m_prebuilt->table->fts->doc_col
< dict_table_get_n_user_cols(m_prebuilt->table));
if (ha_alter_info->handler_flags
& Alter_inplace_info::ADD_SPATIAL_INDEX) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_GIS);
online = false;
}
if (m_prebuilt->table->fts
&& innobase_fulltext_exist(altered_table)) {
/* FULLTEXT indexes are supposed to remain. */
/* Disallow DROP INDEX FTS_DOC_ID_INDEX */
for (uint i = 0; i < ha_alter_info->index_drop_count; i++) {
if (!my_strcasecmp(
system_charset_info,
ha_alter_info->index_drop_buffer[i]->name,
FTS_DOC_ID_INDEX_NAME)) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_CHANGE_FTS);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
}
/* InnoDB can have a hidden FTS_DOC_ID_INDEX on a
visible FTS_DOC_ID column as well. Prevent dropping or
renaming the FTS_DOC_ID. */
for (Field** fp = table->field; *fp; fp++) {
if (!((*fp)->flags
& (FIELD_IS_RENAMED | FIELD_IS_DROPPED))) {
continue;
}
if (!my_strcasecmp(
system_charset_info,
(*fp)->field_name,
FTS_DOC_ID_COL_NAME)) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_CHANGE_FTS);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
}
}
m_prebuilt->trx->will_lock++;
if (!online) {
/* We already determined that only a non-locking
operation is possible. */
} else if (((ha_alter_info->handler_flags
& Alter_inplace_info::ADD_PK_INDEX)
|| innobase_need_rebuild(ha_alter_info))
&& (innobase_fulltext_exist(altered_table)
|| innobase_spatial_exist(altered_table))) {
/* Refuse to rebuild the table online, if
FULLTEXT OR SPATIAL indexes are to survive the rebuild. */
online = false;
/* If the table already contains fulltext indexes,
refuse to rebuild the table natively altogether. */
if (m_prebuilt->table->fts) {
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_INNODB_FT_LIMIT);
DBUG_RETURN(HA_ALTER_INPLACE_NOT_SUPPORTED);
}
if (innobase_spatial_exist(altered_table)) {
ha_alter_info->unsupported_reason =
innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_GIS);
} else {
ha_alter_info->unsupported_reason =
innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_FTS);
}
} else if ((ha_alter_info->handler_flags
& Alter_inplace_info::ADD_INDEX)) {
/* Building a full-text index requires a lock.
We could do without a lock if the table already contains
an FTS_DOC_ID column, but in that case we would have
to apply the modification log to the full-text indexes. */
for (uint i = 0; i < ha_alter_info->index_add_count; i++) {
const KEY* key =
&ha_alter_info->key_info_buffer[
ha_alter_info->index_add_buffer[i]];
if (key->flags & HA_FULLTEXT) {
assert(!(key->flags & HA_KEYFLAG_MASK
& ~(HA_FULLTEXT
| HA_PACK_KEY
| HA_GENERATED_KEY
| HA_BINARY_PACK_KEY)));
ha_alter_info->unsupported_reason = innobase_get_err_msg(
ER_ALTER_OPERATION_NOT_SUPPORTED_REASON_FTS);
online = false;
break;
}
}
}
DBUG_RETURN(online
? HA_ALTER_INPLACE_NO_LOCK_AFTER_PREPARE
: HA_ALTER_INPLACE_SHARED_LOCK_AFTER_PREPARE);
}
/*************************************************************//**
Initialize the dict_foreign_t structure with supplied info
@return true if added, false if duplicate foreign->id */
static
bool
innobase_init_foreign(
/*==================*/
dict_foreign_t* foreign, /*!< in/out: structure to
initialize */
char* constraint_name, /*!< in/out: constraint name if
exists */
dict_table_t* table, /*!< in: foreign table */
dict_index_t* index, /*!< in: foreign key index */
const char** column_names, /*!< in: foreign key column
names */
ulint num_field, /*!< in: number of columns */
const char* referenced_table_name, /*!< in: referenced table
name */
dict_table_t* referenced_table, /*!< in: referenced table */
dict_index_t* referenced_index, /*!< in: referenced index */
const char** referenced_column_names,/*!< in: referenced column
names */
ulint referenced_num_field) /*!< in: number of referenced
columns */
{
ut_ad(mutex_own(&dict_sys->mutex));
if (constraint_name) {
ulint db_len;
/* Catenate 'databasename/' to the constraint name specified
by the user: we conceive the constraint as belonging to the
same MySQL 'database' as the table itself. We store the name
to foreign->id. */
db_len = dict_get_db_name_len(table->name.m_name);
foreign->id = static_cast<char*>(mem_heap_alloc(
foreign->heap, db_len + strlen(constraint_name) + 2));
ut_memcpy(foreign->id, table->name.m_name, db_len);
foreign->id[db_len] = '/';
strcpy(foreign->id + db_len + 1, constraint_name);
/* Check if any existing foreign key has the same id,
this is needed only if user supplies the constraint name */
if (table->foreign_set.find(foreign)
!= table->foreign_set.end()) {
return(false);
}
}
foreign->foreign_table = table;
foreign->foreign_table_name = mem_heap_strdup(
foreign->heap, table->name.m_name);
dict_mem_foreign_table_name_lookup_set(foreign, TRUE);
foreign->foreign_index = index;
foreign->n_fields = (unsigned int) num_field;
foreign->foreign_col_names = static_cast<const char**>(
mem_heap_alloc(foreign->heap, num_field * sizeof(void*)));
for (ulint i = 0; i < foreign->n_fields; i++) {
foreign->foreign_col_names[i] = mem_heap_strdup(
foreign->heap, column_names[i]);
}