Full answer
PostgreSQL uses a process per connection, so thousands of connections waste memory and slow the server even before the limit. Raising max_connections is rarely the right fix.
What to change:
- Pool in the application with a fixed size per instance; total = instances x pool size must fit the server limit with headroom for admin access.
- Add PgBouncer in transaction mode when there are many app instances or serverless functions; many client connections then share a few server connections. Session features such as prepared statements and advisory locks need care in that mode.
- Find leaks: connections that are never returned, or sessions left
idle in transaction, visible inpg_stat_activity. Setidle_in_transaction_session_timeout. - Reduce hold time: shorter transactions free connections faster.
A small pool often gives better throughput than a large one, because the database works best with roughly as many active queries as it has CPU cores and disks to serve them.
Key points
- Connections are expensive in PostgreSQL
- Pool in the app and cap totals
- Use PgBouncer for many clients
- Find idle-in-transaction leaks
- Smaller pools often perform better
Common mistakes
- Raising max_connections to thousands
- Opening a connection per request without pooling
Follow-ups
- What breaks under PgBouncer transaction pooling?
- How would you size a pool?
Tips
- Explain why raising the limit is a poor fix