Prompt
How do I avoid connection limit issues in PostgreSQL?
Latest observation
To avoid PostgreSQL connection limit issues, the main goal is to reduce the number of active database connections and reuse them efficiently.
Best practices
1. Use a connection pool
This is the most common and effective fix.
- In application code, use a pool instead of opening a new connection per request.
- For many apps, keep pool sizes small and controlled.
- If you have many app instances, the total across all instances must still stay below PostgreSQL’s
max_connections.
Examples:
- Node.js:
pg.Pool - Java: HikariCP
- Python:
psycopgpool or SQLAlchemy pooling - Go:
database/sqlalready uses pooling
2. Add PgBouncer
PgBouncer is a lightweight connection pooler in front of PostgreSQL.
Use it when:
- You have many short-lived connections
- You run many app instances or serverless functions
- You need to protect PostgreSQL from connection spikes
Common pooling mode:
- Transaction pooling for high concurrency and many clients
- Session pooling only if your app needs session-level features
3. Keep connection counts low in each app instance
A common mistake is setting a large pool size per container or VM.
Example:
- 20 app instances
- pool size 20
- total potential connections = 400
That can easily exceed PostgreSQL limits.
Rule of thumb:
- Start small, like 5–10 connections per instance
- Scale carefully based on load tests
4. Close connections properly
Make sure connections are returned to the pool or closed when no longer needed.
Watch out for:
- Leaking connections in error paths
- Long-running transactions
- Forgotten cursors or open sessions
5. Avoid long-running idle transactions
Idle transactions hold resources and can tie up connections unnecessarily.
Enable monitoring for:
idle in transaction- very long queries
- stuck sessions
6. Use read replicas for read-heavy workloads
If reads dominate, offload them to replicas to reduce pressure on the primary.
7. Tune PostgreSQL only if needed
You can increase max_connections, but this is usually not the first fix.
Why not just raise it?
- More connections consume more memory
- Too many connections can hurt performance
- PostgreSQL scales better with pooling than with very high connection counts
8. Use serverless-friendly patterns
If your app uses serverless functions:
- Don’t create a new DB connection on every invocation if you can avoid it
- Reuse connections across warm invocations
- Prefer PgBouncer or managed proxy solutions
Examples:
- AWS RDS Proxy
- Cloud SQL Auth Proxy
- PgBouncer
Practical recommendation
For most systems:
- Use a small app-level pool
- Put PgBouncer in front of PostgreSQL
- Monitor connection usage and long-running sessions
- Increase
max_connectionsonly after you’ve optimized pooling
Helpful monitoring queries
See current connections:
SELECT state, COUNT(*)
FROM pg_stat_activity
GROUP BY state;
See active sessions:
SELECT pid, usename, state, query, backend_start
FROM pg_stat_activity
ORDER BY backend_start;
Find idle in transaction sessions:
SELECT pid, usename, state, query, xact_start
FROM pg_stat_activity
WHERE state = 'idle in transaction';
If you want, I can also give you:
- a recommended pool sizing formula
- PgBouncer setup advice
- or connection pooling examples for your language/framework.