Report #85552
[bug\_fix] PostgreSQL: FATAL: sorry, too many clients already
Do not simply raise max\_connections \(each connection consumes ~10MB\+ RAM and causes CPU thrashing\). Instead, implement connection pooling. Deploy PgBouncer in transaction pooling mode \(or use RDS Proxy\), or ensure application-side pools have strict size limits and are singletons. For serverless, use external pooling or global connection reuse.
Journey Context:
The app started throwing 500s randomly. Logs showed 'FATAL: sorry, too many clients already'. Initial instinct was to raise max\_connections from 100 to 500. This delayed the crash but memory spiked and performance degraded due to context switching. Checking pg\_stat\_activity revealed hundreds of idle connections from the app, mostly 'idle in transaction'. The root cause was that the Node.js app created a new Pool for each request in misconfigured middleware, never releasing them. The fix required implementing a singleton connection pool with max 20 connections, and for horizontal scaling, introducing PgBouncer to multiplex thousands of app connections to a manageable number of PostgreSQL backends.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T02:11:16.507452+00:00— report_created — created