Skip to content

Refuse a fetch of a partition whose patches the source has not applied - #119181

Merged
alexey-milovidov merged 21 commits into
masterfrom
fetch-partition-refuses-patches-118599
Oct 31, 2026
Merged

alexey-milovidov merged 21 commits into
masterfrom
fetch-partition-refuses-patches-118599

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

ALTER TABLE ... FETCH PARTITION (and FETCH PART) copies base parts only and silently left the source's patch parts behind - the parts that hold the acknowledged lightweight UPDATEs the base parts do not carry yet. After ATTACH PARTITION the copy served the pre-update values, with no error and no warning, while the same SELECT on the source returned the updated ones.

A patch part lives in a partition of its own (patch-<hash>-<partition_id>), so the filter by the requested partition can never match it. Every sibling part-relocation command - MOVE PARTITION TO TABLE, REPLACE PARTITION FROM, ATTACH PARTITION FROM, DETACH PARTITION, DETACH PART - refuses this situation through assertNoPatchesForParts and tells the user to run APPLY PATCHES first; the fetch was the one boundary with no such guard.

The fetch now refuses it the same way. Only the source's part names are known here, so the patches are compared by the data version their names carry (the mutation component of a patch part's name is the highest data version it patches): a patch the base parts have already been mutated past - which is what APPLY PATCHES does - is no reason to refuse anything, so the documented migration path works as soon as the patches are applied.

Closes: #118599

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):

ALTER TABLE ... FETCH PARTITION/FETCH PART no longer produces a copy that silently lacks the acknowledged lightweight UPDATEs of the source: it refuses a source partition with unapplied patch parts and asks for APPLY PATCHES, as the other part-relocation commands already do.


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

Version info

  • Merged into: 26.11.1.987-master (included in 26.11 and later)

A fetch copies base parts only, and a patch part - which holds the
acknowledged lightweight updates its base parts do not have yet - lives
in a partition of its own, so it was never fetched: the copy silently
served pre-update values. Every other part-relocation command refuses
that through `assertNoPatchesForParts` and asks for `APPLY PATCHES`.

Only the source's part names are known here, so the patches are compared
by the data version a patch part's name carries: one the base parts have
already been mutated past is no reason to refuse anything, so the fetch
works as soon as the patches are applied.

Closes: #118599
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c83113d]

Summary: ✅


AI Review

Summary

The PR makes FETCH PARTITION and FETCH PART reject sources with unapplied lightweight updates or newer overlapping part versions on another replica. The current code and regression coverage address the previous findings. No new actionable findings remain.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 10, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. label Sep 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c83113da5 with master 3c82fa8cc (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.

Object file sizes

3 object files changed (+106.23 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/StorageReplicatedMergeTree.cpp.o 4.70 MiB 4.81 MiB +106.22 KiB (+2.21%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Both CI failures are unrelated to this change.

The stress test hit Logical error: 'Unexpected return type from globalIn. Expected Nullable(UInt8). Got UInt8' (STID: 3262-40c8), which is the sample-block planning of a globalIn over a Distributed table - nothing this touches.

Related: #114254

The Fast test (arm_darwin) failure is 05076_error_trace_file_offsets returning 1 0 instead of 1 1 on macOS, which also shows up on unrelated pull requests (#119114, #119115).

alexey-milovidov and others added 2 commits September 18, 2026 01:24
…n on `FETCH PARTITION`

The partition-level guard took the highest data version among the partition's
base parts as its bound. A part inserted after an `UPDATE` carries a higher
version than the update's patch, so with such a part in the partition the patch
that is still pending for the older parts passed unnoticed and the fetch again
produced a copy without the acknowledged update. Use the lowest data version
instead, exactly as `assertNoPatchesForParts` does for the other part-relocation
commands.

The test now pins a two-part layout with merges stopped: the update touches the
older part only and a later insert sits above the patch. It also checks that the
fetch of the unpatched later part alone stays allowed. Renumbered from `05187`,
which is taken on master.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
alexey-milovidov and others added 3 commits September 24, 2026 12:37
…d `FETCH PART`

A lightweight update commits its patch part on one replica, and the others
learn about it through a `GET_PART` log entry. A replica that has not executed
that entry yet lists the patched base part but not the patch, and neither the
random replica `FETCH PART` picks nor the `log_pointer` heuristic of
`FETCH PARTITION` proves that the chosen source has executed it. So the guard
now takes the union of patch parts over all replicas of the source table, and
still compares them against the chosen replica's base parts.

The new test detaches the replica that committed the update, leaving a replica
with stopped fetches as the only active source; without this change both
fetches copy the pre-update rows from it.

Addresses review threads on #119181

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119181&sha=e2d04af768d2ea88f0cff4c51723db642aa5d6fa&name_0=PR&name_1=Upgrade%20check%20(amd_release) and provide a fix in a separate PR. If the fix is already in progress, link it here.

The Upgrade check (amd_release) failed with Logical error: Part all_1_9_1 intersects previous part all_1_1_1 raised from MergeTreeData::PartLoadingTree::add while loading a plain MergeTree table at server start - this PR only changes FETCH PARTITION / FETCH PART in StorageReplicatedMergeTree. Possibly related open PRs: #122375, #120980.

The read_in_reverse_order_final perf regression (7.9 ms -> 9.3 ms on ARM) is on a read path this PR does not touch. I have merged master to get a fresh run.

@groeneai

groeneai commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

This is the same failure as on #120910 (STID 3562-39cd), not caused by this PR.

The table is test_1.dst from 03755_insert_select_duduplication.sh. In the stress phase, which runs the previous release 26.9.2.8, TRUNCATE TABLE dst failed five times in MetadataOperationsHolder::commit() with an injected memory fault, after its empty covering parts had already been renamed to their final names. The rollback left all_1_1_1 on disk, 0.1 s later a merge wrote all_1_9_1 from level 0 parts, and the new server, started about 5 minutes later (inside old_parts_lifetime), cannot load the table.

The fix is in progress, in two open PRs for this exact producer:

#120980 is a different producer (leftovers of REPLACE PARTITION).

Because the leftover is written by the previous release binary, merging master does not stop this red: in 30 days it hit 29 unrelated PRs (25 of them in Upgrade check (amd_release)), and it will keep doing so until the fix is in a 26.9 release.

alexey-milovidov and others added 2 commits October 4, 2026 15:10
…llReplicas` explicit

`tryGetChildren` already throws on every error except `ZNONODE`, so a replica
whose `parts` cannot be read is never silently skipped. Compare against
`ZNONODE` explicitly and say so in a comment.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. The Upgrade check (amd_release) red (Part all_1_9_1 intersects previous part all_1_1_1) is left to #122375 and #121708. I merged master again and pushed a change for the review thread about getPatchPartsOfAllReplicas.

alexey-milovidov and others added 3 commits October 5, 2026 17:04
…with a materialized update

`clearUnusedPatchParts` drops a patch part as soon as the base parts of its own
replica have been mutated past it, without waiting for the other replicas. After
`UPDATE` + `APPLY PATCHES`, a lagging replica can still hold the old base part
while no replica holds the patch anymore, so the patch lookup over all replicas
saw nothing and `FETCH PART`/`FETCH PARTITION` from that replica silently copied
pre-update rows.

The fetch now also refuses when any replica of the source has a part that
intersects a part to be fetched with a data version above both the fetched
part's data version and the lowest block they share - i.e. a newer version of
the same rows. Parts that merely cover a different block range (inserted later
or merged differently) never satisfy this.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The private CI replaces `ReplicatedMergeTree` with `SharedMergeTree`, which does
not support `FETCH PARTITION`/`FETCH PART`, so the tests failed there with
`NOT_IMPLEMENTED` or produced different results (`CH Inc sync`).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alexey-milovidov and others added 2 commits October 6, 2026 11:48
…t on Keeper in 05321

The tests create replicas with explicit Keeper paths and replica names, which does not
work with `DatabaseReplicated` (05227 and 05255 failed in the `DBReplicated` job), so tag
them `no-replicated-database`, as `03350_alter_table_fetch_partition_thread_pool` does.

Address review: 05321 now waits until the patch part is gone from Keeper's
`/replicas/r1/parts` as well, not only from `system.parts`, so the test exercises the
stale-replica check rather than the check for remaining patches.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119181&sha=84c20efafd4ac729f9892350652853632782db0a&name_0=PR
Related: #119181

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failures: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119181&sha=84c20efafd4ac729f9892350652853632782db0a&name_0=PR and provide a fix in a separate PR. If the fix is already in progress, link it here.

They are unrelated to this PR and also fail on other PRs during the last 5 days:

  • Stateless tests (amd_llvm_coverage, s3 storage, DBReplicated, parallel, 1/3): 05060_create_or_replace_scoped_grants, 05059_mergeTreeCodecBlockCounts_access, 05258_text_index_low_cardinality_dictionary, 05237_with_expression_alias_scope_descendant, 05317_named_collection_kafka_nested_override_access, 03636_storage_alias_metadata_access_rights, 05297_named_collection_override_access_sources, 04825_quantile_deterministic_alter_add_modify_column, 05211_aggregate_parameter_type_name_round_trip
  • Stateless tests (arm_asan_ubsan, azure, parallel): 04633_async_insert_select_general (timed out waiting for the async insert)

The PR's own tests 05227/05255 failed in the DBReplicated job because they create replicas with explicit Keeper paths; that is fixed in this PR by tagging them no-replicated-database.

@groeneai

groeneai commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

The nine DBReplicated failures already have fixes in progress:

04633_async_insert_select_general has no fix yet. This is its only failure in 30 days: in case 4 (wait_for_async_insert=0) the two rows were still not visible after the test's 20 polls, and the 3 reruns passed. I'm investigating it and will follow up here with a PR (or my findings).

@groeneai

groeneai commented Oct 6, 2026

Copy link
Copy Markdown
Collaborator

04633_async_insert_select_general: fixed in #124263.

The rows were not lost. In that job the wait_for_async_insert=0 flush took 27.4 s, 27.1 s of it waiting for the filesystem cache priority lock while writing a 2-row part to cached_azure, longer than case 4's 20-iteration poll (about 19 s on that lane). The PR drops the iteration cap, so the poll waits until the rows land and the runner's per-test timeout stays the bound.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. The DBReplicated failures are covered by #123790, #124021 and #124109, and 04633_async_insert_select_general is covered by #124263. CI on the current head (904f69a) is green, there are no unresolved review threads, and the PR is mergeable, so this branch needs no further change.

kute1654 pushed a commit to kute1654/ClickHouse that referenced this pull request Oct 8, 2026
…for_async_insert=0 poll

Case 4 inserts with `async_insert=1&wait_for_async_insert=0` and then polled
`count()` 20 times (about 19 s on the arm_asan_ubsan azure lane) before printing
the rows. `wait_for_async_insert=0` gives no visibility deadline, and on ClickHouse#119181
the background flush took 27.4 s, 27.1 s of it waiting for the filesystem cache
priority lock while writing a 2-row part to the write-through `cached_azure` disk
(Azure writes took 5 ms). The rows landed 8 s after the final SELECT, so the
reference diff was caused only by the 20-iteration cap.

Loop until the rows land instead. The runner's per-test timeout stays the bound,
so a lost insert still fails. Same remedy as ClickHouse#123897.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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.

Is the check heavy?

Read the `parts` of all source replicas with one batched Keeper request
instead of a round trip per replica, and find the chosen parts that
intersect a part of another replica with a binary search instead of a scan,
so the check is linear in the number of parts instead of quadratic in the
number of parts of the partition.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 > Is the check heavy?

No. It only runs on FETCH PARTITION / FETCH PART, which are rare manual commands, and it is small next to the fetch itself:

  • Keeper: the check lists /replicas/*/parts for every replica of the source table. FETCH PARTITION already lists the parts of the chosen replica, so the extra cost is one listing per additional replica. For FETCH PART this is new; before, it only did exists checks. As of e49e58e, all replicas are read in one batched multi-read request instead of one round trip per replica.
  • CPU: every part name of every replica is parsed once. Only parts in the requested partition are compared with the chosen parts. Before, that comparison scanned all chosen parts, which is quadratic in the partition's part count. The chosen parts are the active parts of one replica, so they don't intersect each other, and e49e58e finds the overlapping ones with a binary search. The total is now O(replicas × parts × log(parts in partition)): with 3 replicas and 100k parts, that is a few hundred thousand part-name parses, i.e. milliseconds.

The parts are not downloaded and there are no extra Keeper reads per part. The only thing that grows with the table is the size of the parts listing, and that listing is already read by FETCH PARTITION and by replica cleanup.

alexey-milovidov and others added 2 commits October 10, 2026 19:08
…t in `FETCH PARTITION` checks

The `parts` node of a replica in Keeper still lists parts the replica has
merged or otherwise replaced until their cleanup removes them, so the
stale-version and patch checks could refuse a correct fetch because of a
part that is no longer live.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119181&sha=e49e58e183c0c712d8e2a10760be9e444b85ddb6&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29 (05210_lightweight_update_materialized_cte_in_ignore fails with UNKNOWN_TABLE under parallel replicas; it fails in many unrelated PRs since 2026-10-08) and provide a fix in a separate PR. If the fix is already in progress, link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

Not caused by this PR. The fix is #123121 (mine, open).

This run fails query 26 of the test with UNKNOWN_TABLE on _materialized_cte_c_*, the parallel replicas race from #112642: the initiator's local plan waits for the materialized CTE only in its own branch, so the query sent to the other replicas can start before the CTE table exists. With a slow CTE body that query fails 10 of 10 runs on master and passes 10 of 10 with #123121. Since 2026-10-08 the test failed on 73 PRs and on 7 master commits. #123121 is mergeable and has been waiting for review since 2026-09-30 (requested from @ novikd and @ devcrafter).

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks @groeneai. The 05210_lightweight_update_materialized_cte_in_ignore failure under parallel replicas is left to #123121. CI on the current head (4124155) is green, there are no unresolved review threads, and the PR is mergeable, so this branch needs no further change.

alexey-milovidov and others added 2 commits October 22, 2026 21:57
…d Keeper request

`FETCH PARTITION` read the `parts` of the chosen replica with a separate
`getChildren` and then again inside the batched read of the `parts` of all
replicas. Take them from the batched read instead, so the new checks add
exactly one extra Keeper request per attempt (besides the `replicas` listing),
regardless of the number of replicas, and the chosen parts and the patch parts
come from the same read.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@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.

Acceptable.

@alexey-milovidov alexey-milovidov self-assigned this Oct 31, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 31, 2026
Merged via the queue into master with commit 26f120e Oct 31, 2026
181 checks passed
@alexey-milovidov
alexey-milovidov deleted the fetch-partition-refuses-patches-118599 branch October 31, 2026 22:38
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. 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.

FETCH PARTITION silently loses acknowledged lightweight UPDATEs: the source's patch parts are never fetched

3 participants