4.4.19 · D1Databases

Foundations — Locking — shared, exclusive, intent locks

1,437 words7 min readBack to topic

Before you can read the parent note fluently, you need to earn every word it throws at you. This page builds each one from absolute zero, in an order where nothing appears before it is defined.


1. What is a "transaction"?

The picture: a sealed envelope holding several instructions. You either open and run the whole envelope, or you throw it away unopened.

Why the topic needs it: locks are held by transactions. "Who is waiting?" and "who owns this lock?" always means which transaction. Without this unit, the word "conflict" has no owner.

Figure — Locking — shared, exclusive, intent locks

The letters T1, T2 you see everywhere in the parent are just names for two such envelopes running at the same time. Related depth lives in ACID Properties and Two-Phase Locking.


2. What is a "data item"? (rows, pages, tables)

The picture: a Russian nesting doll. The table contains pages, a page contains rows. A lock can grab the outer doll (table) or one inner doll (row).

Why the topic needs it: the entire "intent lock" idea only makes sense because objects nest. If you lock a row inside a table, someone eyeing the whole table needs a way to notice. That is why the parent links Lock Granularity.

Figure — Locking — shared, exclusive, intent locks

3. "Concurrent" — at the same time

The picture: two timelines running side by side, with arrows crossing between them where they touch the same data item.

Why the topic needs it: if only one transaction ever ran at a time, locks would be pointless — nobody to conflict with. Locks exist purely to make concurrency safe. The precise "how safe" is tuned by Isolation Levels.


4. The three anomalies (the enemy locks fight)

The parent lists three bad things. Let's name each with a plain picture.

The picture: two hands reaching for the same jar; whoever's hand arrives second smudges what the first was relying on.

Why the topic needs it: these three are the exact reasons S and X locks are split the way they are. Every compatibility ❌ in the parent's table is there to prevent one of these three.


5. Notation: , ,

Why the topic needs it: the whole point of intent locks is turning an row-by-row scan into an single flag check. Without this notation you can't feel why they're worth the trouble.

Figure — Locking — shared, exclusive, intent locks

6. "Acquire", "hold", "release", "block"

The picture: a single key on a hook. You take it (acquire), keep it in your pocket (hold), hang it back (release). Anyone else who needs that key stands in line (block).

Why the topic needs it: "T2 waits" in every parent example means exactly this blocking. When two transactions each block waiting for the other, you get a deadlock — a whole topic of its own.


7. Compatibility — "can two coexist?"

The picture: a table (grid) where the row is "what I want" and the column is "what's already there"; a ✅ means "go ahead," a ❌ means "wait." This grid is the compatibility matrix the parent shows.

Why the topic needs it: every locking decision the engine makes is a single lookup in this grid. Master this idea and the parent's two tables become obvious.


Prerequisite map

Transaction all-or-nothing

Concurrency two run at once

Data item and granularity

Intent locks

Three anomalies

Shared and Exclusive locks

Compatibility matrix

Big-O notation

Locking topic 4.4.19

Read the map top-down: transactions + concurrency create the danger (anomalies); S/X locks are the cure; granularity + Big-O explain why intent locks are added; the compatibility matrix ties it all into the parent topic. Deeper reading branches into Two-Phase Locking, MVCC, and Isolation Levels.


Equipment checklist

In one sentence, what is a transaction?
A bundle of operations that runs all-or-nothing, as a single unit.
What does "granularity" mean?
The size of the data chunk being locked — row, page, or table.
What does "concurrent" mean for two transactions?
They are both in progress during the same time window, with interleaving steps.
Name the three anomalies locks prevent.
Lost update, dirty read, non-repeatable read.
What is a lost update in one line?
Two transactions read the same value, both write their increment, and one write silently overwrites the other.
What does mean versus ?
is work growing with the number of items; is a single constant-time check.
What are the four locking verbs?
Acquire, hold, release, and block (wait).
What does it mean for two locks to be "compatible"?
Both can be held on the same item at once without allowing any anomaly.
Why is compatibility drawn as a grid?
Rows are the requested lock, columns the existing lock; each cell says ✅ coexist or ❌ wait.
Recall Ready check

If you can answer every line above without peeking, you're ready for the parent's S/X and intent-lock matrices. If any stumped you, re-read that section — the parent assumes all of them silently.