Sub topic 4
Estimated Rows and Width
Assume you already know SELECT, WHERE, JOIN, and basic indexes. You should also be comfortable running SQL in psql or another SQL client.
When PostgreSQL plans a query, it estimates how many rows each plan node will produce. It also estimates the average size of each output row. These values help the planner compare different scan, join, sort, and aggregate strategies.
A typical plan line looks like this:
Seq Scan on orders (cost=0.00..180.00 rows=2500 width=48)The important values are:
| Value | Meaning |
|---|---|
| `rows=2500` | Estimated number of rows produced by this node |
| `width=48` | Estimated average size of each output row, in bytes |
| `cost=0.00..180.00` | Estimated startup and total cost |
The rows value describes the node's output, not necessarily the number of rows stored in the table. A scan with a filter might read one million table rows but estimate that only 2,500 pass the filter.
How PostgreSQL estimates rows
PostgreSQL uses table statistics, column statistics, operators, and information about constraints. For example, if a column contains about 100 distinct customer IDs and the planner assumes they are evenly distributed, this condition may estimate about one percent of the table:
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;Possible output:
Index Scan using orders_customer_id_idx on orders
(cost=0.29..45.10 rows=120 width=72)
Index Cond: (customer_id = 42)The planner estimates 120 matching rows. It does not promise that exactly 120 rows will be returned.
For a join, the estimate describes the number of joined rows. A mistake in an early estimate can grow as later nodes use it. For example, if PostgreSQL expects 10 rows from a filter but the filter actually produces 100,000 rows, it may choose a nested loop that becomes very expensive.
What width means
width is the estimated average number of bytes in one row produced by a node. It depends on the columns the node must carry. A query selecting only an integer usually has a smaller width than a query selecting many text columns:
EXPLAIN
SELECT id
FROM orders
WHERE customer_id = 42;
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;A larger width can increase the estimated cost of sorting, hashing, joining, and moving rows between plan nodes. It is an estimate of the plan's row representation, not a guarantee about the exact on-disk size of every row.
When estimates are suspicious
Compare estimates with actual results when you run EXPLAIN ANALYZE:
EXPLAIN (ANALYZE)
SELECT *
FROM orders
WHERE customer_id = 42;A node might show:
Index Scan ... (cost=0.29..45.10 rows=120 width=72)
(actual time=0.05..18.20 rows=9500 loops=1)The estimate of 120 is far below the actual 9,500. This misestimate can lead to a poor plan. Common causes include stale statistics, uneven data distribution, correlated columns, or a filter expression the statistics do not describe well.
Refresh ordinary statistics with ANALYZE, then inspect the plan again:
ANALYZE orders;
EXPLAIN
SELECT *
FROM orders
WHERE customer_id = 42;If the estimate remains poor, inspect the query conditions and consider extended statistics for correlated columns.
Exercise
A plan estimates 50 rows with width 40, but EXPLAIN ANALYZE reports 20,000 actual rows. What does this suggest?
Show answer
The planner has a serious row-count misestimate. Check statistics freshness, data skew, correlated predicates, and expressions that the planner cannot estimate well.