Repository: ClickHouse/ClickHouse
File: src/Databases/DatabaseOrdinary.cpp (C++)

Code before:
            throw;
        }
    }
}

bool DatabaseOrdinary::shouldLazyLoad(const ASTCreateQuery & query, LoadingStrictnessLevel mode) const
{
    if (!database_metadata_disk_settings[DatabaseMetadataDiskSetting::lazy_load_tables])
        return false;

    if (query.is_ordinary_view || query.is_materialized_view || query.is_dictionary
        || query.isParameterizedView() || query.is_window_view)
        return false;

    /// Already handled by `StorageTableFunctionProxy`.
    if (query.as_table_function)
        return false;

    if (mode == LoadingStrictnessLevel::FORCE_RESTORE)
        return false;

    return true;
}

void DatabaseOrdinary::loadTableLazy(
    ContextMutablePtr local_context,
    const QualifiedTableName & name,
    const ASTPtr & ast,
    LoadingStrictnessLevel mode)
{
    const auto & query = ast->as<const ASTCreateQuery &>();

    LOG_TRACE(log, "Lazy-loading table {}", name.getFullName());

    ColumnsDescription columns;
    if (query.columns_list && query.columns_list->columns)
        columns = InterpreterCreateQuery::getColumnsDescription(
            *query.columns_list->columns, local_context, mode);

    StorageID table_id(name.database, query.getTable(), query.uuid);
    String table_data_path = getTableDataPath(query);

    auto get_nested = [query_str = ast->formatWithSecretsMultiLine(), db_name = name.database, table_data_path, local_context, mode]() -> StoragePtr
    {
        ParserCreateQuery parser;
        ASTPtr parsed_ast = parseQuery(
            parser,
            query_str.data(),
            query_str.data() + query_str.size(),
            "lazy load",
            0,

Review comment (bug):
Wrong (Current Implementation)

```
auto get_nested = [
    query_str = ast->formatWithSecretsMultiLine(),
    db_name = name.database,
    table_data_path,
    local_context,  
/// ❌ WRONG: Captures temporary context as shared_ptr
    mode
]() -> StoragePtr
{
    /// Hours later: uses stale context from server startup
    local_context->getSettingsRef()[Setting::max_parser_depth];  
/// ❌ Old settings
    createTableFromAST(..., local_context, mode);  
/// ❌ Old context
    return table;
};

/// ❌ local_context's refcount never reaches 0, causing memory leak
```
Problems:

🔴 Holds temporary local_context indefinitely
🔴 Uses settings from server startup time (stale)
🔴 Memory leak: each lazy table holds a full Context copy
🔴 Inconsistent behavior: different tables use different configs

Code after:
            throw;
        }
    }
}

bool DatabaseOrdinary::shouldLazyLoad(const ASTCreateQuery & query, LoadingStrictnessLevel mode) const
{
    if (!database_metadata_disk_settings[DatabaseMetadataDiskSetting::lazy_load_tables])
        return false;

    if (query.is_ordinary_view || query.is_materialized_view || query.is_dictionary
        || query.isParameterizedView() || query.is_window_view)
        return false;

    /// Already handled by `StorageTableFunctionProxy`.
    if (query.as_table_function)
        return false;

    if (mode == LoadingStrictnessLevel::FORCE_RESTORE)
        return false;

    return true;
}

void DatabaseOrdinary::loadTableLazy(
    ContextMutablePtr local_context,
    const QualifiedTableName & name,
    const ASTPtr & ast,
    LoadingStrictnessLevel mode)
{
    const auto & query = ast->as<const ASTCreateQuery &>();

    LOG_TRACE(log, "Lazy-loading table {}", name.getFullName());

    ColumnsDescription columns;
    if (query.columns_list && query.columns_list->columns)
        columns = InterpreterCreateQuery::getColumnsDescription(
            *query.columns_list->columns, local_context, mode);

    StorageID table_id(name.database, query.getTable(), query.uuid);
    String table_data_path = getTableDataPath(query);

    auto get_nested = [query_str = ast->formatWithSecretsMultiLine(),
                        db_name = name.database,
                        table_data_path,
                        global_context = local_context->getGlobalContext(),
                        mode]() -> StoragePtr
    {
        auto load_context = Context::createCopy(global_context);
        ParserCreateQuery parser;
        ASTPtr parsed_ast = parseQuery(