From 2f918a3b034a86776eecb7bbc594f1352020e744 Mon Sep 17 00:00:00 2001 From: rcourtman Date: Fri, 5 Jun 2026 13:32:44 +0100 Subject: [PATCH] metrics: skip checkpoint when freelist empty in reclaimFreePages Follow-up to 2b8ce06c0: that commit's contract update states reclaim skips the WAL checkpoint entirely when the freelist is empty, but the prior commit captured the pre-refinement store.go (the re-stage was lost to a .git/index.lock race). Make the code match the contract: return early on a freelist_count read error and when freelist == 0, so a steady-state no-op cycle does not run an extra hourly wal_checkpoint(TRUNCATE). --- pkg/metrics/store.go | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/pkg/metrics/store.go b/pkg/metrics/store.go index f3cb0177f..8ac407e47 100644 --- a/pkg/metrics/store.go +++ b/pkg/metrics/store.go @@ -1760,16 +1760,19 @@ func (s *Store) reclaimFreePages() { var freelist int64 if err := s.db.QueryRow(`PRAGMA freelist_count`).Scan(&freelist); err != nil { log.Debug().Err(err).Msg("Failed to read freelist_count") - freelist = maxReclaimPages // fall back to a bounded reclaim + return } - if freelist > 0 { - pages := freelist - if pages > maxReclaimPages { - pages = maxReclaimPages - } - if _, err := s.db.Exec(fmt.Sprintf(`PRAGMA incremental_vacuum(%d)`, pages)); err != nil { - log.Debug().Err(err).Msg("Incremental vacuum failed") - } + if freelist == 0 { + // Nothing to reclaim: skip the checkpoint too so steady-state WAL + // cadence is no more aggressive than before this drained every cycle. + return + } + pages := freelist + if pages > maxReclaimPages { + pages = maxReclaimPages + } + if _, err := s.db.Exec(fmt.Sprintf(`PRAGMA incremental_vacuum(%d)`, pages)); err != nil { + log.Debug().Err(err).Msg("Incremental vacuum failed") } if _, err := s.db.Exec(`PRAGMA wal_checkpoint(TRUNCATE)`); err != nil { log.Debug().Err(err).Msg("WAL checkpoint failed")