Repository navigation
Description
Is your feature request related to a problem or challenge?
The cost of creating Physical Plans (ExecutionPlans) is non trivial (can take ms or more depending on the query complexity, optimizations, and other ideas).
Currently ExecutionPlans have state -- for example the contents of dynamic filters, subquery results, materialized CTEs, etc. so they can not be run concurrently as the concurrent executions can interfere with each other.
thanks to @askalt it is possible to reuse an existing plan; Once a plan has completed executing, you can reset the internal state by calling ExecutionPlan::reset_state so
However, you still can not reliable run the same plan concurrently and there is no easy way to make a "deep copy" or something similar to avoid the cost of re-creating the same plan plan
Some potential usecases where reusing the same physical plan would be valuable:
- Prepared statements: Support reusing the physical plan in prepared statements #26706 from @nuno-faria
- one plan running at the same time, inside a single task as described by @AhmedSoliman in Support dynamic filters and work-table plans state reset / allow CTEs to work with reset_state #19905 (comment)
It is not clear to me if prepared statements would actually run the exact same query plan, or if they would want to make a copy of at least some of the plan to fill in the bound parameter values and try and reoptimize (to do file pruning, for example)
Related work
- Design discussion: [DISCUSS] Representing Shared State /
ExecutionPlan::reset_state#21650 (started by @Dandandan 's disucssion) - Support dynamic filters and work-table plans state reset / allow CTEs to work with reset_state #19905
- Prepared physical plan reusage #14342
Potential designs
- Here is a proposal from @askalt Stateless execution plans for plan caching #19351
- Here is another that came up when I was talking with @adriangb today Support Stateless
ExecutionPlans / concurrently running plans by moving all state to TaskContext #28002 map_expressionsfrom @LiaCastaneda Implement ExecutionPlan::map_expressions #20899- fix: keep the output of a materialized CTE in
TaskContext(per run) not the plan #26270
Additional context
No response
Willingness to contribute
None
Activity
[-][Epic] Concurrent Use and Reuse of Physical Plans (`ExecutionPlan`s)[/-][+][EPIC] Concurrent Use and Reuse of Physical Plans (`ExecutionPlan`s)[/+]nuno-faria commentedon Dec 4, 2026
I think it would be easier to run a copy, since e.g. the DataSourceExec is going to be different depending on the predicates. However, in my experience most nodes are reusable as is (with_new_children) or with reset_state(). So ideally there would be a
ExecutionPlan::replace_placeholdersimplemented by each node that would handle this.adriangb commentedon Dec 4, 2026
One data point from #26270: it moves materialized CTE output into per-run state on
TaskContext, keyed by the CTE id, and it works for recursive queries without an in-place reset. Each iteration ofRecursiveQueryExecgets a child state, so CTEs in the recursive term are computed again and CTEs outside it are not. Copies of the plan that run with separateTaskContexts also stop sharing the CTE buffer. So one mechanism covers both sequential and concurrent reuse.The same shape could work for dynamic filters, if
PhysicalExpr::evaluate(or a bind step atexecute) had access to theTaskContext:MaterializedCteExec/MaterializedCteScanTaskContext, separate bufferTaskContext, separate filterThe expression in the plan would hold only the id, which also helps serialization. Recursion would then need no in-place reset, which is the constraint behind the reset discussion in #19905. TopK pruning inside recursion (#26642) would follow from the scoping rule.
adriangb commentedon Dec 5, 2026
Example of "new state for producers in the recursive term" from my comment above. More precisely:
RecursiveQueryExecrenews a state each iteration only if its producer reads the work table (or a renewed CTE) or is volatile. All other state lives in the parent context and is built once.h1,f1(Plan A)reset_state.h2(Plan B)