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-2007, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $PostgreSQL: pgsql/src/backend/optimizer/plan/createplan.c,v 1.230 2007/05/04 01:13:44 tgl Exp $
15 *-------------------------------------------------------------------------
21 #include "access/skey.h"
22 #include "nodes/makefuncs.h"
23 #include "optimizer/clauses.h"
24 #include "optimizer/cost.h"
25 #include "optimizer/plancat.h"
26 #include "optimizer/planmain.h"
27 #include "optimizer/predtest.h"
28 #include "optimizer/restrictinfo.h"
29 #include "optimizer/tlist.h"
30 #include "optimizer/var.h"
31 #include "parser/parse_clause.h"
32 #include "parser/parse_expr.h"
33 #include "parser/parsetree.h"
34 #include "utils/lsyscache.h"
37 static Plan *create_scan_plan(PlannerInfo *root, Path *best_path);
38 static List *build_relation_tlist(RelOptInfo *rel);
39 static bool use_physical_tlist(RelOptInfo *rel);
40 static void disuse_physical_tlist(Plan *plan, Path *path);
41 static Plan *create_gating_plan(PlannerInfo *root, Plan *plan, List *quals);
42 static Plan *create_join_plan(PlannerInfo *root, JoinPath *best_path);
43 static Plan *create_append_plan(PlannerInfo *root, AppendPath *best_path);
44 static Result *create_result_plan(PlannerInfo *root, ResultPath *best_path);
45 static Material *create_material_plan(PlannerInfo *root, MaterialPath *best_path);
46 static Plan *create_unique_plan(PlannerInfo *root, UniquePath *best_path);
47 static SeqScan *create_seqscan_plan(PlannerInfo *root, Path *best_path,
48 List *tlist, List *scan_clauses);
49 static IndexScan *create_indexscan_plan(PlannerInfo *root, IndexPath *best_path,
50 List *tlist, List *scan_clauses,
51 List **nonlossy_clauses);
52 static BitmapHeapScan *create_bitmap_scan_plan(PlannerInfo *root,
53 BitmapHeapPath *best_path,
54 List *tlist, List *scan_clauses);
55 static Plan *create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
56 List **qual, List **indexqual);
57 static TidScan *create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
58 List *tlist, List *scan_clauses);
59 static SubqueryScan *create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
60 List *tlist, List *scan_clauses);
61 static FunctionScan *create_functionscan_plan(PlannerInfo *root, Path *best_path,
62 List *tlist, List *scan_clauses);
63 static ValuesScan *create_valuesscan_plan(PlannerInfo *root, Path *best_path,
64 List *tlist, List *scan_clauses);
65 static NestLoop *create_nestloop_plan(PlannerInfo *root, NestPath *best_path,
66 Plan *outer_plan, Plan *inner_plan);
67 static MergeJoin *create_mergejoin_plan(PlannerInfo *root, MergePath *best_path,
68 Plan *outer_plan, Plan *inner_plan);
69 static HashJoin *create_hashjoin_plan(PlannerInfo *root, HashPath *best_path,
70 Plan *outer_plan, Plan *inner_plan);
71 static void fix_indexqual_references(List *indexquals, IndexPath *index_path,
72 List **fixed_indexquals,
73 List **nonlossy_indexquals,
76 static Node *fix_indexqual_operand(Node *node, IndexOptInfo *index,
78 static List *get_switched_clauses(List *clauses, Relids outerrelids);
79 static List *order_qual_clauses(PlannerInfo *root, List *clauses);
80 static void copy_path_costsize(Plan *dest, Path *src);
81 static void copy_plan_costsize(Plan *dest, Plan *src);
82 static SeqScan *make_seqscan(List *qptlist, List *qpqual, Index scanrelid);
83 static IndexScan *make_indexscan(List *qptlist, List *qpqual, Index scanrelid,
84 Oid indexid, List *indexqual, List *indexqualorig,
85 List *indexstrategy, List *indexsubtype,
86 ScanDirection indexscandir);
87 static BitmapIndexScan *make_bitmap_indexscan(Index scanrelid, Oid indexid,
92 static BitmapHeapScan *make_bitmap_heapscan(List *qptlist,
97 static TidScan *make_tidscan(List *qptlist, List *qpqual, Index scanrelid,
99 static FunctionScan *make_functionscan(List *qptlist, List *qpqual,
100 Index scanrelid, Node *funcexpr, List *funccolnames,
101 List *funccoltypes, List *funccoltypmods);
102 static ValuesScan *make_valuesscan(List *qptlist, List *qpqual,
103 Index scanrelid, List *values_lists);
104 static BitmapAnd *make_bitmap_and(List *bitmapplans);
105 static BitmapOr *make_bitmap_or(List *bitmapplans);
106 static NestLoop *make_nestloop(List *tlist,
107 List *joinclauses, List *otherclauses,
108 Plan *lefttree, Plan *righttree,
110 static HashJoin *make_hashjoin(List *tlist,
111 List *joinclauses, List *otherclauses,
113 Plan *lefttree, Plan *righttree,
115 static Hash *make_hash(Plan *lefttree);
116 static MergeJoin *make_mergejoin(List *tlist,
117 List *joinclauses, List *otherclauses,
120 int *mergestrategies,
121 bool *mergenullsfirst,
122 Plan *lefttree, Plan *righttree,
124 static Sort *make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
125 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
126 double limit_tuples);
131 * Creates the access plan for a query by tracing backwards through the
132 * desired chain of pathnodes, starting at the node 'best_path'. For
133 * every pathnode found, we create a corresponding plan node containing
134 * appropriate id, target list, and qualification information.
136 * The tlists and quals in the plan tree are still in planner format,
137 * ie, Vars still correspond to the parser's numbering. This will be
138 * fixed later by setrefs.c.
140 * best_path is the best access path
142 * Returns a Plan tree.
145 create_plan(PlannerInfo *root, Path *best_path)
149 switch (best_path->pathtype)
153 case T_BitmapHeapScan:
158 plan = create_scan_plan(root, best_path);
163 plan = create_join_plan(root,
164 (JoinPath *) best_path);
167 plan = create_append_plan(root,
168 (AppendPath *) best_path);
171 plan = (Plan *) create_result_plan(root,
172 (ResultPath *) best_path);
175 plan = (Plan *) create_material_plan(root,
176 (MaterialPath *) best_path);
179 plan = create_unique_plan(root,
180 (UniquePath *) best_path);
183 elog(ERROR, "unrecognized node type: %d",
184 (int) best_path->pathtype);
185 plan = NULL; /* keep compiler quiet */
194 * Create a scan plan for the parent relation of 'best_path'.
197 create_scan_plan(PlannerInfo *root, Path *best_path)
199 RelOptInfo *rel = best_path->parent;
205 * For table scans, rather than using the relation targetlist (which is
206 * only those Vars actually needed by the query), we prefer to generate a
207 * tlist containing all Vars in order. This will allow the executor to
208 * optimize away projection of the table tuples, if possible. (Note that
209 * planner.c may replace the tlist we generate here, forcing projection to
212 if (use_physical_tlist(rel))
214 tlist = build_physical_tlist(root, rel);
215 /* if fail because of dropped cols, use regular method */
217 tlist = build_relation_tlist(rel);
220 tlist = build_relation_tlist(rel);
223 * Extract the relevant restriction clauses from the parent relation. The
224 * executor must apply all these restrictions during the scan, except for
225 * pseudoconstants which we'll take care of below.
227 scan_clauses = rel->baserestrictinfo;
229 switch (best_path->pathtype)
232 plan = (Plan *) create_seqscan_plan(root,
239 plan = (Plan *) create_indexscan_plan(root,
240 (IndexPath *) best_path,
246 case T_BitmapHeapScan:
247 plan = (Plan *) create_bitmap_scan_plan(root,
248 (BitmapHeapPath *) best_path,
254 plan = (Plan *) create_tidscan_plan(root,
255 (TidPath *) best_path,
261 plan = (Plan *) create_subqueryscan_plan(root,
268 plan = (Plan *) create_functionscan_plan(root,
275 plan = (Plan *) create_valuesscan_plan(root,
282 elog(ERROR, "unrecognized node type: %d",
283 (int) best_path->pathtype);
284 plan = NULL; /* keep compiler quiet */
289 * If there are any pseudoconstant clauses attached to this node, insert a
290 * gating Result node that evaluates the pseudoconstants as one-time
293 if (root->hasPseudoConstantQuals)
294 plan = create_gating_plan(root, plan, scan_clauses);
300 * Build a target list (ie, a list of TargetEntry) for a relation.
303 build_relation_tlist(RelOptInfo *rel)
309 foreach(v, rel->reltargetlist)
311 /* Do we really need to copy here? Not sure */
312 Var *var = (Var *) copyObject(lfirst(v));
314 tlist = lappend(tlist, makeTargetEntry((Expr *) var,
325 * Decide whether to use a tlist matching relation structure,
326 * rather than only those Vars actually referenced.
329 use_physical_tlist(RelOptInfo *rel)
334 * We can do this for real relation scans, subquery scans, function scans,
335 * and values scans (but not for, eg, joins).
337 if (rel->rtekind != RTE_RELATION &&
338 rel->rtekind != RTE_SUBQUERY &&
339 rel->rtekind != RTE_FUNCTION &&
340 rel->rtekind != RTE_VALUES)
344 * Can't do it with inheritance cases either (mainly because Append
347 if (rel->reloptkind != RELOPT_BASEREL)
351 * Can't do it if any system columns or whole-row Vars are requested,
352 * either. (This could possibly be fixed but would take some fragile
353 * assumptions in setrefs.c, I think.)
355 for (i = rel->min_attr; i <= 0; i++)
357 if (!bms_is_empty(rel->attr_needed[i - rel->min_attr]))
365 * disuse_physical_tlist
366 * Switch a plan node back to emitting only Vars actually referenced.
368 * If the plan node immediately above a scan would prefer to get only
369 * needed Vars and not a physical tlist, it must call this routine to
370 * undo the decision made by use_physical_tlist(). Currently, Hash, Sort,
371 * and Material nodes want this, so they don't have to store useless columns.
374 disuse_physical_tlist(Plan *plan, Path *path)
376 /* Only need to undo it for path types handled by create_scan_plan() */
377 switch (path->pathtype)
381 case T_BitmapHeapScan:
386 plan->targetlist = build_relation_tlist(path->parent);
395 * Deal with pseudoconstant qual clauses
397 * If the node's quals list includes any pseudoconstant quals, put them
398 * into a gating Result node atop the already-built plan. Otherwise,
399 * return the plan as-is.
401 * Note that we don't change cost or size estimates when doing gating.
402 * The costs of qual eval were already folded into the plan's startup cost.
403 * Leaving the size alone amounts to assuming that the gating qual will
404 * succeed, which is the conservative estimate for planning upper queries.
405 * We certainly don't want to assume the output size is zero (unless the
406 * gating qual is actually constant FALSE, and that case is dealt with in
407 * clausesel.c). Interpolating between the two cases is silly, because
408 * it doesn't reflect what will really happen at runtime, and besides which
409 * in most cases we have only a very bad idea of the probability of the gating
413 create_gating_plan(PlannerInfo *root, Plan *plan, List *quals)
415 List *pseudoconstants;
417 /* Sort into desirable execution order while still in RestrictInfo form */
418 quals = order_qual_clauses(root, quals);
420 /* Pull out any pseudoconstant quals from the RestrictInfo list */
421 pseudoconstants = extract_actual_clauses(quals, true);
423 if (!pseudoconstants)
426 return (Plan *) make_result(root,
428 (Node *) pseudoconstants,
434 * Create a join plan for 'best_path' and (recursively) plans for its
435 * inner and outer paths.
438 create_join_plan(PlannerInfo *root, JoinPath *best_path)
444 outer_plan = create_plan(root, best_path->outerjoinpath);
445 inner_plan = create_plan(root, best_path->innerjoinpath);
447 switch (best_path->path.pathtype)
450 plan = (Plan *) create_mergejoin_plan(root,
451 (MergePath *) best_path,
456 plan = (Plan *) create_hashjoin_plan(root,
457 (HashPath *) best_path,
462 plan = (Plan *) create_nestloop_plan(root,
463 (NestPath *) best_path,
468 elog(ERROR, "unrecognized node type: %d",
469 (int) best_path->path.pathtype);
470 plan = NULL; /* keep compiler quiet */
475 * If there are any pseudoconstant clauses attached to this node, insert a
476 * gating Result node that evaluates the pseudoconstants as one-time
479 if (root->hasPseudoConstantQuals)
480 plan = create_gating_plan(root, plan, best_path->joinrestrictinfo);
485 * * Expensive function pullups may have pulled local predicates * into
486 * this path node. Put them in the qpqual of the plan node. * JMH,
489 if (get_loc_restrictinfo(best_path) != NIL)
490 set_qpqual((Plan) plan,
491 list_concat(get_qpqual((Plan) plan),
492 get_actual_clauses(get_loc_restrictinfo(best_path))));
500 * Create an Append plan for 'best_path' and (recursively) plans
503 * Returns a Plan node.
506 create_append_plan(PlannerInfo *root, AppendPath *best_path)
509 List *tlist = build_relation_tlist(best_path->path.parent);
510 List *subplans = NIL;
514 * It is possible for the subplans list to contain only one entry, or even
515 * no entries. Handle these cases specially.
517 * XXX ideally, if there's just one entry, we'd not bother to generate an
518 * Append node but just return the single child. At the moment this does
519 * not work because the varno of the child scan plan won't match the
520 * parent-rel Vars it'll be asked to emit.
522 if (best_path->subpaths == NIL)
524 /* Generate a Result plan with constant-FALSE gating qual */
525 return (Plan *) make_result(root,
527 (Node *) list_make1(makeBoolConst(false,
532 /* Normal case with multiple subpaths */
533 foreach(subpaths, best_path->subpaths)
535 Path *subpath = (Path *) lfirst(subpaths);
537 subplans = lappend(subplans, create_plan(root, subpath));
540 plan = make_append(subplans, false, tlist);
542 return (Plan *) plan;
547 * Create a Result plan for 'best_path'.
548 * This is only used for the case of a query with an empty jointree.
550 * Returns a Plan node.
553 create_result_plan(PlannerInfo *root, ResultPath *best_path)
558 /* The tlist will be installed later, since we have no RelOptInfo */
559 Assert(best_path->path.parent == NULL);
562 /* best_path->quals is just bare clauses */
564 quals = order_qual_clauses(root, best_path->quals);
566 return make_result(root, tlist, (Node *) quals, NULL);
570 * create_material_plan
571 * Create a Material plan for 'best_path' and (recursively) plans
574 * Returns a Plan node.
577 create_material_plan(PlannerInfo *root, MaterialPath *best_path)
582 subplan = create_plan(root, best_path->subpath);
584 /* We don't want any excess columns in the materialized tuples */
585 disuse_physical_tlist(subplan, best_path->subpath);
587 plan = make_material(subplan);
589 copy_path_costsize(&plan->plan, (Path *) best_path);
596 * Create a Unique plan for 'best_path' and (recursively) plans
599 * Returns a Plan node.
602 create_unique_plan(PlannerInfo *root, UniquePath *best_path)
612 AttrNumber *groupColIdx;
616 subplan = create_plan(root, best_path->subpath);
618 /* Done if we don't need to do any actual unique-ifying */
619 if (best_path->umethod == UNIQUE_PATH_NOOP)
623 * As constructed, the subplan has a "flat" tlist containing just the
624 * Vars needed here and at upper levels. The values we are supposed
625 * to unique-ify may be expressions in these variables. We have to
626 * add any such expressions to the subplan's tlist.
628 * The subplan may have a "physical" tlist if it is a simple scan plan.
629 * This should be left as-is if we don't need to add any expressions;
630 * but if we do have to add expressions, then a projection step will be
631 * needed at runtime anyway, and so we may as well remove unneeded items.
632 * Therefore newtlist starts from build_relation_tlist() not just a
633 * copy of the subplan's tlist; and we don't install it into the subplan
634 * unless stuff has to be added.
636 * To find the correct list of values to unique-ify, we look in the
637 * information saved for IN expressions. If this code is ever used in
638 * other scenarios, some other way of finding what to unique-ify will
639 * be needed. The IN clause's operators are needed too, since they
640 * determine what the meaning of "unique" is in this context.
643 uniq_exprs = NIL; /* just to keep compiler quiet */
645 foreach(l, root->in_info_list)
647 InClauseInfo *ininfo = (InClauseInfo *) lfirst(l);
649 if (bms_equal(ininfo->righthand, best_path->path.parent->relids))
651 uniq_exprs = ininfo->sub_targetlist;
652 in_operators = ininfo->in_operators;
656 if (l == NULL) /* fell out of loop? */
657 elog(ERROR, "could not find UniquePath in in_info_list");
659 /* initialize modified subplan tlist as just the "required" vars */
660 newtlist = build_relation_tlist(best_path->path.parent);
661 nextresno = list_length(newtlist) + 1;
664 foreach(l, uniq_exprs)
666 Node *uniqexpr = lfirst(l);
669 tle = tlist_member(uniqexpr, newtlist);
672 tle = makeTargetEntry((Expr *) uniqexpr,
676 newtlist = lappend(newtlist, tle);
685 * If the top plan node can't do projections, we need to add a Result
686 * node to help it along.
688 if (!is_projection_capable_plan(subplan))
689 subplan = (Plan *) make_result(root, newtlist, NULL, subplan);
691 subplan->targetlist = newtlist;
695 * Build control information showing which subplan output columns are to
696 * be examined by the grouping step. Unfortunately we can't merge this
697 * with the previous loop, since we didn't then know which version of the
698 * subplan tlist we'd end up using.
700 newtlist = subplan->targetlist;
701 numGroupCols = list_length(uniq_exprs);
702 groupColIdx = (AttrNumber *) palloc(numGroupCols * sizeof(AttrNumber));
705 foreach(l, uniq_exprs)
707 Node *uniqexpr = lfirst(l);
710 tle = tlist_member(uniqexpr, newtlist);
711 if (!tle) /* shouldn't happen */
712 elog(ERROR, "failed to find unique expression in subplan tlist");
713 groupColIdx[groupColPos++] = tle->resno;
716 if (best_path->umethod == UNIQUE_PATH_HASH)
721 numGroups = (long) Min(best_path->rows, (double) LONG_MAX);
724 * Get the hashable equality operators for the Agg node to use.
725 * Normally these are the same as the IN clause operators, but if
726 * those are cross-type operators then the equality operators are
727 * the ones for the IN clause operators' RHS datatype.
729 groupOperators = (Oid *) palloc(numGroupCols * sizeof(Oid));
731 foreach(l, in_operators)
733 Oid in_oper = lfirst_oid(l);
736 if (!get_compatible_hash_operators(in_oper, NULL, &eq_oper))
737 elog(ERROR, "could not find compatible hash operator for operator %u",
739 groupOperators[groupColPos++] = eq_oper;
743 * Since the Agg node is going to project anyway, we can give it the
744 * minimum output tlist, without any stuff we might have added to the
747 plan = (Plan *) make_agg(root,
748 build_relation_tlist(best_path->path.parent),
760 List *sortList = NIL;
762 /* Create an ORDER BY list to sort the input compatibly */
764 foreach(l, in_operators)
766 Oid in_oper = lfirst_oid(l);
771 sortop = get_ordering_op_for_equality_op(in_oper, false);
772 if (!OidIsValid(sortop)) /* shouldn't happen */
773 elog(ERROR, "could not find ordering operator for equality operator %u",
775 tle = get_tle_by_resno(subplan->targetlist,
776 groupColIdx[groupColPos]);
778 sortcl = makeNode(SortClause);
779 sortcl->tleSortGroupRef = assignSortGroupRef(tle,
780 subplan->targetlist);
781 sortcl->sortop = sortop;
782 sortcl->nulls_first = false;
783 sortList = lappend(sortList, sortcl);
786 plan = (Plan *) make_sort_from_sortclauses(root, sortList, subplan);
787 plan = (Plan *) make_unique(plan, sortList);
790 /* Adjust output size estimate (other fields should be OK already) */
791 plan->plan_rows = best_path->rows;
797 /*****************************************************************************
799 * BASE-RELATION SCAN METHODS
801 *****************************************************************************/
805 * create_seqscan_plan
806 * Returns a seqscan plan for the base relation scanned by 'best_path'
807 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
810 create_seqscan_plan(PlannerInfo *root, Path *best_path,
811 List *tlist, List *scan_clauses)
814 Index scan_relid = best_path->parent->relid;
816 /* it should be a base rel... */
817 Assert(scan_relid > 0);
818 Assert(best_path->parent->rtekind == RTE_RELATION);
820 /* Sort clauses into best execution order */
821 scan_clauses = order_qual_clauses(root, scan_clauses);
823 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
824 scan_clauses = extract_actual_clauses(scan_clauses, false);
826 scan_plan = make_seqscan(tlist,
830 copy_path_costsize(&scan_plan->plan, best_path);
836 * create_indexscan_plan
837 * Returns an indexscan plan for the base relation scanned by 'best_path'
838 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
840 * The indexquals list of the path contains implicitly-ANDed qual conditions.
841 * The list can be empty --- then no index restrictions will be applied during
844 * If nonlossy_clauses isn't NULL, *nonlossy_clauses receives a list of the
845 * nonlossy indexquals.
848 create_indexscan_plan(PlannerInfo *root,
849 IndexPath *best_path,
852 List **nonlossy_clauses)
854 List *indexquals = best_path->indexquals;
855 Index baserelid = best_path->path.parent->relid;
856 Oid indexoid = best_path->indexinfo->indexoid;
858 List *stripped_indexquals;
859 List *fixed_indexquals;
860 List *nonlossy_indexquals;
864 IndexScan *scan_plan;
866 /* it should be a base rel... */
867 Assert(baserelid > 0);
868 Assert(best_path->path.parent->rtekind == RTE_RELATION);
871 * Build "stripped" indexquals structure (no RestrictInfos) to pass to
872 * executor as indexqualorig
874 stripped_indexquals = get_actual_clauses(indexquals);
877 * The executor needs a copy with the indexkey on the left of each clause
878 * and with index attr numbers substituted for table ones. This pass also
879 * gets strategy info and looks for "lossy" operators.
881 fix_indexqual_references(indexquals, best_path,
883 &nonlossy_indexquals,
887 /* pass back nonlossy quals if caller wants 'em */
888 if (nonlossy_clauses)
889 *nonlossy_clauses = nonlossy_indexquals;
892 * If this is an innerjoin scan, the indexclauses will contain join
893 * clauses that are not present in scan_clauses (since the passed-in value
894 * is just the rel's baserestrictinfo list). We must add these clauses to
895 * scan_clauses to ensure they get checked. In most cases we will remove
896 * the join clauses again below, but if a join clause contains a special
897 * operator, we need to make sure it gets into the scan_clauses.
899 * Note: pointer comparison should be enough to determine RestrictInfo
902 if (best_path->isjoininner)
903 scan_clauses = list_union_ptr(scan_clauses, best_path->indexclauses);
906 * The qpqual list must contain all restrictions not automatically handled
907 * by the index. All the predicates in the indexquals will be checked
908 * (either by the index itself, or by nodeIndexscan.c), but if there are
909 * any "special" operators involved then they must be included in qpqual.
910 * Also, any lossy index operators must be rechecked in the qpqual. The
911 * upshot is that qpqual must contain scan_clauses minus whatever appears
912 * in nonlossy_indexquals.
914 * In normal cases simple pointer equality checks will be enough to spot
915 * duplicate RestrictInfos, so we try that first. In some situations
916 * (particularly with OR'd index conditions) we may have scan_clauses that
917 * are not equal to, but are logically implied by, the index quals; so we
918 * also try a predicate_implied_by() check to see if we can discard quals
919 * that way. (predicate_implied_by assumes its first input contains only
920 * immutable functions, so we have to check that.)
922 * We can also discard quals that are implied by a partial index's
923 * predicate, but only in a plain SELECT; when scanning a target relation
924 * of UPDATE/DELETE/SELECT FOR UPDATE, we must leave such quals in the
925 * plan so that they'll be properly rechecked by EvalPlanQual testing.
928 foreach(l, scan_clauses)
930 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
932 Assert(IsA(rinfo, RestrictInfo));
933 if (rinfo->pseudoconstant)
934 continue; /* we may drop pseudoconstants here */
935 if (list_member_ptr(nonlossy_indexquals, rinfo))
937 if (!contain_mutable_functions((Node *) rinfo->clause))
939 List *clausel = list_make1(rinfo->clause);
941 if (predicate_implied_by(clausel, nonlossy_indexquals))
943 if (best_path->indexinfo->indpred)
945 if (baserelid != root->parse->resultRelation &&
946 get_rowmark(root->parse, baserelid) == NULL)
947 if (predicate_implied_by(clausel,
948 best_path->indexinfo->indpred))
952 qpqual = lappend(qpqual, rinfo);
955 /* Sort clauses into best execution order */
956 qpqual = order_qual_clauses(root, qpqual);
958 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
959 qpqual = extract_actual_clauses(qpqual, false);
961 /* Finally ready to build the plan node */
962 scan_plan = make_indexscan(tlist,
970 best_path->indexscandir);
972 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
973 /* use the indexscan-specific rows estimate, not the parent rel's */
974 scan_plan->scan.plan.plan_rows = best_path->rows;
980 * create_bitmap_scan_plan
981 * Returns a bitmap scan plan for the base relation scanned by 'best_path'
982 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
984 static BitmapHeapScan *
985 create_bitmap_scan_plan(PlannerInfo *root,
986 BitmapHeapPath *best_path,
990 Index baserelid = best_path->path.parent->relid;
991 Plan *bitmapqualplan;
992 List *bitmapqualorig;
996 BitmapHeapScan *scan_plan;
998 /* it should be a base rel... */
999 Assert(baserelid > 0);
1000 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1002 /* Process the bitmapqual tree into a Plan tree and qual lists */
1003 bitmapqualplan = create_bitmap_subplan(root, best_path->bitmapqual,
1004 &bitmapqualorig, &indexquals);
1006 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1007 scan_clauses = extract_actual_clauses(scan_clauses, false);
1010 * If this is a innerjoin scan, the indexclauses will contain join clauses
1011 * that are not present in scan_clauses (since the passed-in value is just
1012 * the rel's baserestrictinfo list). We must add these clauses to
1013 * scan_clauses to ensure they get checked. In most cases we will remove
1014 * the join clauses again below, but if a join clause contains a special
1015 * operator, we need to make sure it gets into the scan_clauses.
1017 if (best_path->isjoininner)
1019 scan_clauses = list_concat_unique(scan_clauses, bitmapqualorig);
1023 * The qpqual list must contain all restrictions not automatically handled
1024 * by the index. All the predicates in the indexquals will be checked
1025 * (either by the index itself, or by nodeBitmapHeapscan.c), but if there
1026 * are any "special" or lossy operators involved then they must be added
1027 * to qpqual. The upshot is that qpqual must contain scan_clauses minus
1028 * whatever appears in indexquals.
1030 * In normal cases simple equal() checks will be enough to spot duplicate
1031 * clauses, so we try that first. In some situations (particularly with
1032 * OR'd index conditions) we may have scan_clauses that are not equal to,
1033 * but are logically implied by, the index quals; so we also try a
1034 * predicate_implied_by() check to see if we can discard quals that way.
1035 * (predicate_implied_by assumes its first input contains only immutable
1036 * functions, so we have to check that.)
1038 * Unlike create_indexscan_plan(), we need take no special thought here
1039 * for partial index predicates; this is because the predicate conditions
1040 * are already listed in bitmapqualorig and indexquals. Bitmap scans have
1041 * to do it that way because predicate conditions need to be rechecked if
1042 * the scan becomes lossy.
1045 foreach(l, scan_clauses)
1047 Node *clause = (Node *) lfirst(l);
1049 if (list_member(indexquals, clause))
1051 if (!contain_mutable_functions(clause))
1053 List *clausel = list_make1(clause);
1055 if (predicate_implied_by(clausel, indexquals))
1058 qpqual = lappend(qpqual, clause);
1061 /* Sort clauses into best execution order */
1062 qpqual = order_qual_clauses(root, qpqual);
1065 * When dealing with special or lossy operators, we will at this point
1066 * have duplicate clauses in qpqual and bitmapqualorig. We may as well
1067 * drop 'em from bitmapqualorig, since there's no point in making the
1070 bitmapqualorig = list_difference_ptr(bitmapqualorig, qpqual);
1072 /* Finally ready to build the plan node */
1073 scan_plan = make_bitmap_heapscan(tlist,
1079 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1080 /* use the indexscan-specific rows estimate, not the parent rel's */
1081 scan_plan->scan.plan.plan_rows = best_path->rows;
1087 * Given a bitmapqual tree, generate the Plan tree that implements it
1089 * As byproducts, we also return in *qual and *indexqual the qual lists
1090 * (in implicit-AND form, without RestrictInfos) describing the original index
1091 * conditions and the generated indexqual conditions. The latter is made to
1092 * exclude lossy index operators. Both lists include partial-index predicates,
1093 * because we have to recheck predicates as well as index conditions if the
1094 * bitmap scan becomes lossy.
1096 * Note: if you find yourself changing this, you probably need to change
1097 * make_restrictinfo_from_bitmapqual too.
1100 create_bitmap_subplan(PlannerInfo *root, Path *bitmapqual,
1101 List **qual, List **indexqual)
1105 if (IsA(bitmapqual, BitmapAndPath))
1107 BitmapAndPath *apath = (BitmapAndPath *) bitmapqual;
1108 List *subplans = NIL;
1109 List *subquals = NIL;
1110 List *subindexquals = NIL;
1114 * There may well be redundant quals among the subplans, since a
1115 * top-level WHERE qual might have gotten used to form several
1116 * different index quals. We don't try exceedingly hard to eliminate
1117 * redundancies, but we do eliminate obvious duplicates by using
1118 * list_concat_unique.
1120 foreach(l, apath->bitmapquals)
1126 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1127 &subqual, &subindexqual);
1128 subplans = lappend(subplans, subplan);
1129 subquals = list_concat_unique(subquals, subqual);
1130 subindexquals = list_concat_unique(subindexquals, subindexqual);
1132 plan = (Plan *) make_bitmap_and(subplans);
1133 plan->startup_cost = apath->path.startup_cost;
1134 plan->total_cost = apath->path.total_cost;
1136 clamp_row_est(apath->bitmapselectivity * apath->path.parent->tuples);
1137 plan->plan_width = 0; /* meaningless */
1139 *indexqual = subindexquals;
1141 else if (IsA(bitmapqual, BitmapOrPath))
1143 BitmapOrPath *opath = (BitmapOrPath *) bitmapqual;
1144 List *subplans = NIL;
1145 List *subquals = NIL;
1146 List *subindexquals = NIL;
1147 bool const_true_subqual = false;
1148 bool const_true_subindexqual = false;
1152 * Here, we only detect qual-free subplans. A qual-free subplan would
1153 * cause us to generate "... OR true ..." which we may as well reduce
1154 * to just "true". We do not try to eliminate redundant subclauses
1155 * because (a) it's not as likely as in the AND case, and (b) we might
1156 * well be working with hundreds or even thousands of OR conditions,
1157 * perhaps from a long IN list. The performance of list_append_unique
1158 * would be unacceptable.
1160 foreach(l, opath->bitmapquals)
1166 subplan = create_bitmap_subplan(root, (Path *) lfirst(l),
1167 &subqual, &subindexqual);
1168 subplans = lappend(subplans, subplan);
1170 const_true_subqual = true;
1171 else if (!const_true_subqual)
1172 subquals = lappend(subquals,
1173 make_ands_explicit(subqual));
1174 if (subindexqual == NIL)
1175 const_true_subindexqual = true;
1176 else if (!const_true_subindexqual)
1177 subindexquals = lappend(subindexquals,
1178 make_ands_explicit(subindexqual));
1182 * In the presence of ScalarArrayOpExpr quals, we might have built
1183 * BitmapOrPaths with just one subpath; don't add an OR step.
1185 if (list_length(subplans) == 1)
1187 plan = (Plan *) linitial(subplans);
1191 plan = (Plan *) make_bitmap_or(subplans);
1192 plan->startup_cost = opath->path.startup_cost;
1193 plan->total_cost = opath->path.total_cost;
1195 clamp_row_est(opath->bitmapselectivity * opath->path.parent->tuples);
1196 plan->plan_width = 0; /* meaningless */
1200 * If there were constant-TRUE subquals, the OR reduces to constant
1201 * TRUE. Also, avoid generating one-element ORs, which could happen
1202 * due to redundancy elimination or ScalarArrayOpExpr quals.
1204 if (const_true_subqual)
1206 else if (list_length(subquals) <= 1)
1209 *qual = list_make1(make_orclause(subquals));
1210 if (const_true_subindexqual)
1212 else if (list_length(subindexquals) <= 1)
1213 *indexqual = subindexquals;
1215 *indexqual = list_make1(make_orclause(subindexquals));
1217 else if (IsA(bitmapqual, IndexPath))
1219 IndexPath *ipath = (IndexPath *) bitmapqual;
1221 List *nonlossy_clauses;
1224 /* Use the regular indexscan plan build machinery... */
1225 iscan = create_indexscan_plan(root, ipath, NIL, NIL,
1227 /* then convert to a bitmap indexscan */
1228 plan = (Plan *) make_bitmap_indexscan(iscan->scan.scanrelid,
1231 iscan->indexqualorig,
1232 iscan->indexstrategy,
1233 iscan->indexsubtype);
1234 plan->startup_cost = 0.0;
1235 plan->total_cost = ipath->indextotalcost;
1237 clamp_row_est(ipath->indexselectivity * ipath->path.parent->tuples);
1238 plan->plan_width = 0; /* meaningless */
1239 *qual = get_actual_clauses(ipath->indexclauses);
1240 *indexqual = get_actual_clauses(nonlossy_clauses);
1241 foreach(l, ipath->indexinfo->indpred)
1243 Expr *pred = (Expr *) lfirst(l);
1246 * We know that the index predicate must have been implied by the
1247 * query condition as a whole, but it may or may not be implied by
1248 * the conditions that got pushed into the bitmapqual. Avoid
1249 * generating redundant conditions.
1251 if (!predicate_implied_by(list_make1(pred), ipath->indexclauses))
1253 *qual = lappend(*qual, pred);
1254 *indexqual = lappend(*indexqual, pred);
1260 elog(ERROR, "unrecognized node type: %d", nodeTag(bitmapqual));
1261 plan = NULL; /* keep compiler quiet */
1268 * create_tidscan_plan
1269 * Returns a tidscan plan for the base relation scanned by 'best_path'
1270 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1273 create_tidscan_plan(PlannerInfo *root, TidPath *best_path,
1274 List *tlist, List *scan_clauses)
1277 Index scan_relid = best_path->path.parent->relid;
1280 /* it should be a base rel... */
1281 Assert(scan_relid > 0);
1282 Assert(best_path->path.parent->rtekind == RTE_RELATION);
1284 /* Sort clauses into best execution order */
1285 scan_clauses = order_qual_clauses(root, scan_clauses);
1287 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1288 scan_clauses = extract_actual_clauses(scan_clauses, false);
1291 * Remove any clauses that are TID quals. This is a bit tricky since the
1292 * tidquals list has implicit OR semantics.
1294 ortidquals = best_path->tidquals;
1295 if (list_length(ortidquals) > 1)
1296 ortidquals = list_make1(make_orclause(ortidquals));
1297 scan_clauses = list_difference(scan_clauses, ortidquals);
1299 scan_plan = make_tidscan(tlist,
1302 best_path->tidquals);
1304 copy_path_costsize(&scan_plan->scan.plan, &best_path->path);
1310 * create_subqueryscan_plan
1311 * Returns a subqueryscan plan for the base relation scanned by 'best_path'
1312 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1314 static SubqueryScan *
1315 create_subqueryscan_plan(PlannerInfo *root, Path *best_path,
1316 List *tlist, List *scan_clauses)
1318 SubqueryScan *scan_plan;
1319 Index scan_relid = best_path->parent->relid;
1321 /* it should be a subquery base rel... */
1322 Assert(scan_relid > 0);
1323 Assert(best_path->parent->rtekind == RTE_SUBQUERY);
1325 /* Sort clauses into best execution order */
1326 scan_clauses = order_qual_clauses(root, scan_clauses);
1328 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1329 scan_clauses = extract_actual_clauses(scan_clauses, false);
1331 scan_plan = make_subqueryscan(tlist,
1334 best_path->parent->subplan,
1335 best_path->parent->subrtable);
1337 copy_path_costsize(&scan_plan->scan.plan, best_path);
1343 * create_functionscan_plan
1344 * Returns a functionscan plan for the base relation scanned by 'best_path'
1345 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1347 static FunctionScan *
1348 create_functionscan_plan(PlannerInfo *root, Path *best_path,
1349 List *tlist, List *scan_clauses)
1351 FunctionScan *scan_plan;
1352 Index scan_relid = best_path->parent->relid;
1355 /* it should be a function base rel... */
1356 Assert(scan_relid > 0);
1357 rte = planner_rt_fetch(scan_relid, root);
1358 Assert(rte->rtekind == RTE_FUNCTION);
1360 /* Sort clauses into best execution order */
1361 scan_clauses = order_qual_clauses(root, scan_clauses);
1363 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1364 scan_clauses = extract_actual_clauses(scan_clauses, false);
1366 scan_plan = make_functionscan(tlist, scan_clauses, scan_relid,
1368 rte->eref->colnames,
1370 rte->funccoltypmods);
1372 copy_path_costsize(&scan_plan->scan.plan, best_path);
1378 * create_valuesscan_plan
1379 * Returns a valuesscan plan for the base relation scanned by 'best_path'
1380 * with restriction clauses 'scan_clauses' and targetlist 'tlist'.
1383 create_valuesscan_plan(PlannerInfo *root, Path *best_path,
1384 List *tlist, List *scan_clauses)
1386 ValuesScan *scan_plan;
1387 Index scan_relid = best_path->parent->relid;
1390 /* it should be a values base rel... */
1391 Assert(scan_relid > 0);
1392 rte = planner_rt_fetch(scan_relid, root);
1393 Assert(rte->rtekind == RTE_VALUES);
1395 /* Sort clauses into best execution order */
1396 scan_clauses = order_qual_clauses(root, scan_clauses);
1398 /* Reduce RestrictInfo list to bare expressions; ignore pseudoconstants */
1399 scan_clauses = extract_actual_clauses(scan_clauses, false);
1401 scan_plan = make_valuesscan(tlist, scan_clauses, scan_relid,
1404 copy_path_costsize(&scan_plan->scan.plan, best_path);
1409 /*****************************************************************************
1413 *****************************************************************************/
1416 create_nestloop_plan(PlannerInfo *root,
1417 NestPath *best_path,
1421 List *tlist = build_relation_tlist(best_path->path.parent);
1422 List *joinrestrictclauses = best_path->joinrestrictinfo;
1425 NestLoop *join_plan;
1427 if (IsA(best_path->innerjoinpath, IndexPath))
1430 * An index is being used to reduce the number of tuples scanned in
1431 * the inner relation. If there are join clauses being used with the
1432 * index, we may remove those join clauses from the list of clauses
1433 * that have to be checked as qpquals at the join node.
1435 * We can also remove any join clauses that are redundant with those
1436 * being used in the index scan; this check is needed because
1437 * find_eclass_clauses_for_index_join() may emit different clauses
1438 * than generate_join_implied_equalities() did.
1440 * We can skip this if the index path is an ordinary indexpath and not
1441 * a special innerjoin path, since it then wouldn't be using any join
1444 IndexPath *innerpath = (IndexPath *) best_path->innerjoinpath;
1446 if (innerpath->isjoininner)
1447 joinrestrictclauses =
1448 select_nonredundant_join_clauses(root,
1449 joinrestrictclauses,
1450 innerpath->indexclauses);
1452 else if (IsA(best_path->innerjoinpath, BitmapHeapPath))
1455 * Same deal for bitmapped index scans.
1457 * Note: both here and above, we ignore any implicit index
1458 * restrictions associated with the use of partial indexes. This is
1459 * OK because we're only trying to prove we can dispense with some
1460 * join quals; failing to prove that doesn't result in an incorrect
1461 * plan. It is the right way to proceed because adding more quals to
1462 * the stuff we got from the original query would just make it harder
1463 * to detect duplication. (Also, to change this we'd have to be wary
1464 * of UPDATE/DELETE/SELECT FOR UPDATE target relations; see notes
1465 * above about EvalPlanQual.)
1467 BitmapHeapPath *innerpath = (BitmapHeapPath *) best_path->innerjoinpath;
1469 if (innerpath->isjoininner)
1471 List *bitmapclauses;
1474 make_restrictinfo_from_bitmapqual(innerpath->bitmapqual,
1477 joinrestrictclauses =
1478 select_nonredundant_join_clauses(root,
1479 joinrestrictclauses,
1484 /* Sort join qual clauses into best execution order */
1485 joinrestrictclauses = order_qual_clauses(root, joinrestrictclauses);
1487 /* Get the join qual clauses (in plain expression form) */
1488 /* Any pseudoconstant clauses are ignored here */
1489 if (IS_OUTER_JOIN(best_path->jointype))
1491 extract_actual_join_clauses(joinrestrictclauses,
1492 &joinclauses, &otherclauses);
1496 /* We can treat all clauses alike for an inner join */
1497 joinclauses = extract_actual_clauses(joinrestrictclauses, false);
1501 join_plan = make_nestloop(tlist,
1506 best_path->jointype);
1508 copy_path_costsize(&join_plan->join.plan, &best_path->path);
1514 create_mergejoin_plan(PlannerInfo *root,
1515 MergePath *best_path,
1519 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1523 List *outerpathkeys;
1524 List *innerpathkeys;
1527 int *mergestrategies;
1528 bool *mergenullsfirst;
1529 MergeJoin *join_plan;
1531 EquivalenceClass *lastoeclass;
1532 EquivalenceClass *lastieclass;
1539 /* Sort join qual clauses into best execution order */
1540 /* NB: do NOT reorder the mergeclauses */
1541 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1543 /* Get the join qual clauses (in plain expression form) */
1544 /* Any pseudoconstant clauses are ignored here */
1545 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1547 extract_actual_join_clauses(joinclauses,
1548 &joinclauses, &otherclauses);
1552 /* We can treat all clauses alike for an inner join */
1553 joinclauses = extract_actual_clauses(joinclauses, false);
1558 * Remove the mergeclauses from the list of join qual clauses, leaving the
1559 * list of quals that must be checked as qpquals.
1561 mergeclauses = get_actual_clauses(best_path->path_mergeclauses);
1562 joinclauses = list_difference(joinclauses, mergeclauses);
1565 * Rearrange mergeclauses, if needed, so that the outer variable is always
1566 * on the left; mark the mergeclause restrictinfos with correct
1567 * outer_is_left status.
1569 mergeclauses = get_switched_clauses(best_path->path_mergeclauses,
1570 best_path->jpath.outerjoinpath->parent->relids);
1573 * Create explicit sort nodes for the outer and inner join paths if
1574 * necessary. The sort cost was already accounted for in the path. Make
1575 * sure there are no excess columns in the inputs if sorting.
1577 if (best_path->outersortkeys)
1579 disuse_physical_tlist(outer_plan, best_path->jpath.outerjoinpath);
1580 outer_plan = (Plan *)
1581 make_sort_from_pathkeys(root,
1583 best_path->outersortkeys,
1585 outerpathkeys = best_path->outersortkeys;
1588 outerpathkeys = best_path->jpath.outerjoinpath->pathkeys;
1590 if (best_path->innersortkeys)
1592 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1593 inner_plan = (Plan *)
1594 make_sort_from_pathkeys(root,
1596 best_path->innersortkeys,
1598 innerpathkeys = best_path->innersortkeys;
1601 innerpathkeys = best_path->jpath.innerjoinpath->pathkeys;
1604 * Compute the opfamily/strategy/nullsfirst arrays needed by the executor.
1605 * The information is in the pathkeys for the two inputs, but we need to
1606 * be careful about the possibility of mergeclauses sharing a pathkey
1607 * (compare find_mergeclauses_for_pathkeys()).
1609 nClauses = list_length(mergeclauses);
1610 Assert(nClauses == list_length(best_path->path_mergeclauses));
1611 mergefamilies = (Oid *) palloc(nClauses * sizeof(Oid));
1612 mergestrategies = (int *) palloc(nClauses * sizeof(int));
1613 mergenullsfirst = (bool *) palloc(nClauses * sizeof(bool));
1619 lop = list_head(outerpathkeys);
1620 lip = list_head(innerpathkeys);
1622 foreach(lc, best_path->path_mergeclauses)
1624 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1625 EquivalenceClass *oeclass;
1626 EquivalenceClass *ieclass;
1628 /* fetch outer/inner eclass from mergeclause */
1629 Assert(IsA(rinfo, RestrictInfo));
1630 if (rinfo->outer_is_left)
1632 oeclass = rinfo->left_ec;
1633 ieclass = rinfo->right_ec;
1637 oeclass = rinfo->right_ec;
1638 ieclass = rinfo->left_ec;
1640 Assert(oeclass != NULL);
1641 Assert(ieclass != NULL);
1643 /* should match current or next pathkeys */
1644 /* we check this carefully for debugging reasons */
1645 if (oeclass != lastoeclass)
1648 elog(ERROR, "too few pathkeys for mergeclauses");
1649 opathkey = (PathKey *) lfirst(lop);
1651 lastoeclass = opathkey->pk_eclass;
1652 if (oeclass != lastoeclass)
1653 elog(ERROR, "outer pathkeys do not match mergeclause");
1655 if (ieclass != lastieclass)
1658 elog(ERROR, "too few pathkeys for mergeclauses");
1659 ipathkey = (PathKey *) lfirst(lip);
1661 lastieclass = ipathkey->pk_eclass;
1662 if (ieclass != lastieclass)
1663 elog(ERROR, "inner pathkeys do not match mergeclause");
1665 /* pathkeys should match each other too (more debugging) */
1666 if (opathkey->pk_opfamily != ipathkey->pk_opfamily ||
1667 opathkey->pk_strategy != ipathkey->pk_strategy ||
1668 opathkey->pk_nulls_first != ipathkey->pk_nulls_first)
1669 elog(ERROR, "left and right pathkeys do not match in mergejoin");
1671 /* OK, save info for executor */
1672 mergefamilies[i] = opathkey->pk_opfamily;
1673 mergestrategies[i] = opathkey->pk_strategy;
1674 mergenullsfirst[i] = opathkey->pk_nulls_first;
1680 * Now we can build the mergejoin node.
1682 join_plan = make_mergejoin(tlist,
1691 best_path->jpath.jointype);
1693 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1699 create_hashjoin_plan(PlannerInfo *root,
1700 HashPath *best_path,
1704 List *tlist = build_relation_tlist(best_path->jpath.path.parent);
1708 HashJoin *join_plan;
1711 /* Sort join qual clauses into best execution order */
1712 joinclauses = order_qual_clauses(root, best_path->jpath.joinrestrictinfo);
1713 /* There's no point in sorting the hash clauses ... */
1715 /* Get the join qual clauses (in plain expression form) */
1716 /* Any pseudoconstant clauses are ignored here */
1717 if (IS_OUTER_JOIN(best_path->jpath.jointype))
1719 extract_actual_join_clauses(joinclauses,
1720 &joinclauses, &otherclauses);
1724 /* We can treat all clauses alike for an inner join */
1725 joinclauses = extract_actual_clauses(joinclauses, false);
1730 * Remove the hashclauses from the list of join qual clauses, leaving the
1731 * list of quals that must be checked as qpquals.
1733 hashclauses = get_actual_clauses(best_path->path_hashclauses);
1734 joinclauses = list_difference(joinclauses, hashclauses);
1737 * Rearrange hashclauses, if needed, so that the outer variable is always
1740 hashclauses = get_switched_clauses(best_path->path_hashclauses,
1741 best_path->jpath.outerjoinpath->parent->relids);
1743 /* We don't want any excess columns in the hashed tuples */
1744 disuse_physical_tlist(inner_plan, best_path->jpath.innerjoinpath);
1747 * Build the hash node and hash join node.
1749 hash_plan = make_hash(inner_plan);
1750 join_plan = make_hashjoin(tlist,
1756 best_path->jpath.jointype);
1758 copy_path_costsize(&join_plan->join.plan, &best_path->jpath.path);
1764 /*****************************************************************************
1766 * SUPPORTING ROUTINES
1768 *****************************************************************************/
1771 * fix_indexqual_references
1772 * Adjust indexqual clauses to the form the executor's indexqual
1773 * machinery needs, and check for recheckable (lossy) index conditions.
1775 * We have five tasks here:
1776 * * Remove RestrictInfo nodes from the input clauses.
1777 * * Index keys must be represented by Var nodes with varattno set to the
1778 * index's attribute number, not the attribute number in the original rel.
1779 * * If the index key is on the right, commute the clause to put it on the
1781 * * We must construct lists of operator strategy numbers and subtypes
1782 * for the top-level operators of each index clause.
1783 * * We must detect any lossy index operators. The API is that we return
1784 * a list of the input clauses whose operators are NOT lossy.
1786 * fixed_indexquals receives a modified copy of the indexquals list --- the
1787 * original is not changed. Note also that the copy shares no substructure
1788 * with the original; this is needed in case there is a subplan in it (we need
1789 * two separate copies of the subplan tree, or things will go awry).
1791 * nonlossy_indexquals receives a list of the original input clauses (with
1792 * RestrictInfos) that contain non-lossy operators.
1794 * indexstrategy receives an integer list of strategy numbers.
1795 * indexsubtype receives an OID list of strategy subtypes.
1798 fix_indexqual_references(List *indexquals, IndexPath *index_path,
1799 List **fixed_indexquals,
1800 List **nonlossy_indexquals,
1801 List **indexstrategy,
1802 List **indexsubtype)
1804 IndexOptInfo *index = index_path->indexinfo;
1807 *fixed_indexquals = NIL;
1808 *nonlossy_indexquals = NIL;
1809 *indexstrategy = NIL;
1810 *indexsubtype = NIL;
1813 * For each qual clause, commute if needed to put the indexkey operand on
1814 * the left, and then fix its varattno. (We do not need to change the
1815 * other side of the clause.) Then determine the operator's strategy
1816 * number and subtype number, and check for lossy index behavior.
1818 foreach(l, indexquals)
1820 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1828 bool is_null_op = false;
1830 Assert(IsA(rinfo, RestrictInfo));
1833 * Make a copy that will become the fixed clause.
1835 * We used to try to do a shallow copy here, but that fails if there
1836 * is a subplan in the arguments of the opclause. So just do a full
1839 clause = (Expr *) copyObject((Node *) rinfo->clause);
1841 if (IsA(clause, OpExpr))
1843 OpExpr *op = (OpExpr *) clause;
1845 if (list_length(op->args) != 2)
1846 elog(ERROR, "indexqual clause is not binary opclause");
1849 * Check to see if the indexkey is on the right; if so, commute
1850 * the clause. The indexkey should be the side that refers to
1851 * (only) the base relation.
1853 if (!bms_equal(rinfo->left_relids, index->rel->relids))
1857 * Now, determine which index attribute this is, change the
1858 * indexkey operand as needed, and get the index opfamily.
1860 linitial(op->args) = fix_indexqual_operand(linitial(op->args),
1863 clause_op = op->opno;
1865 else if (IsA(clause, RowCompareExpr))
1867 RowCompareExpr *rc = (RowCompareExpr *) clause;
1871 * Check to see if the indexkey is on the right; if so, commute
1872 * the clause. The indexkey should be the side that refers to
1873 * (only) the base relation.
1875 if (!bms_overlap(pull_varnos(linitial(rc->largs)),
1876 index->rel->relids))
1877 CommuteRowCompareExpr(rc);
1880 * For each column in the row comparison, determine which index
1881 * attribute this is and change the indexkey operand as needed.
1883 * Save the index opfamily for only the first column. We will
1884 * return the operator and opfamily info for just the first column
1885 * of the row comparison; the executor will have to look up the
1886 * rest if it needs them.
1888 foreach(lc, rc->largs)
1892 lfirst(lc) = fix_indexqual_operand(lfirst(lc),
1895 if (lc == list_head(rc->largs))
1896 opfamily = tmp_opfamily;
1898 clause_op = linitial_oid(rc->opnos);
1900 else if (IsA(clause, ScalarArrayOpExpr))
1902 ScalarArrayOpExpr *saop = (ScalarArrayOpExpr *) clause;
1904 /* Never need to commute... */
1907 * Now, determine which index attribute this is, change the
1908 * indexkey operand as needed, and get the index opfamily.
1910 linitial(saop->args) = fix_indexqual_operand(linitial(saop->args),
1913 clause_op = saop->opno;
1915 else if (IsA(clause, NullTest))
1917 NullTest *nt = (NullTest *) clause;
1919 Assert(nt->nulltesttype == IS_NULL);
1920 nt->arg = (Expr *) fix_indexqual_operand((Node *) nt->arg,
1924 clause_op = InvalidOid; /* keep compiler quiet */
1928 elog(ERROR, "unsupported indexqual type: %d",
1929 (int) nodeTag(clause));
1930 continue; /* keep compiler quiet */
1933 *fixed_indexquals = lappend(*fixed_indexquals, clause);
1937 /* IS NULL doesn't have a clause_op */
1938 stratno = InvalidStrategy;
1939 stratrighttype = InvalidOid;
1940 /* We assume it's non-lossy ... might need more work someday */
1946 * Look up the (possibly commuted) operator in the operator family
1947 * to get its strategy number and the recheck indicator. This also
1948 * double-checks that we found an operator matching the index.
1950 get_op_opfamily_properties(clause_op, opfamily,
1957 *indexstrategy = lappend_int(*indexstrategy, stratno);
1958 *indexsubtype = lappend_oid(*indexsubtype, stratrighttype);
1960 /* If it's not lossy, add to nonlossy_indexquals */
1962 *nonlossy_indexquals = lappend(*nonlossy_indexquals, rinfo);
1967 fix_indexqual_operand(Node *node, IndexOptInfo *index, Oid *opfamily)
1970 * We represent index keys by Var nodes having the varno of the base table
1971 * but varattno equal to the index's attribute number (index column
1972 * position). This is a bit hokey ... would be cleaner to use a
1973 * special-purpose node type that could not be mistaken for a regular Var.
1974 * But it will do for now.
1978 ListCell *indexpr_item;
1981 * Remove any binary-compatible relabeling of the indexkey
1983 if (IsA(node, RelabelType))
1984 node = (Node *) ((RelabelType *) node)->arg;
1986 if (IsA(node, Var) &&
1987 ((Var *) node)->varno == index->rel->relid)
1989 /* Try to match against simple index columns */
1990 int varatt = ((Var *) node)->varattno;
1994 for (pos = 0; pos < index->ncolumns; pos++)
1996 if (index->indexkeys[pos] == varatt)
1998 result = (Var *) copyObject(node);
1999 result->varattno = pos + 1;
2000 /* return the correct opfamily, too */
2001 *opfamily = index->opfamily[pos];
2002 return (Node *) result;
2008 /* Try to match against index expressions */
2009 indexpr_item = list_head(index->indexprs);
2010 for (pos = 0; pos < index->ncolumns; pos++)
2012 if (index->indexkeys[pos] == 0)
2016 if (indexpr_item == NULL)
2017 elog(ERROR, "too few entries in indexprs list");
2018 indexkey = (Node *) lfirst(indexpr_item);
2019 if (indexkey && IsA(indexkey, RelabelType))
2020 indexkey = (Node *) ((RelabelType *) indexkey)->arg;
2021 if (equal(node, indexkey))
2024 result = makeVar(index->rel->relid, pos + 1,
2025 exprType(lfirst(indexpr_item)), -1,
2027 /* return the correct opfamily, too */
2028 *opfamily = index->opfamily[pos];
2029 return (Node *) result;
2031 indexpr_item = lnext(indexpr_item);
2036 elog(ERROR, "node is not an index attribute");
2037 *opfamily = InvalidOid; /* keep compiler quiet */
2042 * get_switched_clauses
2043 * Given a list of merge or hash joinclauses (as RestrictInfo nodes),
2044 * extract the bare clauses, and rearrange the elements within the
2045 * clauses, if needed, so the outer join variable is on the left and
2046 * the inner is on the right. The original clause data structure is not
2047 * touched; a modified list is returned. We do, however, set the transient
2048 * outer_is_left field in each RestrictInfo to show which side was which.
2051 get_switched_clauses(List *clauses, Relids outerrelids)
2058 RestrictInfo *restrictinfo = (RestrictInfo *) lfirst(l);
2059 OpExpr *clause = (OpExpr *) restrictinfo->clause;
2061 Assert(is_opclause(clause));
2062 if (bms_is_subset(restrictinfo->right_relids, outerrelids))
2065 * Duplicate just enough of the structure to allow commuting the
2066 * clause without changing the original list. Could use
2067 * copyObject, but a complete deep copy is overkill.
2069 OpExpr *temp = makeNode(OpExpr);
2071 temp->opno = clause->opno;
2072 temp->opfuncid = InvalidOid;
2073 temp->opresulttype = clause->opresulttype;
2074 temp->opretset = clause->opretset;
2075 temp->args = list_copy(clause->args);
2076 /* Commute it --- note this modifies the temp node in-place. */
2077 CommuteOpExpr(temp);
2078 t_list = lappend(t_list, temp);
2079 restrictinfo->outer_is_left = false;
2083 Assert(bms_is_subset(restrictinfo->left_relids, outerrelids));
2084 t_list = lappend(t_list, clause);
2085 restrictinfo->outer_is_left = true;
2092 * order_qual_clauses
2093 * Given a list of qual clauses that will all be evaluated at the same
2094 * plan node, sort the list into the order we want to check the quals
2097 * Ideally the order should be driven by a combination of execution cost and
2098 * selectivity, but it's not immediately clear how to account for both,
2099 * and given the uncertainty of the estimates the reliability of the decisions
2100 * would be doubtful anyway. So we just order by estimated per-tuple cost,
2101 * being careful not to change the order when (as is often the case) the
2102 * estimates are identical.
2104 * Although this will work on either bare clauses or RestrictInfos, it's
2105 * much faster to apply it to RestrictInfos, since it can re-use cost
2106 * information that is cached in RestrictInfos.
2108 * Note: some callers pass lists that contain entries that will later be
2109 * removed; this is the easiest way to let this routine see RestrictInfos
2110 * instead of bare clauses. It's OK because we only sort by cost, but
2111 * a cost/selectivity combination would likely do the wrong thing.
2114 order_qual_clauses(PlannerInfo *root, List *clauses)
2121 int nitems = list_length(clauses);
2127 /* No need to work hard for 0 or 1 clause */
2132 * Collect the items and costs into an array. This is to avoid repeated
2133 * cost_qual_eval work if the inputs aren't RestrictInfos.
2135 items = (QualItem *) palloc(nitems * sizeof(QualItem));
2137 foreach(lc, clauses)
2139 Node *clause = (Node *) lfirst(lc);
2142 cost_qual_eval_node(&qcost, clause, root);
2143 items[i].clause = clause;
2144 items[i].cost = qcost.per_tuple;
2149 * Sort. We don't use qsort() because it's not guaranteed stable for
2150 * equal keys. The expected number of entries is small enough that
2151 * a simple insertion sort should be good enough.
2153 for (i = 1; i < nitems; i++)
2155 QualItem newitem = items[i];
2158 /* insert newitem into the already-sorted subarray */
2159 for (j = i; j > 0; j--)
2161 if (newitem.cost >= items[j-1].cost)
2163 items[j] = items[j-1];
2168 /* Convert back to a list */
2170 for (i = 0; i < nitems; i++)
2171 result = lappend(result, items[i].clause);
2177 * Copy cost and size info from a Path node to the Plan node created from it.
2178 * The executor won't use this info, but it's needed by EXPLAIN.
2181 copy_path_costsize(Plan *dest, Path *src)
2185 dest->startup_cost = src->startup_cost;
2186 dest->total_cost = src->total_cost;
2187 dest->plan_rows = src->parent->rows;
2188 dest->plan_width = src->parent->width;
2192 dest->startup_cost = 0;
2193 dest->total_cost = 0;
2194 dest->plan_rows = 0;
2195 dest->plan_width = 0;
2200 * Copy cost and size info from a lower plan node to an inserted node.
2201 * This is not critical, since the decisions have already been made,
2202 * but it helps produce more reasonable-looking EXPLAIN output.
2203 * (Some callers alter the info after copying it.)
2206 copy_plan_costsize(Plan *dest, Plan *src)
2210 dest->startup_cost = src->startup_cost;
2211 dest->total_cost = src->total_cost;
2212 dest->plan_rows = src->plan_rows;
2213 dest->plan_width = src->plan_width;
2217 dest->startup_cost = 0;
2218 dest->total_cost = 0;
2219 dest->plan_rows = 0;
2220 dest->plan_width = 0;
2225 /*****************************************************************************
2227 * PLAN NODE BUILDING ROUTINES
2229 * Some of these are exported because they are called to build plan nodes
2230 * in contexts where we're not deriving the plan node from a path node.
2232 *****************************************************************************/
2235 make_seqscan(List *qptlist,
2239 SeqScan *node = makeNode(SeqScan);
2240 Plan *plan = &node->plan;
2242 /* cost should be inserted by caller */
2243 plan->targetlist = qptlist;
2244 plan->qual = qpqual;
2245 plan->lefttree = NULL;
2246 plan->righttree = NULL;
2247 node->scanrelid = scanrelid;
2253 make_indexscan(List *qptlist,
2258 List *indexqualorig,
2259 List *indexstrategy,
2261 ScanDirection indexscandir)
2263 IndexScan *node = makeNode(IndexScan);
2264 Plan *plan = &node->scan.plan;
2266 /* cost should be inserted by caller */
2267 plan->targetlist = qptlist;
2268 plan->qual = qpqual;
2269 plan->lefttree = NULL;
2270 plan->righttree = NULL;
2271 node->scan.scanrelid = scanrelid;
2272 node->indexid = indexid;
2273 node->indexqual = indexqual;
2274 node->indexqualorig = indexqualorig;
2275 node->indexstrategy = indexstrategy;
2276 node->indexsubtype = indexsubtype;
2277 node->indexorderdir = indexscandir;
2282 static BitmapIndexScan *
2283 make_bitmap_indexscan(Index scanrelid,
2286 List *indexqualorig,
2287 List *indexstrategy,
2290 BitmapIndexScan *node = makeNode(BitmapIndexScan);
2291 Plan *plan = &node->scan.plan;
2293 /* cost should be inserted by caller */
2294 plan->targetlist = NIL; /* not used */
2295 plan->qual = NIL; /* not used */
2296 plan->lefttree = NULL;
2297 plan->righttree = NULL;
2298 node->scan.scanrelid = scanrelid;
2299 node->indexid = indexid;
2300 node->indexqual = indexqual;
2301 node->indexqualorig = indexqualorig;
2302 node->indexstrategy = indexstrategy;
2303 node->indexsubtype = indexsubtype;
2308 static BitmapHeapScan *
2309 make_bitmap_heapscan(List *qptlist,
2312 List *bitmapqualorig,
2315 BitmapHeapScan *node = makeNode(BitmapHeapScan);
2316 Plan *plan = &node->scan.plan;
2318 /* cost should be inserted by caller */
2319 plan->targetlist = qptlist;
2320 plan->qual = qpqual;
2321 plan->lefttree = lefttree;
2322 plan->righttree = NULL;
2323 node->scan.scanrelid = scanrelid;
2324 node->bitmapqualorig = bitmapqualorig;
2330 make_tidscan(List *qptlist,
2335 TidScan *node = makeNode(TidScan);
2336 Plan *plan = &node->scan.plan;
2338 /* cost should be inserted by caller */
2339 plan->targetlist = qptlist;
2340 plan->qual = qpqual;
2341 plan->lefttree = NULL;
2342 plan->righttree = NULL;
2343 node->scan.scanrelid = scanrelid;
2344 node->tidquals = tidquals;
2350 make_subqueryscan(List *qptlist,
2356 SubqueryScan *node = makeNode(SubqueryScan);
2357 Plan *plan = &node->scan.plan;
2360 * Cost is figured here for the convenience of prepunion.c. Note this is
2361 * only correct for the case where qpqual is empty; otherwise caller
2362 * should overwrite cost with a better estimate.
2364 copy_plan_costsize(plan, subplan);
2365 plan->total_cost += cpu_tuple_cost * subplan->plan_rows;
2367 plan->targetlist = qptlist;
2368 plan->qual = qpqual;
2369 plan->lefttree = NULL;
2370 plan->righttree = NULL;
2371 node->scan.scanrelid = scanrelid;
2372 node->subplan = subplan;
2373 node->subrtable = subrtable;
2378 static FunctionScan *
2379 make_functionscan(List *qptlist,
2385 List *funccoltypmods)
2387 FunctionScan *node = makeNode(FunctionScan);
2388 Plan *plan = &node->scan.plan;
2390 /* cost should be inserted by caller */
2391 plan->targetlist = qptlist;
2392 plan->qual = qpqual;
2393 plan->lefttree = NULL;
2394 plan->righttree = NULL;
2395 node->scan.scanrelid = scanrelid;
2396 node->funcexpr = funcexpr;
2397 node->funccolnames = funccolnames;
2398 node->funccoltypes = funccoltypes;
2399 node->funccoltypmods = funccoltypmods;
2405 make_valuesscan(List *qptlist,
2410 ValuesScan *node = makeNode(ValuesScan);
2411 Plan *plan = &node->scan.plan;
2413 /* cost should be inserted by caller */
2414 plan->targetlist = qptlist;
2415 plan->qual = qpqual;
2416 plan->lefttree = NULL;
2417 plan->righttree = NULL;
2418 node->scan.scanrelid = scanrelid;
2419 node->values_lists = values_lists;
2425 make_append(List *appendplans, bool isTarget, List *tlist)
2427 Append *node = makeNode(Append);
2428 Plan *plan = &node->plan;
2432 * Compute cost as sum of subplan costs. We charge nothing extra for the
2433 * Append itself, which perhaps is too optimistic, but since it doesn't do
2434 * any selection or projection, it is a pretty cheap node.
2436 plan->startup_cost = 0;
2437 plan->total_cost = 0;
2438 plan->plan_rows = 0;
2439 plan->plan_width = 0;
2440 foreach(subnode, appendplans)
2442 Plan *subplan = (Plan *) lfirst(subnode);
2444 if (subnode == list_head(appendplans)) /* first node? */
2445 plan->startup_cost = subplan->startup_cost;
2446 plan->total_cost += subplan->total_cost;
2447 plan->plan_rows += subplan->plan_rows;
2448 if (plan->plan_width < subplan->plan_width)
2449 plan->plan_width = subplan->plan_width;
2452 plan->targetlist = tlist;
2454 plan->lefttree = NULL;
2455 plan->righttree = NULL;
2456 node->appendplans = appendplans;
2457 node->isTarget = isTarget;
2463 make_bitmap_and(List *bitmapplans)
2465 BitmapAnd *node = makeNode(BitmapAnd);
2466 Plan *plan = &node->plan;
2468 /* cost should be inserted by caller */
2469 plan->targetlist = NIL;
2471 plan->lefttree = NULL;
2472 plan->righttree = NULL;
2473 node->bitmapplans = bitmapplans;
2479 make_bitmap_or(List *bitmapplans)
2481 BitmapOr *node = makeNode(BitmapOr);
2482 Plan *plan = &node->plan;
2484 /* cost should be inserted by caller */
2485 plan->targetlist = NIL;
2487 plan->lefttree = NULL;
2488 plan->righttree = NULL;
2489 node->bitmapplans = bitmapplans;
2495 make_nestloop(List *tlist,
2502 NestLoop *node = makeNode(NestLoop);
2503 Plan *plan = &node->join.plan;
2505 /* cost should be inserted by caller */
2506 plan->targetlist = tlist;
2507 plan->qual = otherclauses;
2508 plan->lefttree = lefttree;
2509 plan->righttree = righttree;
2510 node->join.jointype = jointype;
2511 node->join.joinqual = joinclauses;
2517 make_hashjoin(List *tlist,
2525 HashJoin *node = makeNode(HashJoin);
2526 Plan *plan = &node->join.plan;
2528 /* cost should be inserted by caller */
2529 plan->targetlist = tlist;
2530 plan->qual = otherclauses;
2531 plan->lefttree = lefttree;
2532 plan->righttree = righttree;
2533 node->hashclauses = hashclauses;
2534 node->join.jointype = jointype;
2535 node->join.joinqual = joinclauses;
2541 make_hash(Plan *lefttree)
2543 Hash *node = makeNode(Hash);
2544 Plan *plan = &node->plan;
2546 copy_plan_costsize(plan, lefttree);
2549 * For plausibility, make startup & total costs equal total cost of input
2550 * plan; this only affects EXPLAIN display not decisions.
2552 plan->startup_cost = plan->total_cost;
2553 plan->targetlist = lefttree->targetlist;
2555 plan->lefttree = lefttree;
2556 plan->righttree = NULL;
2562 make_mergejoin(List *tlist,
2567 int *mergestrategies,
2568 bool *mergenullsfirst,
2573 MergeJoin *node = makeNode(MergeJoin);
2574 Plan *plan = &node->join.plan;
2576 /* cost should be inserted by caller */
2577 plan->targetlist = tlist;
2578 plan->qual = otherclauses;
2579 plan->lefttree = lefttree;
2580 plan->righttree = righttree;
2581 node->mergeclauses = mergeclauses;
2582 node->mergeFamilies = mergefamilies;
2583 node->mergeStrategies = mergestrategies;
2584 node->mergeNullsFirst = mergenullsfirst;
2585 node->join.jointype = jointype;
2586 node->join.joinqual = joinclauses;
2592 * make_sort --- basic routine to build a Sort plan node
2594 * Caller must have built the sortColIdx, sortOperators, and nullsFirst
2595 * arrays already. limit_tuples is as for cost_sort (in particular, pass
2599 make_sort(PlannerInfo *root, Plan *lefttree, int numCols,
2600 AttrNumber *sortColIdx, Oid *sortOperators, bool *nullsFirst,
2601 double limit_tuples)
2603 Sort *node = makeNode(Sort);
2604 Plan *plan = &node->plan;
2605 Path sort_path; /* dummy for result of cost_sort */
2607 copy_plan_costsize(plan, lefttree); /* only care about copying size */
2608 cost_sort(&sort_path, root, NIL,
2609 lefttree->total_cost,
2610 lefttree->plan_rows,
2611 lefttree->plan_width,
2613 plan->startup_cost = sort_path.startup_cost;
2614 plan->total_cost = sort_path.total_cost;
2615 plan->targetlist = lefttree->targetlist;
2617 plan->lefttree = lefttree;
2618 plan->righttree = NULL;
2619 node->numCols = numCols;
2620 node->sortColIdx = sortColIdx;
2621 node->sortOperators = sortOperators;
2622 node->nullsFirst = nullsFirst;
2628 * add_sort_column --- utility subroutine for building sort info arrays
2630 * We need this routine because the same column might be selected more than
2631 * once as a sort key column; if so, the extra mentions are redundant.
2633 * Caller is assumed to have allocated the arrays large enough for the
2634 * max possible number of columns. Return value is the new column count.
2637 add_sort_column(AttrNumber colIdx, Oid sortOp, bool nulls_first,
2638 int numCols, AttrNumber *sortColIdx,
2639 Oid *sortOperators, bool *nullsFirst)
2643 for (i = 0; i < numCols; i++)
2646 * Note: we check sortOp because it's conceivable that "ORDER BY
2647 * foo USING <, foo USING <<<" is not redundant, if <<< distinguishes
2648 * values that < considers equal. We need not check nulls_first
2649 * however because a lower-order column with the same sortop but
2650 * opposite nulls direction is redundant.
2652 if (sortColIdx[i] == colIdx &&
2653 sortOperators[numCols] == sortOp)
2655 /* Already sorting by this col, so extra sort key is useless */
2660 /* Add the column */
2661 sortColIdx[numCols] = colIdx;
2662 sortOperators[numCols] = sortOp;
2663 nullsFirst[numCols] = nulls_first;
2668 * make_sort_from_pathkeys
2669 * Create sort plan to sort according to given pathkeys
2671 * 'lefttree' is the node which yields input tuples
2672 * 'pathkeys' is the list of pathkeys by which the result is to be sorted
2673 * 'limit_tuples' is the bound on the number of output tuples;
2676 * We must convert the pathkey information into arrays of sort key column
2677 * numbers and sort operator OIDs.
2679 * If the pathkeys include expressions that aren't simple Vars, we will
2680 * usually need to add resjunk items to the input plan's targetlist to
2681 * compute these expressions (since the Sort node itself won't do it).
2682 * If the input plan type isn't one that can do projections, this means
2683 * adding a Result node just to do the projection.
2686 make_sort_from_pathkeys(PlannerInfo *root, Plan *lefttree, List *pathkeys,
2687 double limit_tuples)
2689 List *tlist = lefttree->targetlist;
2692 AttrNumber *sortColIdx;
2697 * We will need at most list_length(pathkeys) sort columns; possibly less
2699 numsortkeys = list_length(pathkeys);
2700 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2701 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2702 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2706 foreach(i, pathkeys)
2708 PathKey *pathkey = (PathKey *) lfirst(i);
2709 TargetEntry *tle = NULL;
2710 Oid pk_datatype = InvalidOid;
2715 * We can sort by any non-constant expression listed in the pathkey's
2716 * EquivalenceClass. For now, we take the first one that corresponds
2717 * to an available Var in the tlist. If there isn't any, use the first
2718 * one that is an expression in the input's vars. (The non-const
2719 * restriction only matters if the EC is below_outer_join; but if it
2720 * isn't, it won't contain consts anyway, else we'd have discarded
2721 * the pathkey as redundant.)
2723 * XXX if we have a choice, is there any way of figuring out which
2724 * might be cheapest to execute? (For example, int4lt is likely much
2725 * cheaper to execute than numericlt, but both might appear in the
2726 * same equivalence class...) Not clear that we ever will have an
2727 * interesting choice in practice, so it may not matter.
2729 foreach(j, pathkey->pk_eclass->ec_members)
2731 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
2733 if (em->em_is_const || em->em_is_child)
2735 tle = tlist_member((Node *) em->em_expr, tlist);
2738 pk_datatype = em->em_datatype;
2739 break; /* found expr already in tlist */
2744 /* No matching Var; look for a computable expression */
2745 Expr *sortexpr = NULL;
2747 foreach(j, pathkey->pk_eclass->ec_members)
2749 EquivalenceMember *em = (EquivalenceMember *) lfirst(j);
2753 if (em->em_is_const || em->em_is_child)
2755 sortexpr = em->em_expr;
2756 exprvars = pull_var_clause((Node *) sortexpr, false);
2757 foreach(k, exprvars)
2759 if (!tlist_member(lfirst(k), tlist))
2762 list_free(exprvars);
2765 pk_datatype = em->em_datatype;
2766 break; /* found usable expression */
2770 elog(ERROR, "could not find pathkey item to sort");
2773 * Do we need to insert a Result node?
2775 if (!is_projection_capable_plan(lefttree))
2777 /* copy needed so we don't modify input's tlist below */
2778 tlist = copyObject(tlist);
2779 lefttree = (Plan *) make_result(root, tlist, NULL, lefttree);
2783 * Add resjunk entry to input's tlist
2785 tle = makeTargetEntry(sortexpr,
2786 list_length(tlist) + 1,
2789 tlist = lappend(tlist, tle);
2790 lefttree->targetlist = tlist; /* just in case NIL before */
2794 * Look up the correct sort operator from the PathKey's slightly
2795 * abstracted representation.
2797 sortop = get_opfamily_member(pathkey->pk_opfamily,
2800 pathkey->pk_strategy);
2801 if (!OidIsValid(sortop)) /* should not happen */
2802 elog(ERROR, "could not find member %d(%u,%u) of opfamily %u",
2803 pathkey->pk_strategy, pk_datatype, pk_datatype,
2804 pathkey->pk_opfamily);
2807 * The column might already be selected as a sort key, if the pathkeys
2808 * contain duplicate entries. (This can happen in scenarios where
2809 * multiple mergejoinable clauses mention the same var, for example.)
2810 * So enter it only once in the sort arrays.
2812 numsortkeys = add_sort_column(tle->resno,
2814 pathkey->pk_nulls_first,
2816 sortColIdx, sortOperators, nullsFirst);
2819 Assert(numsortkeys > 0);
2821 return make_sort(root, lefttree, numsortkeys,
2822 sortColIdx, sortOperators, nullsFirst, limit_tuples);
2826 * make_sort_from_sortclauses
2827 * Create sort plan to sort according to given sortclauses
2829 * 'sortcls' is a list of SortClauses
2830 * 'lefttree' is the node which yields input tuples
2833 make_sort_from_sortclauses(PlannerInfo *root, List *sortcls, Plan *lefttree)
2835 List *sub_tlist = lefttree->targetlist;
2838 AttrNumber *sortColIdx;
2843 * We will need at most list_length(sortcls) sort columns; possibly less
2845 numsortkeys = list_length(sortcls);
2846 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2847 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2848 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2854 SortClause *sortcl = (SortClause *) lfirst(l);
2855 TargetEntry *tle = get_sortgroupclause_tle(sortcl, sub_tlist);
2858 * Check for the possibility of duplicate order-by clauses --- the
2859 * parser should have removed 'em, but no point in sorting
2862 numsortkeys = add_sort_column(tle->resno, sortcl->sortop,
2863 sortcl->nulls_first,
2865 sortColIdx, sortOperators, nullsFirst);
2868 Assert(numsortkeys > 0);
2870 return make_sort(root, lefttree, numsortkeys,
2871 sortColIdx, sortOperators, nullsFirst, -1.0);
2875 * make_sort_from_groupcols
2876 * Create sort plan to sort based on grouping columns
2878 * 'groupcls' is the list of GroupClauses
2879 * 'grpColIdx' gives the column numbers to use
2881 * This might look like it could be merged with make_sort_from_sortclauses,
2882 * but presently we *must* use the grpColIdx[] array to locate sort columns,
2883 * because the child plan's tlist is not marked with ressortgroupref info
2884 * appropriate to the grouping node. So, only the sort ordering info
2885 * is used from the GroupClause entries.
2888 make_sort_from_groupcols(PlannerInfo *root,
2890 AttrNumber *grpColIdx,
2893 List *sub_tlist = lefttree->targetlist;
2897 AttrNumber *sortColIdx;
2902 * We will need at most list_length(groupcls) sort columns; possibly less
2904 numsortkeys = list_length(groupcls);
2905 sortColIdx = (AttrNumber *) palloc(numsortkeys * sizeof(AttrNumber));
2906 sortOperators = (Oid *) palloc(numsortkeys * sizeof(Oid));
2907 nullsFirst = (bool *) palloc(numsortkeys * sizeof(bool));
2911 foreach(l, groupcls)
2913 GroupClause *grpcl = (GroupClause *) lfirst(l);
2914 TargetEntry *tle = get_tle_by_resno(sub_tlist, grpColIdx[grpno]);
2917 * Check for the possibility of duplicate group-by clauses --- the
2918 * parser should have removed 'em, but no point in sorting
2921 numsortkeys = add_sort_column(tle->resno, grpcl->sortop,
2924 sortColIdx, sortOperators, nullsFirst);
2928 Assert(numsortkeys > 0);
2930 return make_sort(root, lefttree, numsortkeys,
2931 sortColIdx, sortOperators, nullsFirst, -1.0);
2935 make_material(Plan *lefttree)
2937 Material *node = makeNode(Material);
2938 Plan *plan = &node->plan;
2940 /* cost should be inserted by caller */
2941 plan->targetlist = lefttree->targetlist;
2943 plan->lefttree = lefttree;
2944 plan->righttree = NULL;
2950 * materialize_finished_plan: stick a Material node atop a completed plan
2952 * There are a couple of places where we want to attach a Material node
2953 * after completion of subquery_planner(). This currently requires hackery.
2954 * Since subquery_planner has already run SS_finalize_plan on the subplan
2955 * tree, we have to kluge up parameter lists for the Material node.
2956 * Possibly this could be fixed by postponing SS_finalize_plan processing
2957 * until setrefs.c is run?
2960 materialize_finished_plan(Plan *subplan)
2963 Path matpath; /* dummy for result of cost_material */
2965 matplan = (Plan *) make_material(subplan);
2968 cost_material(&matpath,
2969 subplan->total_cost,
2971 subplan->plan_width);
2972 matplan->startup_cost = matpath.startup_cost;
2973 matplan->total_cost = matpath.total_cost;
2974 matplan->plan_rows = subplan->plan_rows;
2975 matplan->plan_width = subplan->plan_width;
2977 /* parameter kluge --- see comments above */
2978 matplan->extParam = bms_copy(subplan->extParam);
2979 matplan->allParam = bms_copy(subplan->allParam);
2985 make_agg(PlannerInfo *root, List *tlist, List *qual,
2986 AggStrategy aggstrategy,
2987 int numGroupCols, AttrNumber *grpColIdx, Oid *grpOperators,
2988 long numGroups, int numAggs,
2991 Agg *node = makeNode(Agg);
2992 Plan *plan = &node->plan;
2993 Path agg_path; /* dummy for result of cost_agg */
2996 node->aggstrategy = aggstrategy;
2997 node->numCols = numGroupCols;
2998 node->grpColIdx = grpColIdx;
2999 node->grpOperators = grpOperators;
3000 node->numGroups = numGroups;
3002 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3003 cost_agg(&agg_path, root,
3004 aggstrategy, numAggs,
3005 numGroupCols, numGroups,
3006 lefttree->startup_cost,
3007 lefttree->total_cost,
3008 lefttree->plan_rows);
3009 plan->startup_cost = agg_path.startup_cost;
3010 plan->total_cost = agg_path.total_cost;
3013 * We will produce a single output tuple if not grouping, and a tuple per
3016 if (aggstrategy == AGG_PLAIN)
3017 plan->plan_rows = 1;
3019 plan->plan_rows = numGroups;
3022 * We also need to account for the cost of evaluation of the qual (ie, the
3023 * HAVING clause) and the tlist. Note that cost_qual_eval doesn't charge
3024 * anything for Aggref nodes; this is okay since they are really
3025 * comparable to Vars.
3027 * See notes in grouping_planner about why this routine and make_group are
3028 * the only ones in this file that worry about tlist eval cost.
3032 cost_qual_eval(&qual_cost, qual, root);
3033 plan->startup_cost += qual_cost.startup;
3034 plan->total_cost += qual_cost.startup;
3035 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3037 cost_qual_eval(&qual_cost, tlist, root);
3038 plan->startup_cost += qual_cost.startup;
3039 plan->total_cost += qual_cost.startup;
3040 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3043 plan->targetlist = tlist;
3044 plan->lefttree = lefttree;
3045 plan->righttree = NULL;
3051 make_group(PlannerInfo *root,
3055 AttrNumber *grpColIdx,
3060 Group *node = makeNode(Group);
3061 Plan *plan = &node->plan;
3062 Path group_path; /* dummy for result of cost_group */
3065 node->numCols = numGroupCols;
3066 node->grpColIdx = grpColIdx;
3067 node->grpOperators = grpOperators;
3069 copy_plan_costsize(plan, lefttree); /* only care about copying size */
3070 cost_group(&group_path, root,
3071 numGroupCols, numGroups,
3072 lefttree->startup_cost,
3073 lefttree->total_cost,
3074 lefttree->plan_rows);
3075 plan->startup_cost = group_path.startup_cost;
3076 plan->total_cost = group_path.total_cost;
3078 /* One output tuple per estimated result group */
3079 plan->plan_rows = numGroups;
3082 * We also need to account for the cost of evaluation of the qual (ie, the
3083 * HAVING clause) and the tlist.
3085 * XXX this double-counts the cost of evaluation of any expressions used
3086 * for grouping, since in reality those will have been evaluated at a
3087 * lower plan level and will only be copied by the Group node. Worth
3090 * See notes in grouping_planner about why this routine and make_agg are
3091 * the only ones in this file that worry about tlist eval cost.
3095 cost_qual_eval(&qual_cost, qual, root);
3096 plan->startup_cost += qual_cost.startup;
3097 plan->total_cost += qual_cost.startup;
3098 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3100 cost_qual_eval(&qual_cost, tlist, root);
3101 plan->startup_cost += qual_cost.startup;
3102 plan->total_cost += qual_cost.startup;
3103 plan->total_cost += qual_cost.per_tuple * plan->plan_rows;
3106 plan->targetlist = tlist;
3107 plan->lefttree = lefttree;
3108 plan->righttree = NULL;
3114 * distinctList is a list of SortClauses, identifying the targetlist items
3115 * that should be considered by the Unique filter. The input path must
3116 * already be sorted accordingly.
3119 make_unique(Plan *lefttree, List *distinctList)
3121 Unique *node = makeNode(Unique);
3122 Plan *plan = &node->plan;
3123 int numCols = list_length(distinctList);
3125 AttrNumber *uniqColIdx;
3129 copy_plan_costsize(plan, lefttree);
3132 * Charge one cpu_operator_cost per comparison per input tuple. We assume
3133 * all columns get compared at most of the tuples. (XXX probably this is
3136 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3139 * plan->plan_rows is left as a copy of the input subplan's plan_rows; ie,
3140 * we assume the filter removes nothing. The caller must alter this if he
3141 * has a better idea.
3144 plan->targetlist = lefttree->targetlist;
3146 plan->lefttree = lefttree;
3147 plan->righttree = NULL;
3150 * convert SortClause list into arrays of attr indexes and equality
3151 * operators, as wanted by executor
3153 Assert(numCols > 0);
3154 uniqColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3155 uniqOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3157 foreach(slitem, distinctList)
3159 SortClause *sortcl = (SortClause *) lfirst(slitem);
3160 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3162 uniqColIdx[keyno] = tle->resno;
3163 uniqOperators[keyno] = get_equality_op_for_ordering_op(sortcl->sortop);
3164 if (!OidIsValid(uniqOperators[keyno])) /* shouldn't happen */
3165 elog(ERROR, "could not find equality operator for ordering operator %u",
3170 node->numCols = numCols;
3171 node->uniqColIdx = uniqColIdx;
3172 node->uniqOperators = uniqOperators;
3178 * distinctList is a list of SortClauses, identifying the targetlist items
3179 * that should be considered by the SetOp filter. The input path must
3180 * already be sorted accordingly.
3183 make_setop(SetOpCmd cmd, Plan *lefttree,
3184 List *distinctList, AttrNumber flagColIdx)
3186 SetOp *node = makeNode(SetOp);
3187 Plan *plan = &node->plan;
3188 int numCols = list_length(distinctList);
3190 AttrNumber *dupColIdx;
3194 copy_plan_costsize(plan, lefttree);
3197 * Charge one cpu_operator_cost per comparison per input tuple. We assume
3198 * all columns get compared at most of the tuples.
3200 plan->total_cost += cpu_operator_cost * plan->plan_rows * numCols;
3203 * We make the unsupported assumption that there will be 10% as many
3204 * tuples out as in. Any way to do better?
3206 plan->plan_rows *= 0.1;
3207 if (plan->plan_rows < 1)
3208 plan->plan_rows = 1;
3210 plan->targetlist = lefttree->targetlist;
3212 plan->lefttree = lefttree;
3213 plan->righttree = NULL;
3216 * convert SortClause list into arrays of attr indexes and equality
3217 * operators, as wanted by executor
3219 Assert(numCols > 0);
3220 dupColIdx = (AttrNumber *) palloc(sizeof(AttrNumber) * numCols);
3221 dupOperators = (Oid *) palloc(sizeof(Oid) * numCols);
3223 foreach(slitem, distinctList)
3225 SortClause *sortcl = (SortClause *) lfirst(slitem);
3226 TargetEntry *tle = get_sortgroupclause_tle(sortcl, plan->targetlist);
3228 dupColIdx[keyno] = tle->resno;
3229 dupOperators[keyno] = get_equality_op_for_ordering_op(sortcl->sortop);
3230 if (!OidIsValid(dupOperators[keyno])) /* shouldn't happen */
3231 elog(ERROR, "could not find equality operator for ordering operator %u",
3237 node->numCols = numCols;
3238 node->dupColIdx = dupColIdx;
3239 node->dupOperators = dupOperators;
3240 node->flagColIdx = flagColIdx;
3246 * Note: offset_est and count_est are passed in to save having to repeat
3247 * work already done to estimate the values of the limitOffset and limitCount
3248 * expressions. Their values are as returned by preprocess_limit (0 means
3249 * "not relevant", -1 means "couldn't estimate"). Keep the code below in sync
3250 * with that function!
3253 make_limit(Plan *lefttree, Node *limitOffset, Node *limitCount,
3254 int64 offset_est, int64 count_est)
3256 Limit *node = makeNode(Limit);
3257 Plan *plan = &node->plan;
3259 copy_plan_costsize(plan, lefttree);
3262 * Adjust the output rows count and costs according to the offset/limit.
3263 * This is only a cosmetic issue if we are at top level, but if we are
3264 * building a subquery then it's important to report correct info to the
3267 * When the offset or count couldn't be estimated, use 10% of the
3268 * estimated number of rows emitted from the subplan.
3270 if (offset_est != 0)
3275 offset_rows = (double) offset_est;
3277 offset_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3278 if (offset_rows > plan->plan_rows)
3279 offset_rows = plan->plan_rows;
3280 if (plan->plan_rows > 0)
3281 plan->startup_cost +=
3282 (plan->total_cost - plan->startup_cost)
3283 * offset_rows / plan->plan_rows;
3284 plan->plan_rows -= offset_rows;
3285 if (plan->plan_rows < 1)
3286 plan->plan_rows = 1;
3294 count_rows = (double) count_est;
3296 count_rows = clamp_row_est(lefttree->plan_rows * 0.10);
3297 if (count_rows > plan->plan_rows)
3298 count_rows = plan->plan_rows;
3299 if (plan->plan_rows > 0)
3300 plan->total_cost = plan->startup_cost +
3301 (plan->total_cost - plan->startup_cost)
3302 * count_rows / plan->plan_rows;
3303 plan->plan_rows = count_rows;
3304 if (plan->plan_rows < 1)
3305 plan->plan_rows = 1;
3308 plan->targetlist = lefttree->targetlist;
3310 plan->lefttree = lefttree;
3311 plan->righttree = NULL;
3313 node->limitOffset = limitOffset;
3314 node->limitCount = limitCount;
3321 * Build a Result plan node
3323 * If we have a subplan, assume that any evaluation costs for the gating qual
3324 * were already factored into the subplan's startup cost, and just copy the
3325 * subplan cost. If there's no subplan, we should include the qual eval
3326 * cost. In either case, tlist eval cost is not to be included here.
3329 make_result(PlannerInfo *root,
3331 Node *resconstantqual,
3334 Result *node = makeNode(Result);
3335 Plan *plan = &node->plan;
3338 copy_plan_costsize(plan, subplan);
3341 plan->startup_cost = 0;
3342 plan->total_cost = cpu_tuple_cost;
3343 plan->plan_rows = 1; /* wrong if we have a set-valued function? */
3344 plan->plan_width = 0; /* XXX is it worth being smarter? */
3345 if (resconstantqual)
3349 cost_qual_eval(&qual_cost, (List *) resconstantqual, root);
3350 /* resconstantqual is evaluated once at startup */
3351 plan->startup_cost += qual_cost.startup + qual_cost.per_tuple;
3352 plan->total_cost += qual_cost.startup + qual_cost.per_tuple;
3356 plan->targetlist = tlist;
3358 plan->lefttree = subplan;
3359 plan->righttree = NULL;
3360 node->resconstantqual = resconstantqual;
3366 * is_projection_capable_plan
3367 * Check whether a given Plan node is able to do projection.
3370 is_projection_capable_plan(Plan *plan)
3372 /* Most plan types can project, so just list the ones that can't */
3373 switch (nodeTag(plan))