Skip to content

Commit c12f02f

Browse files
committed
Don't apply sortgroupref labels to a tlist that might not match.
If we need to use a gating Result node for pseudoconstant quals, create_scan_plan() intentionally suppresses use_physical_tlist's checks on whether there are matches for sortgroupref labels, on the grounds that we don't need matches because we can label the Result's projection output properly. However, it then called apply_pathtarget_labeling_to_tlist anyway. This oversight was harmless when written, but in commit aeb9ae6 I made that function throw an error if there was no match. Thus, the combination of a table scan, pseudoconstant quals, and a non-simple-Var sortgroupref column threw the dreaded "ORDER/GROUP BY expression not found in targetlist" error. To fix, just skip applying the labeling in this case. Per report from Rushabh Lathia. Report: <CAGPqQf2iLB8t6t-XrL-zR233DFTXxEsfVZ4WSqaYfLupEoDxXA@mail.gmail.com>
1 parent 957616d commit c12f02f

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

src/backend/optimizer/plan/createplan.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -544,8 +544,13 @@ create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
544544
{
545545
/* For index-only scan, the preferred tlist is the index's */
546546
tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
547-
/* Transfer any sortgroupref data to the replacement tlist */
548-
apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
547+
548+
/*
549+
* Transfer any sortgroupref data to the replacement tlist, unless
550+
* we don't care because the gating Result will handle it.
551+
*/
552+
if (!gating_clauses)
553+
apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
549554
}
550555
else
551556
{
@@ -557,8 +562,9 @@ create_scan_plan(PlannerInfo *root, Path *best_path, int flags)
557562
}
558563
else
559564
{
560-
/* Transfer any sortgroupref data to the replacement tlist */
561-
apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
565+
/* As above, transfer sortgroupref data to replacement tlist */
566+
if (!gating_clauses)
567+
apply_pathtarget_labeling_to_tlist(tlist, best_path->pathtarget);
562568
}
563569
}
564570
}

src/test/regress/expected/aggregates.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,25 @@ select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
751751
9999 | 1
752752
(3 rows)
753753

754+
-- interesting corner case: constant gets optimized into a seqscan
755+
explain (costs off)
756+
select max(100) from tenk1;
757+
QUERY PLAN
758+
----------------------------------------------------
759+
Result
760+
InitPlan 1 (returns $0)
761+
-> Limit
762+
-> Result
763+
One-Time Filter: (100 IS NOT NULL)
764+
-> Seq Scan on tenk1
765+
(6 rows)
766+
767+
select max(100) from tenk1;
768+
max
769+
-----
770+
100
771+
(1 row)
772+
754773
-- try it on an inheritance tree
755774
create table minmaxtest(f1 int);
756775
create table minmaxtest1() inherits (minmaxtest);

src/test/regress/sql/aggregates.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,11 @@ explain (costs off)
270270
select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
271271
select max(unique2), generate_series(1,3) as g from tenk1 order by g desc;
272272

273+
-- interesting corner case: constant gets optimized into a seqscan
274+
explain (costs off)
275+
select max(100) from tenk1;
276+
select max(100) from tenk1;
277+
273278
-- try it on an inheritance tree
274279
create table minmaxtest(f1 int);
275280
create table minmaxtest1() inherits (minmaxtest);

0 commit comments

Comments
 (0)