Report #70245
[bug\_fix] FATAL: sorry, too many clients already
Use a connection pooler such as PgBouncer in transaction pooling mode, or size the application pool so that \(workers × pool\_size\) stays well under PostgreSQL's max\_connections. Close sessions/cursors with context managers, set idle\_in\_transaction\_session\_timeout, and check for connection leaks. Raising max\_connections is a temporary band-aid because each backend consumes shared memory and process slots.
Journey Context:
You deploy a FastAPI service behind ten uvicorn workers. Under load it starts throwing FATAL: sorry, too many clients already. pg\_stat\_activity shows hundreds of idle connections. You first raise max\_connections from 100 to 500 and restart Postgres; the error disappears for a day but returns under heavier load, and memory usage climbs. Each worker opened its own SQLAlchemy pool of 20 connections and never closed them. The root cause is that PostgreSQL's max\_connections is a hard server limit and every backend connection costs memory and a process slot; without an intermediary, the number of app connections scales linearly with workers. The fix is to size the app pool to roughly \(max\_connections / worker\_count - headroom\), close sessions with context managers, set idle\_in\_transaction\_session\_timeout, and ideally place PgBouncer in transaction pooling mode so thousands of short client requests share a small number of Postgres backends. Pooling multiplexes many short requests onto a few long-lived server connections, which is how Postgres scales horizontally for connection-hungry apps.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T00:29:11.101290+00:00— report_created — created