mirror of
https://github.com/MariaDB/server.git
synced 2025-08-08 11:22:35 +03:00
MDEV-36099 Ensure that creation and usage of temporary tables in replication is predictable
MDEV-36563 Assertion `!mysql_bin_log.is_open()' failed in THD::mark_tmp_table_as_free_for_reuse The purpose of this commit is to ensure that creation and changes of temporary tables are properly and predicable logged to the binary log. It also fixes some bugs where ROW logging was used in MIXED mode, when STATEMENT would be a better (and expected) choice. In this comment STATEMENT stands for logging to binary log in STATEMENT format, MIXED stands for MIXED binlog format and ROW for ROW binlog format. New rules for logging of temporary tables - CREATE of temporary tables are now by default binlogged only if STATEMENT binlog format is used. If it is binlogged, 1 is stored in TABLE_SHARE->table_creation_was_logged. The user can change this behavior by setting create_temporary_table_binlog_formats to MIXED,STATEMENT in which case the create is logged in statement format also in MIXED mode (as before). - Changes to temporary tables are only binlogged if and only if the CREATE was logged. The logging happens under STATEMENT or MIXED. If binlog_format=ROW, temporary table changes are not binlogged. A temporary table that are changed under ROW are marked as 'not up to date in binlog' and no future row changes are logged. Any usage of this temporary table will force row logging of other tables in any future statements using the temporary table to be row logged. - DROP TEMPORARY is binlogged only of the CREATE was binlogged. Changes done: - Row logging is forced for any statement using temporary tables that are not up to date in the binary log. (Before the row logging was forced if the user has a temporary table) - If there is any changes to the temporary table that is not binlogged, the table is marked as not up to date. - TABLE_SHARE->table_creation_was_logged has a new definition for temporary tables: 0 Table creating was not logged to binary log 1 Table creating was logged to binary log and table is up to date. 2 Table creating was logged to binary log but some changes where not logged to binary log. Table is not up to date in binary log is defined as value 0 or 2. - If a multi-table-update or multi-table-delete fails then all updated temporary tables are marked as not up to date. - Enforce row logging if the query is using temporary tables that are not up to date. Before row logging was enforced if the user had any temporary tables. - When dropping temporary tables use IF EXISTS. This ensures that slave will not stop if it had crashed and lost the temporary tables. - Remove comment and version from DROP /*!4000 TEMPORARY.. generated when a connection closes that has open temporary tables. Added 'generated by server' at the end of the DROP. Bugs fixed: - When using temporary tables with commands that forced row based, like INSERT INTO temporary_table VALUES (UUID()), this was never logged which causes the temporary table to be inconsistent on master and slave. - Used binlog format is now clearly defined. It is now only depending on the current binlog_format and the tables used. Before it was depending on the user had ANY temporary tables and the state of 'current_stmt_binlog_format' set by previous queries. This also caused temporary tables to be logged to binary log in some cases. - CREATE TABLE t1 LIKE not_logged_temporary_table caused replication to stop. - Rename of not binlogged temporary tables where binlogged to binary log which caused replication to stop. Changes in behavior: - By default create_temporary_table_binlog_formats=STATEMENT, which means that CREATE TEMPORARY is not logged to binary log under MIXED binary logging. This can be changed by setting create_temporary_table_binlog_formats to MIXED,STATEMENT. - Using temporary tables that was not logged to the binary log will cause any query using them for updating other tables to be logged in ROW format. Before all queries was logged in ROW format if the user had any temporary tables, even if they were not used by the query. - Generated DROP TEMPORARY TABLE is now always using IF EXISTS and has a "generated by server" comment in the binary log. The consequences of the above is that manipulations of a lot of rows through temporary tables will by default be be slower in mixed mode. For example: BEGIN; CREATE TEMPORARY TABLE tmp AS SELECT a, b, c FROM large_table1 JOIN large_table2 ON ...; INSERT INTO other_table SELECT b, c FROM tmp WHERE a <100; DROP TEMPORARY TABLE tmp; COMMIT; By default this will create a huge entry in the binary log, compared to just a few hundred bytes in statement mode. However the change in this commit will make usage of temporary tables more reliable and predicable and is thus worth it. Using statement mode or create_temporary_table_binlog_formats can be used to avoid this issue.
This commit is contained in:
@@ -103,7 +103,7 @@ class Enable_wsrep_ctas_guard
|
||||
#endif
|
||||
|
||||
const Lex_ident_column primary_key_name= "PRIMARY"_Lex_ident_column;
|
||||
static const LEX_CSTRING generated_by_server=
|
||||
LEX_CSTRING generated_by_server=
|
||||
{ STRING_WITH_LEN(" /* generated by server */") };
|
||||
static const LEX_CSTRING SEQUENCE_clex_str= { STRING_WITH_LEN("SEQUENCE") };
|
||||
static const LEX_CSTRING TABLE_clex_str= { STRING_WITH_LEN("TABLE") };
|
||||
@@ -1381,11 +1381,12 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables,
|
||||
built_trans_tmp_query.append(STRING_WITH_LEN("DROP TEMPORARY "));
|
||||
built_trans_tmp_query.append(object_to_drop);
|
||||
built_trans_tmp_query.append(' ');
|
||||
if (thd->is_current_stmt_binlog_format_row() || if_exists)
|
||||
{
|
||||
is_drop_tmp_if_exists_added= true;
|
||||
built_trans_tmp_query.append(STRING_WITH_LEN("IF EXISTS "));
|
||||
}
|
||||
/*
|
||||
Better to always use IF EXISTS for temporary tables to handle the case
|
||||
where the slave died after it created it's temporary tables
|
||||
*/
|
||||
is_drop_tmp_if_exists_added= true;
|
||||
built_trans_tmp_query.append(STRING_WITH_LEN("IF EXISTS "));
|
||||
built_non_trans_tmp_query.set_charset(system_charset_info);
|
||||
built_non_trans_tmp_query.copy(built_trans_tmp_query);
|
||||
}
|
||||
@@ -5268,10 +5269,12 @@ err:
|
||||
thd->abort_on_warning= 0;
|
||||
|
||||
/* In RBR or readonly server we don't need to log CREATE TEMPORARY TABLE */
|
||||
if (!result && create_info->tmp_table() &&
|
||||
(thd->is_current_stmt_binlog_format_row() || (opt_readonly && !thd->slave_thread)))
|
||||
if (!result && create_info->tmp_table() && !thd->binlog_create_tmp_table())
|
||||
{
|
||||
/* Note that table->s->table_creation_was_logged is not set! */
|
||||
/*
|
||||
Note that mark_created_temp_table was not called and thus
|
||||
table->s->table_creation_was_logged is not set!
|
||||
*/
|
||||
DBUG_RETURN(result);
|
||||
}
|
||||
|
||||
@@ -5853,22 +5856,26 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table,
|
||||
/*
|
||||
We have to write the query before we unlock the tables.
|
||||
*/
|
||||
if (thd->is_current_stmt_binlog_disabled())
|
||||
if (thd->is_current_stmt_binlog_disabled() ||
|
||||
(create_info->tmp_table() && !thd->binlog_create_tmp_table()))
|
||||
goto err;
|
||||
|
||||
#ifdef ENABLE_WHEN_S3_CAN_CREATE_TABLES
|
||||
/*
|
||||
If we do a create based on a shared table, log the full create of the
|
||||
resulting table. This is needed as a shared table may look different
|
||||
when the slave executes the command.
|
||||
If we do a create based on a shared table or a not logged temporary table,
|
||||
log the full create of the resulting table. This is needed as a shared table
|
||||
may look different when the slave executes the command or the shared table
|
||||
was never replicated to the slave.
|
||||
*/
|
||||
force_generated_create=
|
||||
(((src_table->table->file->partition_ht()->flags &
|
||||
HTON_TABLE_MAY_NOT_EXIST_ON_SLAVE) &&
|
||||
src_table->table->s->db_type() != local_create_info.db_type));
|
||||
((((src_table->table->file->partition_ht()->flags &
|
||||
HTON_TABLE_MAY_NOT_EXIST_ON_SLAVE) &&
|
||||
src_table->table->s->db_type() != local_create_info.db_type)) ||
|
||||
!src_table->table->s->table_creation_was_logged);
|
||||
#endif
|
||||
force_generated_create|= !src_table->table->s->table_creation_was_logged;
|
||||
|
||||
if (thd->is_current_stmt_binlog_format_row() || force_generated_create)
|
||||
if (thd->is_binlog_format_row() || force_generated_create)
|
||||
{
|
||||
/*
|
||||
Since temporary tables are not replicated under row-based
|
||||
@@ -5994,16 +6001,19 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table,
|
||||
if (create_info->tmp_table())
|
||||
{
|
||||
thd->transaction->stmt.mark_created_temp_table();
|
||||
if (!res && local_create_info.table)
|
||||
if (!res && local_create_info.table &&
|
||||
thd->variables.binlog_format == BINLOG_FORMAT_STMT)
|
||||
{
|
||||
/*
|
||||
Remember that tmp table creation was logged so that we know if
|
||||
we should log a delete of it.
|
||||
*/
|
||||
local_create_info.table->s->table_creation_was_logged= 1;
|
||||
do_logging= TRUE;
|
||||
}
|
||||
}
|
||||
do_logging= TRUE;
|
||||
else
|
||||
do_logging= TRUE;
|
||||
}
|
||||
|
||||
err:
|
||||
@@ -10049,13 +10059,11 @@ simple_tmp_rename_or_index_change(THD *thd, TABLE_LIST *table_list,
|
||||
if (likely(!error))
|
||||
{
|
||||
/*
|
||||
We do not replicate alter table statement on temporary tables under
|
||||
ROW-based replication.
|
||||
Only replicate temporary tables that has already been logged to the
|
||||
binary log.
|
||||
*/
|
||||
if (!thd->is_current_stmt_binlog_format_row())
|
||||
{
|
||||
if (table->s->table_creation_was_logged)
|
||||
error= write_bin_log(thd, true, thd->query(), thd->query_length()) != 0;
|
||||
}
|
||||
if (likely(!error))
|
||||
my_ok(thd);
|
||||
}
|
||||
@@ -11911,8 +11919,6 @@ alter_copy:
|
||||
}
|
||||
}
|
||||
|
||||
new_table->s->table_creation_was_logged=
|
||||
table->s->table_creation_was_logged;
|
||||
/* Remove link to old table and rename the new one */
|
||||
thd->drop_temporary_table(table, NULL, true);
|
||||
/* Should pass the 'new_name' as we store table name in the cache */
|
||||
@@ -11934,7 +11940,7 @@ alter_copy:
|
||||
DBUG_ASSERT(!partial_alter); // temporary table alter
|
||||
|
||||
/* We don't replicate alter table statement on temporary tables */
|
||||
if (!thd->is_current_stmt_binlog_format_row() &&
|
||||
if (!thd->is_binlog_format_row() &&
|
||||
table_creation_was_logged &&
|
||||
!binlog_as_create_select)
|
||||
{
|
||||
@@ -11945,6 +11951,7 @@ alter_copy:
|
||||
thd->binlog_xid= 0;
|
||||
if (tmp_error)
|
||||
goto err_cleanup;
|
||||
new_table->s->table_creation_was_logged= 1;
|
||||
}
|
||||
goto end_temporary;
|
||||
}
|
||||
@@ -12174,7 +12181,7 @@ end_inplace:
|
||||
DEBUG_SYNC(thd, "alter_table_before_main_binlog");
|
||||
|
||||
DBUG_ASSERT(!(mysql_bin_log.is_open() &&
|
||||
thd->is_current_stmt_binlog_format_row() &&
|
||||
thd->is_binlog_format_row() &&
|
||||
(create_info->tmp_table())));
|
||||
|
||||
if (start_alter_id)
|
||||
@@ -13598,7 +13605,7 @@ bool Sql_cmd_create_table_like::execute(THD *thd)
|
||||
if (!check_engine(thd, create_table->db.str,
|
||||
create_table->table_name.str,
|
||||
&create_info) &&
|
||||
(!thd->is_current_stmt_binlog_format_row() ||
|
||||
(!thd->is_binlog_format_row() ||
|
||||
!create_info.tmp_table()))
|
||||
{
|
||||
if (thd->lex->sql_command == SQLCOM_CREATE_SEQUENCE &&
|
||||
|
Reference in New Issue
Block a user