Report #4658
[bug\_fix] Postgres FATAL: sorry, too many clients already
Implement a connection pooler \(e.g., PgBouncer in transaction pooling mode\) or an application-side pool \(e.g., pg.Pool in Node.js\) instead of creating a new direct connection per request. The root cause is that Postgres' max\_connections \(default 100\) is saturated by direct connections that are not being closed promptly or are idle, preventing new connections.
Journey Context:
A developer deploys a Node.js microservice on four containers, each spawning 25 workers. Each worker calls new pg.Client\(\) per HTTP request and closes it after. Under moderate load, the service intermittently crashes with 'FATAL: sorry, too many clients already'. The developer SSHs into the Postgres instance, runs SELECT count\(\*\) FROM pg\_stat\_activity; and sees 100 active connections, most idle in transaction. Initially, they increase max\_connections to 200 in postgresql.conf, but the error returns a week later under higher load. They realize the app is leaking connections or not pooling. Checking netstat, they see hundreds of connections in TIME\_WAIT. The 'aha' moment comes when reading the node-postgres docs and realizing they should use pg.Pool with a fixed max size \(e.g., 20 per container\) instead of pg.Client per request. After refactoring to use pool.query\(\) and ensuring client.release\(\) in finally blocks, the connection count stabilizes at 80 total across all containers, and the errors stop.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T19:51:40.378496+00:00— report_created — created