Skip to content
WebsiteHosting.com

troubleshooting Data & storage

Cardinality-estimation errors and optimizer choices

A lookup can be cheap once and costly when repeated. Compare the plan favored by an estimated row count with the plan favored by the actual count, and see when an estimate error matters.

Latest recorded source check · P0141

What it means

Underestimating how many rows reach a join can make one lookup per row look cheaper than a scan. The lookup itself need not be more expensive than expected. What changes is how many times it has to be done.

Cardinality is a row count at a particular step in a query plan. Selectivity is the fraction of input rows that pass a condition. If a filter passes a larger fraction than the planner expects, the next step receives more rows than estimated. An error at the filter can therefore affect a choice made at the join.

Try it

Simulation: start with 10 estimated outer rows and 1,000 actual outer rows. An outer row is one that drives a lookup on the other side of the join. Each lookup costs 5 synthetic units; the alternative scan costs 200 units in total. Change only Actual outer rows from 1,000 to 20. Compare the estimated lookup cost and the lookup cost at the actual count against the same 200-unit mark.

Cardinality-estimation errors and optimizer choices

Show how wrong selectivity estimates can turn a plausible plan into a slow one

Simulation
Modeled outcome: Mismatch

Estimated cheaper plan is more expensive under actual cardinality.

Cardinality-estimation errors and optimizer choices: synthetic scenario inputs
Replaces all inputs with the documented starting example and the indicated change. Reset restores the starting example.
Estimated versus realized work

Row-count evidence

  1. Estimated rows10
  2. Actual rows1000
  3. Estimated choicenested
  4. Lower actual proxy costscan
  5. Cost mismatchYes
Estimated nested work50 cost units
Reference: 200 cost units
Actual nested work5000 cost units
Reference: 200 cost units

Both bars compare the same per-row cost proxy with the stipulated scan cost. The estimated choice may differ from the cheapest choice under the actual row count; neither is an observed optimizer decision.

Evaluated inputs

Required scenario evidence
provided
Estimated outer rows
10 count
Nested lookup cost
5 count
Actual outer rows
1000 count
Alternative fixed cost
200 count

Two synthetic cost formulas, fixed alternative cost, no real EXPLAIN ANALYZE or planner implementation.

About this model

The controls set only a local, bounded scenario. The computed branch is not a verdict about a live system. Inputs listed as not evaluated did not participate in this branch after validation.

Reset restores the original inputs. It does not change the open or closed state of help or FAQ answers.

Actual outer rowsLookup cost at the estimateChoice using the estimateLookup cost at the actual countCheaper at the actual count
1,00010 × 5 = 50Repeated lookup1,000 × 5 = 5,000Scan at 200
2010 × 5 = 50Repeated lookup20 × 5 = 100Repeated lookup at 100

The estimated choice stays put: the model chooses a plan using 10 rows in both cases. Changing the actual count changes the verdict on that choice. At 1,000 rows the model chose the more expensive plan; at 20 rows the estimate is still wrong, but the choice holds up.

With valid inputs, setting Required scenario evidence to missing leaves the comparison unknown. The displayed row counts and costs are not used. An absent or invalid input, such as a fractional row count, produces an input error instead of a plan. Reset restores the original row counts, costs, and provided evidence setting.

Where the choice changes

At 5 units per lookup, 40 rows cost exactly as much as the scan: 40 × 5 = 200. The model picks the repeated lookup on an estimated tie. At an estimate of 41 rows, the lookup total reaches 205, so it picks the scan instead. For the 1,000-row case, the estimate need not be exactly right to favor the cheaper plan; it just needs to be on the right side of that comparison.

These totals are calculated, not timed. The simulation considers only two plans and holds the scan cost fixed. It neither implements a PostgreSQL planner nor executes a query to see how long either plan takes.

A plan can cost no more under the actual proxy even if its estimated row count is wrong. With the original estimate of ten and actual count of forty, the chosen nested work and the scan both cost 200 units. The model reports no cost mismatch; that does not make the estimate correct. The result concerns the cost of the selected alternative, not accuracy of statistics alone.

In practice

For a slow query with a repeated lookup, start with an existing plan measurement from an authorized, isolated environment. Compare estimated and actual rows for the same step on the same counting basis. Trace the input to the lookup back through its filters: where is the first row-count discrepancy large enough to matter to the plan choice? The final number of rows returned to the application is not a substitute for the count that drove the lookups.

Consider two explanations before blaming stale statistics. If the data distribution has changed, check whether the statistics describe the data used by the query. If estimates look reasonable for two filters separately but not together, look for a relationship between their columns. For example, if a depot belongs to just one region, selecting that depot and its region is no more restrictive than selecting the depot alone. Treating the region as an independent filter can make the estimated count too small.

If the actual rows, comparable plan evidence, or applicable database version are unknown, stop short of a diagnosis. Do not refresh statistics or force a plan on the strength of this example. The useful distinction is between an inaccurate count and a consequential one: would the same cost rule favor a different plan with the observed count?

Frequently asked questions

Editorial questions based on the listed sources and explicitly labeled local examples.

Can correlated predicates mislead estimates?

Yes; independence assumptions can be wrong.

Is estimated cost elapsed time?

No. PostgreSQL planner cost uses abstract cost units and estimates, not measured elapsed milliseconds. Compare actual execution evidence separately in an authorized environment.

Does a wrong estimate prove stale statistics?

No; model limitations and correlation are alternatives.

What is a safe observation?

Start with the estimate and any already available execution statistics for the same query. In an authorized isolated read-only measurement, compare actual rows and loops with the estimates. An execution measurement runs the query; do not treat it as harmless for a statement that changes data.

Sources & model boundaries

Primary references used for this explanation. The interactive example is an educational model, not a live check.

  1. PostgreSQL Using EXPLAIN

    estimated rows and costs. Estimated rows and costs are distinct planner estimates; actual execution need not match them. EXPLAIN distinguishes different plan shapes and execution observations.

    Checked .

  2. PostgreSQL statistics used by planner

    extended statistics. Multivariate statistics can record functional dependencies that correct independence assumptions for applicable conditions. Simple equality and compatible values have important limitations.

    Checked .

Report an issue. Include the article URL and ID, not passwords or access keys.