Repository navigation
Cherry pick #113397 to 26.3: Fix a table permanently stuck in the drop queue after a failed materialized-CTE query - #132478
Merged
Conversation
`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.
…e-plan-self-reference
…self-reference Fix a table permanently stuck in the drop queue after a failed materialized-CTE query
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-picklocally.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:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job