From 82d9d72fb1d625db55829c95a8b54509d9c97be8 Mon Sep 17 00:00:00 2001 From: Anel Husakovic Date: Wed, 19 Jul 2023 13:48:53 +0200 Subject: [PATCH] MDEV-31618: Server crashes in process_i_s_table_temporary_tables/get_all_tables - Pre-open temporary table on sequence creation. - Without this patch, if rename alter is done on the temporary sequence, and after that `create replace`, since table is not preopened and alter rename marked the table as reopen, and such table is deleted in the `find_temporary_table()` leaving the share without the table, that causes `show tables` to fail - Closes PR #2685 - Reviewer: --- .../main/information_schema_temp_table.result | 35 +++++++++++++++++++ .../main/information_schema_temp_table.test | 23 ++++++++++++ sql/sql_parse.cc | 1 + sql/sql_show.cc | 4 +-- 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/mysql-test/main/information_schema_temp_table.result b/mysql-test/main/information_schema_temp_table.result index 125bdd99660..ff5e098f5f3 100644 --- a/mysql-test/main/information_schema_temp_table.result +++ b/mysql-test/main/information_schema_temp_table.result @@ -231,3 +231,38 @@ def test t1 BASE TABLE MyISAM 10 Fixed 1 7 X X X X NULL X X NULL latin1_swedish_ def test t2 TEMPORARY MRG_MyISAM 10 Fixed 0 0 X X X X NULL X X NULL latin1_swedish_ci NULL X Y def test t3 BASE TABLE MRG_MyISAM 10 Fixed 1 5 X X X X NULL X X NULL latin1_swedish_ci NULL X N DROP TABLE t1,t2,t3; +# +# MDEV-31618: Server crashes in +# process_i_s_table_temporary_tables/get_all_tables +# +CREATE TEMPORARY SEQUENCE seq1; +SHOW FULL TABLES; +Tables_in_test Table_type +seq1 TEMPORARY SEQUENCE +SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type='temporary sequence'; +table_schema table_name +test seq1 +mysqltest s2 +mysqltest s1 +ALTER TABLE `seq1` CHANGE `cache_size` cache_size int; +ERROR HY000: Sequence 'test.seq1' table structure is invalid (cache_size) +SHOW FULL TABLES; +Tables_in_test Table_type +seq1 TEMPORARY SEQUENCE +SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type='temporary sequence'; +table_schema table_name +test seq1 +mysqltest s2 +mysqltest s1 +CREATE OR REPLACE TEMPORARY SEQUENCE seq1; +SHOW FULL TABLES; +Tables_in_test Table_type +seq1 TEMPORARY SEQUENCE +SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type='temporary sequence'; +table_schema table_name +test seq1 +mysqltest s2 +mysqltest s1 +DROP TABLE seq1; +DROP TABLE mysqltest.s1; +DROP TABLE mysqltest.s2; diff --git a/mysql-test/main/information_schema_temp_table.test b/mysql-test/main/information_schema_temp_table.test index 62e7a9ffd81..5cc8fa971b5 100644 --- a/mysql-test/main/information_schema_temp_table.test +++ b/mysql-test/main/information_schema_temp_table.test @@ -219,3 +219,26 @@ CREATE TABLE t3 (a INT) ENGINE=MERGE UNION=(t1); --replace_column 10 X 11 X 12 X 13 X 15 X 16 X 22 X SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'test'; DROP TABLE t1,t2,t3; + +--echo # +--echo # MDEV-31618: Server crashes in +--echo # process_i_s_table_temporary_tables/get_all_tables +--echo # + +CREATE TEMPORARY SEQUENCE seq1; +# Check show temp tables before alter +SHOW FULL TABLES; +SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type='temporary sequence'; +--error 4086 +ALTER TABLE `seq1` CHANGE `cache_size` cache_size int; +# Check show temp tables after alter +SHOW FULL TABLES; +SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type='temporary sequence'; + +CREATE OR REPLACE TEMPORARY SEQUENCE seq1; +# Check show temp tables after create/replace alter +SHOW FULL TABLES; +SELECT table_schema, table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_type='temporary sequence'; +DROP TABLE seq1; +DROP TABLE mysqltest.s1; +DROP TABLE mysqltest.s2; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index b6ae771b72d..e745ee3d4d5 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -787,6 +787,7 @@ void init_update_queries(void) Note that SQLCOM_RENAME_TABLE should not be in this list! */ sql_command_flags[SQLCOM_CREATE_TABLE]|= CF_PREOPEN_TMP_TABLES; + sql_command_flags[SQLCOM_CREATE_SEQUENCE]|= CF_PREOPEN_TMP_TABLES; sql_command_flags[SQLCOM_CREATE_INDEX]|= CF_PREOPEN_TMP_TABLES; sql_command_flags[SQLCOM_ALTER_TABLE]|= CF_PREOPEN_TMP_TABLES; sql_command_flags[SQLCOM_TRUNCATE]|= CF_PREOPEN_TMP_TABLES; diff --git a/sql/sql_show.cc b/sql/sql_show.cc index 0ddd589b6a7..8b8ecf526ba 100644 --- a/sql/sql_show.cc +++ b/sql/sql_show.cc @@ -5351,7 +5351,7 @@ int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond) system_charset_info); schema_table_store_record(thd, table); } - else /* SCH_TABLE */ + else /* SCH_TABLES */ process_i_s_table_temporary_tables(thd, table, tmp_tbl); } } @@ -5397,7 +5397,7 @@ int get_all_tables(THD *thd, TABLE_LIST *tables, COND *cond) continue; } #endif - restore_record(table, s->default_values); + restore_record(table, s->default_values); table->field[schema_table->idx_field1]-> store(db_name->str, db_name->length, system_charset_info); table->field[schema_table->idx_field2]->