---
title: "Who is holding all the PostgreSQL connections right now? | Proofline"
description: "When PostgreSQL refuses new sessions, group pg_stat_activity by application, role, client address and state to see who holds the slots."
url: https://proofline.sh/learn/postgres-who-holds-connections/
---

[Learning center](/learn/)

# Who is holding all the PostgreSQL connections right now?

When PostgreSQL refuses new sessions, group pg\_stat\_activity by application, role, client address and state to see who holds the slots. Do it from a reserved slot with a role that can see other sessions, and read only. The answer is usually one client with too many idle sessions, a set of sessions stuck in an open transaction, or a crowd of slow active queries, and each points somewhere different.

Capacity overruns The Proofline team November 23, 2026 6 min read

## 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](https://raw.githubusercontent.com/postgres/postgres/master/src/backend/storage/lmgr/proc.c) · [Reserved slot messages](https://raw.githubusercontent.com/postgres/postgres/master/src/backend/utils/init/postinit.c) · [Connection settings](https://www.postgresql.org/docs/current/runtime-config-connection.html)

If the client instead reports a pool timeout, start with the [pool timeout guide](/learn/connection-pool-timeout-or-slow-database/); 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](/learn/connection-limits/).

## 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](https://www.postgresql.org/docs/current/monitoring-stats.html)

```
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](https://www.postgresql.org/docs/current/monitoring-stats.html)

```
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_name` is whatever the client set at connect time. Empty means the client never set it.
-   `usename` is the role. Services that share one role cannot be told apart by it.
-   `client_addr` is the client's IP address, or null for a Unix socket or an internal process. Behind a pooler it is the pooler's address.
-   `state` is `active` (executing), `idle` (waiting for a command), `idle in transaction` (inside a transaction, not executing) or `idle in transaction (aborted)` (the same after an error).
-   `backend_start` is when the client connected. `state_change` is 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](https://www.postgresql.org/docs/current/runtime-config-client.html)

```
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](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-network-interfaces.html) · [Field selectors](https://kubernetes.io/docs/concepts/overview/working-with-objects/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](/learn/postgres-which-release-is-connected/) shows. [libpq application\_name](https://www.postgresql.org/docs/current/libpq-connect.html) · [PGAPPNAME](https://www.postgresql.org/docs/current/libpq-envars.html) · [pgjdbc ApplicationName](https://jdbc.postgresql.org/documentation/use/)

## 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](https://www.postgresql.org/docs/current/runtime-config-connection.html)

## 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](/learn/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`.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Pool acquire timeout: a busy pool or a struggling database?](/learn/connection-pool-timeout-or-slow-database/)
    
    Capacity overruns The Proofline team November 23, 2026 6 min read
    
-   ### [Tag PostgreSQL connections with the release that opened them](/learn/postgres-which-release-is-connected/)
    
    Breaking migrations The Proofline team November 22, 2026 3 min read
