Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 August PostgreSQL: Script to kill all idle sessions and connections of a Database

PostgreSQL: Script to kill all idle sessions and connections of a Database

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

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.

1
2
3
4
5
6
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;

Aug 4, 2016Anvesh Patel
PostgreSQL: Create an auto incrementing Primary Key using a Custom SequencePostgreSQL: How to convert Table Data into JSON formatted Data?
Comments: 6
  1. Aksh
    September 25, 2018 at 9:48 am

    How do I use this script? Just call it from psql?

    • Anvesh Patel
      Anvesh Patel
      October 3, 2018 at 7:18 pm

      did you try to use via psql?

  2. Azim
    October 3, 2018 at 7:25 am

    Hi Anvesh,

    First of all thanks for this script. Its really works well though there are some sessions in idle state running on the database. Can you please share any advance or better where clause for the below part

    “state_change < current_timestamp – INTERVAL '15' MINUTE;"
    It will be a great help from your end.

    Cheers,
    Azim A.

    • Anvesh Patel
      Anvesh Patel
      October 3, 2018 at 7:12 pm

      Sure will check and share

  3. San
    September 10, 2019 at 5:47 am

    This helped!

  4. Manoj kumar matang
    January 22, 2020 at 9:18 am

    how to clean up old ideal connection approx 1 days ago in postgresql. and set in cronjob.

Anvesh Patel
Anvesh Patel

Database Engineer

August 4, 2016 PostgreSQL, PostgreSQL DBA ScriptAnvesh Patel, database, database research and development, dbrnd, pg_stat_activity, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, state_change
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....