From a0d6a5591f929f521d82f9f4e3fed8ef7a7da2c0 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Wed, 2 Apr 2025 14:36:59 -0700 Subject: [PATCH] kvdb/sqlite: enable incremental auto_vacuum on DB creation In this commit, we make a change that enables the `auto_vacuum = incremental` pragma for SQLite databases, but only when the database file is first created. Incremental auto-vacuum allows SQLite to reclaim unused space within the database file over time, preventing indefinite growth. --- kvdb/sqlite/db.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/kvdb/sqlite/db.go b/kvdb/sqlite/db.go index f04bec3be..07a6d07cf 100644 --- a/kvdb/sqlite/db.go +++ b/kvdb/sqlite/db.go @@ -24,6 +24,12 @@ const ( sqliteTxLockImmediate = "_txlock=immediate" ) +// pragmaOption holds a key-value pair for a SQLite pragma setting. +type pragmaOption struct { + name string + value string +} + // NewSqliteBackend returns a db object initialized with the passed backend // config. If a sqlite connection cannot be established, then an error is // returned. @@ -31,10 +37,7 @@ func NewSqliteBackend(ctx context.Context, cfg *Config, dbPath, fileName, prefix string) (walletdb.DB, error) { // First, we add a set of mandatory pragma options to the query. - pragmaOptions := []struct { - name string - value string - }{ + pragmaOptions := []pragmaOption{ { name: "busy_timeout", value: fmt.Sprintf( @@ -49,7 +52,12 @@ func NewSqliteBackend(ctx context.Context, cfg *Config, dbPath, fileName, name: "journal_mode", value: "WAL", }, + { + name: "auto_vacuum", + value: "incremental", + }, } + sqliteOptions := make(url.Values) for _, option := range pragmaOptions { sqliteOptions.Add(