Add periodic WAL checkpointing to fix slow queries
The WAL file was growing to 700MB+ which caused COUNT(*) queries to timeout. Reads must scan the WAL to find current page versions, and a large WAL makes this slow. Add Checkpoint method to database interface and run PASSIVE checkpoints every 30 seconds via the DBMaintainer. This keeps the WAL small and maintains fast read performance under heavy write load.
This commit is contained in:
@@ -1986,3 +1986,14 @@ func (d *Database) Analyze(ctx context.Context) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Checkpoint runs a WAL checkpoint to transfer data from the WAL to the main database.
|
||||
// Uses PASSIVE mode which doesn't block writers but may not checkpoint all frames.
|
||||
func (d *Database) Checkpoint(ctx context.Context) error {
|
||||
_, err := d.db.ExecContext(ctx, "PRAGMA wal_checkpoint(PASSIVE)")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to checkpoint WAL: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -87,6 +87,7 @@ type Store interface {
|
||||
// Maintenance operations
|
||||
Vacuum(ctx context.Context) error
|
||||
Analyze(ctx context.Context) error
|
||||
Checkpoint(ctx context.Context) error
|
||||
}
|
||||
|
||||
// Ensure Database implements Store
|
||||
|
||||
Reference in New Issue
Block a user