From 184c4ad04040264e098ac675c1271ca2b95bc05d Mon Sep 17 00:00:00 2001 From: Deluan Date: Sat, 5 Sep 2026 01:14:48 -0400 Subject: [PATCH] feat(pglite): checkpoint after ANALYZE at the end of a scan PGlite's single-user backend has no background checkpointer, so nothing written during a scan is checkpointed until shutdown, and a crash would replay the whole scan's WAL. Run CHECKPOINT right after the post-scan ANALYZE. The log directory itself stays around max_wal_size because PostgreSQL recycles segments rather than deleting them. --- db/optimize.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/db/optimize.go b/db/optimize.go index 044590ea5..f4cfff0a1 100644 --- a/db/optimize.go +++ b/db/optimize.go @@ -141,6 +141,14 @@ func optimizeAt(ctx context.Context, db *sql.DB, now time.Time) error { if err = recordAnalyzeSuccess(ctx, db, now); err != nil { return recordAnalyzeError(ctx, db, now, err) } + // The single-user backend has no checkpointer, so a scan's worth of WAL would only be + // checkpointed at shutdown; do it here so a crash never has to replay the whole scan. + start := time.Now() + if _, err := db.ExecContext(ctx, "CHECKPOINT"); err != nil { + log.Warn(ctx, "Error running CHECKPOINT", err) + } else { + log.Debug(ctx, "Checkpoint completed", "elapsed", time.Since(start)) + } return nil }