---
title: "Troubleshoot Connection Pooling"
description: "Diagnose PgBouncer problems on Managed PostgreSQL: exhausted pools, cached login errors, rejected clients and transaction mode side effects."
url: https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/troubleshoot-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`.

# Troubleshoot Connection Pooling

Diagnose the PgBouncer pooler of a Managed PostgreSQL service: read its state, recognize an exhausted pool, a cached login error, a rejected client, and the side effects of `transaction` mode.

## Read the State of the Pooler

A client waiting for a server connection has not reached PostgreSQL yet, so it does not appear in
`pg_stat_activity`. PgBouncer exposes its own state in a virtual database named `pgbouncer`, on
the `pgbouncer` port.

Read the host and the port:

```bash
exo api get-dbaas-service-pg SERVICE_NAME -z ZONE -q 'components'
```

Parameters:

- `SERVICE_NAME`: name of the Managed PostgreSQL service, for example `my-postgresql`.
- `ZONE`: zone the service runs in, for example `ch-gva-2`.

Connect to the console with the admin user:

```bash
psql "host=HOST port=PGBOUNCER_PORT dbname=pgbouncer user=avnadmin sslmode=require"
```

Parameters:

- `HOST`: `host` of the `pgbouncer` component.
- `PGBOUNCER_PORT`: `port` of the `pgbouncer` component.

`psql` reads the password from `PGPASSWORD`, for example from
`exo dbaas user reveal-secrets SERVICE_NAME avnadmin -z ZONE`.

The console accepts PgBouncer `SHOW` commands only. SQL is rejected with `invalid command`:

```text
SHOW POOLS;
SHOW CLIENTS;
SHOW SERVERS;
SHOW DATABASES;
SHOW STATS;
SHOW LISTS;
SHOW CONFIG;
SHOW HELP;
```

Commands that change the pooler, such as `PAUSE` or `RELOAD`, fail with
`ERROR: admin access needed`. Change PgBouncer through the service settings, as described in
[Manage Connection Pools](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-connection-pools/index.md#change-pgbouncer-settings).

`SHOW CONFIG` lists the values PgBouncer runs with. Some of them cannot be changed through the
service settings:

| Setting | Value | Can be changed |
| --- | --- | --- |
| `max_client_conn` | 5000 | No |
| `default_pool_size` | 10 | No, set `size` per pool |
| `query_wait_timeout` | 120 s | No |
| `client_login_timeout` | 60 s | No |
| `server_login_retry` | 15 s | Yes, `server_login_retry` |
| `max_prepared_statements` | 100 | Yes |
| `server_reset_query_always` | off | Yes |

## The Pool Is Exhausted

Symptoms: queries are slow or time out through the pool, while PostgreSQL shows no slow query and
only as many active backends as the pool `size`. The extra clients wait inside PgBouncer.

`SHOW POOLS` shows one row per pool and user:

| Column | Meaning | When the pool is exhausted |
| --- | --- | --- |
| `cl_active` | clients linked to a server connection | equals the pool `size` |
| `cl_waiting` | clients waiting for a server connection | greater than 0 |
| `sv_active` | server connections in use | equals the pool `size` |
| `sv_idle` | free server connections | 0 |
| `maxwait` | seconds the oldest waiting client has waited | grows |

For example, five clients each running a 20-second query on a pool of `size` 2 show `cl_active`
2, `cl_waiting` 3, `sv_active` 2, `sv_idle` 0: the waiting clients run one after the other.

A client waits at most `query_wait_timeout`, 120 seconds. After that, PgBouncer closes the
connection with the error `query_wait_timeout`.

What to do:

1. **Shorten the transactions.** In `transaction` mode a server connection is held for the whole
   transaction, including the time the application spends between statements. Cap idle
   transactions on the service:

   ```bash
   exo dbaas update SERVICE_NAME -z ZONE -f \
     --pg-settings='{"idle_in_transaction_session_timeout":300000}'
   ```

   Parameters:

   - `SERVICE_NAME`: name of the Managed PostgreSQL service.
   - `ZONE`: zone the service runs in.
   - `idle_in_transaction_session_timeout`: milliseconds a session may stay idle inside a
     transaction before PostgreSQL ends it.

2. **Check the mode.** A `session` pool serves at most `size` clients at a time, however short
   their queries are. For a web application, use `transaction`.
3. **Raise the pool `size`**, within the connection budget described in
   [Connection Pooling](https://community.exoscale.com/product/dbaas/service-specific/postgresql/overview/connection-pooling/index.md#pool-sizes-and-the-connection-budget).
   If the budget is too small, change the plan: see
   [Scale DBaaS Services](https://community.exoscale.com/product/dbaas/how-to/scaling/index.md).

## Clients Get a Cached Login Error

```text
server login has been failing, cached error: ... (server_login_retry)
```

When PgBouncer fails to log in to PostgreSQL, it returns the same error to the clients of that
pool for `server_login_retry` seconds, 15 by default, before trying again. The error keeps
appearing for up to 15 seconds after the cause is gone.

A common cause is a full PostgreSQL: when direct connections on the `pg` port use all of
`max_connections`, PgBouncer cannot open a server connection, and its clients get the cached
`remaining connection slots are reserved for roles with the SUPERUSER attribute` error. Count the
direct and pooled backends, as shown in
[Count Connections on Both Sides](#count-connections-on-both-sides), and lower the pool sizes or
the number of direct connections.

## A Client Is Rejected

| Error | Cause |
| --- | --- |
| `FATAL: no such database: NAME` | The database name on the `pgbouncer` port is not a pool name. Use the pool name; `SHOW DATABASES` lists pools in its `name` column and the database behind each one in its `database` column. |
| `FATAL: login rejected` | The pool has a `username`, and the client connects as another user. Connect as that user, or use a pool without `username`. |
| `FATAL: unsupported startup parameter in options: ...` | The client sends a startup parameter PgBouncer does not accept, for example `options=-c statement_timeout=...`. |
| `FATAL: transaction blocks not allowed in statement pooling mode` | The client sends `BEGIN` to a `statement` pool. |
| `FATAL: database removed` | The pool was deleted while the client was connected. |
| The connection hangs, then times out | The client IP address is not in the service IP filter. See [Restrict Connections to a Service](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/restrict-connections/index.md). |

For the startup parameter error, `extra_float_digits` and `search_path` are the only parameters
that can be added to `ignore_startup_parameters`. For any other parameter, remove it from the
client configuration, set it with `ALTER ROLE ... SET`, or connect to the `pg` port.

## The Application Behaves Differently in Transaction Mode

In `transaction` mode, nothing raises an error when the application relies on session state:

- A `SET statement_timeout` (or `work_mem`, or another setting) outside a transaction stays on
  the server connection. The application loses it as soon as its next transaction runs on another
  connection, and the next client of the original connection inherits it.
- `LISTEN` is accepted, but no notification reaches the client.
- A lock taken with `pg_advisory_lock()` stays on the server connection after the client
  disconnects, and is not released.
- A statement prepared with SQL `PREPARE` fails later with
  `prepared statement "..." does not exist` when `EXECUTE` runs on another connection.

> [!CAUTION]
> A test with a single client on an idle pool often hides these effects, because the client keeps
> getting the same server connection. Test with several concurrent clients.

Fixes:

- Use `SET LOCAL` inside the transaction, or attach settings to the role or the database:
  `ALTER ROLE app_user IN DATABASE defaultdb SET statement_timeout = '30s';`.
- Replace `pg_advisory_lock()` with `pg_advisory_xact_lock()`.
- Move `LISTEN` consumers and other session-bound work to a `session` pool or to the `pg` port.
- To clear the state of a server connection each time it returns to the pool, set
  `server_reset_query_always` to `true`, as described in
  [Manage Connection Pools](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-connection-pools/index.md#change-pgbouncer-settings).
  PgBouncer then runs `DISCARD ALL` after every transaction, so no setting leaks to another
  client, and a session-level `SET` does not last beyond its own transaction either.

If driver prepared statements fail with `prepared statement "..." already exists` or
`does not exist`, check that `max_prepared_statements` is not `0` in `SHOW CONFIG`.

## Count Connections on Both Sides

|  | Client side | Server side |
| --- | --- | --- |
| What is counted | connections from applications to PgBouncer | PostgreSQL backends |
| Limit | `max_client_conn`, 5000 | `max_connections`, from the plan |
| Where to read it | `SHOW POOLS` (`cl_*`), `SHOW CLIENTS`, `SHOW LISTS` | `pg_stat_activity`, `SHOW SERVERS`, `SHOW POOLS` (`sv_*`) |

On the server side, connected to the `pg` port:

```sql
SELECT usename, application_name, state, count(*)
  FROM pg_stat_activity
 WHERE backend_type = 'client backend'
 GROUP BY 1, 2, 3
 ORDER BY 4 DESC;
```

Pooled clients do not appear individually in `pg_stat_activity`: you see the server connections
of the pools. Count the clients with `SHOW CLIENTS` or with the `used_clients` line of
`SHOW LISTS`.

## Related Pages

- [Connection Pooling](https://community.exoscale.com/product/dbaas/service-specific/postgresql/overview/connection-pooling/index.md)
- [Manage Connection Pools](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/manage-connection-pools/index.md)
- [Configure PostgreSQL Settings](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/configure-settings/index.md)
- [Upgrade Password Encryption to scram-sha-256](https://community.exoscale.com/product/dbaas/service-specific/postgresql/how-to/upgrade-password-encryption/index.md)
- [PgBouncer usage and SHOW commands](https://www.pgbouncer.org/usage.html) ↗

