1 /*-------------------------------------------------------------------------
4 * Routines to create the desired plan for processing a query.
5 * Planning is complete, we just need to convert the selected
8 * Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/backend/optimizer/plan/createplan.c
15 *-------------------------------------------------------------------------
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"
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,
111 ScanDirection indexscandir);
112 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
114 List *indexqualorig);
115 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
118 List *bitmapqualorig,
120 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
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,
136 static HashJoin *make_hashjoin(List *tlist,
137 List *joinclauses, List *otherclauses,
139 Plan *lefttree, Plan *righttree,
141 static Hash *make_hash(Plan *lefttree,
143 AttrNumber skewColumn,
146 int32 skewColTypmod);
147 static MergeJoin *make_mergejoin(List *tlist,
148 List *joinclauses, List *otherclauses,
151 Oid *mergecollations,
152 int *mergestrategies,
153 bool *mergenullsfirst,
154 Plan *lefttree, Plan *righttree,
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,
163 const AttrNumber *reqColIdx,
164 bool adjust_tlist_in_place,
166 AttrNumber **p_sortColIdx,
167 Oid **p_sortOperators,
169 bool **p_nullsFirst);
170 static EquivalenceMember *find_ec_member_for_tle(EquivalenceClass *ec,
173 static Material *make_material(Plan *lefttree);
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.
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.
187 * best_path is the best access path
189 * Returns a Plan tree.
192 create_plan(PlannerInfo *root, Path *best_path)
196 /* plan_params should not be in use in current query level */
197 Assert(root->plan_params == NIL);
199 /* Initialize this module's private workspace in PlannerInfo */
200 root->curOuterRels = NULL;
201 root->curOuterParams = NIL;
203 /* Recursively process the path tree */
204 plan = create_plan_recurse(root, best_path);
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");
211 * Reset plan_params to ensure param IDs used for nestloop params are not
214 root->plan_params = NIL;
220 * create_plan_recurse
221 * Recursive guts of create_plan().
224 create_plan_recurse(PlannerInfo *root, Path *best_path)
228 switch (best_path->pathtype)
232 case T_IndexOnlyScan:
233 case T_BitmapHeapScan:
239 case T_WorkTableScan:
242 plan = create_scan_plan(root, best_path);
247 plan = create_join_plan(root,
248 (JoinPath *) best_path);
251 plan = create_append_plan(root,
252 (AppendPath *) best_path);
255 plan = create_merge_append_plan(root,
256 (MergeAppendPath *) best_path);
259 plan = (Plan *) create_result_plan(root,
260 (ResultPath *) best_path);
263 plan = (Plan *) create_material_plan(root,
264 (MaterialPath *) best_path);
267 plan = create_unique_plan(root,
268 (UniquePath *) best_path);
271 elog(ERROR, "unrecognized node type: %d",
272 (int) best_path->pathtype);
273 plan = NULL; /* keep compiler quiet */
282 * Create a scan plan for the parent relation of 'best_path'.
285 create_scan_plan(PlannerInfo *root, Path *best_path)
287 RelOptInfo *rel = best_path->parent;
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
300 if (use_physical_tlist(root, rel))
302 if (best_path->pathtype == T_IndexOnlyScan)
304 /* For index-only scan, the preferred tlist is the index's */
305 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
309 tlist = build_physical_tlist(root, rel);
310 /* if fail because of dropped cols, use regular method */
312 tlist = build_path_tlist(root, best_path);
317 tlist = build_path_tlist(root, best_path);
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.
325 scan_clauses = rel->baserestrictinfo;
328 * If this is a parameterized scan, we also need to enforce all the join
329 * clauses available from the outer relation(s).
331 * For paranoia's sake, don't modify the stored baserestrictinfo list.
333 if (best_path->param_info)
334 scan_clauses = list_concat(list_copy(scan_clauses),
335 best_path->param_info->ppi_clauses);
337 switch (best_path->pathtype)
340 plan = (Plan *) create_seqscan_plan(root,
347 plan = (Plan *) create_indexscan_plan(root,
348 (IndexPath *) best_path,
354 case T_IndexOnlyScan:
355 plan = (Plan *) create_indexscan_plan(root,
356 (IndexPath *) best_path,
362 case T_BitmapHeapScan:
363 plan = (Plan *) create_bitmap_scan_plan(root,
364 (BitmapHeapPath *) best_path,
370 plan = (Plan *) create_tidscan_plan(root,
371 (TidPath *) best_path,
377 plan = (Plan *) create_subqueryscan_plan(root,
384 plan = (Plan *) create_functionscan_plan(root,
391 plan = (Plan *) create_valuesscan_plan(root,
398 plan = (Plan *) create_ctescan_plan(root,
404 case T_WorkTableScan:
405 plan = (Plan *) create_worktablescan_plan(root,
412 plan = (Plan *) create_foreignscan_plan(root,
413 (ForeignPath *) best_path,
419 plan = (Plan *) create_customscan_plan(root,
420 (CustomPath *) best_path,
426 elog(ERROR, "unrecognized node type: %d",
427 (int) best_path->pathtype);
428 plan = NULL; /* keep compiler quiet */
433 * If there are any pseudoconstant clauses attached to this node, insert a
434 * gating Result node that evaluates the pseudoconstants as one-time
437 if (root->hasPseudoConstantQuals)
438 plan = create_gating_plan(root, plan, scan_clauses);
444 * Build a target list (ie, a list of TargetEntry) for the Path's output.
447 build_path_tlist(PlannerInfo *root, Path *path)
449 RelOptInfo *rel = path->parent;
454 foreach(v, rel->reltargetlist)
456 /* Do we really need to copy here? Not sure */
457 Node *node = (Node *) copyObject(lfirst(v));
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
465 if (path->param_info)
466 node = replace_nestloop_params(root, node);
468 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
479 * Decide whether to use a tlist matching relation structure,
480 * rather than only those Vars actually referenced.
483 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
489 * We can do this for real relation scans, subquery scans, function scans,
490 * values scans, and CTE scans (but not for, eg, joins).
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)
500 * Can't do it with inheritance cases either (mainly because Append
503 if (rel->reloptkind != RELOPT_BASEREL)
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.)
511 for (i = rel->min_attr; i <= 0; i++)
513 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
518 * Can't do it if the rel is required to emit any placeholder expressions,
521 foreach(lc, root->placeholder_list)
523 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
525 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
526 bms_is_subset(phinfo->ph_eval_at, rel->relids))
534 * disuse_physical_tlist
535 * Switch a plan node back to emitting only Vars actually referenced.
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.
543 disuse_physical_tlist(PlannerInfo *root, Plan *plan, Path *path)
545 /* Only need to undo it for path types handled by create_scan_plan() */
546 switch (path->pathtype)
550 case T_IndexOnlyScan:
551 case T_BitmapHeapScan:
557 case T_WorkTableScan:
560 plan->targetlist = build_path_tlist(root, path);
569 * Deal with pseudoconstant qual clauses
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.
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
587 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
589 List *pseudoconstants;
591 /* Sort into desirable execution order while still in RestrictInfo form */
592 quals = order_qual_clauses(root, quals);
594 /* Pull out any pseudoconstant quals from the RestrictInfo list */
595 pseudoconstants = extract_actual_clauses(quals, true);
597 if (!pseudoconstants)
600 return (Plan *) make_result(root,
602 (Node *) pseudoconstants,
608 * Create a join plan for 'best_path' and (recursively) plans for its
609 * inner and outer paths.
612 create_join_plan(PlannerInfo *root, JoinPath *best_path)
617 Relids saveOuterRels = root->curOuterRels;
619 outer_plan = create_plan_recurse(root, best_path->outerjoinpath);
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);
626 inner_plan = create_plan_recurse(root, best_path->innerjoinpath);
628 switch (best_path->path.pathtype)
631 plan = (Plan *) create_mergejoin_plan(root,
632 (MergePath *) best_path,
637 plan = (Plan *) create_hashjoin_plan(root,
638 (HashPath *) best_path,
643 /* Restore curOuterRels */
644 bms_free(root->curOuterRels);
645 root->curOuterRels = saveOuterRels;
647 plan = (Plan *) create_nestloop_plan(root,
648 (NestPath *) best_path,
653 elog(ERROR, "unrecognized node type: %d",
654 (int) best_path->path.pathtype);
655 plan = NULL; /* keep compiler quiet */
660 * If there are any pseudoconstant clauses attached to this node, insert a
661 * gating Result node that evaluates the pseudoconstants as one-time
664 if (root->hasPseudoConstantQuals)
665 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
670 * * Expensive function pullups may have pulled local predicates * into
671 * this path node. Put them in the qpqual of the plan node. * JMH,
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))));
685 * Create an Append plan for 'best_path' and (recursively) plans
688 * Returns a Plan node.
691 create_append_plan(PlannerInfo *root, AppendPath *best_path)
694 List *tlist = build_path_tlist(root, &best_path->path);
695 List *subplans = NIL;
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
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).
707 if (best_path->subpaths == NIL)
709 /* Generate a Result plan with constant-FALSE gating qual */
710 return (Plan *) make_result(root,
712 (Node *) list_make1(makeBoolConst(false,
717 /* Build the plan for each child */
718 foreach(subpaths, best_path->subpaths)
720 Path *subpath = (Path *) lfirst(subpaths);
722 subplans = lappend(subplans, create_plan_recurse(root, subpath));
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.
732 plan = make_append(subplans, tlist);
734 return (Plan *) plan;
738 * create_merge_append_plan
739 * Create a MergeAppend plan for 'best_path' and (recursively) plans
742 * Returns a Plan node.
745 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
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;
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.
760 copy_path_costsize(plan, (Path *) best_path);
761 plan->targetlist = tlist;
763 plan->lefttree = NULL;
764 plan->righttree = NULL;
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,
773 &node->sortOperators,
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.
782 foreach(subpaths, best_path->subpaths)
784 Path *subpath = (Path *) lfirst(subpaths);
787 AttrNumber *sortColIdx;
792 /* Build the child plan */
793 subplan = create_plan_recurse(root, subpath);
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,
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.
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);
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);
830 subplans = lappend(subplans, subplan);
833 node->mergeplans = subplans;
835 return (Plan *) node;
840 * Create a Result plan for 'best_path'.
841 * This is only used for the case of a query with an empty jointree.
843 * Returns a Plan node.
846 create_result_plan(PlannerInfo *root, ResultPath *best_path)
851 /* The tlist will be installed later, since we have no RelOptInfo */
852 Assert(best_path->path.parent == NULL);
855 /* best_path->quals is just bare clauses */
857 quals = order_qual_clauses(root, best_path->quals);
859 return make_result(root, tlist, (Node *) quals, NULL);
863 * create_material_plan
864 * Create a Material plan for 'best_path' and (recursively) plans
867 * Returns a Plan node.
870 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
875 subplan = create_plan_recurse(root, best_path->subpath);
877 /* We don't want any excess columns in the materialized tuples */
878 disuse_physical_tlist(root, subplan, best_path->subpath);
880 plan = make_material(subplan);
882 copy_path_costsize(&plan->plan, (Path *) best_path);
889 * Create a Unique plan for 'best_path' and (recursively) plans
892 * Returns a Plan node.
895 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
905 AttrNumber *groupColIdx;
909 subplan = create_plan_recurse(root, best_path->subpath);
911 /* Done if we don't need to do any actual unique-ifying */
912 if (best_path->umethod == UNIQUE_PATH_NOOP)
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.
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.
931 in_operators = best_path->in_operators;
932 uniq_exprs = best_path->uniq_exprs;
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;
939 foreach(l, uniq_exprs)
941 Node *uniqexpr = lfirst(l);
944 tle = tlist_member(uniqexpr, newtlist);
947 tle = makeTargetEntry((Expr *) uniqexpr,
951 newtlist = lappend(newtlist, tle);
957 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
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
964 if (!is_projection_capable_plan(subplan) &&
965 !tlist_same_exprs(newtlist, subplan->targetlist))
966 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
968 subplan->targetlist = newtlist;
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.
977 newtlist = subplan->targetlist;
978 numGroupCols = list_length(uniq_exprs);
979 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
982 foreach(l, uniq_exprs)
984 Node *uniqexpr = lfirst(l);
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;
993 if (best_path->umethod == UNIQUE_PATH_HASH)
998 numGroups = (long) Min(best_path->path.rows, (double) LONG_MAX);
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.
1006 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
1008 foreach(l, in_operators)
1010 Oid in_oper = lfirst_oid(l);
1013 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
1014 elog(ERROR, "could not find compatible hash operator for operator %u",
1016 groupOperators[groupColPos++] = eq_oper;
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
1024 plan = (Plan *) make_agg(root,
1025 build_path_tlist(root, &best_path->path),
1037 List *sortList = NIL;
1039 /* Create an ORDER BY list to sort the input compatibly */
1041 foreach(l, in_operators)
1043 Oid in_oper = lfirst_oid(l);
1047 SortGroupClause *sortcl;
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",
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.
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",
1065 tle = get_tle_by_resno(subplan->targetlist,
1066 groupColIdx[groupColPos]);
1067 Assert(tle != NULL);
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);
1079 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
1080 plan = (Plan *) make_unique(plan, sortList);
1083 /* Adjust output size estimate (other fields should be OK already) */
1084 plan->plan_rows = best_path->path.rows;
1090 /*****************************************************************************
1092 * BASE-RELATION SCAN METHODS
1094 *****************************************************************************/
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'.
1103 create_seqscan_plan(PlannerInfo *root, Path *best_path,
1104 List *tlist, List *scan_clauses)
1107 Index scan_relid = best_path->parent->relid;
1109 /* it should be a base rel... */
1110 Assert(scan_relid > 0);
1111 Assert(best_path->parent->rtekind == RTE_RELATION);
1113 /* Sort clauses into best execution order */
1114 scan_clauses = order_qual_clauses(root, scan_clauses);
1116 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1117 scan_clauses = extract_actual_clauses(scan_clauses, false);
1119 /* Replace any outer-relation variables with nestloop params */
1120 if (best_path->param_info)
1122 scan_clauses = (List *)
1123 replace_nestloop_params(root, (Node *) scan_clauses);
1126 scan_plan = make_seqscan(tlist,
1130 copy_path_costsize(&scan_plan->plan, best_path);
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'.
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.
1146 create_indexscan_plan(PlannerInfo *root,
1147 IndexPath *best_path,
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;
1158 List *stripped_indexquals;
1159 List *fixed_indexquals;
1160 List *fixed_indexorderbys;
1163 /* it should be a base rel... */
1164 Assert(baserelid > 0);
1165 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1168 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
1169 * executor as indexqualorig
1171 stripped_indexquals = get_actual_clauses(indexquals);
1174 * The executor needs a copy with the indexkey on the left of each clause
1175 * and with index Vars substituted for table ones.
1177 fixed_indexquals = fix_indexqual_references(root, best_path);
1180 * Likewise fix up index attr references in the ORDER BY expressions.
1182 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
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.
1193 * In normal cases simple pointer equality checks will be enough to spot
1194 * duplicate RestrictInfos, so we try that first.
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.)
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
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.
1214 * Note: if you change this bit of code you should also look at
1215 * extract_nonindex_conditions() in costsize.c.
1218 foreach(l, scan_clauses)
1220 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
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))
1231 List *clausel = list_make1(rinfo->clause);
1233 if (predicate_implied_by(clausel, indexquals))
1234 continue; /* provably implied by indexquals */
1235 if (best_path->indexinfo->indpred)
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 */
1244 qpqual = lappend(qpqual, rinfo);
1247 /* Sort clauses into best execution order */
1248 qpqual = order_qual_clauses(root, qpqual);
1250 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1251 qpqual = extract_actual_clauses(qpqual, false);
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)
1262 if (best_path->path.param_info)
1264 stripped_indexquals = (List *)
1265 replace_nestloop_params(root, (Node *) stripped_indexquals);
1267 replace_nestloop_params(root, (Node *) qpqual);
1268 indexorderbys = (List *)
1269 replace_nestloop_params(root, (Node *) indexorderbys);
1272 /* Finally ready to build the plan node */
1274 scan_plan = (Scan *) make_indexonlyscan(tlist,
1279 fixed_indexorderbys,
1280 best_path->indexinfo->indextlist,
1281 best_path->indexscandir);
1283 scan_plan = (Scan *) make_indexscan(tlist,
1288 stripped_indexquals,
1289 fixed_indexorderbys,
1291 best_path->indexscandir);
1293 copy_path_costsize(&scan_plan->plan, &best_path->path);
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'.
1303 static BitmapHeapScan *
1304 create_bitmap_scan_plan(PlannerInfo *root,
1305 BitmapHeapPath *best_path,
1309 Index baserelid = best_path->path.parent->relid;
1310 Plan *bitmapqualplan;
1311 List *bitmapqualorig;
1316 BitmapHeapScan *scan_plan;
1318 /* it should be a base rel... */
1319 Assert(baserelid > 0);
1320 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1322 /* Process the bitmapqual tree into a Plan tree and qual lists */
1323 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1324 &bitmapqualorig, &indexquals,
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.
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
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().
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.
1353 foreach(l, scan_clauses)
1355 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1356 Node *clause = (Node *) rinfo->clause;
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))
1367 List *clausel = list_make1(clause);
1369 if (predicate_implied_by(clausel, indexquals))
1370 continue; /* provably implied by indexquals */
1372 qpqual = lappend(qpqual, rinfo);
1375 /* Sort clauses into best execution order */
1376 qpqual = order_qual_clauses(root, qpqual);
1378 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1379 qpqual = extract_actual_clauses(qpqual, false);
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
1387 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
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.)
1394 if (best_path->path.param_info)
1397 replace_nestloop_params(root, (Node *) qpqual);
1398 bitmapqualorig = (List *)
1399 replace_nestloop_params(root, (Node *) bitmapqualorig);
1402 /* Finally ready to build the plan node */
1403 scan_plan = make_bitmap_heapscan(tlist,
1409 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1415 * Given a bitmapqual tree, generate the Plan tree that implements it
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.
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.)
1435 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1436 List **qual, List **indexqual, List **indexECs)
1440 if (IsA(bitmapqual, BitmapAndPath))
1442 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1443 List *subplans = NIL;
1444 List *subquals = NIL;
1445 List *subindexquals = NIL;
1446 List *subindexECs = NIL;
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.
1456 foreach(l, apath->bitmapquals)
1463 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1464 &subqual, &subindexqual,
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);
1472 plan = (Plan *) make_bitmap_and(subplans);
1473 plan->startup_cost = apath->path.startup_cost;
1474 plan->total_cost = apath->path.total_cost;
1476 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1477 plan->plan_width = 0; /* meaningless */
1479 *indexqual = subindexquals;
1480 *indexECs = subindexECs;
1482 else if (IsA(bitmapqual, BitmapOrPath))
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;
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.
1501 foreach(l, opath->bitmapquals)
1508 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1509 &subqual, &subindexqual,
1511 subplans = lappend(subplans, subplan);
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));
1525 * In the presence of ScalarArrayOpExpr quals, we might have built
1526 * BitmapOrPaths with just one subpath; don't add an OR step.
1528 if (list_length(subplans) == 1)
1530 plan = (Plan *) linitial(subplans);
1534 plan = (Plan *) make_bitmap_or(subplans);
1535 plan->startup_cost = opath->path.startup_cost;
1536 plan->total_cost = opath->path.total_cost;
1538 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1539 plan->plan_width = 0; /* meaningless */
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.
1547 if (const_true_subqual)
1549 else if (list_length(subquals) <= 1)
1552 *qual = list_make1(make_orclause(subquals));
1553 if (const_true_subindexqual)
1555 else if (list_length(subindexquals) <= 1)
1556 *indexqual = subindexquals;
1558 *indexqual = list_make1(make_orclause(subindexquals));
1561 else if (IsA(bitmapqual, IndexPath))
1563 IndexPath *ipath = (IndexPath *) bitmapqual;
1568 /* Use the regular indexscan plan build machinery... */
1569 iscan = (IndexScan *) create_indexscan_plan(root, ipath,
1571 Assert(IsA(iscan, IndexScan));
1572 /* then convert to a bitmap indexscan */
1573 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1576 iscan->indexqualorig);
1577 plan->startup_cost = 0.0;
1578 plan->total_cost = ipath->indextotalcost;
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)
1586 Expr *pred = (Expr *) lfirst(l);
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.
1594 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1596 *qual = lappend(*qual, pred);
1597 *indexqual = lappend(*indexqual, pred);
1601 foreach(l, ipath->indexquals)
1603 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1605 if (rinfo->parent_ec)
1606 subindexECs = lappend(subindexECs, rinfo->parent_ec);
1608 *indexECs = subindexECs;
1612 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1613 plan = NULL; /* keep compiler quiet */
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'.
1625 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1626 List *tlist, List *scan_clauses)
1629 Index scan_relid = best_path->path.parent->relid;
1630 List *tidquals = best_path->tidquals;
1633 /* it should be a base rel... */
1634 Assert(scan_relid > 0);
1635 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1637 /* Sort clauses into best execution order */
1638 scan_clauses = order_qual_clauses(root, scan_clauses);
1640 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1641 scan_clauses = extract_actual_clauses(scan_clauses, false);
1643 /* Replace any outer-relation variables with nestloop params */
1644 if (best_path->path.param_info)
1647 replace_nestloop_params(root, (Node *) tidquals);
1648 scan_clauses = (List *)
1649 replace_nestloop_params(root, (Node *) scan_clauses);
1653 * Remove any clauses that are TID quals. This is a bit tricky since the
1654 * tidquals list has implicit OR semantics.
1656 ortidquals = tidquals;
1657 if (list_length(ortidquals) > 1)
1658 ortidquals = list_make1(make_orclause(ortidquals));
1659 scan_clauses = list_difference(scan_clauses, ortidquals);
1661 scan_plan = make_tidscan(tlist,
1666 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
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'.
1676 static SubqueryScan *
1677 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1678 List *tlist, List *scan_clauses)
1680 SubqueryScan *scan_plan;
1681 Index scan_relid = best_path->parent->relid;
1683 /* it should be a subquery base rel... */
1684 Assert(scan_relid > 0);
1685 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1687 /* Sort clauses into best execution order */
1688 scan_clauses = order_qual_clauses(root, scan_clauses);
1690 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1691 scan_clauses = extract_actual_clauses(scan_clauses, false);
1693 /* Replace any outer-relation variables with nestloop params */
1694 if (best_path->param_info)
1696 scan_clauses = (List *)
1697 replace_nestloop_params(root, (Node *) scan_clauses);
1698 process_subquery_nestloop_params(root,
1699 best_path->parent->subplan_params);
1702 scan_plan = make_subqueryscan(tlist,
1705 best_path->parent->subplan);
1707 copy_path_costsize(&scan_plan->scan.plan, best_path);
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'.
1717 static FunctionScan *
1718 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1719 List *tlist, List *scan_clauses)
1721 FunctionScan *scan_plan;
1722 Index scan_relid = best_path->parent->relid;
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;
1732 /* Sort clauses into best execution order */
1733 scan_clauses = order_qual_clauses(root, scan_clauses);
1735 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1736 scan_clauses = extract_actual_clauses(scan_clauses, false);
1738 /* Replace any outer-relation variables with nestloop params */
1739 if (best_path->param_info)
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);
1747 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1748 functions, rte->funcordinality);
1750 copy_path_costsize(&scan_plan->scan.plan, best_path);
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'.
1761 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1762 List *tlist, List *scan_clauses)
1764 ValuesScan *scan_plan;
1765 Index scan_relid = best_path->parent->relid;
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;
1775 /* Sort clauses into best execution order */
1776 scan_clauses = order_qual_clauses(root, scan_clauses);
1778 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1779 scan_clauses = extract_actual_clauses(scan_clauses, false);
1781 /* Replace any outer-relation variables with nestloop params */
1782 if (best_path->param_info)
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);
1791 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1794 copy_path_costsize(&scan_plan->scan.plan, best_path);
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'.
1805 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1806 List *tlist, List *scan_clauses)
1809 Index scan_relid = best_path->parent->relid;
1811 SubPlan *ctesplan = NULL;
1814 PlannerInfo *cteroot;
1819 Assert(scan_relid > 0);
1820 rte = planner_rt_fetch(scan_relid, root);
1821 Assert(rte->rtekind == RTE_CTE);
1822 Assert(!rte->self_reference);
1825 * Find the referenced CTE, and locate the SubPlan previously made for it.
1827 levelsup = rte->ctelevelsup;
1829 while (levelsup-- > 0)
1831 cteroot = cteroot->parent_root;
1832 if (!cteroot) /* shouldn't happen */
1833 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
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.
1842 foreach(lc, cteroot->parse->cteList)
1844 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1846 if (strcmp(cte->ctename, rte->ctename) == 0)
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)
1858 ctesplan = (SubPlan *) lfirst(lc);
1859 if (ctesplan->plan_id == plan_id)
1862 if (lc == NULL) /* shouldn't happen */
1863 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1866 * We need the CTE param ID, which is the sole member of the SubPlan's
1869 cte_param_id = linitial_int(ctesplan->setParam);
1871 /* Sort clauses into best execution order */
1872 scan_clauses = order_qual_clauses(root, scan_clauses);
1874 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1875 scan_clauses = extract_actual_clauses(scan_clauses, false);
1877 /* Replace any outer-relation variables with nestloop params */
1878 if (best_path->param_info)
1880 scan_clauses = (List *)
1881 replace_nestloop_params(root, (Node *) scan_clauses);
1884 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1885 plan_id, cte_param_id);
1887 copy_path_costsize(&scan_plan->scan.plan, best_path);
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'.
1897 static WorkTableScan *
1898 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1899 List *tlist, List *scan_clauses)
1901 WorkTableScan *scan_plan;
1902 Index scan_relid = best_path->parent->relid;
1905 PlannerInfo *cteroot;
1907 Assert(scan_relid > 0);
1908 rte = planner_rt_fetch(scan_relid, root);
1909 Assert(rte->rtekind == RTE_CTE);
1910 Assert(rte->self_reference);
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.
1917 levelsup = rte->ctelevelsup;
1918 if (levelsup == 0) /* shouldn't happen */
1919 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1922 while (levelsup-- > 0)
1924 cteroot = cteroot->parent_root;
1925 if (!cteroot) /* shouldn't happen */
1926 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1928 if (cteroot->wt_param_id < 0) /* shouldn't happen */
1929 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1931 /* Sort clauses into best execution order */
1932 scan_clauses = order_qual_clauses(root, scan_clauses);
1934 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1935 scan_clauses = extract_actual_clauses(scan_clauses, false);
1937 /* Replace any outer-relation variables with nestloop params */
1938 if (best_path->param_info)
1940 scan_clauses = (List *)
1941 replace_nestloop_params(root, (Node *) scan_clauses);
1944 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1945 cteroot->wt_param_id);
1947 copy_path_costsize(&scan_plan->scan.plan, best_path);
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'.
1957 static ForeignScan *
1958 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
1959 List *tlist, List *scan_clauses)
1961 ForeignScan *scan_plan;
1962 RelOptInfo *rel = best_path->path.parent;
1963 Index scan_relid = rel->relid;
1965 Bitmapset *attrs_used = NULL;
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);
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.
1979 scan_clauses = order_qual_clauses(root, scan_clauses);
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).
1988 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rte->relid,
1990 tlist, scan_clauses);
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);
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
2002 if (best_path->path.param_info)
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);
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.
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.
2019 pull_varattnos((Node *) rel->reltargetlist, rel->relid, &attrs_used);
2021 /* Add all the attributes used by restriction clauses. */
2022 foreach(lc, rel->baserestrictinfo)
2024 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2026 pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
2029 /* Now, are any system columns requested from rel? */
2030 scan_plan->fsSystemCol = false;
2031 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
2033 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
2035 scan_plan->fsSystemCol = true;
2040 bms_free(attrs_used);
2046 * create_custom_plan
2048 * Transform a CustomPath into a Plan.
2051 create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
2052 List *tlist, List *scan_clauses)
2055 RelOptInfo *rel = best_path->path.parent;
2058 * Right now, all we can support is CustomScan node which is associated
2059 * with a particular base relation to be scanned.
2061 Assert(rel && rel->reloptkind == RELOPT_BASEREL);
2064 * Sort clauses into the best execution order, although custom-scan
2065 * provider can reorder them again.
2067 scan_clauses = order_qual_clauses(root, scan_clauses);
2070 * Invoke custom plan provider to create the Plan node represented by the
2073 cplan = (CustomScan *) best_path->methods->PlanCustomPath(root,
2078 Assert(IsA(cplan, CustomScan));
2081 * Copy cost data from Path to Plan; no need to make custom-plan providers
2084 copy_path_costsize(&cplan->scan.plan, &best_path->path);
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.)
2093 if (best_path->path.param_info)
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);
2105 /*****************************************************************************
2109 *****************************************************************************/
2112 create_nestloop_plan(PlannerInfo *root,
2113 NestPath *best_path,
2117 NestLoop *join_plan;
2118 List *tlist = build_path_tlist(root, &best_path->path);
2119 List *joinrestrictclauses = best_path->joinrestrictinfo;
2128 /* Sort join qual clauses into best execution order */
2129 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
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))
2135 extract_actual_join_clauses(joinrestrictclauses,
2136 &joinclauses, &otherclauses);
2140 /* We can treat all clauses alike for an inner join */
2141 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
2145 /* Replace any outer-relation variables with nestloop params */
2146 if (best_path->path.param_info)
2148 joinclauses = (List *)
2149 replace_nestloop_params(root, (Node *) joinclauses);
2150 otherclauses = (List *)
2151 replace_nestloop_params(root, (Node *) otherclauses);
2155 * Identify any nestloop parameters that should be supplied by this join
2156 * node, and move them from root->curOuterParams to the nestParams list.
2158 outerrelids = best_path->outerjoinpath->parent->relids;
2161 for (cell = list_head(root->curOuterParams); cell; cell = next)
2163 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
2166 if (IsA(nlp->paramval, Var) &&
2167 bms_is_member(nlp->paramval->varno, outerrelids))
2169 root->curOuterParams = list_delete_cell(root->curOuterParams,
2171 nestParams = lappend(nestParams, nlp);
2173 else if (IsA(nlp->paramval, PlaceHolderVar) &&
2174 bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
2176 bms_is_subset(find_placeholder_info(root,
2177 (PlaceHolderVar *) nlp->paramval,
2181 root->curOuterParams = list_delete_cell(root->curOuterParams,
2183 nestParams = lappend(nestParams, nlp);
2189 join_plan = make_nestloop(tlist,
2195 best_path->jointype);
2197 copy_path_costsize(&join_plan->join.plan, &best_path->path);
2203 create_mergejoin_plan(PlannerInfo *root,
2204 MergePath *best_path,
2208 List *tlist = build_path_tlist(root, &best_path->jpath.path);
2212 List *outerpathkeys;
2213 List *innerpathkeys;
2216 Oid *mergecollations;
2217 int *mergestrategies;
2218 bool *mergenullsfirst;
2219 MergeJoin *join_plan;
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);
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))
2233 extract_actual_join_clauses(joinclauses,
2234 &joinclauses, &otherclauses);
2238 /* We can treat all clauses alike for an inner join */
2239 joinclauses = extract_actual_clauses(joinclauses, false);
2244 * Remove the mergeclauses from the list of join qual clauses, leaving the
2245 * list of quals that must be checked as qpquals.
2247 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
2248 joinclauses = list_difference(joinclauses, mergeclauses);
2251 * Replace any outer-relation variables with nestloop params. There
2252 * should not be any in the mergeclauses.
2254 if (best_path->jpath.path.param_info)
2256 joinclauses = (List *)
2257 replace_nestloop_params(root, (Node *) joinclauses);
2258 otherclauses = (List *)
2259 replace_nestloop_params(root, (Node *) otherclauses);
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.
2267 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
2268 best_path->jpath.outerjoinpath->parent->relids);
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.
2274 if (best_path->outersortkeys)
2276 disuse_physical_tlist(root, outer_plan, best_path->jpath.outerjoinpath);
2277 outer_plan = (Plan *)
2278 make_sort_from_pathkeys(root,
2280 best_path->outersortkeys,
2282 outerpathkeys = best_path->outersortkeys;
2285 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
2287 if (best_path->innersortkeys)
2289 disuse_physical_tlist(root, inner_plan, best_path->jpath.innerjoinpath);
2290 inner_plan = (Plan *)
2291 make_sort_from_pathkeys(root,
2293 best_path->innersortkeys,
2295 innerpathkeys = best_path->innersortkeys;
2298 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
2301 * If specified, add a materialize node to shield the inner plan from the
2302 * need to handle mark/restore.
2304 if (best_path->materialize_inner)
2306 Plan *matplan = (Plan *) make_material(inner_plan);
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.)
2313 copy_plan_costsize(matplan, inner_plan);
2314 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
2316 inner_plan = matplan;
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()).
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));
2332 lop = list_head(outerpathkeys);
2333 lip = list_head(innerpathkeys);
2335 foreach(lc, best_path->path_mergeclauses)
2337 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2338 EquivalenceClass *oeclass;
2339 EquivalenceClass *ieclass;
2342 EquivalenceClass *opeclass;
2343 EquivalenceClass *ipeclass;
2346 /* fetch outer/inner eclass from mergeclause */
2347 Assert(IsA(rinfo, RestrictInfo));
2348 if (rinfo->outer_is_left)
2350 oeclass = rinfo->left_ec;
2351 ieclass = rinfo->right_ec;
2355 oeclass = rinfo->right_ec;
2356 ieclass = rinfo->left_ec;
2358 Assert(oeclass != NULL);
2359 Assert(ieclass != NULL);
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.
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.
2375 opathkey = (PathKey *) lfirst(lop);
2376 opeclass = opathkey->pk_eclass;
2377 if (oeclass == opeclass)
2379 /* fast path for typical case */
2384 /* redundant clauses ... must match something before lop */
2385 foreach(l2, outerpathkeys)
2389 opathkey = (PathKey *) lfirst(l2);
2390 opeclass = opathkey->pk_eclass;
2391 if (oeclass == opeclass)
2394 if (oeclass != opeclass)
2395 elog(ERROR, "outer pathkeys do not match mergeclauses");
2400 /* redundant clauses ... must match some already-used pathkey */
2403 foreach(l2, outerpathkeys)
2405 opathkey = (PathKey *) lfirst(l2);
2406 opeclass = opathkey->pk_eclass;
2407 if (oeclass == opeclass)
2411 elog(ERROR, "outer pathkeys do not match mergeclauses");
2416 ipathkey = (PathKey *) lfirst(lip);
2417 ipeclass = ipathkey->pk_eclass;
2418 if (ieclass == ipeclass)
2420 /* fast path for typical case */
2425 /* redundant clauses ... must match something before lip */
2426 foreach(l2, innerpathkeys)
2430 ipathkey = (PathKey *) lfirst(l2);
2431 ipeclass = ipathkey->pk_eclass;
2432 if (ieclass == ipeclass)
2435 if (ieclass != ipeclass)
2436 elog(ERROR, "inner pathkeys do not match mergeclauses");
2441 /* redundant clauses ... must match some already-used pathkey */
2444 foreach(l2, innerpathkeys)
2446 ipathkey = (PathKey *) lfirst(l2);
2447 ipeclass = ipathkey->pk_eclass;
2448 if (ieclass == ipeclass)
2452 elog(ERROR, "inner pathkeys do not match mergeclauses");
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");
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;
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.
2477 * Now we can build the mergejoin node.
2479 join_plan = make_mergejoin(tlist,
2489 best_path->jpath.jointype);
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);
2498 create_hashjoin_plan(PlannerInfo *root,
2499 HashPath *best_path,
2503 List *tlist = build_path_tlist(root, &best_path->jpath.path);
2507 Oid skewTable = InvalidOid;
2508 AttrNumber skewColumn = InvalidAttrNumber;
2509 bool skewInherit = false;
2510 Oid skewColType = InvalidOid;
2511 int32 skewColTypmod = -1;
2512 HashJoin *join_plan;
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 ... */
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))
2523 extract_actual_join_clauses(joinclauses,
2524 &joinclauses, &otherclauses);
2528 /* We can treat all clauses alike for an inner join */
2529 joinclauses = extract_actual_clauses(joinclauses, false);
2534 * Remove the hashclauses from the list of join qual clauses, leaving the
2535 * list of quals that must be checked as qpquals.
2537 hashclauses = get_actual_clauses(best_path->path_hashclauses);
2538 joinclauses = list_difference(joinclauses, hashclauses);
2541 * Replace any outer-relation variables with nestloop params. There
2542 * should not be any in the hashclauses.
2544 if (best_path->jpath.path.param_info)
2546 joinclauses = (List *)
2547 replace_nestloop_params(root, (Node *) joinclauses);
2548 otherclauses = (List *)
2549 replace_nestloop_params(root, (Node *) otherclauses);
2553 * Rearrange hashclauses, if needed, so that the outer variable is always
2556 hashclauses = get_switched_clauses(best_path->path_hashclauses,
2557 best_path->jpath.outerjoinpath->parent->relids);
2559 /* We don't want any excess columns in the hashed tuples */
2560 disuse_physical_tlist(root, inner_plan, best_path->jpath.innerjoinpath);
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);
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.)
2574 if (list_length(hashclauses) == 1)
2576 OpExpr *clause = (OpExpr *) linitial(hashclauses);
2579 Assert(is_opclause(clause));
2580 node = (Node *) linitial(clause->args);
2581 if (IsA(node, RelabelType))
2582 node = (Node *) ((RelabelType *) node)->arg;
2585 Var *var = (Var *) node;
2588 rte = root->simple_rte_array[var->varno];
2589 if (rte->rtekind == RTE_RELATION)
2591 skewTable = rte->relid;
2592 skewColumn = var->varattno;
2593 skewInherit = rte->inh;
2594 skewColType = var->vartype;
2595 skewColTypmod = var->vartypmod;
2601 * Build the hash node and hash join node.
2603 hash_plan = make_hash(inner_plan,
2609 join_plan = make_hashjoin(tlist,
2615 best_path->jpath.jointype);
2617 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2623 /*****************************************************************************
2625 * SUPPORTING ROUTINES
2627 *****************************************************************************/
2630 * replace_nestloop_params
2631 * Replace outer-relation Vars and PlaceHolderVars in the given expression
2632 * with nestloop Params
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.
2639 replace_nestloop_params(PlannerInfo *root, Node *expr)
2641 /* No setup needed for tree walk, so away we go */
2642 return replace_nestloop_params_mutator(expr, root);
2646 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2652 Var *var = (Var *) node;
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))
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)
2667 nlp = (NestLoopParam *) lfirst(lc);
2668 if (nlp->paramno == param->paramid)
2670 Assert(equal(var, nlp->paramval));
2671 /* Present, so we can just return the Param */
2672 return (Node *) param;
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;
2683 if (IsA(node, PlaceHolderVar))
2685 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2690 /* Upper-level PlaceHolderVars should be long gone at this point */
2691 Assert(phv->phlevelsup == 0);
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.
2698 if (!bms_overlap(phv->phrels, root->curOuterRels) ||
2699 !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2700 root->curOuterRels))
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.
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.
2717 PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
2719 memcpy(newphv, phv, sizeof(PlaceHolderVar));
2720 newphv->phexpr = (Expr *)
2721 replace_nestloop_params_mutator((Node *) phv->phexpr,
2723 return (Node *) newphv;
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)
2730 nlp = (NestLoopParam *) lfirst(lc);
2731 if (nlp->paramno == param->paramid)
2733 Assert(equal(phv, nlp->paramval));
2734 /* Present, so we can just return the Param */
2735 return (Node *) param;
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;
2746 return expression_tree_mutator(node,
2747 replace_nestloop_params_mutator,
2752 * process_subquery_nestloop_params
2753 * Handle params of a parameterized subquery that need to be fed
2754 * from an outer nestloop.
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.
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.
2765 process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
2769 foreach(ppl, subplan_params)
2771 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(ppl);
2773 if (IsA(pitem->item, Var))
2775 Var *var = (Var *) pitem->item;
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)
2785 nlp = (NestLoopParam *) lfirst(lc);
2786 if (nlp->paramno == pitem->paramId)
2788 Assert(equal(var, nlp->paramval));
2789 /* Present, so nothing to do */
2796 nlp = makeNode(NestLoopParam);
2797 nlp->paramno = pitem->paramId;
2798 nlp->paramval = copyObject(var);
2799 root->curOuterParams = lappend(root->curOuterParams, nlp);
2802 else if (IsA(pitem->item, PlaceHolderVar))
2804 PlaceHolderVar *phv = (PlaceHolderVar *) pitem->item;
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)
2815 nlp = (NestLoopParam *) lfirst(lc);
2816 if (nlp->paramno == pitem->paramId)
2818 Assert(equal(phv, nlp->paramval));
2819 /* Present, so nothing to do */
2826 nlp = makeNode(NestLoopParam);
2827 nlp->paramno = pitem->paramId;
2828 nlp->paramval = copyObject(phv);
2829 root->curOuterParams = lappend(root->curOuterParams, nlp);
2833 elog(ERROR, "unexpected type of subquery parameter");
2838 * fix_indexqual_references
2839 * Adjust indexqual clauses to the form the executor's indexqual
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
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).
2857 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
2859 IndexOptInfo *index = index_path->indexinfo;
2860 List *fixed_indexquals;
2864 fixed_indexquals = NIL;
2866 forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
2868 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
2869 int indexcol = lfirst_int(lci);
2872 Assert(IsA(rinfo, RestrictInfo));
2875 * Replace any outer-relation variables with nestloop params.
2877 * This also makes a copy of the clause, so it's safe to modify it
2880 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2882 if (IsA(clause, OpExpr))
2884 OpExpr *op = (OpExpr *) clause;
2886 if (list_length(op->args) != 2)
2887 elog(ERROR, "indexqual clause is not binary opclause");
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.
2894 if (!bms_equal(rinfo->left_relids, index->rel->relids))
2898 * Now replace the indexkey expression with an index Var.
2900 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2904 else if (IsA(clause, RowCompareExpr))
2906 RowCompareExpr *rc = (RowCompareExpr *) clause;
2914 * Re-discover which index columns are used in the rowcompare.
2916 newrc = adjust_rowcompare_for_index(rc,
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.
2927 if (newrc != (Expr *) rc)
2928 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
2931 * Check to see if the indexkey is on the right; if so, commute
2935 CommuteRowCompareExpr(rc);
2938 * Now replace the indexkey expressions with index Vars.
2940 Assert(list_length(rc->largs) == list_length(indexcolnos));
2941 forboth(lca, rc->largs, lcai, indexcolnos)
2943 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
2948 else if (IsA(clause, ScalarArrayOpExpr))
2950 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2952 /* Never need to commute... */
2954 /* Replace the indexkey expression with an index Var. */
2955 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2959 else if (IsA(clause, NullTest))
2961 NullTest *nt = (NullTest *) clause;
2963 /* Replace the indexkey expression with an index Var. */
2964 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2969 elog(ERROR, "unsupported indexqual type: %d",
2970 (int) nodeTag(clause));
2972 fixed_indexquals = lappend(fixed_indexquals, clause);
2975 return fixed_indexquals;
2979 * fix_indexorderby_references
2980 * Adjust indexorderby clauses to the form the executor's index
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.
2990 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
2992 IndexOptInfo *index = index_path->indexinfo;
2993 List *fixed_indexorderbys;
2997 fixed_indexorderbys = NIL;
2999 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
3001 Node *clause = (Node *) lfirst(lcc);
3002 int indexcol = lfirst_int(lci);
3005 * Replace any outer-relation variables with nestloop params.
3007 * This also makes a copy of the clause, so it's safe to modify it
3010 clause = replace_nestloop_params(root, clause);
3012 if (IsA(clause, OpExpr))
3014 OpExpr *op = (OpExpr *) clause;
3016 if (list_length(op->args) != 2)
3017 elog(ERROR, "indexorderby clause is not binary opclause");
3020 * Now replace the indexkey expression with an index Var.
3022 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
3027 elog(ERROR, "unsupported indexorderby type: %d",
3028 (int) nodeTag(clause));
3030 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
3033 return fixed_indexorderbys;
3037 * fix_indexqual_operand
3038 * Convert an indexqual expression to a Var referencing the index column.
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).
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.
3047 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
3051 ListCell *indexpr_item;
3054 * Remove any binary-compatible relabeling of the indexkey
3056 if (IsA(node, RelabelType))
3057 node = (Node *) ((RelabelType *) node)->arg;
3059 Assert(indexcol >= 0 && indexcol < index->ncolumns);
3061 if (index->indexkeys[indexcol] != 0)
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])
3068 result = (Var *) copyObject(node);
3069 result->varno = INDEX_VAR;
3070 result->varattno = indexcol + 1;
3071 return (Node *) result;
3074 elog(ERROR, "index key does not match expected index column");
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++)
3081 if (index->indexkeys[pos] == 0)
3083 if (indexpr_item == NULL)
3084 elog(ERROR, "too few entries in indexprs list");
3085 if (pos == indexcol)
3089 indexkey = (Node *) lfirst(indexpr_item);
3090 if (indexkey && IsA(indexkey, RelabelType))
3091 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3092 if (equal(node, indexkey))
3094 result = makeVar(INDEX_VAR, indexcol + 1,
3095 exprType(lfirst(indexpr_item)), -1,
3096 exprCollation(lfirst(indexpr_item)),
3098 return (Node *) result;
3101 elog(ERROR, "index key does not match expected index column");
3103 indexpr_item = lnext(indexpr_item);
3108 elog(ERROR, "index key does not match expected index column");
3109 return NULL; /* keep compiler quiet */
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.
3122 get_switched_clauses(List *clauses, Relids outerrelids)
3129 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
3130 OpExpr *clause = (OpExpr *) restrictinfo->clause;
3132 Assert(is_opclause(clause));
3133 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
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.
3140 OpExpr *temp = makeNode(OpExpr);
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;
3157 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
3158 t_list = lappend(t_list, clause);
3159 restrictinfo->outer_is_left = true;
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
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.
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.
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.
3188 order_qual_clauses(PlannerInfo *root, List *clauses)
3195 int nitems = list_length(clauses);
3201 /* No need to work hard for 0 or 1 clause */
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.
3209 items = (QualItem *) palloc(nitems * sizeof(QualItem));
3211 foreach(lc, clauses)
3213 Node *clause = (Node *) lfirst(lc);
3216 cost_qual_eval_node(&qcost, clause, root);
3217 items[i].clause = clause;
3218 items[i].cost = qcost.per_tuple;
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.
3227 for (i = 1; i < nitems; i++)
3229 QualItem newitem = items[i];
3232 /* insert newitem into the already-sorted subarray */
3233 for (j = i; j > 0; j--)
3235 if (newitem.cost >= items[j - 1].cost)
3237 items[j] = items[j - 1];
3242 /* Convert back to a list */
3244 for (i = 0; i < nitems; i++)
3245 result = lappend(result, items[i].clause);
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.
3255 copy_path_costsize(Plan *dest, Path *src)
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;
3266 dest->startup_cost = 0;
3267 dest->total_cost = 0;
3268 dest->plan_rows = 0;
3269 dest->plan_width = 0;
3274 * Copy cost and size info from a lower plan node to an inserted node.
3275 * (Most callers alter the info after copying it.)
3278 copy_plan_costsize(Plan *dest, Plan *src)
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;
3289 dest->startup_cost = 0;
3290 dest->total_cost = 0;
3291 dest->plan_rows = 0;
3292 dest->plan_width = 0;
3297 /*****************************************************************************
3299 * PLAN NODE BUILDING ROUTINES
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.
3304 *****************************************************************************/
3307 make_seqscan(List *qptlist,
3311 SeqScan *node = makeNode(SeqScan);
3312 Plan *plan = &node->plan;
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;
3325 make_indexscan(List *qptlist,
3330 List *indexqualorig,
3332 List *indexorderbyorig,
3333 ScanDirection indexscandir)
3335 IndexScan *node = makeNode(IndexScan);
3336 Plan *plan = &node->scan.plan;
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;
3354 static IndexOnlyScan *
3355 make_indexonlyscan(List *qptlist,
3362 ScanDirection indexscandir)
3364 IndexOnlyScan *node = makeNode(IndexOnlyScan);
3365 Plan *plan = &node->scan.plan;
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;
3382 static BitmapIndexScan *
3383 make_bitmap_indexscan(Index scanrelid,
3386 List *indexqualorig)
3388 BitmapIndexScan *node = makeNode(BitmapIndexScan);
3389 Plan *plan = &node->scan.plan;
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;
3404 static BitmapHeapScan *
3405 make_bitmap_heapscan(List *qptlist,
3408 List *bitmapqualorig,
3411 BitmapHeapScan *node = makeNode(BitmapHeapScan);
3412 Plan *plan = &node->scan.plan;
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;
3426 make_tidscan(List *qptlist,
3431 TidScan *node = makeNode(TidScan);
3432 Plan *plan = &node->scan.plan;
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;
3446 make_subqueryscan(List *qptlist,
3451 SubqueryScan *node = makeNode(SubqueryScan);
3452 Plan *plan = &node->scan.plan;
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.
3459 copy_plan_costsize(plan, subplan);
3460 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
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;
3472 static FunctionScan *
3473 make_functionscan(List *qptlist,
3477 bool funcordinality)
3479 FunctionScan *node = makeNode(FunctionScan);
3480 Plan *plan = &node->scan.plan;
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;
3495 make_valuesscan(List *qptlist,
3500 ValuesScan *node = makeNode(ValuesScan);
3501 Plan *plan = &node->scan.plan;
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;
3515 make_ctescan(List *qptlist,
3521 CteScan *node = makeNode(CteScan);
3522 Plan *plan = &node->scan.plan;
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;
3536 static WorkTableScan *
3537 make_worktablescan(List *qptlist,
3542 WorkTableScan *node = makeNode(WorkTableScan);
3543 Plan *plan = &node->scan.plan;
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;
3557 make_foreignscan(List *qptlist,
3563 ForeignScan *node = makeNode(ForeignScan);
3564 Plan *plan = &node->scan.plan;
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;
3581 make_append(List *appendplans, List *tlist)
3583 Append *node = makeNode(Append);
3584 Plan *plan = &node->plan;
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.
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.
3599 plan->startup_cost = 0;
3600 plan->total_cost = 0;
3601 plan->plan_rows = 0;
3603 foreach(subnode, appendplans)
3605 Plan *subplan = (Plan *) lfirst(subnode);
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;
3613 if (plan->plan_rows > 0)
3614 plan->plan_width = rint(total_size / plan->plan_rows);
3616 plan->plan_width = 0;
3618 plan->targetlist = tlist;
3620 plan->lefttree = NULL;
3621 plan->righttree = NULL;
3622 node->appendplans = appendplans;
3628 make_recursive_union(List *tlist,
3635 RecursiveUnion *node = makeNode(RecursiveUnion);
3636 Plan *plan = &node->plan;
3637 int numCols = list_length(distinctList);
3639 cost_recursive_union(plan, lefttree, righttree);
3641 plan->targetlist = tlist;
3643 plan->lefttree = lefttree;
3644 plan->righttree = righttree;
3645 node->wtParam = wtParam;
3648 * convert SortGroupClause list into arrays of attr indexes and equality
3649 * operators, as wanted by executor
3651 node->numCols = numCols;
3655 AttrNumber *dupColIdx;
3659 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3660 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3662 foreach(slitem, distinctList)
3664 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3665 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3668 dupColIdx[keyno] = tle->resno;
3669 dupOperators[keyno] = sortcl->eqop;
3670 Assert(OidIsValid(dupOperators[keyno]));
3673 node->dupColIdx = dupColIdx;
3674 node->dupOperators = dupOperators;
3676 node->numGroups = numGroups;
3682 make_bitmap_and(List *bitmapplans)
3684 BitmapAnd *node = makeNode(BitmapAnd);
3685 Plan *plan = &node->plan;
3687 /* cost should be inserted by caller */
3688 plan->targetlist = NIL;
3690 plan->lefttree = NULL;
3691 plan->righttree = NULL;
3692 node->bitmapplans = bitmapplans;
3698 make_bitmap_or(List *bitmapplans)
3700 BitmapOr *node = makeNode(BitmapOr);
3701 Plan *plan = &node->plan;
3703 /* cost should be inserted by caller */
3704 plan->targetlist = NIL;
3706 plan->lefttree = NULL;
3707 plan->righttree = NULL;
3708 node->bitmapplans = bitmapplans;
3714 make_nestloop(List *tlist,
3722 NestLoop *node = makeNode(NestLoop);
3723 Plan *plan = &node->join.plan;
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;
3738 make_hashjoin(List *tlist,
3746 HashJoin *node = makeNode(HashJoin);
3747 Plan *plan = &node->join.plan;
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;
3762 make_hash(Plan *lefttree,
3764 AttrNumber skewColumn,
3767 int32 skewColTypmod)
3769 Hash *node = makeNode(Hash);
3770 Plan *plan = &node->plan;
3772 copy_plan_costsize(plan, lefttree);
3775 * For plausibility, make startup & total costs equal total cost of input
3776 * plan; this only affects EXPLAIN display not decisions.
3778 plan->startup_cost = plan->total_cost;
3779 plan->targetlist = lefttree->targetlist;
3781 plan->lefttree = lefttree;
3782 plan->righttree = NULL;
3784 node->skewTable = skewTable;
3785 node->skewColumn = skewColumn;
3786 node->skewInherit = skewInherit;
3787 node->skewColType = skewColType;
3788 node->skewColTypmod = skewColTypmod;
3794 make_mergejoin(List *tlist,
3799 Oid *mergecollations,
3800 int *mergestrategies,
3801 bool *mergenullsfirst,
3806 MergeJoin *node = makeNode(MergeJoin);
3807 Plan *plan = &node->join.plan;
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;
3826 * make_sort --- basic routine to build a Sort plan node
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)
3833 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3834 AttrNumber *sortColIdx, Oid *sortOperators,
3835 Oid *collations, bool *nullsFirst,
3836 double limit_tuples)
3838 Sort *node = makeNode(Sort);
3839 Plan *plan = &node->plan;
3840 Path sort_path; /* dummy for result of cost_sort */
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,
3850 plan->startup_cost = sort_path.startup_cost;
3851 plan->total_cost = sort_path.total_cost;
3852 plan->targetlist = lefttree->targetlist;
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;
3866 * prepare_sort_from_pathkeys
3867 * Prepare to sort according to given pathkeys
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.
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
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.
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.
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.
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.
3902 * Returns the node which is to be the input to the Sort (either lefttree,
3903 * or a Result stacked atop lefttree).
3906 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3908 const AttrNumber *reqColIdx,
3909 bool adjust_tlist_in_place,
3911 AttrNumber **p_sortColIdx,
3912 Oid **p_sortOperators,
3914 bool **p_nullsFirst)
3916 List *tlist = lefttree->targetlist;
3919 AttrNumber *sortColIdx;
3925 * We will need at most list_length(pathkeys) sort columns; possibly less
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));
3935 foreach(i, pathkeys)
3937 PathKey *pathkey = (PathKey *) lfirst(i);
3938 EquivalenceClass *ec = pathkey->pk_eclass;
3939 EquivalenceMember *em;
3940 TargetEntry *tle = NULL;
3941 Oid pk_datatype = InvalidOid;
3945 if (ec->ec_has_volatile)
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.
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);
3956 Assert(list_length(ec->ec_members) == 1);
3957 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3959 else if (reqColIdx != NULL)
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.
3970 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
3973 em = find_ec_member_for_tle(ec, tle, relids);
3976 /* found expr at right place in tlist */
3977 pk_datatype = em->em_datatype;
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
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.
4003 tle = (TargetEntry *) lfirst(j);
4004 em = find_ec_member_for_tle(ec, tle, relids);
4007 /* found expr already in tlist */
4008 pk_datatype = em->em_datatype;
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).
4024 Expr *sortexpr = NULL;
4026 foreach(j, ec->ec_members)
4028 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
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
4037 if (em->em_is_const)
4041 * Ignore child members unless they match the rel being
4044 if (em->em_is_child &&
4045 !bms_equal(em->em_relids, relids))
4048 sortexpr = em->em_expr;
4049 exprvars = pull_var_clause((Node *) sortexpr,
4050 PVC_INCLUDE_AGGREGATES,
4051 PVC_INCLUDE_PLACEHOLDERS);
4052 foreach(k, exprvars)
4054 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
4057 list_free(exprvars);
4060 pk_datatype = em->em_datatype;
4061 break; /* found usable expression */
4065 elog(ERROR, "could not find pathkey item to sort");
4068 * Do we need to insert a Result node?
4070 if (!adjust_tlist_in_place &&
4071 !is_projection_capable_plan(lefttree))
4073 /* copy needed so we don't modify input's tlist below */
4074 tlist = copyObject(tlist);
4075 lefttree = (Plan *) make_result(root, tlist, NULL,
4079 /* Don't bother testing is_projection_capable_plan again */
4080 adjust_tlist_in_place = true;
4083 * Add resjunk entry to input's tlist
4085 tle = makeTargetEntry(sortexpr,
4086 list_length(tlist) + 1,
4089 tlist = lappend(tlist, tle);
4090 lefttree->targetlist = tlist; /* just in case NIL before */
4094 * Look up the correct sort operator from the PathKey's slightly
4095 * abstracted representation.
4097 sortop = get_opfamily_member(pathkey->pk_opfamily,
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);
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;
4114 /* Return results */
4115 *p_numsortkeys = numsortkeys;
4116 *p_sortColIdx = sortColIdx;
4117 *p_sortOperators = sortOperators;
4118 *p_collations = collations;
4119 *p_nullsFirst = nullsFirst;
4125 * find_ec_member_for_tle
4126 * Locate an EquivalenceClass member matching the given TLE, if any
4128 * Child EC members are ignored unless they match 'relids'.
4130 static EquivalenceMember *
4131 find_ec_member_for_tle(EquivalenceClass *ec,
4138 /* We ignore binary-compatible relabeling on both ends */
4140 while (tlexpr && IsA(tlexpr, RelabelType))
4141 tlexpr = ((RelabelType *) tlexpr)->arg;
4143 foreach(lc, ec->ec_members)
4145 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
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.
4152 if (em->em_is_const)
4156 * Ignore child members unless they match the rel being sorted.
4158 if (em->em_is_child &&
4159 !bms_equal(em->em_relids, relids))
4162 /* Match if same expression (after stripping relabel) */
4163 emexpr = em->em_expr;
4164 while (emexpr && IsA(emexpr, RelabelType))
4165 emexpr = ((RelabelType *) emexpr)->arg;
4167 if (equal(emexpr, tlexpr))
4175 * make_sort_from_pathkeys
4176 * Create sort plan to sort according to given pathkeys
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;
4184 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
4185 double limit_tuples)
4188 AttrNumber *sortColIdx;
4193 /* Compute sort column info, and adjust lefttree as needed */
4194 lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
4204 /* Now build the Sort node */
4205 return make_sort(root, lefttree, numsortkeys,
4206 sortColIdx, sortOperators, collations,
4207 nullsFirst, limit_tuples);
4211 * make_sort_from_sortclauses
4212 * Create sort plan to sort according to given sortclauses
4214 * 'sortcls' is a list of SortGroupClauses
4215 * 'lefttree' is the node which yields input tuples
4218 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
4220 List *sub_tlist = lefttree->targetlist;
4223 AttrNumber *sortColIdx;
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));
4238 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
4239 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
4241 sortColIdx[numsortkeys] = tle->resno;
4242 sortOperators[numsortkeys] = sortcl->sortop;
4243 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4244 nullsFirst[numsortkeys] = sortcl->nulls_first;
4248 return make_sort(root, lefttree, numsortkeys,
4249 sortColIdx, sortOperators, collations,
4254 * make_sort_from_groupcols
4255 * Create sort plan to sort based on grouping columns
4257 * 'groupcls' is the list of SortGroupClauses
4258 * 'grpColIdx' gives the column numbers to use
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.
4267 make_sort_from_groupcols(PlannerInfo *root,
4269 AttrNumber *grpColIdx,
4272 List *sub_tlist = lefttree->targetlist;
4275 AttrNumber *sortColIdx;
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));
4288 foreach(l, groupcls)
4290 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
4291 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
4294 elog(ERROR, "could not retrive tle for sort-from-groupcols");
4296 sortColIdx[numsortkeys] = tle->resno;
4297 sortOperators[numsortkeys] = grpcl->sortop;
4298 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4299 nullsFirst[numsortkeys] = grpcl->nulls_first;
4303 return make_sort(root, lefttree, numsortkeys,
4304 sortColIdx, sortOperators, collations,
4309 make_material(Plan *lefttree)
4311 Material *node = makeNode(Material);
4312 Plan *plan = &node->plan;
4314 /* cost should be inserted by caller */
4315 plan->targetlist = lefttree->targetlist;
4317 plan->lefttree = lefttree;
4318 plan->righttree = NULL;
4324 * materialize_finished_plan: stick a Material node atop a completed plan
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?
4334 materialize_finished_plan(Plan *subplan)
4337 Path matpath; /* dummy for result of cost_material */
4339 matplan = (Plan *) make_material(subplan);
4342 cost_material(&matpath,
4343 subplan->startup_cost,
4344 subplan->total_cost,
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;
4352 /* parameter kluge --- see comments above */
4353 matplan->extParam = bms_copy(subplan->extParam);
4354 matplan->allParam = bms_copy(subplan->allParam);
4360 make_agg(PlannerInfo *root, List *tlist, List *qual,
4361 AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
4362 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
4366 Agg *node = makeNode(Agg);
4367 Plan *plan = &node->plan;
4368 Path agg_path; /* dummy for result of cost_agg */
4371 node->aggstrategy = aggstrategy;
4372 node->numCols = numGroupCols;
4373 node->grpColIdx = grpColIdx;
4374 node->grpOperators = grpOperators;
4375 node->numGroups = numGroups;
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;
4388 * We will produce a single output tuple if not grouping, and a tuple per
4391 if (aggstrategy == AGG_PLAIN)
4392 plan->plan_rows = 1;
4394 plan->plan_rows = numGroups;
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.
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.
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;
4412 add_tlist_costs_to_plan(root, plan, tlist);
4415 plan->targetlist = tlist;
4416 plan->lefttree = lefttree;
4417 plan->righttree = NULL;
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,
4430 WindowAgg *node = makeNode(WindowAgg);
4431 Plan *plan = &node->plan;
4432 Path windowagg_path; /* dummy for result of cost_windowagg */
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;
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;
4455 * We also need to account for the cost of evaluation of the tlist.
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.
4460 add_tlist_costs_to_plan(root, plan, tlist);
4462 plan->targetlist = tlist;
4463 plan->lefttree = lefttree;
4464 plan->righttree = NULL;
4465 /* WindowAgg nodes never have a qual clause */
4472 make_group(PlannerInfo *root,
4476 AttrNumber *grpColIdx,
4481 Group *node = makeNode(Group);
4482 Plan *plan = &node->plan;
4483 Path group_path; /* dummy for result of cost_group */
4486 node->numCols = numGroupCols;
4487 node->grpColIdx = grpColIdx;
4488 node->grpOperators = grpOperators;
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;
4499 /* One output tuple per estimated result group */
4500 plan->plan_rows = numGroups;
4503 * We also need to account for the cost of evaluation of the qual (ie, the
4504 * HAVING clause) and the tlist.
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
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.
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;
4521 add_tlist_costs_to_plan(root, plan, tlist);
4524 plan->targetlist = tlist;
4525 plan->lefttree = lefttree;
4526 plan->righttree = NULL;
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.
4537 make_unique(Plan *lefttree, List *distinctList)
4539 Unique *node = makeNode(Unique);
4540 Plan *plan = &node->plan;
4541 int numCols = list_length(distinctList);
4543 AttrNumber *uniqColIdx;
4547 copy_plan_costsize(plan, lefttree);
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
4554 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
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.
4562 plan->targetlist = lefttree->targetlist;
4564 plan->lefttree = lefttree;
4565 plan->righttree = NULL;
4568 * convert SortGroupClause list into arrays of attr indexes and equality
4569 * operators, as wanted by executor
4571 Assert(numCols > 0);
4572 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4573 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4575 foreach(slitem, distinctList)
4577 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4578 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4580 uniqColIdx[keyno] = tle->resno;
4581 uniqOperators[keyno] = sortcl->eqop;
4582 Assert(OidIsValid(uniqOperators[keyno]));
4586 node->numCols = numCols;
4587 node->uniqColIdx = uniqColIdx;
4588 node->uniqOperators = uniqOperators;
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.
4599 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
4600 List *distinctList, AttrNumber flagColIdx, int firstFlag,
4601 long numGroups, double outputRows)
4603 SetOp *node = makeNode(SetOp);
4604 Plan *plan = &node->plan;
4605 int numCols = list_length(distinctList);
4607 AttrNumber *dupColIdx;
4611 copy_plan_costsize(plan, lefttree);
4612 plan->plan_rows = outputRows;
4615 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4616 * all columns get compared at most of the tuples.
4618 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4620 plan->targetlist = lefttree->targetlist;
4622 plan->lefttree = lefttree;
4623 plan->righttree = NULL;
4626 * convert SortGroupClause list into arrays of attr indexes and equality
4627 * operators, as wanted by executor
4629 Assert(numCols > 0);
4630 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4631 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4633 foreach(slitem, distinctList)
4635 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4636 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4638 dupColIdx[keyno] = tle->resno;
4639 dupOperators[keyno] = sortcl->eqop;
4640 Assert(OidIsValid(dupOperators[keyno]));
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;
4658 * Build a LockRows plan node
4661 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4663 LockRows *node = makeNode(LockRows);
4664 Plan *plan = &node->plan;
4666 copy_plan_costsize(plan, lefttree);
4668 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4669 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4671 plan->targetlist = lefttree->targetlist;
4673 plan->lefttree = lefttree;
4674 plan->righttree = NULL;
4676 node->rowMarks = rowMarks;
4677 node->epqParam = epqParam;
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!
4690 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4691 int64 offset_est, int64 count_est)
4693 Limit *node = makeNode(Limit);
4694 Plan *plan = &node->plan;
4696 copy_plan_costsize(plan, lefttree);
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
4704 * When the offset or count couldn't be estimated, use 10% of the
4705 * estimated number of rows emitted from the subplan.
4707 if (offset_est != 0)
4712 offset_rows = (double) offset_est;
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;
4731 count_rows = (double) count_est;
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;
4745 plan->targetlist = lefttree->targetlist;
4747 plan->lefttree = lefttree;
4748 plan->righttree = NULL;
4750 node->limitOffset = limitOffset;
4751 node->limitCount = limitCount;
4758 * Build a Result plan node
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.
4766 make_result(PlannerInfo *root,
4768 Node *resconstantqual,
4771 Result *node = makeNode(Result);
4772 Plan *plan = &node->plan;
4775 copy_plan_costsize(plan, subplan);
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)
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;
4793 plan->targetlist = tlist;
4795 plan->lefttree = subplan;
4796 plan->righttree = NULL;
4797 node->resconstantqual = resconstantqual;
4804 * Build a ModifyTable plan node
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.
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)
4820 ModifyTable *node = makeNode(ModifyTable);
4821 Plan *plan = &node->plan;
4823 List *fdw_private_list;
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));
4835 * Compute cost as sum of subplan costs.
4837 plan->startup_cost = 0;
4838 plan->total_cost = 0;
4839 plan->plan_rows = 0;
4841 foreach(subnode, subplans)
4843 Plan *subplan = (Plan *) lfirst(subnode);
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;
4851 if (plan->plan_rows > 0)
4852 plan->plan_width = rint(total_size / plan->plan_rows);
4854 plan->plan_width = 0;
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;
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;
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.
4877 fdw_private_list = NIL;
4879 foreach(lc, resultRelations)
4881 Index rti = lfirst_int(lc);
4882 FdwRoutine *fdwroutine;
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.)
4892 if (rti < root->simple_rel_array_size &&
4893 root->simple_rel_array[rti] != NULL)
4895 RelOptInfo *resultRel = root->simple_rel_array[rti];
4897 fdwroutine = resultRel->fdwroutine;
4901 RangeTblEntry *rte = planner_rt_fetch(rti, root);
4903 Assert(rte->rtekind == RTE_RELATION);
4904 if (rte->relkind == RELKIND_FOREIGN_TABLE)
4905 fdwroutine = GetFdwRoutineByRelId(rte->relid);
4910 if (fdwroutine != NULL &&
4911 fdwroutine->PlanForeignModify != NULL)
4912 fdw_private = fdwroutine->PlanForeignModify(root, node, rti, i);
4915 fdw_private_list = lappend(fdw_private_list, fdw_private);
4918 node->fdwPrivLists = fdw_private_list;
4924 * is_projection_capable_plan
4925 * Check whether a given Plan node is able to do projection.
4928 is_projection_capable_plan(Plan *plan)
4930 /* Most plan types can project, so just list the ones that can't */
4931 switch (nodeTag(plan))
4943 case T_RecursiveUnion: