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

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

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

7 Steps to configure BDR replication in postgresql

Postgres Streaming Replication Configuration

vacuumlo - removing large objects orphans from a database PostgreSQL