Sub topic 12
Reading pool and traffic statistics
This chapter shows how to read the numbers that PgBouncer reports about its pools and traffic. It assumes you can already open the admin console (the special pgbouncer database) as a user listed in admin_users or stats_users. You should also know the three pool modes and the meaning of default_pool_size, max_client_conn, and max_connections from earlier chapters.
What the admin console reports
The console answers read-only SHOW commands. Each one is a different view of the same internal state.
| Command | One row per | Answers the question |
|---|---|---|
| `SHOW POOLS` | user and database pair | Right now, how many clients are active or waiting, and how many server connections are busy or idle? |
| `SHOW STATS` | database | How much work has gone through, and how long did it take? |
| `SHOW CLIENTS` | client connection | Who is connected, and who is waiting? |
| `SHOW SERVERS` | server connection | What is each PostgreSQL connection doing? |
| `SHOW LISTS` | internal counter | How many pools, users, and free or used connections exist? |
| `SHOW DATABASES` | configured database | What are the limits and current connection counts per database? |
SHOW POOLS is a live snapshot. SHOW STATS accumulates since startup and also reports averages. Use the snapshot to see a problem now, and use the statistics to see a trend.
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer -c 'SHOW POOLS;'The console accepts only the simple query protocol. psql uses it, but some drivers do not, so use psql for inspection.
SHOW POOLS: the first place to look
A typical output, trimmed to the important columns:
database | user | cl_active | cl_waiting | sv_active | sv_idle | sv_used | maxwait | maxwait_us | pool_mode
----------+----------+-----------+------------+-----------+---------+---------+---------+------------+-------------
appdb | app_user | 38 | 12 | 20 | 0 | 0 | 2 | 410233 | transaction
pgbouncer| pgbouncer| 1 | 0 | 0 | 0 | 0 | 0 | 0 | statementThe column groups are:
cl_activecounts clients that are linked to a server connection right now.cl_waitingcounts clients that sent a request and have no server connection yet. This is the most important column. A value above zero means demand exceeds the pool.sv_activecounts server connections linked to a client.sv_idlecounts server connections that are free and ready for any client.sv_usedcounts server connections that were used and have been idle longer thanserver_check_delay. PgBouncer runsserver_check_queryon them before reuse. Manysv_usedrows are normal on a quiet pool.sv_logincounts server connections that are being established. A persistently nonzero value suggests slow or failing logins to PostgreSQL.maxwaitandmaxwait_usgive the age of the oldest waiting client.maxwaitis whole seconds, andmaxwait_usis the microsecond remainder. Together they show how long the unluckiest client has waited.
Column sets differ slightly between PgBouncer versions. Newer releases add columns for cancel requests, for example. Check the documentation for your version rather than assuming the list above is complete.
The output above shows a saturated pool. All 20 server connections are active, none are idle, and 12 clients are queued with the oldest waiting about 2.4 seconds. The pool is the bottleneck.
Reading the pattern
| Pattern | Meaning |
|---|---|
| `cl_waiting` is 0 and `sv_idle` is greater than 0 | The pool has headroom. |
| `cl_waiting` is greater than 0 and `sv_idle` is 0 | The pool is saturated. Check whether the queries are slow before enlarging the pool. |
| `cl_waiting` is greater than 0 and `sv_active` is below `default_pool_size` | Servers are being opened. Look at `sv_login` and at PostgreSQL's `max_connections`. |
| `sv_active` is 0 and `cl_waiting` is greater than 0 | PgBouncer cannot get servers at all. Suspect a down or refusing database, an authentication failure, or `max_db_connections`. |
SHOW STATS: traffic and timing
SHOW STATS;The totals are cumulative since PgBouncer started. They include total_xact_count, total_query_count, total_received and total_sent (bytes), total_xact_time, total_query_time, and total_wait_time. Each total has an avg_ counterpart, such as avg_xact_count and avg_wait_time. Averages are computed over the last stats_period, which is 60 seconds by default.
All time values are in microseconds. A reading of avg_wait_time = 410000 is 0.41 seconds, not 410 milliseconds off by a unit. Convert before drawing conclusions.
The distinctions that matter are:
avg_xact_timeis the average time a server connection was busy per transaction, including time the client spent idle inside the transaction. Long values with a smallavg_query_timepoint to applications that hold transactions open while doing other work.avg_query_timeis the average time spent executing a query on the server.avg_wait_timeis the average time clients spent waiting for a server connection. This is the pooler's own cost. In a healthy system it is close to zero. It rises before users notice anything else.avg_xact_countandavg_query_countare throughput per second.
Because total_ values only grow, a monitoring system should compute the rate between two samples. A restart resets the counters, so treat a drop as a reset and not as negative traffic.
Saturation arithmetic
A pool of N server connections can run at most N / avg_xact_time transactions per second. With 20 connections and an avg_xact_time of 10 ms (10000 microseconds), the ceiling is about 2000 transactions per second. If avg_xact_count approaches that ceiling and avg_wait_time rises, the pool is at its limit. You can then shorten transactions or add server connections, within the limits of PostgreSQL's max_connections.
SHOW CLIENTS and SHOW SERVERS: the detail view
When SHOW POOLS shows a problem, drill down. SHOW CLIENTS lists each client with its state (active, waiting, or idle), its address, connect_time, request_time, and wait seconds. SHOW SERVERS lists each server connection with its state (active, idle, used, login), connect_time, request_time, and remote_pid.
remote_pid is the PostgreSQL backend process ID. It joins the two systems together:
-- Run in PostgreSQL, not in the PgBouncer console.
SELECT pid, state, wait_event_type, wait_event,
now() - xact_start AS xact_age,
left(query, 80) AS query
FROM pg_stat_activity
WHERE pid IN (12345, 12346, 12347) -- values from SHOW SERVERS remote_pid
ORDER BY xact_start;If a server connection stays active for a long time, the matching pg_stat_activity row shows what it is running. A state of idle in transaction there means a client began a transaction and stopped sending work, so PgBouncer cannot hand that connection to anyone else.
A quick watch loop
During an incident, a repeating query on the console is enough:
watch -n 2 "psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer -Atc 'SHOW POOLS;' | cut -d'|' -f1-8"The flags -A and -t give unaligned output without headers. cut keeps the first eight columns, which include sv_idle, sv_used, and cl_waiting in current versions. Adjust the field list to your version.
For long-term monitoring, export the same views with a Prometheus exporter or a scheduled query. Alert on cl_waiting > 0 for more than a short time, on a rising avg_wait_time, and on maxwait climbing toward your query_wait_timeout.
Decision flow
Common reading errors
- Treating
total_values as rates. They are counters. Take the difference between samples. - Misreading units. Times are microseconds, byte counts are bytes.
- Looking only at the averages. A 60-second average hides a 5-second stall. Sample
SHOW POOLSoften enough to catch peaks inmaxwait. - Enlarging the pool as the first fix. If
avg_xact_timeis long because of slow queries or idle transactions, a bigger pool raises the load on PostgreSQL. It does not remove the cause. - Ignoring the
pgbouncerpool. The console connection appears as its own pool. It is not an application problem.
Exercise
SHOW POOLS reports clactive = 25, clwaiting = 40, svactive = 20, svidle = 0 with defaultpoolsize = 20, and SHOW STATS reports avgquerytime = 800 and avgxacttime = 900000 (both in microseconds). The application runs short queries. What does this suggest, and what do you check next?
Show answer
Queries are fast (0.8 ms on average) but each transaction occupies a server connection for about 0.9 seconds. That gap means clients hold transactions open without sending queries, for example while doing other work between statements. The pool is saturated because connections are held, not because queries are slow. Check SHOW SERVERS for long-running connections, look up their remotepid values in pgstatactivity for "idle in transaction" states, and fix the application to commit sooner, before increasing defaultpool_size.
Exercise
Your monitoring shows totalxactcount dropped from 9,400,000 to 2,100 between two samples one minute apart. Is traffic collapsing?
Show answer
Not necessarily. The total counters reset when PgBouncer restarts, so a sudden drop usually means a restart. Check the process uptime or log, treat the drop as a counter reset, and compute rates only from samples taken after it. If there was no restart, compare avgxact_count to confirm whether real traffic fell.