concept Data & storage
Database deadlocks and wait-for graphs
Follow the arrows from waiting transactions to their blockers. A path that loops back explains a lock deadlock; a path that ends can still leave transactions waiting.
What it means
A database lock deadlock is a circular wait: each transaction in the circle needs another one in the circle to release a conflicting lock. A wait-for graph makes this visible. Each arrow points from the waiter to its blocker. A long wait is not enough to establish deadlock; what matters is whether following the arrows brings you back to a transaction on the same path.
Try it
Simulation. Start with all three waits set to yes, graph completeness set to known, and scenario evidence set to provided. Trace the arrows from A: A→B, then B→C, then C→A. The result is cycle. Although A is waiting directly for B, following B’s wait explains why A’s progress ultimately depends on A itself.
Database deadlocks and wait-for graphs
Identify cycles and possible resolution in a lock wait-for graph
All three edges form a directed cycle; stipulated victim C would break it.
Observation and conclusion
- Graph completenessknown
- Established conclusionCycle in fixture
Arrows point from waiter to blocker. A known complete fixture with all three edges contains a cycle. Unknown observation does not establish a complete graph even if selected edge flags form a cycle.
Supplied edges: A→B, B→C, C→A. All three arrows form a cycle in this fixed fixture.
Evaluated inputs
- Required scenario evidence
- provided
- A waits for B
- yes
- B waits for C
- yes
- C waits for A
- yes
- Graph completeness
- known
Only possible edges A→B→C→A; symbolic fixed victim, no actual database termination or exhaustive deadlock detector.
About this model
The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.
Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.
Change only C waits for A to no. The result is now blocked: A→B and B→C remain, but the path ends at C. A is still waiting for exactly the same transaction as before. It is the loss of the return path, not the end of A’s wait, that changes the result. If you set all three waits to no, the result is clear: there are no wait edges in this graph.
Use Reset to restore the three-arrow cycle. Now set Graph completeness to unknown. The simulation still lists the three selected arrows, but it withholds a verdict because it requires a known graph before classifying the waits. Setting Required scenario evidence to missing is different: the simulation does not evaluate the arrows at all. Neither result means that there are no waits.
How the circle breaks
In the open chain, C is not waiting for A. If C releases the lock B needs, B can proceed and in turn release the lock A needs. In the cycle, C is waiting for A as well. Simply giving the three transactions more time does not remove any of those dependencies.
PostgreSQL resolves a detected deadlock by aborting one of the participating transactions. For illustration, the simulation assumes that participant would be C. If C were aborted, its wait for A would end and its lock would no longer hold up B. That breaks the circle without implying that A has already received its lock. C is an assumption, not a prediction about PostgreSQL. The simulation shows the waits, not a transaction abort or its aftermath.
With all three arrows selected but the graph observation marked unknown, the figure can show the supplied arrows, yet the decision stays unknown. Those arrows are not a complete established wait-for observation. Do not select a transaction to interrupt from the diagram. Compare a confirmed complete observation separately from a partial one.
In practice
Start with the question an arrow answers: Which transaction is this one waiting for? In PostgreSQL 18, a pg_locks row with granted set to false identifies a request that is waiting. Its mode tells you the lock mode requested. Neither that row alone nor its waitstart timestamp identifies the whole chain. With authorized access, use the actual blocker, lock modes, and transaction context to establish each arrow; then ask whether the blocker is itself waiting.
Check the deployed minor version when interpreting timestamps: PostgreSQL 18.7 fixes stale waitstart reporting. Even an accurate timestamp tells you when a wait began, not whom it depends on. Do not use elapsed time or the simulation’s choice of C as a reason to terminate a transaction.
The useful distinction is between an absent return path and an unverified one. In a known graph, an open chain explains blocking and a closed path explains deadlock. If you cannot tell where the path goes next, that missing blocker relationship is the next fact to establish.
Frequently asked questions
Editorial questions based on the listed sources and explicitly labeled local examples.
Can ordinary blocking resolve?
Yes, if the holder releases its lock.
Can consistent lock order reduce deadlocks?
Yes. Acquiring the same required resources in a common order can prevent the opposite-order cycle illustrated here. PostgreSQL recommends consistent lock ordering; other resources and acquisition paths still need their own analysis.
Who chooses a victim?
The engine policy; this lab stipulates one.
Should you kill transactions from this diagram?
No. The diagram is synthetic, and an isolated lock observation does not establish the entire wait-for graph. Inspect actual blocker relationships, transaction context and authorization before considering a recovery action. A known wait chain and a complete cycle call for different conclusions.
Sources & model boundaries
Primary references used for this explanation. The interactive example is an educational model, not a live check.
- PostgreSQL explicit locking
§13.3.4 Deadlocks. PostgreSQL 18 §13.3 describes table lock conflicts and deadlocks. Consistent acquisition order can avoid specific deadlocks; the engine detects deadlocks and aborts a participant, whose identity is not predictable in general.
Checked .
- PostgreSQL 18: pg_locks
pg_locks columns: locktype, mode, granted, waitstart. pg_locks exposes lock types, modes, whether the lock is granted, and wait start information. A non-granted entry denotes a request waiting for a lock; a complete diagnosis requires related transaction context and actual blocker evidence.
Checked .
- PostgreSQL 18.7 release notes
E.1.2 Changes: pg_locks waitstart reporting. 18.7 fixes stale pg_locks.waitstart reporting.
Checked .