Cancel or terminate a backend
pg_killCancel a running query or terminate a backend connection by PID. Use to stop problematic queries when pg_health shows active_queries or locks.
Instructions
Cancel a running query (SIGINT-equivalent) or terminate a backend connection (SIGTERM-equivalent) by PID. Find the PID via pg_health active_queries or pg_inspect_locks. Requires ALLOW_WRITES=1 since this changes database session state. The role in DATABASE_URL must have permission - cancelling another user's query needs the pg_signal_backend role or superuser. Note: pg_signal_backend does NOT cover superuser-owned backends - only a superuser can signal another superuser's session. Cancel is graceful; terminate is forceful. signaled=true means postgres SENT the signal, not that the target stopped: a session that is idle (or idle in a transaction) ignores a cancel, so re-check with pg_health and use terminate for an idle session. signaled=false means postgres found no backend with that PID; the note field carries its own warning (e.g. 'PID 123 is not a PostgreSQL backend process'). A permission denial is NOT a false: postgres raises, so the call returns an error with SQLSTATE 42501 that says what the role lacks.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pid | Yes | Backend PID to signal. | |
| mode | No | `cancel` aborts the current query; `terminate` closes the connection entirely. | cancel |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| pid | Yes | Echoed back from the request. | |
| mode | Yes | Echoed back, after the safer 'cancel' default is applied. | |
| note | Yes | On `signaled: false`, postgres's own warning explaining why -- act on this, not on the boolean. | |
| signaled | Yes | What pg_cancel_backend / pg_terminate_backend returned: true once the signal is SENT, not once it has taken effect. |