Skip to content

Add lazy_load_tables database setting for deferred table loading - #96283

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
xiaohuanlin:feature/lazy-table-loading
Feb 19, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
xiaohuanlin:feature/lazy-table-loading

Conversation

@xiaohuanlin

@xiaohuanlin xiaohuanlin commented Feb 7, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • New Feature

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

Add lazy_load_tables database setting. When enabled, tables are not loaded during database startup — a lightweight StorageTableProxy is created instead and the real table engine is materialized on first access.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Motivation: Large databases with many tables can have slow startup times because every table is loaded eagerly. The lazy_load_tables setting allows deferring table loading until the table is actually accessed, reducing startup time and memory usage for databases where only a subset of tables are actively queried.

Parameters:

  • lazy_load_tables (Bool, default false) — Database-level setting. When set to 1/true, tables are replaced with a lightweight StorageTableProxy during database startup. The real storage engine is created and started on first read/write access.

Scope: Works with Atomic, Ordinary, and similar on-disk database engines. Views, dictionaries, materialized views, and table functions are excluded from lazy loading (they load normally).

Example use:

CREATE DATABASE my_db ENGINE = Atomic SETTINGS lazy_load_tables = 1;
CREATE TABLE my_db.t1 (id UInt64) ENGINE = MergeTree ORDER BY id;

-- After server restart or DETACH/ATTACH, t1 appears as TableProxy in system.tables
-- until it is first accessed:
SELECT name, engine FROM system.tables WHERE database = 'my_db';
-- ┌─name─┬─engine─────┐
-- │ t1   │ TableProxy │
-- └──────┴────────────┘

-- First query triggers real loading transparently:
SELECT * FROM my_db.t1;

Closes #94039

Version info

  • Merged into: 26.2.1.950

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Feb 7, 2026
@clickhouse-gh

clickhouse-gh Bot commented Feb 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [65019fe]

Summary: ❌

job_name test_name status info comment
AST fuzzer (amd_debug) failure
Logical error: Bad cast from type A to B (STID: 2241-4b49) FAIL cidb

@clickhouse-gh clickhouse-gh Bot added the pr-feature Pull request with new product feature label Feb 7, 2026
Introduce a new Bool database setting `lazy_load_tables`. When enabled,
tables are not loaded during database startup. Instead, a lightweight
`StorageTableProxy` is created and the real table is materialized on
first access.

Views, dictionaries, materialized views, and table functions are
excluded from lazy loading.
@xiaohuanlin
xiaohuanlin force-pushed the feature/lazy-table-loading branch from 7a21800 to cf03299 Compare February 7, 2026 04:04
The flaky check runs tests 50 times. Hardcoded database name
`test_lazy_load` caused `DATABASE_ALREADY_EXISTS` errors on
concurrent reruns. Switch to `{CLICKHOUSE_DATABASE_1:Identifier}`
for a unique name per run.
Add `USE` after each DETACH/ATTACH cycle and replace parameterized
database name with `currentDatabase` in `system.tables` WHERE clauses,
as required by the CI style check.
@tiandiwonder
tiandiwonder requested a review from Copilot February 9, 2026 04:50
@tiandiwonder tiandiwonder self-assigned this Feb 9, 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

Adds a new database-level lazy_load_tables setting to defer loading on-disk tables at database startup by attaching a lightweight proxy storage and materializing the real engine on first access.

Changes:

  • Introduces StorageTableProxy to lazily materialize the underlying storage on first read/write/drop.
  • Extends DatabaseOrdinary to attach StorageTableProxy during metadata loading when lazy_load_tables is enabled.
  • Adds a stateless query test covering proxy visibility in system.tables, first access behavior, and DROP on an unloaded proxy.

Reviewed changes

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

Show a summary per file
File Description
tests/queries/0_stateless/03823_lazy_load_tables.sql Adds integration test for lazy table loading via detach/attach cycle and first access.
tests/queries/0_stateless/03823_lazy_load_tables.reference Expected outputs for the new lazy-load test.
src/Storages/StorageTableProxy.h New proxy storage that loads the real table storage on-demand and delegates operations.
src/Databases/DatabaseOrdinary.h Declares helpers to decide and attach tables lazily.
src/Databases/DatabaseOrdinary.cpp Implements lazy attach path and eligibility checks for lazy loading.
src/Databases/DatabaseMetadataDiskSettings.h Extends metadata disk settings to support Bool settings.
src/Databases/DatabaseMetadataDiskSettings.cpp Adds lazy_load_tables setting and ensures applyChanges() is called when disk isn’t specified.
Comments suppressed due to low confidence (1)

src/Databases/DatabaseMetadataDiskSettings.cpp:1

  • This introduces a new user-facing setting (lazy_load_tables) but the PR metadata indicates documentation is not yet written. Please add user docs for the new database setting (scope, limitations/exclusions like views/dictionaries, and the observable behavior in system.tables) to avoid discoverability/support issues.
#include <Databases/DatabaseMetadataDiskSettings.h>

Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Databases/DatabaseOrdinary.cpp Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
@xiaohuanlin

Copy link
Copy Markdown
Contributor Author

Thanks for the review! Addressed in c2799e8:

  • Comment 1 (write metadata): Fixed — now uses storage->getInMemoryMetadataPtr() from the nested storage, consistent with read.
  • Comment 2 (publish-after-init locking): Kept the simple lock for now. StorageTableFunctionProxy uses the same pattern, and the lock is only held on the one-time first access. Concurrent first-access to the same table is rare in practice.
  • Comment 3 (lambda memory): Fixed — lambda now captures ast->formatWithSecretsMultiLine() (a String) instead of ASTPtr, and re-parses on demand. This significantly reduces per-table memory for large databases.
  • Comment 4 (getName): Fixed — returns nested->getName() after load, "TableProxy" before.
  • Comment 5 (log level): Fixed — changed to LOG_TRACE.

Comment thread src/Databases/DatabaseOrdinary.cpp Resolved
Comment thread src/Databases/DatabaseOrdinary.cpp Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
Comment thread src/Storages/StorageTableProxy.h Resolved
- Harden `StorageTableProxy::drop` and `checkTableCanBeDropped`
- Capture `global_context` instead of `local_context` in lazy-load lambda
- Make `renameInMemory` explicit instead of relying on base class
- Expand `lazy_load_tables` documentation
- Fix RENAME test to avoid dependency conflict
`StorageProxy::renameInMemory` calls `getNested()` which triggers lazy
loading. We intentionally call `IStorage::renameInMemory` directly to
update the `StorageID` without loading the nested table.

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

Looks good! @alexey-milovidov

@xiaohuanlin

Copy link
Copy Markdown
Contributor Author

Hi, could someone please this PR?

@alexey-milovidov
alexey-milovidov merged commit da7a5d5 into ClickHouse:master Feb 19, 2026
129 of 130 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 19, 2026
@4ertus2

4ertus2 commented Feb 24, 2026

Copy link
Copy Markdown
Contributor

Would it help with #87707 ?
Shouldn't we set lazy tables liad by default for lakehouse catalogs? They contain lots of tables with very slow loading.
It's also interesting non-blocking tables load: to start as lazy and load some or all tables in background.

@xiaohuanlin

Copy link
Copy Markdown
Contributor Author

Hi, my PR #96283 (lazy_load_tables) won't help here. It's implemented in DatabaseOrdinary and doesn't apply to DatabaseDataLake.
The real issue is that getTablesIterator() in DatabaseDataLake eagerly loads full Iceberg metadata for every table (multiple chained S3 requests per table, no caching). Fixing this would require implementing a similar lazy-loading mechanism specifically for DatabaseDataLake, which is separate work.

@tavplubix

Copy link
Copy Markdown
Member

We already have async_load_databases - do we need a separate mechanism that does the same?

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.

Also, what's the difference from

class StorageProxy : public IStorage
?

@alexey-milovidov

Copy link
Copy Markdown
Member

filimonov added a commit to Altinity/ClickHouse that referenced this pull request Jul 20, 2026
…ETACH catch-22

Deliverable 3 of the stuck-table-load fix. A CAS-free description of the generic
ClickHouse problem: a table whose async load job fails is permanently stuck
until restart, and DETACH cannot recover it because table resolution
(DatabaseCatalog/DatabasesCommon waitTableStarted) waits on the failed load job
before reaching the state-erasing detach path. References lazy_load_tables
(ClickHouse#96283) and related issues ClickHouse#88934/ClickHouse#67521. Draft only; not filed.
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lazy loading of tables inside Atomic, Ordinary, and similar database engines

7 participants