-
Notifications
You must be signed in to change notification settings - Fork 11
/
lock_blocking_waiting_pg_locks.sql
38 lines (35 loc) · 2.1 KB
/
lock_blocking_waiting_pg_locks.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
SELECT
blocked_locks.pid AS blocked_pid,
blocked_activity.usename AS blocked_user,
blocking_locks.pid AS blocking_pid,
blocking_activity.usename AS blocking_user,
blocked_activity.query AS blocked_query,
blocking_activity.query AS blocking_query,
blocked_activity.query_start AS blocked_query_start,
blocking_activity.query_start AS blocking_query_start
FROM pg_catalog.pg_locks blocked_locks
JOIN pg_catalog.pg_stat_activity blocked_activity
ON blocked_activity.pid = blocked_locks.pid
JOIN pg_catalog.pg_locks blocking_locks
ON blocking_locks.locktype = blocked_locks.locktype
AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database
AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation
AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page
AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple
AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid
AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid
AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid
AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid
AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid
AND blocking_locks.pid != blocked_locks.pid
JOIN pg_catalog.pg_stat_activity blocking_activity
ON blocking_activity.pid = blocking_locks.pid
WHERE NOT blocked_locks.granted;
-- Example results:
-- Using Rideshare as an example database, explicit LOCK on the "trips"
-- table within a transaction, then a SELECT * FROM trips that's blocked:
-- blocked_pid | blocked_user | blocking_pid | blocking_user | blocked_query | blocking_query | blocked_query_start | blocking_query_start
-- -------------+--------------+--------------+---------------+----------------------+----------------+-------------------------------+-------------------------------
-- 94003 | postgres | 92606 | postgres | select * from trips; | lock trips; | 2024-07-10 15:13:18.758721-05 | 2024-07-10 15:12:56.844562-05
-- (1 row)
--