Sub topic 14
Which Index Types Support Index-Only Scans
Assume you know basic SELECT, B-tree indexes, and simple EXPLAIN plans. This chapter focuses on one question: can a particular PostgreSQL index access method return the columns needed by a query without reading the table heap?
Access method support
An index access method is the implementation behind an index type. B-tree, GiST, SP-GiST, Hash, GIN, and BRIN are access methods with different internal data.
An index-only scan requires two kinds of support:
- The access method must be able to return the requested indexed values.
- PostgreSQL must confirm tuple visibility using the visibility map, as described in earlier chapters.
The main differences are:
| Index type | Index-only scans | Main qualification |
|---|---|---|
| B-tree | Yes | Supported for all B-tree operator classes |
| GiST | Sometimes | Depends on the operator class |
| SP-GiST | Sometimes | Depends on the operator class |
| Hash | No | Stores hash values, not the original column values |
| GIN | No | Stores keys or fragments, not complete original values |
| BRIN | Generally no | Stores page summaries rather than row values |
B-tree is the dependable choice for ordinary covering indexes. Its entries contain the key values, and INCLUDE columns can add payload values that a query needs to return.
GiST and SP-GiST are more specialized. Their entries can use compressed or geometric representations. Some operator classes can reconstruct and return the indexed value, while others cannot. Therefore, the index type name alone is not enough to predict whether an index-only scan is possible.
Hash indexes store a hash derived from the indexed value. A hash value cannot be converted back into the original value, so a query selecting that column must visit the heap. GIN indexes have a similar limitation. They index individual keys, array elements, or tokens, rather than storing a complete value for every row.
BRIN indexes summarize ranges of table pages. Their summaries can identify candidate pages, but they do not contain each row's exact values. They are useful for reducing heap work on naturally ordered data, but they are not a general replacement for a covering B-tree index.
Inspecting an index
PostgreSQL exposes whether each index column is returnable through pg_index_column_has_property. This query lists the capability for every column in every index:
SELECT
c.oid::regclass AS index_name,
am.amname AS access_method,
column_number,
pg_index_column_has_property(
c.oid::regclass,
column_number,
'returnable'
) AS returnable
FROM pg_class AS c
JOIN pg_index AS i ON i.indexrelid = c.oid
JOIN pg_am AS am ON am.oid = c.relam
CROSS JOIN LATERAL generate_series(1, i.indnatts) AS columns(column_number)
WHERE c.relkind = 'i'
ORDER BY index_name, column_number;A true result means that the access method and the selected operator class can return that indexed column. It does not guarantee that the planner will choose an index-only scan. The query must also select only available columns, match the index conditions, and benefit from avoiding heap visits.
Choosing an index type
Use a B-tree when equality, range, ordering, uniqueness, or ordinary covering queries are the main needs. Choose GiST, SP-GiST, GIN, or BRIN for their specialized search behavior, not merely to avoid heap reads.
Adding payload columns to an unsupported index type does not change its access method. An INCLUDE clause can make a supported index cover more query columns, but it cannot make Hash or GIN entries return the original row values.
Exercise
A GIN index can find rows containing a search token. Why can it usually not answer SELECT tags FROM ... as an index-only scan?
Show answer
GIN stores searchable keys or fragments rather than each row's complete original tags value, so PostgreSQL must fetch the heap tuple.