-
-
Save sashaaro/6bf4c1188705db38afb49bebd6247dd1 to your computer and use it in GitHub Desktop.
lock monitor. postgresql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_locks blockedl | |
JOIN pg_stat_activity blockeda ON ((blockedl.pid = blockeda.pid))) | |
JOIN 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())); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
select relation::regclass as relation, l.*, a.*, now() - a.query_start as duration, pg_blocking_pids(l.pid) from pg_locks l | |
inner join pg_database db on db.oid = l.database | |
inner join pg_stat_activity a on db.oid = a.datid-- and a.pid = l.pid | |
where | |
db.datname = current_database() | |
and not l.pid = pg_backend_pid() | |
and locktype in ('relation','transactionid','tuple') and relation = 'account'::regclass | |
-- and locktype = 'tuple' | |
and application_name in ('Alice', 'Bob') | |
order by duration desc | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment