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.
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:
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));| data_checksumstext |
|---|
| on |
| storedint2 | computedint2 |
|---|---|
| 11440 | 11440 |
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:
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));| storedint2 | computedint2 |
|---|---|
| 11440 | 2466 |
| evictedbool |
|---|
| t |
| storedint2 | computedint2 |
|---|---|
| 2466 | 2466 |
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.
- -- the table file was changed while the server was down:
- -- printf 'A' | dd of=$PGDATA/base/5/16430 bs=1 seek=8181 conv=notrunc
- SELECT id, owner, balance FROM accounts ORDER BY id;
- ERROR: invalid page in block 0 of relation "base/5/16430"
- -- the same byte, a cluster made with initdb --no-data-checksums:
- SELECT id, owner, balance FROM accounts ORDER BY id;
- id | owner | balance
- ----+-------+---------
- 1 | Alice | 100.00
- 2 | bob | 100.00
- 3 | carol | 50.00
- (3 rows)
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:
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"pg_checksums checks a whole cluster offline (the server must be stopped). it found the same page before the restart:
$ 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(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.
SET ignore_checksum_failure = on;
SELECT id, owner, balance FROM accounts ORDER BY id;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)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
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.