Free ops runbook · PostgreSQL

FATAL: sorry, too many clients already: pool math before you raise max_connections

The error does not say which client, and the first fix — raise max_connections — is often the one that turns a slow incident into an out-of-memory one. PostgreSQL is telling you that every backend slot is taken; the useful question is who is holding them and in what state.

This page is free. It covers the count to compare (replicas times pool size, not pool size), the states that waste a slot without doing work, and when a pooler is the right answer instead of a bigger limit.

Scope: reviewed from the PostgreSQL and PgBouncer documentation, no live reproduction
Verified against the PostgreSQL connection settings, monitoring views, client timeout settings and error-code appendix, and the PgBouncer configuration and feature documentation, on 2026-12-03. Command output below is the expected shape, not a capture from a live cluster.
Read the state

Four facts before you change a limit

The budget is per cluster

Every application replica opens its own pool. Ten pods with a pool of twenty want two hundred slots, not twenty. Compare the total demand with max_connections, not one process with it.

A slot can be idle and still wrong

idle in transaction holds a snapshot, blocks vacuum and keeps a backend. It looks cheap in CPU terms and is expensive in every other way.

Reserved slots have a purpose

superuser_reserved_connections exists so an administrator can log in during an incident. Monitoring and migrations that use a superuser quietly consume it.

More backends cost memory

Each backend has a baseline footprint, and each sort or hash can allocate up to work_mem. Raising the limit without headroom moves the failure from connection errors to the OOM killer.

Detect

Who is holding the slots

Command or viewWhat it tells you
show max_connections; show superuser_reserved_connections;The real budget. A managed provider may reset it on reboot from a parameter group, so also check where the value comes from.
select state, count(*) from pg_stat_activity group by 1 order by 2 desc;The split between active, idle and idle in transaction. A large idle group is a pool-sizing problem; a large idle-in-transaction group is an application problem.
select datname, numbackends from pg_stat_database order by 2 desc;Which database consumes the cluster budget. One tenant can starve the others.
select pid, application_name, client_addr, state, now() - state_change as idle_for, left(query, 60) from pg_stat_activity where state like 'idle in transaction%' order by idle_for desc limit 20;The oldest open transactions and the client that owns them. application_name is the fastest way to name the service.
select pid, pg_terminate_backend(pid) from pg_stat_activity where state = 'idle in transaction' and now() - state_change > interval '10 minutes';A bounded cleanup for stuck sessions. Confirm the owner first; terminating a migration mid-flight is its own incident.
ss -tn state established '( sport = :5432 )' | wc -lSocket count on the database host, which also exposes connections that never completed the startup handshake.
psql -p 6432 pgbouncer -c 'SHOW POOLS;'Client and server counts, waiting clients and the oldest wait, when a pooler sits in front.
pool.waitingCount / hikaricp_connections_pendingThe application side of the same queue. Rising waiters with a healthy database means the pool is too small for the latency, not that the database is out of slots.
Failure modes

Seven ways the slots run out

#Failure modeWhat you seeWhat to do
1Pool size multiplied by replicasA deploy scales from three pods to twelve and the cluster runs out of slots without a traffic increase.Budget replicas × pool size + reserved + headroom, and cap concurrency at the client, not at the server.
2Raising max_connections without memoryConnections succeed, then the host swaps or a backend is killed under load.Measure the resident size per backend, add work_mem for concurrent sorts, and only then raise the limit.
3A leaked clientActive count stays high at idle traffic, waitingCount climbs, and a restart clears it for a while.Find the code path that borrows without releasing; a pool with a leak detector and a borrow timeout makes it visible.
4idle in transactionLong xact_start, no query running, vacuum falling behind.Set idle_in_transaction_session_timeout and make the application commit or roll back before doing other work.
5Session state through a transaction poolerAfter moving to PgBouncer, prepared statements, SET, advisory locks or LISTEN behave differently or fail.Use transaction mode only for stateless queries, enable prepared-statement support on a current PgBouncer, or keep a session-mode pool for the jobs that need it.
6A reconnect storm after failoverEvery client reconnects at once and the primary is saturated before it finishes recovery.Add jittered backoff, cap simultaneous reconnects, and let the pooler absorb the herd.
7Administrators locked outThe monitoring user and the DBA both fail with the same error while the application holds every slot.Keep the superuser reserve untouched, monitor with a normal role, and leave a documented break-glass path.
Fix

The order that avoids a second incident

StepActionWhy here
1Count demand and statesYou cannot size a pool until you know whether the slots are working, idle, or stuck in a transaction.
2Cap the client poolThe cheapest fix is usually fewer connections doing more work, with a queue at the application.
3Add timeoutsidle_in_transaction_session_timeout, statement_timeout, and the pool's borrow timeout turn a leak into a visible error.
4Fix the leakA restart only resets the counter. The path that never releases will exhaust the new limit too.
5Add PgBouncerWhen the limit is connection count rather than query load, multiplexing many clients onto few server connections is the structural fix.
6Raise the limit lastWith measured memory headroom and a provider limit you have confirmed, not assumed.
Verify

Four drills in staging

Shrink the pool

Set one connection per replica and run the load test. Requests should queue at the pool and complete, not fail with too many clients.

Open a transaction and walk away

Begin a transaction, run one statement and idle. Confirm the timeout terminates it and that the application logs the cause instead of retrying blindly.

Fail over

Promote the replica and watch reconnect behaviour. The primary should see a shaped ramp, not every client in the same second.

Break the pooler

Stop PgBouncer and confirm the application degrades with a clear 503 path. A pooler that fails into a connection storm is worse than no pooler.

FAQ

Questions from the incident channel

Can we just raise max_connections to 500?

You can, if the memory is there and the provider allows it. It does not fix a leak, an idle transaction or a reconnect storm; it only moves the ceiling. Do the count first and raise the limit with measured headroom.

Why did it happen right after a deploy?

A new version usually means new pods, a new pool per pod, and a cold start where every instance connects at once. Check replica count and pool size in the same deploy diff.

Does PgBouncer fix idle transactions?

No. A transaction that stays open still holds a server connection in transaction mode. The timeout and the application fix are still required; the pooler only stops idle clients from holding a backend each.

Which pool size should we start from?

Start below the measured concurrent query count, not above it, and watch the wait time. A small pool with a short queue usually serves more throughput than a large pool contending for the same CPU.

Sources

Primary sources

Capacity errors are change-window errors

The paid runbooks cover the changes that move limits under you: a platform default, a migration window, and the rollback you have to prove before the change.

This page is free, no checkout. Paid runbooks are $9.99 each and covered by the 7-day refund policy at /runbooks/refund.