How to check if my queries are being blocked by other queries?
Last edited: 1/17/2025
You can set a lock monitor view to help investigate these.
Once you run the query that takes a long time to complete, you can go in the dashboard (or select from this view below) to check what are the blocks.
_28create view_28 public.lock_monitor as_28select_28 coalesce(_28 blockingl.relation::regclass::text,_28 blockingl.locktype_28 ) as locked_item,_28 now() - blockeda.query_start as waiting_duration,_28 blockeda.pid as blocked_pid,_28 blockeda.query as blocked_query,_28 blockedl.mode as blocked_mode,_28 blockinga.pid as blocking_pid,_28 blockinga.query as blocking_query,_28 blockingl.mode as blocking_mode_28from_28 pg_locks blockedl_28 join pg_stat_activity blockeda on blockedl.pid = blockeda.pid_28 join pg_locks blockingl on (_28 blockingl.transactionid = blockedl.transactionid_28 or blockingl.relation = blockedl.relation_28 and blockingl.locktype = blockedl.locktype_28 )_28 and blockedl.pid <> blockingl.pid_28 join pg_stat_activity blockinga on blockingl.pid = blockinga.pid_28 and blockinga.datid = blockeda.datid_28where_28 not blockedl.granted_28 and blockinga.datname = current_database();