Repository navigation
Drop tables in reverse dependency order during DROP DATABASE - #97057
Conversation
`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]>
|
If we at that point when we care about dependencies. Than there is a question This is the great example how to use dependency graph. We need that for the other places too. |
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.
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]>
…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]>
|
Hello! Maybe add a test to check that tables outside the database are not removed? |
…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]>
`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]>
Summary
DROP DATABASEwas dropping tables in arbitrary iterator order, which could leave orphaned dependent tables on disk if the server crashes mid-operationtables_to_dropin reverse topological order of loading dependencies before the drop loop, so dependents are dropped before their dependenciesThe issue was observed in stress tests where
02916_joinget_dependency.shcreates tables withjoinGetdependencies. WhenDROP DATABASEremoved theMapping(Join) table beforeSub_distributed(Distributed withjoinGetsharding key), a mid-operation crash leftSub_distributedwithout its dependency, causingASYNC_LOAD_FAILEDon 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:Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
DROP DATABASEnow drops tables in reverse dependency order, improving crash-safety when the database contains tables with loading dependencies (e.g.Distributedtables usingjoinGet).🤖 Generated with Claude Code
Version info
26.2.1.807