Skip to content
Engineer
Menu
Design
English

Engineer / Glossary

Index-only scan

a query scan that takes its column values from the index

What it is

An index-only scan is a way for a database to produce query results using values already stored in an index. A query might ask for the timestamp and reading for one customer’s meter over a date range. If the index carries the filter keys and both values being returned, a scan can use that structure to find the relevant entries and supply their data to the rest of the query plan.

In PostgreSQL, an index method must be able to return the original values the query needs. A B-tree index can do so. The query must also be satisfiable from columns available in that index. A covering index can keep extra output values as included columns so a common query can retrieve them without turning each of those values into a search key. An included value is stored with the index entry and contributes to the size of that structure.

PostgreSQL also has to check whether each candidate row is visible to the current transaction. Its indexes do not store all of that visibility information. A visibility map tracks heap pages that the database can treat as visible to all relevant transactions. When the map confirms a page is all-visible, the scan can use the index values to return those entries. If it cannot confirm visibility that way, the execution may visit the table. A plan labelled Index Only Scan can therefore report heap fetches. The plan and the measured activity answer different questions.

Why it matters

A query that visits a table for many matching entries performs work beyond the search through the index. If the table pages are already in memory, that cost may be modest. If the working set is larger than available memory or matches are spread across many pages, repeated access to the table can add substantial time. A query that can read the necessary values from an index can have a smaller working set and leave more room for other work.

The improvement depends on the workload. A historical table whose old rows change rarely can keep many pages all-visible. Rows in an active work queue may be updated at every transition, which can require a table visit even when the index covers the selected columns. A single convenient index definition cannot tell you how often each set of rows is visited or updated. Those measurements matter when deciding how much extra data to keep in the index.

A wider covering index takes space, work to maintain, and time when entries are inserted or updated. A report that runs occasionally might be best served by a narrower index already used across the application. A busy, stable range query can justify more specialised storage. Start with the response time and traffic the application needs, then weigh the total cost of reads and writes around the specific query.

What it looks like in practice

Consider a readings table searched by tenant and time interval. An illustrative B-tree index orders entries by tenant and recorded time and includes the amount returned by a particular report. The report filters with the same keys and selects only columns the index can supply. Inspect an actual execution plan on a dataset that resembles production. Record the scan type, number of rows, buffer activity and heap fetches as well as elapsed time.

Run the same scan against recent rows that are still changing and a mostly historical date range. A small number of heap fetches on the second range and many on the first can tell you more than the plan’s name alone. Repeat enough runs to separate cache effects from changes to the query. PostgreSQL’s estimated cost is useful for following the planner’s choice. The measured result helps decide whether the chosen index improves the behaviour that matters to users.

Engineer’s PostgreSQL performance case study documents targeted composite indexes for tenant and date filters, along with execution-plan analysis and other query and storage changes. Those are useful starting points for the narrower example here. The covering-index exercise extends the same measurement discipline to the values returned by one report. It asks whether storing one more value changes heap access and response time enough to justify its own maintenance cost.

Where it goes wrong

A frequent mistake is to add every column the application sometimes returns. The resulting index can occupy significantly more space and require extra work for writes. It can also exceed size limits when an included value is large. Take one important query, identify the small set of values callers need, and make the expected improvement measurable. Retain the narrower index if it serves more important work more effectively after the change.

Coverage can disappear as the query changes. An added output column, a new filtering expression or a join that reads more data can change the plan. A compact access path can also become less attractive when a later filter matches much of the table. Exercise the shapes and parameter ranges the application’s callers actually use, and keep an execution-plan example with the decision when the performance improvement is important.

Finally, a measurement taken just after maintenance may describe a favourable state that the live workload rarely reaches. Changes can clear visibility information, and normal maintenance needs time to confirm pages again. Test the workload over an interval that includes ordinary updates and maintenance cycles. Review buffer use and heap fetches alongside the end-user latency. The index-only scan is a means of reducing specific access work. Its value depends on how often that work is avoided in the application that uses it.

Further reading

Contact

Still working out what you need?

Tell us what is going wrong, or what you are trying to build. We will tell you what it would take, roughly what it would cost, and whether we are the right people for it.

From 1,400 DKK an hour, excluding VAT — and less per hour on a longer booking.

welcome@engineer.company +45 50 10 61 84 Copenhagen, Denmark