Teach reparameterize_path() to handle AppendPaths.
authorTom Lane
Tue, 23 Jan 2018 21:50:35 +0000 (16:50 -0500)
committerTom Lane
Tue, 23 Jan 2018 21:50:35 +0000 (16:50 -0500)
If we're inside a lateral subquery, there may be no unparameterized paths
for a particular child relation of an appendrel, in which case we *must*
be able to create similarly-parameterized paths for each other child
relation, else the planner will fail with "could not devise a query plan
for the given query".  This means that there are situations where we'd
better be able to reparameterize at least one path for each child.

This calls into question the assumption in reparameterize_path() that
it can just punt if it feels like it.  However, the only case that is
known broken right now is where the child is itself an appendrel so that
all its paths are AppendPaths.  (I think possibly I disregarded that in
the original coding on the theory that nested appendrels would get folded
together --- but that only happens *after* reparameterize_path(), so it's
not excused from handling a child AppendPath.)  Given that this code's been
like this since 9.3 when LATERAL was introduced, it seems likely we'd have
heard of other cases by now if there were a larger problem.

Per report from Elvis Pranskevichus.  Back-patch to 9.3.

Discussion: https://postgr.es/m/5981018[email protected]

src/backend/optimizer/util/pathnode.c
src/test/regress/expected/join.out
src/test/regress/sql/join.sql

index 1d735589cb6b35d4c54c224d5ed03be27167fb69..9b90be5a3e6824a3cb975ba19ff93fa84ab938f1 100644 (file)
@@ -2075,6 +2075,28 @@ reparameterize_path(PlannerInfo *root, Path *path,
        case T_SubqueryScan:
            return create_subqueryscan_path(root, rel, path->pathkeys,
                                            required_outer);
+       case T_Append:
+           {
+               AppendPath *apath = (AppendPath *) path;
+               List       *childpaths = NIL;
+               ListCell   *lc;
+
+               /* Reparameterize the children */
+               foreach(lc, apath->subpaths)
+               {
+                   Path       *spath = (Path *) lfirst(lc);
+
+                   spath = reparameterize_path(root, spath,
+                                               required_outer,
+                                               loop_count);
+                   if (spath == NULL)
+                       return NULL;
+                   childpaths = lappend(childpaths, spath);
+               }
+               return (Path *)
+                   create_append_path(rel, childpaths,
+                                      required_outer);
+           }
        default:
            break;
    }
index 2c53c7292268626e4ffd9fc3eb61141eeeca86b9..d14ab18eda1c561b669cca89754d79d0ad63fdf2 100644 (file)
@@ -4954,6 +4954,25 @@ select * from
          Output: 3
 (11 rows)
 
+-- check handling of nested appendrels inside LATERAL
+select * from
+  ((select 2 as v) union all (select 3 as v)) as q1
+  cross join lateral
+  ((select * from
+      ((select 4 as v) union all (select 5 as v)) as q3)
+   union all
+   (select q1.v)
+  ) as q2;
+ v | v 
+---+---
+ 2 | 4
+ 2 | 5
+ 2 | 2
+ 3 | 4
+ 3 | 5
+ 3 | 3
+(6 rows)
+
 -- check we don't try to do a unique-ified semijoin with LATERAL
 explain (verbose, costs off)
 select * from
index 8378ba1b516ed05c5e4cb18b8d4a647f1fac0966..5e69a47850874367806725db0a6181eb570b8a65 100644 (file)
@@ -1569,6 +1569,16 @@ select * from
     select * from (select 3 as z) z where z.z = x.x
   ) zz on zz.z = y.y;
 
+-- check handling of nested appendrels inside LATERAL
+select * from
+  ((select 2 as v) union all (select 3 as v)) as q1
+  cross join lateral
+  ((select * from
+      ((select 4 as v) union all (select 5 as v)) as q3)
+   union all
+   (select q1.v)
+  ) as q2;
+
 -- check we don't try to do a unique-ified semijoin with LATERAL
 explain (verbose, costs off)
 select * from