Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

How would you continuously verify data integrity across a fleet of PostgreSQL databases so corruption is found before users do?

We build several layers of checks that run all the time, so that no corruption depends on a user reading the bad row first. Every cluster has data checksums enabled, amcheck runs on a schedule against restored backups or replicas, every backup is restored and verified automatically, and critical data is reconciled at the application level, such as ledger totals against payment records. Checksum failures and failed checks raise alerts with a runbook attached.

Why this question is asked

Silent corruption is dangerous precisely because it is silent. A bad page can sit in a rarely read table for weeks, get copied into every backup, and only surface when a customer opens an old invoice. With a fleet of hundreds of databases, someone will eventually hit faulty hardware, a kernel bug or a careless operator, so the real question is how quickly we notice. Interviewers ask this to see whether we think in layers and in automation rather than one-off checks. In this answer, we will recap the kinds of corruption, then design each layer: checksums, amcheck, backup restore verification, application reconciliation, and finally alerting and runbooks.

1What We Are Trying to Catch

Corruption comes in different shapes, and each check sees only some of them. Physical corruption is a page whose bytes changed on disk, from bit rot, a bad controller or a torn write. Structural corruption is a page that reads fine but breaks the database's own rules, such as an index entry pointing at a row that does not exist, or an index ordered wrongly after a collation change in the operating system's C library. Logical corruption is data that is valid to the database but wrong for the business, such as an order whose lines do not add up to its total because of an application bug or a bad migration.

No single tool covers all three, which is why the design is a stack of layers.

Step throughLayers of integrity checking
  1. Data checksumsdetect changed bytes on every page read
  2. amcheck on scheduledetect broken index and heap structure
  3. Automated restore verificationprove backups work and scan every page
  4. Application reconciliationdetect business-level wrong data
  5. Alerts and runbooksturn a finding into a fast, rehearsed response

5 layers, from the top down. Step through them one at a time.

2Layer 1: Data Checksums on Every Cluster

With data checksums, PostgreSQL stores a checksum in every page header when it writes the page and verifies it whenever it reads the page from disk. A mismatch raises an error instead of returning wrong data, and the counter checksum_failures in pg_stat_database goes up. Checksums are chosen when the cluster is created with initdb --data-checksums. PostgreSQL 18 turned them on by default for new clusters, while older versions default to off, so check your version. An existing cluster can be converted with pg_checksums --enable, but only while it is stopped, so for large clusters we usually enable them on a new replica or during a planned upgrade.

The cost is a little CPU on reads and writes and somewhat more WAL, because hint bit changes are then logged; in most workloads it is a few percent, a price we accept fleet-wide.

Java
# new clusters
initdb --data-checksums -D /var/lib/postgresql/18/main
# existing cluster, server stopped
pg_checksums --enable -D /var/lib/postgresql/17/main
-- fleet inventory: every cluster must report on
SHOW data_checksums; -- on
SELECT datname, checksum_failures, checksum_last_failure FROM pg_stat_database;

Notice the limit of this layer: checksums only catch pages that are read. A damaged page in a cold table is not noticed until someone reads it, so the next layers read everything on purpose.

3Layer 2: amcheck, Run Where It Costs Nothing

The amcheck extension verifies structure. bt_index_check and bt_index_parent_check walk B-tree indexes and confirm ordering and links, and with heapallindexed they also confirm that every table row has its index entries. verify_heapam, added in PostgreSQL 14, checks table pages. The pg_amcheck command runs all of this across a database.

These checks read every page, which on a busy 5 TB primary would compete with users for I/O for hours. So we run them where the load does not matter. A restored backup is ideal, because its files came from the primary and checking them checks the primary's data as of that backup. A physical replica is another option, though its pages were written by its own replay, so it can miss damage that exists only on the primary's disk. Lighter checks such as bt_index_check without parent verification can still run on the primary at night for the most critical tables.

Text
pg_amcheck --all --heapallindexed --jobs=4 --install-missing
# exit status 0 when no corruption is found, otherwise one line per problem

4Layer 3: Restore Every Backup and Prove It

A backup that has never been restored is a hope, not a backup. So a scheduler takes each database's latest backup, restores it onto a temporary server, replays the archived WAL to a recent point, and then runs the checks: the server starts, pg_amcheck passes, row counts of key tables are within a few percent of the primary's, and a handful of known queries return expected results. The temporary server is then destroyed.

This one job gives us two answers. It proves the backup and WAL archive can actually rebuild the database within our recovery time objective, and because restoring touches every page, combined with checksums and amcheck it is the most complete corruption scan we can run without touching production. With a fleet of 300 databases restored weekly, that is about 43 restores a day, which a pool of short-lived machines handles easily.

Step throughDaily restore verification for one database
  1. Pick the next database in rotationevery database at least weekly
  2. Restore latest base backup and WALtime it against the recovery objective
  3. Run pg_amcheck and checksum scan
  4. Compare row counts and sample queriesagainst the primary
  5. All checks passed?
    YesRecord success, destroy the serverNoAlert the owner, keep the server for analysis

5 steps in order. Play them, or step through them one at a time.

5Layer 4: Reconciliation of Critical Data

Database checks cannot tell that a valid row holds the wrong number. For data that matters most, we compare it with an independent source. A ledger service can check every night that the sum of debits equals the sum of credits, and that its totals match the payment provider's settlement files. An order system can check that the count of paid orders matches the count of captured payments, and that each order total equals the sum of its lines. Data copied into a warehouse or search index can be compared with its source by counts and checksums of key columns.

These jobs belong to the owning teams, because only they know the rules, but the platform can offer a framework so that writing a reconciliation is a small SQL query and a threshold, not a project.

SQL
-- nightly: does every paid order match its lines?
SELECT o.id
FROM orders o JOIN order_line l ON l.order_id = o.id
WHERE o.status = 'PAID'
GROUP BY o.id, o.total
HAVING o.total <> SUM(l.amount); -- expected: 0 rows

6Layer 5: Alerts and Runbooks

Every layer feeds the same alerting. A checksum_failures count above zero pages the on-call engineer immediately, because it means data on disk has already changed. A failed amcheck run, a failed restore or a reconciliation mismatch opens a high-priority ticket for the owning team. Each alert links a runbook that says what to do first: preserve a copy, identify the relation, decide between REINDEX and restore, and check the hardware. We rehearse the runbook in game days by corrupting a page on a test copy, so that the first real incident is not the first time anyone reads it.

We also measure the system itself: how many clusters have checksums on, when each database was last restore-verified, and how long it took from damage to detection in past incidents. Those numbers tell us whether the fleet is actually covered.

7Follow-up questions

Why not just run amcheck on the primary every night?

Full checks read every page and with heapallindexed they also build a filter of every index entry, which costs hours of I/O and CPU on large databases and can push useful pages out of the cache. On restored backups we get the same coverage of the primary's data with no risk to users. On the primary we keep only light, targeted checks.

Do checksums slow PostgreSQL down?

Slightly. Computing a checksum is cheap, but with checksums on, hint bit changes must be WAL-logged, which adds some WAL and full-page writes. Most measurements show a small single-digit percentage overhead. For a fleet, early detection of corruption is worth far more than that.

What do we do when a reconciliation finds a mismatch?

We treat it like a failed test in production: freeze the evidence, find the first record where the two sources disagree, and work back to the cause, which is often an application bug rather than disk damage. The owning team fixes the data with a reviewed correction and adds a check so the same problem is caught earlier next time.

8Conclusion

In this answer, we designed integrity checking as layers that each catch different damage. Data checksums on every cluster catch changed bytes when pages are read. amcheck, run on restored backups or replicas, catches broken structure without loading the primary. Automated restore verification proves the backups and scans every page. Application reconciliation catches data that is valid but wrong. Alerts and rehearsed runbooks make sure each finding leads to a fast, safe response.

What to say in the interviewAbout 70 seconds aloud

Open with

I'd design it in layers, because each check sees a different kind of damage. First, every cluster runs with data checksums, so a page whose bytes changed fails on read instead of returning bad data, and we alert on checksum_failures in pg_stat_database.

Then, if they want more

New clusters get initdb with data checksums, which PostgreSQL 18 now does by default, and older ones are converted with pg_checksums during planned work. But checksums only catch pages someone reads, so second, I run amcheck and pg_amcheck on a schedule, mostly against restored backups, so we check the primary's data without loading it.

Third, every database's backup is restored automatically on a rotation, say weekly, WAL is replayed, and we check that it starts, passes amcheck and matches row counts. That proves our backups and doubles as a full scan. Fourth, for critical data the owning teams run reconciliations, such as ledger debits equal credits and totals match the payment provider.

Finally, all of it feeds one alerting system with runbooks we rehearse, and I'd track coverage: clusters with checksums, days since each database's last verified restore, and time to detect in past incidents.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 14, PostgreSQL 18
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How would you continuously verify data integrity across a fleet of PostgreSQL databases...": https://scrivio-interview-library.netlify.app/databases/database-operations/0581142b99/