Skip to content

Cherry pick #114953 to 26.5: Fix an unkillable hang when dropping a low-sorting-name TimeSeries table - #115857

Merged
PedroTadim merged 4 commits into
backport/26.5/114953from
cherrypick/26.5/114953
Aug 24, 2026
Merged

PedroTadim merged 4 commits into
backport/26.5/114953from
cherrypick/26.5/114953

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #114953

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 3 commits August 15, 2026 08:37
DROP TABLE of a TimeSeries table hung forever, and could not be cancelled
with KILL QUERY, whenever the table's own qualified name sorted below its
inner tables' names:

    SET allow_experimental_time_series_table = 1;
    CREATE TABLE `-ts` ENGINE = TimeSeries;
    DROP TABLE `-ts`;

No unusual name is needed to reach it. A materialized view's inner name is
.inner_id.<uuid>, which always sorts below .inner_id.metrics.<uuid>, so
dropping a view declared with ENGINE = TimeSeries hung as well.

InterpreterDropQuery::executeDropQuery did not honour its own
need_ddl_guard contract. It takes the DDL guard for the inner table, then
synthesizes an ASTDropQuery for that same table without setting
no_ddl_lock, so the nested InterpreterDropQuery takes the guard for the
identical name a second time. DDLGuard::table_lock is a unique_lock over a
non-recursive std::mutex, so the thread deadlocks against itself. A mutex
wait is not a cancellation point, which is why KILL QUERY could not land
and the guard for that name stayed held for the server's lifetime.

need_ddl_guard = true reaches that code from a single call site,
StorageTimeSeries::dropInnerTableIfAny, which requests the inner guard only
when the parent name sorts first. That predicate is correct and is kept,
since DDL guards must be taken in increasing name order and the outer DROP
already holds the parent's guard. Only its comment was wrong: it claimed
the inner name is almost always less than the parent's, which holds for
MaterializedView but not for TimeSeries, where it is almost always false.

Set no_ddl_lock on the synthesized AST so the inner name is guarded exactly
once, by the caller, held across the nested execute. InterpreterCreateQuery
already sets the same field for the same reason. The other ten call sites
take the need_ddl_guard = false default, so the change is inert for them.

The null check on ddl_guard in executeToTableImpl is defensive hardening
rather than part of the fix. That shouldReplicateQuery branch is not
reachable from the TimeSeries paths, because executeDropQuery sets
is_replicated_database_internal only when a metadata transaction is
present, and it is already reachable today from InterpreterCreateQuery. It
is guarded because this change routes a second caller through the line.

Validated against a pristine master build: both shapes above time out
before the change and succeed after it, while a high-sorting-name control
passes on both builds. Across a seven-name matrix, every name sorting below
its inner tables hangs before and passes after, and every name sorting
above passes on both. Eight concurrent drops of one table serialize and
complete, and the name is re-creatable afterwards, so no lock is stranded.
…asttest

The stress runner passes ignore_drop_queries_probability=0.2. A TimeSeries
table does not override storesDataOnDisk, so InterpreterDropQuery rewrites
such a DROP into a TRUNCATE, and TRUNCATE never calls dropInnerTableIfAny.
The test then observes nothing. Forcing the setting to 1 leaves 13 inner
tables behind without the pin and 0 with it.

The no-fasttest tag claimed the TimeSeries engine is absent from the
fast-test image. registerStorageTimeSeries is called unconditionally in
registerStorages.cpp, and the untagged 03222_create_timeseries_table has
run in Fast test 3898 times over the last 7 days, so the claim was wrong
and the tag needlessly removed the test from the fastest job.

No no-replicated-database tag: the test was measured 10 times in that mode
and passes, while the same runs against the pre-fix binary hang in
DatabaseReplicated::dropTable, so that config covers a real carrier.

The comment above may_lock_ddl_guard now states the lock-ordering rule
without asserting that a parent guard is always held, which is false on the
background dropTableFinally path.

Co-Authored-By: Claude Fable 5 <[email protected]>
…d-self-deadlock

Fix an unkillable hang when dropping a low-sorting-name TimeSeries table
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Aug 21, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

The only conflict is in src/Storages/StorageTimeSeries.cpp, and it is comment-only in
substance. src/Interpreters/InterpreterDropQuery.cpp, where the fix actually lives,
auto-merges here with no conflict.

This branch predates master's tryGetTargetTableID refactor and still uses
for (const auto & target : targets), so the surrounding lines differ, but the functional
line is already present and unchanged by the fix:

bool may_lock_ddl_guard = getStorageID().getQualifiedName() < target.table_id.getQualifiedName();

Resolution: keep this branch's loop as it is, and take master's replacement comment at
line 214, i.e. replace

/// Best-effort to make them work: the inner table name is almost always less than the TimeSeries name (so it's safe to lock DDLGuard).
/// (See the comment in StorageMaterializedView::dropInnerTableIfAny.)

with

/// DDLGuards must be locked in order of increasing table name, so the inner guard
/// may be requested only when this table's name sorts first.

Preprocessing both revisions (gcc -fpreprocessed -dD -E -P) and diffing gives an identical
result, so the resolution carries no functional change.

The fix still bites here: the resolved InterpreterDropQuery.cpp carries
no_ddl_lock = need_ddl_guard and the if (ddl_guard) null check, and their consumers already
exist on this branch (ASTDropQuery.h declares no_ddl_lock, and the read site
(!query.no_ddl_lock ? getDDLGuard(...) : nullptr) is present). The test pair
04920_timeseries_drop_low_sorting_name.{sql,reference} comes along, 04920 does not collide
here, and ignore_drop_queries_probability (which the test sets) exists in src/Core/Settings.cpp.

This branch is affected, measured rather than inferred from the label expansion. Official
builds.clickhouse.com/26.5/amd64/clickhouse binary (26.5.8.13), 90s timeout:

Treatment (a TimeSeries whose name sorts below its inner tables) hangs and is killed at 90s:

SET allow_experimental_time_series_table = 1;
CREATE TABLE `-ts` ENGINE = TimeSeries;
DROP TABLE `-ts`;

Control (a name sorting above them) returns normally:

SET allow_experimental_time_series_table = 1;
CREATE TABLE prom ENGINE = TimeSeries;
DROP TABLE prom;

The control returns on this binary, so the hang is the defect and not a broken fixture. On a
master binary containing the fix (26.8.1.1874, GIT_HASH 400ba9d, which has
941dd98 as an ancestor) both arms return.

I cannot push the resolution myself: I do not have push access to this repository, and this
branch is in ClickHouse/ClickHouse rather than a fork. Leaving the disposition to
@ PedroTadim / @ evillique. Note cherry_pick.py auto-closes a cherry-pick PR after 7 days
without an update, so this one lapses around 2026-08-28 if untouched.

Resolve the conflict in src/Storages/StorageTimeSeries.cpp: master reworked the
loop to use tryGetTargetTableID, which does not exist on 26.5, so the branch's own
target.table_id form is kept and only the reworded comment is taken.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants