This article is half-done without your Comment! *** Please share your thoughts via Comment ***
I had shared one article about, how to kill all running connections of PostgreSQL Database.
PostgreSQL: Script to Kill all Running Connections and Sessions of a Database
In this post, I am sharing small, but a very important note on PostgreSQL pg_terminate_backend and pg_cancel_backend which are used to kill the running query or session.
pg_cancel_backend():
It cancels the only running query. You can get the list of long running queries (pid) using pg_stat_activity.
If you want to kill any of those long running queries, you must use pg_cancel_backend() to kill it. You can cancel one query without destroying the connection, stopping the other queries.
pg_terminate_backend():
It terminates the entire process and database connection. It destroys everything for that particular role or user.
If you are very serious about to terminate the session without cancelling it, you can use this function otherwise you should avoid the use of this function.
The best practice is:
First find long running queries and their process ids (pid), cancel those queries using pg_cancel_backend and if it is not releasing, you should use pg_terminate_backend.
Leave a Reply