In the SQLite documentation on the write-ahead-log feature introduced in version 3.7, there are some comments which confused me a bit.
The linked page says "syncing the content to the disk is not required, as long as the application is willing to sacrifice durability following a power loss". Then a couple of paragraphs down, it says "Checkpointing does require sync operations in order to avoid the possibility of database corruption following a power loss or hard reboot"".
So is my database at greater risk of corruption on power loss if I use WAL?
An SQLite database is highly resistant to corruption. If an application crash, or an operating-system crash, or even a power failure occurs in the middle of a transaction, the partially written transaction should be automatically rolled back the next time the database file is accessed.
The WAL journaling mode is persistent; after being set it stays in effect across multiple database connections and after closing and reopening the database. A database in WAL journaling mode can only be accessed by SQLite version 3.7.
Overview. Usually, SQLite allows at most one writer to proceed concurrently. The BEGIN CONCURRENT enhancement allows multiple writers to process write transactions simultanously if the database is in "wal" or "wal2" mode, although the system still serializes COMMIT commands.
16) When can you get an SQLITE_SCHEMA error? The SQLITE_SCHEMA error is returned when a prepared SQL statement is not valid and cannot be executed. Such type occurs only when using the sqlite3 prepare() and sqlite3 step() interfaces to run SQL.
To answer fully, we need to know what you have PRAGMA synchronous
set to, as this affects when fdatasync()
is called and thus when the buffers flushed out on the physical drive.
When you quote "as long as the application is willing to sacrifice durability following a power loss", this is referring to having synchronous=NORMAL
. Here the WAL is only synchronized to disk when checkpointing happens (one fdatasync()
for the WAL and one for the main DB after it's merged). You should be protected against corruption pretty well, but there may be some writes that never made it to the platter and are thus lost: hence the lost durability. The upside though is much less of the slow fdatasync() to actually sync the data.
To have best resiliance against data being lost, you might want synchronous=FULL
. This re-gains durability, but the cost is one fdatasync()
per write transaction. However, this is still better than non-WAL mode where there would be two fdatasync()
calls -- one for the transaction journal and one for the main DB.
There is no increased risk of corruption with WAL (since it uses sync operations when checkpointing).
However, if there is a crash (power loss or hard reboot) you will lose any transactions since the last checkpoint; that's what is meant by "sacrificing durability."
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With