Sub topic 4
Reading Column Statistics in `pg_stats`
Assume you know basic SELECT and WHERE clauses, can run psql, and have a conceptual understanding of indexes and EXPLAIN. This chapter uses the orders table from the earlier example, after ANALYZE orders has run.
PostgreSQL stores sampled column statistics in the pg_stats view. The cost-based planner reads these statistics when it estimates how many rows a condition will match. You can inspect the view when a query plan looks surprising or when a column has an uneven distribution.
Start with the column overview
Run this query to inspect the important fields for two columns:
SELECT
attname,
null_frac,
n_distinct,
most_common_vals,
most_common_freqs,
histogram_bounds,
correlation
FROM pg_stats
WHERE schemaname = 'public'
AND tablename = 'orders'
AND attname IN ('status', 'customer_id');Each returned row describes one column.
| Field | Meaning |
|---|---|
| `attname` | The column name |
| `null_frac` | The estimated fraction of rows containing `NULL` |
| `n_distinct` | The estimated number of distinct values |
| `most_common_vals` | Values that occur more often than others |
| `most_common_freqs` | Frequencies matching `most_common_vals` |
| `histogram_bounds` | Bounds dividing less-common values into ranges |
| `correlation` | Similarity between column order and physical table order |
null_frac is a fraction between 0 and 1. A value of 0.10 means about 10 percent of sampled rows were null.
n_distinct is usually positive when PostgreSQL estimates a fixed number of distinct values. A negative value represents a fraction of the table. For example, -0.5 means PostgreSQL estimates that the column has distinct values equal to about half the table's row count.
Read most common values
A column's most frequent values appear in most_common_vals. PostgreSQL stores their frequencies in the same positions in most_common_freqs.
For the sample orders table, status might show:
most_common_vals:{shipped,pending}most_common_freqs:{0.95,0.05}
The planner can then estimate a predicate such as status = 'pending' using the observed frequency instead of assuming that every status is equally likely.
The arrays are parallel. The first frequency belongs to the first value, and so on. Do not read most_common_freqs independently from most_common_vals.
Understand histograms and correlation
Values that are not listed as most common values are represented by histogram_bounds. PostgreSQL divides the remaining value range into approximate buckets. For a numeric or date column, these bounds help estimate range conditions such as:
SELECT *
FROM orders
WHERE created_at >= now() - interval '7 days';A histogram is not a list of every value. It is a compact model of the distribution. It is therefore useful for estimates, but it cannot describe every rare pattern.
correlation ranges from -1 to 1. A value near 1 means that increasing column values usually appear in increasing physical table order. A value near -1 means the order is mostly reversed. A value near 0 means little relationship exists. PostgreSQL uses this information when comparing access methods such as an index scan and a sequential scan.
Inspect statistics safely
Statistics are estimates based on a sample, not a live audit of every row. Compare them with the table's actual data when investigating a plan:
SELECT status, count(*) AS actual_rows
FROM orders
GROUP BY status
ORDER BY actual_rows DESC;If the actual distribution differs sharply from pg_stats, refresh the statistics:
ANALYZE orders;A large difference between estimated and actual rows in EXPLAIN (ANALYZE) can indicate stale statistics, an insufficient statistics target, or a relationship between columns that ordinary per-column statistics cannot represent.
Exercise
If most_common_vals contains {shipped,pending} and most_common_freqs contains {0.95,0.05}, what fraction of rows does PostgreSQL estimate for status = 'pending'?
Show answer
It estimates about 5 percent of rows, assuming the statistics remain representative of the current table.