Skip to content

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:

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:

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:

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.

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

SettingValueCan be changed
max_client_conn5000No
default_pool_size10No, set size per pool
query_wait_timeout120 sNo
client_login_timeout60 sNo
server_login_retry15 sYes, server_login_retry
max_prepared_statements100Yes
server_reset_query_alwaysoffYes

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:

ColumnMeaningWhen the pool is exhausted
cl_activeclients linked to a server connectionequals the pool size
cl_waitingclients waiting for a server connectiongreater than 0
sv_activeserver connections in useequals the pool size
sv_idlefree server connections0
maxwaitseconds the oldest waiting client has waitedgrows

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:

    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. If the budget is too small, change the plan: see Scale DBaaS Services.

Clients Get a Cached Login Error

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, and lower the pool sizes or the number of direct connections.

A Client Is Rejected

ErrorCause
FATAL: no such database: NAMEThe 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 rejectedThe 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 modeThe client sends BEGIN to a statement pool.
FATAL: database removedThe pool was deleted while the client was connected.
The connection hangs, then times outThe client IP address is not in the service IP filter. See Restrict Connections to a Service.

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. 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 sideServer side
What is countedconnections from applications to PgBouncerPostgreSQL backends
Limitmax_client_conn, 5000max_connections, from the plan
Where to read itSHOW POOLS (cl_*), SHOW CLIENTS, SHOW LISTSpg_stat_activity, SHOW SERVERS, SHOW POOLS (sv_*)

On the server side, connected to the pg port:

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

Last updated on