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:
@ -739,6 +739,7 @@ typedef struct system_variables
|
||||
ulonglong default_regex_flags;
|
||||
ulonglong max_mem_used;
|
||||
ulonglong max_rowid_filter_size;
|
||||
ulonglong create_temporary_table_binlog_formats;
|
||||
|
||||
/**
|
||||
Place holders to store Multi-source variables in sys_var.cc during
|
||||
@ -3275,6 +3276,12 @@ public:
|
||||
bool semi_sync_slave;
|
||||
/* Several threads may share this thd. Used with parallel repair */
|
||||
bool shared_thd;
|
||||
/*
|
||||
Mark if query was logged as statement or to mark that there was no
|
||||
changes in the table. Used to check if tmp table changes are
|
||||
properly logged.
|
||||
*/
|
||||
bool tmp_table_binlog_handled;
|
||||
ulonglong client_capabilities; /* What the client supports */
|
||||
ulong max_client_packet_length;
|
||||
|
||||
@ -3454,6 +3461,33 @@ public:
|
||||
current_stmt_binlog_format == BINLOG_FORMAT_ROW);
|
||||
return current_stmt_binlog_format == BINLOG_FORMAT_ROW;
|
||||
}
|
||||
|
||||
int is_binlog_format_row() const
|
||||
{
|
||||
return variables.binlog_format == BINLOG_FORMAT_ROW;
|
||||
}
|
||||
|
||||
/*
|
||||
Should we binlog a CREATE TEMPORARY statement.
|
||||
|
||||
This should happen only if all of the following is true
|
||||
- binlog format is either BINLOG_FORMAT_STMT or BINLOG_FORMAT_MIXED
|
||||
and the corresponding bit is set in binlog_format_for_create_temporary.
|
||||
- The server is not in readonly mode or this is a slave thread.
|
||||
- Slave threads are not affected by readonly in this case.
|
||||
|
||||
Note that CREATE TEMPORARY is always logged in STATEMENT from as
|
||||
temporary tables does not support ROW logging.
|
||||
|
||||
@result 1 CREATE should be logged
|
||||
@result 0 CREATE should not be logged
|
||||
*/
|
||||
bool binlog_create_tmp_table()
|
||||
{
|
||||
return (((1ULL << variables.binlog_format) &
|
||||
variables.create_temporary_table_binlog_formats) &&
|
||||
(!opt_readonly || slave_thread));
|
||||
}
|
||||
/**
|
||||
Determine if binlogging is disabled for this session
|
||||
@retval 0 if the current statement binlogging is disabled
|
||||
@ -3496,6 +3530,8 @@ public:
|
||||
return m_binlog_filter_state;
|
||||
}
|
||||
|
||||
bool binlog_renamed_tmp_tables(TABLE_LIST *table_list);
|
||||
|
||||
/**
|
||||
Checks if a user connection is read-only
|
||||
*/
|
||||
@ -5148,21 +5184,6 @@ public:
|
||||
inline void reset_current_stmt_binlog_format_row()
|
||||
{
|
||||
DBUG_ENTER("reset_current_stmt_binlog_format_row");
|
||||
/*
|
||||
If there are temporary tables, don't reset back to
|
||||
statement-based. Indeed it could be that:
|
||||
CREATE TEMPORARY TABLE t SELECT UUID(); # row-based
|
||||
# and row-based does not store updates to temp tables
|
||||
# in the binlog.
|
||||
INSERT INTO u SELECT * FROM t; # stmt-based
|
||||
and then the INSERT will fail as data inserted into t was not logged.
|
||||
So we continue with row-based until the temp table is dropped.
|
||||
If we are in a stored function or trigger, we mustn't reset in the
|
||||
middle of its execution (as the binary logging way of a stored function
|
||||
or trigger is decided when it starts executing, depending for example on
|
||||
the caller (for a stored function: if caller is SELECT or
|
||||
INSERT/UPDATE/DELETE...).
|
||||
*/
|
||||
DBUG_PRINT("debug",
|
||||
("temporary_tables: %s, in_sub_stmt: %s, system_thread: %s",
|
||||
YESNO(has_temporary_tables()), YESNO(in_sub_stmt),
|
||||
@ -5171,7 +5192,7 @@ public:
|
||||
{
|
||||
if (wsrep_binlog_format(variables.binlog_format) == BINLOG_FORMAT_ROW)
|
||||
set_current_stmt_binlog_format_row();
|
||||
else if (!has_temporary_tables())
|
||||
else
|
||||
set_current_stmt_binlog_format_stmt();
|
||||
}
|
||||
DBUG_VOID_RETURN;
|
||||
@ -5772,6 +5793,8 @@ public:
|
||||
};
|
||||
bool has_thd_temporary_tables();
|
||||
bool has_temporary_tables();
|
||||
bool has_not_logged_temporary_tables();
|
||||
bool has_logged_temporary_tables();
|
||||
|
||||
TABLE *create_and_open_tmp_table(LEX_CUSTRING *frm,
|
||||
const char *path,
|
||||
@ -6500,7 +6523,7 @@ class select_insert :public select_result_interceptor {
|
||||
int send_data(List<Item> &items) override;
|
||||
virtual bool store_values(List<Item> &values, bool *trg_skip_row);
|
||||
virtual bool can_rollback_data() { return 0; }
|
||||
bool prepare_eof();
|
||||
bool prepare_eof(bool using_create);
|
||||
bool send_ok_packet();
|
||||
bool send_eof() override;
|
||||
void abort_result_set() override;
|
||||
|
Reference in New Issue
Block a user