FULL AI ANALYSIS

PgBouncer reports 'server closed the connection unexpectedly' during peak write bursts

Generated Oct 3, 2026, 12:35:03 AM

Analysis summary

Logs and observations point primarily to resource exhaustion at the PgBouncer process (open file descriptors) compounded by pooling configuration mismatches for a write-heavy workload. The 'Too many open files' messages and improvement after raising ulimits indicate the container's default limits are insufficient. Additionally, transaction pooling modes (transaction vs statement) and server max_connections interplay can cause PgBouncer to aggressively open/close server sockets, exacerbating fd usage.

Possible causes

File descriptor limit in the Docker container prevents PgBouncer from opening the required number of sockets to backend servers and client connections under peaks.

90%

Pooling mode (transaction) leads to many short-lived server connections during bursty writes; statement pooling temporarily masked the issue but may break transactional semantics.

70%

PgBouncer configured with inadequate server_max_conn relative to client demand causes rapid churn of server connections and stress on OS resources.

55%

PostgreSQL server closing idle backend connections due to max_connections or administrative limits leads to PgBouncer reporting unexpected server disconnects.

35%

Suggested fixes

1

Diagnostic step 1

Increase the container and PgBouncer process file descriptor limit (set ulimit -n at container start and configure systemd/docker service limits) and monitor for a reduction in 'Too many open files' errors (reversible configuration change).

2

Diagnostic step 2

Align PgBouncer's server_max_conn with PostgreSQL's max_connections and ensure there is headroom for other tools; avoid setting server_max_conn higher than the database can accept.

3

Diagnostic step 3

Enable PgBouncer's verbose logging temporarily and capture metrics (show pools, SHOW STATS) during a controlled load test to correlate fd usage with connection churn.

4

Diagnostic step 4

If possible, perform a load test in a staging environment to try statement vs transaction pooling modes and observe application correctness and connection stability; use this to choose an appropriate pooling strategy.

5

Diagnostic step 5

Consider horizontal scaling options: run multiple PgBouncer instances and load-balance clients across them to reduce per-process fd pressure. If changes do not stabilize the system, escalate to a DBA or PgBouncer maintainer with collected logs and metrics for deeper tuning advice.