Learning center

Pool acquire timeout: a busy pool or a struggling database?

A connection pool timeout means a request waited for a connection and none came back in time. That happens when every pooled connection is busy (slow queries, a leak, or a transaction left open) or when the pool cannot open new connections because the database refuses or does not answer. Read the pool's own counts at the moment of the timeout, then read the same sessions from the database side; the pair tells you which one ran out.

The symptom and its causes#

HikariCP, the default pool in many Java services, throws this when getConnection() waits longer than connectionTimeout (30 seconds by default):

checkout-pool - Connection is not available, request timed out after 30004ms (total=10, active=10, idle=0, waiting=37)

The message text and the four counts come from the pool's source, and the exception carries the pool's last connection failure as its cause when there was one. Other pools report the same situation in their own words: Go's database/sql blocks until a connection is returned or the context is cancelled, and counts that wait in DB.Stats(). HikariCP timeout exception · HikariCP configuration · Go database/sql

Four causes produce it:

  • Slow queries hold every connection. The pool is full of connections that are executing. The database is the bottleneck: locks, a bad plan, saturated storage.
  • The application holds connections it is not using. Code borrows a connection and does not return it (a leak), or opens a transaction and then waits on something else, such as an HTTP call to another service.
  • The pool cannot open connections. total stays below the configured maximum because each new connection fails: the database is at max_connections, authentication fails, or the network path drops the connection attempt.
  • Demand exceeds a healthy pool. Queries are fast and nothing leaks, but more requests arrive than the pool can serve. That is a sizing and backpressure question, outside this guide.

Why they look alike#

All four end in the same wait and the same exception, and the database's CPU can be flat in three of them. The difference is whether the pool is at its maximum, and what its sessions are doing inside the database.

Read the pool's counts#

The timeout message already carries total, active, idle and waiting. For a trend, HikariCP's Micrometer tracker publishes hikaricp.connections, hikaricp.connections.active, hikaricp.connections.idle, hikaricp.connections.pending, hikaricp.connections.max, hikaricp.connections.timeout, and the timers hikaricp.connections.acquire, hikaricp.connections.usage and hikaricp.connections.creation. Exporters may rename them. usage is how long the application keeps a connection before returning it, which is the number that separates a slow database from slow application code once you compare it with query time. HikariCP Micrometer metric names

In Go, DB.Stats() returns MaxOpenConnections, OpenConnections, InUse, Idle, WaitCount and WaitDuration. A growing WaitDuration with InUse equal to MaxOpenConnections is the same picture as active equal to the maximum. DBStats

If HikariCP's leakDetectionThreshold is set (it is 0, disabled, by default), the pool logs Connection leak detection triggered for with the borrowing thread's stack trace when a connection stays out longer than the threshold. leakDetectionThreshold · Leak task source

Read the same sessions from the database#

Connect with a role that can see other sessions' details (a superuser or a member of pg_read_all_stats; ordinary roles see many columns as null for other roles' sessions) and group the service's sessions by state. The application_name filter works only if the service sets it, as the holders guide describes; otherwise filter on client_addr. pg_stat_activity and visibility

SELECT state, wait_event_type, wait_event, count(*) AS sessions,
       max(now() - state_change) AS longest_in_state
FROM pg_stat_activity
WHERE application_name = 'checkout'
GROUP BY state, wait_event_type, wait_event
ORDER BY sessions DESC;

Then look at the oldest active queries and anything that blocks them. pg_blocking_pids returns the process IDs holding the locks a session waits for. pg_blocking_pids

SELECT pid, now() - query_start AS running_for, wait_event_type, wait_event,
       pg_blocking_pids(pid) AS blocked_by, left(query, 80) AS query
FROM pg_stat_activity
WHERE state = 'active' AND backend_type = 'client backend'
ORDER BY query_start
LIMIT 20;

SELECT count(*) AS connections, current_setting('max_connections') AS max_connections,
       current_setting('superuser_reserved_connections') AS superuser_reserved
FROM pg_stat_activity
WHERE backend_type = 'client backend';

The state values that matter here are active (executing a query), idle (waiting for a new command), and idle in transaction (inside a transaction, not executing). A session in idle or idle in transaction with wait event ClientRead is waiting for the client to send something. pg_stat_activity states and wait events

What each combination means#

PoolDatabase sessions from the servicePoints to
active at max, waiting highMostly active, long running or waiting on LockThe database or a blocking session
active at max, waiting highMostly idle in transaction, ClientReadApplication holds transactions open
active at max, waiting highMostly idle, few sessionsLeak in the application
total below maxFew sessions; connection count near the limitDatabase refuses new sessions
total below maxFew sessions; count well below the limitConnect path or authentication failing
active at max, waiting highactive but each query shortDemand exceeds the pool

For the two rows where total stays low, read the timeout exception's cause. A PostgreSQL refusal arrives as FATAL: sorry, too many clients already or remaining connection slots are reserved for roles with the SUPERUSER attribute. A connect timeout or refusal points at the network path. PostgreSQL refusal messages · Reserved slot messages · Refused or timed out?

A worked example with two outcomes#

This example is illustrative. The checkout service runs 4 tasks with a HikariCP pool of 10 each against the orders database, max_connections 100. At 09:14 UTC checkout logs Connection is not available, request timed out after 30002ms (total=10, active=10, idle=0, waiting=41) on every task.

First outcome, the database is slow:

CheckResult
Sessions from checkout by state40 active, wait event Lock / transactionid
blocked_by on the oldestpid 48211, application orders-backfill, idle in transaction for 11 minutes
hikaricp.connections.usagep99 30 s, matching query time

A backfill job opened a transaction, updated rows checkout reads with SELECT ... FOR UPDATE, and stopped. Every checkout connection waits behind it. The pool is a victim; the fix is the backfill session.

Second outcome, checkout holds the connections:

CheckResult
Sessions from checkout by state38 idle in transaction, wait event ClientRead, 2 active
Active query timesunder 20 ms
hikaricp.connections.usagep99 29 s, far above query time

The release at 09:05 added a call to the payments API inside the order transaction. Payments slowed, so each request held its transaction (and its connection) while waiting for payments. The database is idle. The dependency to investigate is payments, and the code change is the open transaction around a remote call.

Limits#

pg_stat_activity is a snapshot; take it during the timeouts, not after. A pooler such as PgBouncer between the service and PostgreSQL changes what the database sees: client_addr becomes the pooler, and in transaction pooling a session's idle state says little about one application connection. Pool metrics show what one process saw. Add them up across tasks before comparing with the database. Monitoring statistics

When new connections are refused because the database is at its limit, the next question is who holds the slots, and whether a rollout's overlap added them. Who holds the connections · Connection budgets during rollouts · All runtime dependency failures

Questions#

Does raising the pool size fix a pool timeout?

Only in the last row of the table, and only if the database has slots to spare. When slow queries or a blocking session hold the connections, a bigger pool adds more waiting sessions. When the database is at max_connections, a bigger pool cannot open its extra connections at all.

Why does the pool say active when the database says idle?

The pool counts a borrowed connection as active until the application returns it. The database reports what the session is doing, and a session that ran its query and is waiting for the next command shows idle or idle in transaction. The gap is time the application spent holding the connection without using it.

What does waiting mean in the HikariCP message?

It is the number of threads blocked in getConnection() at that moment, waiting for a connection to come back. A large value with active at the maximum means requests queue in the application before they ever reach the database.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading