Skip to content

Failed REPLACE PARTITION leaves old and new rows both active in MergeTree #123723

Open

Description

@clickgapai

Describe what's wrong

  • A failed plain MergeTree REPLACE PARTITION leaves both old and new parts active and visible.
  • Trigger: the covering empty part write fails (e.g. FAULT_INJECTED, MEMORY_LIMIT_EXCEEDED) during REPLACE PARTITION.
  • Expected: nothing changes after a failed replace; actual: duplicate old and new rows appear until the next successful replace or drop.
  • A later merge silently folds both generations into one part, hiding the duplication.

Root cause: StorageMergeTree::replacePartitionFrom commits the new parts before calling removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper. #122610 reordered that function to call createEmptyPart before removePartsFromWorkingSet; if createEmptyPart throws, the old parts are never removed but the new parts are already published.

Analysis details (evidence, affected locations, impact)

Why we believe this is a bug: StorageMergeTree::replacePartitionFrom commits the new parts (StorageMergeTree.cpp:3925), then calls removePartsInRangeFromWorkingSet (:3929). Since #122610, removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper runs createEmptyPart (MergeTreeData.cpp:7768) before removePartsFromWorkingSet (:7796). When createEmptyPart throws, the old parts are never removed, but the new parts are already published.

Affected locations:

Impact: OSS plain MergeTree: a failed REPLACE PARTITION, the usual atomic partition swap in ETL, leaves duplicate rows in the destination partition after the client was told the query failed. A client that reads the error as 'nothing changed' and reloads the partition some other way keeps the duplicates permanently. Only a successful retry of REPLACE PARTITION (or a DROP PARTITION) removes them. Before #122610, the same failure showed only the new rows. The old parts stayed on disk without a covering part, though, so a reload before cleanup brought them back (seen with DETACH/ATTACH in clickhouse-local).

Does it reproduce on most recent release?

No — only master (501d978d7b9d). The bug starts with #122610 (merged as e1541bf4adf5), which is in no release yet, so a release build (and ClickHouse Fiddle) still passes.

How to reproduce

Reproduce with the wide_part_writer_fail_in_add_streams failpoint enabled around the ALTER TABLE ... REPLACE PARTITION; only affects master since #122610 is unreleased.

▶ Run on ClickHouse Fiddle

-- A failed REPLACE PARTITION must not leave the partition with both the replaced and the replacing rows.

DROP TABLE IF EXISTS t_dst;
DROP TABLE IF EXISTS t_src;

CREATE TABLE t_dst (k UInt8, v String) ENGINE = MergeTree PARTITION BY k ORDER BY tuple()
SETTINGS min_bytes_for_wide_part = 0, min_rows_for_wide_part = 0;
CREATE TABLE t_src AS t_dst;

INSERT INTO t_dst VALUES (1, 'old1'), (1, 'old2');
INSERT INTO t_src VALUES (1, 'new1'), (1, 'new2'), (1, 'new3');

SYSTEM ENABLE FAILPOINT wide_part_writer_fail_in_add_streams;
ALTER TABLE t_dst REPLACE PARTITION 1 FROM t_src; -- { serverError FAULT_INJECTED }
SYSTEM DISABLE FAILPOINT wide_part_writer_fail_in_add_streams;

SELECT countIf(startsWith(v, 'old')) > 0 AND countIf(startsWith(v, 'new')) > 0 AS half_replaced FROM t_dst WHERE k = 1;

DROP TABLE t_dst;
DROP TABLE t_src;

Verified: this block reproduces on current master (501d978d7b9d) when pasted as is.

Expected behavior

A failed REPLACE PARTITION should leave the partition unchanged, as it did before #122610 reordered the covering-part creation ahead of the removal step.

Expected output of the reproducer above:

0

Error message and/or stacktrace

The new parts stay committed while the old parts stay active, so both old and new rows are visible until a later replace, drop, or merge.

Actual output of the reproducer above on master (501d978d7b9d):

1
Suggested fix

Write the covering part before the new parts are published, so that a failure aborts the whole REPLACE. For example, in replacePartitionFrom run the range removal before transaction.commit: a throw then rolls back the PreActive new parts. StorageMergeTree::movePartitionToTable already orders it this way, creating its covering parts before any commit (StorageMergeTree.cpp:4076-4077). Trade-off: renameTempPartAndAdd of the covering part then runs while the new parts are PreActive in the same partition, which needs checking. A cheaper option is to catch the createEmptyPart failure in this caller and remove the range without a covering part, as before #122610. That reopens the restart-resurrection window that 04338_replace_partition_no_resurrect_after_restart guards.

Additional context

Same pattern as #122805 (found by: lexical, vector, fix_path; vector: cosine distance 0.21 (agrees with the lexical leg)).

Open risks:

  • Replicated movePartitionToTable (StorageReplicatedMergeTree.cpp:10038) also calls the removal after publishing, after the destination ZK multi, so its failure behaviour changed too. Not exercised: no ZooKeeper on the test server.

Introduced by PR #122610, found during its automated review: the reproducer passes on the master build just before it merged and fails on the build with it. Severity P2 · Finding h_pr122610_001

cc @alexey-milovidov (from #122610)

Activity

  1. added
    comp-mergetree
    on Oct 3, 2026
  2. added
    blocker
    and removed on Oct 4, 2026
  3. added and removed
    blocker
    on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai_p2comp-mergetree

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions