used_clients keeps climbing. Traffic is flat, cl_waiting is zero, queries are fast, and yet the client count creeps toward max_client_conn day after day. When it gets there, PgBouncer starts rejecting new connections with no more connections allowed (max_client_conn), even though the database itself is completely healthy.
This is a client-side connection leak: application instances open connections to PgBouncer and never close them. PgBouncer is doing exactly what it is told to do, holding those sockets open. The pooler just makes the leak visible earlier and more painfully, because it has a hard front-door limit.
PgBouncer exposes enough state to find the leaking clients precisely, and recent versions (1.24+) give you tools to reap them without a restart. This guide covers detection, distinguishing a real leak from expected long-lived connections, and remediation.
What this means
Every client connected to PgBouncer consumes a file descriptor, a small amount of memory, and one slot out of max_client_conn. A client that connects and never disconnects holds all of that indefinitely, whether it ever sends a query or not.
The cost depends on what the leaked client is holding:
- Idle client, no transaction open (cheap). In transaction pooling mode, an idle client holds no server connection. It costs an FD, some memory, and a client slot. Thousands of these slowly march you toward
max_client_conn, but they do not starve the server pool. - Client that ran BEGIN and wandered off (expensive). In transaction mode, a client inside an open transaction holds a server connection in
sv_activestate for as long as the transaction stays open. Enough of these and the pool exhausts:cl_waitinggrows,maxwaitclimbs, and you have the full pool exhaustion cascade on top of the leak.idle_transaction_timeoutexists for exactly this case.
The failure ends in one of two places: the front door closes (used_clients hits max_client_conn and new connections are refused with no queuing and no warning), or the server pool starves (leaked in-transaction clients pin every server connection). This article is about the leak underneath both.
flowchart TD
A[used_clients drifting up] --> B{SHOW CLIENTS: old connect_time, no recent request_time?}
B -- no --> C[Not a leak: legitimate traffic or capacity issue]
B -- yes --> D{Client holds a server link?}
D -- yes --> E[Expensive: idle-in-transaction, pins sv_active]
D -- no --> F[Cheap: idle client, holds FD + client slot only]
E --> G[idle_transaction_timeout + app fix]
F --> H[client_idle_timeout + app fix]Common causes
| Cause | What it looks like | First thing to check |
|---|---|---|
| Application opens connections and never closes them | used_clients grows monotonically, uncorrelated with query rate; many clients with very old connect_time and stale request_time | SHOW CLIENTS sorted by connect_time, grouped by source addr |
| Deployments roll without draining old instances | Connection count steps up at each deploy and never steps back down; connections from IPs of retired hosts or pods | Compare addr values in SHOW CLIENTS against your current instance inventory |
| App-side pool with no max lifetime or idle timeout | A fixed cohort of very old connections per app instance, oldest matching instance start time; stable, not growing | App pool config (HikariCP maxLifetime, pgx, psycopg pool settings). This is expected behavior, not a leak, until instance count grows |
| Clients that BEGIN and never COMMIT | Subset of old clients linked to server connections; sv_active elevated with avg_xact_time >> avg_query_time | SHOW SERVERS for active connections with old request_time; PostgreSQL pg_stat_activity for idle in transaction |
| Load balancer or health checks holding connections | Steady background count of connections from LB addresses | SHOW CLIENTS filtered to LB source addresses |
| Retry loops during past incidents left orphans | One step-up in client count that never recovered after an earlier outage | Correlate the connect_time cluster with the earlier incident window |
The hardest judgment call is the third row: application connection pools are supposed to hold long-lived connections. An old connect_time alone is not a leak. The leak signature is growth uncorrelated with traffic, plus connections whose request_time never advances, plus connections from instances that no longer exist.
Quick checks
All read-only, run against the admin console. Adjust host and port for your deployment.
# Client capacity: used vs free slots
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW LISTS;" | grep -E "used_clients|free_clients"
# The configured limit
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW CONFIG;" | grep max_client_conn
# Full client table for offline analysis (state, addr, database, user, connect_time, request_time, ...)
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW CLIENTS;" > /tmp/clients.txt
# Connections per source address: which host or subnet is hoarding
awk -F'|' '{print $4}' /tmp/clients.txt | sort | uniq -c | sort -rn | head -20
# Are any old clients holding server connections? (expensive leak)
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW SERVERS;" > /tmp/servers.txt
# Transaction hold time vs query time: a large gap means idle-in-transaction
psql -h 127.0.0.1 -p 6432 -U pgbouncer pgbouncer -Atc "SHOW STATS_AVERAGES;"
# FD pressure on the PgBouncer process
ls /proc/$(pgrep -f pgbouncer)/fd | wc -l
grep "Max open files" /proc/$(pgrep -f pgbouncer)/limits
Column positions in the -At output are version-dependent; the SHOW CLIENTS field order changed when columns were added in 1.23, so verify which field is addr on your version before trusting the awk position. Safer: run SHOW CLIENTS; once in aligned mode to see the header, then fix the field index.
How to diagnose it
Confirm the growth pattern. Sample
used_clientsfromSHOW LISTSa few times over an hour. A leak grows monotonically or in steps (at deploy time) and never comes down. Legitimate load tracks traffic and falls during quiet periods. Also checkquery_countinSHOW STATS_AVERAGES: flat or falling query rate with rising client count is the smoking gun.Identify the aged clients. From the captured
SHOW CLIENTSoutput, find rows whereconnect_timeis hours or days old andrequest_timeis equally stale (no recent activity). On PgBouncer 1.25.0+,SHOW CLIENTShas an explicitidlestate, which makes this trivial. On earlier versions there is noidlestate; idle clients appear asactiveorusedand you must infer idleness by comparingconnect_timeagainstrequest_time.Split cheap leaks from expensive leaks. For each suspect client, check the
linkcolumn. A non-empty link means the client is paired with a server connection. Cross-referenceSHOW SERVERS: anactiveserver connection with an oldrequest_timewhose state in PostgreSQL’spg_stat_activityisidle in transactionis the expensive variant. These clients are eating pool capacity right now, not just client slots.Attribute by source. Group the leaked connections by
addrand bydatabase/user. One source IP with thousands of connections points at a single misbehaving instance or a leaked pool in one service. Connections from IPs that no longer map to any live instance confirm orphaned connections from retired deployments.Rule out look-alikes. Check
SHOW DATABASESforpausedordisabledflags before treating anything as an incident. Confirm that the count is not simply (app pool size x instance count) doing exactly what the app was configured to do; if instances scale out and each holds 20 connections,used_clientsclimbs legitimately and the fix is capacity, not leak hunting.Quantify headroom. Compare
used_clientsagainstmax_client_conn, and the process FD count against the ulimit. Above 80% of either, you are one deploy or one traffic spike away from refusals. Admin connections to thepgbouncerdatabase are exempt frommax_client_conn, so you will not lock yourself out of the console.
Metrics and signals to monitor
| Signal | Why it matters | Warning sign |
|---|---|---|
used_clients / max_client_conn | The front-door utilization; at 100% new connections are refused instantly | >80% sustained, or steady upward trend uncorrelated with traffic |
free_clients | Pre-allocated client slots remaining; zero means refusals are happening now | Below 10% of max_client_conn |
Client connection age distribution (SHOW CLIENTS connect_time, request_time) | Direct leak detection: old connect_time with stale request_time | Growing cohort of connections older than the oldest running app instance |
avg_xact_time vs avg_query_time | The gap measures idle-in-transaction time, the expensive leak | Ratio consistently above ~10x |
sv_active with old request_time (SHOW SERVERS) | Finds leaked transactions pinning server connections | Active server connections idle for minutes or hours |
| PgBouncer process FD count vs ulimit | FD exhaustion can refuse connections before max_client_conn is reached | >80% of Max open files |
query_count trend | Baseline to prove client growth is not traffic growth | Client count up, query rate flat or down |
Note the instrumentation gap: PgBouncer has no counter for refused connections. no more connections allowed appears only in the log. If you are not parsing the log, the first signal of a full front door is application-side errors.
Fixes
Reap idle clients: client_idle_timeout
PgBouncer 1.24.0 added client_idle_timeout: client connections idle longer than N seconds are closed. Default is 0 (disabled). This is the direct fix for the cheap leak variant. Two cautions:
- The official docs flag this as a potentially dangerous timeout. Set it higher than the client-side pool’s own idle timeout and connection lifetime, otherwise PgBouncer will close connections the application still considers valid, and the app will see unexpected errors on its next query.
- It does nothing for the expensive variant. A client inside an open transaction is not idle in this sense.
Kill specific leaked clients: KILL_CLIENT
Also added in 1.24.0, KILL_CLIENT terminates a single client connection from the admin console. This is the surgical option for a handful of obvious orphans (for example, connections from an instance that was decommissioned last week) without touching healthy traffic. Prefer it over KILL, which drops every connection for a database.
Reap idle transactions: idle_transaction_timeout
For the expensive variant, idle_transaction_timeout (default 0, disabled) closes clients that sit inside an open transaction without doing work. This frees the pinned server connection back to the pool. Same caution applies: applications with legitimately long interactive transactions will get disconnected.
Cap per-user and per-database client counts
1.24.0 added max_user_client_connections and max_db_client_connections. These contain the blast radius: one leaking service exhausts its own allowance instead of the global max_client_conn and taking the whole front door down. Sizing these requires knowing your per-service connection budgets; it is a capacity planning exercise, not just a leak fix. See PgBouncer capacity planning.
Fix the application (the actual root cause)
Every timeout above is a leash, not a cure. The durable fixes are in the application: ensure connections are returned or closed on every code path, configure the app pool’s maximum lifetime and idle timeout to be shorter than PgBouncer’s reaping timeouts, and make sure deployments drain or kill old instances cleanly. If the app pool deliberately holds long-lived connections, that is fine, but then max_client_conn must be sized for (pool size x instance count) with headroom, and tuned against real FD limits as covered in PgBouncer max_client_conn tuning.
Restart as last resort, with a version caveat
A restart clears everything, but it is the blunt option: it drops all healthy connections too and triggers a reconnection storm against PostgreSQL. If you do restart, note that since 1.23.0, SIGTERM performs a “super safe shutdown” that waits for all clients to disconnect. With leaked clients that will never disconnect voluntarily, a SIGTERM shutdown can hang indefinitely. SIGQUIT gives the old immediate-shutdown behavior. Plan for that before you are staring at a hung shutdown during an incident.
Prevention
- Monitor the trend, not the threshold. Alert on
used_clients / max_client_connsustained above 80%, and separately on week-over-week growth of the ratio. Leaks are slow; a trend alert fires days before the wall. - Baseline client age. Periodically snapshot
SHOW CLIENTSand track the age distribution. Alert when connections are older than the oldest live application instance, since nothing legitimate outlives its own process. - Set app-side lifetimes shorter than PgBouncer-side timeouts. The application should always recycle its own connections before the pooler kills them. That ordering keeps errors out of the app.
- Contain with per-user or per-database caps so one leaking service cannot consume the global limit (1.24+).
- Audit at deploy time. The step-up pattern (count rises at each deploy, never falls) means old instances are not draining. Fix graceful shutdown in the deploy pipeline.
- Parse the log.
no more connections allowed,client_idle_timeoutevents, andquery_wait_timeoutevents exist only in the log. Refusals and reaping events are your confirmation that a leak reached production impact.
How Netdata helps
Netdata’s PgBouncer collector queries the admin console continuously, so the signals this guide uses are already time-series rather than manual snapshots:
- Client slot utilization over time.
used_clientsandfree_clientsas a trend make the slow drift visible weeks beforemax_client_connis hit, which is when a leak is still cheap to fix. - Wait time vs query time correlation.
avg_wait_timeagainstavg_query_timetells you whether latency comes from the pool or the database;avg_xact_timeagainstavg_query_timeexposes the idle-in-transaction variant of the leak. - Pool state correlation. If leaked transactions start pinning server connections,
sv_activeclimbing towardpool_sizealongside growing client counts shows the leak escalating from cheap to expensive. - Anomaly detection on client counts. A step-change in client connections with flat query throughput is exactly the pattern ML-based anomaly flags catch, without you hand-tuning a growth threshold.
- Per-pool breakdown. Leaks are usually one service. Per-database and per-user views isolate the offender instead of averaging it away.
Related guides
- PgBouncer no more connections allowed (max_client_conn): the front door is full
- PgBouncer max_client_conn tuning: setting the client limit against real FD headroom
- PgBouncer pool exhaustion: clients queue, wait times climb, and the retry cascade
- PgBouncer pool utilization high: sv_active approaching pool_size before clients queue
- PgBouncer maxwait high: the oldest client waiter and how close it is to timing out
- PgBouncer avg_wait_time high: the latency the pool itself is injecting
- PgBouncer capacity planning: runway for pools, clients, and PostgreSQL slots
- PgBouncer monitoring checklist: the signals every connection pooler needs
- How PgBouncer actually works in production: a mental model for operators






