Pool exhaustion is not database latency.
A short explanation of why your dashboards lie about which layer is slow, with the four checks that distinguish them.
Here is a confusing pattern. An API is slow under load, but the database dashboard is clean: CPU low, disk idle, query times flat. Every graph says the database is fine, yet every request that touches it takes seconds. The natural response is to start tuning queries, and the queries are already fast.
In that situation the database may well be fine. The connection pool may not be.
The shape of the lie
When a request waits for a connection from an exhausted pool, that wait is not database time. The query itself, once it runs, is as fast as ever, so a query-time metric stays flat. The delay is spent in your own process (or in a pooler in front of the database), in a queue, before a single byte of the query reaches Postgres. The dashboard is measuring the wrong interval.
The slowest part of a request is often the part no one instrumented.
This is why connection-pool problems can persist for so long. The one place you would look says everything is healthy, because by its own definition it is.
Four checks that tell them apart
The four measurements that distinguish pool exhaustion from real database latency:
- Connection acquire time. Instrument the wait to check out a connection separately from query execution. If this rises under load, you have found it.
- Pool saturation. Track in-use connections against pool size. Sustained near the ceiling is the precondition for every wait above.
- Query time at the database. From
pg_stat_statements(mean_exec_timeandtotal_exec_time), not from the application. It records execution inside Postgres only, so if it is flat while the app is slow, the time is being spent before the query arrives. - Hold time per checkout. A connection held across a slow external call starves everyone else, even at low query volume.
The cheapest of these to add is acquire time, measured around the checkout itself:
async function withConnection<T>(fn: (c: Conn) => Promise<T>): Promise<T> {
const start = performance.now();
const conn = await pool.acquire();
+ metrics.observe("pool.acquire_ms", performance.now() - start);
try {
return await fn(conn);
} finally {
pool.release(conn);
}
}The finally matters as much as the metric. A connection that is not released when fn throws is a leak, and a slow leak produces exactly the same symptom: a pool that runs dry while the database sits idle.
Once pool.acquire_ms is on a graph next to query time, the diagnosis is usually quick. The fix is usually unremarkable: a larger pool, a shorter hold, or moving a slow external call out from under a held connection.1
Footnotes
-
A common version of the last case is a request that checks out a connection, opens a transaction, then calls a third-party API before committing. If that call takes a second or two, every in-flight request holds a connection for that long. Moving the external call outside the checkout can return the pool to health without changing its size at all. ↑