From 76f888e45db36ffdaf25f591cdee11b5f76011e4 Mon Sep 17 00:00:00 2001 From: ParadoxV5 Date: Wed, 15 Apr 2026 13:18:53 -0600 Subject: [PATCH] MDEV-38600: Annotate the binlogging of recreating MEMORY tables The data in MEMORY tables is temporary and is lost when the server restarts. Thus, it is well-intentioned to record TRUNCATE statements to the binary log when these tables are rediscovered empty. However, as entries with no explicit user query associated (especially the lack of SHUTDOWN on crashes), those unaware of this mechanism can find them unexpected, not to mention their significance downstream in replication. This commit adds a comment to these automatically generated TRUNCATE entries to briefly self-describe their source and purpose. As a part of the generated query, this comment is visible together with the TRUNCATE itself in SHOW BINLOG EVENTS and `mariadb-binlog`, while maintaining seamlessness in replication. There are no other changes in behaviour or storage engine API. Reviewed-by: Andrei Elkin --- mysql-test/suite/rpl/r/rpl_mdev382.result | 2 +- .../t/rpl_memory_engine_truncate_on_restart.test | 2 +- sql/sql_base.cc | 15 +++++++++------ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/mysql-test/suite/rpl/r/rpl_mdev382.result b/mysql-test/suite/rpl/r/rpl_mdev382.result index b3488da7cc96b..b23cffbd7dd02 100644 --- a/mysql-test/suite/rpl/r/rpl_mdev382.result +++ b/mysql-test/suite/rpl/r/rpl_mdev382.result @@ -355,7 +355,7 @@ a` show binlog events in 'master-bin.000002' from ; Log_name Pos Event_type Server_id End_log_pos Info master-bin.000002 # Gtid 1 # GTID #-#-# -master-bin.000002 # Query 1 # TRUNCATE TABLE `db1``; select 'oops!'`.`t``1` +master-bin.000002 # Query 1 # TRUNCATE TABLE `db1``; select 'oops!'`.`t``1` /* generated by recreated memory table */ connection slave; include/start_slave.inc connection master; diff --git a/mysql-test/suite/rpl/t/rpl_memory_engine_truncate_on_restart.test b/mysql-test/suite/rpl/t/rpl_memory_engine_truncate_on_restart.test index a6e0b39ca8557..90c13c431b1f6 100644 --- a/mysql-test/suite/rpl/t/rpl_memory_engine_truncate_on_restart.test +++ b/mysql-test/suite/rpl/t/rpl_memory_engine_truncate_on_restart.test @@ -54,7 +54,7 @@ if ($seq_no_before_restart == $seq_no_after_restart) --let assert_text= Query to truncate the MEMORY table should be the contents of the new event --let assert_count= 1 ---let assert_select= TRUNCATE TABLE +--let assert_select= ^TRUNCATE TABLE .+generated.+memory table --source include/assert_grep.inc --echo # Ensuring slave MEMORY table is empty diff --git a/sql/sql_base.cc b/sql/sql_base.cc index 386f419a5d411..dd2ad530230a5 100644 --- a/sql/sql_base.cc +++ b/sql/sql_base.cc @@ -3284,15 +3284,18 @@ static bool open_table_entry_fini(THD *thd, TABLE_SHARE *share, TABLE *entry) entry->file->implicit_emptied= 0; if (mysql_bin_log.is_open()) { - char query_buf[2*FN_REFLEN + 21]; - String query(query_buf, sizeof(query_buf), system_charset_info); - - query.length(0); - query.append(STRING_WITH_LEN("TRUNCATE TABLE ")); + static const char + QUERY_START[]= "TRUNCATE TABLE ", + QUERY_COMMENT[]= " /* generated by recreated memory table */"; + StringBuffer< + (sizeof(QUERY_START)-1) + (sizeof(QUERY_COMMENT)-1) + + 2 * (FN_REFLEN+3) // identifiers, quotes, and '.' & '\0' + > query(system_charset_info); + query.append(STRING_WITH_LEN(QUERY_START)); append_identifier(thd, &query, &share->db); query.append('.'); append_identifier(thd, &query, &share->table_name); - + query.append(STRING_WITH_LEN(QUERY_COMMENT)); /* we bypass thd->binlog_query() here, as it does a lot of extra work, that is simply wrong in this case