---
title: "Connection Pooling"
description: "How PgBouncer works on Managed PostgreSQL: the pgbouncer endpoint, the three pool modes, prepared statements and pool sizing."
url: https://community.exoscale.com/product/dbaas/service-specific/postgresql/overview/connection-pooling/
product: "PostgreSQL"
section: product
last_updated: 2026-11-10
---
> For AI agents: the documentation index is at https://community.exoscale.com/llms.txt. Every page is available as markdown at `<page URL>index.md` or with `Accept: text/markdown`.

# 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](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-connection-pools/index.md).

## 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:

```sql
SHOW max_connections;
```

The API returns the same value in the `max-connections` field of the service:

```bash
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q '"max-connections"'
```

Parameters:

- `SERVICE_NAME`: name of your Managed PostgreSQL service, for example `my-postgresql`.
- `ZONE`: zone the service runs in, for example `ch-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:

```bash
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](https://www.pgbouncer.org/features.html).

## 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 with
  `FATAL: 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 `pg` port use the same `max_connections`.
- PgBouncer applies `size` per user: a pool without `username` used by two different users can
  open up to twice its `size`.

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:

```text
postgres://USERNAME:PASSWORD@HOST:PGBOUNCER_PORT/POOL_NAME?sslmode=require
```

Parameters:

- `USERNAME`, `PASSWORD`: credentials of a user of the service.
- `HOST`: hostname of the service.
- `PGBOUNCER_PORT`: port of the `pgbouncer` component.
- `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: ...`.

## Related Pages

- [Manage Connection Pools](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-connection-pools/index.md)
- [Troubleshoot Connection Pooling](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/troubleshoot-connection-pooling/index.md)
- [Connect with psql](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/connect-with-psql/index.md)
- [Limits and Quotas](https://community.exoscale.com/product/dbaas/service-specific/postgresql/service-boundaries/limits-and-quotas/index.md)
- [Scale DBaaS Services](https://community.exoscale.com/product/dbaas/how-to/scaling/index.md)

