Repository navigation
fix(mem_wal): dedup repeated row positions when training the flushed PK sidecar - #9595
Merged
Merged
Conversation
…PK sidecar The memtable primary-key index is a skiplist multiset: inserting the same (value, position) twice stores it twice, and a row is indexed more than once when a WAL replay re-indexes batches the flusher already indexed. The flushed PK sidecar is trained straight from that index, so its BTree page carried a repeated row id. Loading the page builds a RoaringBitmap with from_sorted_iter, which rejects the repeat, so every probe of that generation failed with "RowAddrTreeMap::from_sorted_iter called with non-sorted input" and any LSM scan touching it returned an error. In production one 2-row generation with page rows [(a,0),(a,0),(b,1)] broke reads of a 54k-row table for weeks. Skip a position equal to the previous one within a value group when building training batches; positions within a group are already sorted. Co-Authored-By: Claude Fable 5 <[email protected]>
Contributor
There was a problem hiding this comment.
Deduplicating at training export enforces one (value, position) per new sidecar entry while preserving the memtable index's override detection. The focused training and sidecar regressions pass.
The reported 9.0.0 scan failure is not reproduced by current main's read path: #9462 made BTree search tolerate repeated row IDs, and I did not find a current replay path that double-indexes a batch. This patch does not rewrite sidecars already written by older versions; affected deployments still need a reader upgrade or index rebuild.
Xuanwo
approved these changes
Sep 29, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The memtable primary-key index (
BTreeMemIndex) is a skiplist multiset: inserting the same(value, position)twice stores it twice. A row is indexed twice when a WAL replay (replay_wal→insert_batches_parallel) re-indexes buffered batches the WAL flusher (flush_from_batch_store) already indexed.The flushed generation's PK sidecar (
create_pk_index/write_pk_sidecar) is trained straight frompk_training_batches(), so the on-disk BTree page inherits the repeated row id. Whencontains_keyslater loads that page,FlatIndex::try_newbuilds aRowAddrTreeMapviaRoaringBitmap::from_sorted_iter, which requires strictly increasing input, and every probe of that generation fails with:Because the block list is built for every LSM scan, one bad generation makes every read of the table fail. Observed in production (lance 9.0.0): a 2-row generation whose sidecar page was
[(item:a, 0), (item:a, 0), (item:b, 1)]brokeliston a 54k-row table for weeks, deterministically, for anylimitlarge enough to enable thePkBlockFilterExec.This is the same failure signature as #7230 (base-table BTree merge). The fixes proposed there were rejected in favour of fixing the training input; this PR does exactly that for the MemWAL sidecar path.
Fix
In
BTreeMemIndex::to_training_batches, skip a position equal to the previous one within a value group. Positions within a group are already sorted, so this is a one-comparison dedup at the only place the multiset is turned into training data.The write path is left as a multiset on purpose:
insert_and_check_neighborsrelies on neighbor inspection for thepk_has_overridessignal, and deduping there would change its cost profile.Verification
test_btree_index_to_training_batches_dedups_repeated_positions: indexes a batch twice into both the fixed-int and bytes backends; the training batches carry each position once. Fails without the fix.sidecar_from_doubly_indexed_memtable_is_probeable(block_list): trains a real on-disk sidecar from a doubly-indexedIndexStore, opens it withopen_pk_index, and probes it. On the 9.0.0 read path this is the exact production failure; onmainthe probe path no longer builds aRowAddrTreeMapeagerly (perf(index): accelerate RQ scale search #8924), so the test passes either way there, but it pins the invariant for thesearch/into_row_addr_setpaths that still do.cargo test -p lance --lib mem_wal: 781 passed.cargo fmt --check,cargo clippy --lib --tests -D warningsclean.🤖 Generated with Claude Code