Algorithm Search⌘K

Theoremcore

Two-phase locking theorem

Also called 2PL serializability theorem.

Every schedule produced by transactions that follow two-phase locking is conflict serializable, in the order of their lock points.

The two-phase locking theorem says that if every transaction locks an item before it uses it, and never acquires a lock after it has released one, then the committed work is conflict serializable, and the serial order is the order in which the transactions reach their lock points. Eswaran, Gray, Lorie and Traiger proved it in 1976, together with a partial converse: a single transaction that breaks the two-phase rule can always be paired with a two-phase partner so that the result is not serializable. The theorem is the correctness argument behind the two-phase locking scheduler and all its variants. It is a statement about serializability only; deadlocks and recoverability are separate questions.

Statement

Locked schedules. Extend the schedule model with lock actions sli(x)sl_i(x) (shared lock), xli(x)xl_i(x) (exclusive lock) and ui(x)u_i(x) (release all locks of TiT_i on xx). An xli(x)xl_i(x) while TiT_i holds a shared lock on xx is an upgrade. A locked schedule LL is

  • well formed if each ri(x)r_i(x) happens while TiT_i holds a lock on xx in some mode, each wi(x)w_i(x) happens while TiT_i holds an exclusive lock on xx, and TiT_i releases each of its locks at the latest right after its terminal action;
  • legal if two different transactions never hold locks on the same item at the same time unless both locks are shared;
  • two-phase if, for each TiT_i, no lock action of TiT_i follows a release ui(⋅)u_i(\cdot) of TiT_i.

The lock point LP(Ti)\mathrm{LP}(T_i) is the position in LL of the last lock action of TiT_i. Let SS be LL without its lock actions, and C(S)C(S) its committed projection.

Theorem (two-phase locking theorem). If LL is well formed, legal and two-phase, then:

  1. every arc Ti→TjT_i \to T_j of the serialization graph of C(S)C(S) has LP(Ti)<LP(Tj)\mathrm{LP}(T_i) < \mathrm{LP}(T_j);
  2. hence C(S)C(S) is conflict serializable, and it is conflict equivalent to the serial schedule of the committed transactions sorted by lock point.

Partial converse (Eswaran et al. 1976). Let TT be a transaction that is not two-phase: it releases a lock and later performs a lock action. Assume that each lock action of TT is a new lock or an upgrade, and covers an access of TT that follows it. Then there is a two-phase transaction T′T' and a well formed, legal locked schedule of TT and T′T', in which both commit, whose schedule is not conflict serializable.

What the theorem does not give. It does not say that the converse holds for schedules: some conflict serializable schedules have no two-phase locking. And it says nothing about deadlocks or about recoverability.

In plain words

When two transactions conflict on an item, the locks make them take turns: the first must let go of the item before the second can take it. A two-phase transaction lets go of anything only after it holds everything it will ever hold, which is its lock point. So the first transaction reached its lock point before it let go, and the second reaches its lock point after it took over. Every conflict therefore runs from the earlier lock point to the later one, and the order of the lock points is a serial order that respects all of them.

Proof

Lemma (locks separate conflicts). Let pi(x)<Sqj(x)p_i(x) <_S q_j(x) be conflicting actions of Ti≠TjT_i \ne T_j. Let λi\lambda_i be the lock of TiT_i on xx that covers pi(x)p_i(x) (exclusive if pp is a write), and define λj\lambda_j for qj(x)q_j(x) in the same way. Then TiT_i releases λi\lambda_i before TjT_j acquires λj\lambda_j.

Proof. One of the two actions is a write, so its lock is exclusive, and the two locks conflict. Each lock is held over an interval of LL that contains its action. By legality the two intervals are disjoint. If the interval of λj\lambda_j came first, it would end before the interval of λi\lambda_i starts, so before pi(x)p_i(x), and so before qj(x)q_j(x), which it contains. That is impossible. So the interval of λi\lambda_i comes first: rel(λi)<acq(λj)\mathrm{rel}(\lambda_i) < \mathrm{acq}(\lambda_j). For an upgrade, acq\mathrm{acq} means the position of the upgrade. □\square

Proof of part 1. Let Ti→TjT_i \to T_j be an arc, from conflicting pi(x)<Sqj(x)p_i(x) <_S q_j(x) in C(S)C(S). The release of λi\lambda_i is a release of TiT_i, so by the two-phase rule every lock action of TiT_i comes before it: LP(Ti)<rel(λi)\mathrm{LP}(T_i) < \mathrm{rel}(\lambda_i). The lemma gives rel(λi)<acq(λj)\mathrm{rel}(\lambda_i) < \mathrm{acq}(\lambda_j). The acquisition of λj\lambda_j is a lock action of TjT_j, so acq(λj)≤LP(Tj)\mathrm{acq}(\lambda_j) \le \mathrm{LP}(T_j). Together,

LP(Ti)<rel(λi)<acq(λj)≤LP(Tj).\mathrm{LP}(T_i) < \mathrm{rel}(\lambda_i) < \mathrm{acq}(\lambda_j) \le \mathrm{LP}(T_j).

Proof of part 2. The lock points are positions of different actions of LL, so sorting by lock point gives a linear order of the committed transactions. By part 1, every arc goes forward in that order, so it is a topological order and the graph has no cycle. By the serializability theorem, C(S)C(S) is conflict equivalent to the serial schedule in that order. □\square

Proof of the partial converse. Let u(x)u(x) be the first release of TT and let ℓ(y)\ell(y) be the first lock action of TT after it. Before u(x)u(x), TT held a lock on xx and accessed xx under it. Run TT up to and including u(x)u(x), then all of T′T', then the rest of TT. Choose T′T' by cases.

  • y=xy = x, or y≠xy \ne x and TT holds no lock on yy at u(x)u(x). Let T′=xl′(x) xl′(y) w′(x) w′(y) u′(x) u′(y) c′T' = xl'(x)\ xl'(y)\ w'(x)\ w'(y)\ u'(x)\ u'(y)\ c', with one lock and one write if y=xy = x. Its locks are free: TT has released xx, and holds nothing on yy. The access of TT to xx before u(x)u(x) conflicts with w′(x)w'(x), which gives T→T′T \to T'. The access of TT to yy that ℓ(y)\ell(y) covers comes after w′(y)w'(y) and conflicts with it, which gives T′→TT' \to T.
  • TT holds a shared lock on y≠xy \ne x at u(x)u(x), and ℓ(y)\ell(y) is an upgrade. Let T′=sl′(y) r′(y) xl′(x) w′(x) u′(x) u′(y) c′T' = sl'(y)\ r'(y)\ xl'(x)\ w'(x)\ u'(x)\ u'(y)\ c'. The shared lock is compatible with the one of TT. Again the access of TT to xx gives T→T′T \to T', and r′(y)r'(y) before the write of TT that the upgrade covers gives T′→TT' \to T.

T′T' is two-phase in both cases, and TT then commits, so the serialization graph has a cycle. □\square

Example. Let T1=r1(x) w1(y) c1T_1 = r_1(x)\ w_1(y)\ c_1 and T2=r2(z) w2(x) r2(y) c2T_2 = r_2(z)\ w_2(x)\ r_2(y)\ c_2. The locked schedule

sl2(z) r2(z) sl1(x) r1(x) xl1(y) w1(y) u1(x) xl2(x) w2(x) c1 u1(y) sl2(y) r2(y) c2 u2(x) u2(y) u2(z)sl_2(z)\ r_2(z)\ sl_1(x)\ r_1(x)\ xl_1(y)\ w_1(y)\ u_1(x)\ xl_2(x)\ w_2(x)\ c_1\ u_1(y)\ sl_2(y)\ r_2(y)\ c_2\ u_2(x)\ u_2(y)\ u_2(z)

is well formed, legal and two-phase. LP(T1)\mathrm{LP}(T_1) is xl1(y)xl_1(y) at position 5, and LP(T2)\mathrm{LP}(T_2) is sl2(y)sl_2(y) at position 12. The schedule S=r2(z) r1(x) w1(y) w2(x) c1 r2(y) c2S = r_2(z)\ r_1(x)\ w_1(y)\ w_2(x)\ c_1\ r_2(y)\ c_2 has the arcs T1→T2T_1 \to T_2 on xx and on yy, and it is conflict equivalent to T1T2T_1 T_2 only. T2T_2 started first, but T1T_1 comes first in the serial order, as its lock point does.

Consequences

  • Every 2PL variant is serializable. Strict two-phase locking, rigorous 2PL and conservative two-phase locking produce well formed, legal, two-phase locked schedules, so the theorem applies to them directly. For multiple granularity locking and key-range locking the same proof works once one shows that two conflicting accesses are always covered by two conflicting locks.
  • The serialization order is known online. A transaction that has passed its lock point has a fixed place in the serial order. A scheduler can use that place, for example to number commits, without building the serialization graph.
  • The rule is optimal for item-only knowledge. By the partial converse, a scheduler that sees only which items each transaction locks, and allows one transaction to break the two-phase rule, cannot guarantee serializability.

Tightness

  • Not every serializable schedule is a 2PL schedule. S=r1(x) w2(x) w3(y) w1(y) c1 c2 c3S = r_1(x)\ w_2(x)\ w_3(y)\ w_1(y)\ c_1\ c_2\ c_3 is conflict serializable: its arcs are T1→T2T_1 \to T_2 on xx and T3→T1T_3 \to T_1 on yy, and it is equivalent to T3T1T2T_3 T_1 T_2. Assume a well formed, legal, two-phase locking of it. T1T_1 holds a lock on xx at r1(x)r_1(x), and T2T_2 an exclusive lock on xx at w2(x)w_2(x), so by the lemma T1T_1 releases xx before w2(x)w_2(x). T1T_1 holds an exclusive lock on yy at w1(y)w_1(y), and T3T_3 one at w3(y)w_3(y), so by the lemma T1T_1 acquires it after w3(y)w_3(y). Then T1T_1 acquires a lock after w3(y)>w2(x)w_3(y) > w_2(x), so after it released xx. This breaks the two-phase rule.
  • The partial converse needs a free partner. It builds a partner that locks any items it likes. If all transactions must follow some other rule, transactions that are not two-phase can still be always serializable. The tree locking protocol is the standard example: its transactions are not two-phase, and every schedule it allows is serializable.
  • Deadlocks. The theorem is about the schedules that are produced. Two-phase transactions can wait for each other in a cycle, and then no schedule is produced until a victim aborts; see deadlock.
  • Recoverability. The theorem looks at C(S)C(S) only. xl1(x) w1(x) u1(x) sl2(x) r2(x) c2 u2(x) c1xl_1(x)\ w_1(x)\ u_1(x)\ sl_2(x)\ r_2(x)\ c_2\ u_2(x)\ c_1 is a two-phase locking with the schedule w1(x) r2(x) c2 c1w_1(x)\ r_2(x)\ c_2\ c_1, which is conflict serializable and not recoverable.

History

Eswaran, Gray, Lorie and Traiger, at IBM Research during the System R project, defined two-phase transactions in their 1976 paper and proved both the theorem and the partial converse in a model of entities and lock actions. Bernstein, Hadzilacos and Goodman (1987) gave the proof with the serialization graph and lock points that is used here, and called it the Two Phase Locking Theorem. Later work asked which other locking rules are safe for special data, which led to the tree protocol of Silberschatz and Kedem (1980) and to the graph and DAG protocols.

References

  1. K. P. Eswaran, J. N. Gray, R. A. Lorie and I. L. Traiger, The notions of consistency and predicate locks in a database system, Communications of the ACM 19(11), 1976, pages 624 to 633, two-phase transactions, the theorem that they give consistent schedules, and the theorem that a transaction that is not two-phase has a partner that gives an inconsistent schedule
  2. Philip A. Bernstein, Vassos Hadzilacos and Nathan Goodman, Concurrency Control and Recovery in Database Systems, Addison-Wesley, 1987, Chapter 3 (the Two Phase Locking Theorem and its proof with the serialization graph)
  3. Gerhard Weikum and Gottfried Vossen, Transactional Information Systems, Morgan Kaufmann, 2001, Chapter 4 (locking schedulers and the correctness of 2PL)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/TJPXEY/text.yaml