Crash recovery
Overview
Crash recovery restores a transactionally consistent database after an unexpected interruption. PostgreSQL replays WAL from the checkpoint redo location, using transaction status to determine visibility. SQLite WAL recovery uses valid committed frames; rollback-journal recovery can restore old page images from a hot journal. Recovery after a process crash differs from media failure, corruption, disaster recovery and point-in-time restoration from backups.
First example
BEGIN;UPDATE rc_account SET balance=999 WHERE id=1;ROLLBACK;SELECT balance FROM rc_account WHERE id=1;Observed SQLite result (rows in JSON):
[ [ 100 ]]How it works
The SQL examples verify a rolled-back update and a structural integrity check, not an actual crash. In the host lab an isolated child process commits balance 150, begins an update to 999 and terminates via os._exit without closing. A fresh connection reads 150 and integrity_check returns ok. Repeat in DELETE journal mode. This proves the observed process-exit scenarios only, not every failure interleaving or hot-journal replay; uncommitted changes might not have reached the database file.
Practical example
PRAGMA integrity_check;Observed SQLite result (rows in JSON):
[ [ "ok" ]]Practice and mistakes
Practice: run host-lab.py and compare committed and pending values before process exit and after reopen. Hint 1: never simulate a crash on a live database. Hint 2: an integrity check does not prove business correctness, so assert the expected balance too. Keep the database and its journal files together; use supported backup mechanisms.
PostgreSQL 17 example — NOT RUN
SELECT pg_is_in_recovery();Expected result, not executed: "Boolean recovery state; false on a normal primary. Does not initiate recovery."