Repository navigation
perf(state): snapshot skips time-series tables — 1 GB → ~50 MB - #309
Conversation
… → ~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]>
There was a problem hiding this comment.
💡 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".
| 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 { |
There was a problem hiding this comment.
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 👍 / 👎.
Measured baseline (2026-05-25, live Pi)
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 settingsevents,telemetry,battery_models,prices,forecaststs_drivers/ts_metrics— interner dicts (tiny but referenced)devices— hardware identity (load-bearing for trained models)planner_diagnostics,notification_log,nova_dersenergy_daily— persistent aggregate cacheOut (excluded):
history_hot/history_warm/history_coldts_samplesImplementation
ATTACH destination as
snap, walksqlite_masterfor essential CREATE statements, rewrite identifier to targetsnap.<name>, thenINSERT INTO snap.<table> SELECT * FROM main.<table>. Single connection so ATTACH binding survives; DETACH on every exit path.Tests
TestSnapshotToCapturesLiveState— preservedTestSnapshotToSkipsTimeSeriesTables— new: write history + ts rows, snapshot, assert essentials present + history/ts emptyTestSnapshotToRefusesExistingFile— preserved (stat-check now instead of VACUUM INTO's implicit refusal)Full suite:
go test ./...green.Expected impact
🤖 Generated with Claude Code