[MDEV-18418] Assertion `mdl_ticket->m_type == MDL_SHARED_UPGRADABLE || mdl_ticket->m_type == MDL_SHARED_NO_WRITE || mdl_ticket->m_type == MDL_SHARED_NO_READ_WRITE' failed in MDL_context::upgrade_shared_lock Created: 2019-01-29  Updated: 2023-04-27

Status: Open
Project: MariaDB Server
Component/s: Locking
Affects Version/s: 10.1, 10.2, 10.3, 10.4
Fix Version/s: 10.4

Type: Bug Priority: Major
Reporter: Elena Stepanova Assignee: Alexey Botchkov
Resolution: Unresolved Votes: 0
Labels: regression


 Description   

--source include/have_innodb.inc
 
CREATE TABLE t1 (pk INT, f INT, PRIMARY KEY(pk)) ENGINE=InnoDB;
CREATE VIEW v1 AS SELECT * FROM t1;
LOCK TABLES t1 READ, v1 WRITE;
--error ER_TABLE_NOT_LOCKED_FOR_WRITE
ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f);
 
# Cleanup
UNLOCK TABLES;
DROP VIEW v1;
DROP TABLE t1;

10.1 ef0b91e

mysqld: /data/src/10.1/sql/mdl.cc:2313: bool MDL_context::upgrade_shared_lock(MDL_ticket*, enum_mdl_type, double): Assertion `mdl_ticket->m_type == MDL_SHARED_UPGRADABLE || mdl_ticket->m_type == MDL_SHARED_NO_WRITE || mdl_ticket->m_type == MDL_SHARED_NO_READ_WRITE' failed.
190129 20:39:54 [ERROR] mysqld got signal 6 ;
 
#7  0x00007fb854c45ee2 in __assert_fail () from /lib/x86_64-linux-gnu/libc.so.6
#8  0x0000562de6bb5ba5 in MDL_context::upgrade_shared_lock (this=0x7fb84b7c7168, mdl_ticket=0x7fb83ec570c0, new_type=MDL_SHARED_NO_WRITE, lock_wait_timeout=31536000) at /data/src/10.1/sql/mdl.cc:2311
#9  0x0000562de6b3c727 in mysql_alter_table (thd=0x7fb84b7c7070, new_db=0x7fb83ec43790 "test", new_name=0x0, create_info=0x7fb856cb7300, table_list=0x7fb83ec431a0, alter_info=0x7fb856cb7270, order_num=0, order=0x0, ignore=false) at /data/src/10.1/sql/sql_table.cc:9117
#10 0x0000562de6bae1d5 in Sql_cmd_alter_table::execute (this=0x7fb83ec43990, thd=0x7fb84b7c7070) at /data/src/10.1/sql/sql_alter.cc:330
#11 0x0000562de6a7c6c7 in mysql_execute_command (thd=0x7fb84b7c7070) at /data/src/10.1/sql/sql_parse.cc:5701
#12 0x0000562de6a80f6b in mysql_parse (thd=0x7fb84b7c7070, rawbuf=0x7fb83ec43088 "ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)", length=53, parser_state=0x7fb856cb85e0) at /data/src/10.1/sql/sql_parse.cc:7468
#13 0x0000562de6a6f7bc in dispatch_command (command=COM_QUERY, thd=0x7fb84b7c7070, packet=0x7fb84d6aa071 "ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)", packet_length=53) at /data/src/10.1/sql/sql_parse.cc:1496
#14 0x0000562de6a6e535 in do_command (thd=0x7fb84b7c7070) at /data/src/10.1/sql/sql_parse.cc:1124
#15 0x0000562de6ba9710 in do_handle_one_connection (thd_arg=0x7fb84b7c7070) at /data/src/10.1/sql/sql_connect.cc:1330
#16 0x0000562de6ba9474 in handle_one_connection (arg=0x7fb84b7c7070) at /data/src/10.1/sql/sql_connect.cc:1242
#17 0x0000562de6f677d6 in pfs_spawn_thread (arg=0x7fb84fbea670) at /data/src/10.1/storage/perfschema/pfs.cc:1861
#18 0x00007fb856949494 in start_thread (arg=0x7fb856cb9b00) at pthread_create.c:333
#19 0x00007fb854d0293f in clone () from /lib/x86_64-linux-gnu/libc.so.6
 
Query (0x7fb83ec43088): ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)
Connection ID (thread ID): 3
Status: NOT_KILLED

10.2 3fb6d25

mysqld: /data/src/10.2/sql/mdl.cc:2328: bool MDL_context::upgrade_shared_lock(MDL_ticket*, enum_mdl_type, double): Assertion `mdl_ticket->m_type == MDL_SHARED_UPGRADABLE || mdl_ticket->m_type == MDL_SHARED_NO_WRITE || mdl_ticket->m_type == MDL_SHARED_NO_READ_WRITE || mdl_ticket->m_type == MDL_SHARED_READ || mdl_ticket->m_type == MDL_SHARED_WRITE' failed.
190130  1:59:49 [ERROR] mysqld got signal 6 ;
 
#7  0x00007f7e517bbee2 in __assert_fail () from /lib/x86_64-linux-gnu/libc.so.6
#8  0x00005648e7df34ca in MDL_context::upgrade_shared_lock (this=0x7f7e00000c20, mdl_ticket=0x7f7e00155cb0, new_type=MDL_SHARED_NO_WRITE, lock_wait_timeout=86400) at /data/src/10.2/sql/mdl.cc:2324
#9  0x00005648e7d7046c in mysql_alter_table (thd=0x7f7e00000b00, new_db=0x7f7e00012b70 "test", new_name=0x0, create_info=0x7f7e4c091e50, table_list=0x7f7e00012560, alter_info=0x7f7e4c091da0, order_num=0, order=0x0, ignore=false) at /data/src/10.2/sql/sql_table.cc:9451
#10 0x00005648e7deb639 in Sql_cmd_alter_table::execute (this=0x7f7e00012d70, thd=0x7f7e00000b00) at /data/src/10.2/sql/sql_alter.cc:329
#11 0x00005648e7ca2252 in mysql_execute_command (thd=0x7f7e00000b00) at /data/src/10.2/sql/sql_parse.cc:6228
#12 0x00005648e7ca70b3 in mysql_parse (thd=0x7f7e00000b00, rawbuf=0x7f7e00012448 "ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)", length=53, parser_state=0x7f7e4c093200, is_com_multi=false, is_next_command=false) at /data/src/10.2/sql/sql_parse.cc:8015
#13 0x00005648e7c949ed in dispatch_command (command=COM_QUERY, thd=0x7f7e00000b00, packet=0x7f7e000963d1 "ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)", packet_length=53, is_com_multi=false, is_next_command=false) at /data/src/10.2/sql/sql_parse.cc:1826
#14 0x00005648e7c93344 in do_command (thd=0x7f7e00000b00) at /data/src/10.2/sql/sql_parse.cc:1379
#15 0x00005648e7de62cf in do_handle_one_connection (connect=0x5648ea49e9f0) at /data/src/10.2/sql/sql_connect.cc:1335
#16 0x00005648e7de605c in handle_one_connection (arg=0x5648ea49e9f0) at /data/src/10.2/sql/sql_connect.cc:1241
#17 0x00005648e820bf92 in pfs_spawn_thread (arg=0x5648ea3e4df0) at /data/src/10.2/storage/perfschema/pfs.cc:1862
#18 0x00007f7e53492494 in start_thread (arg=0x7f7e4c094700) at pthread_create.c:333
#19 0x00007f7e5187893f in clone () from /lib/x86_64-linux-gnu/libc.so.6

10.1/10.2 non-debug and 10.3/10.4 debug builds dosn't crash, but they fail with the error I wouldn't expect:

mysqltest: At line 7: query 'ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)' failed with wrong errno 1005: 'Can't create table `test`.`#sql-48ba_3` (errno: 150 "Foreign key constraint is incorrectly formed")', instead of 1099...

The failure appeared in 10.1 tree with this commit:

commit 6472c5c015a174ffd680fddc8f20c7a94684a552
Author: Alexey Botchkov
Date:   Sat Nov 3 14:24:15 2018 +0400
 
    MDEV-15890 Strange error message if you try to FLUSH TABLES <view> after LOCK TABLES <view>.
    
    LOCK view WRITE shouldn't block FLUSH view.
    So we set the view's mdl_request type to it's tables.



 Comments   
Comment by Alice Sherepa [ 2021-07-06 ]

currently 10.2 is still failing, while 10.3-10.6 return "ERROR HY000: Can't create table `test`.`t1` (errno: 150 "Foreign key constraint is incorrectly for"

10.2 c22f7f2323d6f823c9340

#5  0x00007ff4dd399859 in __GI_abort () at abort.c:79
#6  0x00007ff4dd399729 in __assert_fail_base (fmt=0x7ff4dd52f588 "%s%s%s:%u: %s%sAssertion `%s' failed.\n%n", assertion=0x563067eb8b88 "mdl_ticket->m_type == MDL_SHARED_UPGRADABLE || mdl_ticket->m_type == MDL_SHARED_NO_WRITE || mdl_ticket->m_type == MDL_SHARED_NO_READ_WRITE || mdl_ticket->m_type == MDL_SHARED_READ || mdl_ticket->m_typ"..., file=0x563067eb8170 "/10.2/src/sql/mdl.cc", line=2324, function=<optimized out>) at assert.c:92
#7  0x00007ff4dd3aaf36 in __GI___assert_fail (assertion=0x563067eb8b88 "mdl_ticket->m_type == MDL_SHARED_UPGRADABLE || mdl_ticket->m_type == MDL_SHARED_NO_WRITE || mdl_ticket->m_type == MDL_SHARED_NO_READ_WRITE || mdl_ticket->m_type == MDL_SHARED_READ || mdl_ticket->m_typ"..., file=0x563067eb8170 "/10.2/src/sql/mdl.cc", line=2324, function=0x563067eb8b38 "bool MDL_context::upgrade_shared_lock(MDL_ticket*, enum_mdl_type, double)") at assert.c:101
#8  0x00005630675483ac in MDL_context::upgrade_shared_lock (this=0x7ff480000eb0, mdl_ticket=0x7ff48012e4c0, new_type=MDL_SHARED_NO_WRITE, lock_wait_timeout=86400) at /10.2/src/sql/mdl.cc:2324
#9  0x00005630674c1d8b in mysql_alter_table (thd=0x7ff480000d90, new_db=0x7ff480012e38 "test", new_name=0x0, create_info=0x7ff4d8076250, table_list=0x7ff480012818, alter_info=0x7ff4d80761a0, order_num=0, order=0x0, ignore=false) at /10.2/src/sql/sql_table.cc:9735
#10 0x000056306754060c in Sql_cmd_alter_table::execute (this=0x7ff480013038, thd=0x7ff480000d90) at /10.2/src/sql/sql_alter.cc:333
#11 0x00005630673edc30 in mysql_execute_command (thd=0x7ff480000d90) at /10.2/src/sql/sql_parse.cc:6017
#12 0x00005630673f2a96 in mysql_parse (thd=0x7ff480000d90, rawbuf=0x7ff480012708 "ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)", length=53, parser_state=0x7ff4d8077560, is_com_multi=false, is_next_command=false) at /10.2/src/sql/sql_parse.cc:7793
#13 0x00005630673e0cf1 in dispatch_command (command=COM_QUERY, thd=0x7ff480000d90, packet=0x7ff480008b61 "ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f)", packet_length=53, is_com_multi=false, is_next_command=false) at /10.2/src/sql/sql_parse.cc:1827
#14 0x00005630673df7ec in do_command (thd=0x7ff480000d90) at /10.2/src/sql/sql_parse.cc:1381
#15 0x000056306753b001 in do_handle_one_connection (connect=0x56306ad4a8d0) at /10.2/src/sql/sql_connect.cc:1336
#16 0x000056306753ad66 in handle_one_connection (arg=0x56306ad4a8d0) at /10.2/src/sql/sql_connect.cc:1241
#17 0x0000563067d673ca in pfs_spawn_thread (arg=0x56306ad34e80) at /10.2/src/storage/perfschema/pfs.cc:1869
#18 0x00007ff4dd8bb609 in start_thread (arg=<optimized out>) at pthread_create.c:477
#19 0x00007ff4dd496293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95

Comment by Vladislav Vaintroub [ 2022-07-08 ]

"Foreign key constraint is incorrectly formed" is a good error. See
With table created like this

CREATE TABLE t1 (pk INT, f INT, PRIMARY KEY(pk)) ENGINE=InnoDB;

statement

ALTER TABLE t1 ADD FOREIGN KEY (pk) REFERENCES t1 (f);

is invalid, as foreign-key-to-be does not reference an indexed column in the "foreign table", which innodb correctly rejects

Was perhaps

ALTER TABLE t1 ADD FOREIGN KEY (f) REFERENCES t1 (pk);

intented instead?

If this was intented, in 10.3+, this test would succeed

--source include/have_innodb.inc
 
CREATE TABLE t1 (pk INT, f INT, PRIMARY KEY(pk)) ENGINE=InnoDB;
CREATE VIEW v1 AS SELECT * FROM t1;
LOCK TABLES t1 READ, v1 WRITE;
ALTER TABLE t1 ADD FOREIGN KEY (f) REFERENCES t1 (pk);
# Cleanup
UNLOCK TABLES;
DROP VIEW v1;
DROP TABLE t1;

Would this be fine? Feel free to reassign if not.

Comment by Elena Stepanova [ 2022-07-08 ]

wlad,

By "error I wouldn't expect" I meant that in the given circumstances (read lock on the table, write lock on the view) I expected a different error, ER_TABLE_NOT_LOCKED_FOR_WRITE, which apparently was happening before the commit mentioned in the description. But maybe it was done intentionally and I just misinterpreted the intention. If so, and 10.3+ behaves as expected, feel free to close since 10.2 is now EOL.

Without the locks, sure, it was and is ER_CANT_CREATE_TABLE / Foreign key constraint is incorrectly formed.

Comment by Vladislav Vaintroub [ 2022-07-08 ]

Ok, since neither I nor elenst knows for sure whether ER_TABLE_NOT_LOCKED_FOR_WRITE is expected or now, maybe holyfoot does It looks fine to me if there is no error, but maybe I miss some historical importance of the former error. Please free to close the bug, if the example in my previous entry works as expected.

Generated at Thu Feb 08 08:43:57 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.