InnoDB锁冲突案例演示(续)

时间:2025-01-31 12:05:44
Preface
 
    I've demontstrated several InnoDB locking cases in my previous blog.I'm gonna do the rest tests about InnoDB locks.
 
Procedure
 
Test table information and the relevant variables.
 zlm@192.168.56.100: [zlm]>show create table t1\G
*************************** . row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`c1` int() unsigned NOT NULL DEFAULT '',
`c2` int() unsigned NOT NULL DEFAULT '',
`c3` int() unsigned NOT NULL DEFAULT '',
`c4` int() unsigned NOT NULL DEFAULT '',
PRIMARY KEY (`c1`),
KEY `c2` (`c2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
row in set (0.00 sec) zlm@192.168.56.100: [zlm]>select * from t1;
+----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.01 sec) zlm@192.168.56.100: [zlm]>select @@transaction_isolation;
+-------------------------+
| @@transaction_isolation |
+-------------------------+
| REPEATABLE-READ |
+-------------------------+
row in set (0.00 sec) zlm@192.168.56.100: [(none)]>show variables like 'innodb_status_output_locks';
+----------------------------+-------+
| Variable_name | Value |
+----------------------------+-------+
| innodb_status_output_locks | ON |
+----------------------------+-------+
row in set (0.00 sec)

Test 1: session1 update while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c4= where c2>=;select * from t1 where c2>=;
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Lock information of session1.
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index c2 of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000000a; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd28; asc - (;;
: len ; hex 3a0000012727bb; asc : '' ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd28; asc - (;;
: len ; hex 3a000001272799; asc : '' ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Session2 was block because of the gap lock(c2>=4 hold a supremum lock) which was holed by session 1.The value c2=5 which session2 want to insert is conficted with the range lock.

Test 2: session1 update while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c4= where c2>=;select * from t1 where c2>=;
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //This time the transaction in session2 was committed immediately.The value c2=2 didn't conflict with the range lock in session1.

Test 3: session1  update while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c4= where c1>=;select * from t1 where c1>=;
Query OK, rows affected (0.00 sec) Query OK, rows affected (0.00 sec)
Rows matched: Changed: Warnings: +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
Trx read view will not see trx with id >= , sees <
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd2f; asc - /;;
: len ; hex 3e0000012d180e; asc > - ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd2f; asc - /;;
: len ; hex 3e0000012d1830; asc > - ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd2f; asc - /;;
: len ; hex 3e0000012d1852; asc > - R;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Locks information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd63; asc - c;;
: len ; hex 400000012f17f3; asc @ / ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd63; asc - c;;
: len ; hex 400000012f17f3; asc @ / ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Locks information of session2.
Trx id counter
Purge done for trx's n:o < 2997603 undo n:o < 0 state: running but idle
History list length
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION , not started
lock struct(s), heap size , row lock(s)
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //locks information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd68; asc - h;;
: len ; hex 4400000da22824; asc D ($;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd68; asc - h;;
: len ; hex 4400000da22824; asc D ($;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Only c1=5 in session2 can be executed immediately.Because c1=5 didn't conflict with the range lock.

Test 4: session1 select for update while session2 delete.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1<= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Locks information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690110; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a700000269011d; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a700000269012a; asc i *;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690137; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex 470000025802f4; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;delete from t1 where c1=;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //The lock session2 requested for where c1=6 was locked either.So it was blocked.Even though primary key does not contains the gap lock,but when the condition contains a range like c1<=4,it will block the next-key of c1=6 here.

Test 5: session1 select for update while session2 delete.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1>= for update;
Query OK, rows affected (0.00 sec) +----+----+----+----+
| c1 | c2 | c3 | c4 |
+----+----+----+----+
| | | | |
| | | | |
| | | | |
| | | | |
+----+----+----+----+
rows in set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690137; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 73757072656d756d; asc supremum;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex 470000025802f4; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex 0000000a; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>delete from t1 where c1=;
Query OK, row affected (0.00 sec) //Session2 was not blocked this time.Because c1=3 was not in the gap lock here.

Test 5: session1 update while session2 select for update.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;update t1 set c1= where c1=;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Rows matched: Changed: Warnings: //lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd94; asc - ;;
: len ; hex 6000000dab26f6; asc ` & ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session1 holded only 1 record lock with no gap of record c1=4. zlm@192.168.56.100: [zlm]>begin;select * from t1 where c2= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec fetching rows
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm Sending data
select * from t1 where c2= for update
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd94; asc - ;;
: len ; hex 6000000dab26f6; asc ` & ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a7000002690110; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbb07; asc - ;;
: len ; hex a700000269011d; asc i ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd8b; asc - ;;
: len ; hex db0000019f0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd94; asc - ;;
: len ; hex 6000000dab26f6; asc ` & ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2 holded 4 record locks on c1 but only c1=4 was conflicted with session1.It was still blocked.
//I'm a little baffled why the session2 holded so many record locks on the primary key column(c1=0,c1=1,c1=3).

Test 6: session1 delete then insert while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;delete from t1 where c1=;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd9b; asc - ;;
: len ; hex 64000001b9298c; asc d ) ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Session2 did not conflict with the "X" record locks holded by session1.So it was executed immediately.Because there was no gap lock on c1 column(primary key).

Test 7: session1 insert while session2 insert.

 //Session1:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX //Notice that the c1=9 is not exist in primary key.It only generate a "IX" lock(intention lock). //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.01 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbda2; asc - ;;
: len ; hex e8000001b00110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock mode S waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbda2; asc - ;;
: len ; hex e8000001b00110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbda2; asc - ;;
: len ; hex e8000001b00110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2 was waiting for the "S" record lock and it also request for "X" record lock on record c1=9 in primary key.Although the record c1=9 is not exist in primary key,but they were inserting into the same row.As the session2 cannot get the lock for inserting.It was blocked.

Test 8: session1 select for update while session2 insert.(modify the same row)

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session1 holded a gap lock(or we can call it next-key lock either) of c1=8.Even though the record of c1=7 was not exist. //Session2:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec inserting
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm executing
insert into t1 select ,,,
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec insert intention waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2 generated a insert intention lock and request for the next-key lock holded by session1.So it blocked.

Test 9: session1 insert while session2 select for update.(modify the same row)

 //Session1:
zlm@192.168.56.100: [zlm]>begin;insert into t1 select ,,,;
Query OK, rows affected (0.00 sec) Query OK, row affected (0.00 sec)
Records: Duplicates: Warnings: //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX //Session1 generated only a "IX" lock. //Session2:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) ERROR (HY000): Lock wait timeout exceeded; try restarting transaction //Lock information of session2.
---TRANSACTION , ACTIVE sec starting index read
mysql tables in use , locked
LOCK WAIT lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm statistics
select * from t1 where c1= for update
------- TRX HAS BEEN WAITING SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbdaa; asc - ;;
: len ; hex ee000001bd0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; ------------------
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap waiting
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbdaa; asc - ;;
: len ; hex ee000001bd0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s), undo log entries
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks rec but not gap
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbdaa; asc - ;;
: len ; hex ee000001bd0110; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //when session2 intended to update the same row,session1 generated a "X" record lock.Then session2 had to wait for the release of the lock by session1.It was blocked.That means InnoDB adds locks only if it detects multiple concurrent transactions are modifying the same rows no mater whether the record is exist or not.

Test 10: session1 select for update while session2 select for update.(modify the same row)

 //Session1:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //Lock information of session1.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session2:
zlm@192.168.56.100: [zlm]>begin;select * from t1 where c1= for update;
Query OK, rows affected (0.00 sec) Empty set (0.00 sec) //Lock information of session2.
---TRANSACTION , ACTIVE sec
lock struct(s), heap size , row lock(s)
MySQL thread id , OS thread handle , query id zlm1 192.168.56.100 zlm
TABLE LOCK table `zlm`.`t1` trx id lock mode IX
RECORD LOCKS space id page no n bits index PRIMARY of table `zlm`.`t1` trx id lock_mode X locks gap before rec
Record lock, heap no PHYSICAL RECORD: n_fields ; compact format; info bits
: len ; hex ; asc ;;
: len ; hex 0000002dbd6c; asc - l;;
: len ; hex ; asc G X ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;;
: len ; hex ; asc ;; //Session1 together with session2 both hold the gap lock(next-key lock) of c1=8.Because gap lock does not block each other.They are actually coexistent util there's a actual inserting operation.Therefore,session2 was not blocked in the case.
Summary
  • In RR transaction isolation level,the InnoDB locking seems more complicated than that in RC transaction isolation level.
  • InnoDB gap locks in differtent transactions are compatible only if the insert intention appears.
  • Generally,primary index and unique index does not generate gap locks like secondary index does.The exception is that when the condition contains a range of scanned indexes.The gap lock will appear according to the range of condition of query.
  • As for the "select ... for update" statement,if the records are in table,it adds "LOCK_REC_NOT_GAP"(secondary index is "LOCK_ORDINARY") otherwise it adds "LOCK_GAP".