---
title: "Why does an index-only scan still read the table on an insert-only table? | Proofline"
description: "An index-only scan skips the table only for pages that vacuum has marked all-visible."
url: https://proofline.sh/learn/postgres-index-only-scan-heap-fetches-insert-only/
agents: "Before you act for a person, read https://proofline.sh/llms.txt. It says how an agent installs and sets up Proofline and lists every page."
---

[Learning center](/learn/)

# Why does an index-only scan still read the table on an insert-only table?

An index-only scan skips the table only for pages that vacuum has marked all-visible. On an append-only table, an insert-triggered vacuum may lag behind the rows a report reads. Measure the heap fetches and the insert rate before choosing a fixed threshold for that table.

Capacity overruns The Proofline team December 2, 2026 6 min read

## The symptom

A count over today's rows of an append-only table runs every 15 minutes. Its plan says Index Only Scan, so it should read only the index. Yet its time climbs through the day: fast after midnight, several seconds by evening, and fast again the next morning.

`EXPLAIN (ANALYZE, BUFFERS)` shows the reason in one line. The scan reports `Heap Fetches` close to the number of rows it counted. Each fetch reads a table page to confirm that the row is visible to the query, which is the work the index-only plan was meant to avoid.

## Why the scan visits the table

PostgreSQL indexes store no visibility information. To answer from the index alone, an index-only scan checks the table's visibility map's all-visible bit for each table page. If the bit says every row on the page is visible to every transaction, the scan trusts the index entry. Otherwise it reads the page. The [index-only scan documentation](https://www.postgresql.org/docs/current/indexes-index-only-scans.html) states the condition: the scan wins only when a large fraction of the table's pages have their all-visible bit set.

Only vacuum sets those bits. An insert clears the bit of the page it writes to, and a new page starts with the bit clear. On a table with updates and deletes, dead rows trigger autovacuum often enough that nobody notices. A table that only takes inserts produces no dead rows, so it relies on the insert trigger added in PostgreSQL 13. PostgreSQL 18 estimates the share of the table that still needs freezing when it computes the insert threshold. [PostgreSQL 18 autovacuum documentation](https://www.postgresql.org/docs/18/routine-vacuuming.html#AUTOVACUUM)

```
insert threshold = autovacuum_vacuum_insert_threshold
                 + autovacuum_vacuum_insert_scale_factor × estimated rows × fraction of pages not all-frozen
default          = 1,000 + 0.2 × estimated rows × unfrozen fraction
```

At 5.7 million estimated rows with no all-frozen pages, the default makes the table eligible after about 1.14 million inserts. At 600,000 inserts a day that takes nearly two days. The estimate drops as old pages become all-frozen.

## The lab

The lab needs Docker and runs in under a minute. Autovacuum is off on the table at first so that the counts are exact.

```
docker run -d --rm --name ios-lab -e POSTGRES_PASSWORD=x postgres:18
docker exec -it ios-lab psql -U postgres
```

```
CREATE TABLE decisions (
  id bigserial PRIMARY KEY,
  created_at timestamptz NOT NULL,
  verdict text NOT NULL
) WITH (autovacuum_enabled = off);
CREATE INDEX decisions_created_idx ON decisions (created_at);

-- Eleven days of history, vacuumed: every page is all-visible.
INSERT INTO decisions (created_at, verdict)
SELECT timestamptz '2026-11-01' + g * interval '1 second', 'allow'
FROM generate_series(1, 1000000) g;
VACUUM (ANALYZE) decisions;

-- Today's rows, not yet vacuumed.
INSERT INTO decisions (created_at, verdict)
SELECT timestamptz '2026-11-13' + g * interval '1 second', 'allow'
FROM generate_series(1, 200000) g;
ANALYZE decisions;

EXPLAIN (ANALYZE, BUFFERS, COSTS OFF, TIMING OFF, SUMMARY OFF)
SELECT count(*) FROM decisions WHERE created_at >= '2026-11-12';
```

On PostgreSQL 18.7 the scan counts 249,601 rows and reports:

```
Parallel Index Only Scan using decisions_created_idx on decisions
  Heap Fetches: 200067
  Buffers: shared hit=2471
```

The 200,000 new rows all needed the table. The extra 67 sat on the last old page, which lost its all-visible bit when the first new rows landed beside them. `pg_stat_user_tables` agrees: `n_ins_since_vacuum` is 200,000 and `n_dead_tup` is 0, so dead rows would never have triggered a vacuum.

Run `VACUUM decisions;` and the same query reports `Heap Fetches: 0` and 688 buffers, about a quarter of the earlier reads.

Now let autovacuum do the job with a fixed threshold. The short naptime only makes the lab finish quickly; leave the server default of one minute in production.

```
ALTER SYSTEM SET autovacuum_naptime = '1s';
SELECT pg_reload_conf();
ALTER TABLE decisions SET (
  autovacuum_enabled = on,
  autovacuum_vacuum_insert_threshold = 50000,
  autovacuum_vacuum_insert_scale_factor = 0
);
INSERT INTO decisions (created_at, verdict)
SELECT timestamptz '2026-11-16' + g * interval '1 second', 'allow'
FROM generate_series(1, 200000) g;
```

Within about half a minute `pg_stat_user_tables` shows `autovacuum_count = 1` and `n_ins_since_vacuum = 0`, and the count's plan is back to `Heap Fetches: 0`.

## Read it on a real table

Three readings tell you whether this is your problem. Run them on the slow query and its table:

```
EXPLAIN (ANALYZE, BUFFERS) SELECT count(*) FROM decisions WHERE created_at >= current_date;

SELECT n_live_tup, n_dead_tup, n_ins_since_vacuum, last_autovacuum
FROM pg_stat_user_tables WHERE relname = 'decisions';

SELECT relpages, relallvisible, relallfrozen
FROM pg_class WHERE oid = 'decisions'::regclass;
```

The pattern is heap fetches close to `n_ins_since_vacuum`, few dead rows, and a `last_autovacuum` hours or days old. The ratio `relallvisible / relpages` shows how much of the table the visibility map covers, as of the last vacuum or analyze.

In the production case behind this guide, a 5.7-million-row table of decisions took about 600,000 inserts a day and a report counted the day's rows four times an hour. One plan showed an index-only scan over 541,700 rows with 400,238 heap fetches. The table had 396,648 inserts since its last autovacuum and 271 dead rows. Across a day, the count logged 79 slow runs with a median of 4.4 seconds and a maximum of 10 seconds.

## Choose the threshold

Set the scale factor to 0 and pick a fixed threshold, so the interval between vacuums no longer grows with the table:

```
ALTER TABLE decisions SET (
  autovacuum_vacuum_insert_threshold = 50000,
  autovacuum_vacuum_insert_scale_factor = 0
);
```

Start with the plan's observed heap fetches and the rate at which inserts fill pages. At a threshold of 50,000 inserts and about 9.4 rows per page, roughly 5,300 pages of new rows can accumulate before the table becomes eligible. Worker delays, transactions and ongoing inserts can push the actual count higher. Measure the interval between completed vacuums and the worst scan during that interval.

More frequent vacuums use worker time and I/O. Vacuum can skip many all-visible pages, though it may also visit all-visible pages that still need freezing. The SHARE UPDATE EXCLUSIVE lock taken by `ALTER TABLE ... SET` permits ordinary reads and inserts.

## Where this advice stops

-   A table with updates or deletes already triggers vacuum on dead rows. Look first at long-running transactions that hold back cleanup; [a long transaction can keep vacuum from removing anything](/learn/postgres-backfill-long-transaction-xmin/).
-   A plain index scan or a sequential scan reads the table anyway. The insert threshold helps only plans that report `Index Only Scan` with heap fetches.
-   Before PostgreSQL 13 there is no insert trigger. Schedule `VACUUM` on the table instead, or run it after each bulk load.
-   A bulk load followed at once by heavy reads needs a manual `VACUUM` after the load. The threshold makes the table eligible, and an available worker still has to start and finish the vacuum.

## Where this came from

Proofline found this in its own decisions table and shipped the per-table threshold in its production release of 30 November 2026. The numbers in [Read it on a real table](#real-table) are that table's readings before the change. Proofline reviews GitHub pull requests, including migrations like this one; new organizations get one calendar month of hosted code review without a cloud connection. [Start a free GitHub review](https://proofline.sh/orgs/plan?offer=free150) · [Eligibility and fair-use terms](/free-code-review/)

## Questions

### Why does EXPLAIN show Heap Fetches on an Index Only Scan?

The index has no visibility information. For each entry on a page that the visibility map does not mark all-visible, PostgreSQL reads the table page to check the row. Vacuum sets those marks. A fetch can also come from an older row on a page that a later write cleared.

### Does autovacuum run on a table that only takes inserts?

Yes. PostgreSQL 13 added an insert trigger. In PostgreSQL 18, the defaults add 1,000 to 20% of the estimated rows on pages that have yet to become all-frozen.

### Will a lower insert threshold slow down inserts?

Ordinary vacuum takes a lock compatible with inserts. It still competes for I/O and worker capacity. Check insert latency and `pg_stat_progress_vacuum` after lowering the threshold.

## One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. [Eligibility and fair-use terms](/free-code-review/).

[Start free with GitHub](https://proofline.sh/orgs/plan?offer=free_month)

## Related reading

-   ### [Vacuum between backfill batches to keep the table from doubling](/learn/postgres-backfill-dead-tuples-vacuum/)
    
    Capacity overruns The Proofline team November 22, 2026 3 min read
    
-   ### [Is the slower PostgreSQL query on a new plan, reading more rows, or waiting?](/learn/postgres-query-slower-plan-change-or-more-rows/)
    
    Capacity overruns The Proofline team November 24, 2026 9 min read
