Skip to content

Read fresh in-memory metadata at the OPTIMIZE merge-selection read - #114913

Closed
groeneai wants to merge 1 commit into
ClickHouse:26.3from
groeneai:fix-80648-263-optimize-metadata-cache
Closed

groeneai wants to merge 1 commit into
ClickHouse:26.3from
groeneai:fix-80648-263-optimize-metadata-cache

Conversation

@groeneai

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 data loss when a RENAME COLUMN runs concurrently with OPTIMIZE TABLE ... FINAL on a MergeTree table: the merged part could be written with the pre-rename column list, permanently filling the renamed column with its DEFAULT.

Description

26.3-only fix for issue 80648, authorized in #113929 (comment).

StorageMergeTree::optimize calls merge synchronously on the query thread, so the merge-selection
metadata read runs with a live query context. MergeTreeData overrides getInMemoryMetadataPtr with
a per-query cache that InterpreterOptimizeQuery has already populated with the pre-ALTER snapshot,
so the read returns metadata predating the rename while selection pairs it with parts that already
carry the mutation. MergeTask takes storage_columns from that snapshot and never re-reads, so the
merged part keeps the old column list and the renamed column stays DEFAULT.

Passing bypass_metadata_cache=true makes the read honour the invariant the comment above it already
states. scheduleDataProcessingJob keeps its bare read: its only caller is the background pool, which
has no query context, so it already bypasses.

26.4 and later declare the accessor as (ContextPtr, bool) with no default, forcing every caller to
state a context, so the read is already fresh there. 25.8 has the same bare read but ships
enable_shared_storage_snapshot_in_query defaulting to false. 26.3 is the only supported branch
with both the defaulted parameter and that setting true, which is why the backport of 104822
compiled unchanged as getInMemoryMetadataPtr() and took the cache-consulting path.

Validation on two interleaved, load-matched servers built from this tree, setting at its default:
base reddens 16 of 7000 iterations against 0 for the fix (Fisher exact p = 3.0e-05); swapping the
binaries onto each other's port and data directory keeps the effect with the binary (30/7000 against
0/7000); 80 runner runs of the unmodified 04238_alter_rename_column_merge_race per arm give 3 base
failures with the exact CI signature and 80/80 on the fix. None of the 211 merge and mutation tests
with a verdict in both arms regressed.

Fixes the 26.3-only ClickHouse#80648 data loss that makes 04238_alter_rename_column_merge_race
fail with "expected 10 non-empty rows, got 0": a concurrent RENAME COLUMN plus
OPTIMIZE FINAL permanently fills the renamed column with its DEFAULT in the merged
part.

StorageMergeTree::optimize calls merge() synchronously on the query thread, so the
merge-selection read inside the currently_processing_in_background_mutex runs with a
query context. MergeTreeData overrides getInMemoryMetadataPtr with a per-query
metadata cache, which InterpreterOptimizeQuery has already populated with the
pre-ALTER snapshot, so the read returns metadata that predates the rename while
selection pairs it with parts that already carry the mutation. MergeTask derives
storage_columns from that snapshot and never re-reads metadata, so the stale column
list is what gets written.

Reading with bypass_metadata_cache=true makes that read honour the invariant the
comment above it already states. This restores what the backport of ClickHouse#104822 lost:
26.4+ declare the accessor as (ContextPtr, bool) with no default, which forces every
caller to state a context, whereas on 26.3 the parameter is defaulted, so the
cherry-picked call compiled unchanged as getInMemoryMetadataPtr() and silently
selected the cache-consulting path. Taking that PR's own added src/ lines longer than
25 characters that are not comments, whitespace-normalized and deduplicated, and
testing membership against each branch's copy of the seven files it touches, gives
113 of 113 present on 26.4 through 26.7 and on master, and 111 of 113 on 26.3. The
two absent lines are exactly this metadata read and the assignment that follows it.

Measured on two servers built from this tree, each on its own port and data
directory, loops interleaved so the arms are load-matched, with
enable_shared_storage_snapshot_in_query left at its default. Every arm asserts
SELECT lower(buildId()) against the port it uses and aborts on mismatch. Base
reddens 16 times in 7000 iterations and the fix 0 (Fisher exact two-sided
p = 3.0e-05); after swapping the two binaries onto each other's port and data
directory the effect follows the binary, base 30/7000 against fix 0/7000
(p = 1.8e-09). At the runner level, 80 runs of the unmodified test per arm give
3 failures on base carrying the exact CI signature and 80/80 OK on the fix.

scheduleDataProcessingJob keeps its bare read: its only StorageMergeTree caller is
the background pool, where there is no query context, so that read already bypasses.

No test file was modified; 04238_alter_rename_column_merge_race is the oracle and is
byte-identical across branches. No setting, format or API change.
@clickhouse-gh

clickhouse-gh Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [2dbae14]

Summary: ✅

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Aug 15, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 2dbae14

Every failure below has an owner. Attempt 2 of the BackportPR run is fully green
(31 success, 2 skipped, praktika top-level success, 0 dropped jobs).

Check / test Reason Owner / fixing PR
Stateless tests (amd_asan, distributed plan, parallel, 2/2) / 04408_lazy_materialization_part_offset_prewhere Pre-existing 26.3 backport gap, not caused by this PR. Failed on attempt 1 (01:16Z) and passed on the attempt that concluded this job (01:56Z to 02:21Z, success). A fix task owns it, awaiting an owner decision on the missing prerequisite 96682. The question is posted on the 26.3 backport thread; the fixing-PR link will follow here if that decision produces one.

Evidence that the row is unrelated to this change: this test has 8486 OK / 0 FAIL on
master with no time bound
, and all 21 failures in the last 30 days sit on 26.3 or
backport/26.3/*. This PR touches one line of merge selection in
src/Storages/StorageMergeTree.cpp, a different mechanism from the lazy-materialization
_part_offset prewhere path.

Session id: cron:our-pr-ci-monitor:20260815-043000

@groeneai groeneai added the groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding label Aug 19, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 4, 2026
@PedroTadim

Copy link
Copy Markdown
Member

The fix is already merged

@PedroTadim PedroTadim closed this Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants