skip to content

two-phase locking

grow, then shrink. why 2PL guarantees serializability, and strict 2PL avoids cascading aborts.

address
everything.locking.two_phase_locking
part
part 15, locks and concurrency control
chapter
chapter 2 of 10
reading time
7 min

two-phase locking (2PL): a transaction takes locks (shared to read, exclusive to write) and never takes a new one after it has released any. a growing phase, then a shrinking phase. that one rule guarantees conflict serializability. strict 2PL holds write locks until commit, which also rules out cascading aborts. postgres keeps its row locks until commit, strict-style, but its plain reads take no row locks at all: MVCC handles them.

the picture

you know the precedence graph: a schedule is serializable when its conflicts never form a cycle. 2PL is the oldest way to make sure they can't.

think of borrowing tools for a job. you collect every tool you need first, and only once you've started handing tools back do you stop collecting. if you returned the hammer and then went back for the saw, someone could use both in between and the state you see would be half theirs. "collect, then return, never collect again" is the whole protocol.

protocol =
lockstime →123growingshrinkinglock pointlock Alock Block Cunlock Aunlock Bunlock Ccommit

two-phase. all acquires before any release. the peak is the lock point: order the transactions by their lock points and you have the equivalent serial order. but A is released at 5 and the commit is at 9: anyone who reads A in between reads uncommitted data.

locks held by T1the lock point
drawn by hand · the textbook model of two-phase locking
switch the protocol. the line is how many locks T1 holds over time. the lock point is where growing stops. strict 2PL drops everything at COMMIT; the third shape releases and acquires again, and loses the guarantee.

why it works

take two transactions with a conflict between them: T1 touches x first, T2 touches x later, at least one writes. under 2PL, T2's lock on x can only be granted after T1 released its lock on x. T1 released it in its shrinking phase, so T1's lock point came first. every edge T1 → T2 in the precedence graph goes from an earlier lock point to a later one.

lock points are moments in time, and time doesn't loop. so edges only point forward in lock-point order, the graph can't have a cycle, and sorting the transactions by lock point gives the equivalent serial order. that's the proof, more or less in full.

the rule is shared/exclusive with the obvious compatibility: many readers or one writer. the lock modes in a real system multiply from there (table lock modes has eight, for tables alone), but the two-phase rule is the same.

cascading aborts

basic 2PL allows something nasty. T1 writes x and releases its lock early (it's in its shrinking phase, that's allowed). T2 reads x. then T1 aborts:

T1
·
T2
·
1
X-lock(x), W(x) = 5
x
x
5

T1 locks x exclusively and writes 5. not committed.

step 1/5
drawn by hand · the textbook cascading abort
basic 2PL, textbook model. T2 read a value T1 had written but not committed. when T1 aborts, T2 has based its work on data that never existed, so T2 must abort too, and so must anyone who read from T2.

strict 2PL fixes it by holding every exclusive lock until the transaction commits or aborts. nobody can read what you wrote until it's final. (rigorous 2PL, or strong strict, holds shared locks to the end too, which also makes the serial order the commit order.) almost every lock-based database uses one of these two. the growing/shrinking distinction then mostly disappears: you just take locks as you go and drop them all at the end.

predict the output
under strict 2PL, can a transaction ever read data written by a transaction that later aborts?
text
T1: X-lock(x)  W(x)  ...  COMMIT or ABORT  → unlock(x)
T2:                  S-lock(x)  waits ............ until T1 ends

postgres: strict for writes, MVCC for reads

postgres uses locks for writes and keeps them until the end of the transaction, exactly like strict 2PL. but a plain SELECT doesn't lock the rows it reads. it reads the version its snapshot can see, so it never waits for a writer and never makes a writer wait. here's both halves on a real server:

T1
·
T2
·
1
BEGIN; SELECT balance FROM accounts WHERE id = 1→ 100.00
T1 and T2's locks (pg_locks)
  • T1ACCESS SHAREaccounts

T1 opens a transaction and reads alice. the only locks are on the table and its index (ACCESS SHARE) and T1's own virtual xid. nothing on the row.

step 1/6
real output, a postgres 18.6 server · strict-2pl
step through, with pg_locks on the right. T1 reads alice: only table locks, nothing on the row, so T2 updates alice without waiting. T1 updates bob: now it holds an exclusive lock on its transaction id, which is what row-lock waiters queue on, and it keeps it after the statement ends. T2 waits for bob until T1's COMMIT releases everything at once.

that's why postgres's READ COMMITTED and REPEATABLE READ aren't serializable: reads leave no trace, so nothing stops a cycle that goes through a read (write skew). you can get 2PL behaviour for a particular read by asking for it, with SELECT ... FOR SHARE or FOR UPDATE: those are the shared and exclusive locks of the textbook, held to commit. and SERIALIZABLE doesn't use 2PL at all: it tracks reads with notes that block nobody (SSI).

one level deeperphantoms: locking rows isn't enough

2PL on rows protects the rows you read, not the rows that aren't there yet. "count the doctors on call" read two rows; a new on-call doctor inserted by someone else isn't locked by anyone. to be truly serializable a lock-based system must also lock the predicate or a range of an index: next-key or gap locks in InnoDB, key-range locks in SQL Server. postgres's SSI notes cover this by noting index pages and whole relations, not just tuples.

keylines

  • 2PL: acquire, then release, never acquire again. growing, lock point, shrinking.
  • every conflict edge points from an earlier lock point to a later one, so no cycles.
  • basic 2PL allows dirty reads and cascading aborts. strict 2PL holds write locks to commit.
  • postgres holds write locks to commit, like strict 2PL. plain reads take no row locks: MVCC.
  • 2PL can deadlock. it needs a detector or a prevention scheme next to it.