A page that used to load in 200 milliseconds now takes several seconds. “Add an index” is a tempting first move, but it is not a diagnosis. The change might be a larger result set, stale planner statistics, lock waiting, a new join, or a slow downstream call. Start with the user-visible request and establish where its time went.

Find the actual query

Trace one slow request and identify its SQL statement, parameters or parameter shape, and duration. Avoid putting personal data or full secret-bearing queries in general logs. PostgreSQL’s pg_stat_statements can show aggregate call counts and execution time for normalized statements, provided the extension is configured. A query that takes 20 milliseconds but runs 200 times per page may matter more than one 200-millisecond query.

Read the plan with real row counts

Run EXPLAIN first, then use EXPLAIN ANALYZE in a safe environment to compare estimated rows with actual rows and find the expensive node. Be careful: EXPLAIN ANALYZE executes the statement, so a write statement can change data unless wrapped in a transaction that you roll back. The PostgreSQL EXPLAIN guide details plan nodes and this caveat. A sequential scan of a tiny table is often sensible; a large mismatch between estimated and actual rows is a more useful clue than the words “Seq Scan” alone.

Check data shape and waiting

Compare the slow tenant, time range, and filter values with a fast example. Check whether the query now returns many more rows or sorts a large intermediate set. Inspect active sessions and waits before blaming the planner; a row lock can make an otherwise fast query look slow. Look at statistics freshness and table growth. If a recent bulk load changed distribution, updating statistics may be the right next experiment.

Change one thing at a time

Try a rewritten query, a narrower selection, or an index that supports the actual filter and sort order. Measure both the improvement and the costs: every new index takes storage and adds work to writes. On a live large table, check whether the chosen index operation blocks traffic and whether concurrent creation is available. Compare plans using production-like row counts; a tiny staging database can choose an entirely different strategy.

Confirm the fix from the user’s side

After release, compare request latency, query time, call count, and database load. A faster query that causes more calls or increases write latency may move the bottleneck elsewhere. Keep the original slow example as a regression case and record why the change worked. If the same endpoint slows down again, that note is more useful than an unlabeled index left behind six months earlier.

Separate execution time from waiting time

Suppose a query normally runs in 30 milliseconds but occasionally takes four seconds. A plan captured during a quiet period might look perfect. During the slow period, inspect pg_stat_activity and the relevant wait event: the query could be blocked behind a transaction that forgot to commit. If lock waiting dominates, tuning an index will not help. Find the blocker, the application operation that holds it, and whether a shorter transaction or a timeout is appropriate. Keep enough request and transaction identifiers to connect the database wait to a user action without recording private query values in general logs.

An index experiment with a stop condition

For a query filtering by account and ordering by creation time, test an index that matches the actual predicate and ordering. Compare EXPLAIN ANALYZE output on representative data before and after, including buffers and actual row counts. Check the effect on inserts and updates, because every index has a write cost. If the query still spends most of its time returning thousands of rows to the application, pagination or a narrower product requirement may help more than another index. Define the latency improvement you expect before changing production, and remove an unused experimental index if it does not deliver it.

Sources

PostgreSQL: Using EXPLAIN; PostgreSQL: pg_stat_statements; PostgreSQL: CREATE INDEX.

More on Software Engineering →