Skip to content

Add SYSTEM UNLOAD DICTIONARY / DICTIONARIES commands - #109639

Merged
Manerone merged 32 commits into
ClickHouse:masterfrom
Manerone:pr-96064
Jul 16, 2026
Merged

Manerone merged 32 commits into
ClickHouse:masterfrom
Manerone:pr-96064

Conversation

@Manerone

@Manerone Manerone commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Continuation of PR #96064, because I do not have rights to push directly in the related fork.

Changelog category (leave one):

  • New Feature

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

Add SYSTEM UNLOAD DICTIONARY and SYSTEM UNLOAD DICTIONARIES commands to release dictionary memory without dropping the dictionary definition. Dictionaries will be reloaded lazily on next access.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

New SQL commands:

  • SYSTEM UNLOAD DICTIONARY [db.]name — unloads a specific dictionary from memory, releasing its data. The dictionary will be reloaded automatically on next query that accesses it.
  • SYSTEM UNLOAD DICTIONARIES — unloads all loaded dictionaries.

Motivation: When dictionaries consume significant memory but are only needed occasionally, users can free memory without dropping and recreating the dictionary. This was previously not possible — the only options were DROP DICTIONARY (loses definition) or waiting for server restart.

Reuses SYSTEM_RELOAD_DICTIONARY access rights, so users with reload permissions can also unload.

Example:

-- Unload a specific dictionary
SYSTEM UNLOAD DICTIONARY my_database.my_dict;

-- Unload all dictionaries
SYSTEM UNLOAD DICTIONARIES;

-- Dictionary reloads automatically on next access
SELECT dictGet('my_database.my_dict', 'value', toUInt64(1));

Closes #93776

@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [602f590]

Summary: ✅


AI Review

Summary

This PR adds SYSTEM UNLOAD DICTIONARY / SYSTEM UNLOAD DICTIONARIES, resets dictionary loader state so unloaded dictionaries are lazy-reloaded on the next access, and updates the status/docs around NOT_LOADED. The local unload/reload path looks much better now, but I do not think the change is ready to merge: the singular ON CLUSTER form still rewrites unqualified dictionary names against the worker's default database, and unload still erases the last stored source exception from system.dictionaries.

Findings

❌ Blockers

  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/109639#discussion_r3577447891] [src/Interpreters/InterpreterSystemQuery.cpp:352-372] SYSTEM UNLOAD DICTIONARY dict ON CLUSTER ... still returns from execute before the single-dictionary name is canonicalized. The remote rewrite in ASTQueryWithOnCluster::removeOnCluster / DDLTask then fills in a missing database from each worker's default_database, so USE db1; SYSTEM UNLOAD DICTIONARY dict ON CLUSTER c can unload default.dict or throw BAD_ARGUMENTS instead of targeting db1.dict. Qualify singular dictionary targets before the ON CLUSTER early return, or preserve the initiator database in the remote rewrite.

⚠️ Majors

  • [dismissed by author -- https://github.com/ClickHouse/ClickHouse/pull/109639#discussion_r3582000146] [src/Interpreters/ExternalLoader.cpp:836-839] Unload now preserves historical timestamps and error_count, but it still clears info.exception. After a dictionary had a failed background reload and is then unloaded, system.dictionaries keeps showing that errors happened yet loses last_exception, which removes the only concrete diagnostic for the last source failure. Preserve the stored exception together with the other historical metadata instead of zeroing it on unload.
Final Verdict

Needs changes before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Jul 7, 2026
Comment thread src/Interpreters/ExternalLoader.cpp Resolved
@Manerone
Manerone marked this pull request as ready for review July 8, 2026 10:25
@Manerone Manerone added the can be tested Allows running workflows for external contributors label Jul 8, 2026
Comment thread src/Interpreters/ExternalLoader.cpp Resolved
CheSema added a commit that referenced this pull request Jul 10, 2026
Adding CLEAR_ENCRYPTION_HEADERS_CACHE (SYSTEM DROP ENCRYPTION HEADERS
CACHE) pushed the last enumerator RESET_DDL_WORKER from ordinal 127 to
128, outside magic_enum's default reflection range [-128, 127].
ParserSystemQuery matches SYSTEM keywords via magic_enum::enum_values,
so RESET_DDL_WORKER silently dropped out of the value list: SYSTEM RESET
DDL WORKER stopped parsing and its access check was never reached,
making 04401_system_reset_ddl_worker_access fail with both the
unprivileged and on-cluster queries reported as NOT denied.

Specialize magic_enum::customize::enum_range<ASTSystemQuery::Type> to
cover every value (min = 0, max = 512), matching the fix used by other
recent SYSTEM-command additions (e.g. #109639).
@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Jul 13, 2026
@CheSema CheSema self-assigned this Jul 14, 2026
Comment thread src/Interpreters/ExternalLoader.cpp Resolved
Comment thread src/Interpreters/ExternalLoader.cpp Resolved
Comment thread src/Interpreters/ExternalLoader.cpp Resolved
Comment thread docs/reference/statements/system.mdx Resolved
@clickhouse-gh

clickhouse-gh Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 28.60% 85.80% +57.20%
Functions 39.40% 92.70% +53.30%
Branches 20.60% 78.00% +57.40%

Changed lines: Changed C/C++ lines covered: 86/92 (93.48%) · Uncovered code

Full report · Diff report

@Manerone
Manerone added this pull request to the merge queue Jul 16, 2026
Merged via the queue into ClickHouse:master with commit 22d8460 Jul 16, 2026
176 checks passed
@Manerone
Manerone deleted the pr-96064 branch July 16, 2026 10:18
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 16, 2026
@Manerone

Copy link
Copy Markdown
Contributor Author

@groeneai the tag -- Tags: no-parallel was wrongly added to the test file here, please create a new PR with the fix and ping me, and only me, about it.

@groeneai

Copy link
Copy Markdown
Collaborator

@Manerone acknowledged. I'll open a new PR removing the -- Tags: no-parallel line from tests/queries/0_stateless/03814_system_unload_dictionary.sql, keeping the test parallel-safe, and I'll ping you (and only you) on it.

@groeneai

Copy link
Copy Markdown
Collaborator

@Manerone I looked into removing -- Tags: no-parallel from 03814_system_unload_dictionary.sql and it turns out the tag is load-bearing, not spurious. Details, since removing it would introduce flakiness:

Why it flakes without the tag. The test runs SYSTEM UNLOAD DICTIONARIES (plural, step 7). That maps to ExternalDictionariesLoader::unloadAllDictionaries() -> ExternalLoader::unloadAll() (src/Interpreters/ExternalLoader.cpp:677), which iterates every loaded dictionary with no database filter. It is server-global, not scoped to currentDatabase(). The singular SYSTEM UNLOAD DICTIONARY name is db-scoped and safe; the plural one is not.

Two failure modes I reproduced locally (debug build, 12 concurrent copies):

  1. Self-parallel: with the tag removed, 20/240 (~8%) copies failed. A sibling copy's plural unload clears this copy's dictionary between the lazy dictGet (which loads it) and the following status SELECT, so LOADED reads back as NOT_LOADED (reference lines 2/5/7).
  2. Cross-test: the plural unload also clears dictionaries owned by unrelated parallel tests. 04365_dictionary_lazy_load has no no-parallel tag and asserts dict_eager LOADED; a concurrent run of this test would flip it to NOT_LOADED.

Dropping only the racy LOADED assertions fixes mode 1 but not mode 2, so it is not a real fix.

Convention. Every other stateless test that issues a global dictionary command is no-parallel for exactly this reason: 00950_dict_get, 01036_no_superfluous_dict_reload_on_create_database (comment: "Does not allow if other tests do SYSTEM RELOAD DICTIONARIES at the same time"), 02179_dict_reload_on_cluster, 04117_parser_system_query_variants.

Options, your call:

  1. Keep no-parallel (the tag guards a real hazard).
  2. Split the file: a parallel test for the singular db-scoped commands (drop the racy LOADED snapshots there), plus a small no-parallel test for the two SYSTEM UNLOAD DICTIONARIES cases. This keeps most of the test parallel but still needs no-parallel on the global-command part.

I have not pushed anything. Let me know which you prefer and I will open the PR.

@Manerone

Copy link
Copy Markdown
Contributor Author

@groeneai split the test file into two, keep the SYSTEM UNLOAD DICTIONARIES in their own test file with the no-parallel tag, and move the other parts of the test into their own test file where we can allow parallel tests. Create a new PR for this and ping me

@groeneai

Copy link
Copy Markdown
Collaborator

@Manerone acknowledged. I'll split 03814_system_unload_dictionary.sql into two tests: one keeping the server-global SYSTEM UNLOAD DICTIONARIES cases with the no-parallel tag, and a parallel-safe one for the db-scoped SYSTEM UNLOAD DICTIONARY name and the rest. I'll open a new PR and ping you, and only you, on it.

@groeneai

Copy link
Copy Markdown
Collaborator

@Manerone opened the split PR: #111773

03814_system_unload_dictionary keeps the database-scoped singular SYSTEM UNLOAD DICTIONARY cases (parallel), and the new 03814_system_unload_dictionaries holds the two server-global SYSTEM UNLOAD DICTIONARIES cases with no-parallel, exactly as you asked.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Jul 24, 2026
Manerone asked to turn the section-header comments into SELECT queries so the
reference file reads cleanly; apply that to both split tests.

The clickhouse-gh bot noted the split dropped the mixed
SYSTEM UNLOAD DICTIONARIES -> SYSTEM UNLOAD DICTIONARY assertion (verifying a
dictionary already unloaded server-wide is still idempotently unloadable via the
singular path). Restore that assertion at the end of the second global-unload
block in the no-parallel test.

Related: ClickHouse#109639

Co-Authored-By: Claude Opus 4.8 <[email protected]>
pull Bot pushed a commit to Mu-L/ClickHouse that referenced this pull request Jul 28, 2026
…el tests

SYSTEM UNLOAD DICTIONARIES (plural) is server-global: it unloads
dictionaries owned by other concurrently-running tests, which is why
03814_system_unload_dictionary required the no-parallel tag. The singular
SYSTEM UNLOAD DICTIONARY name is database-scoped and parallel-safe.

Move the two server-global SYSTEM UNLOAD DICTIONARIES cases into a new
03814_system_unload_dictionaries test that keeps no-parallel, and drop
no-parallel from 03814_system_unload_dictionary, which now exercises only
the database-scoped singular commands and runs in parallel. All existing
coverage is preserved: the parallel test keeps its LOADED/NOT_LOADED status
transitions because, once the global command is split out, no parallel-batch
test issues a global unload.

Related: ClickHouse#109639

Co-Authored-By: Claude Opus 4.8 <[email protected]>
ianton-ru pushed a commit to Altinity/ClickHouse that referenced this pull request Aug 12, 2026
Adding CLEAR_ENCRYPTION_HEADERS_CACHE (SYSTEM DROP ENCRYPTION HEADERS
CACHE) pushed the last enumerator RESET_DDL_WORKER from ordinal 127 to
128, outside magic_enum's default reflection range [-128, 127].
ParserSystemQuery matches SYSTEM keywords via magic_enum::enum_values,
so RESET_DDL_WORKER silently dropped out of the value list: SYSTEM RESET
DDL WORKER stopped parsing and its access check was never reached,
making 04401_system_reset_ddl_worker_access fail with both the
unprivileged and on-cluster queries reported as NOT denied.

Specialize magic_enum::customize::enum_range<ASTSystemQuery::Type> to
cover every value (min = 0, max = 512), matching the fix used by other
recent SYSTEM-command additions (e.g. ClickHouse#109639).
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Adding CLEAR_ENCRYPTION_HEADERS_CACHE (SYSTEM DROP ENCRYPTION HEADERS
CACHE) pushed the last enumerator RESET_DDL_WORKER from ordinal 127 to
128, outside magic_enum's default reflection range [-128, 127].
ParserSystemQuery matches SYSTEM keywords via magic_enum::enum_values,
so RESET_DDL_WORKER silently dropped out of the value list: SYSTEM RESET
DDL WORKER stopped parsing and its access check was never reached,
making 04401_system_reset_ddl_worker_access fail with both the
unprivileged and on-cluster queries reported as NOT denied.

Specialize magic_enum::customize::enum_range<ASTSystemQuery::Type> to
cover every value (min = 0, max = 512), matching the fix used by other
recent SYSTEM-command additions (e.g. ClickHouse#109639).
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Add SYSTEM UNLOAD DICTIONARY / DICTIONARIES commands
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 pr-feature Pull request with new product feature pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An option to unload dictionary

4 participants