Repository navigation
Conversation
With an uncommitted `BEGIN TRANSACTION; INSERT INTO src ...` in flight, a non-transactional `ALTER TABLE dst REPLACE PARTITION ... FROM src` succeeded and published that row into `dst`. Rolling the source transaction back then emptied `src` but left `dst` holding the row permanently, so a destination table durably kept rows that were never committed anywhere. `ATTACH PARTITION ... FROM` and `CREATE TABLE ... CLONE AS`, which are built on the same statement, behaved the same way. Reachable only with `allow_experimental_transactions` enabled. `getVisibleDataPartsVectorInPartition` applies `filterVisibleDataParts` only when a transaction is present; with none it returns every active part, including one whose creating transaction is still running. `replacePartitionFrom` then clones those parts with `clone_params.txn = nullptr`, so each destination part is created with `Tx::NonTransactionalTID` and is committed and durable on the spot, where the source rollback cannot reach it. Master already states the invariant this violates, on `NonTransactionalRemovalKind::Republish` and on `isCreationCommitted()`: data whose creation is not committed must not be republished somewhere a commit cannot be taken back. `movePartitionToTable` enforces it. `replacePartitionFrom` checked only the destination parts it discards, and only when `replace` was true, so the source parts were never checked at all. The new `MergeTreeData::checkPartsCanBeRepublishedNonTransactionally` throws `SERIALIZATION_ERROR` when a part's creation is not committed. It is called on the source parts from `StorageMergeTree::replacePartitionFrom` and from `StorageReplicatedMergeTree::replacePartitionFromImpl`, and the existing `Republish` branch of `checkPartsCanBeRemovedNonTransactionally` now delegates to it, so the rule has one implementation. Not gated on `replace`: the pre-existing destination-side check is, which is why `ATTACH ... FROM` had no coverage, and both statements clone the source identically. A plain `MergeTree` source with a `ReplicatedMergeTree` destination is the documented migration direction and leaked the same way, so the replicated path is fixed too; it needs no `!txn` guard because that destination does not support transactions, so the query never runs in one. Not reusing `checkPartsCanBeRemovedNonTransactionally(src_parts, Republish)` directly, which would have been a one-line change: it also throws when another transaction holds the part's removal lock. `REPLACE`/`ATTACH ... FROM` never remove the source parts, so that leg would refuse a legal clone whenever a concurrent transactional `DROP PARTITION` on the source held the lock, reporting a removal that is not happening. The creation-committed half is the whole requirement here. Self-`REPLACE`, where source and destination are the same table, gets no exemption: republishing an uncommitted part's rows under a committed creation is unsafe even within one table. The background `executeReplaceRange` clone is left alone. The initiator runs the guard before the log entry is created, so executors only ever see committed parts, and throwing in a replication-queue executor would stall the queue. One existing message changes, from "Cannot non-transactionally move object ..." to "... republish ...", because the predicate now serves both callers. No test asserted the old text. Validated on a debug build: all four surfaces refused with `SERIALIZATION_ERROR` and left the destination empty, while the same statements still succeed once the source transaction commits. The new test fails on the unfixed binary in both of its oracles. A 65-test sweep of every stateless test mentioning `REPLACE PARTITION`, `ATTACH PARTITION ... FROM`, `MOVE PARTITION TO TABLE` or `CLONE AS` has an identical failure set before and after the change, and the new test passes 50/50 with settings randomization.
Drop the sibling-predicate navigation note and the duplicate of the Republish invariant already stated on the enumerator; fold the replicated call site's note into one line.
|
Two questions this is likely to raise, answered up front:
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 0 findings), plus an independent
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-10:20260921-091100 |
Pre-PR validation gate (click to expand)
|
|
Workflow [PR], commit [313cc30] Summary: ✅
AI ReviewSummaryThis PR adds a source-side Findings
|
| Metric | Baseline | Current | Δ |
|---|---|---|---|
| Lines | 88.00% | 88.00% | +0.00% |
| Functions | 91.80% | 91.70% | -0.10% |
| Branches | 79.30% | 79.30% | +0.00% |
Changed lines: Changed C/C++ lines covered: 30/31 (96.77%) · Uncovered code
`StorageReplicatedMergeTree::replacePartitionFrom` expands `ATTACH PARTITION ALL FROM` into an unordered set of partition ids and calls `replacePartitionFromImpl` once per id. Each of those calls commits its own destination parts and creates its `REPLACE_RANGE` log entry before the next id is looked at, so the republish check this branch added inside `replacePartitionFromImpl` could only refuse the partition it was called for: with one committed and one uncommitted partition in the same source, the statement published whichever committed partitions it reached first and only then threw `SERIALIZATION_ERROR`. Measured on a ten-partition source, the destination was left holding partitions 5, 6, 7 and 9; the subset varies, because the id set is unordered. Check every source partition in `replacePartitionFrom` before the loop starts, so a refusal cannot leave part of the statement applied. The check inside `replacePartitionFromImpl` stays, and is the authoritative one: the pre-pass releases the source parts lock before the loop re-reads each partition, so without it a transaction inserting into the source in between would still be cloned. The pre-pass is what makes the ordinary case, where the uncommitted part is already there when the statement starts, refuse without publishing anything. Closing the remaining window would require the whole multi-partition statement to be atomic, which it has never been: `assertNoPatchesForParts`, the empty-source check and `CANNOT_ASSIGN_ALTER` can all throw from the same loop today. One ordering change is visible to users: when one source partition holds an uncommitted part and another has unapplied patches, the error is now `SERIALIZATION_ERROR` instead of whichever condition the first visited partition hit. `StorageMergeTree::replacePartitionFrom` needs no equivalent change. It already collects every source part for `ATTACH PARTITION ALL FROM` into one vector and checks them in a single call before it clones anything. Reported by the AI review on ClickHouse#121308.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units240 translation units recompiled, 2536 s compile time in total, 240 of them have a recent master baseline. |
| src_parts_to_republish.insert(src_parts_to_republish.end(), partition_parts.begin(), partition_parts.end()); | ||
| } | ||
| } | ||
| src_data.checkPartsCanBeRepublishedNonTransactionally(src_parts_to_republish); |
There was a problem hiding this comment.
This only fixes the plain/ReplicatedMergeTree entrypoints. The new regression test immediately opts out of --replace-replicated-with-shared because that rewrite lands on an engine “whose partition commands this check does not reach” (tests/queries/0_stateless/05235_replace_attach_partition_from_uncommitted_source.sh). That means the SharedMergeTree variant can still non-transactionally republish rows from an uncommitted source part, so the PR's REPLACE / ATTACH / CLONE AS contract is still false in that supported mode. Please wire the same checkPartsCanBeRepublishedNonTransactionally guard into the SharedMergeTree partition-copy path (or land the private counterpart in lockstep) and drop the no-shared-merge-tree exclusion so the regression is covered there too.
There was a problem hiding this comment.
Verified, and the call you ask for cannot be added from this repository. Measured on this tree at 313cc309:
MergeTreeData::replacePartitionFromis pure virtual (MergeTreeData.h:2004) and has exactly two overrides here:StorageMergeTree.h:324andStorageReplicatedMergeTree.h:924. Both now run the guard, the replicated one over every source partition before the first clone.StorageSharedMergeTreehas no definition in this repository: no file matchesStorageSharedMergeTree*, the header<Storages/StorageSharedMergeTree.h>is not present, and every reference to the class sits behind#if CLICKHOUSE_CLOUD(src/Interpreters/MergeTreeTransaction.cpp:16-18,src/Storages/System/StorageSystemReplicas.h:14-15). Its override of that pure virtual is in the closed-source tree, so I can neither add the call nor test whether it already refuses, and I will not assert either way about its current behavior.- The predicate was written for that reuse:
checkPartsCanBeRepublishedNonTransactionallyis a publicMergeTreeDatamember (MergeTreeData.h:995), not a private helper of either storage, so the counterpart there is one call on the source parts before the clone. I am putting the gap on the record here for whoever owns that engine.
On dropping no-shared-merge-tree: it is honored in two places, --cloud mode (tests/clickhouse-test:3164-3169) and --replace-replicated-with-shared (:3285-3291), and neither flag is passed by any workflow in this repository (zero hits under ci/ and .github/). In --cloud mode this test is already skipped by its zookeeper tag two lines below, so the tag only decides anything under the engine rewrite, which maps ReplicatedMergeTree to SharedMergeTree (:293). Dropping it adds no coverage here, and unless that closed-source override already refuses it turns the replicated-destination cases red there. I would rather the exclusion be dropped by the change that adds the call than ship a knowingly red test; 260 stateless tests carry the tag for the same reason.
I did consider splitting the file so the four plain MergeTree cases keep running under the rewrite, which does not touch them. That adds a second file for paths this repository's CI already runs, including the s3-storage job, so I left it as one file.
|
Dear @tuanpach, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
ALTER TABLE ... REPLACE PARTITION FROMandALTER TABLE ... ATTACH PARTITION FROM(andCREATE TABLE ... CLONE AS, which is built on the latter) no longer publish source parts belonging to a transaction that has not committed: a later rollback of that transaction used to leave the destination table holding rows that were never committed. Such an operation is now refused withSERIALIZATION_ERRORand succeeds on retry once the source transaction finishes.Description
Related: #110494 (reported there) and #105116 (fixed the removal side, left the source side open).
Needs
allow_experimental_transactions, so not reachable by default.With an uncommitted
BEGIN TRANSACTION; INSERT INTO src VALUES (42)in flight, a non-transactionalALTER TABLE dst REPLACE PARTITION ID 'all' FROM srcsucceeded and published that row. A rollback then emptiedsrcbut leftdstholding the row permanently.ATTACH PARTITION ... FROMandCREATE TABLE ... CLONE ASbehaved the same, as did a plainMergeTreesource with aReplicatedMergeTreedestination: that destination never holds transactional parts of its own, but the source it clones from can.Root cause: without a transaction,
getVisibleDataPartsVectorInPartitiondoes no MVCC filtering and returns every active part, including one whose creating transaction is still running.replacePartitionFromclones those withtxn = nullptr, so each destination part is created withTx::NonTransactionalTID: committed and durable on the spot, where the source rollback cannot reach it.movePartitionToTablealready enforces this invariant throughisCreationCommitted().Change: a new
MergeTreeData::checkPartsCanBeRepublishedNonTransactionallythrowsSERIALIZATION_ERRORwhen a part's creation is not committed. It is called on the source parts fromStorageMergeTree::replacePartitionFromand fromStorageReplicatedMergeTree::replacePartitionFromImpl, and is not gated onreplace(the pre-existing destination-side check is, which is whyATTACH ... FROMhad no coverage). The existingRepublishbranch delegates to it, so the rule has one implementation. On aReplicatedMergeTreedestination,ATTACH PARTITION ALL FROMnow validates every source partition before the first is cloned (raised in review), so it cannot publish a subset and then refuse.Validation: new
05235fails on the unfixed binary in both oracles and passes 50/50 under settings randomization. The 65 stateless tests covering these commands have an identical failure set before and after.Workflow [PR]
Sync PR [sync-upstream/pr/121308]