]> granicus.if.org Git - postgresql/blobdiff - src/backend/optimizer/plan/setrefs.c
Add a Gather executor node.
[postgresql] / src / backend / optimizer / plan / setrefs.c
index 83447082f5b39cb6660c78c21b46c9b54877e726..b1cede2ef0d20e3c5d984c09797e9c8fc11c70ba 100644 (file)
@@ -4,12 +4,12 @@
  *       Post-processing of a completed plan tree: fix references to subplan
  *       vars, compute regproc values for operators, etc
  *
- * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/setrefs.c,v 1.147 2008/12/28 18:53:57 tgl Exp $
+ *       src/backend/optimizer/plan/setrefs.c
  *
  *-------------------------------------------------------------------------
  */
 #include "catalog/pg_type.h"
 #include "nodes/makefuncs.h"
 #include "nodes/nodeFuncs.h"
-#include "optimizer/clauses.h"
+#include "optimizer/pathnode.h"
 #include "optimizer/planmain.h"
+#include "optimizer/planner.h"
 #include "optimizer/tlist.h"
-#include "parser/parsetree.h"
+#include "tcop/utility.h"
 #include "utils/lsyscache.h"
 #include "utils/syscache.h"
 
@@ -40,19 +41,18 @@ typedef struct
        int                     num_vars;               /* number of plain Var tlist entries */
        bool            has_ph_vars;    /* are there PlaceHolderVar entries? */
        bool            has_non_vars;   /* are there other entries? */
-       /* array of num_vars entries: */
-       tlist_vinfo vars[1];            /* VARIABLE LENGTH ARRAY */
-} indexed_tlist;                               /* VARIABLE LENGTH STRUCT */
+       tlist_vinfo vars[FLEXIBLE_ARRAY_MEMBER];        /* has num_vars entries */
+} indexed_tlist;
 
 typedef struct
 {
-       PlannerGlobal *glob;
+       PlannerInfo *root;
        int                     rtoffset;
 } fix_scan_expr_context;
 
 typedef struct
 {
-       PlannerGlobal *glob;
+       PlannerInfo *root;
        indexed_tlist *outer_itlist;
        indexed_tlist *inner_itlist;
        Index           acceptable_rel;
@@ -61,8 +61,9 @@ typedef struct
 
 typedef struct
 {
-       PlannerGlobal *glob;
+       PlannerInfo *root;
        indexed_tlist *subplan_itlist;
+       Index           newvarno;
        int                     rtoffset;
 } fix_upper_expr_context;
 
@@ -77,21 +78,32 @@ typedef struct
        (((con)->consttype == REGCLASSOID || (con)->consttype == OIDOID) && \
         !(con)->constisnull)
 
-#define fix_scan_list(glob, lst, rtoffset) \
-       ((List *) fix_scan_expr(glob, (Node *) (lst), rtoffset))
+#define fix_scan_list(root, lst, rtoffset) \
+       ((List *) fix_scan_expr(root, (Node *) (lst), rtoffset))
 
-static Plan *set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset);
-static Plan *set_subqueryscan_references(PlannerGlobal *glob,
+static void add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing);
+static void flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte);
+static bool flatten_rtes_walker(Node *node, PlannerGlobal *glob);
+static void add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte);
+static Plan *set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset);
+static Plan *set_indexonlyscan_references(PlannerInfo *root,
+                                                        IndexOnlyScan *plan,
+                                                        int rtoffset);
+static Plan *set_subqueryscan_references(PlannerInfo *root,
                                                        SubqueryScan *plan,
                                                        int rtoffset);
 static bool trivial_subqueryscan(SubqueryScan *plan);
-static Node *fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset);
+static void set_foreignscan_references(PlannerInfo *root,
+                                                  ForeignScan *fscan,
+                                                  int rtoffset);
+static void set_customscan_references(PlannerInfo *root,
+                                                 CustomScan *cscan,
+                                                 int rtoffset);
+static Node *fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset);
 static Node *fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context);
 static bool fix_scan_expr_walker(Node *node, fix_scan_expr_context *context);
-static void set_join_references(PlannerGlobal *glob, Join *join, int rtoffset);
-static void set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
-                                                 indexed_tlist *outer_itlist);
-static void set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset);
+static void set_join_references(PlannerInfo *root, Join *join, int rtoffset);
+static void set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset);
 static void set_dummy_tlist_references(Plan *plan, int rtoffset);
 static indexed_tlist *build_tlist_index(List *tlist);
 static Var *search_indexed_tlist_for_var(Var *var,
@@ -101,23 +113,32 @@ static Var *search_indexed_tlist_for_var(Var *var,
 static Var *search_indexed_tlist_for_non_var(Node *node,
                                                                 indexed_tlist *itlist,
                                                                 Index newvarno);
-static List *fix_join_expr(PlannerGlobal *glob,
+static Var *search_indexed_tlist_for_sortgroupref(Node *node,
+                                                                         Index sortgroupref,
+                                                                         indexed_tlist *itlist,
+                                                                         Index newvarno);
+static List *fix_join_expr(PlannerInfo *root,
                          List *clauses,
                          indexed_tlist *outer_itlist,
                          indexed_tlist *inner_itlist,
                          Index acceptable_rel, int rtoffset);
 static Node *fix_join_expr_mutator(Node *node,
                                          fix_join_expr_context *context);
-static Node *fix_upper_expr(PlannerGlobal *glob,
+static Node *fix_upper_expr(PlannerInfo *root,
                           Node *node,
                           indexed_tlist *subplan_itlist,
+                          Index newvarno,
                           int rtoffset);
 static Node *fix_upper_expr_mutator(Node *node,
                                           fix_upper_expr_context *context);
+static List *set_returning_clause_references(PlannerInfo *root,
+                                                               List *rlist,
+                                                               Plan *topplan,
+                                                               Index resultRelation,
+                                                               int rtoffset);
 static bool fix_opfuncids_walker(Node *node, void *context);
 static bool extract_query_dependencies_walker(Node *node,
-                                                                                         PlannerGlobal *context);
-
+                                                                 PlannerInfo *context);
 
 /*****************************************************************************
  *
@@ -128,7 +149,7 @@ static bool extract_query_dependencies_walker(Node *node,
 /*
  * set_plan_references
  *
- * This is the final processing pass of the planner/optimizer. The plan
+ * This is the final processing pass of the planner/optimizer.  The plan
  * tree is complete; we just have to adjust some representational details
  * for the convenience of the executor:
  *
@@ -140,16 +161,21 @@ static bool extract_query_dependencies_walker(Node *node,
  * 3. We adjust Vars in upper plan nodes to refer to the outputs of their
  * subplans.
  *
- * 4. We compute regproc OIDs for operators (ie, we look up the function
+ * 4. PARAM_MULTIEXPR Params are replaced by regular PARAM_EXEC Params,
+ * now that we have finished planning all MULTIEXPR subplans.
+ *
+ * 5. We compute regproc OIDs for operators (ie, we look up the function
  * that implements each op).
  *
- * 5. We create lists of specific objects that the plan depends on.
+ * 6. We create lists of specific objects that the plan depends on.
  * This will be used by plancache.c to drive invalidation of cached plans.
  * Relation dependencies are represented by OIDs, and everything else by
  * PlanInvalItems (this distinction is motivated by the shared-inval APIs).
  * Currently, relations and user-defined functions are the only types of
  * objects that are explicitly tracked this way.
  *
+ * 7. We assign every plan node in the tree a unique ID.
+ *
  * We also perform one final optimization step, which is to delete
  * SubqueryScan plan nodes that aren't doing anything useful (ie, have
  * no qual and a no-op targetlist).  The reason for doing this last is that
@@ -162,88 +188,259 @@ static bool extract_query_dependencies_walker(Node *node,
  *
  * set_plan_references recursively traverses the whole plan tree.
  *
- * Inputs:
- *     glob: global data for planner run
- *     plan: the topmost node of the plan
- *     rtable: the rangetable for the current subquery
- *
  * The return value is normally the same Plan node passed in, but can be
  * different when the passed-in Plan is a SubqueryScan we decide isn't needed.
  *
- * The flattened rangetable entries are appended to glob->finalrtable, and
- * plan dependencies are appended to glob->relationOids (for relations)
- * and glob->invalItems (for everything else).
+ * The flattened rangetable entries are appended to root->glob->finalrtable.
+ * Also, rowmarks entries are appended to root->glob->finalrowmarks, and the
+ * RT indexes of ModifyTable result relations to root->glob->resultRelations.
+ * Plan dependencies are appended to root->glob->relationOids (for relations)
+ * and root->glob->invalItems (for everything else).
  *
  * Notice that we modify Plan nodes in-place, but use expression_tree_mutator
- * to process targetlist and qual expressions. We can assume that the Plan
+ * to process targetlist and qual expressions.  We can assume that the Plan
  * nodes were just built by the planner and are not multiply referenced, but
  * it's not so safe to assume that for expression tree nodes.
  */
 Plan *
-set_plan_references(PlannerGlobal *glob, Plan *plan, List *rtable)
+set_plan_references(PlannerInfo *root, Plan *plan)
 {
+       PlannerGlobal *glob = root->glob;
        int                     rtoffset = list_length(glob->finalrtable);
        ListCell   *lc;
 
        /*
-        * In the flat rangetable, we zero out substructure pointers that are not
-        * needed by the executor; this reduces the storage space and copying cost
-        * for cached plans.  We keep only the alias and eref Alias fields, which
-        * are needed by EXPLAIN.
+        * Add all the query's RTEs to the flattened rangetable.  The live ones
+        * will have their rangetable indexes increased by rtoffset.  (Additional
+        * RTEs, not referenced by the Plan tree, might get added after those.)
+        */
+       add_rtes_to_flat_rtable(root, false);
+
+       /*
+        * Adjust RT indexes of PlanRowMarks and add to final rowmarks list
         */
-       foreach(lc, rtable)
+       foreach(lc, root->rowMarks)
        {
-               RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
-               RangeTblEntry *newrte;
+               PlanRowMark *rc = (PlanRowMark *) lfirst(lc);
+               PlanRowMark *newrc;
+
+               Assert(IsA(rc, PlanRowMark));
+
+               /* flat copy is enough since all fields are scalars */
+               newrc = (PlanRowMark *) palloc(sizeof(PlanRowMark));
+               memcpy(newrc, rc, sizeof(PlanRowMark));
+
+               /* adjust indexes ... but *not* the rowmarkId */
+               newrc->rti += rtoffset;
+               newrc->prti += rtoffset;
+
+               glob->finalrowmarks = lappend(glob->finalrowmarks, newrc);
+       }
+
+       /* Now fix the Plan tree */
+       return set_plan_refs(root, plan, rtoffset);
+}
+
+/*
+ * Extract RangeTblEntries from the plan's rangetable, and add to flat rtable
+ *
+ * This can recurse into subquery plans; "recursing" is true if so.
+ */
+static void
+add_rtes_to_flat_rtable(PlannerInfo *root, bool recursing)
+{
+       PlannerGlobal *glob = root->glob;
+       Index           rti;
+       ListCell   *lc;
 
-               /* flat copy to duplicate all the scalar fields */
-               newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
-               memcpy(newrte, rte, sizeof(RangeTblEntry));
+       /*
+        * Add the query's own RTEs to the flattened rangetable.
+        *
+        * At top level, we must add all RTEs so that their indexes in the
+        * flattened rangetable match up with their original indexes.  When
+        * recursing, we only care about extracting relation RTEs.
+        */
+       foreach(lc, root->parse->rtable)
+       {
+               RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
 
-               /* zap unneeded sub-structure */
-               newrte->subquery = NULL;
-               newrte->joinaliasvars = NIL;
-               newrte->funcexpr = NULL;
-               newrte->funccoltypes = NIL;
-               newrte->funccoltypmods = NIL;
-               newrte->values_lists = NIL;
-               newrte->ctecoltypes = NIL;
-               newrte->ctecoltypmods = NIL;
+               if (!recursing || rte->rtekind == RTE_RELATION)
+                       add_rte_to_flat_rtable(glob, rte);
+       }
 
-               glob->finalrtable = lappend(glob->finalrtable, newrte);
+       /*
+        * If there are any dead subqueries, they are not referenced in the Plan
+        * tree, so we must add RTEs contained in them to the flattened rtable
+        * separately.  (If we failed to do this, the executor would not perform
+        * expected permission checks for tables mentioned in such subqueries.)
+        *
+        * Note: this pass over the rangetable can't be combined with the previous
+        * one, because that would mess up the numbering of the live RTEs in the
+        * flattened rangetable.
+        */
+       rti = 1;
+       foreach(lc, root->parse->rtable)
+       {
+               RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
 
                /*
-                * If it's a plain relation RTE, add the table to relationOids.
-                *
-                * We do this even though the RTE might be unreferenced in the plan
-                * tree; this would correspond to cases such as views that were
-                * expanded, child tables that were eliminated by constraint
-                * exclusion, etc.      Schema invalidation on such a rel must still force
-                * rebuilding of the plan.
-                *
-                * Note we don't bother to avoid duplicate list entries.  We could,
-                * but it would probably cost more cycles than it would save.
+                * We should ignore inheritance-parent RTEs: their contents have been
+                * pulled up into our rangetable already.  Also ignore any subquery
+                * RTEs without matching RelOptInfos, as they likewise have been
+                * pulled up.
                 */
-               if (newrte->rtekind == RTE_RELATION)
-                       glob->relationOids = lappend_oid(glob->relationOids,
-                                                                                        newrte->relid);
+               if (rte->rtekind == RTE_SUBQUERY && !rte->inh &&
+                       rti < root->simple_rel_array_size)
+               {
+                       RelOptInfo *rel = root->simple_rel_array[rti];
+
+                       if (rel != NULL)
+                       {
+                               Assert(rel->relid == rti);              /* sanity check on array */
+
+                               /*
+                                * The subquery might never have been planned at all, if it
+                                * was excluded on the basis of self-contradictory constraints
+                                * in our query level.  In this case apply
+                                * flatten_unplanned_rtes.
+                                *
+                                * If it was planned but the plan is dummy, we assume that it
+                                * has been omitted from our plan tree (see
+                                * set_subquery_pathlist), and recurse to pull up its RTEs.
+                                *
+                                * Otherwise, it should be represented by a SubqueryScan node
+                                * somewhere in our plan tree, and we'll pull up its RTEs when
+                                * we process that plan node.
+                                *
+                                * However, if we're recursing, then we should pull up RTEs
+                                * whether the subplan is dummy or not, because we've found
+                                * that some upper query level is treating this one as dummy,
+                                * and so we won't scan this level's plan tree at all.
+                                */
+                               if (rel->subplan == NULL)
+                                       flatten_unplanned_rtes(glob, rte);
+                               else if (recursing || is_dummy_plan(rel->subplan))
+                               {
+                                       Assert(rel->subroot != NULL);
+                                       add_rtes_to_flat_rtable(rel->subroot, true);
+                               }
+                       }
+               }
+               rti++;
+       }
+}
+
+/*
+ * Extract RangeTblEntries from a subquery that was never planned at all
+ */
+static void
+flatten_unplanned_rtes(PlannerGlobal *glob, RangeTblEntry *rte)
+{
+       /* Use query_tree_walker to find all RTEs in the parse tree */
+       (void) query_tree_walker(rte->subquery,
+                                                        flatten_rtes_walker,
+                                                        (void *) glob,
+                                                        QTW_EXAMINE_RTES);
+}
+
+static bool
+flatten_rtes_walker(Node *node, PlannerGlobal *glob)
+{
+       if (node == NULL)
+               return false;
+       if (IsA(node, RangeTblEntry))
+       {
+               RangeTblEntry *rte = (RangeTblEntry *) node;
+
+               /* As above, we need only save relation RTEs */
+               if (rte->rtekind == RTE_RELATION)
+                       add_rte_to_flat_rtable(glob, rte);
+               return false;
+       }
+       if (IsA(node, Query))
+       {
+               /* Recurse into subselects */
+               return query_tree_walker((Query *) node,
+                                                                flatten_rtes_walker,
+                                                                (void *) glob,
+                                                                QTW_EXAMINE_RTES);
        }
+       return expression_tree_walker(node, flatten_rtes_walker,
+                                                                 (void *) glob);
+}
 
-       /* Now fix the Plan tree */
-       return set_plan_refs(glob, plan, rtoffset);
+/*
+ * Add (a copy of) the given RTE to the final rangetable
+ *
+ * In the flat rangetable, we zero out substructure pointers that are not
+ * needed by the executor; this reduces the storage space and copying cost
+ * for cached plans.  We keep only the ctename, alias and eref Alias fields,
+ * which are needed by EXPLAIN, and the selectedCols, insertedCols and
+ * updatedCols bitmaps, which are needed for executor-startup permissions
+ * checking and for trigger event checking.
+ */
+static void
+add_rte_to_flat_rtable(PlannerGlobal *glob, RangeTblEntry *rte)
+{
+       RangeTblEntry *newrte;
+
+       /* flat copy to duplicate all the scalar fields */
+       newrte = (RangeTblEntry *) palloc(sizeof(RangeTblEntry));
+       memcpy(newrte, rte, sizeof(RangeTblEntry));
+
+       /* zap unneeded sub-structure */
+       newrte->tablesample = NULL;
+       newrte->subquery = NULL;
+       newrte->joinaliasvars = NIL;
+       newrte->functions = NIL;
+       newrte->values_lists = NIL;
+       newrte->values_collations = NIL;
+       newrte->ctecoltypes = NIL;
+       newrte->ctecoltypmods = NIL;
+       newrte->ctecolcollations = NIL;
+       newrte->securityQuals = NIL;
+
+       glob->finalrtable = lappend(glob->finalrtable, newrte);
+
+       /*
+        * Check for RT index overflow; it's very unlikely, but if it did happen,
+        * the executor would get confused by varnos that match the special varno
+        * values.
+        */
+       if (IS_SPECIAL_VARNO(list_length(glob->finalrtable)))
+               ereport(ERROR,
+                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                errmsg("too many range table entries")));
+
+       /*
+        * If it's a plain relation RTE, add the table to relationOids.
+        *
+        * We do this even though the RTE might be unreferenced in the plan tree;
+        * this would correspond to cases such as views that were expanded, child
+        * tables that were eliminated by constraint exclusion, etc. Schema
+        * invalidation on such a rel must still force rebuilding of the plan.
+        *
+        * Note we don't bother to avoid making duplicate list entries.  We could,
+        * but it would probably cost more cycles than it would save.
+        */
+       if (newrte->rtekind == RTE_RELATION)
+               glob->relationOids = lappend_oid(glob->relationOids, newrte->relid);
 }
 
 /*
  * set_plan_refs: recurse through the Plan nodes of a single subquery level
  */
 static Plan *
-set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
+set_plan_refs(PlannerInfo *root, Plan *plan, int rtoffset)
 {
        ListCell   *l;
 
        if (plan == NULL)
                return NULL;
 
+       /* Assign this node a unique ID. */
+       plan->plan_node_id = root->glob->lastPlanNodeId++;
+
        /*
         * Plan-type-specific fixes
         */
@@ -255,9 +452,22 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
 
                                splan->scanrelid += rtoffset;
                                splan->plan.targetlist =
-                                       fix_scan_list(glob, splan->plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->plan.targetlist, rtoffset);
                                splan->plan.qual =
-                                       fix_scan_list(glob, splan->plan.qual, rtoffset);
+                                       fix_scan_list(root, splan->plan.qual, rtoffset);
+                       }
+                       break;
+               case T_SampleScan:
+                       {
+                               SampleScan *splan = (SampleScan *) plan;
+
+                               splan->scan.scanrelid += rtoffset;
+                               splan->scan.plan.targetlist =
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
+                               splan->scan.plan.qual =
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
+                               splan->tablesample = (TableSampleClause *)
+                                       fix_scan_expr(root, (Node *) splan->tablesample, rtoffset);
                        }
                        break;
                case T_IndexScan:
@@ -266,13 +476,24 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
-                                       fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
                                splan->scan.plan.qual =
-                                       fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
                                splan->indexqual =
-                                       fix_scan_list(glob, splan->indexqual, rtoffset);
+                                       fix_scan_list(root, splan->indexqual, rtoffset);
                                splan->indexqualorig =
-                                       fix_scan_list(glob, splan->indexqualorig, rtoffset);
+                                       fix_scan_list(root, splan->indexqualorig, rtoffset);
+                               splan->indexorderby =
+                                       fix_scan_list(root, splan->indexorderby, rtoffset);
+                               splan->indexorderbyorig =
+                                       fix_scan_list(root, splan->indexorderbyorig, rtoffset);
+                       }
+                       break;
+               case T_IndexOnlyScan:
+                       {
+                               IndexOnlyScan *splan = (IndexOnlyScan *) plan;
+
+                               return set_indexonlyscan_references(root, splan, rtoffset);
                        }
                        break;
                case T_BitmapIndexScan:
@@ -284,9 +505,9 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                                Assert(splan->scan.plan.targetlist == NIL);
                                Assert(splan->scan.plan.qual == NIL);
                                splan->indexqual =
-                                       fix_scan_list(glob, splan->indexqual, rtoffset);
+                                       fix_scan_list(root, splan->indexqual, rtoffset);
                                splan->indexqualorig =
-                                       fix_scan_list(glob, splan->indexqualorig, rtoffset);
+                                       fix_scan_list(root, splan->indexqualorig, rtoffset);
                        }
                        break;
                case T_BitmapHeapScan:
@@ -295,11 +516,11 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
-                                       fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
                                splan->scan.plan.qual =
-                                       fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
                                splan->bitmapqualorig =
-                                       fix_scan_list(glob, splan->bitmapqualorig, rtoffset);
+                                       fix_scan_list(root, splan->bitmapqualorig, rtoffset);
                        }
                        break;
                case T_TidScan:
@@ -308,16 +529,16 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
-                                       fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
                                splan->scan.plan.qual =
-                                       fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
                                splan->tidquals =
-                                       fix_scan_list(glob, splan->tidquals, rtoffset);
+                                       fix_scan_list(root, splan->tidquals, rtoffset);
                        }
                        break;
                case T_SubqueryScan:
                        /* Needs special treatment, see comments below */
-                       return set_subqueryscan_references(glob,
+                       return set_subqueryscan_references(root,
                                                                                           (SubqueryScan *) plan,
                                                                                           rtoffset);
                case T_FunctionScan:
@@ -326,11 +547,11 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
-                                       fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
                                splan->scan.plan.qual =
-                                       fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
-                               splan->funcexpr =
-                                       fix_scan_expr(glob, splan->funcexpr, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
+                               splan->functions =
+                                       fix_scan_list(root, splan->functions, rtoffset);
                        }
                        break;
                case T_ValuesScan:
@@ -339,22 +560,22 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
-                                       fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
                                splan->scan.plan.qual =
-                                       fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
                                splan->values_lists =
-                                       fix_scan_list(glob, splan->values_lists, rtoffset);
+                                       fix_scan_list(root, splan->values_lists, rtoffset);
                        }
                        break;
                case T_CteScan:
                        {
-                               CteScan *splan = (CteScan *) plan;
+                               CteScan    *splan = (CteScan *) plan;
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
-                                       fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
                                splan->scan.plan.qual =
-                                       fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
                        }
                        break;
                case T_WorkTableScan:
@@ -363,15 +584,22 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
 
                                splan->scan.scanrelid += rtoffset;
                                splan->scan.plan.targetlist =
-                                       fix_scan_list(glob, splan->scan.plan.targetlist, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.targetlist, rtoffset);
                                splan->scan.plan.qual =
-                                       fix_scan_list(glob, splan->scan.plan.qual, rtoffset);
+                                       fix_scan_list(root, splan->scan.plan.qual, rtoffset);
                        }
                        break;
+               case T_ForeignScan:
+                       set_foreignscan_references(root, (ForeignScan *) plan, rtoffset);
+                       break;
+               case T_CustomScan:
+                       set_customscan_references(root, (CustomScan *) plan, rtoffset);
+                       break;
+
                case T_NestLoop:
                case T_MergeJoin:
                case T_HashJoin:
-                       set_join_references(glob, (Join *) plan, rtoffset);
+                       set_join_references(root, (Join *) plan, rtoffset);
                        break;
 
                case T_Hash:
@@ -379,11 +607,12 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                case T_Sort:
                case T_Unique:
                case T_SetOp:
+               case T_Gather:
 
                        /*
                         * These plan types don't actually bother to evaluate their
                         * targetlists, because they just return their unmodified input
-                        * tuples.      Even though the targetlist won't be used by the
+                        * tuples.  Even though the targetlist won't be used by the
                         * executor, we fix it up for possible use by EXPLAIN (not to
                         * mention ease of debugging --- wrong varnos are very confusing).
                         */
@@ -395,6 +624,27 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                         */
                        Assert(plan->qual == NIL);
                        break;
+               case T_LockRows:
+                       {
+                               LockRows   *splan = (LockRows *) plan;
+
+                               /*
+                                * Like the plan types above, LockRows doesn't evaluate its
+                                * tlist or quals.  But we have to fix up the RT indexes in
+                                * its rowmarks.
+                                */
+                               set_dummy_tlist_references(plan, rtoffset);
+                               Assert(splan->plan.qual == NIL);
+
+                               foreach(l, splan->rowMarks)
+                               {
+                                       PlanRowMark *rc = (PlanRowMark *) lfirst(l);
+
+                                       rc->rti += rtoffset;
+                                       rc->prti += rtoffset;
+                               }
+                       }
+                       break;
                case T_Limit:
                        {
                                Limit      *splan = (Limit *) plan;
@@ -409,15 +659,33 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                                Assert(splan->plan.qual == NIL);
 
                                splan->limitOffset =
-                                       fix_scan_expr(glob, splan->limitOffset, rtoffset);
+                                       fix_scan_expr(root, splan->limitOffset, rtoffset);
                                splan->limitCount =
-                                       fix_scan_expr(glob, splan->limitCount, rtoffset);
+                                       fix_scan_expr(root, splan->limitCount, rtoffset);
                        }
                        break;
                case T_Agg:
-               case T_WindowAgg:
+                       set_upper_references(root, plan, rtoffset);
+                       break;
                case T_Group:
-                       set_upper_references(glob, plan, rtoffset);
+                       set_upper_references(root, plan, rtoffset);
+                       break;
+               case T_WindowAgg:
+                       {
+                               WindowAgg  *wplan = (WindowAgg *) plan;
+
+                               set_upper_references(root, plan, rtoffset);
+
+                               /*
+                                * Like Limit node limit/offset expressions, WindowAgg has
+                                * frame offset expressions, which cannot contain subplan
+                                * variable refs, so fix_scan_expr works for them.
+                                */
+                               wplan->startOffset =
+                                       fix_scan_expr(root, wplan->startOffset, rtoffset);
+                               wplan->endOffset =
+                                       fix_scan_expr(root, wplan->endOffset, rtoffset);
+                       }
                        break;
                case T_Result:
                        {
@@ -428,17 +696,133 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                                 * like a scan node than an upper node.
                                 */
                                if (splan->plan.lefttree != NULL)
-                                       set_upper_references(glob, plan, rtoffset);
+                                       set_upper_references(root, plan, rtoffset);
                                else
                                {
                                        splan->plan.targetlist =
-                                               fix_scan_list(glob, splan->plan.targetlist, rtoffset);
+                                               fix_scan_list(root, splan->plan.targetlist, rtoffset);
                                        splan->plan.qual =
-                                               fix_scan_list(glob, splan->plan.qual, rtoffset);
+                                               fix_scan_list(root, splan->plan.qual, rtoffset);
                                }
                                /* resconstantqual can't contain any subplan variable refs */
                                splan->resconstantqual =
-                                       fix_scan_expr(glob, splan->resconstantqual, rtoffset);
+                                       fix_scan_expr(root, splan->resconstantqual, rtoffset);
+                       }
+                       break;
+               case T_ModifyTable:
+                       {
+                               ModifyTable *splan = (ModifyTable *) plan;
+
+                               Assert(splan->plan.targetlist == NIL);
+                               Assert(splan->plan.qual == NIL);
+
+                               splan->withCheckOptionLists =
+                                       fix_scan_list(root, splan->withCheckOptionLists, rtoffset);
+
+                               if (splan->returningLists)
+                               {
+                                       List       *newRL = NIL;
+                                       ListCell   *lcrl,
+                                                          *lcrr,
+                                                          *lcp;
+
+                                       /*
+                                        * Pass each per-subplan returningList through
+                                        * set_returning_clause_references().
+                                        */
+                                       Assert(list_length(splan->returningLists) == list_length(splan->resultRelations));
+                                       Assert(list_length(splan->returningLists) == list_length(splan->plans));
+                                       forthree(lcrl, splan->returningLists,
+                                                        lcrr, splan->resultRelations,
+                                                        lcp, splan->plans)
+                                       {
+                                               List       *rlist = (List *) lfirst(lcrl);
+                                               Index           resultrel = lfirst_int(lcrr);
+                                               Plan       *subplan = (Plan *) lfirst(lcp);
+
+                                               rlist = set_returning_clause_references(root,
+                                                                                                                               rlist,
+                                                                                                                               subplan,
+                                                                                                                               resultrel,
+                                                                                                                               rtoffset);
+                                               newRL = lappend(newRL, rlist);
+                                       }
+                                       splan->returningLists = newRL;
+
+                                       /*
+                                        * Set up the visible plan targetlist as being the same as
+                                        * the first RETURNING list. This is for the use of
+                                        * EXPLAIN; the executor won't pay any attention to the
+                                        * targetlist.  We postpone this step until here so that
+                                        * we don't have to do set_returning_clause_references()
+                                        * twice on identical targetlists.
+                                        */
+                                       splan->plan.targetlist = copyObject(linitial(newRL));
+                               }
+
+                               /*
+                                * We treat ModifyTable with ON CONFLICT as a form of 'pseudo
+                                * join', where the inner side is the EXCLUDED tuple.
+                                * Therefore use fix_join_expr to setup the relevant variables
+                                * to INNER_VAR. We explicitly don't create any OUTER_VARs as
+                                * those are already used by RETURNING and it seems better to
+                                * be non-conflicting.
+                                */
+                               if (splan->onConflictSet)
+                               {
+                                       indexed_tlist *itlist;
+
+                                       itlist = build_tlist_index(splan->exclRelTlist);
+
+                                       splan->onConflictSet =
+                                               fix_join_expr(root, splan->onConflictSet,
+                                                                         NULL, itlist,
+                                                                         linitial_int(splan->resultRelations),
+                                                                         rtoffset);
+
+                                       splan->onConflictWhere = (Node *)
+                                               fix_join_expr(root, (List *) splan->onConflictWhere,
+                                                                         NULL, itlist,
+                                                                         linitial_int(splan->resultRelations),
+                                                                         rtoffset);
+
+                                       pfree(itlist);
+
+                                       splan->exclRelTlist =
+                                               fix_scan_list(root, splan->exclRelTlist, rtoffset);
+                               }
+
+                               splan->nominalRelation += rtoffset;
+                               splan->exclRelRTI += rtoffset;
+
+                               foreach(l, splan->resultRelations)
+                               {
+                                       lfirst_int(l) += rtoffset;
+                               }
+                               foreach(l, splan->rowMarks)
+                               {
+                                       PlanRowMark *rc = (PlanRowMark *) lfirst(l);
+
+                                       rc->rti += rtoffset;
+                                       rc->prti += rtoffset;
+                               }
+                               foreach(l, splan->plans)
+                               {
+                                       lfirst(l) = set_plan_refs(root,
+                                                                                         (Plan *) lfirst(l),
+                                                                                         rtoffset);
+                               }
+
+                               /*
+                                * Append this ModifyTable node's final result relation RT
+                                * index(es) to the global list for the plan, and set its
+                                * resultRelIndex to reflect their starting position in the
+                                * global list.
+                                */
+                               splan->resultRelIndex = list_length(root->glob->resultRelations);
+                               root->glob->resultRelations =
+                                       list_concat(root->glob->resultRelations,
+                                                               list_copy(splan->resultRelations));
                        }
                        break;
                case T_Append:
@@ -453,7 +837,25 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                                Assert(splan->plan.qual == NIL);
                                foreach(l, splan->appendplans)
                                {
-                                       lfirst(l) = set_plan_refs(glob,
+                                       lfirst(l) = set_plan_refs(root,
+                                                                                         (Plan *) lfirst(l),
+                                                                                         rtoffset);
+                               }
+                       }
+                       break;
+               case T_MergeAppend:
+                       {
+                               MergeAppend *splan = (MergeAppend *) plan;
+
+                               /*
+                                * MergeAppend, like Sort et al, doesn't actually evaluate its
+                                * targetlist or check quals.
+                                */
+                               set_dummy_tlist_references(plan, rtoffset);
+                               Assert(splan->plan.qual == NIL);
+                               foreach(l, splan->mergeplans)
+                               {
+                                       lfirst(l) = set_plan_refs(root,
                                                                                          (Plan *) lfirst(l),
                                                                                          rtoffset);
                                }
@@ -473,7 +875,7 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                                Assert(splan->plan.qual == NIL);
                                foreach(l, splan->bitmapplans)
                                {
-                                       lfirst(l) = set_plan_refs(glob,
+                                       lfirst(l) = set_plan_refs(root,
                                                                                          (Plan *) lfirst(l),
                                                                                          rtoffset);
                                }
@@ -488,7 +890,7 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
                                Assert(splan->plan.qual == NIL);
                                foreach(l, splan->bitmapplans)
                                {
-                                       lfirst(l) = set_plan_refs(glob,
+                                       lfirst(l) = set_plan_refs(root,
                                                                                          (Plan *) lfirst(l),
                                                                                          rtoffset);
                                }
@@ -508,12 +910,55 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
         * reference-adjustments bottom-up, then we would fail to match this
         * plan's var nodes against the already-modified nodes of the children.
         */
-       plan->lefttree = set_plan_refs(glob, plan->lefttree, rtoffset);
-       plan->righttree = set_plan_refs(glob, plan->righttree, rtoffset);
+       plan->lefttree = set_plan_refs(root, plan->lefttree, rtoffset);
+       plan->righttree = set_plan_refs(root, plan->righttree, rtoffset);
 
        return plan;
 }
 
+/*
+ * set_indexonlyscan_references
+ *             Do set_plan_references processing on an IndexOnlyScan
+ *
+ * This is unlike the handling of a plain IndexScan because we have to
+ * convert Vars referencing the heap into Vars referencing the index.
+ * We can use the fix_upper_expr machinery for that, by working from a
+ * targetlist describing the index columns.
+ */
+static Plan *
+set_indexonlyscan_references(PlannerInfo *root,
+                                                        IndexOnlyScan *plan,
+                                                        int rtoffset)
+{
+       indexed_tlist *index_itlist;
+
+       index_itlist = build_tlist_index(plan->indextlist);
+
+       plan->scan.scanrelid += rtoffset;
+       plan->scan.plan.targetlist = (List *)
+               fix_upper_expr(root,
+                                          (Node *) plan->scan.plan.targetlist,
+                                          index_itlist,
+                                          INDEX_VAR,
+                                          rtoffset);
+       plan->scan.plan.qual = (List *)
+               fix_upper_expr(root,
+                                          (Node *) plan->scan.plan.qual,
+                                          index_itlist,
+                                          INDEX_VAR,
+                                          rtoffset);
+       /* indexqual is already transformed to reference index columns */
+       plan->indexqual = fix_scan_list(root, plan->indexqual, rtoffset);
+       /* indexorderby is already transformed to reference index columns */
+       plan->indexorderby = fix_scan_list(root, plan->indexorderby, rtoffset);
+       /* indextlist must NOT be transformed to reference index columns */
+       plan->indextlist = fix_scan_list(root, plan->indextlist, rtoffset);
+
+       pfree(index_itlist);
+
+       return (Plan *) plan;
+}
+
 /*
  * set_subqueryscan_references
  *             Do set_plan_references processing on a SubqueryScan
@@ -522,17 +967,19 @@ set_plan_refs(PlannerGlobal *glob, Plan *plan, int rtoffset)
  * to do the normal processing on it.
  */
 static Plan *
-set_subqueryscan_references(PlannerGlobal *glob,
+set_subqueryscan_references(PlannerInfo *root,
                                                        SubqueryScan *plan,
                                                        int rtoffset)
 {
+       RelOptInfo *rel;
        Plan       *result;
 
-       /* First, recursively process the subplan */
-       plan->subplan = set_plan_references(glob, plan->subplan, plan->subrtable);
+       /* Need to look up the subquery's RelOptInfo, since we need its subroot */
+       rel = find_base_rel(root, plan->scan.scanrelid);
+       Assert(rel->subplan == plan->subplan);
 
-       /* subrtable is no longer needed in the plan tree */
-       plan->subrtable = NIL;
+       /* Recursively process the subplan */
+       plan->subplan = set_plan_references(rel->subroot, plan->subplan);
 
        if (trivial_subqueryscan(plan))
        {
@@ -567,7 +1014,7 @@ set_subqueryscan_references(PlannerGlobal *glob,
        else
        {
                /*
-                * Keep the SubqueryScan node.  We have to do the processing that
+                * Keep the SubqueryScan node.  We have to do the processing that
                 * set_plan_references would otherwise have done on it.  Notice we do
                 * not do set_upper_references() here, because a SubqueryScan will
                 * always have been created with correct references to its subplan's
@@ -575,9 +1022,9 @@ set_subqueryscan_references(PlannerGlobal *glob,
                 */
                plan->scan.scanrelid += rtoffset;
                plan->scan.plan.targetlist =
-                       fix_scan_list(glob, plan->scan.plan.targetlist, rtoffset);
+                       fix_scan_list(root, plan->scan.plan.targetlist, rtoffset);
                plan->scan.plan.qual =
-                       fix_scan_list(glob, plan->scan.plan.qual, rtoffset);
+                       fix_scan_list(root, plan->scan.plan.qual, rtoffset);
 
                result = (Plan *) plan;
        }
@@ -643,6 +1090,142 @@ trivial_subqueryscan(SubqueryScan *plan)
        return true;
 }
 
+/*
+ * set_foreignscan_references
+ *        Do set_plan_references processing on a ForeignScan
+ */
+static void
+set_foreignscan_references(PlannerInfo *root,
+                                                  ForeignScan *fscan,
+                                                  int rtoffset)
+{
+       /* Adjust scanrelid if it's valid */
+       if (fscan->scan.scanrelid > 0)
+               fscan->scan.scanrelid += rtoffset;
+
+       if (fscan->fdw_scan_tlist != NIL || fscan->scan.scanrelid == 0)
+       {
+               /* Adjust tlist, qual, fdw_exprs to reference foreign scan tuple */
+               indexed_tlist *itlist = build_tlist_index(fscan->fdw_scan_tlist);
+
+               fscan->scan.plan.targetlist = (List *)
+                       fix_upper_expr(root,
+                                                  (Node *) fscan->scan.plan.targetlist,
+                                                  itlist,
+                                                  INDEX_VAR,
+                                                  rtoffset);
+               fscan->scan.plan.qual = (List *)
+                       fix_upper_expr(root,
+                                                  (Node *) fscan->scan.plan.qual,
+                                                  itlist,
+                                                  INDEX_VAR,
+                                                  rtoffset);
+               fscan->fdw_exprs = (List *)
+                       fix_upper_expr(root,
+                                                  (Node *) fscan->fdw_exprs,
+                                                  itlist,
+                                                  INDEX_VAR,
+                                                  rtoffset);
+               pfree(itlist);
+               /* fdw_scan_tlist itself just needs fix_scan_list() adjustments */
+               fscan->fdw_scan_tlist =
+                       fix_scan_list(root, fscan->fdw_scan_tlist, rtoffset);
+       }
+       else
+       {
+               /* Adjust tlist, qual, fdw_exprs in the standard way */
+               fscan->scan.plan.targetlist =
+                       fix_scan_list(root, fscan->scan.plan.targetlist, rtoffset);
+               fscan->scan.plan.qual =
+                       fix_scan_list(root, fscan->scan.plan.qual, rtoffset);
+               fscan->fdw_exprs =
+                       fix_scan_list(root, fscan->fdw_exprs, rtoffset);
+       }
+
+       /* Adjust fs_relids if needed */
+       if (rtoffset > 0)
+       {
+               Bitmapset  *tempset = NULL;
+               int                     x = -1;
+
+               while ((x = bms_next_member(fscan->fs_relids, x)) >= 0)
+                       tempset = bms_add_member(tempset, x + rtoffset);
+               fscan->fs_relids = tempset;
+       }
+}
+
+/*
+ * set_customscan_references
+ *        Do set_plan_references processing on a CustomScan
+ */
+static void
+set_customscan_references(PlannerInfo *root,
+                                                 CustomScan *cscan,
+                                                 int rtoffset)
+{
+       ListCell   *lc;
+
+       /* Adjust scanrelid if it's valid */
+       if (cscan->scan.scanrelid > 0)
+               cscan->scan.scanrelid += rtoffset;
+
+       if (cscan->custom_scan_tlist != NIL || cscan->scan.scanrelid == 0)
+       {
+               /* Adjust tlist, qual, custom_exprs to reference custom scan tuple */
+               indexed_tlist *itlist = build_tlist_index(cscan->custom_scan_tlist);
+
+               cscan->scan.plan.targetlist = (List *)
+                       fix_upper_expr(root,
+                                                  (Node *) cscan->scan.plan.targetlist,
+                                                  itlist,
+                                                  INDEX_VAR,
+                                                  rtoffset);
+               cscan->scan.plan.qual = (List *)
+                       fix_upper_expr(root,
+                                                  (Node *) cscan->scan.plan.qual,
+                                                  itlist,
+                                                  INDEX_VAR,
+                                                  rtoffset);
+               cscan->custom_exprs = (List *)
+                       fix_upper_expr(root,
+                                                  (Node *) cscan->custom_exprs,
+                                                  itlist,
+                                                  INDEX_VAR,
+                                                  rtoffset);
+               pfree(itlist);
+               /* custom_scan_tlist itself just needs fix_scan_list() adjustments */
+               cscan->custom_scan_tlist =
+                       fix_scan_list(root, cscan->custom_scan_tlist, rtoffset);
+       }
+       else
+       {
+               /* Adjust tlist, qual, custom_exprs in the standard way */
+               cscan->scan.plan.targetlist =
+                       fix_scan_list(root, cscan->scan.plan.targetlist, rtoffset);
+               cscan->scan.plan.qual =
+                       fix_scan_list(root, cscan->scan.plan.qual, rtoffset);
+               cscan->custom_exprs =
+                       fix_scan_list(root, cscan->custom_exprs, rtoffset);
+       }
+
+       /* Adjust child plan-nodes recursively, if needed */
+       foreach(lc, cscan->custom_plans)
+       {
+               lfirst(lc) = set_plan_refs(root, (Plan *) lfirst(lc), rtoffset);
+       }
+
+       /* Adjust custom_relids if needed */
+       if (rtoffset > 0)
+       {
+               Bitmapset  *tempset = NULL;
+               int                     x = -1;
+
+               while ((x = bms_next_member(cscan->custom_relids, x)) >= 0)
+                       tempset = bms_add_member(tempset, x + rtoffset);
+               cscan->custom_relids = tempset;
+       }
+}
+
 /*
  * copyVar
  *             Copy a Var node.
@@ -665,60 +1248,61 @@ copyVar(Var *var)
  *
  * This is code that is common to all variants of expression-fixing.
  * We must look up operator opcode info for OpExpr and related nodes,
- * add OIDs from regclass Const nodes into glob->relationOids,
- * and add catalog TIDs for user-defined functions into glob->invalItems.
+ * add OIDs from regclass Const nodes into root->glob->relationOids, and
+ * add PlanInvalItems for user-defined functions into root->glob->invalItems.
+ * We also fill in column index lists for GROUPING() expressions.
  *
  * We assume it's okay to update opcode info in-place.  So this could possibly
  * scribble on the planner's input data structures, but it's OK.
  */
 static void
-fix_expr_common(PlannerGlobal *glob, Node *node)
+fix_expr_common(PlannerInfo *root, Node *node)
 {
        /* We assume callers won't call us on a NULL pointer */
        if (IsA(node, Aggref))
        {
-               record_plan_function_dependency(glob,
+               record_plan_function_dependency(root,
                                                                                ((Aggref *) node)->aggfnoid);
        }
        else if (IsA(node, WindowFunc))
        {
-               record_plan_function_dependency(glob,
+               record_plan_function_dependency(root,
                                                                                ((WindowFunc *) node)->winfnoid);
        }
        else if (IsA(node, FuncExpr))
        {
-               record_plan_function_dependency(glob,
+               record_plan_function_dependency(root,
                                                                                ((FuncExpr *) node)->funcid);
        }
        else if (IsA(node, OpExpr))
        {
                set_opfuncid((OpExpr *) node);
-               record_plan_function_dependency(glob,
+               record_plan_function_dependency(root,
                                                                                ((OpExpr *) node)->opfuncid);
        }
        else if (IsA(node, DistinctExpr))
        {
                set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
-               record_plan_function_dependency(glob,
+               record_plan_function_dependency(root,
                                                                                ((DistinctExpr *) node)->opfuncid);
        }
        else if (IsA(node, NullIfExpr))
        {
                set_opfuncid((OpExpr *) node);  /* rely on struct equivalence */
-               record_plan_function_dependency(glob,
+               record_plan_function_dependency(root,
                                                                                ((NullIfExpr *) node)->opfuncid);
        }
        else if (IsA(node, ScalarArrayOpExpr))
        {
                set_sa_opfuncid((ScalarArrayOpExpr *) node);
-               record_plan_function_dependency(glob,
-                                                                               ((ScalarArrayOpExpr *) node)->opfuncid);
+               record_plan_function_dependency(root,
+                                                                        ((ScalarArrayOpExpr *) node)->opfuncid);
        }
        else if (IsA(node, ArrayCoerceExpr))
        {
                if (OidIsValid(((ArrayCoerceExpr *) node)->elemfuncid))
-                       record_plan_function_dependency(glob,
-                                                                                       ((ArrayCoerceExpr *) node)->elemfuncid);
+                       record_plan_function_dependency(root,
+                                                                        ((ArrayCoerceExpr *) node)->elemfuncid);
        }
        else if (IsA(node, Const))
        {
@@ -726,10 +1310,63 @@ fix_expr_common(PlannerGlobal *glob, Node *node)
 
                /* Check for regclass reference */
                if (ISREGCLASSCONST(con))
-                       glob->relationOids =
-                               lappend_oid(glob->relationOids,
+                       root->glob->relationOids =
+                               lappend_oid(root->glob->relationOids,
                                                        DatumGetObjectId(con->constvalue));
        }
+       else if (IsA(node, GroupingFunc))
+       {
+               GroupingFunc *g = (GroupingFunc *) node;
+               AttrNumber *grouping_map = root->grouping_map;
+
+               /* If there are no grouping sets, we don't need this. */
+
+               Assert(grouping_map || g->cols == NIL);
+
+               if (grouping_map)
+               {
+                       ListCell   *lc;
+                       List       *cols = NIL;
+
+                       foreach(lc, g->refs)
+                       {
+                               cols = lappend_int(cols, grouping_map[lfirst_int(lc)]);
+                       }
+
+                       Assert(!g->cols || equal(cols, g->cols));
+
+                       if (!g->cols)
+                               g->cols = cols;
+               }
+       }
+}
+
+/*
+ * fix_param_node
+ *             Do set_plan_references processing on a Param
+ *
+ * If it's a PARAM_MULTIEXPR, replace it with the appropriate Param from
+ * root->multiexpr_params; otherwise no change is needed.
+ * Just for paranoia's sake, we make a copy of the node in either case.
+ */
+static Node *
+fix_param_node(PlannerInfo *root, Param *p)
+{
+       if (p->paramkind == PARAM_MULTIEXPR)
+       {
+               int                     subqueryid = p->paramid >> 16;
+               int                     colno = p->paramid & 0xFFFF;
+               List       *params;
+
+               if (subqueryid <= 0 ||
+                       subqueryid > list_length(root->multiexpr_params))
+                       elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
+               params = (List *) list_nth(root->multiexpr_params, subqueryid - 1);
+               if (colno <= 0 || colno > list_length(params))
+                       elog(ERROR, "unexpected PARAM_MULTIEXPR ID: %d", p->paramid);
+               return copyObject(list_nth(params, colno - 1));
+       }
+       return copyObject(p);
 }
 
 /*
@@ -737,18 +1374,21 @@ fix_expr_common(PlannerGlobal *glob, Node *node)
  *             Do set_plan_references processing on a scan-level expression
  *
  * This consists of incrementing all Vars' varnos by rtoffset,
+ * replacing PARAM_MULTIEXPR Params, expanding PlaceHolderVars,
  * looking up operator opcode info for OpExpr and related nodes,
- * and adding OIDs from regclass Const nodes into glob->relationOids.
+ * and adding OIDs from regclass Const nodes into root->glob->relationOids.
  */
 static Node *
-fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset)
+fix_scan_expr(PlannerInfo *root, Node *node, int rtoffset)
 {
        fix_scan_expr_context context;
 
-       context.glob = glob;
+       context.root = root;
        context.rtoffset = rtoffset;
 
-       if (rtoffset != 0 || glob->lastPHId != 0)
+       if (rtoffset != 0 ||
+               root->multiexpr_params != NIL ||
+               root->glob->lastPHId != 0)
        {
                return fix_scan_expr_mutator(node, &context);
        }
@@ -756,11 +1396,12 @@ fix_scan_expr(PlannerGlobal *glob, Node *node, int rtoffset)
        {
                /*
                 * If rtoffset == 0, we don't need to change any Vars, and if there
-                * are no placeholders anywhere we won't need to remove them.  Then
-                * it's OK to just scribble on the input node tree instead of copying
-                * (since the only change, filling in any unset opfuncid fields,
-                * is harmless).  This saves just enough cycles to be noticeable on
-                * trivial queries.
+                * are no MULTIEXPR subqueries then we don't need to replace
+                * PARAM_MULTIEXPR Params, and if there are no placeholders anywhere
+                * we won't need to remove them.  Then it's OK to just scribble on the
+                * input node tree instead of copying (since the only change, filling
+                * in any unset opfuncid fields, is harmless).  This saves just enough
+                * cycles to be noticeable on trivial queries.
                 */
                (void) fix_scan_expr_walker(node, &context);
                return node;
@@ -779,23 +1420,27 @@ fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
                Assert(var->varlevelsup == 0);
 
                /*
-                * We should not see any Vars marked INNER, but in a nestloop inner
-                * scan there could be OUTER Vars.      Leave them alone.
+                * We should not see any Vars marked INNER_VAR or OUTER_VAR.  But an
+                * indexqual expression could contain INDEX_VAR Vars.
                 */
-               Assert(var->varno != INNER);
-               if (var->varno > 0 && var->varno != OUTER)
+               Assert(var->varno != INNER_VAR);
+               Assert(var->varno != OUTER_VAR);
+               if (!IS_SPECIAL_VARNO(var->varno))
                        var->varno += context->rtoffset;
                if (var->varnoold > 0)
                        var->varnoold += context->rtoffset;
                return (Node *) var;
        }
+       if (IsA(node, Param))
+               return fix_param_node(context->root, (Param *) node);
        if (IsA(node, CurrentOfExpr))
        {
                CurrentOfExpr *cexpr = (CurrentOfExpr *) copyObject(node);
 
-               Assert(cexpr->cvarno != INNER);
-               Assert(cexpr->cvarno != OUTER);
-               cexpr->cvarno += context->rtoffset;
+               Assert(cexpr->cvarno != INNER_VAR);
+               Assert(cexpr->cvarno != OUTER_VAR);
+               if (!IS_SPECIAL_VARNO(cexpr->cvarno))
+                       cexpr->cvarno += context->rtoffset;
                return (Node *) cexpr;
        }
        if (IsA(node, PlaceHolderVar))
@@ -805,7 +1450,7 @@ fix_scan_expr_mutator(Node *node, fix_scan_expr_context *context)
 
                return fix_scan_expr_mutator((Node *) phv->phexpr, context);
        }
-       fix_expr_common(context->glob, node);
+       fix_expr_common(context->root, node);
        return expression_tree_mutator(node, fix_scan_expr_mutator,
                                                                   (void *) context);
 }
@@ -816,7 +1461,7 @@ fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
        if (node == NULL)
                return false;
        Assert(!IsA(node, PlaceHolderVar));
-       fix_expr_common(context->glob, node);
+       fix_expr_common(context->root, node);
        return expression_tree_walker(node, fix_scan_expr_walker,
                                                                  (void *) context);
 }
@@ -824,17 +1469,13 @@ fix_scan_expr_walker(Node *node, fix_scan_expr_context *context)
 /*
  * set_join_references
  *       Modify the target list and quals of a join node to reference its
- *       subplans, by setting the varnos to OUTER or INNER and setting attno
- *       values to the result domain number of either the corresponding outer
- *       or inner join tuple item.  Also perform opcode lookup for these
- *       expressions. and add regclass OIDs to glob->relationOids.
- *
- * In the case of a nestloop with inner indexscan, we will also need to
- * apply the same transformation to any outer vars appearing in the
- * quals of the child indexscan.  set_inner_join_references does that.
+ *       subplans, by setting the varnos to OUTER_VAR or INNER_VAR and setting
+ *       attno values to the result domain number of either the corresponding
+ *       outer or inner join tuple item.  Also perform opcode lookup for these
+ *       expressions, and add regclass OIDs to root->glob->relationOids.
  */
 static void
-set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
+set_join_references(PlannerInfo *root, Join *join, int rtoffset)
 {
        Plan       *outer_plan = join->plan.lefttree;
        Plan       *inner_plan = join->plan.righttree;
@@ -844,20 +1485,14 @@ set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
        outer_itlist = build_tlist_index(outer_plan->targetlist);
        inner_itlist = build_tlist_index(inner_plan->targetlist);
 
-       /* All join plans have tlist, qual, and joinqual */
-       join->plan.targetlist = fix_join_expr(glob,
-                                                                                 join->plan.targetlist,
-                                                                                 outer_itlist,
-                                                                                 inner_itlist,
-                                                                                 (Index) 0,
-                                                                                 rtoffset);
-       join->plan.qual = fix_join_expr(glob,
-                                                                       join->plan.qual,
-                                                                       outer_itlist,
-                                                                       inner_itlist,
-                                                                       (Index) 0,
-                                                                       rtoffset);
-       join->joinqual = fix_join_expr(glob,
+       /*
+        * First process the joinquals (including merge or hash clauses).  These
+        * are logically below the join so they can always use all values
+        * available from the input tlists.  It's okay to also handle
+        * NestLoopParams now, because those couldn't refer to nullable
+        * subexpressions.
+        */
+       join->joinqual = fix_join_expr(root,
                                                                   join->joinqual,
                                                                   outer_itlist,
                                                                   inner_itlist,
@@ -867,14 +1502,29 @@ set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
        /* Now do join-type-specific stuff */
        if (IsA(join, NestLoop))
        {
-               /* This processing is split out to handle possible recursion */
-               set_inner_join_references(glob, inner_plan, outer_itlist);
+               NestLoop   *nl = (NestLoop *) join;
+               ListCell   *lc;
+
+               foreach(lc, nl->nestParams)
+               {
+                       NestLoopParam *nlp = (NestLoopParam *) lfirst(lc);
+
+                       nlp->paramval = (Var *) fix_upper_expr(root,
+                                                                                                  (Node *) nlp->paramval,
+                                                                                                  outer_itlist,
+                                                                                                  OUTER_VAR,
+                                                                                                  rtoffset);
+                       /* Check we replaced any PlaceHolderVar with simple Var */
+                       if (!(IsA(nlp->paramval, Var) &&
+                                 nlp->paramval->varno == OUTER_VAR))
+                               elog(ERROR, "NestLoopParam was not reduced to a simple Var");
+               }
        }
        else if (IsA(join, MergeJoin))
        {
                MergeJoin  *mj = (MergeJoin *) join;
 
-               mj->mergeclauses = fix_join_expr(glob,
+               mj->mergeclauses = fix_join_expr(root,
                                                                                 mj->mergeclauses,
                                                                                 outer_itlist,
                                                                                 inner_itlist,
@@ -885,7 +1535,7 @@ set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
        {
                HashJoin   *hj = (HashJoin *) join;
 
-               hj->hashclauses = fix_join_expr(glob,
+               hj->hashclauses = fix_join_expr(root,
                                                                                hj->hashclauses,
                                                                                outer_itlist,
                                                                                inner_itlist,
@@ -893,195 +1543,51 @@ set_join_references(PlannerGlobal *glob, Join *join, int rtoffset)
                                                                                rtoffset);
        }
 
-       pfree(outer_itlist);
-       pfree(inner_itlist);
-}
-
-/*
- * set_inner_join_references
- *             Handle join references appearing in an inner indexscan's quals
- *
- * To handle bitmap-scan plan trees, we have to be able to recurse down
- * to the bottom BitmapIndexScan nodes; likewise, appendrel indexscans
- * require recursing through Append nodes.     This is split out as a separate
- * function so that it can recurse.
- *
- * Note we do *not* apply any rtoffset for non-join Vars; this is because
- * the quals will be processed again by fix_scan_expr when the set_plan_refs
- * recursion reaches the inner indexscan, and so we'd have done it twice.
- */
-static void
-set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
-                                                 indexed_tlist *outer_itlist)
-{
-       if (IsA(inner_plan, IndexScan))
-       {
-               /*
-                * An index is being used to reduce the number of tuples scanned in
-                * the inner relation.  If there are join clauses being used with the
-                * index, we must update their outer-rel var nodes to refer to the
-                * outer side of the join.
-                */
-               IndexScan  *innerscan = (IndexScan *) inner_plan;
-               List       *indexqualorig = innerscan->indexqualorig;
-
-               /* No work needed if indexqual refers only to its own rel... */
-               if (NumRelids((Node *) indexqualorig) > 1)
-               {
-                       Index           innerrel = innerscan->scan.scanrelid;
-
-                       /* only refs to outer vars get changed in the inner qual */
-                       innerscan->indexqualorig = fix_join_expr(glob,
-                                                                                                        indexqualorig,
-                                                                                                        outer_itlist,
-                                                                                                        NULL,
-                                                                                                        innerrel,
-                                                                                                        0);
-                       innerscan->indexqual = fix_join_expr(glob,
-                                                                                                innerscan->indexqual,
-                                                                                                outer_itlist,
-                                                                                                NULL,
-                                                                                                innerrel,
-                                                                                                0);
-
-                       /*
-                        * We must fix the inner qpqual too, if it has join clauses (this
-                        * could happen if special operators are involved: some indexquals
-                        * may get rechecked as qpquals).
-                        */
-                       if (NumRelids((Node *) inner_plan->qual) > 1)
-                               inner_plan->qual = fix_join_expr(glob,
-                                                                                                inner_plan->qual,
-                                                                                                outer_itlist,
-                                                                                                NULL,
-                                                                                                innerrel,
-                                                                                                0);
-               }
-       }
-       else if (IsA(inner_plan, BitmapIndexScan))
-       {
-               /*
-                * Same, but index is being used within a bitmap plan.
-                */
-               BitmapIndexScan *innerscan = (BitmapIndexScan *) inner_plan;
-               List       *indexqualorig = innerscan->indexqualorig;
-
-               /* No work needed if indexqual refers only to its own rel... */
-               if (NumRelids((Node *) indexqualorig) > 1)
-               {
-                       Index           innerrel = innerscan->scan.scanrelid;
-
-                       /* only refs to outer vars get changed in the inner qual */
-                       innerscan->indexqualorig = fix_join_expr(glob,
-                                                                                                        indexqualorig,
-                                                                                                        outer_itlist,
-                                                                                                        NULL,
-                                                                                                        innerrel,
-                                                                                                        0);
-                       innerscan->indexqual = fix_join_expr(glob,
-                                                                                                innerscan->indexqual,
-                                                                                                outer_itlist,
-                                                                                                NULL,
-                                                                                                innerrel,
-                                                                                                0);
-                       /* no need to fix inner qpqual */
-                       Assert(inner_plan->qual == NIL);
-               }
-       }
-       else if (IsA(inner_plan, BitmapHeapScan))
-       {
-               /*
-                * The inner side is a bitmap scan plan.  Fix the top node, and
-                * recurse to get the lower nodes.
-                *
-                * Note: create_bitmap_scan_plan removes clauses from bitmapqualorig
-                * if they are duplicated in qpqual, so must test these independently.
-                */
-               BitmapHeapScan *innerscan = (BitmapHeapScan *) inner_plan;
-               Index           innerrel = innerscan->scan.scanrelid;
-               List       *bitmapqualorig = innerscan->bitmapqualorig;
-
-               /* only refs to outer vars get changed in the inner qual */
-               if (NumRelids((Node *) bitmapqualorig) > 1)
-                       innerscan->bitmapqualorig = fix_join_expr(glob,
-                                                                                                         bitmapqualorig,
-                                                                                                         outer_itlist,
-                                                                                                         NULL,
-                                                                                                         innerrel,
-                                                                                                         0);
-
-               /*
-                * We must fix the inner qpqual too, if it has join clauses (this
-                * could happen if special operators are involved: some indexquals may
-                * get rechecked as qpquals).
-                */
-               if (NumRelids((Node *) inner_plan->qual) > 1)
-                       inner_plan->qual = fix_join_expr(glob,
-                                                                                        inner_plan->qual,
-                                                                                        outer_itlist,
-                                                                                        NULL,
-                                                                                        innerrel,
-                                                                                        0);
-
-               /* Now recurse */
-               set_inner_join_references(glob, inner_plan->lefttree, outer_itlist);
-       }
-       else if (IsA(inner_plan, BitmapAnd))
-       {
-               /* All we need do here is recurse */
-               BitmapAnd  *innerscan = (BitmapAnd *) inner_plan;
-               ListCell   *l;
-
-               foreach(l, innerscan->bitmapplans)
-               {
-                       set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
-               }
-       }
-       else if (IsA(inner_plan, BitmapOr))
-       {
-               /* All we need do here is recurse */
-               BitmapOr   *innerscan = (BitmapOr *) inner_plan;
-               ListCell   *l;
-
-               foreach(l, innerscan->bitmapplans)
-               {
-                       set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
-               }
-       }
-       else if (IsA(inner_plan, TidScan))
+       /*
+        * Now we need to fix up the targetlist and qpqual, which are logically
+        * above the join.  This means they should not re-use any input expression
+        * that was computed in the nullable side of an outer join.  Vars and
+        * PlaceHolderVars are fine, so we can implement this restriction just by
+        * clearing has_non_vars in the indexed_tlist structs.
+        *
+        * XXX This is a grotty workaround for the fact that we don't clearly
+        * distinguish between a Var appearing below an outer join and the "same"
+        * Var appearing above it.  If we did, we'd not need to hack the matching
+        * rules this way.
+        */
+       switch (join->jointype)
        {
-               TidScan    *innerscan = (TidScan *) inner_plan;
-               Index           innerrel = innerscan->scan.scanrelid;
-
-               innerscan->tidquals = fix_join_expr(glob,
-                                                                                       innerscan->tidquals,
-                                                                                       outer_itlist,
-                                                                                       NULL,
-                                                                                       innerrel,
-                                                                                       0);
+               case JOIN_LEFT:
+               case JOIN_SEMI:
+               case JOIN_ANTI:
+                       inner_itlist->has_non_vars = false;
+                       break;
+               case JOIN_RIGHT:
+                       outer_itlist->has_non_vars = false;
+                       break;
+               case JOIN_FULL:
+                       outer_itlist->has_non_vars = false;
+                       inner_itlist->has_non_vars = false;
+                       break;
+               default:
+                       break;
        }
-       else if (IsA(inner_plan, Append))
-       {
-               /*
-                * The inner side is an append plan.  Recurse to see if it contains
-                * indexscans that need to be fixed.
-                */
-               Append     *appendplan = (Append *) inner_plan;
-               ListCell   *l;
 
-               foreach(l, appendplan->appendplans)
-               {
-                       set_inner_join_references(glob, (Plan *) lfirst(l), outer_itlist);
-               }
-       }
-       else if (IsA(inner_plan, Result))
-       {
-               /* Recurse through a gating Result node (similar to Append case) */
-               Result     *result = (Result *) inner_plan;
+       join->plan.targetlist = fix_join_expr(root,
+                                                                                 join->plan.targetlist,
+                                                                                 outer_itlist,
+                                                                                 inner_itlist,
+                                                                                 (Index) 0,
+                                                                                 rtoffset);
+       join->plan.qual = fix_join_expr(root,
+                                                                       join->plan.qual,
+                                                                       outer_itlist,
+                                                                       inner_itlist,
+                                                                       (Index) 0,
+                                                                       rtoffset);
 
-               if (result->plan.lefttree)
-                       set_inner_join_references(glob, result->plan.lefttree, outer_itlist);
-       }
+       pfree(outer_itlist);
+       pfree(inner_itlist);
 }
 
 /*
@@ -1089,7 +1595,7 @@ set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
  *       Update the targetlist and quals of an upper-level plan node
  *       to refer to the tuples returned by its lefttree subplan.
  *       Also perform opcode lookup for these expressions, and
- *       add regclass OIDs to glob->relationOids.
+ *       add regclass OIDs to root->glob->relationOids.
  *
  * This is used for single-input plan types like Agg, Group, Result.
  *
@@ -1103,7 +1609,7 @@ set_inner_join_references(PlannerGlobal *glob, Plan *inner_plan,
  * the expression.
  */
 static void
-set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
+set_upper_references(PlannerInfo *root, Plan *plan, int rtoffset)
 {
        Plan       *subplan = plan->lefttree;
        indexed_tlist *subplan_itlist;
@@ -1118,10 +1624,27 @@ set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
                TargetEntry *tle = (TargetEntry *) lfirst(l);
                Node       *newexpr;
 
-               newexpr = fix_upper_expr(glob,
-                                                                (Node *) tle->expr,
-                                                                subplan_itlist,
-                                                                rtoffset);
+               /* If it's a non-Var sort/group item, first try to match by sortref */
+               if (tle->ressortgroupref != 0 && !IsA(tle->expr, Var))
+               {
+                       newexpr = (Node *)
+                               search_indexed_tlist_for_sortgroupref((Node *) tle->expr,
+                                                                                                         tle->ressortgroupref,
+                                                                                                         subplan_itlist,
+                                                                                                         OUTER_VAR);
+                       if (!newexpr)
+                               newexpr = fix_upper_expr(root,
+                                                                                (Node *) tle->expr,
+                                                                                subplan_itlist,
+                                                                                OUTER_VAR,
+                                                                                rtoffset);
+               }
+               else
+                       newexpr = fix_upper_expr(root,
+                                                                        (Node *) tle->expr,
+                                                                        subplan_itlist,
+                                                                        OUTER_VAR,
+                                                                        rtoffset);
                tle = flatCopyTargetEntry(tle);
                tle->expr = (Expr *) newexpr;
                output_targetlist = lappend(output_targetlist, tle);
@@ -1129,9 +1652,10 @@ set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
        plan->targetlist = output_targetlist;
 
        plan->qual = (List *)
-               fix_upper_expr(glob,
+               fix_upper_expr(root,
                                           (Node *) plan->qual,
                                           subplan_itlist,
+                                          OUTER_VAR,
                                           rtoffset);
 
        pfree(subplan_itlist);
@@ -1140,7 +1664,7 @@ set_upper_references(PlannerGlobal *glob, Plan *plan, int rtoffset)
 /*
  * set_dummy_tlist_references
  *       Replace the targetlist of an upper-level plan node with a simple
- *       list of OUTER references to its child.
+ *       list of OUTER_VAR references to its child.
  *
  * This is used for plan types like Sort and Append that don't evaluate
  * their targetlists.  Although the executor doesn't care at all what's in
@@ -1163,10 +1687,11 @@ set_dummy_tlist_references(Plan *plan, int rtoffset)
                Var                *oldvar = (Var *) tle->expr;
                Var                *newvar;
 
-               newvar = makeVar(OUTER,
+               newvar = makeVar(OUTER_VAR,
                                                 tle->resno,
                                                 exprType((Node *) oldvar),
                                                 exprTypmod((Node *) oldvar),
+                                                exprCollation((Node *) oldvar),
                                                 0);
                if (IsA(oldvar, Var))
                {
@@ -1194,7 +1719,7 @@ set_dummy_tlist_references(Plan *plan, int rtoffset)
  *
  * In most cases, subplan tlists will be "flat" tlists with only Vars,
  * so we try to optimize that case by extracting information about Vars
- * in advance. Matching a parent tlist to a child is still an O(N^2)
+ * in advance.  Matching a parent tlist to a child is still an O(N^2)
  * operation, but at least with a much smaller constant factor than plain
  * tlist_member() searches.
  *
@@ -1339,7 +1864,9 @@ search_indexed_tlist_for_var(Var *var, indexed_tlist *itlist,
  * If no match, return NULL.
  *
  * NOTE: it is a waste of time to call this unless itlist->has_ph_vars or
- * itlist->has_non_vars
+ * itlist->has_non_vars.  Furthermore, set_join_references() relies on being
+ * able to prevent matching of non-Vars by clearing itlist->has_non_vars,
+ * so there's a correctness reason not to call it unless that's set.
  */
 static Var *
 search_indexed_tlist_for_non_var(Node *node,
@@ -1353,11 +1880,7 @@ search_indexed_tlist_for_non_var(Node *node,
                /* Found a matching subplan output expression */
                Var                *newvar;
 
-               newvar = makeVar(newvarno,
-                                                tle->resno,
-                                                exprType((Node *) tle->expr),
-                                                exprTypmod((Node *) tle->expr),
-                                                0);
+               newvar = makeVarFromTargetEntry(newvarno, tle);
                newvar->varnoold = 0;   /* wasn't ever a plain Var */
                newvar->varoattno = 0;
                return newvar;
@@ -1365,39 +1888,78 @@ search_indexed_tlist_for_non_var(Node *node,
        return NULL;                            /* no match */
 }
 
+/*
+ * search_indexed_tlist_for_sortgroupref --- find a sort/group expression
+ *             (which is assumed not to be just a Var)
+ *
+ * If a match is found, return a Var constructed to reference the tlist item.
+ * If no match, return NULL.
+ *
+ * This is needed to ensure that we select the right subplan TLE in cases
+ * where there are multiple textually-equal()-but-volatile sort expressions.
+ * And it's also faster than search_indexed_tlist_for_non_var.
+ */
+static Var *
+search_indexed_tlist_for_sortgroupref(Node *node,
+                                                                         Index sortgroupref,
+                                                                         indexed_tlist *itlist,
+                                                                         Index newvarno)
+{
+       ListCell   *lc;
+
+       foreach(lc, itlist->tlist)
+       {
+               TargetEntry *tle = (TargetEntry *) lfirst(lc);
+
+               /* The equal() check should be redundant, but let's be paranoid */
+               if (tle->ressortgroupref == sortgroupref &&
+                       equal(node, tle->expr))
+               {
+                       /* Found a matching subplan output expression */
+                       Var                *newvar;
+
+                       newvar = makeVarFromTargetEntry(newvarno, tle);
+                       newvar->varnoold = 0;           /* wasn't ever a plain Var */
+                       newvar->varoattno = 0;
+                       return newvar;
+               }
+       }
+       return NULL;                            /* no match */
+}
+
 /*
  * fix_join_expr
  *        Create a new set of targetlist entries or join qual clauses by
  *        changing the varno/varattno values of variables in the clauses
  *        to reference target list values from the outer and inner join
  *        relation target lists.  Also perform opcode lookup and add
- *        regclass OIDs to glob->relationOids.
+ *        regclass OIDs to root->glob->relationOids.
  *
- * This is used in two different scenarios: a normal join clause, where
- * all the Vars in the clause *must* be replaced by OUTER or INNER references;
- * and an indexscan being used on the inner side of a nestloop join.
- * In the latter case we want to replace the outer-relation Vars by OUTER
- * references, while Vars of the inner relation should be adjusted by rtoffset.
- * (We also implement RETURNING clause fixup using this second scenario.)
+ * This is used in two different scenarios: a normal join clause, where all
+ * the Vars in the clause *must* be replaced by OUTER_VAR or INNER_VAR
+ * references; and a RETURNING clause, which may contain both Vars of the
+ * target relation and Vars of other relations.  In the latter case we want
+ * to replace the other-relation Vars by OUTER_VAR references, while leaving
+ * target Vars alone.
  *
  * For a normal join, acceptable_rel should be zero so that any failure to
- * match a Var will be reported as an error.  For the indexscan case,
- * pass inner_itlist = NULL and acceptable_rel = the (not-offseted-yet) ID
- * of the inner relation.
+ * match a Var will be reported as an error.  For the RETURNING case, pass
+ * inner_itlist = NULL and acceptable_rel = the ID of the target relation.
  *
  * 'clauses' is the targetlist or list of join clauses
- * 'outer_itlist' is the indexed target list of the outer join relation
+ * 'outer_itlist' is the indexed target list of the outer join relation,
+ *             or NULL
  * 'inner_itlist' is the indexed target list of the inner join relation,
  *             or NULL
  * 'acceptable_rel' is either zero or the rangetable index of a relation
- *             whose Vars may appear in the clause without provoking an error.
- * 'rtoffset' is what to add to varno for Vars of acceptable_rel.
+ *             whose Vars may appear in the clause without provoking an error
+ * 'rtoffset': how much to increment varnoold by
  *
  * Returns the new expression tree.  The original clause structure is
  * not modified.
  */
 static List *
-fix_join_expr(PlannerGlobal *glob,
+fix_join_expr(PlannerInfo *root,
                          List *clauses,
                          indexed_tlist *outer_itlist,
                          indexed_tlist *inner_itlist,
@@ -1406,7 +1968,7 @@ fix_join_expr(PlannerGlobal *glob,
 {
        fix_join_expr_context context;
 
-       context.glob = glob;
+       context.root = root;
        context.outer_itlist = outer_itlist;
        context.inner_itlist = inner_itlist;
        context.acceptable_rel = acceptable_rel;
@@ -1426,17 +1988,22 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
                Var                *var = (Var *) node;
 
                /* First look for the var in the input tlists */
-               newvar = search_indexed_tlist_for_var(var,
-                                                                                         context->outer_itlist,
-                                                                                         OUTER,
-                                                                                         context->rtoffset);
-               if (newvar)
-                       return (Node *) newvar;
+               if (context->outer_itlist)
+               {
+                       newvar = search_indexed_tlist_for_var(var,
+                                                                                                 context->outer_itlist,
+                                                                                                 OUTER_VAR,
+                                                                                                 context->rtoffset);
+                       if (newvar)
+                               return (Node *) newvar;
+               }
+
+               /* Then in the outer */
                if (context->inner_itlist)
                {
                        newvar = search_indexed_tlist_for_var(var,
                                                                                                  context->inner_itlist,
-                                                                                                 INNER,
+                                                                                                 INNER_VAR,
                                                                                                  context->rtoffset);
                        if (newvar)
                                return (Node *) newvar;
@@ -1447,7 +2014,8 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
                {
                        var = copyVar(var);
                        var->varno += context->rtoffset;
-                       var->varnoold += context->rtoffset;
+                       if (var->varnoold > 0)
+                               var->varnoold += context->rtoffset;
                        return (Node *) var;
                }
 
@@ -1459,11 +2027,11 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
                PlaceHolderVar *phv = (PlaceHolderVar *) node;
 
                /* See if the PlaceHolderVar has bubbled up from a lower plan node */
-               if (context->outer_itlist->has_ph_vars)
+               if (context->outer_itlist && context->outer_itlist->has_ph_vars)
                {
                        newvar = search_indexed_tlist_for_non_var((Node *) phv,
                                                                                                          context->outer_itlist,
-                                                                                                         OUTER);
+                                                                                                         OUTER_VAR);
                        if (newvar)
                                return (Node *) newvar;
                }
@@ -1471,7 +2039,7 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
                {
                        newvar = search_indexed_tlist_for_non_var((Node *) phv,
                                                                                                          context->inner_itlist,
-                                                                                                         INNER);
+                                                                                                         INNER_VAR);
                        if (newvar)
                                return (Node *) newvar;
                }
@@ -1479,12 +2047,14 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
                /* If not supplied by input plans, evaluate the contained expr */
                return fix_join_expr_mutator((Node *) phv->phexpr, context);
        }
+       if (IsA(node, Param))
+               return fix_param_node(context->root, (Param *) node);
        /* Try matching more complex expressions too, if tlists have any */
-       if (context->outer_itlist->has_non_vars)
+       if (context->outer_itlist && context->outer_itlist->has_non_vars)
        {
                newvar = search_indexed_tlist_for_non_var(node,
                                                                                                  context->outer_itlist,
-                                                                                                 OUTER);
+                                                                                                 OUTER_VAR);
                if (newvar)
                        return (Node *) newvar;
        }
@@ -1492,11 +2062,11 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
        {
                newvar = search_indexed_tlist_for_non_var(node,
                                                                                                  context->inner_itlist,
-                                                                                                 INNER);
+                                                                                                 INNER_VAR);
                if (newvar)
                        return (Node *) newvar;
        }
-       fix_expr_common(context->glob, node);
+       fix_expr_common(context->root, node);
        return expression_tree_mutator(node,
                                                                   fix_join_expr_mutator,
                                                                   (void *) context);
@@ -1506,10 +2076,10 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
  * fix_upper_expr
  *             Modifies an expression tree so that all Var nodes reference outputs
  *             of a subplan.  Also performs opcode lookup, and adds regclass OIDs to
- *             glob->relationOids.
+ *             root->glob->relationOids.
  *
  * This is used to fix up target and qual expressions of non-join upper-level
- * plan nodes.
+ * plan nodes, as well as index-only scan nodes.
  *
  * An error is raised if no matching var can be found in the subplan tlist
  * --- so this routine should only be applied to nodes whose subplans'
@@ -1522,23 +2092,26 @@ fix_join_expr_mutator(Node *node, fix_join_expr_context *context)
  * subplan tlist is just a flattened list of Vars.)
  *
  * 'node': the tree to be fixed (a target item or qual)
- * 'subplan_itlist': indexed target list for subplan
+ * 'subplan_itlist': indexed target list for subplan (or index)
+ * 'newvarno': varno to use for Vars referencing tlist elements
  * 'rtoffset': how much to increment varnoold by
  *
  * The resulting tree is a copy of the original in which all Var nodes have
- * varno = OUTER, varattno = resno of corresponding subplan target.
+ * varno = newvarno, varattno = resno of corresponding targetlist element.
  * The original tree is not modified.
  */
 static Node *
-fix_upper_expr(PlannerGlobal *glob,
+fix_upper_expr(PlannerInfo *root,
                           Node *node,
                           indexed_tlist *subplan_itlist,
+                          Index newvarno,
                           int rtoffset)
 {
        fix_upper_expr_context context;
 
-       context.glob = glob;
+       context.root = root;
        context.subplan_itlist = subplan_itlist;
+       context.newvarno = newvarno;
        context.rtoffset = rtoffset;
        return fix_upper_expr_mutator(node, &context);
 }
@@ -1556,7 +2129,7 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
 
                newvar = search_indexed_tlist_for_var(var,
                                                                                          context->subplan_itlist,
-                                                                                         OUTER,
+                                                                                         context->newvarno,
                                                                                          context->rtoffset);
                if (!newvar)
                        elog(ERROR, "variable not found in subplan target list");
@@ -1571,23 +2144,25 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
                {
                        newvar = search_indexed_tlist_for_non_var((Node *) phv,
                                                                                                          context->subplan_itlist,
-                                                                                                         OUTER);
+                                                                                                         context->newvarno);
                        if (newvar)
                                return (Node *) newvar;
                }
                /* If not supplied by input plan, evaluate the contained expr */
                return fix_upper_expr_mutator((Node *) phv->phexpr, context);
        }
+       if (IsA(node, Param))
+               return fix_param_node(context->root, (Param *) node);
        /* Try matching more complex expressions too, if tlist has any */
        if (context->subplan_itlist->has_non_vars)
        {
                newvar = search_indexed_tlist_for_non_var(node,
                                                                                                  context->subplan_itlist,
-                                                                                                 OUTER);
+                                                                                                 context->newvarno);
                if (newvar)
                        return (Node *) newvar;
        }
-       fix_expr_common(context->glob, node);
+       fix_expr_common(context->root, node);
        return expression_tree_mutator(node,
                                                                   fix_upper_expr_mutator,
                                                                   (void *) context);
@@ -1599,59 +2174,66 @@ fix_upper_expr_mutator(Node *node, fix_upper_expr_context *context)
  *
  * If the query involves more than just the result table, we have to
  * adjust any Vars that refer to other tables to reference junk tlist
- * entries in the top plan's targetlist.  Vars referencing the result
+ * entries in the top subplan's targetlist.  Vars referencing the result
  * table should be left alone, however (the executor will evaluate them
- * using the actual heap tuple, after firing triggers if any). In the
- * adjusted RETURNING list, result-table Vars will still have their
- * original varno, but Vars for other rels will have varno OUTER.
+ * using the actual heap tuple, after firing triggers if any).  In the
+ * adjusted RETURNING list, result-table Vars will have their original
+ * varno (plus rtoffset), but Vars for other rels will have varno OUTER_VAR.
  *
  * We also must perform opcode lookup and add regclass OIDs to
- * glob->relationOids.
+ * root->glob->relationOids.
  *
  * 'rlist': the RETURNING targetlist to be fixed
- * 'topplan': the top Plan node for the query (not yet passed through
- *             set_plan_references)
+ * 'topplan': the top subplan node that will be just below the ModifyTable
+ *             node (note it's not yet passed through set_plan_refs)
  * 'resultRelation': RT index of the associated result relation
+ * 'rtoffset': how much to increment varnos by
  *
- * Note: we assume that result relations will have rtoffset zero, that is,
- * they are not coming from a subplan.
+ * Note: the given 'root' is for the parent query level, not the 'topplan'.
+ * This does not matter currently since we only access the dependency-item
+ * lists in root->glob, but it would need some hacking if we wanted a root
+ * that actually matches the subplan.
+ *
+ * Note: resultRelation is not yet adjusted by rtoffset.
  */
-List *
-set_returning_clause_references(PlannerGlobal *glob,
+static List *
+set_returning_clause_references(PlannerInfo *root,
                                                                List *rlist,
                                                                Plan *topplan,
-                                                               Index resultRelation)
+                                                               Index resultRelation,
+                                                               int rtoffset)
 {
        indexed_tlist *itlist;
 
        /*
         * We can perform the desired Var fixup by abusing the fix_join_expr
-        * machinery that normally handles inner indexscan fixup.  We search the
+        * machinery that formerly handled inner indexscan fixup.  We search the
         * top plan's targetlist for Vars of non-result relations, and use
         * fix_join_expr to convert RETURNING Vars into references to those tlist
         * entries, while leaving result-rel Vars as-is.
         *
         * PlaceHolderVars will also be sought in the targetlist, but no
-        * more-complex expressions will be.  Note that it is not possible for
-        * a PlaceHolderVar to refer to the result relation, since the result
-        * is never below an outer join.  If that case could happen, we'd have
-        * to be prepared to pick apart the PlaceHolderVar and evaluate its
-        * contained expression instead.
+        * more-complex expressions will be.  Note that it is not possible for a
+        * PlaceHolderVar to refer to the result relation, since the result is
+        * never below an outer join.  If that case could happen, we'd have to be
+        * prepared to pick apart the PlaceHolderVar and evaluate its contained
+        * expression instead.
         */
        itlist = build_tlist_index_other_vars(topplan->targetlist, resultRelation);
 
-       rlist = fix_join_expr(glob,
+       rlist = fix_join_expr(root,
                                                  rlist,
                                                  itlist,
                                                  NULL,
                                                  resultRelation,
-                                                 0);
+                                                 rtoffset);
 
        pfree(itlist);
 
        return rlist;
 }
 
+
 /*****************************************************************************
  *                                     OPERATOR REGPROC LOOKUP
  *****************************************************************************/
@@ -1726,71 +2308,71 @@ set_sa_opfuncid(ScalarArrayOpExpr *opexpr)
  * dependency on a function that it's removed from the plan tree.
  */
 void
-record_plan_function_dependency(PlannerGlobal *glob, Oid funcid)
+record_plan_function_dependency(PlannerInfo *root, Oid funcid)
 {
        /*
         * For performance reasons, we don't bother to track built-in functions;
         * we just assume they'll never change (or at least not in ways that'd
         * invalidate plans using them).  For this purpose we can consider a
         * built-in function to be one with OID less than FirstBootstrapObjectId.
-        * Note that the OID generator guarantees never to generate such an
-        * OID after startup, even at OID wraparound.
+        * Note that the OID generator guarantees never to generate such an OID
+        * after startup, even at OID wraparound.
         */
        if (funcid >= (Oid) FirstBootstrapObjectId)
        {
-               HeapTuple       func_tuple;
-               PlanInvalItem *inval_item;
-
-               func_tuple = SearchSysCache(PROCOID,
-                                                                       ObjectIdGetDatum(funcid),
-                                                                       0, 0, 0);
-               if (!HeapTupleIsValid(func_tuple))
-                       elog(ERROR, "cache lookup failed for function %u", funcid);
-
-               inval_item = makeNode(PlanInvalItem);
+               PlanInvalItem *inval_item = makeNode(PlanInvalItem);
 
                /*
-                * It would work to use any syscache on pg_proc, but plancache.c
-                * expects us to use PROCOID.
+                * It would work to use any syscache on pg_proc, but the easiest is
+                * PROCOID since we already have the function's OID at hand.  Note
+                * that plancache.c knows we use PROCOID.
                 */
                inval_item->cacheId = PROCOID;
-               inval_item->tupleId = func_tuple->t_self;
-
-               glob->invalItems = lappend(glob->invalItems, inval_item);
+               inval_item->hashValue = GetSysCacheHashValue1(PROCOID,
+                                                                                                  ObjectIdGetDatum(funcid));
 
-               ReleaseSysCache(func_tuple);
+               root->glob->invalItems = lappend(root->glob->invalItems, inval_item);
        }
 }
 
 /*
  * extract_query_dependencies
- *             Given a list of not-yet-planned queries (i.e. Query nodes),
- *             extract their dependencies just as set_plan_references would do.
+ *             Given a not-yet-planned query or queries (i.e. a Query node or list
+ *             of Query nodes), extract dependencies just as set_plan_references
+ *             would do.
  *
  * This is needed by plancache.c to handle invalidation of cached unplanned
  * queries.
  */
 void
-extract_query_dependencies(List *queries,
+extract_query_dependencies(Node *query,
                                                   List **relationOids,
-                                                  List **invalItems)
+                                                  List **invalItems,
+                                                  bool *hasRowSecurity)
 {
        PlannerGlobal glob;
+       PlannerInfo root;
 
-       /* Make up a dummy PlannerGlobal so we can use this module's machinery */
+       /* Make up dummy planner state so we can use this module's machinery */
        MemSet(&glob, 0, sizeof(glob));
        glob.type = T_PlannerGlobal;
        glob.relationOids = NIL;
        glob.invalItems = NIL;
+       glob.hasRowSecurity = false;
 
-       (void) extract_query_dependencies_walker((Node *) queries, &glob);
+       MemSet(&root, 0, sizeof(root));
+       root.type = T_PlannerInfo;
+       root.glob = &glob;
+
+       (void) extract_query_dependencies_walker(query, &root);
 
        *relationOids = glob.relationOids;
        *invalItems = glob.invalItems;
+       *hasRowSecurity = glob.hasRowSecurity;
 }
 
 static bool
-extract_query_dependencies_walker(Node *node, PlannerGlobal *context)
+extract_query_dependencies_walker(Node *node, PlannerInfo *context)
 {
        if (node == NULL)
                return false;
@@ -1802,14 +2384,28 @@ extract_query_dependencies_walker(Node *node, PlannerGlobal *context)
                Query      *query = (Query *) node;
                ListCell   *lc;
 
+               /* Collect row security information */
+               context->glob->hasRowSecurity = query->hasRowSecurity;
+
+               if (query->commandType == CMD_UTILITY)
+               {
+                       /*
+                        * Ignore utility statements, except those (such as EXPLAIN) that
+                        * contain a parsed-but-not-planned query.
+                        */
+                       query = UtilityContainsQuery(query->utilityStmt);
+                       if (query == NULL)
+                               return false;
+               }
+
                /* Collect relation OIDs in this Query's rtable */
                foreach(lc, query->rtable)
                {
                        RangeTblEntry *rte = (RangeTblEntry *) lfirst(lc);
 
                        if (rte->rtekind == RTE_RELATION)
-                               context->relationOids = lappend_oid(context->relationOids,
-                                                                                                       rte->relid);
+                               context->glob->relationOids =
+                                       lappend_oid(context->glob->relationOids, rte->relid);
                }
 
                /* And recurse into the query's subexpressions */