Skip to content

Fix projection lost or stale on a replica that fetched parts before applying ADD PROJECTION - #123185

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-projection-unloaded-after-fetch
Oct 16, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-projection-unloaded-after-fetch

Conversation

@groeneai

@groeneai groeneai commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

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

Fixed a projection that a ReplicatedMergeTree replica fetched with a part before applying the ALTER TABLE ... ADD PROJECTION that added it: merges dropped the projection, and mutations kept its pre-mutation data, so queries answered from the projection returned wrong results.

Description

On 26.8 (measured on 26.8.10.6), after ALTER TABLE ... ADD PROJECTION on a ReplicatedMergeTree with concurrent inserts, a replica that fetched a part before applying that ALTER drops the projection on the next merge, or keeps the projection's pre-mutation data after ALTER UPDATE, so queries answered from the projection return wrong results.

This makes 03715_projection_settings_zookeeper flaky (master job, 26.8 job): in both server logs a replica fetches a part while applying the ALTER and its merge logs Projection p2 is not merged because some parts don't have it.

Root cause: loadProjections loads only the projections of the table metadata at load time, so a part fetched ahead of its table keeps p.proj in its checksums but never loads p, and merges and mutation planning only look at loaded projections.

Change: a projection that is in the part's checksums but not loaded is treated like a loaded one. Merges rebuild it when every source part has it; mutations include it in their rebuild decision, also for lightweight delete in rebuild mode and on packed part storage (where the mutation failed forever with NOT_IMPLEMENTED). Commit-order projections keep their unconditional rebuild.

Alternative: postpone GET_PART until the replica applies the pending ALTER_METADATA. I did not, because fetches are deliberately not ordered after metadata alters and an ALTER_METADATA can wait behind a long mutation, stalling replication of every new part.

The new test fails on master and 26.8.10.6 and passes with the fix on compact, wide and packed parts.

🤖 Generated with Claude Code


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

Version info

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

A replica can fetch a part before it applies the ALTER that adds a
projection: the part keeps `<p>.proj` on disk and in its checksums, but
`loadProjections` only loads the projections of the table metadata at
load time, so `p` is never loaded. Merges then drop `p` ("Projection p
is not merged because some parts don't have it"), and the other replica
refetches the projection-less part after CHECKSUM_DOESNT_MATCH, so both
replicas lose it. Mutations skip `p`, hardlink its stale directory and
load it, so queries answered from the projection return pre-mutation
data; on packed part storage the hardlink fails with NOT_IMPLEMENTED
and the mutation never finishes.

Treat a projection listed in the part's checksums but not loaded like a
loaded one: merges rebuild it when every source part carries it, and
mutation planning and the lightweight-delete rebuild mode include it.
Commit-order projections keep their unconditional rebuild, which also
covers the ones `loadProjections` skips on purpose when the part's
`_block_number`/`_block_offset` is invalidated (e.g. a part adopted by
ATTACH or REPLACE PARTITION FROM).

After a lightweight delete the test checks the normal projection only
for presence and a remaining row, because a rebuilt normal projection
still returns the deleted rows (ClickHouse#111791); an aggregate projection checks
that the rebuild happened.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 1, 2026
@groeneai

groeneai commented Oct 1, 2026

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 4 findings over 2 rounds) plus my own review (3 nits).

# Sev Finding Verdict Evidence / action
1 ❌ A replica that serves a part with an unloaded projection does not send the projection directory (DataPartsExchange.cpp) DISAGREE The sender is unchanged and behaves the same on master. The receiver loads the part without a consistency check, so the entry stays "in checksums, not loaded": the next merge rebuilds the projection and it is never read in between. A checksum entry without a directory failing the part check is #100413.
2 ⚠️ The lightweight-delete arm does not prove the projection was rebuilt AGREE, fixed @ 3c6624b On wide parts a stale normal projection answers like a rebuilt one (#111791). Added an aggregate projection to the arm: its count drops from 100 to 50 only when the rebuild happens.
3 ⚠️ No deterministic compact/wide/packed proof (raised in both rounds) DISAGREE I ran the test with each layout forced by table settings (confirmed in system.parts): master fails and the fix passes on compact, wide and packed. The committed test keeps the layout random because CI randomizes it.
4 💡 The update arm's projection query reads only the updated column AGREE, fixed @ 3c6624b The query also filters g = 2.
5 💡 Stale "WIP" line in a commit message AGREE, fixed @ 3c6624b The branch is one commit with a corrected message.
6 💡 The new merge log line says "on disk" also for an entry that is only in checksums noted Debug log only, not changed.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are terminal per finding.

Session id: cron:clickhouse-review-slot-8:20261001-052100

@groeneai
groeneai requested review from CurtizJ and Michicosun October 1, 2026 05:58
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [3c6624b]

Summary: ✅


AI Review

Summary

This PR teaches merge and mutation planning to treat projections that exist only in checksums.txt as real source projections, so parts fetched before a replica applies ALTER TABLE ... ADD PROJECTION get their projections rebuilt instead of silently dropping them or keeping stale pre-mutation data. I did not find a new correctness issue in the current diff, and the added regression test exercises the failure mode described in the PR.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 3c6624b.

Metric Baseline Current Δ
Lines 88.30% 88.30% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.70% 79.70% +0.00%

Changed lines: Changed C/C++ lines covered: 48/55 (87.27%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). labels Oct 1, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 3c6624b5f with master e6097f19d (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

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

Job report

@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 31 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 28640d43-7a02-4f0b-bd9e-95216dd8b603
  • MIRAI run: 5d742217-db36-40fa-9dc3-2072739ddf5e
  • PR check IDs:
    • clickbench_768321_1791664160
    • clickbench_768444_1791664215
    • clickbench_768629_1791664262
    • tpch_adapted_1_official_768736_1791664277
    • tpch_adapted_1_official_768833_1791664285
    • tpch_adapted_1_official_768932_1791664298

@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Oct 16, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 16, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 16, 2026
Merged via the queue into ClickHouse:master with commit 9be141e Oct 16, 2026
149 checks passed
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 16, 2026
alexey-milovidov added a commit to groeneai/ClickHouse that referenced this pull request Oct 17, 2026
…er-projection-partial-mutation

In `MutationsInterpreter::prepare`, this branch moved the rebuild of broken and missing commit-order projections into an earlier block that also reads their columns. Master (ClickHouse#123185) added `hasUnloadedProjection` to the projection loop. The loop now skips a projection the part does not have unless it is on disk but not loaded and is not a commit-order projection. Commit-order projections are already handled by the earlier block.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
yariks5s pushed a commit to Onyx2406/ClickHouse that referenced this pull request Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release can be tested Allows running workflows for external contributors comp-projections Projections: precomputed layouts for accelerating queries (projection parts, maintenance). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding 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.

3 participants