Wednesday, September 14, 2016

InnoDB locks

Locks
======

InnoDB Lock Modes
-----------------------------
InnoDB implements standard row-level locking where there are two types of locks, shared (S) locks and exclusive (X) locks.
  - A shared (S) lock permits the transaction that holds the lock to read a row.
  - An exclusive (X) lock permits the transaction that holds the lock to update or delete a row.
 
If transaction T1 holds a shared (S) lock on row r, then requests from some distinct transaction T2 for a lock on row r are handled as follows:
  - A request by T2 for an S lock can be granted immediately. As a result, both T1 and T2 hold an S lock on r.
  - A request by T2 for an X lock cannot be granted immediately.
 
If a transaction T1 holds an exclusive (X) lock on row r, a request from some distinct transaction T2 for a lock of either type on r cannot be granted immediately.
Instead, transaction T2 has to wait for transaction T1 to release its lock on row r.  

Intention Locks
----------------------
Additionally, InnoDB supports multiple granularity locking which permits coexistence of record locks and locks on entire tables. To make locking at multiple granularity levels practical, additional types of locks called intention locks are used. Intention locks are table locks in InnoDB that indicate which type of lock (shared or exclusive) a transaction will require later for a row in that table.
There are two types of intention locks used in InnoDB (assume that transaction T has requested a lock of the indicated type on table t):
   - Intention shared (IS): Transaction T intends to set S locks on individual rows in table t.
   - Intention exclusive (IX): Transaction T intends to set X locks on those rows.
For example, SELECT ... LOCK IN SHARE MODE sets an IS lock and SELECT ... FOR UPDATE sets an IX lock.

The intention locking protocol is as follows:
   - Before a transaction can acquire an S lock on a row in table t, it must first acquire an IS or stronger lock on t.
   - Before a transaction can acquire an X lock on a row, it must first acquire an IX lock on t. b

Record Locks
-------------------
A record lock is a lock on an index record. For example, SELECT c1 FOR UPDATE FROM t WHERE c1 = 10; prevents any other transaction from inserting, updating, or deleting rows where the value of t.c1 is 10.

Record locks always lock index records, even if a table is defined with no indexes. For such cases, InnoDB creates a hidden clustered index and uses this index for record locking.

Gap Locks
-----------------
A gap lock is a lock on a gap between index records, or a lock on the gap before the first or after the last index record. For example, SELECT c1 FOR UPDATE FROM t WHERE c1 BETWEEN 10 and 20; prevents other transactions from inserting a value of 15 into column t.c1, whether or not there was already any such value in the column, because the gaps between all existing values in the range are locked.

A gap might span a single index value, multiple index values, or even be empty. Gap locks are part of the tradeoff between performance and concurrency, and are used in some transaction isolation levels and not others.

Gap locking can be disabled explicitly. This occurs if you change the transaction isolation level to READ COMMITTED

Auto-Inc Locks
---------------------
An AUTO-INC lock is a special table-level lock taken by transactions inserting into tables with AUTO_INCREMENT columns. In the simplest case, if one transaction is inserting values into the table, any other transactions must wait to do their own inserts into that table, so that rows inserted by the first transaction receive consecutive primary key values.

The innodb_autoinc_lock_mode configuration option controls the algorithm used for auto-increment locking. It allows you to choose how to trade off between predictable sequences of auto-increment values and maximum concurrency for insert operations.

Insert Intention Locks
--------------------------------
An insert intention lock is a type of gap lock set by INSERT operations prior to row insertion. This lock signals the intent to insert in such a way that multiple transactions inserting into the same index gap need not wait for each other if they are not inserting at the same position within the gap. Suppose that there are index records with values of 4 and 7. Separate transactions that attempt to insert values of 5 and 6, respectively, each lock the gap between 4 and 7 with insert intention locks prior to obtaining the exclusive lock on the inserted row, but do not block each other because the rows are nonconflicting.


References:
https://dev.mysql.com/doc/refman/5.6/en/innodb-locking.html

No comments:

Post a Comment