ProductionIntermediate
Why "too many connections", and how do you fix it?
“The app fails with too-many-clients but the database CPU is idle. Explain.”
What this tests
- Connections as processes
- Pooling
Answers by level
Read the beginner answer first and notice what is missing.
Each Postgres connection is a process with its own memory. Many app instances each opening their own pool can exceed max_connections while the database is idle — it is starved of usable slots, not compute. Long-held "idle in transaction" connections make it worse.
Fix with a pooler (PgBouncer, transaction mode) multiplexing many clients onto a few server connections; raising max_connections just trades an error for contention.
Green flags · Red flags
Strong green flag · Sizes active connections to cores and queues the rest.
Green flags
- Connections = processes
- Pooling not raising the limit
- idle-in-transaction awareness
Red flags
- "Raise max_connections" as the fix
- Direct connections from every instance
Follow-up questions
F1
What does transaction-mode pooling break?
Scenario
40 app pods × 20 connections hit a 100-connection Postgres and it falls over at idle CPU. Fix it.