Read the plan, then test the estimate

Separate logical query meaning from physical plan choices, read estimated versus actual rows, and use EXPLAIN safely. Query planning and optimizer estimates · Databases.

The SQL stays the same while the plan changes. Read the logical request, inspect the estimate, and choose the measurement that is safe to run.

One request, two plan layers

A logical plan describes the result a query means: choose rows, join relations, and form the requested groups or projections. It does not commit to a table scan, index lookup, hash join, nested loop, or sort. A physical plan is the executor's chosen tree of concrete nodes. Different physical trees can return the same rows when they respect the same logical contract.

Keep meaning separate from route

Read the steps as text

  1. The same request joins customers to paid orders in North and groups by customer. These are illustrative plan choices, not a live SQL execution.
  2. Start at leaf C: scan customers and keep the North region. This is a teaching plan, not output from an SQL engine.
  3. Leaf O scans orders and keeps paid orders. These two filtered inputs feed the parent hash join.
  4. The parent hash join takes C and O and matches orders.customer_id to customers.id. Its joined rows feed the aggregate.
  5. The aggregate consumes J and groups by customer. This candidate preserves the requested result; selective access and probes could also implement it, and neither is declared faster.
  6. Suppose a safe measured run observes 18,000 joined rows against the estimate of 500. Investigate the first node where estimates diverge, including selectivity, statistics, skew and workload; the gap alone does not select a replacement plan.
  7. In PostgreSQL 18, plain EXPLAIN plans without running the statement. EXPLAIN ANALYZE executes it and reports actual rows, loops and timing for that run. Instrumentation can add overhead; a write can have side effects.
  8. The logical result stays fixed. Compare the estimate with an observation, then investigate before judging a route; a measured plan is evidence for one execution.

The first frame uses customers(id, region) and orders(order_id, customer_id, status, amount). The request joins customers to paid orders in the North region and groups by customer. The logical outline is selection of the two predicates, a join on customer_id, then grouping. Two physical candidates are available: one scans and hashes inputs; another uses a selective customer access path and repeated order probes. The scene keeps the result contract fixed while the route changes.

The plan tree is evidence, not the query meaning

Suppose the logical query is written once. The planner can push a filter toward a base scan, choose a different join order, or replace one access path with another while preserving the requested result. In a plan tree, child nodes feed parent nodes; read the leaves first and follow rows upward. A node's name tells you how the executor intends to work, not why the business result is correct. SQL semantics still come from the query and its data rules.

The index-strategy lesson adds workload-fit questions around access paths. The sort and aggregate lesson and join-algorithms lesson explain what those physical nodes do once selected. This lesson supplies the missing bridge: use the plan to ask which route was chosen, which estimate justified it, and what observation would challenge that estimate.

Selectivity turns statistics into estimates

Selectivity is the fraction of a relation expected to satisfy a predicate. If status = 'paid' is expected to match 5% of one million orders, the planner may estimate about 50,000 output rows for that filter. That is an estimate, not a count read from the future. PostgreSQL 18 records statistics collected by ANALYZE; samples, common values, histograms and extended statistics can describe distributions and correlations, but they remain approximate.

A plan can therefore choose an index because its estimate predicts a small result, while the actual data returns many rows. Stale statistics, a changed distribution, correlated predicates, data skew or a different parameter can widen the gap. Compare estimated rows with actual rows at the relevant node before claiming that the index or join algorithm is wrong. Cost units are planner comparisons, not milliseconds; an actual runtime measurement has a different meaning.

EXPLAIN and EXPLAIN ANALYZE answer different questions

In the PostgreSQL 18 profile, plain EXPLAIN asks, “What plan would the planner choose, and what does it estimate?” It does not run the statement. In the same profile, EXPLAIN ANALYZE asks, “What happened when this statement executed under this data and configuration?” PostgreSQL 18 adds actual rows, loops and timing to the plan, so it can expose an estimate gap. The instrumentation itself can add overhead, and the result is evidence for that execution rather than a timeless benchmark.

Treat the word ANALYZE as an execution boundary in this PostgreSQL 18 profile. A SELECT under EXPLAIN ANALYZE still reads and computes; a data-changing statement can perform its side effects. Use a read-only query while learning, or use a transaction-and-rollback procedure only when that procedure is appropriate for the statement and environment. Never infer that discarded result output means discarded work.

Transfer the diagnosis to a new request

Read the steps as text

  1. The new request asks for recent delivered shipments from Harbor. An illustrative plain plan estimates 40 rows; the actual count is not yet measured.
  2. Suppose a measured run observes about 900 rows and Harbor has a larger cluster than the sample suggested. Keep the request fixed and inspect the first gap, statistics and distribution; 40 is not a universal index threshold.
  3. Plain EXPLAIN does not execute the statement in PostgreSQL 18. EXPLAIN ANALYZE runs the SELECT; for a write, choose an appropriate environment and procedure because execution can have side effects.
  4. The request stays fixed. The 40 versus 900 gap calls for investigation, not an automatic route change; measurements execute the statement.

The faded frame uses shipments(shipment_id, depot, state, scanned_at) and asks for recent delivered shipments from Harbor. A plain plan estimates 40 rows from a selective depot-and-state predicate, while the observed distribution has a much larger Harbor cluster. The transfer keeps the logical request stable and changes the evidence: statistics may be stale, the selected scan may differ, and EXPLAIN ANALYZE would execute the read. A separate UPDATE example is present only as a safety warning, not as an action to run.

Recap

  • Logical plans describe relational meaning; physical plans choose concrete nodes that must preserve that meaning.
  • Selectivity, statistics, data distribution and cost assumptions shape estimates, so an access path or join choice is qualified evidence.
  • EXPLAIN plans without executing; EXPLAIN ANALYZE executes and measures one run, including side effects for writes.
Why an estimate gap is a diagnosis clue

A large estimated-versus-actual row gap can point toward stale statistics, skew, correlated predicates, a parameter-sensitive distribution or a predicate whose selectivity is hard to model. It does not, by itself, prove that an index should be added or that a specific join algorithm should replace the chosen one. Check the node where the gap begins, the relevant statistics, the logical predicate and the workload before changing the query or schema. PostgreSQL 18's planner uses cost units and sampled statistics; those details are evidence for the named profile, not universal database behavior.

For a DSA connection, hash maps in Fundamentals make a cost trade-off concrete: direct key association can be useful, but the useful structure depends on the access pattern. Revisit B+ access paths to trace one index route before a planner chooses among alternatives.