Run WAL checkpoint on startup with logging
Explicitly checkpoint the WAL on database initialization to consolidate any large WAL file from previous runs. Log the checkpoint results to help diagnose issues.
This commit is contained in:
parent
a163449a28
commit
f1d7c21478
@ -106,14 +106,13 @@ func New(cfg *config.Config, logger *logger.Logger) (*Database, error) {
|
||||
func (d *Database) Initialize() error {
|
||||
// Set SQLite pragmas for performance
|
||||
pragmas := []string{
|
||||
"PRAGMA journal_mode=WAL", // Write-Ahead Logging
|
||||
"PRAGMA synchronous=OFF", // Don't wait for disk writes
|
||||
"PRAGMA cache_size=-3145728", // 3GB cache (upper limit for 2.4GB DB)
|
||||
"PRAGMA temp_store=MEMORY", // Use memory for temp tables
|
||||
"PRAGMA wal_checkpoint(TRUNCATE)", // Checkpoint and truncate WAL now
|
||||
"PRAGMA busy_timeout=5000", // 5 second busy timeout
|
||||
"PRAGMA analysis_limit=0", // Disable automatic ANALYZE
|
||||
"PRAGMA auto_vacuum=INCREMENTAL", // Enable incremental vacuum
|
||||
"PRAGMA journal_mode=WAL", // Write-Ahead Logging
|
||||
"PRAGMA synchronous=OFF", // Don't wait for disk writes
|
||||
"PRAGMA cache_size=-3145728", // 3GB cache (upper limit for 2.4GB DB)
|
||||
"PRAGMA temp_store=MEMORY", // Use memory for temp tables
|
||||
"PRAGMA busy_timeout=5000", // 5 second busy timeout
|
||||
"PRAGMA analysis_limit=0", // Disable automatic ANALYZE
|
||||
"PRAGMA auto_vacuum=INCREMENTAL", // Enable incremental vacuum
|
||||
}
|
||||
|
||||
for _, pragma := range pragmas {
|
||||
@ -122,7 +121,20 @@ func (d *Database) Initialize() error {
|
||||
}
|
||||
}
|
||||
|
||||
err := d.exec(dbSchema)
|
||||
// Run WAL checkpoint on startup to consolidate any existing WAL
|
||||
var walPages, checkpointedPages, movedPages int
|
||||
err := d.db.QueryRow("PRAGMA wal_checkpoint(TRUNCATE)").Scan(&walPages, &checkpointedPages, &movedPages)
|
||||
if err != nil {
|
||||
d.logger.Warn("Failed to checkpoint WAL on startup", "error", err)
|
||||
} else {
|
||||
d.logger.Info("WAL checkpoint on startup",
|
||||
"wal_pages", walPages,
|
||||
"checkpointed", checkpointedPages,
|
||||
"moved", movedPages,
|
||||
)
|
||||
}
|
||||
|
||||
err = d.exec(dbSchema)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user