Context
PgBouncer can accept many client connections while keeping fewer PostgreSQL backend connections. When clients wait, the delay may come from a deliberately bounded server pool or from transactions holding that pool for too long. Increasing the pool can remove a queue while making the database less responsive. Inspect both sides of the pooler before changing capacity.
Prerequisites
Record PgBouncer and PostgreSQL versions, pool mode, database and user mappings, application replica counts, and the PostgreSQL connection limit. Obtain authorized access to PgBouncer's administrative views and PostgreSQL activity statistics. Identify workloads that depend on session state, such as listeners or session-level advisory locks. The documented feature map for the installed pooler version is part of the compatibility review.
Diagnose the queue
- Inspect PgBouncer pools and statistics using its administrative interface. Record active clients, waiting clients, server connections, and wait behavior during a representative incident interval. Group by database and user. A single aggregate client count can conceal one saturated pool while other pools have idle server connections.
- Compare the pooler snapshot with
pg_stat_activityon PostgreSQL. Look for long transactions, idle-in-transaction sessions, and backend waits. In transaction pooling, a backend is returned when the transaction ends; application work performed inside a transaction can therefore hold scarce capacity while the database itself appears lightly loaded. - Calculate the total backend budget across pool keys, pooler instances, reserve settings, direct clients, and rollout overlap. A per-pool limit is not a global guarantee. Leave capacity for maintenance and operational access. Raising PostgreSQL
max_connectionscan increase allocated resources and is not equivalent to adding useful query-processing capacity. - Test a bounded correction against the demonstrated cause. Shorten unnecessary transaction scope, fix abandoned transactions, limit excessive application concurrency, or adjust an undersized pool only after checking server headroom. If changing pool mode, validate the application's session-dependent features explicitly. Protocol-level prepared statements have version and configuration requirements; SQL-level session assumptions need their own review.
Diagnostic table
| Observation | Likely constraint | Next evidence |
|---|---|---|
| Waiting clients, long open transactions | Backends held too long | Application transaction timeline |
| Several poolers approach database limit | Aggregate budget too large | Per-instance pool inventory |
| Mode change breaks listeners | Session semantics differ | Feature compatibility map |
| Larger pool raises database latency | Concurrency exceeds capacity | Backend waits and workload profile |
Illustrative scenario
An API opens a transaction, reads an order, then calls an external shipping service before committing. The network call holds a PostgreSQL backend while other requests queue at PgBouncer. The team moves the external call outside the transaction using an application design that preserves the business workflow. A staging load test then checks both queue duration and transaction correctness. The example demonstrates why low database CPU does not rule out pool exhaustion.
Verify the correction
Repeat representative traffic, scheduled jobs, and deployment overlap. Confirm bounded acquisition waits, acceptable backend latency, and no new connection rejection. Test commit, rollback, error, and cancellation paths so transactions release their backend consistently. For a pool-mode change, include reconnect and session-state tests rather than verifying only a simple query.
Operating procedure
Maintain a shared inventory of poolers, pool keys, limits, and direct database clients. Monitor queue time together with transaction age and server resource pressure. Review the budget after tenant, user-mapping, or replica-count changes. Document which workloads require session pooling so a future tuning change does not silently invalidate their connection contract.
Explore Clouvenix server performance for implementation and operational handover.