diff options
author | David Rowley | 2024-04-01 23:15:45 +0000 |
---|---|---|
committer | David Rowley | 2024-04-01 23:15:45 +0000 |
commit | d5d2205c8ddc6670fa87474e172fdfab162b7a73 (patch) | |
tree | 6445b3d946f634d0a683650619f0b7cb7694859f /src/backend/optimizer/prep/prepunion.c | |
parent | 3622c8084643cc05bc8f28d0b238615c845eae14 (diff) |
Fix assert failure when planning setop subqueries with CTEs
66c0185a3 adjusted the UNION planner to request that union child queries
produce Paths correctly ordered to implement the UNION by way of
MergeAppend followed by Unique. The code there made a bad assumption
that if the root->parent_root->parse had setOperations set that the
query must be the child subquery of a set operation. That's not true
when it comes to planning a non-inlined CTE which is parented by a set
operation. This causes issues as the CTE's targetlist has no
requirement to match up to the SetOperationStmt's groupClauses
Fix this by adding a new parameter to both subquery_planner() and
grouping_planner() to explicitly pass the SetOperationStmt only when
planning set operation child subqueries.
Thank you to Tom Lane for helping to rationalize the decision on the
best function signature for subquery_planner().
Reported-by: Alexander Lakhin
Discussion: https://postgr.es/m/[email protected]
Diffstat (limited to 'src/backend/optimizer/prep/prepunion.c')
-rw-r--r-- | src/backend/optimizer/prep/prepunion.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/backend/optimizer/prep/prepunion.c b/src/backend/optimizer/prep/prepunion.c index 944afc7192b..afcb5c0f0f0 100644 --- a/src/backend/optimizer/prep/prepunion.c +++ b/src/backend/optimizer/prep/prepunion.c @@ -244,6 +244,7 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, { RangeTblRef *rtr = (RangeTblRef *) setOp; RangeTblEntry *rte = root->simple_rte_array[rtr->rtindex]; + SetOperationStmt *setops; Query *subquery = rte->subquery; PlannerInfo *subroot; List *tlist; @@ -257,11 +258,16 @@ recurse_set_operations(Node *setOp, PlannerInfo *root, /* plan_params should not be in use in current query level */ Assert(root->plan_params == NIL); + /* + * Pass the set operation details to the subquery_planner to have it + * consider generating Paths correctly ordered for the set operation. + */ + setops = castNode(SetOperationStmt, root->parse->setOperations); + /* Generate a subroot and Paths for the subquery */ - subroot = rel->subroot = subquery_planner(root->glob, subquery, - root, - false, - root->tuple_fraction); + subroot = rel->subroot = subquery_planner(root->glob, subquery, root, + false, root->tuple_fraction, + setops); /* * It should not be possible for the primitive query to contain any |