Sub topic 10
Raising the Statistics Target for One Column
When the default target is not enough
PostgreSQL's planner estimates how many rows a condition will match. It uses column statistics collected by ANALYZE. For many columns, the default statistics target gives useful estimates. A column with many distinct values or an uneven distribution may need more detail.
The statistics target controls how much information PostgreSQL stores for a column. A higher target can produce:
- A longer list of most common values.
- A finer histogram of less common values.
- A larger sample during
ANALYZE. - More accurate estimates for some predicates.
The target does not force PostgreSQL to use an index. It improves the information available when the planner compares possible plans.
Inspect the current statistics
Assume that orders.customer_id has many values and that queries filtering on it often choose inefficient plans. First inspect the current target and collected statistics.
SELECT attname, attstattarget
FROM pg_attribute
WHERE attrelid = 'public.orders'::regclass
AND attname = 'customer_id'
AND NOT attisdropped;
SELECT attname, n_distinct, most_common_vals,
most_common_freqs, histogram_bounds, correlation
FROM pg_stats
WHERE schemaname = 'public'
AND tablename = 'orders'
AND attname = 'customer_id';attstattarget is the per-column target. A value of -1 means that the column uses the database-wide default_statistics_target. The default is commonly 100, but check the setting in your server:
SHOW default_statistics_target;The pg_stats view exposes statistics in a readable form. n_distinct estimates the number of distinct values. most_common_vals and most_common_freqs describe values that occur often. histogram_bounds divides other values into ranges. correlation describes how closely the column's order follows the table's physical row order.
Raise the target and refresh the sample
Set a larger target only for the column that needs it. This avoids increasing analysis work for every column in the database.
ALTER TABLE orders
ALTER COLUMN customer_id SET STATISTICS 500;
ANALYZE orders (customer_id);The ALTER TABLE command changes the target for future analysis. The ANALYZE command immediately collects statistics for customer_id. Without the second command, the new target does not improve the statistics until the next automatic or manual analysis.
Check the result:
SELECT attname, attstattarget
FROM pg_attribute
WHERE attrelid = 'public.orders'::regclass
AND attname = 'customer_id';
SELECT n_distinct, most_common_vals,
most_common_freqs, histogram_bounds
FROM pg_stats
WHERE schemaname = 'public'
AND tablename = 'orders'
AND attname = 'customer_id';A higher target does not guarantee a longer value in every field. The data distribution determines which statistics PostgreSQL records.
Measure whether it helped
Use EXPLAIN before and after changing the target. With ANALYZE, PostgreSQL executes the query and reports actual row counts, so use it only when running the query is safe.
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
WHERE customer_id = 42;Compare the estimated rows= value with the actual rows reported as actual rows=. A smaller gap indicates a better estimate. Also check whether the selected plan fits the query. Accurate estimates can still lead to an expensive plan when the available indexes, joins, or costs make another choice reasonable.
| Change | Benefit | Cost |
|---|---|---|
| Higher target for one column | More detailed estimates for that column | More analysis time and statistics storage |
| Higher global default | More detail across many columns | Extra work for most tables |
| No target change | Lowest maintenance overhead | Poor estimates may remain |
> callout note > Raise the target for a demonstrated estimation problem. Do not treat the highest possible target as a general performance setting.
To remove the column-specific override and return to the global default, run:
ALTER TABLE orders
ALTER COLUMN customer_id RESET STATISTICS;
ANALYZE orders (customer_id);If two columns are correlated, a higher target for each column may still be insufficient. In that case, use extended statistics with CREATE STATISTICS, because ordinary column statistics usually estimate columns independently.
Exercise
What commands raise the target for orders.customer_id to 500, refresh its statistics, and verify the target?
Show answer
Run ALTER TABLE orders ALTER COLUMN customerid SET STATISTICS 500; then ANALYZE orders (customerid); finally query pgattribute for attstattarget where attrelid is 'public.orders'::regclass and attname is 'customerid'.