Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Consequences of -wal file disappearing in SQLite?

Tags:

sqlite

wal

If a SQLite database using write-ahead logging is interrupted with un-checkpointed transactions (due to a power failure or whatever), then reopened with the temporary -wal file missing, will the database open cleanly to its state as of the last checkpoint, or will it be corrupted in some way?

We're trying to get SQLite working with iCloud (yes, we know you're not supposed to do that, but we also make a Windows and an Android app and need a cross-platform database solution), and we think that WAL provides a potential way to avoid having to maintain two copies of our database - we'd keep the -wal file outside of iCloud but store the main database in it, thus avoiding the problem of iCloud backing up rollback journals (or backing up databases mid-transaction without those journals).

like image 340
Ertebolle Avatar asked Sep 08 '25 12:09

Ertebolle


2 Answers

The file format documentation mentions a "hot WAL file", but this applies only to uncommitted data.

The database file itself does not contain any information about committed data in the -wal file, i.e., transactions before a checkpoint typically do not alter the main database file at all. Therefore, deleting the -wal file will simply restore the database to the state it was after the last checkpoint (which is outdated, but consistent); all transactions committed later will just be lost.

like image 90
CL. Avatar answered Sep 10 '25 07:09

CL.


See the "Checkpointing" section of SQLite's Write-Ahead Logging. From what I understand, the data in the WAL file would simply not be committed.

In other words, you'd lose the data in the .WAL file that hasn't yet been committed, but the main database itself should be perfectly fine.

like image 27
NuSkooler Avatar answered Sep 10 '25 05:09

NuSkooler