Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

What is a covering index, and how does PostgreSQL's INCLUDE clause differ from adding the column as an extra key column? Why might EXPLAIN show Index Only Scan with Heap Fetches: 90000?

A covering index holds every column a query needs, so the database can answer from the index without reading table rows. In PostgreSQL, INCLUDE (col) stores a column only in the leaf entries, outside the sort order and the uniqueness check, which keeps inner pages small and lets a unique index cover extra columns. Index Only Scan with Heap Fetches: 90000 means PostgreSQL still had to visit the table to check row visibility for pages not marked all-visible, usually because vacuum has not run recently; VACUUM fixes it.

Why this question is asked

An index-only scan is one of the cheapest ways to answer a query: no table pages, just a short walk through the index. It is also a feature that often disappoints in PostgreSQL, because EXPLAIN says Index Only Scan while the query still reads thousands of table pages. Interviewers ask this to see whether we understand both halves: which columns make an index covering, and the visibility rule that decides whether the table is skipped. In this answer, we will recap covering indexes, compare INCLUDE with extra key columns, explain the visibility map and heap fetches, fix them, and compare with InnoDB.

1A Quick Recap of Covering Indexes

A normal index scan finds matching entries in the index and then visits the table, called the heap in PostgreSQL, to read the rest of each row. If the query needs only columns that are in the index, that second step is unnecessary in principle. An index that contains all the columns a query uses, in its WHERE clause, its SELECT list and its ORDER BY, is said to cover the query, and the plan becomes an Index Only Scan.

SQL
CREATE INDEX orders_cust_created ON orders (customer_id, created_at);
EXPLAIN SELECT customer_id, created_at FROM orders WHERE customer_id = 7;
-- Index Only Scan using orders_cust_created on orders -- covered
EXPLAIN SELECT customer_id, created_at, total FROM orders WHERE customer_id = 7;
-- Index Scan using orders_cust_created on orders -- total is not in the index

2INCLUDE Versus an Extra Key Column

To cover the second query we need total in the index. We can add it as a third key column, (customer_id, created_at, total), or since PostgreSQL 11 as an included column, (customer_id, created_at) INCLUDE (total). The difference is where the column lives and what it takes part in.

A key column is part of the sort order. It is stored in leaf entries and, as separator data, can also appear in inner pages, and it is part of any uniqueness check. An INCLUDE column is stored only in leaf entries as a payload. It does not affect the order, cannot be searched by the tree, and is ignored by UNIQUE. Because inner pages carry only key columns, they stay small, and the tree stays shallow.

SQL
-- a unique rule on (customer_id, order_no), and we want to read status without the table
CREATE UNIQUE INDEX orders_cust_no ON orders (customer_id, order_no) INCLUDE (status);
INSERT INTO orders (customer_id, order_no, status) VALUES (7, 1001, 'NEW'); -- ok
INSERT INTO orders (customer_id, order_no, status) VALUES (7, 1001, 'PAID'); -- ERROR: duplicate key
-- with (customer_id, order_no, status) as key columns, the second insert would have succeeded
Key column or INCLUDE column
PropertyKey columnINCLUDE column
Part of the sort orderyesno
Usable in WHERE seeks or ORDER BYyesno, only returned or filtered
Stored in inner pagescan be, as separatorsno, leaf entries only
Part of UNIQUE checkyesno
Makes the index coveringyesyes

That last point is the most useful one. If we added status as a key column to a unique index, the uniqueness would change to (customer_id, order_no, status), a different and wrong rule. INCLUDE lets us cover extra columns on a unique index without changing what is unique.

3Why PostgreSQL Still Visits the Table

PostgreSQL uses MVCC, multi-version concurrency control: an update writes a new version of a row and leaves the old one until vacuum removes it, and each transaction sees only the versions visible to its snapshot. The visibility information lives in the heap, not in the index. So an index entry alone cannot tell PostgreSQL whether the row it points to is visible to our transaction.

To make index-only scans possible, PostgreSQL keeps a visibility map: two bits per heap page, one of which says that every row on the page is visible to all transactions. For an entry pointing to an all-visible page, the scan trusts the index and skips the heap. For any other page, it must fetch the heap page to check visibility. Each such visit is counted as a heap fetch.

Step throughOne entry in an Index Only Scan
  1. Read the index entryhas all needed columns
  2. Is the row's heap page all-visible in the visibility map?
    YesReturn values from the indexNoFetch the heap page
  3. Check the row version's visibilitycounts as one heap fetch
  4. Visible to our snapshot?
    YesReturn valuesNoSkip the entry

4 steps in order. Play them, or step through them one at a time.

4Reading Heap Fetches: 90000

When EXPLAIN ANALYZE shows Index Only Scan with Heap Fetches: 90000, it means 90,000 entries pointed to pages not marked all-visible, so PostgreSQL visited the table for each of them. The plan is called index-only, but it behaved much like a normal index scan.

The usual cause is a frequently updated table where vacuum has not run recently. Every update or delete clears the all-visible bit for its page, and only VACUUM sets it again. On a busy table with default autovacuum settings, a large share of pages can be not-all-visible at any moment. Insert-only tables had the same problem in older versions, because nothing triggered vacuum; since PostgreSQL 13, autovacuum also runs after many inserts.

SQL
EXPLAIN (ANALYZE, BUFFERS) SELECT customer_id, created_at FROM orders WHERE customer_id = 7;
-- Index Only Scan using orders_cust_created on orders (actual rows=95000)
-- Heap Fetches: 90000
-- Buffers: shared hit=1210 read=61400
VACUUM (VERBOSE) orders; -- sets all-visible bits again
-- same query after vacuum
-- Heap Fetches: 0
-- Buffers: shared hit=412
ALTER TABLE orders SET (autovacuum_vacuum_scale_factor = 0.02); -- vacuum this table more often

The buffer numbers show the real effect: from more than 60,000 pages read to a few hundred. For a table that should support index-only scans, we tune autovacuum to run more often on it, so the visibility map stays mostly set.

5How InnoDB Does It

InnoDB has a related rule. Each secondary index page records the newest transaction that changed it, and only when that is too recent for our snapshot does InnoDB look up the clustered row to check visibility, so on stable data covered reads stay in the index. Its secondary indexes also have a useful property: every entry stores the primary key. So a query that uses only the indexed columns plus the primary key is covered automatically. MySQL's EXPLAIN shows this as Using index in the Extra column.

SQL
-- InnoDB, primary key id, secondary index on (customer_id, created_at)
EXPLAIN SELECT id, customer_id, created_at FROM orders WHERE customer_id = 7;
-- type: ref, key: idx_cust_created, Extra: Using index -- covered, id comes for free

MySQL has no INCLUDE clause, so covering extra columns means adding them as key columns at the end of the index.

6Follow-up questions

Why is a very wide INCLUDE list a bad idea?

Every included column makes each leaf entry bigger, so fewer entries fit per page, the index grows, and scans read more pages; at the extreme the index becomes a second copy of the table. Each included column is maintained on writes, and in PostgreSQL an update that changes an included column cannot be a HOT update, so a frequently updated column in INCLUDE slows writes. There is also a size limit per index entry, roughly a third of a page, so wide text columns can make inserts fail. Include only the few small columns a hot query needs.

Can an INCLUDE column be used in a WHERE clause?

It can be checked as a filter on index entries during an index-only scan, but it cannot be used to seek, because it is not part of the sort order. If we search by it, it belongs in the key.

How do we see how much of a table is all-visible?

pg_class.relallvisible compared with relpages gives the share of pages marked all-visible. The pg_visibility extension shows it per page if we need detail.

7Conclusion

In this answer, we saw that a covering index contains every column a query needs, so it can be answered from the index. PostgreSQL's INCLUDE stores columns only in leaf entries, outside the sort order and uniqueness check, keeping inner pages small and letting unique indexes cover extra columns. Index-only scans still visit the heap for pages not marked all-visible in the visibility map, so a large Heap Fetches count means vacuum is behind on a frequently updated table, which VACUUM and tighter autovacuum settings fix. In InnoDB every secondary index carries the primary key, shown as Using index when covered.

What to say in the interviewAbout 90 seconds aloud

Open with

A covering index is one that contains every column the query needs, in the WHERE clause, the select list and the ORDER BY, so the database can answer from the index without reading table rows, which PostgreSQL shows as an Index Only Scan.

Then, if they want more

In PostgreSQL there are two ways to add a column for covering. As a key column it becomes part of the sort order and of any uniqueness rule.

With INCLUDE, available since version 11, it is stored only in the leaf entries as payload, so it is not sorted, cannot be used to seek, is not part of the unique check, and keeps inner pages smaller. That is especially useful on a unique index, where adding a key column would change what is unique.

The Heap Fetches number comes from MVCC. Visibility is stored in the heap, so PostgreSQL can skip the table only for pages marked all-visible in the visibility map. Every update or delete clears that bit, and only vacuum sets it again.

So 90,000 heap fetches means many pages were not all-visible, usually because a frequently updated table has not been vacuumed recently. Running VACUUM fixes it, and I would tune autovacuum to run more often on that table.

In InnoDB, every secondary index stores the primary key, so a query using only the indexed columns plus the key is covered, shown as Using index.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 11, PostgreSQL 13
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a covering index, and how does PostgreSQL's INCLUDE clause differ from adding...": https://scrivio-interview-library.netlify.app/databases/indexing/30a3cf3a98/