skip to content

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:

statistics =
query =
query.sql
  1. 1-- no extended statistics
  2. 2SELECT * FROM customers
  3. 3WHERE city = 'Stockholm' AND country = 'SE';
the result for none | WHERE
estimatedint8actualint8
135
(1 row)
the plan

per-column statistics only: 35/2000 for Stockholm times 35/2000 for SE, times 2,000 rows. about 0.6, rounded up to 1.

real output, postgres 18.6 (PGlite 0.5.12) · city-country
pick a kind of statistics object and a query. without one, Stockholm in Sweden is estimated at 1 row (35x off) and the grouping at 200 groups instead of 41. dependencies and mcv fix the WHERE; ndistinct fixes the GROUP BY. each kind helps one kind of question.

the three kinds

sqldb=shop · session cc
EXPLAIN (ANALYZE, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM customers WHERE city = 'Stockholm' AND country = 'SE';
output, from postgres 18.6
EXPLAIN
Seq Scan on customers (cost=0.00..54.00 rows=1 width=61) (actual rows=35.00 loops=1) Filter: ((city = 'Stockholm'::text) AND (country = 'SE'::bpchar)) Rows Removed by Filter: 1965

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:

sqldb=shop · session cc
continues session cc (1 earlier block run first)
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';
output, from postgres 18.6
#1 · CREATE STATISTICS
#2 · ANALYZE
#3 · SELECT 1
dependenciespg_dependencies
{"4 => 5": 0.972500, "5 => 4": 0.093000}
(1 row)
#4 · EXPLAIN
Seq Scan on customers (cost=0.00..54.00 rows=34 width=61) (actual rows=35.00 loops=1) Filter: ((city = 'Stockholm'::text) AND (country = 'SE'::bpchar)) Rows Removed by Filter: 1965

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:

sqldb=shop · session cc
continues session cc (2 earlier blocks run first)
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';
output, from postgres 18.6
#1 · DROP STATISTICS
#2 · CREATE STATISTICS
#3 · ANALYZE
#4 · SELECT 5
valuestext[]frequencyfloat8
{"New York",US}0.1885
{London,GB}0.0795
{Berlin,DE}0.0665
{Mumbai,IN}0.0505
{Chicago,US}0.048
(5 rows)
#5 · EXPLAIN
Seq Scan on customers (cost=0.00..54.00 rows=1 width=61) (actual rows=0.00 loops=1) Filter: ((city = 'Paris'::text) AND (country = 'US'::bpchar)) Rows Removed by Filter: 2000

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:

sqldb=shop · session cc
continues session cc (3 earlier blocks run first)
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;
output, from postgres 18.6
#1 · DROP STATISTICS
#2 · CREATE STATISTICS
#3 · ANALYZE
#4 · EXPLAIN
HashAggregate (cost=59.00..59.41 rows=41 width=18) (actual rows=41.00 loops=1) Group Key: city, country Batches: 1 Memory Usage: 32kB -> Seq Scan on customers (cost=0.00..44.00 rows=2000 width=10) (actual rows=2000.00 loops=1)

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.

predict the output
a statistics object with only (dependencies). which estimate does it fix?
sql
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:

sqldb=shop
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';
output, from postgres 18.6
#1 · EXPLAIN
Seq Scan on customers (cost=0.00..54.00 rows=10 width=61) Filter: (lower(city) = 'paris'::text)
#2 · CREATE STATISTICS
#3 · ANALYZE
#4 · EXPLAIN
Seq Scan on customers (cost=0.00..54.00 rows=66 width=61) Filter: (lower(city) = 'paris'::text)
estimate, no statistics10
estimate, with statistics66
real count66
real output, postgres 18.6 (PGlite 0.5.12) · city-country
the estimate for 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 orders joined to customers on 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.