Repository navigation
Refuse RENAME TABLE and EXCHANGE TABLES while their database is being dropped - #133361
Conversation
`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]>
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).
Delta recheck at f9593a5: 0 findings. Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261121-034600 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-6:20261121-000201 |
|
Workflow [PR], commit [f9593a5] Summary: ✅
AI ReviewSummaryReviewed #133361 at Missing context / blind spots
Final VerdictStatus: ✅ Approve |
55bb738
Closes: #132379
Related: #133016
Related: #132599
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
RENAME TABLEandEXCHANGE TABLESof the tables of a database that is being dropped, detached or truncated now fail withUNFINISHEDinstead of running concurrently with it. Before, a table renamed or exchanged into another database duringDROP DATABASEstayed unable to merge, or read-only forReplicatedMergeTree, until a restart. Closes #132379.Description
On 26.10,
RENAME TABLE a.t TO b.t(orEXCHANGE TABLES a.t AND b.u) concurrent withDROP DATABASE aleaves the table that lands inbunable to merge (OPTIMIZEfails withABORTED) or read-only (TABLE_IS_READ_ONLYforReplicatedMergeTree) until a restart.DROP/DETACH/TRUNCATE DATABASElist the tables, callflushAndPrepareForShutdownon them and drop them by name, all under the exclusive side ofrestart_replica_mutex. OnlySYSTEM RESTART REPLICAtook 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 REPLICAdoes, and fails withUNFINISHED(retriable inDDLWorker). It never blocks, so a refreshable view whose exchange meets a pending drop fails instead of deadlocking. The inner-table renames thatTimeSeriesand materialized views run fromrenameInMemory(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 andEXCHANGEare refused; a rename already holding the locks finishes, nested inner renames included. It fails with either half of the fix removed.01150_ddl_guard_rwrraces renames withDETACH DATABASEand 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
26.12.1.205-master(included in26.12and later)