Thursday, 25 January 2018

Find out blocked queries in PostgreSQL version 9.5 and earlier

Below is the query that can be used to find out blocked queries in PostgreSQL version 9.6 onwards -

SELECT blockingl.relation::regclass,
  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.relation=blockedl.relation
  AND blockingl.locktype=blockedl.locktype AND blockedl.pid != blockingl.pid)
JOIN pg_stat_activity blockinga ON blockingl.pid = blockinga.pid
WHERE NOT blockedl.granted;

Once we figure out the blocked query, we can kill the blocked query using the PID and below command -

postgres=# select pg_terminate_backend(3014);

We can also figure out if queries are waiting from OS using the below command -

[rahul@linux] ps -ef | grep wait 

1 comment:

  1. Wynn Las Vegas - MapYRO
    Wynn Hotel and 서산 출장샵 Casino is 보령 출장안마 a luxury resort in Las 안산 출장마사지 Vegas, Nevada, United States 안동 출장안마 and is open daily 24 공주 출장샵 hours. The casino is connected to its sister resort,

    ReplyDelete