Lecture: Lecture 17 - Concurrency Control
Related: Two-Phase Locking and Strict 2PL, Concurrency Anomalies and Serializability Theory
Lock granularity and hierarchical locking (Slides 58–69)
Database locks can be acquired across multiple granularity levels arranged in a tree (Slides 59–60):
Choosing one fixed granularity creates a tradeoff (Slide 69):
- Too coarse (e.g., always locking the entire table): reduces concurrency and increases conflict likelihood.
- Too fine (e.g., locking thousands of tuples individually for a bulk scan): creates heavy overhead for the lock manager.
- Ideal matching: Localized reads/updates on a few objects use fine granularity (lower tree levels), while massive reads/updates use coarse granularity (higher tree levels).
Locking primitives and intention locks (Slides 61–63)
To let one transaction lock a single tuple while another checks whether it can lock the whole table—without scanning every tuple lock—hierarchical locking introduces intention locks as parent-level signals:
| Primitive | Meaning |
|---|---|
SL (Shared Lock) | Shared read access on the current node and all its descendants |
XL (eXclusive Lock) | Exclusive write access on the current node and all its descendants |
ISL (Intention of Shared Lock) | Signals intention to place SL on one or more descendant nodes |
IXL (Intention of eXclusive Lock) | Signals intention to place XL on one or more descendant nodes |
SIXL (Shared + Intention of eXclusive Lock) | Holds SL on the current node plus IXL for one or more descendant nodes |
Request protocol (Slide 64)
- Acquire top-down: Locks are always requested starting from the tree root (
DB) downward. - Release bottom-up: Locks are released starting from the smallest-granularity locked node upward.
- To request
SLorISLon a node, the transaction must holdISLorIXLon its parent node. - To request
XL,IXL, orSIXLon a node, the transaction must holdIXLorSIXLon its parent node.
Compatibility matrix and precedence (Slides 66–68)
| Request \ Held State | ISL | IXL | SL | SIXL | XL |
|---|---|---|---|---|---|
ISL | Ok | Ok | Ok | Ok | No |
IXL | Ok | Ok | No | No | No |
SL | Ok | No | Ok | No | No |
SIXL | Ok | No | No | No | No |
XL | No | No | No | No | No |
Notice how ISL and IXL are mutually compatible: two transactions updating or reading different rows under the same table can pass through the table node with ISL/IXL and resolve conflicts (if any) at the specific fragment or tuple below. The lock precedence (strength) graph orders strengths from strongest to weakest as XL → SIXL → (SL, IXL) → ISL (Slide 68).
Predicate locking: stopping Ghost Update (b) (Slide 70)
Standard tuple-level 2PL cannot prevent Ghost Update (b) (inserting a new row that satisfies an active WHERE predicate), because a read operation only locks existing tuples and a row that does not exist yet cannot be locked in advance.
- Predicate locking locks all data satisfying a given predicate (for example, all employees in department
x, including future inserts matching that condition). - Real-system implementation: Implemented in real DBMSs by locking indices (range/key-range locks on the index entries matching the predicate).
Isolation levels in the SQL2 standard (Slides 71–74)
In SQL2, transactions are either READ WRITE (default) or READ ONLY (no data or schema modifications allowed; shared locks suffice) (Slide 71). A transaction specifies its behavior via (Slide 74):
SET TRANSACTION
[ISOLATION LEVEL <IsolationLevel>]
[READ ONLY | READ WRITE]Crucial SQL2 rule (Slide 74): The isolation level may be reduced ONLY for read operations. Write operations are ALWAYS executed under Strict 2PL with exclusive locks.
| Isolation Level (Strongest → Weakest) | Read locking behavior | Anomalies prevented / allowed |
|---|---|---|
SERIALIZABLE (Slide 72) | Strict 2PL + predicate locking | Highest isolation; prevents all anomalies, including Ghost Update (b). |
REPEATABLE READ (Slide 72) | Strict 2PL without predicate locking | Reads of existing objects can be repeated safely (no Dirty Reads, Inconsistent Reads, or Ghost Update (a)), but NO protection against Ghost Update (b)—recomputing aggregates can reflect newly inserted rows. |
READ COMMITTED (Slide 73) | Not 2PL: read lock is released immediately as soon as the object is read | Prevents Dirty Reads (never reads uncommitted writes), but allows Inconsistent Reads and Ghost Updates. |
READ UNCOMMITTED (Slide 73) | Not 2PL: reads data without acquiring any read lock | Allows Dirty Reads (and all weaker anomalies); allowed only for READ ONLY transactions. |
Common confusion
- Does
READ UNCOMMITTEDorREAD COMMITTEDdisable write locks? No! Write operations always use Strict 2PL with exclusive locks (W-Lockheld untilCOMMIT/ROLLBACK) at every isolation level (Slide 74). Only read locks are relaxed when lowering the isolation level. - Why does
REPEATABLE READfail onAVG(Salary)orSUM(Balance)? Existing rows stay locked until commit (so existing rows cannot be updated), but without predicate locking, another transaction canINSERTa brand-new matching row (Ghost Update (b)).
Retrieval questions
1. A transaction wants to read a whole table (SL) while selectively updating a few tuples inside it (XL on those tuples). Which single lock should it hold on the table node?
SIXL (Shared lock on the table + Intention of eXclusive Lock on descendant tuples).
2. Under REPEATABLE READ, a report computes SUM(Balance) for 'Torino' accounts twice. Can another transaction update an existing Torino balance in between? Can it insert a new Torino account? How do you prevent both?
It cannot update an existing Torino account (held under Strict 2PL shared read locks), but it can insert a new Torino account (Ghost Update (b)) because REPEATABLE READ lacks predicate locking. Switching to SERIALIZABLE adds predicate locking (implemented via index locking) and prevents the insert too.
3. Why does SQL2 allow READ UNCOMMITTED only for READ ONLY transactions?
Because READ UNCOMMITTED skips read locks entirely (allowing dirty reads), while SQL2 mandates that any transaction performing writes must use Strict 2PL exclusive write locks to preserve database consistency.