Skip to content

Let a table, that carries table_readonly, successfully load, and be taken out of it - #119224

Merged
alexey-milovidov merged 10 commits into
masterfrom
replicated-table-readonly-gates-118509
Sep 25, 2026
Merged

alexey-milovidov merged 10 commits into
masterfrom
replicated-table-readonly-gates-118509

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

table_readonly is not supported for ReplicatedMergeTree, and the checks that enforce that trapped a table whose metadata carries it anyway - which the documented convert_to_replicated flag produced, because a converted table keeps the settings of the table it was converted from. Such a table works, but:

  • a BACKUP of it succeeded while the RESTORE refused with NOT_IMPLEMENTED: restore creates the table at SECONDARY_CREATE, which the constructor's mode <= ATTACH gate rejected, contrary to what the comment on that gate believed;
  • DETACH TABLE succeeded and ATTACH TABLE then refused, so after a plain detach only a restart brought the table back, and after DETACH TABLE ... PERMANENTLY nothing did;
  • ALTER TABLE ... MODIFY SETTING table_readonly = 0 was refused as well - the alter gate rejected the setting by name, whatever value was assigned - although the setting's own documentation promises it "can always be toggled back".

Now the state is unrepresentable through every entrypoint that mints a table, while a definition that already carries the setting keeps loading:

  • the veto sits where the rest of MergeTree tells a fresh definition from a replayed one (isFreshTableDefinition in registerStorageMergeTree, plus the DDL-replay, stored-definition and Shared Catalog markers), so a CREATE and a full-definition ATTACH TABLE t UUID '...' (...) SETTINGS table_readonly = 1 are both refused, while a short ATTACH TABLE t, a SECONDARY_CREATE (RESTORE, Replicated-database DDL) and the startup levels load the table;
  • the alter gate rejects only turning the setting on, so MODIFY SETTING table_readonly = 0 - the way out of that state - works; a RESET SETTING (or MODIFY SETTING table_readonly = DEFAULT) is judged by the server default it falls back to, so it works unless the replicated_merge_tree / merge_tree config sections turn the setting on;
  • both conversion entrypoints refuse a table that would carry the setting as a ReplicatedMergeTree - from its own definition or, when the definition does not set it, from the merge_tree / replicated_merge_tree config defaults, resolved like registerStorageMergeTree does - through one shared precondition: the startup convert_to_replicated flag leaves the table alone and says so in the log (throwing there would take the table down with the whole database load, and the setting could then not be changed at all), and ATTACH TABLE ... AS REPLICATED throws before any of its side effects. Both point at MODIFY SETTING table_readonly = 0. The flag is kept, so the conversion runs on the next start once the setting is off.

Since no entrypoint can introduce the setting anymore, the load path is covered where a stored definition can be planted: test_modify_engine_on_restart/test_table_readonly.py writes table_readonly = 1 into the stored definition of a ReplicatedMergeTree with the server stopped and checks that the table loads on startup, loads again through a short ATTACH TABLE t, serves its rows, and is taken out of that state by RESET SETTING; the same test drives the convert_to_replicated flag end to end, and a second node whose replicated_merge_tree config section turns the setting on checks that both conversion entrypoints refuse a MergeTree without the setting, that an explicit table_readonly = 0 lets the conversion through, and that a reset on the converted table is refused. The planting goes through the database disk, so the test also runs in the db disk configuration.

Closes: #118509

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

Fixed a ReplicatedMergeTree table whose metadata carries table_readonly = 1 being unusable: its backup could not be restored, it could not be re-attached after DETACH, and ALTER TABLE ... MODIFY SETTING table_readonly = 0 was refused. Converting a table with that setting to replicated is no longer done, by either the convert_to_replicated flag or ATTACH TABLE ... AS REPLICATED.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.705-master (included in 26.10 and later)

`table_readonly` is not supported for `ReplicatedMergeTree`, and the
checks that say so trapped a table whose metadata carries it anyway -
which the `convert_to_replicated` flag produced, since a converted table
keeps the settings of the table it came from. A backup of that working
table could not be restored (restore creates at `SECONDARY_CREATE`,
which the `mode <= ATTACH` gate rejected), a detached table could not be
re-attached, and `MODIFY SETTING table_readonly = 0` was refused too,
although the setting's documentation promises it can always be toggled
back.

The constructor now rejects a `CREATE` only, so everything that loads an
existing table keeps working; the alter gate rejects turning the setting
on, not off; and the conversion leaves a readonly table alone and says so
in the log rather than producing one, keeping the flag so that it runs
once the setting is gone.

`04062_table_readonly_replicated` asserted that the attach is refused,
which is the trap this fixes, so that assertion now checks that it is not.

Closes: #118509
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d233810]

Summary: ✅


AI Review

Summary

This PR moves the table_readonly veto to the fresh-definition paths so legacy ReplicatedMergeTree metadata can still load and be cleared, while new CREATE/full ATTACH definitions and both conversion entrypoints still refuse minting that unsupported state. The code changes themselves look coherent, but one of the headline bug fixes (BACKUP/RESTORE) still lacks a direct regression: the new tests cover startup and short ATTACH, not the distinct SECONDARY_CREATE path that RESTORE uses.

Tests
  • ⚠️ [tests/integration/test_modify_engine_on_restart/test_table_readonly.py:96] The only regression for loading legacy metadata never executes RESTORE / SECONDARY_CREATE, even though that is the path this PR relies on to fix “backup restores refused with NOT_IMPLEMENTED”. A future change that keeps startup and short ATTACH working but re-breaks restore would still pass this suite. Please add a focused BACKUP/RESTORE case, or another direct SECONDARY_CREATE exercise, for a table whose stored definition carries table_readonly = 1.
Final Verdict
  • Status: ⚠️ Request changes
  • Add direct regression coverage for the RESTORE / SECONDARY_CREATE path of a legacy ReplicatedMergeTree definition carrying table_readonly = 1.

LLVM Coverage Report

⚠️ No coverage measurement for commit d233810: incomplete coverage measurement: 1 of 21 shard profiles are missing: LLVM_COVERAGE_FILE_it_8.profdata.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 10, 2026
# The UUID-form is required because `Atomic` rejects `ATTACH TABLE name (cols) ENGINE = ...` syntax
# before reaching the storage check.
$CLICKHOUSE_CLIENT --query="
ATTACH TABLE t_readonly_repl UUID '$uuid' (x UInt64) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{database}/t_readonly_repl', 'r1') ORDER BY x SETTINGS table_readonly = 1;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not actually test the behavior the PR description promises.

At this point the other new test has already done MODIFY SETTING table_readonly = 0 and RESET SETTING table_readonly, so its short ATTACH TABLE t_readonly_replicated is reattaching a normal replicated table, not one whose stored metadata still carries table_readonly = 1. And this shell branch only asserts "the error is not NOT_IMPLEMENTED", so it would still pass on any other failure.

So we still do not have a focused regression for the real contract change: loading stored metadata (short ATTACH TABLE t / SECONDARY_CREATE during RESTORE) when the table definition still contains table_readonly = 1, and then clearing that setting successfully.

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.

🕵 Right on both counts. 04062 is back to what it asserted on master - that full-definition ATTACH is fresh user input and is refused - since the first blocker is fixed.

The contract this PR actually changes can no longer be reached through SQL at all now that every fresh entrypoint refuses the setting, so it is covered where a stored definition can be planted: tests/integration/test_modify_engine_on_restart/test_table_readonly.py writes table_readonly = 1 into the stored definition of a ReplicatedMergeTree with the server stopped, then checks that the table loads on startup, loads again through a short ATTACH TABLE t, serves its rows, and that ALTER TABLE ... RESET SETTING table_readonly takes it out of that state. The same file drives the convert_to_replicated flag end to end: skipped while the setting is on, flag kept, conversion on the next start once it is reset. Both tests pass locally.

@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 d233810d0 with master 711f56031 (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

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

Job report

The refusals the test asserts are `ReplicatedMergeTree`'s own; an engine
substituted for it accepts `table_readonly = 1`, so the expected
`NOT_IMPLEMENTED` never came and the test failed. Tag it `no-shared-merge-tree`.
Comment thread src/Databases/DatabaseOrdinary.cpp Resolved
alexey-milovidov and others added 4 commits September 19, 2026 02:55
…e `ATTACH ... AS REPLICATED`

Two gaps the bot review found in the previous gate:

* A full-definition `ATTACH TABLE t UUID '...' (...) ENGINE = ReplicatedMergeTree ... SETTINGS
  table_readonly = 1` is fresh user input even though it runs under
  `LoadingStrictnessLevel::ATTACH`, so keying the veto on `mode == CREATE` let a user mint a brand
  new table in the unsupported state. The veto now lives in `registerStorageMergeTree`, next to the
  fresh-vs-replayed distinction the rest of `MergeTree` already uses
  (`isFreshTableDefinition` plus the DDL-replay, stored-definition and Shared Catalog markers), so
  only a replayed definition - short `ATTACH TABLE t`, `SECONDARY_CREATE`, the startup levels -
  loads with the setting.
* `ATTACH TABLE ... AS REPLICATED` rewrites the engine of a stored `MergeTree` definition in place
  and so reached the same unsupported state through the manual conversion entrypoint. The
  precondition is now a shared helper, `DatabaseOrdinary::isTableReadonlyInDefinition`, which both
  conversion entrypoints call: the startup `convert_to_replicated` flag logs and leaves the table
  alone, while the query refuses outright before any of its side effects.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
… the closed gate asserts

`04062` goes back to what it asserted on master: a full-definition
`ATTACH TABLE t UUID '...' (...) SETTINGS table_readonly = 1` is fresh user input and is refused
again, so the assertion that it succeeds - which the too-coarse `mode == CREATE` gate had made true -
is wrong and is removed.

The contract that is actually being changed - a definition that already carries the setting keeps
loading, and can be taken out of that state - can no longer be reached through SQL at all now that
every fresh entrypoint refuses it, so it is covered where a stored definition can be planted:
`test_modify_engine_on_restart/test_table_readonly.py` writes `table_readonly = 1` into the stored
definition of a `ReplicatedMergeTree` with the server stopped, and checks that the table loads on
startup, loads again through a short `ATTACH TABLE t`, serves its rows, and that
`ALTER TABLE ... RESET SETTING table_readonly` takes it out. The same test drives the
`convert_to_replicated` flag end to end: the conversion is skipped while the setting is on, the flag
is kept, and the conversion happens on the next start once the setting is reset.

`05191` gains the manual conversion entrypoint: `ATTACH TABLE ... AS REPLICATED` of a table carrying
the setting is refused, the refusal leaves the table untouched, and the conversion goes through once
the setting is reset.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…se name of its own

`system.tables.metadata_path` is reported relative to the server's data directory, so the planted
definition has to be addressed from there, and the database was named after the setting the test
checks for, which made the `RESET SETTING` assertion match the table's own name. Both tests pass now.

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

Copy link
Copy Markdown
Member Author

🕵 @groeneai, investigate the failure: Upgrade check (amd_release) - Logical error: Virtual row has different type for A. Expected B, got C (STID 1637-309b) - and provide a fix in a separate PR. If the fix is already in progress, link it here. This PR only changes where the table_readonly gates for ReplicatedMergeTree sit, so it cannot reach the read-in-order virtual row; the nearest work I found on that error is the draft #111457. The branch has since been merged with master, so the next run will tell whether it still reproduces.

Comment thread src/Databases/DatabaseOrdinary.cpp Resolved
@groeneai

Copy link
Copy Markdown
Collaborator

Investigated: this PR is not implicated and no new fix is needed. The abort came from the previous release's server, not from this PR's build.

The Upgrade check runs its pre-upgrade load against the previous release, and the two binaries write separate logs:

Stage Log Server Virtual row has different type
pre-upgrade clickhouse-server.stress.log 26.8.2.7 (c2930599c567, PID 918) 2
post-upgrade clickhouse-server.upgrade.log 26.9.1.1 (b97c2b6eedc1) 0

The logical error is stamped 00:24:39 for query 98dc2862-1973-46e1-b76e-175049efb75d, inside the pre-upgrade window (00:15:59 to 00:24:52 in the same server clock), the uploaded core dump is core.QueryPipelineEx.918-3611 where PID 918 is the 26.8.2.7 server per its own startup banner, and the check's own No Error messages after server upgrade item passed. So your reading is right: the branch cannot reach the read-in-order virtual row.

The signature is owned by #119385 (merged 2026-09-12 10:39Z, 5b332566d6a8), not by the draft #111457. It makes read-in-order key matching compare result types, so a CAST(json.b, 'String') sorting key is no longer matched to a Nullable(String) ORDER BY element under cast_keep_nullable = 1. Counting sorting_key_type in src/Storages/ReadInOrderOptimizer.cpp: 5 occurrences at that merge and at master, 0 at its first parent and 0 at 26.8.2.7, so the shipped binary that aborted has no fix available to it from master.

The CI side is already closed by your own #119768 (6b1566c47a48, merged 2026-09-13 13:08Z), which stops the upgrade check from randomizing cast_keep_nullable in the pre-upgrade load. It merged the day after this run was dispatched (2026-09-12 19:42Z), which is why the run still saw it. CIDB agrees: 57 rows on 09-11, 23 on 09-12, 11 on 09-13, 1 on 09-14, none since. Head e538fc00 contains both commits, so the fresh run should come back clean on this signature.

One loose end: the engine fix is master-only, and the abort is reachable on 26.8 with cast_keep_nullable = 1. Apply v26.8-must-backport to #119385 if you want it there.

alexey-milovidov and others added 4 commits September 25, 2026 07:18
…adonly-gates-118509

Conflict in `StorageReplicatedMergeTree::alter`: master (db31f04) started to reject
`RESET SETTING table_readonly` (and its `MODIFY SETTING table_readonly = DEFAULT` spelling) on
`ReplicatedMergeTree`, while this PR rejects only turning the setting on, because turning it off is
the way out for a table whose metadata carries it. Resolved in the PR's direction: a reset is judged
by the value it resets to, the server default from `getDefaultSettings`, so it is rejected exactly
when that default is on (a `replicated_merge_tree` config section turning it on). `05183` is
updated accordingly: with the default off the resets are accepted and apply what they carry along,
and turning the setting on in the same `ALTER` is still refused.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…rver defaults

Both conversion entrypoints (the `convert_to_replicated` flag and `ATTACH TABLE ... AS REPLICATED`)
only looked at the explicit `SETTINGS` of the stored definition, while `registerStorageMergeTree`
resolves the settings of a `ReplicatedMergeTree` over the `merge_tree` / `replicated_merge_tree`
config defaults. With `table_readonly = 1` coming from the config, a `MergeTree` without the setting
passed the precondition and became a readonly replicated table, which the later non-fresh load then
accepted.

`DatabaseOrdinary::isTableReadonlyInDefinition` becomes `isTableReadonlyAsReplicated`: the
definition's own value if it has one, otherwise `getReplicatedMergeTreeSettings`. The advice in the
messages is now `MODIFY SETTING table_readonly = 0`, because a reset would fall back to that same
default. The integration test gets a second node whose `replicated_merge_tree` section turns the
setting on, covering both refusals, the explicit `0` that lets the conversion through, and the
refused reset on the converted table.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…shadow `WithContext::context`

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ion test

In the `db disk` configuration the table metadata lives on the database disk rather than under
`/var/lib/clickhouse`, so `sed` on the local file failed with `No such file or directory`. Use
`replace_text_in_metadata` like the other metadata-editing tests, clear the disk metadata cache and
restart with `kill=True`. Passed locally both plain and with `CLICKHOUSE_USE_DATABASE_DISK=1`.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=119224&sha=e538fc009a76d4921c7c7affeb0b2e8250ba1282&name_0=PR
PR: #119224

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@alexey-milovidov alexey-milovidov changed the title Let a table that carries table_readonly load, and be taken out of it Let a table, that carries table_readonly, successfully load, and be taken out of it Sep 25, 2026

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

This is not something that we wanted to fix. But ok.

@alexey-milovidov alexey-milovidov self-assigned this Sep 25, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 25, 2026
Merged via the queue into master with commit 6d96c4d Sep 25, 2026
177 checks passed
@alexey-milovidov
alexey-milovidov deleted the replicated-table-readonly-gates-118509 branch September 25, 2026 23:07
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 25, 2026
kolsha added a commit that referenced this pull request Nov 18, 2026
Keep the release branch side and apply only the fix of #128995: check the
`TABLE ENGINE` grant on the converted engine in `convertMergeTreeTableIfPossible`
before the side effects of `ATTACH ... AS [NOT] REPLICATED`.

The master-only code next to the fix is not carried over: `isTableReadonlyAsReplicated`
(#119224) and the `temporary_uuid` / `ordinary_znode_info` handling of `Ordinary`
databases, none of which exist on 26.9, where the conversion supports `Atomic` only.
Because of that the test is tagged `no-ordinary-database`, as `03169_attach_as_replicated`.
kolsha added a commit that referenced this pull request Nov 18, 2026
Keep the release branch side and apply only the fix of #128995: check the
`TABLE ENGINE` grant on the converted engine in `convertMergeTreeTableIfPossible`
before the side effects of `ATTACH ... AS [NOT] REPLICATED`.

The master-only code next to the fix is not carried over: `isTableReadonlyAsReplicated`
(#119224) and the `temporary_uuid` / `ordinary_znode_info` handling of `Ordinary`
databases, none of which exist on 26.8, where the conversion supports `Atomic` only.
Because of that the test is tagged `no-ordinary-database`, as `03169_attach_as_replicated`.
kolsha added a commit that referenced this pull request Nov 18, 2026
Keep the release branch side and apply only the fix of #128995: check the
`TABLE ENGINE` grant on the converted engine in `convertMergeTreeTableIfPossible`
before the side effects of `ATTACH ... AS [NOT] REPLICATED`.

The master-only code next to the fix is not carried over: `isTableReadonlyAsReplicated`
(#119224) and the `temporary_uuid` / `ordinary_znode_info` handling of `Ordinary`
databases, none of which exist on 26.3, where the conversion supports `Atomic` only.
Because of that the test is tagged `no-ordinary-database`, as `03169_attach_as_replicated`.
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.

Converting a table with table_readonly = 1 to ReplicatedMergeTree yields a table whose backup refuses to RESTORE and whose DETACH cannot be re-ATTACHed

3 participants