Skip to content

Refuse RENAME TABLE and EXCHANGE TABLES while their database is being dropped - #133361

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/drop-database-refuses-concurrent-rename
Nov 22, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:groeneai/drop-database-refuses-concurrent-rename

Conversation

@groeneai

@groeneai groeneai commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #132379
Related: #133016
Related: #132599

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):

RENAME TABLE and EXCHANGE TABLES of the tables of a database that is being dropped, detached or truncated now fail with UNFINISHED instead of running concurrently with it. Before, a table renamed or exchanged into another database during DROP DATABASE stayed unable to merge, or read-only for ReplicatedMergeTree, until a restart. Closes #132379.

Description

On 26.10, RENAME TABLE a.t TO b.t (or EXCHANGE TABLES a.t AND b.u) concurrent with DROP DATABASE a leaves the table that lands in b unable to merge (OPTIMIZE fails with ABORTED) or read-only (TABLE_IS_READ_ONLY for ReplicatedMergeTree) until a restart.

DROP/DETACH/TRUNCATE DATABASE list the tables, call flushAndPrepareForShutdown on them and drop them by name, all under the exclusive side of restart_replica_mutex. Only SYSTEM RESTART REPLICA took its shared side, so a rename could move a prepared table out, swap it, or move a table in (DATABASE_NOT_EMPTY).

Every table rename and exchange now try-locks the shared side for each database it touches, after its table guards, as RESTART REPLICA does, and fails with UNFINISHED (retriable in DDLWorker). It never blocks, so a refreshable view whose exchange meets a pending drop fails instead of deadlocking. The inner-table renames that TimeSeries and materialized views run from renameInMemory (non-Atomic databases) skip the locks their outer rename already holds. A Replicated initiator releases them before enqueueing, like its table locks. This builds on the approach of #133016.

One failpoint, rename_table_pause_after_locks, pauses a rename that holds its locks, so the new test covers both orderings: renames out of, into and within a database being dropped and EXCHANGE are refused; a rename already holding the locks finishes, nested inner renames included. It fails with either half of the fix removed. 01150_ddl_guard_rwr races renames with DETACH DATABASE and now accepts code 341.

Left as is: after the drop releases this lock, a rename into the database can still end it with DATABASE_NOT_EMPTY; that table was never prepared and stays usable.


Workflow [PR]
Sync PR [sync-upstream/pr/133361]

Version info

  • Merged into: 26.12.1.205-master (included in 26.12 and later)

groeneai and others added 2 commits November 21, 2026 02:55
`DROP`, `DETACH` and `TRUNCATE DATABASE` list the tables of the database,
call `flushAndPrepareForShutdown` on them (merges are cancelled for good,
replication partially stops) and then drop them by name, one by one. They
hold the exclusive side of `restart_replica_mutex` for that whole window,
but only `SYSTEM RESTART REPLICA` took its shared side. A concurrent
`RENAME TABLE a.t TO b.t` could therefore move an already prepared table
out of the database: the drop skipped the missing name and succeeded,
leaving `b.t` unable to merge (`OPTIMIZE` fails with `ABORTED`), or
read-only for `ReplicatedMergeTree`, until a restart. `EXCHANGE TABLES`
made the drop remove the other database's table, and a rename within or
into the database ended the drop with `DATABASE_NOT_EMPTY`.

Every table rename and exchange now try-locks the shared side for each
database it touches, after its table DDL guards, the same way
`RESTART REPLICA` does, and fails with `UNFINISHED` while one of them is
being dropped, detached or truncated. It never blocks, so no new wait edge
appears: a refreshable view whose exchange runs while `DROP DATABASE` waits
for its refresh to stop gets `UNFINISHED` instead of deadlocking.
`UNFINISHED` is retriable in `DDLWorker`.

The inner-table renames that `StorageTimeSeries` and
`StorageMaterializedView` run from `renameInMemory` (non-Atomic databases)
are executed inside the rename of their table, which already holds the
locks of both databases, so they skip the locks: a pending drop would
refuse them halfway and leave the table half renamed. The initiator of a
rename in a Replicated database releases its locks before enqueueing the
query, like its table locks, because the local execution of the entry
takes them again on the same thread.

The failpoint `rename_table_pause_after_locks` pauses a rename that holds
its locks, which makes both orderings deterministic in one test.
`01150_ddl_guard_rwr` races renames with `DETACH DATABASE` and now accepts
code 341.

Closes ClickHouse#132379

Co-authored-by: Marouane-elfadli <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
`pause_rename_and_start_drop` probed for `UNFINISHED` and left its loop
silently when it never saw one. Scenario 2 then could pass without
proving anything: if `DROP DATABASE` was not yet waiting, the nested
inner-table renames met no pending writer and succeeded even without
`setSkipDatabaseLocks(true)`. The helper now returns only once the drop
is waiting and prints a line that is not in the reference otherwise.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding over 2 passes: a full pass, then a delta recheck after the fix round).

# Sev Finding Verdict Evidence / action
1 ⚠️ 05839: pause_rename_and_start_drop left its probe loop silently when it never saw UNFINISHED, so scenario 2 (nested inner-table renames) could pass without DROP DATABASE actually waiting AGREE, fixed @ f9593a5 The helper now returns only after the probe sees UNFINISHED and otherwise prints a line absent from the reference. Fix 20/20 and 50/50 with that line never printed; without the lock block it is printed in both scenarios; without setSkipDatabaseLocks(true) scenario 2 fails 5/5 with the drop confirmed waiting.

Delta recheck at f9593a5: 0 findings.

Severity: ❌ blocker / ⚠️ major / 💡 nit. DISAGREE verdicts carry recorded evidence and are
terminal per finding. Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-review-slot-9:20261121-034600

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The failpoint rename_table_pause_after_locks plus DROP DATABASE ... SYNC & gives both orderings on demand. With the lock block removed, the new test fails 4/4; with the nested-rename skips removed, it fails 20/20 on a fresh server.
b Root cause explained? DROP/DETACH/TRUNCATE DATABASE prepare the listed tables and drop them by name under the exclusive restart_replica_mutex, whose shared side only RESTART REPLICA took. A rename could move a prepared table out, swap it, or move one in.
c Fix matches root cause? Yes. Every rename/exchange now try-locks that same mutex (shared) for each database it touches, as RESTART REPLICA does, and fails with UNFINISHED.
d Test intent preserved / new tests added? New 05839 covers rename out/into/within, EXCHANGE, an unaffected database, and nested TimeSeries/MV inner renames under a pending drop. 01150 only accepts the new code 341 (255 such refusals in 50 runs on the fixed build).
e Both directions demonstrated? Yes. The fixed build passes 50/50 with random settings. Each mutant fails. On the mutant without the lock, a renamed-out table's OPTIMIZE FINAL returns ABORTED; on the fix, the rename gets UNFINISHED.
f Fix is general across code paths? All InterpreterRenameQuery callers checked: user RENAME/EXCHANGE, CREATE OR REPLACE publish, refresh exchange (no hang under a drop, measured), system-log rotation, nested inner renames (skip), Replicated initiator (releases before enqueue; runtime sanity check passed).
g Fix generalizes across inputs (params/datatypes/wrappers)? Atomic and Ordinary databases, MergeTree, TimeSeries, MV with an inner table, multi-element renames, IF EXISTS, ReplicatedMergeTree in a Replicated database. Type wrappers do not apply.
h Backward compatible? (maintainer-approved exception only) No setting or format change. A rename touching a database being dropped now fails at once with retriable UNFINISHED; this is stated in the changelog entry.
i Invariants and contracts preserved? No rename changes the tables of a database while its drop holds the lock. The locks are try-only (no new wait edge), taken in set order and released by RAII on every path. Stateless sweep of 301 related tests: 0 regressions.

Session id: cron:clickhouse-impl-slot-6:20261121-000201

@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f9593a5]

Summary: ✅


AI Review

Summary

Reviewed #133361 at f9593a5117b9fa0bba6cc14a9a63aa24852c0a53. The change prevents table renames and exchanges from moving tables during concurrent database shutdown preparation, while allowing nested inner-table renames to finish under their outer operation's locks. No actionable findings.

Missing context / blind spots
  • ⚠️ Runtime tests were not rerun locally because this checkout has no build. The author records repeated fixed-build and mutation-test runs, but the CI finish report reports dropped bugfix-validation jobs and a failed new_tests_check; those CI validation results remain unavailable.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors labels Nov 21, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). label Nov 21, 2026
@alexey-milovidov alexey-milovidov self-assigned this Nov 22, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 22, 2026
Merged via the queue into ClickHouse:master with commit 55bb738 Nov 22, 2026
388 of 390 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-ddl DDL command coordination and execution (ON CLUSTER, DDL queue). pr-bugfix Pull request with bugfix, not backported by default 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.

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

3 participants