Skip to content

perf(state): snapshot skips time-series tables — 1 GB → ~50 MB - #309

Merged
frahlg merged 1 commit into
masterfrom
perf/snapshot-skip-timeseries
May 25, 2026
Merged

frahlg merged 1 commit into
masterfrom
perf/snapshot-skip-timeseries

Conversation

@frahlg

@frahlg frahlg commented May 25, 2026

Copy link
Copy Markdown
Member

Measured baseline (2026-05-25, live Pi)

state.db          = 1.0 GB
snapshot dir      = 6.0 GB (9 snapshots × ~670 MB each)
VACUUM INTO       ≈ 30 s per snapshot

Most of the bulk lives in time-series tables (`history_hot/warm/cold` + `ts_samples`). Those tables already roll off to cold parquet files on a 14-day horizon — they're not the authoritative source after roll-off, and a rollback that loses the hot/warm window only loses the last 14 days of high-resolution data. That data is still recoverable from parquet (and from the persistent `energy_daily` aggregate cache added in #308).

What goes in / out

In (snapshot):

  • config — operator settings
  • events, telemetry, battery_models, prices, forecasts
  • ts_drivers / ts_metrics — interner dicts (tiny but referenced)
  • devices — hardware identity (load-bearing for trained models)
  • planner_diagnostics, notification_log, nova_ders
  • energy_daily — persistent aggregate cache

Out (excluded):

  • history_hot / history_warm / history_cold
  • ts_samples

Implementation

ATTACH destination as snap, walk sqlite_master for essential CREATE statements, rewrite identifier to target snap.<name>, then INSERT INTO snap.<table> SELECT * FROM main.<table>. Single connection so ATTACH binding survives; DETACH on every exit path.

Tests

  • TestSnapshotToCapturesLiveState — preserved
  • TestSnapshotToSkipsTimeSeriesTables — new: write history + ts rows, snapshot, assert essentials present + history/ts empty
  • TestSnapshotToRefusesExistingFile — preserved (stat-check now instead of VACUUM INTO's implicit refusal)

Full suite: go test ./... green.

Expected impact

  • Snapshot wall-clock: ~30 s → ~2 s
  • Snapshot size: ~1 GB → ~50 MB
  • 9 retained snapshots × 50 MB = ~450 MB total dir (vs 6 GB today)

🤖 Generated with Claude Code

… → ~2 s

2026-05-25 measurement on the live Pi:

  state.db                 = 1.0 GB
  snapshot dir             = 6.0 GB (9 snapshots × ~670 MB each)
  VACUUM INTO snapshot     ≈ 30 s

Most of the bulk lives in history_hot / history_warm / history_cold
(tiered time-series) and ts_samples (long-format metric stream).
Those tables already roll off to cold parquet files on a 14-day
horizon (see Prune + RolloffToParquet) — they're not the
authoritative source after roll-off, and a rollback that loses the
hot/warm window only loses the LAST 14 DAYS of high-resolution data.
That data is still derivable from the parquet snapshots (and from
the persistent energy_daily aggregate cache added in the previous
commit).

Snapshot what matters for rollback:

  - config (operator settings)
  - events (op log)
  - telemetry (last-known DER state)
  - battery_models (RLS coefficients)
  - prices / forecasts (market data)
  - ts_drivers / ts_metrics (interner dicts)
  - devices (hardware identity)
  - planner_diagnostics (recent plans)
  - notification_log
  - nova_ders
  - energy_daily (persistent aggregate cache)

Skip:
  - history_hot / history_warm / history_cold
  - ts_samples

Implementation: ATTACH the destination as `snap`, walk
sqlite_master to find every essential CREATE statement, rewrite the
leading identifier to target `snap.<name>`, replay it, then
`INSERT INTO snap.<table> SELECT * FROM main.<table>` for the
essential tables only. Single connection so the ATTACH binding
survives; DETACH on every exit path so the connection returns
clean to the pool. Tests:

  - TestSnapshotToCapturesLiveState — preserved (config + telemetry)
  - TestSnapshotToSkipsTimeSeriesTables — new: write history + ts
    rows, snapshot, assert essentials present + history/ts empty
  - TestSnapshotToRefusesExistingFile — preserved (stat-check now
    instead of relying on VACUUM INTO's implicit refusal)

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@frahlg
frahlg merged commit 1318588 into master May 25, 2026
1 check passed
@frahlg
frahlg deleted the perf/snapshot-skip-timeseries branch May 25, 2026 13:23

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa82de1285

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +188 to +196
for _, r := range items {
if r.objType != "table" {
continue
}
if snapshotExcludedTables[r.name] {
continue
}
if _, err := conn.ExecContext(ctx,
fmt.Sprintf("INSERT INTO snap.%q SELECT * FROM main.%q", r.name, r.name)); err != nil {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve one read snapshot across all table copies

This implementation no longer guarantees a transaction-consistent backup under live writes: each INSERT INTO snap...SELECT * FROM main... runs as its own implicit statement transaction, so concurrent writers can commit between table copies and produce a mixed-time snapshot (for example, config from T1 and devices/battery_models from T2). VACUUM INTO previously gave a single consistent snapshot, and this function is documented as safe during live traffic, so the copy phase should run under an explicit read transaction (BEGIN before schema/table reads, COMMIT after) to pin one source snapshot.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant