Repository navigation
Fix primary indexes of newly written parts staying in memory forever when PK cache is enabled - #115314
Fix primary indexes of newly written parts staying in memory forever when PK cache is enabled#115314azat wants to merge 3 commits into
Conversation
With `use_primary_key_cache = 1` and `prewarm_primary_key_cache = 1` a full-rewrite mutation built the new part's index in memory but, unlike merges and inserts, never moved it into the cache: it stayed pinned in the part (`primary_key_bytes_in_memory` in `system.parts`), unevictable until the part became outdated. Co-Authored-By: Claude Fable 5 <[email protected]>
Whether to save the index and marks in memory during write and whether to move them into the caches after commit were decided independently, with different sizes at different times: on mismatch the index stayed in the part, unevictable, and the saved marks were dropped. One decision per operation, carried in the writer settings, shared with the projection writers, reused for the move into the cache after commit. - v2: CachesToPrewarm in the writer settings instead of the save_* flags - v3: projection writers reuse the operation's decision (a projection index could stay in the part when the parent's index is absent) - v4: assert in moveIndexToCache Co-Authored-By: Claude Fable 5 <[email protected]>
Indexes loaded while the cache was disabled stayed pinned in parts and their projections, unevictable, until SYSTEM UNLOAD PRIMARY KEY or a restart; unloadIndex now also recurses into projections. Co-Authored-By: Claude Fable 5 <[email protected]>
use_primary_key_cache=1)|
Workflow [PR], commit [01309dd] Summary: ❌
AI ReviewSummaryThis PR makes the primary-index prewarm decision consistent across inserts, merges, mutations, and projections, and it unloads currently loaded primary indexes when Findings
Final Verdict
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 264/270 (97.78%) · Uncovered code |
|
|
||
| /// Indexes loaded in parts while the cache was disabled would stay there, unevictable. | ||
| if (has_primary_index_cache_been_enabled) | ||
| unloadPrimaryKeys(); |
There was a problem hiding this comment.
This only fixes parts that are already visible when the ALTER runs. Inserts, merges, and mutations snapshot CachesToPrewarm before commit, so a long-running write that started with use_primary_key_cache = 0 will still publish a new active part with its primary index kept in-memory after this ALTER, and this unloadPrimaryKeys() call will never touch it.
That means the new contract from the PR body is still false for in-flight writes: enabling use_primary_key_cache can still require a later SYSTEM UNLOAD PRIMARY KEY or restart to get rid of unevictable indexes. Can we either re-check the cache-enabled bit at finalize time, or explicitly unload the just-committed part when the setting flipped meanwhile?
| /// this part's own index is absent (e.g. a mutation that rebuilds only a projection). | ||
| for (const auto & [_, projection] : projection_parts) | ||
| projection->moveIndexToCache(index_cache); | ||
| projection->moveIndexToCache(prewarm_caches); |
There was a problem hiding this comment.
Now that projection parts also move their primary indexes into PrimaryIndexCache, the cleanup path needs the symmetric recursion too. MergeTreeData::unloadPrimaryKeysAndClearCachesOfOutdatedParts() still calls clearCaches() only on the parent part, and IMergeTreeDataPart::clearCaches() only removes this part's own cache key.
So when a part becomes outdated, its projection PK entries can stay in the cache for the whole old-part lifetime and evict active entries in the meantime. Please recurse through projection_parts from cache cleanup as well, not just from moveIndexToCache().
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Object file sizes31 object files changed (+6.43 KiB total), 0 added.
737 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units286 translation units recompiled, 2892 s compile time in total, 286 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.08 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix primary indexes of newly written parts staying in memory forever (unevictable, visible as
primary_key_bytes_in_memoryinsystem.parts) when the primary index cache is enabled (use_primary_key_cache). This affected parts produced by mutations, projections rebuilt by mutations, and parts that shrank belowmin_bytes_to_prewarm_cachesduring a merge or mutation. Additionally, enablinguse_primary_key_cacheviaALTER TABLE ... MODIFY SETTINGnow unloads primary indexes (including projections) that were loaded while the cache was disabled, without requiringSYSTEM UNLOAD PRIMARY KEYor a server restart.Workflow [PR]
Sync PR [sync-upstream/pr/115314]