Skip to content

Keep the population of a Replicated database DDL entry on the replica that executes it - #124477

Merged
fm4v merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-replicated-database-populate-parallel-replicas
Oct 10, 2026
Merged

fm4v merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-replicated-database-populate-parallel-replicas

Conversation

@groeneai

@groeneai groeneai commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #124446
Related: #123790

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 CREATE MATERIALIZED VIEW ... POPULATE and CREATE TABLE ... AS SELECT in a Replicated database with parallel replicas enabled: each replica sent its population to other replicas, so its rows landed in other replicas' tables (duplicated there, missing on the replica itself), or the query failed with UNKNOWN_TABLE. The population now ignores the query's parallel replicas and parallel_distributed_insert_select settings, so each replica fills only its own table. Closes #124446.

Description

On 26.8.18.2, CREATE MATERIALIZED VIEW ... POPULATE in a 3-replica Replicated database with enable_parallel_replicas = 1 and a cluster_for_parallel_replicas that reaches one replica (the shape of the CI failure in #124446) leaves every source row three times in that replica's view and no rows in the other two. With the database's own cluster, the CREATE can fail with UNKNOWN_TABLE.

Every replica of a Replicated database executes the CREATE entry and fills its own copy of the table with the non-atomic population (fillTableIfNeeded). That INSERT ... SELECT ran with the entry's settings, so it was sent as INSERT INTO <table> SELECT ... to the replicas of cluster_for_parallel_replicas (parallel_distributed_insert_select = 2 is the default) and read through parallel replicas.

The fix: for a CREATE executed as a Replicated database entry, the population runs with parallel_distributed_insert_select = 0 and enable_parallel_replicas = 0, and these settings (with the alias) are removed from the copied SELECT's SETTINGS, as the atomic POPULATE path already does.

4 new cases in test_parallel_replicas_insert_select fail on master and on 26.8.18.2, and pass with the fix.

04494_atomic_populate_materialized_view_parallel_replicas is unchanged: its no-replicated-database tag comes from #123790, not merged yet. #120775 (open) keeps an INSERT ... SELECT on the initiator when the target does not replicate; it does not cover the read side.


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

Version info

  • Merged into: 26.10.1.2002-master (included in 26.10 and later)
  • Backported to: 26.9.15.8

… that executes it

Every replica of a `Replicated` database executes a `CREATE` entry and fills its
own copy of the table, and such a `CREATE` always takes the non-atomic
population (`shouldPopulateMaterializedViewAtomically` refuses the atomic path
under a ZooKeeper metadata transaction). `fillTableIfNeeded` built that
`INSERT ... SELECT` with the entry's own context, i.e. with the user's
parallel-replicas settings, so `InterpreterInsertQuery` sent
`INSERT INTO <table> SELECT ...` to every replica of
`cluster_for_parallel_replicas` (`parallel_distributed_insert_select` defaults
to 2) and the `SELECT` read through parallel replicas. A replica's rows landed
in other replicas' tables (duplicated there, missing locally), or the remote
INSERT failed with `UNKNOWN_TABLE` where the table did not exist yet; for
`CREATE TABLE ... AS SELECT` the other replicas' DDL workers failed on the
already renamed `_tmp_replace_*` table until they were recovered as lost
replicas with an empty table.

Under a ZooKeeper metadata transaction the population now uses a context copy
with `parallel_distributed_insert_select = 0` and `enable_parallel_replicas = 0`,
and both settings (plus the `allow_experimental_parallel_reading_from_replicas`
alias) are removed from the copied `SELECT`, because its own `SETTINGS` are
reapplied on top of the context; `removeSettingsFromQuery` also drops the
`name = DEFAULT` form, which would restore the default 2. This is what the
atomic `POPULATE` path already does. The single site covers `POPULATE`,
`CREATE OR REPLACE ... POPULATE` and `CREATE TABLE ... AS SELECT`; the view's
stored definition is unchanged, as only the clone is scrubbed.

Reported by @ fm4v from 04494_atomic_populate_materialized_view_parallel_replicas
in the DBReplicated lane, where every replica of the CI cluster points at the
primary server.

Test: 4 cases in `test_parallel_replicas_insert_select` (secondaries populating
through the only live replica, the initiator populating before the other
replicas created the view, CTAS into `Memory`, and the read side with different
data per replica, with parallel replicas also enabled by the view's own
`SETTINGS`), all failing on master and on 26.8.18.2.

Closes ClickHouse#124446

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 7, 2026
@groeneai

groeneai commented Oct 7, 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; round 1: 2 findings, round 2: 0 findings) plus my own review in both rounds.

# Sev Finding Verdict Evidence / action
1 ⚠️ The tests did not prove that the population SELECT's own SETTINGS are stripped: every case passed the settings as client flags AGREE, fixed @ 8ad579d The read-side case now also enables parallel replicas from the view's own SETTINGS; a build without the strip fails it
2 ❌ A stored VIEW whose body has SETTINGS enable_parallel_replicas = 1 is still read through parallel replicas by the population DISAGREE No duplicates: the distributed INSERT is decided only from the insert's own context (InterpreterInsertQuery.cpp:852, 1509), which is forced local. Other rows than local ones need parallel_replicas_for_non_replicated_merge_tree = 1 stored in that view, and then every SELECT of the view returns them. The changelog states only what is guaranteed
3 💡 The 3-name list repeats 3 entries of the atomic POPULATE path's list Noted, not changed The atomic path forces its settings before the snapshot capture, and its 4th entry must not be stripped here
4 💡 The parallel_distributed_insert_select = 0 override is not pinned by a test on its own Noted, not changed Shipping the INSERT also needs parallel replicas, which are forced off; no input observes the override alone. Kept to match the atomic path

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

Session id: cron:clickhouse-review-slot-8:20261007-190500

@groeneai

groeneai commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. 4 new cases in test_parallel_replicas_insert_select; the source is synced on every replica before the population, so master fails 4/4 every run: node1 3000 1000 1498500 and node2/node3 0 0 0; UNKNOWN_TABLE from the initiator's CREATE; CTAS leaves node2/node3 empty; node2/node3 hold node1's rows.
b Root cause explained? A CREATE executed as a Replicated database entry uses the non-atomic population on every replica, and fillTableIfNeeded ran its INSERT ... SELECT with the entry's parallel-replicas settings, so it was sent to and read from the replicas of cluster_for_parallel_replicas instead of the executing replica.
c Fix matches root cause? Yes. The population of a Replicated entry is made local at the one site that builds it (parallel_distributed_insert_select = 0, enable_parallel_replicas = 0, both names removed from the copied SELECT's SETTINGS). No default change, no guard at a symptom site.
d Test intent preserved / new tests added? New integration cases: write side (2), CREATE TABLE ... AS SELECT, read side (with parallel replicas also enabled by the view's own SETTINGS). The 26 existing cases of the module are unchanged and pass.
e Both directions demonstrated? Yes, on Build-ID-checked binaries: 4/4 fail on master and on 26.8.18.2, 4/4 pass with the fix (repeated 20 to 50 times). The read-side case also fails with the SETTINGS scrub removed, and with only the alias removed from it.
f Fix is general across code paths? One site reached by POPULATE, CREATE OR REPLACE ... POPULATE and CREATE TABLE ... AS SELECT, on the initiator and on every other replica. The atomic POPULATE path was already local; CLONE AS is rejected in Replicated databases.
g Fix generalizes across inputs (params/datatypes/wrappers)? Settings as user settings and as query-local SETTINGS of the SELECT, under the name and the alias, with = value and = DEFAULT (the alias in the view's SETTINGS is in the test; the rest checked with a separate probe); MV with a MergeTree inner table and a Memory CTAS; replicated and per-replica sources. Type wrappers N/A.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default, format or protocol change; each replica decides for its own population. The view's stored definition keeps its SETTINGS (only a copy is changed).
i Invariants and contracts preserved? A population run by a Replicated entry writes this replica's table only, and the CREATE's own parallel-replicas settings no longer make it read from other replicas. The context copy keeps the metadata transaction, access and process-list state; live pushes of the view are unchanged.

Session id: cron:clickhouse-impl-slot-5:20261007-183004

@clickhouse-gh

clickhouse-gh Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8ad579d]

Summary: ✅


AI Review

Summary

This PR keeps Replicated-database population local by forcing the internal INSERT ... SELECT in fillTableIfNeeded to ignore parallel-replica and distributed-INSERT settings, and by scrubbing those settings from the cloned population SELECT so a view-local SETTINGS clause cannot re-enable them. The new integration coverage exercises the original misrouting, the initiator-before-followers race, the CREATE TABLE ... AS SELECT path, and the stored-SETTINGS read-side case. I did not find any unresolved correctness, compatibility, or rollout problems in the current diff.

Final Verdict
  • Status: ✅ Approve

LLVM Coverage Report

Measured on commit 8ad579d.

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

Changed lines: Changed C/C++ lines covered: 17/17 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 7, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. label Oct 7, 2026
@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

This PR only changes InterpreterCreateQuery::fillTableIfNeeded, the code that fills a table after CREATE ... AS SELECT or POPULATE. When that runs in a Replicated database, it now turns off parallel replicas and parallel_distributed_insert_select. None of the read-only SELECTs in clickbench or tpch_adapted_1_official run this code. The only flagged query, clickbench Q31 (-10.4%, 297 ms to 266 ms), was correctly kept as no change: it falls inside master's current range of run times and CPU time barely moved. Nothing in this run suggests a performance effect from the PR.

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 218b7248-f14b-4f93-a880-27ebda99cb33
  • MIRAI run: 69d04d82-81b1-45fc-bcbb-87e5502ec016
  • PR check IDs:
    • clickbench_632086_1791591900
    • clickbench_632184_1791591904
    • clickbench_632336_1791591963
    • tpch_adapted_1_official_632559_1791592226
    • tpch_adapted_1_official_632718_1791592254
    • tpch_adapted_1_official_632839_1791592272

@fm4v
fm4v added this pull request to the merge queue Oct 10, 2026
Merged via the queue into ClickHouse:master with commit 2e77cbc Oct 10, 2026
152 of 153 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 10, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Oct 10, 2026
clickhouse-gh Bot added a commit that referenced this pull request Oct 10, 2026
Backport #124477 to 26.9: Keep the population of a Replicated database DDL entry on the replica that executes it
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Oct 20, 2026
fm4v pushed a commit that referenced this pull request Nov 2, 2026
Resolve the cherry-pick of #124477 onto 26.8.

The only conflict is context: on master the new settings list sits above
fillTableIfNeeded, whose signature #117967 changed (published_table_name);
26.8 keeps its own signature (skip_target_insert_access_check), so the
resolution adds only the lines #124477 adds and does not bring #117967 along.
The test files merged cleanly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 removed the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 2, 2026
robot-clickhouse-ci-2 added a commit that referenced this pull request Nov 3, 2026
Cherry pick #124477 to 26.8: Keep the population of a Replicated database DDL entry on the replica that executes it
robot-clickhouse added a commit that referenced this pull request Nov 3, 2026
…e DDL entry on the replica that executes it
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Nov 3, 2026
clickhouse-gh Bot added a commit that referenced this pull request Nov 3, 2026
Backport #124477 to 26.8: Keep the population of a Replicated database DDL entry on the replica that executes it
JTCunning pushed a commit to JTCunning/ClickHouse that referenced this pull request Nov 3, 2026
The bugs behind their `no-parallel-replicas` / `no-replicated-database` tags are fixed on master:
- `01660_join_or_any`: ClickHouse#123683
- `04653_bloom_filter_fixed_string_constant_scalar`: ClickHouse#118988
- `04819_dynamic_aggregate_function_state_version`: ClickHouse#123649
- `04825_quantile_deterministic_alter_add_modify_column`: ClickHouse#115393, ClickHouse#123070
- `04494_atomic_populate_materialized_view_parallel_replicas`: ClickHouse#124477
- `04266_text_index_tokens_cardinality_order`: ClickHouse#124500 already disables parallel replicas for the log-checked query

Related: ClickHouse#124015
leshikus pushed a commit that referenced this pull request Nov 13, 2026
…e DDL entry on the replica that executes it
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-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. groeneai-origin-request PR origin: a maintainer pinged or directed groeneai pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v26.8-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Atomic MATERIALIZED VIEW POPULATE duplicates rows in DBReplicated: 04494_atomic_populate_materialized_view_parallel_replicas

6 participants