Skip to content

Fix race between REPLACE PARTITION and background mutations - #96955

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-replace-partition-merge-blocker-scope
Feb 16, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-replace-partition-merge-blocker-scope

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 15, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fix a race condition in StorageMergeTree::replacePartitionFrom where the merges_blocker ActionLock was scoped inside the if/else block and destroyed before the actual partition replacement
  • A background mutation could start on parts in the target partition after the blocker was released, 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.

Test plan

  • Existing test 02861_alter_replace_partition_do_not_wait_mutations_on_unrelated_partitions should stop being flaky

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix a race condition between REPLACE PARTITION and background mutations that could result in both old and new data being visible after the replace.

Closes #73783

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.779

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]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [91e9ec0]

Summary: ❌

job_name test_name status info comment
Integration tests (amd_msan, 2/6) failure
test_storage_delta/test.py::test_single_log_file[1-s3] FAIL cidb
test_storage_delta/test.py::test_single_log_file[0-s3] FAIL cidb
test_storage_delta/test.py::test_single_log_file[0-azure] FAIL cidb
test_storage_delta/test.py::test_single_log_file[1-local] FAIL cidb
test_storage_delta/test.py::test_partition_by[1-s3] FAIL cidb
test_storage_delta/test.py::test_partition_by[0-s3] FAIL cidb
test_storage_delta/test.py::test_partition_by[0-azure] FAIL cidb
test_storage_delta/test.py::test_partition_by[1-local] FAIL cidb
test_storage_delta/test.py::test_checkpoint[1-s3] FAIL cidb
test_storage_delta/test.py::test_checkpoint[0-s3] FAIL cidb
65 more test cases not shown
Integration tests (amd_msan, 3/6) failure
test_storage_azure_blob_storage/test.py::test_create_new_files_on_insert FAIL cidb
test_storage_azure_blob_storage/test.py::test_overwrite FAIL cidb
test_storage_azure_blob_storage/test.py::test_insert_with_path_with_globs FAIL cidb
test_storage_azure_blob_storage/test.py::test_put_get_with_globs FAIL cidb
test_storage_azure_blob_storage/test.py::test_azure_glob_scheherazade FAIL cidb
test_storage_azure_blob_storage/test.py::test_storage_azure_get_gzip[bin] FAIL cidb
test_storage_azure_blob_storage/test.py::test_storage_azure_get_gzip[gz] FAIL cidb
test_storage_azure_blob_storage/test.py::test_schema_inference_no_globs FAIL cidb
test_storage_azure_blob_storage/test.py::test_schema_inference_from_globs FAIL cidb
test_storage_azure_blob_storage/test.py::test_simple_write_account_string_table_function FAIL cidb
91 more test cases not shown
Integration tests (amd_msan, 4/6) failure
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[y-azure-1] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[y-azure-2] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[y-local-1] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[y-local-2] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[identity(y)-s3-1] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[identity(y)-s3-2] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[identity(y)-azure-1] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[identity(y)-azure-2] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[identity(y)-local-1] FAIL cidb
test_storage_iceberg_with_spark/test_writes_create_partitioned_table.py::test_writes_create_partitioned_table[identity(y)-local-2] FAIL cidb
285 more test cases not shown
Integration tests (amd_msan, 5/6) failure
test_ssh/test.py::test_simple_query_with_openssh_client FAIL cidb
test_ssh/test.py::test_no_queries_from_file FAIL cidb
test_ssh/test.py::test_no_selects_into_outfile FAIL cidb, issue
test_ssh/test.py::test_no_inserts_from_infile FAIL cidb
test_ssh/test.py::test_create_table FAIL cidb
test_ssh/test.py::test_simple_query_with_paramiko FAIL cidb
test_ssh/test.py::test_paramiko_password FAIL cidb
test_ssh/test_options_propagation_enabled.py::test_options_and_settings_propagation FAIL cidb
test_ssh/test_options_propagation_enabled.py::test_no_server_logs_file FAIL cidb, issue
Integration tests (amd_msan, 6/6) failure
test_checking_s3_blobs_paranoid/test.py::test_when_s3_timeout_at_listing FAIL cidb
test_storage_delta_disks/test.py::test_single_log_file[1-s3] FAIL cidb
test_storage_delta_disks/test.py::test_single_log_file[0-s3] FAIL cidb
test_storage_delta_disks/test.py::test_single_log_file[1-local] FAIL cidb
test_storage_delta_disks/test.py::test_single_log_file[0-azure] FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Feb 15, 2026
alexey-milovidov and others added 3 commits February 15, 2026 15:33
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;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is brilliant.

@alexey-milovidov
alexey-milovidov merged commit d7208f3 into master Feb 16, 2026
138 of 143 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-replace-partition-merge-blocker-scope branch February 16, 2026 06:24
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 16, 2026
@alesapin alesapin added the post-approved Approved, but after the PR is merged. label Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

post-approved Approved, but after the PR is merged. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test 02861_alter_replace_partition_do_not_wait_mutations_on_unrelated_partitions is flaky

3 participants