Skip to content

Commit fac1b47

Browse files
committed
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
1 parent ff5d561 commit fac1b47

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

src/backend/optimizer/path/equivclass.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,13 @@ find_em_expr_usable_for_sorting_rel(PlannerInfo *root, EquivalenceClass *ec,
840840
if (require_parallel_safe && !is_parallel_safe(root, (Node *) em_expr))
841841
continue;
842842

843+
/*
844+
* Disallow SRFs so that all of them can be evaluated at the correct
845+
* time as determined by make_sort_input_target.
846+
*/
847+
if (IS_SRF_CALL((Node *) em_expr))
848+
continue;
849+
843850
/*
844851
* As long as the expression isn't volatile then
845852
* prepare_sort_from_pathkeys is able to generate a new target entry,

src/backend/optimizer/util/tlist.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
#include "optimizer/tlist.h"
2222

2323

24-
/* Test if an expression node represents a SRF call. Beware multiple eval! */
25-
#define IS_SRF_CALL(node) \
26-
((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \
27-
(IsA(node, OpExpr) && ((OpExpr *) (node))->opretset))
28-
2924
/*
3025
* Data structures for split_pathtarget_at_srfs(). To preserve the identity
3126
* of sortgroupref items even if they are textually equal(), what we track is

src/include/optimizer/optimizer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424

2525
#include "nodes/parsenodes.h"
2626

27+
/* Test if an expression node represents a SRF call. Beware multiple eval! */
28+
#define IS_SRF_CALL(node) \
29+
((IsA(node, FuncExpr) && ((FuncExpr *) (node))->funcretset) || \
30+
(IsA(node, OpExpr) && ((OpExpr *) (node))->opretset))
31+
2732
/*
2833
* We don't want to include nodes/pathnodes.h here, because non-planner
2934
* code should generally treat PlannerInfo as an opaque typedef.

src/test/regress/expected/incremental_sort.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,3 +1620,15 @@ order by 1, 2;
16201620
-> Function Scan on generate_series
16211621
(7 rows)
16221622

1623+
-- Disallow pushing down sort when pathkey is an SRF.
1624+
explain (costs off) select unique1 from tenk1 order by unnest('{1,2}'::int[]);
1625+
QUERY PLAN
1626+
-------------------------------------------------------------------------
1627+
Sort
1628+
Sort Key: (unnest('{1,2}'::integer[]))
1629+
-> Gather
1630+
Workers Planned: 2
1631+
-> ProjectSet
1632+
-> Parallel Index Only Scan using tenk1_unique1 on tenk1
1633+
(6 rows)
1634+

src/test/regress/sql/incremental_sort.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,5 @@ from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub;
267267
explain (costs off) select sub.unique1, stringu1 || random()::text
268268
from tenk1, lateral (select tenk1.unique1 from generate_series(1, 1000)) as sub
269269
order by 1, 2;
270+
-- Disallow pushing down sort when pathkey is an SRF.
271+
explain (costs off) select unique1 from tenk1 order by unnest('{1,2}'::int[]);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy