Engineering Note

A practical guide to access patterns, B-Tree index selectivity, composite index column ordering, and EXPLAIN ANALYZE validation.

Understanding Index Selectivity & Left-to-Right Ordering

An index is only effective if it drastically narrows down the number of disk pages the database engine must inspect. High-selectivity columns (like UUIDs or emails) benefit greatly from B-Tree indexes, whereas low-selectivity columns (like boolean flags) often trigger full table sequential scans.

For composite multi-column indexes (colA, colB), column ordering is critical: PostgreSQL can only use the index for queries filtering on colB if colA is also present in the WHERE clause, following the left-prefix rule.

Verifying Execution Plans with EXPLAIN ANALYZE

Never assume an index is being used without running EXPLAIN (ANALYZE, BUFFERS). Compare the estimated planning cost with actual execution time and shared hit buffers.

Watch out for implicit type coercion (such as comparing a VARCHAR column against an INTEGER literal) which forces PostgreSQL to bypass indexes and scan the entire table.

Related Engineering Notes

/production-ready-apis

Designing Production-Ready APIs

A comprehensive guide to designing resilient REST APIs: deterministic contracts, idempotency keys, RFC 7807 problem details, and defense-in-depth rate limiting.

/avoiding-n-plus-one

Avoiding N+1 Database Queries

Techniques for eliminating silent database query amplification in ORMs through query planning, batching, and join projection.

← Back to engineering notes