mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +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:
@ -1253,9 +1253,12 @@ values_loop_end:
|
||||
thd->log_current_statement() ||
|
||||
was_insert_delayed)
|
||||
{
|
||||
if(WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open())
|
||||
bool binlogged= 0;
|
||||
if ((WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open()) &&
|
||||
table->s->using_binlog())
|
||||
{
|
||||
int errcode= 0;
|
||||
int errcode= 0, skip_binlog= 0;
|
||||
String log_query;
|
||||
if (error <= 0)
|
||||
{
|
||||
/*
|
||||
@ -1291,27 +1294,42 @@ values_loop_end:
|
||||
if (was_insert_delayed && table_list->lock_type == TL_WRITE)
|
||||
{
|
||||
/* Binlog INSERT DELAYED as INSERT without DELAYED. */
|
||||
String log_query;
|
||||
if (create_insert_stmt_from_insert_delayed(thd, &log_query))
|
||||
{
|
||||
sql_print_error("Event Error: An error occurred while creating query string"
|
||||
"for INSERT DELAYED stmt, before writing it into binary log.");
|
||||
|
||||
error= 1;
|
||||
skip_binlog= error= 1;
|
||||
}
|
||||
else if (thd->binlog_query(THD::ROW_QUERY_TYPE,
|
||||
log_query.c_ptr(), log_query.length(),
|
||||
transactional_table, FALSE, FALSE,
|
||||
errcode) > 0)
|
||||
}
|
||||
else
|
||||
log_query.set(thd->query(), thd->query_length(), log_query.charset());
|
||||
|
||||
if (!skip_binlog)
|
||||
{
|
||||
int binlog_error;
|
||||
binlog_error= thd->binlog_query(THD::ROW_QUERY_TYPE,
|
||||
log_query.c_ptr(),
|
||||
log_query.length(),
|
||||
transactional_table, FALSE, FALSE,
|
||||
errcode);
|
||||
if (likely(binlog_error == 0))
|
||||
binlogged= 1;
|
||||
else if (binlog_error > 0)
|
||||
error= 1;
|
||||
}
|
||||
else if (thd->binlog_query(THD::ROW_QUERY_TYPE,
|
||||
thd->query(), thd->query_length(),
|
||||
transactional_table, FALSE, FALSE,
|
||||
errcode) > 0)
|
||||
error= 1;
|
||||
}
|
||||
if (changed)
|
||||
{
|
||||
if (!binlogged)
|
||||
table->mark_as_not_binlogged();
|
||||
}
|
||||
else
|
||||
thd->tmp_table_binlog_handled= 1; // Temp table not changed
|
||||
}
|
||||
else
|
||||
thd->tmp_table_binlog_handled= 1; // Temp table not changed
|
||||
|
||||
DBUG_ASSERT(transactional_table || !changed ||
|
||||
thd->transaction->stmt.modified_non_trans_table);
|
||||
}
|
||||
@ -4394,7 +4412,7 @@ bool select_insert::store_values(List<Item> &values, bool *trg_skip_row)
|
||||
DBUG_RETURN(error);
|
||||
}
|
||||
|
||||
bool select_insert::prepare_eof()
|
||||
bool select_insert::prepare_eof(bool in_create_table)
|
||||
{
|
||||
int error;
|
||||
bool const trans_table= table->file->has_transactions_and_rollback();
|
||||
@ -4454,14 +4472,22 @@ bool select_insert::prepare_eof()
|
||||
thd->transaction->stmt.modified_non_trans_table);
|
||||
|
||||
/*
|
||||
Write to binlog before commiting transaction. No statement will
|
||||
Write to binlog before committing transaction. No statement will
|
||||
be written by the binlog_query() below in RBR mode. All the
|
||||
events are in the transaction cache and will be written when
|
||||
ha_autocommit_or_rollback() is issued below.
|
||||
|
||||
Temporary tables will be logged only on CREATE in STMT format
|
||||
or on INSERT if all changes to the table is in the binlog.
|
||||
*/
|
||||
if ((WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open()) &&
|
||||
(likely(!error) || thd->transaction->stmt.modified_non_trans_table ||
|
||||
thd->log_current_statement()))
|
||||
(table->s->using_binlog() ||
|
||||
((in_create_table &&
|
||||
(!table->s->tmp_table || thd->binlog_create_tmp_table())))) &&
|
||||
(likely(!error) ||
|
||||
(!in_create_table &&
|
||||
(thd->transaction->stmt.modified_non_trans_table ||
|
||||
thd->log_current_statement()))))
|
||||
{
|
||||
int errcode= 0;
|
||||
int res;
|
||||
@ -4480,8 +4506,12 @@ bool select_insert::prepare_eof()
|
||||
DBUG_RETURN(true);
|
||||
}
|
||||
binary_logged= res == 0 || !table->s->tmp_table;
|
||||
if (binary_logged)
|
||||
table->s->table_creation_was_logged= 1;
|
||||
}
|
||||
table->s->table_creation_was_logged|= binary_logged;
|
||||
else if (changed)
|
||||
table->mark_as_not_binlogged();
|
||||
|
||||
table->file->ha_release_auto_increment();
|
||||
|
||||
if (unlikely(error))
|
||||
@ -4534,7 +4564,7 @@ bool select_insert::send_eof()
|
||||
{
|
||||
bool res;
|
||||
DBUG_ENTER("select_insert::send_eof");
|
||||
res= (prepare_eof() || (!suppress_my_ok && send_ok_packet()));
|
||||
res= (prepare_eof(0) || (!suppress_my_ok && send_ok_packet()));
|
||||
DBUG_RETURN(res);
|
||||
}
|
||||
|
||||
@ -4599,28 +4629,33 @@ void select_insert::abort_result_set()
|
||||
if (thd->transaction->stmt.modified_non_trans_table ||
|
||||
thd->log_current_statement())
|
||||
{
|
||||
if (!can_rollback_data())
|
||||
thd->transaction->all.modified_non_trans_table= TRUE;
|
||||
if (!can_rollback_data())
|
||||
thd->transaction->all.modified_non_trans_table= TRUE;
|
||||
|
||||
if(WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open())
|
||||
{
|
||||
StatementBinlog stmt_binlog(thd, !can_rollback_data() &&
|
||||
thd->binlog_need_stmt_format(transactional_table));
|
||||
int errcode= query_error_code(thd, thd->killed == NOT_KILLED);
|
||||
int res;
|
||||
/* error of writing binary log is ignored */
|
||||
res= thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(),
|
||||
thd->query_length(),
|
||||
transactional_table, FALSE, FALSE, errcode);
|
||||
binary_logged= res == 0 || !table->s->tmp_table;
|
||||
}
|
||||
if (changed)
|
||||
query_cache_invalidate3(thd, table, 1);
|
||||
if (WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open())
|
||||
{
|
||||
StatementBinlog stmt_binlog(thd, !can_rollback_data() &&
|
||||
thd->binlog_need_stmt_format(transactional_table));
|
||||
int errcode= query_error_code(thd, thd->killed == NOT_KILLED);
|
||||
int res;
|
||||
/* error of writing binary log is ignored */
|
||||
res= thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(),
|
||||
thd->query_length(),
|
||||
transactional_table, FALSE, FALSE, errcode);
|
||||
binary_logged= res == 0 || !table->s->tmp_table;
|
||||
}
|
||||
if (changed)
|
||||
{
|
||||
if (!binary_logged)
|
||||
table->mark_as_not_binlogged();
|
||||
query_cache_invalidate3(thd, table, 1);
|
||||
}
|
||||
else
|
||||
thd->tmp_table_binlog_handled= 1; // tmp table not changed
|
||||
}
|
||||
DBUG_ASSERT(transactional_table || !changed ||
|
||||
thd->transaction->stmt.modified_non_trans_table);
|
||||
|
||||
table->s->table_creation_was_logged|= binary_logged;
|
||||
table->file->ha_release_auto_increment();
|
||||
}
|
||||
|
||||
@ -4882,6 +4917,8 @@ TABLE *select_create::create_table_from_items(THD *thd, List<Item> *items,
|
||||
mysql_unlock_tables(thd, *lock);
|
||||
*lock= 0;
|
||||
}
|
||||
table->s->table_creation_was_logged= save_table_creation_was_logged;
|
||||
|
||||
drop_open_table(thd, table, &table_list->db, &table_list->table_name);
|
||||
ddl_log_complete(&ddl_log_state_rm);
|
||||
ddl_log_complete(&ddl_log_state_create);
|
||||
@ -5134,16 +5171,16 @@ bool binlog_create_table(THD *thd, TABLE *table, bool replace)
|
||||
bool result;
|
||||
ulonglong save_option_bits;
|
||||
|
||||
/* Don't log temporary tables in row format */
|
||||
if (thd->variables.binlog_format == BINLOG_FORMAT_ROW &&
|
||||
table->s->tmp_table)
|
||||
/* Don't log temporary tables in row or mixed format */
|
||||
if (table->s->tmp_table && !thd->binlog_create_tmp_table())
|
||||
return 0;
|
||||
if (!thd->binlog_table_should_be_logged(&table->s->db))
|
||||
return 0;
|
||||
|
||||
/*
|
||||
We have to use ROW format to ensure that future row inserts will be
|
||||
logged
|
||||
logged. For temporary tables this means the table will not be binlogged
|
||||
anymore.
|
||||
*/
|
||||
thd->set_current_stmt_binlog_format_row();
|
||||
table->file->prepare_for_row_logging();
|
||||
@ -5176,7 +5213,7 @@ bool binlog_create_table(THD *thd, TABLE *table, bool replace)
|
||||
bool binlog_drop_table(THD *thd, TABLE *table)
|
||||
{
|
||||
StringBuffer<2048> query(system_charset_info);
|
||||
/* Don't log temporary tables in row format */
|
||||
/* Don't log temporary tables if creation was not logged */
|
||||
if (!table->s->table_creation_was_logged)
|
||||
return 0;
|
||||
if (!thd->binlog_table_should_be_logged(&table->s->db))
|
||||
@ -5240,7 +5277,7 @@ bool select_create::send_eof()
|
||||
ddl_log_complete(&ddl_log_state_rm);
|
||||
}
|
||||
|
||||
if (prepare_eof())
|
||||
if (prepare_eof(1))
|
||||
{
|
||||
abort_result_set();
|
||||
DBUG_RETURN(true);
|
||||
@ -5436,7 +5473,8 @@ void select_create::abort_result_set()
|
||||
{
|
||||
bool tmp_table= table->s->tmp_table;
|
||||
bool table_creation_was_logged= (!tmp_table ||
|
||||
table->s->table_creation_was_logged);
|
||||
table->s->table_creation_was_logged ||
|
||||
create_info->table_was_deleted);
|
||||
|
||||
/* CREATE SELECT failed. Remove all row events and clear caches */
|
||||
thd->binlog_remove_rows_events();
|
||||
|
Reference in New Issue
Block a user