]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/createplan.c
Teach tuplesort.c about "top N" sorting, in which only the first N tuples
[postgresql] / src / backend / optimizer / plan / createplan.c
1 /*-------------------------------------------------------------------------
2  *
3  * createplan.c
4  *        Routines to create the desired plan for processing a query.
5  *        Planning is complete, we just need to convert the selected
6  *        Path into a Plan.
7  *
8  * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  *        $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.230 2007/05/04 01:13:44 tgl Exp $
14  *
15  *-------------------------------------------------------------------------
16  */
17 #include "postgres.h"
18
19 #include <limits.h>
20
21 #include "access/skey.h"
22 #include "nodes/makefuncs.h"
23 #include "optimizer/clauses.h"
24 #include "optimizer/cost.h"
25 #include "optimizer/plancat.h"
26 #include "optimizer/planmain.h"
27 #include "optimizer/predtest.h"
28 #include "optimizer/restrictinfo.h"
29 #include "optimizer/tlist.h"
30 #include "optimizer/var.h"
31 #include "parser/parse_clause.h"
32 #include "parser/parse_expr.h"
33 #include "parser/parsetree.h"
34 #include "utils/lsyscache.h"
35
36
37 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
38 static List *build_relation_tlist(RelOptInfo *rel);
39 static bool use_physical_tlist(RelOptInfo *rel);
40 static void disuse_physical_tlist(Plan *plan, Path *path);
41 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
42 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
43 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
44 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
45 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
46 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
47 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
48                                         List *tlist, List *scan_clauses);
49 static IndexScan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
50                                           List *tlist, List *scan_clauses,
51                                           List **nonlossy_clauses);
52 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
53                                                 BitmapHeapPath *best_path,
54                                                 List *tlist, List *scan_clauses);
55 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
56                                           List **qual, List **indexqual);
57 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
58                                         List *tlist, List *scan_clauses);
59 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
60                                                  List *tlist, List *scan_clauses);
61 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
62                                                  List *tlist, List *scan_clauses);
63 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
64                                            List *tlist, List *scan_clauses);
65 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
66                                          Plan *outer_plan, Plan *inner_plan);
67 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
68                                           Plan *outer_plan, Plan *inner_plan);
69 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
70                                          Plan *outer_plan, Plan *inner_plan);
71 static void fix_indexqual_references(List *indexquals, IndexPath *index_path,
72                                                  List **fixed_indexquals,
73                                                  List **nonlossy_indexquals,
74                                                  List **indexstrategy,
75                                                  List **indexsubtype);
76 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index,
77                                           Oid *opfamily);
78 static List *get_switched_clauses(List *clauses, Relids outerrelids);
79 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
80 static void copy_path_costsize(Plan *dest, Path *src);
81 static void copy_plan_costsize(Plan *dest, Plan *src);
82 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
83 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
84                            Oid indexid, List *indexqual, List *indexqualorig,
85                            List *indexstrategy, List *indexsubtype,
86                            ScanDirection indexscandir);
87 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
88                                           List *indexqual,
89                                           List *indexqualorig,
90                                           List *indexstrategy,
91                                           List *indexsubtype);
92 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
93                                          List *qpqual,
94                                          Plan *lefttree,
95                                          List *bitmapqualorig,
96                                          Index scanrelid);
97 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
98                          List *tidquals);
99 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
100                                   Index scanrelid, Node *funcexpr, List *funccolnames,
101                                   List *funccoltypes, List *funccoltypmods);
102 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
103                                 Index scanrelid, List *values_lists);
104 static BitmapAnd *make_bitmap_and(List *bitmapplans);
105 static BitmapOr *make_bitmap_or(List *bitmapplans);
106 static NestLoop *make_nestloop(List *tlist,
107                           List *joinclauses, List *otherclauses,
108                           Plan *lefttree, Plan *righttree,
109                           JoinType jointype);
110 static HashJoin *make_hashjoin(List *tlist,
111                           List *joinclauses, List *otherclauses,
112                           List *hashclauses,
113                           Plan *lefttree, Plan *righttree,
114                           JoinType jointype);
115 static Hash *make_hash(Plan *lefttree);
116 static MergeJoin *make_mergejoin(List *tlist,
117                            List *joinclauses, List *otherclauses,
118                            List *mergeclauses,
119                            Oid *mergefamilies,
120                            int *mergestrategies,
121                            bool *mergenullsfirst,
122                            Plan *lefttree, Plan *righttree,
123                            JoinType jointype);
124 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
125                   AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
126                   double limit_tuples);
127
128
129 /*
130  * create_plan
131  *        Creates the access plan for a query by tracing backwards through the
132  *        desired chain of pathnodes, starting at the node 'best_path'.  For
133  *        every pathnode found, we create a corresponding plan node containing
134  *        appropriate id, target list, and qualification information.
135  *
136  *        The tlists and quals in the plan tree are still in planner format,
137  *        ie, Vars still correspond to the parser's numbering.  This will be
138  *        fixed later by setrefs.c.
139  *
140  *        best_path is the best access path
141  *
142  *        Returns a Plan tree.
143  */
144 Plan *
145 create_plan(PlannerInfo *root, Path *best_path)
146 {
147         Plan       *plan;
148
149         switch (best_path->pathtype)
150         {
151                 case T_SeqScan:
152                 case T_IndexScan:
153                 case T_BitmapHeapScan:
154                 case T_TidScan:
155                 case T_SubqueryScan:
156                 case T_FunctionScan:
157                 case T_ValuesScan:
158                         plan = create_scan_plan(root, best_path);
159                         break;
160                 case T_HashJoin:
161                 case T_MergeJoin:
162                 case T_NestLoop:
163                         plan = create_join_plan(root,
164                                                                         (JoinPath *) best_path);
165                         break;
166                 case T_Append:
167                         plan = create_append_plan(root,
168                                                                           (AppendPath *) best_path);
169                         break;
170                 case T_Result:
171                         plan = (Plan *) create_result_plan(root,
172                                                                                            (ResultPath *) best_path);
173                         break;
174                 case T_Material:
175                         plan = (Plan *) create_material_plan(root,
176                                                                                                  (MaterialPath *) best_path);
177                         break;
178                 case T_Unique:
179                         plan = create_unique_plan(root,
180                                                                           (UniquePath *) best_path);
181                         break;
182                 default:
183                         elog(ERROR, "unrecognized node type: %d",
184                                  (int) best_path->pathtype);
185                         plan = NULL;            /* keep compiler quiet */
186                         break;
187         }
188
189         return plan;
190 }
191
192 /*
193  * create_scan_plan
194  *       Create a scan plan for the parent relation of 'best_path'.
195  */
196 static Plan *
197 create_scan_plan(PlannerInfo *root, Path *best_path)
198 {
199         RelOptInfo *rel = best_path->parent;
200         List       *tlist;
201         List       *scan_clauses;
202         Plan       *plan;
203
204         /*
205          * For table scans, rather than using the relation targetlist (which is
206          * only those Vars actually needed by the query), we prefer to generate a
207          * tlist containing all Vars in order.  This will allow the executor to
208          * optimize away projection of the table tuples, if possible.  (Note that
209          * planner.c may replace the tlist we generate here, forcing projection to
210          * occur.)
211          */
212         if (use_physical_tlist(rel))
213         {
214                 tlist = build_physical_tlist(root, rel);
215                 /* if fail because of dropped cols, use regular method */
216                 if (tlist == NIL)
217                         tlist = build_relation_tlist(rel);
218         }
219         else
220                 tlist = build_relation_tlist(rel);
221
222         /*
223          * Extract the relevant restriction clauses from the parent relation. The
224          * executor must apply all these restrictions during the scan, except for
225          * pseudoconstants which we'll take care of below.
226          */
227         scan_clauses = rel->baserestrictinfo;
228
229         switch (best_path->pathtype)
230         {
231                 case T_SeqScan:
232                         plan = (Plan *) create_seqscan_plan(root,
233                                                                                                 best_path,
234                                                                                                 tlist,
235                                                                                                 scan_clauses);
236                         break;
237
238                 case T_IndexScan:
239                         plan = (Plan *) create_indexscan_plan(root,
240                                                                                                   (IndexPath *) best_path,
241                                                                                                   tlist,
242                                                                                                   scan_clauses,
243                                                                                                   NULL);
244                         break;
245
246                 case T_BitmapHeapScan:
247                         plan = (Plan *) create_bitmap_scan_plan(root,
248                                                                                                 (BitmapHeapPath *) best_path,
249                                                                                                         tlist,
250                                                                                                         scan_clauses);
251                         break;
252
253                 case T_TidScan:
254                         plan = (Plan *) create_tidscan_plan(root,
255                                                                                                 (TidPath *) best_path,
256                                                                                                 tlist,
257                                                                                                 scan_clauses);
258                         break;
259
260                 case T_SubqueryScan:
261                         plan = (Plan *) create_subqueryscan_plan(root,
262                                                                                                          best_path,
263                                                                                                          tlist,
264                                                                                                          scan_clauses);
265                         break;
266
267                 case T_FunctionScan:
268                         plan = (Plan *) create_functionscan_plan(root,
269                                                                                                          best_path,
270                                                                                                          tlist,
271                                                                                                          scan_clauses);
272                         break;
273
274                 case T_ValuesScan:
275                         plan = (Plan *) create_valuesscan_plan(root,
276                                                                                                    best_path,
277                                                                                                    tlist,
278                                                                                                    scan_clauses);
279                         break;
280
281                 default:
282                         elog(ERROR, "unrecognized node type: %d",
283                                  (int) best_path->pathtype);
284                         plan = NULL;            /* keep compiler quiet */
285                         break;
286         }
287
288         /*
289          * If there are any pseudoconstant clauses attached to this node, insert a
290          * gating Result node that evaluates the pseudoconstants as one-time
291          * quals.
292          */
293         if (root->hasPseudoConstantQuals)
294                 plan = create_gating_plan(root, plan, scan_clauses);
295
296         return plan;
297 }
298
299 /*
300  * Build a target list (ie, a list of TargetEntry) for a relation.
301  */
302 static List *
303 build_relation_tlist(RelOptInfo *rel)
304 {
305         List       *tlist = NIL;
306         int                     resno = 1;
307         ListCell   *v;
308
309         foreach(v, rel->reltargetlist)
310         {
311                 /* Do we really need to copy here?      Not sure */
312                 Var                *var = (Var *) copyObject(lfirst(v));
313
314                 tlist = lappend(tlist, makeTargetEntry((Expr *) var,
315                                                                                            resno,
316                                                                                            NULL,
317                                                                                            false));
318                 resno++;
319         }
320         return tlist;
321 }
322
323 /*
324  * use_physical_tlist
325  *              Decide whether to use a tlist matching relation structure,
326  *              rather than only those Vars actually referenced.
327  */
328 static bool
329 use_physical_tlist(RelOptInfo *rel)
330 {
331         int                     i;
332
333         /*
334          * We can do this for real relation scans, subquery scans, function scans,
335          * and values scans (but not for, eg, joins).
336          */
337         if (rel->rtekind != RTE_RELATION &&
338                 rel->rtekind != RTE_SUBQUERY &&
339                 rel->rtekind != RTE_FUNCTION &&
340                 rel->rtekind != RTE_VALUES)
341                 return false;
342
343         /*
344          * Can't do it with inheritance cases either (mainly because Append
345          * doesn't project).
346          */
347         if (rel->reloptkind != RELOPT_BASEREL)
348                 return false;
349
350         /*
351          * Can't do it if any system columns or whole-row Vars are requested,
352          * either.      (This could possibly be fixed but would take some fragile
353          * assumptions in setrefs.c, I think.)
354          */
355         for (i = rel->min_attr; i <= 0; i++)
356         {
357                 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
358                         return false;
359         }
360
361         return true;
362 }
363
364 /*
365  * disuse_physical_tlist
366  *              Switch a plan node back to emitting only Vars actually referenced.
367  *
368  * If the plan node immediately above a scan would prefer to get only
369  * needed Vars and not a physical tlist, it must call this routine to
370  * undo the decision made by use_physical_tlist().      Currently, Hash, Sort,
371  * and Material nodes want this, so they don't have to store useless columns.
372  */
373 static void
374 disuse_physical_tlist(Plan *plan, Path *path)
375 {
376         /* Only need to undo it for path types handled by create_scan_plan() */
377         switch (path->pathtype)
378         {
379                 case T_SeqScan:
380                 case T_IndexScan:
381                 case T_BitmapHeapScan:
382                 case T_TidScan:
383                 case T_SubqueryScan:
384                 case T_FunctionScan:
385                 case T_ValuesScan:
386                         plan->targetlist = build_relation_tlist(path->parent);
387                         break;
388                 default:
389                         break;
390         }
391 }
392
393 /*
394  * create_gating_plan
395  *        Deal with pseudoconstant qual clauses
396  *
397  * If the node's quals list includes any pseudoconstant quals, put them
398  * into a gating Result node atop the already-built plan.  Otherwise,
399  * return the plan as-is.
400  *
401  * Note that we don't change cost or size estimates when doing gating.
402  * The costs of qual eval were already folded into the plan's startup cost.
403  * Leaving the size alone amounts to assuming that the gating qual will
404  * succeed, which is the conservative estimate for planning upper queries.
405  * We certainly don't want to assume the output size is zero (unless the
406  * gating qual is actually constant FALSE, and that case is dealt with in
407  * clausesel.c).  Interpolating between the two cases is silly, because
408  * it doesn't reflect what will really happen at runtime, and besides which
409  * in most cases we have only a very bad idea of the probability of the gating
410  * qual being true.
411  */
412 static Plan *
413 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
414 {
415         List       *pseudoconstants;
416
417         /* Sort into desirable execution order while still in RestrictInfo form */
418         quals = order_qual_clauses(root, quals);
419
420         /* Pull out any pseudoconstant quals from the RestrictInfo list */
421         pseudoconstants = extract_actual_clauses(quals, true);
422
423         if (!pseudoconstants)
424                 return plan;
425
426         return (Plan *) make_result(root,
427                                                                 plan->targetlist,
428                                                                 (Node *) pseudoconstants,
429                                                                 plan);
430 }
431
432 /*
433  * create_join_plan
434  *        Create a join plan for 'best_path' and (recursively) plans for its
435  *        inner and outer paths.
436  */
437 static Plan *
438 create_join_plan(PlannerInfo *root, JoinPath *best_path)
439 {
440         Plan       *outer_plan;
441         Plan       *inner_plan;
442         Plan       *plan;
443
444         outer_plan = create_plan(root, best_path->outerjoinpath);
445         inner_plan = create_plan(root, best_path->innerjoinpath);
446
447         switch (best_path->path.pathtype)
448         {
449                 case T_MergeJoin:
450                         plan = (Plan *) create_mergejoin_plan(root,
451                                                                                                   (MergePath *) best_path,
452                                                                                                   outer_plan,
453                                                                                                   inner_plan);
454                         break;
455                 case T_HashJoin:
456                         plan = (Plan *) create_hashjoin_plan(root,
457                                                                                                  (HashPath *) best_path,
458                                                                                                  outer_plan,
459                                                                                                  inner_plan);
460                         break;
461                 case T_NestLoop:
462                         plan = (Plan *) create_nestloop_plan(root,
463                                                                                                  (NestPath *) best_path,
464                                                                                                  outer_plan,
465                                                                                                  inner_plan);
466                         break;
467                 default:
468                         elog(ERROR, "unrecognized node type: %d",
469                                  (int) best_path->path.pathtype);
470                         plan = NULL;            /* keep compiler quiet */
471                         break;
472         }
473
474         /*
475          * If there are any pseudoconstant clauses attached to this node, insert a
476          * gating Result node that evaluates the pseudoconstants as one-time
477          * quals.
478          */
479         if (root->hasPseudoConstantQuals)
480                 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
481
482 #ifdef NOT_USED
483
484         /*
485          * * Expensive function pullups may have pulled local predicates * into
486          * this path node.      Put them in the qpqual of the plan node. * JMH,
487          * 6/15/92
488          */
489         if (get_loc_restrictinfo(best_path) != NIL)
490                 set_qpqual((Plan) plan,
491                                    list_concat(get_qpqual((Plan) plan),
492                                            get_actual_clauses(get_loc_restrictinfo(best_path))));
493 #endif
494
495         return plan;
496 }
497
498 /*
499  * create_append_plan
500  *        Create an Append plan for 'best_path' and (recursively) plans
501  *        for its subpaths.
502  *
503  *        Returns a Plan node.
504  */
505 static Plan *
506 create_append_plan(PlannerInfo *root, AppendPath *best_path)
507 {
508         Append     *plan;
509         List       *tlist = build_relation_tlist(best_path->path.parent);
510         List       *subplans = NIL;
511         ListCell   *subpaths;
512
513         /*
514          * It is possible for the subplans list to contain only one entry, or even
515          * no entries.  Handle these cases specially.
516          *
517          * XXX ideally, if there's just one entry, we'd not bother to generate an
518          * Append node but just return the single child.  At the moment this does
519          * not work because the varno of the child scan plan won't match the
520          * parent-rel Vars it'll be asked to emit.
521          */
522         if (best_path->subpaths == NIL)
523         {
524                 /* Generate a Result plan with constant-FALSE gating qual */
525                 return (Plan *) make_result(root,
526                                                                         tlist,
527                                                                         (Node *) list_make1(makeBoolConst(false,
528                                                                                                                                           false)),
529                                                                         NULL);
530         }
531
532         /* Normal case with multiple subpaths */
533         foreach(subpaths, best_path->subpaths)
534         {
535                 Path       *subpath = (Path *) lfirst(subpaths);
536
537                 subplans = lappend(subplans, create_plan(root, subpath));
538         }
539
540         plan = make_append(subplans, false, tlist);
541
542         return (Plan *) plan;
543 }
544
545 /*
546  * create_result_plan
547  *        Create a Result plan for 'best_path'.
548  *        This is only used for the case of a query with an empty jointree.
549  *
550  *        Returns a Plan node.
551  */
552 static Result *
553 create_result_plan(PlannerInfo *root, ResultPath *best_path)
554 {
555         List       *tlist;
556         List       *quals;
557
558         /* The tlist will be installed later, since we have no RelOptInfo */
559         Assert(best_path->path.parent == NULL);
560         tlist = NIL;
561
562         /* best_path->quals is just bare clauses */
563
564         quals = order_qual_clauses(root, best_path->quals);
565
566         return make_result(root, tlist, (Node *) quals, NULL);
567 }
568
569 /*
570  * create_material_plan
571  *        Create a Material plan for 'best_path' and (recursively) plans
572  *        for its subpaths.
573  *
574  *        Returns a Plan node.
575  */
576 static Material *
577 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
578 {
579         Material   *plan;
580         Plan       *subplan;
581
582         subplan = create_plan(root, best_path->subpath);
583
584         /* We don't want any excess columns in the materialized tuples */
585         disuse_physical_tlist(subplan, best_path->subpath);
586
587         plan = make_material(subplan);
588
589         copy_path_costsize(&plan->plan, (Path *) best_path);
590
591         return plan;
592 }
593
594 /*
595  * create_unique_plan
596  *        Create a Unique plan for 'best_path' and (recursively) plans
597  *        for its subpaths.
598  *
599  *        Returns a Plan node.
600  */
601 static Plan *
602 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
603 {
604         Plan       *plan;
605         Plan       *subplan;
606         List       *uniq_exprs;
607         List       *in_operators;
608         List       *newtlist;
609         int                     nextresno;
610         bool            newitems;
611         int                     numGroupCols;
612         AttrNumber *groupColIdx;
613         int                     groupColPos;
614         ListCell   *l;
615
616         subplan = create_plan(root, best_path->subpath);
617
618         /* Done if we don't need to do any actual unique-ifying */
619         if (best_path->umethod == UNIQUE_PATH_NOOP)
620                 return subplan;
621
622         /*----------
623          * As constructed, the subplan has a "flat" tlist containing just the
624          * Vars needed here and at upper levels.  The values we are supposed
625          * to unique-ify may be expressions in these variables.  We have to
626          * add any such expressions to the subplan's tlist.
627          *
628          * The subplan may have a "physical" tlist if it is a simple scan plan.
629          * This should be left as-is if we don't need to add any expressions;
630          * but if we do have to add expressions, then a projection step will be
631          * needed at runtime anyway, and so we may as well remove unneeded items.
632          * Therefore newtlist starts from build_relation_tlist() not just a
633          * copy of the subplan's tlist; and we don't install it into the subplan
634          * unless stuff has to be added.
635          *
636          * To find the correct list of values to unique-ify, we look in the
637          * information saved for IN expressions.  If this code is ever used in
638          * other scenarios, some other way of finding what to unique-ify will
639          * be needed.  The IN clause's operators are needed too, since they
640          * determine what the meaning of "unique" is in this context.
641          *----------
642          */
643         uniq_exprs = NIL;                       /* just to keep compiler quiet */
644         in_operators = NIL;
645         foreach(l, root->in_info_list)
646         {
647                 InClauseInfo *ininfo = (InClauseInfo *) lfirst(l);
648
649                 if (bms_equal(ininfo->righthand, best_path->path.parent->relids))
650                 {
651                         uniq_exprs = ininfo->sub_targetlist;
652                         in_operators = ininfo->in_operators;
653                         break;
654                 }
655         }
656         if (l == NULL)                          /* fell out of loop? */
657                 elog(ERROR, "could not find UniquePath in in_info_list");
658
659         /* initialize modified subplan tlist as just the "required" vars */
660         newtlist = build_relation_tlist(best_path->path.parent);
661         nextresno = list_length(newtlist) + 1;
662         newitems = false;
663
664         foreach(l, uniq_exprs)
665         {
666                 Node       *uniqexpr = lfirst(l);
667                 TargetEntry *tle;
668
669                 tle = tlist_member(uniqexpr, newtlist);
670                 if (!tle)
671                 {
672                         tle = makeTargetEntry((Expr *) uniqexpr,
673                                                                   nextresno,
674                                                                   NULL,
675                                                                   false);
676                         newtlist = lappend(newtlist, tle);
677                         nextresno++;
678                         newitems = true;
679                 }
680         }
681
682         if (newitems)
683         {
684                 /*
685                  * If the top plan node can't do projections, we need to add a Result
686                  * node to help it along.
687                  */
688                 if (!is_projection_capable_plan(subplan))
689                         subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
690                 else
691                         subplan->targetlist = newtlist;
692         }
693
694         /*
695          * Build control information showing which subplan output columns are to
696          * be examined by the grouping step.  Unfortunately we can't merge this
697          * with the previous loop, since we didn't then know which version of the
698          * subplan tlist we'd end up using.
699          */
700         newtlist = subplan->targetlist;
701         numGroupCols = list_length(uniq_exprs);
702         groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
703
704         groupColPos = 0;
705         foreach(l, uniq_exprs)
706         {
707                 Node       *uniqexpr = lfirst(l);
708                 TargetEntry *tle;
709
710                 tle = tlist_member(uniqexpr, newtlist);
711                 if (!tle)                               /* shouldn't happen */
712                         elog(ERROR, "failed to find unique expression in subplan tlist");
713                 groupColIdx[groupColPos++] = tle->resno;
714         }
715
716         if (best_path->umethod == UNIQUE_PATH_HASH)
717         {
718                 long            numGroups;
719                 Oid                *groupOperators;
720
721                 numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
722
723                 /*
724                  * Get the hashable equality operators for the Agg node to use.
725                  * Normally these are the same as the IN clause operators, but if
726                  * those are cross-type operators then the equality operators are
727                  * the ones for the IN clause operators' RHS datatype.
728                  */
729                 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
730                 groupColPos = 0;
731                 foreach(l, in_operators)
732                 {
733                         Oid                     in_oper = lfirst_oid(l);
734                         Oid                     eq_oper;
735
736                         if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
737                                 elog(ERROR, "could not find compatible hash operator for operator %u",
738                                          in_oper);
739                         groupOperators[groupColPos++] = eq_oper;
740                 }
741
742                 /*
743                  * Since the Agg node is going to project anyway, we can give it the
744                  * minimum output tlist, without any stuff we might have added to the
745                  * subplan tlist.
746                  */
747                 plan = (Plan *) make_agg(root,
748                                                                  build_relation_tlist(best_path->path.parent),
749                                                                  NIL,
750                                                                  AGG_HASHED,
751                                                                  numGroupCols,
752                                                                  groupColIdx,
753                                                                  groupOperators,
754                                                                  numGroups,
755                                                                  0,
756                                                                  subplan);
757         }
758         else
759         {
760                 List       *sortList = NIL;
761
762                 /* Create an ORDER BY list to sort the input compatibly */
763                 groupColPos = 0;
764                 foreach(l, in_operators)
765                 {
766                         Oid                     in_oper = lfirst_oid(l);
767                         Oid                     sortop;
768                         TargetEntry *tle;
769                         SortClause *sortcl;
770
771                         sortop = get_ordering_op_for_equality_op(in_oper, false);
772                         if (!OidIsValid(sortop))                /* shouldn't happen */
773                                 elog(ERROR, "could not find ordering operator for equality operator %u",
774                                          in_oper);
775                         tle = get_tle_by_resno(subplan->targetlist,
776                                                                    groupColIdx[groupColPos]);
777                         Assert(tle != NULL);
778                         sortcl = makeNode(SortClause);
779                         sortcl->tleSortGroupRef = assignSortGroupRef(tle,
780                                                                                                                  subplan->targetlist);
781                         sortcl->sortop = sortop;
782                         sortcl->nulls_first = false;
783                         sortList = lappend(sortList, sortcl);
784                         groupColPos++;
785                 }
786                 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
787                 plan = (Plan *) make_unique(plan, sortList);
788         }
789
790         /* Adjust output size estimate (other fields should be OK already) */
791         plan->plan_rows = best_path->rows;
792
793         return plan;
794 }
795
796
797 /*****************************************************************************
798  *
799  *      BASE-RELATION SCAN METHODS
800  *
801  *****************************************************************************/
802
803
804 /*
805  * create_seqscan_plan
806  *       Returns a seqscan plan for the base relation scanned by 'best_path'
807  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
808  */
809 static SeqScan *
810 create_seqscan_plan(PlannerInfo *root, Path *best_path,
811                                         List *tlist, List *scan_clauses)
812 {
813         SeqScan    *scan_plan;
814         Index           scan_relid = best_path->parent->relid;
815
816         /* it should be a base rel... */
817         Assert(scan_relid > 0);
818         Assert(best_path->parent->rtekind == RTE_RELATION);
819
820         /* Sort clauses into best execution order */
821         scan_clauses = order_qual_clauses(root, scan_clauses);
822
823         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
824         scan_clauses = extract_actual_clauses(scan_clauses, false);
825
826         scan_plan = make_seqscan(tlist,
827                                                          scan_clauses,
828                                                          scan_relid);
829
830         copy_path_costsize(&scan_plan->plan, best_path);
831
832         return scan_plan;
833 }
834
835 /*
836  * create_indexscan_plan
837  *        Returns an indexscan plan for the base relation scanned by 'best_path'
838  *        with restriction clauses 'scan_clauses' and targetlist 'tlist'.
839  *
840  * The indexquals list of the path contains implicitly-ANDed qual conditions.
841  * The list can be empty --- then no index restrictions will be applied during
842  * the scan.
843  *
844  * If nonlossy_clauses isn't NULL, *nonlossy_clauses receives a list of the
845  * nonlossy indexquals.
846  */
847 static IndexScan *
848 create_indexscan_plan(PlannerInfo *root,
849                                           IndexPath *best_path,
850                                           List *tlist,
851                                           List *scan_clauses,
852                                           List **nonlossy_clauses)
853 {
854         List       *indexquals = best_path->indexquals;
855         Index           baserelid = best_path->path.parent->relid;
856         Oid                     indexoid = best_path->indexinfo->indexoid;
857         List       *qpqual;
858         List       *stripped_indexquals;
859         List       *fixed_indexquals;
860         List       *nonlossy_indexquals;
861         List       *indexstrategy;
862         List       *indexsubtype;
863         ListCell   *l;
864         IndexScan  *scan_plan;
865
866         /* it should be a base rel... */
867         Assert(baserelid > 0);
868         Assert(best_path->path.parent->rtekind == RTE_RELATION);
869
870         /*
871          * Build "stripped" indexquals structure (no RestrictInfos) to pass to
872          * executor as indexqualorig
873          */
874         stripped_indexquals = get_actual_clauses(indexquals);
875
876         /*
877          * The executor needs a copy with the indexkey on the left of each clause
878          * and with index attr numbers substituted for table ones. This pass also
879          * gets strategy info and looks for "lossy" operators.
880          */
881         fix_indexqual_references(indexquals, best_path,
882                                                          &fixed_indexquals,
883                                                          &nonlossy_indexquals,
884                                                          &indexstrategy,
885                                                          &indexsubtype);
886
887         /* pass back nonlossy quals if caller wants 'em */
888         if (nonlossy_clauses)
889                 *nonlossy_clauses = nonlossy_indexquals;
890
891         /*
892          * If this is an innerjoin scan, the indexclauses will contain join
893          * clauses that are not present in scan_clauses (since the passed-in value
894          * is just the rel's baserestrictinfo list).  We must add these clauses to
895          * scan_clauses to ensure they get checked.  In most cases we will remove
896          * the join clauses again below, but if a join clause contains a special
897          * operator, we need to make sure it gets into the scan_clauses.
898          *
899          * Note: pointer comparison should be enough to determine RestrictInfo
900          * matches.
901          */
902         if (best_path->isjoininner)
903                 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
904
905         /*
906          * The qpqual list must contain all restrictions not automatically handled
907          * by the index.  All the predicates in the indexquals will be checked
908          * (either by the index itself, or by nodeIndexscan.c), but if there are
909          * any "special" operators involved then they must be included in qpqual.
910          * Also, any lossy index operators must be rechecked in the qpqual.  The
911          * upshot is that qpqual must contain scan_clauses minus whatever appears
912          * in nonlossy_indexquals.
913          *
914          * In normal cases simple pointer equality checks will be enough to spot
915          * duplicate RestrictInfos, so we try that first.  In some situations
916          * (particularly with OR'd index conditions) we may have scan_clauses that
917          * are not equal to, but are logically implied by, the index quals; so we
918          * also try a predicate_implied_by() check to see if we can discard quals
919          * that way.  (predicate_implied_by assumes its first input contains only
920          * immutable functions, so we have to check that.)
921          *
922          * We can also discard quals that are implied by a partial index's
923          * predicate, but only in a plain SELECT; when scanning a target relation
924          * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
925          * plan so that they'll be properly rechecked by EvalPlanQual testing.
926          */
927         qpqual = NIL;
928         foreach(l, scan_clauses)
929         {
930                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
931
932                 Assert(IsA(rinfo, RestrictInfo));
933                 if (rinfo->pseudoconstant)
934                         continue;                       /* we may drop pseudoconstants here */
935                 if (list_member_ptr(nonlossy_indexquals, rinfo))
936                         continue;
937                 if (!contain_mutable_functions((Node *) rinfo->clause))
938                 {
939                         List       *clausel = list_make1(rinfo->clause);
940
941                         if (predicate_implied_by(clausel, nonlossy_indexquals))
942                                 continue;
943                         if (best_path->indexinfo->indpred)
944                         {
945                                 if (baserelid != root->parse->resultRelation &&
946                                         get_rowmark(root->parse, baserelid) == NULL)
947                                         if (predicate_implied_by(clausel,
948                                                                                          best_path->indexinfo->indpred))
949                                                 continue;
950                         }
951                 }
952                 qpqual = lappend(qpqual, rinfo);
953         }
954
955         /* Sort clauses into best execution order */
956         qpqual = order_qual_clauses(root, qpqual);
957
958         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
959         qpqual = extract_actual_clauses(qpqual, false);
960
961         /* Finally ready to build the plan node */
962         scan_plan = make_indexscan(tlist,
963                                                            qpqual,
964                                                            baserelid,
965                                                            indexoid,
966                                                            fixed_indexquals,
967                                                            stripped_indexquals,
968                                                            indexstrategy,
969                                                            indexsubtype,
970                                                            best_path->indexscandir);
971
972         copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
973         /* use the indexscan-specific rows estimate, not the parent rel's */
974         scan_plan->scan.plan.plan_rows = best_path->rows;
975
976         return scan_plan;
977 }
978
979 /*
980  * create_bitmap_scan_plan
981  *        Returns a bitmap scan plan for the base relation scanned by 'best_path'
982  *        with restriction clauses 'scan_clauses' and targetlist 'tlist'.
983  */
984 static BitmapHeapScan *
985 create_bitmap_scan_plan(PlannerInfo *root,
986                                                 BitmapHeapPath *best_path,
987                                                 List *tlist,
988                                                 List *scan_clauses)
989 {
990         Index           baserelid = best_path->path.parent->relid;
991         Plan       *bitmapqualplan;
992         List       *bitmapqualorig;
993         List       *indexquals;
994         List       *qpqual;
995         ListCell   *l;
996         BitmapHeapScan *scan_plan;
997
998         /* it should be a base rel... */
999         Assert(baserelid > 0);
1000         Assert(best_path->path.parent->rtekind == RTE_RELATION);
1001
1002         /* Process the bitmapqual tree into a Plan tree and qual lists */
1003         bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1004                                                                                    &bitmapqualorig, &indexquals);
1005
1006         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1007         scan_clauses = extract_actual_clauses(scan_clauses, false);
1008
1009         /*
1010          * If this is a innerjoin scan, the indexclauses will contain join clauses
1011          * that are not present in scan_clauses (since the passed-in value is just
1012          * the rel's baserestrictinfo list).  We must add these clauses to
1013          * scan_clauses to ensure they get checked.  In most cases we will remove
1014          * the join clauses again below, but if a join clause contains a special
1015          * operator, we need to make sure it gets into the scan_clauses.
1016          */
1017         if (best_path->isjoininner)
1018         {
1019                 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1020         }
1021
1022         /*
1023          * The qpqual list must contain all restrictions not automatically handled
1024          * by the index.  All the predicates in the indexquals will be checked
1025          * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1026          * are any "special" or lossy operators involved then they must be added
1027          * to qpqual.  The upshot is that qpqual must contain scan_clauses minus
1028          * whatever appears in indexquals.
1029          *
1030          * In normal cases simple equal() checks will be enough to spot duplicate
1031          * clauses, so we try that first.  In some situations (particularly with
1032          * OR'd index conditions) we may have scan_clauses that are not equal to,
1033          * but are logically implied by, the index quals; so we also try a
1034          * predicate_implied_by() check to see if we can discard quals that way.
1035          * (predicate_implied_by assumes its first input contains only immutable
1036          * functions, so we have to check that.)
1037          *
1038          * Unlike create_indexscan_plan(), we need take no special thought here
1039          * for partial index predicates; this is because the predicate conditions
1040          * are already listed in bitmapqualorig and indexquals.  Bitmap scans have
1041          * to do it that way because predicate conditions need to be rechecked if
1042          * the scan becomes lossy.
1043          */
1044         qpqual = NIL;
1045         foreach(l, scan_clauses)
1046         {
1047                 Node       *clause = (Node *) lfirst(l);
1048
1049                 if (list_member(indexquals, clause))
1050                         continue;
1051                 if (!contain_mutable_functions(clause))
1052                 {
1053                         List       *clausel = list_make1(clause);
1054
1055                         if (predicate_implied_by(clausel, indexquals))
1056                                 continue;
1057                 }
1058                 qpqual = lappend(qpqual, clause);
1059         }
1060
1061         /* Sort clauses into best execution order */
1062         qpqual = order_qual_clauses(root, qpqual);
1063
1064         /*
1065          * When dealing with special or lossy operators, we will at this point
1066          * have duplicate clauses in qpqual and bitmapqualorig.  We may as well
1067          * drop 'em from bitmapqualorig, since there's no point in making the
1068          * tests twice.
1069          */
1070         bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1071
1072         /* Finally ready to build the plan node */
1073         scan_plan = make_bitmap_heapscan(tlist,
1074                                                                          qpqual,
1075                                                                          bitmapqualplan,
1076                                                                          bitmapqualorig,
1077                                                                          baserelid);
1078
1079         copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1080         /* use the indexscan-specific rows estimate, not the parent rel's */
1081         scan_plan->scan.plan.plan_rows = best_path->rows;
1082
1083         return scan_plan;
1084 }
1085
1086 /*
1087  * Given a bitmapqual tree, generate the Plan tree that implements it
1088  *
1089  * As byproducts, we also return in *qual and *indexqual the qual lists
1090  * (in implicit-AND form, without RestrictInfos) describing the original index
1091  * conditions and the generated indexqual conditions.  The latter is made to
1092  * exclude lossy index operators.  Both lists include partial-index predicates,
1093  * because we have to recheck predicates as well as index conditions if the
1094  * bitmap scan becomes lossy.
1095  *
1096  * Note: if you find yourself changing this, you probably need to change
1097  * make_restrictinfo_from_bitmapqual too.
1098  */
1099 static Plan *
1100 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1101                                           List **qual, List **indexqual)
1102 {
1103         Plan       *plan;
1104
1105         if (IsA(bitmapqual, BitmapAndPath))
1106         {
1107                 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1108                 List       *subplans = NIL;
1109                 List       *subquals = NIL;
1110                 List       *subindexquals = NIL;
1111                 ListCell   *l;
1112
1113                 /*
1114                  * There may well be redundant quals among the subplans, since a
1115                  * top-level WHERE qual might have gotten used to form several
1116                  * different index quals.  We don't try exceedingly hard to eliminate
1117                  * redundancies, but we do eliminate obvious duplicates by using
1118                  * list_concat_unique.
1119                  */
1120                 foreach(l, apath->bitmapquals)
1121                 {
1122                         Plan       *subplan;
1123                         List       *subqual;
1124                         List       *subindexqual;
1125
1126                         subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1127                                                                                         &subqual, &subindexqual);
1128                         subplans = lappend(subplans, subplan);
1129                         subquals = list_concat_unique(subquals, subqual);
1130                         subindexquals = list_concat_unique(subindexquals, subindexqual);
1131                 }
1132                 plan = (Plan *) make_bitmap_and(subplans);
1133                 plan->startup_cost = apath->path.startup_cost;
1134                 plan->total_cost = apath->path.total_cost;
1135                 plan->plan_rows =
1136                         clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1137                 plan->plan_width = 0;   /* meaningless */
1138                 *qual = subquals;
1139                 *indexqual = subindexquals;
1140         }
1141         else if (IsA(bitmapqual, BitmapOrPath))
1142         {
1143                 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1144                 List       *subplans = NIL;
1145                 List       *subquals = NIL;
1146                 List       *subindexquals = NIL;
1147                 bool            const_true_subqual = false;
1148                 bool            const_true_subindexqual = false;
1149                 ListCell   *l;
1150
1151                 /*
1152                  * Here, we only detect qual-free subplans.  A qual-free subplan would
1153                  * cause us to generate "... OR true ..."  which we may as well reduce
1154                  * to just "true".      We do not try to eliminate redundant subclauses
1155                  * because (a) it's not as likely as in the AND case, and (b) we might
1156                  * well be working with hundreds or even thousands of OR conditions,
1157                  * perhaps from a long IN list.  The performance of list_append_unique
1158                  * would be unacceptable.
1159                  */
1160                 foreach(l, opath->bitmapquals)
1161                 {
1162                         Plan       *subplan;
1163                         List       *subqual;
1164                         List       *subindexqual;
1165
1166                         subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1167                                                                                         &subqual, &subindexqual);
1168                         subplans = lappend(subplans, subplan);
1169                         if (subqual == NIL)
1170                                 const_true_subqual = true;
1171                         else if (!const_true_subqual)
1172                                 subquals = lappend(subquals,
1173                                                                    make_ands_explicit(subqual));
1174                         if (subindexqual == NIL)
1175                                 const_true_subindexqual = true;
1176                         else if (!const_true_subindexqual)
1177                                 subindexquals = lappend(subindexquals,
1178                                                                                 make_ands_explicit(subindexqual));
1179                 }
1180
1181                 /*
1182                  * In the presence of ScalarArrayOpExpr quals, we might have built
1183                  * BitmapOrPaths with just one subpath; don't add an OR step.
1184                  */
1185                 if (list_length(subplans) == 1)
1186                 {
1187                         plan = (Plan *) linitial(subplans);
1188                 }
1189                 else
1190                 {
1191                         plan = (Plan *) make_bitmap_or(subplans);
1192                         plan->startup_cost = opath->path.startup_cost;
1193                         plan->total_cost = opath->path.total_cost;
1194                         plan->plan_rows =
1195                                 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1196                         plan->plan_width = 0;           /* meaningless */
1197                 }
1198
1199                 /*
1200                  * If there were constant-TRUE subquals, the OR reduces to constant
1201                  * TRUE.  Also, avoid generating one-element ORs, which could happen
1202                  * due to redundancy elimination or ScalarArrayOpExpr quals.
1203                  */
1204                 if (const_true_subqual)
1205                         *qual = NIL;
1206                 else if (list_length(subquals) <= 1)
1207                         *qual = subquals;
1208                 else
1209                         *qual = list_make1(make_orclause(subquals));
1210                 if (const_true_subindexqual)
1211                         *indexqual = NIL;
1212                 else if (list_length(subindexquals) <= 1)
1213                         *indexqual = subindexquals;
1214                 else
1215                         *indexqual = list_make1(make_orclause(subindexquals));
1216         }
1217         else if (IsA(bitmapqual, IndexPath))
1218         {
1219                 IndexPath  *ipath = (IndexPath *) bitmapqual;
1220                 IndexScan  *iscan;
1221                 List       *nonlossy_clauses;
1222                 ListCell   *l;
1223
1224                 /* Use the regular indexscan plan build machinery... */
1225                 iscan = create_indexscan_plan(root, ipath, NIL, NIL,
1226                                                                           &nonlossy_clauses);
1227                 /* then convert to a bitmap indexscan */
1228                 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1229                                                                                           iscan->indexid,
1230                                                                                           iscan->indexqual,
1231                                                                                           iscan->indexqualorig,
1232                                                                                           iscan->indexstrategy,
1233                                                                                           iscan->indexsubtype);
1234                 plan->startup_cost = 0.0;
1235                 plan->total_cost = ipath->indextotalcost;
1236                 plan->plan_rows =
1237                         clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1238                 plan->plan_width = 0;   /* meaningless */
1239                 *qual = get_actual_clauses(ipath->indexclauses);
1240                 *indexqual = get_actual_clauses(nonlossy_clauses);
1241                 foreach(l, ipath->indexinfo->indpred)
1242                 {
1243                         Expr       *pred = (Expr *) lfirst(l);
1244
1245                         /*
1246                          * We know that the index predicate must have been implied by the
1247                          * query condition as a whole, but it may or may not be implied by
1248                          * the conditions that got pushed into the bitmapqual.  Avoid
1249                          * generating redundant conditions.
1250                          */
1251                         if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1252                         {
1253                                 *qual = lappend(*qual, pred);
1254                                 *indexqual = lappend(*indexqual, pred);
1255                         }
1256                 }
1257         }
1258         else
1259         {
1260                 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1261                 plan = NULL;                    /* keep compiler quiet */
1262         }
1263
1264         return plan;
1265 }
1266
1267 /*
1268  * create_tidscan_plan
1269  *       Returns a tidscan plan for the base relation scanned by 'best_path'
1270  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1271  */
1272 static TidScan *
1273 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1274                                         List *tlist, List *scan_clauses)
1275 {
1276         TidScan    *scan_plan;
1277         Index           scan_relid = best_path->path.parent->relid;
1278         List       *ortidquals;
1279
1280         /* it should be a base rel... */
1281         Assert(scan_relid > 0);
1282         Assert(best_path->path.parent->rtekind == RTE_RELATION);
1283
1284         /* Sort clauses into best execution order */
1285         scan_clauses = order_qual_clauses(root, scan_clauses);
1286
1287         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1288         scan_clauses = extract_actual_clauses(scan_clauses, false);
1289
1290         /*
1291          * Remove any clauses that are TID quals.  This is a bit tricky since the
1292          * tidquals list has implicit OR semantics.
1293          */
1294         ortidquals = best_path->tidquals;
1295         if (list_length(ortidquals) > 1)
1296                 ortidquals = list_make1(make_orclause(ortidquals));
1297         scan_clauses = list_difference(scan_clauses, ortidquals);
1298
1299         scan_plan = make_tidscan(tlist,
1300                                                          scan_clauses,
1301                                                          scan_relid,
1302                                                          best_path->tidquals);
1303
1304         copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1305
1306         return scan_plan;
1307 }
1308
1309 /*
1310  * create_subqueryscan_plan
1311  *       Returns a subqueryscan plan for the base relation scanned by 'best_path'
1312  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1313  */
1314 static SubqueryScan *
1315 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1316                                                  List *tlist, List *scan_clauses)
1317 {
1318         SubqueryScan *scan_plan;
1319         Index           scan_relid = best_path->parent->relid;
1320
1321         /* it should be a subquery base rel... */
1322         Assert(scan_relid > 0);
1323         Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1324
1325         /* Sort clauses into best execution order */
1326         scan_clauses = order_qual_clauses(root, scan_clauses);
1327
1328         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1329         scan_clauses = extract_actual_clauses(scan_clauses, false);
1330
1331         scan_plan = make_subqueryscan(tlist,
1332                                                                   scan_clauses,
1333                                                                   scan_relid,
1334                                                                   best_path->parent->subplan,
1335                                                                   best_path->parent->subrtable);
1336
1337         copy_path_costsize(&scan_plan->scan.plan, best_path);
1338
1339         return scan_plan;
1340 }
1341
1342 /*
1343  * create_functionscan_plan
1344  *       Returns a functionscan plan for the base relation scanned by 'best_path'
1345  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1346  */
1347 static FunctionScan *
1348 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1349                                                  List *tlist, List *scan_clauses)
1350 {
1351         FunctionScan *scan_plan;
1352         Index           scan_relid = best_path->parent->relid;
1353         RangeTblEntry *rte;
1354
1355         /* it should be a function base rel... */
1356         Assert(scan_relid > 0);
1357         rte = planner_rt_fetch(scan_relid, root);
1358         Assert(rte->rtekind == RTE_FUNCTION);
1359
1360         /* Sort clauses into best execution order */
1361         scan_clauses = order_qual_clauses(root, scan_clauses);
1362
1363         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1364         scan_clauses = extract_actual_clauses(scan_clauses, false);
1365
1366         scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1367                                                                   rte->funcexpr,
1368                                                                   rte->eref->colnames,
1369                                                                   rte->funccoltypes,
1370                                                                   rte->funccoltypmods);
1371
1372         copy_path_costsize(&scan_plan->scan.plan, best_path);
1373
1374         return scan_plan;
1375 }
1376
1377 /*
1378  * create_valuesscan_plan
1379  *       Returns a valuesscan plan for the base relation scanned by 'best_path'
1380  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1381  */
1382 static ValuesScan *
1383 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1384                                            List *tlist, List *scan_clauses)
1385 {
1386         ValuesScan *scan_plan;
1387         Index           scan_relid = best_path->parent->relid;
1388         RangeTblEntry *rte;
1389
1390         /* it should be a values base rel... */
1391         Assert(scan_relid > 0);
1392         rte = planner_rt_fetch(scan_relid, root);
1393         Assert(rte->rtekind == RTE_VALUES);
1394
1395         /* Sort clauses into best execution order */
1396         scan_clauses = order_qual_clauses(root, scan_clauses);
1397
1398         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1399         scan_clauses = extract_actual_clauses(scan_clauses, false);
1400
1401         scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1402                                                                 rte->values_lists);
1403
1404         copy_path_costsize(&scan_plan->scan.plan, best_path);
1405
1406         return scan_plan;
1407 }
1408
1409 /*****************************************************************************
1410  *
1411  *      JOIN METHODS
1412  *
1413  *****************************************************************************/
1414
1415 static NestLoop *
1416 create_nestloop_plan(PlannerInfo *root,
1417                                          NestPath *best_path,
1418                                          Plan *outer_plan,
1419                                          Plan *inner_plan)
1420 {
1421         List       *tlist = build_relation_tlist(best_path->path.parent);
1422         List       *joinrestrictclauses = best_path->joinrestrictinfo;
1423         List       *joinclauses;
1424         List       *otherclauses;
1425         NestLoop   *join_plan;
1426
1427         if (IsA(best_path->innerjoinpath, IndexPath))
1428         {
1429                 /*
1430                  * An index is being used to reduce the number of tuples scanned in
1431                  * the inner relation.  If there are join clauses being used with the
1432                  * index, we may remove those join clauses from the list of clauses
1433                  * that have to be checked as qpquals at the join node.
1434                  *
1435                  * We can also remove any join clauses that are redundant with those
1436                  * being used in the index scan; this check is needed because
1437                  * find_eclass_clauses_for_index_join() may emit different clauses
1438                  * than generate_join_implied_equalities() did.
1439                  *
1440                  * We can skip this if the index path is an ordinary indexpath and not
1441                  * a special innerjoin path, since it then wouldn't be using any join
1442                  * clauses.
1443                  */
1444                 IndexPath  *innerpath = (IndexPath *) best_path->innerjoinpath;
1445
1446                 if (innerpath->isjoininner)
1447                         joinrestrictclauses =
1448                                 select_nonredundant_join_clauses(root,
1449                                                                                                  joinrestrictclauses,
1450                                                                                                  innerpath->indexclauses);
1451         }
1452         else if (IsA(best_path->innerjoinpath, BitmapHeapPath))
1453         {
1454                 /*
1455                  * Same deal for bitmapped index scans.
1456                  *
1457                  * Note: both here and above, we ignore any implicit index
1458                  * restrictions associated with the use of partial indexes.  This is
1459                  * OK because we're only trying to prove we can dispense with some
1460                  * join quals; failing to prove that doesn't result in an incorrect
1461                  * plan.  It is the right way to proceed because adding more quals to
1462                  * the stuff we got from the original query would just make it harder
1463                  * to detect duplication.  (Also, to change this we'd have to be wary
1464                  * of UPDATE/DELETE/SELECT FOR UPDATE target relations; see notes
1465                  * above about EvalPlanQual.)
1466                  */
1467                 BitmapHeapPath *innerpath = (BitmapHeapPath *) best_path->innerjoinpath;
1468
1469                 if (innerpath->isjoininner)
1470                 {
1471                         List       *bitmapclauses;
1472
1473                         bitmapclauses =
1474                                 make_restrictinfo_from_bitmapqual(innerpath->bitmapqual,
1475                                                                                                   true,
1476                                                                                                   false);
1477                         joinrestrictclauses =
1478                                 select_nonredundant_join_clauses(root,
1479                                                                                                  joinrestrictclauses,
1480                                                                                                  bitmapclauses);
1481                 }
1482         }
1483
1484         /* Sort join qual clauses into best execution order */
1485         joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1486
1487         /* Get the join qual clauses (in plain expression form) */
1488         /* Any pseudoconstant clauses are ignored here */
1489         if (IS_OUTER_JOIN(best_path->jointype))
1490         {
1491                 extract_actual_join_clauses(joinrestrictclauses,
1492                                                                         &joinclauses, &otherclauses);
1493         }
1494         else
1495         {
1496                 /* We can treat all clauses alike for an inner join */
1497                 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1498                 otherclauses = NIL;
1499         }
1500
1501         join_plan = make_nestloop(tlist,
1502                                                           joinclauses,
1503                                                           otherclauses,
1504                                                           outer_plan,
1505                                                           inner_plan,
1506                                                           best_path->jointype);
1507
1508         copy_path_costsize(&join_plan->join.plan, &best_path->path);
1509
1510         return join_plan;
1511 }
1512
1513 static MergeJoin *
1514 create_mergejoin_plan(PlannerInfo *root,
1515                                           MergePath *best_path,
1516                                           Plan *outer_plan,
1517                                           Plan *inner_plan)
1518 {
1519         List       *tlist = build_relation_tlist(best_path->jpath.path.parent);
1520         List       *joinclauses;
1521         List       *otherclauses;
1522         List       *mergeclauses;
1523         List       *outerpathkeys;
1524         List       *innerpathkeys;
1525         int                     nClauses;
1526         Oid                *mergefamilies;
1527         int                *mergestrategies;
1528         bool       *mergenullsfirst;
1529         MergeJoin  *join_plan;
1530         int                     i;
1531         EquivalenceClass *lastoeclass;
1532         EquivalenceClass *lastieclass;
1533         PathKey    *opathkey;
1534         PathKey    *ipathkey;
1535         ListCell   *lc;
1536         ListCell   *lop;
1537         ListCell   *lip;
1538
1539         /* Sort join qual clauses into best execution order */
1540         /* NB: do NOT reorder the mergeclauses */
1541         joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1542
1543         /* Get the join qual clauses (in plain expression form) */
1544         /* Any pseudoconstant clauses are ignored here */
1545         if (IS_OUTER_JOIN(best_path->jpath.jointype))
1546         {
1547                 extract_actual_join_clauses(joinclauses,
1548                                                                         &joinclauses, &otherclauses);
1549         }
1550         else
1551         {
1552                 /* We can treat all clauses alike for an inner join */
1553                 joinclauses = extract_actual_clauses(joinclauses, false);
1554                 otherclauses = NIL;
1555         }
1556
1557         /*
1558          * Remove the mergeclauses from the list of join qual clauses, leaving the
1559          * list of quals that must be checked as qpquals.
1560          */
1561         mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1562         joinclauses = list_difference(joinclauses, mergeclauses);
1563
1564         /*
1565          * Rearrange mergeclauses, if needed, so that the outer variable is always
1566          * on the left; mark the mergeclause restrictinfos with correct
1567          * outer_is_left status.
1568          */
1569         mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1570                                                          best_path->jpath.outerjoinpath->parent->relids);
1571
1572         /*
1573          * Create explicit sort nodes for the outer and inner join paths if
1574          * necessary.  The sort cost was already accounted for in the path. Make
1575          * sure there are no excess columns in the inputs if sorting.
1576          */
1577         if (best_path->outersortkeys)
1578         {
1579                 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1580                 outer_plan = (Plan *)
1581                         make_sort_from_pathkeys(root,
1582                                                                         outer_plan,
1583                                                                         best_path->outersortkeys,
1584                                                                         -1.0);
1585                 outerpathkeys = best_path->outersortkeys;
1586         }
1587         else
1588                 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
1589
1590         if (best_path->innersortkeys)
1591         {
1592                 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1593                 inner_plan = (Plan *)
1594                         make_sort_from_pathkeys(root,
1595                                                                         inner_plan,
1596                                                                         best_path->innersortkeys,
1597                                                                         -1.0);
1598                 innerpathkeys = best_path->innersortkeys;
1599         }
1600         else
1601                 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
1602
1603         /*
1604          * Compute the opfamily/strategy/nullsfirst arrays needed by the executor.
1605          * The information is in the pathkeys for the two inputs, but we need to
1606          * be careful about the possibility of mergeclauses sharing a pathkey
1607          * (compare find_mergeclauses_for_pathkeys()).
1608          */
1609         nClauses = list_length(mergeclauses);
1610         Assert(nClauses == list_length(best_path->path_mergeclauses));
1611         mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
1612         mergestrategies = (int *) palloc(nClauses * sizeof(int));
1613         mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
1614
1615         lastoeclass = NULL;
1616         lastieclass = NULL;
1617         opathkey = NULL;
1618         ipathkey = NULL;
1619         lop = list_head(outerpathkeys);
1620         lip = list_head(innerpathkeys);
1621         i = 0;
1622         foreach(lc, best_path->path_mergeclauses)
1623         {
1624                 RestrictInfo   *rinfo = (RestrictInfo *) lfirst(lc);
1625                 EquivalenceClass *oeclass;
1626                 EquivalenceClass *ieclass;
1627
1628                 /* fetch outer/inner eclass from mergeclause */
1629                 Assert(IsA(rinfo, RestrictInfo));
1630                 if (rinfo->outer_is_left)
1631                 {
1632                         oeclass = rinfo->left_ec;
1633                         ieclass = rinfo->right_ec;
1634                 }
1635                 else
1636                 {
1637                         oeclass = rinfo->right_ec;
1638                         ieclass = rinfo->left_ec;
1639                 }
1640                 Assert(oeclass != NULL);
1641                 Assert(ieclass != NULL);
1642
1643                 /* should match current or next pathkeys */
1644                 /* we check this carefully for debugging reasons */
1645                 if (oeclass != lastoeclass)
1646                 {
1647                         if (!lop)
1648                                 elog(ERROR, "too few pathkeys for mergeclauses");
1649                         opathkey = (PathKey *) lfirst(lop);
1650                         lop = lnext(lop);
1651                         lastoeclass = opathkey->pk_eclass;
1652                         if (oeclass != lastoeclass)
1653                                 elog(ERROR, "outer pathkeys do not match mergeclause");
1654                 }
1655                 if (ieclass != lastieclass)
1656                 {
1657                         if (!lip)
1658                                 elog(ERROR, "too few pathkeys for mergeclauses");
1659                         ipathkey = (PathKey *) lfirst(lip);
1660                         lip = lnext(lip);
1661                         lastieclass = ipathkey->pk_eclass;
1662                         if (ieclass != lastieclass)
1663                                 elog(ERROR, "inner pathkeys do not match mergeclause");
1664                 }
1665                 /* pathkeys should match each other too (more debugging) */
1666                 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
1667                         opathkey->pk_strategy != ipathkey->pk_strategy ||
1668                         opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
1669                         elog(ERROR, "left and right pathkeys do not match in mergejoin");
1670
1671                 /* OK, save info for executor */
1672                 mergefamilies[i] = opathkey->pk_opfamily;
1673                 mergestrategies[i] = opathkey->pk_strategy;
1674                 mergenullsfirst[i] = opathkey->pk_nulls_first;
1675                 i++;
1676         }
1677
1678
1679         /*
1680          * Now we can build the mergejoin node.
1681          */
1682         join_plan = make_mergejoin(tlist,
1683                                                            joinclauses,
1684                                                            otherclauses,
1685                                                            mergeclauses,
1686                                                            mergefamilies,
1687                                                            mergestrategies,
1688                                                            mergenullsfirst,
1689                                                            outer_plan,
1690                                                            inner_plan,
1691                                                            best_path->jpath.jointype);
1692
1693         copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1694
1695         return join_plan;
1696 }
1697
1698 static HashJoin *
1699 create_hashjoin_plan(PlannerInfo *root,
1700                                          HashPath *best_path,
1701                                          Plan *outer_plan,
1702                                          Plan *inner_plan)
1703 {
1704         List       *tlist = build_relation_tlist(best_path->jpath.path.parent);
1705         List       *joinclauses;
1706         List       *otherclauses;
1707         List       *hashclauses;
1708         HashJoin   *join_plan;
1709         Hash       *hash_plan;
1710
1711         /* Sort join qual clauses into best execution order */
1712         joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1713         /* There's no point in sorting the hash clauses ... */
1714
1715         /* Get the join qual clauses (in plain expression form) */
1716         /* Any pseudoconstant clauses are ignored here */
1717         if (IS_OUTER_JOIN(best_path->jpath.jointype))
1718         {
1719                 extract_actual_join_clauses(joinclauses,
1720                                                                         &joinclauses, &otherclauses);
1721         }
1722         else
1723         {
1724                 /* We can treat all clauses alike for an inner join */
1725                 joinclauses = extract_actual_clauses(joinclauses, false);
1726                 otherclauses = NIL;
1727         }
1728
1729         /*
1730          * Remove the hashclauses from the list of join qual clauses, leaving the
1731          * list of quals that must be checked as qpquals.
1732          */
1733         hashclauses = get_actual_clauses(best_path->path_hashclauses);
1734         joinclauses = list_difference(joinclauses, hashclauses);
1735
1736         /*
1737          * Rearrange hashclauses, if needed, so that the outer variable is always
1738          * on the left.
1739          */
1740         hashclauses = get_switched_clauses(best_path->path_hashclauses,
1741                                                          best_path->jpath.outerjoinpath->parent->relids);
1742
1743         /* We don't want any excess columns in the hashed tuples */
1744         disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1745
1746         /*
1747          * Build the hash node and hash join node.
1748          */
1749         hash_plan = make_hash(inner_plan);
1750         join_plan = make_hashjoin(tlist,
1751                                                           joinclauses,
1752                                                           otherclauses,
1753                                                           hashclauses,
1754                                                           outer_plan,
1755                                                           (Plan *) hash_plan,
1756                                                           best_path->jpath.jointype);
1757
1758         copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1759
1760         return join_plan;
1761 }
1762
1763
1764 /*****************************************************************************
1765  *
1766  *      SUPPORTING ROUTINES
1767  *
1768  *****************************************************************************/
1769
1770 /*
1771  * fix_indexqual_references
1772  *        Adjust indexqual clauses to the form the executor's indexqual
1773  *        machinery needs, and check for recheckable (lossy) index conditions.
1774  *
1775  * We have five tasks here:
1776  *      * Remove RestrictInfo nodes from the input clauses.
1777  *      * Index keys must be represented by Var nodes with varattno set to the
1778  *        index's attribute number, not the attribute number in the original rel.
1779  *      * If the index key is on the right, commute the clause to put it on the
1780  *        left.
1781  *      * We must construct lists of operator strategy numbers and subtypes
1782  *        for the top-level operators of each index clause.
1783  *      * We must detect any lossy index operators.  The API is that we return
1784  *        a list of the input clauses whose operators are NOT lossy.
1785  *
1786  * fixed_indexquals receives a modified copy of the indexquals list --- the
1787  * original is not changed.  Note also that the copy shares no substructure
1788  * with the original; this is needed in case there is a subplan in it (we need
1789  * two separate copies of the subplan tree, or things will go awry).
1790  *
1791  * nonlossy_indexquals receives a list of the original input clauses (with
1792  * RestrictInfos) that contain non-lossy operators.
1793  *
1794  * indexstrategy receives an integer list of strategy numbers.
1795  * indexsubtype receives an OID list of strategy subtypes.
1796  */
1797 static void
1798 fix_indexqual_references(List *indexquals, IndexPath *index_path,
1799                                                  List **fixed_indexquals,
1800                                                  List **nonlossy_indexquals,
1801                                                  List **indexstrategy,
1802                                                  List **indexsubtype)
1803 {
1804         IndexOptInfo *index = index_path->indexinfo;
1805         ListCell   *l;
1806
1807         *fixed_indexquals = NIL;
1808         *nonlossy_indexquals = NIL;
1809         *indexstrategy = NIL;
1810         *indexsubtype = NIL;
1811
1812         /*
1813          * For each qual clause, commute if needed to put the indexkey operand on
1814          * the left, and then fix its varattno.  (We do not need to change the
1815          * other side of the clause.)  Then determine the operator's strategy
1816          * number and subtype number, and check for lossy index behavior.
1817          */
1818         foreach(l, indexquals)
1819         {
1820                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1821                 Expr       *clause;
1822                 Oid                     clause_op;
1823                 Oid                     opfamily;
1824                 int                     stratno;
1825                 Oid                     stratlefttype;
1826                 Oid                     stratrighttype;
1827                 bool            recheck;
1828                 bool            is_null_op = false;
1829
1830                 Assert(IsA(rinfo, RestrictInfo));
1831
1832                 /*
1833                  * Make a copy that will become the fixed clause.
1834                  *
1835                  * We used to try to do a shallow copy here, but that fails if there
1836                  * is a subplan in the arguments of the opclause.  So just do a full
1837                  * copy.
1838                  */
1839                 clause = (Expr *) copyObject((Node *) rinfo->clause);
1840
1841                 if (IsA(clause, OpExpr))
1842                 {
1843                         OpExpr     *op = (OpExpr *) clause;
1844
1845                         if (list_length(op->args) != 2)
1846                                 elog(ERROR, "indexqual clause is not binary opclause");
1847
1848                         /*
1849                          * Check to see if the indexkey is on the right; if so, commute
1850                          * the clause. The indexkey should be the side that refers to
1851                          * (only) the base relation.
1852                          */
1853                         if (!bms_equal(rinfo->left_relids, index->rel->relids))
1854                                 CommuteOpExpr(op);
1855
1856                         /*
1857                          * Now, determine which index attribute this is, change the
1858                          * indexkey operand as needed, and get the index opfamily.
1859                          */
1860                         linitial(op->args) = fix_indexqual_operand(linitial(op->args),
1861                                                                                                            index,
1862                                                                                                            &opfamily);
1863                         clause_op = op->opno;
1864                 }
1865                 else if (IsA(clause, RowCompareExpr))
1866                 {
1867                         RowCompareExpr *rc = (RowCompareExpr *) clause;
1868                         ListCell   *lc;
1869
1870                         /*
1871                          * Check to see if the indexkey is on the right; if so, commute
1872                          * the clause. The indexkey should be the side that refers to
1873                          * (only) the base relation.
1874                          */
1875                         if (!bms_overlap(pull_varnos(linitial(rc->largs)),
1876                                                          index->rel->relids))
1877                                 CommuteRowCompareExpr(rc);
1878
1879                         /*
1880                          * For each column in the row comparison, determine which index
1881                          * attribute this is and change the indexkey operand as needed.
1882                          *
1883                          * Save the index opfamily for only the first column.  We will
1884                          * return the operator and opfamily info for just the first column
1885                          * of the row comparison; the executor will have to look up the
1886                          * rest if it needs them.
1887                          */
1888                         foreach(lc, rc->largs)
1889                         {
1890                                 Oid                     tmp_opfamily;
1891
1892                                 lfirst(lc) = fix_indexqual_operand(lfirst(lc),
1893                                                                                                    index,
1894                                                                                                    &tmp_opfamily);
1895                                 if (lc == list_head(rc->largs))
1896                                         opfamily = tmp_opfamily;
1897                         }
1898                         clause_op = linitial_oid(rc->opnos);
1899                 }
1900                 else if (IsA(clause, ScalarArrayOpExpr))
1901                 {
1902                         ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1903
1904                         /* Never need to commute... */
1905
1906                         /*
1907                          * Now, determine which index attribute this is, change the
1908                          * indexkey operand as needed, and get the index opfamily.
1909                          */
1910                         linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
1911                                                                                                                  index,
1912                                                                                                                  &opfamily);
1913                         clause_op = saop->opno;
1914                 }
1915                 else if (IsA(clause, NullTest))
1916                 {
1917                         NullTest *nt = (NullTest *) clause;
1918
1919                         Assert(nt->nulltesttype == IS_NULL);
1920                         nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
1921                                                                                                          index,
1922                                                                                                          &opfamily);
1923                         is_null_op = true;
1924                         clause_op = InvalidOid;         /* keep compiler quiet */
1925                 }
1926                 else
1927                 {
1928                         elog(ERROR, "unsupported indexqual type: %d",
1929                                  (int) nodeTag(clause));
1930                         continue;                       /* keep compiler quiet */
1931                 }
1932
1933                 *fixed_indexquals = lappend(*fixed_indexquals, clause);
1934
1935                 if (is_null_op)
1936                 {
1937                         /* IS NULL doesn't have a clause_op */
1938                         stratno = InvalidStrategy;
1939                         stratrighttype = InvalidOid;
1940                         /* We assume it's non-lossy ... might need more work someday */
1941                         recheck = false;
1942                 }
1943                 else
1944                 {
1945                         /*
1946                          * Look up the (possibly commuted) operator in the operator family
1947                          * to get its strategy number and the recheck indicator. This also
1948                          * double-checks that we found an operator matching the index.
1949                          */
1950                         get_op_opfamily_properties(clause_op, opfamily,
1951                                                                            &stratno,
1952                                                                            &stratlefttype,
1953                                                                            &stratrighttype,
1954                                                                            &recheck);
1955                 }
1956
1957                 *indexstrategy = lappend_int(*indexstrategy, stratno);
1958                 *indexsubtype = lappend_oid(*indexsubtype, stratrighttype);
1959
1960                 /* If it's not lossy, add to nonlossy_indexquals */
1961                 if (!recheck)
1962                         *nonlossy_indexquals = lappend(*nonlossy_indexquals, rinfo);
1963         }
1964 }
1965
1966 static Node *
1967 fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opfamily)
1968 {
1969         /*
1970          * We represent index keys by Var nodes having the varno of the base table
1971          * but varattno equal to the index's attribute number (index column
1972          * position).  This is a bit hokey ... would be cleaner to use a
1973          * special-purpose node type that could not be mistaken for a regular Var.
1974          * But it will do for now.
1975          */
1976         Var                *result;
1977         int                     pos;
1978         ListCell   *indexpr_item;
1979
1980         /*
1981          * Remove any binary-compatible relabeling of the indexkey
1982          */
1983         if (IsA(node, RelabelType))
1984                 node = (Node *) ((RelabelType *) node)->arg;
1985
1986         if (IsA(node, Var) &&
1987                 ((Var *) node)->varno == index->rel->relid)
1988         {
1989                 /* Try to match against simple index columns */
1990                 int                     varatt = ((Var *) node)->varattno;
1991
1992                 if (varatt != 0)
1993                 {
1994                         for (pos = 0; pos < index->ncolumns; pos++)
1995                         {
1996                                 if (index->indexkeys[pos] == varatt)
1997                                 {
1998                                         result = (Var *) copyObject(node);
1999                                         result->varattno = pos + 1;
2000                                         /* return the correct opfamily, too */
2001                                         *opfamily = index->opfamily[pos];
2002                                         return (Node *) result;
2003                                 }
2004                         }
2005                 }
2006         }
2007
2008         /* Try to match against index expressions */
2009         indexpr_item = list_head(index->indexprs);
2010         for (pos = 0; pos < index->ncolumns; pos++)
2011         {
2012                 if (index->indexkeys[pos] == 0)
2013                 {
2014                         Node       *indexkey;
2015
2016                         if (indexpr_item == NULL)
2017                                 elog(ERROR, "too few entries in indexprs list");
2018                         indexkey = (Node *) lfirst(indexpr_item);
2019                         if (indexkey && IsA(indexkey, RelabelType))
2020                                 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2021                         if (equal(node, indexkey))
2022                         {
2023                                 /* Found a match */
2024                                 result = makeVar(index->rel->relid, pos + 1,
2025                                                                  exprType(lfirst(indexpr_item)), -1,
2026                                                                  0);
2027                                 /* return the correct opfamily, too */
2028                                 *opfamily = index->opfamily[pos];
2029                                 return (Node *) result;
2030                         }
2031                         indexpr_item = lnext(indexpr_item);
2032                 }
2033         }
2034
2035         /* Ooops... */
2036         elog(ERROR, "node is not an index attribute");
2037         *opfamily = InvalidOid;         /* keep compiler quiet */
2038         return NULL;
2039 }
2040
2041 /*
2042  * get_switched_clauses
2043  *        Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2044  *        extract the bare clauses, and rearrange the elements within the
2045  *        clauses, if needed, so the outer join variable is on the left and
2046  *        the inner is on the right.  The original clause data structure is not
2047  *        touched; a modified list is returned.  We do, however, set the transient
2048  *        outer_is_left field in each RestrictInfo to show which side was which.
2049  */
2050 static List *
2051 get_switched_clauses(List *clauses, Relids outerrelids)
2052 {
2053         List       *t_list = NIL;
2054         ListCell   *l;
2055
2056         foreach(l, clauses)
2057         {
2058                 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2059                 OpExpr     *clause = (OpExpr *) restrictinfo->clause;
2060
2061                 Assert(is_opclause(clause));
2062                 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2063                 {
2064                         /*
2065                          * Duplicate just enough of the structure to allow commuting the
2066                          * clause without changing the original list.  Could use
2067                          * copyObject, but a complete deep copy is overkill.
2068                          */
2069                         OpExpr     *temp = makeNode(OpExpr);
2070
2071                         temp->opno = clause->opno;
2072                         temp->opfuncid = InvalidOid;
2073                         temp->opresulttype = clause->opresulttype;
2074                         temp->opretset = clause->opretset;
2075                         temp->args = list_copy(clause->args);
2076                         /* Commute it --- note this modifies the temp node in-place. */
2077                         CommuteOpExpr(temp);
2078                         t_list = lappend(t_list, temp);
2079                         restrictinfo->outer_is_left = false;
2080                 }
2081                 else
2082                 {
2083                         Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2084                         t_list = lappend(t_list, clause);
2085                         restrictinfo->outer_is_left = true;
2086                 }
2087         }
2088         return t_list;
2089 }
2090
2091 /*
2092  * order_qual_clauses
2093  *              Given a list of qual clauses that will all be evaluated at the same
2094  *              plan node, sort the list into the order we want to check the quals
2095  *              in at runtime.
2096  *
2097  * Ideally the order should be driven by a combination of execution cost and
2098  * selectivity, but it's not immediately clear how to account for both,
2099  * and given the uncertainty of the estimates the reliability of the decisions
2100  * would be doubtful anyway.  So we just order by estimated per-tuple cost,
2101  * being careful not to change the order when (as is often the case) the
2102  * estimates are identical.
2103  *
2104  * Although this will work on either bare clauses or RestrictInfos, it's
2105  * much faster to apply it to RestrictInfos, since it can re-use cost
2106  * information that is cached in RestrictInfos.
2107  *
2108  * Note: some callers pass lists that contain entries that will later be
2109  * removed; this is the easiest way to let this routine see RestrictInfos
2110  * instead of bare clauses.  It's OK because we only sort by cost, but
2111  * a cost/selectivity combination would likely do the wrong thing.
2112  */
2113 static List *
2114 order_qual_clauses(PlannerInfo *root, List *clauses)
2115 {
2116         typedef struct
2117         {
2118                 Node   *clause;
2119                 Cost    cost;
2120         } QualItem;
2121         int                     nitems = list_length(clauses);
2122         QualItem   *items;
2123         ListCell   *lc;
2124         int                     i;
2125         List       *result;
2126
2127         /* No need to work hard for 0 or 1 clause */
2128         if (nitems <= 1)
2129                 return clauses;
2130
2131         /*
2132          * Collect the items and costs into an array.  This is to avoid repeated
2133          * cost_qual_eval work if the inputs aren't RestrictInfos.
2134          */
2135         items = (QualItem *) palloc(nitems * sizeof(QualItem));
2136         i = 0;
2137         foreach(lc, clauses)
2138         {
2139                 Node       *clause = (Node *) lfirst(lc);
2140                 QualCost        qcost;
2141
2142                 cost_qual_eval_node(&qcost, clause, root);
2143                 items[i].clause = clause;
2144                 items[i].cost = qcost.per_tuple;
2145                 i++;
2146         }
2147
2148         /*
2149          * Sort.  We don't use qsort() because it's not guaranteed stable for
2150          * equal keys.  The expected number of entries is small enough that
2151          * a simple insertion sort should be good enough.
2152          */
2153         for (i = 1; i < nitems; i++)
2154         {
2155                 QualItem        newitem = items[i];
2156                 int                     j;
2157
2158                 /* insert newitem into the already-sorted subarray */
2159                 for (j = i; j > 0; j--)
2160                 {
2161                         if (newitem.cost >= items[j-1].cost)
2162                                 break;
2163                         items[j] = items[j-1];
2164                 }
2165                 items[j] = newitem;
2166         }
2167
2168         /* Convert back to a list */
2169         result = NIL;
2170         for (i = 0; i < nitems; i++)
2171                 result = lappend(result, items[i].clause);
2172
2173         return result;
2174 }
2175
2176 /*
2177  * Copy cost and size info from a Path node to the Plan node created from it.
2178  * The executor won't use this info, but it's needed by EXPLAIN.
2179  */
2180 static void
2181 copy_path_costsize(Plan *dest, Path *src)
2182 {
2183         if (src)
2184         {
2185                 dest->startup_cost = src->startup_cost;
2186                 dest->total_cost = src->total_cost;
2187                 dest->plan_rows = src->parent->rows;
2188                 dest->plan_width = src->parent->width;
2189         }
2190         else
2191         {
2192                 dest->startup_cost = 0;
2193                 dest->total_cost = 0;
2194                 dest->plan_rows = 0;
2195                 dest->plan_width = 0;
2196         }
2197 }
2198
2199 /*
2200  * Copy cost and size info from a lower plan node to an inserted node.
2201  * This is not critical, since the decisions have already been made,
2202  * but it helps produce more reasonable-looking EXPLAIN output.
2203  * (Some callers alter the info after copying it.)
2204  */
2205 static void
2206 copy_plan_costsize(Plan *dest, Plan *src)
2207 {
2208         if (src)
2209         {
2210                 dest->startup_cost = src->startup_cost;
2211                 dest->total_cost = src->total_cost;
2212                 dest->plan_rows = src->plan_rows;
2213                 dest->plan_width = src->plan_width;
2214         }
2215         else
2216         {
2217                 dest->startup_cost = 0;
2218                 dest->total_cost = 0;
2219                 dest->plan_rows = 0;
2220                 dest->plan_width = 0;
2221         }
2222 }
2223
2224
2225 /*****************************************************************************
2226  *
2227  *      PLAN NODE BUILDING ROUTINES
2228  *
2229  * Some of these are exported because they are called to build plan nodes
2230  * in contexts where we're not deriving the plan node from a path node.
2231  *
2232  *****************************************************************************/
2233
2234 static SeqScan *
2235 make_seqscan(List *qptlist,
2236                          List *qpqual,
2237                          Index scanrelid)
2238 {
2239         SeqScan    *node = makeNode(SeqScan);
2240         Plan       *plan = &node->plan;
2241
2242         /* cost should be inserted by caller */
2243         plan->targetlist = qptlist;
2244         plan->qual = qpqual;
2245         plan->lefttree = NULL;
2246         plan->righttree = NULL;
2247         node->scanrelid = scanrelid;
2248
2249         return node;
2250 }
2251
2252 static IndexScan *
2253 make_indexscan(List *qptlist,
2254                            List *qpqual,
2255                            Index scanrelid,
2256                            Oid indexid,
2257                            List *indexqual,
2258                            List *indexqualorig,
2259                            List *indexstrategy,
2260                            List *indexsubtype,
2261                            ScanDirection indexscandir)
2262 {
2263         IndexScan  *node = makeNode(IndexScan);
2264         Plan       *plan = &node->scan.plan;
2265
2266         /* cost should be inserted by caller */
2267         plan->targetlist = qptlist;
2268         plan->qual = qpqual;
2269         plan->lefttree = NULL;
2270         plan->righttree = NULL;
2271         node->scan.scanrelid = scanrelid;
2272         node->indexid = indexid;
2273         node->indexqual = indexqual;
2274         node->indexqualorig = indexqualorig;
2275         node->indexstrategy = indexstrategy;
2276         node->indexsubtype = indexsubtype;
2277         node->indexorderdir = indexscandir;
2278
2279         return node;
2280 }
2281
2282 static BitmapIndexScan *
2283 make_bitmap_indexscan(Index scanrelid,
2284                                           Oid indexid,
2285                                           List *indexqual,
2286                                           List *indexqualorig,
2287                                           List *indexstrategy,
2288                                           List *indexsubtype)
2289 {
2290         BitmapIndexScan *node = makeNode(BitmapIndexScan);
2291         Plan       *plan = &node->scan.plan;
2292
2293         /* cost should be inserted by caller */
2294         plan->targetlist = NIL;         /* not used */
2295         plan->qual = NIL;                       /* not used */
2296         plan->lefttree = NULL;
2297         plan->righttree = NULL;
2298         node->scan.scanrelid = scanrelid;
2299         node->indexid = indexid;
2300         node->indexqual = indexqual;
2301         node->indexqualorig = indexqualorig;
2302         node->indexstrategy = indexstrategy;
2303         node->indexsubtype = indexsubtype;
2304
2305         return node;
2306 }
2307
2308 static BitmapHeapScan *
2309 make_bitmap_heapscan(List *qptlist,
2310                                          List *qpqual,
2311                                          Plan *lefttree,
2312                                          List *bitmapqualorig,
2313                                          Index scanrelid)
2314 {
2315         BitmapHeapScan *node = makeNode(BitmapHeapScan);
2316         Plan       *plan = &node->scan.plan;
2317
2318         /* cost should be inserted by caller */
2319         plan->targetlist = qptlist;
2320         plan->qual = qpqual;
2321         plan->lefttree = lefttree;
2322         plan->righttree = NULL;
2323         node->scan.scanrelid = scanrelid;
2324         node->bitmapqualorig = bitmapqualorig;
2325
2326         return node;
2327 }
2328
2329 static TidScan *
2330 make_tidscan(List *qptlist,
2331                          List *qpqual,
2332                          Index scanrelid,
2333                          List *tidquals)
2334 {
2335         TidScan    *node = makeNode(TidScan);
2336         Plan       *plan = &node->scan.plan;
2337
2338         /* cost should be inserted by caller */
2339         plan->targetlist = qptlist;
2340         plan->qual = qpqual;
2341         plan->lefttree = NULL;
2342         plan->righttree = NULL;
2343         node->scan.scanrelid = scanrelid;
2344         node->tidquals = tidquals;
2345
2346         return node;
2347 }
2348
2349 SubqueryScan *
2350 make_subqueryscan(List *qptlist,
2351                                   List *qpqual,
2352                                   Index scanrelid,
2353                                   Plan *subplan,
2354                                   List *subrtable)
2355 {
2356         SubqueryScan *node = makeNode(SubqueryScan);
2357         Plan       *plan = &node->scan.plan;
2358
2359         /*
2360          * Cost is figured here for the convenience of prepunion.c.  Note this is
2361          * only correct for the case where qpqual is empty; otherwise caller
2362          * should overwrite cost with a better estimate.
2363          */
2364         copy_plan_costsize(plan, subplan);
2365         plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
2366
2367         plan->targetlist = qptlist;
2368         plan->qual = qpqual;
2369         plan->lefttree = NULL;
2370         plan->righttree = NULL;
2371         node->scan.scanrelid = scanrelid;
2372         node->subplan = subplan;
2373         node->subrtable = subrtable;
2374
2375         return node;
2376 }
2377
2378 static FunctionScan *
2379 make_functionscan(List *qptlist,
2380                                   List *qpqual,
2381                                   Index scanrelid,
2382                                   Node *funcexpr,
2383                                   List *funccolnames,
2384                                   List *funccoltypes,
2385                                   List *funccoltypmods)
2386 {
2387         FunctionScan *node = makeNode(FunctionScan);
2388         Plan       *plan = &node->scan.plan;
2389
2390         /* cost should be inserted by caller */
2391         plan->targetlist = qptlist;
2392         plan->qual = qpqual;
2393         plan->lefttree = NULL;
2394         plan->righttree = NULL;
2395         node->scan.scanrelid = scanrelid;
2396         node->funcexpr = funcexpr;
2397         node->funccolnames = funccolnames;
2398         node->funccoltypes = funccoltypes;
2399         node->funccoltypmods = funccoltypmods;
2400
2401         return node;
2402 }
2403
2404 static ValuesScan *
2405 make_valuesscan(List *qptlist,
2406                                 List *qpqual,
2407                                 Index scanrelid,
2408                                 List *values_lists)
2409 {
2410         ValuesScan *node = makeNode(ValuesScan);
2411         Plan       *plan = &node->scan.plan;
2412
2413         /* cost should be inserted by caller */
2414         plan->targetlist = qptlist;
2415         plan->qual = qpqual;
2416         plan->lefttree = NULL;
2417         plan->righttree = NULL;
2418         node->scan.scanrelid = scanrelid;
2419         node->values_lists = values_lists;
2420
2421         return node;
2422 }
2423
2424 Append *
2425 make_append(List *appendplans, bool isTarget, List *tlist)
2426 {
2427         Append     *node = makeNode(Append);
2428         Plan       *plan = &node->plan;
2429         ListCell   *subnode;
2430
2431         /*
2432          * Compute cost as sum of subplan costs.  We charge nothing extra for the
2433          * Append itself, which perhaps is too optimistic, but since it doesn't do
2434          * any selection or projection, it is a pretty cheap node.
2435          */
2436         plan->startup_cost = 0;
2437         plan->total_cost = 0;
2438         plan->plan_rows = 0;
2439         plan->plan_width = 0;
2440         foreach(subnode, appendplans)
2441         {
2442                 Plan       *subplan = (Plan *) lfirst(subnode);
2443
2444                 if (subnode == list_head(appendplans))  /* first node? */
2445                         plan->startup_cost = subplan->startup_cost;
2446                 plan->total_cost += subplan->total_cost;
2447                 plan->plan_rows += subplan->plan_rows;
2448                 if (plan->plan_width < subplan->plan_width)
2449                         plan->plan_width = subplan->plan_width;
2450         }
2451
2452         plan->targetlist = tlist;
2453         plan->qual = NIL;
2454         plan->lefttree = NULL;
2455         plan->righttree = NULL;
2456         node->appendplans = appendplans;
2457         node->isTarget = isTarget;
2458
2459         return node;
2460 }
2461
2462 static BitmapAnd *
2463 make_bitmap_and(List *bitmapplans)
2464 {
2465         BitmapAnd  *node = makeNode(BitmapAnd);
2466         Plan       *plan = &node->plan;
2467
2468         /* cost should be inserted by caller */
2469         plan->targetlist = NIL;
2470         plan->qual = NIL;
2471         plan->lefttree = NULL;
2472         plan->righttree = NULL;
2473         node->bitmapplans = bitmapplans;
2474
2475         return node;
2476 }
2477
2478 static BitmapOr *
2479 make_bitmap_or(List *bitmapplans)
2480 {
2481         BitmapOr   *node = makeNode(BitmapOr);
2482         Plan       *plan = &node->plan;
2483
2484         /* cost should be inserted by caller */
2485         plan->targetlist = NIL;
2486         plan->qual = NIL;
2487         plan->lefttree = NULL;
2488         plan->righttree = NULL;
2489         node->bitmapplans = bitmapplans;
2490
2491         return node;
2492 }
2493
2494 static NestLoop *
2495 make_nestloop(List *tlist,
2496                           List *joinclauses,
2497                           List *otherclauses,
2498                           Plan *lefttree,
2499                           Plan *righttree,
2500                           JoinType jointype)
2501 {
2502         NestLoop   *node = makeNode(NestLoop);
2503         Plan       *plan = &node->join.plan;
2504
2505         /* cost should be inserted by caller */
2506         plan->targetlist = tlist;
2507         plan->qual = otherclauses;
2508         plan->lefttree = lefttree;
2509         plan->righttree = righttree;
2510         node->join.jointype = jointype;
2511         node->join.joinqual = joinclauses;
2512
2513         return node;
2514 }
2515
2516 static HashJoin *
2517 make_hashjoin(List *tlist,
2518                           List *joinclauses,
2519                           List *otherclauses,
2520                           List *hashclauses,
2521                           Plan *lefttree,
2522                           Plan *righttree,
2523                           JoinType jointype)
2524 {
2525         HashJoin   *node = makeNode(HashJoin);
2526         Plan       *plan = &node->join.plan;
2527
2528         /* cost should be inserted by caller */
2529         plan->targetlist = tlist;
2530         plan->qual = otherclauses;
2531         plan->lefttree = lefttree;
2532         plan->righttree = righttree;
2533         node->hashclauses = hashclauses;
2534         node->join.jointype = jointype;
2535         node->join.joinqual = joinclauses;
2536
2537         return node;
2538 }
2539
2540 static Hash *
2541 make_hash(Plan *lefttree)
2542 {
2543         Hash       *node = makeNode(Hash);
2544         Plan       *plan = &node->plan;
2545
2546         copy_plan_costsize(plan, lefttree);
2547
2548         /*
2549          * For plausibility, make startup & total costs equal total cost of input
2550          * plan; this only affects EXPLAIN display not decisions.
2551          */
2552         plan->startup_cost = plan->total_cost;
2553         plan->targetlist = lefttree->targetlist;
2554         plan->qual = NIL;
2555         plan->lefttree = lefttree;
2556         plan->righttree = NULL;
2557
2558         return node;
2559 }
2560
2561 static MergeJoin *
2562 make_mergejoin(List *tlist,
2563                            List *joinclauses,
2564                            List *otherclauses,
2565                            List *mergeclauses,
2566                            Oid *mergefamilies,
2567                            int *mergestrategies,
2568                            bool *mergenullsfirst,
2569                            Plan *lefttree,
2570                            Plan *righttree,
2571                            JoinType jointype)
2572 {
2573         MergeJoin  *node = makeNode(MergeJoin);
2574         Plan       *plan = &node->join.plan;
2575
2576         /* cost should be inserted by caller */
2577         plan->targetlist = tlist;
2578         plan->qual = otherclauses;
2579         plan->lefttree = lefttree;
2580         plan->righttree = righttree;
2581         node->mergeclauses = mergeclauses;
2582         node->mergeFamilies = mergefamilies;
2583         node->mergeStrategies = mergestrategies;
2584         node->mergeNullsFirst = mergenullsfirst;
2585         node->join.jointype = jointype;
2586         node->join.joinqual = joinclauses;
2587
2588         return node;
2589 }
2590
2591 /*
2592  * make_sort --- basic routine to build a Sort plan node
2593  *
2594  * Caller must have built the sortColIdx, sortOperators, and nullsFirst
2595  * arrays already.  limit_tuples is as for cost_sort (in particular, pass
2596  * -1 if no limit)
2597  */
2598 static Sort *
2599 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
2600                   AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
2601                   double limit_tuples)
2602 {
2603         Sort       *node = makeNode(Sort);
2604         Plan       *plan = &node->plan;
2605         Path            sort_path;              /* dummy for result of cost_sort */
2606
2607         copy_plan_costsize(plan, lefttree); /* only care about copying size */
2608         cost_sort(&sort_path, root, NIL,
2609                           lefttree->total_cost,
2610                           lefttree->plan_rows,
2611                           lefttree->plan_width,
2612                           limit_tuples);
2613         plan->startup_cost = sort_path.startup_cost;
2614         plan->total_cost = sort_path.total_cost;
2615         plan->targetlist = lefttree->targetlist;
2616         plan->qual = NIL;
2617         plan->lefttree = lefttree;
2618         plan->righttree = NULL;
2619         node->numCols = numCols;
2620         node->sortColIdx = sortColIdx;
2621         node->sortOperators = sortOperators;
2622         node->nullsFirst = nullsFirst;
2623
2624         return node;
2625 }
2626
2627 /*
2628  * add_sort_column --- utility subroutine for building sort info arrays
2629  *
2630  * We need this routine because the same column might be selected more than
2631  * once as a sort key column; if so, the extra mentions are redundant.
2632  *
2633  * Caller is assumed to have allocated the arrays large enough for the
2634  * max possible number of columns.      Return value is the new column count.
2635  */
2636 static int
2637 add_sort_column(AttrNumber colIdx, Oid sortOp, bool nulls_first,
2638                                 int numCols, AttrNumber *sortColIdx,
2639                                 Oid *sortOperators, bool *nullsFirst)
2640 {
2641         int                     i;
2642
2643         for (i = 0; i < numCols; i++)
2644         {
2645                 /*
2646                  * Note: we check sortOp because it's conceivable that "ORDER BY
2647                  * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
2648                  * values that < considers equal.  We need not check nulls_first
2649                  * however because a lower-order column with the same sortop but
2650                  * opposite nulls direction is redundant.
2651                  */
2652                 if (sortColIdx[i] == colIdx &&
2653                         sortOperators[numCols] == sortOp)
2654                 {
2655                         /* Already sorting by this col, so extra sort key is useless */
2656                         return numCols;
2657                 }
2658         }
2659
2660         /* Add the column */
2661         sortColIdx[numCols] = colIdx;
2662         sortOperators[numCols] = sortOp;
2663         nullsFirst[numCols] = nulls_first;
2664         return numCols + 1;
2665 }
2666
2667 /*
2668  * make_sort_from_pathkeys
2669  *        Create sort plan to sort according to given pathkeys
2670  *
2671  *        'lefttree' is the node which yields input tuples
2672  *        'pathkeys' is the list of pathkeys by which the result is to be sorted
2673  *        'limit_tuples' is the bound on the number of output tuples;
2674  *                              -1 if no bound
2675  *
2676  * We must convert the pathkey information into arrays of sort key column
2677  * numbers and sort operator OIDs.
2678  *
2679  * If the pathkeys include expressions that aren't simple Vars, we will
2680  * usually need to add resjunk items to the input plan's targetlist to
2681  * compute these expressions (since the Sort node itself won't do it).
2682  * If the input plan type isn't one that can do projections, this means
2683  * adding a Result node just to do the projection.
2684  */
2685 Sort *
2686 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
2687                                                 double limit_tuples)
2688 {
2689         List       *tlist = lefttree->targetlist;
2690         ListCell   *i;
2691         int                     numsortkeys;
2692         AttrNumber *sortColIdx;
2693         Oid                *sortOperators;
2694         bool       *nullsFirst;
2695
2696         /*
2697          * We will need at most list_length(pathkeys) sort columns; possibly less
2698          */
2699         numsortkeys = list_length(pathkeys);
2700         sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2701         sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2702         nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2703
2704         numsortkeys = 0;
2705
2706         foreach(i, pathkeys)
2707         {
2708                 PathKey    *pathkey = (PathKey *) lfirst(i);
2709                 TargetEntry *tle = NULL;
2710                 Oid                     pk_datatype = InvalidOid;
2711                 Oid                     sortop;
2712                 ListCell   *j;
2713
2714                 /*
2715                  * We can sort by any non-constant expression listed in the pathkey's
2716                  * EquivalenceClass.  For now, we take the first one that corresponds
2717                  * to an available Var in the tlist. If there isn't any, use the first
2718                  * one that is an expression in the input's vars.  (The non-const
2719                  * restriction only matters if the EC is below_outer_join; but if it
2720                  * isn't, it won't contain consts anyway, else we'd have discarded
2721                  * the pathkey as redundant.)
2722                  *
2723                  * XXX if we have a choice, is there any way of figuring out which
2724                  * might be cheapest to execute?  (For example, int4lt is likely much
2725                  * cheaper to execute than numericlt, but both might appear in the
2726                  * same equivalence class...)  Not clear that we ever will have an
2727                  * interesting choice in practice, so it may not matter.
2728                  */
2729                 foreach(j, pathkey->pk_eclass->ec_members)
2730                 {
2731                         EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
2732
2733                         if (em->em_is_const || em->em_is_child)
2734                                 continue;
2735                         tle = tlist_member((Node *) em->em_expr, tlist);
2736                         if (tle)
2737                         {
2738                                 pk_datatype = em->em_datatype;
2739                                 break;                  /* found expr already in tlist */
2740                         }
2741                 }
2742                 if (!tle)
2743                 {
2744                         /* No matching Var; look for a computable expression */
2745                         Expr   *sortexpr = NULL;
2746
2747                         foreach(j, pathkey->pk_eclass->ec_members)
2748                         {
2749                                 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
2750                                 List       *exprvars;
2751                                 ListCell   *k;
2752
2753                                 if (em->em_is_const || em->em_is_child)
2754                                         continue;
2755                                 sortexpr = em->em_expr;
2756                                 exprvars = pull_var_clause((Node *) sortexpr, false);
2757                                 foreach(k, exprvars)
2758                                 {
2759                                         if (!tlist_member(lfirst(k), tlist))
2760                                                 break;
2761                                 }
2762                                 list_free(exprvars);
2763                                 if (!k)
2764                                 {
2765                                         pk_datatype = em->em_datatype;
2766                                         break;          /* found usable expression */
2767                                 }
2768                         }
2769                         if (!j)
2770                                 elog(ERROR, "could not find pathkey item to sort");
2771
2772                         /*
2773                          * Do we need to insert a Result node?
2774                          */
2775                         if (!is_projection_capable_plan(lefttree))
2776                         {
2777                                 /* copy needed so we don't modify input's tlist below */
2778                                 tlist = copyObject(tlist);
2779                                 lefttree = (Plan *) make_result(root, tlist, NULL, lefttree);
2780                         }
2781
2782                         /*
2783                          * Add resjunk entry to input's tlist
2784                          */
2785                         tle = makeTargetEntry(sortexpr,
2786                                                                   list_length(tlist) + 1,
2787                                                                   NULL,
2788                                                                   true);
2789                         tlist = lappend(tlist, tle);
2790                         lefttree->targetlist = tlist;           /* just in case NIL before */
2791                 }
2792
2793                 /*
2794                  * Look up the correct sort operator from the PathKey's slightly
2795                  * abstracted representation.
2796                  */
2797                 sortop = get_opfamily_member(pathkey->pk_opfamily,
2798                                                                          pk_datatype,
2799                                                                          pk_datatype,
2800                                                                          pathkey->pk_strategy);
2801                 if (!OidIsValid(sortop))        /* should not happen */
2802                         elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
2803                                  pathkey->pk_strategy, pk_datatype, pk_datatype,
2804                                  pathkey->pk_opfamily);
2805
2806                 /*
2807                  * The column might already be selected as a sort key, if the pathkeys
2808                  * contain duplicate entries.  (This can happen in scenarios where
2809                  * multiple mergejoinable clauses mention the same var, for example.)
2810                  * So enter it only once in the sort arrays.
2811                  */
2812                 numsortkeys = add_sort_column(tle->resno,
2813                                                                           sortop,
2814                                                                           pathkey->pk_nulls_first,
2815                                                                           numsortkeys,
2816                                                                           sortColIdx, sortOperators, nullsFirst);
2817         }
2818
2819         Assert(numsortkeys > 0);
2820
2821         return make_sort(root, lefttree, numsortkeys,
2822                                          sortColIdx, sortOperators, nullsFirst, limit_tuples);
2823 }
2824
2825 /*
2826  * make_sort_from_sortclauses
2827  *        Create sort plan to sort according to given sortclauses
2828  *
2829  *        'sortcls' is a list of SortClauses
2830  *        'lefttree' is the node which yields input tuples
2831  */
2832 Sort *
2833 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
2834 {
2835         List       *sub_tlist = lefttree->targetlist;
2836         ListCell   *l;
2837         int                     numsortkeys;
2838         AttrNumber *sortColIdx;
2839         Oid                *sortOperators;
2840         bool       *nullsFirst;
2841
2842         /*
2843          * We will need at most list_length(sortcls) sort columns; possibly less
2844          */
2845         numsortkeys = list_length(sortcls);
2846         sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2847         sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2848         nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2849
2850         numsortkeys = 0;
2851
2852         foreach(l, sortcls)
2853         {
2854                 SortClause *sortcl = (SortClause *) lfirst(l);
2855                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
2856
2857                 /*
2858                  * Check for the possibility of duplicate order-by clauses --- the
2859                  * parser should have removed 'em, but no point in sorting
2860                  * redundantly.
2861                  */
2862                 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
2863                                                                           sortcl->nulls_first,
2864                                                                           numsortkeys,
2865                                                                           sortColIdx, sortOperators, nullsFirst);
2866         }
2867
2868         Assert(numsortkeys > 0);
2869
2870         return make_sort(root, lefttree, numsortkeys,
2871                                          sortColIdx, sortOperators, nullsFirst, -1.0);
2872 }
2873
2874 /*
2875  * make_sort_from_groupcols
2876  *        Create sort plan to sort based on grouping columns
2877  *
2878  * 'groupcls' is the list of GroupClauses
2879  * 'grpColIdx' gives the column numbers to use
2880  *
2881  * This might look like it could be merged with make_sort_from_sortclauses,
2882  * but presently we *must* use the grpColIdx[] array to locate sort columns,
2883  * because the child plan's tlist is not marked with ressortgroupref info
2884  * appropriate to the grouping node.  So, only the sort ordering info
2885  * is used from the GroupClause entries.
2886  */
2887 Sort *
2888 make_sort_from_groupcols(PlannerInfo *root,
2889                                                  List *groupcls,
2890                                                  AttrNumber *grpColIdx,
2891                                                  Plan *lefttree)
2892 {
2893         List       *sub_tlist = lefttree->targetlist;
2894         int                     grpno = 0;
2895         ListCell   *l;
2896         int                     numsortkeys;
2897         AttrNumber *sortColIdx;
2898         Oid                *sortOperators;
2899         bool       *nullsFirst;
2900
2901         /*
2902          * We will need at most list_length(groupcls) sort columns; possibly less
2903          */
2904         numsortkeys = list_length(groupcls);
2905         sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2906         sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2907         nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2908
2909         numsortkeys = 0;
2910
2911         foreach(l, groupcls)
2912         {
2913                 GroupClause *grpcl = (GroupClause *) lfirst(l);
2914                 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
2915
2916                 /*
2917                  * Check for the possibility of duplicate group-by clauses --- the
2918                  * parser should have removed 'em, but no point in sorting
2919                  * redundantly.
2920                  */
2921                 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
2922                                                                           grpcl->nulls_first,
2923                                                                           numsortkeys,
2924                                                                           sortColIdx, sortOperators, nullsFirst);
2925                 grpno++;
2926         }
2927
2928         Assert(numsortkeys > 0);
2929
2930         return make_sort(root, lefttree, numsortkeys,
2931                                          sortColIdx, sortOperators, nullsFirst, -1.0);
2932 }
2933
2934 Material *
2935 make_material(Plan *lefttree)
2936 {
2937         Material   *node = makeNode(Material);
2938         Plan       *plan = &node->plan;
2939
2940         /* cost should be inserted by caller */
2941         plan->targetlist = lefttree->targetlist;
2942         plan->qual = NIL;
2943         plan->lefttree = lefttree;
2944         plan->righttree = NULL;
2945
2946         return node;
2947 }
2948
2949 /*
2950  * materialize_finished_plan: stick a Material node atop a completed plan
2951  *
2952  * There are a couple of places where we want to attach a Material node
2953  * after completion of subquery_planner().      This currently requires hackery.
2954  * Since subquery_planner has already run SS_finalize_plan on the subplan
2955  * tree, we have to kluge up parameter lists for the Material node.
2956  * Possibly this could be fixed by postponing SS_finalize_plan processing
2957  * until setrefs.c is run?
2958  */
2959 Plan *
2960 materialize_finished_plan(Plan *subplan)
2961 {
2962         Plan       *matplan;
2963         Path            matpath;                /* dummy for result of cost_material */
2964
2965         matplan = (Plan *) make_material(subplan);
2966
2967         /* Set cost data */
2968         cost_material(&matpath,
2969                                   subplan->total_cost,
2970                                   subplan->plan_rows,
2971                                   subplan->plan_width);
2972         matplan->startup_cost = matpath.startup_cost;
2973         matplan->total_cost = matpath.total_cost;
2974         matplan->plan_rows = subplan->plan_rows;
2975         matplan->plan_width = subplan->plan_width;
2976
2977         /* parameter kluge --- see comments above */
2978         matplan->extParam = bms_copy(subplan->extParam);
2979         matplan->allParam = bms_copy(subplan->allParam);
2980
2981         return matplan;
2982 }
2983
2984 Agg *
2985 make_agg(PlannerInfo *root, List *tlist, List *qual,
2986                  AggStrategy aggstrategy,
2987                  int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
2988                  long numGroups, int numAggs,
2989                  Plan *lefttree)
2990 {
2991         Agg                *node = makeNode(Agg);
2992         Plan       *plan = &node->plan;
2993         Path            agg_path;               /* dummy for result of cost_agg */
2994         QualCost        qual_cost;
2995
2996         node->aggstrategy = aggstrategy;
2997         node->numCols = numGroupCols;
2998         node->grpColIdx = grpColIdx;
2999         node->grpOperators = grpOperators;
3000         node->numGroups = numGroups;
3001
3002         copy_plan_costsize(plan, lefttree); /* only care about copying size */
3003         cost_agg(&agg_path, root,
3004                          aggstrategy, numAggs,
3005                          numGroupCols, numGroups,
3006                          lefttree->startup_cost,
3007                          lefttree->total_cost,
3008                          lefttree->plan_rows);
3009         plan->startup_cost = agg_path.startup_cost;
3010         plan->total_cost = agg_path.total_cost;
3011
3012         /*
3013          * We will produce a single output tuple if not grouping, and a tuple per
3014          * group otherwise.
3015          */
3016         if (aggstrategy == AGG_PLAIN)
3017                 plan->plan_rows = 1;
3018         else
3019                 plan->plan_rows = numGroups;
3020
3021         /*
3022          * We also need to account for the cost of evaluation of the qual (ie, the
3023          * HAVING clause) and the tlist.  Note that cost_qual_eval doesn't charge
3024          * anything for Aggref nodes; this is okay since they are really
3025          * comparable to Vars.
3026          *
3027          * See notes in grouping_planner about why this routine and make_group are
3028          * the only ones in this file that worry about tlist eval cost.
3029          */
3030         if (qual)
3031         {
3032                 cost_qual_eval(&qual_cost, qual, root);
3033                 plan->startup_cost += qual_cost.startup;
3034                 plan->total_cost += qual_cost.startup;
3035                 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3036         }
3037         cost_qual_eval(&qual_cost, tlist, root);
3038         plan->startup_cost += qual_cost.startup;
3039         plan->total_cost += qual_cost.startup;
3040         plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3041
3042         plan->qual = qual;
3043         plan->targetlist = tlist;
3044         plan->lefttree = lefttree;
3045         plan->righttree = NULL;
3046
3047         return node;
3048 }
3049
3050 Group *
3051 make_group(PlannerInfo *root,
3052                    List *tlist,
3053                    List *qual,
3054                    int numGroupCols,
3055                    AttrNumber *grpColIdx,
3056                    Oid *grpOperators,
3057                    double numGroups,
3058                    Plan *lefttree)
3059 {
3060         Group      *node = makeNode(Group);
3061         Plan       *plan = &node->plan;
3062         Path            group_path;             /* dummy for result of cost_group */
3063         QualCost        qual_cost;
3064
3065         node->numCols = numGroupCols;
3066         node->grpColIdx = grpColIdx;
3067         node->grpOperators = grpOperators;
3068
3069         copy_plan_costsize(plan, lefttree); /* only care about copying size */
3070         cost_group(&group_path, root,
3071                            numGroupCols, numGroups,
3072                            lefttree->startup_cost,
3073                            lefttree->total_cost,
3074                            lefttree->plan_rows);
3075         plan->startup_cost = group_path.startup_cost;
3076         plan->total_cost = group_path.total_cost;
3077
3078         /* One output tuple per estimated result group */
3079         plan->plan_rows = numGroups;
3080
3081         /*
3082          * We also need to account for the cost of evaluation of the qual (ie, the
3083          * HAVING clause) and the tlist.
3084          *
3085          * XXX this double-counts the cost of evaluation of any expressions used
3086          * for grouping, since in reality those will have been evaluated at a
3087          * lower plan level and will only be copied by the Group node. Worth
3088          * fixing?
3089          *
3090          * See notes in grouping_planner about why this routine and make_agg are
3091          * the only ones in this file that worry about tlist eval cost.
3092          */
3093         if (qual)
3094         {
3095                 cost_qual_eval(&qual_cost, qual, root);
3096                 plan->startup_cost += qual_cost.startup;
3097                 plan->total_cost += qual_cost.startup;
3098                 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3099         }
3100         cost_qual_eval(&qual_cost, tlist, root);
3101         plan->startup_cost += qual_cost.startup;
3102         plan->total_cost += qual_cost.startup;
3103         plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3104
3105         plan->qual = qual;
3106         plan->targetlist = tlist;
3107         plan->lefttree = lefttree;
3108         plan->righttree = NULL;
3109
3110         return node;
3111 }
3112
3113 /*
3114  * distinctList is a list of SortClauses, identifying the targetlist items
3115  * that should be considered by the Unique filter.  The input path must
3116  * already be sorted accordingly.
3117  */
3118 Unique *
3119 make_unique(Plan *lefttree, List *distinctList)
3120 {
3121         Unique     *node = makeNode(Unique);
3122         Plan       *plan = &node->plan;
3123         int                     numCols = list_length(distinctList);
3124         int                     keyno = 0;
3125         AttrNumber *uniqColIdx;
3126         Oid                *uniqOperators;
3127         ListCell   *slitem;
3128
3129         copy_plan_costsize(plan, lefttree);
3130
3131         /*
3132          * Charge one cpu_operator_cost per comparison per input tuple. We assume
3133          * all columns get compared at most of the tuples.      (XXX probably this is
3134          * an overestimate.)
3135          */
3136         plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3137
3138         /*
3139          * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
3140          * we assume the filter removes nothing.  The caller must alter this if he
3141          * has a better idea.
3142          */
3143
3144         plan->targetlist = lefttree->targetlist;
3145         plan->qual = NIL;
3146         plan->lefttree = lefttree;
3147         plan->righttree = NULL;
3148
3149         /*
3150          * convert SortClause list into arrays of attr indexes and equality
3151          * operators, as wanted by executor
3152          */
3153         Assert(numCols > 0);
3154         uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3155         uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3156
3157         foreach(slitem, distinctList)
3158         {
3159                 SortClause *sortcl = (SortClause *) lfirst(slitem);
3160                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3161
3162                 uniqColIdx[keyno] = tle->resno;
3163                 uniqOperators[keyno] = get_equality_op_for_ordering_op(sortcl->sortop);
3164                 if (!OidIsValid(uniqOperators[keyno]))          /* shouldn't happen */
3165                         elog(ERROR, "could not find equality operator for ordering operator %u",
3166                                  sortcl->sortop);
3167                 keyno++;
3168         }
3169
3170         node->numCols = numCols;
3171         node->uniqColIdx = uniqColIdx;
3172         node->uniqOperators = uniqOperators;
3173
3174         return node;
3175 }
3176
3177 /*
3178  * distinctList is a list of SortClauses, identifying the targetlist items
3179  * that should be considered by the SetOp filter.  The input path must
3180  * already be sorted accordingly.
3181  */
3182 SetOp *
3183 make_setop(SetOpCmd cmd, Plan *lefttree,
3184                    List *distinctList, AttrNumber flagColIdx)
3185 {
3186         SetOp      *node = makeNode(SetOp);
3187         Plan       *plan = &node->plan;
3188         int                     numCols = list_length(distinctList);
3189         int                     keyno = 0;
3190         AttrNumber *dupColIdx;
3191         Oid                *dupOperators;
3192         ListCell   *slitem;
3193
3194         copy_plan_costsize(plan, lefttree);
3195
3196         /*
3197          * Charge one cpu_operator_cost per comparison per input tuple. We assume
3198          * all columns get compared at most of the tuples.
3199          */
3200         plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3201
3202         /*
3203          * We make the unsupported assumption that there will be 10% as many
3204          * tuples out as in.  Any way to do better?
3205          */
3206         plan->plan_rows *= 0.1;
3207         if (plan->plan_rows < 1)
3208                 plan->plan_rows = 1;
3209
3210         plan->targetlist = lefttree->targetlist;
3211         plan->qual = NIL;
3212         plan->lefttree = lefttree;
3213         plan->righttree = NULL;
3214
3215         /*
3216          * convert SortClause list into arrays of attr indexes and equality
3217          * operators, as wanted by executor
3218          */
3219         Assert(numCols > 0);
3220         dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3221         dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3222
3223         foreach(slitem, distinctList)
3224         {
3225                 SortClause *sortcl = (SortClause *) lfirst(slitem);
3226                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3227
3228                 dupColIdx[keyno] = tle->resno;
3229                 dupOperators[keyno] = get_equality_op_for_ordering_op(sortcl->sortop);
3230                 if (!OidIsValid(dupOperators[keyno]))           /* shouldn't happen */
3231                         elog(ERROR, "could not find equality operator for ordering operator %u",
3232                                  sortcl->sortop);
3233                 keyno++;
3234         }
3235
3236         node->cmd = cmd;
3237         node->numCols = numCols;
3238         node->dupColIdx = dupColIdx;
3239         node->dupOperators = dupOperators;
3240         node->flagColIdx = flagColIdx;
3241
3242         return node;
3243 }
3244
3245 /*
3246  * Note: offset_est and count_est are passed in to save having to repeat
3247  * work already done to estimate the values of the limitOffset and limitCount
3248  * expressions.  Their values are as returned by preprocess_limit (0 means
3249  * "not relevant", -1 means "couldn't estimate").  Keep the code below in sync
3250  * with that function!
3251  */
3252 Limit *
3253 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
3254                    int64 offset_est, int64 count_est)
3255 {
3256         Limit      *node = makeNode(Limit);
3257         Plan       *plan = &node->plan;
3258
3259         copy_plan_costsize(plan, lefttree);
3260
3261         /*
3262          * Adjust the output rows count and costs according to the offset/limit.
3263          * This is only a cosmetic issue if we are at top level, but if we are
3264          * building a subquery then it's important to report correct info to the
3265          * outer planner.
3266          *
3267          * When the offset or count couldn't be estimated, use 10% of the
3268          * estimated number of rows emitted from the subplan.
3269          */
3270         if (offset_est != 0)
3271         {
3272                 double          offset_rows;
3273
3274                 if (offset_est > 0)
3275                         offset_rows = (double) offset_est;
3276                 else
3277                         offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3278                 if (offset_rows > plan->plan_rows)
3279                         offset_rows = plan->plan_rows;
3280                 if (plan->plan_rows > 0)
3281                         plan->startup_cost +=
3282                                 (plan->total_cost - plan->startup_cost)
3283                                 * offset_rows / plan->plan_rows;
3284                 plan->plan_rows -= offset_rows;
3285                 if (plan->plan_rows < 1)
3286                         plan->plan_rows = 1;
3287         }
3288
3289         if (count_est != 0)
3290         {
3291                 double          count_rows;
3292
3293                 if (count_est > 0)
3294                         count_rows = (double) count_est;
3295                 else
3296                         count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3297                 if (count_rows > plan->plan_rows)
3298                         count_rows = plan->plan_rows;
3299                 if (plan->plan_rows > 0)
3300                         plan->total_cost = plan->startup_cost +
3301                                 (plan->total_cost - plan->startup_cost)
3302                                 * count_rows / plan->plan_rows;
3303                 plan->plan_rows = count_rows;
3304                 if (plan->plan_rows < 1)
3305                         plan->plan_rows = 1;
3306         }
3307
3308         plan->targetlist = lefttree->targetlist;
3309         plan->qual = NIL;
3310         plan->lefttree = lefttree;
3311         plan->righttree = NULL;
3312
3313         node->limitOffset = limitOffset;
3314         node->limitCount = limitCount;
3315
3316         return node;
3317 }
3318
3319 /*
3320  * make_result
3321  *        Build a Result plan node
3322  *
3323  * If we have a subplan, assume that any evaluation costs for the gating qual
3324  * were already factored into the subplan's startup cost, and just copy the
3325  * subplan cost.  If there's no subplan, we should include the qual eval
3326  * cost.  In either case, tlist eval cost is not to be included here.
3327  */
3328 Result *
3329 make_result(PlannerInfo *root,
3330                         List *tlist,
3331                         Node *resconstantqual,
3332                         Plan *subplan)
3333 {
3334         Result     *node = makeNode(Result);
3335         Plan       *plan = &node->plan;
3336
3337         if (subplan)
3338                 copy_plan_costsize(plan, subplan);
3339         else
3340         {
3341                 plan->startup_cost = 0;
3342                 plan->total_cost = cpu_tuple_cost;
3343                 plan->plan_rows = 1;    /* wrong if we have a set-valued function? */
3344                 plan->plan_width = 0;   /* XXX is it worth being smarter? */
3345                 if (resconstantqual)
3346                 {
3347                         QualCost        qual_cost;
3348
3349                         cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
3350                         /* resconstantqual is evaluated once at startup */
3351                         plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
3352                         plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
3353                 }
3354         }
3355
3356         plan->targetlist = tlist;
3357         plan->qual = NIL;
3358         plan->lefttree = subplan;
3359         plan->righttree = NULL;
3360         node->resconstantqual = resconstantqual;
3361
3362         return node;
3363 }
3364
3365 /*
3366  * is_projection_capable_plan
3367  *              Check whether a given Plan node is able to do projection.
3368  */
3369 bool
3370 is_projection_capable_plan(Plan *plan)
3371 {
3372         /* Most plan types can project, so just list the ones that can't */
3373         switch (nodeTag(plan))
3374         {
3375                 case T_Hash:
3376                 case T_Material:
3377                 case T_Sort:
3378                 case T_Unique:
3379                 case T_SetOp:
3380                 case T_Limit:
3381                 case T_Append:
3382                         return false;
3383                 default:
3384                         break;
3385         }
3386         return true;
3387 }