Skip to content

Check the SETTINGS names of ON CLUSTER CREATE TABLE and CREATE DATABASE on the initiator - #129609

Closed
groeneai wants to merge 14 commits into
ClickHouse:masterfrom
groeneai:groeneai/on-cluster-create-setting-names-on-initiator
Closed

groeneai wants to merge 14 commits into
ClickHouse:masterfrom
groeneai:groeneai/on-cluster-create-setting-names-on-initiator

Conversation

@groeneai

@groeneai groeneai commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #121202
Related: #121661

Changelog category (leave one):

  • Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

CREATE TABLE ... ON CLUSTER and CREATE DATABASE ... ON CLUSTER check on the initiator, before enqueueing the query, the SETTINGS names that older servers accepted although they are not settings, so such a name is reported once instead of by each host, and an upgraded host no longer refuses such a query enqueued by an older server. IF NOT EXISTS is still checked on each host.

Description

Requested in #121661 (comment). Builds on #121661 (open); its commits show here until it merges.

Problem. For ON CLUSTER, the name check (#120898 for tables, #121661 for databases) runs on each host's DDL worker, which judges the enqueued text as a fresh CREATE. So with a 26.9 host and an upgraded one, a query issued on 26.9, which checks nothing, leaves the table on one host only:

CREATE TABLE t ON CLUSTER c (x UInt8) ENGINE = Log SETTINGS not_a_setting_at_all = 1
-- There was an error on [node_new:9000]: Code: 115 ... Unknown setting 'not_a_setting_at_all': for storage Log

Change. The initiator judges the names before enqueueing: tables (inner engines of materialized views included) in createTable, where the definition is normalized, databases in the early ON CLUSTER branch of execute. A plain DDL leg then skips the check: for a database at every entry format, for a table at entry format 4 or higher (format 3 is stored as 2). IF NOT EXISTS stays judged on each host, which alone knows whether the object exists. Replicated databases are unchanged. The engines that check names carry StorageFeatures::checks_setting_names, read by StorageFactory::get and the initiator, instead of seven creator calls. An initiator built without an engine cannot judge it, nor do the hosts.

Validation. A mixed-version integration test with a 26.9 node: the upgraded host creates a table, a database and a format-2 database that 26.9 enqueued; an upgraded initiator rejects a table, a materialized view and a database before 26.9 sees them; the host still rejects a format-2 table and an IF NOT EXISTS object it lacks, and IF NOT EXISTS over existing ones stays a no-op. 6 of the 11 cases fail without the change, and removing each part reddens its own case.


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

groeneai and others added 14 commits September 22, 2026 11:56
A database SETTINGS clause is one ASTSetQuery with three payloads:
`changes` (name = value), `default_settings` (name = DEFAULT) and
`query_parameters` (param_name = value, prefix stripped). All six database
engines that accept settings apply `changes` only, and
InterpreterCreateQuery::createDatabase serialises the whole AST into the
database .sql after the creator ran, so a name in either of the other two
payloads was accepted and then persisted in system.databases.engine_full
where no code ever reads it. DatabaseFactory::validate checks only whether a
clause is allowed for the engine, never the names in it.

Add checkDatabaseSettingNames and call it once from createDatabase, which is
the only caller of DatabaseFactory::get and the only place where all three
replay discriminators exist at once.

`changes` is deliberately left to the engines: every one of the six already
rejects an unknown name there, and DatabaseDataLakeSettings::loadFromQuery
attaches a tailored "use the setting without `iceberg_` prefix" hint that a
generic UNKNOWN_SETTING would replace. A param_name entry is rejected
outright rather than looked up, mirroring the storage side: nothing hoists a
name out of query_parameters and no database engine reads one, so such an
entry did nothing even where a custom setting of that name existed.

The freshness predicate is the database-side one, not isFreshTableDefinition.
loadMetadata.cpp hard-codes /*create*/ true for a database, so setForceAttach
is never called and the stored full `ATTACH DATABASE db ENGINE = ...` is
replayed at plain ATTACH with attach_short_syntax == false, the same
(mode, attach_short_syntax) pair a user's own full-definition ATTACH DATABASE
carries. isFreshTableDefinition would therefore have rejected every
settings-carrying database at server startup. DatabaseBackup.cpp already
computes the correct database-side form, and it is reused here.

RESTORE was the one replay path with no marker: RestorerFromBackup set only
setInternal(true), and the backup stores a CREATE DATABASE, so the mode is
CREATE. It now also sets setIsRestoreFromBackup(true), which the table path
already used for this purpose and which is behaviour-neutral on the database
path today, every other consumer of that member being table-only.
Two alternatives were rejected: passing secondary = is_restore_from_backup
would move the mode to SECONDARY_CREATE and flip DatabaseReplicated's
is_create_query and its DROPPED_MARK handling for every restored Replicated
database, and setRecoveryFromStoredMetadata is the broad flag that also
disables StorageDistributed local-shard access checks.

No is_ddl_replay term is added, because a Replicated database's DDL log
carries the definitions of the tables inside it and never its own, so no
CREATE DATABASE is ever replayed under a ZooKeeperMetadataTransaction.

Related: ClickHouse#121202
Related: ClickHouse#121394
… engines

The sibling checkStorageSettingNames carries five exemption terms. Four have
a correctly translated or correctly absent database analogue, but
is_shared_catalog_replay had none, and the path is real from this tree alone:
the DatabaseMySQL constructor has an explicit
`SharedDatabaseCatalog::initialized() && !isInitialQuery(context_)` branch
that tolerates a connection failure exactly as it does for attach, reachable
only through DatabaseFactory::get and so only through createDatabase. A
CREATE DATABASE therefore is re-executed on a secondary as a non-initial
shared-catalog query, and MySQL is one of the six engines that accept
settings. On that path is_metadata_replay, attach_short_syntax and
is_restore_from_backup are all false with mode CREATE, so nothing exempted
it and a definition stored before this change could be refused on a
secondary, which retries its queue entry forever.

No test: SharedDatabaseCatalog.h is not in this tree, so the branch is
unreachable in an OSS build and a test would assert nothing.

The stateless test previously proved the rejection on Atomic and Ordinary,
which share one has_builtin_setting_fn, leaving four of the six engines
measured only by hand. Because the check runs before the creator, a
rejection needs no external service, so the remaining engines are asserted
directly: Replicated (both spellings, since the param_ loop is
engine-independent), MySQL, DataLakeCatalog, and MaterializedPostgreSQL
behind its experimental gate, which fires earlier than the check.

The Replicated path stays a literal. A {...} inside a Replicated zk path is
expanded as a macro rather than substituted as a query parameter, so the
per-run form fails with NO_ELEMENTS_IN_CONFIG once the path is reached. It is
never reached here, the settings check rejecting first, which is exactly why
the arm would have passed either way.
The fast test build configures -DENABLE_LIBRARIES=0, which defaults
ENABLE_MYSQL, ENABLE_LIBPQXX and ENABLE_AVRO off, so configure_config.cmake
leaves USE_MYSQL, USE_LIBPQXX and USE_AVRO unset and registerDatabases.cpp
never registers MySQL, MaterializedPostgreSQL or DataLakeCatalog. With the
engine absent from the registry the new check takes its own early exit for an
engine it cannot resolve, and DatabaseFactory::get then throws
UNKNOWN_DATABASE_ENGINE where those three arms assert UNKNOWN_SETTING. The
file carried no tag line, so --fast-tests-only selected it.

The merged sibling 05227_storage_settings_unknown_name.sql already carries
the same block for the same reason on the S3 engine.

The companion .sh test needs no tag: its only engines are Atomic, MergeTree
and File backups, and registerBackupEnginesFileAndDisk registers File and
Disk unconditionally.
On a short `ATTACH DATABASE db` the interpreter replaces the query with the
definition parsed out of the stored metadata file, so the stored clause is
the one that reaches the check, and then sets attach_short_syntax. On that
path the metadata-replay flag is false because it is a user query rather than
the startup load, the restore flag is false, the Shared Catalog term is false
in an OSS build, and the mode is exactly ATTACH. So attach_short_syntax is
the only term that exempts it, and nothing in the tree exercised a short
detach and attach of a database whose stored clause names a non-setting.

The other three terms already have live arms: a full-definition ATTACH is
rejected, a stored definition still loads at startup, and a RESTORE still
succeeds. The new arm re-asserts the stored name first, so a definition
rewritten by the load above it cannot make it pass while asserting nothing.
Drop the restore-flag narration; state the ATTACH-mode, DEFAULT-parse and declaration contracts as bare invariants.
…nown-database-setting-name

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nown-database-setting-name

Resolved `src/Databases/DatabaseFactory.cpp` by keeping both additions: master's
`DatabaseFactory::Arguments::isLoadingFromExistingMetadata` and this branch's `checkDatabaseSettingNames`.

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

For a database, the initiator of `CREATE DATABASE ... ON CLUSTER` only enqueues the
query, so the clause is judged on the host that runs the queue entry. No arm covered
that leg, so exempting the DDL worker (which would reopen ClickHouse#121202 for every ON CLUSTER
create) left the suite green. Both spellings are asserted; `FORMAT Null` keeps the
per-host status row out of the output under the default `throw` mode.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…SE on the initiator

A plain `ON CLUSTER` leg runs with no Keeper metadata transaction, so it judged the
`SETTINGS` names of the enqueued `CREATE` as a fresh definition. A host then refused an
entry that an older initiator, which judged nothing, had enqueued: the object ended up
on some hosts only.

The initiator now judges the names before it enqueues anything, and the hosts skip the
check: a database at every entry format (its engine is explicit or `Atomic`), a table
only for entries stored in format 4 or later, whose `CREATE` the initiator normalized
(format 3 is stored as 2); with the legacy formats the hosts keep judging tables.

The engines that judge names carry `StorageFeatures::checks_setting_names`, so
`StorageFactory::get` and the initiator read one flag instead of seven creator calls.

Requested in ClickHouse#121661 (comment)

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

Only a host knows whether the object of `CREATE ... IF NOT EXISTS` exists, and where it does
the statement is a no-op before any name is judged. The initiator therefore skips such a
statement and the hosts judge it as before, with the decision taken from the entry's own
query, so the inner table of a materialized view inherits it.

New arms: a materialized view whose inner engine has an unknown name is refused before
enqueueing, `IF NOT EXISTS` over an existing table or database is still a no-op, and over a
missing one is still refused by the host.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Shorten the flag and DDL entry format comments to one line and drop the repeated IF NOT EXISTS note in checkSettingNamesOnInitiator.

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

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Internal review: 2 rounds, 3 major + 2 nits fixed, 1 finding disagreed
  • ❌ IF NOT EXISTS ... ON CLUSTER over an existing object was newly rejected on the initiator (master and the local path no-op it): AGREE, fixed; IF NOT EXISTS stays judged on each host.
  • ⚠️ The initiator's walk over materialized-view inner engines had no test: AGREE, test added.
  • ⚠️ A comment claimed the hosts judge an engine the initiator lacks; they do not: AGREE, rewritten.
  • 💡 Flag comment wording; a redundant features lookup: AGREE, fixed.
  • ⚠️ Gate B: database name = value names are not checked on the initiator. DISAGREE: each host's database engine rejects an unknown name = value itself (DatabaseMetadataDiskSettings::loadFromQuery, 26.9 included), so no host diverges and no older entry needs grandfathering. This PR moves only the names older servers accepted (MergeTree names also stay with their engine); the changelog entry now says exactly that.

@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: test_backward_compatibility/test_on_cluster_create_setting_names.py (a 26.9 node and an upgraded node, one cluster), 11 cases, about 45 s.
b Root cause explained? A plain ON CLUSTER leg has no Keeper metadata transaction, so it judges the enqueued CREATE as fresh, and cannot tell an entry from a server that judged the names from one that did not.
c Fix matches root cause? Yes: the initiator, the only place that sees the definition as stated, judges it; the legs stop re-judging. The leg exemption never ships without the initiator check. IF NOT EXISTS stays with the hosts, which alone know whether the object exists.
d Test intent preserved / new tests added? New integration module. 05227/05237 unchanged in behaviour (one stale comment line in 05237 replaced).
e Both directions demonstrated? Base: 6 of 11 cases fail; fix: 11 of 11 pass (three runs). Removing the initiator calls, the table flag, the database exemption, the format gate, the materialized-view walk, the initiator's or the hosts' IF NOT EXISTS exemptions, or keying the database exemption on the flag each reddens its own cases.
f Fix is general across code paths? Tables and databases; every executeQueryOnCluster return of InterpreterCreateQuery; inner engines of materialized views and TimeSeries; CREATE OR REPLACE; AS inheritance. Replicated databases unchanged.
g Fix generalizes across inputs (params/datatypes/wrappers)? All three payloads (name = value, name = DEFAULT, param_name); entry formats 1 to 3 vs 4 and above; databases with and without ENGINE; tables without ENGINE (default_table_engine); IF NOT EXISTS over an existing and a missing table or database.
h Backward compatible? (maintainer-approved exception only) Yes: no setting and no entry format change. A host no longer refuses what its initiator accepted; IF NOT EXISTS over an existing object stays a no-op; legacy table formats keep host-side judging.
i Invariants and contracts preserved? The order of checks in StorageFactory::get is unchanged; the flag is copied with the context, so inner tables and CREATE ... AS SELECT inherit it, and it is decided from the entry's own query.

Session id: cron:clickhouse-impl-slot-4:20261105-175700

@clickhouse-gh

clickhouse-gh Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c4a0a31]

Summary: ❌

job_name test_name status info comment
Config Workflow FAIL
can_be_tested FAIL
Dockers Build (amd) DROPPED
Dockers Build (arm) DROPPED
Dockers Build (multiplatform manifest) DROPPED
Style check DROPPED
Code Review DROPPED
Docs check (Mintlify) DROPPED
Fast test DROPPED
Fast test (arm_darwin) DROPPED
Build (arm_tidy, 1/4) DROPPED

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Nov 5, 2026
@groeneai

groeneai commented Nov 5, 2026

Copy link
Copy Markdown
Collaborator Author

cc @tuanpach, could you review this and add the can be tested label so CI runs? The initiator of CREATE TABLE/DATABASE ... ON CLUSTER now checks the SETTINGS names before enqueueing and the DDL worker leg skips that check, so an entry enqueued by an older server is no longer refused on an upgraded host; IF NOT EXISTS is still checked on each host.

@PedroTadim

Copy link
Copy Markdown
Member

Too large PR, ignore

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

Labels

pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants