Cookbook recipe

Detect deadlocks and design them out

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

Transactions occasionally fail with "deadlock detected". PostgreSQL broke the cycle by killing one — your job is to stop the cycles forming. Diagnose it Read the deadlock detail in the server log: -- log shows: Process…

Investigation Path

Transactions occasionally fail with “deadlock detected”. PostgreSQL broke the cycle by killing one — your job is to stop the cycles forming.

Diagnose it

Read the deadlock detail in the server log:

-- log shows: Process A waits for ... ; Process B waits for ...
SHOW deadlock_timeout;

Why it happens

A deadlock happens when two transactions each hold a lock the other needs, in opposite order. The detector waits deadlock_timeout, finds the cycle, and aborts one transaction (SQLSTATE 40P01).

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes