advanced24 min studyLesson 283 in Engineering

Cost-based plan selection

Compare legal physical alternatives using explicit resource models, startup cost, expected rows, and the amount of output the caller needs.

On this page

What you’ll understand

  • Distinguish optimizer cost units from measured execution time.
  • Compare startup and total costs for full versus partial consumption.
  • Explain how row estimates and physical properties change the chosen plan.

Once several equivalent expressions are available, the planner needs a way to choose among their physical implementations. A cost model predicts resource work using estimated cardinalities and properties such as order, indexing, and memory fit. Its output is a decision aid with assumptions. It is not a stopwatch reading, and a plan with the smallest displayed total cost need not deliver the earliest first row.

A cost model is a weighted description of work

A teaching model can add expected sequential page reads, random reads, and CPU operations, each multiplied by a configurable weight. PostgreSQL expresses costs in abstract relative units rather than milliseconds. Those weights encode a rough hardware and workload model; changing one does not make the underlying device faster. Calibration should represent the environment in which plans compete. Cache residency, concurrency, storage latency distributions, and expression complexity can make the same abstract estimate correspond to different measured times on different runs.

Do not sum an entire printed plan's costs as though every node listed independent work. Parent costs generally include their children. Instead, compare complete alternative paths or isolate an operator's incremental assumptions with care. An index scan can avoid reading many rows but incur scattered access; a sequential scan can examine more tuples while moving pages efficiently. The cheaper alternative depends on selectivity, needed columns, visibility checks, order requirements, and the data layout. A named operator alone does not establish which choice is better.

C^=nscs+nrcr+ntct\widehat C=n_s c_s+n_r c_r+n_t c_t
n_s and n_r are estimated sequential and random page operations, and n_t counts modeled tuple operations. The c coefficients have abstract cost units per operation. This illustrative weighted sum is not the full PostgreSQL cost formula and does not produce seconds.↔ Scroll wide equations horizontally.

The caller may need only a prefix

Startup cost measures work before useful output begins. Total cost models consuming the planned output. A blocking sort can have substantial startup work because it must inspect all relevant input before knowing the first globally smallest row. A suitable ordered index can sometimes supply that row earlier even when fetching every row through the index would be more expensive. A LIMIT therefore changes the optimization objective. The exact benefit depends on whether rows can really be stopped early and whether intervening operators must finish their own input first.

A simple interpolation between startup and total cost estimates the price of a requested fraction. It assumes post-startup work is approximately proportional to rows consumed. That can be inaccurate for skewed filters, uneven lookup costs, or late blocking phases. Treat it as a comparison model with stated premises, not a guarantee for every k-row prefix. A count estimate that is too small can also distort the fraction being requested, causing a planner to favor a path whose assumed early-stop advantage never materializes.

C^(f)=Cstart+f(Ctotal−Cstart),0≤f≤1\widehat C(f)=C_{\rm start}+f(C_{\rm total}-C_{\rm start}),\qquad 0\le f\le1
f is the fraction of modeled result rows consumed and both C values use the same abstract cost units. The interpolation assumes roughly uniform incremental work after startup. It is a teaching model and cannot bypass a genuinely blocking dependency.↔ Scroll wide equations horizontally.

Physical properties make alternatives incomparable by one scalar alone

A path that is locally more expensive can provide useful ordering to a later merge or ORDER BY and avoid a separate sort. Parameterized paths require values from an outer input. Parallel paths change startup, coordination, and resource use. A planner must retain useful alternatives rather than discard everything except the cheapest unqualified subplan. Feasibility comes first: an operator must support the required predicates and semantics. Memory limits, data-type comparison behavior, and supported join conditions can rule out an apparently attractive algorithm before cost comparison begins.

The queue lab illustrates a resource-capacity assumption rather than optimizer logic. Reset, set both λ=2 and μ=4, predict the initial four-job backlog drains at t=2, then Play or Step. Separately Reset, set both λ=2 and μ=3, predict drainage at t=4, then Play or Step. These separate fluid runs isolate service capacity while holding arrivals fixed. They do not choose SQL plans or convert cost units to seconds; the M/M/1 mean requires separate stochastic assumptions. Identify which measured resource bottleneck a real cost-weight change is supposed to represent.

The best full-result plan can lose a short-prefix comparison

Two hypothetical plans return an estimated 1,000 rows. Plan A has startup cost 100 and total cost 1,100; plan B has startup 10 and total 2,010. Use linear post-startup interpolation to compare ten rows and all rows.

  1. Ten rows are fraction 10/1,000=0.01. Plan A costs 100+0.01×(1,100−100)=110 abstract units for this prefix.

  2. Plan B costs 10+0.01×(2,010−10)=30 units for the same modeled prefix. Under the interpolation assumptions, B is cheaper when only those ten rows are consumed.

  3. For every row, the totals are 1,100 for A and 2,010 for B, so A wins. The two interpolated lines cross when 100+1,000f=10+2,000f, giving f=0.09 or ninety estimated rows.

The result

The selected path depends on how much output is required and when work must occur. None of these numbers is a measured millisecond value.

Explore the model

Interactive lab

Interactive lab · queue

Inspect one capacity assumption behind a plan

Reset, set both λ=2 and μ=4, predict drainage of the initial four jobs at t=2, then Play or Step. Separately Reset, set both λ=2 and μ=3, predict t=4, then Play or Step. This fluid capacity comparison is not a planner or a conversion from abstract costs to runtime; M/M/1 assumptions are separate.

Inspect one capacity assumption behind a planFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA cost model needs defensible resource assumptions, and its relative units must be checked against actual workload behavior.

Check your understanding

A PostgreSQL plan reports total cost 800. What does that number directly mean?

Try it yourself

Plan X has startup 40 and total 240; plan Y has startup 10 and total 410. Under linear interpolation, at what consumed fraction do their costs coincide?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Set 40+200f equal to 10+400f.

Compare with the solution

30=200f gives f=0.15. Below fifteen percent, Y is cheaper in this model; above fifteen percent, X is cheaper. The conclusion assumes both plans return the same estimated cardinality and can stop according to the stated linear post-startup model. Blocking operators or uneven per-row work can invalidate the interpolation.

Take it with you

  • Cost units compare modeled work and are not elapsed time.
  • Startup, total work, and early stopping can favor different plans.
  • Retain physical properties and semantic feasibility when comparing alternatives.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.