Skip to content

feat: add Database#persistWal() to keep the WAL on close - #49

Merged
darkgnotic merged 2 commits into
mainfrom
persist-wal-api
Oct 8, 2026
Merged

darkgnotic merged 2 commits into
mainfrom
persist-wal-api

Conversation

@darkgnotic

Copy link
Copy Markdown
Contributor

Add a persistWal([toggleState]) method that sets
SQLITE_FCNTL_PERSIST_WAL on the connection's main database. While it is on, the last connection to close still checkpoints the WAL into the database, but keeps the -wal and -shm files instead of deleting them. The next connection then appends to the same WAL with the same salt.

This lets zero-cache keep a replica's WAL intact across closes, so a litestream backup can continue from it instead of taking a new snapshot. Today zero-cache does that by holding an extra connection open (WalKeeper).

Persistence is off by default, so behavior is unchanged for existing users. It is a no-op for in-memory and temporary databases, and throws if the connection is closed.

Turn it off before switching the database out of WAL mode. Otherwise SQLite leaves the -wal file in place and reopens it on the next transaction, so the database effectively stays in WAL mode.

Add a persistWal([toggleState]) method that sets
SQLITE_FCNTL_PERSIST_WAL on the connection's main database. While it
is on, the last connection to close still checkpoints the WAL into the
database, but keeps the -wal and -shm files instead of deleting them.
The next connection then appends to the same WAL with the same salt.

This lets zero-cache keep a replica's WAL intact across closes, so a
litestream backup can continue from it instead of taking a new
snapshot. Today zero-cache does that by holding an extra connection
open (WalKeeper).

Persistence is off by default, so behavior is unchanged for existing
users. It is a no-op for in-memory and temporary databases, and throws
if the connection is closed.

Turn it off before switching the database out of WAL mode. Otherwise
SQLite leaves the -wal file in place and reopens it on the next
transaction, so the database effectively stays in WAL mode.
@darkgnotic
darkgnotic requested a review from tantaman October 8, 2026 22:51
@darkgnotic
darkgnotic added this pull request to the merge queue Oct 8, 2026
Merged via the queue into main with commit 6eb6175 Oct 8, 2026
26 checks passed
@darkgnotic
darkgnotic deleted the persist-wal-api branch October 8, 2026 23:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant