Skip to content

Make a MongoDB table keep its named collection from being dropped - #123636

Merged
alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:mongodb-table-registers-named-collection-dependency
Oct 4, 2026
Merged

alexey-milovidov merged 5 commits into
ClickHouse:masterfrom
groeneai:mongodb-table-registers-named-collection-dependency

Conversation

@groeneai

@groeneai groeneai commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #123240
Related: #123632
Related: #121519

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 DROP NAMED COLLECTION succeeding while a MongoDB table uses the collection, after which the server could not attach the table on restart. The drop is now refused with NAMED_COLLECTION_IS_USED, as for other table engines.

Description

On 26.3.33.24 and 26.8.10.6, DROP NAMED COLLECTION nc succeeds while a table ENGINE = MongoDB(nc, ...) exists. After a restart the table fails to attach with NAMED_COLLECTION_DOESNT_EXIST, which makes its whole database unusable, and with async_load_databases = 0 the server exits.

check_named_collection_dependencies refuses the drop only for tables that registered a dependency, and StorageMongoDB's getConfigurationImpl never passed its table_id to tryGetNamedCollectionWithOverrides, unlike other table engines (lost in #96181's merge with #89616). The line is identical to #112805's hunk.

This is why master Stress jobs fail with Cannot start clickhouse-server (arm_tsan, arm_asan_ubsan s3): 05315_mongodb_connection_options_secrets (#123240) dropped its collections while MongoDB tables still used them. The test now asserts the drop is refused, drops the tables first, and sets ast_fuzzer_any_query = 0 on the statements that name a collection, so a fuzzed clone or DETACH cannot strand one. Test-only is not enough: under the stress profile, fuzzer clones of e06/e07 survive #123632's DROP TABLE and the restart still fails.

Validation, base vs fix: all three MongoDB(nc ...) forms (drop succeeds vs 769, also after a restart); the test fails on base, passes 50/50 randomized; a stress-profile run plus an async_load_databases = 0 restart fails on base, starts on the fix; test_storage_mongodb passes.

Not changed (family-wide gaps): table functions, database engines (#115049), detached tables (#121519), and the same omission in experimental MaterializedPostgreSQL.

🤖 Generated with Claude Code


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

Version info

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

`check_named_collection_dependencies` makes `DROP NAMED COLLECTION` throw
`NAMED_COLLECTION_IS_USED` only for tables that registered a dependency, and a
table engine registers by passing its `StorageID` to
`tryGetNamedCollectionWithOverrides`. `StorageMongoDB`'s `getConfigurationImpl`
had `table_id` in scope but did not pass it: ClickHouse#96181 added the argument in
2311e59, and its branch merge 468aaca with ClickHouse#89616 (which rewrote the
function) kept master's line without it. Every other table engine passes its
id. So the drop succeeded under a live MongoDB table, the table could not be
attached on the next restart (669), its database became unusable, and with
`async_load_databases = 0` the server did not start. The new line is identical
to the hunk in ClickHouse#112805.

`05315_mongodb_connection_options_secrets` (ClickHouse#123240) dropped its two
collections while `e06`, `e07` and `e08` still used them. Stateless CI drops the
test database, but Stress keeps it and restarts the server, so master Stress
jobs failed with `Cannot start clickhouse-server`. The test now expects the
drop to be refused while the tables exist, drops the tables first, and pins
`ast_fuzzer_any_query = 0` on every statement that names a collection: the
stress profile fuzzes DDL, and a fuzzed `DETACH` of such a table hides it from
the catalog, so its dependency is pruned and the collection drop succeeds
(the generic detached-table hole, ClickHouse#121519); `__fuzz_N` clones would otherwise
also hold the collection and make the final drop fail.

Not changed: table functions (`CREATE TABLE ... AS mongodb(nc)`) register no
dependency for any engine except `bigquery`; the experimental
`MaterializedPostgreSQL` engine has the same omission.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding labels Oct 3, 2026
@groeneai

groeneai commented Oct 3, 2026

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

# Sev Finding Verdict Evidence / action
1 ❌ A MongoDB table in a lazy_load_tables = 1 database registers no dependency after a restart (it is attached as a StorageTableProxy), so its collection can be dropped DISAGREE True for every table engine: DatabaseOrdinary::loadTableLazy skips the storage factory, the only place the dependency is registered (NamedCollectionsHelpers.cpp:200,237); with this PR MongoDB matches the other engines in both load modes. The generic fix is the lazy-load part of #112805, whose StorageMongoDB.cpp hunk is identical to this one.

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

Session id: cron:clickhouse-review-slot-10:20261002-233400

@groeneai

groeneai commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. CREATE NAMED COLLECTION nc ...; CREATE TABLE t (x String) ENGINE = MongoDB(nc, options = 'tls=true'); DROP NAMED COLLECTION nc succeeds every time on base (also on 26.3.33.24 and 26.8.10.6); a restart with async_load_databases = 0 then exits with the CI chain (722, 695, 669 There is no named collection).
b Root cause explained? check_named_collection_dependencies refuses the drop only for tables that registered a dependency, and StorageMongoDB getConfigurationImpl did not pass table_id to tryGetNamedCollectionWithOverrides (lost in a merge of #96181 with #89616), so a MongoDB table never held its collection. 05315 dropped its collections while e06/e07/e08 existed; the Stress restart could not attach them.
c Fix matches root cause? Yes. The registration is restored at the source, the same call every other table engine makes (text identical to #112805). The test stops dropping collections in use and asserts the guard.
d Test intent preserved / new tests added? Yes. The secret-masking assertions and the reference are unchanged; the teardown now asserts NAMED_COLLECTION_IS_USED while the tables exist, which fails on base and passes on the fix.
e Both directions demonstrated? Yes. Base: all three MongoDB(nc ...) forms drop, the test fails (The query succeeded but the server error '769' was expected), a stress-profile run plus restart does not start. Fix: 769 for all three (also after a restart), test OK 50/50 randomized, stress-profile run x10 plus restart starts. Mutation (line reverted) fails 3/3.
f Fix is general across code paths? Yes. All other table engines already register. Same omission only in the experimental MaterializedPostgreSQL engine (untestable without a live PostgreSQL; not changed, disclosed). Table functions and database engines are family-wide gaps, out of scope.
g Fix generalizes across inputs (params/datatypes/wrappers)? Yes. Bare MongoDB(nc), key=value overrides and collection=/uri= overrides all take the same call and are covered; ATTACH on restart re-registers; a rejected CREATE throws before registering.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, default or format change; SET check_named_collection_dependencies = 0 still bypasses the guard.
i Invariants and contracts preserved? Yes. Registration is a lock-guarded insert that cannot throw; removal on DROP/RENAME is generic and UUID-keyed; the table-function path (table_id == nullptr) is unchanged.

Session id: cron:clickhouse-impl-slot-6:20261002-222900

@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [edd7ed7]

Summary: ✅


AI Review

Summary

This PR fixes the main MongoDB(<named collection>, ...) table-engine path by passing the table StorageID into tryGetNamedCollectionWithOverrides, so eager CREATE/ATTACH now registers the dependency and the updated stateless test checks that DROP NAMED COLLECTION is refused while those tables exist. I am not ready to approve it yet because the dependency is still rebuilt only when StorageMongoDB is materialized, so a database with SETTINGS lazy_load_tables = 1 can restart with only a StorageTableProxy and still lose the protection before the first access.

Findings

⚠️ Majors

  • [dismissed by author -- https://github.com/Make a MongoDB table keep its named collection from being dropped #123636#issuecomment-5963394707] src/Storages/StorageMongoDB.cpp:183: this change only registers the named-collection dependency when getConfigurationImpl runs inside storage construction. After restart, DatabaseOrdinary::loadTableLazy keeps a proxy instead of materializing the table (src/Databases/DatabaseOrdinary.cpp:568), and InterpreterDropNamedCollectionQuery only consults the in-memory dependency map (src/Interpreters/InterpreterDropNamedCollectionQuery.cpp:41). In an Atomic/Ordinary database with SETTINGS lazy_load_tables = 1, the collection can still be dropped before the first access, and the later materialization fails on the missing collection—the same user-visible unloadability bug the PR claims to fix.
    Suggested fix: re-establish named-collection dependencies from metadata in the lazy-load path before installing the proxy, or add a generic metadata-level hook for storage engines analogous to the table-function re-registration in src/Databases/DatabaseOnDisk.cpp:158.
Tests
  • ⚠️ Add a focused restart test with CREATE DATABASE ... ENGINE = Atomic SETTINGS lazy_load_tables = 1 (or Ordinary), then create a MongoDB(<named collection>) table, restart, and verify DROP NAMED COLLECTION is still refused before the table is first accessed. That is the remaining load-mode divergence.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: close the lazy_load_tables hole, or otherwise prove with code/tests that this load mode cannot drop the collection before the table is materialized.

LLVM Coverage Report

Measured on commit edd7ed7.

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

Changed lines: Changed C/C++ lines covered: 3/3 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). labels Oct 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing edd7ed79c with master 37a251ffa (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

@alexey-milovidov alexey-milovidov left a comment

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.

Perfect.

@alexey-milovidov alexey-milovidov self-assigned this Oct 3, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Oct 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to a conflict with the base branch Oct 3, 2026
# Conflicts:
#	tests/queries/0_stateless/05315_mongodb_connection_options_secrets.sh
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=123636&sha=5fd7924e3dea425dc3a2ac43cadda24407c5bd36&name_0=PR&name_1=Fast%20test (05218_text_index_substreams_opened_lazily: postings_cached_below_threshold is 0 instead of 1) and provide a fix in a separate PR. If the fix is already in progress, link it here. It is unrelated to this PR: it started failing on 2026-10-04 in several PRs and on master (e.g. https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=c9be6a4cabcf05a53776404a40984efb2da2577b&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20parallel%29). I merged master into this branch to rerun CI.

@groeneai

groeneai commented Oct 4, 2026

Copy link
Copy Markdown
Collaborator Author

The fix is already open: #123823

A concurrent test that drops a MergeTree table in a database without table UUIDs (Ordinary, Memory) goes through MergeTreeData::dropAllData -> Context::clearCaches between the warm-up and the measured query, so the measured query reopens every substream (FileOpen 90 against the threshold of 45). The test now repeats the warm-up and the measured query while that query's own mark / text index cache-miss ProfileEvents (plus TextIndexPostingsCacheMisses for postings_cached) show it ran cold; thresholds and reference are unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release can be tested Allows running workflows for external contributors comp-foreign-db Connectivity to external databases (ODBC/JDBC, MySQL, PostgreSQL, etc.). groeneai-origin-ci-master PR origin: master/nightly CI monitoring finding 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.

3 participants