Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Script to find sessions that are blocking other sessions in PostgreSQL

In this post, I am sharing a script to find sessions that are blocking other sessions in PostgreSQL.

Instead of finding blocked sessions or processes, sometimes it is better to find those sessions which are the root of blocking.

PostgreSQL DBA can use this script to find the cause of blocked transactions.

SELECT 
 pl.pid as blocked_pid
 ,psa.usename as blocked_user
 ,pl2.pid as blocking_pid
 ,psa2.usename as blocking_user
 ,psa.query as blocked_statement
FROM pg_catalog.pg_locks pl
JOIN pg_catalog.pg_stat_activity psa
 ON pl.pid = psa.pid
JOIN pg_catalog.pg_locks pl2
JOIN pg_catalog.pg_stat_activity psa2
 ON pl2.pid = psa2.pid
 ON pl.transactionid = pl2.transactionid 
  AND pl.pid != pl2.pid
WHERE NOT pl.granted;

Comments

Popular posts from this blog

7 Steps to configure BDR replication in postgresql

How to find the server is whether standby (slave) or primary(master) in Postgresql replication ?

apt-add-repository

How to Get Table Size, Database Size, Indexes Size, schema Size, Tablespace Size, column Size in PostgreSQL Database

ORA-01261: Parameter db_recovery_file_dest destination string cannot be translated ORA-01262: Stat failed on a file destination directory Linux-x86_64 Error: 2: No such file or directory