Skip to content

PostgreSQL Covering Index Not Used Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about PostgreSQL Covering Index Not Used Fix. We cover key concepts, practical examples, and best practices.

Queries are performing index scans with heap fetches because the SELECT columns are not all included in the index, preventing index-only scans.

The Wrong Way

CREATE INDEX idx_orders_cov ON orders(status, created_at);
EXPLAIN ANALYZE SELECT status, created_at, total FROM orders WHERE status = 'shipped';

Output:

Index Scan using idx_orders_cov ...
Heap Fetches: 5000
Time: 234.567 ms

The Right Way

CREATE INDEX idx_orders_cov ON orders(status, created_at) INCLUDE (total);
EXPLAIN ANALYZE SELECT status, created_at, total FROM orders WHERE status = 'shipped';

Output:

Index Only Scan using idx_orders_cov ...
Heap Fetches: 0
Time: 12.345 ms

Step-by-Step Fix

1. Identify columns in SELECT and WHERE that are not covered by existing indexes

2. Use the INCLUDE clause to add payload columns without expanding the B-Tree

3. Verify with EXPLAIN ANALYZE that you see 'Index Only Scan' instead of 'Index Scan'

4. Check heap fetches in pg_stat_user_tables to confirm reduction

5. Balance covering index size against write performance overhead

Prevention Tips

  • Test all queries with the database's explain plan tool before deploying to production.
  • Monitor query performance trends using built-in monitoring tools.
  • Set up automated index usage analysis in CI/CD pipelines.
  • Review database configuration quarterly against workload patterns.
  • Keep database statistics up to date with regular maintenance operations.
  • Use DodaTech's monitoring tools to track query performance regressions.

Common Mistakes with index covering

  1. Misunderstanding that String is [Char] with poor performance for large text operations
  2. Using foldl instead of foldl' causing stack overflow on large lists
  3. Forgetting deriving (Show, Eq) on custom data types needed for debugging

These mistakes appear frequently in real-world POSTGRESQL code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### How is INCLUDE different from adding a column to the index key?

INCLUDE columns are stored as payload in the leaf pages but are not part of the B-Tree key structure. This means they do not affect index ordering or size of internal pages, and do not count toward the index key length limit (currently 2712 bytes). They are ideal for SELECT columns not used in WHERE.

How do I verify this fix is working?

Use psql to connect to the database and run an explain plan query. Check that the output shows an index scan (IXSCAN, Index Scan, or similar) instead of a full scan (Seq Scan, COLLSCAN, or ALL). Monitor query latency to confirm improvement.

Can this fix impact other queries?

Index and configuration changes can affect other query patterns. Always test in a staging environment first. Review the explain plans of your top 5-10 queries after making changes to ensure no regressions.

Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro