PostgreSQLBlocking & locks
Blocked and blocking sessions
Pairs each waiting backend with the backend holding the lock it needs, using pg_blocking_pids (9.6+).
Not yet verified. How scripts are tested
1SELECT blocked.pid AS blocked_pid,2 blocked.usename AS blocked_user,3 now() - blocked.query_start AS blocked_for,4 blocking.pid AS blocking_pid,5 blocking.usename AS blocking_user,6 blocking.state AS blocking_state,7 left(blocked.query, 120) AS blocked_query,8 left(blocking.query, 120) AS blocking_query9FROM pg_stat_activity blocked10JOIN LATERAL unnest(pg_blocking_pids(blocked.pid)) AS b(pid) ON true11JOIN pg_stat_activity blocking ON blocking.pid = b.pid12ORDER BY blocked_for DESC;Paste it into your query tool.