Skip to content

Leave a part alone while a lightweight update of it is uncommitted - #119155

Merged
alexey-milovidov merged 7 commits into
masterfrom
mergetree-mutation-pending-patch-116686
Oct 3, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
mergetree-mutation-pending-patch-116686

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

On a plain MergeTree table, a heavyweight ALTER TABLE ... UPDATE (or DELETE) that runs while a lightweight UPDATE is still in flight silently dropped the acknowledged lightweight update: both statements returned success and the updated rows reverted to their previous values, with no error anywhere.

A lightweight update allocates its block number before it writes its patch part. StorageMergeTree::selectPartsToMutate selected a part for a mutation by data version alone, so a mutation started in that window read the part without the patch and wrote a part at a higher data version; the patch, committed afterwards, has a lower source data version and no longer applies to it.

ReplicatedMergeTree postpones such an entry in ReplicatedMergeTreeQueue::havePendingPatchPartsForMutation, and the plain engine has no equivalent - waitForCommittingInsertsAndMutations, its only drain primitive, deliberately skips CommittingBlock::Op::Update. The selection now leaves a part alone while an uncommitted lightweight update holds a block number at or below the version the mutation would produce, which is the selection-side counterpart of that guard.

Closes: #116686

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 to CHANGELOG.md):

Fixed a concurrent heavyweight ALTER TABLE ... UPDATE/DELETE silently dropping an acknowledged lightweight UPDATE on a plain MergeTree table.


Workflow [PR]
Sync PR [sync-upstream/pr/119155]

Version info

  • Merged into: 26.10.1.1374-master (included in 26.10 and later)

A lightweight update allocates its block number before it writes its
patch part, and a plain `MergeTree` selected a part for a heavyweight
mutation by data version alone. A mutation started in that window read
the part without the patch and wrote a part at a higher data version,
which the patch - committed with a lower source data version - no
longer applies to: the acknowledged lightweight update was silently
lost.

The selection now leaves such a part for a later round, which is the
counterpart of `havePendingPatchPartsForMutation` on the replicated
queue.

Closes: #116686
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8a53df6]

Summary: ✅


AI Review

Summary

This PR fixes a real plain MergeTree race where a heavyweight mutation could advance a part past an uncommitted lightweight UPDATE and make the later patch part inapplicable. The current implementation now snapshots pending lightweight-update block numbers during selectPartsToMutate, postpones any part whose next mutation version would overtake a still-uncommitted update, and re-triggers background scheduling when the update's CommittingBlock is finally released. The new stateless test uses a failpoint placed immediately after allocateBlockNumber(CommittingBlock::Op::Update) to exercise the exact race window deterministically. I did not find a remaining correctness issue in the patched path.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 8a53df6.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.50% 79.40% -0.10%

Changed lines: Changed C/C++ lines covered: 43/44 (97.73%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). labels Sep 10, 2026
Comment thread src/Storages/StorageMergeTree.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8a53df624 with master 63f946c53 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

244 translation units recompiled, 2615 s compile time in total, 244 of them have a recent master baseline.

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Fast test (arm_darwin) failure is unrelated: 05076_error_trace_file_offsets returns 1 0 instead of 1 1 on macOS, and the same failure shows up on other pull requests that touch nothing near it (#119114, #119115).

Related: #117449

alexey-milovidov and others added 3 commits September 18, 2026 02:04
Address the two review threads.

`StorageMergeTree::removeCommittingBlock` now triggers `background_operations_assignee`
when the released block is a `CommittingBlock::Op::Update`. The block is released by
`PlainCommittingBlockHolder` on every exit of a lightweight update, including the ones
that write no patch part, so a mutation that `selectPartsToMutate` left alone is
retried as soon as it is allowed to run instead of on the next backoff tick.

The test no longer waits for the `UPDATE` to appear in `system.processes`, which does
not prove that its block number is allocated. It enables the new
`mt_lightweight_update_pause_after_block_allocation` failpoint (`PAUSEABLE_ONCE`,
placed in `StorageMergeTree::updateLightweight` right after the block number is
reserved), waits for the pause with `SYSTEM WAIT FAILPOINT ... PAUSE`, fires the
`ALTER TABLE ... UPDATE`, checks that the mutation is postponed with
`PostponeReasons::PENDING_LIGHTWEIGHT_UPDATE`, and only then releases the update.
Renumbered 05184 -> 05227 after merging master.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@filimonov

Copy link
Copy Markdown
Contributor

Opened #122934 on top of this one (both commits kept). It covers the same class of race more broadly: besides a mutation running while a lightweight UPDATE is uncommitted, it also handles mutations registering out of order (#122083), merges crossing or recording a version that is allocated but not yet visible, and OPTIMIZE FINAL retrying with a stale merge plan. The supportsLightweightUpdate() guard is no longer needed there: selection takes one snapshot of committing_blocks and treats every pending Mutation/Update block as a boundary. If #122934 goes in, this PR can be closed.

@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

⚠️ AI verdict: not_sure — no significant changes across 32 queries analysed

This PR only affects MergeTree background mutations. selectPartsToMutate now holds back a mutation while a lightweight update with a lower block number has not committed yet, and releasing that update's block number now wakes the scheduler. The rest of the diff is a failpoint and a test, and none of it runs during read-only benchmark queries. The two flagged rows, tpch_adapted_1_official Q1 (-20.0%) and Q7 (-16.0%), stay not_sure: both fall on an unstable master baseline split around 2026-10-02 and have noisy history, so the speedups are not attributable to this change. No performance impact is expected from this PR.

clickbench

🟢 No significant changes

tpch_adapted_1_official

⚠️ 2 inconclusive

Flagged queries (2 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 1 not_sure 65 52 -20.0% <0.0001 Q1 is a read-only scan and aggregation and never reaches the mutation-selection or lightweight-update code this PR changes. The -20.0% comes from an unstable master baseline, so it is not a PR effect.
⚠️ 7 not_sure 81 68 -16.0% <0.0001 Q7 is a read-only multi-join SELECT and does not run StorageMergeTree::selectPartsToMutate or removeCommittingBlock. The -16.0% matches master's baseline shift, not this diff.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: 42c14309-22a3-4a25-bde5-0b1d2bd749d6
  • MIRAI run: e002ed77-4fba-4b72-9c84-38228a5439d5
  • PR check IDs:
    • clickbench_2933865_1790961069
    • clickbench_2933876_1790961069
    • clickbench_2933883_1790961069
    • tpch_adapted_1_official_2933894_1790961069
    • tpch_adapted_1_official_2933922_1790961069
    • tpch_adapted_1_official_2933937_1790961069

@alexey-milovidov alexey-milovidov left a comment

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.

LGTM.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 1, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 2, 2026
alexey-milovidov and others added 2 commits October 2, 2026 14:45
The test arms a fail point, and the `failpoint_tests_are_isolated` style check
requires such tests to be tagged both `no-parallel` and `no-fasttest`.

Merge queue failure: https://github.com/ClickHouse/ClickHouse/actions/runs/36954992663
PR: #119155

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
Merged via the queue into master with commit 72006b7 Oct 3, 2026
150 checks passed
@alexey-milovidov
alexey-milovidov deleted the mergetree-mutation-pending-patch-116686 branch October 3, 2026 02:17
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 3, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @filimonov, thanks. This pull request was merged on 2026-10-03 (merge commit 72006b7), so it does not need to be closed. #122934 is still open; because it includes both commits from this branch, merging the latest master into it should go through cleanly, and its broader handling of pending Mutation/Update blocks can then be reviewed on its own merits.

Related: #122934

filimonov added a commit to filimonov/ClickHouse that referenced this pull request Oct 3, 2026
Plain `StorageMergeTree` decided what a mutation, a merge or `OPTIMIZE FINAL` may
touch from the registered mutations only, while a version can exist before it is
registered: a mutation between its file write and its registration, a lightweight
`UPDATE` between block allocation and patch commit. Four races followed: two
mutations registered out of order because `mutate` released the alter lock before
`startMutation` (ClickHouse#122083); a mutation was applied to a part while a lightweight
`UPDATE` of it was uncommitted (ClickHouse#116686); a merge spanned an allocated version or
recorded a metadata mutation's version past it, so the update's patch was skipped
for the merged part; `OPTIMIZE FINAL` retried after waiting for running merges
with the predicate and the metadata snapshot built before the wait.

`committing_blocks` is the registry of allocated but not yet visible versions.
Selectors take a `CommittingBlocksSnapshot` (its `Mutation`/`Update` blocks
plus a watermark: the block counter at the copy, read under the allocation
mutex) while holding `currently_processing_in_background_mutex` and pass it to
the merge predicate. The first such block above a part's data version bounds
mutation selection, refuses merges across it, caps the merged part's recorded
version and patch materialization; parts (by `min_block`) and patches above
the watermark do not take part in that selection, so nothing allocated after
the copy can lie between two things a selection compares. `mutate` keeps the
alter lock through `startMutation`; `alter` keeps its rename mutation's block
reserved until the metadata is published or rolled back and finds the mutation
by version on rollback; `OPTIMIZE FINAL` rebuilds the predicate, the collector
and the metadata snapshot on every retry.

Built on top of ClickHouse#119155 (its two commits kept) and supersedes it: the
committing-block scan guarded by `supportsLightweightUpdate()` is replaced by the
snapshot; its reason string and test are kept.

Tests: 05291, 05292, 05294-05299, 05302, 05304 (failpoint-driven; 05294 arm 2,
05295 and the restored-part arm of 05297 are guards that also pass without the
fix), 05227 from ClickHouse#119155.

Closes: ClickHouse#122083
Closes: ClickHouse#116686

Signed-off-by: Mikhail Filimonov <[email protected]>
filimonov added a commit to Altinity/ClickHouse that referenced this pull request Oct 7, 2026
…boundaries, with two codex reviews

Codex round 1 on the reservation-bound fix found it insufficient (merge
predicate crosses an unregistered mutation, barrier ALTER misses it,
alter lock released before startMutation by an if-initializer scope).
Round 2 on the alter-lock fix accepted the production change and
rejected the PAUSEABLE failpoint in the test. The design proposes one
rule, superseding upstream PR ClickHouse#119155: an uncommitted Mutation or Update
reservation is a provisional mutation version for mutation selection,
merge predicate and patch selection, plus the alter-lock fix.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V8mZSGiD8iJumpJMiQnrmC
filimonov added a commit to Altinity/ClickHouse that referenced this pull request Oct 7, 2026
… the audit of upstream PR ClickHouse#119155

Design review round 1: invariant accepted; asked for an explicit
firstVersionReservationAfter helper instead of overloaded accessors,
distinct postpone strings, corrected lifetime argument (update holders
outlive patch commit), transaction-rollback recheck as an Antalya
prerequisite, the orphan-file limitation stated, the StorageAlias::alter
gap, and a test plan without the invalid mirror arm. Audit of ClickHouse#119155:
its guard is skipped when supportsLightweightUpdate() is false, which a
settings ALTER can cause while an update holds its reservation; its
SYSTEM WAIT FAILPOINT has no timeout. rev.2 incorporates all of it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V8mZSGiD8iJumpJMiQnrmC
filimonov added a commit to Altinity/ClickHouse that referenced this pull request Oct 7, 2026
…cisions on a coherent cut

Round 2 found the reservation snapshot taken before the parts are read
(in the merge predicate constructor and in PR ClickHouse#119155's mutation
selection), so a reservation allocated in between is missed while a part
committed above it is seen. rev.3 states the cut (parts and patches
first, then reservations), makes rule 3 the generalization of
min_update_block with the 0-as-infinity sentinel handled, wakes on
Unknown too, settles StorageAlias::alter (fix it here) and the Antalya
transaction recheck (own pull request first), replaces the green S3b
with two late-reservation tests, and makes S1, S2 and S3 deterministic.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V8mZSGiD8iJumpJMiQnrmC
filimonov added a commit to Altinity/ClickHouse that referenced this pull request Oct 7, 2026
…omic allocate-and-announce, fresh predicate after the OPTIMIZE FINAL wait, barrier waits for registration

Round 5 accepted the restated model as the shape a maintainer would take
and gave three exact closures: allocation and announcement are one step
under the selection mutex (MOVE announces before its parts lock; never
the background mutex under a parts lock); OPTIMIZE FINAL rebuilds its
predicate and collector after every wait that released the mutex; a
barrier ALTER waits for a pending mutation to register before
waitForMutation. Accessors renamed to boundary terminology, guard gets
an erase-while-locked operation, ClickHouse#119155's removeCommittingBlock wake-up
dropped, Debug logs for pending insert/erase, tests S6 and S7 added.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01V8mZSGiD8iJumpJMiQnrmC
filimonov added a commit to Altinity/ClickHouse that referenced this pull request Oct 7, 2026
filimonov pushed a commit to Altinity/ClickHouse that referenced this pull request Nov 21, 2026
…date of it is uncommitted

A mutation could pick a part while a lightweight `UPDATE` with a lower block number had not
written its patch part yet; the mutated part then skipped the patch and the acknowledged update
was lost. This branch needs it as the base of ClickHouse#124167 (lost lightweight `DELETE` in CI).

Upstream: ClickHouse#119155
Upstream merge commit: 72006b7
Commits of the upstream pull request:
- 8a53df6 Merge remote-tracking branch 'origin/master' into mergetree-mutation-pending-patch-116686
- 7638e85 Tag `05227_mutation_waits_for_lightweight_update` as `no-fasttest`
- d9f77ec Merge remote-tracking branch 'origin/master' into mergetree-mutation-pending-patch-116686
- 4398d33 Wake up the postponed mutation and make the test deterministic
- cf21123 Merge remote-tracking branch 'origin/master' into mergetree-mutation-pending-patch-116686
- ea880f9 Merge branch 'master' into mergetree-mutation-pending-patch-116686
- 8db4dc2 Leave a part alone while a lightweight update of it is uncommitted

Adaptation for antalya-26.8:
- None: applied as is.

(cherry picked from commit 72006b7)

Signed-off-by: Mikhail Filimonov <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). 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

3 participants