Skip to content

orca: fall back on replicated CTE only when consumed in 2+ slices#398

Draft
Alena0704 wants to merge 2 commits into
OPENGPDB_STABLEfrom
relax-shareinput-walker
Draft

orca: fall back on replicated CTE only when consumed in 2+ slices#398
Alena0704 wants to merge 2 commits into
OPENGPDB_STABLEfrom
relax-shareinput-walker

Conversation

@Alena0704

Copy link
Copy Markdown
Contributor

orca: fall back on replicated CTE only when consumed in 2+ slices

Only a replicated CTE consumed in 2+ different slices hangs, not any
cross-slice consumer. Narrow the fallback check to that case

InnerLife0 and others added 2 commits July 2, 2026 23:21
GPDB has Shared Scan node to share the output of a subplan. Each Shared Scan
node can act like producer or consumer. Each one may be a part of different
slice. One of essential parts of Shared Scan processing is cross-slice
interaction detection. It helps to find the difference in producer's and
consumer's slices and mark underlying Material or Sort nodes as cross-slice.
Cross-slice producer reads all the tuples from child plan before the consumer
Shared Scan can read it.

Before this patch, cross-slice interaction detection of underlying subplan nodes
was broken. We walked through subplans separately from main plan. This, in case
subplan has no motion, caused `shareinput_mutator_xslice_2()` to think we don't
need to mark node as cross-slice - motion stack (`motStack`) used for
cross-slice interaction detection contained only default motion id (0) which
equals to producer's slice id.

Here is the simplified part of new regression test. It shows Shared Scan
consumer under Subplan, which has different (compared to producer upper) slice
id.
```
 Sequence
   ->  Shared Scan (share slice:id 0:0)
     ->  Seq Scan on t1 t1_1
   ->  Gather Motion 3:1  (slice3; segments: 3)
     ->  Redistribute Motion 1:3  (slice1)
       ->  Function Scan on generate_series
         SubPlan 1  (slice1)
           ->  Shared Scan (share slice:id 1:0)
```
Before this patch, we didn't have upper motions in `motStack`. From now, we dive
into subplans as parts of main plan according to the plan tree hierarchy.
Cross-slice detector understands that our slice(1) is different from producer's
slice(0) and marks node as cross-slice.

Existing logic and comments around rtables left almost unchanged.
Existing tricky logic around joins execution order changed to another tricky
logic which is compatible with `plan_tree_walker()`.
`gporca_optimizer.out` test output changed as we now start
`shareinput_mutator_dag_to_tree()` from the main plan. Before, we started from
subplans and global share input context contained only 3 producers at the time
`set_plan_share_id()` called. Now, at the same place producers count is equal to
4, so RangeTblEntry's name changed.

There was a bug when duplicate subplans were added without new corresponded
subroots. The old shareinput_walker() implementation not called for all
subplans, because forboth() loop stopped early. New shareinput_walker()
implementation not found appropriate subplan's subroot (and used main root as
default). From now, we fix old mistake and create new subroot for each new
subplan.

cherry-pick from a39683f #342 (https://github.com/GreengageDB/greengage)

Conflicts resolved in src/backend/cdb/cdbmutate.c: kept the new
shareinput_walker() and dropped HEAD's old manual Append/MergeAppend/ModifyTable
recursion, which plan_tree_walker() now handles.
Only a replicated CTE consumed in 2+ different slices hangs, not any
cross-slice consumer. Narrow the fallback check to that case.
@Alena0704 Alena0704 marked this pull request as draft July 3, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants