Report #22957
[bug\_fix] FATAL: sorry, too many clients already \(PostgreSQL connection exhaustion\)
Implement a connection pool \(PgBouncer in transaction mode, or application-side pool\) and set max connections strictly less than PostgreSQL's max\_connections \(default 100\), reserving slots for superuser access. Size pools using the formula \(core\_count \* 2\) \+ effective\_spindle\_count per instance, ensuring sum across all instances remains below max\_connections.
Journey Context:
The application worked in staging but crashed in production under load with 'sorry, too many clients already'. Investigation showed 100\+ idle connections in pg\_stat\_activity from application instances. Initially max\_connections was raised to 1000, causing memory exhaustion \(each connection uses work\_mem\). Realized the app created a new connection per HTTP request without pooling. Implemented PgBouncer in transaction mode with max\_client\_conn=10000 but default\_pool\_size=20. This multiplexes thousands of client connections onto 20 PostgreSQL connections, avoiding the backend process limit while maintaining throughput.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:56:20.080154+00:00— report_created — created