{"canonical_url":"https://engineer.company/glossary/index-only-scan/","content_markdown":"# Index-only scan\n\n## What it is\n\nAn index-only scan is a way for a database to produce query results using values\nalready stored in an index. A query might ask for the timestamp and reading for\none customer's meter over a date range. If the index carries the filter keys and\nboth values being returned, a scan can use that structure to find the relevant\nentries and supply their data to the rest of the query plan.\n\nIn PostgreSQL, an index method must be able to return the original values the\nquery needs. A B-tree index can do so. The query must also be satisfiable from\ncolumns available in that index. A covering index can keep extra output values\nas included columns so a common query can retrieve them without turning each of\nthose values into a search key. An included value is stored with the index entry\nand contributes to the size of that structure.\n\nPostgreSQL also has to check whether each candidate row is visible to the\ncurrent transaction. Its indexes do not store all of that visibility\ninformation. A visibility map tracks heap pages that the database can treat as\nvisible to all relevant transactions. When the map confirms a page is\nall-visible, the scan can use the index values to return those entries. If it\ncannot confirm visibility that way, the execution may visit the table. A plan\nlabelled Index Only Scan can therefore report heap fetches. The plan and the\nmeasured activity answer different questions.\n\n## Why it matters\n\nA query that visits a table for many matching entries performs work beyond the\nsearch through the index. If the table pages are already in memory, that cost\nmay be modest. If the working set is larger than available memory or matches are\nspread across many pages, repeated access to the table can add substantial time.\nA query that can read the necessary values from an index can have a smaller\nworking set and leave more room for other work.\n\nThe improvement depends on the workload. A historical table whose old rows\nchange rarely can keep many pages all-visible. Rows in an active work queue may\nbe updated at every transition, which can require a table visit even when the\nindex covers the selected columns. A single convenient index definition cannot\ntell you how often each set of rows is visited or updated. Those measurements\nmatter when deciding how much extra data to keep in the index.\n\nA wider covering index takes space, work to maintain, and time when entries are\ninserted or updated. A report that runs occasionally might be best served by a\nnarrower index already used across the application. A busy, stable range query\ncan justify more specialised storage. Start with the response time and traffic\nthe application needs, then weigh the total cost of reads and writes around the\nspecific query.\n\n## What it looks like in practice\n\nConsider a readings table searched by tenant and time interval. An illustrative\nB-tree index orders entries by tenant and recorded time and includes the amount\nreturned by a particular report. The report filters with the same keys and\nselects only columns the index can supply. Inspect an actual execution plan on a\ndataset that resembles production. Record the scan type, number of rows, buffer\nactivity and heap fetches as well as elapsed time.\n\nRun the same scan against recent rows that are still changing and a mostly\nhistorical date range. A small number of heap fetches on the second range and\nmany on the first can tell you more than the plan's name alone. Repeat enough\nruns to separate cache effects from changes to the query. PostgreSQL's estimated\ncost is useful for following the planner's choice. The measured result helps\ndecide whether the chosen index improves the behaviour that matters to users.\n\nEngineer's\n[PostgreSQL performance case study](/portfolio/accelerated-postgresql-performance-by-10x/)\ndocuments targeted composite indexes for tenant and date filters, along with\nexecution-plan analysis and other query and storage changes. Those are useful\nstarting points for the narrower example here. The covering-index exercise\nextends the same measurement discipline to the values returned by one report. It\nasks whether storing one more value changes heap access and response time enough\nto justify its own maintenance cost.\n\n## Where it goes wrong\n\nA frequent mistake is to add every column the application sometimes returns. The\nresulting index can occupy significantly more space and require extra work for\nwrites. It can also exceed size limits when an included value is large. Take one\nimportant query, identify the small set of values callers need, and make the\nexpected improvement measurable. Retain the narrower index if it serves more\nimportant work more effectively after the change.\n\nCoverage can disappear as the query changes. An added output column, a new\nfiltering expression or a join that reads more data can change the plan. A\ncompact access path can also become less attractive when a later filter matches\nmuch of the table. Exercise the shapes and parameter ranges the application's\ncallers actually use, and keep an execution-plan example with the decision when\nthe performance improvement is important.\n\nFinally, a measurement taken just after maintenance may describe a favourable\nstate that the live workload rarely reaches. Changes can clear visibility\ninformation, and normal maintenance needs time to confirm pages again. Test the\nworkload over an interval that includes ordinary updates and maintenance cycles.\nReview buffer use and heap fetches alongside the end-user latency. The\nindex-only scan is a means of reducing specific access work. Its value depends\non how often that work is avoided in the application that uses it.\n\n## Further reading\n\n- [PostgreSQL index-only scans and covering indexes](https://www.postgresql.org/docs/18/indexes-index-only-scans.html).\n  Defines the requirements and visibility checks for the scan.\n- [PostgreSQL execution-plan analysis](https://www.postgresql.org/docs/18/using-explain.html).\n  Shows how to read estimated and measured plan activity.\n- [The PostgreSQL visibility map](https://www.postgresql.org/docs/18/storage-vm.html).\n  Explains how table-page visibility is recorded for scans and maintenance.\n\n\u003chttps://engineer.company/glossary/index-only-scan/\u003e\n","date_modified":"2026-11-25T07:18:46Z","description":"Check whether PostgreSQL can answer a query from its index and use heap fetches to verify how it behaved.","formats":{"gemtext":"https://engineer.company/glossary/index-only-scan/index.gmi","gopher":"https://engineer.company/glossary/index-only-scan/gophermap","html":"https://engineer.company/glossary/index-only-scan/","json":"https://engineer.company/glossary/index-only-scan/index.json","markdown":"https://engineer.company/glossary/index-only-scan/index.md","pdf":"https://engineer.company/glossary/index-only-scan/index.pdf"},"kind":"page","language":"en","title":"What is an index-only scan?","url":"https://engineer.company/glossary/index-only-scan/","version":1}