PostgreSQL "FATAL: sorry, too many clients already": causes and fixes

Postgres refuses new connections once max_connections is reached. How to find idle and leaked sessions, end them safely, add PgBouncer and size the limit so it stops happening.

· 6 min read · By the Spot Downtime team

New connections to your database start failing with one of these:

text
FATAL:  sorry, too many clients already
FATAL:  remaining connection slots are reserved for non-replication superuser connections

Both mean the same thing: PostgreSQL has reached max_connections. The second message appears when only the slots kept back for superusers are left (newer versions word it in terms of reserved roles). Every request that needs a new connection fails until one closes.

Why Postgres runs out sooner than you expect

The default max_connections is 100, and 3 of those are held back by superuser_reserved_connections. Each connection is a separate server process with its own memory, so Postgres is built for a modest number of busy connections, not thousands of idle ones. Modern deployments use them up fast: many app replicas each with a pool, background workers, migrations, and serverless functions that open a fresh connection per invocation.

Step 1: look at pg_stat_activity

Connect as a superuser, which can still use a reserved slot, and see what is holding connections:

sql
SELECT usename, application_name, client_addr, state, COUNT(*) AS n
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY usename, application_name, client_addr, state
ORDER BY n DESC;

The state column tells you the story:

  • idle: connected but doing nothing. A few per pool is normal; hundreds means pools that are too large, or connections leaked by the app.
  • idle in transaction: the app opened a transaction and never committed or rolled back. These are the most harmful, because they also hold locks and block vacuum.
  • active: running a query. Many long-running active sessions point to a slow query or lock contention that makes requests pile up.

To find the long ones:

sql
SELECT pid, usename, state, now() - state_change AS duration, left(query, 80) AS query
FROM pg_stat_activity
WHERE state <> 'idle' AND backend_type = 'client backend'
ORDER BY duration DESC
LIMIT 20;

Free up slots in an emergency

sql
-- End sessions stuck idle in a transaction for more than 10 minutes
SELECT pg_terminate_backend(pid)
FROM pg_stat_activity
WHERE state = 'idle in transaction'
  AND now() - state_change > interval '10 minutes';

Terminating a session rolls back its open transaction. Use pg_cancel_backend(pid) to stop only the current query while keeping the connection.

Step 2: let the server close stuck sessions

Two settings stop leaked sessions from holding slots forever:

sql
ALTER SYSTEM SET idle_in_transaction_session_timeout = '5min';
ALTER SYSTEM SET idle_session_timeout = '30min';  -- PostgreSQL 14+
SELECT pg_reload_conf();

Set idle_session_timeout carefully: a connection pool that keeps idle connections will see them closed and has to handle the reconnect. Set it longer than your pool's own idle timeout.

Step 3: put a pooler in front

The standard fix is PgBouncer. Apps open as many client connections as they like to PgBouncer, which shares a small pool of real server connections between them. In transaction mode, a server connection is used only for the length of a transaction, so a few dozen server connections can serve hundreds of clients.

ini
; pgbouncer.ini
[pgbouncer]
pool_mode = transaction
max_client_conn = 1000
default_pool_size = 20
max_prepared_statements = 100  ; PgBouncer 1.21+

Transaction mode caveats

In transaction mode, session state does not carry between transactions: session-level SET, advisory locks held across transactions and LISTEN do not work as expected. Older PgBouncer versions also break named prepared statements; version 1.21 and later support them when max_prepared_statements is set. Check your driver's documentation before switching.

Managed providers often include a pooler: Supabase, Neon and others offer a pooled connection string, and AWS offers RDS Proxy. For serverless functions, always use the pooled endpoint.

Step 4: raise max_connections only with a budget

Add up what you actually need:

text
replicas × pool size + workers + cron jobs + migrations + admin headroom  ≤  max_connections
  1. First, make each pool smaller. A pool of 5 to 10 per instance is plenty for most web apps.
  2. Then, if you still need more, raise the limit in postgresql.conf or with ALTER SYSTEM SET max_connections = 200. This one needs a full restart, not a reload.
  3. Check memory: each connection can use work_mem several times over in complex queries. On managed databases such as RDS, the default limit is derived from instance memory for this reason.

Monitor it before users see it

A TCP check on port 5432 stays green during this outage, because Postgres still accepts the TCP connection before refusing the client. Instead, expose a health endpoint in your app that runs SELECT 1 through its normal pool and returns an error status on failure. A website monitor on that endpoint, with a keyword check, alerts you within a minute of the first refused connection. For the trend, alert when the connection count passes about 80% of max_connections, so you have time to act. If a monitor and a user disagree about whether the site is up, see monitor says down but the site works.

Get new guides by email

Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.

Unsubscribe from any email with one click. See our privacy policy.

Keep reading