Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit64635c8

Browse files
committed
Correctly identify which EC members are computable at a plan node.
find_computable_ec_member() had the wrong mental model of whatits primary caller prepare_sort_from_pathkeys() would do withthe selected EquivalenceClass member expression. We will notcompute the EC expression in a plan node atop the one returningthe passed-in targetlist; rather, the EC expression will becomputed as an additional column of that targetlist. So anyVar or quasi-Var used in the given tlist is also available to theEC expression. In simple cases this makes no difference becausethe given tlist is just a list of Vars or quasi-Vars --- but ifwe are considering an appendrel member produced by flatteninga UNION ALL, the tlist may contain expressions, resulting infailure to match and a "could not find pathkey item to sort"error.To fix, we can flatten both the tlist and the EC members withpull_var_clause(), and then just check for subset-ness, sothat the code is actually shorter than before.While this bug is quite old, the present patch only works back tov13. We could possibly make it work in v12 by back-patching partsof3753982. On the whole though I don't like the risk/rewardratio of that idea. v12's final release is next month, meaningthere would be no chance to correct matters if the patch causes aregression. 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 LaneDiscussion:https://postgr.es/m/18652-deaa782ebcca85d1@postgresql.org
1 parentf21e51e commit64635c8

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
Oiddatatype);
41-
staticboolis_exprlist_member(Expr*node,List*exprs);
4241
staticvoidgenerate_base_implied_equalities_const(PlannerInfo*root,
4342
EquivalenceClass*ec);
4443
staticvoidgenerate_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
Relidsrelids,
829837
boolrequire_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 areavailable in "exprs".
874+
* Match if all Vars and quasi-Vars arepresent 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
returnNULL;
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-
staticbool
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
@@ -1568,6 +1568,36 @@ select min(1-id) from matest0;
15681568

15691569
reset enable_seqscan;
15701570
reset enable_parallel_append;
1571+
explain (verbose, costs off) -- bug #18652
1572+
select 1 - id as c from
1573+
(select id from matest3 t1 union all select id * 2 from matest3 t2) ss
1574+
order by c;
1575+
QUERY PLAN
1576+
------------------------------------------------------------
1577+
Result
1578+
Output: ((1 - t1.id))
1579+
-> Merge Append
1580+
Sort Key: ((1 - t1.id))
1581+
-> Index Scan using matest3i on public.matest3 t1
1582+
Output: t1.id, (1 - t1.id)
1583+
-> Sort
1584+
Output: ((t2.id * 2)), ((1 - (t2.id * 2)))
1585+
Sort Key: ((1 - (t2.id * 2)))
1586+
-> Seq Scan on public.matest3 t2
1587+
Output: (t2.id * 2), (1 - (t2.id * 2))
1588+
(11 rows)
1589+
1590+
select 1 - id as c from
1591+
(select id from matest3 t1 union all select id * 2 from matest3 t2) ss
1592+
order by c;
1593+
c
1594+
-----
1595+
-11
1596+
-9
1597+
-5
1598+
-4
1599+
(4 rows)
1600+
15711601
drop table matest0 cascade;
15721602
NOTICE: drop cascades to 3 other objects
15731603
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
@@ -562,6 +562,14 @@ select min(1-id) from matest0;
562562
reset enable_seqscan;
563563
reset enable_parallel_append;
564564

565+
explain (verbose, costs off)-- bug #18652
566+
select1- idas cfrom
567+
(select idfrom matest3 t1union allselect id*2from matest3 t2) ss
568+
order by c;
569+
select1- idas cfrom
570+
(select idfrom matest3 t1union allselect id*2from matest3 t2) ss
571+
order by c;
572+
565573
droptable matest0 cascade;
566574

567575
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp