Repository navigation
Reject non-transactional removal of parts created by uncommitted transactions - #110494
tiandiwonder wants to merge 3 commits into
Conversation
…art removal A non-transactional `ALTER TABLE ... DROP PARTITION` covers all active parts, including parts created by transactions that have not committed yet (it selects covered parts ignoring MVCC visibility). Since d6350c1, `VersionMetadata::setAndStoreRemovalTID` wrote `removal_csn = Tx::NonTransactionalCSN` together with `removal_tid` in a single update, so for such parts the version metadata ended up with `removal_csn` set while `creation_csn` was still unresolved, and `validateInfo` raised a logical error `creation_csn is not set while removal_csn is set` (aborting debug and sanitizer builds). Restore the pre-refactor write order (`creation_csn` is always persisted before `removal_csn`): defer the `removal_csn` fill in `setAndStoreRemovalTID` and `updateCSNIfNeeded` until `creation_csn` is resolved, and teach `validateInfo` that a bare non-transactional `removal_tid` over an unresolved creation is a valid deferred state. Once the creating transaction commits, the next metadata update fills `removal_csn` (`creation_csn > removal_csn` is already exempted for `Tx::NonTransactionalCSN`). Closes: ClickHouse/clickhouse-private#64528 Co-Authored-By: Claude Fable 5 <[email protected]>
|
Workflow [PR], commit [072ca1a] Summary: ❌
AI ReviewSummaryThis PR changes plain Findings❌ Blockers
Final VerdictRequest changes. The logical-error exception is fixed, but the current head still allows publishing uncommitted source data via |
…sactions A non-transactional `ALTER TABLE ... DROP PARTITION` covers all active parts ignoring MVCC visibility, so it can cover a part whose creating transaction has not committed yet. Since d6350c1, `VersionMetadata::setAndStoreRemovalTID` persisted `removal_csn = Tx::NonTransactionalCSN` together with `removal_tid`, so for such parts `removal_csn` was persisted while `creation_csn` was still unresolved, and `validateInfo` raised a logical error `creation_csn is not set while removal_csn is set` (aborting debug and sanitizer builds). Fail close instead (supersedes the previous commit, which deferred the `removal_csn` write and let the removal silently discard the uncommitted data): `setAndStoreRemovalTID` now rejects a non-transactional removal with `SERIALIZATION_ERROR` while the creating transaction is still running, and `MergeTreeData::Transaction::commit` checks all covered parts before locking any of them - a non-transactional removal mark cannot be undone once persisted, and the previously collected `locked_parts_to_cleanup` was dead code (populated, never consumed), so a multi-part DROP failing on the Nth part left the earlier parts permanently marked as removed while still Active. The dead code is removed. Closes: ClickHouse/clickhouse-private#64528 Co-Authored-By: Claude Fable 5 <[email protected]>
| if (covering_part) | ||
| continue; | ||
| for (const auto & covered : covered_parts) | ||
| covered->version->checkNonTransactionalRemovalIsPossible(); |
There was a problem hiding this comment.
This pre-pass only checks the creator transaction; it does not reserve the covered parts themselves. The second pass can still fail on the Nth part for a different SERIALIZATION_ERROR, for example if that part is already removal_tid-locked by a concurrent transactional DROP PART / DROP PARTITION. By then parts 1..N-1 have already gone through setAndStoreNonTransactionalRemovalTID, so they become invisible and eligible for cleanup even though the outer non-transactional DROP PARTITION failed. To actually fail close, this first phase needs to acquire/validate the real removal locks (or otherwise guarantee that the later write phase cannot still fail on one part only) before any removal_tid is stored.
… removal Non-transactional `REPLACE PARTITION FROM` and `MOVE PARTITION TO TABLE` remove the old parts via `removePartsInRangeFromWorkingSet` after committing the cloned/moved parts, bypassing the pre-check in `MergeTreeData::Transaction::commit`. A rejected `REPLACE` left a half-replaced destination: the old committed part already removed, the cloned part already published, and the uncommitted part kept. Check earlier instead: - `removePartsFromWorkingSet` checks all parts before mutating any of them, so every non-transactional multi-part removal path is all-or-nothing; - `replacePartitionFrom` checks the active parts inside `drop_range` right after taking the parts lock, before renaming and committing the clones; - `movePartitionToTable` checks the moved source parts after taking both parts locks, before publishing anything in the destination table. Since MOVE publishes the moved data in the destination table, it uses a stricter predicate (`checkCreationIsCommitted`): the creating transaction must be committed, so a rollback happening between source part selection and the final check cannot leak rolled-back rows into the destination (a rollback marks `creation_csn = RolledBackCSN` before removing the part from the working set, and a commit is irreversible once the check passes under the parts lock). The parts lock serializes these checks against concurrent part publish, so they are race-free. Test cases for REPLACE and MOVE added. Related: ClickHouse/clickhouse-private#64528 Co-Authored-By: Claude Fable 5 <[email protected]>
| /// replaced range contains a part of a still-running transaction (see | ||
| /// checkNonTransactionalRemovalIsPossible), and at that point the cloned parts would | ||
| /// already be committed and could not be undone. | ||
| if (replace && !local_context->getCurrentTransaction()) |
There was a problem hiding this comment.
This only pre-checks the destination parts that REPLACE is about to remove. The source side is still unguarded: src_parts above are collected with getVisibleDataPartsVectorInPartition, and in the non-transactional case that returns every active part, including one whose creation_tid is still running. cloneAndLoadDataPart then stamps the clone with Tx::NonTransactionalTID, so ALTER TABLE ... REPLACE PARTITION ... FROM can publish another transaction's uncommitted rows into the destination even if the source transaction later rolls back.
Can we require every src_part to pass checkCreationIsCommitted() before publishing the clones here (same guard as MOVE PARTITION TO TABLE)? ATTACH PARTITION FROM goes through the same helper, so it needs the same protection if we want to preserve MVCC on the source side too.
| /// A non-transactional removal mark cannot be undone once persisted, so check all parts | ||
| /// before mutating anything (see checkNonTransactionalRemovalIsPossible). | ||
| for (const DataPartPtr & part : remove) | ||
| part->version->checkNonTransactionalRemovalIsPossible(); |
There was a problem hiding this comment.
checkNonTransactionalRemovalIsPossible() only proves that the creator transaction is no longer running; it does not reserve the part itself. If remove = [p1, p2] and p2 was already removal_tid-locked by a concurrent transactional DROP PART / DROP PARTITION before we entered here, the loop below will still successfully call setAndStoreNonTransactionalRemovalTID() for p1 and then throw from lockRemovalTID() on p2. That leaves p1 invisible / outdated even though the outer non-transactional removal failed, so the new "check all parts before mutating anything" contract is still violated for direct multi-part removals.
Can we acquire / validate the actual removal locks for the full remove set up front (or otherwise guarantee the write phase cannot still fail on one part) before persisting any non-transactional removal mark?
|
🕵 Cross-linking: #105116 fixes the same defect ( It does close the review finding still open here about the pre-pass not reserving the covered parts: instead of checking and then stamping, The two should probably be reconciled into one change rather than merged independently. |
|
@groeneai could you pick up the one part of this that is still broken? Verified on current master, so it survives #105116: with session A holding |
|
Closing as superseded by #105116, which fixed the same defect on 2026-09-12 with a stronger design: it reserves the removal locks for the whole batch ( |
|
Acknowledged. I am investigating the source side of |
|
Opened #121308 for the source side. Your case reproduces on current master: with Two corrections to the mechanism, neither of which changes your conclusion:
The rest of your analysis holds, including that the check must not be gated on the |
Closes: https://github.com/ClickHouse/clickhouse-private/issues/64528
Problem
A non-transactional
ALTER TABLE ... DROP PARTITIONthat covers a part inserted by a not-yet-committed transaction fails with a logical errorcreation_csn is not set while removal_csn is set to 1, which aborts the server in debug and sanitizer builds. Deterministic reproducer: session A runsBEGIN TRANSACTION; INSERT INTO t VALUES (1);and stays open; session B runsALTER TABLE t DROP PARTITION ID 'all'. This also fires in stress runs of01169_old_alter_partition_isolation_stress.shwhen the query fuzzer moves theDROP PARTITIONout of its transaction.Root cause
The
VersionMetadatarefactor (d6350c1) madesetAndStoreRemovalTIDwriteremoval_tidandremoval_csn = Tx::NonTransactionalCSNin a single update. A non-transactional removal selects covered parts ignoring MVCC visibility, so it can cover a part whosecreation_csnis still unresolved (the creating transaction is running). Persistingremoval_csnfirst violates the write-order invariant thatcreation_csnis persisted beforeremoval_csn(the pre-refactorisVisiblechasserted!had_removal_csn || had_creation_csn), andvalidateInforaises the logical error. Only the non-transactional path can reach this state: a transactional remover picks covered parts from its MVCC snapshot and never sees uncommitted parts of other transactions.Solution
Fail close: reject the non-transactional removal instead of removing data whose fate is not decided yet (removing it would make the creator's
COMMITsucceed while its rows are silently gone).VersionMetadata::checkNonTransactionalRemovalIsPossiblethrowsSERIALIZATION_ERRORwhen the part'screation_csnis unresolved andtryGetCSN(creation_tid)shows the creating transaction is still running. Committed (CSN just not written back yet) and rolled-back creators proceed as before.setAndStoreRemovalTIDcalls it for non-transactional tids — the single choke point of every non-transactional removal.MergeTreeData::Transaction::commitpre-checks all covered parts before locking any of them for non-transactional operations. A non-transactional removal mark cannot be undone once persisted, and the existinglocked_parts_to_cleanupwas dead code (populated, never consumed) — without the pre-pass, a multi-partDROP PARTITIONfailing on the Nth part left parts1..N-1permanently marked as removed while still Active, making them invisible to transactional readers (verified empirically). The dead code is removed.The client sees
Code: 650. SERIALIZATION_ERROR: Cannot remove data object ...: it was created by transaction ... which is not committed yet. Wait for the transaction to commit or roll back, and retry— the standard retryable conflict signal of the transactions MVCC model. After the creating transaction finishes, the sameDROP PARTITIONsucceeds.REPLACE PARTITION FROMandMOVE PARTITION TO TABLE, which remove the old parts viaremovePartsInRangeFromWorkingSetafter committing the cloned/moved parts (a rejectedREPLACEused to leave a half-replaced destination). NowremovePartsFromWorkingSetchecks all parts before mutating any (every non-transactional multi-part removal is all-or-nothing),replacePartitionFromchecks the active parts inside the drop range before renaming and committing the clones, andmovePartitionToTablechecks the moved source parts before publishing anything in the destination.MOVEpublishes the moved data in another table, it uses the strictercheckCreationIsCommitted: the creating transaction must be committed — a rollback markscreation_csn = RolledBackCSNbefore removing the part from the working set, so rejecting only still-running creators would leave a window where rolled-back rows get committed into the destination. A commit is irreversible, so once this check passes under the parts lock the moved data cannot retroactively become rolled back.Adjacent paths verified: background merges never meet uncommitted parts (once transactions are used, merges begin their own transaction and filter invisible parts); non-transactional
OPTIMIZEon such tables is already rejected upstream withABORTEDbefore touching version metadata.The new test covers: rejection with both a committed and an uncommitted part in the partition (and that the committed part is not left locked — a retried
DROPafterCOMMITremoves everything), theROLLBACKvariant, the committed-parts control, and theREPLACE PARTITION FROM/MOVE PARTITION TO TABLErejection with byte-identical destinations. The transactional isolation tests (01168*,01169*,01170,01171) pass locally with the fix.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a logical error (
creation_csn is not set while removal_csn is set) when a non-transactionalALTER TABLE ... DROP PARTITIONremoves a part inserted by a transaction that has not committed yet. Such an operation is now rejected withSERIALIZATION_ERRORand can be retried after the transaction commits or rolls back.Documentation entry for user-facing changes