4.4.19 · D3Databases

Worked examples — Locking — shared, exclusive, intent locks

4,078 words19 min readBack to topic

This page is the exhaustive drill. The parent note taught the rules; here we hunt down every case class a lock-compatibility question can throw at you and work each one to the ground. If you can answer every cell below, no exam question on locking can surprise you.

Before we start, one reminder of the alphabet we already earned, so you never meet a symbol unexplained:

Recall The lock modes used on this page (one-line reminder each)
  • S — shared, "I want to read." Many S together are fine.
  • X — exclusive, "I want to write." Nobody else on that item.
  • IS — intent-shared, a flag on the table saying "I hold an S on some row below."
  • IX — intent-exclusive, a flag saying "I hold an X on some row below."
  • SIX — S on the whole node plus IX for a few children ("read all, write some").
  • U (update lock) — a special lock one transaction takes when it reads a row intending to soon write it. Only one holder allowed (like X), but it still lets other readers keep their S until the writer actually upgrades. It exists purely to prevent the deadlock — we build that deadlock and then apply U in Ex 11.

The scenario matrix

Every locking question is really one of these case classes. We will hit each one at least once.

Case class What varies Example that covers it
Base compatible S vs S on same item Ex 1
Base conflict S vs X on same item Ex 2
Two writers, same item X vs X on same item Ex 3
Two writers, different items IX + IX, disjoint rows Ex 4
Intent vs full lock IX (existing) vs S (requested) Ex 5
SIX combination scan-then-update Ex 6
Intent-shared vs whole-table X IS (existing) vs X (requested) Ex 7
Degenerate / zero input empty table, no rows to lock Ex 8
Phantom / range edge case insert of a future matching row Ex 9
Limiting / starvation many S vs one waiting X Ex 10
Real-world word problem bank transfer, two accounts Ex 11
Exam twist + U lock lock upgrade S → X, deadlock & the U fix Ex 12

The compatibility matrix is our single source of truth. Read a cell as "Existing (row) vs Requested (column)":

Figure — Locking — shared, exclusive, intent locks

Keep this picture in view — every example below is just pointing at one square of it.


The worked examples

Figure — Locking — shared, exclusive, intent locks
Figure — Locking — shared, exclusive, intent locks

Which cell decides Ex 3, and the wrong answer?
Cell (X,X)=❌; ignoring it gives the lost update, final x=11 instead of 12.
Which is the only conflict in the IS row (Ex 7)?
IS vs X — an intent-shared reader below blocks a whole-table exclusive lock; IS is compatible with IS, IX, S, and SIX.
How many locks on the empty table in Ex 8, and why?
One — the table IX; the protocol demands the intent lock on the ancestor before scanning, even with zero rows.
What is locked in Ex 9 when the row is absent, and what does it stop?
A key-range / gap lock on the predicate id=5; it blocks T2's INSERT and so prevents a phantom row appearing between T1's two reads.
What turns harmless S into starvation?
S–X is ❌, so a continuous overlap of readers keeps blocking a waiting writer forever unless the scheduler is fair.
What proves Ex 12 is a deadlock, and how does U fix it?
A directed cycle in the wait-for graph (T1 waits T2, T2 waits T1); U is mutually exclusive with another U, so only one writer-candidate exists, the second edge never forms, and the cycle can't close.