How to detect query which holds the lock in Postgres?

前端 未结 5 2060
醉话见心
醉话见心 2020-12-07 08:51

I want to track mutual locks in postgres constantly.

I came across Locks Monitoring article and tried to run the following query:

SELECT bl.pid     A         


        
5条回答
  •  难免孤独
    2020-12-07 09:20

    Since 9.6 this is a lot easier as it introduced the function pg_blocking_pids() to find the sessions that are blocking another session.

    So you can use something like this:

    select pid, 
           usename, 
           pg_blocking_pids(pid) as blocked_by, 
           query as blocked_query
    from pg_stat_activity
    where cardinality(pg_blocking_pids(pid)) > 0;
    

提交回复
热议问题