Skip to content

Instantly share code, notes, and snippets.

@aks
Created April 12, 2018 22:44
Show Gist options
  • Select an option

  • Save aks/519e5fa3cf564808715cd60011619ed9 to your computer and use it in GitHub Desktop.

Select an option

Save aks/519e5fa3cf564808715cd60011619ed9 to your computer and use it in GitHub Desktop.
Create a view of locks in a postgresql database
CREATE VIEW lock_monitor AS(
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
AND blockinga.datname = current_database()
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment