Skip to content

Do not list the tables of external databases on server shutdown - #133406

Open
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-shutdown-skip-external-databases
Open

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:fix-shutdown-skip-external-databases

Conversation

@groeneai

@groeneai groeneai commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Improvement

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

Server shutdown no longer lists the tables of external databases (DataLakeCatalog, MySQL, PostgreSQL, Remote, ...), which delayed it by about 30 seconds for each such database whose remote side did not answer.

Description

Closes: #126060
Related: #118985 (added the Buffer drain)
Related: #131994 (exposes it in Stress)
Related: #128923 (has the same condition inside its shutdown rewrite)

Failure on master (Possible deadlock on shutdown, then signal 9, on 6 Stress flavours since #131994): https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=2a2f57466616f97b3f6cbbf4dbe29b85b5811dfe&name_0=MasterCI&name_1=Stress%20test%20%28arm_debug%29

Before any database shuts down, DatabaseCatalog::shutdownImpl lists the tables of every user database to flush Buffer tables. For an external database that listing is a request to its remote side: a DataLakeCatalog database loaded from metadata builds its REST catalog there and retries /v1/config for about 33 s when nobody answers, Remote queries the remote server, MySQL and PostgreSQL connect. An external database cannot hold a Buffer table, so nothing is ever found. Since #131994 the Stress fuzzer clones CREATE DATABASE; the clones of a test's DataLakeCatalog databases outlive its mock catalog, and the final shutdown walks into the dead endpoint once per clone until the harness kills the server. #131994 only exposes this, so this PR fixes the listing instead of reverting it.

The loop now skips isExternal() databases, as the other enumerators of internal table engines (system.replicas, system.mutations, ...) already do. One edge: a Buffer table attached into a PostgreSQL database with use_table_cache (a definition the MySQL engine refuses) is no longer flushed early, as before #118985.

Validation (debug builds): the new test fails on master (13 connections to its listener, 34 s: 10 from DataLakeCatalog, 3 from Remote) and passes with the fix (0, 1 s), 50/50 randomized runs. A server with two unreachable DataLakeCatalog databases loaded from metadata shuts down in 67 s on master and 3 s with the fix. test_buffer_flush_on_shutdown passes 13/13.


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

`DatabaseCatalog::shutdownImpl` collects the tables of every non-predefined
database with `getTablesIterator` to flush `Buffer` tables before any
database shuts down (ClickHouse#118985). For an external database that call is a
request to its remote side: a `DataLakeCatalog` database loaded from
metadata builds its catalog lazily on that call (the REST catalog retries
`/v1/config` 10 times, ~33 s when nobody answers), `Remote` and `Cluster`
query the remote server, `MySQL` and `PostgreSQL` connect. An external
database cannot hold a `Buffer` table (`IDatabase::isExternal`), so the
listing never finds one, and each unreachable database delays the shutdown
sequentially.

Since ClickHouse#131994 the Stress server-side fuzzer clones `CREATE DATABASE` under
fuzzed names, so clones of a test's DataLakeCatalog databases outlive its
mock catalog, and the final shutdown of the Stress jobs walks into the dead
endpoint once per clone until the harness kills the server ("Possible
deadlock on shutdown", reported as an allowed OOM).

Skip external databases in that loop, as every other enumerator of internal
table engines already does.

Closes ClickHouse#126060

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@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) plus my own review (1 nit).

# Sev Finding Verdict Evidence / action
1 ❌ A Buffer attached into a PostgreSQL database with use_table_cache (DatabasePostgreSQL.cpp:313-336) is no longer drained before shutdown, and DatabasePostgreSQL::shutdown does not flush it DISAGREE Mechanism confirmed. On master the drain reached such a table only while PostgreSQL answered (getTablesIterator lists the remote schema first, returns nothing on error, :163-188). The input breaks the isExternal() contract (IDatabase.h:236-241); MySQL refuses the same ATTACH (DatabaseMySQL.cpp:785-793). Behaviour matches 26.8 and older; #128923 uses the same skip. Stated in the description.
2 💡 The new comment's "they hold no Buffer table" ignores the case in row 1 DISAGREE It restates the isExternal() contract. No change.

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

Session id: cron:clickhouse-review-slot-9:20261121-144509

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new test run directly with a master binary prints connections: 13 in 34 s on every run; with the fix connections: 0 in 1 s.
b Root cause explained? Yes. The Buffer drain at the top of DatabaseCatalog::shutdownImpl (#118985) calls getTablesIterator on every user database. For an external database that is a remote request (a lazily created DataLakeCatalog REST catalog retries /v1/config for about 33 s, Remote queries the remote server), although such a database cannot hold a Buffer table.
c Fix matches root cause? Yes. The loop skips isExternal() databases, so no request is made at all; no timeout or retry change.
d Test intent preserved / new tests added? New test 05839_shutdown_does_not_list_external_databases (clickhouse-local attaches a DataLakeCatalog and a Remote database pointing at a connection-counting listener and exits). No existing test changed.
e Both directions demonstrated? Yes. Fails on the master snapshot and on the fix tree with the condition removed (13 connections), passes with the fix (0); 50/50 randomized runs pass.
f Fix is general across code paths? Yes. One condition at the only table listing in shutdownImpl covers every external engine and both the server and clickhouse-local (LocalServer::cleanup); the test covers two independent engines (10 and 3 connections each without the fix).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A for data types. Measured with 0, 1 and 2 external databases, attached live and loaded from metadata at startup (server: 67 s on master, 3 s with the fix).
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or metadata change; internal databases are drained exactly as before.
i Invariants and contracts preserved? Yes. Every database that can hold a Buffer table (isExternal() == false) is still drained before any database shuts down (test_buffer_flush_on_shutdown 13/13), and the shutdown order is unchanged. Only edge: a Buffer attached into a PostgreSQL database with use_table_cache is no longer drained early, as before #118985.

Session id: cron:clickhouse-impl-slot-4:20261121-141200

@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [20cb661]

Summary: ✅


AI Review

Summary

The PR skips external databases when collecting tables for the shutdown Buffer drain, avoiding unnecessary remote requests. The new stateless test checks that attaching unreachable DataLakeCatalog and Remote databases produces no connections during shutdown. No actionable findings after reviewing the current code, diff, and complete discussion, including the documented unsupported PostgreSQL attachment limitation. Tests were not rerun locally.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements can be tested Allows running workflows for external contributors comp-database-engines Database engine implementations (e.g., Atomic/Replicated) and database-level behavior. labels Nov 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 20cb66196 with master 3232ee3c9 (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, 13 s compile time in total, 7 of them have a recent master baseline.

Job report

This branch has not been deployed

No deployments
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. pr-improvement Pull request with some product improvements

Projects

None yet

1 participant