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

Commit67349e5

Browse files
committed
Fix mishandling of equivalence-class tests in parameterized plans.
Given a three-or-more-way equivalence class, such as X.Y = Y.Y = Z.Z,it was possible for the planner to omit one of the quals needed toenforce that all members of the equivalence class are actually equal.This only happened in the case of a parameterized join node for twoof the relations, that is a plan tree likeNested Loop -> Scan X -> Nested Loop -> Scan Y -> Scan Z Filter: Z.Z = X.XThe eclass machinery normally expects to apply X.X = Y.Y when thosetwo relations are joined, but in this shape of plan tree they aren'tjoined until the top node --- and, if the lower nested loop is markedas parameterized by X, the top node will assume that the relevant eclasscondition(s) got pushed down into the lower node. On the other hand,the scan of Z assumes that it's only responsible for constraining Z.Zto match any one of the other eclass members. So one or another ofthe required quals sometimes fell between the cracks, depending onwhether consideration of the eclass in get_joinrel_parampathinfo()for the lower nested loop chanced to generate X.X = Y.Y or X.X = Z.Zas the appropriate constraint there. If it generated the latter,it'd erroneously suppose that the Z scan would take care of matters.To fix, force X.X = Y.Y to be generated and applied at that join nodewhen this case occurs.This is *extremely* hard to hit in practice, because various plannerbehaviors conspire to mask the problem; starting with the fact that theplanner doesn't really like to generate a parameterized plan of theabove shape. (It might have been impossible to hit it before wetweaked things to allow this plan shape for star-schema cases.) Manythanks to Alexander Kirkouski for submitting a reproducible test case.The bug can be demonstrated in all branches back to 9.2 where parameterizedpaths were introduced, so back-patch that far.
1 parent707c44f commit67349e5

File tree

5 files changed

+149
-9
lines changed

5 files changed

+149
-9
lines changed

‎src/backend/optimizer/path/equivclass.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -988,13 +988,31 @@ generate_base_implied_equalities_broken(PlannerInfo *root,
988988
*
989989
* join_relids should always equal bms_union(outer_relids, inner_rel->relids).
990990
* We could simplify this function's API by computing it internally, but in
991-
*all current uses, the caller has the value at hand anyway.
991+
*most current uses, the caller has the value at hand anyway.
992992
*/
993993
List*
994994
generate_join_implied_equalities(PlannerInfo*root,
995995
Relidsjoin_relids,
996996
Relidsouter_relids,
997997
RelOptInfo*inner_rel)
998+
{
999+
returngenerate_join_implied_equalities_for_ecs(root,
1000+
root->eq_classes,
1001+
join_relids,
1002+
outer_relids,
1003+
inner_rel);
1004+
}
1005+
1006+
/*
1007+
* generate_join_implied_equalities_for_ecs
1008+
* As above, but consider only the listed ECs.
1009+
*/
1010+
List*
1011+
generate_join_implied_equalities_for_ecs(PlannerInfo*root,
1012+
List*eclasses,
1013+
Relidsjoin_relids,
1014+
Relidsouter_relids,
1015+
RelOptInfo*inner_rel)
9981016
{
9991017
List*result=NIL;
10001018
Relidsinner_relids=inner_rel->relids;
@@ -1016,7 +1034,7 @@ generate_join_implied_equalities(PlannerInfo *root,
10161034
nominal_join_relids=join_relids;
10171035
}
10181036

1019-
foreach(lc,root->eq_classes)
1037+
foreach(lc,eclasses)
10201038
{
10211039
EquivalenceClass*ec= (EquivalenceClass*)lfirst(lc);
10221040
List*sublist=NIL;

‎src/backend/optimizer/util/relnode.c

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,7 @@ get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel,
922922
Relidsinner_and_req;
923923
List*pclauses;
924924
List*eclauses;
925+
List*dropped_ecs;
925926
doublerows;
926927
ListCell*lc;
927928

@@ -975,6 +976,7 @@ get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel,
975976
required_outer,
976977
joinrel);
977978
/* We only want ones that aren't movable to lower levels */
979+
dropped_ecs=NIL;
978980
foreach(lc,eclauses)
979981
{
980982
RestrictInfo*rinfo= (RestrictInfo*)lfirst(lc);
@@ -991,13 +993,79 @@ get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel,
991993
joinrel->relids,
992994
join_and_req));
993995
#endif
994-
if (!join_clause_is_movable_into(rinfo,
995-
outer_path->parent->relids,
996-
outer_and_req)&&
997-
!join_clause_is_movable_into(rinfo,
998-
inner_path->parent->relids,
999-
inner_and_req))
1000-
pclauses=lappend(pclauses,rinfo);
996+
if (join_clause_is_movable_into(rinfo,
997+
outer_path->parent->relids,
998+
outer_and_req))
999+
continue;/* drop if movable into LHS */
1000+
if (join_clause_is_movable_into(rinfo,
1001+
inner_path->parent->relids,
1002+
inner_and_req))
1003+
{
1004+
/* drop if movable into RHS, but remember EC for use below */
1005+
Assert(rinfo->left_ec==rinfo->right_ec);
1006+
dropped_ecs=lappend(dropped_ecs,rinfo->left_ec);
1007+
continue;
1008+
}
1009+
pclauses=lappend(pclauses,rinfo);
1010+
}
1011+
1012+
/*
1013+
* EquivalenceClasses are harder to deal with than we could wish, because
1014+
* of the fact that a given EC can generate different clauses depending on
1015+
* context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1016+
* LHS and RHS of the current join and Z is in required_outer, and further
1017+
* suppose that the inner_path is parameterized by both X and Z. The code
1018+
* above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1019+
* and in the latter case will have discarded it as being movable into the
1020+
* RHS. However, the EC machinery might have produced either Y.Y = X.X or
1021+
* Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1022+
* not have produced both, and we can't readily tell from here which one
1023+
* it did pick. If we add no clause to this join, we'll end up with
1024+
* insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1025+
* constrained to be equal to the other members of the EC. (When we come
1026+
* to join Z to this X/Y path, we will certainly drop whichever EC clause
1027+
* is generated at that join, so this omission won't get fixed later.)
1028+
*
1029+
* To handle this, for each EC we discarded such a clause from, try to
1030+
* generate a clause connecting the required_outer rels to the join's LHS
1031+
* ("Z.Z = X.X" in the terms of the above example). If successful, and if
1032+
* the clause can't be moved to the LHS, add it to the current join's
1033+
* restriction clauses. (If an EC cannot generate such a clause then it
1034+
* has nothing that needs to be enforced here, while if the clause can be
1035+
* moved into the LHS then it should have been enforced within that path.)
1036+
*
1037+
* Note that we don't need similar processing for ECs whose clause was
1038+
* considered to be movable into the LHS, because the LHS can't refer to
1039+
* the RHS so there is no comparable ambiguity about what it might
1040+
* actually be enforcing internally.
1041+
*/
1042+
if (dropped_ecs)
1043+
{
1044+
Relidsreal_outer_and_req;
1045+
1046+
real_outer_and_req=bms_union(outer_path->parent->relids,
1047+
required_outer);
1048+
eclauses=
1049+
generate_join_implied_equalities_for_ecs(root,
1050+
dropped_ecs,
1051+
real_outer_and_req,
1052+
required_outer,
1053+
outer_path->parent);
1054+
foreach(lc,eclauses)
1055+
{
1056+
RestrictInfo*rinfo= (RestrictInfo*)lfirst(lc);
1057+
1058+
/* As above, can't quite assert this here */
1059+
#ifdefNOT_USED
1060+
Assert(join_clause_is_movable_into(rinfo,
1061+
outer_path->parent->relids,
1062+
real_outer_and_req));
1063+
#endif
1064+
if (!join_clause_is_movable_into(rinfo,
1065+
outer_path->parent->relids,
1066+
outer_and_req))
1067+
pclauses=lappend(pclauses,rinfo);
1068+
}
10011069
}
10021070

10031071
/*

‎src/include/optimizer/paths.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ extern List *generate_join_implied_equalities(PlannerInfo *root,
123123
Relidsjoin_relids,
124124
Relidsouter_relids,
125125
RelOptInfo*inner_rel);
126+
externList*generate_join_implied_equalities_for_ecs(PlannerInfo*root,
127+
List*eclasses,
128+
Relidsjoin_relids,
129+
Relidsouter_relids,
130+
RelOptInfo*inner_rel);
126131
externboolexprs_known_equal(PlannerInfo*root,Node*item1,Node*item2);
127132
externvoidadd_child_rel_equivalences(PlannerInfo*root,
128133
AppendRelInfo*appinfo,

‎src/test/regress/expected/join.out

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,6 +2202,38 @@ where t4.thousand = t5.unique1 and ss.x1 = t4.tenthous and ss.x2 = t5.stringu1;
22022202
1000
22032203
(1 row)
22042204

2205+
--
2206+
-- regression test: check a case where we formerly missed including an EC
2207+
-- enforcement clause because it was expected to be handled at scan level
2208+
--
2209+
explain (costs off)
2210+
select a.f1, b.f1, t.thousand, t.tenthous from
2211+
tenk1 t,
2212+
(select sum(f1)+1 as f1 from int4_tbl i4a) a,
2213+
(select sum(f1) as f1 from int4_tbl i4b) b
2214+
where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
2215+
QUERY PLAN
2216+
-----------------------------------------------------------------------------------------------------------------------
2217+
Nested Loop
2218+
-> Aggregate
2219+
-> Seq Scan on int4_tbl i4b
2220+
-> Nested Loop
2221+
Join Filter: ((sum(i4b.f1)) = ((sum(i4a.f1) + 1)))
2222+
-> Aggregate
2223+
-> Seq Scan on int4_tbl i4a
2224+
-> Index Only Scan using tenk1_thous_tenthous on tenk1 t
2225+
Index Cond: ((thousand = (sum(i4b.f1))) AND (tenthous = ((((sum(i4a.f1) + 1)) + (sum(i4b.f1))) + 999)))
2226+
(9 rows)
2227+
2228+
select a.f1, b.f1, t.thousand, t.tenthous from
2229+
tenk1 t,
2230+
(select sum(f1)+1 as f1 from int4_tbl i4a) a,
2231+
(select sum(f1) as f1 from int4_tbl i4b) b
2232+
where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
2233+
f1 | f1 | thousand | tenthous
2234+
----+----+----------+----------
2235+
(0 rows)
2236+
22052237
--
22062238
-- Clean up
22072239
--

‎src/test/regress/sql/join.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,23 @@ from
379379
tenk1 t5
380380
wheret4.thousand=t5.unique1andss.x1=t4.tenthousandss.x2=t5.stringu1;
381381

382+
--
383+
-- regression test: check a case where we formerly missed including an EC
384+
-- enforcement clause because it was expected to be handled at scan level
385+
--
386+
explain (costs off)
387+
selecta.f1,b.f1,t.thousand,t.tenthousfrom
388+
tenk1 t,
389+
(selectsum(f1)+1as f1from int4_tbl i4a) a,
390+
(selectsum(f1)as f1from int4_tbl i4b) b
391+
whereb.f1=t.thousandanda.f1=b.f1and (a.f1+b.f1+999)=t.tenthous;
392+
393+
selecta.f1,b.f1,t.thousand,t.tenthousfrom
394+
tenk1 t,
395+
(selectsum(f1)+1as f1from int4_tbl i4a) a,
396+
(selectsum(f1)as f1from int4_tbl i4b) b
397+
whereb.f1=t.thousandanda.f1=b.f1and (a.f1+b.f1+999)=t.tenthous;
398+
382399

383400
--
384401
-- Clean up

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp