Skip to content

Fix zero initiator version in the table listing of Remote and Cluster databases - #122661

Merged
PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:remote-database-listing-client-version
Sep 29, 2026
Merged

PedroTadim merged 1 commit into
ClickHouse:masterfrom
groeneai:remote-database-listing-client-version

Conversation

@groeneai

@groeneai groeneai commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #122612

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

Fix a Remote or Cluster database without a local shard making SELECT count() through the merge table function return 0, and making the server fail to start when a dictionary reads one of its tables. The server now sends its own version when it lists the tables of such a database by itself, which also removes the LOGICAL_ERROR logged at shutdown. Closes #122612.

Description

On 26.9.4.3, with a Remote database whose only shard is remote, SELECT count() FROM merge('<db>', '^t$') returns 0 instead of the row count (26.8.10.6 too), and a server with a dictionary reading a table of that database fails to start (Caught exception while loading metadata: Code: 49 ... LOGICAL_ERROR). This is the report from #122612, not a CI failure.

DatabaseRemote::fetchTablesList (shared by Remote, RemoteSecure and Cluster) sends its system.tables query with a copy of the caller's context. When the server lists such a database by itself, that is the global context, whose client version is 0.0.0, and the fence from #109408 in RemoteQueryExecutor refuses to send the query. Release builds mostly swallow the error and see an empty list (hence the 0), isTableExist propagates it (hence the failed start), and debug and sanitizer builds abort.

The fix fills a zero version with the server's own, the guard getStructureOfRemoteTable already applies to the DESC TABLE of the same tables (#113675). It is one site, the only RemoteQueryExecutor in DatabaseRemote, so it also covers DatabaseRemote::empty (the issue's open risk; nothing calls it for this engine today). User queries always carry a version and are unchanged.

Routes measured on a master debug build, before and after: shutdown (abort, then clean), SYSTEM RELOAD CONFIG adding a disk to a policy (server abort, then alive), name hints for an Alias whose target was dropped (server abort, then UNKNOWN_TABLE), merge() (abort, then the right count), a dictionary dependency at startup (abort, then starts), and merge() plus shutdown through a Cluster database. These routes now finish the listing they had already connected for, e.g. shutdown sends one listing and one DESC TABLE per table.

The new test fails on master (debug: exit code 134; 26.9.4.3: count 0) and passes with the fix, 50/50 runs.


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

Version info

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

… databases

`DatabaseRemote::fetchTablesList` sends `SELECT name FROM system.tables ...` to a
remote replica with a copy of its caller's context. When the server lists a
remote-only database by itself (the shutdown drain, `reloadDisksTask`, the
`Merge` metadata checks and the `Alias` target lookup under their own global
context, the metadata loader's dependency check), that context is the global
one, whose client version is 0.0.0, and the zero-version fence in
`RemoteQueryExecutor::sendQueryUnlocked` throws `LOGICAL_ERROR`. Debug and
sanitizer builds abort. Release builds either swallow it in `getTablesIterator`
(an empty listing, so a trivial `count()` through `merge` returns 0) or
propagate it from `isTableExist` (a server whose dictionary reads such a table
fails to start).

Fill a zero client version with this server's own version on the listing
query's context, the same guard `getStructureOfRemoteTable` applies to the
`DESC TABLE` of the same tables. User queries always carry a version, so they
are forwarded unchanged.

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 Sep 28, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding; 1 full pass).

# Sev Finding Verdict Evidence / action
1 ⚠️ The test checks the count and the exit code, not that the listing query carries this server's exact version (05291_remote_database_without_local_shard.sh) DISAGREE The fill sends the compile-time VERSION_MAJOR/VERSION_MINOR/VERSION_PATCH/DBMS_TCP_PROTOCOL_VERSION (DatabaseRemote.cpp:259), the same constants as getStructureOfRemoteTable.cpp:108, and only on 0.0.0, so no path yields another non-zero version. The regression that happened (the fill missing) is caught in both builds: debug exits 134, 26.9.4.3 counts 0, and deleting the 3 fill lines exits 134 again. The same guard on the DESC TABLE query (#113675) is tested by outcome only.

Own review (no blocker or major): the shutdown drain and reloadDisksTask now complete one listing plus one DESC TABLE per table of a remote-only database, which the description states.

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

Session id: cron:clickhouse-review-slot-48:20260928-102100

@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [13be46f]

Summary: ✅


AI Review

Summary

This PR backfills the initiator version for DatabaseRemote::fetchTablesList when a server-internal metadata path copies the zero-version global context, bringing the listing path in line with the existing getStructureOfRemoteTable guard. I did not find a remaining correctness, compatibility, or coverage gap in the current patch.

Final Verdict

✅ No new findings.

LLVM Coverage Report

Measured on commit 13be46f.

Metric Baseline Current Δ
Lines 88.20% 88.20% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.60% 79.60% +0.00%

Changed lines: Changed C/C++ lines covered: 5/5 (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 Sep 28, 2026
@PedroTadim PedroTadim self-assigned this Sep 28, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. label Sep 28, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 13be46fd6 with master b58874494 (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, 12 s compile time in total, 7 of them have a recent master baseline.

Job report


/// The server lists the tables on its own under the global context (e.g. at shutdown), which has
/// no client version, and `RemoteQueryExecutor` refuses to send a query without one.
const auto & client_info = query_context->getClientInfo();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🕵️
@groeneai This is the third site to need this exact guard: distributed INSERT replay (ef79cb1, 4f78c0c), DESC TABLE in getStructureOfRemoteTable.cpp:105-108 (#113675), and now fetchTablesList. Each was found the same way, by the #109408 fence firing in production.

Filling the version at the fence itself would defeat it, since its job is catching contexts that lost their version. But three verbatim copies want one helper, e.g. setInitiatorVersionIfUnset(context), plus a comment at the fence pointing to it, so the next internal caller built from the global context has something to reach for. Not a blocker here — fine as a follow-up.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Agreed. It is more than three copies: master already has the same fill in Context::makeQueryContext, Session::makeQueryContextImpl, StorageMaterializedView::createRefreshContext, the scalar subquery context in ExecuteScalarSubqueriesVisitor, getStructureOfRemoteTable, and DistributedAsyncInsertHeader::read (on a bare ClientInfo), so this is the seventh. I will keep this PR to the one site, since the bug also reaches 26.8 and 26.9 and a one-site fix keeps a backport small, and send a follow-up PR that replaces all seven with one helper and adds a comment at the fence pointing to it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Follow-up: #122766. It moves the six copies on master to ClientInfo::setInitiatorVersionIfUnset (with a one-line Context forwarder) and adds the pointer comment at the fence. The copy added here moves to the helper once this PR merges: in #122766 if that one is still open, otherwise in a one-line follow-up.

@PedroTadim

Copy link
Copy Markdown
Member

After this gets merged, I am going to update #122766

@PedroTadim
PedroTadim added this pull request to the merge queue Sep 29, 2026
Merged via the queue into ClickHouse:master with commit 5f5478b Sep 29, 2026
149 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 29, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 29, 2026
…rough the helper

ClickHouse#122661 added a seventh copy of the fill, in `DatabaseRemote::fetchTablesList`.
It now calls `setInitiatorVersionIfUnset` like the other six, and the two
includes it needed only for `VERSION_*` and `DBMS_TCP_PROTOCOL_VERSION` are
removed. No behavior change.

Requested in ClickHouse#122766 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
KangXin4J pushed a commit to KangXin4J/ClickHouse that referenced this pull request Sep 30, 2026
Six places open-coded the same fill of a zero client version with this
server's version: `Context::makeQueryContext`, `Session::makeQueryContextImpl`,
`StorageMaterializedView::createRefreshContext`, the scalar subquery context in
`ExecuteScalarSubqueriesVisitor`, `getStructureOfRemoteTableInShard` and
`DistributedAsyncInsertHeader::read` (on a bare `ClientInfo`). Each copy was
added after the zero-version check in `RemoteQueryExecutor::sendQueryUnlocked`
(ClickHouse#109408) fired for another server-built context, and each repeated the same
rationale in its comment.

Move the logic into `ClientInfo::setInitiatorVersionIfUnset` (next to its
sibling `setClientVersionFromConnectionIfUnknown`), add a one-line
`Context::setInitiatorVersionIfUnset` forwarder for the context sites, and
keep only the site-specific sentence at each call site. The protocol version
is still set unconditionally together with the three version fields, exactly
as every copy did, so there is no behavior change.

The check in `RemoteQueryExecutor` still throws. Its comment now points to
the helper and says not to fill the version there, because the check exists
to catch a context that lost it. The stale "whether to enable the analyzer"
example is dropped: the old analyzer is gone.

Not converted on purpose: `DDLTask` sets the version unconditionally (this
host re-initiates the DDL), and `setClientVersionFromConnectionIfUnknown`
fills from the connection hello, a different source. The same fill in
`DatabaseRemote::fetchTablesList` comes with ClickHouse#122661, which is not merged
yet, and moves to the helper once it is.

Includes that the original fills added only for `VERSION_*` and
`DBMS_TCP_PROTOCOL_VERSION` are removed.

Requested in ClickHouse#122661 (comment)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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-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.

Cluster/Remote database with no local shard hits LOGICAL_ERROR on shutdown

3 participants