Pgbouncer Connection Pooling
Overview
Pigsty uses Pgbouncer for PostgreSQL connection pooling.
By default, it listens on port 6432 and proxies connections to local PostgreSQL on port 5432.
This component is optional. If you do not need many connections, transaction pooling, or query metrics, you can disable it. You can also connect directly to the database or leave Pgbouncer unused.
User & Database Management
Pigsty automatically manages Pgbouncer users and databases. When you create databases or users, it applies the database configuration and user configuration.
Database Management: By default, Pigsty adds databases from pg_databases to Pgbouncer.
To exclude a database, set pgbouncer: false.
User Management: To add a user from pg_users to the pool user list, explicitly set pgbouncer: true.
Since Pigsty
v4.1.0, usepool_reserveandpool_connlimitfor database pool fields. These names replace the legacy aliasespool_size_reserveandpool_max_db_conn.
Service Management
By default, the PostgreSQL Primary Service and Replica Service connect to Pgbouncer on port 6432.
To connect directly to PostgreSQL, customize pg_services.
Alternatively, 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 automatically manages database.txt and userlist.txt.
It updates them when you create databases or users.
You can edit configuration manually, then run RELOAD to apply the changes:
Pool Management
Pgbouncer runs as the same dbsu as PostgreSQL.
The default is the postgres operating system user.
Pigsty provides the pgb alias for administration:
On database nodes, use pgb to connect to the Pgbouncer administration console.
You can then run administration commands and monitoring queries.
| 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 the active transaction or session to finish, according to the pool mode.
It then disconnects server connections.
New client requests wait until RESUME.
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
After pause, SHOW DATABASES shows paused status:
RESUME
Use RESUME to restore databases paused by PAUSE, KILL, or SUSPEND.
This allows new connections and resumes normal service.
DISABLE
Use DISABLE to reject all new client connections for a database.
Existing connections continue.
Typical use cases:
- Temporarily offline a database for maintenance
- Block new connections for safe database migration
- Gradually decommission a database being removed
ENABLE
Use ENABLE to enable a database that DISABLE stopped.
The database accepts new client connections again.
RECONNECT
Use RECONNECT to rebuild server connections gracefully.
Pgbouncer closes connections when they return to the pool.
It creates new connections as needed.
Typical use cases:
- Refresh connections after backend database IP change
- Reroute traffic after switchover
- Rebuild connections after DNS update
After RECONNECT, use WAIT_CLOSE to wait for old connections to fully release.
KILL
Use KILL to disconnect all client and server connections for a database immediately.
Unlike PAUSE, it does not wait for transactions to finish.
After KILL, new connections are blocked until RESUME.
KILL_CLIENT
Use KILL_CLIENT to terminate a specific client connection.
Get the client ID from SHOW CLIENTS.
SUSPEND
Use SUSPEND to suspend Pgbouncer.
It flushes all socket buffers and stops listening until RESUME.
SUSPEND is mainly for Pgbouncer online restart (zero-downtime upgrade):
SHUTDOWN
Use SHUTDOWN to stop the Pgbouncer process.
The command supports several shutdown modes:
| 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 |
RELOAD
Use RELOAD to read the Pgbouncer configuration files again.
It dynamically updates most parameters without a process restart.
Pigsty provides playbook task to reload Pgbouncer config:
WAIT_CLOSE
Use WAIT_CLOSE to wait until server connections finish closing.
Usually, run it after RECONNECT or RELOAD to verify that old connections have closed.
Monitoring
Pgbouncer provides these SHOW commands to monitor 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:
For more monitoring command details, see Pgbouncer official docs.
Unix Signals
Use Unix signals to control Pgbouncer when the administration 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 |
Traffic Switching
Pigsty stores managed database routes in /etc/pgbouncer/database.txt.
To move Pgbouncer traffic for one database to another node, edit this file and reload the configuration.
Then drain and rebuild the existing server connections:
The
pgb-routefunction currently shipped in the source edits only/etc/pgbouncer/pgbouncer.ini. That file includesdatabase.txtbut does not contain the generatedhost=routes for each database. Thus, the function does not change managed database backends. Use the procedure above instead.
Was this page helpful?
Thanks—your feedback helps us improve this page.
What got in the way? (optional)