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 (shared lock), (exclusive lock) and (release all locks of on ). An while holds a shared lock on is an upgrade. A locked schedule is
- well formed if each happens while holds a lock on in some mode, each happens while holds an exclusive lock on , and 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 , no lock action of follows a release of .
The lock point is the position in of the last lock action of . Let be without its lock actions, and its committed projection.
Theorem (two-phase locking theorem). If is well formed, legal and two-phase, then:
- every arc of the serialization graph of has ;
- hence 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 be a transaction that is not two-phase: it releases a lock and later performs a lock action. Assume that each lock action of is a new lock or an upgrade, and covers an access of that follows it. Then there is a two-phase transaction and a well formed, legal locked schedule of and , 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 be conflicting actions of . Let be the lock of on that covers (exclusive if is a write), and define for in the same way. Then releases before acquires .
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 that contains its action. By legality the two intervals are disjoint. If the interval of came first, it would end before the interval of starts, so before , and so before , which it contains. That is impossible. So the interval of comes first: . For an upgrade, means the position of the upgrade.
Proof of part 1. Let be an arc, from conflicting in . The release of is a release of , so by the two-phase rule every lock action of comes before it: . The lemma gives . The acquisition of is a lock action of , so . Together,
Proof of part 2. The lock points are positions of different actions of , 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, is conflict equivalent to the serial schedule in that order.
Proof of the partial converse. Let be the first release of and let be the first lock action of after it. Before , held a lock on and accessed under it. Run up to and including , then all of , then the rest of . Choose by cases.
- , or and holds no lock on at . Let , with one lock and one write if . Its locks are free: has released , and holds nothing on . The access of to before conflicts with , which gives . The access of to that covers comes after and conflicts with it, which gives .
- holds a shared lock on at , and is an upgrade. Let . The shared lock is compatible with the one of . Again the access of to gives , and before the write of that the upgrade covers gives .
is two-phase in both cases, and then commits, so the serialization graph has a cycle.
Example. Let and . The locked schedule
is well formed, legal and two-phase. is at position 5, and is at position 12. The schedule has the arcs on and on , and it is conflict equivalent to only. started first, but 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. is conflict serializable: its arcs are on and on , and it is equivalent to . Assume a well formed, legal, two-phase locking of it. holds a lock on at , and an exclusive lock on at , so by the lemma releases before . holds an exclusive lock on at , and one at , so by the lemma acquires it after . Then acquires a lock after , so after it released . 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 only. is a two-phase locking with the schedule , 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
- 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
- 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)
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag TJPXEY Edit this page History Report an error Markdown