]> granicus.if.org Git - postgresql/blobdiff - src/backend/optimizer/plan/createplan.c
Rearrange the querytree representation of ORDER BY/GROUP BY/DISTINCT items
[postgresql] / src / backend / optimizer / plan / createplan.c
index b743c8348ed68e40a2b105b8f9beb9e8f86d8495..80b6ed2edfe0bcc64330c8c004725991325c21ba 100644 (file)
@@ -5,96 +5,94 @@
  *       Planning is complete, we just need to convert the selected
  *       Path into a Plan.
  *
- * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.190 2005/05/30 18:55:49 tgl Exp $
+ *       $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.242 2008/08/02 21:32:00 tgl Exp $
  *
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
 
 #include <limits.h>
+#include <math.h>
 
+#include "access/skey.h"
 #include "nodes/makefuncs.h"
-#include "nodes/nodeFuncs.h"
 #include "optimizer/clauses.h"
 #include "optimizer/cost.h"
-#include "optimizer/paths.h"
 #include "optimizer/plancat.h"
 #include "optimizer/planmain.h"
+#include "optimizer/predtest.h"
 #include "optimizer/restrictinfo.h"
 #include "optimizer/tlist.h"
 #include "optimizer/var.h"
-#include "parser/parsetree.h"
 #include "parser/parse_clause.h"
 #include "parser/parse_expr.h"
+#include "parser/parsetree.h"
 #include "utils/lsyscache.h"
-#include "utils/syscache.h"
 
 
-static Scan *create_scan_plan(Query *root, Path *best_path);
+static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
 static List *build_relation_tlist(RelOptInfo *rel);
 static bool use_physical_tlist(RelOptInfo *rel);
 static void disuse_physical_tlist(Plan *plan, Path *path);
-static Join *create_join_plan(Query *root, JoinPath *best_path);
-static Append *create_append_plan(Query *root, AppendPath *best_path);
-static Result *create_result_plan(Query *root, ResultPath *best_path);
-static Material *create_material_plan(Query *root, MaterialPath *best_path);
-static Plan *create_unique_plan(Query *root, UniquePath *best_path);
-static SeqScan *create_seqscan_plan(Query *root, Path *best_path,
+static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
+static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
+static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
+static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
+static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
+static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
+static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
                                        List *tlist, List *scan_clauses);
-static IndexScan *create_indexscan_plan(Query *root, IndexPath *best_path,
-                                         List *tlist, List *scan_clauses,
-                                         List **nonlossy_clauses);
-static BitmapHeapScan *create_bitmap_scan_plan(Query *root,
-                                                                                          BitmapHeapPath *best_path,
-                                                                                          List *tlist, List *scan_clauses);
-static Plan *create_bitmap_subplan(Query *root, Path *bitmapqual,
-                                                                  List **qual, List **indexqual);
-static TidScan *create_tidscan_plan(Query *root, TidPath *best_path,
+static IndexScan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
+                                         List *tlist, List *scan_clauses);
+static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
+                                               BitmapHeapPath *best_path,
+                                               List *tlist, List *scan_clauses);
+static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
+                                         List **qual);
+static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
                                        List *tlist, List *scan_clauses);
-static SubqueryScan *create_subqueryscan_plan(Query *root, Path *best_path,
+static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
                                                 List *tlist, List *scan_clauses);
-static FunctionScan *create_functionscan_plan(Query *root, Path *best_path,
+static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
                                                 List *tlist, List *scan_clauses);
-static NestLoop *create_nestloop_plan(Query *root, NestPath *best_path,
+static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
+                                          List *tlist, List *scan_clauses);
+static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
                                         Plan *outer_plan, Plan *inner_plan);
-static MergeJoin *create_mergejoin_plan(Query *root, MergePath *best_path,
+static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
                                          Plan *outer_plan, Plan *inner_plan);
-static HashJoin *create_hashjoin_plan(Query *root, HashPath *best_path,
+static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
                                         Plan *outer_plan, Plan *inner_plan);
-static void fix_indexqual_references(List *indexquals, IndexPath *index_path,
-                                                List **fixed_indexquals,
-                                                List **nonlossy_indexquals,
-                                                List **indexstrategy,
-                                                List **indexsubtype);
-static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index,
-                                                                 Oid *opclass);
+static List *fix_indexqual_references(List *indexquals, IndexPath *index_path);
+static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index);
 static List *get_switched_clauses(List *clauses, Relids outerrelids);
+static List *order_qual_clauses(PlannerInfo *root, List *clauses);
 static void copy_path_costsize(Plan *dest, Path *src);
 static void copy_plan_costsize(Plan *dest, Plan *src);
 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
                           Oid indexid, List *indexqual, List *indexqualorig,
-                          List *indexstrategy, List *indexsubtype,
                           ScanDirection indexscandir);
 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
-                                                                                         List *indexqual,
-                                                                                         List *indexqualorig,
-                                                                                         List *indexstrategy,
-                                                                                         List *indexsubtype);
+                                         List *indexqual,
+                                         List *indexqualorig);
 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
-                                                                                       List *qpqual,
-                                                                                       Plan *lefttree,
-                                                                                       List *bitmapqualorig,
-                                                                                       Index scanrelid);
+                                        List *qpqual,
+                                        Plan *lefttree,
+                                        List *bitmapqualorig,
+                                        Index scanrelid);
 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
-                        List *tideval);
+                        List *tidquals);
 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
-                                 Index scanrelid);
+                                 Index scanrelid, Node *funcexpr, List *funccolnames,
+                                 List *funccoltypes, List *funccoltypmods);
+static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
+                               Index scanrelid, List *values_lists);
 static BitmapAnd *make_bitmap_and(List *bitmapplans);
 static BitmapOr *make_bitmap_or(List *bitmapplans);
 static NestLoop *make_nestloop(List *tlist,
@@ -110,31 +108,34 @@ static Hash *make_hash(Plan *lefttree);
 static MergeJoin *make_mergejoin(List *tlist,
                           List *joinclauses, List *otherclauses,
                           List *mergeclauses,
+                          Oid *mergefamilies,
+                          int *mergestrategies,
+                          bool *mergenullsfirst,
                           Plan *lefttree, Plan *righttree,
                           JoinType jointype);
-static Sort *make_sort(Query *root, Plan *lefttree, int numCols,
-                 AttrNumber *sortColIdx, Oid *sortOperators);
-static Sort *make_sort_from_pathkeys(Query *root, Plan *lefttree,
-                                               List *pathkeys);
+static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
+                 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
+                 double limit_tuples);
+static Material *make_material(Plan *lefttree);
 
 
 /*
  * create_plan
  *       Creates the access plan for a query by tracing backwards through the
  *       desired chain of pathnodes, starting at the node 'best_path'.  For
- *       every pathnode found:
- *       (1) Create a corresponding plan node containing appropriate id,
- *               target list, and qualification information.
- *       (2) Modify qual clauses of join nodes so that subplan attributes are
- *               referenced using relative values.
- *       (3) Target lists are not modified, but will be in setrefs.c.
+ *       every pathnode found, we create a corresponding plan node containing
+ *       appropriate id, target list, and qualification information.
+ *
+ *       The tlists and quals in the plan tree are still in planner format,
+ *       ie, Vars still correspond to the parser's numbering.  This will be
+ *       fixed later by setrefs.c.
  *
  *       best_path is the best access path
  *
  *       Returns a Plan tree.
  */
 Plan *
-create_plan(Query *root, Path *best_path)
+create_plan(PlannerInfo *root, Path *best_path)
 {
        Plan       *plan;
 
@@ -146,17 +147,18 @@ create_plan(Query *root, Path *best_path)
                case T_TidScan:
                case T_SubqueryScan:
                case T_FunctionScan:
-                       plan = (Plan *) create_scan_plan(root, best_path);
+               case T_ValuesScan:
+                       plan = create_scan_plan(root, best_path);
                        break;
                case T_HashJoin:
                case T_MergeJoin:
                case T_NestLoop:
-                       plan = (Plan *) create_join_plan(root,
-                                                                                        (JoinPath *) best_path);
+                       plan = create_join_plan(root,
+                                                                       (JoinPath *) best_path);
                        break;
                case T_Append:
-                       plan = (Plan *) create_append_plan(root,
-                                                                                          (AppendPath *) best_path);
+                       plan = create_append_plan(root,
+                                                                         (AppendPath *) best_path);
                        break;
                case T_Result:
                        plan = (Plan *) create_result_plan(root,
@@ -164,11 +166,11 @@ create_plan(Query *root, Path *best_path)
                        break;
                case T_Material:
                        plan = (Plan *) create_material_plan(root,
-                                                                                        (MaterialPath *) best_path);
+                                                                                                (MaterialPath *) best_path);
                        break;
                case T_Unique:
-                       plan = (Plan *) create_unique_plan(root,
-                                                                                          (UniquePath *) best_path);
+                       plan = create_unique_plan(root,
+                                                                         (UniquePath *) best_path);
                        break;
                default:
                        elog(ERROR, "unrecognized node type: %d",
@@ -183,24 +185,22 @@ create_plan(Query *root, Path *best_path)
 /*
  * create_scan_plan
  *      Create a scan plan for the parent relation of 'best_path'.
- *
- *      Returns a Plan node.
  */
-static Scan *
-create_scan_plan(Query *root, Path *best_path)
+static Plan *
+create_scan_plan(PlannerInfo *root, Path *best_path)
 {
        RelOptInfo *rel = best_path->parent;
        List       *tlist;
        List       *scan_clauses;
-       Scan       *plan;
+       Plan       *plan;
 
        /*
-        * For table scans, rather than using the relation targetlist (which
-        * is only those Vars actually needed by the query), we prefer to
-        * generate a tlist containing all Vars in order.  This will allow the
-        * executor to optimize away projection of the table tuples, if
-        * possible.  (Note that planner.c may replace the tlist we generate
-        * here, forcing projection to occur.)
+        * For table scans, rather than using the relation targetlist (which is
+        * only those Vars actually needed by the query), we prefer to generate a
+        * tlist containing all Vars in order.  This will allow the executor to
+        * optimize away projection of the table tuples, if possible.  (Note that
+        * planner.c may replace the tlist we generate here, forcing projection to
+        * occur.)
         */
        if (use_physical_tlist(rel))
        {
@@ -213,56 +213,63 @@ create_scan_plan(Query *root, Path *best_path)
                tlist = build_relation_tlist(rel);
 
        /*
-        * Extract the relevant restriction clauses from the parent relation;
-        * the executor must apply all these restrictions during the scan.
+        * Extract the relevant restriction clauses from the parent relation. The
+        * executor must apply all these restrictions during the scan, except for
+        * pseudoconstants which we'll take care of below.
         */
        scan_clauses = rel->baserestrictinfo;
 
        switch (best_path->pathtype)
        {
                case T_SeqScan:
-                       plan = (Scan *) create_seqscan_plan(root,
+                       plan = (Plan *) create_seqscan_plan(root,
                                                                                                best_path,
                                                                                                tlist,
                                                                                                scan_clauses);
                        break;
 
                case T_IndexScan:
-                       plan = (Scan *) create_indexscan_plan(root,
+                       plan = (Plan *) create_indexscan_plan(root,
                                                                                                  (IndexPath *) best_path,
                                                                                                  tlist,
-                                                                                                 scan_clauses,
-                                                                                                 NULL);
+                                                                                                 scan_clauses);
                        break;
 
                case T_BitmapHeapScan:
-                       plan = (Scan *) create_bitmap_scan_plan(root,
-                                                                                                       (BitmapHeapPath *) best_path,
+                       plan = (Plan *) create_bitmap_scan_plan(root,
+                                                                                               (BitmapHeapPath *) best_path,
                                                                                                        tlist,
                                                                                                        scan_clauses);
                        break;
 
                case T_TidScan:
-                       plan = (Scan *) create_tidscan_plan(root,
+                       plan = (Plan *) create_tidscan_plan(root,
                                                                                                (TidPath *) best_path,
                                                                                                tlist,
                                                                                                scan_clauses);
                        break;
 
                case T_SubqueryScan:
-                       plan = (Scan *) create_subqueryscan_plan(root,
+                       plan = (Plan *) create_subqueryscan_plan(root,
                                                                                                         best_path,
                                                                                                         tlist,
                                                                                                         scan_clauses);
                        break;
 
                case T_FunctionScan:
-                       plan = (Scan *) create_functionscan_plan(root,
+                       plan = (Plan *) create_functionscan_plan(root,
                                                                                                         best_path,
                                                                                                         tlist,
                                                                                                         scan_clauses);
                        break;
 
+               case T_ValuesScan:
+                       plan = (Plan *) create_valuesscan_plan(root,
+                                                                                                  best_path,
+                                                                                                  tlist,
+                                                                                                  scan_clauses);
+                       break;
+
                default:
                        elog(ERROR, "unrecognized node type: %d",
                                 (int) best_path->pathtype);
@@ -270,6 +277,14 @@ create_scan_plan(Query *root, Path *best_path)
                        break;
        }
 
+       /*
+        * If there are any pseudoconstant clauses attached to this node, insert a
+        * gating Result node that evaluates the pseudoconstants as one-time
+        * quals.
+        */
+       if (root->hasPseudoConstantQuals)
+               plan = create_gating_plan(root, plan, scan_clauses);
+
        return plan;
 }
 
@@ -308,17 +323,14 @@ use_physical_tlist(RelOptInfo *rel)
        int                     i;
 
        /*
-        * OK for subquery and function scans; otherwise, can't do it for
-        * anything except real relations.
+        * We can do this for real relation scans, subquery scans, function scans,
+        * and values scans (but not for, eg, joins).
         */
-       if (rel->rtekind != RTE_RELATION)
-       {
-               if (rel->rtekind == RTE_SUBQUERY)
-                       return true;
-               if (rel->rtekind == RTE_FUNCTION)
-                       return true;
+       if (rel->rtekind != RTE_RELATION &&
+               rel->rtekind != RTE_SUBQUERY &&
+               rel->rtekind != RTE_FUNCTION &&
+               rel->rtekind != RTE_VALUES)
                return false;
-       }
 
        /*
         * Can't do it with inheritance cases either (mainly because Append
@@ -328,15 +340,16 @@ use_physical_tlist(RelOptInfo *rel)
                return false;
 
        /*
-        * Can't do it if any system columns are requested, either.  (This
-        * could possibly be fixed but would take some fragile assumptions in
-        * setrefs.c, I think.)
+        * Can't do it if any system columns or whole-row Vars are requested,
+        * either.      (This could possibly be fixed but would take some fragile
+        * assumptions in setrefs.c, I think.)
         */
        for (i = rel->min_attr; i <= 0; i++)
        {
                if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
                        return false;
        }
+
        return true;
 }
 
@@ -361,6 +374,7 @@ disuse_physical_tlist(Plan *plan, Path *path)
                case T_TidScan:
                case T_SubqueryScan:
                case T_FunctionScan:
+               case T_ValuesScan:
                        plan->targetlist = build_relation_tlist(path->parent);
                        break;
                default:
@@ -368,19 +382,56 @@ disuse_physical_tlist(Plan *plan, Path *path)
        }
 }
 
+/*
+ * create_gating_plan
+ *       Deal with pseudoconstant qual clauses
+ *
+ * If the node's quals list includes any pseudoconstant quals, put them
+ * into a gating Result node atop the already-built plan.  Otherwise,
+ * return the plan as-is.
+ *
+ * Note that we don't change cost or size estimates when doing gating.
+ * The costs of qual eval were already folded into the plan's startup cost.
+ * Leaving the size alone amounts to assuming that the gating qual will
+ * succeed, which is the conservative estimate for planning upper queries.
+ * We certainly don't want to assume the output size is zero (unless the
+ * gating qual is actually constant FALSE, and that case is dealt with in
+ * clausesel.c).  Interpolating between the two cases is silly, because
+ * it doesn't reflect what will really happen at runtime, and besides which
+ * in most cases we have only a very bad idea of the probability of the gating
+ * qual being true.
+ */
+static Plan *
+create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
+{
+       List       *pseudoconstants;
+
+       /* Sort into desirable execution order while still in RestrictInfo form */
+       quals = order_qual_clauses(root, quals);
+
+       /* Pull out any pseudoconstant quals from the RestrictInfo list */
+       pseudoconstants = extract_actual_clauses(quals, true);
+
+       if (!pseudoconstants)
+               return plan;
+
+       return (Plan *) make_result(root,
+                                                               plan->targetlist,
+                                                               (Node *) pseudoconstants,
+                                                               plan);
+}
+
 /*
  * create_join_plan
  *       Create a join plan for 'best_path' and (recursively) plans for its
  *       inner and outer paths.
- *
- *       Returns a Plan node.
  */
-static Join *
-create_join_plan(Query *root, JoinPath *best_path)
+static Plan *
+create_join_plan(PlannerInfo *root, JoinPath *best_path)
 {
        Plan       *outer_plan;
        Plan       *inner_plan;
-       Join       *plan;
+       Plan       *plan;
 
        outer_plan = create_plan(root, best_path->outerjoinpath);
        inner_plan = create_plan(root, best_path->innerjoinpath);
@@ -388,19 +439,19 @@ create_join_plan(Query *root, JoinPath *best_path)
        switch (best_path->path.pathtype)
        {
                case T_MergeJoin:
-                       plan = (Join *) create_mergejoin_plan(root,
+                       plan = (Plan *) create_mergejoin_plan(root,
                                                                                                  (MergePath *) best_path,
                                                                                                  outer_plan,
                                                                                                  inner_plan);
                        break;
                case T_HashJoin:
-                       plan = (Join *) create_hashjoin_plan(root,
+                       plan = (Plan *) create_hashjoin_plan(root,
                                                                                                 (HashPath *) best_path,
                                                                                                 outer_plan,
                                                                                                 inner_plan);
                        break;
                case T_NestLoop:
-                       plan = (Join *) create_nestloop_plan(root,
+                       plan = (Plan *) create_nestloop_plan(root,
                                                                                                 (NestPath *) best_path,
                                                                                                 outer_plan,
                                                                                                 inner_plan);
@@ -412,17 +463,25 @@ create_join_plan(Query *root, JoinPath *best_path)
                        break;
        }
 
+       /*
+        * If there are any pseudoconstant clauses attached to this node, insert a
+        * gating Result node that evaluates the pseudoconstants as one-time
+        * quals.
+        */
+       if (root->hasPseudoConstantQuals)
+               plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
+
 #ifdef NOT_USED
 
        /*
-        * * Expensive function pullups may have pulled local predicates *
-        * into this path node.  Put them in the qpqual of the plan node. *
-        * JMH, 6/15/92
+        * * Expensive function pullups may have pulled local predicates * into
+        * this path node.      Put them in the qpqual of the plan node. * JMH,
+        * 6/15/92
         */
        if (get_loc_restrictinfo(best_path) != NIL)
                set_qpqual((Plan) plan,
                                   list_concat(get_qpqual((Plan) plan),
-                                  get_actual_clauses(get_loc_restrictinfo(best_path))));
+                                          get_actual_clauses(get_loc_restrictinfo(best_path))));
 #endif
 
        return plan;
@@ -435,14 +494,34 @@ create_join_plan(Query *root, JoinPath *best_path)
  *
  *       Returns a Plan node.
  */
-static Append *
-create_append_plan(Query *root, AppendPath *best_path)
+static Plan *
+create_append_plan(PlannerInfo *root, AppendPath *best_path)
 {
        Append     *plan;
        List       *tlist = build_relation_tlist(best_path->path.parent);
        List       *subplans = NIL;
        ListCell   *subpaths;
 
+       /*
+        * It is possible for the subplans list to contain only one entry, or even
+        * no entries.  Handle these cases specially.
+        *
+        * XXX ideally, if there's just one entry, we'd not bother to generate an
+        * Append node but just return the single child.  At the moment this does
+        * not work because the varno of the child scan plan won't match the
+        * parent-rel Vars it'll be asked to emit.
+        */
+       if (best_path->subpaths == NIL)
+       {
+               /* Generate a Result plan with constant-FALSE gating qual */
+               return (Plan *) make_result(root,
+                                                                       tlist,
+                                                                       (Node *) list_make1(makeBoolConst(false,
+                                                                                                                                         false)),
+                                                                       NULL);
+       }
+
+       /* Normal case with multiple subpaths */
        foreach(subpaths, best_path->subpaths)
        {
                Path       *subpath = (Path *) lfirst(subpaths);
@@ -452,39 +531,31 @@ create_append_plan(Query *root, AppendPath *best_path)
 
        plan = make_append(subplans, false, tlist);
 
-       return plan;
+       return (Plan *) plan;
 }
 
 /*
  * create_result_plan
- *       Create a Result plan for 'best_path' and (recursively) plans
- *       for its subpaths.
+ *       Create a Result plan for 'best_path'.
+ *       This is only used for the case of a query with an empty jointree.
  *
  *       Returns a Plan node.
  */
 static Result *
-create_result_plan(Query *root, ResultPath *best_path)
+create_result_plan(PlannerInfo *root, ResultPath *best_path)
 {
-       Result     *plan;
        List       *tlist;
-       List       *constclauses;
-       Plan       *subplan;
+       List       *quals;
 
-       if (best_path->path.parent)
-               tlist = build_relation_tlist(best_path->path.parent);
-       else
-               tlist = NIL;                    /* will be filled in later */
-
-       if (best_path->subpath)
-               subplan = create_plan(root, best_path->subpath);
-       else
-               subplan = NULL;
+       /* The tlist will be installed later, since we have no RelOptInfo */
+       Assert(best_path->path.parent == NULL);
+       tlist = NIL;
 
-       constclauses = order_qual_clauses(root, best_path->constantqual);
+       /* best_path->quals is just bare clauses */
 
-       plan = make_result(tlist, (Node *) constclauses, subplan);
+       quals = order_qual_clauses(root, best_path->quals);
 
-       return plan;
+       return make_result(root, tlist, (Node *) quals, NULL);
 }
 
 /*
@@ -495,7 +566,7 @@ create_result_plan(Query *root, ResultPath *best_path)
  *       Returns a Plan node.
  */
 static Material *
-create_material_plan(Query *root, MaterialPath *best_path)
+create_material_plan(PlannerInfo *root, MaterialPath *best_path)
 {
        Material   *plan;
        Plan       *subplan;
@@ -520,46 +591,51 @@ create_material_plan(Query *root, MaterialPath *best_path)
  *       Returns a Plan node.
  */
 static Plan *
-create_unique_plan(Query *root, UniquePath *best_path)
+create_unique_plan(PlannerInfo *root, UniquePath *best_path)
 {
        Plan       *plan;
        Plan       *subplan;
        List       *uniq_exprs;
-       int                     numGroupCols;
-       AttrNumber *groupColIdx;
-       int                     groupColPos;
+       List       *in_operators;
        List       *newtlist;
        int                     nextresno;
        bool            newitems;
+       int                     numGroupCols;
+       AttrNumber *groupColIdx;
+       int                     groupColPos;
        ListCell   *l;
 
        subplan = create_plan(root, best_path->subpath);
 
-       /*
+       /* Done if we don't need to do any actual unique-ifying */
+       if (best_path->umethod == UNIQUE_PATH_NOOP)
+               return subplan;
+
+       /*----------
         * As constructed, the subplan has a "flat" tlist containing just the
         * Vars needed here and at upper levels.  The values we are supposed
         * to unique-ify may be expressions in these variables.  We have to
-        * add any such expressions to the subplan's tlist.  We then build
-        * control information showing which subplan output columns are to be
-        * examined by the grouping step.  (Since we do not remove any
-        * existing subplan outputs, not all the output columns may be used
-        * for grouping.)
+        * add any such expressions to the subplan's tlist.
         *
-        * Note: the reason we don't remove any subplan outputs is that there are
-        * scenarios where a Var is needed at higher levels even though it is
-        * not one of the nominal outputs of an IN clause.      Consider WHERE x
-        * IN (SELECT y FROM t1,t2 WHERE y = z) Implied equality deduction
-        * will generate an "x = z" clause, which may get used instead of "x =
-        * y" in the upper join step.  Therefore the sub-select had better
-        * deliver both y and z in its targetlist.      It is sufficient to
-        * unique-ify on y, however.
+        * The subplan may have a "physical" tlist if it is a simple scan plan.
+        * If we're going to sort, this should be reduced to the regular tlist,
+        * so that we don't sort more data than we need to.  For hashing, the
+        * tlist should be left as-is if we don't need to add any expressions;
+        * but if we do have to add expressions, then a projection step will be
+        * needed at runtime anyway, so we may as well remove unneeded items.
+        * Therefore newtlist starts from build_relation_tlist() not just a
+        * copy of the subplan's tlist; and we don't install it into the subplan
+        * unless we are sorting or stuff has to be added.
         *
         * To find the correct list of values to unique-ify, we look in the
         * information saved for IN expressions.  If this code is ever used in
         * other scenarios, some other way of finding what to unique-ify will
-        * be needed.
+        * be needed.  The IN clause's operators are needed too, since they
+        * determine what the meaning of "unique" is in this context.
+        *----------
         */
        uniq_exprs = NIL;                       /* just to keep compiler quiet */
+       in_operators = NIL;
        foreach(l, root->in_info_list)
        {
                InClauseInfo *ininfo = (InClauseInfo *) lfirst(l);
@@ -567,18 +643,15 @@ create_unique_plan(Query *root, UniquePath *best_path)
                if (bms_equal(ininfo->righthand, best_path->path.parent->relids))
                {
                        uniq_exprs = ininfo->sub_targetlist;
+                       in_operators = ininfo->in_operators;
                        break;
                }
        }
        if (l == NULL)                          /* fell out of loop? */
                elog(ERROR, "could not find UniquePath in in_info_list");
 
-       /* set up to record positions of unique columns */
-       numGroupCols = list_length(uniq_exprs);
-       groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
-       groupColPos = 0;
-       /* not sure if tlist might be shared with other nodes, so copy */
-       newtlist = copyObject(subplan->targetlist);
+       /* initialize modified subplan tlist as just the "required" vars */
+       newtlist = build_relation_tlist(best_path->path.parent);
        nextresno = list_length(newtlist) + 1;
        newitems = false;
 
@@ -598,37 +671,80 @@ create_unique_plan(Query *root, UniquePath *best_path)
                        nextresno++;
                        newitems = true;
                }
-               groupColIdx[groupColPos++] = tle->resno;
        }
 
-       if (newitems)
+       if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
        {
                /*
-                * If the top plan node can't do projections, we need to add a
-                * Result node to help it along.
+                * If the top plan node can't do projections, we need to add a Result
+                * node to help it along.
                 */
                if (!is_projection_capable_plan(subplan))
-                       subplan = (Plan *) make_result(newtlist, NULL, subplan);
+                       subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
                else
                        subplan->targetlist = newtlist;
        }
 
-       /* Done if we don't need to do any actual unique-ifying */
-       if (best_path->umethod == UNIQUE_PATH_NOOP)
-               return subplan;
+       /*
+        * Build control information showing which subplan output columns are to
+        * be examined by the grouping step.  Unfortunately we can't merge this
+        * with the previous loop, since we didn't then know which version of the
+        * subplan tlist we'd end up using.
+        */
+       newtlist = subplan->targetlist;
+       numGroupCols = list_length(uniq_exprs);
+       groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
+
+       groupColPos = 0;
+       foreach(l, uniq_exprs)
+       {
+               Node       *uniqexpr = lfirst(l);
+               TargetEntry *tle;
+
+               tle = tlist_member(uniqexpr, newtlist);
+               if (!tle)                               /* shouldn't happen */
+                       elog(ERROR, "failed to find unique expression in subplan tlist");
+               groupColIdx[groupColPos++] = tle->resno;
+       }
 
        if (best_path->umethod == UNIQUE_PATH_HASH)
        {
                long            numGroups;
+               Oid                *groupOperators;
 
                numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
 
+               /*
+                * Get the hashable equality operators for the Agg node to use.
+                * Normally these are the same as the IN clause operators, but if
+                * those are cross-type operators then the equality operators are the
+                * ones for the IN clause operators' RHS datatype.
+                */
+               groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
+               groupColPos = 0;
+               foreach(l, in_operators)
+               {
+                       Oid                     in_oper = lfirst_oid(l);
+                       Oid                     eq_oper;
+
+                       if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
+                               elog(ERROR, "could not find compatible hash operator for operator %u",
+                                        in_oper);
+                       groupOperators[groupColPos++] = eq_oper;
+               }
+
+               /*
+                * Since the Agg node is going to project anyway, we can give it the
+                * minimum output tlist, without any stuff we might have added to the
+                * subplan tlist.
+                */
                plan = (Plan *) make_agg(root,
-                                                                copyObject(subplan->targetlist),
+                                                                build_relation_tlist(best_path->path.parent),
                                                                 NIL,
                                                                 AGG_HASHED,
                                                                 numGroupCols,
                                                                 groupColIdx,
+                                                                groupOperators,
                                                                 numGroups,
                                                                 0,
                                                                 subplan);
@@ -637,16 +753,30 @@ create_unique_plan(Query *root, UniquePath *best_path)
        {
                List       *sortList = NIL;
 
-               for (groupColPos = 0; groupColPos < numGroupCols; groupColPos++)
+               /* Create an ORDER BY list to sort the input compatibly */
+               groupColPos = 0;
+               foreach(l, in_operators)
                {
+                       Oid                     in_oper = lfirst_oid(l);
+                       Oid                     sortop;
                        TargetEntry *tle;
+                       SortGroupClause *sortcl;
 
+                       sortop = get_ordering_op_for_equality_op(in_oper, false);
+                       if (!OidIsValid(sortop))        /* shouldn't happen */
+                               elog(ERROR, "could not find ordering operator for equality operator %u",
+                                        in_oper);
                        tle = get_tle_by_resno(subplan->targetlist,
                                                                   groupColIdx[groupColPos]);
                        Assert(tle != NULL);
-                       sortList = addTargetToSortList(NULL, tle,
-                                                                                  sortList, subplan->targetlist,
-                                                                                  SORTBY_ASC, NIL, false);
+                       sortcl = makeNode(SortGroupClause);
+                       sortcl->tleSortGroupRef = assignSortGroupRef(tle,
+                                                                                                                subplan->targetlist);
+                       sortcl->eqop = in_oper;
+                       sortcl->sortop = sortop;
+                       sortcl->nulls_first = false;
+                       sortList = lappend(sortList, sortcl);
+                       groupColPos++;
                }
                plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
                plan = (Plan *) make_unique(plan, sortList);
@@ -672,7 +802,7 @@ create_unique_plan(Query *root, UniquePath *best_path)
  *      with restriction clauses 'scan_clauses' and targetlist 'tlist'.
  */
 static SeqScan *
-create_seqscan_plan(Query *root, Path *best_path,
+create_seqscan_plan(PlannerInfo *root, Path *best_path,
                                        List *tlist, List *scan_clauses)
 {
        SeqScan    *scan_plan;
@@ -682,12 +812,12 @@ create_seqscan_plan(Query *root, Path *best_path,
        Assert(scan_relid > 0);
        Assert(best_path->parent->rtekind == RTE_RELATION);
 
-       /* Reduce RestrictInfo list to bare expressions */
-       scan_clauses = get_actual_clauses(scan_clauses);
-
        /* Sort clauses into best execution order */
        scan_clauses = order_qual_clauses(root, scan_clauses);
 
+       /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
+       scan_clauses = extract_actual_clauses(scan_clauses, false);
+
        scan_plan = make_seqscan(tlist,
                                                         scan_clauses,
                                                         scan_relid);
@@ -705,16 +835,12 @@ create_seqscan_plan(Query *root, Path *best_path,
  * The indexquals list of the path contains implicitly-ANDed qual conditions.
  * The list can be empty --- then no index restrictions will be applied during
  * the scan.
- *
- * If nonlossy_clauses isn't NULL, *nonlossy_clauses receives a list of the
- * nonlossy indexquals.
  */
 static IndexScan *
-create_indexscan_plan(Query *root,
+create_indexscan_plan(PlannerInfo *root,
                                          IndexPath *best_path,
                                          List *tlist,
-                                         List *scan_clauses,
-                                         List **nonlossy_clauses)
+                                         List *scan_clauses)
 {
        List       *indexquals = best_path->indexquals;
        Index           baserelid = best_path->path.parent->relid;
@@ -722,9 +848,6 @@ create_indexscan_plan(Query *root,
        List       *qpqual;
        List       *stripped_indexquals;
        List       *fixed_indexquals;
-       List       *nonlossy_indexquals;
-       List       *indexstrategy;
-       List       *indexsubtype;
        ListCell   *l;
        IndexScan  *scan_plan;
 
@@ -739,28 +862,18 @@ create_indexscan_plan(Query *root,
        stripped_indexquals = get_actual_clauses(indexquals);
 
        /*
-        * The executor needs a copy with the indexkey on the left of each
-        * clause and with index attr numbers substituted for table ones. This
-        * pass also gets strategy info and looks for "lossy" operators.
+        * The executor needs a copy with the indexkey on the left of each clause
+        * and with index attr numbers substituted for table ones.
         */
-       fix_indexqual_references(indexquals, best_path,
-                                                        &fixed_indexquals,
-                                                        &nonlossy_indexquals,
-                                                        &indexstrategy,
-                                                        &indexsubtype);
-
-       /* pass back nonlossy quals if caller wants 'em */
-       if (nonlossy_clauses)
-               *nonlossy_clauses = nonlossy_indexquals;
+       fixed_indexquals = fix_indexqual_references(indexquals, best_path);
 
        /*
         * If this is an innerjoin scan, the indexclauses will contain join
-        * clauses that are not present in scan_clauses (since the passed-in
-        * value is just the rel's baserestrictinfo list).  We must add these
-        * clauses to scan_clauses to ensure they get checked.  In most cases
-        * we will remove the join clauses again below, but if a join clause
-        * contains a special operator, we need to make sure it gets into the
-        * scan_clauses.
+        * clauses that are not present in scan_clauses (since the passed-in value
+        * is just the rel's baserestrictinfo list).  We must add these clauses to
+        * scan_clauses to ensure they get checked.  In most cases we will remove
+        * the join clauses again below, but if a join clause contains a special
+        * operator, we need to make sure it gets into the scan_clauses.
         *
         * Note: pointer comparison should be enough to determine RestrictInfo
         * matches.
@@ -769,23 +882,25 @@ create_indexscan_plan(Query *root,
                scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
 
        /*
-        * The qpqual list must contain all restrictions not automatically
-        * handled by the index.  All the predicates in the indexquals will be
-        * checked (either by the index itself, or by nodeIndexscan.c), but if
-        * there are any "special" operators involved then they must be included
-        * in qpqual.  Also, any lossy index operators must be rechecked in
-        * the qpqual.  The upshot is that qpqual must contain scan_clauses
-        * minus whatever appears in nonlossy_indexquals.
+        * The qpqual list must contain all restrictions not automatically handled
+        * by the index.  All the predicates in the indexquals will be checked
+        * (either by the index itself, or by nodeIndexscan.c), but if there are
+        * any "special" operators involved then they must be included in qpqual.
+        * The upshot is that qpqual must contain scan_clauses minus whatever
+        * appears in indexquals.
         *
-        * In normal cases simple pointer equality checks will be enough to
-        * spot duplicate RestrictInfos, so we try that first.  In some situations
-        * (particularly with OR'd index conditions) we may have scan_clauses
-        * that are not equal to, but are logically implied by, the index quals;
-        * so we also try a pred_test() check to see if we can discard quals
-        * that way.
+        * In normal cases simple pointer equality checks will be enough to spot
+        * duplicate RestrictInfos, so we try that first.  In some situations
+        * (particularly with OR'd index conditions) we may have scan_clauses that
+        * are not equal to, but are logically implied by, the index quals; so we
+        * also try a predicate_implied_by() check to see if we can discard quals
+        * that way.  (predicate_implied_by assumes its first input contains only
+        * immutable functions, so we have to check that.)
         *
-        * While at it, we strip off the RestrictInfos to produce a list of
-        * plain expressions.
+        * We can also discard quals that are implied by a partial index's
+        * predicate, but only in a plain SELECT; when scanning a target relation
+        * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
+        * plan so that they'll be properly rechecked by EvalPlanQual testing.
         */
        qpqual = NIL;
        foreach(l, scan_clauses)
@@ -793,16 +908,34 @@ create_indexscan_plan(Query *root,
                RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
 
                Assert(IsA(rinfo, RestrictInfo));
-               if (list_member_ptr(nonlossy_indexquals, rinfo))
-                       continue;
-               if (pred_test(list_make1(rinfo->clause), nonlossy_indexquals))
+               if (rinfo->pseudoconstant)
+                       continue;                       /* we may drop pseudoconstants here */
+               if (list_member_ptr(indexquals, rinfo))
                        continue;
-               qpqual = lappend(qpqual, rinfo->clause);
+               if (!contain_mutable_functions((Node *) rinfo->clause))
+               {
+                       List       *clausel = list_make1(rinfo->clause);
+
+                       if (predicate_implied_by(clausel, indexquals))
+                               continue;
+                       if (best_path->indexinfo->indpred)
+                       {
+                               if (baserelid != root->parse->resultRelation &&
+                                       get_rowmark(root->parse, baserelid) == NULL)
+                                       if (predicate_implied_by(clausel,
+                                                                                        best_path->indexinfo->indpred))
+                                               continue;
+                       }
+               }
+               qpqual = lappend(qpqual, rinfo);
        }
 
        /* Sort clauses into best execution order */
        qpqual = order_qual_clauses(root, qpqual);
 
+       /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
+       qpqual = extract_actual_clauses(qpqual, false);
+
        /* Finally ready to build the plan node */
        scan_plan = make_indexscan(tlist,
                                                           qpqual,
@@ -810,8 +943,6 @@ create_indexscan_plan(Query *root,
                                                           indexoid,
                                                           fixed_indexquals,
                                                           stripped_indexquals,
-                                                          indexstrategy,
-                                                          indexsubtype,
                                                           best_path->indexscandir);
 
        copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
@@ -827,7 +958,7 @@ create_indexscan_plan(Query *root,
  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
  */
 static BitmapHeapScan *
-create_bitmap_scan_plan(Query *root,
+create_bitmap_scan_plan(PlannerInfo *root,
                                                BitmapHeapPath *best_path,
                                                List *tlist,
                                                List *scan_clauses)
@@ -835,7 +966,6 @@ create_bitmap_scan_plan(Query *root,
        Index           baserelid = best_path->path.parent->relid;
        Plan       *bitmapqualplan;
        List       *bitmapqualorig;
-       List       *indexquals;
        List       *qpqual;
        ListCell   *l;
        BitmapHeapScan *scan_plan;
@@ -844,50 +974,62 @@ create_bitmap_scan_plan(Query *root,
        Assert(baserelid > 0);
        Assert(best_path->path.parent->rtekind == RTE_RELATION);
 
-       /* Process the bitmapqual tree into a Plan tree and qual lists */
+       /* Process the bitmapqual tree into a Plan tree and qual list */
        bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
-                                                                                  &bitmapqualorig, &indexquals);
+                                                                                  &bitmapqualorig);
 
-       /* Reduce RestrictInfo list to bare expressions */
-       scan_clauses = get_actual_clauses(scan_clauses);
+       /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
+       scan_clauses = extract_actual_clauses(scan_clauses, false);
 
        /*
-        * If this is a innerjoin scan, the indexclauses will contain join
-        * clauses that are not present in scan_clauses (since the passed-in
-        * value is just the rel's baserestrictinfo list).  We must add these
-        * clauses to scan_clauses to ensure they get checked.  In most cases
-        * we will remove the join clauses again below, but if a join clause
-        * contains a special operator, we need to make sure it gets into the
-        * scan_clauses.
+        * If this is a innerjoin scan, the indexclauses will contain join clauses
+        * that are not present in scan_clauses (since the passed-in value is just
+        * the rel's baserestrictinfo list).  We must add these clauses to
+        * scan_clauses to ensure they get checked.  In most cases we will remove
+        * the join clauses again below, but if a join clause contains a special
+        * operator, we need to make sure it gets into the scan_clauses.
         */
        if (best_path->isjoininner)
        {
-               scan_clauses = list_union(scan_clauses, bitmapqualorig);
+               scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
        }
 
        /*
-        * The qpqual list must contain all restrictions not automatically
-        * handled by the index.  All the predicates in the indexquals will be
-        * checked (either by the index itself, or by nodeBitmapHeapscan.c),
-        * but if there are any "special" or lossy operators involved then they
-        * must be added to qpqual.  The upshot is that qpquals must contain
-        * scan_clauses minus whatever appears in indexquals.
+        * The qpqual list must contain all restrictions not automatically handled
+        * by the index.  All the predicates in the indexquals will be checked
+        * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
+        * are any "special" operators involved then they must be added to qpqual.
+        * The upshot is that qpqual must contain scan_clauses minus whatever
+        * appears in bitmapqualorig.
         *
         * In normal cases simple equal() checks will be enough to spot duplicate
         * clauses, so we try that first.  In some situations (particularly with
         * OR'd index conditions) we may have scan_clauses that are not equal to,
         * but are logically implied by, the index quals; so we also try a
-        * pred_test() check to see if we can discard quals that way.
+        * predicate_implied_by() check to see if we can discard quals that way.
+        * (predicate_implied_by assumes its first input contains only immutable
+        * functions, so we have to check that.)
+        *
+        * Unlike create_indexscan_plan(), we need take no special thought here
+        * for partial index predicates; this is because the predicate conditions
+        * are already listed in bitmapqualorig.  Bitmap scans have to do it that
+        * way because predicate conditions need to be rechecked if the scan's
+        * bitmap becomes lossy.
         */
        qpqual = NIL;
        foreach(l, scan_clauses)
        {
-               Node   *clause = (Node *) lfirst(l);
+               Node       *clause = (Node *) lfirst(l);
 
-               if (list_member(indexquals, clause))
-                       continue;
-               if (pred_test(list_make1(clause), indexquals))
+               if (list_member(bitmapqualorig, clause))
                        continue;
+               if (!contain_mutable_functions(clause))
+               {
+                       List       *clausel = list_make1(clause);
+
+                       if (predicate_implied_by(clausel, bitmapqualorig))
+                               continue;
+               }
                qpqual = lappend(qpqual, clause);
        }
 
@@ -895,7 +1037,7 @@ create_bitmap_scan_plan(Query *root,
        qpqual = order_qual_clauses(root, qpqual);
 
        /*
-        * When dealing with special or lossy operators, we will at this point
+        * When dealing with special operators, we will at this point
         * have duplicate clauses in qpqual and bitmapqualorig.  We may as well
         * drop 'em from bitmapqualorig, since there's no point in making the
         * tests twice.
@@ -919,14 +1061,19 @@ create_bitmap_scan_plan(Query *root,
 /*
  * Given a bitmapqual tree, generate the Plan tree that implements it
  *
- * As byproducts, we also return in *qual and *indexqual the qual lists
- * (in implicit-AND form, without RestrictInfos) describing the original index
- * conditions and the generated indexqual conditions.  The latter is made to
- * exclude lossy index operators.
+ * As a byproduct, we also return in *qual a qual list (in implicit-AND
+ * form, without RestrictInfos) describing the generated indexqual
+ * conditions, as needed for rechecking heap tuples in lossy cases.
+ * This list also includes partial-index predicates, because we have to
+ * recheck predicates as well as index conditions if the scan's bitmap
+ * becomes lossy.
+ *
+ * Note: if you find yourself changing this, you probably need to change
+ * make_restrictinfo_from_bitmapqual too.
  */
 static Plan *
-create_bitmap_subplan(Query *root, Path *bitmapqual,
-                                         List **qual, List **indexqual)
+create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
+                                         List **qual)
 {
        Plan       *plan;
 
@@ -935,20 +1082,24 @@ create_bitmap_subplan(Query *root, Path *bitmapqual,
                BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
                List       *subplans = NIL;
                List       *subquals = NIL;
-               List       *subindexquals = NIL;
                ListCell   *l;
 
+               /*
+                * There may well be redundant quals among the subplans, since a
+                * top-level WHERE qual might have gotten used to form several
+                * different index quals.  We don't try exceedingly hard to eliminate
+                * redundancies, but we do eliminate obvious duplicates by using
+                * list_concat_unique.
+                */
                foreach(l, apath->bitmapquals)
                {
-                       Plan   *subplan;
-                       List   *subqual;
-                       List   *subindexqual;
+                       Plan       *subplan;
+                       List       *subqual;
 
                        subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
-                                                                                       &subqual, &subindexqual);
+                                                                                       &subqual);
                        subplans = lappend(subplans, subplan);
-                       subquals = list_concat(subquals, subqual);
-                       subindexquals = list_concat(subindexquals, subindexqual);
+                       subquals = list_concat_unique(subquals, subqual);
                }
                plan = (Plan *) make_bitmap_and(subplans);
                plan->startup_cost = apath->path.startup_cost;
@@ -957,62 +1108,101 @@ create_bitmap_subplan(Query *root, Path *bitmapqual,
                        clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
                plan->plan_width = 0;   /* meaningless */
                *qual = subquals;
-               *indexqual = subindexquals;
        }
        else if (IsA(bitmapqual, BitmapOrPath))
        {
                BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
                List       *subplans = NIL;
                List       *subquals = NIL;
-               List       *subindexquals = NIL;
+               bool            const_true_subqual = false;
                ListCell   *l;
 
+               /*
+                * Here, we only detect qual-free subplans.  A qual-free subplan would
+                * cause us to generate "... OR true ..."  which we may as well reduce
+                * to just "true".      We do not try to eliminate redundant subclauses
+                * because (a) it's not as likely as in the AND case, and (b) we might
+                * well be working with hundreds or even thousands of OR conditions,
+                * perhaps from a long IN list.  The performance of list_append_unique
+                * would be unacceptable.
+                */
                foreach(l, opath->bitmapquals)
                {
-                       Plan   *subplan;
-                       List   *subqual;
-                       List   *subindexqual;
+                       Plan       *subplan;
+                       List       *subqual;
 
                        subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
-                                                                                       &subqual, &subindexqual);
+                                                                                       &subqual);
                        subplans = lappend(subplans, subplan);
-                       subquals = lappend(subquals,
-                                                          make_ands_explicit(subqual));
-                       subindexquals = lappend(subindexquals,
-                                                                       make_ands_explicit(subindexqual));
+                       if (subqual == NIL)
+                               const_true_subqual = true;
+                       else if (!const_true_subqual)
+                               subquals = lappend(subquals,
+                                                                  make_ands_explicit(subqual));
                }
-               plan = (Plan *) make_bitmap_or(subplans);
-               plan->startup_cost = opath->path.startup_cost;
-               plan->total_cost = opath->path.total_cost;
-               plan->plan_rows =
-                       clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
-               plan->plan_width = 0;   /* meaningless */
-               *qual = list_make1(make_orclause(subquals));
-               *indexqual = list_make1(make_orclause(subindexquals));
+
+               /*
+                * In the presence of ScalarArrayOpExpr quals, we might have built
+                * BitmapOrPaths with just one subpath; don't add an OR step.
+                */
+               if (list_length(subplans) == 1)
+               {
+                       plan = (Plan *) linitial(subplans);
+               }
+               else
+               {
+                       plan = (Plan *) make_bitmap_or(subplans);
+                       plan->startup_cost = opath->path.startup_cost;
+                       plan->total_cost = opath->path.total_cost;
+                       plan->plan_rows =
+                               clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
+                       plan->plan_width = 0;           /* meaningless */
+               }
+
+               /*
+                * If there were constant-TRUE subquals, the OR reduces to constant
+                * TRUE.  Also, avoid generating one-element ORs, which could happen
+                * due to redundancy elimination or ScalarArrayOpExpr quals.
+                */
+               if (const_true_subqual)
+                       *qual = NIL;
+               else if (list_length(subquals) <= 1)
+                       *qual = subquals;
+               else
+                       *qual = list_make1(make_orclause(subquals));
        }
        else if (IsA(bitmapqual, IndexPath))
        {
-               IndexPath *ipath = (IndexPath *) bitmapqual;
-               IndexScan *iscan;
-               List   *nonlossy_clauses;
+               IndexPath  *ipath = (IndexPath *) bitmapqual;
+               IndexScan  *iscan;
+               ListCell   *l;
 
                /* Use the regular indexscan plan build machinery... */
-               iscan = create_indexscan_plan(root, ipath, NIL, NIL,
-                                                                         &nonlossy_clauses);
+               iscan = create_indexscan_plan(root, ipath, NIL, NIL);
                /* then convert to a bitmap indexscan */
                plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
                                                                                          iscan->indexid,
                                                                                          iscan->indexqual,
-                                                                                         iscan->indexqualorig,
-                                                                                         iscan->indexstrategy,
-                                                                                         iscan->indexsubtype);
+                                                                                         iscan->indexqualorig);
                plan->startup_cost = 0.0;
                plan->total_cost = ipath->indextotalcost;
                plan->plan_rows =
                        clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
                plan->plan_width = 0;   /* meaningless */
                *qual = get_actual_clauses(ipath->indexclauses);
-               *indexqual = get_actual_clauses(nonlossy_clauses);
+               foreach(l, ipath->indexinfo->indpred)
+               {
+                       Expr       *pred = (Expr *) lfirst(l);
+
+                       /*
+                        * We know that the index predicate must have been implied by the
+                        * query condition as a whole, but it may or may not be implied by
+                        * the conditions that got pushed into the bitmapqual.  Avoid
+                        * generating redundant conditions.
+                        */
+                       if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
+                               *qual = lappend(*qual, pred);
+               }
        }
        else
        {
@@ -1029,26 +1219,36 @@ create_bitmap_subplan(Query *root, Path *bitmapqual,
  *      with restriction clauses 'scan_clauses' and targetlist 'tlist'.
  */
 static TidScan *
-create_tidscan_plan(Query *root, TidPath *best_path,
+create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
                                        List *tlist, List *scan_clauses)
 {
        TidScan    *scan_plan;
        Index           scan_relid = best_path->path.parent->relid;
+       List       *ortidquals;
 
        /* it should be a base rel... */
        Assert(scan_relid > 0);
        Assert(best_path->path.parent->rtekind == RTE_RELATION);
 
-       /* Reduce RestrictInfo list to bare expressions */
-       scan_clauses = get_actual_clauses(scan_clauses);
-
        /* Sort clauses into best execution order */
        scan_clauses = order_qual_clauses(root, scan_clauses);
 
+       /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
+       scan_clauses = extract_actual_clauses(scan_clauses, false);
+
+       /*
+        * Remove any clauses that are TID quals.  This is a bit tricky since the
+        * tidquals list has implicit OR semantics.
+        */
+       ortidquals = best_path->tidquals;
+       if (list_length(ortidquals) > 1)
+               ortidquals = list_make1(make_orclause(ortidquals));
+       scan_clauses = list_difference(scan_clauses, ortidquals);
+
        scan_plan = make_tidscan(tlist,
                                                         scan_clauses,
                                                         scan_relid,
-                                                        best_path->tideval);
+                                                        best_path->tidquals);
 
        copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
 
@@ -1061,7 +1261,7 @@ create_tidscan_plan(Query *root, TidPath *best_path,
  *      with restriction clauses 'scan_clauses' and targetlist 'tlist'.
  */
 static SubqueryScan *
-create_subqueryscan_plan(Query *root, Path *best_path,
+create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
                                                 List *tlist, List *scan_clauses)
 {
        SubqueryScan *scan_plan;
@@ -1071,16 +1271,17 @@ create_subqueryscan_plan(Query *root, Path *best_path,
        Assert(scan_relid > 0);
        Assert(best_path->parent->rtekind == RTE_SUBQUERY);
 
-       /* Reduce RestrictInfo list to bare expressions */
-       scan_clauses = get_actual_clauses(scan_clauses);
-
        /* Sort clauses into best execution order */
        scan_clauses = order_qual_clauses(root, scan_clauses);
 
+       /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
+       scan_clauses = extract_actual_clauses(scan_clauses, false);
+
        scan_plan = make_subqueryscan(tlist,
                                                                  scan_clauses,
                                                                  scan_relid,
-                                                                 best_path->parent->subplan);
+                                                                 best_path->parent->subplan,
+                                                                 best_path->parent->subrtable);
 
        copy_path_costsize(&scan_plan->scan.plan, best_path);
 
@@ -1093,23 +1294,61 @@ create_subqueryscan_plan(Query *root, Path *best_path,
  *      with restriction clauses 'scan_clauses' and targetlist 'tlist'.
  */
 static FunctionScan *
-create_functionscan_plan(Query *root, Path *best_path,
+create_functionscan_plan(PlannerInfo *root, Path *best_path,
                                                 List *tlist, List *scan_clauses)
 {
        FunctionScan *scan_plan;
        Index           scan_relid = best_path->parent->relid;
+       RangeTblEntry *rte;
 
        /* it should be a function base rel... */
        Assert(scan_relid > 0);
-       Assert(best_path->parent->rtekind == RTE_FUNCTION);
+       rte = planner_rt_fetch(scan_relid, root);
+       Assert(rte->rtekind == RTE_FUNCTION);
+
+       /* Sort clauses into best execution order */
+       scan_clauses = order_qual_clauses(root, scan_clauses);
+
+       /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
+       scan_clauses = extract_actual_clauses(scan_clauses, false);
 
-       /* Reduce RestrictInfo list to bare expressions */
-       scan_clauses = get_actual_clauses(scan_clauses);
+       scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
+                                                                 rte->funcexpr,
+                                                                 rte->eref->colnames,
+                                                                 rte->funccoltypes,
+                                                                 rte->funccoltypmods);
+
+       copy_path_costsize(&scan_plan->scan.plan, best_path);
+
+       return scan_plan;
+}
+
+/*
+ * create_valuesscan_plan
+ *      Returns a valuesscan plan for the base relation scanned by 'best_path'
+ *      with restriction clauses 'scan_clauses' and targetlist 'tlist'.
+ */
+static ValuesScan *
+create_valuesscan_plan(PlannerInfo *root, Path *best_path,
+                                          List *tlist, List *scan_clauses)
+{
+       ValuesScan *scan_plan;
+       Index           scan_relid = best_path->parent->relid;
+       RangeTblEntry *rte;
+
+       /* it should be a values base rel... */
+       Assert(scan_relid > 0);
+       rte = planner_rt_fetch(scan_relid, root);
+       Assert(rte->rtekind == RTE_VALUES);
 
        /* Sort clauses into best execution order */
        scan_clauses = order_qual_clauses(root, scan_clauses);
 
-       scan_plan = make_functionscan(tlist, scan_clauses, scan_relid);
+       /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
+       scan_clauses = extract_actual_clauses(scan_clauses, false);
+
+       scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
+                                                               rte->values_lists);
 
        copy_path_costsize(&scan_plan->scan.plan, best_path);
 
@@ -1123,7 +1362,7 @@ create_functionscan_plan(Query *root, Path *best_path,
  *****************************************************************************/
 
 static NestLoop *
-create_nestloop_plan(Query *root,
+create_nestloop_plan(PlannerInfo *root,
                                         NestPath *best_path,
                                         Plan *outer_plan,
                                         Plan *inner_plan)
@@ -1137,34 +1376,42 @@ create_nestloop_plan(Query *root,
        if (IsA(best_path->innerjoinpath, IndexPath))
        {
                /*
-                * 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 may remove those join clauses from the list
-                * of clauses that have to be checked as qpquals at the join node.
+                * 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 may remove those join clauses from the list of clauses
+                * that have to be checked as qpquals at the join node.
                 *
                 * We can also remove any join clauses that are redundant with those
-                * being used in the index scan; prior redundancy checks will not
-                * have caught this case because the join clauses would never have
-                * been put in the same joininfo list.
+                * being used in the index scan; this check is needed because
+                * find_eclass_clauses_for_index_join() may emit different clauses
+                * than generate_join_implied_equalities() did.
                 *
-                * We can skip this if the index path is an ordinary indexpath and
-                * not a special innerjoin path.
+                * We can skip this if the index path is an ordinary indexpath and not
+                * a special innerjoin path, since it then wouldn't be using any join
+                * clauses.
                 */
                IndexPath  *innerpath = (IndexPath *) best_path->innerjoinpath;
 
                if (innerpath->isjoininner)
-               {
                        joinrestrictclauses =
                                select_nonredundant_join_clauses(root,
                                                                                                 joinrestrictclauses,
-                                                                                                innerpath->indexclauses,
-                                                                                                IS_OUTER_JOIN(best_path->jointype));
-               }
+                                                                                                innerpath->indexclauses);
        }
        else if (IsA(best_path->innerjoinpath, BitmapHeapPath))
        {
                /*
                 * Same deal for bitmapped index scans.
+                *
+                * Note: both here and above, we ignore any implicit index
+                * restrictions associated with the use of partial indexes.  This is
+                * OK because we're only trying to prove we can dispense with some
+                * join quals; failing to prove that doesn't result in an incorrect
+                * plan.  It is the right way to proceed because adding more quals to
+                * the stuff we got from the original query would just make it harder
+                * to detect duplication.  (Also, to change this we'd have to be wary
+                * of UPDATE/DELETE/SELECT FOR UPDATE target relations; see notes
+                * above about EvalPlanQual.)
                 */
                BitmapHeapPath *innerpath = (BitmapHeapPath *) best_path->innerjoinpath;
 
@@ -1174,32 +1421,32 @@ create_nestloop_plan(Query *root,
 
                        bitmapclauses =
                                make_restrictinfo_from_bitmapqual(innerpath->bitmapqual,
-                                                                                                 true, true);
+                                                                                                 true,
+                                                                                                 false);
                        joinrestrictclauses =
                                select_nonredundant_join_clauses(root,
                                                                                                 joinrestrictclauses,
-                                                                                                bitmapclauses,
-                                                                                                IS_OUTER_JOIN(best_path->jointype));
+                                                                                                bitmapclauses);
                }
        }
 
+       /* Sort join qual clauses into best execution order */
+       joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
+
        /* Get the join qual clauses (in plain expression form) */
+       /* Any pseudoconstant clauses are ignored here */
        if (IS_OUTER_JOIN(best_path->jointype))
        {
-               get_actual_join_clauses(joinrestrictclauses,
-                                                               &joinclauses, &otherclauses);
+               extract_actual_join_clauses(joinrestrictclauses,
+                                                                       &joinclauses, &otherclauses);
        }
        else
        {
                /* We can treat all clauses alike for an inner join */
-               joinclauses = get_actual_clauses(joinrestrictclauses);
+               joinclauses = extract_actual_clauses(joinrestrictclauses, false);
                otherclauses = NIL;
        }
 
-       /* Sort clauses into best execution order */
-       joinclauses = order_qual_clauses(root, joinclauses);
-       otherclauses = order_qual_clauses(root, otherclauses);
-
        join_plan = make_nestloop(tlist,
                                                          joinclauses,
                                                          otherclauses,
@@ -1213,7 +1460,7 @@ create_nestloop_plan(Query *root,
 }
 
 static MergeJoin *
-create_mergejoin_plan(Query *root,
+create_mergejoin_plan(PlannerInfo *root,
                                          MergePath *best_path,
                                          Plan *outer_plan,
                                          Plan *inner_plan)
@@ -1222,44 +1469,59 @@ create_mergejoin_plan(Query *root,
        List       *joinclauses;
        List       *otherclauses;
        List       *mergeclauses;
+       List       *outerpathkeys;
+       List       *innerpathkeys;
+       int                     nClauses;
+       Oid                *mergefamilies;
+       int                *mergestrategies;
+       bool       *mergenullsfirst;
        MergeJoin  *join_plan;
+       int                     i;
+       EquivalenceClass *lastoeclass;
+       EquivalenceClass *lastieclass;
+       PathKey    *opathkey;
+       PathKey    *ipathkey;
+       ListCell   *lc;
+       ListCell   *lop;
+       ListCell   *lip;
+
+       /* Sort join qual clauses into best execution order */
+       /* NB: do NOT reorder the mergeclauses */
+       joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
 
        /* Get the join qual clauses (in plain expression form) */
+       /* Any pseudoconstant clauses are ignored here */
        if (IS_OUTER_JOIN(best_path->jpath.jointype))
        {
-               get_actual_join_clauses(best_path->jpath.joinrestrictinfo,
-                                                               &joinclauses, &otherclauses);
+               extract_actual_join_clauses(joinclauses,
+                                                                       &joinclauses, &otherclauses);
        }
        else
        {
                /* We can treat all clauses alike for an inner join */
-               joinclauses = get_actual_clauses(best_path->jpath.joinrestrictinfo);
+               joinclauses = extract_actual_clauses(joinclauses, false);
                otherclauses = NIL;
        }
 
        /*
-        * Remove the mergeclauses from the list of join qual clauses, leaving
-        * the list of quals that must be checked as qpquals.
+        * Remove the mergeclauses from the list of join qual clauses, leaving the
+        * list of quals that must be checked as qpquals.
         */
        mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
        joinclauses = list_difference(joinclauses, mergeclauses);
 
        /*
-        * Rearrange mergeclauses, if needed, so that the outer variable is
-        * always on the left.
+        * Rearrange mergeclauses, if needed, so that the outer variable is always
+        * on the left; mark the mergeclause restrictinfos with correct
+        * outer_is_left status.
         */
        mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
-                                                best_path->jpath.outerjoinpath->parent->relids);
-
-       /* Sort clauses into best execution order */
-       /* NB: do NOT reorder the mergeclauses */
-       joinclauses = order_qual_clauses(root, joinclauses);
-       otherclauses = order_qual_clauses(root, otherclauses);
+                                                        best_path->jpath.outerjoinpath->parent->relids);
 
        /*
         * Create explicit sort nodes for the outer and inner join paths if
-        * necessary.  The sort cost was already accounted for in the path.
-        * Make sure there are no excess columns in the inputs if sorting.
+        * necessary.  The sort cost was already accounted for in the path. Make
+        * sure there are no excess columns in the inputs if sorting.
         */
        if (best_path->outersortkeys)
        {
@@ -1267,8 +1529,12 @@ create_mergejoin_plan(Query *root,
                outer_plan = (Plan *)
                        make_sort_from_pathkeys(root,
                                                                        outer_plan,
-                                                                       best_path->outersortkeys);
+                                                                       best_path->outersortkeys,
+                                                                       -1.0);
+               outerpathkeys = best_path->outersortkeys;
        }
+       else
+               outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
 
        if (best_path->innersortkeys)
        {
@@ -1276,9 +1542,113 @@ create_mergejoin_plan(Query *root,
                inner_plan = (Plan *)
                        make_sort_from_pathkeys(root,
                                                                        inner_plan,
-                                                                       best_path->innersortkeys);
+                                                                       best_path->innersortkeys,
+                                                                       -1.0);
+               innerpathkeys = best_path->innersortkeys;
+       }
+       else
+               innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
+
+       /*
+        * If inner plan is a sort that is expected to spill to disk, add a
+        * materialize node to shield it from the need to handle mark/restore.
+        * This will allow it to perform the last merge pass on-the-fly, while in
+        * most cases not requiring the materialize to spill to disk.
+        *
+        * XXX really, Sort oughta do this for itself, probably, to avoid the
+        * overhead of a separate plan node.
+        */
+       if (IsA(inner_plan, Sort) &&
+               sort_exceeds_work_mem((Sort *) inner_plan))
+       {
+               Plan       *matplan = (Plan *) make_material(inner_plan);
+
+               /*
+                * We assume the materialize will not spill to disk, and therefore
+                * charge just cpu_tuple_cost per tuple.
+                */
+               copy_plan_costsize(matplan, inner_plan);
+               matplan->total_cost += cpu_tuple_cost * matplan->plan_rows;
+
+               inner_plan = matplan;
+       }
+
+       /*
+        * Compute the opfamily/strategy/nullsfirst arrays needed by the executor.
+        * The information is in the pathkeys for the two inputs, but we need to
+        * be careful about the possibility of mergeclauses sharing a pathkey
+        * (compare find_mergeclauses_for_pathkeys()).
+        */
+       nClauses = list_length(mergeclauses);
+       Assert(nClauses == list_length(best_path->path_mergeclauses));
+       mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
+       mergestrategies = (int *) palloc(nClauses * sizeof(int));
+       mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
+
+       lastoeclass = NULL;
+       lastieclass = NULL;
+       opathkey = NULL;
+       ipathkey = NULL;
+       lop = list_head(outerpathkeys);
+       lip = list_head(innerpathkeys);
+       i = 0;
+       foreach(lc, best_path->path_mergeclauses)
+       {
+               RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
+               EquivalenceClass *oeclass;
+               EquivalenceClass *ieclass;
+
+               /* fetch outer/inner eclass from mergeclause */
+               Assert(IsA(rinfo, RestrictInfo));
+               if (rinfo->outer_is_left)
+               {
+                       oeclass = rinfo->left_ec;
+                       ieclass = rinfo->right_ec;
+               }
+               else
+               {
+                       oeclass = rinfo->right_ec;
+                       ieclass = rinfo->left_ec;
+               }
+               Assert(oeclass != NULL);
+               Assert(ieclass != NULL);
+
+               /* should match current or next pathkeys */
+               /* we check this carefully for debugging reasons */
+               if (oeclass != lastoeclass)
+               {
+                       if (!lop)
+                               elog(ERROR, "too few pathkeys for mergeclauses");
+                       opathkey = (PathKey *) lfirst(lop);
+                       lop = lnext(lop);
+                       lastoeclass = opathkey->pk_eclass;
+                       if (oeclass != lastoeclass)
+                               elog(ERROR, "outer pathkeys do not match mergeclause");
+               }
+               if (ieclass != lastieclass)
+               {
+                       if (!lip)
+                               elog(ERROR, "too few pathkeys for mergeclauses");
+                       ipathkey = (PathKey *) lfirst(lip);
+                       lip = lnext(lip);
+                       lastieclass = ipathkey->pk_eclass;
+                       if (ieclass != lastieclass)
+                               elog(ERROR, "inner pathkeys do not match mergeclause");
+               }
+               /* pathkeys should match each other too (more debugging) */
+               if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
+                       opathkey->pk_strategy != ipathkey->pk_strategy ||
+                       opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
+                       elog(ERROR, "left and right pathkeys do not match in mergejoin");
+
+               /* OK, save info for executor */
+               mergefamilies[i] = opathkey->pk_opfamily;
+               mergestrategies[i] = opathkey->pk_strategy;
+               mergenullsfirst[i] = opathkey->pk_nulls_first;
+               i++;
        }
 
+
        /*
         * Now we can build the mergejoin node.
         */
@@ -1286,6 +1656,9 @@ create_mergejoin_plan(Query *root,
                                                           joinclauses,
                                                           otherclauses,
                                                           mergeclauses,
+                                                          mergefamilies,
+                                                          mergestrategies,
+                                                          mergenullsfirst,
                                                           outer_plan,
                                                           inner_plan,
                                                           best_path->jpath.jointype);
@@ -1296,7 +1669,7 @@ create_mergejoin_plan(Query *root,
 }
 
 static HashJoin *
-create_hashjoin_plan(Query *root,
+create_hashjoin_plan(PlannerInfo *root,
                                         HashPath *best_path,
                                         Plan *outer_plan,
                                         Plan *inner_plan)
@@ -1308,37 +1681,37 @@ create_hashjoin_plan(Query *root,
        HashJoin   *join_plan;
        Hash       *hash_plan;
 
+       /* Sort join qual clauses into best execution order */
+       joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
+       /* There's no point in sorting the hash clauses ... */
+
        /* Get the join qual clauses (in plain expression form) */
+       /* Any pseudoconstant clauses are ignored here */
        if (IS_OUTER_JOIN(best_path->jpath.jointype))
        {
-               get_actual_join_clauses(best_path->jpath.joinrestrictinfo,
-                                                               &joinclauses, &otherclauses);
+               extract_actual_join_clauses(joinclauses,
+                                                                       &joinclauses, &otherclauses);
        }
        else
        {
                /* We can treat all clauses alike for an inner join */
-               joinclauses = get_actual_clauses(best_path->jpath.joinrestrictinfo);
+               joinclauses = extract_actual_clauses(joinclauses, false);
                otherclauses = NIL;
        }
 
        /*
-        * Remove the hashclauses from the list of join qual clauses, leaving
-        * the list of quals that must be checked as qpquals.
+        * Remove the hashclauses from the list of join qual clauses, leaving the
+        * list of quals that must be checked as qpquals.
         */
        hashclauses = get_actual_clauses(best_path->path_hashclauses);
        joinclauses = list_difference(joinclauses, hashclauses);
 
        /*
-        * Rearrange hashclauses, if needed, so that the outer variable is
-        * always on the left.
+        * Rearrange hashclauses, if needed, so that the outer variable is always
+        * on the left.
         */
        hashclauses = get_switched_clauses(best_path->path_hashclauses,
-                                                best_path->jpath.outerjoinpath->parent->relids);
-
-       /* Sort clauses into best execution order */
-       joinclauses = order_qual_clauses(root, joinclauses);
-       otherclauses = order_qual_clauses(root, otherclauses);
-       hashclauses = order_qual_clauses(root, hashclauses);
+                                                        best_path->jpath.outerjoinpath->parent->relids);
 
        /* We don't want any excess columns in the hashed tuples */
        disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
@@ -1370,122 +1743,136 @@ create_hashjoin_plan(Query *root,
 /*
  * fix_indexqual_references
  *       Adjust indexqual clauses to the form the executor's indexqual
- *       machinery needs, and check for recheckable (lossy) index conditions.
+ *       machinery needs.
  *
- * We have five tasks here:
+ * We have three tasks here:
  *     * Remove RestrictInfo nodes from the input clauses.
  *     * Index keys must be represented by Var nodes with varattno set to the
  *       index's attribute number, not the attribute number in the original rel.
  *     * If the index key is on the right, commute the clause to put it on the
  *       left.
- *     * We must construct lists of operator strategy numbers and subtypes
- *       for the top-level operators of each index clause.
- *     * We must detect any lossy index operators.  The API is that we return
- *       a list of the input clauses whose operators are NOT lossy.
  *
- * fixed_indexquals receives a modified copy of the indexquals list --- the
+ * The result is a modified copy of the indexquals list --- the
  * original is not changed.  Note also that the copy shares no substructure
  * with the original; this is needed in case there is a subplan in it (we need
  * two separate copies of the subplan tree, or things will go awry).
- *
- * nonlossy_indexquals receives a list of the original input clauses (with
- * RestrictInfos) that contain non-lossy operators.
- *
- * indexstrategy receives an integer list of strategy numbers.
- * indexsubtype receives an OID list of strategy subtypes.
  */
-static void
-fix_indexqual_references(List *indexquals, IndexPath *index_path,
-                                                List **fixed_indexquals,
-                                                List **nonlossy_indexquals,
-                                                List **indexstrategy,
-                                                List **indexsubtype)
+static List *
+fix_indexqual_references(List *indexquals, IndexPath *index_path)
 {
        IndexOptInfo *index = index_path->indexinfo;
+       List       *fixed_indexquals;
        ListCell   *l;
 
-       *fixed_indexquals = NIL;
-       *nonlossy_indexquals = NIL;
-       *indexstrategy = NIL;
-       *indexsubtype = NIL;
+       fixed_indexquals = NIL;
 
        /*
         * For each qual clause, commute if needed to put the indexkey operand on
         * the left, and then fix its varattno.  (We do not need to change the
-        * other side of the clause.)  Then determine the operator's strategy
-        * number and subtype number, and check for lossy index behavior.
+        * other side of the clause.)
         */
        foreach(l, indexquals)
        {
                RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
-               OpExpr     *clause;
-               OpExpr     *newclause;
-               Oid                     opclass;
-               int                     stratno;
-               Oid                     stratsubtype;
-               bool            recheck;
+               Expr       *clause;
 
                Assert(IsA(rinfo, RestrictInfo));
-               clause = (OpExpr *) rinfo->clause;
-               if (!IsA(clause, OpExpr) ||
-                       list_length(clause->args) != 2)
-                       elog(ERROR, "indexqual clause is not binary opclause");
 
                /*
                 * Make a copy that will become the fixed clause.
                 *
                 * We used to try to do a shallow copy here, but that fails if there
-                * is a subplan in the arguments of the opclause.  So just do a
-                * full copy.
+                * is a subplan in the arguments of the opclause.  So just do a full
+                * copy.
                 */
-               newclause = (OpExpr *) copyObject((Node *) clause);
+               clause = (Expr *) copyObject((Node *) rinfo->clause);
 
-               /*
-                * Check to see if the indexkey is on the right; if so, commute
-                * the clause.  The indexkey should be the side that refers to
-                * (only) the base relation.
-                */
-               if (!bms_equal(rinfo->left_relids, index->rel->relids))
-                       CommuteClause(newclause);
+               if (IsA(clause, OpExpr))
+               {
+                       OpExpr     *op = (OpExpr *) clause;
 
-               /*
-                * Now, determine which index attribute this is, change the
-                * indexkey operand as needed, and get the index opclass.
-                */
-               linitial(newclause->args) =
-                       fix_indexqual_operand(linitial(newclause->args),
-                                                                 index,
-                                                                 &opclass);
+                       if (list_length(op->args) != 2)
+                               elog(ERROR, "indexqual clause is not binary opclause");
 
-               *fixed_indexquals = lappend(*fixed_indexquals, newclause);
+                       /*
+                        * Check to see if the indexkey is on the right; if so, commute
+                        * the clause. The indexkey should be the side that refers to
+                        * (only) the base relation.
+                        */
+                       if (!bms_equal(rinfo->left_relids, index->rel->relids))
+                               CommuteOpExpr(op);
 
-               /*
-                * Look up the (possibly commuted) operator in the operator class
-                * to get its strategy numbers and the recheck indicator.  This
-                * also double-checks that we found an operator matching the
-                * index.
-                */
-               get_op_opclass_properties(newclause->opno, opclass,
-                                                                 &stratno, &stratsubtype, &recheck);
+                       /*
+                        * Now, determine which index attribute this is and change the
+                        * indexkey operand as needed.
+                        */
+                       linitial(op->args) = fix_indexqual_operand(linitial(op->args),
+                                                                                                          index);
+               }
+               else if (IsA(clause, RowCompareExpr))
+               {
+                       RowCompareExpr *rc = (RowCompareExpr *) clause;
+                       ListCell   *lc;
+
+                       /*
+                        * Check to see if the indexkey is on the right; if so, commute
+                        * the clause. The indexkey should be the side that refers to
+                        * (only) the base relation.
+                        */
+                       if (!bms_overlap(pull_varnos(linitial(rc->largs)),
+                                                        index->rel->relids))
+                               CommuteRowCompareExpr(rc);
+
+                       /*
+                        * For each column in the row comparison, determine which index
+                        * attribute this is and change the indexkey operand as needed.
+                        */
+                       foreach(lc, rc->largs)
+                       {
+                               lfirst(lc) = fix_indexqual_operand(lfirst(lc),
+                                                                                                  index);
+                       }
+               }
+               else if (IsA(clause, ScalarArrayOpExpr))
+               {
+                       ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
+
+                       /* Never need to commute... */
 
-               *indexstrategy = lappend_int(*indexstrategy, stratno);
-               *indexsubtype = lappend_oid(*indexsubtype, stratsubtype);
+                       /*
+                        * Determine which index attribute this is and change the
+                        * indexkey operand as needed.
+                        */
+                       linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
+                                                                                                                index);
+               }
+               else if (IsA(clause, NullTest))
+               {
+                       NullTest   *nt = (NullTest *) clause;
 
-               /* If it's not lossy, add to nonlossy_indexquals */
-               if (!recheck)
-                       *nonlossy_indexquals = lappend(*nonlossy_indexquals, rinfo);
+                       Assert(nt->nulltesttype == IS_NULL);
+                       nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
+                                                                                                        index);
+               }
+               else
+                       elog(ERROR, "unsupported indexqual type: %d",
+                                (int) nodeTag(clause));
+
+               fixed_indexquals = lappend(fixed_indexquals, clause);
        }
+
+       return fixed_indexquals;
 }
 
 static Node *
-fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opclass)
+fix_indexqual_operand(Node *node, IndexOptInfo *index)
 {
        /*
-        * We represent index keys by Var nodes having the varno of the base
-        * table but varattno equal to the index's attribute number (index
-        * column position).  This is a bit hokey ... would be cleaner to use
-        * a special-purpose node type that could not be mistaken for a
-        * regular Var.  But it will do for now.
+        * We represent index keys by Var nodes having the varno of the base table
+        * but varattno equal to the index's attribute number (index column
+        * position).  This is a bit hokey ... would be cleaner to use a
+        * special-purpose node type that could not be mistaken for a regular Var.
+        * But it will do for now.
         */
        Var                *result;
        int                     pos;
@@ -1511,8 +1898,6 @@ fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opclass)
                                {
                                        result = (Var *) copyObject(node);
                                        result->varattno = pos + 1;
-                                       /* return the correct opclass, too */
-                                       *opclass = index->classlist[pos];
                                        return (Node *) result;
                                }
                        }
@@ -1538,8 +1923,6 @@ fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opclass)
                                result = makeVar(index->rel->relid, pos + 1,
                                                                 exprType(lfirst(indexpr_item)), -1,
                                                                 0);
-                               /* return the correct opclass, too */
-                               *opclass = index->classlist[pos];
                                return (Node *) result;
                        }
                        indexpr_item = lnext(indexpr_item);
@@ -1556,8 +1939,9 @@ fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opclass)
  *       Given a list of merge or hash joinclauses (as RestrictInfo nodes),
  *       extract the bare clauses, and rearrange the elements within the
  *       clauses, if needed, so the outer join variable is on the left and
- *       the inner is on the right.  The original data structure is not touched;
- *       a modified list is returned.
+ *       the inner is on the right.  The original clause data structure is not
+ *       touched; a modified list is returned.  We do, however, set the transient
+ *       outer_is_left field in each RestrictInfo to show which side was which.
  */
 static List *
 get_switched_clauses(List *clauses, Relids outerrelids)
@@ -1574,8 +1958,8 @@ get_switched_clauses(List *clauses, Relids outerrelids)
                if (bms_is_subset(restrictinfo->right_relids, outerrelids))
                {
                        /*
-                        * Duplicate just enough of the structure to allow commuting
-                        * the clause without changing the original list.  Could use
+                        * Duplicate just enough of the structure to allow commuting the
+                        * clause without changing the original list.  Could use
                         * copyObject, but a complete deep copy is overkill.
                         */
                        OpExpr     *temp = makeNode(OpExpr);
@@ -1586,11 +1970,16 @@ get_switched_clauses(List *clauses, Relids outerrelids)
                        temp->opretset = clause->opretset;
                        temp->args = list_copy(clause->args);
                        /* Commute it --- note this modifies the temp node in-place. */
-                       CommuteClause(temp);
+                       CommuteOpExpr(temp);
                        t_list = lappend(t_list, temp);
+                       restrictinfo->outer_is_left = false;
                }
                else
+               {
+                       Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
                        t_list = lappend(t_list, clause);
+                       restrictinfo->outer_is_left = true;
+               }
        }
        return t_list;
 }
@@ -1602,35 +1991,82 @@ get_switched_clauses(List *clauses, Relids outerrelids)
  *             in at runtime.
  *
  * Ideally the order should be driven by a combination of execution cost and
- * selectivity, but unfortunately we have so little information about
- * execution cost of operators that it's really hard to do anything smart.
- * For now, we just move any quals that contain SubPlan references (but not
- * InitPlan references) to the end of the list.
+ * selectivity, but it's not immediately clear how to account for both,
+ * and given the uncertainty of the estimates the reliability of the decisions
+ * would be doubtful anyway.  So we just order by estimated per-tuple cost,
+ * being careful not to change the order when (as is often the case) the
+ * estimates are identical.
+ *
+ * Although this will work on either bare clauses or RestrictInfos, it's
+ * much faster to apply it to RestrictInfos, since it can re-use cost
+ * information that is cached in RestrictInfos.
+ *
+ * Note: some callers pass lists that contain entries that will later be
+ * removed; this is the easiest way to let this routine see RestrictInfos
+ * instead of bare clauses.  It's OK because we only sort by cost, but
+ * a cost/selectivity combination would likely do the wrong thing.
  */
-List *
-order_qual_clauses(Query *root, List *clauses)
+static List *
+order_qual_clauses(PlannerInfo *root, List *clauses)
 {
-       List       *nosubplans;
-       List       *withsubplans;
-       ListCell   *l;
+       typedef struct
+       {
+               Node       *clause;
+               Cost            cost;
+       } QualItem;
+       int                     nitems = list_length(clauses);
+       QualItem   *items;
+       ListCell   *lc;
+       int                     i;
+       List       *result;
 
-       /* No need to work hard if the query is subselect-free */
-       if (!root->hasSubLinks)
+       /* No need to work hard for 0 or 1 clause */
+       if (nitems <= 1)
                return clauses;
 
-       nosubplans = NIL;
-       withsubplans = NIL;
-       foreach(l, clauses)
+       /*
+        * Collect the items and costs into an array.  This is to avoid repeated
+        * cost_qual_eval work if the inputs aren't RestrictInfos.
+        */
+       items = (QualItem *) palloc(nitems * sizeof(QualItem));
+       i = 0;
+       foreach(lc, clauses)
        {
-               Node       *clause = (Node *) lfirst(l);
+               Node       *clause = (Node *) lfirst(lc);
+               QualCost        qcost;
 
-               if (contain_subplans(clause))
-                       withsubplans = lappend(withsubplans, clause);
-               else
-                       nosubplans = lappend(nosubplans, clause);
+               cost_qual_eval_node(&qcost, clause, root);
+               items[i].clause = clause;
+               items[i].cost = qcost.per_tuple;
+               i++;
+       }
+
+       /*
+        * Sort.  We don't use qsort() because it's not guaranteed stable for
+        * equal keys.  The expected number of entries is small enough that a
+        * simple insertion sort should be good enough.
+        */
+       for (i = 1; i < nitems; i++)
+       {
+               QualItem        newitem = items[i];
+               int                     j;
+
+               /* insert newitem into the already-sorted subarray */
+               for (j = i; j > 0; j--)
+               {
+                       if (newitem.cost >= items[j - 1].cost)
+                               break;
+                       items[j] = items[j - 1];
+               }
+               items[j] = newitem;
        }
 
-       return list_concat(nosubplans, withsubplans);
+       /* Convert back to a list */
+       result = NIL;
+       for (i = 0; i < nitems; i++)
+               result = lappend(result, items[i].clause);
+
+       return result;
 }
 
 /*
@@ -1716,8 +2152,6 @@ make_indexscan(List *qptlist,
                           Oid indexid,
                           List *indexqual,
                           List *indexqualorig,
-                          List *indexstrategy,
-                          List *indexsubtype,
                           ScanDirection indexscandir)
 {
        IndexScan  *node = makeNode(IndexScan);
@@ -1732,8 +2166,6 @@ make_indexscan(List *qptlist,
        node->indexid = indexid;
        node->indexqual = indexqual;
        node->indexqualorig = indexqualorig;
-       node->indexstrategy = indexstrategy;
-       node->indexsubtype = indexsubtype;
        node->indexorderdir = indexscandir;
 
        return node;
@@ -1743,9 +2175,7 @@ static BitmapIndexScan *
 make_bitmap_indexscan(Index scanrelid,
                                          Oid indexid,
                                          List *indexqual,
-                                         List *indexqualorig,
-                                         List *indexstrategy,
-                                         List *indexsubtype)
+                                         List *indexqualorig)
 {
        BitmapIndexScan *node = makeNode(BitmapIndexScan);
        Plan       *plan = &node->scan.plan;
@@ -1759,8 +2189,6 @@ make_bitmap_indexscan(Index scanrelid,
        node->indexid = indexid;
        node->indexqual = indexqual;
        node->indexqualorig = indexqualorig;
-       node->indexstrategy = indexstrategy;
-       node->indexsubtype = indexsubtype;
 
        return node;
 }
@@ -1790,7 +2218,7 @@ static TidScan *
 make_tidscan(List *qptlist,
                         List *qpqual,
                         Index scanrelid,
-                        List *tideval)
+                        List *tidquals)
 {
        TidScan    *node = makeNode(TidScan);
        Plan       *plan = &node->scan.plan;
@@ -1801,7 +2229,7 @@ make_tidscan(List *qptlist,
        plan->lefttree = NULL;
        plan->righttree = NULL;
        node->scan.scanrelid = scanrelid;
-       node->tideval = tideval;
+       node->tidquals = tidquals;
 
        return node;
 }
@@ -1810,15 +2238,16 @@ SubqueryScan *
 make_subqueryscan(List *qptlist,
                                  List *qpqual,
                                  Index scanrelid,
-                                 Plan *subplan)
+                                 Plan *subplan,
+                                 List *subrtable)
 {
        SubqueryScan *node = makeNode(SubqueryScan);
        Plan       *plan = &node->scan.plan;
 
        /*
-        * Cost is figured here for the convenience of prepunion.c.  Note this
-        * is only correct for the case where qpqual is empty; otherwise
-        * caller should overwrite cost with a better estimate.
+        * Cost is figured here for the convenience of prepunion.c.  Note this is
+        * only correct for the case where qpqual is empty; otherwise caller
+        * should overwrite cost with a better estimate.
         */
        copy_plan_costsize(plan, subplan);
        plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
@@ -1829,6 +2258,7 @@ make_subqueryscan(List *qptlist,
        plan->righttree = NULL;
        node->scan.scanrelid = scanrelid;
        node->subplan = subplan;
+       node->subrtable = subrtable;
 
        return node;
 }
@@ -1836,7 +2266,11 @@ make_subqueryscan(List *qptlist,
 static FunctionScan *
 make_functionscan(List *qptlist,
                                  List *qpqual,
-                                 Index scanrelid)
+                                 Index scanrelid,
+                                 Node *funcexpr,
+                                 List *funccolnames,
+                                 List *funccoltypes,
+                                 List *funccoltypmods)
 {
        FunctionScan *node = makeNode(FunctionScan);
        Plan       *plan = &node->scan.plan;
@@ -1847,6 +2281,30 @@ make_functionscan(List *qptlist,
        plan->lefttree = NULL;
        plan->righttree = NULL;
        node->scan.scanrelid = scanrelid;
+       node->funcexpr = funcexpr;
+       node->funccolnames = funccolnames;
+       node->funccoltypes = funccoltypes;
+       node->funccoltypmods = funccoltypmods;
+
+       return node;
+}
+
+static ValuesScan *
+make_valuesscan(List *qptlist,
+                               List *qpqual,
+                               Index scanrelid,
+                               List *values_lists)
+{
+       ValuesScan *node = makeNode(ValuesScan);
+       Plan       *plan = &node->scan.plan;
+
+       /* cost should be inserted by caller */
+       plan->targetlist = qptlist;
+       plan->qual = qpqual;
+       plan->lefttree = NULL;
+       plan->righttree = NULL;
+       node->scan.scanrelid = scanrelid;
+       node->values_lists = values_lists;
 
        return node;
 }
@@ -1856,17 +2314,18 @@ make_append(List *appendplans, bool isTarget, List *tlist)
 {
        Append     *node = makeNode(Append);
        Plan       *plan = &node->plan;
+       double          total_size;
        ListCell   *subnode;
 
        /*
-        * Compute cost as sum of subplan costs.  We charge nothing extra for
-        * the Append itself, which perhaps is too optimistic, but since it
-        * doesn't do any selection or projection, it is a pretty cheap node.
+        * Compute cost as sum of subplan costs.  We charge nothing extra for the
+        * Append itself, which perhaps is too optimistic, but since it doesn't do
+        * any selection or projection, it is a pretty cheap node.
         */
        plan->startup_cost = 0;
        plan->total_cost = 0;
        plan->plan_rows = 0;
-       plan->plan_width = 0;
+       total_size = 0;
        foreach(subnode, appendplans)
        {
                Plan       *subplan = (Plan *) lfirst(subnode);
@@ -1875,9 +2334,12 @@ make_append(List *appendplans, bool isTarget, List *tlist)
                        plan->startup_cost = subplan->startup_cost;
                plan->total_cost += subplan->total_cost;
                plan->plan_rows += subplan->plan_rows;
-               if (plan->plan_width < subplan->plan_width)
-                       plan->plan_width = subplan->plan_width;
+               total_size += subplan->plan_width * subplan->plan_rows;
        }
+       if (plan->plan_rows > 0)
+               plan->plan_width = rint(total_size / plan->plan_rows);
+       else
+               plan->plan_width = 0;
 
        plan->targetlist = tlist;
        plan->qual = NIL;
@@ -1976,11 +2438,11 @@ make_hash(Plan *lefttree)
        copy_plan_costsize(plan, lefttree);
 
        /*
-        * For plausibility, make startup & total costs equal total cost of
-        * input plan; this only affects EXPLAIN display not decisions.
+        * For plausibility, make startup & total costs equal total cost of input
+        * plan; this only affects EXPLAIN display not decisions.
         */
        plan->startup_cost = plan->total_cost;
-       plan->targetlist = copyObject(lefttree->targetlist);
+       plan->targetlist = lefttree->targetlist;
        plan->qual = NIL;
        plan->lefttree = lefttree;
        plan->righttree = NULL;
@@ -1993,6 +2455,9 @@ make_mergejoin(List *tlist,
                           List *joinclauses,
                           List *otherclauses,
                           List *mergeclauses,
+                          Oid *mergefamilies,
+                          int *mergestrategies,
+                          bool *mergenullsfirst,
                           Plan *lefttree,
                           Plan *righttree,
                           JoinType jointype)
@@ -2006,6 +2471,9 @@ make_mergejoin(List *tlist,
        plan->lefttree = lefttree;
        plan->righttree = righttree;
        node->mergeclauses = mergeclauses;
+       node->mergeFamilies = mergefamilies;
+       node->mergeStrategies = mergestrategies;
+       node->mergeNullsFirst = mergenullsfirst;
        node->join.jointype = jointype;
        node->join.joinqual = joinclauses;
 
@@ -2015,11 +2483,14 @@ make_mergejoin(List *tlist,
 /*
  * make_sort --- basic routine to build a Sort plan node
  *
- * Caller must have built the sortColIdx and sortOperators arrays already.
+ * Caller must have built the sortColIdx, sortOperators, and nullsFirst
+ * arrays already.     limit_tuples is as for cost_sort (in particular, pass
+ * -1 if no limit)
  */
 static Sort *
-make_sort(Query *root, Plan *lefttree, int numCols,
-                 AttrNumber *sortColIdx, Oid *sortOperators)
+make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
+                 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
+                 double limit_tuples)
 {
        Sort       *node = makeNode(Sort);
        Plan       *plan = &node->plan;
@@ -2029,16 +2500,18 @@ make_sort(Query *root, Plan *lefttree, int numCols,
        cost_sort(&sort_path, root, NIL,
                          lefttree->total_cost,
                          lefttree->plan_rows,
-                         lefttree->plan_width);
+                         lefttree->plan_width,
+                         limit_tuples);
        plan->startup_cost = sort_path.startup_cost;
        plan->total_cost = sort_path.total_cost;
-       plan->targetlist = copyObject(lefttree->targetlist);
+       plan->targetlist = lefttree->targetlist;
        plan->qual = NIL;
        plan->lefttree = lefttree;
        plan->righttree = NULL;
        node->numCols = numCols;
        node->sortColIdx = sortColIdx;
        node->sortOperators = sortOperators;
+       node->nullsFirst = nullsFirst;
 
        return node;
 }
@@ -2053,14 +2526,25 @@ make_sort(Query *root, Plan *lefttree, int numCols,
  * max possible number of columns.     Return value is the new column count.
  */
 static int
-add_sort_column(AttrNumber colIdx, Oid sortOp,
-                               int numCols, AttrNumber *sortColIdx, Oid *sortOperators)
+add_sort_column(AttrNumber colIdx, Oid sortOp, bool nulls_first,
+                               int numCols, AttrNumber *sortColIdx,
+                               Oid *sortOperators, bool *nullsFirst)
 {
        int                     i;
 
+       Assert(OidIsValid(sortOp));
+
        for (i = 0; i < numCols; i++)
        {
-               if (sortColIdx[i] == colIdx)
+               /*
+                * Note: we check sortOp because it's conceivable that "ORDER BY foo
+                * USING <, foo USING <<<" is not redundant, if <<< distinguishes
+                * values that < considers equal.  We need not check nulls_first
+                * however because a lower-order column with the same sortop but
+                * opposite nulls direction is redundant.
+                */
+               if (sortColIdx[i] == colIdx &&
+                       sortOperators[numCols] == sortOp)
                {
                        /* Already sorting by this col, so extra sort key is useless */
                        return numCols;
@@ -2070,6 +2554,7 @@ add_sort_column(AttrNumber colIdx, Oid sortOp,
        /* Add the column */
        sortColIdx[numCols] = colIdx;
        sortOperators[numCols] = sortOp;
+       nullsFirst[numCols] = nulls_first;
        return numCols + 1;
 }
 
@@ -2079,6 +2564,8 @@ add_sort_column(AttrNumber colIdx, Oid sortOp,
  *
  *       'lefttree' is the node which yields input tuples
  *       'pathkeys' is the list of pathkeys by which the result is to be sorted
+ *       'limit_tuples' is the bound on the number of output tuples;
+ *                             -1 if no bound
  *
  * We must convert the pathkey information into arrays of sort key column
  * numbers and sort operator OIDs.
@@ -2089,139 +2576,212 @@ add_sort_column(AttrNumber colIdx, Oid sortOp,
  * If the input plan type isn't one that can do projections, this means
  * adding a Result node just to do the projection.
  */
-static Sort *
-make_sort_from_pathkeys(Query *root, Plan *lefttree, List *pathkeys)
+Sort *
+make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
+                                               double limit_tuples)
 {
        List       *tlist = lefttree->targetlist;
        ListCell   *i;
        int                     numsortkeys;
        AttrNumber *sortColIdx;
        Oid                *sortOperators;
+       bool       *nullsFirst;
 
        /*
-        * We will need at most list_length(pathkeys) sort columns; possibly
-        * less
+        * We will need at most list_length(pathkeys) sort columns; possibly less
         */
        numsortkeys = list_length(pathkeys);
        sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
        sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
+       nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
 
        numsortkeys = 0;
 
        foreach(i, pathkeys)
        {
-               List       *keysublist = (List *) lfirst(i);
-               PathKeyItem *pathkey = NULL;
+               PathKey    *pathkey = (PathKey *) lfirst(i);
+               EquivalenceClass *ec = pathkey->pk_eclass;
                TargetEntry *tle = NULL;
+               Oid                     pk_datatype = InvalidOid;
+               Oid                     sortop;
                ListCell   *j;
 
-               /*
-                * We can sort by any one of the sort key items listed in this
-                * sublist.  For now, we take the first one that corresponds to an
-                * available Var in the tlist.  If there isn't any, use the first
-                * one that is an expression in the input's vars.
-                *
-                * XXX if we have a choice, is there any way of figuring out which
-                * might be cheapest to execute?  (For example, int4lt is likely
-                * much cheaper to execute than numericlt, but both might appear
-                * in the same pathkey sublist...)      Not clear that we ever will
-                * have a choice in practice, so it may not matter.
-                */
-               foreach(j, keysublist)
+               if (ec->ec_has_volatile)
                {
-                       pathkey = (PathKeyItem *) lfirst(j);
-                       Assert(IsA(pathkey, PathKeyItem));
-                       tle = tlist_member(pathkey->key, tlist);
-                       if (tle)
-                               break;
+                       /*
+                        * If the pathkey's EquivalenceClass is volatile, then it must
+                        * have come from an ORDER BY clause, and we have to match it to
+                        * that same targetlist entry.
+                        */
+                       if (ec->ec_sortref == 0)        /* can't happen */
+                               elog(ERROR, "volatile EquivalenceClass has no sortref");
+                       tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
+                       Assert(tle);
+                       Assert(list_length(ec->ec_members) == 1);
+                       pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
                }
-               if (!tle)
+               else
                {
-                       /* No matching Var; look for a computable expression */
-                       foreach(j, keysublist)
+                       /*
+                        * Otherwise, we can sort by any non-constant expression listed in
+                        * the pathkey's EquivalenceClass.  For now, we take the first one
+                        * that corresponds to an available item in the tlist.  If there
+                        * isn't any, use the first one that is an expression in the
+                        * input's vars.  (The non-const restriction only matters if the
+                        * EC is below_outer_join; but if it isn't, it won't contain
+                        * consts anyway, else we'd have discarded the pathkey as
+                        * redundant.)
+                        *
+                        * XXX if we have a choice, is there any way of figuring out which
+                        * might be cheapest to execute?  (For example, int4lt is likely
+                        * much cheaper to execute than numericlt, but both might appear
+                        * in the same equivalence class...)  Not clear that we ever will
+                        * have an interesting choice in practice, so it may not matter.
+                        */
+                       foreach(j, ec->ec_members)
                        {
-                               List       *exprvars;
-                               ListCell   *k;
+                               EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
 
-                               pathkey = (PathKeyItem *) lfirst(j);
-                               exprvars = pull_var_clause(pathkey->key, false);
-                               foreach(k, exprvars)
+                               if (em->em_is_const || em->em_is_child)
+                                       continue;
+
+                               tle = tlist_member((Node *) em->em_expr, tlist);
+                               if (tle)
+                               {
+                                       pk_datatype = em->em_datatype;
+                                       break;          /* found expr already in tlist */
+                               }
+
+                               /*
+                                * We can also use it if the pathkey expression is a relabel
+                                * of the tlist entry, or vice versa.  This is needed for
+                                * binary-compatible cases (cf. make_pathkey_from_sortinfo).
+                                * We prefer an exact match, though, so we do the basic search
+                                * first.
+                                */
+                               tle = tlist_member_ignore_relabel((Node *) em->em_expr, tlist);
+                               if (tle)
                                {
-                                       if (!tlist_member(lfirst(k), tlist))
-                                               break;
+                                       pk_datatype = em->em_datatype;
+                                       break;          /* found expr already in tlist */
                                }
-                               list_free(exprvars);
-                               if (!k)
-                                       break;          /* found usable expression */
                        }
-                       if (!j)
-                               elog(ERROR, "could not find pathkey item to sort");
 
-                       /*
-                        * Do we need to insert a Result node?
-                        */
-                       if (!is_projection_capable_plan(lefttree))
+                       if (!tle)
                        {
-                               tlist = copyObject(tlist);
-                               lefttree = (Plan *) make_result(tlist, NULL, lefttree);
-                       }
+                               /* No matching tlist item; look for a computable expression */
+                               Expr       *sortexpr = NULL;
 
-                       /*
-                        * Add resjunk entry to input's tlist
-                        */
-                       tle = makeTargetEntry((Expr *) pathkey->key,
-                                                                 list_length(tlist) + 1,
-                                                                 NULL,
-                                                                 true);
-                       tlist = lappend(tlist, tle);
-                       lefttree->targetlist = tlist;           /* just in case NIL before */
+                               foreach(j, ec->ec_members)
+                               {
+                                       EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
+                                       List       *exprvars;
+                                       ListCell   *k;
+
+                                       if (em->em_is_const || em->em_is_child)
+                                               continue;
+                                       sortexpr = em->em_expr;
+                                       exprvars = pull_var_clause((Node *) sortexpr, false);
+                                       foreach(k, exprvars)
+                                       {
+                                               if (!tlist_member_ignore_relabel(lfirst(k), tlist))
+                                                       break;
+                                       }
+                                       list_free(exprvars);
+                                       if (!k)
+                                       {
+                                               pk_datatype = em->em_datatype;
+                                               break;  /* found usable expression */
+                                       }
+                               }
+                               if (!j)
+                                       elog(ERROR, "could not find pathkey item to sort");
+
+                               /*
+                                * Do we need to insert a Result node?
+                                */
+                               if (!is_projection_capable_plan(lefttree))
+                               {
+                                       /* copy needed so we don't modify input's tlist below */
+                                       tlist = copyObject(tlist);
+                                       lefttree = (Plan *) make_result(root, tlist, NULL,
+                                                                                                       lefttree);
+                               }
+
+                               /*
+                                * Add resjunk entry to input's tlist
+                                */
+                               tle = makeTargetEntry(sortexpr,
+                                                                         list_length(tlist) + 1,
+                                                                         NULL,
+                                                                         true);
+                               tlist = lappend(tlist, tle);
+                               lefttree->targetlist = tlist;   /* just in case NIL before */
+                       }
                }
 
                /*
-                * The column might already be selected as a sort key, if the
-                * pathkeys contain duplicate entries.  (This can happen in
-                * scenarios where multiple mergejoinable clauses mention the same
-                * var, for example.)  So enter it only once in the sort arrays.
+                * Look up the correct sort operator from the PathKey's slightly
+                * abstracted representation.
                 */
-               numsortkeys = add_sort_column(tle->resno, pathkey->sortop,
-                                                                numsortkeys, sortColIdx, sortOperators);
+               sortop = get_opfamily_member(pathkey->pk_opfamily,
+                                                                        pk_datatype,
+                                                                        pk_datatype,
+                                                                        pathkey->pk_strategy);
+               if (!OidIsValid(sortop))        /* should not happen */
+                       elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
+                                pathkey->pk_strategy, pk_datatype, pk_datatype,
+                                pathkey->pk_opfamily);
+
+               /*
+                * The column might already be selected as a sort key, if the pathkeys
+                * contain duplicate entries.  (This can happen in scenarios where
+                * multiple mergejoinable clauses mention the same var, for example.)
+                * So enter it only once in the sort arrays.
+                */
+               numsortkeys = add_sort_column(tle->resno,
+                                                                         sortop,
+                                                                         pathkey->pk_nulls_first,
+                                                                         numsortkeys,
+                                                                         sortColIdx, sortOperators, nullsFirst);
        }
 
        Assert(numsortkeys > 0);
 
        return make_sort(root, lefttree, numsortkeys,
-                                        sortColIdx, sortOperators);
+                                        sortColIdx, sortOperators, nullsFirst, limit_tuples);
 }
 
 /*
  * make_sort_from_sortclauses
  *       Create sort plan to sort according to given sortclauses
  *
- *       'sortcls' is a list of SortClauses
+ *       'sortcls' is a list of SortGroupClauses
  *       'lefttree' is the node which yields input tuples
  */
 Sort *
-make_sort_from_sortclauses(Query *root, List *sortcls, Plan *lefttree)
+make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
 {
        List       *sub_tlist = lefttree->targetlist;
        ListCell   *l;
        int                     numsortkeys;
        AttrNumber *sortColIdx;
        Oid                *sortOperators;
+       bool       *nullsFirst;
 
        /*
-        * We will need at most list_length(sortcls) sort columns; possibly
-        * less
+        * We will need at most list_length(sortcls) sort columns; possibly less
         */
        numsortkeys = list_length(sortcls);
        sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
        sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
+       nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
 
        numsortkeys = 0;
 
        foreach(l, sortcls)
        {
-               SortClause *sortcl = (SortClause *) lfirst(l);
+               SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
                TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
 
                /*
@@ -2230,30 +2790,32 @@ make_sort_from_sortclauses(Query *root, List *sortcls, Plan *lefttree)
                 * redundantly.
                 */
                numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
-                                                                numsortkeys, sortColIdx, sortOperators);
+                                                                         sortcl->nulls_first,
+                                                                         numsortkeys,
+                                                                         sortColIdx, sortOperators, nullsFirst);
        }
 
        Assert(numsortkeys > 0);
 
        return make_sort(root, lefttree, numsortkeys,
-                                        sortColIdx, sortOperators);
+                                        sortColIdx, sortOperators, nullsFirst, -1.0);
 }
 
 /*
  * make_sort_from_groupcols
  *       Create sort plan to sort based on grouping columns
  *
- * 'groupcls' is the list of GroupClauses
+ * 'groupcls' is the list of SortGroupClauses
  * 'grpColIdx' gives the column numbers to use
  *
  * This might look like it could be merged with make_sort_from_sortclauses,
  * but presently we *must* use the grpColIdx[] array to locate sort columns,
  * because the child plan's tlist is not marked with ressortgroupref info
- * appropriate to the grouping node.  So, only the sortop is used from the
- * GroupClause entries.
+ * appropriate to the grouping node.  So, only the sort ordering info
+ * is used from the SortGroupClause entries.
  */
 Sort *
-make_sort_from_groupcols(Query *root,
+make_sort_from_groupcols(PlannerInfo *root,
                                                 List *groupcls,
                                                 AttrNumber *grpColIdx,
                                                 Plan *lefttree)
@@ -2264,20 +2826,21 @@ make_sort_from_groupcols(Query *root,
        int                     numsortkeys;
        AttrNumber *sortColIdx;
        Oid                *sortOperators;
+       bool       *nullsFirst;
 
        /*
-        * We will need at most list_length(groupcls) sort columns; possibly
-        * less
+        * We will need at most list_length(groupcls) sort columns; possibly less
         */
        numsortkeys = list_length(groupcls);
        sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
        sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
+       nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
 
        numsortkeys = 0;
 
        foreach(l, groupcls)
        {
-               GroupClause *grpcl = (GroupClause *) lfirst(l);
+               SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
                TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
 
                /*
@@ -2286,24 +2849,26 @@ make_sort_from_groupcols(Query *root,
                 * redundantly.
                 */
                numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
-                                                                numsortkeys, sortColIdx, sortOperators);
+                                                                         grpcl->nulls_first,
+                                                                         numsortkeys,
+                                                                         sortColIdx, sortOperators, nullsFirst);
                grpno++;
        }
 
        Assert(numsortkeys > 0);
 
        return make_sort(root, lefttree, numsortkeys,
-                                        sortColIdx, sortOperators);
+                                        sortColIdx, sortOperators, nullsFirst, -1.0);
 }
 
-Material *
+static Material *
 make_material(Plan *lefttree)
 {
        Material   *node = makeNode(Material);
        Plan       *plan = &node->plan;
 
        /* cost should be inserted by caller */
-       plan->targetlist = copyObject(lefttree->targetlist);
+       plan->targetlist = lefttree->targetlist;
        plan->qual = NIL;
        plan->lefttree = lefttree;
        plan->righttree = NULL;
@@ -2347,9 +2912,9 @@ materialize_finished_plan(Plan *subplan)
 }
 
 Agg *
-make_agg(Query *root, List *tlist, List *qual,
+make_agg(PlannerInfo *root, List *tlist, List *qual,
                 AggStrategy aggstrategy,
-                int numGroupCols, AttrNumber *grpColIdx,
+                int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
                 long numGroups, int numAggs,
                 Plan *lefttree)
 {
@@ -2361,6 +2926,7 @@ make_agg(Query *root, List *tlist, List *qual,
        node->aggstrategy = aggstrategy;
        node->numCols = numGroupCols;
        node->grpColIdx = grpColIdx;
+       node->grpOperators = grpOperators;
        node->numGroups = numGroups;
 
        copy_plan_costsize(plan, lefttree); /* only care about copying size */
@@ -2374,8 +2940,8 @@ make_agg(Query *root, List *tlist, List *qual,
        plan->total_cost = agg_path.total_cost;
 
        /*
-        * We will produce a single output tuple if not grouping, and a tuple
-        * per group otherwise.
+        * We will produce a single output tuple if not grouping, and a tuple per
+        * group otherwise.
         */
        if (aggstrategy == AGG_PLAIN)
                plan->plan_rows = 1;
@@ -2383,22 +2949,22 @@ make_agg(Query *root, List *tlist, List *qual,
                plan->plan_rows = numGroups;
 
        /*
-        * We also need to account for the cost of evaluation of the qual (ie,
-        * the HAVING clause) and the tlist.  Note that cost_qual_eval doesn't
-        * charge anything for Aggref nodes; this is okay since they are
-        * really comparable to Vars.
+        * We also need to account for the cost of evaluation of the qual (ie, the
+        * HAVING clause) and the tlist.  Note that cost_qual_eval doesn't charge
+        * anything for Aggref nodes; this is okay since they are really
+        * comparable to Vars.
         *
-        * See notes in grouping_planner about why this routine and make_group
-        * are the only ones in this file that worry about tlist eval cost.
+        * See notes in grouping_planner about why this routine and make_group are
+        * the only ones in this file that worry about tlist eval cost.
         */
        if (qual)
        {
-               cost_qual_eval(&qual_cost, qual);
+               cost_qual_eval(&qual_cost, qual, root);
                plan->startup_cost += qual_cost.startup;
                plan->total_cost += qual_cost.startup;
                plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
        }
-       cost_qual_eval(&qual_cost, tlist);
+       cost_qual_eval(&qual_cost, tlist, root);
        plan->startup_cost += qual_cost.startup;
        plan->total_cost += qual_cost.startup;
        plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
@@ -2412,11 +2978,12 @@ make_agg(Query *root, List *tlist, List *qual,
 }
 
 Group *
-make_group(Query *root,
+make_group(PlannerInfo *root,
                   List *tlist,
                   List *qual,
                   int numGroupCols,
                   AttrNumber *grpColIdx,
+                  Oid *grpOperators,
                   double numGroups,
                   Plan *lefttree)
 {
@@ -2427,6 +2994,7 @@ make_group(Query *root,
 
        node->numCols = numGroupCols;
        node->grpColIdx = grpColIdx;
+       node->grpOperators = grpOperators;
 
        copy_plan_costsize(plan, lefttree); /* only care about copying size */
        cost_group(&group_path, root,
@@ -2441,8 +3009,8 @@ make_group(Query *root,
        plan->plan_rows = numGroups;
 
        /*
-        * We also need to account for the cost of evaluation of the qual (ie,
-        * the HAVING clause) and the tlist.
+        * We also need to account for the cost of evaluation of the qual (ie, the
+        * HAVING clause) and the tlist.
         *
         * XXX this double-counts the cost of evaluation of any expressions used
         * for grouping, since in reality those will have been evaluated at a
@@ -2454,12 +3022,12 @@ make_group(Query *root,
         */
        if (qual)
        {
-               cost_qual_eval(&qual_cost, qual);
+               cost_qual_eval(&qual_cost, qual, root);
                plan->startup_cost += qual_cost.startup;
                plan->total_cost += qual_cost.startup;
                plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
        }
-       cost_qual_eval(&qual_cost, tlist);
+       cost_qual_eval(&qual_cost, tlist, root);
        plan->startup_cost += qual_cost.startup;
        plan->total_cost += qual_cost.startup;
        plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
@@ -2473,8 +3041,9 @@ make_group(Query *root,
 }
 
 /*
- * distinctList is a list of SortClauses, identifying the targetlist items
- * that should be considered by the Unique filter.
+ * distinctList is a list of SortGroupClauses, identifying the targetlist items
+ * that should be considered by the Unique filter.     The input path must
+ * already be sorted accordingly.
  */
 Unique *
 make_unique(Plan *lefttree, List *distinctList)
@@ -2484,54 +3053,60 @@ make_unique(Plan *lefttree, List *distinctList)
        int                     numCols = list_length(distinctList);
        int                     keyno = 0;
        AttrNumber *uniqColIdx;
+       Oid                *uniqOperators;
        ListCell   *slitem;
 
        copy_plan_costsize(plan, lefttree);
 
        /*
-        * Charge one cpu_operator_cost per comparison per input tuple. We
-        * assume all columns get compared at most of the tuples.  (XXX
-        * probably this is an overestimate.)
+        * Charge one cpu_operator_cost per comparison per input tuple. We assume
+        * all columns get compared at most of the tuples.      (XXX probably this is
+        * an overestimate.)
         */
        plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
 
        /*
-        * plan->plan_rows is left as a copy of the input subplan's plan_rows;
-        * ie, we assume the filter removes nothing.  The caller must alter
-        * this if he has a better idea.
+        * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
+        * we assume the filter removes nothing.  The caller must alter this if he
+        * has a better idea.
         */
 
-       plan->targetlist = copyObject(lefttree->targetlist);
+       plan->targetlist = lefttree->targetlist;
        plan->qual = NIL;
        plan->lefttree = lefttree;
        plan->righttree = NULL;
 
        /*
-        * convert SortClause list into array of attr indexes, as wanted by
-        * exec
+        * convert SortGroupClause list into arrays of attr indexes and equality
+        * operators, as wanted by executor
         */
        Assert(numCols > 0);
        uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
+       uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
 
        foreach(slitem, distinctList)
        {
-               SortClause *sortcl = (SortClause *) lfirst(slitem);
+               SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
                TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
 
-               uniqColIdx[keyno++] = tle->resno;
+               uniqColIdx[keyno] = tle->resno;
+               uniqOperators[keyno] = sortcl->eqop;
+               Assert(OidIsValid(uniqOperators[keyno]));
+               keyno++;
        }
 
        node->numCols = numCols;
        node->uniqColIdx = uniqColIdx;
+       node->uniqOperators = uniqOperators;
 
        return node;
 }
 
 /*
- * distinctList is a list of SortClauses, identifying the targetlist items
- * that should be considered by the SetOp filter.
+ * distinctList is a list of SortGroupClauses, identifying the targetlist
+ * items that should be considered by the SetOp filter.  The input path must
+ * already be sorted accordingly.
  */
-
 SetOp *
 make_setop(SetOpCmd cmd, Plan *lefttree,
                   List *distinctList, AttrNumber flagColIdx)
@@ -2541,13 +3116,14 @@ make_setop(SetOpCmd cmd, Plan *lefttree,
        int                     numCols = list_length(distinctList);
        int                     keyno = 0;
        AttrNumber *dupColIdx;
+       Oid                *dupOperators;
        ListCell   *slitem;
 
        copy_plan_costsize(plan, lefttree);
 
        /*
-        * Charge one cpu_operator_cost per comparison per input tuple. We
-        * assume all columns get compared at most of the tuples.
+        * Charge one cpu_operator_cost per comparison per input tuple. We assume
+        * all columns get compared at most of the tuples.
         */
        plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
 
@@ -2559,36 +3135,49 @@ make_setop(SetOpCmd cmd, Plan *lefttree,
        if (plan->plan_rows < 1)
                plan->plan_rows = 1;
 
-       plan->targetlist = copyObject(lefttree->targetlist);
+       plan->targetlist = lefttree->targetlist;
        plan->qual = NIL;
        plan->lefttree = lefttree;
        plan->righttree = NULL;
 
        /*
-        * convert SortClause list into array of attr indexes, as wanted by
-        * exec
+        * convert SortGroupClause list into arrays of attr indexes and equality
+        * operators, as wanted by executor
         */
        Assert(numCols > 0);
        dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
+       dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
 
        foreach(slitem, distinctList)
        {
-               SortClause *sortcl = (SortClause *) lfirst(slitem);
+               SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
                TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
 
-               dupColIdx[keyno++] = tle->resno;
+               dupColIdx[keyno] = tle->resno;
+               dupOperators[keyno] = sortcl->eqop;
+               Assert(OidIsValid(dupOperators[keyno]));
+               keyno++;
        }
 
        node->cmd = cmd;
        node->numCols = numCols;
        node->dupColIdx = dupColIdx;
+       node->dupOperators = dupOperators;
        node->flagColIdx = flagColIdx;
 
        return node;
 }
 
+/*
+ * Note: offset_est and count_est are passed in to save having to repeat
+ * work already done to estimate the values of the limitOffset and limitCount
+ * expressions.  Their values are as returned by preprocess_limit (0 means
+ * "not relevant", -1 means "couldn't estimate").  Keep the code below in sync
+ * with that function!
+ */
 Limit *
-make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
+make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
+                  int64 offset_est, int64 count_est)
 {
        Limit      *node = makeNode(Limit);
        Plan       *plan = &node->plan;
@@ -2596,49 +3185,53 @@ make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
        copy_plan_costsize(plan, lefttree);
 
        /*
-        * If offset/count are constants, adjust the output rows count and
-        * costs accordingly.  This is only a cosmetic issue if we are at top
-        * level, but if we are building a subquery then it's important to
-        * report correct info to the outer planner.
+        * Adjust the output rows count and costs according to the offset/limit.
+        * This is only a cosmetic issue if we are at top level, but if we are
+        * building a subquery then it's important to report correct info to the
+        * outer planner.
+        *
+        * When the offset or count couldn't be estimated, use 10% of the
+        * estimated number of rows emitted from the subplan.
         */
-       if (limitOffset && IsA(limitOffset, Const))
+       if (offset_est != 0)
        {
-               Const      *limito = (Const *) limitOffset;
-               int32           offset = DatumGetInt32(limito->constvalue);
+               double          offset_rows;
 
-               if (!limito->constisnull && offset > 0)
-               {
-                       if (offset > plan->plan_rows)
-                               offset = (int32) plan->plan_rows;
-                       if (plan->plan_rows > 0)
-                               plan->startup_cost +=
-                                       (plan->total_cost - plan->startup_cost)
-                                       * ((double) offset) / plan->plan_rows;
-                       plan->plan_rows -= offset;
-                       if (plan->plan_rows < 1)
-                               plan->plan_rows = 1;
-               }
+               if (offset_est > 0)
+                       offset_rows = (double) offset_est;
+               else
+                       offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
+               if (offset_rows > plan->plan_rows)
+                       offset_rows = plan->plan_rows;
+               if (plan->plan_rows > 0)
+                       plan->startup_cost +=
+                               (plan->total_cost - plan->startup_cost)
+                               * offset_rows / plan->plan_rows;
+               plan->plan_rows -= offset_rows;
+               if (plan->plan_rows < 1)
+                       plan->plan_rows = 1;
        }
-       if (limitCount && IsA(limitCount, Const))
+
+       if (count_est != 0)
        {
-               Const      *limitc = (Const *) limitCount;
-               int32           count = DatumGetInt32(limitc->constvalue);
+               double          count_rows;
 
-               if (!limitc->constisnull && count >= 0)
-               {
-                       if (count > plan->plan_rows)
-                               count = (int32) plan->plan_rows;
-                       if (plan->plan_rows > 0)
-                               plan->total_cost = plan->startup_cost +
-                                       (plan->total_cost - plan->startup_cost)
-                                       * ((double) count) / plan->plan_rows;
-                       plan->plan_rows = count;
-                       if (plan->plan_rows < 1)
-                               plan->plan_rows = 1;
-               }
+               if (count_est > 0)
+                       count_rows = (double) count_est;
+               else
+                       count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
+               if (count_rows > plan->plan_rows)
+                       count_rows = plan->plan_rows;
+               if (plan->plan_rows > 0)
+                       plan->total_cost = plan->startup_cost +
+                               (plan->total_cost - plan->startup_cost)
+                               * count_rows / plan->plan_rows;
+               plan->plan_rows = count_rows;
+               if (plan->plan_rows < 1)
+                       plan->plan_rows = 1;
        }
 
-       plan->targetlist = copyObject(lefttree->targetlist);
+       plan->targetlist = lefttree->targetlist;
        plan->qual = NIL;
        plan->lefttree = lefttree;
        plan->righttree = NULL;
@@ -2649,8 +3242,18 @@ make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount)
        return node;
 }
 
+/*
+ * make_result
+ *       Build a Result plan node
+ *
+ * If we have a subplan, assume that any evaluation costs for the gating qual
+ * were already factored into the subplan's startup cost, and just copy the
+ * subplan cost.  If there's no subplan, we should include the qual eval
+ * cost.  In either case, tlist eval cost is not to be included here.
+ */
 Result *
-make_result(List *tlist,
+make_result(PlannerInfo *root,
+                       List *tlist,
                        Node *resconstantqual,
                        Plan *subplan)
 {
@@ -2664,17 +3267,16 @@ make_result(List *tlist,
                plan->startup_cost = 0;
                plan->total_cost = cpu_tuple_cost;
                plan->plan_rows = 1;    /* wrong if we have a set-valued function? */
-               plan->plan_width = 0;   /* XXX try to be smarter? */
-       }
-
-       if (resconstantqual)
-       {
-               QualCost        qual_cost;
+               plan->plan_width = 0;   /* XXX is it worth being smarter? */
+               if (resconstantqual)
+               {
+                       QualCost        qual_cost;
 
-               cost_qual_eval(&qual_cost, (List *) resconstantqual);
-               /* resconstantqual is evaluated once at startup */
-               plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
-               plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
+                       cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
+                       /* resconstantqual is evaluated once at startup */
+                       plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
+                       plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
+               }
        }
 
        plan->targetlist = tlist;