Manage Connection Pools
Create, read, update and delete the PgBouncer connection pools of a Managed PostgreSQL service, connect through them, and change the PgBouncer settings. For what the pool modes allow and how pools are sized, see Connection Pooling.
Prerequisites
You need:
- A running Managed PostgreSQL service, on any plan.
- The Exoscale CLI, configured with your API credentials.
psql, to connect through the pool.- Your client IP address in the service IP filter. The filter applies to the
pgbouncerport as well. See Restrict Connections to a Service.
Pools are managed with the exo api commands, which read the request body from standard input.
There is no exo dbaas command to create, change or delete a pool.
Create a Pool
echo '{"name":"POOL_NAME","database-name":"DATABASE_NAME","mode":"transaction","size":10}' | \
exo api create-dbaas-pg-connection-pool SERVICE_NAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service, for examplemy-postgresql.ZONE: zone the service runs in, for examplech-gva-2.name: required. Name of the pool, for exampleapp-pool. Clients use it as the database name.database-name: required. An existing database of the service, for exampledefaultdb. A pool serves one database.mode: optional.transaction(default),sessionorstatement.size: optional. Maximum number of server connections, from 1 to 10000. Defaults to 10.username: optional. Restricts the pool to one existing user. See below.
The request fails with HTTP 403 if the database or the user does not exist, and with HTTP 409 if
the total size of the pools of the service would exceed the connection budget described in
Connection Pooling.
Several pools can point to the same database, for example a large transaction pool for a web
application and a small session pool for a worker that needs LISTEN.
Pools With a Username
Without username, every user of the service can connect through the pool as itself. With
username, only that user can, and the others get FATAL: login rejected.
Caution
When a pool has a username, its connection-uri contains that user’s password, in the
output of exo api get-dbaas-service-pg and of exo dbaas show -O json. Treat that output as
a secret. A pool without username has a connection-uri without credentials.
List the Pools
There is no list operation for pools. Read them from the service:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q '"connection-pools"'Each entry has name, database, mode, size, connection-uri, and username when one is
set. exo dbaas show SERVICE_NAME -z ZONE also lists the pools, with their name, database, size
and mode.
Update a Pool
echo '{"mode":"session","size":20}' | \
exo api update-dbaas-pg-connection-pool SERVICE_NAME POOL_NAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.POOL_NAME: name of the pool to update.ZONE: zone the service runs in.- The body accepts
database-name,mode,sizeandusername. Send only the fields to change.
A pool cannot be renamed: a name in the body is ignored. To rename, create a new pool, move the
clients, and delete the old one.
Delete a Pool
exo api delete-dbaas-pg-connection-pool SERVICE_NAME POOL_NAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.POOL_NAME: name of the pool to delete.ZONE: zone the service runs in.
Clients connected through the pool are disconnected with FATAL: database removed, and new
connections get FATAL: no such database: POOL_NAME. Move the applications to another pool or
to the pg port first.
Connect Through a Pool
Read the host and the ports of the service:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q 'components'The pgbouncer entry carries the port of the pooler. The pg entry carries the port for direct
connections; both work at the same time. Ports are assigned per service: read them instead of
copying them from an example.
Connect with the pool name as database name:
psql "host=HOST port=PGBOUNCER_PORT dbname=POOL_NAME user=USERNAME sslmode=verify-full sslrootcert=CA_PATH"Parameters:
HOST:hostof thepgbouncercomponent.PGBOUNCER_PORT:portof thepgbouncercomponent.POOL_NAME: name of the pool.USERNAME: a user of the service, for exampleavnadmin.psqlreads the password fromPGPASSWORDor~/.pgpass.CA_PATH: path to the CA certificate fromexo dbaas ca-certificate -z ZONE.
The pgbouncer port accepts the same TLS settings as the pg port, including verify-full.
Once connected, SELECT current_database(); returns the database behind the pool, not the pool
name.
Application drivers connect the same way: set the database name to the pool name and the port to
the pgbouncer port. Keep the client connections open between requests, with the client-side
pool of your driver, so that each request does not pay for a new TLS handshake.
Prepare an Application for Transaction Mode
Before you move an application to a transaction pool:
Replace
pg_advisory_lock()withpg_advisory_xact_lock().Move session settings such as
statement_timeoutinto each transaction withSET LOCAL, or attach them to the role or the database so that every server connection starts with them:ALTER ROLE app_user IN DATABASE defaultdb SET statement_timeout = '30s';Keep
LISTENconsumers on asessionpool or on thepgport.
The behavior behind each point is described in Connection Pooling.
Change PgBouncer Settings
PgBouncer settings apply to the whole service, not to one pool. Change them with the API:
echo '{"pgbouncer-settings":{"max_prepared_statements":200}}' | \
exo api update-dbaas-service-pg SERVICE_NAME -z ZONEParameters:
SERVICE_NAME: name of the Managed PostgreSQL service.ZONE: zone the service runs in.
The update is merged into the stored settings and applies within seconds, without a restart of PostgreSQL. Read the stored values with:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q '"pgbouncer-settings"'exo dbaas update SERVICE_NAME -z ZONE -f --pg-bouncer-settings='JSON_OBJECT' sets most of these
settings too, but it silently drops server_connect_timeout, server_login_retry and any setting
whose value is 0. Use the API command above for those.
The available settings, with their ranges and defaults, are listed by:
exo api get-dbaas-settings-pg -q 'settings.pgbouncer.properties'| Setting | Effect |
|---|---|
max_prepared_statements | Protocol-level prepared statements PgBouncer tracks per client, 0 to 3000, default 100. 0 breaks driver prepared statements in transaction and statement modes. |
min_pool_size | Server connections kept open per pool even when idle. |
server_idle_timeout | Seconds before an idle server connection is closed, default 600. |
server_lifetime | Seconds after which an unused server connection is closed, default 3600. |
server_reset_query_always | When true, runs DISCARD ALL when a server connection returns to the pool in every mode, not only in session mode. |
server_login_retry | Seconds PgBouncer waits before retrying after a failed login to PostgreSQL, default 15 in the running configuration. |
ignore_startup_parameters | Startup parameters PgBouncer accepts instead of rejecting the client. Only extra_float_digits and search_path are allowed. |
autodb_pool_mode, autodb_pool_size, autodb_idle_timeout, autodb_max_db_connections | Settings for pools that PgBouncer creates automatically. |
To read the values PgBouncer is running with, use SHOW CONFIG; in the PgBouncer console,
described in
Troubleshoot Connection Pooling.