]> granicus.if.org Git - postgresql/blobdiff - src/backend/optimizer/prep/prepjointree.c
Update copyright for 2016
[postgresql] / src / backend / optimizer / prep / prepjointree.c
index c386586f40d22962782b33ded542ee67b49966b3..b368cf94586f79081d8342957ccdaa30517593aa 100644 (file)
@@ -12,7 +12,7 @@
  *             reduce_outer_joins
  *
  *
- * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2016, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
@@ -31,7 +31,6 @@
 #include "optimizer/prep.h"
 #include "optimizer/subselect.h"
 #include "optimizer/tlist.h"
-#include "optimizer/var.h"
 #include "parser/parse_relation.h"
 #include "parser/parsetree.h"
 #include "rewrite/rewriteManip.h"
@@ -42,6 +41,8 @@ typedef struct pullup_replace_vars_context
        PlannerInfo *root;
        List       *targetlist;         /* tlist of subquery being pulled up */
        RangeTblEntry *target_rte;      /* RTE of subquery */
+       Relids          relids;                 /* relids within subquery, as numbered after
+                                                                * pullup (set only if target_rte->lateral) */
        bool       *outer_hasSubLinks;          /* -> outer query's hasSubLinks */
        int                     varno;                  /* varno of subquery */
        bool            need_phvs;              /* do we need PlaceHolderVars? */
@@ -59,11 +60,19 @@ typedef struct reduce_outer_joins_state
 static Node *pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
                                                                  Relids *relids);
 static Node *pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
-                                                         Relids available_rels, Node **jtlink);
+                                                         Node **jtlink1, Relids available_rels1,
+                                                         Node **jtlink2, Relids available_rels2);
+static Node *pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
+                                                  JoinExpr *lowest_outer_join,
+                                                  JoinExpr *lowest_nulling_outer_join,
+                                                  AppendRelInfo *containing_appendrel,
+                                                  bool deletion_ok);
 static Node *pull_up_simple_subquery(PlannerInfo *root, Node *jtnode,
                                                RangeTblEntry *rte,
                                                JoinExpr *lowest_outer_join,
-                                               AppendRelInfo *containing_appendrel);
+                                               JoinExpr *lowest_nulling_outer_join,
+                                               AppendRelInfo *containing_appendrel,
+                                               bool deletion_ok);
 static Node *pull_up_simple_union_all(PlannerInfo *root, Node *jtnode,
                                                 RangeTblEntry *rte);
 static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
@@ -71,18 +80,29 @@ static void pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root,
                                                   int childRToffset);
 static void make_setop_translation_list(Query *query, Index newvarno,
                                                        List **translated_vars);
-static bool is_simple_subquery(Query *subquery);
+static bool is_simple_subquery(Query *subquery, RangeTblEntry *rte,
+                                  JoinExpr *lowest_outer_join,
+                                  bool deletion_ok);
+static Node *pull_up_simple_values(PlannerInfo *root, Node *jtnode,
+                                         RangeTblEntry *rte);
+static bool is_simple_values(PlannerInfo *root, RangeTblEntry *rte,
+                                bool deletion_ok);
 static bool is_simple_union_all(Query *subquery);
 static bool is_simple_union_all_recurse(Node *setOp, Query *setOpQuery,
                                                        List *colTypes);
 static bool is_safe_append_member(Query *subquery);
+static bool jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted,
+                                                                        Relids safe_upper_varnos);
 static void replace_vars_in_jointree(Node *jtnode,
                                                 pullup_replace_vars_context *context,
-                                                JoinExpr *lowest_outer_join);
+                                                JoinExpr *lowest_nulling_outer_join);
 static Node *pullup_replace_vars(Node *expr,
                                        pullup_replace_vars_context *context);
 static Node *pullup_replace_vars_callback(Var *var,
                                                         replace_rte_variables_context *context);
+static Query *pullup_replace_vars_subquery(Query *query,
+                                                        pullup_replace_vars_context *context);
+static Node *pull_up_subqueries_cleanup(Node *jtnode);
 static reduce_outer_joins_state *reduce_outer_joins_pass1(Node *jtnode);
 static void reduce_outer_joins_pass2(Node *jtnode,
                                                 reduce_outer_joins_state *state,
@@ -104,7 +124,7 @@ static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
  *
  * A clause "foo op ANY (sub-SELECT)" can be processed by pulling the
  * sub-SELECT up to become a rangetable entry and treating the implied
- * comparisons as quals of a semijoin. However, this optimization *only*
+ * comparisons as quals of a semijoin.  However, this optimization *only*
  * works at the top level of WHERE or a JOIN/ON clause, because we cannot
  * distinguish whether the ANY ought to return FALSE or NULL in cases
  * involving NULL inputs.  Also, in an outer join's ON clause we can only
@@ -121,9 +141,9 @@ static Node *find_jointree_node_for_rel(Node *jtnode, int relid);
  * transformations if any are found.
  *
  * This routine has to run before preprocess_expression(), so the quals
- * clauses are not yet reduced to implicit-AND format. That means we need
- * to recursively search through explicit AND clauses, which are
- * probably only binary ANDs.  We stop as soon as we hit a non-AND item.
+ * clauses are not yet reduced to implicit-AND format, and are not guaranteed
+ * to be AND/OR-flat either.  That means we need to recursively search through
+ * explicit AND clauses.  We stop as soon as we hit a non-AND item.
  */
 void
 pull_up_sublinks(PlannerInfo *root)
@@ -193,8 +213,9 @@ pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
                /* Set up a link representing the rebuilt jointree */
                jtlink = (Node *) newf;
                /* Now process qual --- all children are available for use */
-               newf->quals = pull_up_sublinks_qual_recurse(root, f->quals, frelids,
-                                                                                                       &jtlink);
+               newf->quals = pull_up_sublinks_qual_recurse(root, f->quals,
+                                                                                                       &jtlink, frelids,
+                                                                                                       NULL, NULL);
 
                /*
                 * Note that the result will be either newf, or a stack of JoinExprs
@@ -238,15 +259,6 @@ pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
                 * point of the available_rels machinations is to ensure that we only
                 * pull up quals for which that's okay.
                 *
-                * XXX for the moment, we refrain from pulling up IN/EXISTS clauses
-                * appearing in LEFT or RIGHT join conditions.  Although it is
-                * semantically valid to do so under the above conditions, we end up
-                * with a query in which the semijoin or antijoin must be evaluated
-                * below the outer join, which could perform far worse than leaving it
-                * as a sublink that is executed only for row pairs that meet the
-                * other join conditions.  Fixing this seems to require considerable
-                * restructuring of the executor, but maybe someday it can happen.
-                *
                 * We don't expect to see any pre-existing JOIN_SEMI or JOIN_ANTI
                 * nodes here.
                 */
@@ -254,26 +266,25 @@ pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
                {
                        case JOIN_INNER:
                                j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
+                                                                                                                &jtlink,
                                                                                                                 bms_union(leftrelids,
                                                                                                                                rightrelids),
-                                                                                                                &jtlink);
+                                                                                                                NULL, NULL);
                                break;
                        case JOIN_LEFT:
-#ifdef NOT_USED                                        /* see XXX comment above */
                                j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
+                                                                                                                &j->rarg,
                                                                                                                 rightrelids,
-                                                                                                                &j->rarg);
-#endif
+                                                                                                                NULL, NULL);
                                break;
                        case JOIN_FULL:
                                /* can't do anything with full-join quals */
                                break;
                        case JOIN_RIGHT:
-#ifdef NOT_USED                                        /* see XXX comment above */
                                j->quals = pull_up_sublinks_qual_recurse(root, j->quals,
+                                                                                                                &j->larg,
                                                                                                                 leftrelids,
-                                                                                                                &j->larg);
-#endif
+                                                                                                                NULL, NULL);
                                break;
                        default:
                                elog(ERROR, "unrecognized join type: %d",
@@ -284,7 +295,7 @@ pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
                /*
                 * Although we could include the pulled-up subqueries in the returned
                 * relids, there's no need since upper quals couldn't refer to their
-                * outputs anyway.      But we *do* need to include the join's own rtindex
+                * outputs anyway.  But we *do* need to include the join's own rtindex
                 * because we haven't yet collapsed join alias variables, so upper
                 * levels would mistakenly think they couldn't use references to this
                 * join.
@@ -303,14 +314,22 @@ pull_up_sublinks_jointree_recurse(PlannerInfo *root, Node *jtnode,
 /*
  * Recurse through top-level qual nodes for pull_up_sublinks()
  *
- * jtlink points to the link in the jointree where any new JoinExprs should be
- * inserted.  If we find multiple pull-up-able SubLinks, they'll get stacked
- * there in the order we encounter them.  We rely on subsequent optimization
- * to rearrange the stack if appropriate.
+ * jtlink1 points to the link in the jointree where any new JoinExprs should
+ * be inserted if they reference available_rels1 (i.e., available_rels1
+ * denotes the relations present underneath jtlink1).  Optionally, jtlink2 can
+ * point to a second link where new JoinExprs should be inserted if they
+ * reference available_rels2 (pass NULL for both those arguments if not used).
+ * Note that SubLinks referencing both sets of variables cannot be optimized.
+ * If we find multiple pull-up-able SubLinks, they'll get stacked onto jtlink1
+ * and/or jtlink2 in the order we encounter them.  We rely on subsequent
+ * optimization to rearrange the stack if appropriate.
+ *
+ * Returns the replacement qual node, or NULL if the qual should be removed.
  */
 static Node *
 pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
-                                                         Relids available_rels, Node **jtlink)
+                                                         Node **jtlink1, Relids available_rels1,
+                                                         Node **jtlink2, Relids available_rels2)
 {
        if (node == NULL)
                return NULL;
@@ -318,31 +337,114 @@ pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
        {
                SubLink    *sublink = (SubLink *) node;
                JoinExpr   *j;
+               Relids          child_rels;
 
                /* Is it a convertible ANY or EXISTS clause? */
                if (sublink->subLinkType == ANY_SUBLINK)
                {
-                       j = convert_ANY_sublink_to_join(root, sublink,
-                                                                                       available_rels);
-                       if (j)
+                       if ((j = convert_ANY_sublink_to_join(root, sublink,
+                                                                                                available_rels1)) != NULL)
                        {
-                               /* Yes, insert the new join node into the join tree */
-                               j->larg = *jtlink;
-                               *jtlink = (Node *) j;
-                               /* and return NULL representing constant TRUE */
+                               /* Yes; insert the new join node into the join tree */
+                               j->larg = *jtlink1;
+                               *jtlink1 = (Node *) j;
+                               /* Recursively process pulled-up jointree nodes */
+                               j->rarg = pull_up_sublinks_jointree_recurse(root,
+                                                                                                                       j->rarg,
+                                                                                                                       &child_rels);
+
+                               /*
+                                * Now recursively process the pulled-up quals.  Any inserted
+                                * joins can get stacked onto either j->larg or j->rarg,
+                                * depending on which rels they reference.
+                                */
+                               j->quals = pull_up_sublinks_qual_recurse(root,
+                                                                                                                j->quals,
+                                                                                                                &j->larg,
+                                                                                                                available_rels1,
+                                                                                                                &j->rarg,
+                                                                                                                child_rels);
+                               /* Return NULL representing constant TRUE */
+                               return NULL;
+                       }
+                       if (available_rels2 != NULL &&
+                               (j = convert_ANY_sublink_to_join(root, sublink,
+                                                                                                available_rels2)) != NULL)
+                       {
+                               /* Yes; insert the new join node into the join tree */
+                               j->larg = *jtlink2;
+                               *jtlink2 = (Node *) j;
+                               /* Recursively process pulled-up jointree nodes */
+                               j->rarg = pull_up_sublinks_jointree_recurse(root,
+                                                                                                                       j->rarg,
+                                                                                                                       &child_rels);
+
+                               /*
+                                * Now recursively process the pulled-up quals.  Any inserted
+                                * joins can get stacked onto either j->larg or j->rarg,
+                                * depending on which rels they reference.
+                                */
+                               j->quals = pull_up_sublinks_qual_recurse(root,
+                                                                                                                j->quals,
+                                                                                                                &j->larg,
+                                                                                                                available_rels2,
+                                                                                                                &j->rarg,
+                                                                                                                child_rels);
+                               /* Return NULL representing constant TRUE */
                                return NULL;
                        }
                }
                else if (sublink->subLinkType == EXISTS_SUBLINK)
                {
-                       j = convert_EXISTS_sublink_to_join(root, sublink, false,
-                                                                                          available_rels);
-                       if (j)
+                       if ((j = convert_EXISTS_sublink_to_join(root, sublink, false,
+                                                                                                       available_rels1)) != NULL)
                        {
-                               /* Yes, insert the new join node into the join tree */
-                               j->larg = *jtlink;
-                               *jtlink = (Node *) j;
-                               /* and return NULL representing constant TRUE */
+                               /* Yes; insert the new join node into the join tree */
+                               j->larg = *jtlink1;
+                               *jtlink1 = (Node *) j;
+                               /* Recursively process pulled-up jointree nodes */
+                               j->rarg = pull_up_sublinks_jointree_recurse(root,
+                                                                                                                       j->rarg,
+                                                                                                                       &child_rels);
+
+                               /*
+                                * Now recursively process the pulled-up quals.  Any inserted
+                                * joins can get stacked onto either j->larg or j->rarg,
+                                * depending on which rels they reference.
+                                */
+                               j->quals = pull_up_sublinks_qual_recurse(root,
+                                                                                                                j->quals,
+                                                                                                                &j->larg,
+                                                                                                                available_rels1,
+                                                                                                                &j->rarg,
+                                                                                                                child_rels);
+                               /* Return NULL representing constant TRUE */
+                               return NULL;
+                       }
+                       if (available_rels2 != NULL &&
+                               (j = convert_EXISTS_sublink_to_join(root, sublink, false,
+                                                                                                       available_rels2)) != NULL)
+                       {
+                               /* Yes; insert the new join node into the join tree */
+                               j->larg = *jtlink2;
+                               *jtlink2 = (Node *) j;
+                               /* Recursively process pulled-up jointree nodes */
+                               j->rarg = pull_up_sublinks_jointree_recurse(root,
+                                                                                                                       j->rarg,
+                                                                                                                       &child_rels);
+
+                               /*
+                                * Now recursively process the pulled-up quals.  Any inserted
+                                * joins can get stacked onto either j->larg or j->rarg,
+                                * depending on which rels they reference.
+                                */
+                               j->quals = pull_up_sublinks_qual_recurse(root,
+                                                                                                                j->quals,
+                                                                                                                &j->larg,
+                                                                                                                available_rels2,
+                                                                                                                &j->rarg,
+                                                                                                                child_rels);
+                               /* Return NULL representing constant TRUE */
                                return NULL;
                        }
                }
@@ -354,19 +456,61 @@ pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
                /* If the immediate argument of NOT is EXISTS, try to convert */
                SubLink    *sublink = (SubLink *) get_notclausearg((Expr *) node);
                JoinExpr   *j;
+               Relids          child_rels;
 
                if (sublink && IsA(sublink, SubLink))
                {
                        if (sublink->subLinkType == EXISTS_SUBLINK)
                        {
-                               j = convert_EXISTS_sublink_to_join(root, sublink, true,
-                                                                                                  available_rels);
-                               if (j)
+                               if ((j = convert_EXISTS_sublink_to_join(root, sublink, true,
+                                                                                                  available_rels1)) != NULL)
+                               {
+                                       /* Yes; insert the new join node into the join tree */
+                                       j->larg = *jtlink1;
+                                       *jtlink1 = (Node *) j;
+                                       /* Recursively process pulled-up jointree nodes */
+                                       j->rarg = pull_up_sublinks_jointree_recurse(root,
+                                                                                                                               j->rarg,
+                                                                                                                               &child_rels);
+
+                                       /*
+                                        * Now recursively process the pulled-up quals.  Because
+                                        * we are underneath a NOT, we can't pull up sublinks that
+                                        * reference the left-hand stuff, but it's still okay to
+                                        * pull up sublinks referencing j->rarg.
+                                        */
+                                       j->quals = pull_up_sublinks_qual_recurse(root,
+                                                                                                                        j->quals,
+                                                                                                                        &j->rarg,
+                                                                                                                        child_rels,
+                                                                                                                        NULL, NULL);
+                                       /* Return NULL representing constant TRUE */
+                                       return NULL;
+                               }
+                               if (available_rels2 != NULL &&
+                                       (j = convert_EXISTS_sublink_to_join(root, sublink, true,
+                                                                                                  available_rels2)) != NULL)
                                {
-                                       /* Yes, insert the new join node into the join tree */
-                                       j->larg = *jtlink;
-                                       *jtlink = (Node *) j;
-                                       /* and return NULL representing constant TRUE */
+                                       /* Yes; insert the new join node into the join tree */
+                                       j->larg = *jtlink2;
+                                       *jtlink2 = (Node *) j;
+                                       /* Recursively process pulled-up jointree nodes */
+                                       j->rarg = pull_up_sublinks_jointree_recurse(root,
+                                                                                                                               j->rarg,
+                                                                                                                               &child_rels);
+
+                                       /*
+                                        * Now recursively process the pulled-up quals.  Because
+                                        * we are underneath a NOT, we can't pull up sublinks that
+                                        * reference the left-hand stuff, but it's still okay to
+                                        * pull up sublinks referencing j->rarg.
+                                        */
+                                       j->quals = pull_up_sublinks_qual_recurse(root,
+                                                                                                                        j->quals,
+                                                                                                                        &j->rarg,
+                                                                                                                        child_rels,
+                                                                                                                        NULL, NULL);
+                                       /* Return NULL representing constant TRUE */
                                        return NULL;
                                }
                        }
@@ -387,8 +531,10 @@ pull_up_sublinks_qual_recurse(PlannerInfo *root, Node *node,
 
                        newclause = pull_up_sublinks_qual_recurse(root,
                                                                                                          oldclause,
-                                                                                                         available_rels,
-                                                                                                         jtlink);
+                                                                                                         jtlink1,
+                                                                                                         available_rels1,
+                                                                                                         jtlink2,
+                                                                                                         available_rels2);
                        if (newclause)
                                newclauses = lappend(newclauses, newclause);
                }
@@ -442,9 +588,7 @@ inline_set_returning_functions(PlannerInfo *root)
                                /* Successful expansion, replace the rtable entry */
                                rte->rtekind = RTE_SUBQUERY;
                                rte->subquery = funcquery;
-                               rte->funcexpr = NULL;
-                               rte->funccoltypes = NIL;
-                               rte->funccoltypmods = NIL;
+                               rte->functions = NIL;
                        }
                }
        }
@@ -457,37 +601,81 @@ inline_set_returning_functions(PlannerInfo *root)
  *             grouping/aggregation then we can merge it into the parent's jointree.
  *             Also, subqueries that are simple UNION ALL structures can be
  *             converted into "append relations".
+ */
+void
+pull_up_subqueries(PlannerInfo *root)
+{
+       /* Top level of jointree must always be a FromExpr */
+       Assert(IsA(root->parse->jointree, FromExpr));
+       /* Reset flag saying we need a deletion cleanup pass */
+       root->hasDeletedRTEs = false;
+       /* Recursion starts with no containing join nor appendrel */
+       root->parse->jointree = (FromExpr *)
+               pull_up_subqueries_recurse(root, (Node *) root->parse->jointree,
+                                                                  NULL, NULL, NULL, false);
+       /* Apply cleanup phase if necessary */
+       if (root->hasDeletedRTEs)
+               root->parse->jointree = (FromExpr *)
+                       pull_up_subqueries_cleanup((Node *) root->parse->jointree);
+       Assert(IsA(root->parse->jointree, FromExpr));
+}
+
+/*
+ * pull_up_subqueries_recurse
+ *             Recursive guts of pull_up_subqueries.
+ *
+ * This recursively processes the jointree and returns a modified jointree.
+ * Or, if it's valid to drop the current node from the jointree completely,
+ * it returns NULL.
+ *
+ * If this jointree node is within either side of an outer join, then
+ * lowest_outer_join references the lowest such JoinExpr node; otherwise
+ * it is NULL.  We use this to constrain the effects of LATERAL subqueries.
  *
  * If this jointree node is within the nullable side of an outer join, then
- * lowest_outer_join references the lowest such JoinExpr node; otherwise it
- * is NULL.  This forces use of the PlaceHolderVar mechanism for references
- * to non-nullable targetlist items, but only for references above that join.
+ * lowest_nulling_outer_join references the lowest such JoinExpr node;
+ * otherwise it is NULL.  This forces use of the PlaceHolderVar mechanism for
+ * references to non-nullable targetlist items, but only for references above
+ * that join.
  *
  * If we are looking at a member subquery of an append relation,
  * containing_appendrel describes that relation; else it is NULL.
  * This forces use of the PlaceHolderVar mechanism for all non-Var targetlist
  * items, and puts some additional restrictions on what can be pulled up.
  *
+ * deletion_ok is TRUE if the caller can cope with us returning NULL for a
+ * deletable leaf node (for example, a VALUES RTE that could be pulled up).
+ * If it's FALSE, we'll avoid pullup in such cases.
+ *
  * A tricky aspect of this code is that if we pull up a subquery we have
  * to replace Vars that reference the subquery's outputs throughout the
  * parent query, including quals attached to jointree nodes above the one
  * we are currently processing!  We handle this by being careful not to
- * change the jointree structure while recursing: no nodes other than
- * subquery RangeTblRef entries will be replaced.  Also, we can't turn
- * pullup_replace_vars loose on the whole jointree, because it'll return a
- * mutated copy of the tree; we have to invoke it just on the quals, instead.
- * This behavior is what makes it reasonable to pass lowest_outer_join as a
- * pointer rather than some more-indirect way of identifying the lowest OJ.
- * Likewise, we don't replace append_rel_list members but only their
+ * change the jointree structure while recursing: no nodes other than leaf
+ * RangeTblRef entries and entirely-empty FromExprs will be replaced or
+ * deleted.  Also, we can't turn pullup_replace_vars loose on the whole
+ * jointree, because it'll return a mutated copy of the tree; we have to
+ * invoke it just on the quals, instead.  This behavior is what makes it
+ * reasonable to pass lowest_outer_join and lowest_nulling_outer_join as
+ * pointers rather than some more-indirect way of identifying the lowest
+ * OJs.  Likewise, we don't replace append_rel_list members but only their
  * substructure, so the containing_appendrel reference is safe to use.
+ *
+ * Because of the rule that no jointree nodes with substructure can be
+ * replaced, we cannot fully handle the case of deleting nodes from the tree:
+ * when we delete one child of a JoinExpr, we need to replace the JoinExpr
+ * with a FromExpr, and that can't happen here.  Instead, we set the
+ * root->hasDeletedRTEs flag, which tells pull_up_subqueries() that an
+ * additional pass over the tree is needed to clean up.
  */
-Node *
-pull_up_subqueries(PlannerInfo *root, Node *jtnode,
-                                  JoinExpr *lowest_outer_join,
-                                  AppendRelInfo *containing_appendrel)
+static Node *
+pull_up_subqueries_recurse(PlannerInfo *root, Node *jtnode,
+                                                  JoinExpr *lowest_outer_join,
+                                                  JoinExpr *lowest_nulling_outer_join,
+                                                  AppendRelInfo *containing_appendrel,
+                                                  bool deletion_ok)
 {
-       if (jtnode == NULL)
-               return NULL;
+       Assert(jtnode != NULL);
        if (IsA(jtnode, RangeTblRef))
        {
                int                     varno = ((RangeTblRef *) jtnode)->rtindex;
@@ -501,12 +689,15 @@ pull_up_subqueries(PlannerInfo *root, Node *jtnode,
                 * unless is_safe_append_member says so.
                 */
                if (rte->rtekind == RTE_SUBQUERY &&
-                       is_simple_subquery(rte->subquery) &&
+                       is_simple_subquery(rte->subquery, rte,
+                                                          lowest_outer_join, deletion_ok) &&
                        (containing_appendrel == NULL ||
                         is_safe_append_member(rte->subquery)))
                        return pull_up_simple_subquery(root, jtnode, rte,
                                                                                   lowest_outer_join,
-                                                                                  containing_appendrel);
+                                                                                  lowest_nulling_outer_join,
+                                                                                  containing_appendrel,
+                                                                                  deletion_ok);
 
                /*
                 * Alternatively, is it a simple UNION ALL subquery?  If so, flatten
@@ -521,17 +712,68 @@ pull_up_subqueries(PlannerInfo *root, Node *jtnode,
                        is_simple_union_all(rte->subquery))
                        return pull_up_simple_union_all(root, jtnode, rte);
 
+               /*
+                * Or perhaps it's a simple VALUES RTE?
+                *
+                * We don't allow VALUES pullup below an outer join nor into an
+                * appendrel (such cases are impossible anyway at the moment).
+                */
+               if (rte->rtekind == RTE_VALUES &&
+                       lowest_outer_join == NULL &&
+                       containing_appendrel == NULL &&
+                       is_simple_values(root, rte, deletion_ok))
+                       return pull_up_simple_values(root, jtnode, rte);
+
                /* Otherwise, do nothing at this node. */
        }
        else if (IsA(jtnode, FromExpr))
        {
                FromExpr   *f = (FromExpr *) jtnode;
+               bool            have_undeleted_child = false;
                ListCell   *l;
 
                Assert(containing_appendrel == NULL);
+
+               /*
+                * If the FromExpr has quals, it's not deletable even if its parent
+                * would allow deletion.
+                */
+               if (f->quals)
+                       deletion_ok = false;
+
                foreach(l, f->fromlist)
-                       lfirst(l) = pull_up_subqueries(root, lfirst(l),
-                                                                                  lowest_outer_join, NULL);
+               {
+                       /*
+                        * In a non-deletable FromExpr, we can allow deletion of child
+                        * nodes so long as at least one child remains; so it's okay
+                        * either if any previous child survives, or if there's more to
+                        * come.  If all children are deletable in themselves, we'll force
+                        * the last one to remain unflattened.
+                        *
+                        * As a separate matter, we can allow deletion of all children of
+                        * the top-level FromExpr in a query, since that's a special case
+                        * anyway.
+                        */
+                       bool            sub_deletion_ok = (deletion_ok ||
+                                                                                  have_undeleted_child ||
+                                                                                  lnext(l) != NULL ||
+                                                                                  f == root->parse->jointree);
+
+                       lfirst(l) = pull_up_subqueries_recurse(root, lfirst(l),
+                                                                                                  lowest_outer_join,
+                                                                                                  lowest_nulling_outer_join,
+                                                                                                  NULL,
+                                                                                                  sub_deletion_ok);
+                       if (lfirst(l) != NULL)
+                               have_undeleted_child = true;
+               }
+
+               if (deletion_ok && !have_undeleted_child)
+               {
+                       /* OK to delete this FromExpr entirely */
+                       root->hasDeletedRTEs = true;            /* probably is set already */
+                       return NULL;
+               }
        }
        else if (IsA(jtnode, JoinExpr))
        {
@@ -542,30 +784,60 @@ pull_up_subqueries(PlannerInfo *root, Node *jtnode,
                switch (j->jointype)
                {
                        case JOIN_INNER:
-                               j->larg = pull_up_subqueries(root, j->larg,
-                                                                                        lowest_outer_join, NULL);
-                               j->rarg = pull_up_subqueries(root, j->rarg,
-                                                                                        lowest_outer_join, NULL);
+
+                               /*
+                                * INNER JOIN can allow deletion of either child node, but not
+                                * both.  So right child gets permission to delete only if
+                                * left child didn't get removed.
+                                */
+                               j->larg = pull_up_subqueries_recurse(root, j->larg,
+                                                                                                        lowest_outer_join,
+                                                                                                  lowest_nulling_outer_join,
+                                                                                                        NULL,
+                                                                                                        true);
+                               j->rarg = pull_up_subqueries_recurse(root, j->rarg,
+                                                                                                        lowest_outer_join,
+                                                                                                  lowest_nulling_outer_join,
+                                                                                                        NULL,
+                                                                                                        j->larg != NULL);
                                break;
                        case JOIN_LEFT:
                        case JOIN_SEMI:
                        case JOIN_ANTI:
-                               j->larg = pull_up_subqueries(root, j->larg,
-                                                                                        lowest_outer_join, NULL);
-                               j->rarg = pull_up_subqueries(root, j->rarg,
-                                                                                        j, NULL);
+                               j->larg = pull_up_subqueries_recurse(root, j->larg,
+                                                                                                        j,
+                                                                                                  lowest_nulling_outer_join,
+                                                                                                        NULL,
+                                                                                                        false);
+                               j->rarg = pull_up_subqueries_recurse(root, j->rarg,
+                                                                                                        j,
+                                                                                                        j,
+                                                                                                        NULL,
+                                                                                                        false);
                                break;
                        case JOIN_FULL:
-                               j->larg = pull_up_subqueries(root, j->larg,
-                                                                                        j, NULL);
-                               j->rarg = pull_up_subqueries(root, j->rarg,
-                                                                                        j, NULL);
+                               j->larg = pull_up_subqueries_recurse(root, j->larg,
+                                                                                                        j,
+                                                                                                        j,
+                                                                                                        NULL,
+                                                                                                        false);
+                               j->rarg = pull_up_subqueries_recurse(root, j->rarg,
+                                                                                                        j,
+                                                                                                        j,
+                                                                                                        NULL,
+                                                                                                        false);
                                break;
                        case JOIN_RIGHT:
-                               j->larg = pull_up_subqueries(root, j->larg,
-                                                                                        j, NULL);
-                               j->rarg = pull_up_subqueries(root, j->rarg,
-                                                                                        lowest_outer_join, NULL);
+                               j->larg = pull_up_subqueries_recurse(root, j->larg,
+                                                                                                        j,
+                                                                                                        j,
+                                                                                                        NULL,
+                                                                                                        false);
+                               j->rarg = pull_up_subqueries_recurse(root, j->rarg,
+                                                                                                        j,
+                                                                                                  lowest_nulling_outer_join,
+                                                                                                        NULL,
+                                                                                                        false);
                                break;
                        default:
                                elog(ERROR, "unrecognized join type: %d",
@@ -584,17 +856,19 @@ pull_up_subqueries(PlannerInfo *root, Node *jtnode,
  *             Attempt to pull up a single simple subquery.
  *
  * jtnode is a RangeTblRef that has been tentatively identified as a simple
- * subquery by pull_up_subqueries.     We return the replacement jointree node,
- * or jtnode itself if we determine that the subquery can't be pulled up after
- * all.
+ * subquery by pull_up_subqueries.  We return the replacement jointree node,
+ * or NULL if the subquery can be deleted entirely, or jtnode itself if we
+ * determine that the subquery can't be pulled up after all.
  *
  * rte is the RangeTblEntry referenced by jtnode.  Remaining parameters are
- * as for pull_up_subqueries.
+ * as for pull_up_subqueries_recurse.
  */
 static Node *
 pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
                                                JoinExpr *lowest_outer_join,
-                                               AppendRelInfo *containing_appendrel)
+                                               JoinExpr *lowest_nulling_outer_join,
+                                               AppendRelInfo *containing_appendrel,
+                                               bool deletion_ok)
 {
        Query      *parse = root->parse;
        int                     varno = ((RangeTblRef *) jtnode)->rtindex;
@@ -616,7 +890,7 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
         * Create a PlannerInfo data structure for this subquery.
         *
         * NOTE: the next few steps should match the first processing in
-        * subquery_planner().  Can we refactor to avoid code duplication, or
+        * subquery_planner().  Can we refactor to avoid code duplication, or
         * would that just make things uglier?
         */
        subroot = makeNode(PlannerInfo);
@@ -624,9 +898,12 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
        subroot->glob = root->glob;
        subroot->query_level = root->query_level;
        subroot->parent_root = root->parent_root;
+       subroot->plan_params = NIL;
+       subroot->outer_params = NULL;
        subroot->planner_cxt = CurrentMemoryContext;
        subroot->init_plans = NIL;
        subroot->cte_plan_ids = NIL;
+       subroot->multiexpr_params = NIL;
        subroot->eq_classes = NIL;
        subroot->append_rel_list = NIL;
        subroot->rowMarks = NIL;
@@ -654,23 +931,23 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
         * pull_up_subqueries' processing is complete for its jointree and
         * rangetable.
         *
-        * Note: we should pass NULL for containing-join info even if we are
-        * within an outer join in the upper query; the lower query starts with a
-        * clean slate for outer-join semantics.  Likewise, we say we aren't
-        * handling an appendrel member.
+        * Note: it's okay that the subquery's recursion starts with NULL for
+        * containing-join info, even if we are within an outer join in the upper
+        * query; the lower query starts with a clean slate for outer-join
+        * semantics.  Likewise, we needn't pass down appendrel state.
         */
-       subquery->jointree = (FromExpr *)
-               pull_up_subqueries(subroot, (Node *) subquery->jointree, NULL, NULL);
+       pull_up_subqueries(subroot);
 
        /*
         * Now we must recheck whether the subquery is still simple enough to pull
-        * up.  If not, abandon processing it.
+        * up.  If not, abandon processing it.
         *
         * We don't really need to recheck all the conditions involved, but it's
         * easier just to keep this "if" looking the same as the one in
-        * pull_up_subqueries.
+        * pull_up_subqueries_recurse.
         */
-       if (is_simple_subquery(subquery) &&
+       if (is_simple_subquery(subquery, rte,
+                                                  lowest_outer_join, deletion_ok) &&
                (containing_appendrel == NULL || is_safe_append_member(subquery)))
        {
                /* good to go */
@@ -681,13 +958,25 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
                 * Give up, return unmodified RangeTblRef.
                 *
                 * Note: The work we just did will be redone when the subquery gets
-                * planned on its own.  Perhaps we could avoid that by storing the
+                * planned on its own.  Perhaps we could avoid that by storing the
                 * modified subquery back into the rangetable, but I'm not gonna risk
                 * it now.
                 */
                return jtnode;
        }
 
+       /*
+        * We must flatten any join alias Vars in the subquery's targetlist,
+        * because pulling up the subquery's subqueries might have changed their
+        * expansions into arbitrary expressions, which could affect
+        * pullup_replace_vars' decisions about whether PlaceHolderVar wrappers
+        * are needed for tlist entries.  (Likely it'd be better to do
+        * flatten_join_alias_vars on the whole query tree at some earlier stage,
+        * maybe even in the rewriter; but for now let's just fix this case here.)
+        */
+       subquery->targetList = (List *)
+               flatten_join_alias_vars(subroot, (Node *) subquery->targetList);
+
        /*
         * Adjust level-0 varnos in subquery so that we can append its rangetable
         * to upper query's.  We have to fix the subquery's append_rel_list as
@@ -707,17 +996,22 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
        /*
         * The subquery's targetlist items are now in the appropriate form to
         * insert into the top query, but if we are under an outer join then
-        * non-nullable items may have to be turned into PlaceHolderVars.  If we
-        * are dealing with an appendrel member then anything that's not a simple
-        * Var has to be turned into a PlaceHolderVar.  Set up appropriate context
-        * data for pullup_replace_vars.
+        * non-nullable items and lateral references may have to be turned into
+        * PlaceHolderVars.  If we are dealing with an appendrel member then
+        * anything that's not a simple Var has to be turned into a
+        * PlaceHolderVar.  Set up required context data for pullup_replace_vars.
         */
        rvcontext.root = root;
        rvcontext.targetlist = subquery->targetList;
        rvcontext.target_rte = rte;
+       if (rte->lateral)
+               rvcontext.relids = get_relids_in_jointree((Node *) subquery->jointree,
+                                                                                                 true);
+       else    /* won't need relids */
+               rvcontext.relids = NULL;
        rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
        rvcontext.varno = varno;
-       rvcontext.need_phvs = (lowest_outer_join != NULL ||
+       rvcontext.need_phvs = (lowest_nulling_outer_join != NULL ||
                                                   containing_appendrel != NULL);
        rvcontext.wrap_non_vars = (containing_appendrel != NULL);
        /* initialize cache array with indexes 0 .. length(tlist) */
@@ -730,15 +1024,18 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
         * replace any of the jointree structure. (This'd be a lot cleaner if we
         * could use query_tree_mutator.)  We have to use PHVs in the targetList,
         * returningList, and havingQual, since those are certainly above any
-        * outer join.  replace_vars_in_jointree tracks its location in the
+        * outer join.  replace_vars_in_jointree tracks its location in the
         * jointree and uses PHVs or not appropriately.
         */
        parse->targetList = (List *)
                pullup_replace_vars((Node *) parse->targetList, &rvcontext);
        parse->returningList = (List *)
                pullup_replace_vars((Node *) parse->returningList, &rvcontext);
+       if (parse->onConflict)
+               parse->onConflict->onConflictSet = (List *)
+                       pullup_replace_vars((Node *) parse->onConflict->onConflictSet, &rvcontext);
        replace_vars_in_jointree((Node *) parse->jointree, &rvcontext,
-                                                        lowest_outer_join);
+                                                        lowest_nulling_outer_join);
        Assert(parse->setOperations == NULL);
        parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
 
@@ -765,10 +1062,10 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
         * Replace references in the joinaliasvars lists of join RTEs.
         *
         * You might think that we could avoid using PHVs for alias vars of joins
-        * below lowest_outer_join, but that doesn't work because the alias vars
-        * could be referenced above that join; we need the PHVs to be present in
-        * such references after the alias vars get flattened.  (It might be worth
-        * trying to be smarter here, someday.)
+        * below lowest_nulling_outer_join, but that doesn't work because the
+        * alias vars could be referenced above that join; we need the PHVs to be
+        * present in such references after the alias vars get flattened.  (It
+        * might be worth trying to be smarter here, someday.)
         */
        foreach(lc, parse->rtable)
        {
@@ -780,6 +1077,38 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
                                                                        &rvcontext);
        }
 
+       /*
+        * If the subquery had a LATERAL marker, propagate that to any of its
+        * child RTEs that could possibly now contain lateral cross-references.
+        * The children might or might not contain any actual lateral
+        * cross-references, but we have to mark the pulled-up child RTEs so that
+        * later planner stages will check for such.
+        */
+       if (rte->lateral)
+       {
+               foreach(lc, subquery->rtable)
+               {
+                       RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(lc);
+
+                       switch (child_rte->rtekind)
+                       {
+                               case RTE_RELATION:
+                                       if (child_rte->tablesample)
+                                               child_rte->lateral = true;
+                                       break;
+                               case RTE_SUBQUERY:
+                               case RTE_FUNCTION:
+                               case RTE_VALUES:
+                                       child_rte->lateral = true;
+                                       break;
+                               case RTE_JOIN:
+                               case RTE_CTE:
+                                       /* these can't contain any lateral references */
+                                       break;
+                       }
+               }
+       }
+
        /*
         * Now append the adjusted rtable entries to upper query. (We hold off
         * until after fixing the upper rtable entries; no point in running that
@@ -847,8 +1176,18 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
 
        /*
         * Return the adjusted subquery jointree to replace the RangeTblRef entry
-        * in parent's jointree.
+        * in parent's jointree; or, if we're flattening a subquery with empty
+        * FROM list, return NULL to signal deletion of the subquery from the
+        * parent jointree (and set hasDeletedRTEs to ensure cleanup later).
         */
+       if (subquery->jointree->fromlist == NIL)
+       {
+               Assert(deletion_ok);
+               Assert(subquery->jointree->quals == NULL);
+               root->hasDeletedRTEs = true;
+               return NULL;
+       }
+
        return (Node *) subquery->jointree;
 }
 
@@ -857,7 +1196,7 @@ pull_up_simple_subquery(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte,
  *             Pull up a single simple UNION ALL subquery.
  *
  * jtnode is a RangeTblRef that has been identified as a simple UNION ALL
- * subquery by pull_up_subqueries.     We pull up the leaf subqueries and
+ * subquery by pull_up_subqueries.  We pull up the leaf subqueries and
  * build an "append relation" for the union set.  The result value is just
  * jtnode, since we don't actually need to change the query jointree.
  */
@@ -866,20 +1205,45 @@ pull_up_simple_union_all(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
 {
        int                     varno = ((RangeTblRef *) jtnode)->rtindex;
        Query      *subquery = rte->subquery;
-       int                     rtoffset;
+       int                     rtoffset = list_length(root->parse->rtable);
        List       *rtable;
 
        /*
-        * Append child RTEs to parent rtable.
-        *
+        * Make a modifiable copy of the subquery's rtable, so we can adjust
+        * upper-level Vars in it.  There are no such Vars in the setOperations
+        * tree proper, so fixing the rtable should be sufficient.
+        */
+       rtable = copyObject(subquery->rtable);
+
+       /*
         * Upper-level vars in subquery are now one level closer to their parent
         * than before.  We don't have to worry about offsetting varnos, though,
-        * because any such vars must refer to stuff above the level of the query
-        * we are pulling into.
+        * because the UNION leaf queries can't cross-reference each other.
         */
-       rtoffset = list_length(root->parse->rtable);
-       rtable = copyObject(subquery->rtable);
        IncrementVarSublevelsUp_rtable(rtable, -1, 1);
+
+       /*
+        * If the UNION ALL subquery had a LATERAL marker, propagate that to all
+        * its children.  The individual children might or might not contain any
+        * actual lateral cross-references, but we have to mark the pulled-up
+        * child RTEs so that later planner stages will check for such.
+        */
+       if (rte->lateral)
+       {
+               ListCell   *rt;
+
+               foreach(rt, rtable)
+               {
+                       RangeTblEntry *child_rte = (RangeTblEntry *) lfirst(rt);
+
+                       Assert(child_rte->rtekind == RTE_SUBQUERY);
+                       child_rte->lateral = true;
+               }
+       }
+
+       /*
+        * Append child RTEs to parent rtable.
+        */
        root->parse->rtable = list_concat(root->parse->rtable, rtable);
 
        /*
@@ -950,11 +1314,15 @@ pull_up_union_leaf_queries(Node *setOp, PlannerInfo *root, int parentRTindex,
                 * must build the AppendRelInfo first, because this will modify it.)
                 * Note that we can pass NULL for containing-join info even if we're
                 * actually under an outer join, because the child's expressions
-                * aren't going to propagate up above the join.
+                * aren't going to propagate up to the join.  Also, we ignore the
+                * possibility that pull_up_subqueries_recurse() returns a different
+                * jointree node than what we pass it; if it does, the important thing
+                * is that it replaced the child relid in the AppendRelInfo node.
                 */
                rtr = makeNode(RangeTblRef);
                rtr->rtindex = childRTindex;
-               (void) pull_up_subqueries(root, (Node *) rtr, NULL, appinfo);
+               (void) pull_up_subqueries_recurse(root, (Node *) rtr,
+                                                                                 NULL, NULL, appinfo, false);
        }
        else if (IsA(setOp, SetOperationStmt))
        {
@@ -1004,17 +1372,24 @@ make_setop_translation_list(Query *query, Index newvarno,
  * is_simple_subquery
  *       Check a subquery in the range table to see if it's simple enough
  *       to pull up into the parent query.
+ *
+ * rte is the RTE_SUBQUERY RangeTblEntry that contained the subquery.
+ * (Note subquery is not necessarily equal to rte->subquery; it could be a
+ * processed copy of that.)
+ * lowest_outer_join is the lowest outer join above the subquery, or NULL.
+ * deletion_ok is TRUE if it'd be okay to delete the subquery entirely.
  */
 static bool
-is_simple_subquery(Query *subquery)
+is_simple_subquery(Query *subquery, RangeTblEntry *rte,
+                                  JoinExpr *lowest_outer_join,
+                                  bool deletion_ok)
 {
        /*
         * Let's just make sure it's a valid subselect ...
         */
        if (!IsA(subquery, Query) ||
                subquery->commandType != CMD_SELECT ||
-               subquery->utilityStmt != NULL ||
-               subquery->intoClause != NULL)
+               subquery->utilityStmt != NULL)
                elog(ERROR, "subquery is bogus");
 
        /*
@@ -1038,6 +1413,7 @@ is_simple_subquery(Query *subquery)
        if (subquery->hasAggs ||
                subquery->hasWindowFuncs ||
                subquery->groupClause ||
+               subquery->groupingSets ||
                subquery->havingQual ||
                subquery->sortClause ||
                subquery->distinctClause ||
@@ -1047,18 +1423,119 @@ is_simple_subquery(Query *subquery)
                subquery->cteList)
                return false;
 
+       /*
+        * Don't pull up if the RTE represents a security-barrier view; we
+        * couldn't prevent information leakage once the RTE's Vars are scattered
+        * about in the upper query.
+        */
+       if (rte->security_barrier)
+               return false;
+
+       /*
+        * Don't pull up a subquery with an empty jointree, unless it has no quals
+        * and deletion_ok is TRUE and we're not underneath an outer join.
+        *
+        * query_planner() will correctly generate a Result plan for a jointree
+        * that's totally empty, but we can't cope with an empty FromExpr
+        * appearing lower down in a jointree: we identify join rels via baserelid
+        * sets, so we couldn't distinguish a join containing such a FromExpr from
+        * one without it.  We can only handle such cases if the place where the
+        * subquery is linked is a FromExpr or inner JOIN that would still be
+        * nonempty after removal of the subquery, so that it's still identifiable
+        * via its contained baserelids.  Safe contexts are signaled by
+        * deletion_ok.
+        *
+        * But even in a safe context, we must keep the subquery if it has any
+        * quals, because it's unclear where to put them in the upper query.
+        *
+        * Also, we must forbid pullup if such a subquery is underneath an outer
+        * join, because then we might need to wrap its output columns with
+        * PlaceHolderVars, and the PHVs would then have empty relid sets meaning
+        * we couldn't tell where to evaluate them.  (This test is separate from
+        * the deletion_ok flag for possible future expansion: deletion_ok tells
+        * whether the immediate parent site in the jointree could cope, not
+        * whether we'd have PHV issues.  It's possible this restriction could be
+        * fixed by letting the PHVs use the relids of the parent jointree item,
+        * but that complication is for another day.)
+        *
+        * Note that deletion of a subquery is also dependent on the check below
+        * that its targetlist contains no set-returning functions.  Deletion from
+        * a FROM list or inner JOIN is okay only if the subquery must return
+        * exactly one row.
+        */
+       if (subquery->jointree->fromlist == NIL &&
+               (subquery->jointree->quals != NULL ||
+                !deletion_ok ||
+                lowest_outer_join != NULL))
+               return false;
+
+       /*
+        * If the subquery is LATERAL, check for pullup restrictions from that.
+        */
+       if (rte->lateral)
+       {
+               bool            restricted;
+               Relids          safe_upper_varnos;
+
+               /*
+                * The subquery's WHERE and JOIN/ON quals mustn't contain any lateral
+                * references to rels outside a higher outer join (including the case
+                * where the outer join is within the subquery itself).  In such a
+                * case, pulling up would result in a situation where we need to
+                * postpone quals from below an outer join to above it, which is
+                * probably completely wrong and in any case is a complication that
+                * doesn't seem worth addressing at the moment.
+                */
+               if (lowest_outer_join != NULL)
+               {
+                       restricted = true;
+                       safe_upper_varnos = get_relids_in_jointree((Node *) lowest_outer_join,
+                                                                                                          true);
+               }
+               else
+               {
+                       restricted = false;
+                       safe_upper_varnos = NULL;       /* doesn't matter */
+               }
+
+               if (jointree_contains_lateral_outer_refs((Node *) subquery->jointree,
+                                                                                         restricted, safe_upper_varnos))
+                       return false;
+
+               /*
+                * If there's an outer join above the LATERAL subquery, also disallow
+                * pullup if the subquery's targetlist has any references to rels
+                * outside the outer join, since these might get pulled into quals
+                * above the subquery (but in or below the outer join) and then lead
+                * to qual-postponement issues similar to the case checked for above.
+                * (We wouldn't need to prevent pullup if no such references appear in
+                * outer-query quals, but we don't have enough info here to check
+                * that.  Also, maybe this restriction could be removed if we forced
+                * such refs to be wrapped in PlaceHolderVars, even when they're below
+                * the nearest outer join?      But it's a pretty hokey usage, so not
+                * clear this is worth sweating over.)
+                */
+               if (lowest_outer_join != NULL)
+               {
+                       Relids          lvarnos = pull_varnos_of_level((Node *) subquery->targetList, 1);
+
+                       if (!bms_is_subset(lvarnos, safe_upper_varnos))
+                               return false;
+               }
+       }
+
        /*
         * Don't pull up a subquery that has any set-returning functions in its
-        * targetlist.  Otherwise we might well wind up inserting set-returning
+        * targetlist.  Otherwise we might well wind up inserting set-returning
         * functions into places where they mustn't go, such as quals of higher
-        * queries.
+        * queries.  This also ensures deletion of an empty jointree is valid.
         */
        if (expression_returns_set((Node *) subquery->targetList))
                return false;
 
        /*
         * Don't pull up a subquery that has any volatile functions in its
-        * targetlist.  Otherwise we might introduce multiple evaluations of these
+        * targetlist.  Otherwise we might introduce multiple evaluations of these
         * functions, if they get copied to multiple places in the upper query,
         * leading to surprising results.  (Note: the PlaceHolderVar mechanism
         * doesn't quite guarantee single evaluation; else we could pull up anyway
@@ -1067,18 +1544,162 @@ is_simple_subquery(Query *subquery)
        if (contain_volatile_functions((Node *) subquery->targetList))
                return false;
 
+       return true;
+}
+
+/*
+ * pull_up_simple_values
+ *             Pull up a single simple VALUES RTE.
+ *
+ * jtnode is a RangeTblRef that has been identified as a simple VALUES RTE
+ * by pull_up_subqueries.  We always return NULL indicating that the RTE
+ * can be deleted entirely (all failure cases should have been detected by
+ * is_simple_values()).
+ *
+ * rte is the RangeTblEntry referenced by jtnode.  Because of the limited
+ * possible usage of VALUES RTEs, we do not need the remaining parameters
+ * of pull_up_subqueries_recurse.
+ */
+static Node *
+pull_up_simple_values(PlannerInfo *root, Node *jtnode, RangeTblEntry *rte)
+{
+       Query      *parse = root->parse;
+       int                     varno = ((RangeTblRef *) jtnode)->rtindex;
+       List       *values_list;
+       List       *tlist;
+       AttrNumber      attrno;
+       pullup_replace_vars_context rvcontext;
+       ListCell   *lc;
+
+       Assert(rte->rtekind == RTE_VALUES);
+       Assert(list_length(rte->values_lists) == 1);
+
        /*
-        * Hack: don't try to pull up a subquery with an empty jointree.
-        * query_planner() will correctly generate a Result plan for a jointree
-        * that's totally empty, but I don't think the right things happen if an
-        * empty FromExpr appears lower down in a jointree.  It would pose a
-        * problem for the PlaceHolderVar mechanism too, since we'd have no way to
-        * identify where to evaluate a PHV coming out of the subquery. Not worth
-        * working hard on this, just to collapse SubqueryScan/Result into Result;
-        * especially since the SubqueryScan can often be optimized away by
-        * setrefs.c anyway.
+        * Need a modifiable copy of the VALUES list to hack on, just in case it's
+        * multiply referenced.
         */
-       if (subquery->jointree->fromlist == NIL)
+       values_list = (List *) copyObject(linitial(rte->values_lists));
+
+       /*
+        * The VALUES RTE can't contain any Vars of level zero, let alone any that
+        * are join aliases, so no need to flatten join alias Vars.
+        */
+       Assert(!contain_vars_of_level((Node *) values_list, 0));
+
+       /*
+        * Set up required context data for pullup_replace_vars.  In particular,
+        * we have to make the VALUES list look like a subquery targetlist.
+        */
+       tlist = NIL;
+       attrno = 1;
+       foreach(lc, values_list)
+       {
+               tlist = lappend(tlist,
+                                               makeTargetEntry((Expr *) lfirst(lc),
+                                                                               attrno,
+                                                                               NULL,
+                                                                               false));
+               attrno++;
+       }
+       rvcontext.root = root;
+       rvcontext.targetlist = tlist;
+       rvcontext.target_rte = rte;
+       rvcontext.relids = NULL;
+       rvcontext.outer_hasSubLinks = &parse->hasSubLinks;
+       rvcontext.varno = varno;
+       rvcontext.need_phvs = false;
+       rvcontext.wrap_non_vars = false;
+       /* initialize cache array with indexes 0 .. length(tlist) */
+       rvcontext.rv_cache = palloc0((list_length(tlist) + 1) *
+                                                                sizeof(Node *));
+
+       /*
+        * Replace all of the top query's references to the RTE's outputs with
+        * copies of the adjusted VALUES expressions, being careful not to replace
+        * any of the jointree structure. (This'd be a lot cleaner if we could use
+        * query_tree_mutator.)  Much of this should be no-ops in the dummy Query
+        * that surrounds a VALUES RTE, but it's not enough code to be worth
+        * removing.
+        */
+       parse->targetList = (List *)
+               pullup_replace_vars((Node *) parse->targetList, &rvcontext);
+       parse->returningList = (List *)
+               pullup_replace_vars((Node *) parse->returningList, &rvcontext);
+       if (parse->onConflict)
+               parse->onConflict->onConflictSet = (List *)
+                       pullup_replace_vars((Node *) parse->onConflict->onConflictSet, &rvcontext);
+       replace_vars_in_jointree((Node *) parse->jointree, &rvcontext, NULL);
+       Assert(parse->setOperations == NULL);
+       parse->havingQual = pullup_replace_vars(parse->havingQual, &rvcontext);
+
+       /*
+        * There should be no appendrels to fix, nor any join alias Vars, nor any
+        * outer joins and hence no PlaceHolderVars.
+        */
+       Assert(root->append_rel_list == NIL);
+       Assert(list_length(parse->rtable) == 1);
+       Assert(root->join_info_list == NIL);
+       Assert(root->placeholder_list == NIL);
+
+       /*
+        * Return NULL to signal deletion of the VALUES RTE from the parent
+        * jointree (and set hasDeletedRTEs to ensure cleanup later).
+        */
+       root->hasDeletedRTEs = true;
+       return NULL;
+}
+
+/*
+ * is_simple_values
+ *       Check a VALUES RTE in the range table to see if it's simple enough
+ *       to pull up into the parent query.
+ *
+ * rte is the RTE_VALUES RangeTblEntry to check.
+ * deletion_ok is TRUE if it'd be okay to delete the VALUES RTE entirely.
+ */
+static bool
+is_simple_values(PlannerInfo *root, RangeTblEntry *rte, bool deletion_ok)
+{
+       Assert(rte->rtekind == RTE_VALUES);
+
+       /*
+        * We can only pull up a VALUES RTE if deletion_ok is TRUE.  It's
+        * basically the same case as a sub-select with empty FROM list; see
+        * comments in is_simple_subquery().
+        */
+       if (!deletion_ok)
+               return false;
+
+       /*
+        * Also, there must be exactly one VALUES list, else it's not semantically
+        * correct to delete the VALUES RTE.
+        */
+       if (list_length(rte->values_lists) != 1)
+               return false;
+
+       /*
+        * Because VALUES can't appear under an outer join (or at least, we won't
+        * try to pull it up if it does), we need not worry about LATERAL, nor
+        * about validity of PHVs for the VALUES' outputs.
+        */
+
+       /*
+        * Don't pull up a VALUES that contains any set-returning or volatile
+        * functions.  Again, the considerations here are basically identical to
+        * restrictions on a subquery's targetlist.
+        */
+       if (expression_returns_set((Node *) rte->values_lists) ||
+               contain_volatile_functions((Node *) rte->values_lists))
+               return false;
+
+       /*
+        * Do not pull up a VALUES that's not the only RTE in its parent query.
+        * This is actually the only case that the parser will generate at the
+        * moment, and assuming this is true greatly simplifies
+        * pull_up_simple_values().
+        */
+       if (list_length(root->parse->rtable) != 1 ||
+               rte != (RangeTblEntry *) linitial(root->parse->rtable))
                return false;
 
        return true;
@@ -1100,8 +1721,7 @@ is_simple_union_all(Query *subquery)
        /* Let's just make sure it's a valid subselect ... */
        if (!IsA(subquery, Query) ||
                subquery->commandType != CMD_SELECT ||
-               subquery->utilityStmt != NULL ||
-               subquery->intoClause != NULL)
+               subquery->utilityStmt != NULL)
                elog(ERROR, "subquery is bogus");
 
        /* Is it a set-operation query at all? */
@@ -1135,7 +1755,7 @@ is_simple_union_all_recurse(Node *setOp, Query *setOpQuery, List *colTypes)
                Assert(subquery != NULL);
 
                /* Leaf nodes are OK if they match the toplevel column types */
-               /* We don't have to compare typmods here */
+               /* We don't have to compare typmods or collations here */
                return tlist_same_datatypes(subquery->targetList, colTypes, true);
        }
        else if (IsA(setOp, SetOperationStmt))
@@ -1194,24 +1814,147 @@ is_safe_append_member(Query *subquery)
        return true;
 }
 
+/*
+ * jointree_contains_lateral_outer_refs
+ *             Check for disallowed lateral references in a jointree's quals
+ *
+ * If restricted is false, all level-1 Vars are allowed (but we still must
+ * search the jointree, since it might contain outer joins below which there
+ * will be restrictions).  If restricted is true, return TRUE when any qual
+ * in the jointree contains level-1 Vars coming from outside the rels listed
+ * in safe_upper_varnos.
+ */
+static bool
+jointree_contains_lateral_outer_refs(Node *jtnode, bool restricted,
+                                                                        Relids safe_upper_varnos)
+{
+       if (jtnode == NULL)
+               return false;
+       if (IsA(jtnode, RangeTblRef))
+               return false;
+       else if (IsA(jtnode, FromExpr))
+       {
+               FromExpr   *f = (FromExpr *) jtnode;
+               ListCell   *l;
+
+               /* First, recurse to check child joins */
+               foreach(l, f->fromlist)
+               {
+                       if (jointree_contains_lateral_outer_refs(lfirst(l),
+                                                                                                        restricted,
+                                                                                                        safe_upper_varnos))
+                               return true;
+               }
+
+               /* Then check the top-level quals */
+               if (restricted &&
+                       !bms_is_subset(pull_varnos_of_level(f->quals, 1),
+                                                  safe_upper_varnos))
+                       return true;
+       }
+       else if (IsA(jtnode, JoinExpr))
+       {
+               JoinExpr   *j = (JoinExpr *) jtnode;
+
+               /*
+                * If this is an outer join, we mustn't allow any upper lateral
+                * references in or below it.
+                */
+               if (j->jointype != JOIN_INNER)
+               {
+                       restricted = true;
+                       safe_upper_varnos = NULL;
+               }
+
+               /* Check the child joins */
+               if (jointree_contains_lateral_outer_refs(j->larg,
+                                                                                                restricted,
+                                                                                                safe_upper_varnos))
+                       return true;
+               if (jointree_contains_lateral_outer_refs(j->rarg,
+                                                                                                restricted,
+                                                                                                safe_upper_varnos))
+                       return true;
+
+               /* Check the JOIN's qual clauses */
+               if (restricted &&
+                       !bms_is_subset(pull_varnos_of_level(j->quals, 1),
+                                                  safe_upper_varnos))
+                       return true;
+       }
+       else
+               elog(ERROR, "unrecognized node type: %d",
+                        (int) nodeTag(jtnode));
+       return false;
+}
+
 /*
  * Helper routine for pull_up_subqueries: do pullup_replace_vars on every
  * expression in the jointree, without changing the jointree structure itself.
  * Ugly, but there's no other way...
  *
- * If we are at or below lowest_outer_join, we can suppress use of
+ * If we are at or below lowest_nulling_outer_join, we can suppress use of
  * PlaceHolderVars wrapped around the replacement expressions.
  */
 static void
 replace_vars_in_jointree(Node *jtnode,
                                                 pullup_replace_vars_context *context,
-                                                JoinExpr *lowest_outer_join)
+                                                JoinExpr *lowest_nulling_outer_join)
 {
        if (jtnode == NULL)
                return;
        if (IsA(jtnode, RangeTblRef))
        {
-               /* nothing to do here */
+               /*
+                * If the RangeTblRef refers to a LATERAL subquery (that isn't the
+                * same subquery we're pulling up), it might contain references to the
+                * target subquery, which we must replace.  We drive this from the
+                * jointree scan, rather than a scan of the rtable, for a couple of
+                * reasons: we can avoid processing no-longer-referenced RTEs, and we
+                * can use the appropriate setting of need_phvs depending on whether
+                * the RTE is above possibly-nulling outer joins or not.
+                */
+               int                     varno = ((RangeTblRef *) jtnode)->rtindex;
+
+               if (varno != context->varno)    /* ignore target subquery itself */
+               {
+                       RangeTblEntry *rte = rt_fetch(varno, context->root->parse->rtable);
+
+                       Assert(rte != context->target_rte);
+                       if (rte->lateral)
+                       {
+                               switch (rte->rtekind)
+                               {
+                                       case RTE_RELATION:
+                                               /* shouldn't be marked LATERAL unless tablesample */
+                                               Assert(rte->tablesample);
+                                               rte->tablesample = (TableSampleClause *)
+                                                       pullup_replace_vars((Node *) rte->tablesample,
+                                                                                               context);
+                                               break;
+                                       case RTE_SUBQUERY:
+                                               rte->subquery =
+                                                       pullup_replace_vars_subquery(rte->subquery,
+                                                                                                                context);
+                                               break;
+                                       case RTE_FUNCTION:
+                                               rte->functions = (List *)
+                                                       pullup_replace_vars((Node *) rte->functions,
+                                                                                               context);
+                                               break;
+                                       case RTE_VALUES:
+                                               rte->values_lists = (List *)
+                                                       pullup_replace_vars((Node *) rte->values_lists,
+                                                                                               context);
+                                               break;
+                                       case RTE_JOIN:
+                                       case RTE_CTE:
+                                               /* these shouldn't be marked LATERAL */
+                                               Assert(false);
+                                               break;
+                               }
+                       }
+               }
        }
        else if (IsA(jtnode, FromExpr))
        {
@@ -1219,7 +1962,8 @@ replace_vars_in_jointree(Node *jtnode,
                ListCell   *l;
 
                foreach(l, f->fromlist)
-                       replace_vars_in_jointree(lfirst(l), context, lowest_outer_join);
+                       replace_vars_in_jointree(lfirst(l), context,
+                                                                        lowest_nulling_outer_join);
                f->quals = pullup_replace_vars(f->quals, context);
        }
        else if (IsA(jtnode, JoinExpr))
@@ -1227,14 +1971,14 @@ replace_vars_in_jointree(Node *jtnode,
                JoinExpr   *j = (JoinExpr *) jtnode;
                bool            save_need_phvs = context->need_phvs;
 
-               if (j == lowest_outer_join)
+               if (j == lowest_nulling_outer_join)
                {
                        /* no more PHVs in or below this join */
                        context->need_phvs = false;
-                       lowest_outer_join = NULL;
+                       lowest_nulling_outer_join = NULL;
                }
-               replace_vars_in_jointree(j->larg, context, lowest_outer_join);
-               replace_vars_in_jointree(j->rarg, context, lowest_outer_join);
+               replace_vars_in_jointree(j->larg, context, lowest_nulling_outer_join);
+               replace_vars_in_jointree(j->rarg, context, lowest_nulling_outer_join);
                j->quals = pullup_replace_vars(j->quals, context);
 
                /*
@@ -1331,7 +2075,7 @@ pullup_replace_vars_callback(Var *var,
                /*
                 * Insert PlaceHolderVar if needed.  Notice that we are wrapping one
                 * PlaceHolderVar around the whole RowExpr, rather than putting one
-                * around each element of the row.      This is because we need the
+                * around each element of the row.  This is because we need the
                 * expression to yield NULL, not ROW(NULL,NULL,...) when it is forced
                 * to null by an outer join.
                 */
@@ -1366,7 +2110,23 @@ pullup_replace_vars_callback(Var *var,
                        if (newnode && IsA(newnode, Var) &&
                                ((Var *) newnode)->varlevelsup == 0)
                        {
-                               /* Simple Vars always escape being wrapped */
+                               /*
+                                * Simple Vars always escape being wrapped, unless they are
+                                * lateral references to something outside the subquery being
+                                * pulled up.  (Even then, we could omit the PlaceHolderVar if
+                                * the referenced rel is under the same lowest outer join, but
+                                * it doesn't seem worth the trouble to check that.)
+                                */
+                               if (rcon->target_rte->lateral &&
+                                       !bms_is_member(((Var *) newnode)->varno, rcon->relids))
+                                       wrap = true;
+                               else
+                                       wrap = false;
+                       }
+                       else if (newnode && IsA(newnode, PlaceHolderVar) &&
+                                        ((PlaceHolderVar *) newnode)->phlevelsup == 0)
+                       {
+                               /* No need to wrap a PlaceHolderVar with another one, either */
                                wrap = false;
                        }
                        else if (rcon->wrap_non_vars)
@@ -1377,13 +2137,26 @@ pullup_replace_vars_callback(Var *var,
                        else
                        {
                                /*
-                                * If it contains a Var of current level, and does not contain
-                                * any non-strict constructs, then it's certainly nullable and
-                                * we don't need to insert a PlaceHolderVar.  (Note: in future
-                                * maybe we should insert PlaceHolderVars anyway, when a tlist
-                                * item is expensive to evaluate?
+                                * If it contains a Var of the subquery being pulled up, and
+                                * does not contain any non-strict constructs, then it's
+                                * certainly nullable so we don't need to insert a
+                                * PlaceHolderVar.
+                                *
+                                * This analysis could be tighter: in particular, a non-strict
+                                * construct hidden within a lower-level PlaceHolderVar is not
+                                * reason to add another PHV.  But for now it doesn't seem
+                                * worth the code to be more exact.
+                                *
+                                * Note: in future maybe we should insert a PlaceHolderVar
+                                * anyway, if the tlist item is expensive to evaluate?
+                                *
+                                * For a LATERAL subquery, we have to check the actual var
+                                * membership of the node, but if it's non-lateral then any
+                                * level-zero var must belong to the subquery.
                                 */
-                               if (contain_vars_of_level((Node *) newnode, 0) &&
+                               if ((rcon->target_rte->lateral ?
+                                  bms_overlap(pull_varnos((Node *) newnode), rcon->relids) :
+                                        contain_vars_of_level((Node *) newnode, 0)) &&
                                        !contain_nonstrict_functions((Node *) newnode))
                                {
                                        /* No wrap needed */
@@ -1404,7 +2177,7 @@ pullup_replace_vars_callback(Var *var,
 
                        /*
                         * Cache it if possible (ie, if the attno is in range, which it
-                        * probably always should be).  We can cache the value even if we
+                        * probably always should be).  We can cache the value even if we
                         * decided we didn't need a PHV, since this result will be
                         * suitable for any request that has need_phvs.
                         */
@@ -1421,6 +2194,84 @@ pullup_replace_vars_callback(Var *var,
        return newnode;
 }
 
+/*
+ * Apply pullup variable replacement to a subquery
+ *
+ * This needs to be different from pullup_replace_vars() because
+ * replace_rte_variables will think that it shouldn't increment sublevels_up
+ * before entering the Query; so we need to call it with sublevels_up == 1.
+ */
+static Query *
+pullup_replace_vars_subquery(Query *query,
+                                                        pullup_replace_vars_context *context)
+{
+       Assert(IsA(query, Query));
+       return (Query *) replace_rte_variables((Node *) query,
+                                                                                  context->varno, 1,
+                                                                                  pullup_replace_vars_callback,
+                                                                                  (void *) context,
+                                                                                  NULL);
+}
+
+/*
+ * pull_up_subqueries_cleanup
+ *             Recursively fix up jointree after deletion of some subqueries.
+ *
+ * The jointree now contains some NULL subtrees, which we need to get rid of.
+ * In a FromExpr, just rebuild the child-node list with null entries deleted.
+ * In an inner JOIN, replace the JoinExpr node with a one-child FromExpr.
+ */
+static Node *
+pull_up_subqueries_cleanup(Node *jtnode)
+{
+       Assert(jtnode != NULL);
+       if (IsA(jtnode, RangeTblRef))
+       {
+               /* Nothing to do at leaf nodes. */
+       }
+       else if (IsA(jtnode, FromExpr))
+       {
+               FromExpr   *f = (FromExpr *) jtnode;
+               List       *newfrom = NIL;
+               ListCell   *l;
+
+               foreach(l, f->fromlist)
+               {
+                       Node       *child = (Node *) lfirst(l);
+
+                       if (child == NULL)
+                               continue;
+                       child = pull_up_subqueries_cleanup(child);
+                       newfrom = lappend(newfrom, child);
+               }
+               f->fromlist = newfrom;
+       }
+       else if (IsA(jtnode, JoinExpr))
+       {
+               JoinExpr   *j = (JoinExpr *) jtnode;
+
+               if (j->larg)
+                       j->larg = pull_up_subqueries_cleanup(j->larg);
+               if (j->rarg)
+                       j->rarg = pull_up_subqueries_cleanup(j->rarg);
+               if (j->larg == NULL)
+               {
+                       Assert(j->jointype == JOIN_INNER);
+                       Assert(j->rarg != NULL);
+                       return (Node *) makeFromExpr(list_make1(j->rarg), j->quals);
+               }
+               else if (j->rarg == NULL)
+               {
+                       Assert(j->jointype == JOIN_INNER);
+                       return (Node *) makeFromExpr(list_make1(j->larg), j->quals);
+               }
+       }
+       else
+               elog(ERROR, "unrecognized node type: %d",
+                        (int) nodeTag(jtnode));
+       return jtnode;
+}
+
 
 /*
  * flatten_simple_union_all
@@ -1477,10 +2328,10 @@ flatten_simple_union_all(PlannerInfo *root)
 
        /*
         * Make a copy of the leftmost RTE and add it to the rtable.  This copy
-        * will represent the leftmost leaf query in its capacity as a member
-        * of the appendrel.  The original will represent the appendrel as a
-        * whole.  (We must do things this way because the upper query's Vars
-        * have to be seen as referring to the whole appendrel.)
+        * will represent the leftmost leaf query in its capacity as a member of
+        * the appendrel.  The original will represent the appendrel as a whole.
+        * (We must do things this way because the upper query's Vars have to be
+        * seen as referring to the whole appendrel.)
         */
        childRTE = copyObject(leftmostRTE);
        parse->rtable = lappend(parse->rtable, childRTE);
@@ -1502,8 +2353,8 @@ flatten_simple_union_all(PlannerInfo *root)
        parse->jointree->fromlist = list_make1(rtr);
 
        /*
-        * Now pretend the query has no setops.  We must do this before trying
-        * to do subquery pullup, because of Assert in pull_up_simple_subquery.
+        * Now pretend the query has no setops.  We must do this before trying to
+        * do subquery pullup, because of Assert in pull_up_simple_subquery.
         */
        parse->setOperations = NULL;
 
@@ -1540,7 +2391,7 @@ flatten_simple_union_all(PlannerInfo *root)
  *             SELECT ... FROM a LEFT JOIN b ON (a.x = b.y) WHERE b.y IS NULL;
  * If the join clause is strict for b.y, then only null-extended rows could
  * pass the upper WHERE, and we can conclude that what the query is really
- * specifying is an anti-semijoin.     We change the join type from JOIN_LEFT
+ * specifying is an anti-semijoin.  We change the join type from JOIN_LEFT
  * to JOIN_ANTI.  The IS NULL clause then becomes redundant, and must be
  * removed to prevent bogus selectivity calculations, but we leave it to
  * distribute_qual_to_rels to get rid of such clauses.
@@ -1780,7 +2631,7 @@ reduce_outer_joins_pass2(Node *jtnode,
                /*
                 * See if we can reduce JOIN_LEFT to JOIN_ANTI.  This is the case if
                 * the join's own quals are strict for any var that was forced null by
-                * higher qual levels.  NOTE: there are other ways that we could
+                * higher qual levels.  NOTE: there are other ways that we could
                 * detect an anti-join, in particular if we were to check whether Vars
                 * coming from the RHS must be non-null because of table constraints.
                 * That seems complicated and expensive though (in particular, one
@@ -1840,6 +2691,11 @@ reduce_outer_joins_pass2(Node *jtnode,
                         * is that we pass either the local or the upper constraints,
                         * never both, to the children of an outer join.
                         *
+                        * Note that a SEMI join works like an inner join here: it's okay
+                        * to pass down both local and upper constraints.  (There can't be
+                        * any upper constraints affecting its inner side, but it's not
+                        * worth having a separate code path to avoid passing them.)
+                        *
                         * At a FULL join we just punt and pass nothing down --- is it
                         * possible to be smarter?
                         */
@@ -1849,7 +2705,7 @@ reduce_outer_joins_pass2(Node *jtnode,
                                if (!computed_local_nonnullable_vars)
                                        local_nonnullable_vars = find_nonnullable_vars(j->quals);
                                local_forced_null_vars = find_forced_null_vars(j->quals);
-                               if (jointype == JOIN_INNER)
+                               if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
                                {
                                        /* OK to merge upper and local constraints */
                                        local_nonnullable_rels = bms_add_members(local_nonnullable_rels,
@@ -1869,14 +2725,14 @@ reduce_outer_joins_pass2(Node *jtnode,
 
                        if (left_state->contains_outer)
                        {
-                               if (jointype == JOIN_INNER)
+                               if (jointype == JOIN_INNER || jointype == JOIN_SEMI)
                                {
                                        /* pass union of local and upper constraints */
                                        pass_nonnullable_rels = local_nonnullable_rels;
                                        pass_nonnullable_vars = local_nonnullable_vars;
                                        pass_forced_null_vars = local_forced_null_vars;
                                }
-                               else if (jointype != JOIN_FULL) /* ie, LEFT/SEMI/ANTI */
+                               else if (jointype != JOIN_FULL) /* ie, LEFT or ANTI */
                                {
                                        /* can't pass local constraints to non-nullable side */
                                        pass_nonnullable_rels = nonnullable_rels;
@@ -1931,11 +2787,9 @@ reduce_outer_joins_pass2(Node *jtnode,
  *
  * Find any PlaceHolderVar nodes in the given tree that reference the
  * pulled-up relid, and change them to reference the replacement relid(s).
- * We do not need to recurse into subqueries, since no subquery of the current
- * top query could (yet) contain such a reference.
  *
  * NOTE: although this has the form of a walker, we cheat and modify the
- * nodes in-place.     This should be OK since the tree was copied by
+ * nodes in-place.  This should be OK since the tree was copied by
  * pullup_replace_vars earlier.  Avoid scribbling on the original values of
  * the bitmapsets, though, because expression_tree_mutator doesn't copy those.
  */
@@ -1943,6 +2797,7 @@ reduce_outer_joins_pass2(Node *jtnode,
 typedef struct
 {
        int                     varno;
+       int                     sublevels_up;
        Relids          subrelids;
 } substitute_multiple_relids_context;
 
@@ -1956,7 +2811,8 @@ substitute_multiple_relids_walker(Node *node,
        {
                PlaceHolderVar *phv = (PlaceHolderVar *) node;
 
-               if (bms_is_member(context->varno, phv->phrels))
+               if (phv->phlevelsup == context->sublevels_up &&
+                       bms_is_member(context->varno, phv->phrels))
                {
                        phv->phrels = bms_union(phv->phrels,
                                                                        context->subrelids);
@@ -1965,6 +2821,18 @@ substitute_multiple_relids_walker(Node *node,
                }
                /* fall through to examine children */
        }
+       if (IsA(node, Query))
+       {
+               /* Recurse into subselects */
+               bool            result;
+
+               context->sublevels_up++;
+               result = query_tree_walker((Query *) node,
+                                                                  substitute_multiple_relids_walker,
+                                                                  (void *) context, 0);
+               context->sublevels_up--;
+               return result;
+       }
        /* Shouldn't need to handle planner auxiliary nodes here */
        Assert(!IsA(node, SpecialJoinInfo));
        Assert(!IsA(node, AppendRelInfo));
@@ -1981,6 +2849,7 @@ substitute_multiple_relids(Node *node, int varno, Relids subrelids)
        substitute_multiple_relids_context context;
 
        context.varno = varno;
+       context.sublevels_up = 0;
        context.subrelids = subrelids;
 
        /*