A slow request does not establish that its query needs another index. The delay may come from a poor row estimate, repeated inner work, lock contention, or waiting for a connection before execution begins. Capture the request boundary and read the plan before choosing a database change.
The examples use PostgreSQL 18 semantics and synthetic plan values. They are interpretation exercises, not measured plans. Apply the procedure in an isolated environment with representative data, and use documentation for the deployed major version when plan fields differ.
Capture the query shape and its real workload
Start with the query text, parameter types, relevant schema, existing indexes, database version, and the application's elapsed time. Keep confidential values out of shared evidence. A normalized statement and synthetic parameter classes can often explain the problem without publishing production records.
Include representative parameter distributions. A query for a small account and the same query for a large account may need different amounts of work. Test selective and broad cases, empty results, tied ordering values, and limits that reflect supported usage.
Separate time spent obtaining a connection from time spent executing the statement and transferring results. A plan cannot explain an application queue that happened before the database received the query. Use connection pool evidence when that boundary is uncertain.
Record concurrency, cache conditions, and dataset size with the observation. An isolated query against a tiny fixture can validate correctness while remaining a weak performance model. Choose a dataset that preserves the relationships and skew relevant to the decision.
Choose whether the inspection may execute the statement
Ordinary EXPLAIN reports the planned execution strategy. Adding ANALYZE executes the statement and reports observed work, as the EXPLAIN reference makes explicit. That distinction matters for mutations and for reads that invoke functions with side effects.
Choose the inspection mode before opening the session. Use a known isolated target for execution measurements, with explicit access scope and bounded resource use. Review functions, triggers, extensions, and outbound integrations that the statement could reach.
Do not treat transaction rollback as complete isolation from every possible effect. Execution still consumes resources and takes locks, and database transactions do not automatically undo an external action. PostgreSQL's transaction isolation documentation also notes that sequence changes are not rolled back when a transaction aborts.
If execution is inappropriate, capture the estimated plan and state the evidence gap. An estimate can support a hypothesis about access paths, but it cannot establish actual latency or row counts. Avoid labeling an unmeasured index change as a verified optimization.
Locate the first meaningful estimate mismatch
Read the plan from the data access steps toward the final result. Compare estimated and observed rows where execution evidence is available. Look for the earliest substantial divergence that could make later join or sorting decisions inappropriate.
In a synthetic account query, suppose a filter was expected to return a few rows but actually returns most of the account's history. Investigate the filter's selectivity and the data distribution before blaming a later sort. The sort may be expensive because it received far more rows than expected.
The planner statistics guide explains how statistics inform estimates and why relationships between columns can escape ordinary single-column statistics. A predicate involving account, status, and region may combine values that are strongly related.
Check when and how relevant statistics were collected, and whether the fixture represents current distribution. If correlated conditions are the demonstrated cause, evaluate whether extended statistics fit those expressions and supported planner behavior. The CREATE STATISTICS reference describes the available kinds and the need for statistics collection.
Do not add statistics objects indiscriminately. Name the estimate they are intended to improve, collect the required evidence, and compare the resulting plan with the original hypothesis.
Interpret repeated work without double-counting
The PostgreSQL plan walkthrough distinguishes planner cost units from elapsed milliseconds. It also explains that rows and time for a repeatedly executed node are reported as per-execution averages alongside loops. Read those fields together.
A cheap inner lookup repeated for many outer rows can account for substantial work. In a synthetic nested loop, examine both how often the lookup runs and how many rows each execution returns. A small time printed on one line is insufficient evidence that the node is irrelevant.
For example, suppose an inner scan has estimated rows=1, observed rows=8, actual time=0.020..0.060, and loops=5000. These are deliberately invented field values, not copied execution output. It returns approximately 40,000 rows across its executions, and its reported average completion time implies roughly 300 milliseconds of accumulated node time. Looking only at 0.060 would miss the repeated work.
The row estimate is also wrong by a factor of eight per execution. Investigate whether the join key is less selective than expected, whether statistics represent the data, and whether the query actually needs all eight rows. Adding an index cannot make an eight-row relationship become a one-row relationship without changing the data or query semantics.
In this serial example, compare accumulated node work with the complete plan and statement timing. Do not generalize the arithmetic into a wall-clock prediction for parallel plans, and do not add a parent's inclusive time to the same children's time again. The PostgreSQL 18 walkthrough explains the per-loop reporting convention; the specific diagnosis still depends on the query and data.
Parent nodes generally include work performed by their children, so adding every displayed node time produces a misleading total. Use the tree to locate the mechanism, then compare statement-level measurements under controlled conditions.
Buffer and sort information can sharpen that hypothesis. Distinguish data found in shared buffers from blocks read into them, and inspect whether a sort used disk. These observations describe this execution; they do not establish the same cache state or memory pressure for every future request.
A sequential scan can be reasonable when much of a table is needed. An index scan can still perform excessive work when many rows require additional access or filtering. Judge the complete access pattern against the workload rather than treating a particular node name as success.
Change one supported cause and compare outcomes
Choose a change whose expected effect can be stated before applying it. Examples include correcting a query predicate, improving a demonstrated estimate, returning fewer unnecessary columns, or adding an index aligned with filtering and ordering. Preserve authorization predicates during any query rewrite.
Compare the original and candidate using the same parameter classes, dataset, concurrency, and measurement method. Confirm equivalent results and ordering. Include cases that may regress, such as broad scans or writes that must maintain an added index.
Retain the old and new plans with the test conditions and the reason for the change. Report observed differences without turning one run into a general performance guarantee. Before rollout, decide which application signal would confirm improvement and which would trigger recovery.
If the plan does not explain the delay, keep investigating the request boundary. Lock waits, connection acquisition, network transfer, and application processing deserve their own evidence. The next useful action is the one that tests the remaining hypothesis, rather than another index added without a demonstrated cause.
Was this guide useful?
Your rating helps us prioritize clearer, more practical technical content.
Review diffs, run checks, and prepare the release in Arezgit.
Keep Git review, security scanning, API checks, database inspection, and release preparation together in one local desktop application.
Explore Arezgit