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.
1create view2 public.lock_monitor as3select4 coalesce(5 blockingl.relation::regclass::text,6 blockingl.locktype7 ) as locked_item,8 now() - blockeda.query_start as waiting_duration,9 blockeda.pid as blocked_pid,10 blockeda.query as blocked_query,11 blockedl.mode as blocked_mode,12 blockinga.pid as blocking_pid,13 blockinga.query as blocking_query,14 blockingl.mode as blocking_mode15from16 pg_locks blockedl17 join pg_stat_activity blockeda on blockedl.pid = blockeda.pid18 join pg_locks blockingl on (19 blockingl.transactionid = blockedl.transactionid20 or blockingl.relation = blockedl.relation21 and blockingl.locktype = blockedl.locktype22 )23 and blockedl.pid <> blockingl.pid24 join pg_stat_activity blockinga on blockingl.pid = blockinga.pid25 and blockinga.datid = blockeda.datid26where27 not blockedl.granted28 and blockinga.datname = current_database();