Skip to content

Commit 54889ea

Browse files
committed
Correctly identify which EC members are computable at a plan node.
find_computable_ec_member() had the wrong mental model of what its primary caller prepare_sort_from_pathkeys() would do with the selected EquivalenceClass member expression. We will not compute the EC expression in a plan node atop the one returning the passed-in targetlist; rather, the EC expression will be computed as an additional column of that targetlist. So any Var or quasi-Var used in the given tlist is also available to the EC expression. In simple cases this makes no difference because the given tlist is just a list of Vars or quasi-Vars --- but if we are considering an appendrel member produced by flattening a UNION ALL, the tlist may contain expressions, resulting in failure to match and a "could not find pathkey item to sort" error. To fix, we can flatten both the tlist and the EC members with pull_var_clause(), and then just check for subset-ness, so that the code is actually shorter than before. While this bug is quite old, the present patch only works back to v13. We could possibly make it work in v12 by back-patching parts of 3753982. On the whole though I don't like the risk/reward ratio of that idea. v12's final release is next month, meaning there would be no chance to correct matters if the patch causes a regression. Since this failure has escaped notice for 14 years, it's likely nobody will hit it in the field with v12. Per bug #18652 from Alexander Lakhin. Andrei Lepikhov and Tom Lane Discussion: https://postgr.es/m/18652-deaa782ebcca85d1@postgresql.org
1 parent ff33df2 commit 54889ea

File tree

3 files changed

+71
-38
lines changed

3 files changed

+71
-38
lines changed

src/backend/optimizer/path/equivclass.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ static EquivalenceMember *add_eq_member(EquivalenceClass *ec,
3838
JoinDomain *jdomain,
3939
EquivalenceMember *parent,
4040
Oid datatype);
41-
static bool is_exprlist_member(Expr *node, List *exprs);
4241
static void generate_base_implied_equalities_const(PlannerInfo *root,
4342
EquivalenceClass *ec);
4443
static void generate_base_implied_equalities_no_const(PlannerInfo *root,
@@ -806,9 +805,18 @@ find_ec_member_matching_expr(EquivalenceClass *ec,
806805
* expressions appearing in "exprs"; return NULL if no match.
807806
*
808807
* "exprs" can be either a list of bare expression trees, or a list of
809-
* TargetEntry nodes. Either way, it should contain Vars and possibly
810-
* Aggrefs and WindowFuncs, which are matched to the corresponding elements
811-
* of the EquivalenceClass's expressions.
808+
* TargetEntry nodes. Typically it will contain Vars and possibly Aggrefs
809+
* and WindowFuncs; however, when considering an appendrel member the list
810+
* could contain arbitrary expressions. We consider an EC member to be
811+
* computable if all the Vars, PlaceHolderVars, Aggrefs, and WindowFuncs
812+
* it needs are present in "exprs".
813+
*
814+
* There is some subtlety in that definition: for example, if an EC member is
815+
* Var_A + 1 while what is in "exprs" is Var_A + 2, it's still computable.
816+
* This works because in the final plan tree, the EC member's expression will
817+
* be computed as part of the same plan node targetlist that is currently
818+
* represented by "exprs". So if we have Var_A available for the existing
819+
* tlist member, it must be OK to use it in the EC expression too.
812820
*
813821
* Unlike find_ec_member_matching_expr, there's no special provision here
814822
* for binary-compatible relabeling. This is intentional: if we have to
@@ -828,12 +836,24 @@ find_computable_ec_member(PlannerInfo *root,
828836
Relids relids,
829837
bool require_parallel_safe)
830838
{
839+
List *exprvars;
831840
ListCell *lc;
832841

842+
/*
843+
* Pull out the Vars and quasi-Vars present in "exprs". In the typical
844+
* non-appendrel case, this is just another representation of the same
845+
* list. However, it does remove the distinction between the case of a
846+
* list of plain expressions and a list of TargetEntrys.
847+
*/
848+
exprvars = pull_var_clause((Node *) exprs,
849+
PVC_INCLUDE_AGGREGATES |
850+
PVC_INCLUDE_WINDOWFUNCS |
851+
PVC_INCLUDE_PLACEHOLDERS);
852+
833853
foreach(lc, ec->ec_members)
834854
{
835855
EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
836-
List *exprvars;
856+
List *emvars;
837857
ListCell *lc2;
838858

839859
/*
@@ -851,18 +871,18 @@ find_computable_ec_member(PlannerInfo *root,
851871
continue;
852872

853873
/*
854-
* Match if all Vars and quasi-Vars are available in "exprs".
874+
* Match if all Vars and quasi-Vars are present in "exprs".
855875
*/
856-
exprvars = pull_var_clause((Node *) em->em_expr,
857-
PVC_INCLUDE_AGGREGATES |
858-
PVC_INCLUDE_WINDOWFUNCS |
859-
PVC_INCLUDE_PLACEHOLDERS);
860-
foreach(lc2, exprvars)
876+
emvars = pull_var_clause((Node *) em->em_expr,
877+
PVC_INCLUDE_AGGREGATES |
878+
PVC_INCLUDE_WINDOWFUNCS |
879+
PVC_INCLUDE_PLACEHOLDERS);
880+
foreach(lc2, emvars)
861881
{
862-
if (!is_exprlist_member(lfirst(lc2), exprs))
882+
if (!list_member(exprvars, lfirst(lc2)))
863883
break;
864884
}
865-
list_free(exprvars);
885+
list_free(emvars);
866886
if (lc2)
867887
continue; /* we hit a non-available Var */
868888

@@ -880,31 +900,6 @@ find_computable_ec_member(PlannerInfo *root,
880900
return NULL;
881901
}
882902

883-
/*
884-
* is_exprlist_member
885-
* Subroutine for find_computable_ec_member: is "node" in "exprs"?
886-
*
887-
* Per the requirements of that function, "exprs" might or might not have
888-
* TargetEntry superstructure.
889-
*/
890-
static bool
891-
is_exprlist_member(Expr *node, List *exprs)
892-
{
893-
ListCell *lc;
894-
895-
foreach(lc, exprs)
896-
{
897-
Expr *expr = (Expr *) lfirst(lc);
898-
899-
if (expr && IsA(expr, TargetEntry))
900-
expr = ((TargetEntry *) expr)->expr;
901-
902-
if (equal(node, expr))
903-
return true;
904-
}
905-
return false;
906-
}
907-
908903
/*
909904
* relation_can_be_sorted_early
910905
* Can this relation be sorted on this EC before the final output step?

src/test/regress/expected/inherit.out

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,36 @@ select min(1-id) from matest0;
16771677

16781678
reset enable_seqscan;
16791679
reset enable_parallel_append;
1680+
explain (verbose, costs off) -- bug #18652
1681+
select 1 - id as c from
1682+
(select id from matest3 t1 union all select id * 2 from matest3 t2) ss
1683+
order by c;
1684+
QUERY PLAN
1685+
------------------------------------------------------------
1686+
Result
1687+
Output: ((1 - t1.id))
1688+
-> Merge Append
1689+
Sort Key: ((1 - t1.id))
1690+
-> Index Scan using matest3i on public.matest3 t1
1691+
Output: t1.id, (1 - t1.id)
1692+
-> Sort
1693+
Output: ((t2.id * 2)), ((1 - (t2.id * 2)))
1694+
Sort Key: ((1 - (t2.id * 2)))
1695+
-> Seq Scan on public.matest3 t2
1696+
Output: (t2.id * 2), (1 - (t2.id * 2))
1697+
(11 rows)
1698+
1699+
select 1 - id as c from
1700+
(select id from matest3 t1 union all select id * 2 from matest3 t2) ss
1701+
order by c;
1702+
c
1703+
-----
1704+
-11
1705+
-9
1706+
-5
1707+
-4
1708+
(4 rows)
1709+
16801710
drop table matest0 cascade;
16811711
NOTICE: drop cascades to 3 other objects
16821712
DETAIL: drop cascades to table matest1

src/test/regress/sql/inherit.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,14 @@ select min(1-id) from matest0;
601601
reset enable_seqscan;
602602
reset enable_parallel_append;
603603

604+
explain (verbose, costs off) -- bug #18652
605+
select 1 - id as c from
606+
(select id from matest3 t1 union all select id * 2 from matest3 t2) ss
607+
order by c;
608+
select 1 - id as c from
609+
(select id from matest3 t1 union all select id * 2 from matest3 t2) ss
610+
order by c;
611+
604612
drop table matest0 cascade;
605613

606614
--

0 commit comments

Comments
 (0)