How to Kill an PostgreSQL process from inside PostgreSQL?

How to Kill an PostgreSQL process from inside PostgreSQL?


In PostgreSQL, you can use the pg_terminate_backend() function to kill a specific process. This function takes a single argument, the process ID of the process you want to kill, and it terminates that process immediately. Here's an example of how you can use the pg_terminate_backend() function to kill a process with a process ID of 123: You can also use the pg_cancel_backend() function to send a cancel request to a specific process, which allows the process to terminate gracefully and complete any ongoing transactions. Here's an example of how you can use the pg_cancel_backend() function to cancel a process with a process ID of 123: SELECT pg_cancel_backend(123); It lists all the current connections with their process ID, username, current query, and state of the connection. Once you have the process ID of the connection, you can use pg_terminate_backend() or pg_cancel_backend() function to kill or cancel the process. It's important to note that using the pg_terminate_backend() function can cause data loss, as the process may not have a chance to complete any ongoing transactions. So it's always recommended to use pg_cancel_backend() whenever possible.
About Shiv Iyer 72 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.

Be the first to comment

Leave a Reply