Your PgBouncer log is filling with pooler error: query_wait_timeout lines and application teams are reporting intermittent database errors. The error string sounds like a query problem. It is not. query_wait_timeout fires when a client has been sitting in PgBouncer’s wait queue, blocked on getting a server connection, for longer than the configured timeout. The client never reached PostgreSQL.
The default is 120 seconds. When this error appears, the pool has been exhausted for at least that long. query_wait_timeout is a lagging indicator: it tells you an incident happened, not that one is starting.
A trap makes this worse in most deployments. If your application-side timeout (5 to 30 seconds is typical) is shorter than query_wait_timeout, the application gives up and retries long before PgBouncer ejects the waiter. Each retry adds a new client to the queue. PgBouncer’s timeout never saves you; it just cleans up abandoned waiters while the retry storm deepens the queue.
What this means
PgBouncer maintains one connection pool per (database, user) pair, sized by pool_size. When all server connections in a pool are busy, incoming clients enter a FIFO wait queue. The age of the oldest waiter is exposed as maxwait in SHOW POOLS. When a waiter’s age exceeds query_wait_timeout (default 120s), PgBouncer disconnects that client with an error and logs the event.
The disconnection is a cleanup mechanism, not a rescue. When the application timeout is shorter than query_wait_timeout, the app times out and retries first, and the PgBouncer timeout only reaps the abandoned queue entries. The retry amplification loop looks like this:
flowchart TD A[All server connections busy] --> B[Clients enter wait queue] B --> C[App timeout fires at 5-30s] C --> D[App retries with new connection] D --> B B --> E[Queue grows faster than it drains] E --> F[Waiter age hits query_wait_timeout at 120s] F --> G[PgBouncer disconnects waiter and logs error] G --> H[Log-only event: no SHOW counter exists]
Two things to note about the mechanics:
maxwaitmeasures from when the query was sent, not when the client connected. A client can be connected for hours in session mode; the clock only starts on the pending query.- The event is log-only. PgBouncer exposes no error counter for it in any
SHOWcommand. If you only scrapeSHOWoutput, you will not see these disconnections at all.
Do not confuse query_wait_timeout with query_timeout. The latter cancels queries that run too long on the backend (default 0, disabled). query_wait_timeout disconnects clients that never got a server connection. Setting query_timeout has no effect on wait-queue behavior.
Common causes
| Cause | What it looks like | First thing to check |
|---|---|---|
| Pool exhaustion from slow backend | sv_active = pool_size, avg_query_time elevated, cl_waiting growing | SHOW STATS_AVERAGES - is query_time above baseline? |
| Idle-in-transaction holding connections | avg_xact_time much larger than avg_query_time, connections “active” but idle | PostgreSQL pg_stat_activity for idle in transaction |
| Undersized pool_size | Saturation under normal traffic, not just spikes | sv_active / pool_size ratio sustained above 85% |
| Backend unreachable (Postgres down, DNS failure, network partition) | sv_login elevated, total server connections declining, pool draining | SHOW DNS_HOSTS, direct connection test to PostgreSQL |
| Long transactions in session pooling mode | Connections held for entire sessions, low pool turnover | SHOW POOLS - which pool mode, and which pool is saturated |
| Administrative PAUSE | cl_waiting spikes, sv_active drops to zero, looks like an outage | SHOW DATABASES - paused and disabled columns |
The paused/disabled check matters because PAUSE produces exactly the same signal shape as an outage: high cl_waiting, zero sv_active, waiters aging toward the timeout. Rule it out before treating this as an incident.
Quick checks
All of these are read-only and safe during an incident. They assume the admin console on port 6432; adjust for your deployment.
# 1. Admin console responsiveness - if this is slow, the event loop itself is impaired
time psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW VERSION;" > /dev/null
# 2. Pool state: which pool is saturated, how deep is the queue, how old is the oldest waiter
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW POOLS;"
# 3. Paused/disabled context before escalating anything
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW DATABASES;"
# 4. Averages: is the backend slow (query_time) or is the pool just small (wait_time)?
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW STATS_AVERAGES;"
# 5. Timeout events from the log (the only place they exist)
grep -cE "query_wait_timeout" /var/log/pgbouncer/pgbouncer.log
grep -E "timeout" /var/log/pgbouncer/pgbouncer.log | tail -20
# 6. Per-connection detail: which server connections are holding the pool
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW SERVERS;"
# 7. Per-waiter detail: how close each waiting client is to the timeout
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -c "SHOW CLIENTS;"
# 8. Verify the configured timeout actually is what you think it is
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW CONFIG;" | grep -i wait
In SHOW POOLS, the columns that matter are cl_waiting (queue depth per pool), sv_active (compare against pool_size), sv_idle (headroom), and maxwait plus maxwait_us (oldest waiter’s age). In SHOW CLIENTS, wait and wait_us show how long each waiting client has been blocked and how close it is to being ejected.
How to diagnose it
Confirm the pool is saturated, not just busy. In
SHOW POOLS, find the pool wherecl_waiting > 0. Check thatsv_activeequalspool_sizeandsv_idleis zero. Ifsv_activeis well belowpool_sizewhile clients wait, something else is wrong: check for a paused database or backend connection failures.Check paused/disabled state.
SHOW DATABASESshowspausedanddisabledper database. A paused database queues all new queries while existing transactions complete. This is expected during maintenance and is not an incident.Attribute the latency: pool or database. Compare
avg_wait_timeagainstavg_query_timeinSHOW STATS_AVERAGES. High wait time with low query time means the database is fine and the pool is too small. High query time means PostgreSQL is slow and connections are held longer, which shrinks effective pool capacity.Distinguish pool exhaustion from backend failure. In pool exhaustion, the total server connection count is stable at
pool_sizewith everything insv_active. In backend failure, the total is declining andsv_loginis elevated as connections attempt and fail to establish.SHOW DNS_HOSTStells you whether resolution is returning current addresses; a stale cache after failover points PgBouncer at a dead backend.Find the connections holding the pool.
SHOW SERVERSlists every server connection withstateandrequest_time. Anactiveconnection with an oldrequest_timeis a long-running query or transaction monopolizing a slot. Thelinkcolumn lets you trace it back to the responsible client inSHOW CLIENTS.Check for idle-in-transaction. If
avg_xact_timeis many times larger thanavg_query_time, applications are holding transactions open while doing non-database work. Confirm on the PostgreSQL side withpg_stat_activityfiltered onidle in transaction.Reconstruct the timeline from the log. Count and timestamp the
query_wait_timeoutlines. Subtract your configured timeout from the first occurrence to get when the saturation event started.
Metrics and signals to monitor
| Signal | Why it matters | Warning sign |
|---|---|---|
cl_waiting (SHOW POOLS) | Primary saturation signal; any sustained nonzero value means clients are blocked | Nonzero for more than 60 seconds with maxwait climbing |
maxwait / maxwait_us (SHOW POOLS) | Age of the oldest waiter; the direct user-facing impact measure | Above 5s; approaching query_wait_timeout means disconnections are imminent |
avg_wait_time (SHOW STATS_AVERAGES) | Average queuing delay PgBouncer injects per request | Sustained above 100ms; any large deviation from baseline |
avg_query_time vs avg_wait_time | Separates “database is slow” from “pool is too small” | Must be read together; misattribution leads to wrong fixes |
sv_active / pool_size | Leading indicator; at 100% the next request queues | Sustained above 85% |
sv_idle | Headroom; zero idle with zero waiting means you are one slow query from a cascade | Zero sustained |
sv_login | Backend connection establishment health | Persistently above zero with rising cl_waiting |
| Timeout events in log | The only record of disconnections; no SHOW counter exists | Any query_wait_timeout in production is abnormal |
paused / disabled (SHOW DATABASES) | Context that must gate every alert on the above | Suppress cl_waiting/maxwait alerts during administrative states |
Alert on sustained maxwait above a threshold, not on cl_waiting > 0. Brief queuing during bursts is normal in transaction pooling mode and will drown you in false positives. Set the threshold relative to your application timeout, not an absolute number.
Fixes
Fix the immediate saturation
Identify long-running queries or transactions holding server connections via SHOW SERVERS (old request_time on active connections), then cancel them on the PostgreSQL side with pg_cancel_backend or pg_terminate_backend. Coordinate with the application team first: terminating a backend errors the client attached to it through PgBouncer, so this is a disruptive action.
Do not restart PgBouncer as a first response. A restart drops all cached server connections and every client triggers re-authentication against PostgreSQL simultaneously, producing a thundering herd that is often worse than the original saturation.
Right-size the pool
If the database is healthy and avg_query_time is at baseline, the pool is too small. Increase pool_size (per-database override or default_pool_size) and issue RELOAD. Constraint: the sum of all PgBouncer pools’ pool_size values must fit within PostgreSQL’s max_connections with room for superuser connections, direct access, and replication. Multiple PgBouncer instances targeting the same backend multiply the demand.
If reserve_pool_size is configured and you see reserve connections drawn regularly (total server connections per pool exceeding pool_size, plus taking connection from reserve_pool warnings in the log), the base pool is chronically undersized. The reserve is for brief spikes; sustained usage masks the real sizing problem.
Fix the application behavior
If avg_xact_time >> avg_query_time, the fix is in the application: transactions held open across non-database work. On the PostgreSQL side, idle_in_transaction_session_timeout can auto-terminate these as a guardrail while the code is fixed.
Align the timeouts
This is the fix specific to this error. The relationship that matters:
- Application timeout shorter than
query_wait_timeout(the common case: 5-30s vs 120s) produces retry amplification. The app retries, the queue deepens, and the PgBouncer timeout only reaps corpses. query_wait_timeout = 0disables the timeout entirely, so clients queue indefinitely. That trades disconnection errors for unbounded queue growth and is rarely what you want.
Either lower query_wait_timeout to a value near the application’s retry budget so PgBouncer fails waiters fast and predictably, or keep it high only if clients genuinely cannot retry and must wait. What you cannot do is leave a 120-second PgBouncer timeout under a 10-second application timeout and expect the log lines to mean anything other than “we have been saturated for a very long time.”
Prevention
- Alert on the leading indicators, not the timeout. Sustained
maxwaitandsv_active / pool_sizeabove 85% fire minutes before the first disconnection. Thequery_wait_timeoutlog line is the post-mortem. - Parse the log. PgBouncer exposes no error counters via
SHOWcommands. Timeout events, connection refusals, and auth failures exist only in the log file. A monitoring setup that ignores logs is blind to every error condition PgBouncer reports. - Track timeout alignment in configuration review. Whenever application timeout settings change, check them against
query_wait_timeout. Drift here is common and invisible until an incident. - Set
max_client_connwith the retry storm in mind. During a queueing event, retries create new client connections. Ifused_clientsreachesmax_client_conn, new connections are refused withno more connections allowed, converting a latency incident into a hard outage. - Load-test the queue behavior. The interaction between pool size, application timeout, retry policy, and
query_wait_timeoutis only observable under saturation. A controlled test that drives the pool to exhaustion will show you which timeout fires first and how the retry cascade behaves.
How Netdata helps
- Per-second
cl_waitingandmaxwaitcollection catches sub-minute queueing events that interval polling misses, and timestamps the start of saturation precisely instead of inferring it from the first log line 120 seconds later. - Wait time vs query time correlation puts
avg_wait_timeandavg_query_timeon the same timeline, so “pool too small” and “database slow” are distinguishable at a glance rather than requiring two separate queries during an incident. - Pool utilization trending (
sv_activeagainstpool_size,sv_idleheadroom) shows the slow creep toward saturation days before the firstquery_wait_timeoutappears, turning a reactive fix into a capacity change. - Anomaly detection on
maxwaitflags the transition from normal burst queuing to sustained starvation without a hand-tuned static threshold. - Cross-pool visibility surfaces the single saturated
(database, user)pool that aggregate dashboards hide behind healthy averages. - Log-based alerting on timeout events closes the gap left by PgBouncer’s lack of error counters, so disconnections page the right people instead of being discovered in a log grep the next morning.






