The symptom#
New sessions fail with one of these, which PostgreSQL's source prints verbatim:
FATAL: sorry, too many clients already
FATAL: remaining connection slots are reserved for roles with the SUPERUSER attribute
FATAL: remaining connection slots are reserved for roles with privileges of the "pg_use_reserved_connections" role
The first means every slot up to max_connections is taken. The other two mean only the reserved slots are left: superuser_reserved_connections (three by default) for superusers, and reserved_connections (zero by default) for roles with privileges of pg_use_reserved_connections. Either way the database is at its limit for ordinary roles. too many clients source · Reserved slot messages · Connection settings
If the client instead reports a pool timeout, start with the pool timeout guide; it hands off here when the pool cannot open connections. If you are planning capacity for a rollout rather than finding today's holders, read connection budgets during rolling deployments.
Get in to look#
The reserved slots exist for this moment. A superuser, or a role with pg_use_reserved_connections when reserved_connections is above zero, can still connect when ordinary roles cannot. Use that role from one client, run read-only queries, and disconnect: each extra session you open takes one of the few remaining slots.
The role also needs to see other roles' sessions. Ordinary users see all columns only for sessions of roles they belong to; for everyone else's sessions many columns are null, although the session's user and database stay visible. Superusers and roles with privileges of pg_read_all_stats see everything. A grouping run as the application's own role undercounts the other clients. Statistics visibility
SHOW max_connections;
SHOW superuser_reserved_connections;
SHOW reserved_connections;
Group the holders#
Count client backends only; pg_stat_activity also lists background processes such as the checkpointer and autovacuum workers, with their own backend_type. pg_stat_activity columns
SELECT application_name, usename, client_addr, state, count(*) AS sessions,
min(backend_start) AS oldest_connect,
max(now() - state_change) AS longest_in_state
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY application_name, usename, client_addr, state
ORDER BY sessions DESC
LIMIT 30;
What the columns tell you:
application_nameis whatever the client set at connect time. Empty means the client never set it.usenameis the role. Services that share one role cannot be told apart by it.client_addris the client's IP address, or null for a Unix socket or an internal process. Behind a pooler it is the pooler's address.stateisactive(executing),idle(waiting for a command),idle in transaction(inside a transaction, not executing) oridle in transaction (aborted)(the same after an error).backend_startis when the client connected.state_changeis when the state last changed.
Then list the sessions that have sat in an open transaction longest. These hold locks and prevent vacuum from removing dead rows as well as holding a slot. idle_in_transaction_session_timeout
SELECT pid, application_name, usename, client_addr, client_port,
now() - xact_start AS in_transaction_for,
now() - state_change AS idle_for, left(query, 80) AS last_query
FROM pg_stat_activity
WHERE state IN ('idle in transaction', 'idle in transaction (aborted)')
ORDER BY xact_start
LIMIT 20;
For idle sessions, query shows the last statement that ran, not a current one. Query text is truncated at 1024 bytes by default (track_activity_query_size).
Map an address to a workload#
When application_name is empty, client_addr is the lead. On AWS, find the network interface with that private address; its description, interface type and requester identify an ECS task, a Lambda function, an instance or a load balancer. On Kubernetes, find the pod by IP. describe-network-interfaces · Field selectors
aws ec2 describe-network-interfaces \
--profile PROFILE --region REGION \
--filters Name=addresses.private-ip-address,Values=10.0.12.34 \
--query 'NetworkInterfaces[].{id:NetworkInterfaceId,type:InterfaceType,requester:RequesterId,description:Description,subnet:SubnetId}' \
--output json --no-cli-pager
kubectl get pods --all-namespaces --field-selector status.podIP=10.0.12.34 -o wide
Then set application_name in every client so the next incident skips this step. libpq-based drivers take it as a connection parameter or from PGAPPNAME; the PostgreSQL JDBC driver takes ApplicationName. Putting the release in the name lets you split holders by version, as tagging connections with the release shows. libpq application_name · PGAPPNAME · pgjdbc ApplicationName
What the grouping says#
| Largest group | Likely holder | Look at next |
|---|---|---|
One application, mostly idle | Pools sized or multiplied beyond the budget | Task count times pool size; a recent scale-out |
One application, idle in transaction | Code holding transactions open | Its slowest downstream call; leak logs |
Many active, long query_start ages | Slow queries holding sessions | Locks and plans, not connection counts |
Empty application_name, one address | An unlabeled job or tool | The workload behind the address |
Old backend_start on retired addresses | Connections from hosts no longer running | Whether those clients are gone |
The last row matters after a failover or a scale-in: if a client vanished without closing its connections, the server may keep the session until TCP keepalive or tcp_user_timeout notices, and those settings default to the operating system's values. TCP keepalive settings
A worked example#
This example is illustrative. At 16:40 UTC the orders database (max_connections 200) refuses checkout with remaining connection slots are reserved for roles with the SUPERUSER attribute. An operator connects as a superuser and runs the grouping (rows from hosts in one subnet are combined here):
| application_name | usename | client_addr | state | sessions | longest_in_state |
|---|---|---|---|---|---|
| checkout | app | 10.0.12.0/24 (8 hosts) | idle | 80 | 00:00:04 |
| (empty) | app | 10.0.40.17 | idle in transaction | 61 | 00:47:12 |
| orders | app | 10.0.13.0/24 (4 hosts) | idle | 44 | 00:00:02 |
| checkout | app | 10.0.12.0/24 | active | 12 | 00:00:01 |
Checkout's 92 sessions match 8 tasks with pools of 12, and they turn over every few seconds. The 61 sessions from 10.0.40.17 have sat in open transactions for 47 minutes. The network interface lookup names an ECS task of a fictional payments-reconcile job started at 15:52; its last query in each session is a SELECT ... FOR UPDATE on the ledger table.
In one version of this incident, the job's log shows it stuck waiting on a third-party API with no timeout, inside each transaction. The holder is the job, and stopping it frees 61 slots. In the other version, the job finished at 15:58 and its task stopped, yet the sessions remain: the client is gone and the server has not noticed. The sessions still hold their locks. Ending them is a write that the database owner decides on; the read here only names them.
Limits#
pg_stat_activity is a snapshot whose columns are not read atomically, so run the grouping twice a minute apart before drawing a conclusion. A pooler in front of PostgreSQL hides the real clients; read the pooler's own client list for them. The grouping says who holds slots now, not who held them when the first refusal happened; for that you need the database log or a connection-count metric from earlier. See all runtime dependency failures for the neighbouring cases.
Questions#
Are pg_stat_activity queries read-only?
Yes. Selecting from the view and reading settings with SHOW change nothing. Functions such as pg_terminate_backend and pg_cancel_backend do change state; they are not part of this read.
Why is client_addr null for some sessions?
The session connected over a Unix socket on the database host, or it is an internal process such as autovacuum. Filtering on backend_type = 'client backend' removes the internal ones.
Why do I see sessions but no query text for other applications?
Your role cannot see details of other roles' sessions. Use a superuser or a role with privileges of pg_read_all_stats.