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 examplemy-postgresql.ZONE: zone the service runs in, for examplech-gva-2.
Connect to the console with the admin user:
psql "host=HOST port=PGBOUNCER_PORT dbname=pgbouncer user=avnadmin sslmode=require"Parameters:
HOST:hostof thepgbouncercomponent.PGBOUNCER_PORT:portof thepgbouncercomponent.
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:
| 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:
Shorten the transactions. In
transactionmode 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.
Check the mode. A
sessionpool serves at mostsizeclients at a time, however short their queries are. For a web application, usetransaction.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
| 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. |
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(orwork_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. LISTENis 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
PREPAREfails later withprepared statement "..." does not existwhenEXECUTEruns 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 LOCALinside 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()withpg_advisory_xact_lock(). - Move
LISTENconsumers and other session-bound work to asessionpool or to thepgport. - To clear the state of a server connection each time it returns to the pool, set
server_reset_query_alwaystotrue, as described in Manage Connection Pools. PgBouncer then runsDISCARD ALLafter every transaction, so no setting leaks to another client, and a session-levelSETdoes 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:
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.