Recursive AI

Checkpoint a SQLite WAL in C#

OneLite is a free, open-source SQLite-compatible database for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

A checkpoint copies committed WAL frames back into the main .db file.

Terminal
dotnet add package RecursiveAI.OneLite

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneLite;
var file = Path.GetRandomFileName();
using var db = Database.OpenDefault(file);
db.Execute($"CREATE TABLE notes(id)");
using var conn = db.Connect();
using var cmd = conn.Prepare(
    "PRAGMA wal_checkpoint(FULL)");
if (cmd.Step()) Console.Write(cmd.GetInt64(2));

Unzip the example, then dotnet run.

Text
2

The snippet prints how many page versions it copied from the write-ahead log (WAL): 2 in this scratch database.

db.Execute takes an interpolated string ($"..."); any values inserted with {...} become SQL parameters.

The full program

C#
using RecursiveAI.OneLite;
 
string path = Path.GetRandomFileName();
using (var db = Database.OpenDefault(path))
using (var conn = db.Connect())
{
    db.Execute($"CREATE TABLE notes(body TEXT)");
    using var cmd = conn.Prepare(
        "PRAGMA wal_checkpoint(FULL)");
    if (!cmd.Step()) throw new InvalidOperationException();
    Console.WriteLine($"busy={cmd.GetInt64(0)}");
    Console.WriteLine($"log={cmd.GetInt64(1)}");
    Console.WriteLine($"copied={cmd.GetInt64(2)}");
}
File.Delete(path);
Text
busy=0
log=2
copied=2

The program creates and deletes a scratch database. OpenDefault enables WAL. The FULL checkpoint copied both frames (page versions) in the log; a reader or writer can prevent it from completing, in which case the first result column can be 1.

Variations

Empty the WAL file after copying its frames. Replace FULL with TRUNCATE in the pragma. After a successful truncate, the counters are zero; the log no longer contains any frames.

SQL
PRAGMA wal_checkpoint(TRUNCATE)
Text
busy=0
log=0
copied=0

Open an existing SQLite database · Use SQLite transactions · Back up a SQLite database


The short answer

Run PRAGMA wal_checkpoint(FULL) on a file-backed database in WAL mode. Read its result row: the first column is the busy flag, the second is the number of frames in the log, and the third is the number already copied back into the database by the end of the checkpoint. These are counts of pages in the log, not SQL rows. If the busy flag is 1, don’t treat the requested checkpoint as complete.

The same PRAGMA works through Microsoft.Data.Sqlite, including on EF Core’s SQLite connection when configured with the OneLite adapter.

What is a SQLite WAL file?

In WAL mode, SQLite records committed changes in a separate -wal file. A reader may need that file to see the committed state even when the latest pages are not yet in the main database file. A checkpoint copies those pages back. It is not a commit, and a successful commit doesn’t mean that a checkpoint has already happened. OneLite’s automatic checkpoint is passive and defaults to a threshold of 1,000 frames. You can request a checkpoint before that threshold, as the example does.

Why can a SQLite WAL checkpoint be blocked?

A reader can still be using an older snapshot, so the checkpoint can’t overwrite main-file pages that reader needs. PASSIVE copies whatever it can without waiting, so compare its counts rather than treating a zero busy flag as proof of a complete copy. FULL can wait according to the busy policy. If an operation still needs the log, let the reader finish or retry the checkpoint later. See the SQLite pragma reference for the result columns and when the counts are -1.

A successful FULL checkpoint doesn’t promise a zero-byte -wal file. RESTART also waits until readers are done with the log so the next writer can restart it. TRUNCATE goes one step further and truncates the log immediately on success. Don’t delete -wal to make it smaller: committed changes might still be there.

All onelite recipes