Commit 5392b4a3 authored by Sergei Golubchik's avatar Sergei Golubchik

MDEV-20354 All but last insert ignored in InnoDB tables when table locked

mysql_insert() first opens all affected tables (which implicitly
starts a transaction in InnoDB), then stat tables.
A failure to open a stat table caused open_tables() to abort
the current stmt transaction (trans_rollback_stmt()). So, from the
server point of view the following ha_write_row()-s happened outside
of a transactions, and the server didn't bother to commit them.

The server has a mechanism to prevent a transaction being
unexpectedly committed or rolled back in the middle of a statement -
if an operation takes place _in a sub-statement_ it cannot change
the transaction state. Operations on stat tables are exactly that -
they are not allowed to change a transaction state. Put them in
a sub-statement to make sure they don't.
parent c8ba9820
rename table mysql.table_stats to mysql.table_stats_save;
flush tables;
set use_stat_tables= PREFERABLY;
create table t1 (a int) engine=InnoDB;
start transaction;
insert t1 values (1);
insert t1 values (2);
commit;
select * from t1;
a
1
2
drop table t1;
rename table mysql.table_stats_save to mysql.table_stats;
flush tables;
source include/have_innodb.inc;
#
# MDEV-20354 All but last insert ignored in InnoDB tables when table locked
#
rename table mysql.table_stats to mysql.table_stats_save;
flush tables;
set use_stat_tables= PREFERABLY;
create table t1 (a int) engine=InnoDB;
start transaction;
insert t1 values (1);
insert t1 values (2);
commit;
select * from t1;
drop table t1;
rename table mysql.table_stats_save to mysql.table_stats;
flush tables;
......@@ -1444,12 +1444,16 @@ class Open_tables_backup: public Open_tables_state
/**
@class Sub_statement_state
@brief Used to save context when executing a function or trigger
operations on stat tables aren't technically a sub-statement, but they are
similar in a sense that they cannot change the transaction status.
*/
/* Defines used for Sub_statement_state::in_sub_stmt */
#define SUB_STMT_TRIGGER 1
#define SUB_STMT_FUNCTION 2
#define SUB_STMT_STAT_TABLES 4
class Sub_statement_state
......
......@@ -268,7 +268,9 @@ inline int open_stat_tables(THD *thd, TABLE_LIST *tables,
thd->push_internal_handler(&deh);
init_table_list_for_stat_tables(tables, for_write);
init_mdl_requests(tables);
thd->in_sub_stmt|= SUB_STMT_STAT_TABLES;
rc= open_system_tables_for_read(thd, tables, backup);
thd->in_sub_stmt&= ~SUB_STMT_STAT_TABLES;
thd->pop_internal_handler();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment