Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

PostgreSQL Killling All IDLE session Script Every 2 minute

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.


Here I have prepared this script will run every 2 minutes and and kill all IDLE connection such as ( 'idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled' ) which are IDLE session reached maximum threshold of 5 minutes.


crontab -e
*/2 * * * * sh /home/postgres/eds/ALL_IDLE_CONN.sh
cat /home/postgres/eds/ALL_IDLE_CONN.sh

HOSTNAME=`hostname`
PSQL="/opt/PostgreSQL/9.3/bin/psql"
PORT=5432
HOST="localhost"
DB="template1"
USER="postgres"
DATE=`date +'%d%m%G'`
DETAIL="/tmp/ALL_IDLE_CONN_$DATE.log"
touch $DETAIL
$PSQL -d $DB -U $USER -p $PORT -c " SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE  pid <> pg_backend_pid() 
AND state in ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled') 
AND state_change < current_timestamp - INTERVAL '5' MINUTE;" >> $DETAIL
exit 0;

Comments

Popular posts from this blog

VMWARE WORKSTATION 3,4,5,6,7,8,9,10,11,12,14,15...etc LICENSE KEYS COLLECTION

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

How to Configuration UDEV SCSI Rules In Oracle Linux 5, 6 and 7

Linux ctime,mtime,atime,cmin,amin,mmin

aspell