pg_stat_activity is a PostgreSQL system view that shows information about current server sessions and queries. It exposes details such as session state, connected user, database, query text and wait events, which makes it useful for monitoring active workload and diagnosing locks or long-running statements.
How pg_stat_activity works
pg_stat_activity is a live server view, not a historical log. It reflects the sessions currently known to PostgreSQL, so its value is in giving operators a point-in-time picture of who is connected, what each session is doing, and whether a backend is active, idle, waiting, or blocked.
That makes the view especially useful during incidents and performance investigations, because it narrows the search from “something is slow” to a specific session, query, or wait condition. It is a diagnostic lens on runtime behaviour, so its usefulness depends on how current the snapshot is when you query it.
What the view reveals about session state
The key value in pg_stat_activity is the session context it exposes. You can typically see the connected role, database, backend state, query text, wait events, and timing information, which helps distinguish a long-running query from an idle connection or a session that is blocked behind another lock.
That distinction matters because not every slow response is caused by the same problem. A busy system may include active execution, lock contention, connection pile-up, or application churn, and the view helps separate those cases before deeper tuning or remediation begins.
Why it matters for troubleshooting and monitoring
For database operations, pg_stat_activity is often the first place to look when you need to understand what the server is doing right now. It is useful for identifying runaway queries, spotting blocked sessions, finding connection storms, and correlating application behaviour with database pressure.
Because it shows current queries and session metadata, it also supports operational triage during outages. A short-lived issue may disappear before a full logging pipeline captures it, but a timely query against this view can still show the active workload and the session relationships that caused the symptom.
Access, visibility, and security implications
The view is operationally useful, but it can also expose sensitive context about how the database is being used. Query text may reveal table names, business logic, literal values, or internal workflows, and session metadata can disclose which users or applications are connected and how frequently they interact with the system.
That visibility is valuable for diagnostics, but it also means access should be limited to people and tooling that genuinely need it. In practice, teams often combine it with broader database monitoring controls, because the same detail that helps detect locks or long-running statements can also reveal information that should not be broadly visible.
How to interpret the output safely
pg_stat_activity should be read as a live operational signal, not as proof of compromise or proof of innocence. A session that appears idle may still be important, and a query that looks harmless may be part of a chain of blocking or resource exhaustion elsewhere in the system.
Use it alongside logs, performance counters, and application traces when you need to understand cause and effect. The view is strongest when it is treated as one layer in a wider diagnostic picture, especially in environments where concurrency, connection pooling, and application retries can make symptoms misleading.
Risk and Threat Considerations
pg_stat_activity can expose enough runtime detail to help an attacker understand which users, applications, and queries are active, and it can reveal query text that should not be broadly observable. In a compromised or over-permissioned environment, that visibility can assist reconnaissance, targeted abuse, or follow-on movement through the database workload.
Failure mechanism: Excessive visibility into live sessions, combined with weak access control or poor query hygiene, can leak operationally sensitive information and make it easier to identify valuable targets inside the database.
Impact: The result can include disclosure of business logic, sensitive literals, privileged activity patterns, or blocking conditions that an attacker or insider can use to refine exploitation, evade detection, or disrupt service.
Standards & Framework Alignment
This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.
OWASP API Security Top 10 addresses the attack and risk surface, while NIST SP 800-53 Rev 5, CIS Controls v8 and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.
| Framework | Control / Reference | Relevance |
|---|---|---|
| NIST SP 800-53 Rev 5 | AU-6 — Audit Record Review, Analysis, and Reporting | pg_stat_activity supports live review of database activity and anomalies. |
| AC-6 — Least Privilege | The view can expose query and session details, so access should be limited to needed roles. | |
| IA-5 — Authenticator Management | Session visibility helps investigate authenticated database activity and unusual connection patterns. | |
| Recommendation — Review active session data to detect blocking, runaway queries, and unusual database behaviour. Restrict visibility of session and query details to operators who need diagnostic access. Investigate unexpected session creation and reuse when authenticated database activity looks abnormal. | ||
| CIS Controls v8 | CIS-6 — Access Control Management | Database session inspection is an access-control concern because it reveals active users and workload details. |
| Recommendation — Limit who can inspect live database sessions and protect detailed query visibility. | ||
| OWASP API Security Top 10 | API8 — Security Misconfiguration | Overexposed session views are often a configuration issue in database and application monitoring. |
| Recommendation — Harden database monitoring and privilege settings so live session data is not overexposed. | ||
| NIST CSF 2.0 | PR.AA-05 — Access Permissions and Authorizations | Access to live session information should follow explicit authorization boundaries. |
| Recommendation — Authorize visibility into live database sessions only for roles with a clear operational need. | ||
Practitioner Guidance
Why practitioners should care: The main governance decision is not whether to use pg_stat_activity, but who is allowed to see it and in what form. Database operators usually need the full diagnostic view, while broader operational audiences may only need summarized telemetry or redacted query details.
What to watch for: Treat unexpectedly long-lived sessions, repeated waits, sudden connection surges, and frequently blocked statements as signals worth investigating. Those patterns often point to application defects, lock contention, connection management problems, or resource pressure rather than a single isolated query.
Practitioner takeaway: Use the view to accelerate diagnosis, but pair it with least-privilege access, logging, and monitoring so the diagnostic benefit does not become unnecessary exposure.
Related resources from NHI Mgmt Group
- How should security teams monitor AI agent activity without disrupting developers?
- How can SOC teams use identity context to improve response to agent activity?
- What is the difference between activity metrics and risk metrics in IAM?
- How can organisations tell legitimate automation from compromised service account activity?
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 3, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org