Skip to content

Add check to ensure no named collection is dropped if still in use - #96181

Merged
pamarcos merged 12 commits into
ClickHouse:masterfrom
pamarcos:prevent-named-collection-drop-if-any-table-uses-it
Feb 17, 2026
Merged

pamarcos merged 12 commits into
ClickHouse:masterfrom
pamarcos:prevent-named-collection-drop-if-any-table-uses-it

Conversation

@pamarcos

@pamarcos pamarcos commented Feb 6, 2026

Copy link
Copy Markdown
Member

New setting check_named_collection_dependencies set to true by default. When enabled, dropping a named collection that is used by any table will fail with NAMED_COLLECTION_IS_USED error.

This is similar to how check_referential_table_dependencies works for table dependencies.

The implementation uses UUID-based tracking for Atomic databases (where UUIDs persist across renames) and database+table name tracking for Ordinary databases (which don't have UUIDs).

Closes #77366

Changelog category (leave one):

  • New Feature

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

Add new check enabled by default via setting check_named_collection_dependencies to avoid dropping named collections used by tables.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

New setting `check_named_collection_dependencies` set to true by
default. When enabled, dropping a named collection that is used by
any table will fail with NAMED_COLLECTION_IS_USED error.

This is similar to how `check_referential_table_dependencies` works
for table dependencies.

The implementation uses UUID-based tracking for Atomic databases
(where UUIDs persist across renames) and database+table name tracking
for Ordinary databases (which don't have UUIDs).
@pamarcos
pamarcos requested a review from Copilot February 6, 2026 09:53
@clickhouse-gh

clickhouse-gh Bot commented Feb 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1a07806]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Feb 6, 2026

Copilot AI 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.

Pull request overview

This PR adds a new setting check_named_collection_dependencies (enabled by default) to prevent dropping named collections that are currently in use by tables. The implementation tracks dependencies between named collections and tables using UUID-based tracking for Atomic databases and database+table name tracking for Ordinary databases.

Changes:

  • Adds dependency tracking for named collections with UUID-based tracking for Atomic databases and name-based tracking for Ordinary databases
  • Introduces a new error code NAMED_COLLECTION_IS_USED and setting check_named_collection_dependencies
  • Updates all storage engine registration functions to register table dependencies with named collections

Reviewed changes

Copilot reviewed 34 out of 35 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
tests/queries/0_stateless/03822_named_collection_drop_dependency_check.sh Comprehensive test cases covering Atomic and Ordinary databases, rename operations, and exchange tables
src/Common/ErrorCodes.cpp Adds new error code NAMED_COLLECTION_IS_USED
src/Core/Settings.cpp Adds check_named_collection_dependencies setting
src/Core/SettingsChangesHistory.cpp Records new setting in version history
src/Common/NamedCollections/NamedCollectionsFactory.h Defines dependency tracking data structures using Boost.MultiIndex
src/Common/NamedCollections/NamedCollectionsFactory.cpp Implements dependency management methods (add, remove, rename, getDependents)
src/Storages/NamedCollectionsHelpers.h Updates function signature to accept optional table_id for dependency registration
src/Storages/NamedCollectionsHelpers.cpp Registers dependencies when tables use named collections
src/Interpreters/InterpreterDropNamedCollectionQuery.cpp Checks for dependent tables before allowing named collection drop
src/Interpreters/InterpreterRenameQuery.cpp Updates dependency tracking during table rename and exchange operations
src/Interpreters/DatabaseCatalog.h Adds is_rename parameter to removeDependencies
src/Interpreters/DatabaseCatalog.cpp Removes named collection dependencies when tables are dropped (except during rename)
src/Storages/*.cpp Updates all storage engines to pass table_id when using named collections

@pamarcos
pamarcos marked this pull request as draft February 10, 2026 22:10
1. Verify dependent tables exist via `DatabaseCatalog` before blocking
   `DROP NAMED COLLECTION`, cleaning up stale entries from failed
   `CREATE TABLE` automatically.

2. Add `no-replicated-database` tag to stateless test that uses
   Ordinary database.
@pamarcos
pamarcos marked this pull request as ready for review February 11, 2026 15:51
@vitlibar vitlibar self-assigned this Feb 16, 2026
Comment thread src/Interpreters/DatabaseCatalog.cpp Resolved
@pamarcos
pamarcos added this pull request to the merge queue Feb 17, 2026
Merged via the queue into ClickHouse:master with commit b0f5f0a Feb 17, 2026
143 checks passed
@pamarcos
pamarcos deleted the prevent-named-collection-drop-if-any-table-uses-it branch February 17, 2026 15:27
@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 17, 2026
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 4, 2026
`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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-feature Pull request with new product feature 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.

Cannot startup when named collection dropped

4 participants