{"record":{"id":"778c08cdc206f9e2","repo":"benbjohnson/litestream","slug":"fetch-db-position-w","errorCode":null,"errorMessage":"fetch db position: %w","messagePattern":"fetch db position: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"store.go","lineNumber":790,"sourceCode":"\t\treturn nil, &DBNotReadyError{Reason: \"page size not initialized\"}\n\t}\n\n\tdstLevel := lvl.Level\n\n\t// Ensure we are not re-compacting before the most recent compaction time.\n\tprevCompactionAt := lvl.PrevCompactionAt(time.Now())\n\tdstInfo, err := db.MaxLTXFileInfo(ctx, dstLevel)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"fetch dst level info: %w\", err)\n\t} else if dstInfo.CreatedAt.After(prevCompactionAt) {\n\t\treturn nil, ErrCompactionTooEarly\n\t}\n\n\t// Shortcut if this is a snapshot since we are not pulling from a previous level.\n\tif dstLevel == SnapshotLevel {\n\t\tpos, err := db.Pos()\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"fetch db position: %w\", err)\n\t\t}\n\t\tif dstInfo.MaxTXID != 0 && dstInfo.MaxTXID >= pos.TXID {\n\t\t\treturn nil, ErrNoCompaction\n\t\t}\n\n\t\tinfo, err := db.Snapshot(ctx)\n\t\tif err != nil {\n\t\t\treturn info, err\n\t\t}\n\t\tdb.Logger.InfoContext(ctx, \"snapshot complete\", \"txid\", info.MaxTXID.String(), \"size\", info.Size)\n\t\treturn info, nil\n\t}\n\n\t// Fetch latest LTX files for both the source & destination so we can see if we need to make progress.\n\tsrcLevel := s.levels.PrevLevel(dstLevel)\n\tsrcInfo, err := db.MaxLTXFileInfo(ctx, srcLevel)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"fetch src level info: %w\", err)","sourceCodeStart":772,"sourceCodeEnd":808,"githubUrl":"https://github.com/benbjohnson/litestream/blob/4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3/store.go#L772-L808","documentation":"Store.CompactDB failed reading the database's current write position via db.Pos() while shortcutting the snapshot-level compaction path. db.Pos() reads the in-memory replication position of the open database, so failures here indicate internal state inconsistency (the position is normally cached). The underlying cause is wrapped with %w.","triggerScenarios":"CompactDB(ctx, db, SnapshotLevel) where db.Pos() returns an error instead of the cached TXID position.","commonSituations":"Calling compaction on a DB that is in an unexpected internal state (closed concurrently, not fully initialized), or an internal bug/race between store monitoring and DB shutdown.","solutions":["Ensure the DB is open and running when compaction monitors execute; do not close the DB while compaction is in flight.","Retry the compaction cycle; the monitor (monitorCompactionLevel) will attempt the next interval anyway.","If persistent, restart Litestream to rebuild DB state.","Report a bug with the wrapped error if Pos() consistently fails on a healthy, open database."],"exampleFix":"// before\n_, err := store.CompactDB(ctx, db, litestream.SnapshotLevel)\n// after\n_, err := store.CompactDB(ctx, db, litestream.SnapshotLevel)\nif err != nil && db.SyncStatus() != litestream.SyncStatusRunning {\n    // skip compaction: DB not in a running state\n}","handlingStrategy":"retry","validationCode":"if db.SyncStatus() != litestream.SyncStatusRunning {\n    return fmt.Errorf(\"db not running; skip compaction\")\n}","typeGuard":"func dbHealthy(db *litestream.DB) bool {\n    return db != nil && db.SyncStatus() == litestream.SyncStatusRunning\n}","tryCatchPattern":"if _, err := store.CompactDB(ctx, db, litestream.SnapshotLevel); err != nil {\n    // compaction monitor retries next cycle; treat as recoverable\n    log.Printf(\"snapshot compaction deferred: %v\", err)\n}","preventionTips":["Avoid closing DBs while compaction monitors are active","Restart Litestream if Pos() persistently fails","Report persistent failures on open DBs as bugs","Gate compaction on DB sync status"],"tags":["go","litestream","compaction","internal"],"backgroundTag":"internal-invariant-violation","analyzedSha":"4ed7a308f6271ebfd2b0a6e4b70b03011a37e4a3","analyzedAt":"2026-09-06T18:29:25.564Z","contentChangedAt":"2026-09-06T18:29:25.564Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}