DBAtlas · Database operations atlas

Cancel a running query (pg_cancel_backend)

PostgreSQLSessions

Command

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.

Open in DBAtlas →

Related