hn.today

How WAL mode works in SQLite

slicker.me5 points0 comments
Screenshot of How WAL mode works in SQLite

SQLite’s write-ahead logging (WAL) mode improves concurrency by recording changed pages in a separate log instead of modifying the database file during a transaction. Readers combine the database with the log, while a single writer appends changes, allowing readers and the writer to proceed at the same time. The log stores complete page images, and chained checksums let SQLite detect damaged or incomplete writes. A transaction becomes visible when its valid commit frame is published in the shared wal-index; readers capture a fixed frame number as their snapshot, so later commits do not alter what they see. The wal-index maps pages to their newest logged versions and can be rebuilt from the log after a crash.

Checkpointing transfers logged pages into the database file, but cannot overwrite pages needed by readers holding older snapshots. SQLite offers passive, full, restart, and truncate checkpoints, with increasingly strict waiting and log cleanup. Automatic checkpoints normally begin after 1,000 pages, and the committing connection may bear their cost. WAL avoids undo recovery because uncommitted changes never reach the database file; after a crash, SQLite validates frames and ignores anything after the last valid commit. Important constraints remain: only one writer can operate at once, long-lived readers can prevent checkpoints and cause log growth, and WAL generally requires local shared-memory and file-lock support. NORMAL synchronization can lose recent commits after a crash but does not corrupt the database; FULL synchronization flushes the log at commit.

Read on slicker.me0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

Lua as a Query Language

Lua as a Query Language

Lua can serve as a dynamic query language by expressing relational operations as Lua code that builds an AST for a Rust backend, avoiding a custom parser and Rust recompilation for ad hoc queries. Lua variables provide reusable views and built-in language features support query constructs such as filters and arithmetic. (remy.wang)

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.