Sub topic 10
Timeouts and the Reserve Pool
Prerequisites and the problem
Assume you know basic PostgreSQL roles, max_connections, psql, and the main sections of pgbouncer.ini. This chapter focuses on what happens when all normal server connections are busy.
PgBouncer can accept many client connections, but each client eventually needs a PostgreSQL server connection to run a query. If every server connection is busy, new work waits in a queue. A long queue can make an application appear broken even when PostgreSQL is still healthy.
A reserve pool gives PgBouncer permission to open a few additional server connections when waiting lasts long enough. Timeouts prevent clients or transactions from waiting forever.
Configuring a reserve pool
These settings belong in the [pgbouncer] section:
default_pool_size = 20
reserve_pool_size = 5
reserve_pool_timeout = 3
query_wait_timeout = 30For each user and database pair, PgBouncer normally creates at most 20 server connections. If clients wait for a server connection for three seconds, PgBouncer can temporarily create up to five more. The reserve connections are still subject to PostgreSQL's available capacity and other PgBouncer limits.
query_wait_timeout limits how long a client query may wait for a server connection. A value of 0 disables this timeout. Choose a value that matches the application's request deadline. If the application gives up after 10 seconds, allowing PgBouncer to wait for 30 seconds only creates work that the application no longer needs.
The reserve pool is a pressure-release valve, not a replacement for sizing. If the reserve pool activates constantly, investigate slow queries, long transactions, insufficient normal pool capacity, or excessive client concurrency.
Other useful timeouts
Timeout names describe different waiting or idle conditions:
| Setting | What it limits | Typical reason to set it |
|---|---|---|
| `query_wait_timeout` | Time waiting for a server connection | Fail queued work instead of waiting indefinitely |
| `client_idle_timeout` | An idle client connection | Remove abandoned client connections |
| `server_idle_timeout` | An unused server connection | Return idle capacity to PostgreSQL |
| `idle_transaction_timeout` | An open transaction with no activity | Prevent a transaction from holding a connection |
| `client_login_timeout` | Time allowed for client authentication | Clear stalled login attempts |
Do not use an aggressive idle_transaction_timeout without checking application behavior. Some applications intentionally pause inside a transaction, although long idle transactions can block cleanup and consume pool capacity.
Choosing values and diagnosing waits
Start with the database connection budget. For example, if PostgreSQL allows 100 connections, reserve capacity for administrators, maintenance, replicas, and other services. Do not configure PgBouncer to consume all 100.
Then measure:
- How many concurrent database operations the application needs.
- How long normal queries take.
- How long clients can wait before their request fails.
- Whether transactions remain open while the application performs unrelated work.
Use the admin console to find waiting clients:
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer
-- Inside the PgBouncer console:
SHOW POOLS;
SHOW CLIENTS;
SHOW SERVERS;In SHOW POOLS, compare cl_waiting with sv_active and sv_idle. A growing cl_waiting value means clients are queued. If sv_active stays full, server connections are busy. If query durations are high, adding connections may increase contention rather than improve throughput.
Exercise
If a pool has default_pool_size = 20, reserve_pool_size = 5, and clients wait longer than reserve_pool_timeout, what does the reserve pool allow?
Show answer
PgBouncer may open up to five additional server connections for that pool, subject to PostgreSQL and PgBouncer connection limits.