Fix generation of EC join conditions at the wrong plan level.
authorTom Lane <[email protected]>
Tue, 16 Apr 2024 15:22:39 +0000 (11:22 -0400)
committerTom Lane <[email protected]>
Tue, 16 Apr 2024 15:22:39 +0000 (11:22 -0400)
get_baserel_parampathinfo previously assumed without checking that
the results of generate_join_implied_equalities "necessarily satisfy
join_clause_is_movable_into".  This turns out to be wrong in the
presence of outer joins, because the generated clauses could include
Vars that mustn't be evaluated below a relevant outer join.  That
led to applying clauses at the wrong plan level and possibly getting
incorrect query results.  We must check each clause's nullable_relids,
and really the right thing to do is test join_clause_is_movable_into.

However, trying to fix it that way exposes an oversight in
equivclass.c: it wasn't careful about marking join clauses for
appendrel children with the correct clause_relids.  That caused the
modified get_baserel_parampathinfo code to reject some clauses it
still needs to accept.  (See parallel commit for HEAD/v16 for more
commentary about that.)

Per bug #18429 from BenoĆ®t Ryder.  This misbehavior existed for
a long time before commit 2489d76c4, so patch v12-v15 this way.

Discussion: https://postgr.es/m/18429-8982d4a348cc86c6@postgresql.org

src/backend/optimizer/path/equivclass.c
src/backend/optimizer/util/relnode.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index e54da55924ea602782625443fde6392aa3afaaa7..94387fdaca86de875a65707e1752dd10dac33188 100644 (file)
@@ -1465,6 +1465,21 @@ create_join_clause(PlannerInfo *root,
                                                  rightem->em_nullable_relids),
                                        ec->ec_min_security);
 
+   /*
+    * If either EM is a child, force the clause's clause_relids to include
+    * the relid(s) of the child rel.  In normal cases it would already, but
+    * not if we are considering appendrel child relations with pseudoconstant
+    * translated variables (i.e., UNION ALL sub-selects with constant output
+    * items).  We must do this so that join_clause_is_movable_into() will
+    * think that the clause should be evaluated at the correct place.
+    */
+   if (leftem->em_is_child)
+       rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
+                                              leftem->em_relids);
+   if (rightem->em_is_child)
+       rinfo->clause_relids = bms_add_members(rinfo->clause_relids,
+                                              rightem->em_relids);
+
    /* Mark the clause as redundant, or not */
    rinfo->parent_ec = parent_ec;
 
index 7758a49fa4c0317373a97427e28cd3e55b72e414..380ce9fc9a4977650441e708e8b210cc7bd6b928 100644 (file)
@@ -1264,6 +1264,7 @@ get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel,
    ParamPathInfo *ppi;
    Relids      joinrelids;
    List       *pclauses;
+   List       *eqclauses;
    double      rows;
    ListCell   *lc;
 
@@ -1297,14 +1298,24 @@ get_baserel_parampathinfo(PlannerInfo *root, RelOptInfo *baserel,
    }
 
    /*
-    * Add in joinclauses generated by EquivalenceClasses, too.  (These
-    * necessarily satisfy join_clause_is_movable_into.)
+    * Add in joinclauses generated by EquivalenceClasses, too.  In principle
+    * these should always satisfy join_clause_is_movable_into; but if we are
+    * below an outer join the clauses might contain Vars that should only be
+    * evaluated above the join, so we have to check.
     */
-   pclauses = list_concat(pclauses,
-                          generate_join_implied_equalities(root,
-                                                           joinrelids,
-                                                           required_outer,
-                                                           baserel));
+   eqclauses = generate_join_implied_equalities(root,
+                                                joinrelids,
+                                                required_outer,
+                                                baserel);
+   foreach(lc, eqclauses)
+   {
+       RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
+
+       if (join_clause_is_movable_into(rinfo,
+                                       baserel->relids,
+                                       joinrelids))
+           pclauses = lappend(pclauses, rinfo);
+   }
 
    /* Estimate the number of rows returned by the parameterized scan */
    rows = get_parameterized_baserel_size(root, baserel, pclauses);
index b32440f87df23eea929d2e2370e742ae46e49da1..239ca699df8e2f88e06f579ce790d028969ae563 100644 (file)
@@ -5672,6 +5672,37 @@ select * from
  3 | 3
 (6 rows)
 
+-- check for generation of join EC conditions at wrong level (bug #18429)
+explain (costs off)
+select * from (
+  select arrayd.ad, coalesce(c.hundred, 0) as h
+  from unnest(array[1]) as arrayd(ad)
+  left join lateral (
+    select hundred from tenk1 where unique2 = arrayd.ad
+  ) c on true
+) c2
+where c2.h * c2.ad = c2.h * (c2.ad + 1);
+                                              QUERY PLAN                                               
+-------------------------------------------------------------------------------------------------------
+ Nested Loop Left Join
+   Filter: ((COALESCE(tenk1.hundred, 0) * arrayd.ad) = (COALESCE(tenk1.hundred, 0) * (arrayd.ad + 1)))
+   ->  Function Scan on unnest arrayd
+   ->  Index Scan using tenk1_unique2 on tenk1
+         Index Cond: (unique2 = arrayd.ad)
+(5 rows)
+
+select * from (
+  select arrayd.ad, coalesce(c.hundred, 0) as h
+  from unnest(array[1]) as arrayd(ad)
+  left join lateral (
+    select hundred from tenk1 where unique2 = arrayd.ad
+  ) c on true
+) c2
+where c2.h * c2.ad = c2.h * (c2.ad + 1);
+ ad | h 
+----+---
+(0 rows)
+
 -- check the number of columns specified
 SELECT * FROM (int8_tbl i cross join int4_tbl j) ss(a,b,c,d);
 ERROR:  join expression "ss" has 3 columns available but 4 columns specified
index 33a737b1c9aa3a42637e0444e537c67e233d2465..e8d3e4d0e677cbdc5caaea425f6b46e38db5dd22 100644 (file)
@@ -1920,6 +1920,25 @@ select * from
    (select q1.v)
   ) as q2;
 
+-- check for generation of join EC conditions at wrong level (bug #18429)
+explain (costs off)
+select * from (
+  select arrayd.ad, coalesce(c.hundred, 0) as h
+  from unnest(array[1]) as arrayd(ad)
+  left join lateral (
+    select hundred from tenk1 where unique2 = arrayd.ad
+  ) c on true
+) c2
+where c2.h * c2.ad = c2.h * (c2.ad + 1);
+select * from (
+  select arrayd.ad, coalesce(c.hundred, 0) as h
+  from unnest(array[1]) as arrayd(ad)
+  left join lateral (
+    select hundred from tenk1 where unique2 = arrayd.ad
+  ) c on true
+) c2
+where c2.h * c2.ad = c2.h * (c2.ad + 1);
+
 -- check the number of columns specified
 SELECT * FROM (int8_tbl i cross join int4_tbl j) ss(a,b,c,d);