Sub topic 10
Timeouts and the reserve pool
This chapter assumes you can read a pgbouncer.ini, know the three pool modes, and have seen default_pool_size and max_client_conn before. It covers two things that decide how PgBouncer behaves under load: how long it lets clients and connections wait, and how the reserve pool absorbs short bursts.
What happens when a pool is full
Each pool, meaning one database and user pair, has at most default_pool_size server connections. When every one of them is busy, a new request cannot run. PgBouncer queues the client and waits. Three settings govern that wait:
reserve_pool_timeoutis how long a client waits before PgBouncer taps the reserve pool.reserve_pool_sizeis how many extra server connections that pool may open.query_wait_timeoutis how long a client waits before PgBouncer gives up and disconnects it.
The reserve pool
The reserve pool is a burst allowance. It does nothing while the main pool has free connections. If a client has waited longer than reserve_pool_timeout seconds, PgBouncer opens one more server connection, up to reserve_pool_size extra connections for that pool.
[pgbouncer]
default_pool_size = 20
reserve_pool_size = 5
reserve_pool_timeout = 3With these values, a pool normally uses 20 server connections. If all 20 stay busy and a client has waited 3 seconds, PgBouncer allows up to 5 more, so the pool can reach 25.
Three details matter:
- The limit is per pool, not global. Ten pools with a reserve of 5 can add 50 connections in the worst case.
- Reserve connections are not special. They are ordinary server connections, and once the burst ends they close like any idle server connection (see
server_idle_timeoutbelow). - The delay is deliberate. Because the reserve only opens after
reserve_pool_timeout, a very short spike is absorbed by the queue and costs nothing. Only sustained pressure triggers new backend processes.
If you leave reserve_pool_size at its default of 0, the reserve pool is disabled and the queue is the only buffer.
Budgeting the reserve against max_connections
The reserve counts toward PostgreSQL's max_connections when it is in use. Size for the worst case:
worst case = pools x (default_pool_size + reserve_pool_size)Suppose you have 3 pools with the settings above. The worst case is 3 x 25 = 75 server connections. PostgreSQL also holds superuser_reserved_connections (3 by default) for administrators and may serve other clients that bypass PgBouncer, such as replication or monitoring. So max_connections should be comfortably above 75, for example 100.
You can also cap a whole database or user with max_db_connections and max_user_connections. These limits include reserve connections, so they work as a hard ceiling that the reserve cannot exceed.
[pgbouncer]
max_db_connections = 60
max_user_connections = 0The value 0 means no limit. Setting max_db_connections below the sum of your pools protects PostgreSQL even if every pool bursts at once.
Waiting clients and query_wait_timeout
query_wait_timeout is the longest a query may wait for a server connection. The default is 120 seconds. When it expires, PgBouncer closes the client connection, and the application sees an error that names query_wait_timeout.
query_wait_timeout = 30The default is usually too long for a web request. A user will have given up after a few seconds, but the request still sits in the queue and consumes a client slot. A value near your application's request timeout fails fast and sheds load. Setting it to 0 disables the limit, so clients wait forever, which is rarely what you want.
Keep the ordering in mind: reserve_pool_timeout should be much smaller than query_wait_timeout. If the reserve timeout is longer, clients are disconnected before the reserve is ever tried.
Timeouts for connections in each direction
PgBouncer has client-side limits (application to PgBouncer) and server-side limits (PgBouncer to PostgreSQL).
| Setting | Default | Applies to | What it does |
|---|---|---|---|
| `client_login_timeout` | 60 | Client | Disconnects a client that connects but does not finish login in time |
| `client_idle_timeout` | 0 (off) | Client | Closes client connections that send nothing for this long |
| `idle_transaction_timeout` | 0 (off) | Client | Closes a client that sits inside an open transaction without sending queries |
| `query_timeout` | 0 (off) | Client | Cancels queries that run longer than this |
| `query_wait_timeout` | 120 | Client | Disconnects clients waiting for a server connection |
| `server_connect_timeout` | 15 | Server | Gives up on opening a connection to PostgreSQL |
| `server_login_retry` | 15 | Server | Waits this long after a failed server login before retrying |
| `server_idle_timeout` | 600 | Server | Closes server connections idle this long |
| `server_lifetime` | 3600 | Server | Closes server connections older than this once they are free |
idle_transaction_timeout in transaction mode
In transaction mode a server connection is held for the whole transaction. A client that runs BEGIN and then stalls, perhaps while calling an external API, holds that connection and blocks everyone else in the pool. idle_transaction_timeout cuts these clients off:
idle_transaction_timeout = 30This is one of the most useful protections in a transaction-pooled setup, because a few stuck clients can otherwise exhaust a pool of 20.
query_timeout versus statement_timeout
PostgreSQL has its own statement_timeout, which cancels a statement on the server. PgBouncer's query_timeout works at the pooler. The PgBouncer documentation recommends setting query_timeout slightly higher than any statement_timeout, so that PostgreSQL cancels the statement cleanly first and query_timeout acts only as a backstop for a stuck backend.
query_timeout = 65Pair that with ALTER ROLE app_user SET statement_timeout = '60s'; on the server side. Because session-level SET is unsafe in transaction mode, per-role or per-database defaults set with ALTER ROLE are the reliable way to apply it.
Recycling server connections
server_idle_timeout closes unused server connections, which shrinks a pool after a burst, including reserve connections. server_lifetime closes connections after an hour, so memory that PostgreSQL backends have accumulated is released and DNS changes are noticed. If you keep a floor with min_pool_size, PgBouncer reopens connections to stay at it, which avoids a cold-start delay after quiet periods.
server_idle_timeout = 300
server_lifetime = 3600
min_pool_size = 5Observing waits
Waiting clients show up in the admin console. Connect to the pgbouncer database and look at SHOW POOLS:
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer
-- inside the admin console:
SHOW POOLS;
SHOW CONFIG;In SHOW POOLS, check these columns for each pool:
cl_waitingis the number of clients waiting for a server connection. A value that stays above 0 means the pool is undersized or queries are too slow.maxwaitis the age in seconds of the oldest waiting client. If it climbs towardreserve_pool_timeout, the reserve is about to be used. If it approachesquery_wait_timeout, clients are about to be dropped.sv_activeandsv_idleshow how many server connections are busy and free. Ifsv_activeequals the pool size plus the reserve, the pool is saturated.
You can change many timeouts without a restart. Edit pgbouncer.ini and run RELOAD; in the console. Existing client connections stay connected, and new limits apply as connections cycle.
Choosing values
- Start from what your application tolerates. If requests time out at 10 seconds, set
query_wait_timeoutto about 10 or less. - Set
reserve_pool_timeoutto a small fraction of that, for example 2 or 3 seconds, so bursts are served before clients are dropped. - Size
reserve_pool_sizeat roughly 10 to 25 percent ofdefault_pool_size. A large reserve defeats the purpose of capping server connections. - Turn on
idle_transaction_timeoutfor transaction mode. - Verify the worst-case total against
max_connections.
Exercise
A deployment has 4 pools, each with defaultpoolsize = 30 and reservepoolsize = 6. PostgreSQL has maxconnections = 120 and superuserreservedconnections = 3. Is the configuration safe in the worst case, and what is one change that makes it safe without shrinking defaultpool_size?
Show answer
The worst case is 4 x (30 + 6) = 144 server connections, which exceeds the 117 usable connections (120 minus 3 reserved). It is not safe. One fix is to set maxdbconnections (or maxuserconnections) so the total stays below 117, or to lower reservepoolsize to 0 and raise maxconnections, giving 4 x 30 = 120 which still needs maxconnections above that. The simplest option without changing pool sizes is a cap such as maxdbconnections = 110 per database, provided the pools share a database, or raising max_connections to about 160.
Exercise
Clients report errors mentioning querywaittimeout. SHOW POOLS shows clwaiting = 40 and maxwait = 29 for one pool, and querywait_timeout is 30. Name two possible causes and one setting that would reduce the damage while you investigate.
Show answer
Possible causes are a pool that is too small for the load, and long-running or idle-in-transaction clients holding server connections. Setting idletransactiontimeout to a small value frees connections held by stalled transactions, and lowering querywaittimeout makes waiting clients fail faster instead of piling up.