Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

PostgreSQL Script to kill 'idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled' sessions of a Database

In this post, I am sharing one of the important script to kill all running idle connections and sessions of the PostgreSQL Database.

I have prepared this script such a way that you can also filter idle connections base on a particular time interval.


We are Database Administrator, and this is our responsibility to check idle connection periodically and if it requires to kill, we should do it.


To kill the connections is always not advisable, but in large systems where lots of transactions are going on in that situation we should kill idle connections base on a particular time interval.

SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE datname = 'Database_Name'
 AND pid <> pg_backend_pid()
 AND state in ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled') 
 AND state_change < current_timestamp - INTERVAL '15' MINUTE;

Comments

Popular posts from this blog

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

PostgreSQL pgBadger

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

PostgreSQL Pgbadger Installation On Linux

Migrating From Oracle to PostgreSQL using ora2pg open source tools