Repository navigation
Fix race between REPLACE PARTITION and background mutations - #96955
Merged
alexey-milovidov merged 4 commits intoFeb 16, 2026
Merged
Conversation
The `merges_blocker` `ActionLock` in `replacePartitionFrom` was scoped inside the `if/else` block and destroyed before the actual partition replacement, allowing background mutations to start on parts in the target partition. A mutation could then complete on an already-removed part and add a new version back to the working set, "resurrecting" old data alongside the new data from `REPLACE PARTITION`. Move the `merges_blocker` to function scope so it stays alive until `replacePartitionFrom` returns. The bug was introduced in #59138. Closes #73783 Co-Authored-By: Claude Opus 4.6 <[email protected]>
Contributor
|
Workflow [PR], commit [91e9ec0] Summary: ❌
|
The test runs concurrent `REPLACE PARTITION` and `ALTER TABLE ... UPDATE` mutations on the same partition. After each replace, it verifies that the partition contains exactly the expected number of rows from the source table. If the `merges_blocker` is released too early (the bug fixed in this PR), background mutations can "resurrect" old parts, causing the row count to exceed expectations. Co-Authored-By: Claude Opus 4.6 <[email protected]>
… mutations 1. In `MutatePlainMergeTreeTask`, hold `data_parts_lock` across both `renameTempPartAndReplaceUnlocked` and `transaction.commit` to eliminate the PreActive window. Previously, these were two separate lock acquisitions, allowing REPLACE PARTITION's `removePartsInRangeFromWorkingSet` to miss the PreActive mutation result, which would then be promoted to Active and "resurrect" old data. 2. In `selectPartsToMutate`, check per-partition merge blockers under `currently_processing_in_background_mutex`. Previously, the only partition-level blocker check was in `scheduleDataProcessingJob` outside the mutex, creating a race where a mutation could be selected after `stopMergesAndWaitForPartition` finished waiting but before REPLACE PARTITION ran `removePartsInRangeFromWorkingSet`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
| /// actually remove old parts via removePartsInRangeFromWorkingSet. Without this, a mutation | ||
| /// could "resurrect" old data by completing on an already-removed part and adding a new version | ||
| /// of it back to the working set. | ||
| ActionLock merges_blocker; |
Member
Author
There was a problem hiding this comment.
This is brilliant.
alexey-milovidov
deleted the
fix-replace-partition-merge-blocker-scope
branch
February 16, 2026 06:24
1 of 2 tasks
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.
Summary
StorageMergeTree::replacePartitionFromwhere themerges_blockerActionLockwas scoped inside theif/elseblock and destroyed before the actual partition replacementREPLACE PARTITIONmerges_blockerto function scope so it stays alive untilreplacePartitionFromreturnsThe bug was introduced in #59138.
Test plan
02861_alter_replace_partition_do_not_wait_mutations_on_unrelated_partitionsshould stop being flakyChangelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a race condition between
REPLACE PARTITIONand background mutations that could result in both old and new data being visible after the replace.Closes #73783
🤖 Generated with Claude Code
Version info
26.2.1.779