PostgreSQL · SQL
SELECT w.pid AS waiting_pid,
w.usename AS waiting_user,
EXTRACT(EPOCH FROM (now() - w.query_start))::int AS waiting_seconds,
LEFT(w.query, 120) AS waiting_query,
b.pid AS blocking_pid,
b.usename AS blocking_user,
b.state AS blocking_state,
LEFT(b.query, 120) AS blocking_query
FROM pg_stat_activity w
JOIN LATERAL unnest(pg_blocking_pids(w.pid)) AS bp(pid) ON true
JOIN pg_stat_activity b ON b.pid = bp.pid
WHERE w.wait_event_type = 'Lock'
ORDER BY w.query_startThis query is part of the "PostgreSQL health" pack: connect the database in DataRunner and install the whole pack — queries, dashboard and checks — at once.
- Check that ships with it: Nobody is waiting on a lock