Skip to content

Drop tables in reverse dependency order during DROP DATABASE - #97057

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-drop-database-dependency-order
Feb 16, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-drop-database-dependency-order

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 16, 2026 •

Copy link
Copy Markdown
Member

Summary

  • DROP DATABASE was dropping tables in arbitrary iterator order, which could leave orphaned dependent tables on disk if the server crashes mid-operation
  • Sort tables_to_drop in reverse topological order of loading dependencies before the drop loop, so dependents are dropped before their dependencies
  • This ensures crash-safety: if the server crashes mid-drop, remaining tables still have their dependencies intact and can be loaded on restart

The issue was observed in stress tests where 02916_joinget_dependency.sh creates tables with joinGet dependencies. When DROP DATABASE removed the Mapping (Join) table before Sub_distributed (Distributed with joinGet sharding key), a mid-operation crash left Sub_distributed without its dependency, causing ASYNC_LOAD_FAILED on restart.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=97053&sha=f9ec529e4ff983e3a9a1c0bba4b5b091aba147e0&name_0=PR
#97053

This addresses the TODO in DatabaseCatalog::checkTableCanBeRemovedOrRenamedUnlocked:

/// TODO unload tables in reverse topological order and remove this code

Changelog category (leave one):

  • Improvement

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

DROP DATABASE now drops tables in reverse dependency order, improving crash-safety when the database contains tables with loading dependencies (e.g. Distributed tables using joinGet).

🤖 Generated with Claude Code

Version info

  • Merged into: 26.2.1.807

`DROP DATABASE` was dropping tables in arbitrary iterator order while
bypassing the `HAVE_DEPENDENT_OBJECTS` check for same-database
dependencies. If the server crashes mid-drop after a dependency table
(e.g. a `Join` table used by `joinGet`) was removed but its dependent
(e.g. a `Distributed` table) still exists on disk, the server fails
to restart because the dependent table tries to evaluate `joinGet`
referencing the now-missing table.

This was observed in stress tests where `02916_joinget_dependency.sh`
creates `Mapping` (Join), `Sub` (MergeTree), and `Sub_distributed`
(Distributed with `joinGet` sharding key) in a test database. When
the database was dropped and the server crashed mid-operation,
`Sub_distributed` metadata remained on disk without its dependency,
causing `ASYNC_LOAD_FAILED` on restart.

The fix sorts `tables_to_drop` in reverse topological order of
loading dependencies before the drop loop. This ensures dependents
are dropped before their dependencies, so if the server crashes
mid-drop, the remaining tables still have their dependencies intact
and can be loaded on restart.

This addresses the TODO in `DatabaseCatalog::checkTableCanBeRemovedOrRenamedUnlocked`:
"TODO unload tables in reverse topological order and remove this code"

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a5455c3]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Feb 16, 2026
@CheSema

CheSema commented Feb 16, 2026

Copy link
Copy Markdown
Member

If we at that point when we care about dependencies. Than there is a question
what we do when db has a table which has dependent table in other db? For example the cases when db_remove is being removed and
- there is a MV in db_other with the target table in the db_remove
- there is a MV in db_other with a source table in the db_remove.
In both cases MV is broken, either target table is missed or source.
Should Clickhouse reject such operation with explanation? Or maybe it is already like that?

This is the great example how to use dependency graph. We need that for the other places too.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Should Clickhouse reject such operation with explanation?

Yes, it will be the easiest option. The user will not be able to drop any of the databases until manually dropping dependencies in the right order.

If we at that point when we care about dependencies.

We do, as this error surfaced in the upgrade check, which we have run for a few years already. It was just ignored all these years.

In Replicated databases, when a MaterializedView is dropped, its
`dropInnerTableIfAny` tries to drop the inner table via `executeDropQuery`.
This path creates a `SECONDARY_QUERY` context without the
`is_replicated_database_internal` flag (no ZK transaction in the
DROP DATABASE path), causing `shouldReplicateQuery` to return true
and `tryEnqueueReplicatedDDL` to reject it with "It's not initial query".

By sorting inner tables (`.inner_id.*`, `.inner.*`) before their parent
views in the drop order, the inner tables are already gone when the MV
is dropped, making `dropInnerTableIfAny` a no-op.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added pr-improvement Pull request with some product improvements and removed pr-bugfix Pull request with bugfix, not backported by default labels Feb 16, 2026
@alexey-milovidov alexey-milovidov self-assigned this Feb 16, 2026
@alexey-milovidov
alexey-milovidov merged commit edc85b7 into master Feb 16, 2026
137 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-drop-database-dependency-order branch February 16, 2026 17:07
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 16, 2026
alexey-milovidov added a commit that referenced this pull request Feb 17, 2026
…d databases

The sort introduced by #97057 classified `.inner_id.*` and `.inner.*`
tables as inner, but missed `.tmp.inner_id.*` and `.tmp.inner.*` tables.
These temporary tables are created by refreshable materialized views
during the table-swap phase of a refresh. `dropInnerTableIfAny` drops
both the main inner table and the temporary one. Without classifying the
temporary tables as inner, the sort could place the MV before its temp
inner table, causing `dropInnerTableIfAny` to attempt dropping the temp
table via a new `InterpreterDropQuery` that goes through the replicated
DDL path and fails with "It's not initial query. ON CLUSTER is not
allowed for Replicated database."

CI report: https://play.clickhouse.com/play?user=play&run=1#U0VMRUNUIGNoZWNrX3N0YXJ0X3RpbWUsIGNoZWNrX25hbWUsIHRlc3RfbmFtZSwgcmVwb3J0X3VybApGUk9NIGNoZWNrcwpXSEVSRSAxCiAgICBBTkQgY2hlY2tfc3RhcnRfdGltZSA+PSBub3coKSAtIElOVEVSVkFMIDI0IEhPVVIKICAgIEFORCAoaGVhZF9yZWYgPSAnbWFzdGVyJyBBTkQgc3RhcnRzV2l0aChoZWFkX3JlcG8sICdDbGlja0hvdXNlLycpKQogICAgQU5EIHRlc3Rfc3RhdHVzICE9ICdTS0lQUEVEJwogICAgQU5EICh0ZXN0X3N0YXR1cyBMSUtFICdGJScgT1IgdGVzdF9zdGF0dXMgTElLRSAnRSUnKQogICAgQU5EIGNoZWNrX3N0YXR1cyAhPSAnc3VjY2VzcycKICAgIEFORCBjaGVja19uYW1lIE5PVCBMSUtFICdsaWJGdXp6ZXIlJwogICAgQU5EIGNoZWNrX25hbWUgIT0gJ0NsaWNrSG91c2UgS2VlcGVyIEplcHNlbicKICAgIEFORCBjaGVja19uYW1lIE5PVCBMSUtFICdJbnRlZ3JhdGlvbiB0ZXN0cyAoYW1kX21zYW4lJwogICAgQU5EIGNoZWNrX25hbWUgTk9UIExJS0UgJ0ludGVncmF0aW9uIHRlc3RzIChhbWRfbGx2bV9jb3ZlcmFnZSUnCk9SREVSIEJZIGNoZWNrX3N0YXJ0X3RpbWUgREVTQw==

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@PedroTadim

Copy link
Copy Markdown
Member

Hello! Maybe add a test to check that tables outside the database are not removed?

bacek pushed a commit to bacek/ClickHouse that referenced this pull request Aug 15, 2026
…rder

`DatabaseCatalog::checkTableCanBeRemovedOrRenamedUnlocked` skips dependent
tables from the same database when the removal comes from `DROP DATABASE`,
and carried a TODO asking for tables to be unloaded in reverse topological
order instead.

That ordering exists since ClickHouse#97057:
`InterpreterDropQuery::executeToDatabaseImpl` sorts the tables of a
`DROP DATABASE` in reverse loading dependency order, so a dependent is always
dropped before the table it depends on. The skip here is still required - the
dependency graph is consulted per table while the database is being emptied -
so only the obsolete TODO is replaced by a pointer to the code that maintains
the invariant.

Related: ClickHouse#87369
Related: ClickHouse#102506

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
alexey-milovidov added a commit to BoloniniD/ClickHouse that referenced this pull request Aug 16, 2026
`InterpreterDropQuery::executeToDatabaseImpl` sorted the tables of a
database being dropped in reverse topological order of loading
dependencies only (see ClickHouse#97057). Now referential dependencies are taken
into account as well, so a dependent is always dropped before the
tables it depends on, and if the server crashes mid-drop, the remaining
tables keep their dependencies intact.

Implements the TODO from ClickHouse#114917

Co-Authored-By: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-improvement Pull request with some product improvements 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.

4 participants