liveBook · Manning

Pg_stat_activity

As explained in PostgreSQL Mistakes and How to Avoid Them

pg_stat_activity is a PostgreSQL system view that exposes information about server processes and their current activity.

pg_stat_activity is a PostgreSQL system view for observing server processes and their current activity. Use it when performance symptoms need to be tied to work actually happening inside the database: excessive concurrent activity, long-running statements, or clues that point back to application code, schema design, configuration, or ingestion. section 11.3.3 section 6.3

A common mistake is to treat the number of client connections as the database’s real workload. When many sessions access the same objects, Lightweight Locks (LWLocks) can become contended and reduce throughput; many rows showing wait_event_type LWLock in pg_stat_activity indicate possible excessive concurrency. The safer response is usually to limit clients, let requests queue for available connections, or place a pooler such as PgBouncer in front of PostgreSQL rather than continually raising connection capacity. section 6.3 section 6.3

Checking a long-running active statement

For actively executing queries, inspect now() - query_start in pg_stat_activity to find statement duration. Do not equate this with every slow completion: a query may be delayed because it is blocked rather than actively executing. Long execution holds a transaction snapshot, which can prevent autovacuum from cleaning dead tuples and contribute to bloat and XID wraparound. If necessary, pg_cancel_backend(<PID>) can stop a long-running query; that is generally safe for the database, though the client application may be affected. section 6.5.2 section 6.5.2 section 6.5.2

Use the view as part of a methodical investigation, not as an isolated tuning ritual. Combine activity monitoring with logs, pg_stat_statements, schema inspection, exploratory queries, and configuration review; then turn confirmed findings into small, measurable repair projects. section 11.3.3

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Uses wait_event_type and now() - query_start as operational signals, while deferring exhaustive view-reference detail.
Examples
Uses many LWLock wait events, now() - query_start, and pg_cancel_backend(<PID>) to investigate overloaded or long-running work.
What it assumes
Assumes familiarity with PostgreSQL performance investigation and SQL, while grounding diagnosis in activity, waits, and query duration.
What you can do after
Diagnose/debug with it by recognizing excessive concurrency and identifying actively running long queries for remediation.
How it connects
Connects concurrency and LWLocks with connection pooling, and connects long-running queries with autovacuum, bloat, XID wraparound, and replication.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.