debugsqlpostgresqlCriticalpending
Debug: PostgreSQL connection pool exhaustion
Viewed 0 times
connection poolexhaustiontoo many clientspgbouncerpg_stat_activity
Error Messages
Problem
Application hangs or times out acquiring database connections. Errors like 'connection pool exhausted' or 'too many clients already'.
Solution
- Check current connections:
SELECT count(*), state, wait_event_type
FROM pg_stat_activity
GROUP BY state, wait_event_type;- Find long-running queries holding connections:
SELECT pid, now() - pg_stat_activity.query_start AS duration,
query, state
FROM pg_stat_activity
WHERE (now() - pg_stat_activity.query_start) > interval '5 minutes'
ORDER BY duration DESC;- Check max_connections setting:
SHOW max_connections;- Common fixes:
- Ensure connections are returned to pool (close/release after use)
- Set pool max size < max_connections (leave room for admin)
- Add connection timeout to pool config
- Use pgbouncer for connection multiplexing
- Fix N+1 queries that hold connections too long
Why
Each connection uses ~10MB RAM on the server. Leaked connections accumulate until the pool or server limit is reached.
Context
Production database under load with connection pooling
Revisions (0)
No revisions yet.