Repository navigation
Attach the system tables in clickhouse local on first access - #115489
Conversation
`clickhouse local` attached all ~164 system tables and both spellings of `information_schema` up front. The latter alone parses 20 embedded `CREATE VIEW` statements and builds 40 view storages, and an invocation such as `clickhouse local --query "SELECT 1"` never reads any of them. Now `DatabaseWithOwnTablesBase::setDeferredPopulation` fills the table list on first access instead. `system.one` is the exception and stays eager: every `FROM`-less query resolves it during analysis, so deferring it along with the rest would just move the whole cost into the first query. Accordingly the table lookups check the already attached tables before triggering population. The server keeps attaching its system tables eagerly. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
Workflow [PR], commit [c5bf967] Summary: ✅
AI ReviewSummaryThis PR defers Missing context / blind spots
Final Verdict✅ No new findings on the current head. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 169/170 (99.41%) · Uncovered code |
alexey-milovidov
left a comment
There was a problem hiding this comment.
Ok. However, an interactive client requests system.comptetions immediately. Just a note, because the startup of the interactive client is not a concern.
|
Also, I'm planning to make |
`04516_user_query_log_local` checks that `clickhouse local` rejects `query_log.table = user_query_log` right away, but the check lived inside `attachSystemTablesServer`, which this branch defers until the first access to the `system` database. `clickhouse local --query "SELECT 1"` only resolves `system.one`, so the bad configuration was silently accepted. Split the check out as `validateUserQueryLogConfig` - it inspects the configuration rather than the database - and call it eagerly at both `clickhouse local` call sites that defer the system tables.
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units69 translation units recompiled, 621 s compile time in total, 69 of them have a recent master baseline. Symbol sizes
|
`clickhouse local --path` loads a `system` database from disk when the path holds one, as a running server leaves behind, and until now that path attached all the system table storages eagerly, so the deferral only applied to the ephemeral `system` database that `clickhouse local` creates on its own. Arm the deferred population for the database loaded from disk too: the tables that come from disk stay attached and visible, and the system table storages are added on top of them on the first access to the database. The collision checks that `attachSystemTablesServer` runs before it attaches `system.user_query_log` move into `validateSystemUserQueryLog`, which now covers the pre-existing-table check as well - it is the one that matters for a database loaded from disk - and runs before the population is armed, so that the existence check does not trigger it. The on-disk database engines reach the table list through a few accessors of their own, so those get the `ensurePopulated` hook as well: `DatabaseOrdinary::getAllTableNames` reads `tables` directly, and `DatabaseAtomic::detachTable` does not go through `DatabaseWithOwnTablesBase::detachTable`. `DatabaseOnDisk::createTable` gets it so that a `CREATE TABLE system.<name>` colliding with a not-yet-attached system table fails at `CREATE` time, rather than throwing `TABLE_ALREADY_EXISTS` from an unrelated later query.
`RENAME TABLE ... TO system.numbers` reached the rename path in `DatabaseAtomic` and `DatabaseOnDisk` without the destination database ever being asked for its full table list: the target-name checks there look at metadata files only, and a deferred `system` database keeps its reserved names in the not-yet-attached populator. `InterpreterRenameQuery` does call `assertTableDoesntExist`, which forces population, so the user-facing behaviour was already correct, but the invariant "anything that inspects the table list sees a populated database" did not hold for the rename entry points themselves. Populate the source and the destination at the start of both rename implementations - before the source table is detached, so a collision can no longer be discovered halfway through the `Ordinary` rename - and cover `RENAME` into a reserved `system` name, for both the persistent and the ephemeral `system` database, in `05023_local_system_database_on_disk_deferred`.
|
🕵 Addressed the AI review blocker in 2f31c32: The only red check, |
|
📊 Cloud Performance Report ✅ AI verdict: Release-transition baseline: compared against the last 26.8 master because no 26.9 master baseline exists at or before the trusted base-commit anchor. This PR only changes clickhouse-local startup, deferring system and information_schema table population until first access; it does not touch the server's query-execution hot path that ClickBench and TPC-H exercise. Both flagged ClickBench queries (Q16 at +11.1%, Q28 at -6.5%) landed as not_sure, and neither can plausibly be attributed to this change, so they read as run-to-run variance rather than real deltas. tpch_adapted_1_official showed nothing flagged. No action needed on this PR from a performance standpoint. clickbenchFlagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
…the path `DatabaseWithOwnTablesBase::isTableExist` and `tryGetTableNoWait` answer from the already attached tables without running the deferred population, which is what keeps `system.one` from dragging in the whole `system` database. For a `system` database loaded from a `--path` that fast path also covered the tables the path stores, so a stored `system.settings` (or any other name a later version gave to a system table) was returned instead of the system table, while the eager attachment it replaces reports `TABLE_ALREADY_EXISTS` at startup. `setDeferredPopulation` now remembers the names attached before it was armed, and a lookup of one of them populates the database first, so the collision is reported. A failed population is remembered too and rethrown by every later `ensurePopulated`, instead of leaving a half-filled database behind. `system.one` is attached after the population is armed, so that it is not taken for such a name, and `validateSystemUserQueryLog` runs before, so that its existence check does not trigger what it is supposed to precede.
A failed deferred population leaves `has_deferred_population` set so that `ensurePopulated` rethrows the saved error, but the lock-free fast path in `tryGetTableNoWait` and `isTableExist` only consulted the shadowing candidates: a table the populator managed to attach before it threw (such as `system.numbers` before a `system.settings` collision), or the eagerly attached `system.one`, kept being served, exposing an observably half-populated database. Remember the failure in an atomic flag and route every lookup through `ensurePopulated` once it is set.
…ation test The `--path` coverage only staged an `Ordinary` `system` database, while a running server leaves behind an `Atomic` one, and the deferred population touches `DatabaseAtomic::detachTable` and `DatabaseAtomic::renameTable` specifically. Stage an `Atomic` persistent `system` database (created under a staging name and renamed on disk, since the database metadata file stores a placeholder instead of the name) and repeat the reserved-name `CREATE`/`RENAME` collision cases and the stored-table shadowing cases on it. Co-Authored-By: Claude Fable 5 <[email protected]>
| StoragePtr tryGetTableNoWait(const String & table_name) const; | ||
|
|
||
| private: | ||
| mutable std::atomic<bool> has_deferred_population{false}; |
There was a problem hiding this comment.
The implementation is complex, especially here, but acceptable.
Split out of #112496, where profiling showed that
clickhouse local --query "SELECT 1"spendsalmost all of its 45ms before the query, 1ms of which is the query itself.
clickhouse localattached all ~164 system tables and both spellings ofinformation_schemaup front, about 10ms of work.information_schemaalone parses 20 embeddedCREATE VIEWstatements and builds 40 view storages, and an invocation such asclickhouse local --query "SELECT 1"never reads any of it.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
clickhouse localnow attaches thesystemandinformation_schematables on first accessinstead of at startup.
Workflow [PR]
Sync PR [sync-upstream/pr/115489]
Version info
26.9.1.304(included in26.9and later)