Skip to content

Why your managed Postgres connection pool is exhausted

A "too many connections" error on managed Postgres is almost never about raising max_connections. It is about what is holding connections open and why.

A managed Postgres instance with max_connections = 200 should be plenty for an application serving moderate traffic. When the error log fills with FATAL: sorry, too many clients already, the first instinct is to raise the limit. The first instinct is wrong. Postgres connections are expensive — each is a backend process holding work_mem, parse caches, and a slot on the primary. The right answer is almost always a pooler in front and a code change behind.

What’s happening

Without a connection pooler, every application worker holds its own long-lived connection to Postgres. An autoscaling Cloud PaaS deployment with 12 application nodes and 40 PHP-FPM workers each is sitting on 480 potential connections before the database has done any real work. Add a background worker fleet, a few scheduled jobs, and the staging environment pointing at the same instance for a debugging session, and you exhaust 200 connections without serving a single extra request.

How to verify

See who is connected right now:

SELECT application_name, state, count(*)
FROM pg_stat_activity
WHERE datname = current_database()
GROUP BY application_name, state
ORDER BY count(*) DESC;

A large idle count from one application_name is your culprit: workers are holding open connections they are not actively using. A large idle in transaction count is worse — those are transactions that started, did not commit, and are holding row locks. Find the oldest:

SELECT pid, now() - xact_start AS age, state, query
FROM pg_stat_activity
WHERE state IN ('idle in transaction', 'idle in transaction (aborted)')
ORDER BY xact_start ASC
LIMIT 10;

Anything over a few seconds is a code bug — usually a transaction opened in a handler that returned early without committing or rolling back.

The fix

  1. Put PgBouncer (or the managed pooler that ships with the service) in front of Postgres in transaction-pooling mode. The application connects to the pooler with a small per-worker pool; the pooler maintains a much smaller real connection pool to Postgres. The 480 application-side connections collapse to 30 or 40 server-side connections without losing throughput.
  2. Set a statement_timeout and an idle_in_transaction_session_timeout. Five seconds and thirty seconds respectively are reasonable defaults for an OLTP workload. The first kills runaway queries; the second kills the “I forgot to commit” bug before it locks the table.
  3. Fix the application code that opens transactions and forgets them. The idle in transaction query above will name the culprit by application_name and SQL fragment.
  4. Only after the pooler is in place and the timeouts are set, consider whether max_connections actually needs to go up. Most of the time it does not.

The connection storm stops, the database has room to breathe, and the next vertical resize is for a real reason instead of a configuration symptom.

If your managed Postgres keeps hitting the connection ceiling, our Cloud PaaS team can put a pooler in and trace the leaking transactions — request a session.