Skip to content

Support Stateless ExecutionPlans / concurrently running plans by moving all state to TaskContext #28002

Backlog

Description

@alamb

Is your feature request related to a problem or challenge?

This is a potential alternate to this idea from @askalt but with the same usecase

Quoting the usecase

We are working on a DBMS that uses DataFusion as the SQL query engine. In general, there is a challenge: physical plan building bottlenecks. The impact can be reduced to some degree, but as the number of optimizations increases, query latency becomes uncontrollable.

It would be beneficial to reuse a single plan across many executions (like a prepared statement). The problem is not so much ideological as technical: the current implementation stores execution-stage state in the plan itself and generally does not assume that execute(...) can be called more than once or concurrently for the same Arc.

Describe the solution you'd like

An alternate solution would be to move the plan state on to the existing TaskContext structure, which is already created once per query.

The idea here would then be that DynamicFilters, materialized CTEs, pointers to shared join hash tables, etc would all live on the TaskContext rather than as fields on ExecutionPlan. Thus to run multiple queries, we would just fire up multiple task contexts

For example

    // Plan ONCE: after this the plan is immutable and can be cached
    let plan: Arc<dyn ExecutionPlan> = ctx
        .sql("SELECT t.a, u.b FROM t JOIN u ON t.k = u.k ORDER BY t.a LIMIT 10")
        .await?
        .create_physical_plan()
        .await?;

    // One TaskContext per execution. Each one owns that execution's state:
    // the join build side, dynamic filter values, repartition channels, metrics, ...
    let task_ctx1 = ctx.task_ctx();
    let task_ctx2 = ctx.task_ctx();

    // Run the SAME Arc<dyn ExecutionPlan> twice, concurrently.
    let (batches1, batches2) = tokio::try_join!(
        collect(Arc::clone(&plan), Arc::clone(&task_ctx1)),
        collect(Arc::clone(&plan), Arc::clone(&task_ctx2)),
    )?;
    assert_eq!(batches1, batches2);
}

Describe alternatives you've considered

We would have to add some way to attach generic state to TaskContext (some sort of type erased thing)

Then we would have to move the state from the built in datafusion ExecutionPlans -- for example Metrics, and join rendezvous state etc. Once we had migrated that all over we would have to help downstream users / implementers of ExecutionPlan migrate their usages over too

We would also need to thread TaskContext to expression evaluation, etc

Additional context

No response

Willingness to contribute

I am just writing this up based on a conversation with @adriangb - I don't plan to work on this myself

Activity

  1. added theissue type on Dec 3, 2026
  2. added
    enhancement
    on Dec 3, 2026
  3. alamb commented on Dec 3, 2026

    @alamb
    ContributorAuthor

    Some thoughts from https://github.com/apache/datafusion/pull/26270/changes#r4791679698

    If we went with an approach like this it seems to me like we would want some general way to associate a plan node with its "state" in some way that could be shared with other nodes

    For example, in a dynamic filter, we would need some way for both the producer (e.g. topk) and cosumer (e.g. scan) to find the same state

    (SortExec -- dynamic filter producer) 
      ...
        (Scan -- dynamic filter consumer)
    

    One way could be something like a generic
    NodeId or StateId field that was assigned at plan time (so the SortExec would have NodeId=100 say, and the scan would have NodeId=200, but also know that its dynamic filter came from NodeId=100

    The actual state I would imagine in an Arc<dyn Any + Send + Sync> type situation

  4. Kristofferjoha commented on Dec 4, 2026

    @Kristofferjoha
    Contributor

    https://duckdb.org/2026/08/25/how-duckdb-runs-recursive-ctes-faster is a blog post about reusing state in recursive CTEs. They build the hash table on edges once and reuse it across iterations. Is that relevant to how long state should live with StateId?

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