Sub topic 14
Multiple Granularity Locking and Intention Locks
This chapter assumes you know shared (S) and exclusive (X) locks, two-phase locking, and the idea of a lock compatibility matrix. It also assumes you can read a tree. The goal is to see why locking a single kind of object is not enough, and how intention locks fix that.
Why one granularity is not enough
Granularity is the size of the object a lock covers. A database can lock a whole database, a table (file), a page, or a single row (record).
- Fine granularity (rows) allows high concurrency, because transactions touching different rows never block each other. It costs more lock overhead. A transaction that scans a million rows would need a million locks.
- Coarse granularity (tables) has low overhead, since one lock covers everything. It blocks many transactions that do not actually conflict.
A real system needs both. A transaction that updates one row should lock a row. A transaction that scans a whole table should lock the table. The two kinds of transaction must still detect each other's conflicts.
The granularity hierarchy
Multiple granularity locking arranges the lockable objects in a tree. Locking a node implicitly locks all of its descendants in the same mode.
If a transaction holds an S lock on table F1, it can read every page and record under F1 without asking for more locks. If it holds an X lock on F1, it can read and write all of them.
The problem: detecting conflicts across levels
Suppose T1 holds an X lock on record r1. Now T2 asks for an S lock on table F1, which would implicitly cover r1. The lock manager must refuse T2. But the lock on F1 and the lock on r1 are stored on different nodes. How does the manager find the conflict?
Checking every descendant of F1 for locks would be slow. Checking only F1 would miss the conflict. Intention locks solve this by leaving a marker on every ancestor.
Intention lock modes
Before locking a node, a transaction first locks all of its ancestors with an intention mode. The intention mode says "I plan to take real locks lower down."
| Mode | Name | Meaning |
|---|---|---|
| IS | Intention shared | Some descendant will be locked in S mode |
| IX | Intention exclusive | Some descendant will be locked in X mode |
| S | Shared | The node and all descendants are readable |
| SIX | Shared and intention exclusive | The whole subtree is read, and some descendants will be locked in X mode |
| X | Exclusive | The node and all descendants are read and written |
SIX is the same as holding S and IX on the same node. It suits a transaction that scans a table but updates only a few rows.
The compatibility matrix
A requested mode can be granted only if it is compatible with every mode already held on that node by other transactions. A "Yes" means compatible.
| Requested \ Held | IS | IX | S | SIX | X |
|---|---|---|---|---|---|
| IS | Yes | Yes | Yes | Yes | No |
| IX | Yes | Yes | No | No | No |
| S | Yes | No | Yes | No | No |
| SIX | Yes | No | No | No | No |
| X | No | No | No | No | No |
Some patterns help you remember the table:
- IS conflicts only with X.
- IX is compatible with IS and IX only. Two writers in different subtrees can coexist.
- S is compatible with IS and S. It conflicts with IX because a writer lower down would change data the S lock claims to protect.
- SIX is compatible only with IS.
- X is compatible with nothing.
The locking protocol
A transaction Ti follows these rules.
- Ti must obey the lock compatibility matrix.
- Ti must lock the root of the tree first, in any mode.
- Ti can lock a node in S or IS mode only if it currently holds IS or IX on the parent.
- Ti can lock a node in X, SIX, or IX mode only if it currently holds IX or SIX on the parent.
- Ti can lock a node only if it has not previously unlocked any node (two-phase locking).
- Ti can unlock a node only if it holds no locks on any child of that node.
Rules 2 to 4 mean locks are acquired top-down, root to leaf. Rule 6 means locks are released bottom-up, leaf to root. In practice, strict variants hold everything until commit and then release all of it.
Worked example
Use the hierarchy above. Three transactions run concurrently.
- T1 updates record r1.
- T2 reads the whole table F1.
- T3 reads record r3.
T1 locks top-down: IX on DB, IX on F1, IX on P1, then X on r1.
T2 wants S on F1. It first takes IS on DB, which is compatible with T1's IX. It then requests S on F1. T1 holds IX on F1, and S conflicts with IX. T2 waits. The conflict was detected at F1 alone, without searching down to r1.
T3 takes IS on DB, IS on F1, IS on P2, then S on r3. IS is compatible with IX on DB and F1. P2 and r3 are untouched by T1. T3 proceeds immediately, so the fine granularity still pays off.
Here is the state of the lock table at that moment.
| Node | T1 | T2 | T3 |
|---|---|---|---|
| DB | IX | IS | IS |
| F1 | IX | waiting for S | IS |
| P1 | IX | ||
| P2 | IS | ||
| r1 | X | ||
| r3 | S |
A simple simulation
The following program implements the compatibility matrix and checks whether a request on one node can be granted.
COMPAT = {
("IS", "IS"): True, ("IS", "IX"): True, ("IS", "S"): True,
("IS", "SIX"): True, ("IS", "X"): False,
("IX", "IX"): True, ("IX", "S"): False, ("IX", "SIX"): False,
("IX", "X"): False,
("S", "S"): True, ("S", "SIX"): False, ("S", "X"): False,
("SIX", "SIX"): False, ("SIX", "X"): False,
("X", "X"): False,
}
def compatible(a, b):
return COMPAT.get((a, b), COMPAT.get((b, a)))
def can_grant(requested, held_modes):
return all(compatible(requested, h) for h in held_modes)
# T2 requests S on F1 while T1 holds IX and T3 holds IS
print(can_grant("S", ["IX", "IS"])) # False
# T3 requests IS on F1 while T1 holds IX
print(can_grant("IS", ["IX"])) # True
# A SIX request next to an IS holder
print(can_grant("SIX", ["IS"])) # TrueThe table stores each unordered pair once, and compatible looks up either order, so the matrix stays symmetric.
Common errors
- Taking the real lock without the intention locks on ancestors. Other transactions then cannot see the conflict at a higher level.
- Locking bottom-up. Rules 3 and 4 require the parent intention lock to exist first.
- Thinking IX blocks IX. It does not. Two transactions can both intend to write, as long as they write different descendants. A conflict appears only when they reach the same node with X.
- Forgetting that S conflicts with IX. This is the entry GATE questions most often test.
- Releasing a parent lock while still holding a child lock. Rule 6 forbids it.
Lock escalation
If a transaction accumulates too many fine locks, the system may replace them with one coarse lock. For example, after many row locks on a table, the system may take an S or X lock on the table and drop the row locks. Escalation reduces memory use. It can also block other transactions that previously worked on different rows, and it can create deadlocks when two transactions try to escalate on the same table.
Exercises
Exercise
T1 holds IX on table F1 and X on record r1 in page P1. T2 wants IS on F1, IS on page P2, and S on record r3 in P2. Can T2 proceed? What if T2 instead wants S on F1?
Show answer
The first request can proceed. IS is compatible with IX on F1, and T1 holds nothing on P2 or r3. The request for S on F1 cannot be granted, because S is not compatible with IX. T2 must wait until T1 releases its locks.
Exercise
A transaction scans table F1 and updates only a few records. Which single lock mode on F1 fits, and what other locks does it still need?
Show answer
SIX on F1. This gives S on the whole table, so the scan needs no per-row read locks. It also acts as IX, so the transaction takes IX on the pages it will update, and then X on those specific records. It also needs IX on the database root first.
Exercise
Using the compatibility matrix, list every mode that can be granted on a node where another transaction already holds SIX.
Show answer
Only IS. IX, S, SIX, and X all conflict with SIX.