Skip to content

Estimated Versus Actual Rows: the First Number to Check

One comparison localizes most plan problems. Find the deepest node where the planner's estimate and reality diverge, and stop reading upward.

Elias Rowe

3 min readPostgreSQL 18

Estimated Versus Actual Rows: the First Number to Check — PostgreSQL article cover

One comparison does most of the diagnostic work on a slow plan: estimated rows against actual rows. Read the tree from the leaves upward, stop at the first sharp divergence, and ignore everything above it — those nodes are wrong as a consequence, not as a cause. It is the first check because it explains the largest share of problems, not all of them.

Why start at the leaves?

An estimate error at the bottom of a plan does not stay there. The planner chose the join strategy above a scan based on how many rows that scan would produce. If the scan estimate was off by three orders of magnitude, every decision above it was made from a false premise.

So the nodes above a bad estimate will also look wrong. They are wrong as a consequence. Fixing them directly — usually by forcing a different join — produces a plan that is wrong in a new way rather than a plan that is right.

Walk up from the leaves. The first node where estimate and actual diverge sharply is where the investigation belongs, and everything above it can be ignored until that one is understood.

Reading the divergence

Nested Loop (cost=0.86..3421.55 rows=1 width=64)
(actual time=0.042..2841.663 rows=48122 loops=1)
-> Index Scan using shipments_carrier_idx on shipments s
(cost=0.43..812.11 rows=1 width=32)
(actual time=0.021..38.114 rows=48122 loops=1)
Index Cond: (carrier_id = 12)
-> Index Scan using orders_pkey on orders o
(actual time=0.049..0.052 rows=1 loops=48122)

The leaf expected one row and produced 48,122. That single number explains the whole plan: with an estimate of one row, a nested loop is obviously correct, and the inner index scan running once is free. With 48,122 rows it runs 48,122 times, and a hash join would have been the right choice.

The nested loop at the top is not the problem. It is the consequence.

Two directions of error behave differently. An underestimate leads the planner to pick a strategy that is cheap per row and expensive in bulk, which is the case above. An overestimate does the reverse: it discards a path that would have been fine — an index the planner refuses to use is the familiar shape — and reaches for one that pays a fixed setup cost the query never earns back.

What makes a row estimate wrong?

Stale statistics. The distribution changed and ANALYZE has not run since. The usual signature is a table that recently received a large load, or a column whose values shift over time such as a status that most rows leave quickly — the same distribution that makes a partial index worth considering.

Correlated columns. The planner multiplies selectivities as if columns were independent, and per-column statistics cannot capture cross-column correlation at all. WHERE country = 'JP' AND city = 'Osaka' is estimated as if living in Osaka were unrelated to living in Japan, so the estimate lands far below reality. CREATE STATISTICS on the column group gives the planner the dependency.

Unestimatable expressions. A condition the planner has no statistics for — a volatile function, a comparison against the result of a subquery it cannot inline — is estimated from a fixed assumption rather than from the data. That assumption is a guess and can be wrong by orders of magnitude.

When the estimate is right

Sometimes estimate and actual agree at every node and the query is still slow. That is a different problem, and it is worth recognizing early rather than continuing to chase statistics. The plan is the best available plan; the query is asking for more work than it needs to, or the schema does not support the access pattern, or the data volume genuinely requires the time.

That case is the honest one. It is also the one where adding an index actually helps, because the planner will use it — its estimate was accurate all along. What remains is a schema question: which columns the index carries and in what order.

Limits

Row counts are only one of several things a plan can be wrong about. Width estimates affect memory and the choice to spill; the planner’s model of how much of a table is cached affects its cost arithmetic and is not visible in the plan at all. Estimate-versus-actual is the first check because it explains the largest share of problems, not because it explains all of them.

Frequently asked questions

Why does PostgreSQL estimate one row when the query returns thousands?
There are three usual causes. The statistics are stale, so the distribution the planner is reading no longer matches the table. The predicate spans correlated columns, which the planner multiplies as if they were independent. Or the condition is one it cannot reason about at all, such as a volatile function, in which case it is estimated from a fixed assumption rather than from the data, and that assumption can be wrong by orders of magnitude.
Where should I start when estimated and actual rows disagree in a PostgreSQL plan?
Start at the leaves and walk up. The first node where estimate and actual diverge sharply is where the investigation belongs. Estimate errors propagate upward, because every join strategy above a scan was chosen from the row count that scan was expected to produce, so the nodes above a bad estimate are wrong as a consequence rather than as a cause.
How do I fix a bad PostgreSQL row estimate on correlated columns?
Declare the dependency with CREATE STATISTICS on the column group. Without it the planner multiplies the selectivity of each column as if they were unrelated, so a predicate on country and city is estimated as if living in Osaka had nothing to do with living in Japan, and the estimate lands far below reality.
What does it mean when PostgreSQL row estimates are accurate but the query is still slow?
It means the plan is the best available one, and the problem lies elsewhere: the query asks for more work than it needs to, the schema does not support the access pattern, or the data volume genuinely requires the time. It is also the case where adding an index actually helps, because the planner's estimate was accurate all along and it will use it.

References

  1. docsPostgreSQL 18 documentation — Row Estimation Examples (opens in a new tab)

    Works through the selectivity arithmetic referenced in this article.

share

-- written by

Elias RoweDatabase engineer

Elias Rowe writes about database engineering, SQL performance, and production systems. He focuses on measurable behavior, practical trade-offs, and conclusions that can be reproduced rather than assumed.

PostgreSQL

Reading a Plan Tree: Start at the Leaves

A plan is a tree printed upside down. Reading it in execution order instead of top to bottom turns a wall of text into a diagnosis.

· 4 min

Start typing to search the archive.