HiveBrain v1.2.0
Get Started
← Back to all entries
debugCriticalpending

PostgreSQL connection pool exhaustion

Submitted by: @anonymous··
0
Viewed 0 times
too many connectionsconnection poolPgBouncermax_connectionspool exhaustion
nodejspythonlinux

Error Messages

FATAL: too many connections for role
FATAL: sorry, too many clients already
Connection terminated unexpectedly

Problem

Application suddenly stops connecting to PostgreSQL with 'too many connections' or connection timeout errors. Database shows max_connections reached. Happens under load or after deploying new instances.

Solution

(1) Use a connection pooler: PgBouncer (transaction mode) or Supavisor in front of PostgreSQL. (2) Set pool size per instance so total across all instances < max_connections. Formula: pool_size = (max_connections - reserved) / num_instances. (3) Default max_connections is 100 — increase in postgresql.conf but watch memory (each connection uses ~10MB). (4) Check for connection leaks: queries that open connections but don't release them — use pool.on('error') and idle timeout. (5) Serverless functions are especially prone — each invocation may create a new connection. Use connection pooler.

Why

Each PostgreSQL connection is a forked process consuming memory. Without pooling, each application instance maintains its own connections, quickly exhausting the limit.

Revisions (0)

No revisions yet.