Sub topic 11
Extended Statistics for Correlated Columns
Assume you know basic SELECT, WHERE, JOIN, CREATE TABLE, indexes, and simple EXPLAIN output. This chapter focuses on cases where PostgreSQL estimates several column conditions independently, even though the columns are related.
Why single-column statistics can fail
PostgreSQL normally stores statistics for each column separately. Suppose an addresses table has city and zip columns:
SELECT *
FROM addresses
WHERE city = 'Springfield'
AND zip = '01103';The planner estimates the selectivity of city = 'Springfield' and zip = '01103', then combines those estimates. This works reasonably when the columns are independent. It fails when knowing one value makes the other value much more likely.
A ZIP code usually belongs to one city. Treating city and zip as independent can make the planner expect too few or too many rows. That can lead it to choose an inefficient join order, join algorithm, or scan method.
Extended statistics store relationships across columns.
Creating and collecting extended statistics
Use CREATE STATISTICS to define the relationship that PostgreSQL should track. The statistics are not populated until ANALYZE runs.
CREATE STATISTICS addresses_city_zip (dependencies, ndistinct)
ON city, zip
FROM addresses;
ANALYZE addresses;The dependencies kind records functional relationships. For example, a ZIP code may strongly determine a city. The ndistinct kind records how many distinct combinations occur, which helps with grouping and some multi-column estimates.
You can also collect frequent combinations with the mcv kind. MCV means “most common values.” It is useful when particular pairs or combinations are common:
CREATE STATISTICS addresses_city_zip_mcv (mcv)
ON city, zip
FROM addresses;
ANALYZE addresses;Choose the kind based on the problem:
| Statistics kind | Helps describe | Typical use |
|---|---|---|
| `dependencies` | Relationship between column values | Correlated `WHERE` predicates |
| `ndistinct` | Distinct multi-column combinations | `GROUP BY` and distinct-count estimates |
| `mcv` | Common combinations and their frequencies | Skewed multi-column filters |
Extended statistics do not create an index and do not change table data. They only give the cost-based planner more information.
Verifying the stored statistics
The pg_stats_ext view shows extended statistics that are visible to your user:
SELECT statistics_name,
attnames,
kinds,
n_distinct,
dependencies
FROM pg_stats_ext
WHERE schemaname = 'public'
AND tablename = 'addresses';The exact values depend on the table contents and the sample collected by ANALYZE. An empty result often means the statistics object has not been analyzed yet, the object belongs to another schema, or you do not have permission to view it.
Checking whether estimates improved
Compare the planner's estimate with the actual row count:
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM addresses
WHERE city = 'Springfield'
AND zip = '01103';For each plan node, compare rows= with actual rows=. A large difference shows a misestimate. Run the query before and after creating the statistics on comparable data. Do not assume that a new statistics object must change the plan. If the old estimate was already accurate, or another cost dominates, the plan may remain the same.
Common errors and limits
- Forgetting
ANALYZEafterCREATE STATISTICS. - Creating many overlapping objects without measuring whether they help. Each object adds collection and storage work.
- Expecting extended statistics to replace indexes. They improve planning, but they do not make a scan faster.
- Testing with stale or very small data. Statistics describe a sample of the current table.
- Assuming all possible column relationships are detected automatically. You must define the columns that belong together.
Exercise
Which statistics kind is a strong first choice when two equality predicates are correlated, such as city and zip?
Show answer
Use dependencies to describe the relationship. Consider mcv as well when specific column combinations are unusually common.