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/stratnum.h"
23 #include "access/sysattr.h"
24 #include "catalog/pg_class.h"
25 #include "catalog/pg_operator.h"
26 #include "foreign/fdwapi.h"
27 #include "miscadmin.h"
28 #include "nodes/makefuncs.h"
29 #include "nodes/nodeFuncs.h"
30 #include "optimizer/clauses.h"
31 #include "optimizer/cost.h"
32 #include "optimizer/paths.h"
33 #include "optimizer/placeholder.h"
34 #include "optimizer/plancat.h"
35 #include "optimizer/planmain.h"
36 #include "optimizer/planner.h"
37 #include "optimizer/predtest.h"
38 #include "optimizer/prep.h"
39 #include "optimizer/restrictinfo.h"
40 #include "optimizer/subselect.h"
41 #include "optimizer/tlist.h"
42 #include "optimizer/var.h"
43 #include "parser/parse_clause.h"
44 #include "parser/parsetree.h"
45 #include "utils/lsyscache.h"
48 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path);
49 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
50 static List *build_path_tlist(PlannerInfo *root, Path *path);
51 static bool use_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
52 static void disuse_physical_tlist(PlannerInfo *root, Plan *plan, Path *path);
53 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
54 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
55 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
56 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
57 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
58 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
59 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
60 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
61 List *tlist, List *scan_clauses);
62 static SampleScan *create_samplescan_plan(PlannerInfo *root, Path *best_path,
63 List *tlist, List *scan_clauses);
64 static Scan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
65 List *tlist, List *scan_clauses, bool indexonly);
66 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
67 BitmapHeapPath *best_path,
68 List *tlist, List *scan_clauses);
69 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
70 List **qual, List **indexqual, List **indexECs);
71 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
72 List *tlist, List *scan_clauses);
73 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
74 List *tlist, List *scan_clauses);
75 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
76 List *tlist, List *scan_clauses);
77 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
78 List *tlist, List *scan_clauses);
79 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
80 List *tlist, List *scan_clauses);
81 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
82 List *tlist, List *scan_clauses);
83 static ForeignScan *create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
84 List *tlist, List *scan_clauses);
85 static CustomScan *create_customscan_plan(PlannerInfo *root,
86 CustomPath *best_path,
87 List *tlist, List *scan_clauses);
88 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
89 Plan *outer_plan, Plan *inner_plan);
90 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
91 Plan *outer_plan, Plan *inner_plan);
92 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
93 Plan *outer_plan, Plan *inner_plan);
94 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
95 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
96 static void process_subquery_nestloop_params(PlannerInfo *root,
97 List *subplan_params);
98 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path);
99 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path);
100 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol);
101 static List *get_switched_clauses(List *clauses, Relids outerrelids);
102 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
103 static void copy_path_costsize(Plan *dest, Path *src);
104 static void copy_plan_costsize(Plan *dest, Plan *src);
105 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
106 static SampleScan *make_samplescan(List *qptlist, List *qpqual, Index scanrelid);
107 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
108 Oid indexid, List *indexqual, List *indexqualorig,
109 List *indexorderby, List *indexorderbyorig, Oid *indexorderbyops,
110 ScanDirection indexscandir);
111 static IndexOnlyScan *make_indexonlyscan(List *qptlist, List *qpqual,
112 Index scanrelid, Oid indexid,
113 List *indexqual, List *indexorderby,
115 ScanDirection indexscandir);
116 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
118 List *indexqualorig);
119 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
122 List *bitmapqualorig,
124 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
126 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
127 Index scanrelid, List *functions, bool funcordinality);
128 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
129 Index scanrelid, List *values_lists);
130 static CteScan *make_ctescan(List *qptlist, List *qpqual,
131 Index scanrelid, int ctePlanId, int cteParam);
132 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
133 Index scanrelid, int wtParam);
134 static BitmapAnd *make_bitmap_and(List *bitmapplans);
135 static BitmapOr *make_bitmap_or(List *bitmapplans);
136 static NestLoop *make_nestloop(List *tlist,
137 List *joinclauses, List *otherclauses, List *nestParams,
138 Plan *lefttree, Plan *righttree,
140 static HashJoin *make_hashjoin(List *tlist,
141 List *joinclauses, List *otherclauses,
143 Plan *lefttree, Plan *righttree,
145 static Hash *make_hash(Plan *lefttree,
147 AttrNumber skewColumn,
150 int32 skewColTypmod);
151 static MergeJoin *make_mergejoin(List *tlist,
152 List *joinclauses, List *otherclauses,
155 Oid *mergecollations,
156 int *mergestrategies,
157 bool *mergenullsfirst,
158 Plan *lefttree, Plan *righttree,
160 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
161 AttrNumber *sortColIdx, Oid *sortOperators,
162 Oid *collations, bool *nullsFirst,
163 double limit_tuples);
164 static Plan *prepare_sort_from_pathkeys(PlannerInfo *root,
165 Plan *lefttree, List *pathkeys,
167 const AttrNumber *reqColIdx,
168 bool adjust_tlist_in_place,
170 AttrNumber **p_sortColIdx,
171 Oid **p_sortOperators,
173 bool **p_nullsFirst);
174 static EquivalenceMember *find_ec_member_for_expr(EquivalenceClass *ec,
177 static Material *make_material(Plan *lefttree);
182 * Creates the access plan for a query by recursively processing the
183 * desired tree of pathnodes, starting at the node 'best_path'. For
184 * every pathnode found, we create a corresponding plan node containing
185 * appropriate id, target list, and qualification information.
187 * The tlists and quals in the plan tree are still in planner format,
188 * ie, Vars still correspond to the parser's numbering. This will be
189 * fixed later by setrefs.c.
191 * best_path is the best access path
193 * Returns a Plan tree.
196 create_plan(PlannerInfo *root, Path *best_path)
200 /* plan_params should not be in use in current query level */
201 Assert(root->plan_params == NIL);
203 /* Initialize this module's private workspace in PlannerInfo */
204 root->curOuterRels = NULL;
205 root->curOuterParams = NIL;
207 /* Recursively process the path tree */
208 plan = create_plan_recurse(root, best_path);
210 /* Check we successfully assigned all NestLoopParams to plan nodes */
211 if (root->curOuterParams != NIL)
212 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
215 * Reset plan_params to ensure param IDs used for nestloop params are not
218 root->plan_params = NIL;
224 * create_plan_recurse
225 * Recursive guts of create_plan().
228 create_plan_recurse(PlannerInfo *root, Path *best_path)
232 switch (best_path->pathtype)
237 case T_IndexOnlyScan:
238 case T_BitmapHeapScan:
244 case T_WorkTableScan:
247 plan = create_scan_plan(root, best_path);
252 plan = create_join_plan(root,
253 (JoinPath *) best_path);
256 plan = create_append_plan(root,
257 (AppendPath *) best_path);
260 plan = create_merge_append_plan(root,
261 (MergeAppendPath *) best_path);
264 plan = (Plan *) create_result_plan(root,
265 (ResultPath *) best_path);
268 plan = (Plan *) create_material_plan(root,
269 (MaterialPath *) best_path);
272 plan = create_unique_plan(root,
273 (UniquePath *) best_path);
276 elog(ERROR, "unrecognized node type: %d",
277 (int) best_path->pathtype);
278 plan = NULL; /* keep compiler quiet */
287 * Create a scan plan for the parent relation of 'best_path'.
290 create_scan_plan(PlannerInfo *root, Path *best_path)
292 RelOptInfo *rel = best_path->parent;
298 * For table scans, rather than using the relation targetlist (which is
299 * only those Vars actually needed by the query), we prefer to generate a
300 * tlist containing all Vars in order. This will allow the executor to
301 * optimize away projection of the table tuples, if possible. (Note that
302 * planner.c may replace the tlist we generate here, forcing projection to
305 if (use_physical_tlist(root, rel))
307 if (best_path->pathtype == T_IndexOnlyScan)
309 /* For index-only scan, the preferred tlist is the index's */
310 tlist = copyObject(((IndexPath *) best_path)->indexinfo->indextlist);
314 tlist = build_physical_tlist(root, rel);
315 /* if fail because of dropped cols, use regular method */
317 tlist = build_path_tlist(root, best_path);
322 tlist = build_path_tlist(root, best_path);
326 * Extract the relevant restriction clauses from the parent relation. The
327 * executor must apply all these restrictions during the scan, except for
328 * pseudoconstants which we'll take care of below.
330 scan_clauses = rel->baserestrictinfo;
333 * If this is a parameterized scan, we also need to enforce all the join
334 * clauses available from the outer relation(s).
336 * For paranoia's sake, don't modify the stored baserestrictinfo list.
338 if (best_path->param_info)
339 scan_clauses = list_concat(list_copy(scan_clauses),
340 best_path->param_info->ppi_clauses);
342 switch (best_path->pathtype)
345 plan = (Plan *) create_seqscan_plan(root,
352 plan = (Plan *) create_samplescan_plan(root,
359 plan = (Plan *) create_indexscan_plan(root,
360 (IndexPath *) best_path,
366 case T_IndexOnlyScan:
367 plan = (Plan *) create_indexscan_plan(root,
368 (IndexPath *) best_path,
374 case T_BitmapHeapScan:
375 plan = (Plan *) create_bitmap_scan_plan(root,
376 (BitmapHeapPath *) best_path,
382 plan = (Plan *) create_tidscan_plan(root,
383 (TidPath *) best_path,
389 plan = (Plan *) create_subqueryscan_plan(root,
396 plan = (Plan *) create_functionscan_plan(root,
403 plan = (Plan *) create_valuesscan_plan(root,
410 plan = (Plan *) create_ctescan_plan(root,
416 case T_WorkTableScan:
417 plan = (Plan *) create_worktablescan_plan(root,
424 plan = (Plan *) create_foreignscan_plan(root,
425 (ForeignPath *) best_path,
431 plan = (Plan *) create_customscan_plan(root,
432 (CustomPath *) best_path,
438 elog(ERROR, "unrecognized node type: %d",
439 (int) best_path->pathtype);
440 plan = NULL; /* keep compiler quiet */
445 * If there are any pseudoconstant clauses attached to this node, insert a
446 * gating Result node that evaluates the pseudoconstants as one-time
449 if (root->hasPseudoConstantQuals)
450 plan = create_gating_plan(root, plan, scan_clauses);
456 * Build a target list (ie, a list of TargetEntry) for the Path's output.
459 build_path_tlist(PlannerInfo *root, Path *path)
461 RelOptInfo *rel = path->parent;
466 foreach(v, rel->reltargetlist)
468 /* Do we really need to copy here? Not sure */
469 Node *node = (Node *) copyObject(lfirst(v));
472 * If it's a parameterized path, there might be lateral references in
473 * the tlist, which need to be replaced with Params. There's no need
474 * to remake the TargetEntry nodes, so apply this to each list item
477 if (path->param_info)
478 node = replace_nestloop_params(root, node);
480 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
491 * Decide whether to use a tlist matching relation structure,
492 * rather than only those Vars actually referenced.
495 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
501 * We can do this for real relation scans, subquery scans, function scans,
502 * values scans, and CTE scans (but not for, eg, joins).
504 if (rel->rtekind != RTE_RELATION &&
505 rel->rtekind != RTE_SUBQUERY &&
506 rel->rtekind != RTE_FUNCTION &&
507 rel->rtekind != RTE_VALUES &&
508 rel->rtekind != RTE_CTE)
512 * Can't do it with inheritance cases either (mainly because Append
515 if (rel->reloptkind != RELOPT_BASEREL)
519 * Can't do it if any system columns or whole-row Vars are requested.
520 * (This could possibly be fixed but would take some fragile assumptions
521 * in setrefs.c, I think.)
523 for (i = rel->min_attr; i <= 0; i++)
525 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
530 * Can't do it if the rel is required to emit any placeholder expressions,
533 foreach(lc, root->placeholder_list)
535 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
537 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
538 bms_is_subset(phinfo->ph_eval_at, rel->relids))
546 * disuse_physical_tlist
547 * Switch a plan node back to emitting only Vars actually referenced.
549 * If the plan node immediately above a scan would prefer to get only
550 * needed Vars and not a physical tlist, it must call this routine to
551 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
552 * and Material nodes want this, so they don't have to store useless columns.
555 disuse_physical_tlist(PlannerInfo *root, Plan *plan, Path *path)
557 /* Only need to undo it for path types handled by create_scan_plan() */
558 switch (path->pathtype)
563 case T_IndexOnlyScan:
564 case T_BitmapHeapScan:
570 case T_WorkTableScan:
573 plan->targetlist = build_path_tlist(root, path);
582 * Deal with pseudoconstant qual clauses
584 * If the node's quals list includes any pseudoconstant quals, put them
585 * into a gating Result node atop the already-built plan. Otherwise,
586 * return the plan as-is.
588 * Note that we don't change cost or size estimates when doing gating.
589 * The costs of qual eval were already folded into the plan's startup cost.
590 * Leaving the size alone amounts to assuming that the gating qual will
591 * succeed, which is the conservative estimate for planning upper queries.
592 * We certainly don't want to assume the output size is zero (unless the
593 * gating qual is actually constant FALSE, and that case is dealt with in
594 * clausesel.c). Interpolating between the two cases is silly, because
595 * it doesn't reflect what will really happen at runtime, and besides which
596 * in most cases we have only a very bad idea of the probability of the gating
600 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
602 List *pseudoconstants;
604 /* Sort into desirable execution order while still in RestrictInfo form */
605 quals = order_qual_clauses(root, quals);
607 /* Pull out any pseudoconstant quals from the RestrictInfo list */
608 pseudoconstants = extract_actual_clauses(quals, true);
610 if (!pseudoconstants)
613 return (Plan *) make_result(root,
615 (Node *) pseudoconstants,
621 * Create a join plan for 'best_path' and (recursively) plans for its
622 * inner and outer paths.
625 create_join_plan(PlannerInfo *root, JoinPath *best_path)
630 Relids saveOuterRels = root->curOuterRels;
632 outer_plan = create_plan_recurse(root, best_path->outerjoinpath);
634 /* For a nestloop, include outer relids in curOuterRels for inner side */
635 if (best_path->path.pathtype == T_NestLoop)
636 root->curOuterRels = bms_union(root->curOuterRels,
637 best_path->outerjoinpath->parent->relids);
639 inner_plan = create_plan_recurse(root, best_path->innerjoinpath);
641 switch (best_path->path.pathtype)
644 plan = (Plan *) create_mergejoin_plan(root,
645 (MergePath *) best_path,
650 plan = (Plan *) create_hashjoin_plan(root,
651 (HashPath *) best_path,
656 /* Restore curOuterRels */
657 bms_free(root->curOuterRels);
658 root->curOuterRels = saveOuterRels;
660 plan = (Plan *) create_nestloop_plan(root,
661 (NestPath *) best_path,
666 elog(ERROR, "unrecognized node type: %d",
667 (int) best_path->path.pathtype);
668 plan = NULL; /* keep compiler quiet */
673 * If there are any pseudoconstant clauses attached to this node, insert a
674 * gating Result node that evaluates the pseudoconstants as one-time
677 if (root->hasPseudoConstantQuals)
678 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
683 * * Expensive function pullups may have pulled local predicates * into
684 * this path node. Put them in the qpqual of the plan node. * JMH,
687 if (get_loc_restrictinfo(best_path) != NIL)
688 set_qpqual((Plan) plan,
689 list_concat(get_qpqual((Plan) plan),
690 get_actual_clauses(get_loc_restrictinfo(best_path))));
698 * Create an Append plan for 'best_path' and (recursively) plans
701 * Returns a Plan node.
704 create_append_plan(PlannerInfo *root, AppendPath *best_path)
707 List *tlist = build_path_tlist(root, &best_path->path);
708 List *subplans = NIL;
712 * The subpaths list could be empty, if every child was proven empty by
713 * constraint exclusion. In that case generate a dummy plan that returns
716 * Note that an AppendPath with no members is also generated in certain
717 * cases where there was no appending construct at all, but we know the
718 * relation is empty (see set_dummy_rel_pathlist).
720 if (best_path->subpaths == NIL)
722 /* Generate a Result plan with constant-FALSE gating qual */
723 return (Plan *) make_result(root,
725 (Node *) list_make1(makeBoolConst(false,
730 /* Build the plan for each child */
731 foreach(subpaths, best_path->subpaths)
733 Path *subpath = (Path *) lfirst(subpaths);
735 subplans = lappend(subplans, create_plan_recurse(root, subpath));
739 * XXX ideally, if there's just one child, we'd not bother to generate an
740 * Append node but just return the single child. At the moment this does
741 * not work because the varno of the child scan plan won't match the
742 * parent-rel Vars it'll be asked to emit.
745 plan = make_append(subplans, tlist);
747 return (Plan *) plan;
751 * create_merge_append_plan
752 * Create a MergeAppend plan for 'best_path' and (recursively) plans
755 * Returns a Plan node.
758 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
760 MergeAppend *node = makeNode(MergeAppend);
761 Plan *plan = &node->plan;
762 List *tlist = build_path_tlist(root, &best_path->path);
763 List *pathkeys = best_path->path.pathkeys;
764 List *subplans = NIL;
768 * We don't have the actual creation of the MergeAppend node split out
769 * into a separate make_xxx function. This is because we want to run
770 * prepare_sort_from_pathkeys on it before we do so on the individual
771 * child plans, to make cross-checking the sort info easier.
773 copy_path_costsize(plan, (Path *) best_path);
774 plan->targetlist = tlist;
776 plan->lefttree = NULL;
777 plan->righttree = NULL;
779 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
780 (void) prepare_sort_from_pathkeys(root, plan, pathkeys,
781 best_path->path.parent->relids,
786 &node->sortOperators,
791 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
792 * even to subplans that don't need an explicit sort, to make sure they
793 * are returning the same sort key columns the MergeAppend expects.
795 foreach(subpaths, best_path->subpaths)
797 Path *subpath = (Path *) lfirst(subpaths);
800 AttrNumber *sortColIdx;
805 /* Build the child plan */
806 subplan = create_plan_recurse(root, subpath);
808 /* Compute sort column info, and adjust subplan's tlist as needed */
809 subplan = prepare_sort_from_pathkeys(root, subplan, pathkeys,
810 subpath->parent->relids,
820 * Check that we got the same sort key information. We just Assert
821 * that the sortops match, since those depend only on the pathkeys;
822 * but it seems like a good idea to check the sort column numbers
823 * explicitly, to ensure the tlists really do match up.
825 Assert(numsortkeys == node->numCols);
826 if (memcmp(sortColIdx, node->sortColIdx,
827 numsortkeys * sizeof(AttrNumber)) != 0)
828 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
829 Assert(memcmp(sortOperators, node->sortOperators,
830 numsortkeys * sizeof(Oid)) == 0);
831 Assert(memcmp(collations, node->collations,
832 numsortkeys * sizeof(Oid)) == 0);
833 Assert(memcmp(nullsFirst, node->nullsFirst,
834 numsortkeys * sizeof(bool)) == 0);
836 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
837 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
838 subplan = (Plan *) make_sort(root, subplan, numsortkeys,
839 sortColIdx, sortOperators,
840 collations, nullsFirst,
841 best_path->limit_tuples);
843 subplans = lappend(subplans, subplan);
846 node->mergeplans = subplans;
848 return (Plan *) node;
853 * Create a Result plan for 'best_path'.
854 * This is only used for the case of a query with an empty jointree.
856 * Returns a Plan node.
859 create_result_plan(PlannerInfo *root, ResultPath *best_path)
864 /* The tlist will be installed later, since we have no RelOptInfo */
865 Assert(best_path->path.parent == NULL);
868 /* best_path->quals is just bare clauses */
870 quals = order_qual_clauses(root, best_path->quals);
872 return make_result(root, tlist, (Node *) quals, NULL);
876 * create_material_plan
877 * Create a Material plan for 'best_path' and (recursively) plans
880 * Returns a Plan node.
883 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
888 subplan = create_plan_recurse(root, best_path->subpath);
890 /* We don't want any excess columns in the materialized tuples */
891 disuse_physical_tlist(root, subplan, best_path->subpath);
893 plan = make_material(subplan);
895 copy_path_costsize(&plan->plan, (Path *) best_path);
902 * Create a Unique plan for 'best_path' and (recursively) plans
905 * Returns a Plan node.
908 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
918 AttrNumber *groupColIdx;
922 subplan = create_plan_recurse(root, best_path->subpath);
924 /* Done if we don't need to do any actual unique-ifying */
925 if (best_path->umethod == UNIQUE_PATH_NOOP)
929 * As constructed, the subplan has a "flat" tlist containing just the Vars
930 * needed here and at upper levels. The values we are supposed to
931 * unique-ify may be expressions in these variables. We have to add any
932 * such expressions to the subplan's tlist.
934 * The subplan may have a "physical" tlist if it is a simple scan plan. If
935 * we're going to sort, this should be reduced to the regular tlist, so
936 * that we don't sort more data than we need to. For hashing, the tlist
937 * should be left as-is if we don't need to add any expressions; but if we
938 * do have to add expressions, then a projection step will be needed at
939 * runtime anyway, so we may as well remove unneeded items. Therefore
940 * newtlist starts from build_path_tlist() not just a copy of the
941 * subplan's tlist; and we don't install it into the subplan unless we are
942 * sorting or stuff has to be added.
944 in_operators = best_path->in_operators;
945 uniq_exprs = best_path->uniq_exprs;
947 /* initialize modified subplan tlist as just the "required" vars */
948 newtlist = build_path_tlist(root, &best_path->path);
949 nextresno = list_length(newtlist) + 1;
952 foreach(l, uniq_exprs)
954 Node *uniqexpr = lfirst(l);
957 tle = tlist_member(uniqexpr, newtlist);
960 tle = makeTargetEntry((Expr *) uniqexpr,
964 newtlist = lappend(newtlist, tle);
970 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
973 * If the top plan node can't do projections and its existing target
974 * list isn't already what we need, we need to add a Result node to
977 if (!is_projection_capable_plan(subplan) &&
978 !tlist_same_exprs(newtlist, subplan->targetlist))
979 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
981 subplan->targetlist = newtlist;
985 * Build control information showing which subplan output columns are to
986 * be examined by the grouping step. Unfortunately we can't merge this
987 * with the previous loop, since we didn't then know which version of the
988 * subplan tlist we'd end up using.
990 newtlist = subplan->targetlist;
991 numGroupCols = list_length(uniq_exprs);
992 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
995 foreach(l, uniq_exprs)
997 Node *uniqexpr = lfirst(l);
1000 tle = tlist_member(uniqexpr, newtlist);
1001 if (!tle) /* shouldn't happen */
1002 elog(ERROR, "failed to find unique expression in subplan tlist");
1003 groupColIdx[groupColPos++] = tle->resno;
1006 if (best_path->umethod == UNIQUE_PATH_HASH)
1009 Oid *groupOperators;
1011 numGroups = (long) Min(best_path->path.rows, (double) LONG_MAX);
1014 * Get the hashable equality operators for the Agg node to use.
1015 * Normally these are the same as the IN clause operators, but if
1016 * those are cross-type operators then the equality operators are the
1017 * ones for the IN clause operators' RHS datatype.
1019 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
1021 foreach(l, in_operators)
1023 Oid in_oper = lfirst_oid(l);
1026 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
1027 elog(ERROR, "could not find compatible hash operator for operator %u",
1029 groupOperators[groupColPos++] = eq_oper;
1033 * Since the Agg node is going to project anyway, we can give it the
1034 * minimum output tlist, without any stuff we might have added to the
1037 plan = (Plan *) make_agg(root,
1038 build_path_tlist(root, &best_path->path),
1051 List *sortList = NIL;
1053 /* Create an ORDER BY list to sort the input compatibly */
1055 foreach(l, in_operators)
1057 Oid in_oper = lfirst_oid(l);
1061 SortGroupClause *sortcl;
1063 sortop = get_ordering_op_for_equality_op(in_oper, false);
1064 if (!OidIsValid(sortop)) /* shouldn't happen */
1065 elog(ERROR, "could not find ordering operator for equality operator %u",
1069 * The Unique node will need equality operators. Normally these
1070 * are the same as the IN clause operators, but if those are
1071 * cross-type operators then the equality operators are the ones
1072 * for the IN clause operators' RHS datatype.
1074 eqop = get_equality_op_for_ordering_op(sortop, NULL);
1075 if (!OidIsValid(eqop)) /* shouldn't happen */
1076 elog(ERROR, "could not find equality operator for ordering operator %u",
1079 tle = get_tle_by_resno(subplan->targetlist,
1080 groupColIdx[groupColPos]);
1081 Assert(tle != NULL);
1083 sortcl = makeNode(SortGroupClause);
1084 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
1085 subplan->targetlist);
1086 sortcl->eqop = eqop;
1087 sortcl->sortop = sortop;
1088 sortcl->nulls_first = false;
1089 sortcl->hashable = false; /* no need to make this accurate */
1090 sortList = lappend(sortList, sortcl);
1093 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
1094 plan = (Plan *) make_unique(plan, sortList);
1097 /* Adjust output size estimate (other fields should be OK already) */
1098 plan->plan_rows = best_path->path.rows;
1104 /*****************************************************************************
1106 * BASE-RELATION SCAN METHODS
1108 *****************************************************************************/
1112 * create_seqscan_plan
1113 * Returns a seqscan plan for the base relation scanned by 'best_path'
1114 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1117 create_seqscan_plan(PlannerInfo *root, Path *best_path,
1118 List *tlist, List *scan_clauses)
1121 Index scan_relid = best_path->parent->relid;
1123 /* it should be a base rel... */
1124 Assert(scan_relid > 0);
1125 Assert(best_path->parent->rtekind == RTE_RELATION);
1127 /* Sort clauses into best execution order */
1128 scan_clauses = order_qual_clauses(root, scan_clauses);
1130 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1131 scan_clauses = extract_actual_clauses(scan_clauses, false);
1133 /* Replace any outer-relation variables with nestloop params */
1134 if (best_path->param_info)
1136 scan_clauses = (List *)
1137 replace_nestloop_params(root, (Node *) scan_clauses);
1140 scan_plan = make_seqscan(tlist,
1144 copy_path_costsize(&scan_plan->plan, best_path);
1150 * create_samplescan_plan
1151 * Returns a samplecan plan for the base relation scanned by 'best_path'
1152 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1155 create_samplescan_plan(PlannerInfo *root, Path *best_path,
1156 List *tlist, List *scan_clauses)
1158 SampleScan *scan_plan;
1159 Index scan_relid = best_path->parent->relid;
1161 /* it should be a base rel with tablesample clause... */
1162 Assert(scan_relid > 0);
1163 Assert(best_path->parent->rtekind == RTE_RELATION);
1164 Assert(best_path->pathtype == T_SampleScan);
1166 /* Sort clauses into best execution order */
1167 scan_clauses = order_qual_clauses(root, scan_clauses);
1169 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1170 scan_clauses = extract_actual_clauses(scan_clauses, false);
1172 /* Replace any outer-relation variables with nestloop params */
1173 if (best_path->param_info)
1175 scan_clauses = (List *)
1176 replace_nestloop_params(root, (Node *) scan_clauses);
1179 scan_plan = make_samplescan(tlist,
1183 copy_path_costsize(&scan_plan->plan, best_path);
1189 * create_indexscan_plan
1190 * Returns an indexscan plan for the base relation scanned by 'best_path'
1191 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1193 * We use this for both plain IndexScans and IndexOnlyScans, because the
1194 * qual preprocessing work is the same for both. Note that the caller tells
1195 * us which to build --- we don't look at best_path->path.pathtype, because
1196 * create_bitmap_subplan needs to be able to override the prior decision.
1199 create_indexscan_plan(PlannerInfo *root,
1200 IndexPath *best_path,
1206 List *indexquals = best_path->indexquals;
1207 List *indexorderbys = best_path->indexorderbys;
1208 Index baserelid = best_path->path.parent->relid;
1209 Oid indexoid = best_path->indexinfo->indexoid;
1211 List *stripped_indexquals;
1212 List *fixed_indexquals;
1213 List *fixed_indexorderbys;
1214 Oid *indexorderbyops = NULL;
1217 /* it should be a base rel... */
1218 Assert(baserelid > 0);
1219 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1222 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
1223 * executor as indexqualorig
1225 stripped_indexquals = get_actual_clauses(indexquals);
1228 * The executor needs a copy with the indexkey on the left of each clause
1229 * and with index Vars substituted for table ones.
1231 fixed_indexquals = fix_indexqual_references(root, best_path);
1234 * Likewise fix up index attr references in the ORDER BY expressions.
1236 fixed_indexorderbys = fix_indexorderby_references(root, best_path);
1239 * The qpqual list must contain all restrictions not automatically handled
1240 * by the index, other than pseudoconstant clauses which will be handled
1241 * by a separate gating plan node. All the predicates in the indexquals
1242 * will be checked (either by the index itself, or by nodeIndexscan.c),
1243 * but if there are any "special" operators involved then they must be
1244 * included in qpqual. The upshot is that qpqual must contain
1245 * scan_clauses minus whatever appears in indexquals.
1247 * In normal cases simple pointer equality checks will be enough to spot
1248 * duplicate RestrictInfos, so we try that first.
1250 * Another common case is that a scan_clauses entry is generated from the
1251 * same EquivalenceClass as some indexqual, and is therefore redundant
1252 * with it, though not equal. (This happens when indxpath.c prefers a
1253 * different derived equality than what generate_join_implied_equalities
1254 * picked for a parameterized scan's ppi_clauses.)
1256 * In some situations (particularly with OR'd index conditions) we may
1257 * have scan_clauses that are not equal to, but are logically implied by,
1258 * the index quals; so we also try a predicate_implied_by() check to see
1259 * if we can discard quals that way. (predicate_implied_by assumes its
1260 * first input contains only immutable functions, so we have to check
1263 * We can also discard quals that are implied by a partial index's
1264 * predicate, but only in a plain SELECT; when scanning a target relation
1265 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
1266 * plan so that they'll be properly rechecked by EvalPlanQual testing.
1268 * Note: if you change this bit of code you should also look at
1269 * extract_nonindex_conditions() in costsize.c.
1272 foreach(l, scan_clauses)
1274 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1276 Assert(IsA(rinfo, RestrictInfo));
1277 if (rinfo->pseudoconstant)
1278 continue; /* we may drop pseudoconstants here */
1279 if (list_member_ptr(indexquals, rinfo))
1280 continue; /* simple duplicate */
1281 if (is_redundant_derived_clause(rinfo, indexquals))
1282 continue; /* derived from same EquivalenceClass */
1283 if (!contain_mutable_functions((Node *) rinfo->clause))
1285 List *clausel = list_make1(rinfo->clause);
1287 if (predicate_implied_by(clausel, indexquals))
1288 continue; /* provably implied by indexquals */
1289 if (best_path->indexinfo->indpred)
1291 if (baserelid != root->parse->resultRelation &&
1292 get_plan_rowmark(root->rowMarks, baserelid) == NULL)
1293 if (predicate_implied_by(clausel,
1294 best_path->indexinfo->indpred))
1295 continue; /* implied by index predicate */
1298 qpqual = lappend(qpqual, rinfo);
1301 /* Sort clauses into best execution order */
1302 qpqual = order_qual_clauses(root, qpqual);
1304 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1305 qpqual = extract_actual_clauses(qpqual, false);
1308 * We have to replace any outer-relation variables with nestloop params in
1309 * the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
1310 * annoying to have to do this separately from the processing in
1311 * fix_indexqual_references --- rethink this when generalizing the inner
1312 * indexscan support. But note we can't really do this earlier because
1313 * it'd break the comparisons to predicates above ... (or would it? Those
1314 * wouldn't have outer refs)
1316 if (best_path->path.param_info)
1318 stripped_indexquals = (List *)
1319 replace_nestloop_params(root, (Node *) stripped_indexquals);
1321 replace_nestloop_params(root, (Node *) qpqual);
1322 indexorderbys = (List *)
1323 replace_nestloop_params(root, (Node *) indexorderbys);
1327 * If there are ORDER BY expressions, look up the sort operators for
1330 if (best_path->path.pathkeys && indexorderbys)
1332 int numOrderBys = list_length(indexorderbys);
1334 ListCell *pathkeyCell,
1338 EquivalenceMember *em;
1340 indexorderbyops = (Oid *) palloc(numOrderBys * sizeof(Oid));
1343 * PathKey contains pointer to the equivalence class, but that's not
1344 * enough because we need the expression's datatype to look up the
1345 * sort operator in the operator family. We have to dig the
1346 * equivalence member for the datatype.
1349 forboth (pathkeyCell, best_path->path.pathkeys, exprCell, indexorderbys)
1351 pathkey = (PathKey *) lfirst(pathkeyCell);
1352 expr = (Expr *) lfirst(exprCell);
1354 /* Find equivalence member for the order by expression */
1355 em = find_ec_member_for_expr(pathkey->pk_eclass, expr, NULL);
1357 /* Get sort operator from opfamily */
1358 indexorderbyops[i] = get_opfamily_member(pathkey->pk_opfamily,
1361 pathkey->pk_strategy);
1366 /* Finally ready to build the plan node */
1368 scan_plan = (Scan *) make_indexonlyscan(tlist,
1373 fixed_indexorderbys,
1374 best_path->indexinfo->indextlist,
1375 best_path->indexscandir);
1377 scan_plan = (Scan *) make_indexscan(tlist,
1382 stripped_indexquals,
1383 fixed_indexorderbys,
1386 best_path->indexscandir);
1388 copy_path_costsize(&scan_plan->plan, &best_path->path);
1394 * create_bitmap_scan_plan
1395 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
1396 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1398 static BitmapHeapScan *
1399 create_bitmap_scan_plan(PlannerInfo *root,
1400 BitmapHeapPath *best_path,
1404 Index baserelid = best_path->path.parent->relid;
1405 Plan *bitmapqualplan;
1406 List *bitmapqualorig;
1411 BitmapHeapScan *scan_plan;
1413 /* it should be a base rel... */
1414 Assert(baserelid > 0);
1415 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1417 /* Process the bitmapqual tree into a Plan tree and qual lists */
1418 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1419 &bitmapqualorig, &indexquals,
1423 * The qpqual list must contain all restrictions not automatically handled
1424 * by the index, other than pseudoconstant clauses which will be handled
1425 * by a separate gating plan node. All the predicates in the indexquals
1426 * will be checked (either by the index itself, or by
1427 * nodeBitmapHeapscan.c), but if there are any "special" operators
1428 * involved then they must be added to qpqual. The upshot is that qpqual
1429 * must contain scan_clauses minus whatever appears in indexquals.
1431 * This loop is similar to the comparable code in create_indexscan_plan(),
1432 * but with some differences because it has to compare the scan clauses to
1433 * stripped (no RestrictInfos) indexquals. See comments there for more
1436 * In normal cases simple equal() checks will be enough to spot duplicate
1437 * clauses, so we try that first. We next see if the scan clause is
1438 * redundant with any top-level indexqual by virtue of being generated
1439 * from the same EC. After that, try predicate_implied_by().
1441 * Unlike create_indexscan_plan(), we need take no special thought here
1442 * for partial index predicates; this is because the predicate conditions
1443 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1444 * to do it that way because predicate conditions need to be rechecked if
1445 * the scan becomes lossy, so they have to be included in bitmapqualorig.
1448 foreach(l, scan_clauses)
1450 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1451 Node *clause = (Node *) rinfo->clause;
1453 Assert(IsA(rinfo, RestrictInfo));
1454 if (rinfo->pseudoconstant)
1455 continue; /* we may drop pseudoconstants here */
1456 if (list_member(indexquals, clause))
1457 continue; /* simple duplicate */
1458 if (rinfo->parent_ec && list_member_ptr(indexECs, rinfo->parent_ec))
1459 continue; /* derived from same EquivalenceClass */
1460 if (!contain_mutable_functions(clause))
1462 List *clausel = list_make1(clause);
1464 if (predicate_implied_by(clausel, indexquals))
1465 continue; /* provably implied by indexquals */
1467 qpqual = lappend(qpqual, rinfo);
1470 /* Sort clauses into best execution order */
1471 qpqual = order_qual_clauses(root, qpqual);
1473 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1474 qpqual = extract_actual_clauses(qpqual, false);
1477 * When dealing with special operators, we will at this point have
1478 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1479 * 'em from bitmapqualorig, since there's no point in making the tests
1482 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1485 * We have to replace any outer-relation variables with nestloop params in
1486 * the qpqual and bitmapqualorig expressions. (This was already done for
1487 * expressions attached to plan nodes in the bitmapqualplan tree.)
1489 if (best_path->path.param_info)
1492 replace_nestloop_params(root, (Node *) qpqual);
1493 bitmapqualorig = (List *)
1494 replace_nestloop_params(root, (Node *) bitmapqualorig);
1497 /* Finally ready to build the plan node */
1498 scan_plan = make_bitmap_heapscan(tlist,
1504 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1510 * Given a bitmapqual tree, generate the Plan tree that implements it
1512 * As byproducts, we also return in *qual and *indexqual the qual lists
1513 * (in implicit-AND form, without RestrictInfos) describing the original index
1514 * conditions and the generated indexqual conditions. (These are the same in
1515 * simple cases, but when special index operators are involved, the former
1516 * list includes the special conditions while the latter includes the actual
1517 * indexable conditions derived from them.) Both lists include partial-index
1518 * predicates, because we have to recheck predicates as well as index
1519 * conditions if the bitmap scan becomes lossy.
1521 * In addition, we return a list of EquivalenceClass pointers for all the
1522 * top-level indexquals that were possibly-redundantly derived from ECs.
1523 * This allows removal of scan_clauses that are redundant with such quals.
1524 * (We do not attempt to detect such redundancies for quals that are within
1525 * OR subtrees. This could be done in a less hacky way if we returned the
1526 * indexquals in RestrictInfo form, but that would be slower and still pretty
1527 * messy, since we'd have to build new RestrictInfos in many cases.)
1530 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1531 List **qual, List **indexqual, List **indexECs)
1535 if (IsA(bitmapqual, BitmapAndPath))
1537 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1538 List *subplans = NIL;
1539 List *subquals = NIL;
1540 List *subindexquals = NIL;
1541 List *subindexECs = NIL;
1545 * There may well be redundant quals among the subplans, since a
1546 * top-level WHERE qual might have gotten used to form several
1547 * different index quals. We don't try exceedingly hard to eliminate
1548 * redundancies, but we do eliminate obvious duplicates by using
1549 * list_concat_unique.
1551 foreach(l, apath->bitmapquals)
1558 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1559 &subqual, &subindexqual,
1561 subplans = lappend(subplans, subplan);
1562 subquals = list_concat_unique(subquals, subqual);
1563 subindexquals = list_concat_unique(subindexquals, subindexqual);
1564 /* Duplicates in indexECs aren't worth getting rid of */
1565 subindexECs = list_concat(subindexECs, subindexEC);
1567 plan = (Plan *) make_bitmap_and(subplans);
1568 plan->startup_cost = apath->path.startup_cost;
1569 plan->total_cost = apath->path.total_cost;
1571 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1572 plan->plan_width = 0; /* meaningless */
1574 *indexqual = subindexquals;
1575 *indexECs = subindexECs;
1577 else if (IsA(bitmapqual, BitmapOrPath))
1579 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1580 List *subplans = NIL;
1581 List *subquals = NIL;
1582 List *subindexquals = NIL;
1583 bool const_true_subqual = false;
1584 bool const_true_subindexqual = false;
1588 * Here, we only detect qual-free subplans. A qual-free subplan would
1589 * cause us to generate "... OR true ..." which we may as well reduce
1590 * to just "true". We do not try to eliminate redundant subclauses
1591 * because (a) it's not as likely as in the AND case, and (b) we might
1592 * well be working with hundreds or even thousands of OR conditions,
1593 * perhaps from a long IN list. The performance of list_append_unique
1594 * would be unacceptable.
1596 foreach(l, opath->bitmapquals)
1603 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1604 &subqual, &subindexqual,
1606 subplans = lappend(subplans, subplan);
1608 const_true_subqual = true;
1609 else if (!const_true_subqual)
1610 subquals = lappend(subquals,
1611 make_ands_explicit(subqual));
1612 if (subindexqual == NIL)
1613 const_true_subindexqual = true;
1614 else if (!const_true_subindexqual)
1615 subindexquals = lappend(subindexquals,
1616 make_ands_explicit(subindexqual));
1620 * In the presence of ScalarArrayOpExpr quals, we might have built
1621 * BitmapOrPaths with just one subpath; don't add an OR step.
1623 if (list_length(subplans) == 1)
1625 plan = (Plan *) linitial(subplans);
1629 plan = (Plan *) make_bitmap_or(subplans);
1630 plan->startup_cost = opath->path.startup_cost;
1631 plan->total_cost = opath->path.total_cost;
1633 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1634 plan->plan_width = 0; /* meaningless */
1638 * If there were constant-TRUE subquals, the OR reduces to constant
1639 * TRUE. Also, avoid generating one-element ORs, which could happen
1640 * due to redundancy elimination or ScalarArrayOpExpr quals.
1642 if (const_true_subqual)
1644 else if (list_length(subquals) <= 1)
1647 *qual = list_make1(make_orclause(subquals));
1648 if (const_true_subindexqual)
1650 else if (list_length(subindexquals) <= 1)
1651 *indexqual = subindexquals;
1653 *indexqual = list_make1(make_orclause(subindexquals));
1656 else if (IsA(bitmapqual, IndexPath))
1658 IndexPath *ipath = (IndexPath *) bitmapqual;
1663 /* Use the regular indexscan plan build machinery... */
1664 iscan = (IndexScan *) create_indexscan_plan(root, ipath,
1666 Assert(IsA(iscan, IndexScan));
1667 /* then convert to a bitmap indexscan */
1668 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1671 iscan->indexqualorig);
1672 plan->startup_cost = 0.0;
1673 plan->total_cost = ipath->indextotalcost;
1675 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1676 plan->plan_width = 0; /* meaningless */
1677 *qual = get_actual_clauses(ipath->indexclauses);
1678 *indexqual = get_actual_clauses(ipath->indexquals);
1679 foreach(l, ipath->indexinfo->indpred)
1681 Expr *pred = (Expr *) lfirst(l);
1684 * We know that the index predicate must have been implied by the
1685 * query condition as a whole, but it may or may not be implied by
1686 * the conditions that got pushed into the bitmapqual. Avoid
1687 * generating redundant conditions.
1689 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1691 *qual = lappend(*qual, pred);
1692 *indexqual = lappend(*indexqual, pred);
1696 foreach(l, ipath->indexquals)
1698 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1700 if (rinfo->parent_ec)
1701 subindexECs = lappend(subindexECs, rinfo->parent_ec);
1703 *indexECs = subindexECs;
1707 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1708 plan = NULL; /* keep compiler quiet */
1715 * create_tidscan_plan
1716 * Returns a tidscan plan for the base relation scanned by 'best_path'
1717 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1720 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1721 List *tlist, List *scan_clauses)
1724 Index scan_relid = best_path->path.parent->relid;
1725 List *tidquals = best_path->tidquals;
1728 /* it should be a base rel... */
1729 Assert(scan_relid > 0);
1730 Assert(best_path->path.parent->rtekind == RTE_RELATION);
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->path.param_info)
1742 replace_nestloop_params(root, (Node *) tidquals);
1743 scan_clauses = (List *)
1744 replace_nestloop_params(root, (Node *) scan_clauses);
1748 * Remove any clauses that are TID quals. This is a bit tricky since the
1749 * tidquals list has implicit OR semantics.
1751 ortidquals = tidquals;
1752 if (list_length(ortidquals) > 1)
1753 ortidquals = list_make1(make_orclause(ortidquals));
1754 scan_clauses = list_difference(scan_clauses, ortidquals);
1756 scan_plan = make_tidscan(tlist,
1761 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1767 * create_subqueryscan_plan
1768 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1769 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1771 static SubqueryScan *
1772 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1773 List *tlist, List *scan_clauses)
1775 SubqueryScan *scan_plan;
1776 Index scan_relid = best_path->parent->relid;
1778 /* it should be a subquery base rel... */
1779 Assert(scan_relid > 0);
1780 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1782 /* Sort clauses into best execution order */
1783 scan_clauses = order_qual_clauses(root, scan_clauses);
1785 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1786 scan_clauses = extract_actual_clauses(scan_clauses, false);
1788 /* Replace any outer-relation variables with nestloop params */
1789 if (best_path->param_info)
1791 scan_clauses = (List *)
1792 replace_nestloop_params(root, (Node *) scan_clauses);
1793 process_subquery_nestloop_params(root,
1794 best_path->parent->subplan_params);
1797 scan_plan = make_subqueryscan(tlist,
1800 best_path->parent->subplan);
1802 copy_path_costsize(&scan_plan->scan.plan, best_path);
1808 * create_functionscan_plan
1809 * Returns a functionscan plan for the base relation scanned by 'best_path'
1810 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1812 static FunctionScan *
1813 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1814 List *tlist, List *scan_clauses)
1816 FunctionScan *scan_plan;
1817 Index scan_relid = best_path->parent->relid;
1821 /* it should be a function base rel... */
1822 Assert(scan_relid > 0);
1823 rte = planner_rt_fetch(scan_relid, root);
1824 Assert(rte->rtekind == RTE_FUNCTION);
1825 functions = rte->functions;
1827 /* Sort clauses into best execution order */
1828 scan_clauses = order_qual_clauses(root, scan_clauses);
1830 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1831 scan_clauses = extract_actual_clauses(scan_clauses, false);
1833 /* Replace any outer-relation variables with nestloop params */
1834 if (best_path->param_info)
1836 scan_clauses = (List *)
1837 replace_nestloop_params(root, (Node *) scan_clauses);
1838 /* The function expressions could contain nestloop params, too */
1839 functions = (List *) replace_nestloop_params(root, (Node *) functions);
1842 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1843 functions, rte->funcordinality);
1845 copy_path_costsize(&scan_plan->scan.plan, best_path);
1851 * create_valuesscan_plan
1852 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1853 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1856 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1857 List *tlist, List *scan_clauses)
1859 ValuesScan *scan_plan;
1860 Index scan_relid = best_path->parent->relid;
1864 /* it should be a values base rel... */
1865 Assert(scan_relid > 0);
1866 rte = planner_rt_fetch(scan_relid, root);
1867 Assert(rte->rtekind == RTE_VALUES);
1868 values_lists = rte->values_lists;
1870 /* Sort clauses into best execution order */
1871 scan_clauses = order_qual_clauses(root, scan_clauses);
1873 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1874 scan_clauses = extract_actual_clauses(scan_clauses, false);
1876 /* Replace any outer-relation variables with nestloop params */
1877 if (best_path->param_info)
1879 scan_clauses = (List *)
1880 replace_nestloop_params(root, (Node *) scan_clauses);
1881 /* The values lists could contain nestloop params, too */
1882 values_lists = (List *)
1883 replace_nestloop_params(root, (Node *) values_lists);
1886 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1889 copy_path_costsize(&scan_plan->scan.plan, best_path);
1895 * create_ctescan_plan
1896 * Returns a ctescan plan for the base relation scanned by 'best_path'
1897 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1900 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1901 List *tlist, List *scan_clauses)
1904 Index scan_relid = best_path->parent->relid;
1906 SubPlan *ctesplan = NULL;
1909 PlannerInfo *cteroot;
1914 Assert(scan_relid > 0);
1915 rte = planner_rt_fetch(scan_relid, root);
1916 Assert(rte->rtekind == RTE_CTE);
1917 Assert(!rte->self_reference);
1920 * Find the referenced CTE, and locate the SubPlan previously made for it.
1922 levelsup = rte->ctelevelsup;
1924 while (levelsup-- > 0)
1926 cteroot = cteroot->parent_root;
1927 if (!cteroot) /* shouldn't happen */
1928 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1932 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1933 * on planning the CTEs (ie, this is a side-reference from another CTE).
1934 * So we mustn't use forboth here.
1937 foreach(lc, cteroot->parse->cteList)
1939 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1941 if (strcmp(cte->ctename, rte->ctename) == 0)
1945 if (lc == NULL) /* shouldn't happen */
1946 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1947 if (ndx >= list_length(cteroot->cte_plan_ids))
1948 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1949 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1950 Assert(plan_id > 0);
1951 foreach(lc, cteroot->init_plans)
1953 ctesplan = (SubPlan *) lfirst(lc);
1954 if (ctesplan->plan_id == plan_id)
1957 if (lc == NULL) /* shouldn't happen */
1958 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1961 * We need the CTE param ID, which is the sole member of the SubPlan's
1964 cte_param_id = linitial_int(ctesplan->setParam);
1966 /* Sort clauses into best execution order */
1967 scan_clauses = order_qual_clauses(root, scan_clauses);
1969 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1970 scan_clauses = extract_actual_clauses(scan_clauses, false);
1972 /* Replace any outer-relation variables with nestloop params */
1973 if (best_path->param_info)
1975 scan_clauses = (List *)
1976 replace_nestloop_params(root, (Node *) scan_clauses);
1979 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1980 plan_id, cte_param_id);
1982 copy_path_costsize(&scan_plan->scan.plan, best_path);
1988 * create_worktablescan_plan
1989 * Returns a worktablescan plan for the base relation scanned by 'best_path'
1990 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1992 static WorkTableScan *
1993 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1994 List *tlist, List *scan_clauses)
1996 WorkTableScan *scan_plan;
1997 Index scan_relid = best_path->parent->relid;
2000 PlannerInfo *cteroot;
2002 Assert(scan_relid > 0);
2003 rte = planner_rt_fetch(scan_relid, root);
2004 Assert(rte->rtekind == RTE_CTE);
2005 Assert(rte->self_reference);
2008 * We need to find the worktable param ID, which is in the plan level
2009 * that's processing the recursive UNION, which is one level *below* where
2010 * the CTE comes from.
2012 levelsup = rte->ctelevelsup;
2013 if (levelsup == 0) /* shouldn't happen */
2014 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2017 while (levelsup-- > 0)
2019 cteroot = cteroot->parent_root;
2020 if (!cteroot) /* shouldn't happen */
2021 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
2023 if (cteroot->wt_param_id < 0) /* shouldn't happen */
2024 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
2026 /* Sort clauses into best execution order */
2027 scan_clauses = order_qual_clauses(root, scan_clauses);
2029 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
2030 scan_clauses = extract_actual_clauses(scan_clauses, false);
2032 /* Replace any outer-relation variables with nestloop params */
2033 if (best_path->param_info)
2035 scan_clauses = (List *)
2036 replace_nestloop_params(root, (Node *) scan_clauses);
2039 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
2040 cteroot->wt_param_id);
2042 copy_path_costsize(&scan_plan->scan.plan, best_path);
2048 * create_foreignscan_plan
2049 * Returns a foreignscan plan for the relation scanned by 'best_path'
2050 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
2052 static ForeignScan *
2053 create_foreignscan_plan(PlannerInfo *root, ForeignPath *best_path,
2054 List *tlist, List *scan_clauses)
2056 ForeignScan *scan_plan;
2057 RelOptInfo *rel = best_path->path.parent;
2058 Index scan_relid = rel->relid;
2059 Oid rel_oid = InvalidOid;
2060 Bitmapset *attrs_used = NULL;
2064 Assert(rel->fdwroutine != NULL);
2067 * If we're scanning a base relation, fetch its OID. (Irrelevant if
2068 * scanning a join relation.)
2074 Assert(rel->rtekind == RTE_RELATION);
2075 rte = planner_rt_fetch(scan_relid, root);
2076 Assert(rte->rtekind == RTE_RELATION);
2077 rel_oid = rte->relid;
2081 * Sort clauses into best execution order. We do this first since the FDW
2082 * might have more info than we do and wish to adjust the ordering.
2084 scan_clauses = order_qual_clauses(root, scan_clauses);
2087 * Let the FDW perform its processing on the restriction clauses and
2088 * generate the plan node. Note that the FDW might remove restriction
2089 * clauses that it intends to execute remotely, or even add more (if it
2090 * has selected some join clauses for remote use but also wants them
2091 * rechecked locally).
2093 scan_plan = rel->fdwroutine->GetForeignPlan(root, rel, rel_oid,
2095 tlist, scan_clauses);
2097 /* Copy cost data from Path to Plan; no need to make FDW do this */
2098 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
2100 /* Copy foreign server OID; likewise, no need to make FDW do this */
2101 scan_plan->fs_server = rel->serverid;
2103 /* Likewise, copy the relids that are represented by this foreign scan */
2104 scan_plan->fs_relids = best_path->path.parent->relids;
2107 * Replace any outer-relation variables with nestloop params in the qual
2108 * and fdw_exprs expressions. We do this last so that the FDW doesn't
2109 * have to be involved. (Note that parts of fdw_exprs could have come
2110 * from join clauses, so doing this beforehand on the scan_clauses
2111 * wouldn't work.) We assume fdw_scan_tlist contains no such variables.
2113 if (best_path->path.param_info)
2115 scan_plan->scan.plan.qual = (List *)
2116 replace_nestloop_params(root, (Node *) scan_plan->scan.plan.qual);
2117 scan_plan->fdw_exprs = (List *)
2118 replace_nestloop_params(root, (Node *) scan_plan->fdw_exprs);
2122 * Detect whether any system columns are requested from rel. This is a
2123 * bit of a kluge and might go away someday, so we intentionally leave it
2124 * out of the API presented to FDWs.
2126 * First, examine all the attributes needed for joins or final output.
2127 * Note: we must look at reltargetlist, not the attr_needed data, because
2128 * attr_needed isn't computed for inheritance child rels.
2130 pull_varattnos((Node *) rel->reltargetlist, rel->relid, &attrs_used);
2132 /* Add all the attributes used by restriction clauses. */
2133 foreach(lc, rel->baserestrictinfo)
2135 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2137 pull_varattnos((Node *) rinfo->clause, rel->relid, &attrs_used);
2140 /* Now, are any system columns requested from rel? */
2141 scan_plan->fsSystemCol = false;
2142 for (i = FirstLowInvalidHeapAttributeNumber + 1; i < 0; i++)
2144 if (bms_is_member(i - FirstLowInvalidHeapAttributeNumber, attrs_used))
2146 scan_plan->fsSystemCol = true;
2151 bms_free(attrs_used);
2157 * create_custom_plan
2159 * Transform a CustomPath into a Plan.
2162 create_customscan_plan(PlannerInfo *root, CustomPath *best_path,
2163 List *tlist, List *scan_clauses)
2166 RelOptInfo *rel = best_path->path.parent;
2169 * Sort clauses into the best execution order, although custom-scan
2170 * provider can reorder them again.
2172 scan_clauses = order_qual_clauses(root, scan_clauses);
2175 * Invoke custom plan provider to create the Plan node represented by the
2178 cplan = (CustomScan *) best_path->methods->PlanCustomPath(root,
2183 Assert(IsA(cplan, CustomScan));
2186 * Copy cost data from Path to Plan; no need to make custom-plan providers
2189 copy_path_costsize(&cplan->scan.plan, &best_path->path);
2191 /* Likewise, copy the relids that are represented by this custom scan */
2192 cplan->custom_relids = best_path->path.parent->relids;
2195 * Replace any outer-relation variables with nestloop params in the qual
2196 * and custom_exprs expressions. We do this last so that the custom-plan
2197 * provider doesn't have to be involved. (Note that parts of custom_exprs
2198 * could have come from join clauses, so doing this beforehand on the
2199 * scan_clauses wouldn't work.) We assume custom_scan_tlist contains no
2202 if (best_path->path.param_info)
2204 cplan->scan.plan.qual = (List *)
2205 replace_nestloop_params(root, (Node *) cplan->scan.plan.qual);
2206 cplan->custom_exprs = (List *)
2207 replace_nestloop_params(root, (Node *) cplan->custom_exprs);
2214 /*****************************************************************************
2218 *****************************************************************************/
2221 create_nestloop_plan(PlannerInfo *root,
2222 NestPath *best_path,
2226 NestLoop *join_plan;
2227 List *tlist = build_path_tlist(root, &best_path->path);
2228 List *joinrestrictclauses = best_path->joinrestrictinfo;
2237 /* Sort join qual clauses into best execution order */
2238 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
2240 /* Get the join qual clauses (in plain expression form) */
2241 /* Any pseudoconstant clauses are ignored here */
2242 if (IS_OUTER_JOIN(best_path->jointype))
2244 extract_actual_join_clauses(joinrestrictclauses,
2245 &joinclauses, &otherclauses);
2249 /* We can treat all clauses alike for an inner join */
2250 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
2254 /* Replace any outer-relation variables with nestloop params */
2255 if (best_path->path.param_info)
2257 joinclauses = (List *)
2258 replace_nestloop_params(root, (Node *) joinclauses);
2259 otherclauses = (List *)
2260 replace_nestloop_params(root, (Node *) otherclauses);
2264 * Identify any nestloop parameters that should be supplied by this join
2265 * node, and move them from root->curOuterParams to the nestParams list.
2267 outerrelids = best_path->outerjoinpath->parent->relids;
2270 for (cell = list_head(root->curOuterParams); cell; cell = next)
2272 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
2275 if (IsA(nlp->paramval, Var) &&
2276 bms_is_member(nlp->paramval->varno, outerrelids))
2278 root->curOuterParams = list_delete_cell(root->curOuterParams,
2280 nestParams = lappend(nestParams, nlp);
2282 else if (IsA(nlp->paramval, PlaceHolderVar) &&
2283 bms_overlap(((PlaceHolderVar *) nlp->paramval)->phrels,
2285 bms_is_subset(find_placeholder_info(root,
2286 (PlaceHolderVar *) nlp->paramval,
2290 root->curOuterParams = list_delete_cell(root->curOuterParams,
2292 nestParams = lappend(nestParams, nlp);
2298 join_plan = make_nestloop(tlist,
2304 best_path->jointype);
2306 copy_path_costsize(&join_plan->join.plan, &best_path->path);
2312 create_mergejoin_plan(PlannerInfo *root,
2313 MergePath *best_path,
2317 List *tlist = build_path_tlist(root, &best_path->jpath.path);
2321 List *outerpathkeys;
2322 List *innerpathkeys;
2325 Oid *mergecollations;
2326 int *mergestrategies;
2327 bool *mergenullsfirst;
2328 MergeJoin *join_plan;
2334 /* Sort join qual clauses into best execution order */
2335 /* NB: do NOT reorder the mergeclauses */
2336 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2338 /* Get the join qual clauses (in plain expression form) */
2339 /* Any pseudoconstant clauses are ignored here */
2340 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2342 extract_actual_join_clauses(joinclauses,
2343 &joinclauses, &otherclauses);
2347 /* We can treat all clauses alike for an inner join */
2348 joinclauses = extract_actual_clauses(joinclauses, false);
2353 * Remove the mergeclauses from the list of join qual clauses, leaving the
2354 * list of quals that must be checked as qpquals.
2356 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
2357 joinclauses = list_difference(joinclauses, mergeclauses);
2360 * Replace any outer-relation variables with nestloop params. There
2361 * should not be any in the mergeclauses.
2363 if (best_path->jpath.path.param_info)
2365 joinclauses = (List *)
2366 replace_nestloop_params(root, (Node *) joinclauses);
2367 otherclauses = (List *)
2368 replace_nestloop_params(root, (Node *) otherclauses);
2372 * Rearrange mergeclauses, if needed, so that the outer variable is always
2373 * on the left; mark the mergeclause restrictinfos with correct
2374 * outer_is_left status.
2376 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
2377 best_path->jpath.outerjoinpath->parent->relids);
2380 * Create explicit sort nodes for the outer and inner paths if necessary.
2381 * Make sure there are no excess columns in the inputs if sorting.
2383 if (best_path->outersortkeys)
2385 disuse_physical_tlist(root, outer_plan, best_path->jpath.outerjoinpath);
2386 outer_plan = (Plan *)
2387 make_sort_from_pathkeys(root,
2389 best_path->outersortkeys,
2391 outerpathkeys = best_path->outersortkeys;
2394 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
2396 if (best_path->innersortkeys)
2398 disuse_physical_tlist(root, inner_plan, best_path->jpath.innerjoinpath);
2399 inner_plan = (Plan *)
2400 make_sort_from_pathkeys(root,
2402 best_path->innersortkeys,
2404 innerpathkeys = best_path->innersortkeys;
2407 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
2410 * If specified, add a materialize node to shield the inner plan from the
2411 * need to handle mark/restore.
2413 if (best_path->materialize_inner)
2415 Plan *matplan = (Plan *) make_material(inner_plan);
2418 * We assume the materialize will not spill to disk, and therefore
2419 * charge just cpu_operator_cost per tuple. (Keep this estimate in
2420 * sync with final_cost_mergejoin.)
2422 copy_plan_costsize(matplan, inner_plan);
2423 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
2425 inner_plan = matplan;
2429 * Compute the opfamily/collation/strategy/nullsfirst arrays needed by the
2430 * executor. The information is in the pathkeys for the two inputs, but
2431 * we need to be careful about the possibility of mergeclauses sharing a
2432 * pathkey (compare find_mergeclauses_for_pathkeys()).
2434 nClauses = list_length(mergeclauses);
2435 Assert(nClauses == list_length(best_path->path_mergeclauses));
2436 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
2437 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
2438 mergestrategies = (int *) palloc(nClauses * sizeof(int));
2439 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
2441 lop = list_head(outerpathkeys);
2442 lip = list_head(innerpathkeys);
2444 foreach(lc, best_path->path_mergeclauses)
2446 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
2447 EquivalenceClass *oeclass;
2448 EquivalenceClass *ieclass;
2451 EquivalenceClass *opeclass;
2452 EquivalenceClass *ipeclass;
2455 /* fetch outer/inner eclass from mergeclause */
2456 Assert(IsA(rinfo, RestrictInfo));
2457 if (rinfo->outer_is_left)
2459 oeclass = rinfo->left_ec;
2460 ieclass = rinfo->right_ec;
2464 oeclass = rinfo->right_ec;
2465 ieclass = rinfo->left_ec;
2467 Assert(oeclass != NULL);
2468 Assert(ieclass != NULL);
2471 * For debugging purposes, we check that the eclasses match the paths'
2472 * pathkeys. In typical cases the merge clauses are one-to-one with
2473 * the pathkeys, but when dealing with partially redundant query
2474 * conditions, we might have clauses that re-reference earlier path
2475 * keys. The case that we need to reject is where a pathkey is
2476 * entirely skipped over.
2478 * lop and lip reference the first as-yet-unused pathkey elements;
2479 * it's okay to match them, or any element before them. If they're
2480 * NULL then we have found all pathkey elements to be used.
2484 opathkey = (PathKey *) lfirst(lop);
2485 opeclass = opathkey->pk_eclass;
2486 if (oeclass == opeclass)
2488 /* fast path for typical case */
2493 /* redundant clauses ... must match something before lop */
2494 foreach(l2, outerpathkeys)
2498 opathkey = (PathKey *) lfirst(l2);
2499 opeclass = opathkey->pk_eclass;
2500 if (oeclass == opeclass)
2503 if (oeclass != opeclass)
2504 elog(ERROR, "outer pathkeys do not match mergeclauses");
2509 /* redundant clauses ... must match some already-used pathkey */
2512 foreach(l2, outerpathkeys)
2514 opathkey = (PathKey *) lfirst(l2);
2515 opeclass = opathkey->pk_eclass;
2516 if (oeclass == opeclass)
2520 elog(ERROR, "outer pathkeys do not match mergeclauses");
2525 ipathkey = (PathKey *) lfirst(lip);
2526 ipeclass = ipathkey->pk_eclass;
2527 if (ieclass == ipeclass)
2529 /* fast path for typical case */
2534 /* redundant clauses ... must match something before lip */
2535 foreach(l2, innerpathkeys)
2539 ipathkey = (PathKey *) lfirst(l2);
2540 ipeclass = ipathkey->pk_eclass;
2541 if (ieclass == ipeclass)
2544 if (ieclass != ipeclass)
2545 elog(ERROR, "inner pathkeys do not match mergeclauses");
2550 /* redundant clauses ... must match some already-used pathkey */
2553 foreach(l2, innerpathkeys)
2555 ipathkey = (PathKey *) lfirst(l2);
2556 ipeclass = ipathkey->pk_eclass;
2557 if (ieclass == ipeclass)
2561 elog(ERROR, "inner pathkeys do not match mergeclauses");
2564 /* pathkeys should match each other too (more debugging) */
2565 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2566 opathkey->pk_eclass->ec_collation != ipathkey->pk_eclass->ec_collation ||
2567 opathkey->pk_strategy != ipathkey->pk_strategy ||
2568 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2569 elog(ERROR, "left and right pathkeys do not match in mergejoin");
2571 /* OK, save info for executor */
2572 mergefamilies[i] = opathkey->pk_opfamily;
2573 mergecollations[i] = opathkey->pk_eclass->ec_collation;
2574 mergestrategies[i] = opathkey->pk_strategy;
2575 mergenullsfirst[i] = opathkey->pk_nulls_first;
2580 * Note: it is not an error if we have additional pathkey elements (i.e.,
2581 * lop or lip isn't NULL here). The input paths might be better-sorted
2582 * than we need for the current mergejoin.
2586 * Now we can build the mergejoin node.
2588 join_plan = make_mergejoin(tlist,
2598 best_path->jpath.jointype);
2600 /* Costs of sort and material steps are included in path cost already */
2601 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2607 create_hashjoin_plan(PlannerInfo *root,
2608 HashPath *best_path,
2612 List *tlist = build_path_tlist(root, &best_path->jpath.path);
2616 Oid skewTable = InvalidOid;
2617 AttrNumber skewColumn = InvalidAttrNumber;
2618 bool skewInherit = false;
2619 Oid skewColType = InvalidOid;
2620 int32 skewColTypmod = -1;
2621 HashJoin *join_plan;
2624 /* Sort join qual clauses into best execution order */
2625 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2626 /* There's no point in sorting the hash clauses ... */
2628 /* Get the join qual clauses (in plain expression form) */
2629 /* Any pseudoconstant clauses are ignored here */
2630 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2632 extract_actual_join_clauses(joinclauses,
2633 &joinclauses, &otherclauses);
2637 /* We can treat all clauses alike for an inner join */
2638 joinclauses = extract_actual_clauses(joinclauses, false);
2643 * Remove the hashclauses from the list of join qual clauses, leaving the
2644 * list of quals that must be checked as qpquals.
2646 hashclauses = get_actual_clauses(best_path->path_hashclauses);
2647 joinclauses = list_difference(joinclauses, hashclauses);
2650 * Replace any outer-relation variables with nestloop params. There
2651 * should not be any in the hashclauses.
2653 if (best_path->jpath.path.param_info)
2655 joinclauses = (List *)
2656 replace_nestloop_params(root, (Node *) joinclauses);
2657 otherclauses = (List *)
2658 replace_nestloop_params(root, (Node *) otherclauses);
2662 * Rearrange hashclauses, if needed, so that the outer variable is always
2665 hashclauses = get_switched_clauses(best_path->path_hashclauses,
2666 best_path->jpath.outerjoinpath->parent->relids);
2668 /* We don't want any excess columns in the hashed tuples */
2669 disuse_physical_tlist(root, inner_plan, best_path->jpath.innerjoinpath);
2671 /* If we expect batching, suppress excess columns in outer tuples too */
2672 if (best_path->num_batches > 1)
2673 disuse_physical_tlist(root, outer_plan, best_path->jpath.outerjoinpath);
2676 * If there is a single join clause and we can identify the outer variable
2677 * as a simple column reference, supply its identity for possible use in
2678 * skew optimization. (Note: in principle we could do skew optimization
2679 * with multiple join clauses, but we'd have to be able to determine the
2680 * most common combinations of outer values, which we don't currently have
2681 * enough stats for.)
2683 if (list_length(hashclauses) == 1)
2685 OpExpr *clause = (OpExpr *) linitial(hashclauses);
2688 Assert(is_opclause(clause));
2689 node = (Node *) linitial(clause->args);
2690 if (IsA(node, RelabelType))
2691 node = (Node *) ((RelabelType *) node)->arg;
2694 Var *var = (Var *) node;
2697 rte = root->simple_rte_array[var->varno];
2698 if (rte->rtekind == RTE_RELATION)
2700 skewTable = rte->relid;
2701 skewColumn = var->varattno;
2702 skewInherit = rte->inh;
2703 skewColType = var->vartype;
2704 skewColTypmod = var->vartypmod;
2710 * Build the hash node and hash join node.
2712 hash_plan = make_hash(inner_plan,
2718 join_plan = make_hashjoin(tlist,
2724 best_path->jpath.jointype);
2726 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2732 /*****************************************************************************
2734 * SUPPORTING ROUTINES
2736 *****************************************************************************/
2739 * replace_nestloop_params
2740 * Replace outer-relation Vars and PlaceHolderVars in the given expression
2741 * with nestloop Params
2743 * All Vars and PlaceHolderVars belonging to the relation(s) identified by
2744 * root->curOuterRels are replaced by Params, and entries are added to
2745 * root->curOuterParams if not already present.
2748 replace_nestloop_params(PlannerInfo *root, Node *expr)
2750 /* No setup needed for tree walk, so away we go */
2751 return replace_nestloop_params_mutator(expr, root);
2755 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2761 Var *var = (Var *) node;
2766 /* Upper-level Vars should be long gone at this point */
2767 Assert(var->varlevelsup == 0);
2768 /* If not to be replaced, we can just return the Var unmodified */
2769 if (!bms_is_member(var->varno, root->curOuterRels))
2771 /* Create a Param representing the Var */
2772 param = assign_nestloop_param_var(root, var);
2773 /* Is this param already listed in root->curOuterParams? */
2774 foreach(lc, root->curOuterParams)
2776 nlp = (NestLoopParam *) lfirst(lc);
2777 if (nlp->paramno == param->paramid)
2779 Assert(equal(var, nlp->paramval));
2780 /* Present, so we can just return the Param */
2781 return (Node *) param;
2785 nlp = makeNode(NestLoopParam);
2786 nlp->paramno = param->paramid;
2787 nlp->paramval = var;
2788 root->curOuterParams = lappend(root->curOuterParams, nlp);
2789 /* And return the replacement Param */
2790 return (Node *) param;
2792 if (IsA(node, PlaceHolderVar))
2794 PlaceHolderVar *phv = (PlaceHolderVar *) node;
2799 /* Upper-level PlaceHolderVars should be long gone at this point */
2800 Assert(phv->phlevelsup == 0);
2803 * Check whether we need to replace the PHV. We use bms_overlap as a
2804 * cheap/quick test to see if the PHV might be evaluated in the outer
2805 * rels, and then grab its PlaceHolderInfo to tell for sure.
2807 if (!bms_overlap(phv->phrels, root->curOuterRels) ||
2808 !bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2809 root->curOuterRels))
2812 * We can't replace the whole PHV, but we might still need to
2813 * replace Vars or PHVs within its expression, in case it ends up
2814 * actually getting evaluated here. (It might get evaluated in
2815 * this plan node, or some child node; in the latter case we don't
2816 * really need to process the expression here, but we haven't got
2817 * enough info to tell if that's the case.) Flat-copy the PHV
2818 * node and then recurse on its expression.
2820 * Note that after doing this, we might have different
2821 * representations of the contents of the same PHV in different
2822 * parts of the plan tree. This is OK because equal() will just
2823 * match on phid/phlevelsup, so setrefs.c will still recognize an
2824 * upper-level reference to a lower-level copy of the same PHV.
2826 PlaceHolderVar *newphv = makeNode(PlaceHolderVar);
2828 memcpy(newphv, phv, sizeof(PlaceHolderVar));
2829 newphv->phexpr = (Expr *)
2830 replace_nestloop_params_mutator((Node *) phv->phexpr,
2832 return (Node *) newphv;
2834 /* Create a Param representing the PlaceHolderVar */
2835 param = assign_nestloop_param_placeholdervar(root, phv);
2836 /* Is this param already listed in root->curOuterParams? */
2837 foreach(lc, root->curOuterParams)
2839 nlp = (NestLoopParam *) lfirst(lc);
2840 if (nlp->paramno == param->paramid)
2842 Assert(equal(phv, nlp->paramval));
2843 /* Present, so we can just return the Param */
2844 return (Node *) param;
2848 nlp = makeNode(NestLoopParam);
2849 nlp->paramno = param->paramid;
2850 nlp->paramval = (Var *) phv;
2851 root->curOuterParams = lappend(root->curOuterParams, nlp);
2852 /* And return the replacement Param */
2853 return (Node *) param;
2855 return expression_tree_mutator(node,
2856 replace_nestloop_params_mutator,
2861 * process_subquery_nestloop_params
2862 * Handle params of a parameterized subquery that need to be fed
2863 * from an outer nestloop.
2865 * Currently, that would be *all* params that a subquery in FROM has demanded
2866 * from the current query level, since they must be LATERAL references.
2868 * The subplan's references to the outer variables are already represented
2869 * as PARAM_EXEC Params, so we need not modify the subplan here. What we
2870 * do need to do is add entries to root->curOuterParams to signal the parent
2871 * nestloop plan node that it must provide these values.
2874 process_subquery_nestloop_params(PlannerInfo *root, List *subplan_params)
2878 foreach(ppl, subplan_params)
2880 PlannerParamItem *pitem = (PlannerParamItem *) lfirst(ppl);
2882 if (IsA(pitem->item, Var))
2884 Var *var = (Var *) pitem->item;
2888 /* If not from a nestloop outer rel, complain */
2889 if (!bms_is_member(var->varno, root->curOuterRels))
2890 elog(ERROR, "non-LATERAL parameter required by subquery");
2891 /* Is this param already listed in root->curOuterParams? */
2892 foreach(lc, root->curOuterParams)
2894 nlp = (NestLoopParam *) lfirst(lc);
2895 if (nlp->paramno == pitem->paramId)
2897 Assert(equal(var, nlp->paramval));
2898 /* Present, so nothing to do */
2905 nlp = makeNode(NestLoopParam);
2906 nlp->paramno = pitem->paramId;
2907 nlp->paramval = copyObject(var);
2908 root->curOuterParams = lappend(root->curOuterParams, nlp);
2911 else if (IsA(pitem->item, PlaceHolderVar))
2913 PlaceHolderVar *phv = (PlaceHolderVar *) pitem->item;
2917 /* If not from a nestloop outer rel, complain */
2918 if (!bms_is_subset(find_placeholder_info(root, phv, false)->ph_eval_at,
2919 root->curOuterRels))
2920 elog(ERROR, "non-LATERAL parameter required by subquery");
2921 /* Is this param already listed in root->curOuterParams? */
2922 foreach(lc, root->curOuterParams)
2924 nlp = (NestLoopParam *) lfirst(lc);
2925 if (nlp->paramno == pitem->paramId)
2927 Assert(equal(phv, nlp->paramval));
2928 /* Present, so nothing to do */
2935 nlp = makeNode(NestLoopParam);
2936 nlp->paramno = pitem->paramId;
2937 nlp->paramval = copyObject(phv);
2938 root->curOuterParams = lappend(root->curOuterParams, nlp);
2942 elog(ERROR, "unexpected type of subquery parameter");
2947 * fix_indexqual_references
2948 * Adjust indexqual clauses to the form the executor's indexqual
2951 * We have four tasks here:
2952 * * Remove RestrictInfo nodes from the input clauses.
2953 * * Replace any outer-relation Var or PHV nodes with nestloop Params.
2954 * (XXX eventually, that responsibility should go elsewhere?)
2955 * * Index keys must be represented by Var nodes with varattno set to the
2956 * index's attribute number, not the attribute number in the original rel.
2957 * * If the index key is on the right, commute the clause to put it on the
2960 * The result is a modified copy of the path's indexquals list --- the
2961 * original is not changed. Note also that the copy shares no substructure
2962 * with the original; this is needed in case there is a subplan in it (we need
2963 * two separate copies of the subplan tree, or things will go awry).
2966 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path)
2968 IndexOptInfo *index = index_path->indexinfo;
2969 List *fixed_indexquals;
2973 fixed_indexquals = NIL;
2975 forboth(lcc, index_path->indexquals, lci, index_path->indexqualcols)
2977 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lcc);
2978 int indexcol = lfirst_int(lci);
2981 Assert(IsA(rinfo, RestrictInfo));
2984 * Replace any outer-relation variables with nestloop params.
2986 * This also makes a copy of the clause, so it's safe to modify it
2989 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2991 if (IsA(clause, OpExpr))
2993 OpExpr *op = (OpExpr *) clause;
2995 if (list_length(op->args) != 2)
2996 elog(ERROR, "indexqual clause is not binary opclause");
2999 * Check to see if the indexkey is on the right; if so, commute
3000 * the clause. The indexkey should be the side that refers to
3001 * (only) the base relation.
3003 if (!bms_equal(rinfo->left_relids, index->rel->relids))
3007 * Now replace the indexkey expression with an index Var.
3009 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
3013 else if (IsA(clause, RowCompareExpr))
3015 RowCompareExpr *rc = (RowCompareExpr *) clause;
3023 * Re-discover which index columns are used in the rowcompare.
3025 newrc = adjust_rowcompare_for_index(rc,
3032 * Trouble if adjust_rowcompare_for_index thought the
3033 * RowCompareExpr didn't match the index as-is; the clause should
3034 * have gone through that routine already.
3036 if (newrc != (Expr *) rc)
3037 elog(ERROR, "inconsistent results from adjust_rowcompare_for_index");
3040 * Check to see if the indexkey is on the right; if so, commute
3044 CommuteRowCompareExpr(rc);
3047 * Now replace the indexkey expressions with index Vars.
3049 Assert(list_length(rc->largs) == list_length(indexcolnos));
3050 forboth(lca, rc->largs, lcai, indexcolnos)
3052 lfirst(lca) = fix_indexqual_operand(lfirst(lca),
3057 else if (IsA(clause, ScalarArrayOpExpr))
3059 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
3061 /* Never need to commute... */
3063 /* Replace the indexkey expression with an index Var. */
3064 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
3068 else if (IsA(clause, NullTest))
3070 NullTest *nt = (NullTest *) clause;
3072 /* Replace the indexkey expression with an index Var. */
3073 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
3078 elog(ERROR, "unsupported indexqual type: %d",
3079 (int) nodeTag(clause));
3081 fixed_indexquals = lappend(fixed_indexquals, clause);
3084 return fixed_indexquals;
3088 * fix_indexorderby_references
3089 * Adjust indexorderby clauses to the form the executor's index
3092 * This is a simplified version of fix_indexqual_references. The input does
3093 * not have RestrictInfo nodes, and we assume that indxpath.c already
3094 * commuted the clauses to put the index keys on the left. Also, we don't
3095 * bother to support any cases except simple OpExprs, since nothing else
3096 * is allowed for ordering operators.
3099 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path)
3101 IndexOptInfo *index = index_path->indexinfo;
3102 List *fixed_indexorderbys;
3106 fixed_indexorderbys = NIL;
3108 forboth(lcc, index_path->indexorderbys, lci, index_path->indexorderbycols)
3110 Node *clause = (Node *) lfirst(lcc);
3111 int indexcol = lfirst_int(lci);
3114 * Replace any outer-relation variables with nestloop params.
3116 * This also makes a copy of the clause, so it's safe to modify it
3119 clause = replace_nestloop_params(root, clause);
3121 if (IsA(clause, OpExpr))
3123 OpExpr *op = (OpExpr *) clause;
3125 if (list_length(op->args) != 2)
3126 elog(ERROR, "indexorderby clause is not binary opclause");
3129 * Now replace the indexkey expression with an index Var.
3131 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
3136 elog(ERROR, "unsupported indexorderby type: %d",
3137 (int) nodeTag(clause));
3139 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
3142 return fixed_indexorderbys;
3146 * fix_indexqual_operand
3147 * Convert an indexqual expression to a Var referencing the index column.
3149 * We represent index keys by Var nodes having varno == INDEX_VAR and varattno
3150 * equal to the index's attribute number (index column position).
3152 * Most of the code here is just for sanity cross-checking that the given
3153 * expression actually matches the index column it's claimed to.
3156 fix_indexqual_operand(Node *node, IndexOptInfo *index, int indexcol)
3160 ListCell *indexpr_item;
3163 * Remove any binary-compatible relabeling of the indexkey
3165 if (IsA(node, RelabelType))
3166 node = (Node *) ((RelabelType *) node)->arg;
3168 Assert(indexcol >= 0 && indexcol < index->ncolumns);
3170 if (index->indexkeys[indexcol] != 0)
3172 /* It's a simple index column */
3173 if (IsA(node, Var) &&
3174 ((Var *) node)->varno == index->rel->relid &&
3175 ((Var *) node)->varattno == index->indexkeys[indexcol])
3177 result = (Var *) copyObject(node);
3178 result->varno = INDEX_VAR;
3179 result->varattno = indexcol + 1;
3180 return (Node *) result;
3183 elog(ERROR, "index key does not match expected index column");
3186 /* It's an index expression, so find and cross-check the expression */
3187 indexpr_item = list_head(index->indexprs);
3188 for (pos = 0; pos < index->ncolumns; pos++)
3190 if (index->indexkeys[pos] == 0)
3192 if (indexpr_item == NULL)
3193 elog(ERROR, "too few entries in indexprs list");
3194 if (pos == indexcol)
3198 indexkey = (Node *) lfirst(indexpr_item);
3199 if (indexkey && IsA(indexkey, RelabelType))
3200 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
3201 if (equal(node, indexkey))
3203 result = makeVar(INDEX_VAR, indexcol + 1,
3204 exprType(lfirst(indexpr_item)), -1,
3205 exprCollation(lfirst(indexpr_item)),
3207 return (Node *) result;
3210 elog(ERROR, "index key does not match expected index column");
3212 indexpr_item = lnext(indexpr_item);
3217 elog(ERROR, "index key does not match expected index column");
3218 return NULL; /* keep compiler quiet */
3222 * get_switched_clauses
3223 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
3224 * extract the bare clauses, and rearrange the elements within the
3225 * clauses, if needed, so the outer join variable is on the left and
3226 * the inner is on the right. The original clause data structure is not
3227 * touched; a modified list is returned. We do, however, set the transient
3228 * outer_is_left field in each RestrictInfo to show which side was which.
3231 get_switched_clauses(List *clauses, Relids outerrelids)
3238 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
3239 OpExpr *clause = (OpExpr *) restrictinfo->clause;
3241 Assert(is_opclause(clause));
3242 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
3245 * Duplicate just enough of the structure to allow commuting the
3246 * clause without changing the original list. Could use
3247 * copyObject, but a complete deep copy is overkill.
3249 OpExpr *temp = makeNode(OpExpr);
3251 temp->opno = clause->opno;
3252 temp->opfuncid = InvalidOid;
3253 temp->opresulttype = clause->opresulttype;
3254 temp->opretset = clause->opretset;
3255 temp->opcollid = clause->opcollid;
3256 temp->inputcollid = clause->inputcollid;
3257 temp->args = list_copy(clause->args);
3258 temp->location = clause->location;
3259 /* Commute it --- note this modifies the temp node in-place. */
3260 CommuteOpExpr(temp);
3261 t_list = lappend(t_list, temp);
3262 restrictinfo->outer_is_left = false;
3266 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
3267 t_list = lappend(t_list, clause);
3268 restrictinfo->outer_is_left = true;
3275 * order_qual_clauses
3276 * Given a list of qual clauses that will all be evaluated at the same
3277 * plan node, sort the list into the order we want to check the quals
3280 * Ideally the order should be driven by a combination of execution cost and
3281 * selectivity, but it's not immediately clear how to account for both,
3282 * and given the uncertainty of the estimates the reliability of the decisions
3283 * would be doubtful anyway. So we just order by estimated per-tuple cost,
3284 * being careful not to change the order when (as is often the case) the
3285 * estimates are identical.
3287 * Although this will work on either bare clauses or RestrictInfos, it's
3288 * much faster to apply it to RestrictInfos, since it can re-use cost
3289 * information that is cached in RestrictInfos.
3291 * Note: some callers pass lists that contain entries that will later be
3292 * removed; this is the easiest way to let this routine see RestrictInfos
3293 * instead of bare clauses. It's OK because we only sort by cost, but
3294 * a cost/selectivity combination would likely do the wrong thing.
3297 order_qual_clauses(PlannerInfo *root, List *clauses)
3304 int nitems = list_length(clauses);
3310 /* No need to work hard for 0 or 1 clause */
3315 * Collect the items and costs into an array. This is to avoid repeated
3316 * cost_qual_eval work if the inputs aren't RestrictInfos.
3318 items = (QualItem *) palloc(nitems * sizeof(QualItem));
3320 foreach(lc, clauses)
3322 Node *clause = (Node *) lfirst(lc);
3325 cost_qual_eval_node(&qcost, clause, root);
3326 items[i].clause = clause;
3327 items[i].cost = qcost.per_tuple;
3332 * Sort. We don't use qsort() because it's not guaranteed stable for
3333 * equal keys. The expected number of entries is small enough that a
3334 * simple insertion sort should be good enough.
3336 for (i = 1; i < nitems; i++)
3338 QualItem newitem = items[i];
3341 /* insert newitem into the already-sorted subarray */
3342 for (j = i; j > 0; j--)
3344 if (newitem.cost >= items[j - 1].cost)
3346 items[j] = items[j - 1];
3351 /* Convert back to a list */
3353 for (i = 0; i < nitems; i++)
3354 result = lappend(result, items[i].clause);
3360 * Copy cost and size info from a Path node to the Plan node created from it.
3361 * The executor usually won't use this info, but it's needed by EXPLAIN.
3364 copy_path_costsize(Plan *dest, Path *src)
3368 dest->startup_cost = src->startup_cost;
3369 dest->total_cost = src->total_cost;
3370 dest->plan_rows = src->rows;
3371 dest->plan_width = src->parent->width;
3375 dest->startup_cost = 0;
3376 dest->total_cost = 0;
3377 dest->plan_rows = 0;
3378 dest->plan_width = 0;
3383 * Copy cost and size info from a lower plan node to an inserted node.
3384 * (Most callers alter the info after copying it.)
3387 copy_plan_costsize(Plan *dest, Plan *src)
3391 dest->startup_cost = src->startup_cost;
3392 dest->total_cost = src->total_cost;
3393 dest->plan_rows = src->plan_rows;
3394 dest->plan_width = src->plan_width;
3398 dest->startup_cost = 0;
3399 dest->total_cost = 0;
3400 dest->plan_rows = 0;
3401 dest->plan_width = 0;
3406 /*****************************************************************************
3408 * PLAN NODE BUILDING ROUTINES
3410 * Some of these are exported because they are called to build plan nodes
3411 * in contexts where we're not deriving the plan node from a path node.
3413 *****************************************************************************/
3416 make_seqscan(List *qptlist,
3420 SeqScan *node = makeNode(SeqScan);
3421 Plan *plan = &node->plan;
3423 /* cost should be inserted by caller */
3424 plan->targetlist = qptlist;
3425 plan->qual = qpqual;
3426 plan->lefttree = NULL;
3427 plan->righttree = NULL;
3428 node->scanrelid = scanrelid;
3434 make_samplescan(List *qptlist,
3438 SampleScan *node = makeNode(SampleScan);
3439 Plan *plan = &node->plan;
3441 /* cost should be inserted by caller */
3442 plan->targetlist = qptlist;
3443 plan->qual = qpqual;
3444 plan->lefttree = NULL;
3445 plan->righttree = NULL;
3446 node->scanrelid = scanrelid;
3452 make_indexscan(List *qptlist,
3457 List *indexqualorig,
3459 List *indexorderbyorig,
3460 Oid *indexorderbyops,
3461 ScanDirection indexscandir)
3463 IndexScan *node = makeNode(IndexScan);
3464 Plan *plan = &node->scan.plan;
3466 /* cost should be inserted by caller */
3467 plan->targetlist = qptlist;
3468 plan->qual = qpqual;
3469 plan->lefttree = NULL;
3470 plan->righttree = NULL;
3471 node->scan.scanrelid = scanrelid;
3472 node->indexid = indexid;
3473 node->indexqual = indexqual;
3474 node->indexqualorig = indexqualorig;
3475 node->indexorderby = indexorderby;
3476 node->indexorderbyorig = indexorderbyorig;
3477 node->indexorderbyops = indexorderbyops;
3478 node->indexorderdir = indexscandir;
3483 static IndexOnlyScan *
3484 make_indexonlyscan(List *qptlist,
3491 ScanDirection indexscandir)
3493 IndexOnlyScan *node = makeNode(IndexOnlyScan);
3494 Plan *plan = &node->scan.plan;
3496 /* cost should be inserted by caller */
3497 plan->targetlist = qptlist;
3498 plan->qual = qpqual;
3499 plan->lefttree = NULL;
3500 plan->righttree = NULL;
3501 node->scan.scanrelid = scanrelid;
3502 node->indexid = indexid;
3503 node->indexqual = indexqual;
3504 node->indexorderby = indexorderby;
3505 node->indextlist = indextlist;
3506 node->indexorderdir = indexscandir;
3511 static BitmapIndexScan *
3512 make_bitmap_indexscan(Index scanrelid,
3515 List *indexqualorig)
3517 BitmapIndexScan *node = makeNode(BitmapIndexScan);
3518 Plan *plan = &node->scan.plan;
3520 /* cost should be inserted by caller */
3521 plan->targetlist = NIL; /* not used */
3522 plan->qual = NIL; /* not used */
3523 plan->lefttree = NULL;
3524 plan->righttree = NULL;
3525 node->scan.scanrelid = scanrelid;
3526 node->indexid = indexid;
3527 node->indexqual = indexqual;
3528 node->indexqualorig = indexqualorig;
3533 static BitmapHeapScan *
3534 make_bitmap_heapscan(List *qptlist,
3537 List *bitmapqualorig,
3540 BitmapHeapScan *node = makeNode(BitmapHeapScan);
3541 Plan *plan = &node->scan.plan;
3543 /* cost should be inserted by caller */
3544 plan->targetlist = qptlist;
3545 plan->qual = qpqual;
3546 plan->lefttree = lefttree;
3547 plan->righttree = NULL;
3548 node->scan.scanrelid = scanrelid;
3549 node->bitmapqualorig = bitmapqualorig;
3555 make_tidscan(List *qptlist,
3560 TidScan *node = makeNode(TidScan);
3561 Plan *plan = &node->scan.plan;
3563 /* cost should be inserted by caller */
3564 plan->targetlist = qptlist;
3565 plan->qual = qpqual;
3566 plan->lefttree = NULL;
3567 plan->righttree = NULL;
3568 node->scan.scanrelid = scanrelid;
3569 node->tidquals = tidquals;
3575 make_subqueryscan(List *qptlist,
3580 SubqueryScan *node = makeNode(SubqueryScan);
3581 Plan *plan = &node->scan.plan;
3584 * Cost is figured here for the convenience of prepunion.c. Note this is
3585 * only correct for the case where qpqual is empty; otherwise caller
3586 * should overwrite cost with a better estimate.
3588 copy_plan_costsize(plan, subplan);
3589 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
3591 plan->targetlist = qptlist;
3592 plan->qual = qpqual;
3593 plan->lefttree = NULL;
3594 plan->righttree = NULL;
3595 node->scan.scanrelid = scanrelid;
3596 node->subplan = subplan;
3601 static FunctionScan *
3602 make_functionscan(List *qptlist,
3606 bool funcordinality)
3608 FunctionScan *node = makeNode(FunctionScan);
3609 Plan *plan = &node->scan.plan;
3611 /* cost should be inserted by caller */
3612 plan->targetlist = qptlist;
3613 plan->qual = qpqual;
3614 plan->lefttree = NULL;
3615 plan->righttree = NULL;
3616 node->scan.scanrelid = scanrelid;
3617 node->functions = functions;
3618 node->funcordinality = funcordinality;
3624 make_valuesscan(List *qptlist,
3629 ValuesScan *node = makeNode(ValuesScan);
3630 Plan *plan = &node->scan.plan;
3632 /* cost should be inserted by caller */
3633 plan->targetlist = qptlist;
3634 plan->qual = qpqual;
3635 plan->lefttree = NULL;
3636 plan->righttree = NULL;
3637 node->scan.scanrelid = scanrelid;
3638 node->values_lists = values_lists;
3644 make_ctescan(List *qptlist,
3650 CteScan *node = makeNode(CteScan);
3651 Plan *plan = &node->scan.plan;
3653 /* cost should be inserted by caller */
3654 plan->targetlist = qptlist;
3655 plan->qual = qpqual;
3656 plan->lefttree = NULL;
3657 plan->righttree = NULL;
3658 node->scan.scanrelid = scanrelid;
3659 node->ctePlanId = ctePlanId;
3660 node->cteParam = cteParam;
3665 static WorkTableScan *
3666 make_worktablescan(List *qptlist,
3671 WorkTableScan *node = makeNode(WorkTableScan);
3672 Plan *plan = &node->scan.plan;
3674 /* cost should be inserted by caller */
3675 plan->targetlist = qptlist;
3676 plan->qual = qpqual;
3677 plan->lefttree = NULL;
3678 plan->righttree = NULL;
3679 node->scan.scanrelid = scanrelid;
3680 node->wtParam = wtParam;
3686 make_foreignscan(List *qptlist,
3691 List *fdw_scan_tlist)
3693 ForeignScan *node = makeNode(ForeignScan);
3694 Plan *plan = &node->scan.plan;
3696 /* cost will be filled in by create_foreignscan_plan */
3697 plan->targetlist = qptlist;
3698 plan->qual = qpqual;
3699 plan->lefttree = NULL;
3700 plan->righttree = NULL;
3701 node->scan.scanrelid = scanrelid;
3702 /* fs_server will be filled in by create_foreignscan_plan */
3703 node->fs_server = InvalidOid;
3704 node->fdw_exprs = fdw_exprs;
3705 node->fdw_private = fdw_private;
3706 node->fdw_scan_tlist = fdw_scan_tlist;
3707 /* fs_relids will be filled in by create_foreignscan_plan */
3708 node->fs_relids = NULL;
3709 /* fsSystemCol will be filled in by create_foreignscan_plan */
3710 node->fsSystemCol = false;
3716 make_append(List *appendplans, List *tlist)
3718 Append *node = makeNode(Append);
3719 Plan *plan = &node->plan;
3724 * Compute cost as sum of subplan costs. We charge nothing extra for the
3725 * Append itself, which perhaps is too optimistic, but since it doesn't do
3726 * any selection or projection, it is a pretty cheap node.
3728 * If you change this, see also create_append_path(). Also, the size
3729 * calculations should match set_append_rel_pathlist(). It'd be better
3730 * not to duplicate all this logic, but some callers of this function
3731 * aren't working from an appendrel or AppendPath, so there's noplace to
3732 * copy the data from.
3734 plan->startup_cost = 0;
3735 plan->total_cost = 0;
3736 plan->plan_rows = 0;
3738 foreach(subnode, appendplans)
3740 Plan *subplan = (Plan *) lfirst(subnode);
3742 if (subnode == list_head(appendplans)) /* first node? */
3743 plan->startup_cost = subplan->startup_cost;
3744 plan->total_cost += subplan->total_cost;
3745 plan->plan_rows += subplan->plan_rows;
3746 total_size += subplan->plan_width * subplan->plan_rows;
3748 if (plan->plan_rows > 0)
3749 plan->plan_width = rint(total_size / plan->plan_rows);
3751 plan->plan_width = 0;
3753 plan->targetlist = tlist;
3755 plan->lefttree = NULL;
3756 plan->righttree = NULL;
3757 node->appendplans = appendplans;
3763 make_recursive_union(List *tlist,
3770 RecursiveUnion *node = makeNode(RecursiveUnion);
3771 Plan *plan = &node->plan;
3772 int numCols = list_length(distinctList);
3774 cost_recursive_union(plan, lefttree, righttree);
3776 plan->targetlist = tlist;
3778 plan->lefttree = lefttree;
3779 plan->righttree = righttree;
3780 node->wtParam = wtParam;
3783 * convert SortGroupClause list into arrays of attr indexes and equality
3784 * operators, as wanted by executor
3786 node->numCols = numCols;
3790 AttrNumber *dupColIdx;
3794 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3795 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3797 foreach(slitem, distinctList)
3799 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3800 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3803 dupColIdx[keyno] = tle->resno;
3804 dupOperators[keyno] = sortcl->eqop;
3805 Assert(OidIsValid(dupOperators[keyno]));
3808 node->dupColIdx = dupColIdx;
3809 node->dupOperators = dupOperators;
3811 node->numGroups = numGroups;
3817 make_bitmap_and(List *bitmapplans)
3819 BitmapAnd *node = makeNode(BitmapAnd);
3820 Plan *plan = &node->plan;
3822 /* cost should be inserted by caller */
3823 plan->targetlist = NIL;
3825 plan->lefttree = NULL;
3826 plan->righttree = NULL;
3827 node->bitmapplans = bitmapplans;
3833 make_bitmap_or(List *bitmapplans)
3835 BitmapOr *node = makeNode(BitmapOr);
3836 Plan *plan = &node->plan;
3838 /* cost should be inserted by caller */
3839 plan->targetlist = NIL;
3841 plan->lefttree = NULL;
3842 plan->righttree = NULL;
3843 node->bitmapplans = bitmapplans;
3849 make_nestloop(List *tlist,
3857 NestLoop *node = makeNode(NestLoop);
3858 Plan *plan = &node->join.plan;
3860 /* cost should be inserted by caller */
3861 plan->targetlist = tlist;
3862 plan->qual = otherclauses;
3863 plan->lefttree = lefttree;
3864 plan->righttree = righttree;
3865 node->join.jointype = jointype;
3866 node->join.joinqual = joinclauses;
3867 node->nestParams = nestParams;
3873 make_hashjoin(List *tlist,
3881 HashJoin *node = makeNode(HashJoin);
3882 Plan *plan = &node->join.plan;
3884 /* cost should be inserted by caller */
3885 plan->targetlist = tlist;
3886 plan->qual = otherclauses;
3887 plan->lefttree = lefttree;
3888 plan->righttree = righttree;
3889 node->hashclauses = hashclauses;
3890 node->join.jointype = jointype;
3891 node->join.joinqual = joinclauses;
3897 make_hash(Plan *lefttree,
3899 AttrNumber skewColumn,
3902 int32 skewColTypmod)
3904 Hash *node = makeNode(Hash);
3905 Plan *plan = &node->plan;
3907 copy_plan_costsize(plan, lefttree);
3910 * For plausibility, make startup & total costs equal total cost of input
3911 * plan; this only affects EXPLAIN display not decisions.
3913 plan->startup_cost = plan->total_cost;
3914 plan->targetlist = lefttree->targetlist;
3916 plan->lefttree = lefttree;
3917 plan->righttree = NULL;
3919 node->skewTable = skewTable;
3920 node->skewColumn = skewColumn;
3921 node->skewInherit = skewInherit;
3922 node->skewColType = skewColType;
3923 node->skewColTypmod = skewColTypmod;
3929 make_mergejoin(List *tlist,
3934 Oid *mergecollations,
3935 int *mergestrategies,
3936 bool *mergenullsfirst,
3941 MergeJoin *node = makeNode(MergeJoin);
3942 Plan *plan = &node->join.plan;
3944 /* cost should be inserted by caller */
3945 plan->targetlist = tlist;
3946 plan->qual = otherclauses;
3947 plan->lefttree = lefttree;
3948 plan->righttree = righttree;
3949 node->mergeclauses = mergeclauses;
3950 node->mergeFamilies = mergefamilies;
3951 node->mergeCollations = mergecollations;
3952 node->mergeStrategies = mergestrategies;
3953 node->mergeNullsFirst = mergenullsfirst;
3954 node->join.jointype = jointype;
3955 node->join.joinqual = joinclauses;
3961 * make_sort --- basic routine to build a Sort plan node
3963 * Caller must have built the sortColIdx, sortOperators, collations, and
3964 * nullsFirst arrays already.
3965 * limit_tuples is as for cost_sort (in particular, pass -1 if no limit)
3968 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3969 AttrNumber *sortColIdx, Oid *sortOperators,
3970 Oid *collations, bool *nullsFirst,
3971 double limit_tuples)
3973 Sort *node = makeNode(Sort);
3974 Plan *plan = &node->plan;
3975 Path sort_path; /* dummy for result of cost_sort */
3977 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3978 cost_sort(&sort_path, root, NIL,
3979 lefttree->total_cost,
3980 lefttree->plan_rows,
3981 lefttree->plan_width,
3985 plan->startup_cost = sort_path.startup_cost;
3986 plan->total_cost = sort_path.total_cost;
3987 plan->targetlist = lefttree->targetlist;
3989 plan->lefttree = lefttree;
3990 plan->righttree = NULL;
3991 node->numCols = numCols;
3992 node->sortColIdx = sortColIdx;
3993 node->sortOperators = sortOperators;
3994 node->collations = collations;
3995 node->nullsFirst = nullsFirst;
4001 * prepare_sort_from_pathkeys
4002 * Prepare to sort according to given pathkeys
4004 * This is used to set up for both Sort and MergeAppend nodes. It calculates
4005 * the executor's representation of the sort key information, and adjusts the
4006 * plan targetlist if needed to add resjunk sort columns.
4009 * 'lefttree' is the plan node which yields input tuples
4010 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
4011 * 'relids' identifies the child relation being sorted, if any
4012 * 'reqColIdx' is NULL or an array of required sort key column numbers
4013 * 'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
4015 * We must convert the pathkey information into arrays of sort key column
4016 * numbers, sort operator OIDs, collation OIDs, and nulls-first flags,
4017 * which is the representation the executor wants. These are returned into
4018 * the output parameters *p_numsortkeys etc.
4020 * When looking for matches to an EquivalenceClass's members, we will only
4021 * consider child EC members if they match 'relids'. This protects against
4022 * possible incorrect matches to child expressions that contain no Vars.
4024 * If reqColIdx isn't NULL then it contains sort key column numbers that
4025 * we should match. This is used when making child plans for a MergeAppend;
4026 * it's an error if we can't match the columns.
4028 * If the pathkeys include expressions that aren't simple Vars, we will
4029 * usually need to add resjunk items to the input plan's targetlist to
4030 * compute these expressions, since the Sort/MergeAppend node itself won't
4031 * do any such calculations. If the input plan type isn't one that can do
4032 * projections, this means adding a Result node just to do the projection.
4033 * However, the caller can pass adjust_tlist_in_place = TRUE to force the
4034 * lefttree tlist to be modified in-place regardless of whether the node type
4035 * can project --- we use this for fixing the tlist of MergeAppend itself.
4037 * Returns the node which is to be the input to the Sort (either lefttree,
4038 * or a Result stacked atop lefttree).
4041 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
4043 const AttrNumber *reqColIdx,
4044 bool adjust_tlist_in_place,
4046 AttrNumber **p_sortColIdx,
4047 Oid **p_sortOperators,
4049 bool **p_nullsFirst)
4051 List *tlist = lefttree->targetlist;
4054 AttrNumber *sortColIdx;
4060 * We will need at most list_length(pathkeys) sort columns; possibly less
4062 numsortkeys = list_length(pathkeys);
4063 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
4064 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
4065 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
4066 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
4070 foreach(i, pathkeys)
4072 PathKey *pathkey = (PathKey *) lfirst(i);
4073 EquivalenceClass *ec = pathkey->pk_eclass;
4074 EquivalenceMember *em;
4075 TargetEntry *tle = NULL;
4076 Oid pk_datatype = InvalidOid;
4080 if (ec->ec_has_volatile)
4083 * If the pathkey's EquivalenceClass is volatile, then it must
4084 * have come from an ORDER BY clause, and we have to match it to
4085 * that same targetlist entry.
4087 if (ec->ec_sortref == 0) /* can't happen */
4088 elog(ERROR, "volatile EquivalenceClass has no sortref");
4089 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
4091 Assert(list_length(ec->ec_members) == 1);
4092 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
4094 else if (reqColIdx != NULL)
4097 * If we are given a sort column number to match, only consider
4098 * the single TLE at that position. It's possible that there is
4099 * no such TLE, in which case fall through and generate a resjunk
4100 * targetentry (we assume this must have happened in the parent
4101 * plan as well). If there is a TLE but it doesn't match the
4102 * pathkey's EC, we do the same, which is probably the wrong thing
4103 * but we'll leave it to caller to complain about the mismatch.
4105 tle = get_tle_by_resno(tlist, reqColIdx[numsortkeys]);
4108 em = find_ec_member_for_expr(ec, tle->expr, relids);
4111 /* found expr at right place in tlist */
4112 pk_datatype = em->em_datatype;
4121 * Otherwise, we can sort by any non-constant expression listed in
4122 * the pathkey's EquivalenceClass. For now, we take the first
4123 * tlist item found in the EC. If there's no match, we'll generate
4124 * a resjunk entry using the first EC member that is an expression
4125 * in the input's vars. (The non-const restriction only matters
4126 * if the EC is below_outer_join; but if it isn't, it won't
4127 * contain consts anyway, else we'd have discarded the pathkey as
4130 * XXX if we have a choice, is there any way of figuring out which
4131 * might be cheapest to execute? (For example, int4lt is likely
4132 * much cheaper to execute than numericlt, but both might appear
4133 * in the same equivalence class...) Not clear that we ever will
4134 * have an interesting choice in practice, so it may not matter.
4138 tle = (TargetEntry *) lfirst(j);
4139 em = find_ec_member_for_expr(ec, tle->expr, relids);
4142 /* found expr already in tlist */
4143 pk_datatype = em->em_datatype;
4153 * No matching tlist item; look for a computable expression. Note
4154 * that we treat Aggrefs as if they were variables; this is
4155 * necessary when attempting to sort the output from an Agg node
4156 * for use in a WindowFunc (since grouping_planner will have
4157 * treated the Aggrefs as variables, too).
4159 Expr *sortexpr = NULL;
4161 foreach(j, ec->ec_members)
4163 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
4168 * We shouldn't be trying to sort by an equivalence class that
4169 * contains a constant, so no need to consider such cases any
4172 if (em->em_is_const)
4176 * Ignore child members unless they match the rel being
4179 if (em->em_is_child &&
4180 !bms_equal(em->em_relids, relids))
4183 sortexpr = em->em_expr;
4184 exprvars = pull_var_clause((Node *) sortexpr,
4185 PVC_INCLUDE_AGGREGATES,
4186 PVC_INCLUDE_PLACEHOLDERS);
4187 foreach(k, exprvars)
4189 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
4192 list_free(exprvars);
4195 pk_datatype = em->em_datatype;
4196 break; /* found usable expression */
4200 elog(ERROR, "could not find pathkey item to sort");
4203 * Do we need to insert a Result node?
4205 if (!adjust_tlist_in_place &&
4206 !is_projection_capable_plan(lefttree))
4208 /* copy needed so we don't modify input's tlist below */
4209 tlist = copyObject(tlist);
4210 lefttree = (Plan *) make_result(root, tlist, NULL,
4214 /* Don't bother testing is_projection_capable_plan again */
4215 adjust_tlist_in_place = true;
4218 * Add resjunk entry to input's tlist
4220 tle = makeTargetEntry(sortexpr,
4221 list_length(tlist) + 1,
4224 tlist = lappend(tlist, tle);
4225 lefttree->targetlist = tlist; /* just in case NIL before */
4229 * Look up the correct sort operator from the PathKey's slightly
4230 * abstracted representation.
4232 sortop = get_opfamily_member(pathkey->pk_opfamily,
4235 pathkey->pk_strategy);
4236 if (!OidIsValid(sortop)) /* should not happen */
4237 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
4238 pathkey->pk_strategy, pk_datatype, pk_datatype,
4239 pathkey->pk_opfamily);
4241 /* Add the column to the sort arrays */
4242 sortColIdx[numsortkeys] = tle->resno;
4243 sortOperators[numsortkeys] = sortop;
4244 collations[numsortkeys] = ec->ec_collation;
4245 nullsFirst[numsortkeys] = pathkey->pk_nulls_first;
4249 /* Return results */
4250 *p_numsortkeys = numsortkeys;
4251 *p_sortColIdx = sortColIdx;
4252 *p_sortOperators = sortOperators;
4253 *p_collations = collations;
4254 *p_nullsFirst = nullsFirst;
4260 * find_ec_member_for_expr
4261 * Locate an EquivalenceClass member matching the given expression, if any
4263 * Child EC members are ignored unless they match 'relids'.
4265 static EquivalenceMember *
4266 find_ec_member_for_expr(EquivalenceClass *ec,
4272 /* We ignore binary-compatible relabeling on both ends */
4273 while (expr && IsA(expr, RelabelType))
4274 expr = ((RelabelType *) expr)->arg;
4276 foreach(lc, ec->ec_members)
4278 EquivalenceMember *em = (EquivalenceMember *) lfirst(lc);
4282 * We shouldn't be trying to sort by an equivalence class that
4283 * contains a constant, so no need to consider such cases any further.
4285 if (em->em_is_const)
4289 * Ignore child members unless they match the rel being sorted.
4291 if (em->em_is_child &&
4292 !bms_equal(em->em_relids, relids))
4295 /* Match if same expression (after stripping relabel) */
4296 emexpr = em->em_expr;
4297 while (emexpr && IsA(emexpr, RelabelType))
4298 emexpr = ((RelabelType *) emexpr)->arg;
4300 if (equal(emexpr, expr))
4308 * make_sort_from_pathkeys
4309 * Create sort plan to sort according to given pathkeys
4311 * 'lefttree' is the node which yields input tuples
4312 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
4313 * 'limit_tuples' is the bound on the number of output tuples;
4317 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
4318 double limit_tuples)
4321 AttrNumber *sortColIdx;
4326 /* Compute sort column info, and adjust lefttree as needed */
4327 lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
4337 /* Now build the Sort node */
4338 return make_sort(root, lefttree, numsortkeys,
4339 sortColIdx, sortOperators, collations,
4340 nullsFirst, limit_tuples);
4344 * make_sort_from_sortclauses
4345 * Create sort plan to sort according to given sortclauses
4347 * 'sortcls' is a list of SortGroupClauses
4348 * 'lefttree' is the node which yields input tuples
4351 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
4353 List *sub_tlist = lefttree->targetlist;
4356 AttrNumber *sortColIdx;
4361 /* Convert list-ish representation to arrays wanted by executor */
4362 numsortkeys = list_length(sortcls);
4363 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
4364 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
4365 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
4366 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
4371 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
4372 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
4374 sortColIdx[numsortkeys] = tle->resno;
4375 sortOperators[numsortkeys] = sortcl->sortop;
4376 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4377 nullsFirst[numsortkeys] = sortcl->nulls_first;
4381 return make_sort(root, lefttree, numsortkeys,
4382 sortColIdx, sortOperators, collations,
4387 * make_sort_from_groupcols
4388 * Create sort plan to sort based on grouping columns
4390 * 'groupcls' is the list of SortGroupClauses
4391 * 'grpColIdx' gives the column numbers to use
4393 * This might look like it could be merged with make_sort_from_sortclauses,
4394 * but presently we *must* use the grpColIdx[] array to locate sort columns,
4395 * because the child plan's tlist is not marked with ressortgroupref info
4396 * appropriate to the grouping node. So, only the sort ordering info
4397 * is used from the SortGroupClause entries.
4400 make_sort_from_groupcols(PlannerInfo *root,
4402 AttrNumber *grpColIdx,
4405 List *sub_tlist = lefttree->targetlist;
4408 AttrNumber *sortColIdx;
4413 /* Convert list-ish representation to arrays wanted by executor */
4414 numsortkeys = list_length(groupcls);
4415 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
4416 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
4417 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
4418 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
4421 foreach(l, groupcls)
4423 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
4424 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[numsortkeys]);
4427 elog(ERROR, "could not retrive tle for sort-from-groupcols");
4429 sortColIdx[numsortkeys] = tle->resno;
4430 sortOperators[numsortkeys] = grpcl->sortop;
4431 collations[numsortkeys] = exprCollation((Node *) tle->expr);
4432 nullsFirst[numsortkeys] = grpcl->nulls_first;
4436 return make_sort(root, lefttree, numsortkeys,
4437 sortColIdx, sortOperators, collations,
4442 make_material(Plan *lefttree)
4444 Material *node = makeNode(Material);
4445 Plan *plan = &node->plan;
4447 /* cost should be inserted by caller */
4448 plan->targetlist = lefttree->targetlist;
4450 plan->lefttree = lefttree;
4451 plan->righttree = NULL;
4457 * materialize_finished_plan: stick a Material node atop a completed plan
4459 * There are a couple of places where we want to attach a Material node
4460 * after completion of subquery_planner(). This currently requires hackery.
4461 * Since subquery_planner has already run SS_finalize_plan on the subplan
4462 * tree, we have to kluge up parameter lists for the Material node.
4463 * Possibly this could be fixed by postponing SS_finalize_plan processing
4464 * until setrefs.c is run?
4467 materialize_finished_plan(Plan *subplan)
4470 Path matpath; /* dummy for result of cost_material */
4472 matplan = (Plan *) make_material(subplan);
4475 cost_material(&matpath,
4476 subplan->startup_cost,
4477 subplan->total_cost,
4479 subplan->plan_width);
4480 matplan->startup_cost = matpath.startup_cost;
4481 matplan->total_cost = matpath.total_cost;
4482 matplan->plan_rows = subplan->plan_rows;
4483 matplan->plan_width = subplan->plan_width;
4485 /* parameter kluge --- see comments above */
4486 matplan->extParam = bms_copy(subplan->extParam);
4487 matplan->allParam = bms_copy(subplan->allParam);
4493 make_agg(PlannerInfo *root, List *tlist, List *qual,
4494 AggStrategy aggstrategy, const AggClauseCosts *aggcosts,
4495 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
4500 Agg *node = makeNode(Agg);
4501 Plan *plan = &node->plan;
4502 Path agg_path; /* dummy for result of cost_agg */
4505 node->aggstrategy = aggstrategy;
4506 node->numCols = numGroupCols;
4507 node->grpColIdx = grpColIdx;
4508 node->grpOperators = grpOperators;
4509 node->numGroups = numGroups;
4511 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4512 cost_agg(&agg_path, root,
4513 aggstrategy, aggcosts,
4514 numGroupCols, numGroups,
4515 lefttree->startup_cost,
4516 lefttree->total_cost,
4517 lefttree->plan_rows);
4518 plan->startup_cost = agg_path.startup_cost;
4519 plan->total_cost = agg_path.total_cost;
4522 * We will produce a single output tuple if not grouping, and a tuple per
4525 if (aggstrategy == AGG_PLAIN)
4526 plan->plan_rows = groupingSets ? list_length(groupingSets) : 1;
4528 plan->plan_rows = numGroups;
4530 node->groupingSets = groupingSets;
4533 * We also need to account for the cost of evaluation of the qual (ie, the
4534 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
4535 * anything for Aggref nodes; this is okay since they are really
4536 * comparable to Vars.
4538 * See notes in add_tlist_costs_to_plan about why only make_agg,
4539 * make_windowagg and make_group worry about tlist eval cost.
4543 cost_qual_eval(&qual_cost, qual, root);
4544 plan->startup_cost += qual_cost.startup;
4545 plan->total_cost += qual_cost.startup;
4546 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4548 add_tlist_costs_to_plan(root, plan, tlist);
4551 plan->targetlist = tlist;
4553 plan->lefttree = lefttree;
4554 plan->righttree = NULL;
4560 make_windowagg(PlannerInfo *root, List *tlist,
4561 List *windowFuncs, Index winref,
4562 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
4563 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
4564 int frameOptions, Node *startOffset, Node *endOffset,
4567 WindowAgg *node = makeNode(WindowAgg);
4568 Plan *plan = &node->plan;
4569 Path windowagg_path; /* dummy for result of cost_windowagg */
4571 node->winref = winref;
4572 node->partNumCols = partNumCols;
4573 node->partColIdx = partColIdx;
4574 node->partOperators = partOperators;
4575 node->ordNumCols = ordNumCols;
4576 node->ordColIdx = ordColIdx;
4577 node->ordOperators = ordOperators;
4578 node->frameOptions = frameOptions;
4579 node->startOffset = startOffset;
4580 node->endOffset = endOffset;
4582 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4583 cost_windowagg(&windowagg_path, root,
4584 windowFuncs, partNumCols, ordNumCols,
4585 lefttree->startup_cost,
4586 lefttree->total_cost,
4587 lefttree->plan_rows);
4588 plan->startup_cost = windowagg_path.startup_cost;
4589 plan->total_cost = windowagg_path.total_cost;
4592 * We also need to account for the cost of evaluation of the tlist.
4594 * See notes in add_tlist_costs_to_plan about why only make_agg,
4595 * make_windowagg and make_group worry about tlist eval cost.
4597 add_tlist_costs_to_plan(root, plan, tlist);
4599 plan->targetlist = tlist;
4600 plan->lefttree = lefttree;
4601 plan->righttree = NULL;
4602 /* WindowAgg nodes never have a qual clause */
4609 make_group(PlannerInfo *root,
4613 AttrNumber *grpColIdx,
4618 Group *node = makeNode(Group);
4619 Plan *plan = &node->plan;
4620 Path group_path; /* dummy for result of cost_group */
4623 node->numCols = numGroupCols;
4624 node->grpColIdx = grpColIdx;
4625 node->grpOperators = grpOperators;
4627 copy_plan_costsize(plan, lefttree); /* only care about copying size */
4628 cost_group(&group_path, root,
4629 numGroupCols, numGroups,
4630 lefttree->startup_cost,
4631 lefttree->total_cost,
4632 lefttree->plan_rows);
4633 plan->startup_cost = group_path.startup_cost;
4634 plan->total_cost = group_path.total_cost;
4636 /* One output tuple per estimated result group */
4637 plan->plan_rows = numGroups;
4640 * We also need to account for the cost of evaluation of the qual (ie, the
4641 * HAVING clause) and the tlist.
4643 * XXX this double-counts the cost of evaluation of any expressions used
4644 * for grouping, since in reality those will have been evaluated at a
4645 * lower plan level and will only be copied by the Group node. Worth
4648 * See notes in add_tlist_costs_to_plan about why only make_agg,
4649 * make_windowagg and make_group worry about tlist eval cost.
4653 cost_qual_eval(&qual_cost, qual, root);
4654 plan->startup_cost += qual_cost.startup;
4655 plan->total_cost += qual_cost.startup;
4656 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
4658 add_tlist_costs_to_plan(root, plan, tlist);
4661 plan->targetlist = tlist;
4662 plan->lefttree = lefttree;
4663 plan->righttree = NULL;
4669 * distinctList is a list of SortGroupClauses, identifying the targetlist items
4670 * that should be considered by the Unique filter. The input path must
4671 * already be sorted accordingly.
4674 make_unique(Plan *lefttree, List *distinctList)
4676 Unique *node = makeNode(Unique);
4677 Plan *plan = &node->plan;
4678 int numCols = list_length(distinctList);
4680 AttrNumber *uniqColIdx;
4684 copy_plan_costsize(plan, lefttree);
4687 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4688 * all columns get compared at most of the tuples. (XXX probably this is
4691 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
4694 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
4695 * we assume the filter removes nothing. The caller must alter this if he
4696 * has a better idea.
4699 plan->targetlist = lefttree->targetlist;
4701 plan->lefttree = lefttree;
4702 plan->righttree = NULL;
4705 * convert SortGroupClause list into arrays of attr indexes and equality
4706 * operators, as wanted by executor
4708 Assert(numCols > 0);
4709 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4710 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4712 foreach(slitem, distinctList)
4714 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4715 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4717 uniqColIdx[keyno] = tle->resno;
4718 uniqOperators[keyno] = sortcl->eqop;
4719 Assert(OidIsValid(uniqOperators[keyno]));
4723 node->numCols = numCols;
4724 node->uniqColIdx = uniqColIdx;
4725 node->uniqOperators = uniqOperators;
4731 * distinctList is a list of SortGroupClauses, identifying the targetlist
4732 * items that should be considered by the SetOp filter. The input path must
4733 * already be sorted accordingly.
4736 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
4737 List *distinctList, AttrNumber flagColIdx, int firstFlag,
4738 long numGroups, double outputRows)
4740 SetOp *node = makeNode(SetOp);
4741 Plan *plan = &node->plan;
4742 int numCols = list_length(distinctList);
4744 AttrNumber *dupColIdx;
4748 copy_plan_costsize(plan, lefttree);
4749 plan->plan_rows = outputRows;
4752 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4753 * all columns get compared at most of the tuples.
4755 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4757 plan->targetlist = lefttree->targetlist;
4759 plan->lefttree = lefttree;
4760 plan->righttree = NULL;
4763 * convert SortGroupClause list into arrays of attr indexes and equality
4764 * operators, as wanted by executor
4766 Assert(numCols > 0);
4767 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4768 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4770 foreach(slitem, distinctList)
4772 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4773 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4775 dupColIdx[keyno] = tle->resno;
4776 dupOperators[keyno] = sortcl->eqop;
4777 Assert(OidIsValid(dupOperators[keyno]));
4782 node->strategy = strategy;
4783 node->numCols = numCols;
4784 node->dupColIdx = dupColIdx;
4785 node->dupOperators = dupOperators;
4786 node->flagColIdx = flagColIdx;
4787 node->firstFlag = firstFlag;
4788 node->numGroups = numGroups;
4795 * Build a LockRows plan node
4798 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4800 LockRows *node = makeNode(LockRows);
4801 Plan *plan = &node->plan;
4803 copy_plan_costsize(plan, lefttree);
4805 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4806 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4808 plan->targetlist = lefttree->targetlist;
4810 plan->lefttree = lefttree;
4811 plan->righttree = NULL;
4813 node->rowMarks = rowMarks;
4814 node->epqParam = epqParam;
4820 * Note: offset_est and count_est are passed in to save having to repeat
4821 * work already done to estimate the values of the limitOffset and limitCount
4822 * expressions. Their values are as returned by preprocess_limit (0 means
4823 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
4824 * with that function!
4827 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4828 int64 offset_est, int64 count_est)
4830 Limit *node = makeNode(Limit);
4831 Plan *plan = &node->plan;
4833 copy_plan_costsize(plan, lefttree);
4836 * Adjust the output rows count and costs according to the offset/limit.
4837 * This is only a cosmetic issue if we are at top level, but if we are
4838 * building a subquery then it's important to report correct info to the
4841 * When the offset or count couldn't be estimated, use 10% of the
4842 * estimated number of rows emitted from the subplan.
4844 if (offset_est != 0)
4849 offset_rows = (double) offset_est;
4851 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4852 if (offset_rows > plan->plan_rows)
4853 offset_rows = plan->plan_rows;
4854 if (plan->plan_rows > 0)
4855 plan->startup_cost +=
4856 (plan->total_cost - plan->startup_cost)
4857 * offset_rows / plan->plan_rows;
4858 plan->plan_rows -= offset_rows;
4859 if (plan->plan_rows < 1)
4860 plan->plan_rows = 1;
4868 count_rows = (double) count_est;
4870 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4871 if (count_rows > plan->plan_rows)
4872 count_rows = plan->plan_rows;
4873 if (plan->plan_rows > 0)
4874 plan->total_cost = plan->startup_cost +
4875 (plan->total_cost - plan->startup_cost)
4876 * count_rows / plan->plan_rows;
4877 plan->plan_rows = count_rows;
4878 if (plan->plan_rows < 1)
4879 plan->plan_rows = 1;
4882 plan->targetlist = lefttree->targetlist;
4884 plan->lefttree = lefttree;
4885 plan->righttree = NULL;
4887 node->limitOffset = limitOffset;
4888 node->limitCount = limitCount;
4895 * Build a Result plan node
4897 * If we have a subplan, assume that any evaluation costs for the gating qual
4898 * were already factored into the subplan's startup cost, and just copy the
4899 * subplan cost. If there's no subplan, we should include the qual eval
4900 * cost. In either case, tlist eval cost is not to be included here.
4903 make_result(PlannerInfo *root,
4905 Node *resconstantqual,
4908 Result *node = makeNode(Result);
4909 Plan *plan = &node->plan;
4912 copy_plan_costsize(plan, subplan);
4915 plan->startup_cost = 0;
4916 plan->total_cost = cpu_tuple_cost;
4917 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
4918 plan->plan_width = 0; /* XXX is it worth being smarter? */
4919 if (resconstantqual)
4923 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
4924 /* resconstantqual is evaluated once at startup */
4925 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
4926 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
4930 plan->targetlist = tlist;
4932 plan->lefttree = subplan;
4933 plan->righttree = NULL;
4934 node->resconstantqual = resconstantqual;
4941 * Build a ModifyTable plan node
4943 * Currently, we don't charge anything extra for the actual table modification
4944 * work, nor for the WITH CHECK OPTIONS or RETURNING expressions if any. It
4945 * would only be window dressing, since these are always top-level nodes and
4946 * there is no way for the costs to change any higher-level planning choices.
4947 * But we might want to make it look better sometime.
4950 make_modifytable(PlannerInfo *root,
4951 CmdType operation, bool canSetTag,
4952 Index nominalRelation,
4953 List *resultRelations, List *subplans,
4954 List *withCheckOptionLists, List *returningLists,
4955 List *rowMarks, OnConflictExpr *onconflict, int epqParam)
4957 ModifyTable *node = makeNode(ModifyTable);
4958 Plan *plan = &node->plan;
4960 List *fdw_private_list;
4965 Assert(list_length(resultRelations) == list_length(subplans));
4966 Assert(withCheckOptionLists == NIL ||
4967 list_length(resultRelations) == list_length(withCheckOptionLists));
4968 Assert(returningLists == NIL ||
4969 list_length(resultRelations) == list_length(returningLists));
4972 * Compute cost as sum of subplan costs.
4974 plan->startup_cost = 0;
4975 plan->total_cost = 0;
4976 plan->plan_rows = 0;
4978 foreach(subnode, subplans)
4980 Plan *subplan = (Plan *) lfirst(subnode);
4982 if (subnode == list_head(subplans)) /* first node? */
4983 plan->startup_cost = subplan->startup_cost;
4984 plan->total_cost += subplan->total_cost;
4985 plan->plan_rows += subplan->plan_rows;
4986 total_size += subplan->plan_width * subplan->plan_rows;
4988 if (plan->plan_rows > 0)
4989 plan->plan_width = rint(total_size / plan->plan_rows);
4991 plan->plan_width = 0;
4993 node->plan.lefttree = NULL;
4994 node->plan.righttree = NULL;
4995 node->plan.qual = NIL;
4996 /* setrefs.c will fill in the targetlist, if needed */
4997 node->plan.targetlist = NIL;
4999 node->operation = operation;
5000 node->canSetTag = canSetTag;
5001 node->nominalRelation = nominalRelation;
5002 node->resultRelations = resultRelations;
5003 node->resultRelIndex = -1; /* will be set correctly in setrefs.c */
5004 node->plans = subplans;
5007 node->onConflictAction = ONCONFLICT_NONE;
5008 node->onConflictSet = NIL;
5009 node->onConflictWhere = NULL;
5010 node->arbiterIndexes = NIL;
5014 node->onConflictAction = onconflict->action;
5015 node->onConflictSet = onconflict->onConflictSet;
5016 node->onConflictWhere = onconflict->onConflictWhere;
5019 * If a set of unique index inference elements was provided (an
5020 * INSERT...ON CONFLICT "inference specification"), then infer
5021 * appropriate unique indexes (or throw an error if none are
5024 node->arbiterIndexes = infer_arbiter_indexes(root);
5026 node->exclRelRTI = onconflict->exclRelIndex;
5027 node->exclRelTlist = onconflict->exclRelTlist;
5029 node->withCheckOptionLists = withCheckOptionLists;
5030 node->returningLists = returningLists;
5031 node->rowMarks = rowMarks;
5032 node->epqParam = epqParam;
5035 * For each result relation that is a foreign table, allow the FDW to
5036 * construct private plan data, and accumulate it all into a list.
5038 fdw_private_list = NIL;
5040 foreach(lc, resultRelations)
5042 Index rti = lfirst_int(lc);
5043 FdwRoutine *fdwroutine;
5047 * If possible, we want to get the FdwRoutine from our RelOptInfo for
5048 * the table. But sometimes we don't have a RelOptInfo and must get
5049 * it the hard way. (In INSERT, the target relation is not scanned,
5050 * so it's not a baserel; and there are also corner cases for
5051 * updatable views where the target rel isn't a baserel.)
5053 if (rti < root->simple_rel_array_size &&
5054 root->simple_rel_array[rti] != NULL)
5056 RelOptInfo *resultRel = root->simple_rel_array[rti];
5058 fdwroutine = resultRel->fdwroutine;
5062 RangeTblEntry *rte = planner_rt_fetch(rti, root);
5064 Assert(rte->rtekind == RTE_RELATION);
5065 if (rte->relkind == RELKIND_FOREIGN_TABLE)
5066 fdwroutine = GetFdwRoutineByRelId(rte->relid);
5071 if (fdwroutine != NULL &&
5072 fdwroutine->PlanForeignModify != NULL)
5073 fdw_private = fdwroutine->PlanForeignModify(root, node, rti, i);
5076 fdw_private_list = lappend(fdw_private_list, fdw_private);
5079 node->fdwPrivLists = fdw_private_list;
5085 * is_projection_capable_plan
5086 * Check whether a given Plan node is able to do projection.
5089 is_projection_capable_plan(Plan *plan)
5091 /* Most plan types can project, so just list the ones that can't */
5092 switch (nodeTag(plan))
5104 case T_RecursiveUnion: