Skip to content

Error explained

Database connection pool exhausted

Requests wait for a database connection and time out. Either connections are held too long, leaked, or the pool is too small for the work.

3 min read9 sectionsUpdated

On this page

Key points

  • Slow queries hold connections; that is the most common cause.
  • Leaked connections never return to the pool.
  • Bigger pools are not always better: the database has a limit too.
  • Pending-connection count is the metric to alert on.

How it looks in different stacks

StackMessage
Java (HikariCP)Connection is not available, request timed out after 30000ms
Node (knex)Knex: Timeout acquiring a connection. The pool is probably full.
Node (pg)timeout exceeded when trying to connect
Python (SQLAlchemy)QueuePool limit of size 5 overflow 10 reached, connection timed out
RailsActiveRecord::ConnectionTimeoutError: could not obtain a connection from the pool

Causes

  1. Slow queries or long transactions keep connections busy; find them in traces and pg_stat_activity.
  2. Leaks: a code path that checks out a connection and never releases it on error.
  3. External calls inside a transaction: holding a connection while waiting on an HTTP API.
  4. Pool smaller than concurrency: e.g. Puma with 5 threads and a pool of 2.
  5. Many instances × pool size above the database's max_connections, so new connections are refused.

Sizing the pool

Start from the database's limit: instances × pool size must stay below max_connections with headroom for admin and migrations. With many instances, put a pooler such as PgBouncer in front. Inside each instance, the pool should match real concurrency: enough for the threads or concurrent requests that need the database at the same time, not more.

Preventing leaks

python
# always release, even on error
with engine.connect() as conn:
    rows = conn.execute(text("SELECT ...")).all()
javascript
const client = await pool.connect();
try {
  await client.query('BEGIN');
  // ...
  await client.query('COMMIT');
} catch (e) {
  await client.query('ROLLBACK');
  throw e;
} finally {
  client.release();
}

Catching it in production

Track active, idle and pending connections per instance and alert when pending stays above zero. In traces, a long gap before the first database span is time spent waiting for a connection.

VitalRix error tracking groups exceptions recorded on OpenTelemetry spans into issues with counts, first and last seen, affected endpoints and versions, and marks an issue as regressed if it comes back after being resolved. Alert rules on error rate per service or endpoint route the problem to the right team before customers report it.

Frequently asked questions

Should I just increase the pool size?

Only if the database has room and the pool is genuinely smaller than concurrency. If queries are slow, a bigger pool just moves the queue into the database.

How do I find a leak?

Enable the pool’s leak detection (for example HikariCP leakDetectionThreshold) to log the stack of connections held too long.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.