Sub topic 12
Reading Pool and Traffic Statistics in PgBouncer
Assume you know basic SQL, can use psql, and understand PostgreSQL roles, databases, and max_connections. This chapter shows how to use PgBouncer's admin console to answer three questions:
- Are clients waiting for a server connection?
- How many PostgreSQL connections are active?
- Is the pooler handling the expected amount of traffic?
Open the admin console
PgBouncer exposes a special database named pgbouncer. Connect to it through the PgBouncer port, not PostgreSQL's usual port:
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncerThe admin user must be listed in admin_users in pgbouncer.ini. The console accepts PgBouncer commands such as SHOW POOLS and SHOW STATS. These commands are not PostgreSQL catalog queries.
A typical first check is:
SHOW POOLS;
SHOW STATS;
SHOW CLIENTS;
SHOW SERVERS;Read `SHOW POOLS`
SHOW POOLS reports one row for each database and user pool. A pool is the set of client and server connections that share a database and user mapping.
| Column | Meaning |
|---|---|
| `cl_active` | Client connections currently running a request |
| `cl_waiting` | Clients waiting for a server connection |
| `sv_active` | Server connections currently assigned to clients |
| `sv_idle` | Server connections ready for use |
| `maxwait` | Longest current client wait, in seconds |
| `pool_mode` | Session, transaction, or statement pooling |
The most important warning sign is a sustained positive cl_waiting value. It means clients have reached PgBouncer, but the pool cannot give them PostgreSQL server connections quickly enough.
sv_idle gives the opposite signal. Many idle servers combined with waiting clients can indicate a different pool or user is limiting access, or that a transaction has not been released as expected. Always inspect the row for the exact database and user.
SHOW POOLS;Read `SHOW STATS`
SHOW STATS reports cumulative traffic and timing for each database. Useful columns include:
total_xact_count: completed transactions.total_query_count: completed queries.total_received: bytes received from clients.total_sent: bytes sent to clients.avg_xact_time: average transaction time in microseconds.avg_query_time: average query time in microseconds.avg_wait_time: average time clients waited for a server connection.
These counters usually increase from the time PgBouncer starts. A single value is less useful than a comparison over time. Record the output, wait several minutes, and compare the change in transaction count, traffic, and wait time.
A rising avg_wait_time with rising cl_waiting points to pool pressure. A rising avg_query_time without client waiting usually points to slow PostgreSQL queries or database resource pressure instead.
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer \
-c "SHOW STATS;"Connect pool and traffic data
The statistics describe different layers of the same path:
Use SHOW CLIENTS when you need to identify client addresses, states, or connection ages. Use SHOW SERVERS to inspect the PostgreSQL connections behind the pool. These commands help distinguish a client-side connection problem from a database-side problem.
A practical investigation
When users report delays:
- Run
SHOW POOLSand find rows with waiting clients. - Check whether those rows have active or idle server connections.
- Run
SHOW STATSand compare wait time with query and transaction time. - Use
SHOW CLIENTSto find long-lived or stuck clients. - Use
SHOW SERVERSto see which PostgreSQL connections are active. - Check PostgreSQL's own activity and resource metrics before increasing pool sizes.
Increasing default_pool_size may reduce waiting, but it also consumes PostgreSQL connections. Make sure the total across databases, users, and PgBouncer instances fits below PostgreSQL's max_connections.
Exercise
SHOW POOLS reports cl_waiting = 12, sv_active = 20, and sv_idle = 0 for the application pool. What does this indicate, and what should you inspect next?
Show answer
Clients are waiting because every server connection in that pool is busy. Inspect SHOW STATS for wait and query times, then inspect SHOW SERVERS and PostgreSQL activity for slow or blocked work before changing pool limits.