PgBouncer reports 'server closed the connection unexpectedly' during peak write bursts
Environment
Production web service using PostgreSQL 13 primary on a dedicated VM and PgBouncer 1.17 in transaction-pooling mode running in a Docker container on same host as application. Peak load: 600 conn/s with many short write transactions; PgBouncer max_client_conn set to 5000; server_max_conn to 200. Docker container has ulimit defaults.
Symptoms
Under peak write bursts, applications intermittently see errors: 'server closed the connection unexpectedly' and transactions abort. PgBouncer logs show repeated 'server_error' entries and 'server disconnected' lines. The PostgreSQL server logs show idle connections being terminated with 'terminating connection due to administrator command' occasionally and OOM killer not invoked.
Observations
When the issue occurs, pg_stat_activity on the primary shows many connections in 'idle in transaction' state briefly then disappearing. Docker container running PgBouncer shows 'Too many open files' errors in PgBouncer logs during reproductions. Increasing file descriptor limits at the container runtime (ulimit -n) reduces the frequency of errors but does not eliminate them. Network latency between application and PgBouncer is negligible (localhost).
Already attempted
Raised PgBouncer's 'max_client_conn' and 'reserve_pool_size'; adjusted server_max_conn to better match PostgreSQL's max_connections; restarted PgBouncer to clear state. Tuned pool_mode to statement briefly for testing (potentially unsafe for application logic) and observed fewer errors but different transactional behavior. Monitored host fd usage with lsof and found PgBouncer approaching OS limits during bursts.
Question
How can I determine whether PgBouncer 'server closed the connection unexpectedly' is caused by file descriptor exhaustion in the container, server-side max_connections limits, or a misconfiguration of pooling mode under heavy write load, and what reversible mitigation steps will stabilize connections during peak bursts?
Verified community troubleshooting
No answers yet..
Incident analysis
Most likely causes
Summary
Suggested Fixes
Continue troubleshooting
Related troubles
Matched from the title, description and tags
- Databases29% match
PostgreSQL standby stops applying WAL during long-running transactions on primary after pg_basebackup restore
Open0 answers - Databases29% match
PostgreSQL logical replication slot grows until disk full after subscriber disconnects during nightly load
Open0 answers - Databases26% match
WAL archiving stalls and primary reports 'could not flush file' during checkpoint under nightly bulk load
Open0 answers - Databases26% match
pg_dump fails intermittently with 'could not serialize access' during high-concurrency writes on PostgreSQL 13
Open0 answers - Databases25% match
PostgreSQL replica falls behind and reports WAL streaming delay when frequent autovacuum runs on primary
Open0 answers