#1540·litestream

WAL truncation or salt reset after an external checkpoint can drop committed rows from the replica

Author: liu1700Created Sep 27, 2026Updated Sep 27, 2026

Problem

On upstream main (checked at 3b91159; the regression below was run against 4ed7a30:db.go, and verifyWithExecutor still has the same syncedToWALEnd branch on main), a WAL reset can leave committed changes out of the replica. Reaching the end of the WAL in one sync does not prove that every commit before a later external checkpoint was replicated. Additional commits can be checkpointed into the main database and removed from the WAL before the next sync reads them.

The truncation branch in verifyWithExecutor treats syncedToWALEnd as proof that an incremental restart is safe. The salt-change branch can also resume incrementally after inspecting retained frames. Neither observation establishes that the intervening commits were captured.

In the regression described below, restoring the old implementation returns 80 rows instead of 201, for both TRUNCATE and RESTART. SQLite's integrity check still passes. This is missing application data in a structurally valid restore.

Reproduction

The test uses one small database and a local file-backed test replica:

  1. Create a data table and a separate heartbeat table in WAL mode.
  2. Write 80 rows of approximately 3.5 KB and sync through the current WAL end.
  3. Release Litestream's read transaction in the test harness to permit the external checkpoint window.
  4. Write another 121 rows without syncing them to Litestream.
  5. Run an external PRAGMA wal_checkpoint(TRUNCATE) or PRAGMA wal_checkpoint(RESTART).
  6. Write only to the heartbeat table so that the new WAL does not rewrite the data table's affected pages. Reacquire Litestream's read transaction.
  7. Sync, upload, restore the LTX chain, run PRAGMA integrity_check, and count rows in the data table.

The explicit read-lock release makes the vulnerable ordering deterministic. It is a test harness operation, not a proposed application API or a claim that every external checkpoint bypasses Litestream's reader.

Change in the fork

The fork takes a full snapshot when it cannot prove continuity across a WAL truncation or salt reset. The snapshot covers the main database as well as the current WAL, so pages moved by the external checkpoint are represented in the next LTX.

A Litestream-controlled PASSIVE checkpoint retains an incremental path. A writer barrier seals and syncs the old generation; the implementation captures the verified position while that barrier is held and carries the proof into the post-checkpoint sync. Generic reset detection does not assume that proof exists.

State changes remain deferred until applySyncResult; interrupted or failed snapshot creation must not publish a new sync position.

Reference commits in the fork:

Proposed upstream option and trade-off

Issue #927 reported severe local disk growth, and #1198 and #1332 describe the cost of full snapshots at checkpoint time. Taking a full snapshot after every unproven reset can increase local L0 storage, disk I/O, upload bytes, and sync latency, especially for large databases with frequent external checkpoints. The sealed PASSIVE path limits that cost for checkpoints whose continuity Litestream can prove.

For upstream discussion, propose a per-database option named snapshot-on-wal-reset, disabled by default to preserve current behavior. When enabled, it selects the conservative reset behavior above; ordinary sealed PASSIVE checkpoints remain incremental. Document the correctness-versus-storage trade-off explicitly. Maintainers may prefer a different default after assessing the missing-data reproduction.

This option is a proposal, not an option already implemented by the fork. The fork currently applies the conservative behavior unconditionally. This proposal does not change scheduled snapshot intervals or retention, and does not claim to resolve the original #927 incident.

Tests and investigation evidence

Existing fork regressions can be run with:

bash
go test -race -v -run '^(TestDB_SyncExternalWALResetTakesBoundarySnapshot|TestDB_CheckpointDoesNotTriggerSnapshot|TestVerifyAndSync_DelaysResetSnapshotStateMutationUntilApply)$' .

The reset test verifies full page coverage and a restored count of 201 for both reset modes. The PASSIVE test verifies fewer encoded pages than the full database and restores 101 rows. The deferred-state test verifies that snapshot preparation does not mutate live sync state before application.

For this investigation, the reset regression was run with a Go overlay substituting upstream 4ed7a30:db.go: both reset cases failed with restored row count=80, want 201. The fork's current full test suite passes. A proposed option needs additional enabled/disabled configuration tests before an upstream PR is ready.