How to Verify Database Integrity After a Restore (MySQL & PostgreSQL)
By Jérémy Sculfort · · 3 min read
Key takeaways
- Start with an inventory: every expected database, schema and table must be present.
- Use exact
COUNT(*)—information_schemaandpg_statnumbers are estimates. - Check freshness with the newest timestamp to confirm the recovery point.
- Compare with the source using a tolerance, since production keeps changing after the snapshot.
Once a snapshot has been restored, the real question is whether the database inside is complete and usable. The checks below go from cheap to expensive. Run them from a host inside the restore VPC, against the restored copy — never against production.
1. Inventory: is everything there?
Missing schemas or tables are the most common silent failure, for example when data lives in a database that was never part of the backup scope.
MySQL / Aurora MySQL
SELECT table_schema, COUNT(*) AS tables
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys')
GROUP BY table_schema;PostgreSQL / Aurora PostgreSQL
-- databases
SELECT datname FROM pg_database WHERE NOT datistemplate;
-- tables per schema (run in each database)
SELECT schemaname, COUNT(*) AS tables
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
GROUP BY schemaname;Compare the result with a list taken from production, or with the previous successful validation.
2. Exact row counts for every table
Do not rely on information_schema.tables.TABLE_ROWS in MySQL: for InnoDB it is an estimate that can be off by a large margin. In PostgreSQL, pg_stat_user_tables.n_live_tup and pg_class.reltuples are estimates too, and statistics may be reset on a restored copy. Generate exact counts instead.
MySQL: generate one COUNT(*) per table
SELECT CONCAT('SELECT ''', table_schema, '.', table_name, ''' AS t, COUNT(*) AS n FROM `',
table_schema, '`.`', table_name, '`;')
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys');PostgreSQL: psql with \gexec
SELECT format('SELECT %L AS t, count(*) AS n FROM %I.%I',
schemaname || '.' || tablename, schemaname, tablename)
FROM pg_tables
WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
\gexecA table that errors out or returns zero rows when production has millions is an immediate failure. Note that the first counts on a freshly restored RDS instance are slower than usual, because storage blocks are lazily loaded from Amazon S3 the first time they are read.
3. Freshness: does it match the recovery point?
Pick one or two tables written constantly (orders, events, audit logs) and check the newest row:
SELECT MAX(created_at) FROM orders;The value should be close to the snapshot creation time. A gap of hours means the backup does not meet your RPO — for example because writes go to a database that is not included, or a replica was snapshotted instead of the primary.
4. Structural checks
MySQL
CHECK TABLE app.orders;
CHECKSUM TABLE app.orders;CHECK TABLE reports corruption in InnoDB tables and indexes. CHECKSUM TABLE reads every row and returns a checksum you can compare with the same command run on production for static or archived tables; both are expensive on large tables, so restrict them to critical ones.
PostgreSQL
CREATE EXTENSION IF NOT EXISTS amcheck;
SELECT c.relname, bt_index_check(c.oid)
FROM pg_index i
JOIN pg_class c ON c.oid = i.indexrelid
JOIN pg_am am ON am.oid = c.relam
WHERE am.amname = 'btree';The amcheck extension verifies B-tree index structure and raises an error on corruption. For small reference tables, a content fingerprint such as SELECT md5(string_agg(t::text, '' ORDER BY t::text)) FROM ref.countries t; lets you compare restored and source data exactly.
5. Compare with the source, with a tolerance
Production keeps changing after the snapshot, so expecting identical counts is unrealistic. Compare each table's restored count with the current production count and flag differences above a threshold (for instance 5% for busy tables, 0% for reference data). Large negative differences on tables that only grow are the signal to investigate.
6. Application smoke tests
Finally, run a few read-only queries your application depends on: logging in a test user, loading the last invoices, a typical report. They catch problems row counts miss, such as missing views, functions, extensions or permissions.
7. Record the results
Save the output — tables found, counts, failed checks, duration — with the snapshot identifier and timestamps. That record is what turns a technical test into audit evidence. If you would rather not script and schedule all of this, SnapshotShield runs the inventory, row counts and sample queries on every new snapshot in your AWS account, and supports custom SQL queries on Team and Enterprise plans.
Sources
Automate this for every RDS snapshot
SnapshotShield restores each new snapshot in an isolated VPC in your own AWS account, checks every table and sends you a report — then cleans everything up.
Start the 14-day free trial See a sample report