Talk about your understanding of gap lock

Gap lock

Introduction

Gap lock is a lock on a certain range of database tables. It can form a next-key lock with row locks to solve the phantom read problem generated under the repeatable read transaction isolation level. A phantom read is when a transaction performs a range query, the results of the query before and after are different.

in principle

To record the rules that MySQL locks under the repeatable read transaction isolation level:

  1. The basic unit of locking is next-key lock, which is the principle of opening before closing and closing.
  2. Objects accessed during the query will increase the lock
  3. Equivalent query on the index-when the unique index is locked, next-key lock will be upgraded to row lock
  4. Equivalent query on the index-when traversing to the right, if the last value does not meet the query requirements, the next-key lock will be reduced to a gap lock
  5. The unique index will access to the first value that does not meet the condition during range query

Explain the above principles through cases

先声明一下,下面的这些案例都是基于可重复读事务隔离级别的

Data introduction

Suppose now I have several pieces of data:

step id (unique index) c (normal index) d (no index)
1 1 1 1
2 5 5 5
3 10 10 10
4 15 15 15

In order to solve the problem of phantom reading, the above data is not only locked for each row of data mentioned above, but also locked for the middle value range when updating. This lock is what we call gap lock. In this way, row lock + gap lock constitute next-key lock, the information of this lock is: (-°,1],(1,5],(5,10),(10,15],(15,+supernum) ], (Among them, supernum is the maximum value maintained by the MySQL database to ensure that the next-key lock is the principle of opening left and right closing)

Case

Case 1: Simple case of gap lock

step Transaction A Transaction B
1 begin;
select * from table where id = 3 for update
2 - insert into tabble value(4,4,4)
block;
3 commit; -

This is a simple case, let’s analyze his process:

  • When there are the above two transactions, the operation of transaction A will add a (1,5) range lock to the database. At this time, transaction B cannot insert data and is in a blocking state.

Case 2: Gap lock deadlock problem

step Transaction A Transaction B
1 begin;
select * from table where id = 3 for update
2 - begin;
select * from table where id = 4 for update
3 - insert into table value(2,2,2)
block;
3 nsert into table value(2,2,2)
block;
-

Unlike write locks which are mutually exclusive, gap locks are not mutually exclusive. Both transactions can obtain this gap lock, but after obtaining this gap lock, other transactions are not allowed to perform DDL operations on this gap lock, so two Deadlock

Case 3: Equivalent query-unique index

step Transaction A Transaction B Transaction C
1 begin;
update table set d=d+1 where id = 7
- -
2 - insert into table value(8,8,8)
block;
-
3 - - update table set d=d+1 where id=10

1. Let’s take a look at what range of locks MySQL will generate for the database: (5,10)
2. According to principle 4 above, we see that the last value of this range 10 is not equal to the id=7 we want to check. This next-key lock will be degenerated into a gap lock (5, 10);
3. So it can be explained that the insert operation performed by transaction B is blocked, while the operation of transaction C is not blocked.

Case 4: Equivalent query-ordinary index

step Transaction A Transaction B Transaction C
1 begin;
select id from table where c=5 lock in share mode
- -
2 - update table set d=d+1 where id=5 -
3 - - insert into table values(7,7,7)
block;

1、先来看看MySQL会给数据库生成什么范围的锁:(0,5],(5,10]
2、由于查询是等值查询,并且最后一个值不满足查询要求,故退化为间隙锁(5,10)
3、事务B能正常执行不阻塞的原因就是,事务A走的是覆盖索引并没有对主键索引加锁,所以B能正常执行
4、事务C插入的信息是7,在(5,10)之间,故会阻塞。

Guess you like

Origin blog.csdn.net/MarkusZhang/article/details/108379193