opyeb SQLPlan

Guides · Plan basics

Cost units versus milliseconds: what the planner minimises and what you measure

Updated 28 Nov 2026 · 7 min read

PostgreSQL's planner does not measure time. It computes a cost from a model of the storage and CPU, then chooses the plan with the lowest estimated total cost. The numbers printed on each node are in that abstract unit, not milliseconds. A cost of 18334 might run in 250 ms on one server and 2 seconds on another, depending on cache, storage and load.

The default model gives a sequential page read a cost of 1.0, a random page read 4.0, a processed row 0.01, and an operator CPU cost that varies. A sequential scan of 1,000 pages therefore costs about 1000 plus the per-row CPU. An index scan pays random reads for the heap plus the index descent, which is why it can be more expensive than a sequential scan when a large fraction of the table matches.

These constants live in postgresql.conf as seq_page_cost, random_page_cost, cpu_tuple_cost, cpu_index_tuple_cost and cpu_operator_cost. The default ratio of 4 between random and sequential reads dates from spinning disks. On NVMe storage the real ratio is closer to 1, and leaving random_page_cost at 4 makes the planner avoid indexes that would be faster.

ParameterDefaultMeaning
seq_page_cost1.0Cost of one sequential page read
random_page_cost4.0Cost of one random page read
cpu_tuple_cost0.01Cost per row processed
cpu_index_tuple_cost0.005Cost per index entry
cpu_operator_cost0.0025Cost per operator or function call
effective_cache_size4GBHint for how much data is cached

Reading the two pairs of numbers

A PostgreSQL node line has two parenthesised groups. The first is (cost=startup..total rows=N width=W); the second, with ANALYZE, is (actual time=first..last rows=N loops=L). The cost pair is what the planner used to choose the plan. The actual pair is what happened. Comparing them across nodes is more informative than comparing them in absolute terms.

If a node was estimated at 100 rows and returns 1,000,000, the planner chose a join order and memory size for the wrong cardinality. If a node was estimated at 100,000 rows and returns 100,000 but still takes most of the time, the estimate was right and the work itself is expensive — a missing index, a wide row, or a sort that spills. The two failures need different fixes.

Startup cost and LIMIT

The first number in the cost pair is the startup cost: the work needed before the first row can be returned. A sequential scan has a startup cost near zero; a sort has a startup cost close to its total because it must consume all input. For a query with a small LIMIT, the planner prefers a plan with low startup cost even if its total cost is higher, because it will stop early. That is why an index scan often wins for a top-N query.

This also explains a common surprise: the same query is fast with LIMIT 10 and slow without it, and the plan changes shape between the two. The LIMIT changes the objective from total cost to startup cost plus a fraction of the run cost. When you tune the slow version, do not assume the fast version's plan applies.

When the model and the machine disagree

Cost constants are a model, and models have assumptions: pages are equally expensive, rows are processed at a constant rate, and statistics describe the data. A table with a hot cache, a column with a skewed distribution, or a predicate the planner cannot estimate breaks one of them. The symptom is a plan that looks cheap and runs slowly, or a plan that looks expensive and runs fast.

The practical workflow is to read the plan for shape and estimates, then measure. If the plan shape is wrong, fix statistics or cost constants and re-plan. If the shape is right but one node is slow, look at that node's actual time, loops and buffers. Do not tune cost constants to make a specific query choose a specific plan; tune them to describe the hardware, then let the planner do its job.

Cost is a currency the planner spends, not a stopwatch. Measure with EXPLAIN ANALYZE before and after every change.

Sources

Questions

Can I convert cost to milliseconds?

Only roughly and only on one machine. The default constants assume a sequential page read costs 1.0 and a random page read 4.0, with CPU costs in hundredths of a unit. On SSDs the ratio between sequential and random I/O is much smaller, which is why tuning random_page_cost matters more than converting the total.

Why does a cheaper plan sometimes run slower?

The planner minimises estimated cost, not time. If statistics are wrong, a predicate is unestimatable, or the cost constants do not match the storage, the cheaper plan can be the slower one. EXPLAIN ANALYZE is how you find out.

What does startup cost mean?

The cost to produce the first row. A sort has a high startup cost because it must read all input before returning anything; a sequential scan has a low one. For a query with LIMIT, startup cost matters more than total cost.

The analyzer shows estimated cost and measured time on the same node, so the gap is visible.

Open the analyzer

Written against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.

Back to the guides