You can terminate a problematic session using the pg_terminate_backend function.
SELECT pg_terminate_backend(PID);
Before you keep reading, I just wanted to let you know about our fast, modern PostgreSQL GUI, Beekeeper Studio. Its SQL editor is a comfortable place to find and end runaway sessions, with autocomplete and a history of every query you’ve run, and it’s free to use for PostgreSQL, download it here.
Identifying Long-Running Queries
Before terminating a session in PostgreSQL, you need to know which session to target. To find long-running queries, you can use the pg_stat_activity view and filter by the state and query_start columns.
SELECT pid, usename, state, query, now() - query_start AS duration
FROM pg_stat_activity
WHERE state = 'active'
ORDER BY duration DESC;
Expected Output:
| pid | usename | state | query | duration |
|---|---|---|---|---|
| 1236 | appuser | active | UPDATE orders SET status=’paid’; | 00:20:34 |
| 1234 | postgres | active | SELECT * FROM users; | 00:05:12 |
Terminating a Session
Once you have identified the problematic session, you can terminate it using the pg_terminate_backend function.
SELECT pg_terminate_backend(1236);
This command forcibly terminaties the connection to the backend. It sends a SIGTERM signal to the process and allows it to terminate safely, offering a final chance to clean up before it terminates. Before executing this command, make sure to check the nature of the running queries and consider the output.
Automating Session Termination
For environments where session issues are frequent, you can automate session termination using a script. Here is an example of a basic script to terminate long-running queries:
DO $$
DECLARE
r RECORD;
BEGIN
FOR r IN
SELECT pid
FROM pg_stat_activity
WHERE state = 'active'
AND now() - query_start > interval '10 minutes'
LOOP
PERFORM pg_terminate_backend(r.pid);
END LOOP;
END $$;
This script will terminate any active session that has been running for more than 10 minutes.
Using the pg_cancel_backend function
Sometimes, it’s not desirable to kill a session but to simply cancel the query that the session is executing. In such cases, you can use the pg_cancel_backend function.
SELECT pg_cancel_backend(PID);
Replace PID with the process ID of the session with the query you want to cancel.
This command sends a SIGINT to the process to cancel the query. It’s less brute force compared to pg_terminate_backend, and allows the session itself to remain alive, even though the running query gets cancelled.
Remember, that both pg_cancel_backend and pg_terminate_backend have the superuser access requirement. If the session you are trying to terminate or cancel belongs to a superuser, you will need superuser privileges to run these commands successfully.
Conclusion
You now know how to kill sessions in PostgreSQL. Managing sessions in PostgreSQL is an important skill for database administrators. By effectively identifying and terminating problematic sessions, you can better maintain the performance and reliability of your PostgreSQL databases. Remember to use these commands judiciously, as their misuse can interrupt important data processing steps and compromise the integrity of your PostgreSQL databases.
Other posts you might enjoy:
Beekeeper Studio Is A Free & Open Source PostgreSQL GUI
This is the PostgreSQL client I’ve been looking for! The UI is what sets it apart: Clean, intuitive, powerful. Goodbye clunky PGAdmin. - David Cyprian
Beekeeper Studio is a fast, intuitive and easy to use PostgreSQL client. It supports loads of other databases too, and works great on Windows, Mac and Linux.
What PostgreSQL Users Say About Beekeeper Studio
Unlike pgAdmin, Beekeeper is remarkably simple to use yet not lacking in features–and the price is right! The database backup feature is particularly useful. Just great software.
Fabulous tool, the most practical IDE I’ve found to date for Postgres and MySql db admin - it just works, no fuss :-)