Sub topic 9
Sampling and `default_statistics_target`
Prerequisites: you should know basic SELECT and WHERE clauses, understand indexes at a conceptual level, and be able to run psql and read simple EXPLAIN output.
ANALYZE does not usually inspect every row in a large table. It reads a sample of the table, then uses that sample to build column statistics. The planner uses those statistics to estimate how many rows match a condition.
The default_statistics_target setting controls how much detail PostgreSQL tries to retain for each column. Its default value is commonly 100. A higher target usually gives the planner more most-common values and a finer histogram, but it also makes ANALYZE use more work and storage.
The target is not a percentage and is not an exact number of sampled rows. PostgreSQL chooses the sample size and statistics structures internally.
How sampling affects statistics
Suppose a table has a status column where 95% of rows contain shipped and 5% contain pending. A small sample might represent this distribution poorly, especially when a value is rare. The planner could then estimate the wrong number of matching rows.
A higher target gives PostgreSQL more information to work with:
SHOW default_statistics_target;
SET default_statistics_target = 250;
ANALYZE orders;The SET command changes the value only for the current database session. The new setting affects the next ANALYZE; it does not immediately rebuild existing statistics.
You can inspect the resulting statistics:
SELECT attname,
most_common_vals,
most_common_freqs,
histogram_bounds
FROM pg_stats
WHERE schemaname = 'public'
AND tablename = 'orders'
AND attname = 'status';The most-common-value list is useful for skewed columns. The histogram represents the distribution of values that are not captured as most-common values. More target generally means more entries, although PostgreSQL can choose the exact representation.
Choosing a target
Use the default target first. Raise it when evidence shows that a column's distribution is difficult to describe or that estimates remain inaccurate.
Common candidates include:
- Columns with many distinct values.
- Columns with a long tail of rare values.
- Columns with strong skew.
- Columns used frequently in selective filters.
- Columns where
EXPLAIN (ANALYZE)shows large estimate errors.
A higher target is not automatically better. It can increase analysis time and planner memory use. Raising targets for every column often spends resources where they do not improve query plans.
Per-column targets
You can change one column without changing the database-wide default:
ALTER TABLE orders
ALTER COLUMN status SET STATISTICS 500;
ANALYZE orders;This setting is stored with the column and takes effect after the next analysis. To return the column to the default target:
ALTER TABLE orders
ALTER COLUMN status RESET (STATISTICS);
ANALYZE orders;Check the result instead of assuming that a higher target fixed the query. Compare estimated and actual rows before and after the change.
Exercise
Why might raising the statistics target for every column be a poor choice?
Show answer
It can make ANALYZE take longer and consume more statistics storage without improving plans for columns whose distributions are already easy to estimate.