Skip to content

Fix a table disappearing after a failed SYSTEM RESTART REPLICA - #131883

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
tiandiwonder:fix-restart-replica-lost-table
Nov 21, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
tiandiwonder:fix-restart-replica-lost-table

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Related: #129515

Problem

When SYSTEM RESTART REPLICA fails after the table has been detached but before the re-created table is attached, the table disappears from the database until the server is restarted (its metadata file stays on disk). For DatabaseReplicated the in-memory digest is also not adjusted on this path.

One way to hit it: a table whose constraint has a scalar subquery reading another table, while that table is missing or is being restarted at the same time:

CREATE TABLE t_probe (k UInt64) ENGINE = MergeTree ORDER BY k;
CREATE TABLE t_check (x UInt64, CONSTRAINT c CHECK x < (SELECT count() + 1000 FROM t_probe))
    ENGINE = ReplicatedMergeTree('/clickhouse/tables/{database}/t_check', 'r1') ORDER BY tuple();
DROP TABLE t_probe SYNC SETTINGS check_table_dependencies = 0;
SYSTEM RESTART REPLICA t_check;   -- UNKNOWN_TABLE
SELECT count() FROM t_check;      -- UNKNOWN_TABLE: t_check is gone

The stress test exposes this (with TABLE_IS_BEING_RESTARTED) since it started to run random SYSTEM RESTART REPLICA(S) in #129515.

Root cause

InterpreterSystemQuery::doRestartReplica detaches the table, waits for it to become unused, and only then analyzes the stored definition with InterpreterCreateQuery::getColumnsDescription and InterpreterCreateQuery::getConstraintsDescription. The latter runs TreeRewriter::analyze, which evaluates the constraint's scalar subquery. Its exception, like one from waitDetachedTableNotInUse (the query being killed), is thrown before the try block that retries StorageFactory::instance().get and adjusts the DatabaseReplicated digest, so nothing re-attaches the table.

Solution

  • Read and analyze the stored definition before the table is shut down, so a definition that cannot be analyzed aborts the restart while the table is still attached and working. It is not done under lockExclusively, because the constraint's subquery may need to lock a table that is being restarted. Reading the definition before the exclusive lock is safe: the table's DDL guard, held for the whole restart, is also taken by every ALTER (including replicated metadata alters applied in the background), so the definition cannot change before the detach.
  • Move waitDetachedTableNotInUse into the try block (before the retry loop), so its exception, e.g. the query being killed during the wait, also goes through the DatabaseReplicated digest adjustment.

Test: 01646_restart_replica_definition_error_keeps_table, which fails on master (the table vanishes) and passes with the fix.

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 table disappearing until server restart after SYSTEM RESTART REPLICA failed, for example because the table's constraint references a table that is missing or being restarted.


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

Version info

  • Merged into: 26.12.1.141-master (included in 26.12 and later)

…e its definition

`doRestartReplica` detached the table, waited for it to become unused, and only then
ran `getColumnsDescription` / `getConstraintsDescription`. The latter evaluates a
constraint's scalar subquery, so a constraint reading a missing table (or one being
restarted at the same time, `TABLE_IS_BEING_RESTARTED`) threw before the `try` block
that retries re-creation and adjusts the `DatabaseReplicated` digest. The table then
disappeared from the database until server restart.

Analyze the stored definition before the table is shut down, outside the exclusive
lock (the subquery may need to lock a table that is being restarted); the table's DDL
guard, held for the whole restart, keeps the definition from changing until the
detach. Move `waitDetachedTableNotInUse` into the `try` block so a kill during the
wait also goes through the digest adjustment.

Related: ClickHouse#129515

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

clickhouse-gh Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bbbee29]

Summary: ✅


AI Review

Summary

The PR analyzes the stored table definition before shutdown, preserving the attached table when analysis fails, and includes detached-table wait failures in the existing DatabaseReplicated digest cleanup. The regression test covers a missing constraint dependency and successful restart after restoring it. No actionable findings at bbbee29fd739279666249ac0cb874e07dab22565.

Missing context / blind spots
  • ⚠️ No local ClickHouse binary or build was available, so tests were not executed locally. The fetched CI reports exposed aggregate statuses without individual test results; a test-level result for 01646_restart_replica_definition_error_keeps_table would close this execution-verification gap.
Final Verdict

✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Nov 15, 2026
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Nov 15, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. label Nov 16, 2026
@tiandiwonder tiandiwonder changed the title Keep the table attached when SYSTEM RESTART REPLICA fails to analyze its definition Fix a table disappearing after a failed SYSTEM RESTART REPLICA Nov 16, 2026
tiandiwonder and others added 2 commits November 16, 2026 01:02
`01646_system_restart_replicas_smoke` already pins the same invariant: a
`SYSTEM RESTART REPLICA` whose re-parse of the stored definition fails must not
leave the table lost. Keep both under one prefix so they sort together.

Co-Authored-By: Claude Opus 5 <[email protected]>
`check_style.py` rejects any test whose path contains `fail`, so
`..._failed_analysis_...` fails the style check. Name it after the condition it
covers instead: an error while analyzing the stored definition.

Co-Authored-By: Claude Opus 5 <[email protected]>
@tiandiwonder
tiandiwonder marked this pull request as ready for review November 16, 2026 01:45
@tiandiwonder tiandiwonder added the CI for issues found in CI label Nov 16, 2026
clickhouse-gh[bot]
clickhouse-gh Bot previously approved these changes Nov 16, 2026

@clickhouse-gh clickhouse-gh Bot left a comment

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.

AI review approves this commit, see the AI Review comment.

@tiandiwonder tiandiwonder added pr-ci and removed CI for issues found in CI labels Nov 16, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=131883&sha=528af85a999afd1fadeca7909590126e087afd9a&name_0=PR&name_1=Build%20profile%20diff (LogClusterNotReady: the endpoint never became ready, unrelated to this PR) and provide a fix in a separate PR. If the fix is already in progress, link it here — possibly #129675 or #126200.

@clickhouse-gh clickhouse-gh Bot removed the pr-ci label Nov 17, 2026
@clickhouse-gh
clickhouse-gh Bot dismissed their stale review November 17, 2026 02:35

AI review of a later commit does not approve the PR, see the AI Review comment.

@groeneai

Copy link
Copy Markdown
Collaborator

The fix is #129675 (mine, open). In this run (job log) all 8 readiness probes connected to the CI logs cluster and then got Read timed out. (read timeout=3), after which has_pr_data raised LogClusterNotReady. With #129675 a probe that connects and then times out is reported as a CI logs cluster outage (green, nothing compared) instead of failing the check.

@clickhouse-gh

clickhouse-gh Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bbbee29fd with master dbb0516e4 (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

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Thanks @groeneai — the Build profile diff failure is tracked by #129675. The re-run on bbbee29fd is green (no significant changes), and every check on this PR now passes. There are no open review threads, so this PR needs nothing more from me.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 21, 2026
Merged via the queue into ClickHouse:master with commit 369aeea Nov 21, 2026
195 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 21, 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-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.

4 participants