#284 The catalogue text rebuild reads one snapshot
–
- github
- github/openchronology/timeline-app
- github/openchronology/timeline-app#284
- pull request
- state:merged
Closes #261. **Cause.** The background catalogue text rebuild (`refreshText` in `server/store.mjs`) ran `oc_event_text` under READ COMMITTED. `oc_events` (and `oc_events_lineage` for forks) is a volatile PL/pgSQL function, so every node and moment lookup in its walk took a fresh snapshot. When a save or deletion committed partway through, the walk followed a node ID it had already read to a row that was gone, and raised "Broken timeline index". For a fork it's worse: a lookup tries one version (live or retired) and then the other in separate statements, so a row moving from live to retired between the two (a save to the upstream, or the upstream being deleted) is missed in both. This isn't limited to tests: deleting or saving a timeline, or saving a timeline that forks read through to, while a rebuild runs could log it. **Fix.** The rebuild's read now runs in a `REPEATABLE READ, READ ONLY` transaction, so the walk finishes on the index it started with. The optimistic update after it already applies only at the revision it read, so a deleted timeline's update matches no row, and a saved one's leaves the text stale for the save's own refresh. A genuinely broken index still raises. **Test.** `test/catalogue-text-postgres.mjs` holds `oc_moments` locked so the rebuild stops after reading the root and nodes, deletes the timeline there, and checks that nothing is logged. It failed 3/3 runs without the change (with the PL/pgSQL context `oc_events … line 17 at RAISE`) and passed 5/5 with it, in about 7 s. A heavier race (a 20,000-moment fork rebuilt while its upstream was saved and deleted) reproduced both the `oc_events` and `oc_events_lineage` errors in 3 of 7 runs before the fix and none in 10 after, but took over a minute, so it isn't included. Checked locally: `npm test` (305 pass), `npm run test:platform:postgres`, `shared-history-postgres.mjs` 6×, plus `incremental`, `fork-read-through`, `search` and `deletion` Postgres tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_011fFY8eruEi1xAkkMBni13B https://github.com/openchronology/timeline-app/pull/284
Related
- #261 Background catalogue text refresh can fail with "Broken timeline index" while timelines are deleted
- #285 Rebuilding a large fork's catalogue text after many upstream saves is slow
- #290 A small timeline's catalogue text sorts its moments instead of walking
- Merge pull request #284 from openchronology/fix/261-text-refresh-race
Open #284 The catalogue text rebuild reads one snapshot in the timeline