mariadb/mysql-test/main/alter_table_online.result
Monty e62dc52420 MDEV-25292 Atomic CREATE OR REPLACE TABLE
Atomic CREATE OR REPLACE allows to keep an old table intact if the
command fails or during the crash. That is done by renaming the
original table to temporary name, as a backup and restoring it if the
CREATE fails. When the command is complete and logged the backup
table is deleted.

Atomic replace algorithm

  Two DDL chains are used for CREATE OR REPLACE:
  ddl_log_state_create (C) and ddl_log_state_rm (D).

  1. (C) Log rename of ORIG to TMP table (Rename TMP to original).
  2. Rename orignal to TMP.
  3. (C) Log CREATE_TABLE_ACTION of ORIG (drops ORIG);
  4. Do everything with ORIG (like insert data)
  5. (D) Log drop of TMP
  6. Write query to binlog (this marks (C) to be closed in
     case of failure)
  7. Execute drop of TMP through (D)
  8. Close (C) and (D)

  If there is a failure before 6) we revert the changes in (C)
  Chain (D) is only executed if 6) succeded (C is closed on
  crash recovery).

Foreign key errors will be found at the 1) stage.

Additional notes

  - CREATE TABLE without REPLACE and temporary tables is not affected
    by this commit.
    set @@drop_before_create_or_replace=1 can be used to
    get old behaviour where existing tables are dropped
    in CREATE OR REPLACE.

  - CREATE TABLE is reverted if binlogging the query fails.

  - Engines having HTON_EXPENSIVE_RENAME flag set are not affected by
    this commit. Conflicting tables marked with this flag will be
    deleted with CREATE OR REPLACE.

  - Replication execution is not affected by this commit.
    - Replication will first drop the conflicting table and then
      creating the new one.

  - CREATE TABLE .. SELECT XID usage is fixed and now there is no need
    to log DROP TABLE via DDL_CREATE_TABLE_PHASE_LOG (see comments in
    do_postlock()). XID is now correctly updated so it disables
    DDL_LOG_DROP_TABLE_ACTION. Note that binary log is flushed at the
    final stage when the table is ready. So if we have XID in the
    binary log we don't need to drop the table.

  - Three variations of CREATE OR REPLACE handled:

    1. CREATE OR REPLACE TABLE t1 (..);
    2. CREATE OR REPLACE TABLE t1 LIKE t2;
    3. CREATE OR REPLACE TABLE t1 SELECT ..;

  - Test case uses 6 combinations for engines (aria, aria_notrans,
    myisam, ib, lock_tables, expensive_rename) and 2 combinations for
    binlog types (row, stmt). Combinations help to check differences
    between the results. Error failures are tested for the above three
    variations.

  - expensive_rename tests CREATE OR REPLACE without atomic
    replace. The effect should be the same as with the old behaviour
    before this commit.

  - Triggers mechanism is unaffected by this change. This is tested in
    create_replace.test.

  - LOCK TABLES is affected. Lock restoration must be done after new
    table is created or TMP is renamed back to ORIG

  - Moved ddl_log_complete() from send_eof() to finalize_ddl(). This
    checkpoint was not executed before for normal CREATE TABLE but is
    executed now.

  - CREATE TABLE will now rollback also if writing to the binary
    logging failed. See rpl_gtid_strict.test

backup ddl log changes

- In case of a successfull CREATE OR REPLACE we only log
  the CREATE event, not the DROP TABLE event of the old table.

ddl_log.cc changes

  ddl_log_execute_action() now properly return error conditions.
  ddl_log_disable_entry() added to allow one to disable one entry.
  The entry on disk is still reserved until ddl_log_complete() is
  executed.

On XID usage

  Like with all other atomic DDL operations XID is used to avoid
  inconsistency between master and slave in the case of a crash after
  binary log is written and before ddl_log_state_create is closed. On
  recovery XIDs are taken from binary log and corresponding DDL log
  events get disabled.  That is done by
  ddl_log_close_binlogged_events().

On linking two chains together

  Chains are executed in the ascending order of entry_pos of execute
  entries. But entry_pos assignment order is undefined: it may assign
  bigger number for the first chain and then smaller number for the
  second chain. So the execution order in that case will be reverse:
  second chain will be executed first.

  To avoid that we link one chain to another. While the base chain
  (ddl_log_state_create) is active the secondary chain
  (ddl_log_state_rm) is not executed. That is: only one chain can be
  executed in two linked chains.

  The interface ddl_log_link_chains() was defined in "MDEV-22166
  ddl_log_write_execute_entry() extension".

Atomic info parameters in HA_CREATE_INFO

  Many functions in CREATE TABLE pass the same parameters. These
  parameters are part of table creation info and should be in
  HA_CREATE_INFO (or whatever). Passing parameters via single
  structure is much easier for adding new data and
  refactoring.

InnoDB changes
  Added ha_innobase::can_be_renamed_to_backup() to check if
  a table with foreign keys can be renamed.

Aria changes:
- Fixed issue in Aria engine with CREATE + locked tables
  that data was not properly commited in some cases in
  case of crashes.

Known issues:
- InnoDB tables with foreign key definitions are not fully supported
  with atomic create and replace:
  - ha_innobase::can_be_renamed_to_backup() can detect some cases
    where InnoDB does not support renaming table with foreign key
    constraints.  In this case MariaDB will drop the old table before
    creating the new one.
    The detected cases are:
    - The new and old table is using the same foreign key constraint
      name.
    - The old table has self referencing constraints.
  - If the old and new table uses the same name for a constraint the
    create of the new table will fail. The orignal table will be
    restored in this case.
  - The above issues will be fixed in a future commit.
- CREATE OR REPLACE TEMPORARY table is not full atomic. Any conflicting
  table will always be dropped before creating a new one. (Old behaviour).
2025-03-18 18:28:16 +01:00

303 lines
12 KiB
Text

#
# alter ignore cannot be done online
#
create table t (a int);
alter ignore table t add primary key (a), algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: ALTER IGNORE TABLE. Try LOCK=SHARED
drop table t;
#
# MDEV-28771 Assertion `table->in_use&&tdc->flushed' failed after ALTER
#
create table t (a char(1));
insert into t values ('a'),('b');
select * from t join t as t2 join t as t3;
a a a
a a a
b a a
a b a
b b a
a a b
b a b
a b b
b b b
alter table t modify a int;
ERROR 22007: Truncated incorrect INTEGER value: 'a'
select * from t;
a
a
b
drop table t;
create table t (c double precision key,c2 char,c3 year);
insert into t values (7,3,1);
select a from t where a=all (select a from t where b=2 union select a from t where b=2);
ERROR 42S22: Unknown column 'a' in 'SELECT'
insert into t values (3,1,1);
alter table t change c c date,add key(c);
ERROR 22007: Incorrect date value: '7' for column `test`.`t`.`c` at row 1
select * from t;
c c2 c3
7 3 2001
3 1 2001
drop table t;
set sql_mode='';
create table t (c char unique,c2 int,stamp timestamp);
insert into t values (1,1,1),(2,2,2),(3,3,3),(4,4,4),(5,5,5);
Warnings:
Warning 1265 Data truncated for column 'stamp' at row 1
Warning 1265 Data truncated for column 'stamp' at row 2
Warning 1265 Data truncated for column 'stamp' at row 3
Warning 1265 Data truncated for column 'stamp' at row 4
Warning 1265 Data truncated for column 'stamp' at row 5
update t set c=(select * from t) where c in (select * from t);
ERROR 21000: Operand should contain 1 column(s)
alter table t modify c date;
ERROR 23000: Duplicate entry '0000-00-00' for key 'c'
select * from t;
c c2 stamp
1 1 0000-00-00 00:00:00
2 2 0000-00-00 00:00:00
3 3 0000-00-00 00:00:00
4 4 0000-00-00 00:00:00
5 5 0000-00-00 00:00:00
drop table t;
set sql_mode=default;
#
# MDEV-28944 XA assertions failing in binlog_rollback and binlog_commit
#
CREATE TABLE t (a INT) ENGINE=MyISAM;
INSERT INTO t VALUES (1);
connect con1,localhost,root,,test;
XA START 'xid';
SELECT * FROM t;
a
1
connection default;
ALTER TABLE t NOWAIT ADD KEY (a);
ERROR HY000: Lock wait timeout exceeded; try restarting transaction
connection con1;
UPDATE t SET a = 2;
XA END 'xid';
XA COMMIT 'xid' ONE PHASE;
DROP TABLE t;
disconnect con1;
connection default;
#
# MDEV-29068 Cascade foreign key updates do not apply in online alter
#
create table t1 (a int primary key) engine=InnoDB;
insert into t1 values (1),(2),(3);
create table t2 (b int, foreign key (b)
references t1 (a)
on update cascade) engine=InnoDB;
insert into t2 values (1),(2),(3);
alter table t2 add c int, algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: ON UPDATE CASCADE. Try LOCK=SHARED
alter table t2 add c int, algorithm=inplace, lock=none;
create or replace table t2 (b int, foreign key (b)
references t1 (a)
on delete set null) engine=InnoDB;
Warnings:
Warning 122 Engine does not support atomic create or replace for table 't2'. Original table will be deleted
Warning 1451 Cannot delete or update a parent row: a foreign key constraint fails
alter table t2 add c int, algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: ON DELETE SET NULL. Try LOCK=SHARED
alter table t2 add c int, algorithm=inplace, lock=none;
create or replace table t2 (b int, foreign key (b)
references t1 (a)
on delete no action) engine=InnoDB;
Warnings:
Warning 122 Engine does not support atomic create or replace for table 't2'. Original table will be deleted
Warning 1451 Cannot delete or update a parent row: a foreign key constraint fails
insert into t2 values (1),(2),(3);
alter table t2 add c int, algorithm=copy, lock=none;
create or replace table t2 (b int, foreign key (b)
references t1 (a)
on update restrict) engine=InnoDB;
Warnings:
Warning 122 Engine does not support atomic create or replace for table 't2'. Original table will be deleted
Warning 1451 Cannot delete or update a parent row: a foreign key constraint fails
insert into t2 values (1),(2),(3);
alter table t2 add c int, algorithm=copy, lock=none;
drop table t2, t1;
create table t1 (a int primary key, b int unique) engine=InnoDB;
insert into t1 values (1, 1),(2, 2),(3, 3);
create table t2 (a int references t1 (a),
b int references t1 (b) on update cascade) engine=InnoDB;
insert into t2 values (1, 1),(2, 2);
alter table t2 add c int, algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: ON UPDATE CASCADE. Try LOCK=SHARED
alter table t2 add c int, algorithm=copy;
alter table t2 add d int, algorithm=inplace;
drop table t2, t1;
#
# MDEV-30891 Assertion `!table->versioned(VERS_TRX_ID)' failed
# in Write_rows_log_event::binlog_row_logging_function
#
set system_versioning_alter_history= keep;
create table t1 (id int,
row_start bigint unsigned generated always as row start,
row_end bigint unsigned generated always as row end,
period for system_time (row_start, row_end))
engine=innodb with system versioning;
alter table t1 add c int, algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: BIGINT GENERATED ALWAYS AS ROW_START. Try LOCK=SHARED
alter table t1 add c int, algorithm=inplace;
alter table t1 add d int, lock=none;
set system_versioning_alter_history= default;
drop table t1;
#
# MDEV-31058 ER_KEY_NOT_FOUND upon concurrent CHANGE column autoinc
# and DML
#
create table t (a serial, b int) engine=innodb;
alter table t drop a, modify b serial, algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: CHANGE COLUMN ... AUTO_INCREMENT. Try LOCK=SHARED
set statement sql_mode= NO_AUTO_VALUE_ON_ZERO for
alter table t drop a, modify b serial, algorithm=copy, lock=none;
create or replace table t (a serial, b int) engine=innodb;
show create table t;
Table Create Table
t CREATE TABLE `t` (
`a` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`b` int(11) DEFAULT NULL,
UNIQUE KEY `a` (`a`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
# a is unique in the old table, but is shrunk in the new one.
# Only unsafe approach is fine because of possible collisions.
alter table t modify a int, modify b serial, algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: CHANGE COLUMN ... AUTO_INCREMENT. Try LOCK=SHARED
#
# Check that we treat autoinc columns correctly modify old autoinc is
# fine, adding new autoinc for existed column is unsafe.
#
create or replace table t (a serial) engine=innodb;
alter table t change a b serial, algorithm=copy, lock=none;
Warnings:
Note 1831 Duplicate index `b`. This is deprecated and will be disallowed in a future release
# Shrinking the autoinc field is considered safe.
# ER_WARN_DATA_OUT_OF_RANGE should be emitted otherwise.
alter table t change b b int auto_increment primary key,
algorithm=copy, lock=none;
alter table t add c int default(0), drop primary key, drop key a;
# key `b` is still there
show create table t;
Table Create Table
t CREATE TABLE `t` (
`b` int(11) NOT NULL AUTO_INCREMENT,
`c` int(11) DEFAULT 0,
UNIQUE KEY `b` (`b`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_uca1400_ai_ci
alter table t drop b, change c c serial, algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: CHANGE COLUMN ... AUTO_INCREMENT. Try LOCK=SHARED
# Check existed unique keys.
create or replace table t(a int, b int not null, c int not null, d int);
# No unique in the old table;
alter table t add unique(b, c), modify d int auto_increment, add key(d),
algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: CHANGE COLUMN ... AUTO_INCREMENT. Try LOCK=SHARED
alter table t add unique(a, b);
# Unique in the old table has nulls;
alter table t modify d int auto_increment, add key(d),
algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: CHANGE COLUMN ... AUTO_INCREMENT. Try LOCK=SHARED
alter table t add unique(b, c);
# Change unique's column;
alter table t change b x bigint, modify d int auto_increment, add key(d),
algorithm=copy, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: CHANGE COLUMN ... AUTO_INCREMENT. Try LOCK=SHARED
# Finally good. Simple renames with a type unchanged will not affect
# the result. Also NOT NULL -> NULL transform is fine.
alter table t modify d int auto_increment, add key(d),
change b x int null,
algorithm=copy, lock=none;
drop table t;
# MDEV-31172 Server crash or ASAN errors in online_alter_check_autoinc
create table t (a int, b int, c char(8), key(a,b,c));
alter table t modify c int auto_increment key, algorithm=copy;
drop table t;
# MDEV-31601 Some ALTER TABLE .. fail when they worked before, and with
# a wrong error message
create table t (a int) engine=aria;
insert into t values (1),(2);
alter table t algorithm=nocopy, order by a;
ERROR 0A000: ALGORITHM=NOCOPY is not supported for this operation. Try ALGORITHM=COPY
alter table t engine=myisam, algorithm=inplace;
ERROR 0A000: ALGORITHM=INPLACE is not supported for this operation. Try ALGORITHM=COPY
drop table t;
create temporary table t (f int);
alter table t force, algorithm=instant;
ERROR 0A000: ALGORITHM=INSTANT is not supported for this operation. Try ALGORITHM=COPY
drop table t;
create sequence s engine=MyISAM;
alter table s engine=Aria, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: SEQUENCE. Try LOCK=SHARED
alter table s engine=Aria;
drop sequence s;
# MDEV-31631 Adding auto-increment column to a table with history online
# behaves differently from non-online
create sequence s;
create table t1(a int, x int NULL default(nextval(s)));
alter table t1 add b int default (nextval(s)), lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: Function or expression 'NEXTVAL()' cannot be used in the DEFAULT clause of `b`. Try LOCK=SHARED
alter table t1 add b int primary key auto_increment, lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: ADD COLUMN ... AUTO_INCREMENT. Try LOCK=SHARED
create table t2(a int, b int NULL default(nextval(s)));
alter table t2 modify b int not null default (nextval(s)), lock=none;
ERROR 0A000: LOCK=NONE is not supported. Reason: Function or expression 'NEXTVAL()' cannot be used in the DEFAULT clause of `b`. Try LOCK=SHARED
drop table t2;
drop table t1;
drop sequence s;
#
# MDEV-33348 ALTER TABLE lock waiting stages are indistinguishable
#
connect con2, localhost, root;
create table t1 (a int);
insert t1 values (5);
start transaction;
select * from t1;
a
5
connection default;
alter table t1 add b int NULL, algorithm= copy, lock= none;
connection con2;
set @con= $con;
select stage, state, info from information_schema.processlist where id = @con;
stage 4
state Waiting for table metadata lock
info alter table t1 add b int NULL, algorithm= copy, lock= none
rollback;
connection default;
drop table t1;
disconnect con2;
# MDEV-34164 Server crashes when executing OPTIMIZE or REPAIR TABLE for InnoDB temporary tables
create temporary table t1 (i int) engine=innodb;
create table t2 (i int) engine=aria ;
optimize table t1,t2;
Table Op Msg_type Msg_text
test.t1 optimize note Table does not support optimize, doing recreate + analyze instead
test.t1 optimize status OK
test.t2 optimize status Table is already up to date
drop table t1,t2;
create temporary table t1 (f int) engine=innodb;
create temporary table t2 (f int) engine=innodb;
optimize local table t1,t2;
Table Op Msg_type Msg_text
test.t1 optimize note Table does not support optimize, doing recreate + analyze instead
test.t1 optimize status OK
test.t2 optimize note Table does not support optimize, doing recreate + analyze instead
test.t2 optimize status OK
drop table t1,t2;
set @save_sql_mode = @@sql_mode;
set sql_mode= '';
create temporary table t (c decimal zerofill,c2 int zerofill,c3 char binary,key(c)) engine=innodb;
insert into t values (1,1,1);
set session enforce_storage_engine=aria;
optimize no_write_to_binlog table t;
Table Op Msg_type Msg_text
test.t optimize note Table does not support optimize, doing recreate + analyze instead
test.t optimize status OK
Warnings:
Note 1266 Using storage engine Aria for table 't'
drop table t;
set sql_mode= @save_sql_mode;
# End of 11.2 tests