Troubleshooting guide
PostgreSQL "too many clients already": how to fix it
Postgres has run out of connection slots. Raising the limit is the tempting fix and rarely the right one, because each connection is a whole server process.
3 min read10 sectionsUpdated
On this page
Common causes
- Too many app instances, each with its own pool. Twenty pods with a pool of 20 is 400 connections.
- Connection leaks, where code forgets to return a connection.
- Slow queries holding connections for longer during a traffic rise.
- Idle-in-transaction sessions.
- Serverless functions, each opening its own connection.
Diagnose it
Keep a superuser session or use a reserved slot, then group connections by owner and state.
-- Run in psql
SELECT count(*), state, usename, application_name, client_addr
FROM pg_stat_activity GROUP BY 2,3,4,5 ORDER BY 1 DESC LIMIT 15;
SHOW max_connections;
-- Oldest idle-in-transaction sessions
SELECT pid, now() - xact_start AS age, left(query, 60)
FROM pg_stat_activity WHERE state = 'idle in transaction'
ORDER BY age DESC LIMIT 10;Fixes
As immediate relief, end leaked sessions with SELECT pg_terminate_backend(pid);. Then reduce per-instance pool sizes so that total connections across all instances stay under the limit. Fix leaks by always releasing connections in a finally block or using context managers. Set idle_in_transaction_session_timeout. Put PgBouncer in transaction-pooling mode in front of the database, which lets thousands of clients share a small number of server connections.
Why not simply raise maxconnections?
Each backend process costs memory, and more runnable processes than cores leads to contention that lowers total throughput. Benchmarks commonly find a peak at a few times the core count, well below what many applications configure. Changing the setting also requires a restart. A moderate increase is reasonable when the current value is truly undersized, but if connections are climbing because queries got slower, more connections will make the slowdown worse, not better.
Catch it automatically
Alert when used connections exceed 80 percent of the limit, because at 100 percent every new request fails. PostgreSQL monitoring charts connections by state, and APM shows time spent waiting for a connection, which is the first sign of pool pressure.
What is a good value for max_connections?
As low as works. With a pooler, a few hundred or fewer is common even on large systems.
What does PgBouncer transaction mode break?
Session features that span transactions, such as session-level advisory locks and SET commands. Check your driver and ORM support.
Why do I hit the limit only during deployments?
Old and new instances run together, briefly doubling the connections.
Related reading
- MySQL: Too many connections: Fix MySQL error 1040 Too many connections: check max_connections and current threads, find sleeping or leaked connections, size application pools.
- Nginx "worker_connections are not enough": how to fix it: Fix Nginx worker_connections are not enough: why proxying uses two connections per client, how to size limits, raise descriptors and add upstream keepalive.
- PostgreSQL idle in transaction: find and fix it: Find and fix PostgreSQL idle in transaction sessions: why they block vacuum and hold locks, which application patterns cause them and which timeout to set.
- PostgreSQL replication lag: causes and how to fix it: Diagnose PostgreSQL replication lag: use pg_stat_replication to tell send, write and replay delays apart, and fix slow replicas, long queries and WAL pile-ups.
- Too many TIME_WAIT connections: what to do: Understand Linux TIME_WAIT sockets, when they cause ephemeral port exhaustion, why keep-alive is the real fix and which kernel settings not to touch.
- 431 Request Header Fields Too Large: how to fix it: Fix 431 Request Header Fields Too Large: bloated cookies, Node.js header limits and localhost cookie build-up, with commands to measure real header size.
Watch your own site and services with VitalRix
Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.