How a database backup paired a new score with an old limit
Our test database had a simple rule. A score had to stay at or below its limit. At first, the score was 45 and the limit was 50. We changed them together to 75 and 80. Both pairs passed. Then we restored a backup and found a score of 75 against a limit of 50.
The backup had put together two numbers that had never belonged to the same saved version. It copied the limit from one table, we saved the change, and then it copied the score from another table. We ran the same test with one difference. This time the backup kept reading from a single point in time.
We made the backup as a dump, a list of instructions for rebuilding a database. When we ran those instructions, the restored database let us inspect exactly which numbers the backup had copied. The tool we tested was Python’s sqlite3.Connection.iterdump() for SQLite. Python documentation · How SQLite readers see saved changes
The two versions side by side
The database used write-ahead logging (WAL), which let a second connection save changes while the backup was reading. The score and its limit, called the cap in the table, were changed in one transaction. That means the database saved both changes together.
Read the numbers in parentheses as version numbers. In the mixed run, the backup got the limit from version 1 and the score from version 2. In another run, we saved the change at the same moment but kept the backup on a single view. It copied both values from version 1.
The other rows show what happened when nobody changed the database, when the writer canceled the change (“rolls back”), or when the change was already saved before the backup started reading (“before consuming the iterator”).
| Source timing | Cap (revision) | Score (revision) | Score ≤ cap |
|---|---|---|---|
| No intervening write | 50 (1) | 45 (1) | Pass |
| Commit between tables | 50 (1) | 75 (2) | Fail |
| Commit with a source read transaction | 50 (1) | 45 (1) | Pass |
| Writer rolls back between tables | 50 (1) | 45 (1) | Pass |
| Commit before consuming the iterator | 80 (2) | 75 (2) | Pass |
Why the mixed backup still passed an integrity check
All five backups rebuilt successfully. All five passed SQLite’s integrity check, which examines the internal structure of the database. To find the failing pair, we needed a different check. Did the score belong to the same version as its limit?
The backup instructions themselves began with BEGIN TRANSACTION. That might sound like protection against a change halfway through. But the instruction is for the person restoring the database later. In the mixed run, the connection reading the original database still showed reader.in_transaction as false when it produced that line. Python code that produces the dump
To keep a changing WAL database on one view, start a read transaction on the source connection before the dump begins reading. Keep that same connection and transaction open until the last instruction has been copied.
With a deferred BEGIN, the first actual read fixes the snapshot, the point in time the backup will see. If you need a particular revision, read its ID first inside that transaction. Check that it is the one you need before copying the tables. SQLite transaction documentation
Reproduce the test
We tested CPython 3.12.15 and SQLite 3.53.1. Run python -I -B replay.py to recreate the deliberately timed change and its four comparison runs. Put the verifier and saved observations in the same directory and run python -I -B verify.py to check the restored rows. Replay · Saved observations · Verifier