Terminate a session in PostgreSQL
pg_terminate_backend ends the connection and rolls back its transaction. Use it when there is no statement to cancel, or the cancel did not work.
The task
pg_terminate_backend(pid) sends the backend a SIGTERM: the connection closes, its open transaction rolls back, and the application sees a connection error on its next call. In PostgreSQL the rollback itself is quick, because the changes stay in place as dead row versions for vacuum to clean up later, so the locks are released almost immediately even after a large write. It is the step after pg_cancel_backend, for sessions idle in transaction (nothing to cancel) and for sessions that ignore a cancel.
The snippet
Written for PostgreSQL. Versions for SQL Server are under Variations.
SELECT pid, usename, datname, application_name, state,
now() - xact_start AS xact_age,
now() - state_change AS idle_for,
LEFT(query, 100) AS last_query,
pg_terminate_backend(pid) AS terminate_requested
FROM pg_stat_activity
WHERE state = 'idle in transaction'
AND now() - state_change > interval '30 minutes'
AND backend_type = 'client backend'
AND pid <> pg_backend_pid()
ORDER BY state_change;
So funktioniert es
The recipe targets the common case, sessions idle inside an open transaction for over 30 minutes, but the same function terminates any pid. Run it without the pg_terminate_backend column first and read the list. Checks: leave out your own pid; keep backend_type = 'client backend' so WAL senders and background workers are never hit; prefer pg_cancel_backend when a statement is running; and remember the work is lost, so terminating a bulk import at 90 percent means starting it over. Without superuser, you can terminate only sessions of your own role or, with membership in pg_signal_backend, those of other non-superusers.
Variations
Terminate one explicit pid
SELECT pg_terminate_backend(12345);
SQL Server: check rollback progress after a KILL
SQL Server-- SQL Server rolls back by undoing the logged changes, which can take as long as the work did
KILL 53 WITH STATUSONLY;
Questions that come up
Terminate or cancel: what is the decision rule?
If the session is running a statement, cancel. If it is idle in transaction (no statement to cancel) and holding locks, or it ignored a cancel, terminate. Cancel rolls back the statement, and inside an explicit transaction leaves that transaction aborted until the client rolls back; terminate rolls back the whole transaction and drops the connection.
Can I make the database do this automatically?
Yes. idle_in_transaction_session_timeout ends exactly the sessions this query looks for, statement_timeout caps how long a statement may run, and lock_timeout caps how long a statement waits for a lock. PostgreSQL 17 added transaction_timeout, which caps a whole transaction. Set them per role (ALTER ROLE ... SET) so interactive and batch roles get different limits.
Keep reading
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.