In PgBouncer, how do default_pool_size and max_client_conn differ, and how do you read SHOW POOLS when clients start queueing?
answer
- clients are cheap, servers are not
- per database and user pair
- 20 and 100
- cl_waiting with maxwait climbing
basics
~10 smax_client_conn (default 100) caps client connections to PgBouncer; default_pool_size (default 20) caps server connections per database and user pair. In SHOW POOLS, rising cl_waiting and maxwait with no sv_idle mean every server is busy.
solid answer
~40 s`max_client_conn` limits how many client connections PgBouncer accepts in total, default 100; a client over the limit is disconnected with `no more connections allowed (max_client_conn)`. Clients are cheap, so it is usually set in the thousands, with OS file-descriptor limits raised to match. `default_pool_size`, default 20, limits **server** connections per (database, user) pair, overridable by `pool_size` per user or database; the sum over all pools must fit inside PostgreSQL's `max_connections`. On the admin console, `SHOW POOLS` shows `cl_waiting` (clients that sent a query but have no server), `sv_active`, `sv_idle` and `maxwait`. Rising `cl_waiting` and `maxwait` with `sv_idle` at 0 mean the pool is saturated: find what holds the servers before you enlarge it.
code
ini · 11 lines[databases]
ticketing = host=10.0.0.5 dbname=ticketing pool_mode=transaction max_db_connections=40
[users]
ticket_app = pool_size=30
[pgbouncer]
max_client_conn = 5000
default_pool_size = 20
reserve_pool_size = 5
reserve_pool_timeout = 3go deeper
Remember the two sides: max_client_conn limits clients (default 100), default_pool_size limits server connections per database and user (default 20).
Explain why pools multiply per database and user pair, and read SHOW POOLS columns correctly, especially cl_waiting versus cl_active and what maxwait measures.
Diagnose a queue before resizing: find idle-in-transaction holders, check whether PostgreSQL is already saturated, then size pools inside the server's connection budget.
Treat the server-side total as a shared budget across services and PgBouncer instances, and decide who gets reserve capacity during spikes.
## Two limits on two sides PgBouncer sits between many **client connections** (the application side) and few **server connections** (real PostgreSQL backends). Each side has its own limit, and confusing them is the classic sizing mistake. | Setting | Limits | Default | Cost of raising it | |---|---|---|---| | `max_client_conn` | client connections to PgBouncer, in total | 100 | file descriptors and a little PgBouncer memory | | `default_pool_size` | server connections per (database, user) pair | 20 | one PostgreSQL backend process each | | `reserve_pool_size` | extra servers a pool may open after clients waited `reserve_pool_timeout` (5 s) | 0 (off) | backends, only under pressure | | `max_db_connections` / `max_user_connections` | server connections per database / per user across pools | 0 (unlimited) | none, they only cap | ## `max_client_conn`: how many can knock A client over the limit is not queued; it is disconnected with `no more connections allowed (max_client_conn)`. For a ticketing site whose serverless functions each open a connection, 100 is far too low, and values in the thousands are normal. Raise the operating system's open-file limit with it. PgBouncer's documentation gives the theoretical file-descriptor maximum as `max_client_conn` plus the pool size times databases times users, because every server connection is a socket too. ## `default_pool_size`: how many get served A **pool** exists per (database, user) pair, and `default_pool_size` caps each one. The value comes from the most specific setting: 1. `pool_size` on the user's line in `[users]`; 2. `pool_size` on the database's entry in `[databases]`; 3. `default_pool_size`. Because pools multiply, a fleet that logs in as three roles to one database can open 3 x 20 = 60 server connections with defaults. `max_db_connections` caps the total per database whatever the user. The sum of all pools, plus anything connecting directly, must fit inside PostgreSQL's `max_connections` (default 100, of which `superuser_reserved_connections` keeps 3). Sizing the server side is a PostgreSQL capacity question owned elsewhere; PgBouncer's job is to keep the busy count bounded. ## Reading `SHOW POOLS` Connect to the admin console, the virtual database `pgbouncer`, as a user in `admin_users` or `stats_users`, and run `SHOW POOLS`. One row per (database, user) pair: - `cl_active`: clients linked to a server **or idle with no query waiting**. A large value is normal and harmless. - `cl_waiting`: clients that have **sent a query** but have no server yet. This is the queue. - `sv_active`: servers linked to a client right now. - `sv_idle`: servers ready for immediate reuse. - `sv_used`: servers idle longer than `server_check_delay` (30 s) that need `server_check_query` before reuse. - `sv_login`: servers still logging in to PostgreSQL. - `maxwait` (+ `maxwait_us`): how long the oldest waiting client has waited, in seconds. - `pool_mode`: the mode the pool uses. ```console pgbouncer=# SHOW POOLS; database | user | cl_active | cl_waiting | sv_active | sv_idle | sv_used | maxwait -----------+------------+-----------+------------+-----------+---------+---------+--------- ticketing | ticket_app | 812 | 347 | 20 | 0 | 0 | 9 ``` ## Diagnosing a queue The row above, taken during an on-sale spike, says every one of the 20 servers is busy and 347 clients are waiting. Work through it in order: 1. **What is each server doing?** In transaction mode a server stays `sv_active` for the whole transaction, including time the client spends **idle in transaction** between statements. A function that opens a transaction, then calls a payment API, holds a server for the whole call. Check `pg_stat_activity` on PostgreSQL for long transactions; fixing that code is usually worth more than more connections. 2. **Is PostgreSQL itself saturated?** If the backends are busy on CPU or I/O, a bigger pool adds contention, not throughput. 3. **Only then resize.** Raise `pool_size` for that pool, or allow a small `reserve_pool_size` for bursts, inside the server's connection budget. 4. **Look below the counts.** `SHOW CLIENTS` and `SHOW SERVERS` list the individual connections behind each `SHOW POOLS` row, which tells you whether one client population or one database user is hogging the pool. Several busy pools can also share one database's `max_db_connections`, so a single noisy pool may be starving its neighbours rather than itself. Two timers bound the queue. After `query_wait_notify` (5 s) a waiting client gets a NOTICE that it is queued. After `query_wait_timeout` (120 s) it is disconnected with the reason `query_wait_timeout`. Both are worth alerting on alongside `maxwait`.
- Why can a fleet that logs in to PgBouncer as three different database users open more server connections than default_pool_size suggests?Pools are per (database, user) pair, so three users on one database are three pools of up to 20 each, 60 server connections with defaults. Cap the total with `max_db_connections`, or set `pool_size` per user, and keep the sum inside PostgreSQL's `max_connections`.
- What happens to a PgBouncer client that sits in cl_waiting longer than query_wait_timeout?PgBouncer disconnects it with the reason `query_wait_timeout`; the default is 120 seconds and 0 disables the limit, which lets clients queue forever. A client queued past `query_wait_notify` (5 seconds by default) first receives a NOTICE saying it is waiting.
- max_client_conn should be kept close to PostgreSQL's max_connections.
- default_pool_size is one global cap on server connections for the whole PgBouncer.
- cl_active counts only clients that are running a query right now.
- Clients beyond max_client_conn wait in a queue until a slot frees up.
- A growing maxwait is always fixed by raising default_pool_size.