Your API latency jumps from 40 ms to 9 seconds, the logs fill with timeouts, and someone in the incident channel types "PgBouncer pool exhausted, bump default_pool_size?" Should you? Sometimes. More often the pool was big enough, and something was holding its connections far longer than it should.

The argument of this guide is simple: a PgBouncer pool runs out because of time, not count. Each server connection can serve hundreds of short transactions per second, and it can be blocked for minutes by one transaction that never commits. Fix the time, and the count usually takes care of itself.

Two limits that both look like exhaustion

PgBouncer has two separate ceilings, and the error tells you which one you hit.

The first is on the client side. max_client_conn, 100 by default, caps how many application connections PgBouncer accepts at all. When you cross it, new clients are refused with no more connections allowed (max_client_conn). That is a configuration limit on the front door, and raising it is cheap: client connections cost PgBouncer a few kilobytes each, though you also need enough file descriptors in ulimit -n.

The second is the pool itself. default_pool_size, 20 by default, sets how many server connections PgBouncer opens to Postgres for each database and user pair. When all of them are busy, extra clients do not fail immediately. They wait in a queue. If they wait longer than query_wait_timeout, 120 seconds by default, PgBouncer disconnects them with a query_wait_timeout error.

The second case is the real "pool exhausted" situation, and it shows up first as latency, then as timeouts. Note the per-pair detail: an app that connects as 3 different users to the same database gets 3 pools of 20, not one pool of 60.

Read SHOW POOLS before changing anything

Connect to the admin console (psql -p 6432 -U pgbouncer pgbouncer) and run SHOW POOLS;. Five columns tell you most of the story in under a minute.

  • cl_active: clients currently paired with a server connection.
  • cl_waiting: clients queued for one. Anything above 0 for more than a few seconds means the pool is saturated.
  • sv_active: server connections in use. If this equals your pool size, every backend is busy.
  • sv_idle: server connections ready to use. If this is above 0 while cl_waiting is also above 0, the problem is not pool size at all; look at max_db_connections or a per-user limit.
  • maxwait: how long the oldest waiting client has been queued, in seconds. This is your user-facing pain number.

Run it 3 or 4 times, 5 seconds apart. A cl_waiting that spikes and drains points to a burst. A cl_waiting that grows steadily while sv_active sits at the limit points to connections that are not coming back, and that is the case where a bigger pool only delays the same outage by a few minutes.

Why raising the pool size first is usually wrong

Raising default_pool_size from 20 to 60 feels like a fix because cl_waiting drops right away. But look at what the extra 40 connections do on the Postgres side.

If the original 20 were stuck behind a lock, the new 40 will queue behind the same lock, now as Postgres backends that each use memory and a process. If the original 20 were slow because the database was CPU bound, 60 concurrent queries make every query slower, and throughput can go down. Postgres does not get faster with more concurrent work past the point where its CPU cores and disks are saturated; it gets more context switching.

There is also a hard ceiling. The sum of all pool sizes across every PgBouncer instance must stay below Postgres max_connections, which defaults to 100, minus the slots reserved for superusers. Three PgBouncer pods with a pool size of 60 each would want 180 backends and start failing with sorry, too many clients already on the database side, which moves the outage instead of ending it.

The case for a bigger pool is real when the queries are short, the database has spare CPU, and traffic simply grew. That case is easy to confirm with numbers, which is the next step.

Find out who holds the server connections

Go to Postgres directly and ask what each backend is doing:

SELECT state, count(*), max(now() - state_change) AS longest
FROM pg_stat_activity
WHERE backend_type = 'client backend'
GROUP BY state;

Three patterns cover most incidents. Many rows in idle in transaction, some for minutes, means application code opened a transaction and went off to do something else: an HTTP call to a payment provider, a file upload, a sleep in a retry loop. In transaction pooling mode, that backend stays assigned to the client until COMMIT or ROLLBACK.

Many rows in active with the same query and a long duration means a slow query, often after a deploy changed a plan or a migration dropped an index. Many active rows waiting on locks, visible through wait_event_type = 'Lock', means one blocking transaction and a pile behind it. pg_blocking_pids(pid) tells you which backend is at the front.

Each pattern has a different immediate action: kill the idle holders with pg_terminate_backend, cancel the slow query, or terminate the single blocker. None of them requires touching PgBouncer configuration in the middle of the incident.

Sizing the pool with Little's law

Once the incident is over, size the pool with arithmetic instead of guesses. Little's law relates concurrency to throughput and time:

connections needed = transactions per second x average transaction duration (s)

example (illustrative):
  peak load          = 1,200 transactions per second
  average duration   = 15 ms   -> 1,200 x 0.015 = 18 connections
  during a slow spell = 120 ms -> 1,200 x 0.120 = 144 connections

The example shows why the pool of 20 was enough on a normal day: 18 busy connections on average, with a small margin. It also shows why it collapsed when the average transaction time went to 120 ms. No sane pool size covers an 8x slowdown, and Postgres could not run 144 concurrent queries efficiently anyway.

Two practical rules come out of this. Measure transaction duration, not query duration, because the time between BEGIN and COMMIT is what holds the connection, including application work in between. And set the pool to roughly 1.5 to 2 times the average need at peak, then protect it with timeouts, which handle the tail better than extra connections do.

Where do the inputs come from? Throughput is xact_commit plus xact_rollback from pg_stat_database, sampled twice a minute apart. PgBouncer's own SHOW STATS reports average transaction time per database as avg_xact_time in microseconds, which is exactly the duration Little's law needs, application gaps included.

A decision tree for the next ten minutes

  1. Do clients get no more connections allowed (max_client_conn)? If yes, raise max_client_conn and check file descriptor limits. If no, go to 2.
  2. Is cl_waiting above 0 while sv_idle is also above 0? If yes, a database-level or user-level cap is blocking, so check max_db_connections and max_user_connections. If no, go to 3.
  3. Does pg_stat_activity show more than a few idle in transaction sessions older than 30 seconds? If yes, terminate them and find the code path. If no, go to 4.
  4. Are many backends waiting on a lock? If yes, find and end the blocker with pg_blocking_pids. If no, go to 5.
  5. Is one query pattern dominating active with long durations? If yes, cancel it and look at the plan or the last migration. If no, go to 6.
  6. Is the database CPU below 70% and did traffic genuinely grow? If yes, raise default_pool_size by a modest step, within the max_connections budget. If no, add capacity to the database or reduce load before adding connections.

An illustrative incident: the receipt email that held the pool

This is a composite example built from common failure patterns, not a single real company. A checkout service runs 6 pods behind one PgBouncer with transaction pooling and a pool size of 25. On a Friday afternoon a release adds a step: after inserting the order, the code sends a receipt through an external email API, and the call happens inside the same database transaction.

The email provider normally answers in 80 ms, so tests pass. At 17:10 the provider slows to 4 seconds per call. Each checkout now holds a server connection for more than 4 seconds. At 50 checkouts per second, Little's law says the service needs about 200 connections. It has 25. cl_waiting climbs past 300 within a minute, maxwait hits 40 seconds, and checkout requests start timing out.

The on-call engineer raises the pool to 75. Waiting drops for about 90 seconds, then climbs again, now with 75 backends showing idle in transaction. The real fix took one line: commit the order before sending the email, and move the email to a background job. After the change, transaction time fell back to about 12 ms, and the original pool of 25 was more than enough.

Checklist to keep pool exhaustion from coming back

  • Set Postgres idle_in_transaction_session_timeout, for example 30 to 60 seconds for web workloads, so abandoned transactions die on their own.
  • Set PgBouncer idle_transaction_timeout as a second guard; it is disabled by default.
  • Lower query_wait_timeout from 120 seconds to something close to your request timeout, so clients fail fast instead of piling up.
  • Configure reserve_pool_size with a small number, such as 5, and reserve_pool_timeout, to absorb short bursts without raising the base pool.
  • Alert on cl_waiting above 0 for more than 30 seconds and on maxwait above 1 second, scraped from SHOW POOLS by an exporter.
  • Keep the sum of pool sizes across all PgBouncer instances in a written budget below max_connections.
  • Review new code for external calls, sleeps or file operations between BEGIN and COMMIT.

The recommendation, with its conditions: when PgBouncer queues clients, spend the first five minutes in SHOW POOLS and pg_stat_activity, not in the config file. Raise default_pool_size only when transactions are short, the database has CPU headroom and the connection budget allows it. If any of those three fails, a larger pool turns a slow incident into a bigger one.