fix(invertedindex): stamp flushed posting keys with the deduped doccount - #114
Merged
Conversation
flushPendingWrites stamped a posting row's key doccount with the pre-dedup buffer length (updateIndex appends a docid per keyword occurrence without dedup), while the row value is sorted+deduped. For hot keywords the inflated doccount crosses the merger's maxInvertedIndexSize/2 "already batched" threshold, so those rows were quarantined from compaction forever — inflating index disk and hot-term scan cost (search results were unaffected; doccount is read only by the merger). The delete and merge paths already stamped the true deduped count; only the common flush path was wrong. Route all three writers through a single named seam defaultWriteInvertedIndex that builds the key from encodeInvertedValueCounted's post-dedup unique count in the same pass that encodes the value, so a stamped doccount can never again disagree with the row it labels. No on-disk format change, no reindex; existing inflated rows heal only on a natural re-index (merger self-heal was out of scope). Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
codetrek
approved these changes
Jul 11, 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.
What & why
flushPendingWritesstamped a posting row's on-disk keydoccountwith the pre-dedup bufferlength.
updateIndexappends a docid to a keyword's buffer once per occurrence without dedup (foringest speed), so a hot keyword (e.g. every function name in a file contains
get) accumulates a flushrow whose buffer length ≫ its unique docid count. But the row VALUE is
encodeInvertedValue(docids),which sorts and dedups. So the key's
doccountcould be many times the number of docids actuallystored.
The background merger uses that
doccountto decide "already well batched": a row withdoccount > maxInvertedIndexSize/2is skipped from merge groups (keywords_merger.go:242), and akeyword group with a high
DocCount/len(rows)is early-returned (rewriteIndex). An inflateddoccounttherefore permanently quarantines hot-keyword rows from compaction — inflating
indexdisk usageand the number of rows a hot-term
Searchmust scan.doccount(3rd return ofdecodeInvertedKey) is read only bythe merger;
Search/GetDocsread the value bytes. This is a build-throughput / footprint /defeated-compaction defect, not a wrong-results bug.
invertedindex_internal.goeven names this exact "inflated doccount → quarantined forever" hazard) —only the common flush path was wrong.
Fix
Make the stamped
doccountalways equal the value's post-dedup unique count, computed in the samepass that encodes the value, and route all three writers through one seam:
encodeInvertedValueCounted(docids) ([]byte, int)— the existing encoder body, now also returning thepost-
Compactunique count (len(us)== the varint count).encodeInvertedValuedelegates to it.defaultWriteInvertedIndex(idx, batch, tableId, kw, docids, tick)— a single named seam that buildsthe key from that unique count, so a stamped
doccountcan never again disagree with the row itlabels. flush / delete / merge all call it (delete & merge are behavior-identical — they already
passed a deduped set).
No on-disk format change (the key layout is unchanged; only the
doccountvalue in new keys becomescorrect), no reindex, no StorageVersion bump. Existing inflated rows on disk heal only on a natural
re-index — merger self-heal was considered and deliberately left out of scope.
Tests
Two new behavior tests (real
Indexon at.TempDirKV, real on-disk format):TestFlush_StampsDedupedDoccount— a keyword flushed with duplicate occurrences: asserts the stampeddoccountequals the value's unique docid count. RED before (3 ≠ 1), GREEN after.TestMerge_CompactsHotKeywordRows— several inflated hot-keyword rows (MaxInvertedIndexSize=10,each
doccount 6 > 5): asserts the merger compacts them and the searchable docid set is preserved.RED before (rows quarantined, count unchanged), GREEN after.
encodeInvertedValueCountedanddefaultWriteInvertedIndexare both 100% covered; the seam's 10 testoverrides + 1 direct caller were migrated; full package passes with
-race.🤖 Generated with Claude Code
via Happy