Sub topic 11
Using the admin console
This chapter assumes you have a working pgbouncer.ini from the earlier chapters, can use psql, and know how pool modes and default_pool_size shape the connections to PostgreSQL. It covers how to reach the admin console, which commands read state, which commands change state, and how to use them safely.
What the admin console is
The admin console is a virtual database named pgbouncer. PgBouncer answers queries sent to it itself and never forwards them to PostgreSQL. You connect with a normal PostgreSQL client, but the commands are PgBouncer's own. They look like SQL but are not SQL, so you can't join or filter their output with a WHERE clause.
Two settings in the [pgbouncer] section control who may connect:
admin_userslists roles that can run every command, including those that change state.stats_userslists roles that can run only read-onlySHOWcommands. It suits monitoring agents.
Neither setting creates a role. PgBouncer still has to authenticate the user, so the name must be known through auth_file or, on recent versions, auth_query. A user in admin_users with no entry in userlist.txt is rejected at login. If you set auth_type = peer or auth_type = hba, a local Unix-socket connection can authenticate through the operating system instead.
Connecting
Connect to the pgbouncer database on the PgBouncer port, not the PostgreSQL port:
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncerThe console speaks only the simple query protocol. Plain psql works. A driver that sends prepared statements by default, or that tries to read catalog tables on connect, may fail against it. Backslash commands such as \d will not work either, because they query system catalogs that the console doesn't have.
You can also run one command non-interactively:
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer -c "SHOW POOLS;"Commands that read state
Every SHOW command returns a table. Start with SHOW HELP; to see the commands your version supports, because the list grows between releases.
| Command | What it reports |
|---|---|
| `SHOW DATABASES` | One row per `[databases]` entry: host, port, the pool size limits, current connection counts, and whether the database is paused or disabled |
| `SHOW POOLS` | One row per user and database pair: client and server connection counts by state, and the longest wait |
| `SHOW STATS` | Per-database totals and averages for requests, bytes, and time |
| `SHOW CLIENTS` | One row per client connection, with its state and addresses |
| `SHOW SERVERS` | One row per server connection to PostgreSQL, with its state and addresses |
| `SHOW CONFIG` | Every setting, its value, its default, and whether it can be changed at runtime |
| `SHOW LISTS` | Internal counters, such as how many clients and servers are free or in use |
| `SHOW VERSION` | The PgBouncer version |
Reading SHOW POOLS
SHOW POOLS is the command you will use most. A trimmed example:
database | user | cl_active | cl_waiting | sv_active | sv_idle | sv_used | maxwait | pool_mode
----------+----------+-----------+------------+-----------+---------+---------+---------+-------------
appdb | app_user | 18 | 37 | 20 | 0 | 0 | 4 | transaction
pgbouncer| pgbouncer| 1 | 0 | 0 | 0 | 0 | 0 | statementThe columns that matter most:
cl_activecounts clients that are linked to a server connection right now.cl_waitingcounts clients that have sent a query and are waiting for a server connection. A value that stays above zero means the pool is the bottleneck.sv_activecounts server connections that are linked to a client.sv_idlecounts server connections that are free for immediate use.sv_usedcounts server connections that were idle longer thanserver_check_delayand will be checked before reuse.maxwaitis the age in seconds of the oldest waiting client. It is a better alarm signal thancl_waiting, because it tells you how long users are actually blocked.
In the example, sv_active is 20, which equals default_pool_size, while 37 clients wait. The pool is full and busy. Raising the pool size is one answer. Finding slow transactions that hold connections too long is the better one. Chapter 12 covers how to interpret these numbers over time.
Finding who holds a server connection
When a pool is saturated, SHOW SERVERS shows what each server connection is doing:
SHOW SERVERS;Look at the state, connect_time, and request_time columns. A server in the active state whose request_time is minutes old points to a long transaction. Match its local_addr and local_port against SHOW CLIENTS to find which client owns it. You can then match that client in pg_stat_activity on the PostgreSQL side.
Commands that change state
These commands require admin_users.
| Command | Effect |
|---|---|
| `RELOAD` | Re-reads `pgbouncer.ini` and the authentication file. Clients stay connected. |
| `SET key = value` | Changes one setting at runtime. The change is lost on the next `RELOAD` or restart. |
| `PAUSE [db]` | Stops handing out server connections. It waits for in-flight transactions to finish, then releases the server connections. |
| `RESUME [db]` | Ends a `PAUSE` or `SUSPEND`. |
| `DISABLE db` | Rejects new client connections to that database. Existing clients keep working. |
| `ENABLE db` | Undoes `DISABLE`. |
| `RECONNECT [db]` | Closes server connections as they become idle, so new ones are opened. |
| `KILL db` | Drops all client and server connections for that database immediately. |
| `SHUTDOWN` | Stops PgBouncer immediately. |
RELOAD and SET
RELOAD is the normal way to apply a configuration edit:
RELOAD;Some settings can't change this way. listen_addr, listen_port, and unix_socket_dir, for example, need a restart. Check the changeable column of SHOW CONFIG before relying on a runtime change:
SHOW CONFIG;SET is useful for quick experiments:
SET default_pool_size = 30;New pool limits apply to connections opened after the change. Write the value into pgbouncer.ini as well, because the next RELOAD replaces the value you set with the one from the file.
Pausing for maintenance
PAUSE lets you restart PostgreSQL or switch its address without clients seeing errors. Clients keep their connections to PgBouncer. Their new queries simply wait.
In the console, the sequence is:
PAUSE appdb;
-- restart or fail over PostgreSQL now
RESUME appdb;PAUSE does not return until every server connection in the pool is released. A long-running transaction therefore blocks it. Use SHOW SERVERS in a second console session to see what it is waiting for. If your tool has a timeout, make it longer than your longest expected transaction. Always RESUME afterwards, even after a failed maintenance step. Clients stay queued until you do.
Draining a database
To take a database out of service gracefully:
DISABLE appdb;
SHOW CLIENTS;New logins to appdb now fail, and existing sessions finish normally. Once SHOW CLIENTS shows no remaining clients for it, do your work. Then run ENABLE appdb;.
Common mistakes
- Using the PostgreSQL port. Connecting to 5432 with database
pgbouncergives "database does not exist". Use the PgBouncer port, 6432 in this tutorial. - Forgetting the auth entry. The role in
admin_usersmust also authenticate. Withauth_type = scram-sha-256, it needs a hash inuserlist.txt. - Relying on
SETalone. The value disappears at the nextRELOAD. - Leaving PgBouncer paused. Clients appear to hang.
SHOW DATABASESshows a paused flag. - Treating
SHOWoutput as SQL. You can't filter it withWHERE. Pipepsqloutput throughgreporawk, or usepsql -A -tfor scripted parsing.
A small script that alerts on a waiting client uses psql flags for unaligned, tuples-only output:
psql -h 127.0.0.1 -p 6432 -U pgbouncer_admin pgbouncer -A -t -F ',' -c "SHOW POOLS;" \
| awk -F',' '$1 == "appdb" && $4 > 0 { print "waiting clients:", $4 }'Column 4 is cl_waiting only in some versions, because newer releases add columns. Check the header from a plain SHOW POOLS; first, and prefer a monitoring exporter for anything permanent.
Exercise
Your application's latency has spiked. SHOW POOLS shows cl_waiting = 40 and maxwait = 12 for appdb, with sv_active equal to default_pool_size. Which console commands help you find the cause, and why is raising default_pool_size with SET alone not a lasting fix?
Show answer
Run SHOW SERVERS to see which server connections are active with an old request_time, then match their local address and port in SHOW CLIENTS to find the client that owns them. Compare with pg_stat_activity on PostgreSQL to see the query and transaction age. SET default_pool_size changes only the running process, so the next RELOAD or restart restores the file value. A bigger pool also doesn't remove a slow transaction that holds a connection, and it can push the total over PostgreSQL's max_connections.
Exercise
You must restart PostgreSQL for a minor version upgrade, and clients should see a short delay and not errors. Give the console commands in order, and name one risk of the first step.
Show answer
Run PAUSE appdb;, restart PostgreSQL, then run RESUME appdb;. The risk is that PAUSE waits for all open transactions to finish, so a long transaction can make it block for a long time. Meanwhile new client queries queue up. If you forget RESUME, clients stay stuck.