Costs and row estimates
Overview
Estimated cost is a planner comparison metric, not milliseconds. Estimated rows describe node output cardinality, which may differ from reality because statistics summarize data and distributions can be skewed or correlated.
First example
SELECT stat FROM sqlite_stat1 WHERE idx='ep_status';Observed SQLite result (rows in JSON):
[ [ "100 50" ]]How it works
PostgreSQL reports startup and total cost in planner units, estimated output rows, and estimated average row width in bytes. Total cost includes child costs; do not sum all node totals. Compare estimated rows to actual rows per loop at the same node; a large mismatch can influence join order or method. With SQLite ANALYZE already run, sqlite_stat1 stores "100 50" for the single-column status index: 100 entries and an average of 50 per distinct key. Actual group counts are 90 common and 10 rare. The average differs from either group; it is not evidence that the optimizer predicted exactly 50 for every predicate. Additional statistics, if available, and planner rules can alter estimates. PostgreSQL statistics and extended statistics address some skew and correlation; update statistics before drawing conclusions. A selective predicate does not guarantee a particular plan.
Practical example
SELECT status,COUNT(*) FROM ep_order GROUP BY status ORDER BY status;Observed SQLite result (rows in JSON):
[ [ "common", 90 ], [ "rare", 10 ]]Practice and mistakes
Practice: compare the mean of 50 with the rare count of 10 and common count of 90. Hint 1: use GROUP BY status. Hint 2: divide the larger by the smaller for a mismatch factor; rare differs by 5x, common by 1.8x. These compare a summary average with counts, not captured PostgreSQL estimates.
PostgreSQL 17 example — NOT RUN
BEGIN;SET LOCAL statement_timeout = '10s';SET LOCAL lock_timeout = '2s';CREATE TEMP TABLE ep_customer(id INTEGER PRIMARY KEY, name TEXT NOT NULL);INSERT INTO ep_customer VALUES(1,'Ada'),(2,'Reza'),(3,'Sara');CREATE TEMP TABLE ep_order(id INTEGER PRIMARY KEY, customer_id INTEGER NOT NULL REFERENCES ep_customer(id), status TEXT NOT NULL CHECK(status IN ('common','rare')), amount INTEGER NOT NULL CHECK(amount>=0));WITH RECURSIVE seq(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM seq WHERE n<100)INSERT INTO ep_order SELECT n, CASE WHEN n<=60 THEN 1 ELSE 2 END, CASE WHEN n<=90 THEN 'common' ELSE 'rare' END,n*10 FROM seq;CREATE INDEX ep_status ON ep_order(status);CREATE INDEX ep_customer_id ON ep_order(customer_id);CREATE INDEX ep_amount ON ep_order(amount);ANALYZE ep_order;
EXPLAIN (ANALYZE, TIMING OFF) SELECT id FROM ep_order WHERE status='rare';ROLLBACK;Expected result, not executed: "Compare estimated rows with actual 10 at the relevant node; plan not fixed."