]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/plan/createplan.c
Fix long-obsolete code for separating filter conditions in cost_index().
[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-2015, PostgreSQL Global Development Group
9  * Portions Copyright (c) 1994, Regents of the University of California
10  *
11  *
12  * IDENTIFICATION
13  *        src/backend/optimizer/plan/createplan.c
14  *
15  *-------------------------------------------------------------------------
16  */
17 #include "postgres.h"
18
19 #include <limits.h>
20 #include <math.h>
21
22 #include "access/skey.h"
23 #include "access/sysattr.h"
24 #include "catalog/pg_class.h"
25 #include "foreign/fdwapi.h"
26 #include "miscadmin.h"
27 #include "nodes/makefuncs.h"
28 #include "nodes/nodeFuncs.h"
29 #include "optimizer/clauses.h"
30 #include "optimizer/cost.h"
31 #include "optimizer/paths.h"
32 #include "optimizer/placeholder.h"
33 #include "optimizer/plancat.h"
34 #include "optimizer/planmain.h"
35 #include "optimizer/planner.h"
36 #include "optimizer/predtest.h"
37 #include "optimizer/prep.h"
38 #include "optimizer/restrictinfo.h"
39 #include "optimizer/subselect.h"
40 #include "optimizer/tlist.h"
41 #include "optimizer/var.h"
42 #include "parser/parse_clause.h"
43 #include "parser/parsetree.h"
44 #include "utils/lsyscache.h"
45
46
47 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path);
48 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
49 static List *build_path_tlist(PlannerInfo *root, Path *path);
50 static bool use_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
51 static void disuse_physical_tlist(PlannerInfo *root, Plan *plan, Path *path);
52 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
53 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
54 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
55 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
56 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
57 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
58 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
59 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
60                                         List *tlist, List *scan_clauses);
61 static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
62                                           List *tlist, List *scan_clauses, bool indexonly);
63 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
64                                                 BitmapHeapPath *best_path,
65                                                 List *tlist, List *scan_clauses);
66 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
67                                           List **qual, List **indexqual, List **indexECs);
68 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
69                                         List *tlist, List *scan_clauses);
70 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
71                                                  List *tlist, List *scan_clauses);
72 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
73                                                  List *tlist, List *scan_clauses);
74 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
75                                            List *tlist, List *scan_clauses);
76 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
77                                         List *tlist, List *scan_clauses);
78 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
79                                                   List *tlist, List *scan_clauses);
80 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
81                                                 List *tlist, List *scan_clauses);
82 static CustomScan *create_customscan_plan(PlannerInfo *root,
83                                            CustomPath *best_path,
84                                            List *tlist, List *scan_clauses);
85 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
86                                          Plan *outer_plan, Plan *inner_plan);
87 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
88                                           Plan *outer_plan, Plan *inner_plan);
89 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
90                                          Plan *outer_plan, Plan *inner_plan);
91 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
92 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
93 static void process_subquery_nestloop_params(PlannerInfo *root,
94                                                                  List *subplan_params);
95 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
96 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
97 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
98 static List *get_switched_clauses(List *clauses, Relids outerrelids);
99 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
100 static void copy_path_costsize(Plan *dest, Path *src);
101 static void copy_plan_costsize(Plan *dest, Plan *src);
102 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
103 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
104                            Oid indexid, List *indexqual, List *indexqualorig,
105                            List *indexorderby, List *indexorderbyorig,
106                            ScanDirection indexscandir);
107 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
108                                    Index scanrelid, Oid indexid,
109                                    List *indexqual, List *indexorderby,
110                                    List *indextlist,
111                                    ScanDirection indexscandir);
112 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
113                                           List *indexqual,
114                                           List *indexqualorig);
115 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
116                                          List *qpqual,
117                                          Plan *lefttree,
118                                          List *bitmapqualorig,
119                                          Index scanrelid);
120 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
121                          List *tidquals);
122 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
123                                   Index scanrelid, List *functions, bool funcordinality);
124 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
125                                 Index scanrelid, List *values_lists);
126 static CteScan *make_ctescan(List *qptlist, List *qpqual,
127                          Index scanrelid, int ctePlanId, int cteParam);
128 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
129                                    Index scanrelid, int wtParam);
130 static BitmapAnd *make_bitmap_and(List *bitmapplans);
131 static BitmapOr *make_bitmap_or(List *bitmapplans);
132 static NestLoop *make_nestloop(List *tlist,
133                           List *joinclauses, List *otherclauses, List *nestParams,
134                           Plan *lefttree, Plan *righttree,
135                           JoinType jointype);
136 static HashJoin *make_hashjoin(List *tlist,
137                           List *joinclauses, List *otherclauses,
138                           List *hashclauses,
139                           Plan *lefttree, Plan *righttree,
140                           JoinType jointype);
141 static Hash *make_hash(Plan *lefttree,
142                   Oid skewTable,
143                   AttrNumber skewColumn,
144                   bool skewInherit,
145                   Oid skewColType,
146                   int32 skewColTypmod);
147 static MergeJoin *make_mergejoin(List *tlist,
148                            List *joinclauses, List *otherclauses,
149                            List *mergeclauses,
150                            Oid *mergefamilies,
151                            Oid *mergecollations,
152                            int *mergestrategies,
153                            bool *mergenullsfirst,
154                            Plan *lefttree, Plan *righttree,
155                            JoinType jointype);
156 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
157                   AttrNumber *sortColIdx, Oid *sortOperators,
158                   Oid *collations, bool *nullsFirst,
159                   double limit_tuples);
160 static Plan *prepare_sort_from_pathkeys(PlannerInfo *root,
161                                                    Plan *lefttree, List *pathkeys,
162                                                    Relids relids,
163                                                    const AttrNumber *reqColIdx,
164                                                    bool adjust_tlist_in_place,
165                                                    int *p_numsortkeys,
166                                                    AttrNumber **p_sortColIdx,
167                                                    Oid **p_sortOperators,
168                                                    Oid **p_collations,
169                                                    bool **p_nullsFirst);
170 static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
171                                            TargetEntry *tle,
172                                            Relids relids);
173 static Material *make_material(Plan *lefttree);
174
175
176 /*
177  * create_plan
178  *        Creates the access plan for a query by recursively processing the
179  *        desired tree of pathnodes, starting at the node 'best_path'.  For
180  *        every pathnode found, we create a corresponding plan node containing
181  *        appropriate id, target list, and qualification information.
182  *
183  *        The tlists and quals in the plan tree are still in planner format,
184  *        ie, Vars still correspond to the parser's numbering.  This will be
185  *        fixed later by setrefs.c.
186  *
187  *        best_path is the best access path
188  *
189  *        Returns a Plan tree.
190  */
191 Plan *
192 create_plan(PlannerInfo *root, Path *best_path)
193 {
194         Plan       *plan;
195
196         /* plan_params should not be in use in current query level */
197         Assert(root->plan_params == NIL);
198
199         /* Initialize this module's private workspace in PlannerInfo */
200         root->curOuterRels = NULL;
201         root->curOuterParams = NIL;
202
203         /* Recursively process the path tree */
204         plan = create_plan_recurse(root, best_path);
205
206         /* Check we successfully assigned all NestLoopParams to plan nodes */
207         if (root->curOuterParams != NIL)
208                 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
209
210         /*
211          * Reset plan_params to ensure param IDs used for nestloop params are not
212          * re-used later
213          */
214         root->plan_params = NIL;
215
216         return plan;
217 }
218
219 /*
220  * create_plan_recurse
221  *        Recursive guts of create_plan().
222  */
223 static Plan *
224 create_plan_recurse(PlannerInfo *root, Path *best_path)
225 {
226         Plan       *plan;
227
228         switch (best_path->pathtype)
229         {
230                 case T_SeqScan:
231                 case T_IndexScan:
232                 case T_IndexOnlyScan:
233                 case T_BitmapHeapScan:
234                 case T_TidScan:
235                 case T_SubqueryScan:
236                 case T_FunctionScan:
237                 case T_ValuesScan:
238                 case T_CteScan:
239                 case T_WorkTableScan:
240                 case T_ForeignScan:
241                 case T_CustomScan:
242                         plan = create_scan_plan(root, best_path);
243                         break;
244                 case T_HashJoin:
245                 case T_MergeJoin:
246                 case T_NestLoop:
247                         plan = create_join_plan(root,
248                                                                         (JoinPath *) best_path);
249                         break;
250                 case T_Append:
251                         plan = create_append_plan(root,
252                                                                           (AppendPath *) best_path);
253                         break;
254                 case T_MergeAppend:
255                         plan = create_merge_append_plan(root,
256                                                                                         (MergeAppendPath *) best_path);
257                         break;
258                 case T_Result:
259                         plan = (Plan *) create_result_plan(root,
260                                                                                            (ResultPath *) best_path);
261                         break;
262                 case T_Material:
263                         plan = (Plan *) create_material_plan(root,
264                                                                                                  (MaterialPath *) best_path);
265                         break;
266                 case T_Unique:
267                         plan = create_unique_plan(root,
268                                                                           (UniquePath *) best_path);
269                         break;
270                 default:
271                         elog(ERROR, "unrecognized node type: %d",
272                                  (int) best_path->pathtype);
273                         plan = NULL;            /* keep compiler quiet */
274                         break;
275         }
276
277         return plan;
278 }
279
280 /*
281  * create_scan_plan
282  *       Create a scan plan for the parent relation of 'best_path'.
283  */
284 static Plan *
285 create_scan_plan(PlannerInfo *root, Path *best_path)
286 {
287         RelOptInfo *rel = best_path->parent;
288         List       *tlist;
289         List       *scan_clauses;
290         Plan       *plan;
291
292         /*
293          * For table scans, rather than using the relation targetlist (which is
294          * only those Vars actually needed by the query), we prefer to generate a
295          * tlist containing all Vars in order.  This will allow the executor to
296          * optimize away projection of the table tuples, if possible.  (Note that
297          * planner.c may replace the tlist we generate here, forcing projection to
298          * occur.)
299          */
300         if (use_physical_tlist(root, rel))
301         {
302                 if (best_path->pathtype == T_IndexOnlyScan)
303                 {
304                         /* For index-only scan, the preferred tlist is the index's */
305                         tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
306                 }
307                 else
308                 {
309                         tlist = build_physical_tlist(root, rel);
310                         /* if fail because of dropped cols, use regular method */
311                         if (tlist == NIL)
312                                 tlist = build_path_tlist(root, best_path);
313                 }
314         }
315         else
316         {
317                 tlist = build_path_tlist(root, best_path);
318         }
319
320         /*
321          * Extract the relevant restriction clauses from the parent relation. The
322          * executor must apply all these restrictions during the scan, except for
323          * pseudoconstants which we'll take care of below.
324          */
325         scan_clauses = rel->baserestrictinfo;
326
327         /*
328          * If this is a parameterized scan, we also need to enforce all the join
329          * clauses available from the outer relation(s).
330          *
331          * For paranoia's sake, don't modify the stored baserestrictinfo list.
332          */
333         if (best_path->param_info)
334                 scan_clauses = list_concat(list_copy(scan_clauses),
335                                                                    best_path->param_info->ppi_clauses);
336
337         switch (best_path->pathtype)
338         {
339                 case T_SeqScan:
340                         plan = (Plan *) create_seqscan_plan(root,
341                                                                                                 best_path,
342                                                                                                 tlist,
343                                                                                                 scan_clauses);
344                         break;
345
346                 case T_IndexScan:
347                         plan = (Plan *) create_indexscan_plan(root,
348                                                                                                   (IndexPath *) best_path,
349                                                                                                   tlist,
350                                                                                                   scan_clauses,
351                                                                                                   false);
352                         break;
353
354                 case T_IndexOnlyScan:
355                         plan = (Plan *) create_indexscan_plan(root,
356                                                                                                   (IndexPath *) best_path,
357                                                                                                   tlist,
358                                                                                                   scan_clauses,
359                                                                                                   true);
360                         break;
361
362                 case T_BitmapHeapScan:
363                         plan = (Plan *) create_bitmap_scan_plan(root,
364                                                                                                 (BitmapHeapPath *) best_path,
365                                                                                                         tlist,
366                                                                                                         scan_clauses);
367                         break;
368
369                 case T_TidScan:
370                         plan = (Plan *) create_tidscan_plan(root,
371                                                                                                 (TidPath *) best_path,
372                                                                                                 tlist,
373                                                                                                 scan_clauses);
374                         break;
375
376                 case T_SubqueryScan:
377                         plan = (Plan *) create_subqueryscan_plan(root,
378                                                                                                          best_path,
379                                                                                                          tlist,
380                                                                                                          scan_clauses);
381                         break;
382
383                 case T_FunctionScan:
384                         plan = (Plan *) create_functionscan_plan(root,
385                                                                                                          best_path,
386                                                                                                          tlist,
387                                                                                                          scan_clauses);
388                         break;
389
390                 case T_ValuesScan:
391                         plan = (Plan *) create_valuesscan_plan(root,
392                                                                                                    best_path,
393                                                                                                    tlist,
394                                                                                                    scan_clauses);
395                         break;
396
397                 case T_CteScan:
398                         plan = (Plan *) create_ctescan_plan(root,
399                                                                                                 best_path,
400                                                                                                 tlist,
401                                                                                                 scan_clauses);
402                         break;
403
404                 case T_WorkTableScan:
405                         plan = (Plan *) create_worktablescan_plan(root,
406                                                                                                           best_path,
407                                                                                                           tlist,
408                                                                                                           scan_clauses);
409                         break;
410
411                 case T_ForeignScan:
412                         plan = (Plan *) create_foreignscan_plan(root,
413                                                                                                         (ForeignPath *) best_path,
414                                                                                                         tlist,
415                                                                                                         scan_clauses);
416                         break;
417
418                 case T_CustomScan:
419                         plan = (Plan *) create_customscan_plan(root,
420                                                                                                    (CustomPath *) best_path,
421                                                                                                    tlist,
422                                                                                                    scan_clauses);
423                         break;
424
425                 default:
426                         elog(ERROR, "unrecognized node type: %d",
427                                  (int) best_path->pathtype);
428                         plan = NULL;            /* keep compiler quiet */
429                         break;
430         }
431
432         /*
433          * If there are any pseudoconstant clauses attached to this node, insert a
434          * gating Result node that evaluates the pseudoconstants as one-time
435          * quals.
436          */
437         if (root->hasPseudoConstantQuals)
438                 plan = create_gating_plan(root, plan, scan_clauses);
439
440         return plan;
441 }
442
443 /*
444  * Build a target list (ie, a list of TargetEntry) for the Path's output.
445  */
446 static List *
447 build_path_tlist(PlannerInfo *root, Path *path)
448 {
449         RelOptInfo *rel = path->parent;
450         List       *tlist = NIL;
451         int                     resno = 1;
452         ListCell   *v;
453
454         foreach(v, rel->reltargetlist)
455         {
456                 /* Do we really need to copy here?      Not sure */
457                 Node       *node = (Node *) copyObject(lfirst(v));
458
459                 /*
460                  * If it's a parameterized path, there might be lateral references in
461                  * the tlist, which need to be replaced with Params.  There's no need
462                  * to remake the TargetEntry nodes, so apply this to each list item
463                  * separately.
464                  */
465                 if (path->param_info)
466                         node = replace_nestloop_params(root, node);
467
468                 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
469                                                                                            resno,
470                                                                                            NULL,
471                                                                                            false));
472                 resno++;
473         }
474         return tlist;
475 }
476
477 /*
478  * use_physical_tlist
479  *              Decide whether to use a tlist matching relation structure,
480  *              rather than only those Vars actually referenced.
481  */
482 static bool
483 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
484 {
485         int                     i;
486         ListCell   *lc;
487
488         /*
489          * We can do this for real relation scans, subquery scans, function scans,
490          * values scans, and CTE scans (but not for, eg, joins).
491          */
492         if (rel->rtekind != RTE_RELATION &&
493                 rel->rtekind != RTE_SUBQUERY &&
494                 rel->rtekind != RTE_FUNCTION &&
495                 rel->rtekind != RTE_VALUES &&
496                 rel->rtekind != RTE_CTE)
497                 return false;
498
499         /*
500          * Can't do it with inheritance cases either (mainly because Append
501          * doesn't project).
502          */
503         if (rel->reloptkind != RELOPT_BASEREL)
504                 return false;
505
506         /*
507          * Can't do it if any system columns or whole-row Vars are requested.
508          * (This could possibly be fixed but would take some fragile assumptions
509          * in setrefs.c, I think.)
510          */
511         for (i = rel->min_attr; i <= 0; i++)
512         {
513                 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
514                         return false;
515         }
516
517         /*
518          * Can't do it if the rel is required to emit any placeholder expressions,
519          * either.
520          */
521         foreach(lc, root->placeholder_list)
522         {
523                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
524
525                 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
526                         bms_is_subset(phinfo->ph_eval_at, rel->relids))
527                         return false;
528         }
529
530         return true;
531 }
532
533 /*
534  * disuse_physical_tlist
535  *              Switch a plan node back to emitting only Vars actually referenced.
536  *
537  * If the plan node immediately above a scan would prefer to get only
538  * needed Vars and not a physical tlist, it must call this routine to
539  * undo the decision made by use_physical_tlist().  Currently, Hash, Sort,
540  * and Material nodes want this, so they don't have to store useless columns.
541  */
542 static void
543 disuse_physical_tlist(PlannerInfo *root, Plan *plan, Path *path)
544 {
545         /* Only need to undo it for path types handled by create_scan_plan() */
546         switch (path->pathtype)
547         {
548                 case T_SeqScan:
549                 case T_IndexScan:
550                 case T_IndexOnlyScan:
551                 case T_BitmapHeapScan:
552                 case T_TidScan:
553                 case T_SubqueryScan:
554                 case T_FunctionScan:
555                 case T_ValuesScan:
556                 case T_CteScan:
557                 case T_WorkTableScan:
558                 case T_ForeignScan:
559                 case T_CustomScan:
560                         plan->targetlist = build_path_tlist(root, path);
561                         break;
562                 default:
563                         break;
564         }
565 }
566
567 /*
568  * create_gating_plan
569  *        Deal with pseudoconstant qual clauses
570  *
571  * If the node's quals list includes any pseudoconstant quals, put them
572  * into a gating Result node atop the already-built plan.  Otherwise,
573  * return the plan as-is.
574  *
575  * Note that we don't change cost or size estimates when doing gating.
576  * The costs of qual eval were already folded into the plan's startup cost.
577  * Leaving the size alone amounts to assuming that the gating qual will
578  * succeed, which is the conservative estimate for planning upper queries.
579  * We certainly don't want to assume the output size is zero (unless the
580  * gating qual is actually constant FALSE, and that case is dealt with in
581  * clausesel.c).  Interpolating between the two cases is silly, because
582  * it doesn't reflect what will really happen at runtime, and besides which
583  * in most cases we have only a very bad idea of the probability of the gating
584  * qual being true.
585  */
586 static Plan *
587 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
588 {
589         List       *pseudoconstants;
590
591         /* Sort into desirable execution order while still in RestrictInfo form */
592         quals = order_qual_clauses(root, quals);
593
594         /* Pull out any pseudoconstant quals from the RestrictInfo list */
595         pseudoconstants = extract_actual_clauses(quals, true);
596
597         if (!pseudoconstants)
598                 return plan;
599
600         return (Plan *) make_result(root,
601                                                                 plan->targetlist,
602                                                                 (Node *) pseudoconstants,
603                                                                 plan);
604 }
605
606 /*
607  * create_join_plan
608  *        Create a join plan for 'best_path' and (recursively) plans for its
609  *        inner and outer paths.
610  */
611 static Plan *
612 create_join_plan(PlannerInfo *root, JoinPath *best_path)
613 {
614         Plan       *outer_plan;
615         Plan       *inner_plan;
616         Plan       *plan;
617         Relids          saveOuterRels = root->curOuterRels;
618
619         outer_plan = create_plan_recurse(root, best_path->outerjoinpath);
620
621         /* For a nestloop, include outer relids in curOuterRels for inner side */
622         if (best_path->path.pathtype == T_NestLoop)
623                 root->curOuterRels = bms_union(root->curOuterRels,
624                                                                    best_path->outerjoinpath->parent->relids);
625
626         inner_plan = create_plan_recurse(root, best_path->innerjoinpath);
627
628         switch (best_path->path.pathtype)
629         {
630                 case T_MergeJoin:
631                         plan = (Plan *) create_mergejoin_plan(root,
632                                                                                                   (MergePath *) best_path,
633                                                                                                   outer_plan,
634                                                                                                   inner_plan);
635                         break;
636                 case T_HashJoin:
637                         plan = (Plan *) create_hashjoin_plan(root,
638                                                                                                  (HashPath *) best_path,
639                                                                                                  outer_plan,
640                                                                                                  inner_plan);
641                         break;
642                 case T_NestLoop:
643                         /* Restore curOuterRels */
644                         bms_free(root->curOuterRels);
645                         root->curOuterRels = saveOuterRels;
646
647                         plan = (Plan *) create_nestloop_plan(root,
648                                                                                                  (NestPath *) best_path,
649                                                                                                  outer_plan,
650                                                                                                  inner_plan);
651                         break;
652                 default:
653                         elog(ERROR, "unrecognized node type: %d",
654                                  (int) best_path->path.pathtype);
655                         plan = NULL;            /* keep compiler quiet */
656                         break;
657         }
658
659         /*
660          * If there are any pseudoconstant clauses attached to this node, insert a
661          * gating Result node that evaluates the pseudoconstants as one-time
662          * quals.
663          */
664         if (root->hasPseudoConstantQuals)
665                 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
666
667 #ifdef NOT_USED
668
669         /*
670          * * Expensive function pullups may have pulled local predicates * into
671          * this path node.  Put them in the qpqual of the plan node. * JMH,
672          * 6/15/92
673          */
674         if (get_loc_restrictinfo(best_path) != NIL)
675                 set_qpqual((Plan) plan,
676                                    list_concat(get_qpqual((Plan) plan),
677                                            get_actual_clauses(get_loc_restrictinfo(best_path))));
678 #endif
679
680         return plan;
681 }
682
683 /*
684  * create_append_plan
685  *        Create an Append plan for 'best_path' and (recursively) plans
686  *        for its subpaths.
687  *
688  *        Returns a Plan node.
689  */
690 static Plan *
691 create_append_plan(PlannerInfo *root, AppendPath *best_path)
692 {
693         Append     *plan;
694         List       *tlist = build_path_tlist(root, &best_path->path);
695         List       *subplans = NIL;
696         ListCell   *subpaths;
697
698         /*
699          * The subpaths list could be empty, if every child was proven empty by
700          * constraint exclusion.  In that case generate a dummy plan that returns
701          * no rows.
702          *
703          * Note that an AppendPath with no members is also generated in certain
704          * cases where there was no appending construct at all, but we know the
705          * relation is empty (see set_dummy_rel_pathlist).
706          */
707         if (best_path->subpaths == NIL)
708         {
709                 /* Generate a Result plan with constant-FALSE gating qual */
710                 return (Plan *) make_result(root,
711                                                                         tlist,
712                                                                         (Node *) list_make1(makeBoolConst(false,
713                                                                                                                                           false)),
714                                                                         NULL);
715         }
716
717         /* Build the plan for each child */
718         foreach(subpaths, best_path->subpaths)
719         {
720                 Path       *subpath = (Path *) lfirst(subpaths);
721
722                 subplans = lappend(subplans, create_plan_recurse(root, subpath));
723         }
724
725         /*
726          * XXX ideally, if there's just one child, we'd not bother to generate an
727          * Append node but just return the single child.  At the moment this does
728          * not work because the varno of the child scan plan won't match the
729          * parent-rel Vars it'll be asked to emit.
730          */
731
732         plan = make_append(subplans, tlist);
733
734         return (Plan *) plan;
735 }
736
737 /*
738  * create_merge_append_plan
739  *        Create a MergeAppend plan for 'best_path' and (recursively) plans
740  *        for its subpaths.
741  *
742  *        Returns a Plan node.
743  */
744 static Plan *
745 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
746 {
747         MergeAppend *node = makeNode(MergeAppend);
748         Plan       *plan = &node->plan;
749         List       *tlist = build_path_tlist(root, &best_path->path);
750         List       *pathkeys = best_path->path.pathkeys;
751         List       *subplans = NIL;
752         ListCell   *subpaths;
753
754         /*
755          * We don't have the actual creation of the MergeAppend node split out
756          * into a separate make_xxx function.  This is because we want to run
757          * prepare_sort_from_pathkeys on it before we do so on the individual
758          * child plans, to make cross-checking the sort info easier.
759          */
760         copy_path_costsize(plan, (Path *) best_path);
761         plan->targetlist = tlist;
762         plan->qual = NIL;
763         plan->lefttree = NULL;
764         plan->righttree = NULL;
765
766         /* Compute sort column info, and adjust MergeAppend's tlist as needed */
767         (void) prepare_sort_from_pathkeys(root, plan, pathkeys,
768                                                                           best_path->path.parent->relids,
769                                                                           NULL,
770                                                                           true,
771                                                                           &node->numCols,
772                                                                           &node->sortColIdx,
773                                                                           &node->sortOperators,
774                                                                           &node->collations,
775                                                                           &node->nullsFirst);
776
777         /*
778          * Now prepare the child plans.  We must apply prepare_sort_from_pathkeys
779          * even to subplans that don't need an explicit sort, to make sure they
780          * are returning the same sort key columns the MergeAppend expects.
781          */
782         foreach(subpaths, best_path->subpaths)
783         {
784                 Path       *subpath = (Path *) lfirst(subpaths);
785                 Plan       *subplan;
786                 int                     numsortkeys;
787                 AttrNumber *sortColIdx;
788                 Oid                *sortOperators;
789                 Oid                *collations;
790                 bool       *nullsFirst;
791
792                 /* Build the child plan */
793                 subplan = create_plan_recurse(root, subpath);
794
795                 /* Compute sort column info, and adjust subplan's tlist as needed */
796                 subplan = prepare_sort_from_pathkeys(root, subplan, pathkeys,
797                                                                                          subpath->parent->relids,
798                                                                                          node->sortColIdx,
799                                                                                          false,
800                                                                                          &numsortkeys,
801                                                                                          &sortColIdx,
802                                                                                          &sortOperators,
803                                                                                          &collations,
804                                                                                          &nullsFirst);
805
806                 /*
807                  * Check that we got the same sort key information.  We just Assert
808                  * that the sortops match, since those depend only on the pathkeys;
809                  * but it seems like a good idea to check the sort column numbers
810                  * explicitly, to ensure the tlists really do match up.
811                  */
812                 Assert(numsortkeys == node->numCols);
813                 if (memcmp(sortColIdx, node->sortColIdx,
814                                    numsortkeys * sizeof(AttrNumber)) != 0)
815                         elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
816                 Assert(memcmp(sortOperators, node->sortOperators,
817                                           numsortkeys * sizeof(Oid)) == 0);
818                 Assert(memcmp(collations, node->collations,
819                                           numsortkeys * sizeof(Oid)) == 0);
820                 Assert(memcmp(nullsFirst, node->nullsFirst,
821                                           numsortkeys * sizeof(bool)) == 0);
822
823                 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
824                 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
825                         subplan = (Plan *) make_sort(root, subplan, numsortkeys,
826                                                                                  sortColIdx, sortOperators,
827                                                                                  collations, nullsFirst,
828                                                                                  best_path->limit_tuples);
829
830                 subplans = lappend(subplans, subplan);
831         }
832
833         node->mergeplans = subplans;
834
835         return (Plan *) node;
836 }
837
838 /*
839  * create_result_plan
840  *        Create a Result plan for 'best_path'.
841  *        This is only used for the case of a query with an empty jointree.
842  *
843  *        Returns a Plan node.
844  */
845 static Result *
846 create_result_plan(PlannerInfo *root, ResultPath *best_path)
847 {
848         List       *tlist;
849         List       *quals;
850
851         /* The tlist will be installed later, since we have no RelOptInfo */
852         Assert(best_path->path.parent == NULL);
853         tlist = NIL;
854
855         /* best_path->quals is just bare clauses */
856
857         quals = order_qual_clauses(root, best_path->quals);
858
859         return make_result(root, tlist, (Node *) quals, NULL);
860 }
861
862 /*
863  * create_material_plan
864  *        Create a Material plan for 'best_path' and (recursively) plans
865  *        for its subpaths.
866  *
867  *        Returns a Plan node.
868  */
869 static Material *
870 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
871 {
872         Material   *plan;
873         Plan       *subplan;
874
875         subplan = create_plan_recurse(root, best_path->subpath);
876
877         /* We don't want any excess columns in the materialized tuples */
878         disuse_physical_tlist(root, subplan, best_path->subpath);
879
880         plan = make_material(subplan);
881
882         copy_path_costsize(&plan->plan, (Path *) best_path);
883
884         return plan;
885 }
886
887 /*
888  * create_unique_plan
889  *        Create a Unique plan for 'best_path' and (recursively) plans
890  *        for its subpaths.
891  *
892  *        Returns a Plan node.
893  */
894 static Plan *
895 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
896 {
897         Plan       *plan;
898         Plan       *subplan;
899         List       *in_operators;
900         List       *uniq_exprs;
901         List       *newtlist;
902         int                     nextresno;
903         bool            newitems;
904         int                     numGroupCols;
905         AttrNumber *groupColIdx;
906         int                     groupColPos;
907         ListCell   *l;
908
909         subplan = create_plan_recurse(root, best_path->subpath);
910
911         /* Done if we don't need to do any actual unique-ifying */
912         if (best_path->umethod == UNIQUE_PATH_NOOP)
913                 return subplan;
914
915         /*
916          * As constructed, the subplan has a "flat" tlist containing just the Vars
917          * needed here and at upper levels.  The values we are supposed to
918          * unique-ify may be expressions in these variables.  We have to add any
919          * such expressions to the subplan's tlist.
920          *
921          * The subplan may have a "physical" tlist if it is a simple scan plan. If
922          * we're going to sort, this should be reduced to the regular tlist, so
923          * that we don't sort more data than we need to.  For hashing, the tlist
924          * should be left as-is if we don't need to add any expressions; but if we
925          * do have to add expressions, then a projection step will be needed at
926          * runtime anyway, so we may as well remove unneeded items. Therefore
927          * newtlist starts from build_path_tlist() not just a copy of the
928          * subplan's tlist; and we don't install it into the subplan unless we are
929          * sorting or stuff has to be added.
930          */
931         in_operators = best_path->in_operators;
932         uniq_exprs = best_path->uniq_exprs;
933
934         /* initialize modified subplan tlist as just the "required" vars */
935         newtlist = build_path_tlist(root, &best_path->path);
936         nextresno = list_length(newtlist) + 1;
937         newitems = false;
938
939         foreach(l, uniq_exprs)
940         {
941                 Node       *uniqexpr = lfirst(l);
942                 TargetEntry *tle;
943
944                 tle = tlist_member(uniqexpr, newtlist);
945                 if (!tle)
946                 {
947                         tle = makeTargetEntry((Expr *) uniqexpr,
948                                                                   nextresno,
949                                                                   NULL,
950                                                                   false);
951                         newtlist = lappend(newtlist, tle);
952                         nextresno++;
953                         newitems = true;
954                 }
955         }
956
957         if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
958         {
959                 /*
960                  * If the top plan node can't do projections and its existing target
961                  * list isn't already what we need, we need to add a Result node to
962                  * help it along.
963                  */
964                 if (!is_projection_capable_plan(subplan) &&
965                         !tlist_same_exprs(newtlist, subplan->targetlist))
966                         subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
967                 else
968                         subplan->targetlist = newtlist;
969         }
970
971         /*
972          * Build control information showing which subplan output columns are to
973          * be examined by the grouping step.  Unfortunately we can't merge this
974          * with the previous loop, since we didn't then know which version of the
975          * subplan tlist we'd end up using.
976          */
977         newtlist = subplan->targetlist;
978         numGroupCols = list_length(uniq_exprs);
979         groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
980
981         groupColPos = 0;
982         foreach(l, uniq_exprs)
983         {
984                 Node       *uniqexpr = lfirst(l);
985                 TargetEntry *tle;
986
987                 tle = tlist_member(uniqexpr, newtlist);
988                 if (!tle)                               /* shouldn't happen */
989                         elog(ERROR, "failed to find unique expression in subplan tlist");
990                 groupColIdx[groupColPos++] = tle->resno;
991         }
992
993         if (best_path->umethod == UNIQUE_PATH_HASH)
994         {
995                 long            numGroups;
996                 Oid                *groupOperators;
997
998                 numGroups = (long) Min(best_path->path.rows, (double) LONG_MAX);
999
1000                 /*
1001                  * Get the hashable equality operators for the Agg node to use.
1002                  * Normally these are the same as the IN clause operators, but if
1003                  * those are cross-type operators then the equality operators are the
1004                  * ones for the IN clause operators' RHS datatype.
1005                  */
1006                 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
1007                 groupColPos = 0;
1008                 foreach(l, in_operators)
1009                 {
1010                         Oid                     in_oper = lfirst_oid(l);
1011                         Oid                     eq_oper;
1012
1013                         if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
1014                                 elog(ERROR, "could not find compatible hash operator for operator %u",
1015                                          in_oper);
1016                         groupOperators[groupColPos++] = eq_oper;
1017                 }
1018
1019                 /*
1020                  * Since the Agg node is going to project anyway, we can give it the
1021                  * minimum output tlist, without any stuff we might have added to the
1022                  * subplan tlist.
1023                  */
1024                 plan = (Plan *) make_agg(root,
1025                                                                  build_path_tlist(root, &best_path->path),
1026                                                                  NIL,
1027                                                                  AGG_HASHED,
1028                                                                  NULL,
1029                                                                  numGroupCols,
1030                                                                  groupColIdx,
1031                                                                  groupOperators,
1032                                                                  numGroups,
1033                                                                  subplan);
1034         }
1035         else
1036         {
1037                 List       *sortList = NIL;
1038
1039                 /* Create an ORDER BY list to sort the input compatibly */
1040                 groupColPos = 0;
1041                 foreach(l, in_operators)
1042                 {
1043                         Oid                     in_oper = lfirst_oid(l);
1044                         Oid                     sortop;
1045                         Oid                     eqop;
1046                         TargetEntry *tle;
1047                         SortGroupClause *sortcl;
1048
1049                         sortop = get_ordering_op_for_equality_op(in_oper, false);
1050                         if (!OidIsValid(sortop))        /* shouldn't happen */
1051                                 elog(ERROR, "could not find ordering operator for equality operator %u",
1052                                          in_oper);
1053
1054                         /*
1055                          * The Unique node will need equality operators.  Normally these
1056                          * are the same as the IN clause operators, but if those are
1057                          * cross-type operators then the equality operators are the ones
1058                          * for the IN clause operators' RHS datatype.
1059                          */
1060                         eqop = get_equality_op_for_ordering_op(sortop, NULL);
1061                         if (!OidIsValid(eqop))          /* shouldn't happen */
1062                                 elog(ERROR, "could not find equality operator for ordering operator %u",
1063                                          sortop);
1064
1065                         tle = get_tle_by_resno(subplan->targetlist,
1066                                                                    groupColIdx[groupColPos]);
1067                         Assert(tle != NULL);
1068
1069                         sortcl = makeNode(SortGroupClause);
1070                         sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1071                                                                                                                  subplan->targetlist);
1072                         sortcl->eqop = eqop;
1073                         sortcl->sortop = sortop;
1074                         sortcl->nulls_first = false;
1075                         sortcl->hashable = false;       /* no need to make this accurate */
1076                         sortList = lappend(sortList, sortcl);
1077                         groupColPos++;
1078                 }
1079                 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
1080                 plan = (Plan *) make_unique(plan, sortList);
1081         }
1082
1083         /* Adjust output size estimate (other fields should be OK already) */
1084         plan->plan_rows = best_path->path.rows;
1085
1086         return plan;
1087 }
1088
1089
1090 /*****************************************************************************
1091  *
1092  *      BASE-RELATION SCAN METHODS
1093  *
1094  *****************************************************************************/
1095
1096
1097 /*
1098  * create_seqscan_plan
1099  *       Returns a seqscan plan for the base relation scanned by 'best_path'
1100  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1101  */
1102 static SeqScan *
1103 create_seqscan_plan(PlannerInfo *root, Path *best_path,
1104                                         List *tlist, List *scan_clauses)
1105 {
1106         SeqScan    *scan_plan;
1107         Index           scan_relid = best_path->parent->relid;
1108
1109         /* it should be a base rel... */
1110         Assert(scan_relid > 0);
1111         Assert(best_path->parent->rtekind == RTE_RELATION);
1112
1113         /* Sort clauses into best execution order */
1114         scan_clauses = order_qual_clauses(root, scan_clauses);
1115
1116         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1117         scan_clauses = extract_actual_clauses(scan_clauses, false);
1118
1119         /* Replace any outer-relation variables with nestloop params */
1120         if (best_path->param_info)
1121         {
1122                 scan_clauses = (List *)
1123                         replace_nestloop_params(root, (Node *) scan_clauses);
1124         }
1125
1126         scan_plan = make_seqscan(tlist,
1127                                                          scan_clauses,
1128                                                          scan_relid);
1129
1130         copy_path_costsize(&scan_plan->plan, best_path);
1131
1132         return scan_plan;
1133 }
1134
1135 /*
1136  * create_indexscan_plan
1137  *        Returns an indexscan plan for the base relation scanned by 'best_path'
1138  *        with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1139  *
1140  * We use this for both plain IndexScans and IndexOnlyScans, because the
1141  * qual preprocessing work is the same for both.  Note that the caller tells
1142  * us which to build --- we don't look at best_path->path.pathtype, because
1143  * create_bitmap_subplan needs to be able to override the prior decision.
1144  */
1145 static Scan *
1146 create_indexscan_plan(PlannerInfo *root,
1147                                           IndexPath *best_path,
1148                                           List *tlist,
1149                                           List *scan_clauses,
1150                                           bool indexonly)
1151 {
1152         Scan       *scan_plan;
1153         List       *indexquals = best_path->indexquals;
1154         List       *indexorderbys = best_path->indexorderbys;
1155         Index           baserelid = best_path->path.parent->relid;
1156         Oid                     indexoid = best_path->indexinfo->indexoid;
1157         List       *qpqual;
1158         List       *stripped_indexquals;
1159         List       *fixed_indexquals;
1160         List       *fixed_indexorderbys;
1161         ListCell   *l;
1162
1163         /* it should be a base rel... */
1164         Assert(baserelid > 0);
1165         Assert(best_path->path.parent->rtekind == RTE_RELATION);
1166
1167         /*
1168          * Build "stripped" indexquals structure (no RestrictInfos) to pass to
1169          * executor as indexqualorig
1170          */
1171         stripped_indexquals = get_actual_clauses(indexquals);
1172
1173         /*
1174          * The executor needs a copy with the indexkey on the left of each clause
1175          * and with index Vars substituted for table ones.
1176          */
1177         fixed_indexquals = fix_indexqual_references(root, best_path);
1178
1179         /*
1180          * Likewise fix up index attr references in the ORDER BY expressions.
1181          */
1182         fixed_indexorderbys = fix_indexorderby_references(root, best_path);
1183
1184         /*
1185          * The qpqual list must contain all restrictions not automatically handled
1186          * by the index, other than pseudoconstant clauses which will be handled
1187          * by a separate gating plan node.  All the predicates in the indexquals
1188          * will be checked (either by the index itself, or by nodeIndexscan.c),
1189          * but if there are any "special" operators involved then they must be
1190          * included in qpqual.  The upshot is that qpqual must contain
1191          * scan_clauses minus whatever appears in indexquals.
1192          *
1193          * In normal cases simple pointer equality checks will be enough to spot
1194          * duplicate RestrictInfos, so we try that first.
1195          *
1196          * Another common case is that a scan_clauses entry is generated from the
1197          * same EquivalenceClass as some indexqual, and is therefore redundant
1198          * with it, though not equal.  (This happens when indxpath.c prefers a
1199          * different derived equality than what generate_join_implied_equalities
1200          * picked for a parameterized scan's ppi_clauses.)
1201          *
1202          * In some situations (particularly with OR'd index conditions) we may
1203          * have scan_clauses that are not equal to, but are logically implied by,
1204          * the index quals; so we also try a predicate_implied_by() check to see
1205          * if we can discard quals that way.  (predicate_implied_by assumes its
1206          * first input contains only immutable functions, so we have to check
1207          * that.)
1208          *
1209          * We can also discard quals that are implied by a partial index's
1210          * predicate, but only in a plain SELECT; when scanning a target relation
1211          * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
1212          * plan so that they'll be properly rechecked by EvalPlanQual testing.
1213          *
1214          * Note: if you change this bit of code you should also look at
1215          * extract_nonindex_conditions() in costsize.c.
1216          */
1217         qpqual = NIL;
1218         foreach(l, scan_clauses)
1219         {
1220                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1221
1222                 Assert(IsA(rinfo, RestrictInfo));
1223                 if (rinfo->pseudoconstant)
1224                         continue;                       /* we may drop pseudoconstants here */
1225                 if (list_member_ptr(indexquals, rinfo))
1226                         continue;                       /* simple duplicate */
1227                 if (is_redundant_derived_clause(rinfo, indexquals))
1228                         continue;                       /* derived from same EquivalenceClass */
1229                 if (!contain_mutable_functions((Node *) rinfo->clause))
1230                 {
1231                         List       *clausel = list_make1(rinfo->clause);
1232
1233                         if (predicate_implied_by(clausel, indexquals))
1234                                 continue;               /* provably implied by indexquals */
1235                         if (best_path->indexinfo->indpred)
1236                         {
1237                                 if (baserelid != root->parse->resultRelation &&
1238                                         get_plan_rowmark(root->rowMarks, baserelid) == NULL)
1239                                         if (predicate_implied_by(clausel,
1240                                                                                          best_path->indexinfo->indpred))
1241                                                 continue;               /* implied by index predicate */
1242                         }
1243                 }
1244                 qpqual = lappend(qpqual, rinfo);
1245         }
1246
1247         /* Sort clauses into best execution order */
1248         qpqual = order_qual_clauses(root, qpqual);
1249
1250         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1251         qpqual = extract_actual_clauses(qpqual, false);
1252
1253         /*
1254          * We have to replace any outer-relation variables with nestloop params in
1255          * the indexqualorig, qpqual, and indexorderbyorig expressions.  A bit
1256          * annoying to have to do this separately from the processing in
1257          * fix_indexqual_references --- rethink this when generalizing the inner
1258          * indexscan support.  But note we can't really do this earlier because
1259          * it'd break the comparisons to predicates above ... (or would it?  Those
1260          * wouldn't have outer refs)
1261          */
1262         if (best_path->path.param_info)
1263         {
1264                 stripped_indexquals = (List *)
1265                         replace_nestloop_params(root, (Node *) stripped_indexquals);
1266                 qpqual = (List *)
1267                         replace_nestloop_params(root, (Node *) qpqual);
1268                 indexorderbys = (List *)
1269                         replace_nestloop_params(root, (Node *) indexorderbys);
1270         }
1271
1272         /* Finally ready to build the plan node */
1273         if (indexonly)
1274                 scan_plan = (Scan *) make_indexonlyscan(tlist,
1275                                                                                                 qpqual,
1276                                                                                                 baserelid,
1277                                                                                                 indexoid,
1278                                                                                                 fixed_indexquals,
1279                                                                                                 fixed_indexorderbys,
1280                                                                                         best_path->indexinfo->indextlist,
1281                                                                                                 best_path->indexscandir);
1282         else
1283                 scan_plan = (Scan *) make_indexscan(tlist,
1284                                                                                         qpqual,
1285                                                                                         baserelid,
1286                                                                                         indexoid,
1287                                                                                         fixed_indexquals,
1288                                                                                         stripped_indexquals,
1289                                                                                         fixed_indexorderbys,
1290                                                                                         indexorderbys,
1291                                                                                         best_path->indexscandir);
1292
1293         copy_path_costsize(&scan_plan->plan, &best_path->path);
1294
1295         return scan_plan;
1296 }
1297
1298 /*
1299  * create_bitmap_scan_plan
1300  *        Returns a bitmap scan plan for the base relation scanned by 'best_path'
1301  *        with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1302  */
1303 static BitmapHeapScan *
1304 create_bitmap_scan_plan(PlannerInfo *root,
1305                                                 BitmapHeapPath *best_path,
1306                                                 List *tlist,
1307                                                 List *scan_clauses)
1308 {
1309         Index           baserelid = best_path->path.parent->relid;
1310         Plan       *bitmapqualplan;
1311         List       *bitmapqualorig;
1312         List       *indexquals;
1313         List       *indexECs;
1314         List       *qpqual;
1315         ListCell   *l;
1316         BitmapHeapScan *scan_plan;
1317
1318         /* it should be a base rel... */
1319         Assert(baserelid > 0);
1320         Assert(best_path->path.parent->rtekind == RTE_RELATION);
1321
1322         /* Process the bitmapqual tree into a Plan tree and qual lists */
1323         bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1324                                                                                    &bitmapqualorig, &indexquals,
1325                                                                                    &indexECs);
1326
1327         /*
1328          * The qpqual list must contain all restrictions not automatically handled
1329          * by the index, other than pseudoconstant clauses which will be handled
1330          * by a separate gating plan node.  All the predicates in the indexquals
1331          * will be checked (either by the index itself, or by
1332          * nodeBitmapHeapscan.c), but if there are any "special" operators
1333          * involved then they must be added to qpqual.  The upshot is that qpqual
1334          * must contain scan_clauses minus whatever appears in indexquals.
1335          *
1336          * This loop is similar to the comparable code in create_indexscan_plan(),
1337          * but with some differences because it has to compare the scan clauses to
1338          * stripped (no RestrictInfos) indexquals.  See comments there for more
1339          * info.
1340          *
1341          * In normal cases simple equal() checks will be enough to spot duplicate
1342          * clauses, so we try that first.  We next see if the scan clause is
1343          * redundant with any top-level indexqual by virtue of being generated
1344          * from the same EC.  After that, try predicate_implied_by().
1345          *
1346          * Unlike create_indexscan_plan(), we need take no special thought here
1347          * for partial index predicates; this is because the predicate conditions
1348          * are already listed in bitmapqualorig and indexquals.  Bitmap scans have
1349          * to do it that way because predicate conditions need to be rechecked if
1350          * the scan becomes lossy, so they have to be included in bitmapqualorig.
1351          */
1352         qpqual = NIL;
1353         foreach(l, scan_clauses)
1354         {
1355                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1356                 Node       *clause = (Node *) rinfo->clause;
1357
1358                 Assert(IsA(rinfo, RestrictInfo));
1359                 if (rinfo->pseudoconstant)
1360                         continue;                       /* we may drop pseudoconstants here */
1361                 if (list_member(indexquals, clause))
1362                         continue;                       /* simple duplicate */
1363                 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
1364                         continue;                       /* derived from same EquivalenceClass */
1365                 if (!contain_mutable_functions(clause))
1366                 {
1367                         List       *clausel = list_make1(clause);
1368
1369                         if (predicate_implied_by(clausel, indexquals))
1370                                 continue;               /* provably implied by indexquals */
1371                 }
1372                 qpqual = lappend(qpqual, rinfo);
1373         }
1374
1375         /* Sort clauses into best execution order */
1376         qpqual = order_qual_clauses(root, qpqual);
1377
1378         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1379         qpqual = extract_actual_clauses(qpqual, false);
1380
1381         /*
1382          * When dealing with special operators, we will at this point have
1383          * duplicate clauses in qpqual and bitmapqualorig.  We may as well drop
1384          * 'em from bitmapqualorig, since there's no point in making the tests
1385          * twice.
1386          */
1387         bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1388
1389         /*
1390          * We have to replace any outer-relation variables with nestloop params in
1391          * the qpqual and bitmapqualorig expressions.  (This was already done for
1392          * expressions attached to plan nodes in the bitmapqualplan tree.)
1393          */
1394         if (best_path->path.param_info)
1395         {
1396                 qpqual = (List *)
1397                         replace_nestloop_params(root, (Node *) qpqual);
1398                 bitmapqualorig = (List *)
1399                         replace_nestloop_params(root, (Node *) bitmapqualorig);
1400         }
1401
1402         /* Finally ready to build the plan node */
1403         scan_plan = make_bitmap_heapscan(tlist,
1404                                                                          qpqual,
1405                                                                          bitmapqualplan,
1406                                                                          bitmapqualorig,
1407                                                                          baserelid);
1408
1409         copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1410
1411         return scan_plan;
1412 }
1413
1414 /*
1415  * Given a bitmapqual tree, generate the Plan tree that implements it
1416  *
1417  * As byproducts, we also return in *qual and *indexqual the qual lists
1418  * (in implicit-AND form, without RestrictInfos) describing the original index
1419  * conditions and the generated indexqual conditions.  (These are the same in
1420  * simple cases, but when special index operators are involved, the former
1421  * list includes the special conditions while the latter includes the actual
1422  * indexable conditions derived from them.)  Both lists include partial-index
1423  * predicates, because we have to recheck predicates as well as index
1424  * conditions if the bitmap scan becomes lossy.
1425  *
1426  * In addition, we return a list of EquivalenceClass pointers for all the
1427  * top-level indexquals that were possibly-redundantly derived from ECs.
1428  * This allows removal of scan_clauses that are redundant with such quals.
1429  * (We do not attempt to detect such redundancies for quals that are within
1430  * OR subtrees.  This could be done in a less hacky way if we returned the
1431  * indexquals in RestrictInfo form, but that would be slower and still pretty
1432  * messy, since we'd have to build new RestrictInfos in many cases.)
1433  */
1434 static Plan *
1435 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1436                                           List **qual, List **indexqual, List **indexECs)
1437 {
1438         Plan       *plan;
1439
1440         if (IsA(bitmapqual, BitmapAndPath))
1441         {
1442                 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1443                 List       *subplans = NIL;
1444                 List       *subquals = NIL;
1445                 List       *subindexquals = NIL;
1446                 List       *subindexECs = NIL;
1447                 ListCell   *l;
1448
1449                 /*
1450                  * There may well be redundant quals among the subplans, since a
1451                  * top-level WHERE qual might have gotten used to form several
1452                  * different index quals.  We don't try exceedingly hard to eliminate
1453                  * redundancies, but we do eliminate obvious duplicates by using
1454                  * list_concat_unique.
1455                  */
1456                 foreach(l, apath->bitmapquals)
1457                 {
1458                         Plan       *subplan;
1459                         List       *subqual;
1460                         List       *subindexqual;
1461                         List       *subindexEC;
1462
1463                         subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1464                                                                                         &subqual, &subindexqual,
1465                                                                                         &subindexEC);
1466                         subplans = lappend(subplans, subplan);
1467                         subquals = list_concat_unique(subquals, subqual);
1468                         subindexquals = list_concat_unique(subindexquals, subindexqual);
1469                         /* Duplicates in indexECs aren't worth getting rid of */
1470                         subindexECs = list_concat(subindexECs, subindexEC);
1471                 }
1472                 plan = (Plan *) make_bitmap_and(subplans);
1473                 plan->startup_cost = apath->path.startup_cost;
1474                 plan->total_cost = apath->path.total_cost;
1475                 plan->plan_rows =
1476                         clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1477                 plan->plan_width = 0;   /* meaningless */
1478                 *qual = subquals;
1479                 *indexqual = subindexquals;
1480                 *indexECs = subindexECs;
1481         }
1482         else if (IsA(bitmapqual, BitmapOrPath))
1483         {
1484                 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1485                 List       *subplans = NIL;
1486                 List       *subquals = NIL;
1487                 List       *subindexquals = NIL;
1488                 bool            const_true_subqual = false;
1489                 bool            const_true_subindexqual = false;
1490                 ListCell   *l;
1491
1492                 /*
1493                  * Here, we only detect qual-free subplans.  A qual-free subplan would
1494                  * cause us to generate "... OR true ..."  which we may as well reduce
1495                  * to just "true".  We do not try to eliminate redundant subclauses
1496                  * because (a) it's not as likely as in the AND case, and (b) we might
1497                  * well be working with hundreds or even thousands of OR conditions,
1498                  * perhaps from a long IN list.  The performance of list_append_unique
1499                  * would be unacceptable.
1500                  */
1501                 foreach(l, opath->bitmapquals)
1502                 {
1503                         Plan       *subplan;
1504                         List       *subqual;
1505                         List       *subindexqual;
1506                         List       *subindexEC;
1507
1508                         subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1509                                                                                         &subqual, &subindexqual,
1510                                                                                         &subindexEC);
1511                         subplans = lappend(subplans, subplan);
1512                         if (subqual == NIL)
1513                                 const_true_subqual = true;
1514                         else if (!const_true_subqual)
1515                                 subquals = lappend(subquals,
1516                                                                    make_ands_explicit(subqual));
1517                         if (subindexqual == NIL)
1518                                 const_true_subindexqual = true;
1519                         else if (!const_true_subindexqual)
1520                                 subindexquals = lappend(subindexquals,
1521                                                                                 make_ands_explicit(subindexqual));
1522                 }
1523
1524                 /*
1525                  * In the presence of ScalarArrayOpExpr quals, we might have built
1526                  * BitmapOrPaths with just one subpath; don't add an OR step.
1527                  */
1528                 if (list_length(subplans) == 1)
1529                 {
1530                         plan = (Plan *) linitial(subplans);
1531                 }
1532                 else
1533                 {
1534                         plan = (Plan *) make_bitmap_or(subplans);
1535                         plan->startup_cost = opath->path.startup_cost;
1536                         plan->total_cost = opath->path.total_cost;
1537                         plan->plan_rows =
1538                                 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1539                         plan->plan_width = 0;           /* meaningless */
1540                 }
1541
1542                 /*
1543                  * If there were constant-TRUE subquals, the OR reduces to constant
1544                  * TRUE.  Also, avoid generating one-element ORs, which could happen
1545                  * due to redundancy elimination or ScalarArrayOpExpr quals.
1546                  */
1547                 if (const_true_subqual)
1548                         *qual = NIL;
1549                 else if (list_length(subquals) <= 1)
1550                         *qual = subquals;
1551                 else
1552                         *qual = list_make1(make_orclause(subquals));
1553                 if (const_true_subindexqual)
1554                         *indexqual = NIL;
1555                 else if (list_length(subindexquals) <= 1)
1556                         *indexqual = subindexquals;
1557                 else
1558                         *indexqual = list_make1(make_orclause(subindexquals));
1559                 *indexECs = NIL;
1560         }
1561         else if (IsA(bitmapqual, IndexPath))
1562         {
1563                 IndexPath  *ipath = (IndexPath *) bitmapqual;
1564                 IndexScan  *iscan;
1565                 List       *subindexECs;
1566                 ListCell   *l;
1567
1568                 /* Use the regular indexscan plan build machinery... */
1569                 iscan = (IndexScan *) create_indexscan_plan(root, ipath,
1570                                                                                                         NIL, NIL, false);
1571                 Assert(IsA(iscan, IndexScan));
1572                 /* then convert to a bitmap indexscan */
1573                 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1574                                                                                           iscan->indexid,
1575                                                                                           iscan->indexqual,
1576                                                                                           iscan->indexqualorig);
1577                 plan->startup_cost = 0.0;
1578                 plan->total_cost = ipath->indextotalcost;
1579                 plan->plan_rows =
1580                         clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1581                 plan->plan_width = 0;   /* meaningless */
1582                 *qual = get_actual_clauses(ipath->indexclauses);
1583                 *indexqual = get_actual_clauses(ipath->indexquals);
1584                 foreach(l, ipath->indexinfo->indpred)
1585                 {
1586                         Expr       *pred = (Expr *) lfirst(l);
1587
1588                         /*
1589                          * We know that the index predicate must have been implied by the
1590                          * query condition as a whole, but it may or may not be implied by
1591                          * the conditions that got pushed into the bitmapqual.  Avoid
1592                          * generating redundant conditions.
1593                          */
1594                         if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1595                         {
1596                                 *qual = lappend(*qual, pred);
1597                                 *indexqual = lappend(*indexqual, pred);
1598                         }
1599                 }
1600                 subindexECs = NIL;
1601                 foreach(l, ipath->indexquals)
1602                 {
1603                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1604
1605                         if (rinfo->parent_ec)
1606                                 subindexECs = lappend(subindexECs, rinfo->parent_ec);
1607                 }
1608                 *indexECs = subindexECs;
1609         }
1610         else
1611         {
1612                 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1613                 plan = NULL;                    /* keep compiler quiet */
1614         }
1615
1616         return plan;
1617 }
1618
1619 /*
1620  * create_tidscan_plan
1621  *       Returns a tidscan plan for the base relation scanned by 'best_path'
1622  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1623  */
1624 static TidScan *
1625 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1626                                         List *tlist, List *scan_clauses)
1627 {
1628         TidScan    *scan_plan;
1629         Index           scan_relid = best_path->path.parent->relid;
1630         List       *tidquals = best_path->tidquals;
1631         List       *ortidquals;
1632
1633         /* it should be a base rel... */
1634         Assert(scan_relid > 0);
1635         Assert(best_path->path.parent->rtekind == RTE_RELATION);
1636
1637         /* Sort clauses into best execution order */
1638         scan_clauses = order_qual_clauses(root, scan_clauses);
1639
1640         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1641         scan_clauses = extract_actual_clauses(scan_clauses, false);
1642
1643         /* Replace any outer-relation variables with nestloop params */
1644         if (best_path->path.param_info)
1645         {
1646                 tidquals = (List *)
1647                         replace_nestloop_params(root, (Node *) tidquals);
1648                 scan_clauses = (List *)
1649                         replace_nestloop_params(root, (Node *) scan_clauses);
1650         }
1651
1652         /*
1653          * Remove any clauses that are TID quals.  This is a bit tricky since the
1654          * tidquals list has implicit OR semantics.
1655          */
1656         ortidquals = tidquals;
1657         if (list_length(ortidquals) > 1)
1658                 ortidquals = list_make1(make_orclause(ortidquals));
1659         scan_clauses = list_difference(scan_clauses, ortidquals);
1660
1661         scan_plan = make_tidscan(tlist,
1662                                                          scan_clauses,
1663                                                          scan_relid,
1664                                                          tidquals);
1665
1666         copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1667
1668         return scan_plan;
1669 }
1670
1671 /*
1672  * create_subqueryscan_plan
1673  *       Returns a subqueryscan plan for the base relation scanned by 'best_path'
1674  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1675  */
1676 static SubqueryScan *
1677 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1678                                                  List *tlist, List *scan_clauses)
1679 {
1680         SubqueryScan *scan_plan;
1681         Index           scan_relid = best_path->parent->relid;
1682
1683         /* it should be a subquery base rel... */
1684         Assert(scan_relid > 0);
1685         Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1686
1687         /* Sort clauses into best execution order */
1688         scan_clauses = order_qual_clauses(root, scan_clauses);
1689
1690         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1691         scan_clauses = extract_actual_clauses(scan_clauses, false);
1692
1693         /* Replace any outer-relation variables with nestloop params */
1694         if (best_path->param_info)
1695         {
1696                 scan_clauses = (List *)
1697                         replace_nestloop_params(root, (Node *) scan_clauses);
1698                 process_subquery_nestloop_params(root,
1699                                                                                  best_path->parent->subplan_params);
1700         }
1701
1702         scan_plan = make_subqueryscan(tlist,
1703                                                                   scan_clauses,
1704                                                                   scan_relid,
1705                                                                   best_path->parent->subplan);
1706
1707         copy_path_costsize(&scan_plan->scan.plan, best_path);
1708
1709         return scan_plan;
1710 }
1711
1712 /*
1713  * create_functionscan_plan
1714  *       Returns a functionscan plan for the base relation scanned by 'best_path'
1715  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1716  */
1717 static FunctionScan *
1718 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1719                                                  List *tlist, List *scan_clauses)
1720 {
1721         FunctionScan *scan_plan;
1722         Index           scan_relid = best_path->parent->relid;
1723         RangeTblEntry *rte;
1724         List       *functions;
1725
1726         /* it should be a function base rel... */
1727         Assert(scan_relid > 0);
1728         rte = planner_rt_fetch(scan_relid, root);
1729         Assert(rte->rtekind == RTE_FUNCTION);
1730         functions = rte->functions;
1731
1732         /* Sort clauses into best execution order */
1733         scan_clauses = order_qual_clauses(root, scan_clauses);
1734
1735         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1736         scan_clauses = extract_actual_clauses(scan_clauses, false);
1737
1738         /* Replace any outer-relation variables with nestloop params */
1739         if (best_path->param_info)
1740         {
1741                 scan_clauses = (List *)
1742                         replace_nestloop_params(root, (Node *) scan_clauses);
1743                 /* The function expressions could contain nestloop params, too */
1744                 functions = (List *) replace_nestloop_params(root, (Node *) functions);
1745         }
1746
1747         scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1748                                                                   functions, rte->funcordinality);
1749
1750         copy_path_costsize(&scan_plan->scan.plan, best_path);
1751
1752         return scan_plan;
1753 }
1754
1755 /*
1756  * create_valuesscan_plan
1757  *       Returns a valuesscan plan for the base relation scanned by 'best_path'
1758  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1759  */
1760 static ValuesScan *
1761 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1762                                            List *tlist, List *scan_clauses)
1763 {
1764         ValuesScan *scan_plan;
1765         Index           scan_relid = best_path->parent->relid;
1766         RangeTblEntry *rte;
1767         List       *values_lists;
1768
1769         /* it should be a values base rel... */
1770         Assert(scan_relid > 0);
1771         rte = planner_rt_fetch(scan_relid, root);
1772         Assert(rte->rtekind == RTE_VALUES);
1773         values_lists = rte->values_lists;
1774
1775         /* Sort clauses into best execution order */
1776         scan_clauses = order_qual_clauses(root, scan_clauses);
1777
1778         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1779         scan_clauses = extract_actual_clauses(scan_clauses, false);
1780
1781         /* Replace any outer-relation variables with nestloop params */
1782         if (best_path->param_info)
1783         {
1784                 scan_clauses = (List *)
1785                         replace_nestloop_params(root, (Node *) scan_clauses);
1786                 /* The values lists could contain nestloop params, too */
1787                 values_lists = (List *)
1788                         replace_nestloop_params(root, (Node *) values_lists);
1789         }
1790
1791         scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1792                                                                 values_lists);
1793
1794         copy_path_costsize(&scan_plan->scan.plan, best_path);
1795
1796         return scan_plan;
1797 }
1798
1799 /*
1800  * create_ctescan_plan
1801  *       Returns a ctescan plan for the base relation scanned by 'best_path'
1802  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1803  */
1804 static CteScan *
1805 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1806                                         List *tlist, List *scan_clauses)
1807 {
1808         CteScan    *scan_plan;
1809         Index           scan_relid = best_path->parent->relid;
1810         RangeTblEntry *rte;
1811         SubPlan    *ctesplan = NULL;
1812         int                     plan_id;
1813         int                     cte_param_id;
1814         PlannerInfo *cteroot;
1815         Index           levelsup;
1816         int                     ndx;
1817         ListCell   *lc;
1818
1819         Assert(scan_relid > 0);
1820         rte = planner_rt_fetch(scan_relid, root);
1821         Assert(rte->rtekind == RTE_CTE);
1822         Assert(!rte->self_reference);
1823
1824         /*
1825          * Find the referenced CTE, and locate the SubPlan previously made for it.
1826          */
1827         levelsup = rte->ctelevelsup;
1828         cteroot = root;
1829         while (levelsup-- > 0)
1830         {
1831                 cteroot = cteroot->parent_root;
1832                 if (!cteroot)                   /* shouldn't happen */
1833                         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1834         }
1835
1836         /*
1837          * Note: cte_plan_ids can be shorter than cteList, if we are still working
1838          * on planning the CTEs (ie, this is a side-reference from another CTE).
1839          * So we mustn't use forboth here.
1840          */
1841         ndx = 0;
1842         foreach(lc, cteroot->parse->cteList)
1843         {
1844                 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1845
1846                 if (strcmp(cte->ctename, rte->ctename) == 0)
1847                         break;
1848                 ndx++;
1849         }
1850         if (lc == NULL)                         /* shouldn't happen */
1851                 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1852         if (ndx >= list_length(cteroot->cte_plan_ids))
1853                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1854         plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1855         Assert(plan_id > 0);
1856         foreach(lc, cteroot->init_plans)
1857         {
1858                 ctesplan = (SubPlan *) lfirst(lc);
1859                 if (ctesplan->plan_id == plan_id)
1860                         break;
1861         }
1862         if (lc == NULL)                         /* shouldn't happen */
1863                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1864
1865         /*
1866          * We need the CTE param ID, which is the sole member of the SubPlan's
1867          * setParam list.
1868          */
1869         cte_param_id = linitial_int(ctesplan->setParam);
1870
1871         /* Sort clauses into best execution order */
1872         scan_clauses = order_qual_clauses(root, scan_clauses);
1873
1874         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1875         scan_clauses = extract_actual_clauses(scan_clauses, false);
1876
1877         /* Replace any outer-relation variables with nestloop params */
1878         if (best_path->param_info)
1879         {
1880                 scan_clauses = (List *)
1881                         replace_nestloop_params(root, (Node *) scan_clauses);
1882         }
1883
1884         scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1885                                                          plan_id, cte_param_id);
1886
1887         copy_path_costsize(&scan_plan->scan.plan, best_path);
1888
1889         return scan_plan;
1890 }
1891
1892 /*
1893  * create_worktablescan_plan
1894  *       Returns a worktablescan plan for the base relation scanned by 'best_path'
1895  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1896  */
1897 static WorkTableScan *
1898 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1899                                                   List *tlist, List *scan_clauses)
1900 {
1901         WorkTableScan *scan_plan;
1902         Index           scan_relid = best_path->parent->relid;
1903         RangeTblEntry *rte;
1904         Index           levelsup;
1905         PlannerInfo *cteroot;
1906
1907         Assert(scan_relid > 0);
1908         rte = planner_rt_fetch(scan_relid, root);
1909         Assert(rte->rtekind == RTE_CTE);
1910         Assert(rte->self_reference);
1911
1912         /*
1913          * We need to find the worktable param ID, which is in the plan level
1914          * that's processing the recursive UNION, which is one level *below* where
1915          * the CTE comes from.
1916          */
1917         levelsup = rte->ctelevelsup;
1918         if (levelsup == 0)                      /* shouldn't happen */
1919                 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1920         levelsup--;
1921         cteroot = root;
1922         while (levelsup-- > 0)
1923         {
1924                 cteroot = cteroot->parent_root;
1925                 if (!cteroot)                   /* shouldn't happen */
1926                         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1927         }
1928         if (cteroot->wt_param_id < 0)           /* shouldn't happen */
1929                 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1930
1931         /* Sort clauses into best execution order */
1932         scan_clauses = order_qual_clauses(root, scan_clauses);
1933
1934         /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1935         scan_clauses = extract_actual_clauses(scan_clauses, false);
1936
1937         /* Replace any outer-relation variables with nestloop params */
1938         if (best_path->param_info)
1939         {
1940                 scan_clauses = (List *)
1941                         replace_nestloop_params(root, (Node *) scan_clauses);
1942         }
1943
1944         scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1945                                                                    cteroot->wt_param_id);
1946
1947         copy_path_costsize(&scan_plan->scan.plan, best_path);
1948
1949         return scan_plan;
1950 }
1951
1952 /*
1953  * create_foreignscan_plan
1954  *       Returns a foreignscan plan for the base relation scanned by 'best_path'
1955  *       with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1956  */
1957 static ForeignScan *
1958 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
1959                                                 List *tlist, List *scan_clauses)
1960 {
1961         ForeignScan *scan_plan;
1962         RelOptInfo *rel = best_path->path.parent;
1963         Index           scan_relid = rel->relid;
1964         RangeTblEntry *rte;
1965         Bitmapset  *attrs_used = NULL;
1966         ListCell   *lc;
1967         int                     i;
1968
1969         /* it should be a base rel... */
1970         Assert(scan_relid > 0);
1971         Assert(rel->rtekind == RTE_RELATION);
1972         rte = planner_rt_fetch(scan_relid, root);
1973         Assert(rte->rtekind == RTE_RELATION);
1974
1975         /*
1976          * Sort clauses into best execution order.  We do this first since the FDW
1977          * might have more info than we do and wish to adjust the ordering.
1978          */
1979         scan_clauses = order_qual_clauses(root, scan_clauses);
1980
1981         /*
1982          * Let the FDW perform its processing on the restriction clauses and
1983          * generate the plan node.  Note that the FDW might remove restriction
1984          * clauses that it intends to execute remotely, or even add more (if it
1985          * has selected some join clauses for remote use but also wants them
1986          * rechecked locally).
1987          */
1988         scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rte->relid,
1989                                                                                                 best_path,
1990                                                                                                 tlist, scan_clauses);
1991
1992         /* Copy cost data from Path to Plan; no need to make FDW do this */
1993         copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1994
1995         /*
1996          * Replace any outer-relation variables with nestloop params in the qual
1997          * and fdw_exprs expressions.  We do this last so that the FDW doesn't
1998          * have to be involved.  (Note that parts of fdw_exprs could have come
1999          * from join clauses, so doing this beforehand on the scan_clauses
2000          * wouldn't work.)
2001          */
2002         if (best_path->path.param_info)
2003         {
2004                 scan_plan->scan.plan.qual = (List *)
2005                         replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
2006                 scan_plan->fdw_exprs = (List *)
2007                         replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
2008         }
2009
2010         /*
2011          * Detect whether any system columns are requested from rel.  This is a
2012          * bit of a kluge and might go away someday, so we intentionally leave it
2013          * out of the API presented to FDWs.
2014          *
2015          * First, examine all the attributes needed for joins or final output.
2016          * Note: we must look at reltargetlist, not the attr_needed data, because
2017          * attr_needed isn't computed for inheritance child rels.
2018          */
2019         pull_varattnos((Node *) rel->reltargetlist, rel->relid, &attrs_used);
2020
2021         /* Add all the attributes used by restriction clauses. */
2022         foreach(lc, rel->baserestrictinfo)
2023         {
2024                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2025
2026                 pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
2027         }
2028
2029         /* Now, are any system columns requested from rel? */
2030         scan_plan->fsSystemCol = false;
2031         for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
2032         {
2033                 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
2034                 {
2035                         scan_plan->fsSystemCol = true;
2036                         break;
2037                 }
2038         }
2039
2040         bms_free(attrs_used);
2041
2042         return scan_plan;
2043 }
2044
2045 /*
2046  * create_custom_plan
2047  *
2048  * Transform a CustomPath into a Plan.
2049  */
2050 static CustomScan *
2051 create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
2052                                            List *tlist, List *scan_clauses)
2053 {
2054         CustomScan *cplan;
2055         RelOptInfo *rel = best_path->path.parent;
2056
2057         /*
2058          * Right now, all we can support is CustomScan node which is associated
2059          * with a particular base relation to be scanned.
2060          */
2061         Assert(rel && rel->reloptkind == RELOPT_BASEREL);
2062
2063         /*
2064          * Sort clauses into the best execution order, although custom-scan
2065          * provider can reorder them again.
2066          */
2067         scan_clauses = order_qual_clauses(root, scan_clauses);
2068
2069         /*
2070          * Invoke custom plan provider to create the Plan node represented by the
2071          * CustomPath.
2072          */
2073         cplan = (CustomScan *) best_path->methods->PlanCustomPath(root,
2074                                                                                                                           rel,
2075                                                                                                                           best_path,
2076                                                                                                                           tlist,
2077                                                                                                                           scan_clauses);
2078         Assert(IsA(cplan, CustomScan));
2079
2080         /*
2081          * Copy cost data from Path to Plan; no need to make custom-plan providers
2082          * do this
2083          */
2084         copy_path_costsize(&cplan->scan.plan, &best_path->path);
2085
2086         /*
2087          * Replace any outer-relation variables with nestloop params in the qual
2088          * and custom_exprs expressions.  We do this last so that the custom-plan
2089          * provider doesn't have to be involved.  (Note that parts of custom_exprs
2090          * could have come from join clauses, so doing this beforehand on the
2091          * scan_clauses wouldn't work.)
2092          */
2093         if (best_path->path.param_info)
2094         {
2095                 cplan->scan.plan.qual = (List *)
2096                         replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
2097                 cplan->custom_exprs = (List *)
2098                         replace_nestloop_params(root, (Node *) cplan->custom_exprs);
2099         }
2100
2101         return cplan;
2102 }
2103
2104
2105 /*****************************************************************************
2106  *
2107  *      JOIN METHODS
2108  *
2109  *****************************************************************************/
2110
2111 static NestLoop *
2112 create_nestloop_plan(PlannerInfo *root,
2113                                          NestPath *best_path,
2114                                          Plan *outer_plan,
2115                                          Plan *inner_plan)
2116 {
2117         NestLoop   *join_plan;
2118         List       *tlist = build_path_tlist(root, &best_path->path);
2119         List       *joinrestrictclauses = best_path->joinrestrictinfo;
2120         List       *joinclauses;
2121         List       *otherclauses;
2122         Relids          outerrelids;
2123         List       *nestParams;
2124         ListCell   *cell;
2125         ListCell   *prev;
2126         ListCell   *next;
2127
2128         /* Sort join qual clauses into best execution order */
2129         joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
2130
2131         /* Get the join qual clauses (in plain expression form) */
2132         /* Any pseudoconstant clauses are ignored here */
2133         if (IS_OUTER_JOIN(best_path->jointype))
2134         {
2135                 extract_actual_join_clauses(joinrestrictclauses,
2136                                                                         &joinclauses, &otherclauses);
2137         }
2138         else
2139         {
2140                 /* We can treat all clauses alike for an inner join */
2141                 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
2142                 otherclauses = NIL;
2143         }
2144
2145         /* Replace any outer-relation variables with nestloop params */
2146         if (best_path->path.param_info)
2147         {
2148                 joinclauses = (List *)
2149                         replace_nestloop_params(root, (Node *) joinclauses);
2150                 otherclauses = (List *)
2151                         replace_nestloop_params(root, (Node *) otherclauses);
2152         }
2153
2154         /*
2155          * Identify any nestloop parameters that should be supplied by this join
2156          * node, and move them from root->curOuterParams to the nestParams list.
2157          */
2158         outerrelids = best_path->outerjoinpath->parent->relids;
2159         nestParams = NIL;
2160         prev = NULL;
2161         for (cell = list_head(root->curOuterParams); cell; cell = next)
2162         {
2163                 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
2164
2165                 next = lnext(cell);
2166                 if (IsA(nlp->paramval, Var) &&
2167                         bms_is_member(nlp->paramval->varno, outerrelids))
2168                 {
2169                         root->curOuterParams = list_delete_cell(root->curOuterParams,
2170                                                                                                         cell, prev);
2171                         nestParams = lappend(nestParams, nlp);
2172                 }
2173                 else if (IsA(nlp->paramval, PlaceHolderVar) &&
2174                                  bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
2175                                                          outerrelids) &&
2176                                  bms_is_subset(find_placeholder_info(root,
2177                                                                                         (PlaceHolderVar *) nlp->paramval,
2178                                                                                                          false)->ph_eval_at,
2179                                                            outerrelids))
2180                 {
2181                         root->curOuterParams = list_delete_cell(root->curOuterParams,
2182                                                                                                         cell, prev);
2183                         nestParams = lappend(nestParams, nlp);
2184                 }
2185                 else
2186                         prev = cell;
2187         }
2188
2189         join_plan = make_nestloop(tlist,
2190                                                           joinclauses,
2191                                                           otherclauses,
2192                                                           nestParams,
2193                                                           outer_plan,
2194                                                           inner_plan,
2195                                                           best_path->jointype);
2196
2197         copy_path_costsize(&join_plan->join.plan, &best_path->path);
2198
2199         return join_plan;
2200 }
2201
2202 static MergeJoin *
2203 create_mergejoin_plan(PlannerInfo *root,
2204                                           MergePath *best_path,
2205                                           Plan *outer_plan,
2206                                           Plan *inner_plan)
2207 {
2208         List       *tlist = build_path_tlist(root, &best_path->jpath.path);
2209         List       *joinclauses;
2210         List       *otherclauses;
2211         List       *mergeclauses;
2212         List       *outerpathkeys;
2213         List       *innerpathkeys;
2214         int                     nClauses;
2215         Oid                *mergefamilies;
2216         Oid                *mergecollations;
2217         int                *mergestrategies;
2218         bool       *mergenullsfirst;
2219         MergeJoin  *join_plan;
2220         int                     i;
2221         ListCell   *lc;
2222         ListCell   *lop;
2223         ListCell   *lip;
2224
2225         /* Sort join qual clauses into best execution order */
2226         /* NB: do NOT reorder the mergeclauses */
2227         joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2228
2229         /* Get the join qual clauses (in plain expression form) */
2230         /* Any pseudoconstant clauses are ignored here */
2231         if (IS_OUTER_JOIN(best_path->jpath.jointype))
2232         {
2233                 extract_actual_join_clauses(joinclauses,
2234                                                                         &joinclauses, &otherclauses);
2235         }
2236         else
2237         {
2238                 /* We can treat all clauses alike for an inner join */
2239                 joinclauses = extract_actual_clauses(joinclauses, false);
2240                 otherclauses = NIL;
2241         }
2242
2243         /*
2244          * Remove the mergeclauses from the list of join qual clauses, leaving the
2245          * list of quals that must be checked as qpquals.
2246          */
2247         mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
2248         joinclauses = list_difference(joinclauses, mergeclauses);
2249
2250         /*
2251          * Replace any outer-relation variables with nestloop params.  There
2252          * should not be any in the mergeclauses.
2253          */
2254         if (best_path->jpath.path.param_info)
2255         {
2256                 joinclauses = (List *)
2257                         replace_nestloop_params(root, (Node *) joinclauses);
2258                 otherclauses = (List *)
2259                         replace_nestloop_params(root, (Node *) otherclauses);
2260         }
2261
2262         /*
2263          * Rearrange mergeclauses, if needed, so that the outer variable is always
2264          * on the left; mark the mergeclause restrictinfos with correct
2265          * outer_is_left status.
2266          */
2267         mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
2268                                                          best_path->jpath.outerjoinpath->parent->relids);
2269
2270         /*
2271          * Create explicit sort nodes for the outer and inner paths if necessary.
2272          * Make sure there are no excess columns in the inputs if sorting.
2273          */
2274         if (best_path->outersortkeys)
2275         {
2276                 disuse_physical_tlist(root, outer_plan, best_path->jpath.outerjoinpath);
2277                 outer_plan = (Plan *)
2278                         make_sort_from_pathkeys(root,
2279                                                                         outer_plan,
2280                                                                         best_path->outersortkeys,
2281                                                                         -1.0);
2282                 outerpathkeys = best_path->outersortkeys;
2283         }
2284         else
2285                 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
2286
2287         if (best_path->innersortkeys)
2288         {
2289                 disuse_physical_tlist(root, inner_plan, best_path->jpath.innerjoinpath);
2290                 inner_plan = (Plan *)
2291                         make_sort_from_pathkeys(root,
2292                                                                         inner_plan,
2293                                                                         best_path->innersortkeys,
2294                                                                         -1.0);
2295                 innerpathkeys = best_path->innersortkeys;
2296         }
2297         else
2298                 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
2299
2300         /*
2301          * If specified, add a materialize node to shield the inner plan from the
2302          * need to handle mark/restore.
2303          */
2304         if (best_path->materialize_inner)
2305         {
2306                 Plan       *matplan = (Plan *) make_material(inner_plan);
2307
2308                 /*
2309                  * We assume the materialize will not spill to disk, and therefore
2310                  * charge just cpu_operator_cost per tuple.  (Keep this estimate in
2311                  * sync with final_cost_mergejoin.)
2312                  */
2313                 copy_plan_costsize(matplan, inner_plan);
2314                 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
2315
2316                 inner_plan = matplan;
2317         }
2318
2319         /*
2320          * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
2321          * executor.  The information is in the pathkeys for the two inputs, but
2322          * we need to be careful about the possibility of mergeclauses sharing a
2323          * pathkey (compare find_mergeclauses_for_pathkeys()).
2324          */
2325         nClauses = list_length(mergeclauses);
2326         Assert(nClauses == list_length(best_path->path_mergeclauses));
2327         mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
2328         mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
2329         mergestrategies = (int *) palloc(nClauses * sizeof(int));
2330         mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
2331
2332         lop = list_head(outerpathkeys);
2333         lip = list_head(innerpathkeys);
2334         i = 0;
2335         foreach(lc, best_path->path_mergeclauses)
2336         {
2337                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2338                 EquivalenceClass *oeclass;
2339                 EquivalenceClass *ieclass;
2340                 PathKey    *opathkey;
2341                 PathKey    *ipathkey;
2342                 EquivalenceClass *opeclass;
2343                 EquivalenceClass *ipeclass;
2344                 ListCell   *l2;
2345
2346                 /* fetch outer/inner eclass from mergeclause */
2347                 Assert(IsA(rinfo, RestrictInfo));
2348                 if (rinfo->outer_is_left)
2349                 {
2350                         oeclass = rinfo->left_ec;
2351                         ieclass = rinfo->right_ec;
2352                 }
2353                 else
2354                 {
2355                         oeclass = rinfo->right_ec;
2356                         ieclass = rinfo->left_ec;
2357                 }
2358                 Assert(oeclass != NULL);
2359                 Assert(ieclass != NULL);
2360
2361                 /*
2362                  * For debugging purposes, we check that the eclasses match the paths'
2363                  * pathkeys.  In typical cases the merge clauses are one-to-one with
2364                  * the pathkeys, but when dealing with partially redundant query
2365                  * conditions, we might have clauses that re-reference earlier path
2366                  * keys.  The case that we need to reject is where a pathkey is
2367                  * entirely skipped over.
2368                  *
2369                  * lop and lip reference the first as-yet-unused pathkey elements;
2370                  * it's okay to match them, or any element before them.  If they're
2371                  * NULL then we have found all pathkey elements to be used.
2372                  */
2373                 if (lop)
2374                 {
2375                         opathkey = (PathKey *) lfirst(lop);
2376                         opeclass = opathkey->pk_eclass;
2377                         if (oeclass == opeclass)
2378                         {
2379                                 /* fast path for typical case */
2380                                 lop = lnext(lop);
2381                         }
2382                         else
2383                         {
2384                                 /* redundant clauses ... must match something before lop */
2385                                 foreach(l2, outerpathkeys)
2386                                 {
2387                                         if (l2 == lop)
2388                                                 break;
2389                                         opathkey = (PathKey *) lfirst(l2);
2390                                         opeclass = opathkey->pk_eclass;
2391                                         if (oeclass == opeclass)
2392                                                 break;
2393                                 }
2394                                 if (oeclass != opeclass)
2395                                         elog(ERROR, "outer pathkeys do not match mergeclauses");
2396                         }
2397                 }
2398                 else
2399                 {
2400                         /* redundant clauses ... must match some already-used pathkey */
2401                         opathkey = NULL;
2402                         opeclass = NULL;
2403                         foreach(l2, outerpathkeys)
2404                         {
2405                                 opathkey = (PathKey *) lfirst(l2);
2406                                 opeclass = opathkey->pk_eclass;
2407                                 if (oeclass == opeclass)
2408                                         break;
2409                         }
2410                         if (l2 == NULL)
2411                                 elog(ERROR, "outer pathkeys do not match mergeclauses");
2412                 }
2413
2414                 if (lip)
2415                 {
2416                         ipathkey = (PathKey *) lfirst(lip);
2417                         ipeclass = ipathkey->pk_eclass;
2418                         if (ieclass == ipeclass)
2419                         {
2420                                 /* fast path for typical case */
2421                                 lip = lnext(lip);
2422                         }
2423                         else
2424                         {
2425                                 /* redundant clauses ... must match something before lip */
2426                                 foreach(l2, innerpathkeys)
2427                                 {
2428                                         if (l2 == lip)
2429                                                 break;
2430                                         ipathkey = (PathKey *) lfirst(l2);
2431                                         ipeclass = ipathkey->pk_eclass;
2432                                         if (ieclass == ipeclass)
2433                                                 break;
2434                                 }
2435                                 if (ieclass != ipeclass)
2436                                         elog(ERROR, "inner pathkeys do not match mergeclauses");
2437                         }
2438                 }
2439                 else
2440                 {
2441                         /* redundant clauses ... must match some already-used pathkey */
2442                         ipathkey = NULL;
2443                         ipeclass = NULL;
2444                         foreach(l2, innerpathkeys)
2445                         {
2446                                 ipathkey = (PathKey *) lfirst(l2);
2447                                 ipeclass = ipathkey->pk_eclass;
2448                                 if (ieclass == ipeclass)
2449                                         break;
2450                         }
2451                         if (l2 == NULL)
2452                                 elog(ERROR, "inner pathkeys do not match mergeclauses");
2453                 }
2454
2455                 /* pathkeys should match each other too (more debugging) */
2456                 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2457                         opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
2458                         opathkey->pk_strategy != ipathkey->pk_strategy ||
2459                         opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2460                         elog(ERROR, "left and right pathkeys do not match in mergejoin");
2461
2462                 /* OK, save info for executor */
2463                 mergefamilies[i] = opathkey->pk_opfamily;
2464                 mergecollations[i] = opathkey->pk_eclass->ec_collation;
2465                 mergestrategies[i] = opathkey->pk_strategy;
2466                 mergenullsfirst[i] = opathkey->pk_nulls_first;
2467                 i++;
2468         }
2469
2470         /*
2471          * Note: it is not an error if we have additional pathkey elements (i.e.,
2472          * lop or lip isn't NULL here).  The input paths might be better-sorted
2473          * than we need for the current mergejoin.
2474          */
2475
2476         /*
2477          * Now we can build the mergejoin node.
2478          */
2479         join_plan = make_mergejoin(tlist,
2480                                                            joinclauses,
2481                                                            otherclauses,
2482                                                            mergeclauses,
2483                                                            mergefamilies,
2484                                                            mergecollations,
2485                                                            mergestrategies,
2486                                                            mergenullsfirst,
2487                                                            outer_plan,
2488                                                            inner_plan,
2489                                                            best_path->jpath.jointype);
2490
2491         /* Costs of sort and material steps are included in path cost already */
2492         copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2493
2494         return join_plan;
2495 }
2496
2497 static HashJoin *
2498 create_hashjoin_plan(PlannerInfo *root,
2499                                          HashPath *best_path,
2500                                          Plan *outer_plan,
2501                                          Plan *inner_plan)
2502 {
2503         List       *tlist = build_path_tlist(root, &best_path->jpath.path);
2504         List       *joinclauses;
2505         List       *otherclauses;
2506         List       *hashclauses;
2507         Oid                     skewTable = InvalidOid;
2508         AttrNumber      skewColumn = InvalidAttrNumber;
2509         bool            skewInherit = false;
2510         Oid                     skewColType = InvalidOid;
2511         int32           skewColTypmod = -1;
2512         HashJoin   *join_plan;
2513         Hash       *hash_plan;
2514
2515         /* Sort join qual clauses into best execution order */
2516         joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2517         /* There's no point in sorting the hash clauses ... */
2518
2519         /* Get the join qual clauses (in plain expression form) */
2520         /* Any pseudoconstant clauses are ignored here */
2521         if (IS_OUTER_JOIN(best_path->jpath.jointype))
2522         {
2523                 extract_actual_join_clauses(joinclauses,
2524                                                                         &joinclauses, &otherclauses);
2525         }
2526         else
2527         {
2528                 /* We can treat all clauses alike for an inner join */
2529                 joinclauses = extract_actual_clauses(joinclauses, false);
2530                 otherclauses = NIL;
2531         }
2532
2533         /*
2534          * Remove the hashclauses from the list of join qual clauses, leaving the
2535          * list of quals that must be checked as qpquals.
2536          */
2537         hashclauses = get_actual_clauses(best_path->path_hashclauses);
2538         joinclauses = list_difference(joinclauses, hashclauses);
2539
2540         /*
2541          * Replace any outer-relation variables with nestloop params.  There
2542          * should not be any in the hashclauses.
2543          */
2544         if (best_path->jpath.path.param_info)
2545         {
2546                 joinclauses = (List *)
2547                         replace_nestloop_params(root, (Node *) joinclauses);
2548                 otherclauses = (List *)
2549                         replace_nestloop_params(root, (Node *) otherclauses);
2550         }
2551
2552         /*
2553          * Rearrange hashclauses, if needed, so that the outer variable is always
2554          * on the left.
2555          */
2556         hashclauses = get_switched_clauses(best_path->path_hashclauses,
2557                                                          best_path->jpath.outerjoinpath->parent->relids);
2558
2559         /* We don't want any excess columns in the hashed tuples */
2560         disuse_physical_tlist(root, inner_plan, best_path->jpath.innerjoinpath);
2561
2562         /* If we expect batching, suppress excess columns in outer tuples too */
2563         if (best_path->num_batches > 1)
2564                 disuse_physical_tlist(root, outer_plan, best_path->jpath.outerjoinpath);
2565
2566         /*
2567          * If there is a single join clause and we can identify the outer variable
2568          * as a simple column reference, supply its identity for possible use in
2569          * skew optimization.  (Note: in principle we could do skew optimization
2570          * with multiple join clauses, but we'd have to be able to determine the
2571          * most common combinations of outer values, which we don't currently have
2572          * enough stats for.)
2573          */
2574         if (list_length(hashclauses) == 1)
2575         {
2576                 OpExpr     *clause = (OpExpr *) linitial(hashclauses);
2577                 Node       *node;
2578
2579                 Assert(is_opclause(clause));
2580                 node = (Node *) linitial(clause->args);
2581                 if (IsA(node, RelabelType))
2582                         node = (Node *) ((RelabelType *) node)->arg;
2583                 if (IsA(node, Var))
2584                 {
2585                         Var                *var = (Var *) node;
2586                         RangeTblEntry *rte;
2587
2588                         rte = root->simple_rte_array[var->varno];
2589                         if (rte->rtekind == RTE_RELATION)
2590                         {
2591                                 skewTable = rte->relid;
2592                                 skewColumn = var->varattno;
2593                                 skewInherit = rte->inh;
2594                                 skewColType = var->vartype;
2595                                 skewColTypmod = var->vartypmod;
2596                         }
2597                 }
2598         }
2599
2600         /*
2601          * Build the hash node and hash join node.
2602          */
2603         hash_plan = make_hash(inner_plan,
2604                                                   skewTable,
2605                                                   skewColumn,
2606                                                   skewInherit,
2607                                                   skewColType,
2608                                                   skewColTypmod);
2609         join_plan = make_hashjoin(tlist,
2610                                                           joinclauses,
2611                                                           otherclauses,
2612                                                           hashclauses,
2613                                                           outer_plan,
2614                                                           (Plan *) hash_plan,
2615                                                           best_path->jpath.jointype);
2616
2617         copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2618
2619         return join_plan;
2620 }
2621
2622
2623 /*****************************************************************************
2624  *
2625  *      SUPPORTING ROUTINES
2626  *
2627  *****************************************************************************/
2628
2629 /*
2630  * replace_nestloop_params
2631  *        Replace outer-relation Vars and PlaceHolderVars in the given expression
2632  *        with nestloop Params
2633  *
2634  * All Vars and PlaceHolderVars belonging to the relation(s) identified by
2635  * root->curOuterRels are replaced by Params, and entries are added to
2636  * root->curOuterParams if not already present.
2637  */
2638 static Node *
2639 replace_nestloop_params(PlannerInfo *root, Node *expr)
2640 {
2641         /* No setup needed for tree walk, so away we go */
2642         return replace_nestloop_params_mutator(expr, root);
2643 }
2644
2645 static Node *
2646 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2647 {
2648         if (node == NULL)
2649                 return NULL;
2650         if (IsA(node, Var))
2651         {
2652                 Var                *var = (Var *) node;
2653                 Param      *param;
2654                 NestLoopParam *nlp;
2655                 ListCell   *lc;
2656
2657                 /* Upper-level Vars should be long gone at this point */
2658                 Assert(var->varlevelsup == 0);
2659                 /* If not to be replaced, we can just return the Var unmodified */
2660                 if (!bms_is_member(var->varno, root->curOuterRels))
2661                         return node;
2662                 /* Create a Param representing the Var */
2663                 param = assign_nestloop_param_var(root, var);
2664                 /* Is this param already listed in root->curOuterParams? */
2665                 foreach(lc, root->curOuterParams)
2666                 {
2667                         nlp = (NestLoopParam *) lfirst(lc);
2668                         if (nlp->paramno == param->paramid)
2669                         {
2670                                 Assert(equal(var, nlp->paramval));
2671                                 /* Present, so we can just return the Param */
2672                                 return (Node *) param;
2673                         }
2674                 }
2675                 /* No, so add it */
2676                 nlp = makeNode(NestLoopParam);
2677                 nlp->paramno = param->paramid;
2678                 nlp->paramval = var;
2679                 root->curOuterParams = lappend(root->curOuterParams, nlp);
2680                 /* And return the replacement Param */
2681                 return (Node *) param;
2682         }
2683         if (IsA(node, PlaceHolderVar))
2684         {
2685                 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2686                 Param      *param;
2687                 NestLoopParam *nlp;
2688                 ListCell   *lc;
2689
2690                 /* Upper-level PlaceHolderVars should be long gone at this point */
2691                 Assert(phv->phlevelsup == 0);
2692
2693                 /*
2694                  * Check whether we need to replace the PHV.  We use bms_overlap as a
2695                  * cheap/quick test to see if the PHV might be evaluated in the outer
2696                  * rels, and then grab its PlaceHolderInfo to tell for sure.
2697                  */
2698                 if (!bms_overlap(phv->phrels, root->curOuterRels) ||
2699                   !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2700                                                  root->curOuterRels))
2701                 {
2702                         /*
2703                          * We can't replace the whole PHV, but we might still need to
2704                          * replace Vars or PHVs within its expression, in case it ends up
2705                          * actually getting evaluated here.  (It might get evaluated in
2706                          * this plan node, or some child node; in the latter case we don't
2707                          * really need to process the expression here, but we haven't got
2708                          * enough info to tell if that's the case.)  Flat-copy the PHV
2709                          * node and then recurse on its expression.
2710                          *
2711                          * Note that after doing this, we might have different
2712                          * representations of the contents of the same PHV in different
2713                          * parts of the plan tree.  This is OK because equal() will just
2714                          * match on phid/phlevelsup, so setrefs.c will still recognize an
2715                          * upper-level reference to a lower-level copy of the same PHV.
2716                          */
2717                         PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
2718
2719                         memcpy(newphv, phv, sizeof(PlaceHolderVar));
2720                         newphv->phexpr = (Expr *)
2721                                 replace_nestloop_params_mutator((Node *) phv->phexpr,
2722                                                                                                 root);
2723                         return (Node *) newphv;
2724                 }
2725                 /* Create a Param representing the PlaceHolderVar */
2726                 param = assign_nestloop_param_placeholdervar(root, phv);
2727                 /* Is this param already listed in root->curOuterParams? */
2728                 foreach(lc, root->curOuterParams)
2729                 {
2730                         nlp = (NestLoopParam *) lfirst(lc);
2731                         if (nlp->paramno == param->paramid)
2732                         {
2733                                 Assert(equal(phv, nlp->paramval));
2734                                 /* Present, so we can just return the Param */
2735                                 return (Node *) param;
2736                         }
2737                 }
2738                 /* No, so add it */
2739                 nlp = makeNode(NestLoopParam);
2740                 nlp->paramno = param->paramid;
2741                 nlp->paramval = (Var *) phv;
2742                 root->curOuterParams = lappend(root->curOuterParams, nlp);
2743                 /* And return the replacement Param */
2744                 return (Node *) param;
2745         }
2746         return expression_tree_mutator(node,
2747                                                                    replace_nestloop_params_mutator,
2748                                                                    (void *) root);
2749 }
2750
2751 /*
2752  * process_subquery_nestloop_params
2753  *        Handle params of a parameterized subquery that need to be fed
2754  *        from an outer nestloop.
2755  *
2756  * Currently, that would be *all* params that a subquery in FROM has demanded
2757  * from the current query level, since they must be LATERAL references.
2758  *
2759  * The subplan's references to the outer variables are already represented
2760  * as PARAM_EXEC Params, so we need not modify the subplan here.  What we
2761  * do need to do is add entries to root->curOuterParams to signal the parent
2762  * nestloop plan node that it must provide these values.
2763  */
2764 static void
2765 process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
2766 {
2767         ListCell   *ppl;
2768
2769         foreach(ppl, subplan_params)
2770         {
2771                 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(ppl);
2772
2773                 if (IsA(pitem->item, Var))
2774                 {
2775                         Var                *var = (Var *) pitem->item;
2776                         NestLoopParam *nlp;
2777                         ListCell   *lc;
2778
2779                         /* If not from a nestloop outer rel, complain */
2780                         if (!bms_is_member(var->varno, root->curOuterRels))
2781                                 elog(ERROR, "non-LATERAL parameter required by subquery");
2782                         /* Is this param already listed in root->curOuterParams? */
2783                         foreach(lc, root->curOuterParams)
2784                         {
2785                                 nlp = (NestLoopParam *) lfirst(lc);
2786                                 if (nlp->paramno == pitem->paramId)
2787                                 {
2788                                         Assert(equal(var, nlp->paramval));
2789                                         /* Present, so nothing to do */
2790                                         break;
2791                                 }
2792                         }
2793                         if (lc == NULL)
2794                         {
2795                                 /* No, so add it */
2796                                 nlp = makeNode(NestLoopParam);
2797                                 nlp->paramno = pitem->paramId;
2798                                 nlp->paramval = copyObject(var);
2799                                 root->curOuterParams = lappend(root->curOuterParams, nlp);
2800                         }
2801                 }
2802                 else if (IsA(pitem->item, PlaceHolderVar))
2803                 {
2804                         PlaceHolderVar *phv = (PlaceHolderVar *) pitem->item;
2805                         NestLoopParam *nlp;
2806                         ListCell   *lc;
2807
2808                         /* If not from a nestloop outer rel, complain */
2809                         if (!bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2810                                                            root->curOuterRels))
2811                                 elog(ERROR, "non-LATERAL parameter required by subquery");
2812                         /* Is this param already listed in root->curOuterParams? */
2813                         foreach(lc, root->curOuterParams)
2814                         {
2815                                 nlp = (NestLoopParam *) lfirst(lc);
2816                                 if (nlp->paramno == pitem->paramId)
2817                                 {
2818                                         Assert(equal(phv, nlp->paramval));
2819                                         /* Present, so nothing to do */
2820                                         break;
2821                                 }
2822                         }
2823                         if (lc == NULL)
2824                         {
2825                                 /* No, so add it */
2826                                 nlp = makeNode(NestLoopParam);
2827                                 nlp->paramno = pitem->paramId;
2828                                 nlp->paramval = copyObject(phv);
2829                                 root->curOuterParams = lappend(root->curOuterParams, nlp);
2830                         }
2831                 }
2832                 else
2833                         elog(ERROR, "unexpected type of subquery parameter");
2834         }
2835 }
2836
2837 /*
2838  * fix_indexqual_references
2839  *        Adjust indexqual clauses to the form the executor's indexqual
2840  *        machinery needs.
2841  *
2842  * We have four tasks here:
2843  *      * Remove RestrictInfo nodes from the input clauses.
2844  *      * Replace any outer-relation Var or PHV nodes with nestloop Params.
2845  *        (XXX eventually, that responsibility should go elsewhere?)
2846  *      * Index keys must be represented by Var nodes with varattno set to the
2847  *        index's attribute number, not the attribute number in the original rel.
2848  *      * If the index key is on the right, commute the clause to put it on the
2849  *        left.
2850  *
2851  * The result is a modified copy of the path's indexquals list --- the
2852  * original is not changed.  Note also that the copy shares no substructure
2853  * with the original; this is needed in case there is a subplan in it (we need
2854  * two separate copies of the subplan tree, or things will go awry).
2855  */
2856 static List *
2857 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
2858 {
2859         IndexOptInfo *index = index_path->indexinfo;
2860         List       *fixed_indexquals;
2861         ListCell   *lcc,
2862                            *lci;
2863
2864         fixed_indexquals = NIL;
2865
2866         forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
2867         {
2868                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
2869                 int                     indexcol = lfirst_int(lci);
2870                 Node       *clause;
2871
2872                 Assert(IsA(rinfo, RestrictInfo));
2873
2874                 /*
2875                  * Replace any outer-relation variables with nestloop params.
2876                  *
2877                  * This also makes a copy of the clause, so it's safe to modify it
2878                  * in-place below.
2879                  */
2880                 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2881
2882                 if (IsA(clause, OpExpr))
2883                 {
2884                         OpExpr     *op = (OpExpr *) clause;
2885
2886                         if (list_length(op->args) != 2)
2887                                 elog(ERROR, "indexqual clause is not binary opclause");
2888
2889                         /*
2890                          * Check to see if the indexkey is on the right; if so, commute
2891                          * the clause.  The indexkey should be the side that refers to
2892                          * (only) the base relation.
2893                          */
2894                         if (!bms_equal(rinfo->left_relids, index->rel->relids))
2895                                 CommuteOpExpr(op);
2896
2897                         /*
2898                          * Now replace the indexkey expression with an index Var.
2899                          */
2900                         linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2901                                                                                                            index,
2902                                                                                                            indexcol);
2903                 }
2904                 else if (IsA(clause, RowCompareExpr))
2905                 {
2906                         RowCompareExpr *rc = (RowCompareExpr *) clause;
2907                         Expr       *newrc;
2908                         List       *indexcolnos;
2909                         bool            var_on_left;
2910                         ListCell   *lca,
2911                                            *lcai;
2912
2913                         /*
2914                          * Re-discover which index columns are used in the rowcompare.
2915                          */
2916                         newrc = adjust_rowcompare_for_index(rc,
2917                                                                                                 index,
2918                                                                                                 indexcol,
2919                                                                                                 &indexcolnos,
2920                                                                                                 &var_on_left);
2921
2922                         /*
2923                          * Trouble if adjust_rowcompare_for_index thought the
2924                          * RowCompareExpr didn't match the index as-is; the clause should
2925                          * have gone through that routine already.
2926                          */
2927                         if (newrc != (Expr *) rc)
2928                                 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
2929
2930                         /*
2931                          * Check to see if the indexkey is on the right; if so, commute
2932                          * the clause.
2933                          */
2934                         if (!var_on_left)
2935                                 CommuteRowCompareExpr(rc);
2936
2937                         /*
2938                          * Now replace the indexkey expressions with index Vars.
2939                          */
2940                         Assert(list_length(rc->largs) == list_length(indexcolnos));
2941                         forboth(lca, rc->largs, lcai, indexcolnos)
2942                         {
2943                                 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
2944                                                                                                         index,
2945                                                                                                         lfirst_int(lcai));
2946                         }
2947                 }
2948                 else if (IsA(clause, ScalarArrayOpExpr))
2949                 {
2950                         ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2951
2952                         /* Never need to commute... */
2953
2954                         /* Replace the indexkey expression with an index Var. */
2955                         linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2956                                                                                                                  index,
2957                                                                                                                  indexcol);
2958                 }
2959                 else if (IsA(clause, NullTest))
2960                 {
2961                         NullTest   *nt = (NullTest *) clause;
2962
2963                         /* Replace the indexkey expression with an index Var. */
2964                         nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2965                                                                                                          index,
2966                                                                                                          indexcol);
2967                 }
2968                 else
2969                         elog(ERROR, "unsupported indexqual type: %d",
2970                                  (int) nodeTag(clause));
2971
2972                 fixed_indexquals = lappend(fixed_indexquals, clause);
2973         }
2974
2975         return fixed_indexquals;
2976 }
2977
2978 /*
2979  * fix_indexorderby_references
2980  *        Adjust indexorderby clauses to the form the executor's index
2981  *        machinery needs.
2982  *
2983  * This is a simplified version of fix_indexqual_references.  The input does
2984  * not have RestrictInfo nodes, and we assume that indxpath.c already
2985  * commuted the clauses to put the index keys on the left.  Also, we don't
2986  * bother to support any cases except simple OpExprs, since nothing else
2987  * is allowed for ordering operators.
2988  */
2989 static List *
2990 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
2991 {
2992         IndexOptInfo *index = index_path->indexinfo;
2993         List       *fixed_indexorderbys;
2994         ListCell   *lcc,
2995                            *lci;
2996
2997         fixed_indexorderbys = NIL;
2998
2999         forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
3000         {
3001                 Node       *clause = (Node *) lfirst(lcc);
3002                 int                     indexcol = lfirst_int(lci);
3003
3004                 /*
3005                  * Replace any outer-relation variables with nestloop params.
3006                  *
3007                  * This also makes a copy of the clause, so it's safe to modify it
3008                  * in-place below.
3009                  */
3010                 clause = replace_nestloop_params(root, clause);
3011
3012                 if (IsA(clause, OpExpr))
3013                 {
3014                         OpExpr     *op = (OpExpr *) clause;
3015
3016                         if (list_length(op->args) != 2)
3017                                 elog(ERROR, "indexorderby clause is not binary opclause");
3018
3019                         /*
3020                          * Now replace the indexkey expression with an index Var.
3021                          */
3022                         linitial(op->args) = fix_indexqual_operand(linitial(op->args),
3023                                                                                                            index,
3024                                                                                                            indexcol);
3025                 }
3026                 else
3027                         elog(ERROR, "unsupported indexorderby type: %d",
3028                                  (int) nodeTag(clause));
3029
3030                 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
3031         }
3032
3033         return fixed_indexorderbys;
3034 }
3035
3036 /*
3037  * fix_indexqual_operand
3038  *        Convert an indexqual expression to a Var referencing the index column.
3039  *
3040  * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
3041  * equal to the index's attribute number (index column position).
3042  *
3043  * Most of the code here is just for sanity cross-checking that the given
3044  * expression actually matches the index column it's claimed to.
3045  */
3046 static Node *
3047 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
3048 {
3049         Var                *result;
3050         int                     pos;
3051         ListCell   *indexpr_item;
3052
3053         /*
3054          * Remove any binary-compatible relabeling of the indexkey
3055          */
3056         if (IsA(node, RelabelType))
3057                 node = (Node *) ((RelabelType *) node)->arg;
3058
3059         Assert(indexcol >= 0 && indexcol < index->ncolumns);
3060
3061         if (index->indexkeys[indexcol] != 0)
3062         {
3063                 /* It's a simple index column */
3064                 if (IsA(node, Var) &&
3065                         ((Var *) node)->varno == index->rel->relid &&
3066                         ((Var *) node)->varattno == index->indexkeys[indexcol])
3067                 {
3068                         result = (Var *) copyObject(node);
3069                         result->varno = INDEX_VAR;
3070                         result->varattno = indexcol + 1;
3071                         return (Node *) result;
3072                 }
3073                 else
3074                         elog(ERROR, "index key does not match expected index column");
3075         }
3076
3077         /* It's an index expression, so find and cross-check the expression */
3078         indexpr_item = list_head(index->indexprs);
3079         for (pos = 0; pos < index->ncolumns; pos++)
3080         {
3081                 if (index->indexkeys[pos] == 0)
3082                 {
3083                         if (indexpr_item == NULL)
3084                                 elog(ERROR, "too few entries in indexprs list");
3085                         if (pos == indexcol)
3086                         {
3087                                 Node       *indexkey;
3088
3089                                 indexkey = (Node *) lfirst(indexpr_item);
3090                                 if (indexkey && IsA(indexkey, RelabelType))
3091                                         indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3092                                 if (equal(node, indexkey))
3093                                 {
3094                                         result = makeVar(INDEX_VAR, indexcol + 1,
3095                                                                          exprType(lfirst(indexpr_item)), -1,
3096                                                                          exprCollation(lfirst(indexpr_item)),
3097                                                                          0);
3098                                         return (Node *) result;
3099                                 }
3100                                 else
3101                                         elog(ERROR, "index key does not match expected index column");
3102                         }
3103                         indexpr_item = lnext(indexpr_item);
3104                 }
3105         }
3106
3107         /* Ooops... */
3108         elog(ERROR, "index key does not match expected index column");
3109         return NULL;                            /* keep compiler quiet */
3110 }
3111
3112 /*
3113  * get_switched_clauses
3114  *        Given a list of merge or hash joinclauses (as RestrictInfo nodes),
3115  *        extract the bare clauses, and rearrange the elements within the
3116  *        clauses, if needed, so the outer join variable is on the left and
3117  *        the inner is on the right.  The original clause data structure is not
3118  *        touched; a modified list is returned.  We do, however, set the transient
3119  *        outer_is_left field in each RestrictInfo to show which side was which.
3120  */
3121 static List *
3122 get_switched_clauses(List *clauses, Relids outerrelids)
3123 {
3124         List       *t_list = NIL;
3125         ListCell   *l;
3126
3127         foreach(l, clauses)
3128         {
3129                 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
3130                 OpExpr     *clause = (OpExpr *) restrictinfo->clause;
3131
3132                 Assert(is_opclause(clause));
3133                 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
3134                 {
3135                         /*
3136                          * Duplicate just enough of the structure to allow commuting the
3137                          * clause without changing the original list.  Could use
3138                          * copyObject, but a complete deep copy is overkill.
3139                          */
3140                         OpExpr     *temp = makeNode(OpExpr);
3141
3142                         temp->opno = clause->opno;
3143                         temp->opfuncid = InvalidOid;
3144                         temp->opresulttype = clause->opresulttype;
3145                         temp->opretset = clause->opretset;
3146                         temp->opcollid = clause->opcollid;
3147                         temp->inputcollid = clause->inputcollid;
3148                         temp->args = list_copy(clause->args);
3149                         temp->location = clause->location;
3150                         /* Commute it --- note this modifies the temp node in-place. */
3151                         CommuteOpExpr(temp);
3152                         t_list = lappend(t_list, temp);
3153                         restrictinfo->outer_is_left = false;
3154                 }
3155                 else
3156                 {
3157                         Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
3158                         t_list = lappend(t_list, clause);
3159                         restrictinfo->outer_is_left = true;
3160                 }
3161         }
3162         return t_list;
3163 }
3164
3165 /*
3166  * order_qual_clauses
3167  *              Given a list of qual clauses that will all be evaluated at the same
3168  *              plan node, sort the list into the order we want to check the quals
3169  *              in at runtime.
3170  *
3171  * Ideally the order should be driven by a combination of execution cost and
3172  * selectivity, but it's not immediately clear how to account for both,
3173  * and given the uncertainty of the estimates the reliability of the decisions
3174  * would be doubtful anyway.  So we just order by estimated per-tuple cost,
3175  * being careful not to change the order when (as is often the case) the
3176  * estimates are identical.
3177  *
3178  * Although this will work on either bare clauses or RestrictInfos, it's
3179  * much faster to apply it to RestrictInfos, since it can re-use cost
3180  * information that is cached in RestrictInfos.
3181  *
3182  * Note: some callers pass lists that contain entries that will later be
3183  * removed; this is the easiest way to let this routine see RestrictInfos
3184  * instead of bare clauses.  It's OK because we only sort by cost, but
3185  * a cost/selectivity combination would likely do the wrong thing.
3186  */
3187 static List *
3188 order_qual_clauses(PlannerInfo *root, List *clauses)
3189 {
3190         typedef struct
3191         {
3192                 Node       *clause;
3193                 Cost            cost;
3194         } QualItem;
3195         int                     nitems = list_length(clauses);
3196         QualItem   *items;
3197         ListCell   *lc;
3198         int                     i;
3199         List       *result;
3200
3201         /* No need to work hard for 0 or 1 clause */
3202         if (nitems <= 1)
3203                 return clauses;
3204
3205         /*
3206          * Collect the items and costs into an array.  This is to avoid repeated
3207          * cost_qual_eval work if the inputs aren't RestrictInfos.
3208          */
3209         items = (QualItem *) palloc(nitems * sizeof(QualItem));
3210         i = 0;
3211         foreach(lc, clauses)
3212         {
3213                 Node       *clause = (Node *) lfirst(lc);
3214                 QualCost        qcost;
3215
3216                 cost_qual_eval_node(&qcost, clause, root);
3217                 items[i].clause = clause;
3218                 items[i].cost = qcost.per_tuple;
3219                 i++;
3220         }
3221
3222         /*
3223          * Sort.  We don't use qsort() because it's not guaranteed stable for
3224          * equal keys.  The expected number of entries is small enough that a
3225          * simple insertion sort should be good enough.
3226          */
3227         for (i = 1; i < nitems; i++)
3228         {
3229                 QualItem        newitem = items[i];
3230                 int                     j;
3231
3232                 /* insert newitem into the already-sorted subarray */
3233                 for (j = i; j > 0; j--)
3234                 {
3235                         if (newitem.cost >= items[j - 1].cost)
3236                                 break;
3237                         items[j] = items[j - 1];
3238                 }
3239                 items[j] = newitem;
3240         }
3241
3242         /* Convert back to a list */
3243         result = NIL;
3244         for (i = 0; i < nitems; i++)
3245                 result = lappend(result, items[i].clause);
3246
3247         return result;
3248 }
3249
3250 /*
3251  * Copy cost and size info from a Path node to the Plan node created from it.
3252  * The executor usually won't use this info, but it's needed by EXPLAIN.
3253  */
3254 static void
3255 copy_path_costsize(Plan *dest, Path *src)
3256 {
3257         if (src)
3258         {
3259                 dest->startup_cost = src->startup_cost;
3260                 dest->total_cost = src->total_cost;
3261                 dest->plan_rows = src->rows;
3262                 dest->plan_width = src->parent->width;
3263         }
3264         else
3265         {
3266                 dest->startup_cost = 0;
3267                 dest->total_cost = 0;
3268                 dest->plan_rows = 0;
3269                 dest->plan_width = 0;
3270         }
3271 }
3272
3273 /*
3274  * Copy cost and size info from a lower plan node to an inserted node.
3275  * (Most callers alter the info after copying it.)
3276  */
3277 static void
3278 copy_plan_costsize(Plan *dest, Plan *src)
3279 {
3280         if (src)
3281         {
3282                 dest->startup_cost = src->startup_cost;
3283                 dest->total_cost = src->total_cost;
3284                 dest->plan_rows = src->plan_rows;
3285                 dest->plan_width = src->plan_width;
3286         }
3287         else
3288         {
3289                 dest->startup_cost = 0;
3290                 dest->total_cost = 0;
3291                 dest->plan_rows = 0;
3292                 dest->plan_width = 0;
3293         }
3294 }
3295
3296
3297 /*****************************************************************************
3298  *
3299  *      PLAN NODE BUILDING ROUTINES
3300  *
3301  * Some of these are exported because they are called to build plan nodes
3302  * in contexts where we're not deriving the plan node from a path node.
3303  *
3304  *****************************************************************************/
3305
3306 static SeqScan *
3307 make_seqscan(List *qptlist,
3308                          List *qpqual,
3309                          Index scanrelid)
3310 {
3311         SeqScan    *node = makeNode(SeqScan);
3312         Plan       *plan = &node->plan;
3313
3314         /* cost should be inserted by caller */
3315         plan->targetlist = qptlist;
3316         plan->qual = qpqual;
3317         plan->lefttree = NULL;
3318         plan->righttree = NULL;
3319         node->scanrelid = scanrelid;
3320
3321         return node;
3322 }
3323
3324 static IndexScan *
3325 make_indexscan(List *qptlist,
3326                            List *qpqual,
3327                            Index scanrelid,
3328                            Oid indexid,
3329                            List *indexqual,
3330                            List *indexqualorig,
3331                            List *indexorderby,
3332                            List *indexorderbyorig,
3333                            ScanDirection indexscandir)
3334 {
3335         IndexScan  *node = makeNode(IndexScan);
3336         Plan       *plan = &node->scan.plan;
3337
3338         /* cost should be inserted by caller */
3339         plan->targetlist = qptlist;
3340         plan->qual = qpqual;
3341         plan->lefttree = NULL;
3342         plan->righttree = NULL;
3343         node->scan.scanrelid = scanrelid;
3344         node->indexid = indexid;
3345         node->indexqual = indexqual;
3346         node->indexqualorig = indexqualorig;
3347         node->indexorderby = indexorderby;
3348         node->indexorderbyorig = indexorderbyorig;
3349         node->indexorderdir = indexscandir;
3350
3351         return node;
3352 }
3353
3354 static IndexOnlyScan *
3355 make_indexonlyscan(List *qptlist,
3356                                    List *qpqual,
3357                                    Index scanrelid,
3358                                    Oid indexid,
3359                                    List *indexqual,
3360                                    List *indexorderby,
3361                                    List *indextlist,
3362                                    ScanDirection indexscandir)
3363 {
3364         IndexOnlyScan *node = makeNode(IndexOnlyScan);
3365         Plan       *plan = &node->scan.plan;
3366
3367         /* cost should be inserted by caller */
3368         plan->targetlist = qptlist;
3369         plan->qual = qpqual;
3370         plan->lefttree = NULL;
3371         plan->righttree = NULL;
3372         node->scan.scanrelid = scanrelid;
3373         node->indexid = indexid;
3374         node->indexqual = indexqual;
3375         node->indexorderby = indexorderby;
3376         node->indextlist = indextlist;
3377         node->indexorderdir = indexscandir;
3378
3379         return node;
3380 }
3381
3382 static BitmapIndexScan *
3383 make_bitmap_indexscan(Index scanrelid,
3384                                           Oid indexid,
3385                                           List *indexqual,
3386                                           List *indexqualorig)
3387 {
3388         BitmapIndexScan *node = makeNode(BitmapIndexScan);
3389         Plan       *plan = &node->scan.plan;
3390
3391         /* cost should be inserted by caller */
3392         plan->targetlist = NIL;         /* not used */
3393         plan->qual = NIL;                       /* not used */
3394         plan->lefttree = NULL;
3395         plan->righttree = NULL;
3396         node->scan.scanrelid = scanrelid;
3397         node->indexid = indexid;
3398         node->indexqual = indexqual;
3399         node->indexqualorig = indexqualorig;
3400
3401         return node;
3402 }
3403
3404 static BitmapHeapScan *
3405 make_bitmap_heapscan(List *qptlist,
3406                                          List *qpqual,
3407                                          Plan *lefttree,
3408                                          List *bitmapqualorig,
3409                                          Index scanrelid)
3410 {
3411         BitmapHeapScan *node = makeNode(BitmapHeapScan);
3412         Plan       *plan = &node->scan.plan;
3413
3414         /* cost should be inserted by caller */
3415         plan->targetlist = qptlist;
3416         plan->qual = qpqual;
3417         plan->lefttree = lefttree;
3418         plan->righttree = NULL;
3419         node->scan.scanrelid = scanrelid;
3420         node->bitmapqualorig = bitmapqualorig;
3421
3422         return node;
3423 }
3424
3425 static TidScan *
3426 make_tidscan(List *qptlist,
3427                          List *qpqual,
3428                          Index scanrelid,
3429                          List *tidquals)
3430 {
3431         TidScan    *node = makeNode(TidScan);
3432         Plan       *plan = &node->scan.plan;
3433
3434         /* cost should be inserted by caller */
3435         plan->targetlist = qptlist;
3436         plan->qual = qpqual;
3437         plan->lefttree = NULL;
3438         plan->righttree = NULL;
3439         node->scan.scanrelid = scanrelid;
3440         node->tidquals = tidquals;
3441
3442         return node;
3443 }
3444
3445 SubqueryScan *
3446 make_subqueryscan(List *qptlist,
3447                                   List *qpqual,
3448                                   Index scanrelid,
3449                                   Plan *subplan)
3450 {
3451         SubqueryScan *node = makeNode(SubqueryScan);
3452         Plan       *plan = &node->scan.plan;
3453
3454         /*
3455          * Cost is figured here for the convenience of prepunion.c.  Note this is
3456          * only correct for the case where qpqual is empty; otherwise caller
3457          * should overwrite cost with a better estimate.
3458          */
3459         copy_plan_costsize(plan, subplan);
3460         plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
3461
3462         plan->targetlist = qptlist;
3463         plan->qual = qpqual;
3464         plan->lefttree = NULL;
3465         plan->righttree = NULL;
3466         node->scan.scanrelid = scanrelid;
3467         node->subplan = subplan;
3468
3469         return node;
3470 }
3471
3472 static FunctionScan *
3473 make_functionscan(List *qptlist,
3474                                   List *qpqual,
3475                                   Index scanrelid,
3476                                   List *functions,
3477                                   bool funcordinality)
3478 {
3479         FunctionScan *node = makeNode(FunctionScan);
3480         Plan       *plan = &node->scan.plan;
3481
3482         /* cost should be inserted by caller */
3483         plan->targetlist = qptlist;
3484         plan->qual = qpqual;
3485         plan->lefttree = NULL;
3486         plan->righttree = NULL;
3487         node->scan.scanrelid = scanrelid;
3488         node->functions = functions;
3489         node->funcordinality = funcordinality;
3490
3491         return node;
3492 }
3493
3494 static ValuesScan *
3495 make_valuesscan(List *qptlist,
3496                                 List *qpqual,
3497                                 Index scanrelid,
3498                                 List *values_lists)
3499 {
3500         ValuesScan *node = makeNode(ValuesScan);
3501         Plan       *plan = &node->scan.plan;
3502
3503         /* cost should be inserted by caller */
3504         plan->targetlist = qptlist;
3505         plan->qual = qpqual;
3506         plan->lefttree = NULL;
3507         plan->righttree = NULL;
3508         node->scan.scanrelid = scanrelid;
3509         node->values_lists = values_lists;
3510
3511         return node;
3512 }
3513
3514 static CteScan *
3515 make_ctescan(List *qptlist,
3516                          List *qpqual,
3517                          Index scanrelid,
3518                          int ctePlanId,
3519                          int cteParam)
3520 {
3521         CteScan    *node = makeNode(CteScan);
3522         Plan       *plan = &node->scan.plan;
3523
3524         /* cost should be inserted by caller */
3525         plan->targetlist = qptlist;
3526         plan->qual = qpqual;
3527         plan->lefttree = NULL;
3528         plan->righttree = NULL;
3529         node->scan.scanrelid = scanrelid;
3530         node->ctePlanId = ctePlanId;
3531         node->cteParam = cteParam;
3532
3533         return node;
3534 }
3535
3536 static WorkTableScan *
3537 make_worktablescan(List *qptlist,
3538                                    List *qpqual,
3539                                    Index scanrelid,
3540                                    int wtParam)
3541 {
3542         WorkTableScan *node = makeNode(WorkTableScan);
3543         Plan       *plan = &node->scan.plan;
3544
3545         /* cost should be inserted by caller */
3546         plan->targetlist = qptlist;
3547         plan->qual = qpqual;
3548         plan->lefttree = NULL;
3549         plan->righttree = NULL;
3550         node->scan.scanrelid = scanrelid;
3551         node->wtParam = wtParam;
3552
3553         return node;
3554 }
3555
3556 ForeignScan *
3557 make_foreignscan(List *qptlist,
3558                                  List *qpqual,
3559                                  Index scanrelid,
3560                                  List *fdw_exprs,
3561                                  List *fdw_private)
3562 {
3563         ForeignScan *node = makeNode(ForeignScan);
3564         Plan       *plan = &node->scan.plan;
3565
3566         /* cost will be filled in by create_foreignscan_plan */
3567         plan->targetlist = qptlist;
3568         plan->qual = qpqual;
3569         plan->lefttree = NULL;
3570         plan->righttree = NULL;
3571         node->scan.scanrelid = scanrelid;
3572         node->fdw_exprs = fdw_exprs;
3573         node->fdw_private = fdw_private;
3574         /* fsSystemCol will be filled in by create_foreignscan_plan */
3575         node->fsSystemCol = false;
3576
3577         return node;
3578 }
3579
3580 Append *
3581 make_append(List *appendplans, List *tlist)
3582 {
3583         Append     *node = makeNode(Append);
3584         Plan       *plan = &node->plan;
3585         double          total_size;
3586         ListCell   *subnode;
3587
3588         /*
3589          * Compute cost as sum of subplan costs.  We charge nothing extra for the
3590          * Append itself, which perhaps is too optimistic, but since it doesn't do
3591          * any selection or projection, it is a pretty cheap node.
3592          *
3593          * If you change this, see also create_append_path().  Also, the size
3594          * calculations should match set_append_rel_pathlist().  It'd be better
3595          * not to duplicate all this logic, but some callers of this function
3596          * aren't working from an appendrel or AppendPath, so there's noplace to
3597          * copy the data from.
3598          */
3599         plan->startup_cost = 0;
3600         plan->total_cost = 0;
3601         plan->plan_rows = 0;
3602         total_size = 0;
3603         foreach(subnode, appendplans)
3604         {
3605                 Plan       *subplan = (Plan *) lfirst(subnode);
3606
3607                 if (subnode == list_head(appendplans))  /* first node? */
3608                         plan->startup_cost = subplan->startup_cost;
3609                 plan->total_cost += subplan->total_cost;
3610                 plan->plan_rows += subplan->plan_rows;
3611                 total_size += subplan->plan_width * subplan->plan_rows;
3612         }
3613         if (plan->plan_rows > 0)
3614                 plan->plan_width = rint(total_size / plan->plan_rows);
3615         else
3616                 plan->plan_width = 0;
3617
3618         plan->targetlist = tlist;
3619         plan->qual = NIL;
3620         plan->lefttree = NULL;
3621         plan->righttree = NULL;
3622         node->appendplans = appendplans;
3623
3624         return node;
3625 }
3626
3627 RecursiveUnion *
3628 make_recursive_union(List *tlist,
3629                                          Plan *lefttree,
3630                                          Plan *righttree,
3631                                          int wtParam,
3632                                          List *distinctList,
3633                                          long numGroups)
3634 {
3635         RecursiveUnion *node = makeNode(RecursiveUnion);
3636         Plan       *plan = &node->plan;
3637         int                     numCols = list_length(distinctList);
3638
3639         cost_recursive_union(plan, lefttree, righttree);
3640
3641         plan->targetlist = tlist;
3642         plan->qual = NIL;
3643         plan->lefttree = lefttree;
3644         plan->righttree = righttree;
3645         node->wtParam = wtParam;
3646
3647         /*
3648          * convert SortGroupClause list into arrays of attr indexes and equality
3649          * operators, as wanted by executor
3650          */
3651         node->numCols = numCols;
3652         if (numCols > 0)
3653         {
3654                 int                     keyno = 0;
3655                 AttrNumber *dupColIdx;
3656                 Oid                *dupOperators;
3657                 ListCell   *slitem;
3658
3659                 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3660                 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3661
3662                 foreach(slitem, distinctList)
3663                 {
3664                         SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3665                         TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3666                                                                                                            plan->targetlist);
3667
3668                         dupColIdx[keyno] = tle->resno;
3669                         dupOperators[keyno] = sortcl->eqop;
3670                         Assert(OidIsValid(dupOperators[keyno]));
3671                         keyno++;
3672                 }
3673                 node->dupColIdx = dupColIdx;
3674                 node->dupOperators = dupOperators;
3675         }
3676         node->numGroups = numGroups;
3677
3678         return node;
3679 }
3680
3681 static BitmapAnd *
3682 make_bitmap_and(List *bitmapplans)
3683 {
3684         BitmapAnd  *node = makeNode(BitmapAnd);
3685         Plan       *plan = &node->plan;
3686
3687         /* cost should be inserted by caller */
3688         plan->targetlist = NIL;
3689         plan->qual = NIL;
3690         plan->lefttree = NULL;
3691         plan->righttree = NULL;
3692         node->bitmapplans = bitmapplans;
3693
3694         return node;
3695 }
3696
3697 static BitmapOr *
3698 make_bitmap_or(List *bitmapplans)
3699 {
3700         BitmapOr   *node = makeNode(BitmapOr);
3701         Plan       *plan = &node->plan;
3702
3703         /* cost should be inserted by caller */
3704         plan->targetlist = NIL;
3705         plan->qual = NIL;
3706         plan->lefttree = NULL;
3707         plan->righttree = NULL;
3708         node->bitmapplans = bitmapplans;
3709
3710         return node;
3711 }
3712
3713 static NestLoop *
3714 make_nestloop(List *tlist,
3715                           List *joinclauses,
3716                           List *otherclauses,
3717                           List *nestParams,
3718                           Plan *lefttree,
3719                           Plan *righttree,
3720                           JoinType jointype)
3721 {
3722         NestLoop   *node = makeNode(NestLoop);
3723         Plan       *plan = &node->join.plan;
3724
3725         /* cost should be inserted by caller */
3726         plan->targetlist = tlist;
3727         plan->qual = otherclauses;
3728         plan->lefttree = lefttree;
3729         plan->righttree = righttree;
3730         node->join.jointype = jointype;
3731         node->join.joinqual = joinclauses;
3732         node->nestParams = nestParams;
3733
3734         return node;
3735 }
3736
3737 static HashJoin *
3738 make_hashjoin(List *tlist,
3739                           List *joinclauses,
3740                           List *otherclauses,
3741                           List *hashclauses,
3742                           Plan *lefttree,
3743                           Plan *righttree,
3744                           JoinType jointype)
3745 {
3746         HashJoin   *node = makeNode(HashJoin);
3747         Plan       *plan = &node->join.plan;
3748
3749         /* cost should be inserted by caller */
3750         plan->targetlist = tlist;
3751         plan->qual = otherclauses;
3752         plan->lefttree = lefttree;
3753         plan->righttree = righttree;
3754         node->hashclauses = hashclauses;
3755         node->join.jointype = jointype;
3756         node->join.joinqual = joinclauses;
3757
3758         return node;
3759 }
3760
3761 static Hash *
3762 make_hash(Plan *lefttree,
3763                   Oid skewTable,
3764                   AttrNumber skewColumn,
3765                   bool skewInherit,
3766                   Oid skewColType,
3767                   int32 skewColTypmod)
3768 {
3769         Hash       *node = makeNode(Hash);
3770         Plan       *plan = &node->plan;
3771
3772         copy_plan_costsize(plan, lefttree);
3773
3774         /*
3775          * For plausibility, make startup & total costs equal total cost of input
3776          * plan; this only affects EXPLAIN display not decisions.
3777          */
3778         plan->startup_cost = plan->total_cost;
3779         plan->targetlist = lefttree->targetlist;
3780         plan->qual = NIL;
3781         plan->lefttree = lefttree;
3782         plan->righttree = NULL;
3783
3784         node->skewTable = skewTable;
3785         node->skewColumn = skewColumn;
3786         node->skewInherit = skewInherit;
3787         node->skewColType = skewColType;
3788         node->skewColTypmod = skewColTypmod;
3789
3790         return node;
3791 }
3792
3793 static MergeJoin *
3794 make_mergejoin(List *tlist,
3795                            List *joinclauses,
3796                            List *otherclauses,
3797                            List *mergeclauses,
3798                            Oid *mergefamilies,
3799                            Oid *mergecollations,
3800                            int *mergestrategies,
3801                            bool *mergenullsfirst,
3802                            Plan *lefttree,
3803                            Plan *righttree,
3804                            JoinType jointype)
3805 {
3806         MergeJoin  *node = makeNode(MergeJoin);
3807         Plan       *plan = &node->join.plan;
3808
3809         /* cost should be inserted by caller */
3810         plan->targetlist = tlist;
3811         plan->qual = otherclauses;
3812         plan->lefttree = lefttree;
3813         plan->righttree = righttree;
3814         node->mergeclauses = mergeclauses;
3815         node->mergeFamilies = mergefamilies;
3816         node->mergeCollations = mergecollations;
3817         node->mergeStrategies = mergestrategies;
3818         node->mergeNullsFirst = mergenullsfirst;
3819         node->join.jointype = jointype;
3820         node->join.joinqual = joinclauses;
3821
3822         return node;
3823 }
3824
3825 /*
3826  * make_sort --- basic routine to build a Sort plan node
3827  *
3828  * Caller must have built the sortColIdx, sortOperators, collations, and
3829  * nullsFirst arrays already.
3830  * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
3831  */
3832 static Sort *
3833 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3834                   AttrNumber *sortColIdx, Oid *sortOperators,
3835                   Oid *collations, bool *nullsFirst,
3836                   double limit_tuples)
3837 {
3838         Sort       *node = makeNode(Sort);
3839         Plan       *plan = &node->plan;
3840         Path            sort_path;              /* dummy for result of cost_sort */
3841
3842         copy_plan_costsize(plan, lefttree); /* only care about copying size */
3843         cost_sort(&sort_path, root, NIL,
3844                           lefttree->total_cost,
3845                           lefttree->plan_rows,
3846                           lefttree->plan_width,
3847                           0.0,
3848                           work_mem,
3849                           limit_tuples);
3850         plan->startup_cost = sort_path.startup_cost;
3851         plan->total_cost = sort_path.total_cost;
3852         plan->targetlist = lefttree->targetlist;
3853         plan->qual = NIL;
3854         plan->lefttree = lefttree;
3855         plan->righttree = NULL;
3856         node->numCols = numCols;
3857         node->sortColIdx = sortColIdx;
3858         node->sortOperators = sortOperators;
3859         node->collations = collations;
3860         node->nullsFirst = nullsFirst;
3861
3862         return node;
3863 }
3864
3865 /*
3866  * prepare_sort_from_pathkeys
3867  *        Prepare to sort according to given pathkeys
3868  *
3869  * This is used to set up for both Sort and MergeAppend nodes.  It calculates
3870  * the executor's representation of the sort key information, and adjusts the
3871  * plan targetlist if needed to add resjunk sort columns.
3872  *
3873  * Input parameters:
3874  *        'lefttree' is the plan node which yields input tuples
3875  *        'pathkeys' is the list of pathkeys by which the result is to be sorted
3876  *        'relids' identifies the child relation being sorted, if any
3877  *        'reqColIdx' is NULL or an array of required sort key column numbers
3878  *        'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
3879  *
3880  * We must convert the pathkey information into arrays of sort key column
3881  * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
3882  * which is the representation the executor wants.  These are returned into
3883  * the output parameters *p_numsortkeys etc.
3884  *
3885  * When looking for matches to an EquivalenceClass's members, we will only
3886  * consider child EC members if they match 'relids'.  This protects against
3887  * possible incorrect matches to child expressions that contain no Vars.
3888  *
3889  * If reqColIdx isn't NULL then it contains sort key column numbers that
3890  * we should match.  This is used when making child plans for a MergeAppend;
3891  * it's an error if we can't match the columns.
3892  *
3893  * If the pathkeys include expressions that aren't simple Vars, we will
3894  * usually need to add resjunk items to the input plan's targetlist to
3895  * compute these expressions, since the Sort/MergeAppend node itself won't
3896  * do any such calculations.  If the input plan type isn't one that can do
3897  * projections, this means adding a Result node just to do the projection.
3898  * However, the caller can pass adjust_tlist_in_place = TRUE to force the
3899  * lefttree tlist to be modified in-place regardless of whether the node type
3900  * can project --- we use this for fixing the tlist of MergeAppend itself.
3901  *
3902  * Returns the node which is to be the input to the Sort (either lefttree,
3903  * or a Result stacked atop lefttree).
3904  */
3905 static Plan *
3906 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3907                                                    Relids relids,
3908                                                    const AttrNumber *reqColIdx,
3909                                                    bool adjust_tlist_in_place,
3910                                                    int *p_numsortkeys,
3911                                                    AttrNumber **p_sortColIdx,
3912                                                    Oid **p_sortOperators,
3913                                                    Oid **p_collations,
3914                                                    bool **p_nullsFirst)
3915 {
3916         List       *tlist = lefttree->targetlist;
3917         ListCell   *i;
3918         int                     numsortkeys;
3919         AttrNumber *sortColIdx;
3920         Oid                *sortOperators;
3921         Oid                *collations;
3922         bool       *nullsFirst;
3923
3924         /*
3925          * We will need at most list_length(pathkeys) sort columns; possibly less
3926          */
3927         numsortkeys = list_length(pathkeys);
3928         sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3929         sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3930         collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3931         nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3932
3933         numsortkeys = 0;
3934
3935         foreach(i, pathkeys)
3936         {
3937                 PathKey    *pathkey = (PathKey *) lfirst(i);
3938                 EquivalenceClass *ec = pathkey->pk_eclass;
3939                 EquivalenceMember *em;
3940                 TargetEntry *tle = NULL;
3941                 Oid                     pk_datatype = InvalidOid;
3942                 Oid                     sortop;
3943                 ListCell   *j;
3944
3945                 if (ec->ec_has_volatile)
3946                 {
3947                         /*
3948                          * If the pathkey's EquivalenceClass is volatile, then it must
3949                          * have come from an ORDER BY clause, and we have to match it to
3950                          * that same targetlist entry.
3951                          */
3952                         if (ec->ec_sortref == 0)        /* can't happen */
3953                                 elog(ERROR, "volatile EquivalenceClass has no sortref");
3954                         tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
3955                         Assert(tle);
3956                         Assert(list_length(ec->ec_members) == 1);
3957                         pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3958                 }
3959                 else if (reqColIdx != NULL)
3960                 {
3961                         /*
3962                          * If we are given a sort column number to match, only consider
3963                          * the single TLE at that position.  It's possible that there is
3964                          * no such TLE, in which case fall through and generate a resjunk
3965                          * targetentry (we assume this must have happened in the parent
3966                          * plan as well).  If there is a TLE but it doesn't match the
3967                          * pathkey's EC, we do the same, which is probably the wrong thing
3968                          * but we'll leave it to caller to complain about the mismatch.
3969                          */
3970                         tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
3971                         if (tle)
3972                         {
3973                                 em = find_ec_member_for_tle(ec, tle, relids);
3974                                 if (em)
3975                                 {
3976                                         /* found expr at right place in tlist */
3977                                         pk_datatype = em->em_datatype;
3978                                 }
3979                                 else
3980                                         tle = NULL;
3981                         }
3982                 }
3983                 else
3984                 {
3985                         /*
3986                          * Otherwise, we can sort by any non-constant expression listed in
3987                          * the pathkey's EquivalenceClass.  For now, we take the first
3988                          * tlist item found in the EC. If there's no match, we'll generate
3989                          * a resjunk entry using the first EC member that is an expression
3990                          * in the input's vars.  (The non-const restriction only matters
3991                          * if the EC is below_outer_join; but if it isn't, it won't
3992                          * contain consts anyway, else we'd have discarded the pathkey as
3993                          * redundant.)
3994                          *
3995                          * XXX if we have a choice, is there any way of figuring out which
3996                          * might be cheapest to execute?  (For example, int4lt is likely
3997                          * much cheaper to execute than numericlt, but both might appear
3998                          * in the same equivalence class...)  Not clear that we ever will
3999                          * have an interesting choice in practice, so it may not matter.
4000                          */
4001                         foreach(j, tlist)
4002                         {
4003                                 tle = (TargetEntry *) lfirst(j);
4004                                 em = find_ec_member_for_tle(ec, tle, relids);
4005                                 if (em)
4006                                 {
4007                                         /* found expr already in tlist */
4008                                         pk_datatype = em->em_datatype;
4009                                         break;
4010                                 }
4011                                 tle = NULL;
4012                         }
4013                 }
4014
4015                 if (!tle)
4016                 {
4017                         /*
4018                          * No matching tlist item; look for a computable expression. Note
4019                          * that we treat Aggrefs as if they were variables; this is
4020                          * necessary when attempting to sort the output from an Agg node
4021                          * for use in a WindowFunc (since grouping_planner will have
4022                          * treated the Aggrefs as variables, too).
4023                          */
4024                         Expr       *sortexpr = NULL;
4025
4026                         foreach(j, ec->ec_members)
4027                         {
4028                                 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
4029                                 List       *exprvars;
4030                                 ListCell   *k;
4031
4032                                 /*
4033                                  * We shouldn't be trying to sort by an equivalence class that
4034                                  * contains a constant, so no need to consider such cases any
4035                                  * further.
4036                                  */
4037                                 if (em->em_is_const)
4038                                         continue;
4039
4040                                 /*
4041                                  * Ignore child members unless they match the rel being
4042                                  * sorted.
4043                                  */
4044                                 if (em->em_is_child &&
4045                                         !bms_equal(em->em_relids, relids))
4046                                         continue;
4047
4048                                 sortexpr = em->em_expr;
4049                                 exprvars = pull_var_clause((Node *) sortexpr,
4050                                                                                    PVC_INCLUDE_AGGREGATES,
4051                                                                                    PVC_INCLUDE_PLACEHOLDERS);
4052                                 foreach(k, exprvars)
4053                                 {
4054                                         if (!tlist_member_ignore_relabel(lfirst(k), tlist))
4055                                                 break;
4056                                 }
4057                                 list_free(exprvars);
4058                                 if (!k)
4059                                 {
4060                                         pk_datatype = em->em_datatype;
4061                                         break;          /* found usable expression */
4062                                 }
4063                         }
4064                         if (!j)
4065                                 elog(ERROR, "could not find pathkey item to sort");
4066
4067                         /*
4068                          * Do we need to insert a Result node?
4069                          */
4070                         if (!adjust_tlist_in_place &&
4071                                 !is_projection_capable_plan(lefttree))
4072                         {
4073                                 /* copy needed so we don't modify input's tlist below */
4074                                 tlist = copyObject(tlist);
4075                                 lefttree = (Plan *) make_result(root, tlist, NULL,
4076                                                                                                 lefttree);
4077                         }
4078
4079                         /* Don't bother testing is_projection_capable_plan again */
4080                         adjust_tlist_in_place = true;
4081
4082                         /*
4083                          * Add resjunk entry to input's tlist
4084                          */
4085                         tle = makeTargetEntry(sortexpr,
4086                                                                   list_length(tlist) + 1,
4087                                                                   NULL,
4088                                                                   true);
4089                         tlist = lappend(tlist, tle);
4090                         lefttree->targetlist = tlist;           /* just in case NIL before */
4091                 }
4092
4093                 /*
4094                  * Look up the correct sort operator from the PathKey's slightly
4095                  * abstracted representation.
4096                  */
4097                 sortop = get_opfamily_member(pathkey->pk_opfamily,
4098                                                                          pk_datatype,
4099                                                                          pk_datatype,
4100                                                                          pathkey->pk_strategy);
4101                 if (!OidIsValid(sortop))        /* should not happen */
4102                         elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
4103                                  pathkey->pk_strategy, pk_datatype, pk_datatype,
4104                                  pathkey->pk_opfamily);
4105
4106                 /* Add the column to the sort arrays */
4107                 sortColIdx[numsortkeys] = tle->resno;
4108                 sortOperators[numsortkeys] = sortop;
4109                 collations[numsortkeys] = ec->ec_collation;
4110                 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
4111                 numsortkeys++;
4112         }
4113
4114         /* Return results */
4115         *p_numsortkeys = numsortkeys;
4116         *p_sortColIdx = sortColIdx;
4117         *p_sortOperators = sortOperators;
4118         *p_collations = collations;
4119         *p_nullsFirst = nullsFirst;
4120
4121         return lefttree;
4122 }
4123
4124 /*
4125  * find_ec_member_for_tle
4126  *              Locate an EquivalenceClass member matching the given TLE, if any
4127  *
4128  * Child EC members are ignored unless they match 'relids'.
4129  */
4130 static EquivalenceMember *
4131 find_ec_member_for_tle(EquivalenceClass *ec,
4132                                            TargetEntry *tle,
4133                                            Relids relids)
4134 {
4135         Expr       *tlexpr;
4136         ListCell   *lc;
4137
4138         /* We ignore binary-compatible relabeling on both ends */
4139         tlexpr = tle->expr;
4140         while (tlexpr && IsA(tlexpr, RelabelType))
4141                 tlexpr = ((RelabelType *) tlexpr)->arg;
4142
4143         foreach(lc, ec->ec_members)
4144         {
4145                 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
4146                 Expr       *emexpr;
4147
4148                 /*
4149                  * We shouldn't be trying to sort by an equivalence class that
4150                  * contains a constant, so no need to consider such cases any further.
4151                  */
4152                 if (em->em_is_const)
4153                         continue;
4154
4155                 /*
4156                  * Ignore child members unless they match the rel being sorted.
4157                  */
4158                 if (em->em_is_child &&
4159                         !bms_equal(em->em_relids, relids))
4160                         continue;
4161
4162                 /* Match if same expression (after stripping relabel) */
4163                 emexpr = em->em_expr;
4164                 while (emexpr && IsA(emexpr, RelabelType))
4165                         emexpr = ((RelabelType *) emexpr)->arg;
4166
4167                 if (equal(emexpr, tlexpr))
4168                         return em;
4169         }
4170
4171         return NULL;
4172 }
4173
4174 /*
4175  * make_sort_from_pathkeys
4176  *        Create sort plan to sort according to given pathkeys
4177  *
4178  *        'lefttree' is the node which yields input tuples
4179  *        'pathkeys' is the list of pathkeys by which the result is to be sorted
4180  *        'limit_tuples' is the bound on the number of output tuples;
4181  *                              -1 if no bound
4182  */
4183 Sort *
4184 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
4185                                                 double limit_tuples)
4186 {
4187         int                     numsortkeys;
4188         AttrNumber *sortColIdx;
4189         Oid                *sortOperators;
4190         Oid                *collations;
4191         bool       *nullsFirst;
4192
4193         /* Compute sort column info, and adjust lefttree as needed */
4194         lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
4195                                                                                   NULL,
4196                                                                                   NULL,
4197                                                                                   false,
4198                                                                                   &numsortkeys,
4199                                                                                   &sortColIdx,
4200                                                                                   &sortOperators,
4201                                                                                   &collations,
4202                                                                                   &nullsFirst);
4203
4204         /* Now build the Sort node */
4205         return make_sort(root, lefttree, numsortkeys,
4206                                          sortColIdx, sortOperators, collations,
4207                                          nullsFirst, limit_tuples);
4208 }
4209
4210 /*
4211  * make_sort_from_sortclauses
4212  *        Create sort plan to sort according to given sortclauses
4213  *
4214  *        'sortcls' is a list of SortGroupClauses
4215  *        'lefttree' is the node which yields input tuples
4216  */
4217 Sort *
4218 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
4219 {
4220         List       *sub_tlist = lefttree->targetlist;
4221         ListCell   *l;
4222         int                     numsortkeys;
4223         AttrNumber *sortColIdx;
4224         Oid                *sortOperators;
4225         Oid                *collations;
4226         bool       *nullsFirst;
4227
4228         /* Convert list-ish representation to arrays wanted by executor */
4229         numsortkeys = list_length(sortcls);
4230         sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
4231         sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
4232         collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
4233         nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
4234
4235         numsortkeys = 0;
4236         foreach(l, sortcls)
4237         {
4238                 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
4239                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
4240
4241                 sortColIdx[numsortkeys] = tle->resno;
4242                 sortOperators[numsortkeys] = sortcl->sortop;
4243                 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4244                 nullsFirst[numsortkeys] = sortcl->nulls_first;
4245                 numsortkeys++;
4246         }
4247
4248         return make_sort(root, lefttree, numsortkeys,
4249                                          sortColIdx, sortOperators, collations,
4250                                          nullsFirst, -1.0);
4251 }
4252
4253 /*
4254  * make_sort_from_groupcols
4255  *        Create sort plan to sort based on grouping columns
4256  *
4257  * 'groupcls' is the list of SortGroupClauses
4258  * 'grpColIdx' gives the column numbers to use
4259  *
4260  * This might look like it could be merged with make_sort_from_sortclauses,
4261  * but presently we *must* use the grpColIdx[] array to locate sort columns,
4262  * because the child plan's tlist is not marked with ressortgroupref info
4263  * appropriate to the grouping node.  So, only the sort ordering info
4264  * is used from the SortGroupClause entries.
4265  */
4266 Sort *
4267 make_sort_from_groupcols(PlannerInfo *root,
4268                                                  List *groupcls,
4269                                                  AttrNumber *grpColIdx,
4270                                                  Plan *lefttree)
4271 {
4272         List       *sub_tlist = lefttree->targetlist;
4273         ListCell   *l;
4274         int                     numsortkeys;
4275         AttrNumber *sortColIdx;
4276         Oid                *sortOperators;
4277         Oid                *collations;
4278         bool       *nullsFirst;
4279
4280         /* Convert list-ish representation to arrays wanted by executor */
4281         numsortkeys = list_length(groupcls);
4282         sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
4283         sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
4284         collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
4285         nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
4286
4287         numsortkeys = 0;
4288         foreach(l, groupcls)
4289         {
4290                 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
4291                 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
4292
4293                 if (!tle)
4294                         elog(ERROR, "could not retrive tle for sort-from-groupcols");
4295
4296                 sortColIdx[numsortkeys] = tle->resno;
4297                 sortOperators[numsortkeys] = grpcl->sortop;
4298                 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4299                 nullsFirst[numsortkeys] = grpcl->nulls_first;
4300                 numsortkeys++;
4301         }
4302
4303         return make_sort(root, lefttree, numsortkeys,
4304                                          sortColIdx, sortOperators, collations,
4305                                          nullsFirst, -1.0);
4306 }
4307
4308 static Material *
4309 make_material(Plan *lefttree)
4310 {
4311         Material   *node = makeNode(Material);
4312         Plan       *plan = &node->plan;
4313
4314         /* cost should be inserted by caller */
4315         plan->targetlist = lefttree->targetlist;
4316         plan->qual = NIL;
4317         plan->lefttree = lefttree;
4318         plan->righttree = NULL;
4319
4320         return node;
4321 }
4322
4323 /*
4324  * materialize_finished_plan: stick a Material node atop a completed plan
4325  *
4326  * There are a couple of places where we want to attach a Material node
4327  * after completion of subquery_planner().  This currently requires hackery.
4328  * Since subquery_planner has already run SS_finalize_plan on the subplan
4329  * tree, we have to kluge up parameter lists for the Material node.
4330  * Possibly this could be fixed by postponing SS_finalize_plan processing
4331  * until setrefs.c is run?
4332  */
4333 Plan *
4334 materialize_finished_plan(Plan *subplan)
4335 {
4336         Plan       *matplan;
4337         Path            matpath;                /* dummy for result of cost_material */
4338
4339         matplan = (Plan *) make_material(subplan);
4340
4341         /* Set cost data */
4342         cost_material(&matpath,
4343                                   subplan->startup_cost,
4344                                   subplan->total_cost,
4345                                   subplan->plan_rows,
4346                                   subplan->plan_width);
4347         matplan->startup_cost = matpath.startup_cost;
4348         matplan->total_cost = matpath.total_cost;
4349         matplan->plan_rows = subplan->plan_rows;
4350         matplan->plan_width = subplan->plan_width;
4351
4352         /* parameter kluge --- see comments above */
4353         matplan->extParam = bms_copy(subplan->extParam);
4354         matplan->allParam = bms_copy(subplan->allParam);
4355
4356         return matplan;
4357 }
4358
4359 Agg *
4360 make_agg(PlannerInfo *root, List *tlist, List *qual,
4361                  AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
4362                  int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
4363                  long numGroups,
4364                  Plan *lefttree)
4365 {
4366         Agg                *node = makeNode(Agg);
4367         Plan       *plan = &node->plan;
4368         Path            agg_path;               /* dummy for result of cost_agg */
4369         QualCost        qual_cost;
4370
4371         node->aggstrategy = aggstrategy;
4372         node->numCols = numGroupCols;
4373         node->grpColIdx = grpColIdx;
4374         node->grpOperators = grpOperators;
4375         node->numGroups = numGroups;
4376
4377         copy_plan_costsize(plan, lefttree); /* only care about copying size */
4378         cost_agg(&agg_path, root,
4379                          aggstrategy, aggcosts,
4380                          numGroupCols, numGroups,
4381                          lefttree->startup_cost,
4382                          lefttree->total_cost,
4383                          lefttree->plan_rows);
4384         plan->startup_cost = agg_path.startup_cost;
4385         plan->total_cost = agg_path.total_cost;
4386
4387         /*
4388          * We will produce a single output tuple if not grouping, and a tuple per
4389          * group otherwise.
4390          */
4391         if (aggstrategy == AGG_PLAIN)
4392                 plan->plan_rows = 1;
4393         else
4394                 plan->plan_rows = numGroups;
4395
4396         /*
4397          * We also need to account for the cost of evaluation of the qual (ie, the
4398          * HAVING clause) and the tlist.  Note that cost_qual_eval doesn't charge
4399          * anything for Aggref nodes; this is okay since they are really
4400          * comparable to Vars.
4401          *
4402          * See notes in add_tlist_costs_to_plan about why only make_agg,
4403          * make_windowagg and make_group worry about tlist eval cost.
4404          */
4405         if (qual)
4406         {
4407                 cost_qual_eval(&qual_cost, qual, root);
4408                 plan->startup_cost += qual_cost.startup;
4409                 plan->total_cost += qual_cost.startup;
4410                 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4411         }
4412         add_tlist_costs_to_plan(root, plan, tlist);
4413
4414         plan->qual = qual;
4415         plan->targetlist = tlist;
4416         plan->lefttree = lefttree;
4417         plan->righttree = NULL;
4418
4419         return node;
4420 }
4421
4422 WindowAgg *
4423 make_windowagg(PlannerInfo *root, List *tlist,
4424                            List *windowFuncs, Index winref,
4425                            int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
4426                            int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
4427                            int frameOptions, Node *startOffset, Node *endOffset,
4428                            Plan *lefttree)
4429 {
4430         WindowAgg  *node = makeNode(WindowAgg);
4431         Plan       *plan = &node->plan;
4432         Path            windowagg_path; /* dummy for result of cost_windowagg */
4433
4434         node->winref = winref;
4435         node->partNumCols = partNumCols;
4436         node->partColIdx = partColIdx;
4437         node->partOperators = partOperators;
4438         node->ordNumCols = ordNumCols;
4439         node->ordColIdx = ordColIdx;
4440         node->ordOperators = ordOperators;
4441         node->frameOptions = frameOptions;
4442         node->startOffset = startOffset;
4443         node->endOffset = endOffset;
4444
4445         copy_plan_costsize(plan, lefttree); /* only care about copying size */
4446         cost_windowagg(&windowagg_path, root,
4447                                    windowFuncs, partNumCols, ordNumCols,
4448                                    lefttree->startup_cost,
4449                                    lefttree->total_cost,
4450                                    lefttree->plan_rows);
4451         plan->startup_cost = windowagg_path.startup_cost;
4452         plan->total_cost = windowagg_path.total_cost;
4453
4454         /*
4455          * We also need to account for the cost of evaluation of the tlist.
4456          *
4457          * See notes in add_tlist_costs_to_plan about why only make_agg,
4458          * make_windowagg and make_group worry about tlist eval cost.
4459          */
4460         add_tlist_costs_to_plan(root, plan, tlist);
4461
4462         plan->targetlist = tlist;
4463         plan->lefttree = lefttree;
4464         plan->righttree = NULL;
4465         /* WindowAgg nodes never have a qual clause */
4466         plan->qual = NIL;
4467
4468         return node;
4469 }
4470
4471 Group *
4472 make_group(PlannerInfo *root,
4473                    List *tlist,
4474                    List *qual,
4475                    int numGroupCols,
4476                    AttrNumber *grpColIdx,
4477                    Oid *grpOperators,
4478                    double numGroups,
4479                    Plan *lefttree)
4480 {
4481         Group      *node = makeNode(Group);
4482         Plan       *plan = &node->plan;
4483         Path            group_path;             /* dummy for result of cost_group */
4484         QualCost        qual_cost;
4485
4486         node->numCols = numGroupCols;
4487         node->grpColIdx = grpColIdx;
4488         node->grpOperators = grpOperators;
4489
4490         copy_plan_costsize(plan, lefttree); /* only care about copying size */
4491         cost_group(&group_path, root,
4492                            numGroupCols, numGroups,
4493                            lefttree->startup_cost,
4494                            lefttree->total_cost,
4495                            lefttree->plan_rows);
4496         plan->startup_cost = group_path.startup_cost;
4497         plan->total_cost = group_path.total_cost;
4498
4499         /* One output tuple per estimated result group */
4500         plan->plan_rows = numGroups;
4501
4502         /*
4503          * We also need to account for the cost of evaluation of the qual (ie, the
4504          * HAVING clause) and the tlist.
4505          *
4506          * XXX this double-counts the cost of evaluation of any expressions used
4507          * for grouping, since in reality those will have been evaluated at a
4508          * lower plan level and will only be copied by the Group node. Worth
4509          * fixing?
4510          *
4511          * See notes in add_tlist_costs_to_plan about why only make_agg,
4512          * make_windowagg and make_group worry about tlist eval cost.
4513          */
4514         if (qual)
4515         {
4516                 cost_qual_eval(&qual_cost, qual, root);
4517                 plan->startup_cost += qual_cost.startup;
4518                 plan->total_cost += qual_cost.startup;
4519                 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4520         }
4521         add_tlist_costs_to_plan(root, plan, tlist);
4522
4523         plan->qual = qual;
4524         plan->targetlist = tlist;
4525         plan->lefttree = lefttree;
4526         plan->righttree = NULL;
4527
4528         return node;
4529 }
4530
4531 /*
4532  * distinctList is a list of SortGroupClauses, identifying the targetlist items
4533  * that should be considered by the Unique filter.  The input path must
4534  * already be sorted accordingly.
4535  */
4536 Unique *
4537 make_unique(Plan *lefttree, List *distinctList)
4538 {
4539         Unique     *node = makeNode(Unique);
4540         Plan       *plan = &node->plan;
4541         int                     numCols = list_length(distinctList);
4542         int                     keyno = 0;
4543         AttrNumber *uniqColIdx;
4544         Oid                *uniqOperators;
4545         ListCell   *slitem;
4546
4547         copy_plan_costsize(plan, lefttree);
4548
4549         /*
4550          * Charge one cpu_operator_cost per comparison per input tuple. We assume
4551          * all columns get compared at most of the tuples.  (XXX probably this is
4552          * an overestimate.)
4553          */
4554         plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
4555
4556         /*
4557          * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
4558          * we assume the filter removes nothing.  The caller must alter this if he
4559          * has a better idea.
4560          */
4561
4562         plan->targetlist = lefttree->targetlist;
4563         plan->qual = NIL;
4564         plan->lefttree = lefttree;
4565         plan->righttree = NULL;
4566
4567         /*
4568          * convert SortGroupClause list into arrays of attr indexes and equality
4569          * operators, as wanted by executor
4570          */
4571         Assert(numCols > 0);
4572         uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4573         uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4574
4575         foreach(slitem, distinctList)
4576         {
4577                 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4578                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4579
4580                 uniqColIdx[keyno] = tle->resno;
4581                 uniqOperators[keyno] = sortcl->eqop;
4582                 Assert(OidIsValid(uniqOperators[keyno]));
4583                 keyno++;
4584         }
4585
4586         node->numCols = numCols;
4587         node->uniqColIdx = uniqColIdx;
4588         node->uniqOperators = uniqOperators;
4589
4590         return node;
4591 }
4592
4593 /*
4594  * distinctList is a list of SortGroupClauses, identifying the targetlist
4595  * items that should be considered by the SetOp filter.  The input path must
4596  * already be sorted accordingly.
4597  */
4598 SetOp *
4599 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
4600                    List *distinctList, AttrNumber flagColIdx, int firstFlag,
4601                    long numGroups, double outputRows)
4602 {
4603         SetOp      *node = makeNode(SetOp);
4604         Plan       *plan = &node->plan;
4605         int                     numCols = list_length(distinctList);
4606         int                     keyno = 0;
4607         AttrNumber *dupColIdx;
4608         Oid                *dupOperators;
4609         ListCell   *slitem;
4610
4611         copy_plan_costsize(plan, lefttree);
4612         plan->plan_rows = outputRows;
4613
4614         /*
4615          * Charge one cpu_operator_cost per comparison per input tuple. We assume
4616          * all columns get compared at most of the tuples.
4617          */
4618         plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4619
4620         plan->targetlist = lefttree->targetlist;
4621         plan->qual = NIL;
4622         plan->lefttree = lefttree;
4623         plan->righttree = NULL;
4624
4625         /*
4626          * convert SortGroupClause list into arrays of attr indexes and equality
4627          * operators, as wanted by executor
4628          */
4629         Assert(numCols > 0);
4630         dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4631         dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4632
4633         foreach(slitem, distinctList)
4634         {
4635                 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4636                 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4637
4638                 dupColIdx[keyno] = tle->resno;
4639                 dupOperators[keyno] = sortcl->eqop;
4640                 Assert(OidIsValid(dupOperators[keyno]));
4641                 keyno++;
4642         }
4643
4644         node->cmd = cmd;
4645         node->strategy = strategy;
4646         node->numCols = numCols;
4647         node->dupColIdx = dupColIdx;
4648         node->dupOperators = dupOperators;
4649         node->flagColIdx = flagColIdx;
4650         node->firstFlag = firstFlag;
4651         node->numGroups = numGroups;
4652
4653         return node;
4654 }
4655
4656 /*
4657  * make_lockrows
4658  *        Build a LockRows plan node
4659  */
4660 LockRows *
4661 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4662 {
4663         LockRows   *node = makeNode(LockRows);
4664         Plan       *plan = &node->plan;
4665
4666         copy_plan_costsize(plan, lefttree);
4667
4668         /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4669         plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4670
4671         plan->targetlist = lefttree->targetlist;
4672         plan->qual = NIL;
4673         plan->lefttree = lefttree;
4674         plan->righttree = NULL;
4675
4676         node->rowMarks = rowMarks;
4677         node->epqParam = epqParam;
4678
4679         return node;
4680 }
4681
4682 /*
4683  * Note: offset_est and count_est are passed in to save having to repeat
4684  * work already done to estimate the values of the limitOffset and limitCount
4685  * expressions.  Their values are as returned by preprocess_limit (0 means
4686  * "not relevant", -1 means "couldn't estimate").  Keep the code below in sync
4687  * with that function!
4688  */
4689 Limit *
4690 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4691                    int64 offset_est, int64 count_est)
4692 {
4693         Limit      *node = makeNode(Limit);
4694         Plan       *plan = &node->plan;
4695
4696         copy_plan_costsize(plan, lefttree);
4697
4698         /*
4699          * Adjust the output rows count and costs according to the offset/limit.
4700          * This is only a cosmetic issue if we are at top level, but if we are
4701          * building a subquery then it's important to report correct info to the
4702          * outer planner.
4703          *
4704          * When the offset or count couldn't be estimated, use 10% of the
4705          * estimated number of rows emitted from the subplan.
4706          */
4707         if (offset_est != 0)
4708         {
4709                 double          offset_rows;
4710
4711                 if (offset_est > 0)
4712                         offset_rows = (double) offset_est;
4713                 else
4714                         offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4715                 if (offset_rows > plan->plan_rows)
4716                         offset_rows = plan->plan_rows;
4717                 if (plan->plan_rows > 0)
4718                         plan->startup_cost +=
4719                                 (plan->total_cost - plan->startup_cost)
4720                                 * offset_rows / plan->plan_rows;
4721                 plan->plan_rows -= offset_rows;
4722                 if (plan->plan_rows < 1)
4723                         plan->plan_rows = 1;
4724         }
4725
4726         if (count_est != 0)
4727         {
4728                 double          count_rows;
4729
4730                 if (count_est > 0)
4731                         count_rows = (double) count_est;
4732                 else
4733                         count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4734                 if (count_rows > plan->plan_rows)
4735                         count_rows = plan->plan_rows;
4736                 if (plan->plan_rows > 0)
4737                         plan->total_cost = plan->startup_cost +
4738                                 (plan->total_cost - plan->startup_cost)
4739                                 * count_rows / plan->plan_rows;
4740                 plan->plan_rows = count_rows;
4741                 if (plan->plan_rows < 1)
4742                         plan->plan_rows = 1;
4743         }
4744
4745         plan->targetlist = lefttree->targetlist;
4746         plan->qual = NIL;
4747         plan->lefttree = lefttree;
4748         plan->righttree = NULL;
4749
4750         node->limitOffset = limitOffset;
4751         node->limitCount = limitCount;
4752
4753         return node;
4754 }
4755
4756 /*
4757  * make_result
4758  *        Build a Result plan node
4759  *
4760  * If we have a subplan, assume that any evaluation costs for the gating qual
4761  * were already factored into the subplan's startup cost, and just copy the
4762  * subplan cost.  If there's no subplan, we should include the qual eval
4763  * cost.  In either case, tlist eval cost is not to be included here.
4764  */
4765 Result *
4766 make_result(PlannerInfo *root,
4767                         List *tlist,
4768                         Node *resconstantqual,
4769                         Plan *subplan)
4770 {
4771         Result     *node = makeNode(Result);
4772         Plan       *plan = &node->plan;
4773
4774         if (subplan)
4775                 copy_plan_costsize(plan, subplan);
4776         else
4777         {
4778                 plan->startup_cost = 0;
4779                 plan->total_cost = cpu_tuple_cost;
4780                 plan->plan_rows = 1;    /* wrong if we have a set-valued function? */
4781                 plan->plan_width = 0;   /* XXX is it worth being smarter? */
4782                 if (resconstantqual)
4783                 {
4784                         QualCost        qual_cost;
4785
4786                         cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
4787                         /* resconstantqual is evaluated once at startup */
4788                         plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
4789                         plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
4790                 }
4791         }
4792
4793         plan->targetlist = tlist;
4794         plan->qual = NIL;
4795         plan->lefttree = subplan;
4796         plan->righttree = NULL;
4797         node->resconstantqual = resconstantqual;
4798
4799         return node;
4800 }
4801
4802 /*
4803  * make_modifytable
4804  *        Build a ModifyTable plan node
4805  *
4806  * Currently, we don't charge anything extra for the actual table modification
4807  * work, nor for the WITH CHECK OPTIONS or RETURNING expressions if any.  It
4808  * would only be window dressing, since these are always top-level nodes and
4809  * there is no way for the costs to change any higher-level planning choices.
4810  * But we might want to make it look better sometime.
4811  */
4812 ModifyTable *
4813 make_modifytable(PlannerInfo *root,
4814                                  CmdType operation, bool canSetTag,
4815                                  Index nominalRelation,
4816                                  List *resultRelations, List *subplans,
4817                                  List *withCheckOptionLists, List *returningLists,
4818                                  List *rowMarks, int epqParam)
4819 {
4820         ModifyTable *node = makeNode(ModifyTable);
4821         Plan       *plan = &node->plan;
4822         double          total_size;
4823         List       *fdw_private_list;
4824         ListCell   *subnode;
4825         ListCell   *lc;
4826         int                     i;
4827
4828         Assert(list_length(resultRelations) == list_length(subplans));
4829         Assert(withCheckOptionLists == NIL ||
4830                    list_length(resultRelations) == list_length(withCheckOptionLists));
4831         Assert(returningLists == NIL ||
4832                    list_length(resultRelations) == list_length(returningLists));
4833
4834         /*
4835          * Compute cost as sum of subplan costs.
4836          */
4837         plan->startup_cost = 0;
4838         plan->total_cost = 0;
4839         plan->plan_rows = 0;
4840         total_size = 0;
4841         foreach(subnode, subplans)
4842         {
4843                 Plan       *subplan = (Plan *) lfirst(subnode);
4844
4845                 if (subnode == list_head(subplans))             /* first node? */
4846                         plan->startup_cost = subplan->startup_cost;
4847                 plan->total_cost += subplan->total_cost;
4848                 plan->plan_rows += subplan->plan_rows;
4849                 total_size += subplan->plan_width * subplan->plan_rows;
4850         }
4851         if (plan->plan_rows > 0)
4852                 plan->plan_width = rint(total_size / plan->plan_rows);
4853         else
4854                 plan->plan_width = 0;
4855
4856         node->plan.lefttree = NULL;
4857         node->plan.righttree = NULL;
4858         node->plan.qual = NIL;
4859         /* setrefs.c will fill in the targetlist, if needed */
4860         node->plan.targetlist = NIL;
4861
4862         node->operation = operation;
4863         node->canSetTag = canSetTag;
4864         node->nominalRelation = nominalRelation;
4865         node->resultRelations = resultRelations;
4866         node->resultRelIndex = -1;      /* will be set correctly in setrefs.c */
4867         node->plans = subplans;
4868         node->withCheckOptionLists = withCheckOptionLists;
4869         node->returningLists = returningLists;
4870         node->rowMarks = rowMarks;
4871         node->epqParam = epqParam;
4872
4873         /*
4874          * For each result relation that is a foreign table, allow the FDW to
4875          * construct private plan data, and accumulate it all into a list.
4876          */
4877         fdw_private_list = NIL;
4878         i = 0;
4879         foreach(lc, resultRelations)
4880         {
4881                 Index           rti = lfirst_int(lc);
4882                 FdwRoutine *fdwroutine;
4883                 List       *fdw_private;
4884
4885                 /*
4886                  * If possible, we want to get the FdwRoutine from our RelOptInfo for
4887                  * the table.  But sometimes we don't have a RelOptInfo and must get
4888                  * it the hard way.  (In INSERT, the target relation is not scanned,
4889                  * so it's not a baserel; and there are also corner cases for
4890                  * updatable views where the target rel isn't a baserel.)
4891                  */
4892                 if (rti < root->simple_rel_array_size &&
4893                         root->simple_rel_array[rti] != NULL)
4894                 {
4895                         RelOptInfo *resultRel = root->simple_rel_array[rti];
4896
4897                         fdwroutine = resultRel->fdwroutine;
4898                 }
4899                 else
4900                 {
4901                         RangeTblEntry *rte = planner_rt_fetch(rti, root);
4902
4903                         Assert(rte->rtekind == RTE_RELATION);
4904                         if (rte->relkind == RELKIND_FOREIGN_TABLE)
4905                                 fdwroutine = GetFdwRoutineByRelId(rte->relid);
4906                         else
4907                                 fdwroutine = NULL;
4908                 }
4909
4910                 if (fdwroutine != NULL &&
4911                         fdwroutine->PlanForeignModify != NULL)
4912                         fdw_private = fdwroutine->PlanForeignModify(root, node, rti, i);
4913                 else
4914                         fdw_private = NIL;
4915                 fdw_private_list = lappend(fdw_private_list, fdw_private);
4916                 i++;
4917         }
4918         node->fdwPrivLists = fdw_private_list;
4919
4920         return node;
4921 }
4922
4923 /*
4924  * is_projection_capable_plan
4925  *              Check whether a given Plan node is able to do projection.
4926  */
4927 bool
4928 is_projection_capable_plan(Plan *plan)
4929 {
4930         /* Most plan types can project, so just list the ones that can't */
4931         switch (nodeTag(plan))
4932         {
4933                 case T_Hash:
4934                 case T_Material:
4935                 case T_Sort:
4936                 case T_Unique:
4937                 case T_SetOp:
4938                 case T_LockRows:
4939                 case T_Limit:
4940                 case T_ModifyTable:
4941                 case T_Append:
4942                 case T_MergeAppend:
4943                 case T_RecursiveUnion:
4944                         return false;
4945                 default:
4946                         break;
4947         }
4948         return true;
4949 }