Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
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.
| Property | Key column | INCLUDE column |
|---|---|---|
| Part of the sort order | yes | no |
| Usable in WHERE seeks or ORDER BY | yes | no, only returned or filtered |
| Stored in inner pages | can be, as separators | no, leaf entries only |
| Part of UNIQUE check | yes | no |
| Makes the index covering | yes | yes |
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.
- Read the index entryhas all needed columns
- Is the row's heap page all-visible in the visibility map?YesReturn values from the indexNoFetch the heap page
- Check the row version's visibilitycounts as one heap fetch
- 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.
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.
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.
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.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
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/