Skip to content

Instantly share code, notes, and snippets.

@chocobn69
Created February 20, 2018 08:31
Show Gist options
  • Save chocobn69/43c7b4ced007afdb88482b50481dc0d8 to your computer and use it in GitHub Desktop.
Save chocobn69/43c7b4ced007afdb88482b50481dc0d8 to your computer and use it in GitHub Desktop.
SELECT
COALESCE(blockingl.relation::regclass::text,blockingl.locktype) as locked_item,
now() - blockeda.query_start AS waiting_duration,
blockeda.pid AS blocked_pid,
blockeda.query as blocked_query, blockedl.mode as blocked_mode,
blockinga.pid AS blocking_pid, blockinga.query as blocking_query,
blockingl.mode as blocking_mode
FROM pg_catalog.pg_locks blockedl
JOIN pg_stat_activity blockeda ON blockedl.pid = blockeda.pid
JOIN pg_catalog.pg_locks blockingl ON(
( (blockingl.transactionid=blockedl.transactionid) OR
(blockingl.relation=blockedl.relation AND blockingl.locktype=blockedl.locktype)
) AND blockedl.pid != blockingl.pid)
JOIN pg_stat_activity blockinga ON blockingl.pid = blockinga.pid
AND blockinga.datid = blockeda.datid
WHERE NOT blockedl.granted
order by blockeda.pid asc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment