Bug #15136 | Deadlock on tables with foreign key reference | ||
---|---|---|---|
Submitted: | 22 Nov 2005 14:52 | Modified: | 23 Nov 2005 10:07 |
Reporter: | Maxim M | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: InnoDB storage engine | Severity: | S2 (Serious) |
Version: | 5.0.15-standard-log,4.1.15,possible all | OS: | Linux (RH AS3, AS4,possible all) |
Assigned to: | Bugs System | CPU Architecture: | Any |
[22 Nov 2005 14:52]
Maxim M
[22 Nov 2005 14:58]
Maxim M
Log of Mysql 5.0.15 with deadlock happens in Connection 6 (before SHOW ENGINE INNODB STATUS)
Attachment: mysql.log (application/octet-stream, text), 22.59 KiB.
[22 Nov 2005 14:59]
Maxim M
Java test file
Attachment: DatabaseDeadlockTest.java (text/x-java), 3.36 KiB.
[22 Nov 2005 15:04]
Maxim M
Example deadlock log (SHOW INNODB STATUS)
Attachment: deadlock.log (application/octet-stream, text), 6.83 KiB.
[22 Nov 2005 16:33]
Heikki Tuuri
Jan, please analyze the deadlock. I guess it comes from next-key locking. Maybe we could somehow optimize locking to avoid this deadlock? Regards, Heikki
[22 Nov 2005 17:04]
Jorge del Conde
I was able to reproduce this bug in 5.0.16/FC4 using the supplied test case
[23 Nov 2005 8:26]
Jan Lindström
TRANSACTION 0 1812, ACTIVE 0 sec, process no 9139, OS thread id 57359 inserting mysql tables in use 1, locked 1 LOCK WAIT 8 lock struct(s), heap size 1024, undo log entries 2 MySQL thread id 5, query id 154 localhost 127.0.0.1 root update INSERT INTO test1 (value, test_id_ref) VALUES (6, 3) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 0 page no 55 n bits 160 index `test_id_ref` of table `deadlock/test1` trx id 0 1812 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 8; hex 8000000000000004; asc ;; 1: len 8; hex 8000000000000004; asc ;; This transaction is waiting a X with insert intention to a gap. *** (2) TRANSACTION: TRANSACTION 0 1813, ACTIVE 0 sec, process no 9141, OS thread id 61456 setting auto-inc lock mysql tables in use 1, locked 1 5 lock struct(s), heap size 320, undo log entries 1 MySQL thread id 6, query id 155 localhost 127.0.0.1 root update INSERT INTO test1 (value, test_id_ref) VALUES (2, 4) *** (2) HOLDS THE LOCK(S): RECORD LOCKS space id 0 page no 55 n bits 160 index `test_id_ref` of table `deadlock/test1` trx id 0 1813 lock_mode X Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 8; hex 8000000000000004; asc ;; 1: len 8; hex 8000000000000004; asc ;; This transactions holds a X-lock to a record (granted for delete) *** (2) WAITING FOR THIS LOCK TO BE GRANTED: TABLE LOCK table `deadlock/test1` trx id 0 1813 lock mode AUTO-INC waiting *** WE ROLL BACK TRANSACTION (2) Transaction is waiting an auto-increment table lock which is granted to the transaction 1. Fundamental reason behind this deadlock is the next-key locking. Transaction 1 has requested a next-key lock for the index text-id-ref.
[23 Nov 2005 8:54]
Heikki Tuuri
Hi! Ok, this is a classic case of next-key locking versus predicate locking. The index test_id_ref is not unique. When trx (2) deleted all records with value 4, it had to lock also the 'gap' after the last record with value 3. That was to prevent further insertions with value 4 to the 'gap'. Predicate locking would remember that the delete ONLY operated on records with value 4, and it would allow an insertion with value 3. But next-key locking does not remember why we locked the gap. You can remove the deadlock problem (and next-key locking) with the my.cnf option: innodb_locks_unsafe_for_binlog but read carefully the warnings about that option. In MySQL-5.1 with 'row-based replication' (better name would be row-based binlogging), that option will be safe to use. I am setting this bug report to the state 'Not a bug'. Regards, Heikki INSERT INTO test1 (value, test_id_ref) VALUES (6, 3) *** (1) WAITING FOR THIS LOCK TO BE GRANTED: RECORD LOCKS space id 0 page no 55 n bits 160 index `test_id_ref` of table `deadlock/test1` trx id 0 1812 lock_mode X locks gap before rec insert intention waiting Record lock, heap no 5 PHYSICAL RECORD: n_fields 2; compact format; info bits 32 0: len 8; hex 8000000000000004; asc ;; 1: len 8; hex 8000000000000004; asc ;;
[23 Nov 2005 10:07]
Maxim M
Unfortunatly you wrong. This flag is set and i attached configurations file for DB
[23 Nov 2005 10:09]
Maxim M
MySql configuration file attached
Attachment: my.ini (application/octet-stream, text), 11.80 KiB.
[24 Nov 2005 8:22]
Jan Lindström
InnoDB startup option locks_unsafe_for_binlog does not affect on insert intention type locks. Thus if you have a lock on a record or a gap insert intention has to wait for it. Note that there is a X-lock on a record (value 4) in the second transaction in the deadlock. Regards, JanL