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.

 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;


Popular posts from this blog

Ora2PG - Oracle/MySQL to Postgres DB migration Version 20.0

spfile and pfile errors

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

PostgreSQL New version upgrade

How to Return a Result Set from a PostgreSQL Stored Procedure