This is a very classic scenario, a search on the Internet for deadlock caused by the insertion of intent lock, most will point to this example
Table structure
CREATE TABLE `t1` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a` varchar(5),
`b` varchar(5),
PRIMARY KEY (`id`),
UNIQUE KEY `uk_name` (`a`,`b`)
);
Copy the code
Insert ignore into T1 (a, B)values(“1”, “1”);
The steps are as follows:
t1 | t2 | t3 | note |
---|---|---|---|
begin | begin | begin | |
insert | successful | ||
insert | T1’s implicit lock is promoted to X lock, t2 enters S lock wait | ||
insert | T3 enters S lock and waits | ||
rollback; | After t1 is rolled back, the X lock is released, and T2 and T3 both acquire the S lock | ||
ok | deadlock | Both T2 and T3 want to insert the intent lock X lock, causing a deadlock condition |
The deadlock log is as follows
------------------------
LATEST DETECTED DEADLOCK
------------------------
181101 23:22:59
*** (1) TRANSACTION:
TRANSACTION 5032, ACTIVE 11 sec inserting
mysql tables in use 1, locked 1
LOCK WAIT 4 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 5, OS thread handle 0x70000d736000, query id 125 localhost root update
insert ignore into t1(a, b)values("1"."1")
*** (1) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 56 page no 4 n bits 584 index `uk_name` of table `d1`.`t1` trx id 5032 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 139 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 3; hex 313031; asc 101;;
1: len 3; hex 313031; asc 101;;
2: len 4; hex 800007b1; asc ;;
*** (2) TRANSACTION:
TRANSACTION 5033, ACTIVE 6 sec inserting
mysql tables in use 1, locked 1
4 lock struct(s), heap size 1248, 2 row lock(s), undo log entries 1
MySQL thread id 6, OS thread handle 0x70000d779000, query id 126 localhost root update
insert ignore into t1(a, b)values("1"."1")
*** (2) HOLDS THE LOCK(S):
RECORD LOCKS space id 56 page no 4 n bits 584 index `uk_name` of table `d1`.`t1` trx id 5033 lock mode S locks gap before rec
Record lock, heap no 139 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 3; hex 313031; asc 101;;
1: len 3; hex 313031; asc 101;;
2: len 4; hex 800007b1; asc ;;
*** (2) WAITING FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 56 page no 4 n bits 584 index `uk_name` of table `d1`.`t1` trx id 5033 lock_mode X locks gap before rec insert intention waiting
Record lock, heap no 139 PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 3; hex 313031; asc 101;;
1: len 3; hex 313031; asc 101;;
2: len 4; hex 800007b1; asc ;;
*** WE ROLL BACK TRANSACTION (2)
Copy the code
This is where we first mentioned insert Intention lock.
Insert intention lock It is necessary to strengthen the mutex X lock for the modification and deletion of existing data rows. Is it necessary to add such a strong lock for data insertion to implement mutex? Insert intent lock, gestation and birth. Insert intent Locks are a type of Gap Locks that are specific to INSERT operations and are one of the few cases where Gap Locks occur at the RC level