Cancel or terminate a backend
pg_killCancel a running query or terminate a backend connection by PID to clear stuck sessions. Signals the PostgreSQL backend gracefully or forcefully, requiring proper user permissions.
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. When signaled=false, the note field surfaces postgres's NOTICE explaining why (e.g. 'not a PostgreSQL backend process' for a non-pg PID, 'must be a member of...' for permission denial) so an agent can act on the specific cause rather than guess from a three-way list.
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 NOTICE explaining why -- act on this, not on the boolean. | |
| signaled | Yes | What pg_cancel_backend / pg_terminate_backend returned. |