Cookbook recipe

Diagnosing a blocked query queue

Applies to PostgreSQL 13–17 Last reviewed Nov 2025 Grounded in source
Estimated investigation4 min

Scenario

Multiple queries are waiting for the same lock — pg_stat_activity shows a chain of blocked sessions, and application latency is rising. Diagnose it Use the built-in pg_blocking_pids() function (available since PostgreSQL 9.6) together with pg_locks to…

Investigation Path

Multiple queries are waiting for the same lockpg_stat_activity shows a chain of blocked sessions, and application latency is rising.

Diagnose it

Use the built-in pg_blocking_pids() function (available since PostgreSQL 9.6)
together with pg_locks to map the full blocking chain:

-- Blocked sessions and their blockers:
SELECT blocked.pid                               AS blocked_pid,
       blocked.usename                           AS blocked_user,
       blocked.application_name,
       LEFT(blocked.query, 100)                  AS blocked_query,
       blocked.wait_event_type,
       blocked.wait_event,
       blocking.pid                              AS blocking_pid,
       blocking.usename                          AS blocking_user,
       LEFT(blocking.query, 100)                 AS blocking_query,
       blocking.state                            AS blocking_state
FROM pg_stat_activity AS blocked
JOIN pg_stat_activity AS blocking
  ON blocking.pid = ANY(pg_blocking_pids(blocked.pid))
WHERE pg_blocking_pids(blocked.pid) <> '{}'
ORDER BY blocked.pid;

-- Lock detail for the blocking backend:
SELECT pid, locktype, relation::regclass, mode, granted,
       waitstart
FROM pg_locks
WHERE pid = <blocking_pid>
ORDER BY granted DESC;

Why it happens

Every DML statement (and many DDL statements) acquires a lock on affected rows and tables.
If two transactions need conflicting lock modes on the same object, the later one waits.
A long-running transaction holding a table-level or row lock can cause a queue of waiters
to grow, eventually stalling the application entirely.

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