Skip to content

Take referential dependencies into account in DROP DATABASE table order - #114952

Merged
alexey-milovidov merged 2 commits into
masterfrom
drop-database-referential-deps-order
Aug 16, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
drop-database-referential-deps-order

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 15, 2026 •

Copy link
Copy Markdown
Member

Related: #114917
Related: #97057

InterpreterDropQuery::executeToDatabaseImpl sorts the tables of a database being dropped in reverse topological order, so that 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 and can be loaded on restart. Previously the ordering took only loading dependencies into account (#97057); now it takes the union of loading and referential dependencies (loading dependencies are mostly a subset of referential ones, but that is not enforced anywhere, so we merge both). This implements the TODO added in #114917, and the comment in DatabaseCatalog::checkTableCanBeRemovedOrRenamedUnlocked now simply documents the invariant.

Like #97057, this comes without a functional test: within a single DROP DATABASE, dependents from the same database are deliberately ignored by the dependency check, so the drop order is not observable without crash injection in the middle of DROP DATABASE.

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 topological order of both loading and referential dependencies (previously only loading dependencies), so a crash in the middle of DROP DATABASE cannot leave a table whose dependencies were already dropped.

Version info

  • Merged into: 26.8.1.1488 (included in 26.8 and later)

`InterpreterDropQuery::executeToDatabaseImpl` sorted the tables of a
database being dropped in reverse topological order of loading
dependencies only (see #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 #114917

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2bb5dc5]

Summary: ✅


AI Review

Summary

This PR updates DROP DATABASE table ordering to include both loading and referential dependencies before the per-table drops run, so same-database dependents are removed before the tables they reference. I reviewed the changed drop path, the dependency-graph behavior it relies on, and the existing PR metadata; I did not find a correctness or contract issue in the current code.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.80% 86.70% -0.10%
Functions 92.00% 92.00% +0.00%
Branches 79.20% 79.10% -0.10%

Changed lines: Changed C/C++ lines covered: 15/16 (93.75%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 15, 2026
…ential-deps-order

# Conflicts:
#	src/Interpreters/DatabaseCatalog.cpp
@clickhouse-gh

clickhouse-gh Bot commented Aug 15, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 2bb5dc5cc with master 98ee3b2b4 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes
Binary Master PR Δ
programs/clickhouse-stripped 694.45 MiB 691.43 MiB -3.01 MiB (-0.43%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Compile time of recompiled translation units

9 translation units recompiled, 35 s compile time in total, 8 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks ok.

@alexey-milovidov alexey-milovidov self-assigned this Aug 16, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 16, 2026
Merged via the queue into master with commit 5c9f28e Aug 16, 2026
181 checks passed
@alexey-milovidov
alexey-milovidov deleted the drop-database-referential-deps-order branch August 16, 2026 02:22
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Aug 16, 2026
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-ClickHouse__ClickHouse that referenced this pull request Nov 7, 2026
…ing DROP DATABASE

`DROP DATABASE` and `DETACH DATABASE` build a local dependency graph from the
union of each table's loading and referential dependencies (ClickHouse#114952). The two
global graphs can disagree on the UUID of the same name: the referential node
of a view gets its UUID from `ALTER`, and it survives `DROP TABLE` + `CREATE TABLE`
of that name while a dictionary still depends on it. A dictionary reading that
name then has the dependencies `[db.x, db.x (stale uuid)]`. In the local graph
the first entry resolves to the node of the real table, and the second one
takes the name-conflict branch of `addOrUpdateNode`, which removes that node.
The vector overload of `addDependencies` had already stored its raw pointer,
and then inserted into the freed node's `dependents`.

Keep every node collected by `addDependencies` alive until the end of the
call, replace `table_node` by the node that took its name (the rule the
single-dependency overload already uses since ClickHouse#126292), and drop the removed
nodes from the new dependency set: each of them gave its name to the node of a
later dependency, which is in the set. When no node is removed, the resulting
graph is the same as before.

Found by La Casa Del Dolor (arm_asan_ubsan), STID 4010-5179, on PR ClickHouse#129186.

Co-Authored-By: Claude Opus 5.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.

2 participants