Core conceptsadvanced
Deadlock detector
A routine that runs after deadlock_timeout on a waiting backend, builds the wait-for graph, and cancels one transaction when it finds a lock cycle.
What this means
When a transaction has waited for a lock past deadlock_timeout, this routine wakes up and draws a "who is waiting for whom" map. If that map contains a loop, where A waits for B and B waits for A, the two would wait forever, so it cancels one of them to set the other free.
Why it matters operationally
It explains the exact behavior behind SQLSTATE 40P01: PostgreSQL does not prevent deadlocks, it detects and breaks them.
Related errors
Where it lives in the source
src/backend/storage/lmgr/deadlock.c