Skip to content

Rebuild the text index without holding writers up - #129

Merged
venkat1701 merged 1 commit into
mainfrom
feature/online-text-rebuild
Nov 25, 2026
Merged

venkat1701 merged 1 commit into
mainfrom
feature/online-text-rebuild

Conversation

@venkat1701

@venkat1701 venkat1701 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Stacked on #127. Merge after #124 to #127, and retarget to main once #127 is in.

REBUILD TEXT INDEX used to run inside the caller's transaction. The whole new index sat in heap, its pages counted against the transaction quota, and a text write committed during the rebuild made the commit redo all of it under the commit lock.

What it does now:

  • Runs in its own transaction, so it is refused inside BEGIN and on a generation pinned with AT.
  • Reads the sections of its snapshot once, sorts postings and statistics in batches of 524,288 written to scratch/ in the database directory, and merges them (at most 128 files at a time) into bulk-built trees whose pages spill as they fill.
  • Catches up by diffing sections, revisions and documents between its snapshot and newer ones and replaying the difference through the text derivation only, in a scratch workspace. It repeats this up to eight times, so the commit applies only the last small difference.
  • Defers payload erasures while a rebuild runs, because removing a purged section's postings needs its text. Without this, a purge during a rebuild left stale postings (a mutation test shows it).

Engine changes: PostingIndex.bulk, Workspace.scratch, and a node bound in ChangeLog. A tree rebuilt from scratch shares no pages with the old one, so the old diff rule read both trees under the commit lock to find few or no changes. It now gives up after 64 nodes plus 16 per change found and logs the root.

An independent review found no correctness problem in the result. It did find that the first version still diffed the whole index under the lock and re-tokenised the corpus once per pass; both are fixed here, with tests that fail on the old code.

Tests:

  • Full build: engine 274, database 386, server 41.
  • TextRebuildTest covers: matching the live index entry for entry from memory, from sorted runs, and through a multi-level merge; the page quota; commits folded in after the build and after catch-up, starting from a corrupted index; two rebuilds racing a writer; failure; branches; asynchronous durability with a checkpoint during the rebuild.
  • Mutation checks killed: skipping the commit-time catch-up, removing revisions before sections, dropping merge input, not summing statistics, removing the erasure gate. Two survivors are equivalent today: syncing the transaction's trees after each catch-up (only observable if the fast path ever followed a catch-up that changed something) and dedupe in the merge (only on 64-bit term hash collisions).

Live over the wire:

  • The memory harness gives 0 failures before and after SIGKILL (531 commits replayed). The rebuild re-indexed 287 sections, and search matched the Python BM25 on 8 of 8 queries in both tenants. The other tenant's admin is refused, and the mutated reference still fails with 90.
  • An HTTP race on 3,000 seed documents ran 6 rebuilds of about 9,000 to 9,800 sections, 2.7 to 5.8 s each. 901 writer statements, 67 of them purges, ran during them with a median of 21 ms, p99 of 117 ms and max of 244 ms. The index afterwards matched a quiet rebuild on all 12 queries, and the writer saw no errors.

Known limit: if writes arrive faster than the catch-up rounds can absorb, the difference applied at commit can still be large. The docs say so.

Merge order for the memory stack: #128 (CI fix), #124, #125, #126, #127, #129 (online text index rebuild), #130 (starter kit). Retarget each to main once the one below it merges. A private preview image built from #129 merged with #128 is at ghcr.io/metacoglabs/hstore-memory:0.2.0-dec80f2.

REBUILD TEXT INDEX used to run as one operation inside the caller's
transaction. The whole new index lived in heap, its pages counted
against the transaction quota, and a concurrent text write made the
commit redo the entire rebuild under the commit lock.

The rebuild now runs in its own transaction. It reads the sections of
its snapshot once, sorts postings and statistics in batches written to
scratch/, and merges them into bulk-built trees whose pages are
spilled as they fill. After the build it diffs the sections, revisions
and documents of its snapshot against newer ones and replays the
difference through the text derivation alone, so the commit only
applies the last small difference. Payload erasures wait for a running
rebuild, since removing a section's postings needs its text.

Engine changes:
- PostingIndex.bulk builds a posting index from ascending entries with
  the same inline and promotion rules as single adds.
- Workspace.scratch gives a workspace that runs one derivation.
- ChangeLog gives up on a diff that reads more than 64 nodes plus 16
  per change and logs the root instead. A rebuilt tree shares no pages
  with the old one, so the old rule walked both trees under the lock.

The statement is now refused inside BEGIN and on a pinned generation.
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