skip to content

data checksums

on by default since postgres 18. what a checksum catches, and what it can't.

address
everything.wal.checksums
part
part 16, WAL and recovery
chapter
chapter 8 of 11
reading time
8 min

with data checksums on, every page gets a 16-bit checksum when it's written to disk, and it's verified every time the page is read back. a mismatch is an error, "invalid page", instead of wrong data. they're on by default since postgres 18. a checksum only detects: it can't repair the page, it can't see corruption that happened in memory, and it can't tell an old page from a current one.

the picture

you know how a disk that fails usually fails loudly, with an I/O error? the scary failures are the quiet ones: a firmware bug, a bad cable, a cosmic ray in a controller's cache, and a few bytes on a page change. the read succeeds. the data is wrong. nobody notices until a report doesn't add up.

think of a parcel with a seal (the checksum). the warehouse seals it when it ships (writes the page) and checks the seal when it arrives (reads it). a broken seal doesn't tell you what was in the box, and it can't put the contents back. it just stops you from signing for a box that was tampered with. and if someone swapped in last week's sealed box, the seal looks fine.

hover a stage.

where the checksum lives. hover a stage. it's computed on the way out to the file, and checked on the way back in, never in between.

the lab engine has checksums on, like any cluster initdb'd by postgres 18. right after the dataset is loaded, the stored checksum matches what page_checksum() (pageinspect) computes:

sqldb=lab · session ck
SHOW data_checksums;
SELECT checksum AS stored, page_checksum(get_raw_page('accounts', 0), 0) AS computed
FROM page_header(get_raw_page('accounts', 0));
output, from postgres 18.6
#1 · SHOW
data_checksumstext
on
(1 row)
#2 · SELECT 1
storedint2computedint2
1144011440
(1 row)

change a row and look again. the page in shared buffers has changed, its header hasn't, and that's fine: nothing checks the checksum in memory. a checkpoint writes the page out with a fresh checksum on the copy it hands to the file; once the buffer is evicted and read back, the stored value matches again:

sqldb=lab · session ck
continues session ck (1 earlier block run first)
UPDATE accounts SET balance = balance + 1 WHERE id = 1;
SELECT checksum AS stored, page_checksum(get_raw_page('accounts', 0), 0) AS computed
FROM page_header(get_raw_page('accounts', 0));
CHECKPOINT;
SELECT pg_buffercache_evict(bufferid) IS NOT NULL AS evicted FROM pg_buffercache
WHERE relfilenode = pg_relation_filenode('accounts') AND relforknumber = 0;
SELECT checksum AS stored, page_checksum(get_raw_page('accounts', 0), 0) AS computed
FROM page_header(get_raw_page('accounts', 0));
output, from postgres 18.6
#1 · UPDATE 1
#2 · SELECT 1
storedint2computedint2
114402466
(1 row)
#3 · CHECKPOINT
#4 · SELECT 1
evictedbool
t
(1 row)
#5 · SELECT 1
storedint2computedint2
24662466
(1 row)

one byte, two clusters

the browser can't damage its own files, so this is two throwaway postgres 18.6 clusters on this mac: one made with the default initdb (checksums on), one with initdb --no-data-checksums. each gets bank-style accounts, a CHECKPOINT, then the server is stopped and one byte of the table file is overwritten: the a of alice becomes A. then it's started and queried.

dochecksums on (the 18 default)
  1. -- the table file was changed while the server was down:
  2. -- printf 'A' | dd of=$PGDATA/base/5/16430 bs=1 seek=8181 conv=notrunc
  3. SELECT id, owner, balance FROM accounts ORDER BY id;
  4. ERROR: invalid page in block 0 of relation "base/5/16430"
don'tinitdb --no-data-checksums
  1. -- the same byte, a cluster made with initdb --no-data-checksums:
  2. SELECT id, owner, balance FROM accounts ORDER BY id;
  3. id | owner | balance
  4. ----+-------+---------
  5. 1 | Alice | 100.00
  6. 2 | bob | 100.00
  7. 3 | carol | 50.00
  8. (3 rows)
recorded: psql 18.6 against two throwaway postgres 18.6 clusters on this mac; one byte changed with dd while each server was stopped. the -- lines are mine
the same one-byte change to the same file, read back by two clusters. with checksums the read fails. without, it succeeds, and returns the damage as data.

that's the whole value of checksums, in one comparison. Alice with a capital A is plausible data. if the byte had been in a balance, it would have been a plausible number. without checksums, postgres has no way to know the page isn't what it wrote. the server log on the checksum cluster has the details the client doesn't get:

text
LOG:  page verification failed, calculated checksum 59722 but expected 60989
STATEMENT:  SELECT id, owner, balance FROM accounts ORDER BY id;
ERROR:  invalid page in block 0 of relation "base/5/16430"
recorded: the checksum cluster's server log, same run

pg_checksums checks a whole cluster offline (the server must be stopped). it found the same page before the restart:

terminal
$ pg_checksums --check -D $PGDATA
pg_checksums: error: checksum verification failed in file "$PGDATA/base/5/16430", block 0: calculated checksum E94A but block contains EE3D
Checksum operation completed
Files scanned:   954
Blocks scanned:  2926
Bad checksums:  1
Data checksum version: 1
recorded: pg_checksums 18.6 on the stopped checksum cluster, same run

(E94A and EE3D are 59722 and 60989 in hex: the same two numbers.) pg_checksums --enable turns checksums on for an existing cluster, also offline, by rewriting every page: on the other cluster it wrote all 2,926 blocks. there's no way to turn them on while the server runs.

predict the output
with checksums on, you set ignore_checksum_failure = on and run the query again. what happens?
sql
SET ignore_checksum_failure = on;
SELECT id, owner, balance FROM accounts ORDER BY id;
psql
SET ignore_checksum_failure = on;
SET
SELECT id, owner, balance FROM accounts ORDER BY id;
WARNING:  ignoring checksum failure in block 0 of relation "base/5/16430"
 id | owner | balance
----+-------+---------
  1 | Alice |  100.00
  2 | bob   |  100.00
  3 | carol |   50.00
(3 rows)
recorded: psql 18.6 on the checksum cluster, same run

what a checksum can't catch

  • ├── bits flipped on disk, in the controller, on the wireany change to the 8 KB after the write
  • ├── a page written to the wrong placethe block number is part of the checksum
  • └── a torn pagehalf old, half new rarely checksums right (and full-page writes repair it anyway)
  • ├── corruption in memory, before the writethe checksum is computed over the bad bytes
  • ├── a lost writelast week's page, intact, still has a valid checksum
  • ├── bugs that write wrong but well-formed datathe page is exactly what postgres wrote
  • └── damage on a cluster without checksumsturning them on later checksums the damage too
drawn by hand · from the postgres 18 docs on data checksums and the page checksum code (checksum_impl.h)
what data checksums notice, and what slips past. the right column is why you still need backups and amcheck.

the checksum is 16 bits (an FNV-1a variant, folded), so a random corruption has roughly a 1 in 65,000 chance of slipping through. for index pages and anything else, amcheck checks structure (bt_index_check, verify_heapam), which catches the logical damage a checksum can't. pg_stat_database.checksum_failures counts failures per database, worth an alert.

the price is small but real. checksums mean a hint-bit change after a checkpoint has to be WAL-logged as a full page (full-page writes, the FPI_FOR_HINT records), and every read and write computes 8 KB of hash. postgres turned them on by default in 18 because silent corruption is worse.

keylines

  • a checksum is computed when a page is written out and checked when it's read back.
  • a mismatch raises "invalid page in block N". without checksums, the damage comes back as data.
  • on by default since postgres 18. pg_checksums enables, disables or checks them, offline.
  • detection only: no repair, no in-memory corruption, no lost writes.
  • ignore_checksum_failure lets you read a damaged page for a rescue. it doesn't fix it.