extended statistics
CREATE STATISTICS for columns that move together. city and country, and a 35x misestimate fixed.
- address
- everything.optimizer.extended_statistics
- part
- part 10, the optimizer
- chapter
- chapter 5 of 10
- reading time
- 6 min
postgres keeps statistics per column, so it can't know that two columns move together. CREATE STATISTICS tells it which ones do, and the next ANALYZE collects statistics about the combination. three kinds: dependencies (one column mostly determines another), ndistinct (how many distinct combinations exist) and mcv (the most common combinations, with their frequencies).
on customers, city and country turn a 1-row estimate for Stockholm in Sweden into the real 35.
the picture
you know how the planner multiplies conditions as if they were unrelated (when estimates go wrong)? every customer in Stockholm is in Sweden, and postgres has no idea.
think of a seating chart that only says "35 of 2,000 guests are from Stockholm" and, separately, "35 of 2,000 are Swedish". to know they're the same 35 people, someone has to look at both columns of the guest list at once. that's what a statistics object asks ANALYZE to do.
here's the same two queries without one, and with each kind:
- 1-- no extended statistics
- 2SELECT * FROM customers
- 3WHERE city = 'Stockholm' AND country = 'SE';
| estimatedint8 | actualint8 |
|---|---|
| 1 | 35 |
per-column statistics only: 35/2000 for Stockholm times 35/2000 for SE, times 2,000 rows. about 0.6, rounded up to 1.
the three kinds
EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM customers WHERE city = 'Stockholm' AND country = 'SE';1 estimated, 35 actual: 35/2,000 for the city times 35/2,000 for the country times 2,000 rows is 0.6, rounded up to 1.
dependencies measures how often knowing one column tells you the other, as a degree from 0 to 1:
CREATE STATISTICS customers_city_country (dependencies) ON city, country FROM customers;
ANALYZE customers;
SELECT dependencies FROM pg_stats_ext WHERE statistics_name = 'customers_city_country';
EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM customers WHERE city = 'Stockholm' AND country = 'SE';| dependenciespg_dependencies |
|---|
| {"4 => 5": 0.972500, "5 => 4": 0.093000} |
the columns are listed by number (4 is city, 5 is country). 4 => 5 is 0.97: city determines country for almost every row (the NULL cities break it). 5 => 4 is 0.09: knowing the country says little about the city, since the US has five. with the degree in hand, the planner stops multiplying in the full country selectivity, and the estimate becomes 34.
dependencies are small and cheap, but they only apply to equality (and IN) conditions on plain columns, and they assume the values you ask for are compatible: ask for Paris in the US and they predict 65 rows, the share of Paris, as if the country didn't matter.
mcv stores the most common combinations and their exact frequencies, like a column's MCV list but for tuples:
DROP STATISTICS customers_city_country;
CREATE STATISTICS customers_city_country (mcv) ON city, country FROM customers;
ANALYZE customers;
SELECT m.values, m.frequency
FROM pg_statistic_ext AS s
JOIN pg_statistic_ext_data AS d ON d.stxoid = s.oid,
pg_mcv_list_items(d.stxdmcv) AS m
WHERE s.stxname = 'customers_city_country'
ORDER BY m.frequency DESC, m.index
LIMIT 5;
EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM customers WHERE city = 'Paris' AND country = 'US';| valuestext[] | frequencyfloat8 |
|---|---|
| {"New York",US} | 0.1885 |
| {London,GB} | 0.0795 |
| {Berlin,DE} | 0.0665 |
| {Mumbai,IN} | 0.0505 |
| {Chicago,US} | 0.048 |
the Paris in the US query, which dependencies would get wrong, comes out at 1 (as low as an estimate goes): the pair isn't in the list, and the list knows it. mcv also handles ranges, OR and IS NULL, which dependencies don't. it's the biggest of the three to store.
ndistinct counts distinct combinations, for GROUP BY and DISTINCT:
DROP STATISTICS customers_city_country;
CREATE STATISTICS customers_city_country (ndistinct) ON city, country FROM customers;
ANALYZE customers;
EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT city, country, count(*) FROM customers GROUP BY city, country;41 groups, exactly. without it, the planner had no count for pairs at all and fell back to 200. a group count that's 5x too high decides between a HashAggregate and a sort, and how much memory the hash table gets.
CREATE STATISTICS s (dependencies) ON a, b FROM t;statistics on an expression
since postgres 14 a statistics object can cover an expression, which gives the planner what an expression index would, without the index:
EXPLAIN SELECT * FROM customers WHERE lower(city) = 'paris';
CREATE STATISTICS customers_city_lower ON (lower(city)) FROM customers;
ANALYZE customers;
EXPLAIN SELECT * FROM customers WHERE lower(city) = 'paris';lower(city) = 'paris' before and after statistics on the expression, against the real count. before, it's the default guess: 0.005 × 2,000 = 10.what it doesn't do
- only within one table. there's no statistics object across a join, so
ordersjoined tocustomerson correlated columns still multiplies. - only after ANALYZE. creating the object stores nothing until the next ANALYZE (or autovacuum's), like any statistics.
- only the combinations you declare. postgres won't find correlated columns for you. the clue is a misestimate on a multi-column WHERE, and the test is the query above with and without the object.
- pg_upgrade doesn't keep them. since postgres 18 pg_upgrade carries ordinary statistics over, but extended statistics need an ANALYZE after the upgrade.
keylines
- per-column statistics can't describe pairs, so correlated columns get multiplied.
- CREATE STATISTICS + ANALYZE collects statistics on the combination.
- dependencies: equality filters. ndistinct: GROUP BY and DISTINCT. mcv: exact common pairs, ranges, OR.
- statistics on expressions (postgres 14+) give the planner what an expression index would.
- one table only, declared by you, filled by ANALYZE.