From b9fbd102dd294eb8247eb22d92c68e933fac4595 Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Wed, 28 Apr 2021 00:36:19 +0200 Subject: [PATCH] MDEV-19198 - DBUG assert in CREATE IF NOT EXIST under LOCK TABLES WRITE Relax the assert condition. A locked table that did existed prior to CREATE IF NOT EXIST, retains the MDL_NO_SHARED_READ_WRITE MDL lock prio. --- mysql-test/r/mdev19198.result | 15 +++++++++++++++ mysql-test/t/mdev19198.test | 15 +++++++++++++++ sql/sql_table.cc | 9 ++++++++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 mysql-test/r/mdev19198.result create mode 100644 mysql-test/t/mdev19198.test diff --git a/mysql-test/r/mdev19198.result b/mysql-test/r/mdev19198.result new file mode 100644 index 00000000000..77c08ca0fb7 --- /dev/null +++ b/mysql-test/r/mdev19198.result @@ -0,0 +1,15 @@ +CREATE TABLE t1 (c INT); +CREATE TABLE t2 (c INT); +LOCK TABLES t1 WRITE, t2 READ; +CREATE TABLE IF NOT EXISTS t1 LIKE t2; +Warnings: +Note 1050 Table 't1' already exists +UNLOCK TABLES; +LOCK TABLES t1 READ , t2 READ; +CREATE TABLE IF NOT EXISTS t1 LIKE t2; +ERROR HY000: Table 't1' was locked with a READ lock and can't be updated +UNLOCK TABLES; +CREATE TABLE IF NOT EXISTS t1 LIKE t2; +Warnings: +Note 1050 Table 't1' already exists +DROP TABLES t1,t2; diff --git a/mysql-test/t/mdev19198.test b/mysql-test/t/mdev19198.test new file mode 100644 index 00000000000..19b45ed7510 --- /dev/null +++ b/mysql-test/t/mdev19198.test @@ -0,0 +1,15 @@ +CREATE TABLE t1 (c INT); +CREATE TABLE t2 (c INT); + +LOCK TABLES t1 WRITE, t2 READ; +CREATE TABLE IF NOT EXISTS t1 LIKE t2; +UNLOCK TABLES; + +LOCK TABLES t1 READ , t2 READ; +--error ER_TABLE_NOT_LOCKED_FOR_WRITE +CREATE TABLE IF NOT EXISTS t1 LIKE t2; +UNLOCK TABLES; + +CREATE TABLE IF NOT EXISTS t1 LIKE t2; + +DROP TABLES t1,t2; diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 2b26af6e9ba..3a3a903bc35 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -5615,11 +5615,18 @@ bool mysql_create_like_table(THD* thd, TABLE_LIST* table, /* Ensure that we have an exclusive lock on target table if we are creating non-temporary table. + If we're creating non-temporary table, then either + - there is an exclusive lock on the table + or + - there was CREATE IF EXIST, and the table was not created + (it existed), and was previously locked */ DBUG_ASSERT((create_info->tmp_table()) || thd->mdl_context.is_lock_owner(MDL_key::TABLE, table->db, table->table_name, - MDL_EXCLUSIVE)); + MDL_EXCLUSIVE) || + (thd->locked_tables_mode && pos_in_locked_tables && + create_info->if_not_exists())); } DEBUG_SYNC(thd, "create_table_like_before_binlog");