UPGRD
← Home

Database Engineering

Why PostgreSQL Sometimes Ignores a Perfectly Good Index

An index can be valid and still be a poor plan choice. PostgreSQL estimates the work of alternatives, and its estimates depend on data distribution and statistics.

Home · · 5 min read · Updated November 26, 2026
  • PostgreSQL
  • SQL
  • Indexes
  • Query Planner
  • Performance
PostgreSQL query branching into an index scan or a sequential table scan.

Quick summary

An index can be valid and still be a poor plan choice. PostgreSQL estimates the work of alternatives, and its estimates depend on data distribution and statistics.

Seeing a sequential scan where you expected an index scan can look like the planner overlooked an obvious optimization. Usually it made a cost decision using its model and the statistics available. The index may be perfectly valid; fetching its entries and then visiting table rows may simply be estimated to cost more than reading the table sequentially.

Indexes are not free shortcuts

An index is attractive when a predicate selects a relatively small portion of a table and the index can narrow the search efficiently. But a conventional index scan often requires heap fetches to retrieve columns or verify visibility. If many rows qualify, those heap accesses may be scattered across pages. A sequential scan reads pages in order and can be cheaper, especially when much of the table will be needed anyway. PostgreSQL may also select a bitmap heap scan, combining index matches before visiting heap pages.

There is no universal selectivity threshold: table size, cache state assumptions, page costs, row width, correlation between index order and heap order, requested columns, parallelism and configuration all influence estimates. An index-only scan can reduce heap access when the index covers the query and visibility-map information permits it, but index existence alone does not guarantee that plan.

Check estimates against reality

Begin with EXPLAIN (ANALYZE, BUFFERS) on a representative statement. Compare estimated rows with actual rows at each plan node, not only the top-level runtime. A large estimate/actual mismatch is a clue that statistics or assumptions need investigation. Buffers reveal shared blocks hit or read and can help distinguish repeated heap work from broad scanning. Keep in mind that ANALYZE executes the statement. For a SELECT this can still impose production load; for data-changing statements it changes data unless wrapped safely in a transaction and rolled back. Prefer a safe environment or carefully scoped, low-impact query.

Planner cost is not elapsed time. It is a relative estimate using configured cost parameters and statistics; a plan with a lower estimated cost is not a promise of a faster wall-clock result on every machine. Likewise, a single cold or warm run is not a robust comparison. Compare realistic parameters, concurrency and cache conditions.

Statistics describe distributions, imperfectly

PostgreSQL’s ordinary statistics are maintained by ANALYZE (also run by autovacuum). They estimate quantities such as distinct values and most-common values for individual columns. Stale or too-coarse statistics can mislead the planner, especially after bulk changes or for skewed distributions. Running ANALYZE is a sensible first check, not a guarantee that the planner knows every relationship in the data.

Independence assumptions are especially troublesome for predicates on related columns. Suppose a table’s country and postal_region values are correlated. Individual-column estimates may multiply selectivities as if predicates were unrelated, underestimating or overestimating the rows satisfying both. Extended statistics can capture selected cross-column information:

CREATE STATISTICS customer_region_stats
    (dependencies, mcv)
    ON country, postal_region
    FROM customer;
ANALYZE customer;

Functional-dependency statistics model some dependency relationships, while most-common-value statistics capture frequent combinations. They are not a general model of arbitrary correlations, and they do not make every expression, join or range predicate perfectly predictable. Confirm that the query’s conditions align with the information collected, then compare estimated and actual row counts. Increasing a statistics target can help some distributions but increases analysis work and catalog storage; apply it to evidence-backed cases.

Test the query, not an index fantasy

Do not force index use as the first fix. Disabling sequential scans can be a diagnostic experiment, but it can also produce a worse plan and does not solve bad estimates. First verify predicate types and expressions match the index definition, the statistics are current, the query is selective, and the plan’s heap visits and buffer behavior make sense. If the index is unused across the real workload, it may be unnecessary write and maintenance cost; if an important query is slow, investigate its measured plan and data shape.

PostgreSQL’s EXPLAIN guide, planner statistics documentation, and CREATE STATISTICS reference explain the mechanics and limitations. The useful question is not “Why won’t PostgreSQL use my index?” but “What does the planner believe this query will cost, and where does that belief diverge from reality?”

Plan changes with production safety in mind

Before experimenting, capture the exact SQL, representative bind values, table size, index definitions and PostgreSQL version. An index may benefit one parameter but harm another when data is skewed. Prepared statements can select plans under conditions unlike an ad hoc query, so test the execution path the application actually uses. Avoid comparing plans against different snapshots of rapidly changing data.

When EXPLAIN ANALYZE is safe, inspect loops as well as rows: a node executed thousands of times may be expensive despite small per-loop work. Check whether a nested loop multiplies heap lookups, whether a bitmap scan reduces random access, and whether a sort or filter discards rows after a broad scan. BUFFERS counts are block activity, not direct physical-disk I/O; shared hits still consume CPU and memory bandwidth, and shared reads may be served by the operating-system cache.

Extended-statistics objects provide additional distribution information; they do not change query results or force a plan. Run ANALYZE after creating one, then compare estimates for predicates the object was intended to describe. If estimates are already close, investigate another cause such as join shape, row width, cost assumptions or a missing covering index. The planner’s model is useful, but it is not a live measurement of every storage and cache condition. Application-level caching is a separate trade-off; profiling Redis object caching in WordPress shows why moving work does not always remove it.

References

PostgreSQL: Using EXPLAIN
PostgreSQL: Statistics Used by the Planner
PostgreSQL: CREATE STATISTICS