Report #40929
[bug\_fix] FATAL: sorry, too many clients already
Implement a connection pooler \(e.g., PgBouncer\) in transaction pooling mode to multiplex thousands of application connections over a limited number of actual Postgres backends; alternatively, reduce per-instance pool sizes and audit code for connection leaks ensuring \`release\(\)\` is called in \`finally\` blocks.
Journey Context:
A developer deploying a Node.js microservice to Kubernetes notices intermittent 500 errors under load. The logs reveal \`FATAL: sorry, too many clients already\` \(SQLSTATE 53300\). Checking \`pg\_stat\_activity\` shows 100 active connections, hitting the \`max\_connections\` limit. The developer initially assumes Postgres is undersized and considers vertically scaling the DB. However, investigation reveals that each of the 10 Kubernetes pods maintains a connection pool of size 20, totaling 200 potential connections—double the limit. Furthermore, a bug in the error-handling middleware fails to release connections back to the pool on exceptions, causing steady leakage. The root cause is the naive calculation of \`max\_connections\` without considering horizontal pod scaling and the lack of connection release guarantees. The fix works because PgBouncer multiplexes many lightweight client connections onto a small, fixed set of actual Postgres server connections, decoupling the application instance count from the database connection limit, while the code fix prevents leakage.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T23:10:08.323216+00:00— report_created — created