Cookbook recipe

Detect and resolve lock contention

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

Scenario

Queries hang waiting on locks. You need to see who blocks whom and break the jam. Diagnose it Show the blocking chain: SELECT blocked.pid AS blocked_pid, blocking.pid AS blocking_pid, left(blocked.query,50) AS blocked_q, left(blocking.query,50) AS blocking_q FROM…

Investigation Path

Queries hang waiting on locks. You need to see who blocks whom and break the jam.

Diagnose it

Show the blocking chain:

SELECT blocked.pid AS blocked_pid, blocking.pid AS blocking_pid,
  left(blocked.query,50) AS blocked_q, left(blocking.query,50) AS blocking_q
FROM pg_stat_activity blocked
JOIN pg_stat_activity blocking
  ON blocking.pid = ANY(pg_blocking_pids(blocked.pid));

Why it happens

A long transaction holds a lock that conflicts with what others want (e.g. an idle-in-transaction session holding a row lock, or DDL waiting behind a long SELECT). Everything downstream queues.

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