Skip to main content
Interactive pgbouncer Configurator →Open Architecture Blueprint ↗
P0 INCIDENTL6 BARM02 · 6 MIN READ

PgBouncer cl_waiting Queue Saturation & Little’s Law Pool Sizing

Use Little’s Law to size PgBouncer default_pool_size, fix transaction-mode prepared statement errors, and eliminate connection queue storms.

PRODUCTION SYMPTOM & ALERT SIGNATURE

API p99 latency spikes from 40ms to 8,000ms while PostgreSQL CPU sits at only 28%. SHOW POOLS in PgBouncer reports cl_waiting > 350 and maxwait > 5s.

pgbouncer_pools_client_waiting_connections > 25 AND pgbouncer_pools_client_maxwait_seconds > 0.5
01 · ROOT-CAUSE MECHANICS (WHY IT HAPPENS)

By Little’s Law (L = lambda * W), concurrency equals throughput times average query hold time. If a missing composite index slows a query from 4ms to 80ms (20x), the exact same 400 RPS workload suddenly demands 32 active Postgres server connections instead of 2.

When PgBouncer default_pool_size is set too low (or pool_mode=session holds connections across idle app think-time), excess requests pile up in cl_waiting until client timeouts trigger retry storms.

Conversely, setting default_pool_size above ~2x-4x vCPU cores causes OS context-switching thrash and LWLock contention inside PostgreSQL.

02 · LIVE TRIAGE COMMANDS (COPY & RUN)
Inspect live client wait queues and server utilization in PgBouncer
$ psql -p 6432 -U pgbouncer pgbouncer -c "SHOW POOLS;"
Smoking gun: cl_waiting > 0, sv_idle = 0, and maxwait climbing in seconds.
Find the top latency-amplifying query holding pool slots
$ psql -c "SELECT left(query, 90), calls, round(mean_exec_time::numeric, 2) AS mean_ms, round(total_exec_time::numeric, 1) AS total_ms FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 8;"
Smoking gun: One high-QPS query whose mean_exec_time jumped (e.g., Seq Scan on a growing table).
Confirm active vs idle-in-transaction server slots on PostgreSQL
$ psql -c "SELECT state, count(*) FROM pg_stat_activity GROUP BY state ORDER BY count DESC;"
Smoking gun: High active or idle-in-transaction counts pinning every PgBouncer server slot.
03 · PRODUCTION DIFF (BROKEN VS HARDENED)
BROKEN · Broken pgbouncer.ini (session pooling + undersized pool + no wait cap)
[pgbouncer]
pool_mode = session             # Holds a Postgres backend for the entire app connection life!
max_client_conn = 5000
default_pool_size = 8           # Starves under 400 RPS whenever query latency nudges above 20ms
query_wait_timeout = 120        # Queues doomed requests for 2 minutes, amplifying retry storms
HARDENED · Production pgbouncer.ini (transaction pooling + bounded queues + prepared statements)
[pgbouncer]
pool_mode = transaction
max_client_conn = 4000
default_pool_size = 32          # Sized to ~2x-4x Postgres vCPU cores (16 vCPU -> 32-48)
reserve_pool_size = 8
reserve_pool_timeout = 2
max_prepared_statements = 200   # Native PgBouncer 1.21+ protocol-level prepared statement support
query_wait_timeout = 5          # Fail fast instead of building a 2-minute zombie backlog
server_idle_timeout = 300
04 · MINUTE-24 BAR-RAISER CHECKPOINT (L6 · M02)TEST YOUR INSTINCT · ZERO SIGNUP

An 8-vCPU PostgreSQL primary sits behind PgBouncer (pool_mode=transaction, default_pool_size=24). Traffic is flat at 500 RPS, yet cl_waiting jumps to 400 and Postgres CPU is 95%. What is the fastest safe mitigation?

05 · POST-FIX VERIFICATION CHECKLIST
  • Verify SHOW POOLS shows cl_waiting = 0 and maxwait < 0.05s at peak RPS.
  • Fix the underlying slow query with CREATE INDEX CONCURRENTLY so query hold time W drops back below 10ms.
  • Verify application drivers do not rely on session-scoped SET variables or advisory locks when running in pool_mode=transaction.
06 · FREQUENTLY ASKED ENGINEERING QUESTIONS

How do I calculate default_pool_size with Little’s Law?

Multiply peak queries per second (lambda) by p95 query duration in seconds (W), then cap the pool at roughly 2x to 4x PostgreSQL vCPU cores so CPU saturation does not trigger lock thrash.

Does PgBouncer transaction pooling support prepared statements?

Yes. PgBouncer 1.21+ natively supports protocol-level prepared statements in transaction mode when max_prepared_statements is set above 0.

Why should query_wait_timeout be low (e.g., 5s) instead of 120s?

Clients and load balancers time out after 5-15s anyway. Letting PgBouncer queue abandoned requests for 120s executes dead work after the caller gave up and fuels cascading retry storms.