Skip to content

Refuse cross-database RENAME/EXCHANGE while DROP DATABASE is running - #132599

Closed
Marouane-elfadli wants to merge 13 commits into
ClickHouse:masterfrom
Marouane-elfadli:fix-drop-database-cross-db-rename-132379
Closed

Marouane-elfadli wants to merge 13 commits into
ClickHouse:masterfrom
Marouane-elfadli:fix-drop-database-cross-db-rename-132379

Conversation

@Marouane-elfadli

@Marouane-elfadli Marouane-elfadli commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #132379
Related: #123694
Related: #51851
Related: #71333

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

A RENAME TABLE or EXCHANGE TABLES of the tables of a database no longer runs concurrently with DROP DATABASE of that database. Before, a table moved to another database during the drop was still shut down by it and stayed read-only (TABLE_IS_READ_ONLY for ReplicatedMergeTree) and unable to merge (MergeTree) until a restart, and an EXCHANGE could make the drop remove the wrong table. Such a rename is now refused with UNFINISHED while the database is being dropped. Closes #132379.

Description

DROP DATABASE collects the storages of the database, calls flushAndPrepareForShutdown on them, then drops them by name. A concurrent RENAME/EXCHANGE could change the tables of the database in between:

  • RENAME TABLE a.t TO b.t moved a prepared table to another database, where it stayed read-only / without merges after the drop succeeded.
  • EXCHANGE TABLES a.t AND b.u made the drop remove the original b.u, while the prepared a.t survived as b.u.
  • RENAME TABLE a.t TO a.t2 made the drop fail with DATABASE_NOT_EMPTY and left the prepared table unusable.

DROP DATABASE already holds getLockForDropDatabase exclusively for the whole collect/prepare/drop phase. The fix makes every RENAME/EXCHANGE take a shared lock on all databases it touches (tryGetLockForRestartReplica) before taking its table guards:

  • while a drop runs, the rename fails immediately with UNFINISHED;
  • a rename already in progress finishes first, because the drop waits for the exclusive lock;
  • the lock is only tried, so there is no lock-order deadlock with the drop.

Changes:

  • InterpreterRenameQuery.cpp: the fix.
  • InterpreterDropQuery.cpp, FailPoint.cpp: failpoint drop_database_holding_drop_lock, a pause point right after prepare_tables, under the lock, used by the test. No behaviour change otherwise.
  • Test 05330_drop_database_blocks_cross_db_rename: pauses the drop after its tables are prepared, checks that cross-database RENAME/EXCHANGE and an in-database RENAME of its tables are refused with UNFINISHED while a rename inside the other database still works, then checks that the surviving table is writable and mergeable. The test fails without the fix.
  • 01150_ddl_guard_rwr: tolerates the new error code 341 (UNFINISHED), because the test races renames with DETACH DATABASE.

Refuse cross-database RENAME/EXCHANGE while DROP DATABASE runs
Add test 05330 for DROP DATABASE vs cross-database RENAME
@Marouane-elfadli

Copy link
Copy Markdown
Contributor Author

@groeneai HEY CAN YOU TAKE A LOOK ON THIS pr if it's fix the issues for all the side ! ?

@clickhouse-gh clickhouse-gh Bot added the comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). label Nov 18, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

I checked it on current master (6483b80fc) with and without this PR, in the window from the issue: DROP DATABASE a held inside the final prepare_tables (an APPEND refreshable MV in a paused at refresh_mv_pause_after_executor_published), with a.t already prepared, then one DDL:

DDL while the drop runs master this PR
RENAME TABLE a.t TO b.t drop succeeds, b.t survives, OPTIMIZE fails ABORTED UNFINISHED, drop succeeds
EXCHANGE TABLES a.t AND b.u drop succeeds but drops the original b.u; the old a.t survives as b.u, OPTIMIZE fails ABORTED UNFINISHED, drop succeeds
RENAME TABLE a.t TO a.t2 drop fails DATABASE_NOT_EMPTY, a.t2 stays, OPTIMIZE fails ABORTED same as master

So the cross-database sides are fixed, including the exchange, which is worse than the issue says (it drops the other database's table). Other paths, from the code: every user-reachable cross-database move goes through InterpreterRenameQuery::execute (tables and dictionaries, ON CLUSTER per host); Replicated databases refuse moves in or out except during replica recovery, which runs in the DDL worker that the drop stops before taking the lock; RENAME DATABASE is serialized by the (db, "") DDL guard the drop holds; DETACH/TRUNCATE DATABASE run the same locked block. The rename side only try-locks, so no lock-order deadlock. Your test and 01150_ddl_guard_rwr pass with the PR (run without the test runner).

What is left:

  1. The in-database side (last row). Taking the lock for every RENAME/EXCHANGE (removing the from != to filter) closes it: the same repro then gives UNFINISHED and a clean drop, and your test still passes. 01150_ddl_guard_rwr then prints Code: 341, because it races in-database renames with DETACH DATABASE (it already tolerates the 219 this race produces), so 341 needs adding to its filter, and the error message should not say "between databases".
  2. The test pauses before collect_tables(), where a rename cannot cause the bug (the drop then collects the post-rename state). Pausing after prepare_tables(tables_to_prepare), still under the lock, puts the test in the window of the issue.
  3. The branch is based on master from 2026-11-06 while the files carry current master content, so "Files changed" shows about 30 unrelated failpoints, a moved access check in executeToTableImpl and a query_ptr->clone() change, all already in master. A rebase would leave only your change in the diff.

Updated comments to clarify the behavior of RENAME and EXCHANGE operations during a DROP DATABASE.
Updated comments for clarity regarding the behavior of RENAME/EXCHANGE operations during a DROP DATABASE process.
Fix formatting by adding a newline at the end of the file.
@Marouane-elfadli

Copy link
Copy Markdown
Contributor Author

@groeneai so how about know if i need you to tell me is it good note me frome 1 to 20 ?/20

@groeneai

Copy link
Copy Markdown
Collaborator

I re-checked the new head (0131ffa): I put your six files on current master (520f78242b5), built it, and ran the tests with tests/clickhouse-test.

Done:

  • Every RENAME/EXCHANGE now takes the lock, so the in-database rename is refused during the drop too. The message and the 01150_ddl_guard_rwr filter are updated, and 01150 passes.
  • The failpoint now pauses after prepare_tables(tables_to_prepare), under the lock, which is the window of the issue. With the InterpreterRenameQuery.cpp change reverted, 05330 fails, so the test catches the bug.

Left:

  1. 05330 fails as committed: it now prints UNFINISHED four times (you added the RENAME TABLE ${DB_A}.t TO ${DB_A}.t2 check), but the .reference still has three. With a fourth UNFINISHED line it passes 5 out of 5 runs with randomized settings.
  2. The branch is still based on master from 2026-11-06 (7340 commits behind), so "Files changed" still shows the 30 unrelated failpoints, the moved access check and the query_ptr->clone() change. Rebasing onto current master (for example git pull --rebase https://github.com/ClickHouse/ClickHouse.git master, then git push --force-with-lease) would leave only your change.

I'd rather not give a score. With these two fixed I see nothing else blocking; the review and the merge decision are up to the maintainers. CI has not run yet: it needs the can be tested label from a maintainer, which I cannot add.

@Marouane-elfadli

Copy link
Copy Markdown
Contributor Author

@PedroTadim hi am sorry if i bother you i take time to fix this would you like if you have free time to take a look on it and thank you so much !

Comment thread src/Common/FailPoint.cpp

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Marouane-elfadli why are you creating so many failpoints?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). external Author is not a member of the ClickHouse organization

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DROP DATABASE concurrent with RENAME TABLE to another database leaves the moved table read-only / without merges

3 participants