Skip to content

Restore a dictionary left unusable by a rejected DETACH ... PERMANENTLY under concurrent DDL - #111742

Open
tiandiwonder wants to merge 3 commits into
ClickHouse:masterfrom
tiandiwonder:fix-detach-dict-shutdown-dep-race
Open

tiandiwonder wants to merge 3 commits into
ClickHouse:masterfrom
tiandiwonder:fix-detach-dict-shutdown-dep-race

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Related: #105259

Problem

#105259 fixed the user-visible case where DETACH DICTIONARY ... PERMANENTLY was rejected because a dependent object already existed: it moved the dependency check before flushAndShutdown, so a rejected detach no longer shuts the dictionary down.

A narrow residual remains under concurrent DDL: dependency registration is not serialized on the referenced object (InterpreterCreateQuery::createTable calls addTableDependencies after releasing its DDLGuard; ALTER locks only the dependent). So a dependent registered after the pre-shutdown check passes still makes the later DatabaseCatalog::removeDependencies throw HAVE_DEPENDENT_OBJECTS — by which point flushAndShutdown has already deregistered the dictionary from ExternalDictionariesLoader, leaving it attached but unloadable (dictGet* → Dictionary ... not found, BAD_ARGUMENTS) until re-attach or restart.

This PR is defense-in-depth for that concurrent window; it does not change the already-correct behavior for the pre-existing-dependent case.

Root cause

In the DETACH ... PERMANENTLY branch of InterpreterDropQuery::executeToTableImpl, removeDependencies runs after flushAndShutdown and re-checks dependents, throwing when any exist. The pre-shutdown check cannot close the window because a concurrent CREATE/ALTER can register a dependent between the two checks, so the throw can still happen after the dictionary has been deregistered from the loader.

Fix

Make the permanent detach atomic: if removeDependencies throws after flushAndShutdown, restore the dictionary before rethrowing. StorageDictionary::restoreDictionaryConfigurationInRepository() re-registers the loader config repository exactly as the constructor does (a bare startup() is insufficient — it only reloadConfigs, is a no-op under dictionaries_lazy_load, and never re-adds the repository). InterpreterDropQuery.cpp wraps the removeDependencies call and restores the dictionary on throw; the rejection itself is unchanged (HAVE_DEPENDENT_OBJECTS is still raised). Scoped to DETACH PERMANENTLY (the DROP path genuinely removes the table).

Because the window is only reachable under concurrency, the regression test 04628_detach_permanently_dict_dependency_race.sh uses a failpoint (detach_permanently_pause_before_remove_dependencies) to pause the detach between shutdown and dependency removal, registers a dependent in that window, then resumes and asserts the dictionary is still usable. The pre-existing-dependent case is already covered by 04256_105207_detach_dictionary_permanently_dep_check (from #105259).

Changelog category (leave one):

  • Improvement

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

Made a rejected DETACH DICTIONARY ... PERMANENTLY leave the dictionary usable even when the rejecting dependency is registered concurrently with the detach. Previously such a race could leave the dictionary attached but unloadable (Dictionary ... not found) until it was re-attached or the server restarted.

DETACH DICTIONARY ... PERMANENTLY runs flushAndShutdown() before removeDependencies().
For a dictionary, shutdown deregisters it from ExternalDictionariesLoader, and
removeDependencies then throws HAVE_DEPENDENT_OBJECTS when another object still depends on
it -- after the dictionary has already been deregistered. The rejected detach left the
dictionary attached but unloadable (Dictionary ... not found / BAD_ARGUMENTS) until
re-attach or server restart.

Make the permanent detach atomic: if removeDependencies throws, re-register the dictionary's
loader config repository (new StorageDictionary::restoreDictionaryConfigurationInRepository,
mirroring the constructor -- startup() alone does not re-add it and is a no-op under
dictionaries_lazy_load) before rethrowing.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@tiandiwonder tiandiwonder added the can be tested Allows running workflows for external contributors label Jul 24, 2026
@clickhouse-gh clickhouse-gh Bot closed this Jul 24, 2026
@clickhouse-gh clickhouse-gh Bot reopened this Jul 24, 2026
PR ClickHouse#105259 (pre-shutdown dependency precheck) merged to master and touched the same DETACH
PERMANENTLY branch. Resolve InterpreterDropQuery.cpp keeping both: ClickHouse#105259's precheck (rejects
pre-existing dependents before shutdown) and this PR's rollback (restores a dictionary when
removeDependencies throws for a dependent registered concurrently, after shutdown). The two are
complementary.

Add failpoint detach_permanently_pause_before_remove_dependencies and concurrent regression test
04628 that exercises the rollback via the post-precheck race; 04627 now covers the
pre-existing-dependent path handled by the precheck.

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

clickhouse-gh Bot commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [02a3809]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_asan_ubsan, flaky check) FAIL
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_tsan, flaky check) FAIL
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_msan, flaky check) FAIL
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_debug, flaky check) FAIL
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
Too many test failures FAIL cidb
Stateless tests (amd_asan_ubsan, db disk, distributed plan, sequential, 1/3) FAIL
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
Stateless tests (arm_binary, sequential) FAIL
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
04628_detach_permanently_dict_dependency_race FAIL cidb
Fast test (arm_darwin) DROPPED
Build (amd_darwin) DROPPED
Build (arm_darwin) DROPPED
Build (arm_v80compat) DROPPED

AI Review

Summary

This PR makes DETACH DICTIONARY ... PERMANENTLY roll back the loader registration when a concurrent DDL adds a dependent after the pre-shutdown dependency check, and it adds a failpoint-based regression test for that race. The intended fix is sound, but the rollback currently applies to every StorageDictionary, including ENGINE = Dictionary(...) tables that never owned a loader repository, which introduces a new null-configuration path.

PR Metadata
  • Changelog category should be Bug Fix, not Improvement: this PR fixes user-visible misbehavior in an existing feature.
  • Changelog entry is required and the current entry is specific, but it should match the Bug Fix category and use present tense. Suggested replacement: Fixes a race where \DETACH DICTIONARY ... PERMANENTLY` can be rejected after shutdown if a dependency is registered concurrently. Previously the dictionary could remain attached but unloadable until it was re-attached or the server restarted.`
Findings

❌ Blockers

  • [src/Interpreters/InterpreterDropQuery.cpp:320] The rollback assumes every StorageDictionary had previously deregistered an ExternalDictionariesLoader repository, but StorageDictionary also represents ENGINE = Dictionary(...) tables (Location::Custom). Those storages never set configuration or remove_repository_callback, so restoreDictionaryConfigurationInRepository() creates a fresh ExternalLoaderDictionaryStorageConfigRepository whose load() returns nullptr; ExternalLoader::readFileInfo() then dereferences that null config. A rejected DETACH TABLE ... PERMANENTLY on a Dictionary-engine table can therefore replace the intended HAVE_DEPENDENT_OBJECTS failure with a null-config dereference during rollback. Limit the restore path to DDL-owned dictionaries only, or make the helper a no-op unless the storage actually owns a loader repository/configuration.
Final Verdict

Request changes. The rollback needs to distinguish real DDL dictionaries from Dictionary-engine tables before it is safe to merge.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 24, 2026
@Manerone Manerone self-assigned this Jul 24, 2026
04627 duplicated the pre-existing-dependent case already covered by
04256_105207_detach_dictionary_permanently_dep_check (added by the merged precheck PR);
it also passes on master HEAD, so it is not a regression test. Remove it and keep only the
concurrent-race test 04628, which exercises the rollback via a failpoint. Tag 04628 no-fasttest
because the failpoint (libfiu) is not built in the fast-test image.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@tiandiwonder tiandiwonder changed the title Fix dictionary left unusable after a rejected DETACH ... PERMANENTLY Restore a dictionary left unusable by a rejected DETACH ... PERMANENTLY under concurrent DDL Jul 24, 2026
@tiandiwonder tiandiwonder added pr-improvement Pull request with some product improvements and removed pr-bugfix Pull request with bugfix, not backported by default labels Jul 24, 2026
}
catch (...)
{
if (auto * dictionary = dynamic_cast<StorageDictionary *>(table.get()))

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.

StorageDictionary also backs CREATE TABLE ... ENGINE = Dictionary(...) (Location::Custom), and that variant never registers an ExternalDictionariesLoader repository or stores a non-null configuration. On that path removeDependencies can still throw for a concurrently added dependent, but this rollback calls restoreDictionaryConfigurationInRepository() anyway. The helper then creates an ExternalLoaderDictionaryStorageConfigRepository whose load() returns nullptr, and ExternalLoader::readFileInfo dereferences it unconditionally, so the rejected DETACH TABLE ... PERMANENTLY now goes through a null-config path during rollback instead of preserving the original HAVE_DEPENDENT_OBJECTS exception. Please gate the restore path to real DDL dictionaries only (for example Location::SameDatabaseAndNameAsDictionary / getConfiguration() != nullptr).

@Manerone Manerone 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.

Implementation is fine by me, it would be good to fix the comment from the bot and add a test related to that.

if (query.permanently)
{
DatabaseCatalog::instance().removeDependencies(table_id, check_ref_deps, check_loading_deps, is_drop_or_detach_database);
/// The pre-shutdown check above rejects pre-existing dependents; a dependent registered

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.

For my understanding .. what is a "dependent" in the case of dictionaries?


/// Re-register the loader config repository that shutdown() removed, so an aborted
/// DETACH ... PERMANENTLY leaves the dictionary loadable again (startup() alone does not re-add it).
void restoreDictionaryConfigurationInRepository();

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.

Shouldn't that method be called undoShutdown?

catch (...)
{
if (auto * dictionary = dynamic_cast<StorageDictionary *>(table.get()))
dictionary->restoreDictionaryConfigurationInRepository();

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.

Tbh, the fix looks rather fragile to me and it has a weird effect on the dictionary lifecycle (there is now a new state "undead" from which we can recover to "live" again).

We are now trying to restore the dictionary state if something goes wrong during removeDependencies.

I'm asking myself if it's not the better option to make removeDependencies fail-safe so that it always succeeds (no exceptions)? Then we wouldn't need any of the workaround.

@clickhouse-gh

clickhouse-gh Bot commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Dear @Manerone, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@clickhouse-gh clickhouse-gh Bot added the comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). label Sep 4, 2026
@clickhouse-gh clickhouse-gh Bot added comp-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). and removed comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). labels Nov 19, 2026

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-dictionary Dictionaries (in-memory key-value, periodically refreshed from sources). pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants