Skip to content

[EPIC] Concurrent Use and Reuse of Physical Plans (ExecutionPlans) #28003

Backlog

Description

@alamb

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:

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

Potential designs

Additional context

No response

Willingness to contribute

None

Activity

  1. added
    enhancement
    on Dec 3, 2026
  2. added theissue type on Dec 3, 2026
  3. changed the title [-][Epic] Concurrent Use and Reuse of Physical Plans (`ExecutionPlan`s)[/-] [+][EPIC] Concurrent Use and Reuse of Physical Plans (`ExecutionPlan`s)[/+] on Dec 3, 2026
  4. nuno-faria commented on Dec 4, 2026

    @nuno-faria
    Contributor

    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)

    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_placeholders implemented by each node that would handle this.

  5. adriangb commented on Dec 4, 2026

    @adriangb
    Contributor

    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 of RecursiveQueryExec gets 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 separate TaskContexts 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 at execute) had access to the TaskContext:

    Materialized CTE (#26270) Dynamic filter
    Id assigned at plan time CTE id Filter id
    Per-run state Buffered output Current bounds
    Writer / reader MaterializedCteExec / MaterializedCteScan Producer (hash join, TopK, aggregate) / scan
    Recursive iteration New state for CTEs in the recursive term New state for producers in the recursive term
    Concurrent copies Separate TaskContext, separate buffer Separate TaskContext, separate filter

    The 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.

  6. adriangb commented on Dec 5, 2026

    @adriangb
    Contributor

    Example of "new state for producers in the recursive term" from my comment above. More precisely: RecursiveQueryExec renews 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.

    WITH RECURSIVE reach AS (
      SELECT 1 AS node
      UNION
      SELECT e.dst FROM reach r JOIN edges e ON e.src = r.node
    )
    SELECT * FROM reach
    Plan A: build side = reach                Plan B: build side = edges
    RecursiveQueryExec                        RecursiveQueryExec
    └─ HashJoinExec  writes h1, f1            └─ HashJoinExec  writes h2
       ├─ WorkTableExec (reach)  build           ├─ DataSourceExec (edges)  build
       └─ DataSourceExec (edges) reads f1        └─ WorkTableExec (reach)   probe
    
    TaskContext (query)                       TaskContext (query)
     ├─ iteration 1: h1, f1 = src IN (1)       ├─ h2  <- built once, every iteration reads it
     ├─ iteration 2: h1, f1 = src IN (2, 5)    ├─ iteration 1: (nothing renewed)
     └─ ...  new slots, dropped after          └─ ...
    
    State Reads work table? Per iteration
    h1, f1 (Plan A) Yes New slot. No stale bounds, so no #26054 and no in-place reset_state.
    h2 (Plan B) No Reused. This is the DuckDB optimization from this comment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions