SQLite snapshot handle
A SQLite snapshot handle records a historical WAL-mode database state that a connection may reopen while the required history remains available.
A handle identifies a WAL view, not a standalone database copy. It can outlive the history; snapshot_open can return SQLITE_ERROR_SNAPSHOT after checkpointing overwrites that history. [1] [3]
Capture requires autocommit off, WAL mode, no write transaction on the named schema and a transaction written to the current WAL. These APIs require SQLITE_ENABLE_SNAPSHOT at build time. [2]
Failed capture prerequisites leave the output pointer undefined; do not use or free it as a valid handle. Require SQLITE_OK before use; capture can also return SQLITE_NOMEM. [2]
If snapshot_get itself starts the read transaction, writers and checkpointers cannot invalidate its handle until that read ends. The documentation gives no such guarantee when a read was already open. An active read alone does not prove handle validity. [2]
Reopening requires autocommit off and a connection aware of WAL mode. Replacing an existing read requires no active SELECT statements. [3]
sqlite3_snapshot_cmp orders valid handles from the same database obtained since the last WAL deletion. Its sign is relative ordering, not a timestamp or global commit sequence. [5]
If a WAL remains after all database connections close, a new connection initially exposes only the last transaction in that WAL to snapshot_open, even when older valid transactions remain. sqlite3_snapshot_recover attempts to discover valid snapshots by rescanning that WAL; it cannot recreate missing history. The call requires WAL mode and no open read transaction on the target database. [6]
sqlite3_snapshot_free releases the allocated handle, not the application's read transaction. [4]
Relations
Citations
- SQLite C API: historical database snapshot object
- SQLite C API: snapshot capture prerequisites and conditional read-transaction protection
- SQLite C API: reopening a historical snapshot and error-state boundaries
- SQLite C API: destroying a snapshot object
- SQLite C API: snapshot comparison scope and WAL deletion boundary
- SQLite C API: discovering valid snapshots in a retained WAL
Sources & caveats
- Primary API documentation reviewed on 2026-11-28. Documentation synthesis, not a native snapshot-API experiment, backup validation or performance measurement.