Cancel a running query (pg_cancel_backend)
PostgreSQLSessionsCommand
SELECT pg_cancel_backend(12345);What it does
Cancels the query currently running on the given PID; the connection stays open.
When to use it
To stop a long-running query without dropping the connection.
Caution
It only cancels an active query; it does not close a session in idle in transaction state, which requires pg_terminate_backend.
Related
- Terminate a backend (pg_terminate_backend)
- Show active and blocking queries
- Build the blocking tree (find the root blocker)
- Inspect lock waits in detail by object and lock mode (pg_locks)
- Find long-running and 'idle in transaction' transactions
- Terminate long 'idle in transaction' sessions in bulk, in a controlled way