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.
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.
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.
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.
superuser_reserved_connections exists so an administrator can log in during an incident. Monitoring and migrations that use a superuser quietly consume it.
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.
| Command or view | What 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 -l | Socket 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_pending | The 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 mode | What you see | What to do |
|---|---|---|---|
| 1 | Pool size multiplied by replicas | A 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. |
| 2 | Raising max_connections without memory | Connections 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. |
| 3 | A leaked client | Active 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. |
| 4 | idle in transaction | Long 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. |
| 5 | Session state through a transaction pooler | After 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. |
| 6 | A reconnect storm after failover | Every 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. |
| 7 | Administrators locked out | The 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. |
| Step | Action | Why here |
|---|---|---|
| 1 | Count demand and states | You cannot size a pool until you know whether the slots are working, idle, or stuck in a transaction. |
| 2 | Cap the client pool | The cheapest fix is usually fewer connections doing more work, with a queue at the application. |
| 3 | Add timeouts | idle_in_transaction_session_timeout, statement_timeout, and the pool's borrow timeout turn a leak into a visible error. |
| 4 | Fix the leak | A restart only resets the counter. The path that never releases will exhaust the new limit too. |
| 5 | Add PgBouncer | When the limit is connection count rather than query load, multiplexing many clients onto few server connections is the structural fix. |
| 6 | Raise the limit last | With measured memory headroom and a provider limit you have confirmed, not assumed. |
Set one connection per replica and run the load test. Requests should queue at the pool and complete, not fail with too many clients.
Begin a transaction, run one statement and idle. Confirm the timeout terminates it and that the application logs the cause instead of retrying blindly.
Promote the replica and watch reconnect behaviour. The primary should see a shaped ramp, not every client in the same second.
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.
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.
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.
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.
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.
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.