Skip to content

Cherry pick #113224 to 25.8: Do not read the internal database of temporary tables in Merge - #113879

Merged
robot-clickhouse merged 9 commits into
backport/25.8/113224from
cherrypick/25.8/113224
Aug 8, 2026
Merged

robot-clickhouse merged 9 commits into
backport/25.8/113224from
cherrypick/25.8/113224

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #113224

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

alexey-milovidov and others added 7 commits August 4, 2026 02:19
`_temporary_and_external_tables` holds the temporary tables of all
sessions and all users, and is not covered by access control, so direct
access to it is denied in `DatabaseCatalog::tryGetDatabaseAndTable`.
`StorageMerge` enumerated it like any other database: a database regexp
such as `merge(REGEXP('.+'), 't.*')` matched it, and naming it explicitly
worked as well, so one session could read the temporary tables of another
session and of another user.

On the old analyzer path this also aborted a debug server on the
`chassert(database_name != "_temporary_and_external_tables")` in
`ASTSelectQuery::replaceDatabaseAndTable`.

A database regexp now skips it, the same way `system.databases`,
`system.tables` and `system.columns` do, and naming it explicitly throws
`DATABASE_ACCESS_DENIED`.
…engine at CREATE time

With an explicit column list, `CREATE TABLE ... ENGINE = Merge('_temporary_and_external_tables', ...)`
did not need schema inference, so the read-time check never ran and an unusable table definition was
stored. Reject the internal database of temporary tables in the `Merge` engine creation path as well
(only for `CREATE`, so existing tables still attach on server startup), and cover the DDL case in the test.

Co-Authored-By: Claude Fable 5 <[email protected]>
…engine on a full-definition `ATTACH` as well

A full-definition `ATTACH TABLE ... ENGINE = Merge(...)` is CREATE-like user input
(`InterpreterCreateQuery` forwards it as `LoadingStrictnessLevel::ATTACH`), so it bypassed
the CREATE-time guard and could persist the forbidden and unusable definition. Deny it the
same way, using the `isLoadingFromExistingMetadata(args.mode) || args.query.attach_short_syntax`
idiom from `StorageDistributed`, so only loads of previously stored metadata (server startup,
short-syntax `ATTACH`) are exempt.
…-database DDL replay) able to load a pre-existing `Merge('_temporary_and_external_tables', ...)` definition

The registration guard now denies only fresh user-supplied definitions
(`CREATE` and a full-definition `ATTACH`), and
`StorageMerge::getInMemoryMetadataPtr` tolerates `DATABASE_ACCESS_DENIED`
from the source-database enumeration the same way as `UNKNOWN_DATABASE`,
because `validateStorage` resolves the metadata through it on every
create-like path, including `RESTORE`. Reading from such a table is still
denied. Adds `04813_merge_temporary_database_restore`, which restores an
edited backup to exercise the `SECONDARY_CREATE` path.
…_temporary_and_external_tables', ...)` table

`StorageMerge::tryGetColumnSizes` only tolerated `UNKNOWN_DATABASE`, so the size columns of
`system.columns` threw `DATABASE_ACCESS_DENIED` for exactly the pre-existing definition that
`getInMemoryMetadataPtr` keeps loadable. The column sizes are a best-effort introspection, so
treat the forbidden database the same way as a dropped one. Extended
`04813_merge_temporary_database_restore` to query the size columns of the restored table.

Co-Authored-By: Claude Fable 5 <[email protected]>
Do not read the internal database of temporary tables in `Merge`
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-critical-bugfix labels Aug 7, 2026
…ypick/25.8/113224

# Conflicts:
#	src/Storages/StorageMerge.cpp
…CESS_DENIED` in place, there is no `tryGetColumnSizes`
@robot-clickhouse
robot-clickhouse merged commit 8b0c0eb into backport/25.8/113224 Aug 8, 2026
1 check passed
@robot-clickhouse
robot-clickhouse deleted the cherrypick/25.8/113224 branch August 8, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants