Connection Pooling
Every Managed PostgreSQL service runs PgBouncer next to PostgreSQL. This page explains what the pooler does, what each pool mode allows, and how the platform limits pool sizes. The procedures are in Manage Connection Pools.
Why Use a Pooler
PostgreSQL starts one server process per connection, and the number of connections is capped by
max_connections, which depends on the plan. Read it on your service:
SHOW max_connections;The API returns the same value in the max-connections field of the service:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q '"max-connections"'Parameters:
SERVICE_NAME: name of your Managed PostgreSQL service, for examplemy-postgresql.ZONE: zone the service runs in, for examplech-gva-2.
When many application instances each keep their own client-side pool, the total number of connections they want can exceed that limit, while most of those connections sit idle. PgBouncer accepts many client connections and shares a small number of server connections between them.
The PgBouncer Endpoint
Every service, Hobbyist plans included, exposes a pgbouncer component on its own port, next to
the pg component:
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q 'components'You do not enable PgBouncer and cannot remove it. To use it, you create pools: a pool is a name
that clients use as the database name on the pgbouncer port, and that points to one database
of the service, in one mode, with a maximum number of server connections. Pools are created
through the API, not with exo dbaas.
Pool Modes
The mode decides how long a client keeps a server connection.
| Mode | A server connection is held for | Use it for |
|---|---|---|
session | the whole client connection | applications that rely on session state |
transaction | one transaction | most applications; the default when no mode is given |
statement | one statement | autocommit-only workloads |
In session mode, everything a direct connection can do works, but an idle client still holds
a server connection, so the pool serves at most size clients at a time.
In transaction mode, a client holds no server connection between transactions. Session state
does not follow the client from one transaction to the next:
| Feature | Behavior in transaction mode |
|---|---|
SET outside a transaction, for example SET statement_timeout | Stays on the server connection: the client loses it when it moves to another connection, and the next client of that connection inherits it. search_path is an exception: PgBouncer keeps it per client. |
SET LOCAL inside a transaction | Works. |
LISTEN | Accepted, but notifications are never delivered to the client. |
Session advisory locks (pg_advisory_lock) | The lock stays on the server connection, even after the client disconnects. Use pg_advisory_xact_lock(). |
SQL PREPARE then EXECUTE in a later transaction | Fails with prepared statement "..." does not exist when the later transaction runs on another connection. |
| Driver prepared statements (extended query protocol) | Work, see below. |
statement mode has the same limits, and also rejects any transaction block: a BEGIN closes
the client connection with transaction blocks not allowed in statement pooling mode.
For the full upstream feature map, see PgBouncer features.
Prepared Statements in Transaction Mode
PgBouncer tracks the prepared statements that drivers create with the extended query protocol,
and prepares them again on whichever server connection the client lands on. The
max_prepared_statements setting controls this. It defaults to 100 on Managed PostgreSQL and
accepts values from 0 to 3000.
With the default, drivers that prepare statements automatically, such as pgx in Go, work
through a transaction pool. With max_prepared_statements set to 0, the same driver fails
with errors such as prepared statement "stmtcache_..." already exists.
SQL-level PREPARE is not tracked. Keep PREPARE and EXECUTE in the same transaction, or let
the driver prepare statements.
Who Connects Through a Pool
A pool can be created with or without a username:
- Without
username: any user of the service can connect through the pool, and the session runs as that user. The pool URI contains no credentials. - With
username: only that user can connect through the pool. Other users are rejected withFATAL: login rejected. The pool URI returned by the API then contains that user’s password.
Pool Sizes and the Connection Budget
size is the maximum number of server connections the pool opens, from 1 to 10000, and 10 when
omitted.
The platform refuses a pool if the sum of the size of all pools exceeds max_connections
minus 3 connections reserved for maintenance: on a hobbyist-2 service, max_connections is
25 and the pools can total 22. The error is an HTTP 409 that says
Pools attached to the PostgreSQL instance have more connections=... than the database can handle=....
That check does not cover everything that uses connections:
- Direct connections on the
pgport use the samemax_connections. - PgBouncer applies
sizeper user: a pool withoutusernameused by two different users can open up to twice itssize.
A useful starting point is a few connections per CPU core of a node, not one per client: the pooler exists so that the two numbers are no longer tied.
The Pool Name Is the Database Name
On the pgbouncer port, the database name in the connection string is the pool name:
postgres://USERNAME:PASSWORD@HOST:PGBOUNCER_PORT/POOL_NAME?sslmode=requireParameters:
USERNAME,PASSWORD: credentials of a user of the service.HOST: hostname of the service.PGBOUNCER_PORT: port of thepgbouncercomponent.POOL_NAME: name of the pool.
Once connected, SELECT current_database(); returns the database behind the pool. A database
name that is not a pool name fails with FATAL: no such database: ....