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-2011, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * src/backend/optimizer/plan/createplan.c
15 *-------------------------------------------------------------------------
22 #include "access/skey.h"
23 #include "miscadmin.h"
24 #include "nodes/makefuncs.h"
25 #include "nodes/nodeFuncs.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/cost.h"
28 #include "optimizer/paths.h"
29 #include "optimizer/plancat.h"
30 #include "optimizer/planmain.h"
31 #include "optimizer/predtest.h"
32 #include "optimizer/restrictinfo.h"
33 #include "optimizer/subselect.h"
34 #include "optimizer/tlist.h"
35 #include "optimizer/var.h"
36 #include "parser/parse_clause.h"
37 #include "parser/parsetree.h"
38 #include "utils/lsyscache.h"
41 static Plan *create_plan_recurse(PlannerInfo *root, Path *best_path);
42 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
43 static List *build_relation_tlist(RelOptInfo *rel);
44 static bool use_physical_tlist(PlannerInfo *root, RelOptInfo *rel);
45 static void disuse_physical_tlist(Plan *plan, Path *path);
46 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
47 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
48 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
49 static Plan *create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path);
50 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
51 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
52 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
53 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
54 List *tlist, List *scan_clauses);
55 static IndexScan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
56 List *tlist, List *scan_clauses);
57 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
58 BitmapHeapPath *best_path,
59 List *tlist, List *scan_clauses);
60 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
61 List **qual, List **indexqual);
62 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
63 List *tlist, List *scan_clauses);
64 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
65 List *tlist, List *scan_clauses);
66 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
67 List *tlist, List *scan_clauses);
68 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
69 List *tlist, List *scan_clauses);
70 static CteScan *create_ctescan_plan(PlannerInfo *root, Path *best_path,
71 List *tlist, List *scan_clauses);
72 static WorkTableScan *create_worktablescan_plan(PlannerInfo *root, Path *best_path,
73 List *tlist, List *scan_clauses);
74 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
75 Plan *outer_plan, Plan *inner_plan);
76 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
77 Plan *outer_plan, Plan *inner_plan);
78 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
79 Plan *outer_plan, Plan *inner_plan);
80 static Node *replace_nestloop_params(PlannerInfo *root, Node *expr);
81 static Node *replace_nestloop_params_mutator(Node *node, PlannerInfo *root);
82 static List *fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
84 static List *fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path,
86 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index);
87 static List *get_switched_clauses(List *clauses, Relids outerrelids);
88 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
89 static void copy_path_costsize(Plan *dest, Path *src);
90 static void copy_plan_costsize(Plan *dest, Plan *src);
91 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
92 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
93 Oid indexid, List *indexqual, List *indexqualorig,
94 List *indexorderby, List *indexorderbyorig,
95 ScanDirection indexscandir);
96 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
99 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
102 List *bitmapqualorig,
104 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
106 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
107 Index scanrelid, Node *funcexpr, List *funccolnames,
108 List *funccoltypes, List *funccoltypmods, List *funccolcollations);
109 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
110 Index scanrelid, List *values_lists);
111 static CteScan *make_ctescan(List *qptlist, List *qpqual,
112 Index scanrelid, int ctePlanId, int cteParam);
113 static WorkTableScan *make_worktablescan(List *qptlist, List *qpqual,
114 Index scanrelid, int wtParam);
115 static BitmapAnd *make_bitmap_and(List *bitmapplans);
116 static BitmapOr *make_bitmap_or(List *bitmapplans);
117 static NestLoop *make_nestloop(List *tlist,
118 List *joinclauses, List *otherclauses, List *nestParams,
119 Plan *lefttree, Plan *righttree,
121 static HashJoin *make_hashjoin(List *tlist,
122 List *joinclauses, List *otherclauses,
124 Plan *lefttree, Plan *righttree,
126 static Hash *make_hash(Plan *lefttree,
128 AttrNumber skewColumn,
131 int32 skewColTypmod);
132 static MergeJoin *make_mergejoin(List *tlist,
133 List *joinclauses, List *otherclauses,
136 Oid *mergecollations,
137 int *mergestrategies,
138 bool *mergenullsfirst,
139 Plan *lefttree, Plan *righttree,
141 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
142 AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst,
143 double limit_tuples);
144 static Plan *prepare_sort_from_pathkeys(PlannerInfo *root,
145 Plan *lefttree, List *pathkeys,
146 bool adjust_tlist_in_place,
148 AttrNumber **p_sortColIdx,
149 Oid **p_sortOperators,
151 bool **p_nullsFirst);
152 static Material *make_material(Plan *lefttree);
157 * Creates the access plan for a query by recursively processing the
158 * desired tree of pathnodes, starting at the node 'best_path'. For
159 * every pathnode found, we create a corresponding plan node containing
160 * appropriate id, target list, and qualification information.
162 * The tlists and quals in the plan tree are still in planner format,
163 * ie, Vars still correspond to the parser's numbering. This will be
164 * fixed later by setrefs.c.
166 * best_path is the best access path
168 * Returns a Plan tree.
171 create_plan(PlannerInfo *root, Path *best_path)
175 /* Initialize this module's private workspace in PlannerInfo */
176 root->curOuterRels = NULL;
177 root->curOuterParams = NIL;
179 /* Recursively process the path tree */
180 plan = create_plan_recurse(root, best_path);
182 /* Check we successfully assigned all NestLoopParams to plan nodes */
183 if (root->curOuterParams != NIL)
184 elog(ERROR, "failed to assign all NestLoopParams to plan nodes");
190 * create_plan_recurse
191 * Recursive guts of create_plan().
194 create_plan_recurse(PlannerInfo *root, Path *best_path)
198 switch (best_path->pathtype)
202 case T_BitmapHeapScan:
208 case T_WorkTableScan:
209 plan = create_scan_plan(root, best_path);
214 plan = create_join_plan(root,
215 (JoinPath *) best_path);
218 plan = create_append_plan(root,
219 (AppendPath *) best_path);
222 plan = create_merge_append_plan(root,
223 (MergeAppendPath *) best_path);
226 plan = (Plan *) create_result_plan(root,
227 (ResultPath *) best_path);
230 plan = (Plan *) create_material_plan(root,
231 (MaterialPath *) best_path);
234 plan = create_unique_plan(root,
235 (UniquePath *) best_path);
238 elog(ERROR, "unrecognized node type: %d",
239 (int) best_path->pathtype);
240 plan = NULL; /* keep compiler quiet */
249 * Create a scan plan for the parent relation of 'best_path'.
252 create_scan_plan(PlannerInfo *root, Path *best_path)
254 RelOptInfo *rel = best_path->parent;
260 * For table scans, rather than using the relation targetlist (which is
261 * only those Vars actually needed by the query), we prefer to generate a
262 * tlist containing all Vars in order. This will allow the executor to
263 * optimize away projection of the table tuples, if possible. (Note that
264 * planner.c may replace the tlist we generate here, forcing projection to
267 if (use_physical_tlist(root, rel))
269 tlist = build_physical_tlist(root, rel);
270 /* if fail because of dropped cols, use regular method */
272 tlist = build_relation_tlist(rel);
275 tlist = build_relation_tlist(rel);
278 * Extract the relevant restriction clauses from the parent relation. The
279 * executor must apply all these restrictions during the scan, except for
280 * pseudoconstants which we'll take care of below.
282 scan_clauses = rel->baserestrictinfo;
284 switch (best_path->pathtype)
287 plan = (Plan *) create_seqscan_plan(root,
294 plan = (Plan *) create_indexscan_plan(root,
295 (IndexPath *) best_path,
300 case T_BitmapHeapScan:
301 plan = (Plan *) create_bitmap_scan_plan(root,
302 (BitmapHeapPath *) best_path,
308 plan = (Plan *) create_tidscan_plan(root,
309 (TidPath *) best_path,
315 plan = (Plan *) create_subqueryscan_plan(root,
322 plan = (Plan *) create_functionscan_plan(root,
329 plan = (Plan *) create_valuesscan_plan(root,
336 plan = (Plan *) create_ctescan_plan(root,
342 case T_WorkTableScan:
343 plan = (Plan *) create_worktablescan_plan(root,
350 elog(ERROR, "unrecognized node type: %d",
351 (int) best_path->pathtype);
352 plan = NULL; /* keep compiler quiet */
357 * If there are any pseudoconstant clauses attached to this node, insert a
358 * gating Result node that evaluates the pseudoconstants as one-time
361 if (root->hasPseudoConstantQuals)
362 plan = create_gating_plan(root, plan, scan_clauses);
368 * Build a target list (ie, a list of TargetEntry) for a relation.
371 build_relation_tlist(RelOptInfo *rel)
377 foreach(v, rel->reltargetlist)
379 /* Do we really need to copy here? Not sure */
380 Node *node = (Node *) copyObject(lfirst(v));
382 tlist = lappend(tlist, makeTargetEntry((Expr *) node,
393 * Decide whether to use a tlist matching relation structure,
394 * rather than only those Vars actually referenced.
397 use_physical_tlist(PlannerInfo *root, RelOptInfo *rel)
403 * We can do this for real relation scans, subquery scans, function scans,
404 * values scans, and CTE scans (but not for, eg, joins).
406 if (rel->rtekind != RTE_RELATION &&
407 rel->rtekind != RTE_SUBQUERY &&
408 rel->rtekind != RTE_FUNCTION &&
409 rel->rtekind != RTE_VALUES &&
410 rel->rtekind != RTE_CTE)
414 * Can't do it with inheritance cases either (mainly because Append
417 if (rel->reloptkind != RELOPT_BASEREL)
421 * Can't do it if any system columns or whole-row Vars are requested.
422 * (This could possibly be fixed but would take some fragile assumptions
423 * in setrefs.c, I think.)
425 for (i = rel->min_attr; i <= 0; i++)
427 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
432 * Can't do it if the rel is required to emit any placeholder expressions,
435 foreach(lc, root->placeholder_list)
437 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(lc);
439 if (bms_nonempty_difference(phinfo->ph_needed, rel->relids) &&
440 bms_is_subset(phinfo->ph_eval_at, rel->relids))
448 * disuse_physical_tlist
449 * Switch a plan node back to emitting only Vars actually referenced.
451 * If the plan node immediately above a scan would prefer to get only
452 * needed Vars and not a physical tlist, it must call this routine to
453 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
454 * and Material nodes want this, so they don't have to store useless columns.
457 disuse_physical_tlist(Plan *plan, Path *path)
459 /* Only need to undo it for path types handled by create_scan_plan() */
460 switch (path->pathtype)
464 case T_BitmapHeapScan:
470 case T_WorkTableScan:
471 plan->targetlist = build_relation_tlist(path->parent);
480 * Deal with pseudoconstant qual clauses
482 * If the node's quals list includes any pseudoconstant quals, put them
483 * into a gating Result node atop the already-built plan. Otherwise,
484 * return the plan as-is.
486 * Note that we don't change cost or size estimates when doing gating.
487 * The costs of qual eval were already folded into the plan's startup cost.
488 * Leaving the size alone amounts to assuming that the gating qual will
489 * succeed, which is the conservative estimate for planning upper queries.
490 * We certainly don't want to assume the output size is zero (unless the
491 * gating qual is actually constant FALSE, and that case is dealt with in
492 * clausesel.c). Interpolating between the two cases is silly, because
493 * it doesn't reflect what will really happen at runtime, and besides which
494 * in most cases we have only a very bad idea of the probability of the gating
498 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
500 List *pseudoconstants;
502 /* Sort into desirable execution order while still in RestrictInfo form */
503 quals = order_qual_clauses(root, quals);
505 /* Pull out any pseudoconstant quals from the RestrictInfo list */
506 pseudoconstants = extract_actual_clauses(quals, true);
508 if (!pseudoconstants)
511 return (Plan *) make_result(root,
513 (Node *) pseudoconstants,
519 * Create a join plan for 'best_path' and (recursively) plans for its
520 * inner and outer paths.
523 create_join_plan(PlannerInfo *root, JoinPath *best_path)
528 Relids saveOuterRels = root->curOuterRels;
530 outer_plan = create_plan_recurse(root, best_path->outerjoinpath);
532 /* For a nestloop, include outer relids in curOuterRels for inner side */
533 if (best_path->path.pathtype == T_NestLoop)
534 root->curOuterRels = bms_union(root->curOuterRels,
535 best_path->outerjoinpath->parent->relids);
537 inner_plan = create_plan_recurse(root, best_path->innerjoinpath);
539 switch (best_path->path.pathtype)
542 plan = (Plan *) create_mergejoin_plan(root,
543 (MergePath *) best_path,
548 plan = (Plan *) create_hashjoin_plan(root,
549 (HashPath *) best_path,
554 /* Restore curOuterRels */
555 bms_free(root->curOuterRels);
556 root->curOuterRels = saveOuterRels;
558 plan = (Plan *) create_nestloop_plan(root,
559 (NestPath *) best_path,
564 elog(ERROR, "unrecognized node type: %d",
565 (int) best_path->path.pathtype);
566 plan = NULL; /* keep compiler quiet */
571 * If there are any pseudoconstant clauses attached to this node, insert a
572 * gating Result node that evaluates the pseudoconstants as one-time
575 if (root->hasPseudoConstantQuals)
576 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
581 * * Expensive function pullups may have pulled local predicates * into
582 * this path node. Put them in the qpqual of the plan node. * JMH,
585 if (get_loc_restrictinfo(best_path) != NIL)
586 set_qpqual((Plan) plan,
587 list_concat(get_qpqual((Plan) plan),
588 get_actual_clauses(get_loc_restrictinfo(best_path))));
596 * Create an Append plan for 'best_path' and (recursively) plans
599 * Returns a Plan node.
602 create_append_plan(PlannerInfo *root, AppendPath *best_path)
605 List *tlist = build_relation_tlist(best_path->path.parent);
606 List *subplans = NIL;
610 * It is possible for the subplans list to contain only one entry, or even
611 * no entries. Handle these cases specially.
613 * XXX ideally, if there's just one entry, we'd not bother to generate an
614 * Append node but just return the single child. At the moment this does
615 * not work because the varno of the child scan plan won't match the
616 * parent-rel Vars it'll be asked to emit.
618 if (best_path->subpaths == NIL)
620 /* Generate a Result plan with constant-FALSE gating qual */
621 return (Plan *) make_result(root,
623 (Node *) list_make1(makeBoolConst(false,
628 /* Normal case with multiple subpaths */
629 foreach(subpaths, best_path->subpaths)
631 Path *subpath = (Path *) lfirst(subpaths);
633 subplans = lappend(subplans, create_plan_recurse(root, subpath));
636 plan = make_append(subplans, tlist);
638 return (Plan *) plan;
642 * create_merge_append_plan
643 * Create a MergeAppend plan for 'best_path' and (recursively) plans
646 * Returns a Plan node.
649 create_merge_append_plan(PlannerInfo *root, MergeAppendPath *best_path)
651 MergeAppend *node = makeNode(MergeAppend);
652 Plan *plan = &node->plan;
653 List *tlist = build_relation_tlist(best_path->path.parent);
654 List *pathkeys = best_path->path.pathkeys;
655 List *subplans = NIL;
659 * We don't have the actual creation of the MergeAppend node split out
660 * into a separate make_xxx function. This is because we want to run
661 * prepare_sort_from_pathkeys on it before we do so on the individual
662 * child plans, to make cross-checking the sort info easier.
664 copy_path_costsize(plan, (Path *) best_path);
665 plan->targetlist = tlist;
667 plan->lefttree = NULL;
668 plan->righttree = NULL;
670 /* Compute sort column info, and adjust MergeAppend's tlist as needed */
671 (void) prepare_sort_from_pathkeys(root, plan, pathkeys,
675 &node->sortOperators,
680 * Now prepare the child plans. We must apply prepare_sort_from_pathkeys
681 * even to subplans that don't need an explicit sort, to make sure they
682 * are returning the same sort key columns the MergeAppend expects.
684 foreach(subpaths, best_path->subpaths)
686 Path *subpath = (Path *) lfirst(subpaths);
689 AttrNumber *sortColIdx;
694 /* Build the child plan */
695 subplan = create_plan_recurse(root, subpath);
697 /* Compute sort column info, and adjust subplan's tlist as needed */
698 subplan = prepare_sort_from_pathkeys(root, subplan, pathkeys,
707 * Check that we got the same sort key information. We just Assert
708 * that the sortops match, since those depend only on the pathkeys;
709 * but it seems like a good idea to check the sort column numbers
710 * explicitly, to ensure the tlists really do match up.
712 Assert(numsortkeys == node->numCols);
713 if (memcmp(sortColIdx, node->sortColIdx,
714 numsortkeys * sizeof(AttrNumber)) != 0)
715 elog(ERROR, "MergeAppend child's targetlist doesn't match MergeAppend");
716 Assert(memcmp(sortOperators, node->sortOperators,
717 numsortkeys * sizeof(Oid)) == 0);
718 Assert(memcmp(collations, node->collations,
719 numsortkeys * sizeof(Oid)) == 0);
720 Assert(memcmp(nullsFirst, node->nullsFirst,
721 numsortkeys * sizeof(bool)) == 0);
723 /* Now, insert a Sort node if subplan isn't sufficiently ordered */
724 if (!pathkeys_contained_in(pathkeys, subpath->pathkeys))
725 subplan = (Plan *) make_sort(root, subplan, numsortkeys,
726 sortColIdx, sortOperators, collations, nullsFirst,
727 best_path->limit_tuples);
729 subplans = lappend(subplans, subplan);
732 node->mergeplans = subplans;
734 return (Plan *) node;
739 * Create a Result plan for 'best_path'.
740 * This is only used for the case of a query with an empty jointree.
742 * Returns a Plan node.
745 create_result_plan(PlannerInfo *root, ResultPath *best_path)
750 /* The tlist will be installed later, since we have no RelOptInfo */
751 Assert(best_path->path.parent == NULL);
754 /* best_path->quals is just bare clauses */
756 quals = order_qual_clauses(root, best_path->quals);
758 return make_result(root, tlist, (Node *) quals, NULL);
762 * create_material_plan
763 * Create a Material plan for 'best_path' and (recursively) plans
766 * Returns a Plan node.
769 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
774 subplan = create_plan_recurse(root, best_path->subpath);
776 /* We don't want any excess columns in the materialized tuples */
777 disuse_physical_tlist(subplan, best_path->subpath);
779 plan = make_material(subplan);
781 copy_path_costsize(&plan->plan, (Path *) best_path);
788 * Create a Unique plan for 'best_path' and (recursively) plans
791 * Returns a Plan node.
794 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
804 AttrNumber *groupColIdx;
808 subplan = create_plan_recurse(root, best_path->subpath);
810 /* Done if we don't need to do any actual unique-ifying */
811 if (best_path->umethod == UNIQUE_PATH_NOOP)
815 * As constructed, the subplan has a "flat" tlist containing just the Vars
816 * needed here and at upper levels. The values we are supposed to
817 * unique-ify may be expressions in these variables. We have to add any
818 * such expressions to the subplan's tlist.
820 * The subplan may have a "physical" tlist if it is a simple scan plan. If
821 * we're going to sort, this should be reduced to the regular tlist, so
822 * that we don't sort more data than we need to. For hashing, the tlist
823 * should be left as-is if we don't need to add any expressions; but if we
824 * do have to add expressions, then a projection step will be needed at
825 * runtime anyway, so we may as well remove unneeded items. Therefore
826 * newtlist starts from build_relation_tlist() not just a copy of the
827 * subplan's tlist; and we don't install it into the subplan unless we are
828 * sorting or stuff has to be added.
830 in_operators = best_path->in_operators;
831 uniq_exprs = best_path->uniq_exprs;
833 /* initialize modified subplan tlist as just the "required" vars */
834 newtlist = build_relation_tlist(best_path->path.parent);
835 nextresno = list_length(newtlist) + 1;
838 foreach(l, uniq_exprs)
840 Node *uniqexpr = lfirst(l);
843 tle = tlist_member(uniqexpr, newtlist);
846 tle = makeTargetEntry((Expr *) uniqexpr,
850 newtlist = lappend(newtlist, tle);
856 if (newitems || best_path->umethod == UNIQUE_PATH_SORT)
859 * If the top plan node can't do projections, we need to add a Result
860 * node to help it along.
862 if (!is_projection_capable_plan(subplan))
863 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
865 subplan->targetlist = newtlist;
869 * Build control information showing which subplan output columns are to
870 * be examined by the grouping step. Unfortunately we can't merge this
871 * with the previous loop, since we didn't then know which version of the
872 * subplan tlist we'd end up using.
874 newtlist = subplan->targetlist;
875 numGroupCols = list_length(uniq_exprs);
876 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
879 foreach(l, uniq_exprs)
881 Node *uniqexpr = lfirst(l);
884 tle = tlist_member(uniqexpr, newtlist);
885 if (!tle) /* shouldn't happen */
886 elog(ERROR, "failed to find unique expression in subplan tlist");
887 groupColIdx[groupColPos++] = tle->resno;
890 if (best_path->umethod == UNIQUE_PATH_HASH)
895 numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
898 * Get the hashable equality operators for the Agg node to use.
899 * Normally these are the same as the IN clause operators, but if
900 * those are cross-type operators then the equality operators are the
901 * ones for the IN clause operators' RHS datatype.
903 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
905 foreach(l, in_operators)
907 Oid in_oper = lfirst_oid(l);
910 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
911 elog(ERROR, "could not find compatible hash operator for operator %u",
913 groupOperators[groupColPos++] = eq_oper;
917 * Since the Agg node is going to project anyway, we can give it the
918 * minimum output tlist, without any stuff we might have added to the
921 plan = (Plan *) make_agg(root,
922 build_relation_tlist(best_path->path.parent),
934 List *sortList = NIL;
936 /* Create an ORDER BY list to sort the input compatibly */
938 foreach(l, in_operators)
940 Oid in_oper = lfirst_oid(l);
944 SortGroupClause *sortcl;
946 sortop = get_ordering_op_for_equality_op(in_oper, false);
947 if (!OidIsValid(sortop)) /* shouldn't happen */
948 elog(ERROR, "could not find ordering operator for equality operator %u",
952 * The Unique node will need equality operators. Normally these
953 * are the same as the IN clause operators, but if those are
954 * cross-type operators then the equality operators are the ones
955 * for the IN clause operators' RHS datatype.
957 eqop = get_equality_op_for_ordering_op(sortop, NULL);
958 if (!OidIsValid(eqop)) /* shouldn't happen */
959 elog(ERROR, "could not find equality operator for ordering operator %u",
962 tle = get_tle_by_resno(subplan->targetlist,
963 groupColIdx[groupColPos]);
966 sortcl = makeNode(SortGroupClause);
967 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
968 subplan->targetlist);
970 sortcl->sortop = sortop;
971 sortcl->nulls_first = false;
972 sortcl->hashable = false; /* no need to make this accurate */
973 sortList = lappend(sortList, sortcl);
976 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
977 plan = (Plan *) make_unique(plan, sortList);
980 /* Adjust output size estimate (other fields should be OK already) */
981 plan->plan_rows = best_path->rows;
987 /*****************************************************************************
989 * BASE-RELATION SCAN METHODS
991 *****************************************************************************/
995 * create_seqscan_plan
996 * Returns a seqscan plan for the base relation scanned by 'best_path'
997 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1000 create_seqscan_plan(PlannerInfo *root, Path *best_path,
1001 List *tlist, List *scan_clauses)
1004 Index scan_relid = best_path->parent->relid;
1006 /* it should be a base rel... */
1007 Assert(scan_relid > 0);
1008 Assert(best_path->parent->rtekind == RTE_RELATION);
1010 /* Sort clauses into best execution order */
1011 scan_clauses = order_qual_clauses(root, scan_clauses);
1013 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1014 scan_clauses = extract_actual_clauses(scan_clauses, false);
1016 scan_plan = make_seqscan(tlist,
1020 copy_path_costsize(&scan_plan->plan, best_path);
1026 * create_indexscan_plan
1027 * Returns an indexscan plan for the base relation scanned by 'best_path'
1028 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1030 * The indexquals list of the path contains implicitly-ANDed qual conditions.
1031 * The list can be empty --- then no index restrictions will be applied during
1035 create_indexscan_plan(PlannerInfo *root,
1036 IndexPath *best_path,
1040 List *indexquals = best_path->indexquals;
1041 List *indexorderbys = best_path->indexorderbys;
1042 Index baserelid = best_path->path.parent->relid;
1043 Oid indexoid = best_path->indexinfo->indexoid;
1045 List *stripped_indexquals;
1046 List *fixed_indexquals;
1047 List *fixed_indexorderbys;
1049 IndexScan *scan_plan;
1051 /* it should be a base rel... */
1052 Assert(baserelid > 0);
1053 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1056 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
1057 * executor as indexqualorig
1059 stripped_indexquals = get_actual_clauses(indexquals);
1062 * The executor needs a copy with the indexkey on the left of each clause
1063 * and with index attr numbers substituted for table ones.
1065 fixed_indexquals = fix_indexqual_references(root, best_path, indexquals);
1068 * Likewise fix up index attr references in the ORDER BY expressions.
1070 fixed_indexorderbys = fix_indexorderby_references(root, best_path, indexorderbys);
1073 * If this is an innerjoin scan, the indexclauses will contain join
1074 * clauses that are not present in scan_clauses (since the passed-in value
1075 * is just the rel's baserestrictinfo list). We must add these clauses to
1076 * scan_clauses to ensure they get checked. In most cases we will remove
1077 * the join clauses again below, but if a join clause contains a special
1078 * operator, we need to make sure it gets into the scan_clauses.
1080 * Note: pointer comparison should be enough to determine RestrictInfo
1083 if (best_path->isjoininner)
1084 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
1087 * The qpqual list must contain all restrictions not automatically handled
1088 * by the index. All the predicates in the indexquals will be checked
1089 * (either by the index itself, or by nodeIndexscan.c), but if there are
1090 * any "special" operators involved then they must be included in qpqual.
1091 * The upshot is that qpqual must contain scan_clauses minus whatever
1092 * appears in indexquals.
1094 * In normal cases simple pointer equality checks will be enough to spot
1095 * duplicate RestrictInfos, so we try that first. In some situations
1096 * (particularly with OR'd index conditions) we may have scan_clauses that
1097 * are not equal to, but are logically implied by, the index quals; so we
1098 * also try a predicate_implied_by() check to see if we can discard quals
1099 * that way. (predicate_implied_by assumes its first input contains only
1100 * immutable functions, so we have to check that.)
1102 * We can also discard quals that are implied by a partial index's
1103 * predicate, but only in a plain SELECT; when scanning a target relation
1104 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
1105 * plan so that they'll be properly rechecked by EvalPlanQual testing.
1108 foreach(l, scan_clauses)
1110 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1112 Assert(IsA(rinfo, RestrictInfo));
1113 if (rinfo->pseudoconstant)
1114 continue; /* we may drop pseudoconstants here */
1115 if (list_member_ptr(indexquals, rinfo))
1117 if (!contain_mutable_functions((Node *) rinfo->clause))
1119 List *clausel = list_make1(rinfo->clause);
1121 if (predicate_implied_by(clausel, indexquals))
1123 if (best_path->indexinfo->indpred)
1125 if (baserelid != root->parse->resultRelation &&
1126 get_parse_rowmark(root->parse, baserelid) == NULL)
1127 if (predicate_implied_by(clausel,
1128 best_path->indexinfo->indpred))
1132 qpqual = lappend(qpqual, rinfo);
1135 /* Sort clauses into best execution order */
1136 qpqual = order_qual_clauses(root, qpqual);
1138 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1139 qpqual = extract_actual_clauses(qpqual, false);
1142 * We have to replace any outer-relation variables with nestloop params
1143 * in the indexqualorig, qpqual, and indexorderbyorig expressions. A bit
1144 * annoying to have to do this separately from the processing in
1145 * fix_indexqual_references --- rethink this when generalizing the inner
1146 * indexscan support. But note we can't really do this earlier because
1147 * it'd break the comparisons to predicates above ... (or would it? Those
1148 * wouldn't have outer refs)
1150 if (best_path->isjoininner)
1152 stripped_indexquals = (List *)
1153 replace_nestloop_params(root, (Node *) stripped_indexquals);
1155 replace_nestloop_params(root, (Node *) qpqual);
1156 indexorderbys = (List *)
1157 replace_nestloop_params(root, (Node *) indexorderbys);
1160 /* Finally ready to build the plan node */
1161 scan_plan = make_indexscan(tlist,
1166 stripped_indexquals,
1167 fixed_indexorderbys,
1169 best_path->indexscandir);
1171 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1172 /* use the indexscan-specific rows estimate, not the parent rel's */
1173 scan_plan->scan.plan.plan_rows = best_path->rows;
1179 * create_bitmap_scan_plan
1180 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
1181 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1183 static BitmapHeapScan *
1184 create_bitmap_scan_plan(PlannerInfo *root,
1185 BitmapHeapPath *best_path,
1189 Index baserelid = best_path->path.parent->relid;
1190 Plan *bitmapqualplan;
1191 List *bitmapqualorig;
1195 BitmapHeapScan *scan_plan;
1197 /* it should be a base rel... */
1198 Assert(baserelid > 0);
1199 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1201 /* Process the bitmapqual tree into a Plan tree and qual lists */
1202 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1203 &bitmapqualorig, &indexquals);
1205 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1206 scan_clauses = extract_actual_clauses(scan_clauses, false);
1209 * If this is a innerjoin scan, the indexclauses will contain join clauses
1210 * that are not present in scan_clauses (since the passed-in value is just
1211 * the rel's baserestrictinfo list). We must add these clauses to
1212 * scan_clauses to ensure they get checked. In most cases we will remove
1213 * the join clauses again below, but if a join clause contains a special
1214 * operator, we need to make sure it gets into the scan_clauses.
1216 if (best_path->isjoininner)
1218 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1222 * The qpqual list must contain all restrictions not automatically handled
1223 * by the index. All the predicates in the indexquals will be checked
1224 * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1225 * are any "special" operators involved then they must be added to qpqual.
1226 * The upshot is that qpqual must contain scan_clauses minus whatever
1227 * appears in indexquals.
1229 * In normal cases simple equal() checks will be enough to spot duplicate
1230 * clauses, so we try that first. In some situations (particularly with
1231 * OR'd index conditions) we may have scan_clauses that are not equal to,
1232 * but are logically implied by, the index quals; so we also try a
1233 * predicate_implied_by() check to see if we can discard quals that way.
1234 * (predicate_implied_by assumes its first input contains only immutable
1235 * functions, so we have to check that.)
1237 * Unlike create_indexscan_plan(), we need take no special thought here
1238 * for partial index predicates; this is because the predicate conditions
1239 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1240 * to do it that way because predicate conditions need to be rechecked if
1241 * the scan becomes lossy.
1244 foreach(l, scan_clauses)
1246 Node *clause = (Node *) lfirst(l);
1248 if (list_member(indexquals, clause))
1250 if (!contain_mutable_functions(clause))
1252 List *clausel = list_make1(clause);
1254 if (predicate_implied_by(clausel, indexquals))
1257 qpqual = lappend(qpqual, clause);
1260 /* Sort clauses into best execution order */
1261 qpqual = order_qual_clauses(root, qpqual);
1264 * When dealing with special operators, we will at this point have
1265 * duplicate clauses in qpqual and bitmapqualorig. We may as well drop
1266 * 'em from bitmapqualorig, since there's no point in making the tests
1269 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1271 /* Finally ready to build the plan node */
1272 scan_plan = make_bitmap_heapscan(tlist,
1278 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1279 /* use the indexscan-specific rows estimate, not the parent rel's */
1280 scan_plan->scan.plan.plan_rows = best_path->rows;
1286 * Given a bitmapqual tree, generate the Plan tree that implements it
1288 * As byproducts, we also return in *qual and *indexqual the qual lists
1289 * (in implicit-AND form, without RestrictInfos) describing the original index
1290 * conditions and the generated indexqual conditions. (These are the same in
1291 * simple cases, but when special index operators are involved, the former
1292 * list includes the special conditions while the latter includes the actual
1293 * indexable conditions derived from them.) Both lists include partial-index
1294 * predicates, because we have to recheck predicates as well as index
1295 * conditions if the bitmap scan becomes lossy.
1297 * Note: if you find yourself changing this, you probably need to change
1298 * make_restrictinfo_from_bitmapqual too.
1301 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1302 List **qual, List **indexqual)
1306 if (IsA(bitmapqual, BitmapAndPath))
1308 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1309 List *subplans = NIL;
1310 List *subquals = NIL;
1311 List *subindexquals = NIL;
1315 * There may well be redundant quals among the subplans, since a
1316 * top-level WHERE qual might have gotten used to form several
1317 * different index quals. We don't try exceedingly hard to eliminate
1318 * redundancies, but we do eliminate obvious duplicates by using
1319 * list_concat_unique.
1321 foreach(l, apath->bitmapquals)
1327 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1328 &subqual, &subindexqual);
1329 subplans = lappend(subplans, subplan);
1330 subquals = list_concat_unique(subquals, subqual);
1331 subindexquals = list_concat_unique(subindexquals, subindexqual);
1333 plan = (Plan *) make_bitmap_and(subplans);
1334 plan->startup_cost = apath->path.startup_cost;
1335 plan->total_cost = apath->path.total_cost;
1337 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1338 plan->plan_width = 0; /* meaningless */
1340 *indexqual = subindexquals;
1342 else if (IsA(bitmapqual, BitmapOrPath))
1344 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1345 List *subplans = NIL;
1346 List *subquals = NIL;
1347 List *subindexquals = NIL;
1348 bool const_true_subqual = false;
1349 bool const_true_subindexqual = false;
1353 * Here, we only detect qual-free subplans. A qual-free subplan would
1354 * cause us to generate "... OR true ..." which we may as well reduce
1355 * to just "true". We do not try to eliminate redundant subclauses
1356 * because (a) it's not as likely as in the AND case, and (b) we might
1357 * well be working with hundreds or even thousands of OR conditions,
1358 * perhaps from a long IN list. The performance of list_append_unique
1359 * would be unacceptable.
1361 foreach(l, opath->bitmapquals)
1367 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1368 &subqual, &subindexqual);
1369 subplans = lappend(subplans, subplan);
1371 const_true_subqual = true;
1372 else if (!const_true_subqual)
1373 subquals = lappend(subquals,
1374 make_ands_explicit(subqual));
1375 if (subindexqual == NIL)
1376 const_true_subindexqual = true;
1377 else if (!const_true_subindexqual)
1378 subindexquals = lappend(subindexquals,
1379 make_ands_explicit(subindexqual));
1383 * In the presence of ScalarArrayOpExpr quals, we might have built
1384 * BitmapOrPaths with just one subpath; don't add an OR step.
1386 if (list_length(subplans) == 1)
1388 plan = (Plan *) linitial(subplans);
1392 plan = (Plan *) make_bitmap_or(subplans);
1393 plan->startup_cost = opath->path.startup_cost;
1394 plan->total_cost = opath->path.total_cost;
1396 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1397 plan->plan_width = 0; /* meaningless */
1401 * If there were constant-TRUE subquals, the OR reduces to constant
1402 * TRUE. Also, avoid generating one-element ORs, which could happen
1403 * due to redundancy elimination or ScalarArrayOpExpr quals.
1405 if (const_true_subqual)
1407 else if (list_length(subquals) <= 1)
1410 *qual = list_make1(make_orclause(subquals));
1411 if (const_true_subindexqual)
1413 else if (list_length(subindexquals) <= 1)
1414 *indexqual = subindexquals;
1416 *indexqual = list_make1(make_orclause(subindexquals));
1418 else if (IsA(bitmapqual, IndexPath))
1420 IndexPath *ipath = (IndexPath *) bitmapqual;
1424 /* Use the regular indexscan plan build machinery... */
1425 iscan = create_indexscan_plan(root, ipath, NIL, NIL);
1426 /* then convert to a bitmap indexscan */
1427 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1430 iscan->indexqualorig);
1431 plan->startup_cost = 0.0;
1432 plan->total_cost = ipath->indextotalcost;
1434 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1435 plan->plan_width = 0; /* meaningless */
1436 *qual = get_actual_clauses(ipath->indexclauses);
1437 *indexqual = get_actual_clauses(ipath->indexquals);
1438 foreach(l, ipath->indexinfo->indpred)
1440 Expr *pred = (Expr *) lfirst(l);
1443 * We know that the index predicate must have been implied by the
1444 * query condition as a whole, but it may or may not be implied by
1445 * the conditions that got pushed into the bitmapqual. Avoid
1446 * generating redundant conditions.
1448 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1450 *qual = lappend(*qual, pred);
1451 *indexqual = lappend(*indexqual, pred);
1455 * Replace outer-relation variables with nestloop params, but only
1456 * after doing the above comparisons to index predicates.
1458 if (ipath->isjoininner)
1461 replace_nestloop_params(root, (Node *) *qual);
1462 *indexqual = (List *)
1463 replace_nestloop_params(root, (Node *) *indexqual);
1468 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1469 plan = NULL; /* keep compiler quiet */
1476 * create_tidscan_plan
1477 * Returns a tidscan plan for the base relation scanned by 'best_path'
1478 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1481 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1482 List *tlist, List *scan_clauses)
1485 Index scan_relid = best_path->path.parent->relid;
1488 /* it should be a base rel... */
1489 Assert(scan_relid > 0);
1490 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1492 /* Sort clauses into best execution order */
1493 scan_clauses = order_qual_clauses(root, scan_clauses);
1495 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1496 scan_clauses = extract_actual_clauses(scan_clauses, false);
1499 * Remove any clauses that are TID quals. This is a bit tricky since the
1500 * tidquals list has implicit OR semantics.
1502 ortidquals = best_path->tidquals;
1503 if (list_length(ortidquals) > 1)
1504 ortidquals = list_make1(make_orclause(ortidquals));
1505 scan_clauses = list_difference(scan_clauses, ortidquals);
1507 scan_plan = make_tidscan(tlist,
1510 best_path->tidquals);
1512 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1518 * create_subqueryscan_plan
1519 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1520 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1522 static SubqueryScan *
1523 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1524 List *tlist, List *scan_clauses)
1526 SubqueryScan *scan_plan;
1527 Index scan_relid = best_path->parent->relid;
1529 /* it should be a subquery base rel... */
1530 Assert(scan_relid > 0);
1531 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1533 /* Sort clauses into best execution order */
1534 scan_clauses = order_qual_clauses(root, scan_clauses);
1536 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1537 scan_clauses = extract_actual_clauses(scan_clauses, false);
1539 scan_plan = make_subqueryscan(tlist,
1542 best_path->parent->subplan,
1543 best_path->parent->subrtable,
1544 best_path->parent->subrowmark);
1546 copy_path_costsize(&scan_plan->scan.plan, best_path);
1552 * create_functionscan_plan
1553 * Returns a functionscan plan for the base relation scanned by 'best_path'
1554 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1556 static FunctionScan *
1557 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1558 List *tlist, List *scan_clauses)
1560 FunctionScan *scan_plan;
1561 Index scan_relid = best_path->parent->relid;
1564 /* it should be a function base rel... */
1565 Assert(scan_relid > 0);
1566 rte = planner_rt_fetch(scan_relid, root);
1567 Assert(rte->rtekind == RTE_FUNCTION);
1569 /* Sort clauses into best execution order */
1570 scan_clauses = order_qual_clauses(root, scan_clauses);
1572 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1573 scan_clauses = extract_actual_clauses(scan_clauses, false);
1575 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1577 rte->eref->colnames,
1579 rte->funccoltypmods,
1580 rte->funccolcollations);
1582 copy_path_costsize(&scan_plan->scan.plan, best_path);
1588 * create_valuesscan_plan
1589 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1590 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1593 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1594 List *tlist, List *scan_clauses)
1596 ValuesScan *scan_plan;
1597 Index scan_relid = best_path->parent->relid;
1600 /* it should be a values base rel... */
1601 Assert(scan_relid > 0);
1602 rte = planner_rt_fetch(scan_relid, root);
1603 Assert(rte->rtekind == RTE_VALUES);
1605 /* Sort clauses into best execution order */
1606 scan_clauses = order_qual_clauses(root, scan_clauses);
1608 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1609 scan_clauses = extract_actual_clauses(scan_clauses, false);
1611 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1614 copy_path_costsize(&scan_plan->scan.plan, best_path);
1620 * create_ctescan_plan
1621 * Returns a ctescan plan for the base relation scanned by 'best_path'
1622 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1625 create_ctescan_plan(PlannerInfo *root, Path *best_path,
1626 List *tlist, List *scan_clauses)
1629 Index scan_relid = best_path->parent->relid;
1631 SubPlan *ctesplan = NULL;
1634 PlannerInfo *cteroot;
1639 Assert(scan_relid > 0);
1640 rte = planner_rt_fetch(scan_relid, root);
1641 Assert(rte->rtekind == RTE_CTE);
1642 Assert(!rte->self_reference);
1645 * Find the referenced CTE, and locate the SubPlan previously made for it.
1647 levelsup = rte->ctelevelsup;
1649 while (levelsup-- > 0)
1651 cteroot = cteroot->parent_root;
1652 if (!cteroot) /* shouldn't happen */
1653 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1657 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1658 * on planning the CTEs (ie, this is a side-reference from another CTE).
1659 * So we mustn't use forboth here.
1662 foreach(lc, cteroot->parse->cteList)
1664 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1666 if (strcmp(cte->ctename, rte->ctename) == 0)
1670 if (lc == NULL) /* shouldn't happen */
1671 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1672 if (ndx >= list_length(cteroot->cte_plan_ids))
1673 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1674 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1675 Assert(plan_id > 0);
1676 foreach(lc, cteroot->init_plans)
1678 ctesplan = (SubPlan *) lfirst(lc);
1679 if (ctesplan->plan_id == plan_id)
1682 if (lc == NULL) /* shouldn't happen */
1683 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1686 * We need the CTE param ID, which is the sole member of the SubPlan's
1689 cte_param_id = linitial_int(ctesplan->setParam);
1691 /* Sort clauses into best execution order */
1692 scan_clauses = order_qual_clauses(root, scan_clauses);
1694 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1695 scan_clauses = extract_actual_clauses(scan_clauses, false);
1697 scan_plan = make_ctescan(tlist, scan_clauses, scan_relid,
1698 plan_id, cte_param_id);
1700 copy_path_costsize(&scan_plan->scan.plan, best_path);
1706 * create_worktablescan_plan
1707 * Returns a worktablescan plan for the base relation scanned by 'best_path'
1708 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1710 static WorkTableScan *
1711 create_worktablescan_plan(PlannerInfo *root, Path *best_path,
1712 List *tlist, List *scan_clauses)
1714 WorkTableScan *scan_plan;
1715 Index scan_relid = best_path->parent->relid;
1718 PlannerInfo *cteroot;
1720 Assert(scan_relid > 0);
1721 rte = planner_rt_fetch(scan_relid, root);
1722 Assert(rte->rtekind == RTE_CTE);
1723 Assert(rte->self_reference);
1726 * We need to find the worktable param ID, which is in the plan level
1727 * that's processing the recursive UNION, which is one level *below* where
1728 * the CTE comes from.
1730 levelsup = rte->ctelevelsup;
1731 if (levelsup == 0) /* shouldn't happen */
1732 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1735 while (levelsup-- > 0)
1737 cteroot = cteroot->parent_root;
1738 if (!cteroot) /* shouldn't happen */
1739 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1741 if (cteroot->wt_param_id < 0) /* shouldn't happen */
1742 elog(ERROR, "could not find param ID for CTE \"%s\"", rte->ctename);
1744 /* Sort clauses into best execution order */
1745 scan_clauses = order_qual_clauses(root, scan_clauses);
1747 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1748 scan_clauses = extract_actual_clauses(scan_clauses, false);
1750 scan_plan = make_worktablescan(tlist, scan_clauses, scan_relid,
1751 cteroot->wt_param_id);
1753 copy_path_costsize(&scan_plan->scan.plan, best_path);
1759 /*****************************************************************************
1763 *****************************************************************************/
1766 create_nestloop_plan(PlannerInfo *root,
1767 NestPath *best_path,
1771 NestLoop *join_plan;
1772 List *tlist = build_relation_tlist(best_path->path.parent);
1773 List *joinrestrictclauses = best_path->joinrestrictinfo;
1783 * If the inner path is a nestloop inner indexscan, it might be using some
1784 * of the join quals as index quals, in which case we don't have to check
1785 * them again at the join node. Remove any join quals that are redundant.
1787 joinrestrictclauses =
1788 select_nonredundant_join_clauses(root,
1789 joinrestrictclauses,
1790 best_path->innerjoinpath);
1792 /* Sort join qual clauses into best execution order */
1793 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1795 /* Get the join qual clauses (in plain expression form) */
1796 /* Any pseudoconstant clauses are ignored here */
1797 if (IS_OUTER_JOIN(best_path->jointype))
1799 extract_actual_join_clauses(joinrestrictclauses,
1800 &joinclauses, &otherclauses);
1804 /* We can treat all clauses alike for an inner join */
1805 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1810 * Identify any nestloop parameters that should be supplied by this join
1811 * node, and move them from root->curOuterParams to the nestParams list.
1813 outerrelids = best_path->outerjoinpath->parent->relids;
1816 for (cell = list_head(root->curOuterParams); cell; cell = next)
1818 NestLoopParam *nlp = (NestLoopParam *) lfirst(cell);
1821 if (bms_is_member(nlp->paramval->varno, outerrelids))
1823 root->curOuterParams = list_delete_cell(root->curOuterParams,
1825 nestParams = lappend(nestParams, nlp);
1831 join_plan = make_nestloop(tlist,
1837 best_path->jointype);
1839 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1845 create_mergejoin_plan(PlannerInfo *root,
1846 MergePath *best_path,
1850 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1854 List *outerpathkeys;
1855 List *innerpathkeys;
1858 Oid *mergecollations;
1859 int *mergestrategies;
1860 bool *mergenullsfirst;
1861 MergeJoin *join_plan;
1867 /* Sort join qual clauses into best execution order */
1868 /* NB: do NOT reorder the mergeclauses */
1869 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1871 /* Get the join qual clauses (in plain expression form) */
1872 /* Any pseudoconstant clauses are ignored here */
1873 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1875 extract_actual_join_clauses(joinclauses,
1876 &joinclauses, &otherclauses);
1880 /* We can treat all clauses alike for an inner join */
1881 joinclauses = extract_actual_clauses(joinclauses, false);
1886 * Remove the mergeclauses from the list of join qual clauses, leaving the
1887 * list of quals that must be checked as qpquals.
1889 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1890 joinclauses = list_difference(joinclauses, mergeclauses);
1893 * Rearrange mergeclauses, if needed, so that the outer variable is always
1894 * on the left; mark the mergeclause restrictinfos with correct
1895 * outer_is_left status.
1897 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1898 best_path->jpath.outerjoinpath->parent->relids);
1901 * Create explicit sort nodes for the outer and inner paths if necessary.
1902 * Make sure there are no excess columns in the inputs if sorting.
1904 if (best_path->outersortkeys)
1906 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1907 outer_plan = (Plan *)
1908 make_sort_from_pathkeys(root,
1910 best_path->outersortkeys,
1912 outerpathkeys = best_path->outersortkeys;
1915 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
1917 if (best_path->innersortkeys)
1919 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1920 inner_plan = (Plan *)
1921 make_sort_from_pathkeys(root,
1923 best_path->innersortkeys,
1925 innerpathkeys = best_path->innersortkeys;
1928 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
1931 * If specified, add a materialize node to shield the inner plan from the
1932 * need to handle mark/restore.
1934 if (best_path->materialize_inner)
1936 Plan *matplan = (Plan *) make_material(inner_plan);
1939 * We assume the materialize will not spill to disk, and therefore
1940 * charge just cpu_operator_cost per tuple. (Keep this estimate in
1941 * sync with cost_mergejoin.)
1943 copy_plan_costsize(matplan, inner_plan);
1944 matplan->total_cost += cpu_operator_cost * matplan->plan_rows;
1946 inner_plan = matplan;
1950 * Compute the opfamily/strategy/nullsfirst arrays needed by the executor.
1951 * The information is in the pathkeys for the two inputs, but we need to
1952 * be careful about the possibility of mergeclauses sharing a pathkey
1953 * (compare find_mergeclauses_for_pathkeys()).
1955 nClauses = list_length(mergeclauses);
1956 Assert(nClauses == list_length(best_path->path_mergeclauses));
1957 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
1958 mergecollations = (Oid *) palloc(nClauses * sizeof(Oid));
1959 mergestrategies = (int *) palloc(nClauses * sizeof(int));
1960 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
1962 lop = list_head(outerpathkeys);
1963 lip = list_head(innerpathkeys);
1965 foreach(lc, best_path->path_mergeclauses)
1967 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1968 EquivalenceClass *oeclass;
1969 EquivalenceClass *ieclass;
1972 EquivalenceClass *opeclass;
1973 EquivalenceClass *ipeclass;
1976 /* fetch outer/inner eclass from mergeclause */
1977 Assert(IsA(rinfo, RestrictInfo));
1978 if (rinfo->outer_is_left)
1980 oeclass = rinfo->left_ec;
1981 ieclass = rinfo->right_ec;
1985 oeclass = rinfo->right_ec;
1986 ieclass = rinfo->left_ec;
1988 Assert(oeclass != NULL);
1989 Assert(ieclass != NULL);
1992 * For debugging purposes, we check that the eclasses match the paths'
1993 * pathkeys. In typical cases the merge clauses are one-to-one with
1994 * the pathkeys, but when dealing with partially redundant query
1995 * conditions, we might have clauses that re-reference earlier path
1996 * keys. The case that we need to reject is where a pathkey is
1997 * entirely skipped over.
1999 * lop and lip reference the first as-yet-unused pathkey elements;
2000 * it's okay to match them, or any element before them. If they're
2001 * NULL then we have found all pathkey elements to be used.
2005 opathkey = (PathKey *) lfirst(lop);
2006 opeclass = opathkey->pk_eclass;
2007 if (oeclass == opeclass)
2009 /* fast path for typical case */
2014 /* redundant clauses ... must match something before lop */
2015 foreach(l2, outerpathkeys)
2019 opathkey = (PathKey *) lfirst(l2);
2020 opeclass = opathkey->pk_eclass;
2021 if (oeclass == opeclass)
2024 if (oeclass != opeclass)
2025 elog(ERROR, "outer pathkeys do not match mergeclauses");
2030 /* redundant clauses ... must match some already-used pathkey */
2033 foreach(l2, outerpathkeys)
2035 opathkey = (PathKey *) lfirst(l2);
2036 opeclass = opathkey->pk_eclass;
2037 if (oeclass == opeclass)
2041 elog(ERROR, "outer pathkeys do not match mergeclauses");
2046 ipathkey = (PathKey *) lfirst(lip);
2047 ipeclass = ipathkey->pk_eclass;
2048 if (ieclass == ipeclass)
2050 /* fast path for typical case */
2055 /* redundant clauses ... must match something before lip */
2056 foreach(l2, innerpathkeys)
2060 ipathkey = (PathKey *) lfirst(l2);
2061 ipeclass = ipathkey->pk_eclass;
2062 if (ieclass == ipeclass)
2065 if (ieclass != ipeclass)
2066 elog(ERROR, "inner pathkeys do not match mergeclauses");
2071 /* redundant clauses ... must match some already-used pathkey */
2074 foreach(l2, innerpathkeys)
2076 ipathkey = (PathKey *) lfirst(l2);
2077 ipeclass = ipathkey->pk_eclass;
2078 if (ieclass == ipeclass)
2082 elog(ERROR, "inner pathkeys do not match mergeclauses");
2085 /* pathkeys should match each other too (more debugging) */
2086 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
2087 opathkey->pk_collation != ipathkey->pk_collation ||
2088 opathkey->pk_strategy != ipathkey->pk_strategy ||
2089 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
2090 elog(ERROR, "left and right pathkeys do not match in mergejoin");
2092 /* OK, save info for executor */
2093 mergefamilies[i] = opathkey->pk_opfamily;
2094 mergecollations[i] = opathkey->pk_collation;
2095 mergestrategies[i] = opathkey->pk_strategy;
2096 mergenullsfirst[i] = opathkey->pk_nulls_first;
2101 * Note: it is not an error if we have additional pathkey elements (i.e.,
2102 * lop or lip isn't NULL here). The input paths might be better-sorted
2103 * than we need for the current mergejoin.
2107 * Now we can build the mergejoin node.
2109 join_plan = make_mergejoin(tlist,
2119 best_path->jpath.jointype);
2121 /* Costs of sort and material steps are included in path cost already */
2122 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2128 create_hashjoin_plan(PlannerInfo *root,
2129 HashPath *best_path,
2133 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
2137 Oid skewTable = InvalidOid;
2138 AttrNumber skewColumn = InvalidAttrNumber;
2139 bool skewInherit = false;
2140 Oid skewColType = InvalidOid;
2141 int32 skewColTypmod = -1;
2142 HashJoin *join_plan;
2145 /* Sort join qual clauses into best execution order */
2146 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
2147 /* There's no point in sorting the hash clauses ... */
2149 /* Get the join qual clauses (in plain expression form) */
2150 /* Any pseudoconstant clauses are ignored here */
2151 if (IS_OUTER_JOIN(best_path->jpath.jointype))
2153 extract_actual_join_clauses(joinclauses,
2154 &joinclauses, &otherclauses);
2158 /* We can treat all clauses alike for an inner join */
2159 joinclauses = extract_actual_clauses(joinclauses, false);
2164 * Remove the hashclauses from the list of join qual clauses, leaving the
2165 * list of quals that must be checked as qpquals.
2167 hashclauses = get_actual_clauses(best_path->path_hashclauses);
2168 joinclauses = list_difference(joinclauses, hashclauses);
2171 * Rearrange hashclauses, if needed, so that the outer variable is always
2174 hashclauses = get_switched_clauses(best_path->path_hashclauses,
2175 best_path->jpath.outerjoinpath->parent->relids);
2177 /* We don't want any excess columns in the hashed tuples */
2178 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
2180 /* If we expect batching, suppress excess columns in outer tuples too */
2181 if (best_path->num_batches > 1)
2182 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
2185 * If there is a single join clause and we can identify the outer variable
2186 * as a simple column reference, supply its identity for possible use in
2187 * skew optimization. (Note: in principle we could do skew optimization
2188 * with multiple join clauses, but we'd have to be able to determine the
2189 * most common combinations of outer values, which we don't currently have
2190 * enough stats for.)
2192 if (list_length(hashclauses) == 1)
2194 OpExpr *clause = (OpExpr *) linitial(hashclauses);
2197 Assert(is_opclause(clause));
2198 node = (Node *) linitial(clause->args);
2199 if (IsA(node, RelabelType))
2200 node = (Node *) ((RelabelType *) node)->arg;
2203 Var *var = (Var *) node;
2206 rte = root->simple_rte_array[var->varno];
2207 if (rte->rtekind == RTE_RELATION)
2209 skewTable = rte->relid;
2210 skewColumn = var->varattno;
2211 skewInherit = rte->inh;
2212 skewColType = var->vartype;
2213 skewColTypmod = var->vartypmod;
2219 * Build the hash node and hash join node.
2221 hash_plan = make_hash(inner_plan,
2227 join_plan = make_hashjoin(tlist,
2233 best_path->jpath.jointype);
2235 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
2241 /*****************************************************************************
2243 * SUPPORTING ROUTINES
2245 *****************************************************************************/
2248 * replace_nestloop_params
2249 * Replace outer-relation Vars in the given expression with nestloop Params
2251 * All Vars belonging to the relation(s) identified by root->curOuterRels
2252 * are replaced by Params, and entries are added to root->curOuterParams if
2253 * not already present.
2256 replace_nestloop_params(PlannerInfo *root, Node *expr)
2258 /* No setup needed for tree walk, so away we go */
2259 return replace_nestloop_params_mutator(expr, root);
2263 replace_nestloop_params_mutator(Node *node, PlannerInfo *root)
2269 Var *var = (Var *) node;
2274 /* Upper-level Vars should be long gone at this point */
2275 Assert(var->varlevelsup == 0);
2276 /* If not to be replaced, we can just return the Var unmodified */
2277 if (!bms_is_member(var->varno, root->curOuterRels))
2279 /* Create a Param representing the Var */
2280 param = assign_nestloop_param(root, var);
2281 /* Is this param already listed in root->curOuterParams? */
2282 foreach(lc, root->curOuterParams)
2284 nlp = (NestLoopParam *) lfirst(lc);
2285 if (nlp->paramno == param->paramid)
2287 Assert(equal(var, nlp->paramval));
2288 /* Present, so we can just return the Param */
2289 return (Node *) param;
2293 nlp = makeNode(NestLoopParam);
2294 nlp->paramno = param->paramid;
2295 nlp->paramval = var;
2296 root->curOuterParams = lappend(root->curOuterParams, nlp);
2297 /* And return the replacement Param */
2298 return (Node *) param;
2300 return expression_tree_mutator(node,
2301 replace_nestloop_params_mutator,
2306 * fix_indexqual_references
2307 * Adjust indexqual clauses to the form the executor's indexqual
2310 * We have four tasks here:
2311 * * Remove RestrictInfo nodes from the input clauses.
2312 * * Replace any outer-relation Var nodes with nestloop Params.
2313 * (XXX eventually, that responsibility should go elsewhere?)
2314 * * Index keys must be represented by Var nodes with varattno set to the
2315 * index's attribute number, not the attribute number in the original rel.
2316 * * If the index key is on the right, commute the clause to put it on the
2319 * The result is a modified copy of the indexquals list --- the
2320 * original is not changed. Note also that the copy shares no substructure
2321 * with the original; this is needed in case there is a subplan in it (we need
2322 * two separate copies of the subplan tree, or things will go awry).
2325 fix_indexqual_references(PlannerInfo *root, IndexPath *index_path,
2328 IndexOptInfo *index = index_path->indexinfo;
2329 List *fixed_indexquals;
2332 fixed_indexquals = NIL;
2334 foreach(l, indexquals)
2336 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
2339 Assert(IsA(rinfo, RestrictInfo));
2342 * Replace any outer-relation variables with nestloop params.
2344 * This also makes a copy of the clause, so it's safe to modify it
2347 clause = replace_nestloop_params(root, (Node *) rinfo->clause);
2349 if (IsA(clause, OpExpr))
2351 OpExpr *op = (OpExpr *) clause;
2353 if (list_length(op->args) != 2)
2354 elog(ERROR, "indexqual clause is not binary opclause");
2357 * Check to see if the indexkey is on the right; if so, commute
2358 * the clause. The indexkey should be the side that refers to
2359 * (only) the base relation.
2361 if (!bms_equal(rinfo->left_relids, index->rel->relids))
2365 * Now, determine which index attribute this is and change the
2366 * indexkey operand as needed.
2368 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2371 else if (IsA(clause, RowCompareExpr))
2373 RowCompareExpr *rc = (RowCompareExpr *) clause;
2377 * Check to see if the indexkey is on the right; if so, commute
2378 * the clause. The indexkey should be the side that refers to
2379 * (only) the base relation.
2381 if (!bms_overlap(pull_varnos(linitial(rc->largs)),
2382 index->rel->relids))
2383 CommuteRowCompareExpr(rc);
2386 * For each column in the row comparison, determine which index
2387 * attribute this is and change the indexkey operand as needed.
2389 foreach(lc, rc->largs)
2391 lfirst(lc) = fix_indexqual_operand(lfirst(lc),
2395 else if (IsA(clause, ScalarArrayOpExpr))
2397 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
2399 /* Never need to commute... */
2402 * Determine which index attribute this is and change the indexkey
2403 * operand as needed.
2405 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
2408 else if (IsA(clause, NullTest))
2410 NullTest *nt = (NullTest *) clause;
2412 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
2416 elog(ERROR, "unsupported indexqual type: %d",
2417 (int) nodeTag(clause));
2419 fixed_indexquals = lappend(fixed_indexquals, clause);
2422 return fixed_indexquals;
2426 * fix_indexorderby_references
2427 * Adjust indexorderby clauses to the form the executor's index
2430 * This is a simplified version of fix_indexqual_references. The input does
2431 * not have RestrictInfo nodes, and we assume that indxqual.c already
2432 * commuted the clauses to put the index keys on the left. Also, we don't
2433 * bother to support any cases except simple OpExprs, since nothing else
2434 * is allowed for ordering operators.
2437 fix_indexorderby_references(PlannerInfo *root, IndexPath *index_path,
2438 List *indexorderbys)
2440 IndexOptInfo *index = index_path->indexinfo;
2441 List *fixed_indexorderbys;
2444 fixed_indexorderbys = NIL;
2446 foreach(l, indexorderbys)
2448 Node *clause = (Node *) lfirst(l);
2451 * Replace any outer-relation variables with nestloop params.
2453 * This also makes a copy of the clause, so it's safe to modify it
2456 clause = replace_nestloop_params(root, clause);
2458 if (IsA(clause, OpExpr))
2460 OpExpr *op = (OpExpr *) clause;
2462 if (list_length(op->args) != 2)
2463 elog(ERROR, "indexorderby clause is not binary opclause");
2466 * Now, determine which index attribute this is and change the
2467 * indexkey operand as needed.
2469 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
2473 elog(ERROR, "unsupported indexorderby type: %d",
2474 (int) nodeTag(clause));
2476 fixed_indexorderbys = lappend(fixed_indexorderbys, clause);
2479 return fixed_indexorderbys;
2483 * fix_indexqual_operand
2484 * Convert an indexqual expression to a Var referencing the index column.
2487 fix_indexqual_operand(Node *node, IndexOptInfo *index)
2490 * We represent index keys by Var nodes having the varno of the base table
2491 * but varattno equal to the index's attribute number (index column
2492 * position). This is a bit hokey ... would be cleaner to use a
2493 * special-purpose node type that could not be mistaken for a regular Var.
2494 * But it will do for now.
2498 ListCell *indexpr_item;
2501 * Remove any binary-compatible relabeling of the indexkey
2503 if (IsA(node, RelabelType))
2504 node = (Node *) ((RelabelType *) node)->arg;
2506 if (IsA(node, Var) &&
2507 ((Var *) node)->varno == index->rel->relid)
2509 /* Try to match against simple index columns */
2510 int varatt = ((Var *) node)->varattno;
2514 for (pos = 0; pos < index->ncolumns; pos++)
2516 if (index->indexkeys[pos] == varatt)
2518 result = (Var *) copyObject(node);
2519 result->varattno = pos + 1;
2520 return (Node *) result;
2526 /* Try to match against index expressions */
2527 indexpr_item = list_head(index->indexprs);
2528 for (pos = 0; pos < index->ncolumns; pos++)
2530 if (index->indexkeys[pos] == 0)
2534 if (indexpr_item == NULL)
2535 elog(ERROR, "too few entries in indexprs list");
2536 indexkey = (Node *) lfirst(indexpr_item);
2537 if (indexkey && IsA(indexkey, RelabelType))
2538 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2539 if (equal(node, indexkey))
2542 result = makeVar(index->rel->relid, pos + 1,
2543 exprType(lfirst(indexpr_item)), -1,
2544 exprCollation(lfirst(indexpr_item)),
2546 return (Node *) result;
2548 indexpr_item = lnext(indexpr_item);
2553 elog(ERROR, "node is not an index attribute");
2554 return NULL; /* keep compiler quiet */
2558 * get_switched_clauses
2559 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2560 * extract the bare clauses, and rearrange the elements within the
2561 * clauses, if needed, so the outer join variable is on the left and
2562 * the inner is on the right. The original clause data structure is not
2563 * touched; a modified list is returned. We do, however, set the transient
2564 * outer_is_left field in each RestrictInfo to show which side was which.
2567 get_switched_clauses(List *clauses, Relids outerrelids)
2574 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2575 OpExpr *clause = (OpExpr *) restrictinfo->clause;
2577 Assert(is_opclause(clause));
2578 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2581 * Duplicate just enough of the structure to allow commuting the
2582 * clause without changing the original list. Could use
2583 * copyObject, but a complete deep copy is overkill.
2585 OpExpr *temp = makeNode(OpExpr);
2587 temp->opno = clause->opno;
2588 temp->opfuncid = InvalidOid;
2589 temp->opresulttype = clause->opresulttype;
2590 temp->opretset = clause->opretset;
2591 temp->args = list_copy(clause->args);
2592 temp->location = clause->location;
2593 /* Commute it --- note this modifies the temp node in-place. */
2594 CommuteOpExpr(temp);
2595 t_list = lappend(t_list, temp);
2596 restrictinfo->outer_is_left = false;
2600 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2601 t_list = lappend(t_list, clause);
2602 restrictinfo->outer_is_left = true;
2609 * order_qual_clauses
2610 * Given a list of qual clauses that will all be evaluated at the same
2611 * plan node, sort the list into the order we want to check the quals
2614 * Ideally the order should be driven by a combination of execution cost and
2615 * selectivity, but it's not immediately clear how to account for both,
2616 * and given the uncertainty of the estimates the reliability of the decisions
2617 * would be doubtful anyway. So we just order by estimated per-tuple cost,
2618 * being careful not to change the order when (as is often the case) the
2619 * estimates are identical.
2621 * Although this will work on either bare clauses or RestrictInfos, it's
2622 * much faster to apply it to RestrictInfos, since it can re-use cost
2623 * information that is cached in RestrictInfos.
2625 * Note: some callers pass lists that contain entries that will later be
2626 * removed; this is the easiest way to let this routine see RestrictInfos
2627 * instead of bare clauses. It's OK because we only sort by cost, but
2628 * a cost/selectivity combination would likely do the wrong thing.
2631 order_qual_clauses(PlannerInfo *root, List *clauses)
2638 int nitems = list_length(clauses);
2644 /* No need to work hard for 0 or 1 clause */
2649 * Collect the items and costs into an array. This is to avoid repeated
2650 * cost_qual_eval work if the inputs aren't RestrictInfos.
2652 items = (QualItem *) palloc(nitems * sizeof(QualItem));
2654 foreach(lc, clauses)
2656 Node *clause = (Node *) lfirst(lc);
2659 cost_qual_eval_node(&qcost, clause, root);
2660 items[i].clause = clause;
2661 items[i].cost = qcost.per_tuple;
2666 * Sort. We don't use qsort() because it's not guaranteed stable for
2667 * equal keys. The expected number of entries is small enough that a
2668 * simple insertion sort should be good enough.
2670 for (i = 1; i < nitems; i++)
2672 QualItem newitem = items[i];
2675 /* insert newitem into the already-sorted subarray */
2676 for (j = i; j > 0; j--)
2678 if (newitem.cost >= items[j - 1].cost)
2680 items[j] = items[j - 1];
2685 /* Convert back to a list */
2687 for (i = 0; i < nitems; i++)
2688 result = lappend(result, items[i].clause);
2694 * Copy cost and size info from a Path node to the Plan node created from it.
2695 * The executor usually won't use this info, but it's needed by EXPLAIN.
2698 copy_path_costsize(Plan *dest, Path *src)
2702 dest->startup_cost = src->startup_cost;
2703 dest->total_cost = src->total_cost;
2704 dest->plan_rows = src->parent->rows;
2705 dest->plan_width = src->parent->width;
2709 dest->startup_cost = 0;
2710 dest->total_cost = 0;
2711 dest->plan_rows = 0;
2712 dest->plan_width = 0;
2717 * Copy cost and size info from a lower plan node to an inserted node.
2718 * (Most callers alter the info after copying it.)
2721 copy_plan_costsize(Plan *dest, Plan *src)
2725 dest->startup_cost = src->startup_cost;
2726 dest->total_cost = src->total_cost;
2727 dest->plan_rows = src->plan_rows;
2728 dest->plan_width = src->plan_width;
2732 dest->startup_cost = 0;
2733 dest->total_cost = 0;
2734 dest->plan_rows = 0;
2735 dest->plan_width = 0;
2740 /*****************************************************************************
2742 * PLAN NODE BUILDING ROUTINES
2744 * Some of these are exported because they are called to build plan nodes
2745 * in contexts where we're not deriving the plan node from a path node.
2747 *****************************************************************************/
2750 make_seqscan(List *qptlist,
2754 SeqScan *node = makeNode(SeqScan);
2755 Plan *plan = &node->plan;
2757 /* cost should be inserted by caller */
2758 plan->targetlist = qptlist;
2759 plan->qual = qpqual;
2760 plan->lefttree = NULL;
2761 plan->righttree = NULL;
2762 node->scanrelid = scanrelid;
2768 make_indexscan(List *qptlist,
2773 List *indexqualorig,
2775 List *indexorderbyorig,
2776 ScanDirection indexscandir)
2778 IndexScan *node = makeNode(IndexScan);
2779 Plan *plan = &node->scan.plan;
2781 /* cost should be inserted by caller */
2782 plan->targetlist = qptlist;
2783 plan->qual = qpqual;
2784 plan->lefttree = NULL;
2785 plan->righttree = NULL;
2786 node->scan.scanrelid = scanrelid;
2787 node->indexid = indexid;
2788 node->indexqual = indexqual;
2789 node->indexqualorig = indexqualorig;
2790 node->indexorderby = indexorderby;
2791 node->indexorderbyorig = indexorderbyorig;
2792 node->indexorderdir = indexscandir;
2797 static BitmapIndexScan *
2798 make_bitmap_indexscan(Index scanrelid,
2801 List *indexqualorig)
2803 BitmapIndexScan *node = makeNode(BitmapIndexScan);
2804 Plan *plan = &node->scan.plan;
2806 /* cost should be inserted by caller */
2807 plan->targetlist = NIL; /* not used */
2808 plan->qual = NIL; /* not used */
2809 plan->lefttree = NULL;
2810 plan->righttree = NULL;
2811 node->scan.scanrelid = scanrelid;
2812 node->indexid = indexid;
2813 node->indexqual = indexqual;
2814 node->indexqualorig = indexqualorig;
2819 static BitmapHeapScan *
2820 make_bitmap_heapscan(List *qptlist,
2823 List *bitmapqualorig,
2826 BitmapHeapScan *node = makeNode(BitmapHeapScan);
2827 Plan *plan = &node->scan.plan;
2829 /* cost should be inserted by caller */
2830 plan->targetlist = qptlist;
2831 plan->qual = qpqual;
2832 plan->lefttree = lefttree;
2833 plan->righttree = NULL;
2834 node->scan.scanrelid = scanrelid;
2835 node->bitmapqualorig = bitmapqualorig;
2841 make_tidscan(List *qptlist,
2846 TidScan *node = makeNode(TidScan);
2847 Plan *plan = &node->scan.plan;
2849 /* cost should be inserted by caller */
2850 plan->targetlist = qptlist;
2851 plan->qual = qpqual;
2852 plan->lefttree = NULL;
2853 plan->righttree = NULL;
2854 node->scan.scanrelid = scanrelid;
2855 node->tidquals = tidquals;
2861 make_subqueryscan(List *qptlist,
2868 SubqueryScan *node = makeNode(SubqueryScan);
2869 Plan *plan = &node->scan.plan;
2872 * Cost is figured here for the convenience of prepunion.c. Note this is
2873 * only correct for the case where qpqual is empty; otherwise caller
2874 * should overwrite cost with a better estimate.
2876 copy_plan_costsize(plan, subplan);
2877 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
2879 plan->targetlist = qptlist;
2880 plan->qual = qpqual;
2881 plan->lefttree = NULL;
2882 plan->righttree = NULL;
2883 node->scan.scanrelid = scanrelid;
2884 node->subplan = subplan;
2885 node->subrtable = subrtable;
2886 node->subrowmark = subrowmark;
2891 static FunctionScan *
2892 make_functionscan(List *qptlist,
2898 List *funccoltypmods,
2899 List *funccolcollations)
2901 FunctionScan *node = makeNode(FunctionScan);
2902 Plan *plan = &node->scan.plan;
2904 /* cost should be inserted by caller */
2905 plan->targetlist = qptlist;
2906 plan->qual = qpqual;
2907 plan->lefttree = NULL;
2908 plan->righttree = NULL;
2909 node->scan.scanrelid = scanrelid;
2910 node->funcexpr = funcexpr;
2911 node->funccolnames = funccolnames;
2912 node->funccoltypes = funccoltypes;
2913 node->funccoltypmods = funccoltypmods;
2914 node->funccolcollations = funccolcollations;
2920 make_valuesscan(List *qptlist,
2925 ValuesScan *node = makeNode(ValuesScan);
2926 Plan *plan = &node->scan.plan;
2928 /* cost should be inserted by caller */
2929 plan->targetlist = qptlist;
2930 plan->qual = qpqual;
2931 plan->lefttree = NULL;
2932 plan->righttree = NULL;
2933 node->scan.scanrelid = scanrelid;
2934 node->values_lists = values_lists;
2940 make_ctescan(List *qptlist,
2946 CteScan *node = makeNode(CteScan);
2947 Plan *plan = &node->scan.plan;
2949 /* cost should be inserted by caller */
2950 plan->targetlist = qptlist;
2951 plan->qual = qpqual;
2952 plan->lefttree = NULL;
2953 plan->righttree = NULL;
2954 node->scan.scanrelid = scanrelid;
2955 node->ctePlanId = ctePlanId;
2956 node->cteParam = cteParam;
2961 static WorkTableScan *
2962 make_worktablescan(List *qptlist,
2967 WorkTableScan *node = makeNode(WorkTableScan);
2968 Plan *plan = &node->scan.plan;
2970 /* cost should be inserted by caller */
2971 plan->targetlist = qptlist;
2972 plan->qual = qpqual;
2973 plan->lefttree = NULL;
2974 plan->righttree = NULL;
2975 node->scan.scanrelid = scanrelid;
2976 node->wtParam = wtParam;
2982 make_append(List *appendplans, List *tlist)
2984 Append *node = makeNode(Append);
2985 Plan *plan = &node->plan;
2990 * Compute cost as sum of subplan costs. We charge nothing extra for the
2991 * Append itself, which perhaps is too optimistic, but since it doesn't do
2992 * any selection or projection, it is a pretty cheap node.
2994 * If you change this, see also create_append_path(). Also, the size
2995 * calculations should match set_append_rel_pathlist(). It'd be better
2996 * not to duplicate all this logic, but some callers of this function
2997 * aren't working from an appendrel or AppendPath, so there's noplace
2998 * to copy the data from.
3000 plan->startup_cost = 0;
3001 plan->total_cost = 0;
3002 plan->plan_rows = 0;
3004 foreach(subnode, appendplans)
3006 Plan *subplan = (Plan *) lfirst(subnode);
3008 if (subnode == list_head(appendplans)) /* first node? */
3009 plan->startup_cost = subplan->startup_cost;
3010 plan->total_cost += subplan->total_cost;
3011 plan->plan_rows += subplan->plan_rows;
3012 total_size += subplan->plan_width * subplan->plan_rows;
3014 if (plan->plan_rows > 0)
3015 plan->plan_width = rint(total_size / plan->plan_rows);
3017 plan->plan_width = 0;
3019 plan->targetlist = tlist;
3021 plan->lefttree = NULL;
3022 plan->righttree = NULL;
3023 node->appendplans = appendplans;
3029 make_recursive_union(List *tlist,
3036 RecursiveUnion *node = makeNode(RecursiveUnion);
3037 Plan *plan = &node->plan;
3038 int numCols = list_length(distinctList);
3040 cost_recursive_union(plan, lefttree, righttree);
3042 plan->targetlist = tlist;
3044 plan->lefttree = lefttree;
3045 plan->righttree = righttree;
3046 node->wtParam = wtParam;
3049 * convert SortGroupClause list into arrays of attr indexes and equality
3050 * operators, as wanted by executor
3052 node->numCols = numCols;
3056 AttrNumber *dupColIdx;
3060 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3061 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3063 foreach(slitem, distinctList)
3065 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3066 TargetEntry *tle = get_sortgroupclause_tle(sortcl,
3069 dupColIdx[keyno] = tle->resno;
3070 dupOperators[keyno] = sortcl->eqop;
3071 Assert(OidIsValid(dupOperators[keyno]));
3074 node->dupColIdx = dupColIdx;
3075 node->dupOperators = dupOperators;
3077 node->numGroups = numGroups;
3083 make_bitmap_and(List *bitmapplans)
3085 BitmapAnd *node = makeNode(BitmapAnd);
3086 Plan *plan = &node->plan;
3088 /* cost should be inserted by caller */
3089 plan->targetlist = NIL;
3091 plan->lefttree = NULL;
3092 plan->righttree = NULL;
3093 node->bitmapplans = bitmapplans;
3099 make_bitmap_or(List *bitmapplans)
3101 BitmapOr *node = makeNode(BitmapOr);
3102 Plan *plan = &node->plan;
3104 /* cost should be inserted by caller */
3105 plan->targetlist = NIL;
3107 plan->lefttree = NULL;
3108 plan->righttree = NULL;
3109 node->bitmapplans = bitmapplans;
3115 make_nestloop(List *tlist,
3123 NestLoop *node = makeNode(NestLoop);
3124 Plan *plan = &node->join.plan;
3126 /* cost should be inserted by caller */
3127 plan->targetlist = tlist;
3128 plan->qual = otherclauses;
3129 plan->lefttree = lefttree;
3130 plan->righttree = righttree;
3131 node->join.jointype = jointype;
3132 node->join.joinqual = joinclauses;
3133 node->nestParams = nestParams;
3139 make_hashjoin(List *tlist,
3147 HashJoin *node = makeNode(HashJoin);
3148 Plan *plan = &node->join.plan;
3150 /* cost should be inserted by caller */
3151 plan->targetlist = tlist;
3152 plan->qual = otherclauses;
3153 plan->lefttree = lefttree;
3154 plan->righttree = righttree;
3155 node->hashclauses = hashclauses;
3156 node->join.jointype = jointype;
3157 node->join.joinqual = joinclauses;
3163 make_hash(Plan *lefttree,
3165 AttrNumber skewColumn,
3168 int32 skewColTypmod)
3170 Hash *node = makeNode(Hash);
3171 Plan *plan = &node->plan;
3173 copy_plan_costsize(plan, lefttree);
3176 * For plausibility, make startup & total costs equal total cost of input
3177 * plan; this only affects EXPLAIN display not decisions.
3179 plan->startup_cost = plan->total_cost;
3180 plan->targetlist = lefttree->targetlist;
3182 plan->lefttree = lefttree;
3183 plan->righttree = NULL;
3185 node->skewTable = skewTable;
3186 node->skewColumn = skewColumn;
3187 node->skewInherit = skewInherit;
3188 node->skewColType = skewColType;
3189 node->skewColTypmod = skewColTypmod;
3195 make_mergejoin(List *tlist,
3200 Oid *mergecollations,
3201 int *mergestrategies,
3202 bool *mergenullsfirst,
3207 MergeJoin *node = makeNode(MergeJoin);
3208 Plan *plan = &node->join.plan;
3210 /* cost should be inserted by caller */
3211 plan->targetlist = tlist;
3212 plan->qual = otherclauses;
3213 plan->lefttree = lefttree;
3214 plan->righttree = righttree;
3215 node->mergeclauses = mergeclauses;
3216 node->mergeFamilies = mergefamilies;
3217 node->mergeCollations = mergecollations;
3218 node->mergeStrategies = mergestrategies;
3219 node->mergeNullsFirst = mergenullsfirst;
3220 node->join.jointype = jointype;
3221 node->join.joinqual = joinclauses;
3227 * make_sort --- basic routine to build a Sort plan node
3229 * Caller must have built the sortColIdx, sortOperators, and nullsFirst
3230 * arrays already. limit_tuples is as for cost_sort (in particular, pass
3234 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
3235 AttrNumber *sortColIdx, Oid *sortOperators, Oid *collations, bool *nullsFirst,
3236 double limit_tuples)
3238 Sort *node = makeNode(Sort);
3239 Plan *plan = &node->plan;
3240 Path sort_path; /* dummy for result of cost_sort */
3242 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3243 cost_sort(&sort_path, root, NIL,
3244 lefttree->total_cost,
3245 lefttree->plan_rows,
3246 lefttree->plan_width,
3250 plan->startup_cost = sort_path.startup_cost;
3251 plan->total_cost = sort_path.total_cost;
3252 plan->targetlist = lefttree->targetlist;
3254 plan->lefttree = lefttree;
3255 plan->righttree = NULL;
3256 node->numCols = numCols;
3257 node->sortColIdx = sortColIdx;
3258 node->sortOperators = sortOperators;
3259 node->collations = collations;
3260 node->nullsFirst = nullsFirst;
3266 * add_sort_column --- utility subroutine for building sort info arrays
3268 * We need this routine because the same column might be selected more than
3269 * once as a sort key column; if so, the extra mentions are redundant.
3271 * Caller is assumed to have allocated the arrays large enough for the
3272 * max possible number of columns. Return value is the new column count.
3275 add_sort_column(AttrNumber colIdx, Oid sortOp, Oid coll, bool nulls_first,
3276 int numCols, AttrNumber *sortColIdx,
3277 Oid *sortOperators, Oid *collations, bool *nullsFirst)
3281 Assert(OidIsValid(sortOp));
3283 for (i = 0; i < numCols; i++)
3286 * Note: we check sortOp because it's conceivable that "ORDER BY foo
3287 * USING <, foo USING <<<" is not redundant, if <<< distinguishes
3288 * values that < considers equal. We need not check nulls_first
3289 * however because a lower-order column with the same sortop but
3290 * opposite nulls direction is redundant.
3292 if (sortColIdx[i] == colIdx &&
3293 sortOperators[numCols] == sortOp &&
3294 collations[numCols] == coll)
3296 /* Already sorting by this col, so extra sort key is useless */
3301 /* Add the column */
3302 sortColIdx[numCols] = colIdx;
3303 sortOperators[numCols] = sortOp;
3304 collations[numCols] = coll;
3305 nullsFirst[numCols] = nulls_first;
3310 * prepare_sort_from_pathkeys
3311 * Prepare to sort according to given pathkeys
3313 * This is used to set up for both Sort and MergeAppend nodes. It calculates
3314 * the executor's representation of the sort key information, and adjusts the
3315 * plan targetlist if needed to add resjunk sort columns.
3318 * 'lefttree' is the node which yields input tuples
3319 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3320 * 'adjust_tlist_in_place' is TRUE if lefttree must be modified in-place
3322 * We must convert the pathkey information into arrays of sort key column
3323 * numbers and sort operator OIDs, which is the representation the executor
3324 * wants. These are returned into the output parameters *p_numsortkeys etc.
3326 * If the pathkeys include expressions that aren't simple Vars, we will
3327 * usually need to add resjunk items to the input plan's targetlist to
3328 * compute these expressions, since the Sort/MergeAppend node itself won't
3329 * do any such calculations. If the input plan type isn't one that can do
3330 * projections, this means adding a Result node just to do the projection.
3331 * However, the caller can pass adjust_tlist_in_place = TRUE to force the
3332 * lefttree tlist to be modified in-place regardless of whether the node type
3333 * can project --- we use this for fixing the tlist of MergeAppend itself.
3335 * Returns the node which is to be the input to the Sort (either lefttree,
3336 * or a Result stacked atop lefttree).
3339 prepare_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3340 bool adjust_tlist_in_place,
3342 AttrNumber **p_sortColIdx,
3343 Oid **p_sortOperators,
3345 bool **p_nullsFirst)
3347 List *tlist = lefttree->targetlist;
3350 AttrNumber *sortColIdx;
3356 * We will need at most list_length(pathkeys) sort columns; possibly less
3358 numsortkeys = list_length(pathkeys);
3359 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3360 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3361 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3362 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3366 foreach(i, pathkeys)
3368 PathKey *pathkey = (PathKey *) lfirst(i);
3369 EquivalenceClass *ec = pathkey->pk_eclass;
3370 TargetEntry *tle = NULL;
3371 Oid pk_datatype = InvalidOid;
3375 if (ec->ec_has_volatile)
3378 * If the pathkey's EquivalenceClass is volatile, then it must
3379 * have come from an ORDER BY clause, and we have to match it to
3380 * that same targetlist entry.
3382 if (ec->ec_sortref == 0) /* can't happen */
3383 elog(ERROR, "volatile EquivalenceClass has no sortref");
3384 tle = get_sortgroupref_tle(ec->ec_sortref, tlist);
3386 Assert(list_length(ec->ec_members) == 1);
3387 pk_datatype = ((EquivalenceMember *) linitial(ec->ec_members))->em_datatype;
3392 * Otherwise, we can sort by any non-constant expression listed in
3393 * the pathkey's EquivalenceClass. For now, we take the first one
3394 * that corresponds to an available item in the tlist. If there
3395 * isn't any, use the first one that is an expression in the
3396 * input's vars. (The non-const restriction only matters if the
3397 * EC is below_outer_join; but if it isn't, it won't contain
3398 * consts anyway, else we'd have discarded the pathkey as
3401 * XXX if we have a choice, is there any way of figuring out which
3402 * might be cheapest to execute? (For example, int4lt is likely
3403 * much cheaper to execute than numericlt, but both might appear
3404 * in the same equivalence class...) Not clear that we ever will
3405 * have an interesting choice in practice, so it may not matter.
3407 foreach(j, ec->ec_members)
3409 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3412 * We shouldn't be trying to sort by an equivalence class that
3413 * contains a constant, so no need to consider such cases any
3416 if (em->em_is_const)
3419 tle = tlist_member((Node *) em->em_expr, tlist);
3422 pk_datatype = em->em_datatype;
3423 break; /* found expr already in tlist */
3427 * We can also use it if the pathkey expression is a relabel
3428 * of the tlist entry, or vice versa. This is needed for
3429 * binary-compatible cases (cf. make_pathkey_from_sortinfo).
3430 * We prefer an exact match, though, so we do the basic search
3433 tle = tlist_member_ignore_relabel((Node *) em->em_expr, tlist);
3436 pk_datatype = em->em_datatype;
3437 break; /* found expr already in tlist */
3443 /* No matching tlist item; look for a computable expression */
3444 Expr *sortexpr = NULL;
3446 foreach(j, ec->ec_members)
3448 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
3452 if (em->em_is_const)
3454 sortexpr = em->em_expr;
3455 exprvars = pull_var_clause((Node *) sortexpr,
3456 PVC_INCLUDE_PLACEHOLDERS);
3457 foreach(k, exprvars)
3459 if (!tlist_member_ignore_relabel(lfirst(k), tlist))
3462 list_free(exprvars);
3465 pk_datatype = em->em_datatype;
3466 break; /* found usable expression */
3470 elog(ERROR, "could not find pathkey item to sort");
3473 * Do we need to insert a Result node?
3475 if (!adjust_tlist_in_place &&
3476 !is_projection_capable_plan(lefttree))
3478 /* copy needed so we don't modify input's tlist below */
3479 tlist = copyObject(tlist);
3480 lefttree = (Plan *) make_result(root, tlist, NULL,
3484 /* Don't bother testing is_projection_capable_plan again */
3485 adjust_tlist_in_place = true;
3488 * Add resjunk entry to input's tlist
3490 tle = makeTargetEntry(sortexpr,
3491 list_length(tlist) + 1,
3494 tlist = lappend(tlist, tle);
3495 lefttree->targetlist = tlist; /* just in case NIL before */
3500 * Look up the correct sort operator from the PathKey's slightly
3501 * abstracted representation.
3503 sortop = get_opfamily_member(pathkey->pk_opfamily,
3506 pathkey->pk_strategy);
3507 if (!OidIsValid(sortop)) /* should not happen */
3508 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
3509 pathkey->pk_strategy, pk_datatype, pk_datatype,
3510 pathkey->pk_opfamily);
3513 * The column might already be selected as a sort key, if the pathkeys
3514 * contain duplicate entries. (This can happen in scenarios where
3515 * multiple mergejoinable clauses mention the same var, for example.)
3516 * So enter it only once in the sort arrays.
3518 numsortkeys = add_sort_column(tle->resno,
3520 pathkey->pk_collation,
3521 pathkey->pk_nulls_first,
3523 sortColIdx, sortOperators, collations, nullsFirst);
3526 Assert(numsortkeys > 0);
3528 /* Return results */
3529 *p_numsortkeys = numsortkeys;
3530 *p_sortColIdx = sortColIdx;
3531 *p_sortOperators = sortOperators;
3532 *p_collations = collations;
3533 *p_nullsFirst = nullsFirst;
3539 * make_sort_from_pathkeys
3540 * Create sort plan to sort according to given pathkeys
3542 * 'lefttree' is the node which yields input tuples
3543 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
3544 * 'limit_tuples' is the bound on the number of output tuples;
3548 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
3549 double limit_tuples)
3552 AttrNumber *sortColIdx;
3557 /* Compute sort column info, and adjust lefttree as needed */
3558 lefttree = prepare_sort_from_pathkeys(root, lefttree, pathkeys,
3566 /* Now build the Sort node */
3567 return make_sort(root, lefttree, numsortkeys,
3568 sortColIdx, sortOperators, collations, nullsFirst, limit_tuples);
3572 * make_sort_from_sortclauses
3573 * Create sort plan to sort according to given sortclauses
3575 * 'sortcls' is a list of SortGroupClauses
3576 * 'lefttree' is the node which yields input tuples
3579 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
3581 List *sub_tlist = lefttree->targetlist;
3584 AttrNumber *sortColIdx;
3590 * We will need at most list_length(sortcls) sort columns; possibly less
3592 numsortkeys = list_length(sortcls);
3593 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3594 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3595 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3596 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3602 SortGroupClause *sortcl = (SortGroupClause *) lfirst(l);
3603 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
3606 * Check for the possibility of duplicate order-by clauses --- the
3607 * parser should have removed 'em, but no point in sorting
3610 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
3611 exprCollation((Node *) tle->expr),
3612 sortcl->nulls_first,
3614 sortColIdx, sortOperators, collations, nullsFirst);
3617 Assert(numsortkeys > 0);
3619 return make_sort(root, lefttree, numsortkeys,
3620 sortColIdx, sortOperators, collations, nullsFirst, -1.0);
3624 * make_sort_from_groupcols
3625 * Create sort plan to sort based on grouping columns
3627 * 'groupcls' is the list of SortGroupClauses
3628 * 'grpColIdx' gives the column numbers to use
3630 * This might look like it could be merged with make_sort_from_sortclauses,
3631 * but presently we *must* use the grpColIdx[] array to locate sort columns,
3632 * because the child plan's tlist is not marked with ressortgroupref info
3633 * appropriate to the grouping node. So, only the sort ordering info
3634 * is used from the SortGroupClause entries.
3637 make_sort_from_groupcols(PlannerInfo *root,
3639 AttrNumber *grpColIdx,
3642 List *sub_tlist = lefttree->targetlist;
3646 AttrNumber *sortColIdx;
3652 * We will need at most list_length(groupcls) sort columns; possibly less
3654 numsortkeys = list_length(groupcls);
3655 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
3656 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
3657 collations = (Oid *) palloc(numsortkeys * sizeof(Oid));
3658 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
3662 foreach(l, groupcls)
3664 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
3665 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
3668 * Check for the possibility of duplicate group-by clauses --- the
3669 * parser should have removed 'em, but no point in sorting
3672 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
3673 exprCollation((Node *) tle->expr),
3676 sortColIdx, sortOperators, collations, nullsFirst);
3680 Assert(numsortkeys > 0);
3682 return make_sort(root, lefttree, numsortkeys,
3683 sortColIdx, sortOperators, collations, nullsFirst, -1.0);
3687 make_material(Plan *lefttree)
3689 Material *node = makeNode(Material);
3690 Plan *plan = &node->plan;
3692 /* cost should be inserted by caller */
3693 plan->targetlist = lefttree->targetlist;
3695 plan->lefttree = lefttree;
3696 plan->righttree = NULL;
3702 * materialize_finished_plan: stick a Material node atop a completed plan
3704 * There are a couple of places where we want to attach a Material node
3705 * after completion of subquery_planner(). This currently requires hackery.
3706 * Since subquery_planner has already run SS_finalize_plan on the subplan
3707 * tree, we have to kluge up parameter lists for the Material node.
3708 * Possibly this could be fixed by postponing SS_finalize_plan processing
3709 * until setrefs.c is run?
3712 materialize_finished_plan(Plan *subplan)
3715 Path matpath; /* dummy for result of cost_material */
3717 matplan = (Plan *) make_material(subplan);
3720 cost_material(&matpath,
3721 subplan->startup_cost,
3722 subplan->total_cost,
3724 subplan->plan_width);
3725 matplan->startup_cost = matpath.startup_cost;
3726 matplan->total_cost = matpath.total_cost;
3727 matplan->plan_rows = subplan->plan_rows;
3728 matplan->plan_width = subplan->plan_width;
3730 /* parameter kluge --- see comments above */
3731 matplan->extParam = bms_copy(subplan->extParam);
3732 matplan->allParam = bms_copy(subplan->allParam);
3738 make_agg(PlannerInfo *root, List *tlist, List *qual,
3739 AggStrategy aggstrategy,
3740 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
3741 long numGroups, int numAggs,
3744 Agg *node = makeNode(Agg);
3745 Plan *plan = &node->plan;
3746 Path agg_path; /* dummy for result of cost_agg */
3749 node->aggstrategy = aggstrategy;
3750 node->numCols = numGroupCols;
3751 node->grpColIdx = grpColIdx;
3752 node->grpOperators = grpOperators;
3753 node->numGroups = numGroups;
3755 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3756 cost_agg(&agg_path, root,
3757 aggstrategy, numAggs,
3758 numGroupCols, numGroups,
3759 lefttree->startup_cost,
3760 lefttree->total_cost,
3761 lefttree->plan_rows);
3762 plan->startup_cost = agg_path.startup_cost;
3763 plan->total_cost = agg_path.total_cost;
3766 * We will produce a single output tuple if not grouping, and a tuple per
3769 if (aggstrategy == AGG_PLAIN)
3770 plan->plan_rows = 1;
3772 plan->plan_rows = numGroups;
3775 * We also need to account for the cost of evaluation of the qual (ie, the
3776 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
3777 * anything for Aggref nodes; this is okay since they are really
3778 * comparable to Vars.
3780 * See notes in grouping_planner about why only make_agg, make_windowagg
3781 * and make_group worry about tlist eval cost.
3785 cost_qual_eval(&qual_cost, qual, root);
3786 plan->startup_cost += qual_cost.startup;
3787 plan->total_cost += qual_cost.startup;
3788 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3790 cost_qual_eval(&qual_cost, tlist, root);
3791 plan->startup_cost += qual_cost.startup;
3792 plan->total_cost += qual_cost.startup;
3793 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3796 plan->targetlist = tlist;
3797 plan->lefttree = lefttree;
3798 plan->righttree = NULL;
3804 make_windowagg(PlannerInfo *root, List *tlist,
3805 int numWindowFuncs, Index winref,
3806 int partNumCols, AttrNumber *partColIdx, Oid *partOperators,
3807 int ordNumCols, AttrNumber *ordColIdx, Oid *ordOperators,
3808 int frameOptions, Node *startOffset, Node *endOffset,
3811 WindowAgg *node = makeNode(WindowAgg);
3812 Plan *plan = &node->plan;
3813 Path windowagg_path; /* dummy for result of cost_windowagg */
3816 node->winref = winref;
3817 node->partNumCols = partNumCols;
3818 node->partColIdx = partColIdx;
3819 node->partOperators = partOperators;
3820 node->ordNumCols = ordNumCols;
3821 node->ordColIdx = ordColIdx;
3822 node->ordOperators = ordOperators;
3823 node->frameOptions = frameOptions;
3824 node->startOffset = startOffset;
3825 node->endOffset = endOffset;
3827 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3828 cost_windowagg(&windowagg_path, root,
3829 numWindowFuncs, partNumCols, ordNumCols,
3830 lefttree->startup_cost,
3831 lefttree->total_cost,
3832 lefttree->plan_rows);
3833 plan->startup_cost = windowagg_path.startup_cost;
3834 plan->total_cost = windowagg_path.total_cost;
3837 * We also need to account for the cost of evaluation of the tlist.
3839 * See notes in grouping_planner about why only make_agg, make_windowagg
3840 * and make_group worry about tlist eval cost.
3842 cost_qual_eval(&qual_cost, tlist, root);
3843 plan->startup_cost += qual_cost.startup;
3844 plan->total_cost += qual_cost.startup;
3845 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3847 plan->targetlist = tlist;
3848 plan->lefttree = lefttree;
3849 plan->righttree = NULL;
3850 /* WindowAgg nodes never have a qual clause */
3857 make_group(PlannerInfo *root,
3861 AttrNumber *grpColIdx,
3866 Group *node = makeNode(Group);
3867 Plan *plan = &node->plan;
3868 Path group_path; /* dummy for result of cost_group */
3871 node->numCols = numGroupCols;
3872 node->grpColIdx = grpColIdx;
3873 node->grpOperators = grpOperators;
3875 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3876 cost_group(&group_path, root,
3877 numGroupCols, numGroups,
3878 lefttree->startup_cost,
3879 lefttree->total_cost,
3880 lefttree->plan_rows);
3881 plan->startup_cost = group_path.startup_cost;
3882 plan->total_cost = group_path.total_cost;
3884 /* One output tuple per estimated result group */
3885 plan->plan_rows = numGroups;
3888 * We also need to account for the cost of evaluation of the qual (ie, the
3889 * HAVING clause) and the tlist.
3891 * XXX this double-counts the cost of evaluation of any expressions used
3892 * for grouping, since in reality those will have been evaluated at a
3893 * lower plan level and will only be copied by the Group node. Worth
3896 * See notes in grouping_planner about why only make_agg, make_windowagg
3897 * and make_group worry about tlist eval cost.
3901 cost_qual_eval(&qual_cost, qual, root);
3902 plan->startup_cost += qual_cost.startup;
3903 plan->total_cost += qual_cost.startup;
3904 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3906 cost_qual_eval(&qual_cost, tlist, root);
3907 plan->startup_cost += qual_cost.startup;
3908 plan->total_cost += qual_cost.startup;
3909 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3912 plan->targetlist = tlist;
3913 plan->lefttree = lefttree;
3914 plan->righttree = NULL;
3920 * distinctList is a list of SortGroupClauses, identifying the targetlist items
3921 * that should be considered by the Unique filter. The input path must
3922 * already be sorted accordingly.
3925 make_unique(Plan *lefttree, List *distinctList)
3927 Unique *node = makeNode(Unique);
3928 Plan *plan = &node->plan;
3929 int numCols = list_length(distinctList);
3931 AttrNumber *uniqColIdx;
3935 copy_plan_costsize(plan, lefttree);
3938 * Charge one cpu_operator_cost per comparison per input tuple. We assume
3939 * all columns get compared at most of the tuples. (XXX probably this is
3942 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3945 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
3946 * we assume the filter removes nothing. The caller must alter this if he
3947 * has a better idea.
3950 plan->targetlist = lefttree->targetlist;
3952 plan->lefttree = lefttree;
3953 plan->righttree = NULL;
3956 * convert SortGroupClause list into arrays of attr indexes and equality
3957 * operators, as wanted by executor
3959 Assert(numCols > 0);
3960 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3961 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3963 foreach(slitem, distinctList)
3965 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
3966 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3968 uniqColIdx[keyno] = tle->resno;
3969 uniqOperators[keyno] = sortcl->eqop;
3970 Assert(OidIsValid(uniqOperators[keyno]));
3974 node->numCols = numCols;
3975 node->uniqColIdx = uniqColIdx;
3976 node->uniqOperators = uniqOperators;
3982 * distinctList is a list of SortGroupClauses, identifying the targetlist
3983 * items that should be considered by the SetOp filter. The input path must
3984 * already be sorted accordingly.
3987 make_setop(SetOpCmd cmd, SetOpStrategy strategy, Plan *lefttree,
3988 List *distinctList, AttrNumber flagColIdx, int firstFlag,
3989 long numGroups, double outputRows)
3991 SetOp *node = makeNode(SetOp);
3992 Plan *plan = &node->plan;
3993 int numCols = list_length(distinctList);
3995 AttrNumber *dupColIdx;
3999 copy_plan_costsize(plan, lefttree);
4000 plan->plan_rows = outputRows;
4003 * Charge one cpu_operator_cost per comparison per input tuple. We assume
4004 * all columns get compared at most of the tuples.
4006 plan->total_cost += cpu_operator_cost * lefttree->plan_rows * numCols;
4008 plan->targetlist = lefttree->targetlist;
4010 plan->lefttree = lefttree;
4011 plan->righttree = NULL;
4014 * convert SortGroupClause list into arrays of attr indexes and equality
4015 * operators, as wanted by executor
4017 Assert(numCols > 0);
4018 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
4019 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
4021 foreach(slitem, distinctList)
4023 SortGroupClause *sortcl = (SortGroupClause *) lfirst(slitem);
4024 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
4026 dupColIdx[keyno] = tle->resno;
4027 dupOperators[keyno] = sortcl->eqop;
4028 Assert(OidIsValid(dupOperators[keyno]));
4033 node->strategy = strategy;
4034 node->numCols = numCols;
4035 node->dupColIdx = dupColIdx;
4036 node->dupOperators = dupOperators;
4037 node->flagColIdx = flagColIdx;
4038 node->firstFlag = firstFlag;
4039 node->numGroups = numGroups;
4046 * Build a LockRows plan node
4049 make_lockrows(Plan *lefttree, List *rowMarks, int epqParam)
4051 LockRows *node = makeNode(LockRows);
4052 Plan *plan = &node->plan;
4054 copy_plan_costsize(plan, lefttree);
4056 /* charge cpu_tuple_cost to reflect locking costs (underestimate?) */
4057 plan->total_cost += cpu_tuple_cost * plan->plan_rows;
4059 plan->targetlist = lefttree->targetlist;
4061 plan->lefttree = lefttree;
4062 plan->righttree = NULL;
4064 node->rowMarks = rowMarks;
4065 node->epqParam = epqParam;
4071 * Note: offset_est and count_est are passed in to save having to repeat
4072 * work already done to estimate the values of the limitOffset and limitCount
4073 * expressions. Their values are as returned by preprocess_limit (0 means
4074 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
4075 * with that function!
4078 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
4079 int64 offset_est, int64 count_est)
4081 Limit *node = makeNode(Limit);
4082 Plan *plan = &node->plan;
4084 copy_plan_costsize(plan, lefttree);
4087 * Adjust the output rows count and costs according to the offset/limit.
4088 * This is only a cosmetic issue if we are at top level, but if we are
4089 * building a subquery then it's important to report correct info to the
4092 * When the offset or count couldn't be estimated, use 10% of the
4093 * estimated number of rows emitted from the subplan.
4095 if (offset_est != 0)
4100 offset_rows = (double) offset_est;
4102 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4103 if (offset_rows > plan->plan_rows)
4104 offset_rows = plan->plan_rows;
4105 if (plan->plan_rows > 0)
4106 plan->startup_cost +=
4107 (plan->total_cost - plan->startup_cost)
4108 * offset_rows / plan->plan_rows;
4109 plan->plan_rows -= offset_rows;
4110 if (plan->plan_rows < 1)
4111 plan->plan_rows = 1;
4119 count_rows = (double) count_est;
4121 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
4122 if (count_rows > plan->plan_rows)
4123 count_rows = plan->plan_rows;
4124 if (plan->plan_rows > 0)
4125 plan->total_cost = plan->startup_cost +
4126 (plan->total_cost - plan->startup_cost)
4127 * count_rows / plan->plan_rows;
4128 plan->plan_rows = count_rows;
4129 if (plan->plan_rows < 1)
4130 plan->plan_rows = 1;
4133 plan->targetlist = lefttree->targetlist;
4135 plan->lefttree = lefttree;
4136 plan->righttree = NULL;
4138 node->limitOffset = limitOffset;
4139 node->limitCount = limitCount;
4146 * Build a Result plan node
4148 * If we have a subplan, assume that any evaluation costs for the gating qual
4149 * were already factored into the subplan's startup cost, and just copy the
4150 * subplan cost. If there's no subplan, we should include the qual eval
4151 * cost. In either case, tlist eval cost is not to be included here.
4154 make_result(PlannerInfo *root,
4156 Node *resconstantqual,
4159 Result *node = makeNode(Result);
4160 Plan *plan = &node->plan;
4163 copy_plan_costsize(plan, subplan);
4166 plan->startup_cost = 0;
4167 plan->total_cost = cpu_tuple_cost;
4168 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
4169 plan->plan_width = 0; /* XXX is it worth being smarter? */
4170 if (resconstantqual)
4174 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
4175 /* resconstantqual is evaluated once at startup */
4176 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
4177 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
4181 plan->targetlist = tlist;
4183 plan->lefttree = subplan;
4184 plan->righttree = NULL;
4185 node->resconstantqual = resconstantqual;
4192 * Build a ModifyTable plan node
4194 * Currently, we don't charge anything extra for the actual table modification
4195 * work, nor for the RETURNING expressions if any. It would only be window
4196 * dressing, since these are always top-level nodes and there is no way for
4197 * the costs to change any higher-level planning choices. But we might want
4198 * to make it look better sometime.
4201 make_modifytable(CmdType operation, List *resultRelations,
4202 List *subplans, List *returningLists,
4203 List *rowMarks, int epqParam)
4205 ModifyTable *node = makeNode(ModifyTable);
4206 Plan *plan = &node->plan;
4210 Assert(list_length(resultRelations) == list_length(subplans));
4211 Assert(returningLists == NIL ||
4212 list_length(resultRelations) == list_length(returningLists));
4215 * Compute cost as sum of subplan costs.
4217 plan->startup_cost = 0;
4218 plan->total_cost = 0;
4219 plan->plan_rows = 0;
4221 foreach(subnode, subplans)
4223 Plan *subplan = (Plan *) lfirst(subnode);
4225 if (subnode == list_head(subplans)) /* first node? */
4226 plan->startup_cost = subplan->startup_cost;
4227 plan->total_cost += subplan->total_cost;
4228 plan->plan_rows += subplan->plan_rows;
4229 total_size += subplan->plan_width * subplan->plan_rows;
4231 if (plan->plan_rows > 0)
4232 plan->plan_width = rint(total_size / plan->plan_rows);
4234 plan->plan_width = 0;
4236 node->plan.lefttree = NULL;
4237 node->plan.righttree = NULL;
4238 node->plan.qual = NIL;
4241 * Set up the visible plan targetlist as being the same as the first
4242 * RETURNING list. This is for the use of EXPLAIN; the executor won't pay
4243 * any attention to the targetlist.
4246 node->plan.targetlist = copyObject(linitial(returningLists));
4248 node->plan.targetlist = NIL;
4250 node->operation = operation;
4251 node->resultRelations = resultRelations;
4252 node->plans = subplans;
4253 node->returningLists = returningLists;
4254 node->rowMarks = rowMarks;
4255 node->epqParam = epqParam;
4261 * is_projection_capable_plan
4262 * Check whether a given Plan node is able to do projection.
4265 is_projection_capable_plan(Plan *plan)
4267 /* Most plan types can project, so just list the ones that can't */
4268 switch (nodeTag(plan))
4280 case T_RecursiveUnion: