Choosing the Right Index: Measuring Column Skew Before You Build One

Aug 26, 2026postgresrdsindexingperformance

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:

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

  1. Make sure statistics are fresh. Run ANALYZE on the table, or check pg_stat_user_tables.last_analyze / last_autoanalyze before trusting the numbers.
  2. Run the skew query scoped to your actual candidate columns — the ones that show up in WHERE, JOIN, or ORDER BY clauses for the queries you’re trying to speed up. Skew on a column nothing filters by isn’t relevant.
  3. Filter out tables that are too small to care — check table_pages / estimated_rows first; if the whole table already fits in a handful of pages, any index is likely to lose to a sequential scan on cost anyway.
  4. Under 15%, and reasonably high n_distinct — good candidate for a plain B-tree index on that column.
  5. 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.
  6. After creating the index, confirm with EXPLAIN that 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.
  7. 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