Killing a specific session
Sometimes, the only way to let the system continue as a whole is by surgically terminating some offending database sessions. Yes, you read that right: surgically.
In this recipe, you will learn how to intervene, from gracefully canceling a query to brutally killing the actual process from the command line.
How to do it…
Once you have figured out the backend you need to kill, try to use pg_cancel_backend(pid)
, which cancels the current query, though only if there is one. This can be executed by anyone who is a member of the role whose backend is being canceled.
If that is not enough, then you can use pg_terminate_backend(pid)
, which kills the backend. This works even for client backends that are idle or idle in a transaction.
You can run these functions as a superuser
, or if the calling role is a member of the role whose backend pid
is being signed (look for the usename
field in the pg_stat_activity
view).
You can also grant...