Quick revision
The short answer
Pool exhaustion while the database CPU sits at 30 percent means connections are being held, not used: sessions are waiting on locks, on slow external calls inside transactions, or sitting idle in transaction. We catch the spike in pg_stat_activity and pg_locks to find blocked queries and their blockers, look for scheduled jobs at that hour, check the application for transactions that span HTTP calls or lack timeouts, and fix the cause rather than enlarging the pool.
What to remember
In this answer, we read the symptoms first: a full pool with a quiet database means connections are held while waiting, not doing work. We captured the spike with pg_stat_activity, pg_locks and pg_blocking_pids to find the blocking session, looked for scheduled jobs at that hour, and checked the application for transactions that span HTTP calls and for missing timeouts. Finally we saw why enlarging the pool only pushes contention into the database, and fixed the cause instead.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Pool exhaustion while the database CPU sits at 30 percent means connections are being held, not used: sessions are waiting on locks, on slow external calls inside transactions, or sitting idle in transaction. We catch the spike in pg_stat_activity and pg_locks to find blocked queries and their blockers, look for scheduled jobs at that hour, check the application for transactions that span HTTP calls or lack timeouts, and fix the cause rather than enlarging the pool.
Why this question is asked
A latency spike that arrives at the same time every day is one of the friendlier production problems, because we know when to look. It is also a classic trap: the obvious move is to raise the pool size, and that usually makes things worse. Interviewers ask this to see whether we can read the clues (pending threads, timeouts, low CPU) and reason from them to a short list of causes. In this answer, we will first interpret what the symptoms mean, then capture evidence during the spike with pg_stat_activity and pg_locks, check for scheduled jobs, look at the application side, and finally see why a bigger pool is not the fix.
1Reading the Symptoms
HikariCP lends a fixed number of connections to request threads. Pending threads means requests are waiting for one of those connections, and connection timeouts mean some waited longer than connectionTimeout (30 seconds by default, often set lower) and failed with an SQLTransientConnectionException. So for ten minutes every connection is busy.
Busy with what? If the connections were running heavy queries, the database CPU would be high. At 30 percent it is not working hard. That leaves a few possibilities: the connections are waiting inside the database for something else, usually a lock held by another session; or they are checked out by the application but the application is not using them, for example because it is waiting for a slow HTTP call inside a transaction; or the database is waiting on I/O, which CPU does not show. Notice how much this narrows the search before we have run a single query.
- Pool pending and timeouts during spikeevery connection is checked out
- Is database CPU high?here it is 30 percentYesLook at heavy queries in pg_stat_statementsNoConnections are held, not working
- Sessions waiting on locks?pg_stat_activity wait_event_type = LockYesFind the blocker with pg_blocking_pidsNoCheck next
- Sessions idle in transaction?state columnYesApplication holds transactions during slow workNoCheck I/O wait and scheduled jobs
4 steps in order. Play them, or step through them one at a time.
2Capturing Evidence During the Spike
Because the spike is short, we need data from inside the window. We can run queries by hand at the right time, but it is better to schedule a small script that samples pg_stat_activity every 10 seconds from 13:50 to 14:30 and stores the result. pg_stat_activity shows every session with its state, the query it is running, when its transaction started, and what it is waiting for in wait_event_type and wait_event.
The function pg_blocking_pids(pid) returns the sessions that block a given session. Combined with pg_locks, it lets us draw the chain: dozens of request sessions waiting on a lock, and one session at the head of the chain holding it. That head session is what we want to identify, together with its application_name, client address and query.
3Looking for Something Scheduled at That Hour
Something that happens every afternoon at the same time is almost always scheduled. We list the candidates: a batch job in the application (a Spring @Scheduled method or a Quartz job), a nightly-style report that someone moved to the afternoon, a data export, an external partner's sync, a manual VACUUM FULL, REINDEX or ANALYZE, a backup or snapshot, or a migration pipeline. Cron entries, the job scheduler's history and the database log with log_lock_waits enabled usually reveal it quickly.
The patterns we look for are a job that updates many rows in one long transaction, which locks those rows until it commits so requests touching them wait; a job that takes a stronger lock, such as ALTER TABLE or VACUUM FULL, which blocks reads or writes on the whole table; or a backup or report that saturates disk I/O, so every query takes longer and connections are held longer. In the sample above, session 4410 is the batch job: it updated a slice of accounts and then sat idle in transaction while it fetched data from another system.
4Checking the Application Side
The second family of causes lives in our code. A @Transactional method checks out a connection when the transaction starts and keeps it until it ends. If the method calls a payment provider or another service in the middle, the connection is held for the whole call. When that provider slows down every afternoon, say because of its own batch window, our pool empties while our database idles.
We also check timeouts. Without a read timeout on the HTTP client, a stuck call can hold a connection for minutes. On the database side, statement_timeout, lock_timeout and idle_in_transaction_session_timeout put upper limits on how long a session can run, wait for a lock or sit idle in a transaction. Setting them per role or per application turns an endless queue into fast, visible failures.
5Why a Bigger Pool Does Not Fix It
Raising the pool from 20 to 80 connections feels like it should help, and for a few minutes it might. But if the requests are waiting on a lock, more connections only means more sessions queued inside PostgreSQL for the same lock, more memory used by server processes and more contention when the lock is released. If they are waiting on a slow external call, a bigger pool just lets more threads pile up behind that call. The pain moves from the application into the database, and other services that share the database can suffer too.
So the fix follows the cause: split the batch job into small transactions of a few hundred rows each, move it to a quieter time, or make it skip locked rows; move external calls outside transactions and add timeouts; schedule heavy maintenance in a real low-traffic window. Then we keep the sampling script and add an alert on idle in transaction age, so the next regression shows up in minutes, not after a week of afternoons.
6Follow-up questions
How would you split a long batch update?
We process rows in chunks by primary key, for example 500 at a time, each chunk in its own short transaction. Locks are then held for milliseconds instead of minutes, and requests can interleave with the job. If request traffic and the job touch the same rows, SELECT ... FOR UPDATE SKIP LOCKED lets the job skip rows that a request is currently changing.
What is the difference between lock_timeout and statement_timeout?
lock_timeout limits how long a statement waits to acquire a lock; statement_timeout limits the total time a statement may run, including any waiting. A low lock_timeout on request traffic makes requests fail fast instead of queueing behind a blocker, while statement_timeout protects against runaway queries.
Could autovacuum be the cause?
Ordinary autovacuum does not block normal reads and writes, but it can add I/O load on large tables, and an anti-wraparound vacuum can be heavy. It is worth checking pg_stat_user_tables for vacuum timestamps around the spike, but it rarely explains pool exhaustion with lock waits on its own.
7Conclusion
In this answer, we read the symptoms first: a full pool with a quiet database means connections are held while waiting, not doing work. We captured the spike with pg_stat_activity, pg_locks and pg_blocking_pids to find the blocking session, looked for scheduled jobs at that hour, and checked the application for transactions that span HTTP calls and for missing timeouts. Finally we saw why enlarging the pool only pushes contention into the database, and fixed the cause instead.
Open with
The combination is the clue: HikariCP has pending threads and timeouts, so every connection is checked out, but database CPU is only 30 percent, so those connections are not doing much work.
Then, if they want more
They are being held while waiting, either on locks inside the database, on slow external calls inside transactions, or idle in transaction.
Since it happens at the same time every day, I would schedule a script to sample pg_stat_activity every few seconds during the window, recording state, wait events, transaction age and pg_blocking_pids, so I can see blocked sessions and the session at the head of the chain.
Then I would look for scheduled work at that hour: batch jobs in the application, reports, exports, maintenance like VACUUM FULL or REINDEX, or backups saturating I/O. In parallel I would check the code for @Transactional methods that call other services, and for missing HTTP read timeouts and database timeouts like lock_timeout and idle_in_transaction_session_timeout.
The fix depends on what I find, for example chunking a long batch update or moving external calls outside the transaction. I would not just raise the pool size, because that moves the queue into the database and makes contention worse.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Every afternoon p99 latency jumps from 20 ms to 2 s for ten minutes. HikariCP shows...": https://scrivio-interview-library.netlify.app/databases/database-operations/c85128fb21/