-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PS-9238: Make MySQL 5.7 compatible with
CREATE TABLE AS SELECT [...] START TRANSACTION to improve 8.0 -> 5.7 replication reliability https://perconadev.atlassian.net/browse/PS-9238 Problem: 5.7 does not understand the new syntax and complains about CREATE TABLE ... START TRANSACTION received in binlog. Cause: Commit 4fe3f2f (8.0.21) introduced changes that allow the execution of CREATE TABLE AS SELECT as a single transaction. Before that change, CREATE TABLE was the 1st transaction, then rows were inserted, which was unsafe. To achieve this we don't commit the transaction after CREATE TABLE. The commit is done after rows are inserted. For async replica to understand this protocol, START TRANSACTION clause was added to CREATE TABLE in binlog. When the replica sees this, it knows not to commit after CREATE TABLE. Solution: Introduced ctas_compatibility_mode global, read-only variable, OFF by default. If switched to ON, it enforces the behavior from before the above-mentioned commit. Implementation details: The fix contains 2 parts: 1. Query_result_create::binlog_show_create_table() - When SE supports atomic DDL, CREATE TABLE query was binlogged with direct=false / is_trans=true flags. This caused the event to be cached in trx_cache instead of stmt_cache. MYSQL_BIN_LOG::commit() logic skips trx_cache commit, if this is not the transaction commit (it does only stmt_cache commit). Then, when rows are inserted, it was detected that there is ongoing transaction (trx_cache not empty), so no new BEGIN statement was generated in binlog. Effectively CREATE TABLE and rows insertion were done in one transaction. The fix is to revert to the default behavior, i.e., Query_result_create::binlog_show_create_table() creates the event in stmt_cache, which is committed after table creation. When rows are being inserted, it is detected that trx_cache is empty, so BEGIN statement is added to binlog. 2. store_create_info()—When executing CTAS, the START TRANSACTION clause was added to the rewritten query to inform the replica about what was happening. The fix is not to add the START TRANSACTION clause.
- Loading branch information
1 parent
79c1086
commit 09f8fc5
Showing
8 changed files
with
71 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
mysql-test/suite/sys_vars/r/ctas_compatibility_mode.result
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
include/assert.inc [ctas_compatibility_mode default should be OFF] | ||
SET GLOBAL ctas_compatibility_mode = ON; | ||
ERROR HY000: Variable 'ctas_compatibility_mode' is a read only variable | ||
CREATE TABLE t1 (a int); | ||
INSERT INTO t1 VALUES (0); | ||
# restart: --ctas-compatibility-mode=ON --log-bin=ctas_binlog | ||
CREATE TABLE t2 AS SELECT * FROM t1; | ||
# restart: | ||
include/assert_grep.inc ["Checking if ctas_compatibility_mode works"] | ||
include/assert_grep.inc ["Checking if rows are inserted as the separate transaction"] | ||
DROP TABLE t1, t2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# | ||
# The default is OFF | ||
# | ||
--let $assert_text= ctas_compatibility_mode default should be OFF | ||
--let $assert_cond= "[SHOW GLOBAL VARIABLES LIKE "ctas_compatibility_mode", Value, 1]" = "OFF" | ||
--source include/assert.inc | ||
|
||
# | ||
# Check that it is read-only | ||
# | ||
--error ER_INCORRECT_GLOBAL_LOCAL_VAR | ||
SET GLOBAL ctas_compatibility_mode = ON; | ||
|
||
# | ||
# Check that compatibility mode works | ||
# | ||
CREATE TABLE t1 (a int); | ||
INSERT INTO t1 VALUES (0); | ||
|
||
--let $binlog_file = ctas_binlog | ||
--let $restart_parameters = "restart: --ctas-compatibility-mode=ON --log-bin=$binlog_file" | ||
--source include/restart_mysqld.inc | ||
|
||
CREATE TABLE t2 AS SELECT * FROM t1; | ||
|
||
--let $restart_parameters = "restart:" | ||
--source include/restart_mysqld.inc | ||
|
||
let $MYSQLD_DATADIR= `select @@datadir;`; | ||
--exec $MYSQL_BINLOG --verbose $MYSQLD_DATADIR/$binlog_file.000001 > $MYSQLTEST_VARDIR/tmp/$binlog_file.sql | ||
--let $assert_file = $MYSQLTEST_VARDIR/tmp/$binlog_file.sql | ||
--let $assert_text = "Checking if ctas_compatibility_mode works" | ||
--let $assert_select = START TRANSACTION | ||
--let $assert_count = 0 | ||
--source include/assert_grep.inc | ||
|
||
--let $assert_text = "Checking if rows are inserted as the separate transaction" | ||
--let $assert_select = BEGIN | ||
--let $assert_count = 1 | ||
--source include/assert_grep.inc | ||
|
||
# cleanup | ||
DROP TABLE t1, t2; | ||
--remove_file $MYSQLD_DATADIR/$binlog_file.000001 | ||
--remove_file $MYSQLTEST_VARDIR/tmp/$binlog_file.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters