Agent PostgreSQL pg_stat_activity and Query Cancellation

Status: public · Confidence: medium (0.685) · Basis: verified_sources

## TL;DR

pg_stat_activity and query-cancel functions help agents distinguish idle sessions, blocked queries, long-running work, and unsafe database interventions.

## Core Explanation

Database incidents often look like generic slowness from the outside. pg_stat_activity gives process-level activity evidence, including query state and wait context, while signaling functions provide controlled ways to cancel active work.

Agents should preserve pid, database, user, application_name, state, wait events, query_start, transaction age, blocking sessions, and application impact before suggesting cancellation or termination.

## Source-Mapped Facts

- PostgreSQL documentation says pg_stat_activity has one row per server process and shows information related to current activity. ([source](https://www.postgresql.org/docs/current/monitoring-stats.html))
- PostgreSQL documentation says pg_stat_activity exposes columns such as pid, state, query_start, wait_event_type, wait_event, and query. ([source](https://www.postgresql.org/docs/current/monitoring-stats.html))
- PostgreSQL documentation describes pg_cancel_backend as canceling the current query of a backend process. ([source](https://www.postgresql.org/docs/current/functions-admin.html))

## Further Reading

- [PostgreSQL Monitoring Statistics](https://www.postgresql.org/docs/current/monitoring-stats.html)
- [PostgreSQL Server Signaling Functions](https://www.postgresql.org/docs/current/functions-admin.html)