StartWALCheckpointing starts a background goroutine that periodically checkpoints the WAL file to prevent it from growing unbounded
(db *gorm.DB, interval time.Duration)
| 109 | // StartWALCheckpointing starts a background goroutine that periodically |
| 110 | // checkpoints the WAL file to prevent it from growing unbounded |
| 111 | func StartWALCheckpointing(db *gorm.DB, interval time.Duration) { |
| 112 | go func() { |
| 113 | ticker := time.NewTicker(interval) |
| 114 | defer ticker.Stop() |
| 115 | |
| 116 | for range ticker.C { |
| 117 | // TRUNCATE mode removes the WAL file after checkpointing |
| 118 | if err := db.Exec("PRAGMA wal_checkpoint(TRUNCATE)").Error; err != nil { |
| 119 | log.ErrorWrap(err, "WAL checkpoint failed") |
| 120 | } |
| 121 | } |
| 122 | }() |
| 123 | } |
| 124 | |
| 125 | // StartPeriodicVacuum runs full VACUUM on a schedule to reclaim space and defragment. |
| 126 | // VACUUM acquires an exclusive lock and blocks all database operations briefly. |