diff options
author | David Rowley <drowley@postgresql.org> | 2024-05-05 12:54:46 +1200 |
---|---|---|
committer | David Rowley <drowley@postgresql.org> | 2024-05-05 12:54:46 +1200 |
commit | 7d2c7f08d9c5ad02101c0fb47cd8e859dd89083b (patch) | |
tree | a84db7d521410918fab94680bff9bdce1ae7643d /src/backend/nodes/nodeFuncs.c | |
parent | 01df14763431df1506cbae206945cd165c66d1d3 (diff) | |
download | postgresql-7d2c7f08d9c5ad02101c0fb47cd8e859dd89083b.tar.gz postgresql-7d2c7f08d9c5ad02101c0fb47cd8e859dd89083b.zip |
Fix query pullup issue with WindowClause runCondition
94985c210 added code to detect when WindowFuncs were monotonic and
allowed additional quals to be "pushed down" into the subquery to be
used as WindowClause runConditions in order to short-circuit execution
in nodeWindowAgg.c.
The Node representation of runConditions wasn't well selected and
because we do qual pushdown before planning the subquery, the planning
of the subquery could perform subquery pull-up of nested subqueries.
For WindowFuncs with args, the arguments could be changed after pushing
the qual down to the subquery.
This was made more difficult by the fact that the code duplicated the
WindowFunc inside an OpExpr to include in the WindowClauses runCondition
field. This could result in duplication of subqueries and a pull-up of
such a subquery could result in another initplan parameter being issued
for the 2nd version of the subplan. This could result in errors such as:
ERROR: WindowFunc not found in subplan target lists
To fix this, we change the node representation of these run conditions
and instead of storing an OpExpr containing the WindowFunc in a list
inside WindowClause, we now store a new node type named
WindowFuncRunCondition within a new field in the WindowFunc. These get
transformed into OpExprs later in planning once subquery pull-up has been
performed.
This problem did exist in v15 and v16, but that was fixed by 9d36b883b
and e5d20bbd.
Cat version bump due to new node type and modifying WindowFunc struct.
Bug: #18305
Reported-by: Zuming Jiang
Discussion: https://postgr.es/m/18305-33c49b4c830b37b3%40postgresql.org
Diffstat (limited to 'src/backend/nodes/nodeFuncs.c')
-rw-r--r-- | src/backend/nodes/nodeFuncs.c | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index e1df1894b69..89ee4b61f2f 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -2163,6 +2163,16 @@ expression_tree_walker_impl(Node *node, return true; if (WALK(expr->aggfilter)) return true; + if (WALK(expr->runCondition)) + return true; + } + break; + case T_WindowFuncRunCondition: + { + WindowFuncRunCondition *expr = (WindowFuncRunCondition *) node; + + if (WALK(expr->arg)) + return true; } break; case T_SubscriptingRef: @@ -2400,8 +2410,6 @@ expression_tree_walker_impl(Node *node, return true; if (WALK(wc->endOffset)) return true; - if (WALK(wc->runCondition)) - return true; } break; case T_CTECycleClause: @@ -2752,8 +2760,6 @@ query_tree_walker_impl(Query *query, return true; if (WALK(wc->endOffset)) return true; - if (WALK(wc->runCondition)) - return true; } } @@ -3053,6 +3059,16 @@ expression_tree_mutator_impl(Node *node, return (Node *) newnode; } break; + case T_WindowFuncRunCondition: + { + WindowFuncRunCondition *wfuncrc = (WindowFuncRunCondition *) node; + WindowFuncRunCondition *newnode; + + FLATCOPY(newnode, wfuncrc, WindowFuncRunCondition); + MUTATE(newnode->arg, wfuncrc->arg, Expr *); + return (Node *) newnode; + } + break; case T_SubscriptingRef: { SubscriptingRef *sbsref = (SubscriptingRef *) node; @@ -3466,7 +3482,6 @@ expression_tree_mutator_impl(Node *node, MUTATE(newnode->orderClause, wc->orderClause, List *); MUTATE(newnode->startOffset, wc->startOffset, Node *); MUTATE(newnode->endOffset, wc->endOffset, Node *); - MUTATE(newnode->runCondition, wc->runCondition, List *); return (Node *) newnode; } break; @@ -3799,7 +3814,6 @@ query_tree_mutator_impl(Query *query, FLATCOPY(newnode, wc, WindowClause); MUTATE(newnode->startOffset, wc->startOffset, Node *); MUTATE(newnode->endOffset, wc->endOffset, Node *); - MUTATE(newnode->runCondition, wc->runCondition, List *); resultlist = lappend(resultlist, (Node *) newnode); } |