Skip to content
Preptima
hardScenarioConceptMidSeniorStaff

You are getting a handful of deadlock errors an hour under load. How do you find the cause, and how do you stop them?

7 min readUpdated 2026-08-15

Answer

Short answer

A deadlock is a cycle in the wait-for graph, so read the engine's own report to get both statements and the locks each held. Nearly all of them come from two code paths taking the same rows in different orders; impose one order, shorten transactions, and retry, because a deadlock is always safe to retry.

Was this answer helpful?

Likely follow-ups

  • Why can a single UPDATE statement with no explicit locking still deadlock against itself?
  • How does SKIP LOCKED change the picture for workers pulling jobs from a table?
  • What is the difference between lock_timeout, statement_timeout and deadlock_timeout in PostgreSQL?
  • Under what circumstances would you rather have lock-wait timeouts than deadlock detection?

Related questions

Further reading