skip to content

Right after a bulk load into a PostgreSQL table, why does the first full scan dirty pages and generate WAL while later scans do not?

level: seniorimportance: should knowfreq 32%

answer

  1. the header names who, not whether
  2. a cache written into the tuple
  3. the first reader pays
  4. checksums turn it into WAL

basics

~20 s

Freshly loaded tuples carry only the loader's XID. The first reader checks pg_xact and caches the commit status as hint bits in each tuple header, dirtying every page; with data checksums on, PostgreSQL 18's initdb default, that also writes full-page images to WAL.

solid answer

~50 s

A tuple header stores the creating XID in `xmin`, not whether that transaction committed. The status lives in `pg_xact`, two bits per transaction. To save repeated lookups, the first backend that resolves a status sets **hint bits** in the tuple header, for example 'xmin committed', and marks the page dirty. After a bulk load no tuple has hints yet, so the first scan touches every page that way; those pages are written out later by the checkpointer, the background writer or the scanning backend itself. With data checksums on, or `wal_log_hints = on`, the first hint-bit change to a page after each checkpoint also writes a full-page image to WAL. Since PostgreSQL 18, `initdb` enables checksums by default, so new clusters see that WAL unless created with `--no-data-checksums`. Running `VACUUM` after the load sets the hints in one controlled pass.

code

sql · 13 lines
sql
INSERT INTO order_import_line (sku, qty)
SELECT 'SKU-' || g, g % 10 + 1
FROM generate_series(1, 2000000) AS g;
CHECKPOINT;

-- first scan: resolves xmin through pg_xact and sets hint bits
EXPLAIN (ANALYZE, BUFFERS, WAL) SELECT count(*) FROM order_import_line;
--   Buffers: shared ... dirtied=...   WAL: ... fpi=...

-- second scan: nothing dirtied, no WAL
EXPLAIN (ANALYZE, BUFFERS, WAL) SELECT count(*) FROM order_import_line;

SHOW data_checksums;  -- on for a default PostgreSQL 18 initdb

go deeper

for a junior

Remember that a tuple stores its creator's transaction ID, and that PostgreSQL looks the commit status up in pg_xact and caches it on the row.

for a middle

Explain hint bits as a cache in the tuple header, why setting them dirties pages during a read, and why the second scan is clean.

for a senior

Connect it to production: checksums on by default in 18 turn hint setting into WAL, standbys write their own hints, and a VACUUM after loads makes the cost predictable.

for a principal

Weigh checksum protection against the extra WAL and I/O it brings to read-heavy, load-heavy workloads, and plan archive and replica capacity for it.

## Where commit status lives Every tuple records **who** created it (`xmin`) and who deleted or locked it (`xmax`), as transaction IDs. It does not record **whether** those transactions committed. That answer lives in `pg_xact`, the commit log: two bits per transaction, meaning in progress, committed, aborted or sub-committed. `pg_xact` is read through a small shared-memory cache that `pg_stat_slru` reports as the `transaction` row; since PostgreSQL 17 its size is the `transaction_buffers` setting, which needs a restart to change. ## What hint bits are Looking up `pg_xact` for every tuple of every scan would be wasteful, so PostgreSQL caches the answer **in the tuple header itself**. Four flags exist: xmin committed, xmin aborted, xmax committed and xmax aborted. The rules: - A hint is set only once the transaction has **finished**; an in-progress XID is never hinted. - With **asynchronous commit**, a 'committed' hint is held back until the commit record's WAL is flushed, so a crash cannot leave a hint claiming a commit that was lost. - A hint is a **cache**. Losing one is harmless; the next reader simply looks `pg_xact` up again. - A hint says *committed*, not *visible to you*. A reader still compares `xmin` with its snapshot unless the tuple is frozen. Setting a hint does not go through the normal logged-change path. It marks the buffer dirty as a non-critical change, which is why a pure read can create disk writes. ## Why the first scan writes and the second does not 1. A bulk load commits millions of tuples whose `xmin` is the loader's XID, with no hints, because the loader was still running while it wrote them. 2. The first full scan reaches each tuple, finds no hint, checks the snapshot, consults `pg_xact`, learns the loader committed, and sets 'xmin committed'. 3. Every page touched that way becomes dirty. A large sequential scan runs through a small ring of buffers, so the scanning backend often has to write those pages itself before reusing ring slots; otherwise the checkpointer or background writer does it. 4. If **data checksums** are on, changing a hint alters the page checksum, and a torn write could leave a page whose checksum fails. So the first hint-bit change to a page after each checkpoint writes a **full-page image** to WAL. `wal_log_hints = on` forces the same logging without checksums; it is off by default and is ignored when checksums are on. 5. The second scan normally finds the hints in place, so it skips the `pg_xact` lookups, dirties nothing and writes no WAL. ## What changed in PostgreSQL 18 | Cluster created by | Data checksums | Hint-bit full-page images in WAL | |---|---|---| | `initdb` in 18, default | On | Yes | | `initdb` in 18 with `--no-data-checksums` | Off | Only if `wal_log_hints = on` | | `initdb` in 17 or earlier, default | Off | Only if `wal_log_hints = on` | `SHOW data_checksums;` tells you which case you are in. The setting itself, and why a cluster should keep it, are durability topics. ## Standbys write too The manual notes that hint bits set on the primary are not WAL-logged as such, so a hot standby re-sets them as its own readers resolve statuses. A standby serving only reads still writes data pages. ## Making the cost predictable - **Run `VACUUM` after a bulk load.** It sets the hints, and marks pages all-visible, in one planned pass instead of on the first unlucky user query; vacuum behaviour is its own topic. - **`COPY ... FREEZE`** loads rows already frozen when the table was created or truncated earlier in the same transaction and no older snapshot is held, so no later hint pass is needed. - **Benchmark honestly.** Warm-up reads after a load measure hint-setting, not query speed. `EXPLAIN (ANALYZE, BUFFERS, WAL)` shows the `dirtied` buffers and the WAL full-page images on the first run. - **Budget WAL.** On checksummed clusters, the first read after a load can generate WAL comparable to the table size, which matters for archiving and replicas. ## How it shows up in an incident The typical report is that a nightly load finishes quickly but the first morning report runs several times slower than usual, the disks show heavy writes during a read-only workload, and WAL archiving briefly falls behind. Nothing is wrong with the plan or the indexes. The cost was simply deferred from the load to the first reader. The cure is to move it back to a moment you choose: a `VACUUM` at the end of the load job, before users arrive.

  • In PostgreSQL, does a hint bit saying xmin committed let a reader skip the snapshot check?
    No. The hint caches the commit status from `pg_xact`, not visibility for a particular snapshot. A `REPEATABLE READ` transaction whose snapshot predates the load must still treat those rows as invisible, so it compares `xmin` with its snapshot first. Only a frozen tuple skips that comparison.
  • Why does PostgreSQL delay setting a committed hint bit under synchronous_commit = off?
    With asynchronous commit the commit record may not yet be flushed to WAL. If the page carrying a 'committed' hint reached disk first and the server then crashed, the hint would claim a commit that recovery never replays. So the hint is held back until the commit's WAL is flushed, unless the page's own LSN already covers it.

  • A SELECT never writes to data files, so first-scan writes must be autovacuum.
  • Every hint-bit change writes its own WAL record, just as an UPDATE does.
  • The tuple header records whether the inserting transaction committed when the row is written.
  • A tuple with the xmin-committed hint is visible to every snapshot.
  • wal_log_hints must be turned on to get hint-bit WAL on a checksummed cluster.

Assumes PostgreSQL 18, where initdb enables data checksums by default, so hint-bit full-page WAL appears on new clusters; clusters initialized by 17 or earlier default to checksums off.