While troubleshooting deadlocks for a customer, I came around an interesting situation involving InnoDB gap locks. For a non-INSERT write operation where the WHERE clause does not match any row, I expected there should’ve been no locks to be held by the transaction, but I was wrong. Let’s take a look at this table and and example UPDATE.

mysql> SHOW CREATE TABLE preferences G *************************** 1. row *************************** Table: preferences Create Table: CREATE TABLE `preferences` ( `numericId` int(10) unsigned NOT NULL, `receiveNotifications` tinyint(1) DEFAULT NULL, PRIMARY KEY (`numericId`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) mysql> BEGIN; Query OK, 0 rows affected (0.00 sec) mysql> SELECT COUNT(*) FROM preferences; +----------+ | COUNT(*) | +----------+ | 0 | +----------+ 1 row in set (0.01 sec) mysql> UPDATE preferences SET receiveNotifications='1' WHERE numericId = '2'; Query OK, 0 rows affected (0.01 sec) Rows matched: 0 Changed: 0 Warnings: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 mysql > SHOW CREATE TABLE preferences G * * * * * * * * * * * * * * * * * * * * * * * * * * * 1. row * * * * * * * * * * * * * * * * * * * * * * * * * * * Table : preferences Create Table : CREATE TABLE ` preferences ` ( ` numericId ` int ( 10 ) unsigned NOT NULL , ` receiveNotifications ` tinyint ( 1 ) DEFAULT NULL , PRIMARY KEY ( ` numericId ` ) ) ENGINE = InnoDB DEFAULT CHARSET = latin1 1 row in set ( 0.00 sec ) mysql > BEGIN ; Query OK , 0 rows affected ( 0.00 sec ) mysql > SELECT COUNT ( * ) FROM preferences ; + -- -- -- -- -- + | COUNT ( * ) | + -- -- -- -- -- + | 0 | + -- -- -- -- -- + 1 row in set ( 0.01 sec ) mysql > UPDATE preferences SET receiveNotifications = '1' WHERE numericId = '2' ; Query OK , 0 rows affected ( 0.01 sec ) Rows matched : 0 Changed : 0 Warnings : 0

InnoDB status shows that this UPDATE holds an X lock on a PRIMARY index record:

---TRANSACTION 4A18101, ACTIVE 12 sec 2 lock struct(s), heap size 376, 1 row lock(s) MySQL thread id 3, OS thread handle 0x7ff2200cd700, query id 35 localhost msandbox Trx read view will not see trx with id >= 4A18102, sees < 4A18102 TABLE LOCK table `test`.`preferences` trx id 4A18101 lock mode IX RECORD LOCKS space id 31766 page no 3 n bits 72 index `PRIMARY` of table `test`.`preferences` trx id 4A18101 lock_mode X 1 2 3 4 5 6 -- - TRANSACTION 4A18101 , ACTIVE 12 sec 2 lock struct ( s ) , heap size 376 , 1 row lock ( s ) MySQL thread id 3 , OS thread handle 0x7ff2200cd700 , query id 35 localhost msandbox Trx read view will not see trx with id >= 4A18102 , sees < 4A18102 TABLE LOCK table ` test ` . ` preferences ` trx id 4A18101 lock mode IX RECORD LOCKS space id 31766 page no 3 n bits 72 index ` PRIMARY ` of table ` test ` . ` preferences ` trx id 4A18101 lock _ mode X

For the why, Heikki explained on this aging bug report, which makes sense and I understand the fix can be difficult, though at the same time a bit annoying I wished it was handled differently. To complete this post, let me demonstrate the deadlock situation I was telling about earlier mysql1 for first session and mysql2 for the second, the order of queries apply:

mysql1> BEGIN; Query OK, 0 rows affected (0.00 sec) mysql1> UPDATE preferences SET receiveNotifications='1' WHERE numericId = '1'; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql2> BEGIN; Query OK, 0 rows affected (0.00 sec) mysql2> UPDATE preferences SET receiveNotifications='1' WHERE numericId = '2'; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql1> INSERT INTO preferences (numericId, receiveNotifications) VALUES ('1', '1'); -- This one goes into LOCK WAIT mysql2> INSERT INTO preferences (numericId, receiveNotifications) VALUES ('2', '1'); ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 mysql1 > BEGIN ; Query OK , 0 rows affected ( 0.00 sec ) mysql1 > UPDATE preferences SET receiveNotifications = '1' WHERE numericId = '1' ; Query OK , 0 rows affected ( 0.00 sec ) Rows matched : 0 Changed : 0 Warnings : 0 mysql2 > BEGIN ; Query OK , 0 rows affected ( 0.00 sec ) mysql2 > UPDATE preferences SET receiveNotifications = '1' WHERE numericId = '2' ; Query OK , 0 rows affected ( 0.00 sec ) Rows matched : 0 Changed : 0 Warnings : 0 mysql1 > INSERT INTO preferences ( numericId , receiveNotifications ) VALUES ( '1' , '1' ) ; -- This one goes into LOCK WAIT mysql2 > INSERT INTO preferences ( numericId , receiveNotifications ) VALUES ( '2' , '1' ) ; ERROR 1213 ( 40001 ) : Deadlock found when trying to get lock ; try restarting transaction

So you see how easy it is to cause a deadlock, so make sure to avoid this situation – if a non-INSERT write operation is more likely to not match any row because the INSERT part is yet to come on the transaction, don’t do it or use REPLACE INTO or use READ-COMMITTED transaction isolation.