mirror of
https://github.com/MariaDB/server.git
synced 2025-10-26 16:38:11 +01:00
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).
|
||
|---|---|---|
| .. | ||
| binlog_incident.combinations | ||
| binlog_incident.result | ||
| binlog_incident.test | ||
| binlog_index-master.opt | ||
| binlog_index.result | ||
| binlog_index.test | ||
| binlog_ioerr.result | ||
| binlog_ioerr.test | ||
| binlog_mdev_20574_old_binlog.result | ||
| binlog_mdev_20574_old_binlog.test | ||
| binlog_mysqlbinlog-cp932-master.opt | ||
| binlog_mysqlbinlog-cp932.result | ||
| binlog_mysqlbinlog-cp932.test | ||
| binlog_row_annotate-master.opt | ||
| binlog_row_annotate.combinations | ||
| binlog_row_annotate.result | ||
| binlog_row_annotate.test | ||
| binlog_write_error.result | ||
| binlog_write_error.test | ||
| binlog_xa_recover.result | ||
| binlog_xa_recover.test | ||
| disabled.def | ||
| encrypted_master.result | ||
| encrypted_master.test | ||
| encrypted_master_lost_key.result | ||
| encrypted_master_lost_key.test | ||
| encrypted_master_switch_to_unencrypted_coords.cnf | ||
| encrypted_master_switch_to_unencrypted_coords.result | ||
| encrypted_master_switch_to_unencrypted_coords.test | ||
| encrypted_master_switch_to_unencrypted_gtid.cnf | ||
| encrypted_master_switch_to_unencrypted_gtid.result | ||
| encrypted_master_switch_to_unencrypted_gtid.test | ||
| encrypted_slave.cnf | ||
| encrypted_slave.result | ||
| encrypted_slave.test | ||
| encryption_algorithms.combinations | ||
| encryption_algorithms.inc | ||
| encryption_combo.cnf | ||
| encryption_combo.result | ||
| encryption_combo.test | ||
| multisource.cnf | ||
| multisource.result | ||
| multisource.test | ||
| my.cnf | ||
| mysqlbinlog.combinations | ||
| mysqlbinlog.result | ||
| mysqlbinlog.test | ||
| restart_server.inc | ||
| rpl_binlog_errors.cnf | ||
| rpl_binlog_errors.result | ||
| rpl_binlog_errors.test | ||
| rpl_cant_read_event_incident.result | ||
| rpl_cant_read_event_incident.test | ||
| rpl_checksum.cnf | ||
| rpl_checksum.result | ||
| rpl_checksum.test | ||
| rpl_checksum_cache.result | ||
| rpl_checksum_cache.test | ||
| rpl_corruption.cnf | ||
| rpl_corruption.result | ||
| rpl_corruption.test | ||
| rpl_gtid_basic.cnf | ||
| rpl_gtid_basic.combinations | ||
| rpl_gtid_basic.result | ||
| rpl_gtid_basic.test | ||
| rpl_incident.cnf | ||
| rpl_incident.result | ||
| rpl_incident.test | ||
| rpl_init_slave_errors.result | ||
| rpl_init_slave_errors.test | ||
| rpl_loaddata_local.result | ||
| rpl_loaddata_local.test | ||
| rpl_loadfile.result | ||
| rpl_loadfile.test | ||
| rpl_mixed_binlog_max_cache_size.result | ||
| rpl_mixed_binlog_max_cache_size.test | ||
| rpl_packet.cnf | ||
| rpl_packet.result | ||
| rpl_packet.test | ||
| rpl_parallel_analyze_table_hang.result | ||
| rpl_parallel_analyze_table_hang.test | ||
| rpl_parallel_deadlock_corrupt_binlog.result | ||
| rpl_parallel_deadlock_corrupt_binlog.test | ||
| rpl_parallel_domain.result | ||
| rpl_parallel_domain.test | ||
| rpl_parallel_domain_slave_single_grp.result | ||
| rpl_parallel_domain_slave_single_grp.test | ||
| rpl_parallel_free_deferred_event.result | ||
| rpl_parallel_free_deferred_event.test | ||
| rpl_parallel_gco_wait_kill.result | ||
| rpl_parallel_gco_wait_kill.test | ||
| rpl_parallel_gtid_slave_pos_update_fail.result | ||
| rpl_parallel_gtid_slave_pos_update_fail.test | ||
| rpl_parallel_ignore_error_on_rotate.result | ||
| rpl_parallel_ignore_error_on_rotate.test | ||
| rpl_parallel_ignored_errors.result | ||
| rpl_parallel_ignored_errors.test | ||
| rpl_parallel_incorrect_relay_pos.result | ||
| rpl_parallel_incorrect_relay_pos.test | ||
| rpl_parallel_innodb_lock_conflict.result | ||
| rpl_parallel_innodb_lock_conflict.test | ||
| rpl_parallel_missed_error_handling.result | ||
| rpl_parallel_missed_error_handling.test | ||
| rpl_parallel_mode.result | ||
| rpl_parallel_mode.test | ||
| rpl_parallel_partial_binlog_trans.result | ||
| rpl_parallel_partial_binlog_trans.test | ||
| rpl_parallel_record_gtid_wakeup.result | ||
| rpl_parallel_record_gtid_wakeup.test | ||
| rpl_parallel_retry_deadlock.result | ||
| rpl_parallel_retry_deadlock.test | ||
| rpl_parallel_rollback_assert.result | ||
| rpl_parallel_rollback_assert.test | ||
| rpl_parallel_show_binlog_events_purge_logs.cnf | ||
| rpl_parallel_show_binlog_events_purge_logs.result | ||
| rpl_parallel_show_binlog_events_purge_logs.test | ||
| rpl_parallel_single_grpcmt.result | ||
| rpl_parallel_single_grpcmt.test | ||
| rpl_parallel_slave_bgc_kill.result | ||
| rpl_parallel_slave_bgc_kill.test | ||
| rpl_parallel_stop_on_con_kill.result | ||
| rpl_parallel_stop_on_con_kill.test | ||
| rpl_parallel_stop_slave.result | ||
| rpl_parallel_stop_slave.test | ||
| rpl_parallel_wrong_binlog_order.result | ||
| rpl_parallel_wrong_binlog_order.test | ||
| rpl_parallel_wrong_exec_master_pos.result | ||
| rpl_parallel_wrong_exec_master_pos.test | ||
| rpl_relayrotate-slave.opt | ||
| rpl_relayrotate.result | ||
| rpl_relayrotate.test | ||
| rpl_semi_sync.result | ||
| rpl_semi_sync.test | ||
| rpl_skip_replication.cnf | ||
| rpl_skip_replication.result | ||
| rpl_skip_replication.test | ||
| rpl_special_charset.opt | ||
| rpl_special_charset.result | ||
| rpl_special_charset.test | ||
| rpl_sporadic_master-master.opt | ||
| rpl_sporadic_master.result | ||
| rpl_sporadic_master.test | ||
| rpl_ssl.result | ||
| rpl_ssl.test | ||
| rpl_stm_relay_ign_space-slave.opt | ||
| rpl_stm_relay_ign_space.result | ||
| rpl_stm_relay_ign_space.test | ||
| rpl_switch_stm_row_mixed.result | ||
| rpl_switch_stm_row_mixed.test | ||
| rpl_sync-master.opt | ||
| rpl_sync-slave.opt | ||
| rpl_sync.result | ||
| rpl_sync.test | ||
| rpl_temporal_format_default_to_default.cnf | ||
| rpl_temporal_format_default_to_default.result | ||
| rpl_temporal_format_default_to_default.test | ||
| rpl_temporal_format_mariadb53_to_mysql56.cnf | ||
| rpl_temporal_format_mariadb53_to_mysql56.result | ||
| rpl_temporal_format_mariadb53_to_mysql56.test | ||
| rpl_temporal_format_mysql56_to_mariadb53.cnf | ||
| rpl_temporal_format_mysql56_to_mariadb53.result | ||
| rpl_temporal_format_mysql56_to_mariadb53.test | ||
| rpl_typeconv.result | ||
| rpl_typeconv.test | ||
| suite.pm | ||
| testdata.inc | ||