Choosing the Right Index: Measuring Column Skew Before You Build One
An index is only worth what it narrows down. If the value a query filters on is common enough that matching it still touches a large slice of the table, the planner will skip the index and scan the table anyway — the index just becomes dead weight the writer pays for on every insert/update. Data skew — one or a few values making up a disproportionate share of the rows — is what causes that. Before defining an index, it’s worth measuring skew on the candidate column rather than assuming it’ll help.
Why 15%
There’s no single official cutoff, but in practice, once a value matches roughly 15% or more of a table’s rows, an index scan for that value tends to cost more than a sequential scan does (random I/O per matching row vs. one linear pass) — so the planner stops choosing the index for it. A column where the most common value sits comfortably under that line is a good index candidate; a column where it’s well over it isn’t, at least not as a plain, full-column index.
Query: Measuring Skew from the System Catalogs
Postgres already tracks this — ANALYZE populates pg_stats with the most common
values per column and what fraction of rows each one covers. Joining that against
pg_class, pg_namespace, and pg_attribute gives a full picture: which columns
exist, how big their table actually is, and how skewed each column is.
SELECT
n.nspname AS schema_name,
c.relname AS table_name,
c.reltuples::bigint AS estimated_rows,
c.relpages AS table_pages,
a.attname AS column_name,
s.n_distinct,
round((s.null_frac * 100)::numeric, 2) AS null_pct,
round((s.most_common_freqs[1] * 100)::numeric, 2) AS top_value_pct,
CASE
WHEN s.most_common_freqs IS NULL THEN 'no MCVs — likely high cardinality'
WHEN s.most_common_freqs[1] > 0.15 THEN 'SKEWED (>15%)'
ELSE 'OK'
END AS skew_flag
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_attribute a ON a.attrelid = c.oid
AND a.attnum > 0
AND NOT a.attisdropped
LEFT JOIN pg_stats s ON s.schemaname = n.nspname
AND s.tablename = c.relname
AND s.attname = a.attname
WHERE c.relkind = 'r'
AND n.nspname = 'public'
ORDER BY top_value_pct DESC NULLS LAST;
What each part is doing:
pg_class.relpages/reltuples— physical table size. Worth filtering on before acting on anything else: a table with a handful of pages gets sequentially scanned regardless of skew, so measuring skew on it is wasted effort.pg_attribute— the real, current column list for the table (attnum > 0excludes system columns,NOT attisdroppedexcludes dropped-but-not-vacuumed column slots that would otherwise show up as ghost columns).pg_stats.most_common_freqs[1]— the fraction of rows held by the single most common value in the column. This is the number the 15% check is against.NULLhere means Postgres didn’t compute an MCV list for the column at all — usually because it’s close to unique, which is the opposite of skewed.pg_stats.n_distinct— cardinality. A positive number is an estimated distinct count; a negative number (e.g.-0.9) means “distinct values scale with table size,” which is typical of near-unique columns like primary keys.
This depends on pg_stats being current — run ANALYZE schema.table; first (or
confirm autovacuum has run recently) or the skew numbers reflect whatever the data
looked like at the last analyze, not now.
Steps to Follow When Defining an Index
- Make sure statistics are fresh. Run
ANALYZEon the table, or checkpg_stat_user_tables.last_analyze/last_autoanalyzebefore trusting the numbers. - Run the skew query scoped to your actual candidate columns — the ones that
show up in
WHERE,JOIN, orORDER BYclauses for the queries you’re trying to speed up. Skew on a column nothing filters by isn’t relevant. - Filter out tables that are too small to care — check
table_pages/estimated_rowsfirst; if the whole table already fits in a handful of pages, any index is likely to lose to a sequential scan on cost anyway. - Under 15%, and reasonably high
n_distinct— good candidate for a plain B-tree index on that column. - Over 15% (skewed) — a full-column index will simply be ignored by the
planner whenever the query matches the hot value, while still costing writes to
maintain. Options:
- A partial index that excludes the hot value, if queries mostly care about
the long tail:
CREATE INDEX ... ON table (col) WHERE col <> 'hot_value'; - Confirm whether the hot value is even one your queries filter for — if it isn’t, the skew may not matter for your actual access pattern.
- A partial index that excludes the hot value, if queries mostly care about
the long tail:
- After creating the index, confirm with
EXPLAINthat the planner is actually choosing it for the queries you built it for — a syntactically correct index that the planner never picks is no better than not having one. - Revisit after significant data growth or a bulk load. Skew is a snapshot of the data at analyze time; a large import or a change in usage pattern can shift which values are “hot” enough to matter.
Concepts demonstrated
- Data skew: why an index’s usefulness depends on the query’s predicate value matching a small share of rows, not just on the column being indexed
- System catalogs:
pg_class,pg_namespace,pg_attribute, andpg_statsjoined together to inspect real column- and table-level statistics pg_statsinternals:most_common_freqs,n_distinct, and why they depend onANALYZEbeing current- Index design process: a repeatable sequence — check stats freshness, measure
skew, filter out tables too small to matter, decide plain vs. partial index, then
verify with
EXPLAIN