fix(sqlite): Add WAL checkpoints when vacuuming

For some reason, the automatic WAL checkpoints don't seem to be working as expected. Since we should periodically run VACUUM operations, we might as well add checkpoints before vacuuming (so the WAL size is reset and can grow to fit the whole DB) and after (so we clean up after that).
This commit is contained in:
Jorge Martín
2026-01-07 12:14:00 +01:00
committed by Jorge Martin Espinosa
parent 5dcd877dcd
commit 3be6fb1a80

View File

@@ -187,6 +187,7 @@ pub(crate) trait SqliteAsyncConnExt {
///
/// Only returns an error in tests, otherwise the error is only logged.
async fn vacuum(&self) -> Result<()> {
self.wal_checkpoint().await;
if let Err(error) = self.execute_batch("VACUUM").await {
// Since this is an optimisation step, do not propagate the error
// but log it.
@@ -198,11 +199,19 @@ pub(crate) trait SqliteAsyncConnExt {
return Err(error.into());
} else {
trace!("VACUUM complete");
self.wal_checkpoint().await;
}
Ok(())
}
async fn wal_checkpoint(&self) {
match self.execute_batch("PRAGMA wal_checkpoint(TRUNCATE);").await {
Ok(_) => trace!("WAL checkpoint completed"),
Err(error) => error!(?error, "WAL checkpoint error"),
}
}
async fn get_db_size(&self) -> Result<usize> {
let page_size =
self.query_row("PRAGMA page_size;", (), |row| row.get::<_, usize>(0)).await?;