Skip to content

Instantly share code, notes, and snippets.

@richardbasile
Created May 24, 2019 12:38
Show Gist options
  • Save richardbasile/5d8d350af494158dff869a0c970b66e0 to your computer and use it in GitHub Desktop.
Save richardbasile/5d8d350af494158dff869a0c970b66e0 to your computer and use it in GitHub Desktop.
Finding blocked queries and their blockers in PostgreSQL
with blocked as (
SELECT pid AS blocked_pid
, usename AS blocked_username
, query AS blocked_query
, query_start AS blocked_start
, wait_event AS blocked_wait_event
, wait_event_type AS blocked_event_type
, unnest( pg_blocking_pids( pid ) ) AS blocked_by
FROM pg_stat_activity
WHERE cardinality( pg_blocking_pids( pid ) ) > 0
)
select blocked.*
, blocking.usename
, blocking.query
, blocking.query_start
, blocking.wait_event
, blocking.wait_event_type
, blocking.state
from blocked
join pg_stat_activity blocking on blocking.pid = blocked.blocked_by
order by query_start ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment