171

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     AS blocked_pid,
     a.usename  AS blocked_user,
     kl.pid     AS blocking_pid,
     ka.usename AS blocking_user,
     a.query    AS blocked_statement
FROM  pg_catalog.pg_locks         bl
 JOIN pg_catalog.pg_stat_activity a  ON a.pid = bl.pid
 JOIN pg_catalog.pg_locks         kl ON kl.transactionid = bl.transactionid AND kl.pid != bl.pid
 JOIN pg_catalog.pg_stat_activity ka ON ka.pid = kl.pid
WHERE NOT bl.granted;

Unfortunately, it never returns non-empty result set. If I simplify given query to the following form:

SELECT bl.pid     AS blocked_pid,
     a.usename  AS blocked_user,
     a.query    AS blocked_statement
FROM  pg_catalog.pg_locks         bl
 JOIN pg_catalog.pg_stat_activity a  ON a.pid = bl.pid
WHERE NOT bl.granted;

then it returns queries which are waiting to acquire a lock. But I cannot manage to change it so that it can return both blocked and blocker queries.

Any ideas?

3
  • 1
    What's a blocker query? It's a transaction that holds a lock, the specific query that took it may be finished and gone within that transaction, while the lock is still held. Oct 21, 2014 at 16:05
  • Sounds reasonable, but what did the Locks Monitoring article authors mean in this case?
    – Roman
    Oct 21, 2014 at 16:33
  • 1
    That only shows row-level locks. I find this one more useful (although more complex) as it shows object level locks as well: wiki.postgresql.org/wiki/Lock_dependency_information
    – jjanes
    Oct 22, 2014 at 2:46

8 Answers 8

391

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;
4
  • 85
    I always come to this answer to copy the query. Thanks for the answer Oct 14, 2020 at 8:08
  • 1
    Devi's answer below contains this information plus additional information.
    – MichaelG
    Jul 13, 2021 at 14:04
  • Your answer still the most powerful answer in 2023 because it's simple enough and does what I need at the same time, I always come to copy it from here ♥ Mar 28 at 12:09
  • 1
    FYI this amzing article talks more about the very useful pg_blocking_pids() and give examples who use WHERE clause instead of cardinality().
    – Ray Eldath
    Aug 8 at 7:00
89

From this excellent article on query locks in Postgres, one can get blocked query and blocker query and their information from the following query.

CREATE VIEW lock_monitor AS(
SELECT
  COALESCE(blockingl.relation::regclass::text,blockingl.locktype) as locked_item,
  now() - blockeda.query_start AS waiting_duration, blockeda.pid AS blocked_pid,
  blockeda.query as blocked_query, blockedl.mode as blocked_mode,
  blockinga.pid AS blocking_pid, blockinga.query as blocking_query,
  blockingl.mode as blocking_mode
FROM pg_catalog.pg_locks blockedl
JOIN pg_stat_activity blockeda ON blockedl.pid = blockeda.pid
JOIN pg_catalog.pg_locks blockingl ON(
  ( (blockingl.transactionid=blockedl.transactionid) OR
  (blockingl.relation=blockedl.relation AND blockingl.locktype=blockedl.locktype)
  ) AND blockedl.pid != blockingl.pid)
JOIN pg_stat_activity blockinga ON blockingl.pid = blockinga.pid
  AND blockinga.datid = blockeda.datid
WHERE NOT blockedl.granted
AND blockinga.datname = current_database()
);

SELECT * from lock_monitor;

As the query is long but useful, the article author has created a view for it to simplify it's usage.

4
  • "ERROR: column blockeda.pid does not exist" ... mmh which version ?
    – Massimo
    Aug 14, 2018 at 5:46
  • 1
    that was on 9.6.2
    – Devi
    Aug 14, 2018 at 7:13
  • Works on 9.3 as well.
    – Nux
    Jun 28, 2021 at 12:37
  • 4
    It's important to note that blocking_query does not necessarily show the query that causes the blocking. It only shows the last query executed in the transaction that is blocking.
    – T3rm1
    Nov 9, 2022 at 8:29
44

This modification of a_horse_with_no_name's answer will give you the last (or current, if it's still actively running) query of the blocking session in addition to just the blocked sessions:

SELECT
    activity.pid,
    activity.usename,
    activity.query,
    blocking.pid AS blocking_id,
    blocking.query AS blocking_query
FROM pg_stat_activity AS activity
JOIN pg_stat_activity AS blocking ON blocking.pid = ANY(pg_blocking_pids(activity.pid));

This helps you understand which operations are interfering with each other (even if the block comes from a previous query), helping you understand the impact of killing one session and figuring out how to prevent blocking in the future.

3
  • 3
    Note that this will not give you the blocking query. It will only show the last query that was executed in that session. That is not necessarily the query that causes the lock! E.g. if the session runs an UPDATE which locks a row, but does not commit and then runs a SELECT, the SELECT will be shown in pg_stat_activity but the lock is actually caused by the UPDATE
    – user330315
    Jan 21, 2022 at 16:19
  • 1
    @a_horse_with_no_name I reworded slightly because the important part is to be able to identity what the session is or was doing, which gives you some idea where it came from and what the impact of terminating it will be and whether you need to change something your application to prevent it.
    – jpmc26
    Jan 21, 2022 at 17:16
  • Great answer! Helped me to find the bug in my procedure. Aug 19, 2022 at 11:48
20

How to show all blocked queries.

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;

You can kill a blocked query by using the below command.

SELECT pg_cancel_backend(a.pid), pg_terminate_backend(a.pid);

You can terminate all blocked queries using it.

SELECT pg_cancel_backend(a.pid), pg_terminate_backend(a.pid)
FROM( 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) a
11

Postgres has a very rich system catalog exposed via SQL tables. PG's statistics collector is a subsystem that supports collection and reporting of information about server activity.

Now to figure out the blocking PIDs you can simply query pg_stat_activity.

select pg_blocking_pids(pid) as blocked_by
from pg_stat_activity
where cardinality(pg_blocking_pids(pid)) > 0;

To, get the query corresponding to the blocking PID, you can self-join or use it as a where clause in a subquery.

SELECT query
FROM pg_stat_activity
WHERE pid IN (select unnest(pg_blocking_pids(pid)) as blocked_by from pg_stat_activity where cardinality(pg_blocking_pids(pid)) > 0);

Note: Since pg_blocking_pids(pid) returns an Integer[], so you need to unnest it before you use it in a WHERE pid IN clause.

Hunting for slow queries can be tedious sometimes, so have patience. Happy hunting.

1
  • 1
    You don't nee unnest() you can simplify that to where pid = any(pg_blocking_pids(pid))
    – user330315
    Dec 17, 2019 at 8:22
7

for postgresql versions earlier than postgresql 9.6 which does not have pg_blocking_pids function,you can use following query to find blocked query and blocking query.

SELECT w.query                          AS waiting_query,
       w.pid                            AS waiting_pid,
       w.usename                        AS waiting_user,
       now() - w.query_start            AS waiting_duration,
       l.query                          AS locking_query,
       l.pid                            AS locking_pid,
       l.usename                        AS locking_user,
       t.schemaname || '.' || t.relname AS tablename,
       now() - l.query_start            AS locking_duration
FROM pg_stat_activity w
         JOIN pg_locks l1 ON w.pid = l1.pid AND NOT l1.granted
         JOIN pg_locks l2 ON l1.relation = l2.relation AND l2.granted
         JOIN pg_stat_activity l ON l2.pid = l.pid
         JOIN pg_stat_user_tables t ON l1.relation = t.relid
WHERE w.waiting;
5

Others have already answered your query, but I had certain situation: There were lots of queries blocking each other and I wanted to find the main blocker sessions for each blocked session.

First example: session 5 was blocked by session 4 and 4 was waiting for 3 and 2 while 3 and 2 were waiting for session 1.

5->4->{3,2}->1

In this case session 1 is real problem, once it is cleared, others will take care of themselves.

So I wanted a query which will show me results like this:

Blocked pid Blocker pid
5 1
4 1
3 1
2 1

So if there is any chain locked sessions, this query will show you the main blocker sessions for each blocked session.

;with recursive 
    find_the_source_blocker as (
        select  pid
               ,pid as blocker_id
        from pg_stat_activity pa
        where pa.state<>'idle'
              and array_length(pg_blocking_pids(pa.pid), 1) is null

        union all

        select              
                t.pid  as  pid
               ,f.blocker_id as blocker_id
        from find_the_source_blocker f 
        join (  SELECT
                    act.pid,
                    blc.pid AS blocker_id
                FROM pg_stat_activity AS act
                LEFT JOIN pg_stat_activity AS blc ON blc.pid = ANY(pg_blocking_pids(act.pid))
                where act.state<>'idle') t on f.pid=t.blocker_id
        )
    
select distinct 
       s.pid
      ,s.blocker_id
      ,pb.usename       as blocker_user
      ,pb.query_start   as blocker_start
      ,pb.query         as blocker_query
      ,pt.query_start   as trans_start
      ,pt.query         as trans_query
from find_the_source_blocker s
join pg_stat_activity pb on s.blocker_id=pb.pid
join pg_stat_activity pt on s.pid=pt.pid
where s.pid<>s.blocker_id
1
  • This is far more useful information that the other queries!
    – user398139
    Jun 23 at 12:28
4

One thing I find that is often missing from these is an ability to look up row locks. At least on the larger databases I have worked on, row locks are not shown in pg_locks (if they were, pg_locks would be much, much larger and there isn't a real data type to show the locked row in that view properly).

I don't know that there is a simple solution to this but usually what I do is look at the table where the lock is waiting and search for rows where the xmax is less than the transaction id present there. That usually gives me a place to start, but it is a bit hands-on and not automation friendly.

Note that shows you uncommitted writes on rows on those tables. Once committed, the rows are not visible in the current snapshot. But for large tables, that is a pain.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.