Skip to content

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

Key points

  • Each connection is a backend process with its own memory.
  • pg_stat_activity shows who holds them and what they are doing.
  • Idle-in-transaction connections are the usual waste.
  • A pooler such as PgBouncer is the proper fix at scale.

What it means

Every slot up to max_connections, minus superuser_reserved_connections, is taken. New clients get FATAL: sorry, too many clients already. Applications then fail at the point of connecting, often with what looks like a total database outage, even though the server is fine.

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.

bash
-- 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.

Frequently asked questions

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.

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.