Slow APIs and database bottlenecks

On real Postgres: a 27 ms query that became a 218 ms p50 under concurrency, a lock wait named by pg_blocking_pids, and a drained pool that failed every fast query.

13 min read🚨 Production Engineering

The most common production complaint about a backend is not that it is down. It is that it is slow — p99 latency up, a timeout here and there, a page that takes four seconds on a bad day. And in most Java services, when an API is slow and the service's own CPU is not busy, the time is being spent waiting: for a query, for a lock, or for a connection to run the query on.

Those three look alike from the outside and have completely different fixes. This lesson shows each one on a real PostgreSQL 16 instance, with a table of two million orders, and the evidence that tells them apart.

Latency up, CPU flat

The first thing to establish is where the time goes. If the service's CPU is high, the time is spent in your code, and the diagnosing lesson's thread-dump method applies. If the service's CPU is low and latency is high, the service is waiting on something — and for most services, the database is the first thing to check.

A distributed trace answers "where" directly: a request span of 800 ms with a 780 ms database span inside it is not ambiguous. Without traces, compare the API's latency graph with the database's query latency and the connection pool's metrics — hikaricp.connections.active, hikaricp.connections.pending, and hikaricp.connections.acquire in a Spring Boot service.

Suspect one: the slow query

An endpoint shows a customer's recent orders:

sql
SELECT id, status, total_cents FROM orders
WHERE customer_id = $1 ORDER BY created_at DESC LIMIT 20

On its own, it seems fine. Thirty calls from one client:

plaintext
30 calls of the "recent orders" endpoint query, no index: p50 27 ms, max 278 ms

27 ms is nobody's idea of a crisis. Now the same query from 16 concurrent callers, which is a quiet afternoon for a real service:

plaintext
no index: 16 concurrent callers × 10 queries: p50 217.8 ms, p99 558.6 ms, 62 queries/s

The median went from 27 ms to 218 ms, with no change to the query. Every call scans the table, and sixteen scans compete for the same CPU and the same memory. A query that is merely slow alone becomes the bottleneck under concurrency — which is why it passed every test and failed in production.

Finding it. pg_stat_statements records every normalised query with its call count and total time. Sorted by total time, not mean, because the query that costs the most overall is the one worth fixing first:

plaintext
│ calls │ total_ms │ mean_ms │ query                                                                    │
│ '30'  │ 1146     │ '38.2'  │ 'SELECT id, status, total_cents FROM orders WHERE customer_id = $1 ORDE' │
│ '1'   │ 0        │ '0.1'   │ 'SELECT $1 FROM orders WHERE id = $2'                                    │

Understanding it. EXPLAIN (ANALYZE, BUFFERS) runs the query and shows what actually happened:

plaintext
Limit (actual time=24.228..26.945 rows=12 loops=1)
  ->  Gather Merge (actual time=24.227..26.943 rows=12 loops=1)
        Workers Planned: 2
        ->  Sort (actual time=22.860..22.860 rows=4 loops=3)
              Sort Key: created_at DESC
              ->  Parallel Seq Scan on orders (actual time=4.561..22.790 rows=4 loops=3)
                    Filter: (customer_id = 1041)
                    Rows Removed by Filter: 666663
                    Buffers: shared hit=16227 read=815 written=70
Execution Time: 26.973 ms

Read it from the bottom up. A sequential scan, on three processes, read about 17,000 pages and threw away two million rows (666663 removed per loop, three loops) to find twelve. That ratio — rows examined against rows returned — is the signature of a missing index.

Fixing it safely. An index on the filter column, with the sort column after it:

plaintext
CREATE INDEX CONCURRENTLY orders_customer_recent ON orders (customer_id, created_at DESC)
--- took 1.8 s

CONCURRENTLY matters in production: a plain CREATE INDEX blocks writes to the table for as long as it runs, which on a large table is a self-inflicted outage. The concurrent form is slower and cannot run inside a transaction, and it does not block writes. After:

plaintext
->  Bitmap Index Scan on orders_customer_recent (actual time=0.031..0.031 rows=12 loops=1)
      Index Cond: (customer_id = 1041)
      Buffers: shared hit=3
Execution Time: 0.301 ms
plaintext
with the index: 16 concurrent callers × 10 queries: p50 1.6 ms, p99 61.8 ms, 1988 queries/s

From 62 queries a second to about 2,000, and a p50 from 218 ms to 1.6 ms. The p99 of 62 ms in that run most likely comes from each caller's first query, which also had to open a new connection; the lab did not separate the two, so treat that tail with caution.

Suspect two: the lock wait

The second suspect produces no slow query at all. A refund job opens a transaction, locks an order row with SELECT … FOR UPDATE, and then does something slow before committing. Meanwhile the API tries to update the same order.

While the API request hangs, pg_stat_activity shows exactly what is going on:

plaintext
│ pid │ app          │ state                 │ wait_type │ wait_event      │ blocked_by │ xact_age_s │ query                                              │
│ 362 │ 'refund-job' │ 'idle in transaction' │ 'Client'  │ 'ClientRead'    │ []         │ '0.5'      │ 'SELECT * FROM orders WHERE id = 1041 FOR UPDATE'  │
│ 363 │ 'orders-api' │ 'active'              │ 'Lock'    │ 'transactionid' │ [ 362 ]    │ '0.5'      │ "UPDATE orders SET status = 'PAID' WHERE id = 104" │
plaintext
orders-api UPDATE finished after 3.03 s

Three columns carry the diagnosis:

  • The API's session is active, but its wait type is Lock — it is not executing, it is queued.
  • pg_blocking_pids names the session blocking it: 362.
  • Session 362 is idle in transaction: it holds a lock and is doing nothing in the database. The slow work is happening in the application, with a transaction open.

The UPDATE itself takes microseconds. It waited three seconds for someone else's transaction. pg_stat_statements would record it as slow, and no index would help.

Set idle_in_transaction_session_timeout so a forgotten transaction is ended by the database, and alert on the age of the oldest open transaction.

Suspect three: the empty pool

The third suspect happens before the query reaches the database. A service's connection pool has a fixed size. If every connection is busy, the next request waits for a connection — and if none is freed in time, it fails.

A pool of five connections. Five slow queries — pg_sleep(3), standing in for a report or a lock wait — take all of them. Then ten ordinary one-millisecond queries arrive:

plaintext
pool of 5: total 5, idle 0, waiting 0
10 one-millisecond queries: 0 succeeded, 10 failed after 1.00 s: timeout exceeded when trying to connect

Every fast query failed, and none of them reached the database. From the database's side, nothing is wrong: five sessions are running, and the ten failed requests never appear in pg_stat_activity or pg_stat_statements. The evidence lives only in the application — pool metrics, and the error message about acquiring a connection. (This lab used Node's pg pool, whose timeout message is quoted above; HikariCP's equivalent is Connection is not available, request timed out after 1000ms.)

Pool exhaustion is usually a symptom of one of the other two suspects: slow queries or lock waits hold connections longer, and the pool runs dry. The fix is to find what is holding connections, not to enlarge the pool — a bigger pool lets more queries compete for the same database CPU and makes the first suspect worse. The connection pooling lesson in the relational databases course covers sizing.

Telling them apart

evidenceslow querylock waitpool exhaustion
service CPUlowlowlow
database CPUhighlowdepends on the cause
pg_stat_statements total timethe query is at the topthe blocked statement looks slowthe failing requests are absent
pg_stat_activitymany active sessions running itwait_event_type = Lock, a blocker idle in transactionall pool connections busy
pool pending / acquire timerises under loadrises during the waithigh, with acquire timeouts
EXPLAINseq scan, many rows removednormal plannot relevant

Fixing safely under pressure

  • Kill the blocker, not the victims. pg_terminate_backend(362) ends the transaction holding the lock; cancelling the waiting queries only adds retries.
  • Indexes: CONCURRENTLY, always, on any table that takes writes, and check afterwards that the index is valid — a failed concurrent build leaves an invalid index behind.
  • Timeouts at every layer: a statement timeout on the database role used by the API, a connection-acquire timeout in the pool, a request timeout at the edge. A request that waits forever holds everything it touched.
  • Verify with the same evidence you diagnosed with: the query's total time in pg_stat_statements falls, lock waits disappear from pg_stat_activity, pending connections return to zero.

Under the hood: how the planner decides seq scan vs. index scan, and the arithmetic behind pool sizing

EXPLAIN's output in this lesson looks like a report of what happened, but the plan it describes was chosen before execution, by PostgreSQL's cost-based query planner — and understanding roughly how it decides is what makes an unexpected plan diagnosable instead of mysterious. The planner does not know your data; it estimates. It maintains statistics on each column — most importantly n_distinct (roughly how many distinct values exist) and a histogram of value frequencies — refreshed by ANALYZE (which autovacuum runs periodically on its own). For a query filtering on customer_id = $1, the planner uses those statistics to estimate the selectivity: what fraction of the table's rows it expects to match. A high estimated selectivity (many matching rows) favours a sequential scan, because reading the whole table sequentially is cheap per row and a full scan avoids the overhead of index lookups; a low selectivity (few matching rows, like one customer's orders among two million) favours an index scan, because the number of pages the index touches is small compared to scanning everything. The planner combines this selectivity estimate with configured cost constants (seq_page_cost, random_page_cost, cpu_tuple_cost) to produce a total estimated cost for each candidate plan, and picks the cheapest one.

This is exactly why the "before" plan in this lesson chose a sequential scan even though it turned out to be dramatically wrong for the twelve rows actually needed: without an index on customer_id, a sequential scan is the only plan available at all — there is nothing to estimate a choice between. Adding the index does not just make lookups faster; it makes an entirely different, previously nonexistent plan possible for the planner to consider and cost. This is also why stale statistics cause the same symptom without any missing index: if ANALYZE has not run since a large data change, the planner's selectivity estimate can be badly wrong, and it will confidently choose a sequential scan (or a bad join order) based on numbers that no longer describe the table — which is what ANALYZE (or a manual run after a bulk load) fixes, distinct from adding an index.

The connection pool's sizing has its own, less intuitive arithmetic. The intuitive assumption — "more connections handle more concurrent load" — is only true up to the number of CPU cores actually available to the database, and past that point more connections make things worse, not better: every additional connection running a query adds context-switching and lock-contention overhead on the same fixed CPU, so throughput can degrade even as the pool grows. HikariCP's own sizing guidance (echoing research summarized in the well-known "PostgreSQL connections and pool sizing" formula) suggests a pool size in the neighbourhood of ((core_count × 2) + effective_spindle_count) — often landing on numbers far smaller than instinct suggests, commonly under 20 connections even for a database host with real capacity. This directly explains why the pool-exhaustion suspect in this lesson is described as "usually a symptom": enlarging the pool without first finding what is holding connections open (the slow query, the lock wait) just lets more waiting requests reach the database at once, competing for the same fixed CPU that was already the bottleneck — the empty-pool suspect and the slow-query suspect are not independent, a bigger pool without fixing the first two suspects can turn a queueing problem into a throughput problem.

Try it yourself

Decide what a stale-statistics symptom looks like versus a missing index

A query filtering on status = 'CANCELLED' was fast for months, then suddenly started running as a sequential scan (confirmed by EXPLAIN) after a bulk import added five million new rows, nearly all with status = 'ACTIVE'. The index on status still exists and \d orders confirms it.

Without dropping and recreating anything, what is the most likely cause, and what single command would you try before assuming the index needs to be rebuilt?

Reasoning to the statistics, not the index

The index existing and the plan still choosing a sequential scan, immediately after a bulk import, points at stale statistics rather than a broken index: ANALYZE typically runs via autovacuum on a schedule tied to how much of the table has changed, and a sudden five-million-row import can outrun that schedule, leaving the planner's n_distinct and histogram badly out of date for a few minutes to hours until autovacuum catches up. With stale statistics, the planner may still believe 'CANCELLED' matches a much larger fraction of the table than it actually does post-import (or, in the other direction, may not yet know the table has grown enough that a scan-versus-index tradeoff has flipped), and choose the wrong plan based on outdated numbers rather than any defect in the index itself. Running ANALYZE orders manually (cheap compared to rebuilding an index, and non-blocking) refreshes the statistics immediately rather than waiting for autovacuum's next scheduled pass, and re-running EXPLAIN afterward is the fast way to confirm whether stale statistics, not a broken index, was the cause.

Misconceptions

  • "An index always makes a query faster, so add one to every column that appears in a WHERE clause." An index that the planner estimates would match a large fraction of rows is often not chosen even when it exists, because a sequential scan can be genuinely cheaper for low selectivity — and every index adds write overhead (every INSERT/UPDATE/DELETE on the table must also maintain the index) and storage cost. Indexes are a trade, not a free win; the ones worth adding are the ones EXPLAIN and pg_stat_statements show are actually needed.
  • "A bigger connection pool is always a safe fix for 'connection timeout' errors." As the under-the-hood section shows, past the database's real CPU capacity, more connections compete for the same fixed resource and can reduce total throughput rather than increase it — a bigger pool fixes true under-provisioning but makes a symptom-of-something-else problem (a slow query, a lock wait) worse by letting more work pile onto an already-saturated database.

Sources

  • PostgreSQL documentation, Chapter 14, "Performance Tips" (planner statistics, EXPLAIN, ANALYZE) and Chapter 73, "How the Planner Uses Statistics".
  • PostgreSQL documentation, pg_stat_statements and pg_stat_activity reference.
  • HikariCP wiki, "About Pool Sizing".
Progress is saved on this device and to your account when signed in.