Skip to content

Commit f25d061

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 to enforce that all members of the equivalence class are actually equal. This only happened in the case of a parameterized join node for two of the relations, that is a plan tree like Nested Loop -> Scan X -> Nested Loop -> Scan Y -> Scan Z Filter: Z.Z = X.X The eclass machinery normally expects to apply X.X = Y.Y when those two relations are joined, but in this shape of plan tree they aren't joined until the top node --- and, if the lower nested loop is marked as parameterized by X, the top node will assume that the relevant eclass condition(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.Z to match any one of the other eclass members. So one or another of the required quals sometimes fell between the cracks, depending on whether consideration of the eclass in get_joinrel_parampathinfo() for the lower nested loop chanced to generate X.X = Y.Y or X.X = Z.Z as 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 node when this case occurs. This is *extremely* hard to hit in practice, because various planner behaviors conspire to mask the problem; starting with the fact that the planner doesn't really like to generate a parameterized plan of the above shape. (It might have been impossible to hit it before we tweaked things to allow this plan shape for star-schema cases.) Many thanks to Alexander Kirkouski for submitting a reproducible test case. The bug can be demonstrated in all branches back to 9.2 where parameterized paths were introduced, so back-patch that far.
1 parent eac3e16 commit f25d061

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
@@ -987,13 +987,31 @@ generate_base_implied_equalities_broken(PlannerInfo *root,
987987
*
988988
* join_relids should always equal bms_union(outer_relids, inner_rel->relids).
989989
* We could simplify this function's API by computing it internally, but in
990-
* all current uses, the caller has the value at hand anyway.
990+
* most current uses, the caller has the value at hand anyway.
991991
*/
992992
List *
993993
generate_join_implied_equalities(PlannerInfo *root,
994994
Relids join_relids,
995995
Relids outer_relids,
996996
RelOptInfo *inner_rel)
997+
{
998+
return generate_join_implied_equalities_for_ecs(root,
999+
root->eq_classes,
1000+
join_relids,
1001+
outer_relids,
1002+
inner_rel);
1003+
}
1004+
1005+
/*
1006+
* generate_join_implied_equalities_for_ecs
1007+
* As above, but consider only the listed ECs.
1008+
*/
1009+
List *
1010+
generate_join_implied_equalities_for_ecs(PlannerInfo *root,
1011+
List *eclasses,
1012+
Relids join_relids,
1013+
Relids outer_relids,
1014+
RelOptInfo *inner_rel)
9971015
{
9981016
List *result = NIL;
9991017
Relids inner_relids = inner_rel->relids;
@@ -1015,7 +1033,7 @@ generate_join_implied_equalities(PlannerInfo *root,
10151033
nominal_join_relids = join_relids;
10161034
}
10171035

1018-
foreach(lc, root->eq_classes)
1036+
foreach(lc, eclasses)
10191037
{
10201038
EquivalenceClass *ec = (EquivalenceClass *) lfirst(lc);
10211039
List *sublist = NIL;

src/backend/optimizer/util/relnode.c

Lines changed: 75 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,7 @@ get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel,
982982
Relids inner_and_req;
983983
List *pclauses;
984984
List *eclauses;
985+
List *dropped_ecs;
985986
double rows;
986987
ListCell *lc;
987988

@@ -1035,6 +1036,7 @@ get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel,
10351036
required_outer,
10361037
joinrel);
10371038
/* We only want ones that aren't movable to lower levels */
1039+
dropped_ecs = NIL;
10381040
foreach(lc, eclauses)
10391041
{
10401042
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
@@ -1051,13 +1053,79 @@ get_joinrel_parampathinfo(PlannerInfo *root, RelOptInfo *joinrel,
10511053
joinrel->relids,
10521054
join_and_req));
10531055
#endif
1054-
if (!join_clause_is_movable_into(rinfo,
1055-
outer_path->parent->relids,
1056-
outer_and_req) &&
1057-
!join_clause_is_movable_into(rinfo,
1058-
inner_path->parent->relids,
1059-
inner_and_req))
1060-
pclauses = lappend(pclauses, rinfo);
1056+
if (join_clause_is_movable_into(rinfo,
1057+
outer_path->parent->relids,
1058+
outer_and_req))
1059+
continue; /* drop if movable into LHS */
1060+
if (join_clause_is_movable_into(rinfo,
1061+
inner_path->parent->relids,
1062+
inner_and_req))
1063+
{
1064+
/* drop if movable into RHS, but remember EC for use below */
1065+
Assert(rinfo->left_ec == rinfo->right_ec);
1066+
dropped_ecs = lappend(dropped_ecs, rinfo->left_ec);
1067+
continue;
1068+
}
1069+
pclauses = lappend(pclauses, rinfo);
1070+
}
1071+
1072+
/*
1073+
* EquivalenceClasses are harder to deal with than we could wish, because
1074+
* of the fact that a given EC can generate different clauses depending on
1075+
* context. Suppose we have an EC {X.X, Y.Y, Z.Z} where X and Y are the
1076+
* LHS and RHS of the current join and Z is in required_outer, and further
1077+
* suppose that the inner_path is parameterized by both X and Z. The code
1078+
* above will have produced either Z.Z = X.X or Z.Z = Y.Y from that EC,
1079+
* and in the latter case will have discarded it as being movable into the
1080+
* RHS. However, the EC machinery might have produced either Y.Y = X.X or
1081+
* Y.Y = Z.Z as the EC enforcement clause within the inner_path; it will
1082+
* not have produced both, and we can't readily tell from here which one
1083+
* it did pick. If we add no clause to this join, we'll end up with
1084+
* insufficient enforcement of the EC; either Z.Z or X.X will fail to be
1085+
* constrained to be equal to the other members of the EC. (When we come
1086+
* to join Z to this X/Y path, we will certainly drop whichever EC clause
1087+
* is generated at that join, so this omission won't get fixed later.)
1088+
*
1089+
* To handle this, for each EC we discarded such a clause from, try to
1090+
* generate a clause connecting the required_outer rels to the join's LHS
1091+
* ("Z.Z = X.X" in the terms of the above example). If successful, and if
1092+
* the clause can't be moved to the LHS, add it to the current join's
1093+
* restriction clauses. (If an EC cannot generate such a clause then it
1094+
* has nothing that needs to be enforced here, while if the clause can be
1095+
* moved into the LHS then it should have been enforced within that path.)
1096+
*
1097+
* Note that we don't need similar processing for ECs whose clause was
1098+
* considered to be movable into the LHS, because the LHS can't refer to
1099+
* the RHS so there is no comparable ambiguity about what it might
1100+
* actually be enforcing internally.
1101+
*/
1102+
if (dropped_ecs)
1103+
{
1104+
Relids real_outer_and_req;
1105+
1106+
real_outer_and_req = bms_union(outer_path->parent->relids,
1107+
required_outer);
1108+
eclauses =
1109+
generate_join_implied_equalities_for_ecs(root,
1110+
dropped_ecs,
1111+
real_outer_and_req,
1112+
required_outer,
1113+
outer_path->parent);
1114+
foreach(lc, eclauses)
1115+
{
1116+
RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1117+
1118+
/* As above, can't quite assert this here */
1119+
#ifdef NOT_USED
1120+
Assert(join_clause_is_movable_into(rinfo,
1121+
outer_path->parent->relids,
1122+
real_outer_and_req));
1123+
#endif
1124+
if (!join_clause_is_movable_into(rinfo,
1125+
outer_path->parent->relids,
1126+
outer_and_req))
1127+
pclauses = lappend(pclauses, rinfo);
1128+
}
10611129
}
10621130

10631131
/*

src/include/optimizer/paths.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ extern List *generate_join_implied_equalities(PlannerInfo *root,
130130
Relids join_relids,
131131
Relids outer_relids,
132132
RelOptInfo *inner_rel);
133+
extern List *generate_join_implied_equalities_for_ecs(PlannerInfo *root,
134+
List *eclasses,
135+
Relids join_relids,
136+
Relids outer_relids,
137+
RelOptInfo *inner_rel);
133138
extern bool exprs_known_equal(PlannerInfo *root, Node *item1, Node *item2);
134139
extern void add_child_rel_equivalences(PlannerInfo *root,
135140
AppendRelInfo *appinfo,

src/test/regress/expected/join.out

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

2239+
--
2240+
-- regression test: check a case where we formerly missed including an EC
2241+
-- enforcement clause because it was expected to be handled at scan level
2242+
--
2243+
explain (costs off)
2244+
select a.f1, b.f1, t.thousand, t.tenthous from
2245+
tenk1 t,
2246+
(select sum(f1)+1 as f1 from int4_tbl i4a) a,
2247+
(select sum(f1) as f1 from int4_tbl i4b) b
2248+
where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
2249+
QUERY PLAN
2250+
-----------------------------------------------------------------------------------------------------------------------
2251+
Nested Loop
2252+
-> Aggregate
2253+
-> Seq Scan on int4_tbl i4b
2254+
-> Nested Loop
2255+
Join Filter: ((sum(i4b.f1)) = ((sum(i4a.f1) + 1)))
2256+
-> Aggregate
2257+
-> Seq Scan on int4_tbl i4a
2258+
-> Index Only Scan using tenk1_thous_tenthous on tenk1 t
2259+
Index Cond: ((thousand = (sum(i4b.f1))) AND (tenthous = ((((sum(i4a.f1) + 1)) + (sum(i4b.f1))) + 999)))
2260+
(9 rows)
2261+
2262+
select a.f1, b.f1, t.thousand, t.tenthous from
2263+
tenk1 t,
2264+
(select sum(f1)+1 as f1 from int4_tbl i4a) a,
2265+
(select sum(f1) as f1 from int4_tbl i4b) b
2266+
where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
2267+
f1 | f1 | thousand | tenthous
2268+
----+----+----------+----------
2269+
(0 rows)
2270+
22392271
--
22402272
-- Clean up
22412273
--

src/test/regress/sql/join.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,23 @@ from
391391
tenk1 t5
392392
where t4.thousand = t5.unique1 and ss.x1 = t4.tenthous and ss.x2 = t5.stringu1;
393393

394+
--
395+
-- regression test: check a case where we formerly missed including an EC
396+
-- enforcement clause because it was expected to be handled at scan level
397+
--
398+
explain (costs off)
399+
select a.f1, b.f1, t.thousand, t.tenthous from
400+
tenk1 t,
401+
(select sum(f1)+1 as f1 from int4_tbl i4a) a,
402+
(select sum(f1) as f1 from int4_tbl i4b) b
403+
where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
404+
405+
select a.f1, b.f1, t.thousand, t.tenthous from
406+
tenk1 t,
407+
(select sum(f1)+1 as f1 from int4_tbl i4a) a,
408+
(select sum(f1) as f1 from int4_tbl i4b) b
409+
where b.f1 = t.thousand and a.f1 = b.f1 and (a.f1+b.f1+999) = t.tenthous;
410+
394411

395412
--
396413
-- Clean up

0 commit comments

Comments
 (0)