Pgbouncer Connection Pooling
Overview
Pigsty uses Pgbouncer as PostgreSQL connection pooling middleware, listening on port 6432 by default, proxying access to local PostgreSQL on port 5432.
This is an optional component. If you don’t have massive connections or need transaction pooling and query metrics, you can disable it, connect directly to the database, or keep it unused.
User & Database Management
Pgbouncer users and databases are auto-managed by Pigsty, applying database config and user config when creating databases and creating users.
Database Management: Databases defined in pg_databases are auto-added to Pgbouncer by default. Set pgbouncer: false to exclude specific databases.
pg_databases:
- name: mydb # Added to connection pool by default
pool_mode: transaction # Database-level pool mode
pool_size: 64 # Default pool size
- name: internal
pgbouncer: false # Excluded from connection pool
User Management: Users defined in pg_users need explicit pgbouncer: true to be added to connection pool user list.
pg_users:
- name: dbuser_app
password: DBUser.App
pgbouncer: true # Add to connection pool user list
pool_mode: transaction # User-level pool mode
Service Management
In Pigsty, PostgreSQL cluster Primary Service and Replica Service default to Pgbouncer port 6432.
To bypass connection pool and access PostgreSQL directly, customize pg_services, or set pg_default_service_dest to postgres.
Config Management
Pgbouncer config files are in /etc/pgbouncer/, generated and managed by Pigsty:
| File | Description |
|---|---|
pgbouncer.ini | Main config, pool-level params |
database.txt | Database list, database-level params |
userlist.txt | User password list |
useropts.txt | User-level pool params |
pgb_hba.conf | HBA access control rules |
Pigsty auto-manages database.txt and userlist.txt, updating them when creating databases or creating users.
You can manually edit config then RELOAD to apply:
# Edit config
$ vim /etc/pgbouncer/pgbouncer.ini
# Reload via systemctl
$ sudo systemctl reload pgbouncer
# Reload as pg_dbsu / postgres user
$ pgb -c "RELOAD;"
Pool Management
Pgbouncer runs as the same dbsu as PostgreSQL, default postgres OS user. Pigsty provides pgb alias for easy management:
alias pgb="psql -p 6432 -d pgbouncer -U postgres"
Use pgb on database nodes to connect to Pgbouncer admin console for management commands and monitoring queries.
$ pgb
pgbouncer=# SHOW POOLS;
pgbouncer=# SHOW CLIENTS;
pgbouncer=# SHOW SERVERS;
| Command | Function | Description |
|---|---|---|
PAUSE | Pause | Pause database, wait for txn completion then disconnect |
RESUME | Resume | Resume database paused by PAUSE/KILL/SUSPEND |
DISABLE | Disable | Reject new client connections for database |
ENABLE | Enable | Allow new client connections for database |
RECONNECT | Reconnect | Gracefully close and rebuild server connections |
KILL | Kill | Immediately disconnect all client and server connections |
KILL_CLIENT | Kill Client | Terminate specific client connection |
SUSPEND | Suspend | Flush buffers and stop listening, for online restart |
SHUTDOWN | Shutdown | Shutdown Pgbouncer process |
RELOAD | Reload | Reload config files |
WAIT_CLOSE | Wait Close | Wait for server connections to close after RECONNECT/RELOAD |
| Monitor Commands | Monitor | View pool status, clients, servers, etc. |
PAUSE
Use PAUSE to pause database connections. Pgbouncer waits for active txn/session to complete based on pool mode, then disconnects server connections. New client requests are blocked until RESUME.
PAUSE [db]; -- Pause specified database, or all if not specified
Typical use cases:
- Online backend database switch (e.g., update connection target after switchover)
- Maintenance operations requiring all connections disconnected
- Combined with
SUSPENDfor Pgbouncer online restart
$ pgb -c "PAUSE mydb;" # Pause mydb database
$ pgb -c "PAUSE;" # Pause all databases
After pause, SHOW DATABASES shows paused status:
pgbouncer=# SHOW DATABASES;
name | host | port | database | ... | paused | disabled
----------+-----------+------+----------+-----+--------+----------
mydb | /var/run | 5432 | mydb | ... | 1 | 0
RESUME
Use RESUME to restore databases paused by PAUSE, KILL, or SUSPEND, allowing new connections and resuming normal service.
RESUME [db]; -- Resume specified database, or all if not specified
$ pgb -c "RESUME mydb;" # Resume mydb database
$ pgb -c "RESUME;" # Resume all databases
DISABLE
Use DISABLE to disable a database, rejecting all new client connection requests. Existing connections are unaffected.
DISABLE db; -- Disable specified database (database name required)
Typical use cases:
- Temporarily offline a database for maintenance
- Block new connections for safe database migration
- Gradually decommission a database being removed
$ pgb -c "DISABLE mydb;" # Disable mydb, new connections rejected
ENABLE
Use ENABLE to enable a database previously disabled by DISABLE, accepting new client connections again.
ENABLE db; -- Enable specified database (database name required)
$ pgb -c "ENABLE mydb;" # Enable mydb, allow new connections
RECONNECT
Use RECONNECT to gracefully rebuild server connections. Pgbouncer closes connections when released back to pool, creating new ones when needed.
RECONNECT [db]; -- Rebuild server connections for database, or all if not specified
Typical use cases:
- Refresh connections after backend database IP change
- Reroute traffic after switchover
- Rebuild connections after DNS update
$ pgb -c "RECONNECT mydb;" # Rebuild mydb server connections
$ pgb -c "RECONNECT;" # Rebuild all server connections
After RECONNECT, use WAIT_CLOSE to wait for old connections to fully release.
KILL
Use KILL to immediately disconnect all client and server connections for a database. Unlike PAUSE, KILL doesn’t wait for transaction completion - forces immediate disconnect.
KILL [db]; -- Kill all connections for database, or all (except admin) if not specified
$ pgb -c "KILL mydb;" # Force disconnect all mydb connections
$ pgb -c "KILL;" # Force disconnect all database connections (except admin)
After KILL, new connections are blocked until RESUME.
KILL_CLIENT
Use KILL_CLIENT to terminate a specific client connection. Client ID can be obtained from SHOW CLIENTS output.
KILL_CLIENT id; -- Terminate client connection with specified ID
# View client connections
$ pgb -c "SHOW CLIENTS;"
# Terminate specific client (assuming ptr column shows ID 0x1234567890)
$ pgb -c "KILL_CLIENT 0x1234567890;"
SUSPEND
Use SUSPEND to suspend Pgbouncer. Flushes all socket buffers and stops listening until RESUME.
SUSPEND; -- Suspend Pgbouncer
SUSPEND is mainly for Pgbouncer online restart (zero-downtime upgrade):
# 1. Suspend current Pgbouncer
$ pgb -c "SUSPEND;"
# 2. Start new Pgbouncer process (with -R option to take over sockets)
$ pgbouncer -R /etc/pgbouncer/pgbouncer.ini
# 3. New process takes over, old process exits automatically
SHUTDOWN
Use SHUTDOWN to shut down Pgbouncer process. Multiple shutdown modes supported:
SHUTDOWN; -- Immediate shutdown
SHUTDOWN WAIT_FOR_SERVERS; -- Wait for server connections to release
SHUTDOWN WAIT_FOR_CLIENTS; -- Wait for clients to disconnect (zero-downtime rolling restart)
| Mode | Description |
|---|---|
SHUTDOWN | Immediately shutdown Pgbouncer |
WAIT_FOR_SERVERS | Stop accepting new connections, wait for server release |
WAIT_FOR_CLIENTS | Stop accepting new connections, wait for all clients disconnect, for rolling restart |
$ pgb -c "SHUTDOWN WAIT_FOR_CLIENTS;" # Graceful shutdown, wait for clients
RELOAD
Use RELOAD to reload Pgbouncer config files. Dynamically updates most config params without process restart.
RELOAD; -- Reload config files
$ pgb -c "RELOAD;" # Reload via admin console
$ systemctl reload pgbouncer # Reload via systemd
$ kill -SIGHUP $(cat /var/run/pgbouncer/pgbouncer.pid) # Reload via signal
Pigsty provides playbook task to reload Pgbouncer config:
./pgsql.yml -l <cls> -t pgbouncer_reload # Reload cluster Pgbouncer config
WAIT_CLOSE
Use WAIT_CLOSE to wait for server connections to finish closing. Typically used after RECONNECT or RELOAD to ensure old connections are fully released.
WAIT_CLOSE [db]; -- Wait for server connections to close, or all if not specified
# Complete connection rebuild flow
$ pgb -c "RECONNECT mydb;"
$ pgb -c "WAIT_CLOSE mydb;" # Wait for old connections to release
Monitoring
Pgbouncer provides rich SHOW commands for monitoring pool status:
| Command | Description |
|---|---|
SHOW HELP | Show available commands |
SHOW DATABASES | Show database config and status |
SHOW POOLS | Show pool statistics |
SHOW CLIENTS | Show client connection list |
SHOW SERVERS | Show server connection list |
SHOW USERS | Show user config |
SHOW STATS | Show statistics (requests, bytes) |
SHOW STATS_TOTALS | Show cumulative statistics |
SHOW STATS_AVERAGES | Show average statistics |
SHOW CONFIG | Show current config params |
SHOW MEM | Show memory usage |
SHOW DNS_HOSTS | Show DNS cached hostnames |
SHOW DNS_ZONES | Show DNS cached zones |
SHOW SOCKETS | Show open socket info |
SHOW ACTIVE_SOCKETS | Show active sockets |
SHOW LISTS | Show internal list counts |
SHOW FDS | Show file descriptor usage |
SHOW STATE | Show Pgbouncer running state |
SHOW VERSION | Show Pgbouncer version |
Common monitoring examples:
# View pool status
$ pgb -c "SHOW POOLS;"
# View client connections
$ pgb -c "SHOW CLIENTS;"
# View server connections
$ pgb -c "SHOW SERVERS;"
# View statistics
$ pgb -c "SHOW STATS;"
# View database status
$ pgb -c "SHOW DATABASES;"
For more monitoring command details, see Pgbouncer official docs.
Unix Signals
Pgbouncer supports Unix signal control, useful when admin console is unavailable:
| Signal | Equivalent Command | Description |
|---|---|---|
SIGHUP | RELOAD | Reload config files |
SIGTERM | SHUTDOWN WAIT_FOR_CLIENTS | Graceful shutdown, wait clients |
SIGINT | SHUTDOWN WAIT_FOR_SERVERS | Graceful shutdown, wait servers |
SIGQUIT | SHUTDOWN | Immediate shutdown |
SIGUSR1 | PAUSE | Pause all databases |
SIGUSR2 | RESUME | Resume all databases |
# Reload config via signal
$ kill -SIGHUP $(cat /var/run/pgbouncer/pgbouncer.pid)
# Graceful shutdown via signal
$ kill -SIGTERM $(cat /var/run/pgbouncer/pgbouncer.pid)
# Pause via signal
$ kill -SIGUSR1 $(cat /var/run/pgbouncer/pgbouncer.pid)
# Resume via signal
$ kill -SIGUSR2 $(cat /var/run/pgbouncer/pgbouncer.pid)
Traffic Switching
Pigsty provides pgb-route utility function to quickly switch Pgbouncer traffic to other nodes for zero-downtime migration:
# Definition (already in /etc/profile.d/pg-alias.sh)
function pgb-route(){
local ip=${1-'\/var\/run\/postgresql'}
sed -ie "s/host=[^[:space:]]\+/host=${ip}/g" /etc/pgbouncer/pgbouncer.ini
cat /etc/pgbouncer/pgbouncer.ini
}
# Usage: Route traffic to 10.10.10.12
$ pgb-route 10.10.10.12
$ pgb -c "RECONNECT; WAIT_CLOSE;"
Complete zero-downtime switching flow:
# 1. Modify route target
$ pgb-route 10.10.10.12
# 2. Reload config
$ pgb -c "RELOAD;"
# 3. Rebuild connections and wait for old connections to release
$ pgb -c "RECONNECT;"
$ pgb -c "WAIT_CLOSE;"
Feedback
Was this page helpful?
Thanks for the feedback! Please let us know how we can improve.
Sorry to hear that. Please let us know how we can improve.