Skip to content

perf(state): keep forecast archive prune off the insert transaction #1149

Closed

Description

@frahlg

Gzip of model snapshots moved outside the write lock in 481cca7 / v3.1.2. Prune, budget DELETE, and model-state enforcement still run inside the same SQLite transaction as the insert.

Found on master 6eadec3f in go/internal/state/forecast_issues.go. Forecast archives stay in SQLite even after DuckDB PR #1129.

What happens

prepareForecastIssue compresses before BeginTx. SaveForecastIssue then, in one write transaction:

  • INSERT OR IGNORE the issue
  • DELETE by retention time
  • DELETE with ROW_NUMBER / SUM(length(payload)) over forecast_issues
  • cleanForecastModelStateRefs
  • enforceForecastModelStateBudget, which loops SUM(length(payload)) + delete oldest until ≤ 64 MiB

Write timeout is 6s vs busy_timeout(5000). A blocked writer can occupy SQLite for most of that window. Control-tick history writes (master) and config/forecast (both master and #1129) wait on the same lock.

VisitForecastIssues holds a read cursor while gzip-decoding every issue. Scoring does this every 10s over ~50h (go/cmd/ftw forecast tracking). That stalls WAL checkpoints.

Retry is 3 attempts, 250ms × attempt. That hides the contention; it does not shorten the lock.

What a fix needs

  • Keep insert+idempotency in the short tx.
  • Run retention/budget prune in a later tx, or after commit, with the same fairness pause history prune already uses.
  • Do not gzip or JSON-marshal inside the write tx (SaveForecastErrors still marshals inside).
  • Tests: existing forecast_issue_contention_test.go plus one that asserts prune SQL is not in the insert tx (or that a concurrent tick writer succeeds during prune).

Related: #882 (tick writer), #1145 (/api/status vs the same lock). #1129 removes history from this lock but not forecast archives.

Activity

  1. added
    enhancement
    on Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions