Skip to content

Commit

Permalink
PS-8927: Create table like doesn't write the charset on binlog if its…
Browse files Browse the repository at this point in the history
… created from a temporary table (#5149)

https://jira.percona.com/browse/PS-8927

When creating a table with CREATE TABLE ... LIKE ... from a temporary table, the create table isn't written entirely on the binary log, causing an error on the replica, which will create the table with the charset from the default database. Doesn't happen with persistent tables.

This patch solves the problem by forcing the CHARSET to be present into CREATE TABLE statement of the binary log, as the reference table is temporary and may not exist on replica.
  • Loading branch information
aybek authored and inikep committed Sep 25, 2024
1 parent 06c3748 commit 41c514e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion mysql-test/suite/binlog_nogtid/r/binlog_row_binlog.result
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ binlog.000001 # Query # # use `test`; CREATE TABLE IF NOT EXISTS `t2` (
binlog.000001 # Xid # # COMMIT /* XID */
binlog.000001 # Query # # use `test`; CREATE TABLE IF NOT EXISTS `t3` (
`a` int DEFAULT NULL
) ENGINE=InnoDB
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
binlog.000001 # Query # # BEGIN
binlog.000001 # Table_map # # table_id: # (mysql.user)
binlog.000001 # Write_rows # # table_id: # flags: STMT_END_F
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ binlog.000001 # Query # # use `test`; CREATE TABLE IF NOT EXISTS `t2` (
)
binlog.000001 # Query # # use `test`; CREATE TABLE IF NOT EXISTS `t3` (
`a` int DEFAULT NULL
) ENGINE=InnoDB
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
binlog.000001 # Query # # BEGIN
binlog.000001 # Table_map # # table_id: # (mysql.user)
binlog.000001 # Write_rows # # table_id: # flags: STMT_END_F
Expand Down
2 changes: 1 addition & 1 deletion mysql-test/suite/binlog_nogtid/r/binlog_stm_binlog.result
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ binlog.000001 # Query # # use `test`; create table t1 (a int)
binlog.000001 # Query # # use `test`; create table if not exists t2 select * from t1
binlog.000001 # Query # # use `test`; CREATE TABLE IF NOT EXISTS `t3` (
`a` int DEFAULT NULL
) ENGINE=InnoDB
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
binlog.000001 # Query # # BEGIN
binlog.000001 # Query # # use `mysql`; INSERT IGNORE INTO user SET host='localhost', user='@#@', authentication_string='*1111111111111111111111111111111111111111'
binlog.000001 # Xid # # COMMIT /* XID */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ binlog.000001 # Query # # use `test`; create table t1 (a int)
binlog.000001 # Query # # use `test`; create table if not exists t2 select * from t1
binlog.000001 # Query # # use `test`; CREATE TABLE IF NOT EXISTS `t3` (
`a` int DEFAULT NULL
) ENGINE=InnoDB
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
binlog.000001 # Query # # BEGIN
binlog.000001 # Query # # use `mysql`; INSERT IGNORE INTO user SET host='localhost', user='@#@', authentication_string='*1111111111111111111111111111111111111111'
binlog.000001 # Xid # # COMMIT /* XID */
Expand Down
6 changes: 6 additions & 0 deletions mysql-test/suite/rpl/r/rpl_tmp_table_and_DDL.result
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,10 @@ include/assert.inc ["t1 on master and temp_t1 have the same storage engine"]
include/assert.inc ["t1 on slave and temp_t1 have the same storage engine"]
DROP TEMPORARY TABLE temp_t1;
DROP TABLE t1;
CREATE TEMPORARY TABLE temp_t1 (c1 INT) DEFAULT CHARSET=latin1;
CREATE TABLE t1 LIKE temp_t1;
include/assert.inc ["t1 on master and temp_t1 have the same character set"]
include/assert.inc ["t1 on slave and temp_t1 have the same character set"]
DROP TEMPORARY TABLE temp_t1;
DROP TABLE t1;
include/rpl_end.inc
41 changes: 41 additions & 0 deletions mysql-test/suite/rpl/t/rpl_tmp_table_and_DDL.test
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,47 @@ CREATE TABLE t1 LIKE temp_t1;
--let $assert_text= "t1 on slave and temp_t1 have the same storage engine"
--source include/assert.inc

# Cleanup
--connection master
DROP TEMPORARY TABLE temp_t1;
DROP TABLE t1;

--sync_slave_with_master
#
# BUG#112364
# CREATE TABLE LIKE <TEMP_TABLE> does not write the charset on binary log
# when using row based replication
#
--connection master

# Define temp_t1 character set and collation
--let $charset_temp_t1= latin1
--let $collation_temp_t1= latin1_swedish_ci

# Create the temporary tables
--eval CREATE TEMPORARY TABLE temp_t1 (c1 INT) DEFAULT CHARSET=$charset_temp_t1

# Create t1 based on temporary tables
CREATE TABLE t1 LIKE temp_t1;
--sync_slave_with_master

# On master
--connection master
# Assert that t1 have the same character set as temp_t1
--let $collation_t1= query_get_value(SHOW TABLE STATUS WHERE Name='t1', Collation, 1)
--let $assert_cond= "$collation_t1" = "$collation_temp_t1"
--let $assert_text= "t1 on master and temp_t1 have the same character set"
--source include/assert.inc


# On slave
--connection slave
# Assert that t1 have the same character set as temp_t1
--let $collation_t1= query_get_value(SHOW TABLE STATUS WHERE Name='t1', Collation, 1)
--let $assert_cond= "$collation_t1" = "$collation_temp_t1"
--let $assert_text= "t1 on slave and temp_t1 have the same character set"
--source include/assert.inc

# Cleanup
--connection master
DROP TEMPORARY TABLE temp_t1;
Expand Down
3 changes: 2 additions & 1 deletion sql/sql_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11274,9 +11274,10 @@ bool mysql_create_like_table(THD *thd, Table_ref *table, Table_ref *src_table,

/*
As the reference table is temporary and may not exist on slave, we
must force the ENGINE to be present into CREATE TABLE.
must force the ENGINE and CHARSET to be present into CREATE TABLE.
*/
create_info->used_fields |= HA_CREATE_USED_ENGINE;
create_info->used_fields |= HA_CREATE_USED_DEFAULT_CHARSET;

bool result [[maybe_unused]] = store_create_info(
thd, table, &query, create_info, true /* show_database */,
Expand Down

0 comments on commit 41c514e

Please sign in to comment.