7.3 The Lock Family: S, U, X and Intent Locks
Locks are not taken only on rows. They are also taken on pages and on whole tables. A transaction that modifies one row only needs a row lock, but an operation that rebuilds an entire table must take a table lock. A problem arises when the two meet. The side that wants the table lock needs to know "is there a lock on any row of this table right now?", but it cannot check millions of rows one by one. The mechanism that solves this is the intent lock, and the whole set of lock types including intent locks is commonly called the lock family. Here we explain using SQL Server's names, and the correspondence in other DBMSs is summarized in a table at the end of the section.
The Three Basic Locks: S, U, X
- S (Shared lock): For reading. Any number of S locks can coexist.
- X (Exclusive lock): For writing. It coexists with no other lock.
- U (Update lock): Taken on a row that is "going to be read and then modified". It coexists with S but not with another U. When the decision to modify is made, it is converted to X.
U exists to prevent one classic deadlock. A statement such as UPDATE account SET ... WHERE balance > 1000000 reads rows one at a time, checks the condition, and modifies only the matching rows. Suppose it takes S during the read phase and converts to X during the modify phase. If two transactions run the same statement concurrently, both take S on the same row, both try to convert to X, and each waits for the other's S to be released. Two transactions trying to do the same thing end up in a deadlock (a lock conversion deadlock). U breaks this cycle. If U is taken instead of S in the read phase, the second transaction waits already in the read phase because U and U are incompatible, and the first transaction converts to X without being blocked and proceeds. Meanwhile, a plain read-only query takes S and so coexists with U. The design of U makes only the transactions that "intend to modify" wait for each other in order, without blocking transactions that only read.
The Three Intent Locks: IS, IU, IX
An intent lock is a lock placed in advance on a larger granularity (page, table) to indicate "somewhere below this, this kind of lock will be taken (or is being held)". A transaction that wants X on Hong Gildong's row first takes IX on the account table, then IX on the page containing the row, and finally X on the row. The order goes from top to bottom.
- IS (Intent Shared): Intends to take S on a lower granularity.
- IU (Intent Update): Intends to take U on a lower granularity.
- IX (Intent Exclusive): Intends to take X on a lower granularity.
Now an operation that wants S or X on the entire table does not have to scan millions of rows. If even one IX is held on the table, "some row somewhere is being modified", so table S must wait; if only IS is held, "something is being read somewhere", so table S is possible and table X is not. Checking a single table settles the decision.
It is common to add one more. SIX (Shared with Intent Exclusive) is a lock meaning "read the whole table while modifying some rows", combining table S and IX. It is used by operations that scan the whole table and modify only some of the matching rows.
Compatibility Matrix
When another transaction requests a lock on a resource that already has a lock, whether it can be taken immediately (○) or must wait (×) is determined by the following table. Rows are the requested lock, and columns are the lock already held.
| Requested ↓ / Held → | IS | S | U | IU | IX | SIX | X |
|---|---|---|---|---|---|---|---|
| IS | ○ | ○ | ○ | ○ | ○ | ○ | × |
| S | ○ | ○ | ○ | ○ | × | × | × |
| U | ○ | ○ | × | ○ | × | × | × |
| IU | ○ | ○ | ○ | ○ | ○ | ○ | × |
| IX | ○ | × | × | ○ | ○ | × | × |
| SIX | ○ | × | × | ○ | × | × | × |
| X | × | × | × | × | × | × | × |
Table 7-1. Lock compatibility matrix, based on SQL Server's lock modes.
NOTE The cells involving IU and SIX may differ by product and version, so confirm the actual decision against the compatibility matrix in the official documentation of the version you use.
Rather than memorizing the table, it is better to read it with three rules. First, X coexists with nothing. Second, intent locks (IS, IU, IX) coexist with one another. Even if two transactions both intend to "modify something somewhere", whether they modify the same row is sorted out at the row lock. Third, among real locks (S, U, X) at the same granularity, only reads coexist, and when a write is involved they do not. S cannot coexist with IX because if "reading the whole table" overlaps with "modifying a row somewhere", the read result is not consistent.
Reading Through Scenarios
Let us walk through four situations using the transfer example. We assume an isolation level that works by locking (for example, the default Read Committed of on-premises SQL Server).
| Situation | Lock taken first | Next request | Result |
|---|---|---|---|
| ① Transfer T1 is modifying Hong Gildong's row, and another transfer T3 modifies Park Younghee's row | Table IX, Hong Gildong row X | Table IX, Park Younghee row X | IX–IX is compatible at the table, and the rows differ so row X does not conflict either. Proceed concurrently |
| ② T1 is modifying Hong Gildong's row, and query T2 reads Hong Gildong's balance | Table IX, Hong Gildong row X | Table IS, Hong Gildong row S | IS–IX is compatible at the table, S–X is incompatible at the row. T2 waits on Hong Gildong's row |
| ③ T1 is modifying Hong Gildong's row, and an operation to rebuild the whole table starts | Table IX, Hong Gildong row X | Table X | X–IX is incompatible at the table. The rebuild waits until T1 ends, and new requests arriving in the meantime may also wait behind it |
| ④ Two transactions run the same-condition UPDATE concurrently | Table IX, row U being scanned | Table IX, same row U | U–U is incompatible at the row. The second waits and the first converts to X and proceeds. Had S been used, this would be a deadlock situation |
Situation ③ explains, in terms of locks, "why an ALTER on a large table stops the service" seen in Section 2.6. While the ALTER waits for the table exclusive lock, ordinary requests that would normally be compatible with one another end up in the lock queue behind the ALTER, so it looks as if one short operation blocks everything. This is why, in lock wait analysis, you should look for the transaction at the head of the queue that holds the lock, not the transaction at the end of the queue.
Differences Among DBMSs
| DBMS | Table-level locks | Row-level locks | Notes |
|---|---|---|---|
| SQL Server / Azure SQL | IS, IU, IX, S, U, X, SIX, etc. (the same scheme for the page level) | S, U, X | The original of the scheme described above. Schema locks (Sch-S, Sch-M) exist separately |
| MySQL (InnoDB) | Two intent locks, IS and IX, plus S and X | S, X. There are gap locks and next-key locks that lock the gaps between rows | No lock corresponding to U. UPDATE takes X directly on the rows it scans |
| PostgreSQL | Eight modes, from ACCESS SHARE to ACCESS EXCLUSIVE | Four: FOR KEY SHARE, FOR SHARE, FOR NO KEY UPDATE, FOR UPDATE | Names differ, but ROW SHARE≈IS, ROW EXCLUSIVE≈IX, SHARE≈S, ACCESS EXCLUSIVE≈X correspond. A plain SELECT takes no row lock |
| Oracle | TM lock: RS, RX, S, SRX, X | TX lock (recorded inside the block that contains the row) | RS≈IS, RX≈IX, SRX≈SIX correspond. A plain SELECT takes no row lock |
Learn more — SQL Server Locks and Lock Escalation, InnoDB Gap Locks and Next-Key Locks, PostgreSQL Lock Modes, Oracle TM and TX Locks
Table 7-2. Lock types by DBMS.
NOTE Lock names and detailed behavior may differ by version.
The names vary, but the structure is the same. All four DBMSs have a multi-level structure that marks intent at a large granularity and takes real locks at a small granularity. One important difference is that in PostgreSQL, Oracle, and SQL Server in MVCC mode, a plain query does not take row locks. In those environments, T2 waiting in scenario ② above does not happen. The reason is the MVCC material in Section 7.5.
Figure 7-3. Multi-level structure of locks
flowchart TB
subgraph L1 ["Level 1: table (account)"]
TBL["T1: holds IX<br/>T2: requests IS<br/>IS and IX are compatible, granted"]
end
subgraph L2 ["Level 2: page (page containing Hong Gildong's row)"]
PG["T1: holds IX<br/>T2: requests IS<br/>IS and IX are compatible, granted"]
end
subgraph L3 ["Level 3: row (Hong Gildong)"]
ROW["T1: holds X<br/>T2: requests S<br/>S and X are incompatible, T2 waits"]
end
TBL -->|"acquire locks top to bottom"| PG
PG -->|"acquire locks top to bottom"| ROW
The diagram draws, from top to bottom, the three granularities passed through when accessing one row. To modify Hong Gildong's row, the transfer T1 took locks in the order table IX → page IX → row X, and to read the same row, the query T2 requests locks in the order table IS → page IS → row S (scenario ②).
- Table level: T2 requests IS on the table where T1's IX is held. In the compatibility matrix, IS–IX is ○, so T2 obtains IS immediately.
- Page level: for the same reason, T2 obtains IS on the page too. Intent locks are merely marks saying "something will be done somewhere below", so they do not block each other.
- Row level: here, for the first time, real locks meet. The X held by T1 and the S requested by T2 are incompatible, so T2 waits on this row for T1 to end.
Conflict occurs only at the lowest level, where two transactions actually access the same resource. Had T2 read a row other than Hong Gildong's, there would have been no conflict at level 3 either. Conversely, an operation requesting table X, as in scenario ③, is immediately judged incompatible with T1's IX at level 1, so whether to wait is decided without scanning a single row.