Commit fac1b470 authored by Tomas Vondra's avatar Tomas Vondra

Disallow SRFs when considering sorts below Gather Merge

While we do allow SRFs in ORDER BY, scan/join processing should not
consider such cases - such sorts should only happen via final Sort atop
a ProjectSet. So make sure we don't try adding such sorts below Gather
Merge, just like we do for expressions that are volatile and/or not
parallel safe.

Backpatch to PostgreSQL 13, where this code was introduced as part of
the Incremental Sort patch.

Author: James Coleman
Reviewed-by: Tomas Vondra
Backpatch-through: 13
Discussion: https://postgr.es/m/CAAaqYe8cK3g5CfLC4w7bs=hC0mSksZC=H5M8LSchj5e5OxpTAg@mail.gmail.com
Discussion: https://postgr.es/m/295524.1606246314%40sss.pgh.pa.us
parent ff5d5611
...@@ -840,6 +840,13 @@ find_em_expr_usable_for_sorting_rel(PlannerInfo *root, EquivalenceClass *ec, ...@@ -840,6 +840,13 @@ find_em_expr_usable_for_sorting_rel(PlannerInfo *root, EquivalenceClass *ec,
if (require_parallel_safe && !is_parallel_safe(root, (Node *) em_expr)) if (require_parallel_safe && !is_parallel_safe(root, (Node *) em_expr))
continue; continue;
/*
* Disallow SRFs so that all of them can be evaluated at the correct
* time as determined by make_sort_input_target.
*/
if (IS_SRF_CALL((Node *) em_expr))
continue;
/* /*
* As long as the expression isn't volatile then * As long as the expression isn't volatile then
* prepare_sort_from_pathkeys is able to generate a new target entry, * prepare_sort_from_pathkeys is able to generate a new target entry,
......
...@@ -21,11 +21,6 @@ ...@@ -21,11 +21,6 @@
#include "optimizer/tlist.h" #include "optimizer/tlist.h"
/* Test if an expression node represents a SRF call. Beware multiple eval! */
#define IS_SRF_CALL(node) \
((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \
(IsA(node, OpExpr) && ((OpExpr *) (node))->opretset))
/* /*
* Data structures for split_pathtarget_at_srfs(). To preserve the identity * Data structures for split_pathtarget_at_srfs(). To preserve the identity
* of sortgroupref items even if they are textually equal(), what we track is * of sortgroupref items even if they are textually equal(), what we track is
......
...@@ -24,6 +24,11 @@ ...@@ -24,6 +24,11 @@
#include "nodes/parsenodes.h" #include "nodes/parsenodes.h"
/* Test if an expression node represents a SRF call. Beware multiple eval! */
#define IS_SRF_CALL(node) \
((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \
(IsA(node, OpExpr) && ((OpExpr *) (node))->opretset))
/* /*
* We don't want to include nodes/pathnodes.h here, because non-planner * We don't want to include nodes/pathnodes.h here, because non-planner
* code should generally treat PlannerInfo as an opaque typedef. * code should generally treat PlannerInfo as an opaque typedef.
......
...@@ -1620,3 +1620,15 @@ order by 1, 2; ...@@ -1620,3 +1620,15 @@ order by 1, 2;
-> Function Scan on generate_series -> Function Scan on generate_series
(7 rows) (7 rows)
-- Disallow pushing down sort when pathkey is an SRF.
explain (costs off) select unique1 from tenk1 order by unnest('{1,2}'::int[]);
QUERY PLAN
-------------------------------------------------------------------------
Sort
Sort Key: (unnest('{1,2}'::integer[]))
-> Gather
Workers Planned: 2
-> ProjectSet
-> Parallel Index Only Scan using tenk1_unique1 on tenk1
(6 rows)
...@@ -267,3 +267,5 @@ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub; ...@@ -267,3 +267,5 @@ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub;
explain (costs off) select sub.unique1, stringu1 || random()::text explain (costs off) select sub.unique1, stringu1 || random()::text
from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub
order by 1, 2; order by 1, 2;
-- Disallow pushing down sort when pathkey is an SRF.
explain (costs off) select unique1 from tenk1 order by unnest('{1,2}'::int[]);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment