Skip to content

Cherry pick #113397 to 26.3: Fix a table permanently stuck in the drop queue after a failed materialized-CTE query - #132478

Merged
robot-ch-test-poll merged 5 commits into
backport/26.3/113397from
cherrypick/26.3/113397
Nov 21, 2026
Merged

robot-ch-test-poll merged 5 commits into
backport/26.3/113397from
cherrypick/26.3/113397

Conversation

@robot-clickhouse

Copy link
Copy Markdown
Member

Original pull-request #113397

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.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

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

tiandiwonder and others added 4 commits August 4, 2026 08:33
`Planner::addPlansForMaterializedCTEs` appends a `MaterializingCTEStep`
into the CTE's own plan and then stores that plan into
`materialized_cte->plan`, while the step holds a strong
`MaterializedCTEPtr`. That is a reference cycle with no external holder,
and the only thing that broke it was
`DelayedMaterializingCTEsStep::makePlansForCTEs` moving the plan out -
reached from `resolveMaterializingCTEs` at the very end of
`QueryPlan::optimize`, after both optimization passes and after
`addStepsToBuildSets`.

So a materialized-CTE query that threw inside `optimize` before that
point leaked the whole graph for the life of the process, including the
`StoragePtr` copies and table locks the plan carries from
`extendQueryContextAndStoragesLifetime`. Both `DatabaseCatalog` and
`SharedDatabaseCatalog` gate the final drop on
`isSharedPtrUnique(storage)`, so the leaked reference pinned the table
in the drop queue forever: `DROP TABLE ... SYNC` never returned, and
SharedCatalog re-emitted `stuck in the local drop or detach queue` every
60 s.

Move the strong reference out of the cycle, the way
`StorageMemory::materialized_cte` already does: `MaterializingCTEStep`
holds `MaterializedCTEWeakPtr`, and `MaterializingCTEsStep` - which
replaces `DelayedMaterializingCTEsStep` in
`addPlansForMaterializingCTEs` and becomes the parent of every claimed
CTE plan - takes over the strong handles. Ownership is then continuous
across planning, claim and pipeline build without ever forming a cycle.

Observed in `Stress test (amd_tsan, SharedCatalog, meta in keeper)` on
ClickHouse/clickhouse-private#67155 (test
`04041_materialized_cte_parallel_replicas.sql`); pre-existing, not
introduced there.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
… the weak handle directly

Two review comments from @novikd on #113397:

1. `MaterializingCTEsStep` should own exactly the CTEs whose plans hang below it.
   `DelayedMaterializingCTEsStep::detachCTEs` handed over every CTE the delayed step
   referenced, including ones a recursive `buildSetInplace` had already claimed and
   materialized inplace - those are owned by whichever step attached their plan, and
   holding them here only kept them alive longer than needed. `makePlansForCTEs` now
   returns a `ClaimedCTE{cte, plan}` per claim, so the strong handle travels with the
   plan it belongs to. This also removes the call on the moved-from step that
   `detachCTEs` required.

2. `MaterializingCTEStep` now takes a `MaterializedCTEWeakPtr` by value and moves it
   into the member, instead of a `const MaterializedCTEPtr &`. The signature states
   the step's weak ownership, and there is no `shared_ptr` copy at the call site.
…self-reference

Fix a table permanently stuck in the drop queue after a failed materialized-CTE query
@robot-clickhouse robot-clickhouse 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 Nov 17, 2026
@robot-ch-test-poll2

Copy link
Copy Markdown
Contributor

Dear @novikd, @tiandiwonder, @alexey-milovidov, @nikitamikhaylov, this cherry-pick PR has not been updated for 3d0h7m14s. Please resolve the conflicts to backport #113397, or close this PR if the backport is no longer needed. This PR will be automatically closed after 7 days of inactivity.

Resolve the conflicts of backporting #113397 to 26.3.

The 26.3 branch does not have the two-pass `resolveMaterializingCTEs`
(`DelayedMaterializingCTEsStep::optimizePlans`) and
`removeAllDelayedMaterializingCTEsStep`, so `makePlansForCTEs` keeps its
26.3 signature with `optimization_settings` and still optimizes the plan
of each CTE it claims. On top of that the fix is applied as in master:
`MaterializingCTEStep` refers to its `MaterializedCTE` weakly,
`makePlansForCTEs` returns `ClaimedCTE` pairs, and `MaterializingCTEsStep`
takes over the strong handles of exactly the CTEs it claimed.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@robot-ch-test-poll
robot-ch-test-poll merged commit dfb1b56 into backport/26.3/113397 Nov 21, 2026
1 check passed
@robot-ch-test-poll
robot-ch-test-poll deleted the cherrypick/26.3/113397 branch November 21, 2026 03:25
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.

7 participants