1 /*-------------------------------------------------------------------------
4 * Routines to find possible search paths for processing a query
6 * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/optimizer/path/allpaths.c
13 *-------------------------------------------------------------------------
20 #include "catalog/pg_class.h"
21 #include "foreign/fdwapi.h"
22 #include "nodes/nodeFuncs.h"
23 #ifdef OPTIMIZER_DEBUG
24 #include "nodes/print.h"
26 #include "optimizer/clauses.h"
27 #include "optimizer/cost.h"
28 #include "optimizer/geqo.h"
29 #include "optimizer/pathnode.h"
30 #include "optimizer/paths.h"
31 #include "optimizer/plancat.h"
32 #include "optimizer/planner.h"
33 #include "optimizer/prep.h"
34 #include "optimizer/restrictinfo.h"
35 #include "optimizer/var.h"
36 #include "parser/parse_clause.h"
37 #include "parser/parsetree.h"
38 #include "rewrite/rewriteManip.h"
39 #include "utils/lsyscache.h"
42 /* These parameters are set by GUC */
43 bool enable_geqo = false; /* just in case GUC doesn't set it */
46 /* Hook for plugins to replace standard_join_search() */
47 join_search_hook_type join_search_hook = NULL;
50 static void set_base_rel_sizes(PlannerInfo *root);
51 static void set_base_rel_pathlists(PlannerInfo *root);
52 static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
53 Index rti, RangeTblEntry *rte);
54 static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
55 Index rti, RangeTblEntry *rte);
56 static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
58 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
60 static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
62 static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
64 static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
65 Index rti, RangeTblEntry *rte);
66 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
67 Index rti, RangeTblEntry *rte);
68 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
70 List *all_child_pathkeys,
71 Relids required_outer);
72 static List *accumulate_append_subpath(List *subpaths, Path *path);
73 static void set_dummy_rel_pathlist(RelOptInfo *rel);
74 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
75 Index rti, RangeTblEntry *rte);
76 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
78 static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
80 static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
82 static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
84 static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
85 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
86 bool *differentTypes);
87 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
88 bool *differentTypes);
89 static void compare_tlist_datatypes(List *tlist, List *colTypes,
90 bool *differentTypes);
91 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
92 bool *differentTypes);
93 static void subquery_push_qual(Query *subquery,
94 RangeTblEntry *rte, Index rti, Node *qual);
95 static void recurse_push_qual(Node *setOp, Query *topquery,
96 RangeTblEntry *rte, Index rti, Node *qual);
101 * Finds all possible access paths for executing a query, returning a
102 * single rel that represents the join of all base rels in the query.
105 make_one_rel(PlannerInfo *root, List *joinlist)
111 * Construct the all_baserels Relids set.
113 root->all_baserels = NULL;
114 for (rti = 1; rti < root->simple_rel_array_size; rti++)
116 RelOptInfo *brel = root->simple_rel_array[rti];
118 /* there may be empty slots corresponding to non-baserel RTEs */
122 Assert(brel->relid == rti); /* sanity check on array */
124 /* ignore RTEs that are "other rels" */
125 if (brel->reloptkind != RELOPT_BASEREL)
128 root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
132 * Generate access paths for the base rels.
134 set_base_rel_sizes(root);
135 set_base_rel_pathlists(root);
138 * Generate access paths for the entire join tree.
140 rel = make_rel_from_joinlist(root, joinlist);
143 * The result should join all and only the query's base rels.
145 Assert(bms_equal(rel->relids, root->all_baserels));
152 * Set the size estimates (rows and widths) for each base-relation entry.
154 * We do this in a separate pass over the base rels so that rowcount
155 * estimates are available for parameterized path generation.
158 set_base_rel_sizes(PlannerInfo *root)
162 for (rti = 1; rti < root->simple_rel_array_size; rti++)
164 RelOptInfo *rel = root->simple_rel_array[rti];
166 /* there may be empty slots corresponding to non-baserel RTEs */
170 Assert(rel->relid == rti); /* sanity check on array */
172 /* ignore RTEs that are "other rels" */
173 if (rel->reloptkind != RELOPT_BASEREL)
176 set_rel_size(root, rel, rti, root->simple_rte_array[rti]);
181 * set_base_rel_pathlists
182 * Finds all paths available for scanning each base-relation entry.
183 * Sequential scan and any available indices are considered.
184 * Each useful path is attached to its relation's 'pathlist' field.
187 set_base_rel_pathlists(PlannerInfo *root)
191 for (rti = 1; rti < root->simple_rel_array_size; rti++)
193 RelOptInfo *rel = root->simple_rel_array[rti];
195 /* there may be empty slots corresponding to non-baserel RTEs */
199 Assert(rel->relid == rti); /* sanity check on array */
201 /* ignore RTEs that are "other rels" */
202 if (rel->reloptkind != RELOPT_BASEREL)
205 set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
211 * Set size estimates for a base relation
214 set_rel_size(PlannerInfo *root, RelOptInfo *rel,
215 Index rti, RangeTblEntry *rte)
217 if (rel->reloptkind == RELOPT_BASEREL &&
218 relation_excluded_by_constraints(root, rel, rte))
221 * We proved we don't need to scan the rel via constraint exclusion,
222 * so set up a single dummy path for it. Here we only check this for
223 * regular baserels; if it's an otherrel, CE was already checked in
224 * set_append_rel_pathlist().
226 * In this case, we go ahead and set up the relation's path right away
227 * instead of leaving it for set_rel_pathlist to do. This is because
228 * we don't have a convention for marking a rel as dummy except by
229 * assigning a dummy path to it.
231 set_dummy_rel_pathlist(rel);
235 /* It's an "append relation", process accordingly */
236 set_append_rel_size(root, rel, rti, rte);
240 switch (rel->rtekind)
243 if (rte->relkind == RELKIND_FOREIGN_TABLE)
246 set_foreign_size(root, rel, rte);
251 set_plain_rel_size(root, rel, rte);
256 * Subqueries don't support parameterized paths, so just go
257 * ahead and build their paths immediately.
259 set_subquery_pathlist(root, rel, rti, rte);
262 set_function_size_estimates(root, rel);
265 set_values_size_estimates(root, rel);
269 * CTEs don't support parameterized paths, so just go ahead
270 * and build their paths immediately.
272 if (rte->self_reference)
273 set_worktable_pathlist(root, rel, rte);
275 set_cte_pathlist(root, rel, rte);
278 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
286 * Build access paths for a base relation
289 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
290 Index rti, RangeTblEntry *rte)
292 if (IS_DUMMY_REL(rel))
294 /* We already proved the relation empty, so nothing more to do */
298 /* It's an "append relation", process accordingly */
299 set_append_rel_pathlist(root, rel, rti, rte);
303 switch (rel->rtekind)
306 if (rte->relkind == RELKIND_FOREIGN_TABLE)
309 set_foreign_pathlist(root, rel, rte);
314 set_plain_rel_pathlist(root, rel, rte);
318 /* Subquery --- fully handled during set_rel_size */
322 set_function_pathlist(root, rel, rte);
326 set_values_pathlist(root, rel, rte);
329 /* CTE reference --- fully handled during set_rel_size */
332 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
337 #ifdef OPTIMIZER_DEBUG
338 debug_print_rel(root, rel);
344 * Set size estimates for a plain relation (no subquery, no inheritance)
347 set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
350 * Test any partial indexes of rel for applicability. We must do this
351 * first since partial unique indexes can affect size estimates.
353 check_partial_indexes(root, rel);
355 /* Mark rel with estimated output rows, width, etc */
356 set_baserel_size_estimates(root, rel);
359 * Check to see if we can extract any restriction conditions from join
360 * quals that are OR-of-AND structures. If so, add them to the rel's
361 * restriction list, and redo the above steps.
363 if (create_or_index_quals(root, rel))
365 check_partial_indexes(root, rel);
366 set_baserel_size_estimates(root, rel);
371 * set_plain_rel_pathlist
372 * Build access paths for a plain relation (no subquery, no inheritance)
375 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
377 /* Consider sequential scan */
378 add_path(rel, create_seqscan_path(root, rel));
380 /* Consider index scans */
381 create_index_paths(root, rel);
383 /* Consider TID scans */
384 create_tidscan_paths(root, rel);
386 /* Now find the cheapest of the paths for this rel */
392 * Set size estimates for a foreign table RTE
395 set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
397 /* Mark rel with estimated output rows, width, etc */
398 set_foreign_size_estimates(root, rel);
402 * set_foreign_pathlist
403 * Build access paths for a foreign table RTE
406 set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
408 FdwRoutine *fdwroutine;
410 /* Call the FDW's PlanForeignScan function to generate path(s) */
411 fdwroutine = GetFdwRoutineByRelId(rte->relid);
412 fdwroutine->PlanForeignScan(rte->relid, root, rel);
414 /* Select cheapest path */
419 * set_append_rel_size
420 * Set size estimates for an "append relation"
422 * The passed-in rel and RTE represent the entire append relation. The
423 * relation's contents are computed by appending together the output of
424 * the individual member relations. Note that in the inheritance case,
425 * the first member relation is actually the same table as is mentioned in
426 * the parent RTE ... but it has a different RTE and RelOptInfo. This is
427 * a good thing because their outputs are not the same size.
430 set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
431 Index rti, RangeTblEntry *rte)
433 int parentRTindex = rti;
436 double *parent_attrsizes;
441 * Initialize to compute size estimates for whole append relation.
443 * We handle width estimates by weighting the widths of different child
444 * rels proportionally to their number of rows. This is sensible because
445 * the use of width estimates is mainly to compute the total relation
446 * "footprint" if we have to sort or hash it. To do this, we sum the
447 * total equivalent size (in "double" arithmetic) and then divide by the
448 * total rowcount estimate. This is done separately for the total rel
449 * width and each attribute.
451 * Note: if you consider changing this logic, beware that child rels could
452 * have zero rows and/or width, if they were excluded by constraints.
456 nattrs = rel->max_attr - rel->min_attr + 1;
457 parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
459 foreach(l, root->append_rel_list)
461 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
463 RangeTblEntry *childRTE;
464 RelOptInfo *childrel;
467 ListCell *parentvars;
470 /* append_rel_list contains all append rels; ignore others */
471 if (appinfo->parent_relid != parentRTindex)
474 childRTindex = appinfo->child_relid;
475 childRTE = root->simple_rte_array[childRTindex];
478 * The child rel's RelOptInfo was already created during
479 * add_base_rels_to_query.
481 childrel = find_base_rel(root, childRTindex);
482 Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
485 * We have to copy the parent's targetlist and quals to the child,
486 * with appropriate substitution of variables. However, only the
487 * baserestrictinfo quals are needed before we can check for
488 * constraint exclusion; so do that first and then check to see if we
489 * can disregard this child.
491 * As of 8.4, the child rel's targetlist might contain non-Var
492 * expressions, which means that substitution into the quals could
493 * produce opportunities for const-simplification, and perhaps even
494 * pseudoconstant quals. To deal with this, we strip the RestrictInfo
495 * nodes, do the substitution, do const-simplification, and then
496 * reconstitute the RestrictInfo layer.
498 childquals = get_all_actual_clauses(rel->baserestrictinfo);
499 childquals = (List *) adjust_appendrel_attrs(root,
502 childqual = eval_const_expressions(root, (Node *)
503 make_ands_explicit(childquals));
504 if (childqual && IsA(childqual, Const) &&
505 (((Const *) childqual)->constisnull ||
506 !DatumGetBool(((Const *) childqual)->constvalue)))
509 * Restriction reduces to constant FALSE or constant NULL after
510 * substitution, so this child need not be scanned.
512 set_dummy_rel_pathlist(childrel);
515 childquals = make_ands_implicit((Expr *) childqual);
516 childquals = make_restrictinfos_from_actual_clauses(root,
518 childrel->baserestrictinfo = childquals;
520 if (relation_excluded_by_constraints(root, childrel, childRTE))
523 * This child need not be scanned, so we can omit it from the
526 set_dummy_rel_pathlist(childrel);
531 * CE failed, so finish copying/modifying targetlist and join quals.
533 * Note: the resulting childrel->reltargetlist may contain arbitrary
534 * expressions, which normally would not occur in a reltargetlist.
535 * That is okay because nothing outside of this routine will look at
536 * the child rel's reltargetlist. We do have to cope with the case
537 * while constructing attr_widths estimates below, though.
539 childrel->joininfo = (List *)
540 adjust_appendrel_attrs(root,
541 (Node *) rel->joininfo,
543 childrel->reltargetlist = (List *)
544 adjust_appendrel_attrs(root,
545 (Node *) rel->reltargetlist,
549 * We have to make child entries in the EquivalenceClass data
550 * structures as well. This is needed either if the parent
551 * participates in some eclass joins (because we will want to consider
552 * inner-indexscan joins on the individual children) or if the parent
553 * has useful pathkeys (because we should try to build MergeAppend
554 * paths that produce those sort orderings).
556 if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
557 add_child_rel_equivalences(root, appinfo, rel, childrel);
558 childrel->has_eclass_joins = rel->has_eclass_joins;
561 * Note: we could compute appropriate attr_needed data for the child's
562 * variables, by transforming the parent's attr_needed through the
563 * translated_vars mapping. However, currently there's no need
564 * because attr_needed is only examined for base relations not
565 * otherrels. So we just leave the child's attr_needed empty.
569 * Compute the child's size.
571 set_rel_size(root, childrel, childRTindex, childRTE);
574 * It is possible that constraint exclusion detected a contradiction
575 * within a child subquery, even though we didn't prove one above.
576 * If so, we can skip this child.
578 if (IS_DUMMY_REL(childrel))
582 * Accumulate size information from each live child.
584 if (childrel->rows > 0)
586 parent_rows += childrel->rows;
587 parent_size += childrel->width * childrel->rows;
590 * Accumulate per-column estimates too. We need not do anything
591 * for PlaceHolderVars in the parent list. If child expression
592 * isn't a Var, or we didn't record a width estimate for it, we
593 * have to fall back on a datatype-based estimate.
595 * By construction, child's reltargetlist is 1-to-1 with parent's.
597 forboth(parentvars, rel->reltargetlist,
598 childvars, childrel->reltargetlist)
600 Var *parentvar = (Var *) lfirst(parentvars);
601 Node *childvar = (Node *) lfirst(childvars);
603 if (IsA(parentvar, Var))
605 int pndx = parentvar->varattno - rel->min_attr;
606 int32 child_width = 0;
608 if (IsA(childvar, Var))
610 int cndx = ((Var *) childvar)->varattno - childrel->min_attr;
612 child_width = childrel->attr_widths[cndx];
614 if (child_width <= 0)
615 child_width = get_typavgwidth(exprType(childvar),
616 exprTypmod(childvar));
617 Assert(child_width > 0);
618 parent_attrsizes[pndx] += child_width * childrel->rows;
625 * Save the finished size estimates.
627 rel->rows = parent_rows;
632 rel->width = rint(parent_size / parent_rows);
633 for (i = 0; i < nattrs; i++)
634 rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
637 rel->width = 0; /* attr_widths should be zero already */
640 * Set "raw tuples" count equal to "rows" for the appendrel; needed
641 * because some places assume rel->tuples is valid for any baserel.
643 rel->tuples = parent_rows;
645 pfree(parent_attrsizes);
649 * set_append_rel_pathlist
650 * Build access paths for an "append relation"
653 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
654 Index rti, RangeTblEntry *rte)
656 int parentRTindex = rti;
657 List *live_childrels = NIL;
658 List *subpaths = NIL;
659 List *all_child_pathkeys = NIL;
660 List *all_child_outers = NIL;
664 * Generate access paths for each member relation, and remember the
665 * cheapest path for each one. Also, identify all pathkeys (orderings)
666 * and parameterizations (required_outer sets) available for the member
669 foreach(l, root->append_rel_list)
671 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
673 RangeTblEntry *childRTE;
674 RelOptInfo *childrel;
677 /* append_rel_list contains all append rels; ignore others */
678 if (appinfo->parent_relid != parentRTindex)
681 /* Re-locate the child RTE and RelOptInfo */
682 childRTindex = appinfo->child_relid;
683 childRTE = root->simple_rte_array[childRTindex];
684 childrel = root->simple_rel_array[childRTindex];
687 * Compute the child's access paths.
689 set_rel_pathlist(root, childrel, childRTindex, childRTE);
692 * If child is dummy, ignore it.
694 if (IS_DUMMY_REL(childrel))
698 * Child is live, so add its cheapest access path to the Append path
699 * we are constructing for the parent.
701 subpaths = accumulate_append_subpath(subpaths,
702 childrel->cheapest_total_path);
704 /* Remember which childrels are live, for logic below */
705 live_childrels = lappend(live_childrels, childrel);
708 * Collect lists of all the available path orderings and
709 * parameterizations for all the children. We use these as a
710 * heuristic to indicate which sort orderings and parameterizations we
711 * should build Append and MergeAppend paths for.
713 foreach(lcp, childrel->pathlist)
715 Path *childpath = (Path *) lfirst(lcp);
716 List *childkeys = childpath->pathkeys;
717 Relids childouter = childpath->required_outer;
719 /* Unsorted paths don't contribute to pathkey list */
720 if (childkeys != NIL)
725 /* Have we already seen this ordering? */
726 foreach(lpk, all_child_pathkeys)
728 List *existing_pathkeys = (List *) lfirst(lpk);
730 if (compare_pathkeys(existing_pathkeys,
731 childkeys) == PATHKEYS_EQUAL)
739 /* No, so add it to all_child_pathkeys */
740 all_child_pathkeys = lappend(all_child_pathkeys,
745 /* Unparameterized paths don't contribute to param-set list */
751 /* Have we already seen this param set? */
752 foreach(lco, all_child_outers)
754 Relids existing_outers = (Relids) lfirst(lco);
756 if (bms_equal(existing_outers, childouter))
764 /* No, so add it to all_child_outers */
765 all_child_outers = lappend(all_child_outers,
773 * Next, build an unordered, unparameterized Append path for the rel.
774 * (Note: this is correct even if we have zero or one live subpath due to
775 * constraint exclusion.)
777 add_path(rel, (Path *) create_append_path(rel, subpaths));
780 * Build unparameterized MergeAppend paths based on the collected list of
783 generate_mergeappend_paths(root, rel, live_childrels,
784 all_child_pathkeys, NULL);
787 * Build Append and MergeAppend paths for each parameterization seen
788 * among the child rels. (This may look pretty expensive, but in most
789 * cases of practical interest, the child relations will tend to expose
790 * the same parameterizations and pathkeys, so that not that many cases
791 * actually get considered here.)
793 foreach(l, all_child_outers)
795 Relids required_outer = (Relids) lfirst(l);
798 /* Select the child paths for an Append with this parameterization */
800 foreach(lcr, live_childrels)
802 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
803 Path *cheapest_total;
806 get_cheapest_path_for_pathkeys(childrel->pathlist,
810 Assert(cheapest_total != NULL);
812 subpaths = accumulate_append_subpath(subpaths, cheapest_total);
814 add_path(rel, (Path *) create_append_path(rel, subpaths));
816 /* And build parameterized MergeAppend paths */
817 generate_mergeappend_paths(root, rel, live_childrels,
818 all_child_pathkeys, required_outer);
821 /* Select cheapest paths */
826 * generate_mergeappend_paths
827 * Generate MergeAppend paths for an append relation
829 * Generate a path for each ordering (pathkey list) appearing in
830 * all_child_pathkeys. If required_outer isn't NULL, accept paths having
831 * those relations as required outer relations.
833 * We consider both cheapest-startup and cheapest-total cases, ie, for each
834 * interesting ordering, collect all the cheapest startup subpaths and all the
835 * cheapest total paths, and build a MergeAppend path for each case.
838 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
839 List *live_childrels,
840 List *all_child_pathkeys,
841 Relids required_outer)
845 foreach(lcp, all_child_pathkeys)
847 List *pathkeys = (List *) lfirst(lcp);
848 List *startup_subpaths = NIL;
849 List *total_subpaths = NIL;
850 bool startup_neq_total = false;
853 /* Select the child paths for this ordering... */
854 foreach(lcr, live_childrels)
856 RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
857 Path *cheapest_startup,
860 /* Locate the right paths, if they are available. */
862 get_cheapest_path_for_pathkeys(childrel->pathlist,
867 get_cheapest_path_for_pathkeys(childrel->pathlist,
873 * If we can't find any paths with the right order just use the
874 * cheapest-total path; we'll have to sort it later. We can
875 * use the cheapest path for the parameterization, though.
877 if (cheapest_startup == NULL || cheapest_total == NULL)
880 cheapest_startup = cheapest_total =
881 get_cheapest_path_for_pathkeys(childrel->pathlist,
886 cheapest_startup = cheapest_total =
887 childrel->cheapest_total_path;
888 Assert(cheapest_total != NULL);
892 * Notice whether we actually have different paths for the
893 * "cheapest" and "total" cases; frequently there will be no point
894 * in two create_merge_append_path() calls.
896 if (cheapest_startup != cheapest_total)
897 startup_neq_total = true;
900 accumulate_append_subpath(startup_subpaths, cheapest_startup);
902 accumulate_append_subpath(total_subpaths, cheapest_total);
905 /* ... and build the MergeAppend paths */
906 add_path(rel, (Path *) create_merge_append_path(root,
910 if (startup_neq_total)
911 add_path(rel, (Path *) create_merge_append_path(root,
919 * accumulate_append_subpath
920 * Add a subpath to the list being built for an Append or MergeAppend
922 * It's possible that the child is itself an Append path, in which case
923 * we can "cut out the middleman" and just add its child paths to our
924 * own list. (We don't try to do this earlier because we need to
925 * apply both levels of transformation to the quals.)
928 accumulate_append_subpath(List *subpaths, Path *path)
930 if (IsA(path, AppendPath))
932 AppendPath *apath = (AppendPath *) path;
934 /* list_copy is important here to avoid sharing list substructure */
935 return list_concat(subpaths, list_copy(apath->subpaths));
938 return lappend(subpaths, path);
942 * set_dummy_rel_pathlist
943 * Build a dummy path for a relation that's been excluded by constraints
945 * Rather than inventing a special "dummy" path type, we represent this as an
946 * AppendPath with no members (see also IS_DUMMY_PATH/IS_DUMMY_REL macros).
949 set_dummy_rel_pathlist(RelOptInfo *rel)
951 /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
955 /* Discard any pre-existing paths; no further need for them */
958 add_path(rel, (Path *) create_append_path(rel, NIL));
960 /* Select cheapest path (pretty easy in this case...) */
964 /* quick-and-dirty test to see if any joining is needed */
966 has_multiple_baserels(PlannerInfo *root)
968 int num_base_rels = 0;
971 for (rti = 1; rti < root->simple_rel_array_size; rti++)
973 RelOptInfo *brel = root->simple_rel_array[rti];
978 /* ignore RTEs that are "other rels" */
979 if (brel->reloptkind == RELOPT_BASEREL)
980 if (++num_base_rels > 1)
987 * set_subquery_pathlist
988 * Build the (single) access path for a subquery RTE
990 * There's no need for a separate set_subquery_size phase, since we don't
991 * support parameterized paths for subqueries.
994 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
995 Index rti, RangeTblEntry *rte)
997 Query *parse = root->parse;
998 Query *subquery = rte->subquery;
999 bool *differentTypes;
1000 double tuple_fraction;
1001 PlannerInfo *subroot;
1005 * Must copy the Query so that planning doesn't mess up the RTE contents
1006 * (really really need to fix the planner to not scribble on its input,
1009 subquery = copyObject(subquery);
1011 /* We need a workspace for keeping track of set-op type coercions */
1012 differentTypes = (bool *)
1013 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
1016 * If there are any restriction clauses that have been attached to the
1017 * subquery relation, consider pushing them down to become WHERE or HAVING
1018 * quals of the subquery itself. This transformation is useful because it
1019 * may allow us to generate a better plan for the subquery than evaluating
1020 * all the subquery output rows and then filtering them.
1022 * There are several cases where we cannot push down clauses. Restrictions
1023 * involving the subquery are checked by subquery_is_pushdown_safe().
1024 * Restrictions on individual clauses are checked by
1025 * qual_is_pushdown_safe(). Also, we don't want to push down
1026 * pseudoconstant clauses; better to have the gating node above the
1029 * Also, if the sub-query has "security_barrier" flag, it means the
1030 * sub-query originated from a view that must enforce row-level security.
1031 * We must not push down quals in order to avoid information leaks, either
1032 * via side-effects or error output.
1034 * Non-pushed-down clauses will get evaluated as qpquals of the
1035 * SubqueryScan node.
1037 * XXX Are there any cases where we want to make a policy decision not to
1038 * push down a pushable qual, because it'd result in a worse plan?
1040 if (rel->baserestrictinfo != NIL &&
1041 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
1043 /* OK to consider pushing down individual quals */
1044 List *upperrestrictlist = NIL;
1047 foreach(l, rel->baserestrictinfo)
1049 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1050 Node *clause = (Node *) rinfo->clause;
1052 if (!rinfo->pseudoconstant &&
1053 (!rte->security_barrier ||
1054 !contain_leaky_functions(clause)) &&
1055 qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
1058 subquery_push_qual(subquery, rte, rti, clause);
1062 /* Keep it in the upper query */
1063 upperrestrictlist = lappend(upperrestrictlist, rinfo);
1066 rel->baserestrictinfo = upperrestrictlist;
1069 pfree(differentTypes);
1072 * We can safely pass the outer tuple_fraction down to the subquery if the
1073 * outer level has no joining, aggregation, or sorting to do. Otherwise
1074 * we'd better tell the subquery to plan for full retrieval. (XXX This
1075 * could probably be made more intelligent ...)
1077 if (parse->hasAggs ||
1078 parse->groupClause ||
1079 parse->havingQual ||
1080 parse->distinctClause ||
1081 parse->sortClause ||
1082 has_multiple_baserels(root))
1083 tuple_fraction = 0.0; /* default case */
1085 tuple_fraction = root->tuple_fraction;
1087 /* Generate the plan for the subquery */
1088 rel->subplan = subquery_planner(root->glob, subquery,
1090 false, tuple_fraction,
1092 rel->subroot = subroot;
1095 * It's possible that constraint exclusion proved the subquery empty.
1096 * If so, it's convenient to turn it back into a dummy path so that we
1097 * will recognize appropriate optimizations at this level.
1099 if (is_dummy_plan(rel->subplan))
1101 set_dummy_rel_pathlist(rel);
1105 /* Mark rel with estimated output rows, width, etc */
1106 set_subquery_size_estimates(root, rel);
1108 /* Convert subquery pathkeys to outer representation */
1109 pathkeys = convert_subquery_pathkeys(root, rel, subroot->query_pathkeys);
1111 /* Generate appropriate path */
1112 add_path(rel, create_subqueryscan_path(rel, pathkeys));
1114 /* Select cheapest path (pretty easy in this case...) */
1119 * set_function_pathlist
1120 * Build the (single) access path for a function RTE
1123 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1125 /* Generate appropriate path */
1126 add_path(rel, create_functionscan_path(root, rel));
1128 /* Select cheapest path (pretty easy in this case...) */
1133 * set_values_pathlist
1134 * Build the (single) access path for a VALUES RTE
1137 set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1139 /* Generate appropriate path */
1140 add_path(rel, create_valuesscan_path(root, rel));
1142 /* Select cheapest path (pretty easy in this case...) */
1148 * Build the (single) access path for a non-self-reference CTE RTE
1150 * There's no need for a separate set_cte_size phase, since we don't
1151 * support parameterized paths for CTEs.
1154 set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1157 PlannerInfo *cteroot;
1164 * Find the referenced CTE, and locate the plan previously made for it.
1166 levelsup = rte->ctelevelsup;
1168 while (levelsup-- > 0)
1170 cteroot = cteroot->parent_root;
1171 if (!cteroot) /* shouldn't happen */
1172 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1176 * Note: cte_plan_ids can be shorter than cteList, if we are still working
1177 * on planning the CTEs (ie, this is a side-reference from another CTE).
1178 * So we mustn't use forboth here.
1181 foreach(lc, cteroot->parse->cteList)
1183 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1185 if (strcmp(cte->ctename, rte->ctename) == 0)
1189 if (lc == NULL) /* shouldn't happen */
1190 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1191 if (ndx >= list_length(cteroot->cte_plan_ids))
1192 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1193 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1194 Assert(plan_id > 0);
1195 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
1197 /* Mark rel with estimated output rows, width, etc */
1198 set_cte_size_estimates(root, rel, cteplan);
1200 /* Generate appropriate path */
1201 add_path(rel, create_ctescan_path(root, rel));
1203 /* Select cheapest path (pretty easy in this case...) */
1208 * set_worktable_pathlist
1209 * Build the (single) access path for a self-reference CTE RTE
1211 * There's no need for a separate set_worktable_size phase, since we don't
1212 * support parameterized paths for CTEs.
1215 set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1218 PlannerInfo *cteroot;
1222 * We need to find the non-recursive term's plan, which is in the plan
1223 * level that's processing the recursive UNION, which is one level *below*
1224 * where the CTE comes from.
1226 levelsup = rte->ctelevelsup;
1227 if (levelsup == 0) /* shouldn't happen */
1228 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1231 while (levelsup-- > 0)
1233 cteroot = cteroot->parent_root;
1234 if (!cteroot) /* shouldn't happen */
1235 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1237 cteplan = cteroot->non_recursive_plan;
1238 if (!cteplan) /* shouldn't happen */
1239 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1241 /* Mark rel with estimated output rows, width, etc */
1242 set_cte_size_estimates(root, rel, cteplan);
1244 /* Generate appropriate path */
1245 add_path(rel, create_worktablescan_path(root, rel));
1247 /* Select cheapest path (pretty easy in this case...) */
1252 * make_rel_from_joinlist
1253 * Build access paths using a "joinlist" to guide the join path search.
1255 * See comments for deconstruct_jointree() for definition of the joinlist
1259 make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
1266 * Count the number of child joinlist nodes. This is the depth of the
1267 * dynamic-programming algorithm we must employ to consider all ways of
1268 * joining the child nodes.
1270 levels_needed = list_length(joinlist);
1272 if (levels_needed <= 0)
1273 return NULL; /* nothing to do? */
1276 * Construct a list of rels corresponding to the child joinlist nodes.
1277 * This may contain both base rels and rels constructed according to
1281 foreach(jl, joinlist)
1283 Node *jlnode = (Node *) lfirst(jl);
1284 RelOptInfo *thisrel;
1286 if (IsA(jlnode, RangeTblRef))
1288 int varno = ((RangeTblRef *) jlnode)->rtindex;
1290 thisrel = find_base_rel(root, varno);
1292 else if (IsA(jlnode, List))
1294 /* Recurse to handle subproblem */
1295 thisrel = make_rel_from_joinlist(root, (List *) jlnode);
1299 elog(ERROR, "unrecognized joinlist node type: %d",
1300 (int) nodeTag(jlnode));
1301 thisrel = NULL; /* keep compiler quiet */
1304 initial_rels = lappend(initial_rels, thisrel);
1307 if (levels_needed == 1)
1310 * Single joinlist node, so we're done.
1312 return (RelOptInfo *) linitial(initial_rels);
1317 * Consider the different orders in which we could join the rels,
1318 * using a plugin, GEQO, or the regular join search code.
1320 * We put the initial_rels list into a PlannerInfo field because
1321 * has_legal_joinclause() needs to look at it (ugly :-().
1323 root->initial_rels = initial_rels;
1325 if (join_search_hook)
1326 return (*join_search_hook) (root, levels_needed, initial_rels);
1327 else if (enable_geqo && levels_needed >= geqo_threshold)
1328 return geqo(root, levels_needed, initial_rels);
1330 return standard_join_search(root, levels_needed, initial_rels);
1335 * standard_join_search
1336 * Find possible joinpaths for a query by successively finding ways
1337 * to join component relations into join relations.
1339 * 'levels_needed' is the number of iterations needed, ie, the number of
1340 * independent jointree items in the query. This is > 1.
1342 * 'initial_rels' is a list of RelOptInfo nodes for each independent
1343 * jointree item. These are the components to be joined together.
1344 * Note that levels_needed == list_length(initial_rels).
1346 * Returns the final level of join relations, i.e., the relation that is
1347 * the result of joining all the original relations together.
1348 * At least one implementation path must be provided for this relation and
1349 * all required sub-relations.
1351 * To support loadable plugins that modify planner behavior by changing the
1352 * join searching algorithm, we provide a hook variable that lets a plugin
1353 * replace or supplement this function. Any such hook must return the same
1354 * final join relation as the standard code would, but it might have a
1355 * different set of implementation paths attached, and only the sub-joinrels
1356 * needed for these paths need have been instantiated.
1358 * Note to plugin authors: the functions invoked during standard_join_search()
1359 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
1360 * than one join-order search, you'll probably need to save and restore the
1361 * original states of those data structures. See geqo_eval() for an example.
1364 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
1370 * This function cannot be invoked recursively within any one planning
1371 * problem, so join_rel_level[] can't be in use already.
1373 Assert(root->join_rel_level == NULL);
1376 * We employ a simple "dynamic programming" algorithm: we first find all
1377 * ways to build joins of two jointree items, then all ways to build joins
1378 * of three items (from two-item joins and single items), then four-item
1379 * joins, and so on until we have considered all ways to join all the
1380 * items into one rel.
1382 * root->join_rel_level[j] is a list of all the j-item rels. Initially we
1383 * set root->join_rel_level[1] to represent all the single-jointree-item
1386 root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
1388 root->join_rel_level[1] = initial_rels;
1390 for (lev = 2; lev <= levels_needed; lev++)
1395 * Determine all possible pairs of relations to be joined at this
1396 * level, and build paths for making each one from every available
1397 * pair of lower-level relations.
1399 join_search_one_level(root, lev);
1402 * Do cleanup work on each just-processed rel.
1404 foreach(lc, root->join_rel_level[lev])
1406 rel = (RelOptInfo *) lfirst(lc);
1408 /* Find and save the cheapest paths for this rel */
1411 #ifdef OPTIMIZER_DEBUG
1412 debug_print_rel(root, rel);
1418 * We should have a single rel at the final level.
1420 if (root->join_rel_level[levels_needed] == NIL)
1421 elog(ERROR, "failed to build any %d-way joins", levels_needed);
1422 Assert(list_length(root->join_rel_level[levels_needed]) == 1);
1424 rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
1426 root->join_rel_level = NULL;
1431 /*****************************************************************************
1432 * PUSHING QUALS DOWN INTO SUBQUERIES
1433 *****************************************************************************/
1436 * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
1438 * subquery is the particular component query being checked. topquery
1439 * is the top component of a set-operations tree (the same Query if no
1440 * set-op is involved).
1442 * Conditions checked here:
1444 * 1. If the subquery has a LIMIT clause, we must not push down any quals,
1445 * since that could change the set of rows returned.
1447 * 2. If the subquery contains any window functions, we can't push quals
1448 * into it, because that could change the results.
1450 * 3. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
1451 * quals into it, because that could change the results.
1453 * 4. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
1454 * push quals into each component query, but the quals can only reference
1455 * subquery columns that suffer no type coercions in the set operation.
1456 * Otherwise there are possible semantic gotchas. So, we check the
1457 * component queries to see if any of them have different output types;
1458 * differentTypes[k] is set true if column k has different type in any
1462 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
1463 bool *differentTypes)
1465 SetOperationStmt *topop;
1468 if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
1472 if (subquery->hasWindowFuncs)
1475 /* Are we at top level, or looking at a setop component? */
1476 if (subquery == topquery)
1478 /* Top level, so check any component queries */
1479 if (subquery->setOperations != NULL)
1480 if (!recurse_pushdown_safe(subquery->setOperations, topquery,
1486 /* Setop component must not have more components (too weird) */
1487 if (subquery->setOperations != NULL)
1489 /* Check whether setop component output types match top level */
1490 topop = (SetOperationStmt *) topquery->setOperations;
1491 Assert(topop && IsA(topop, SetOperationStmt));
1492 compare_tlist_datatypes(subquery->targetList,
1500 * Helper routine to recurse through setOperations tree
1503 recurse_pushdown_safe(Node *setOp, Query *topquery,
1504 bool *differentTypes)
1506 if (IsA(setOp, RangeTblRef))
1508 RangeTblRef *rtr = (RangeTblRef *) setOp;
1509 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
1510 Query *subquery = rte->subquery;
1512 Assert(subquery != NULL);
1513 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
1515 else if (IsA(setOp, SetOperationStmt))
1517 SetOperationStmt *op = (SetOperationStmt *) setOp;
1519 /* EXCEPT is no good */
1520 if (op->op == SETOP_EXCEPT)
1523 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
1525 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
1530 elog(ERROR, "unrecognized node type: %d",
1531 (int) nodeTag(setOp));
1537 * Compare tlist's datatypes against the list of set-operation result types.
1538 * For any items that are different, mark the appropriate element of
1539 * differentTypes[] to show that this column will have type conversions.
1541 * We don't have to care about typmods here: the only allowed difference
1542 * between set-op input and output typmods is input is a specific typmod
1543 * and output is -1, and that does not require a coercion.
1546 compare_tlist_datatypes(List *tlist, List *colTypes,
1547 bool *differentTypes)
1550 ListCell *colType = list_head(colTypes);
1554 TargetEntry *tle = (TargetEntry *) lfirst(l);
1557 continue; /* ignore resjunk columns */
1558 if (colType == NULL)
1559 elog(ERROR, "wrong number of tlist entries");
1560 if (exprType((Node *) tle->expr) != lfirst_oid(colType))
1561 differentTypes[tle->resno] = true;
1562 colType = lnext(colType);
1564 if (colType != NULL)
1565 elog(ERROR, "wrong number of tlist entries");
1569 * qual_is_pushdown_safe - is a particular qual safe to push down?
1571 * qual is a restriction clause applying to the given subquery (whose RTE
1572 * has index rti in the parent query).
1574 * Conditions checked here:
1576 * 1. The qual must not contain any subselects (mainly because I'm not sure
1577 * it will work correctly: sublinks will already have been transformed into
1578 * subplans in the qual, but not in the subquery).
1580 * 2. The qual must not refer to the whole-row output of the subquery
1581 * (since there is no easy way to name that within the subquery itself).
1583 * 3. The qual must not refer to any subquery output columns that were
1584 * found to have inconsistent types across a set operation tree by
1585 * subquery_is_pushdown_safe().
1587 * 4. If the subquery uses DISTINCT ON, we must not push down any quals that
1588 * refer to non-DISTINCT output columns, because that could change the set
1589 * of rows returned. (This condition is vacuous for DISTINCT, because then
1590 * there are no non-DISTINCT output columns, so we needn't check. But note
1591 * we are assuming that the qual can't distinguish values that the DISTINCT
1592 * operator sees as equal. This is a bit shaky but we have no way to test
1593 * for the case, and it's unlikely enough that we shouldn't refuse the
1594 * optimization just because it could theoretically happen.)
1596 * 5. We must not push down any quals that refer to subselect outputs that
1597 * return sets, else we'd introduce functions-returning-sets into the
1598 * subquery's WHERE/HAVING quals.
1600 * 6. We must not push down any quals that refer to subselect outputs that
1601 * contain volatile functions, for fear of introducing strange results due
1602 * to multiple evaluation of a volatile function.
1605 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
1606 bool *differentTypes)
1611 Bitmapset *tested = NULL;
1613 /* Refuse subselects (point 1) */
1614 if (contain_subplans(qual))
1618 * It would be unsafe to push down window function calls, but at least for
1619 * the moment we could never see any in a qual anyhow. (The same applies
1620 * to aggregates, which we check for in pull_var_clause below.)
1622 Assert(!contain_window_function(qual));
1625 * Examine all Vars used in clause; since it's a restriction clause, all
1626 * such Vars must refer to subselect output columns.
1628 vars = pull_var_clause(qual,
1629 PVC_REJECT_AGGREGATES,
1630 PVC_INCLUDE_PLACEHOLDERS);
1633 Var *var = (Var *) lfirst(vl);
1637 * XXX Punt if we find any PlaceHolderVars in the restriction clause.
1638 * It's not clear whether a PHV could safely be pushed down, and even
1639 * less clear whether such a situation could arise in any cases of
1640 * practical interest anyway. So for the moment, just refuse to push
1649 Assert(var->varno == rti);
1652 if (var->varattno == 0)
1659 * We use a bitmapset to avoid testing the same attno more than once.
1660 * (NB: this only works because subquery outputs can't have negative
1663 if (bms_is_member(var->varattno, tested))
1665 tested = bms_add_member(tested, var->varattno);
1668 if (differentTypes[var->varattno])
1674 /* Must find the tlist element referenced by the Var */
1675 tle = get_tle_by_resno(subquery->targetList, var->varattno);
1676 Assert(tle != NULL);
1677 Assert(!tle->resjunk);
1679 /* If subquery uses DISTINCT ON, check point 4 */
1680 if (subquery->hasDistinctOn &&
1681 !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
1683 /* non-DISTINCT column, so fail */
1688 /* Refuse functions returning sets (point 5) */
1689 if (expression_returns_set((Node *) tle->expr))
1695 /* Refuse volatile functions (point 6) */
1696 if (contain_volatile_functions((Node *) tle->expr))
1710 * subquery_push_qual - push down a qual that we have determined is safe
1713 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
1715 if (subquery->setOperations != NULL)
1717 /* Recurse to push it separately to each component query */
1718 recurse_push_qual(subquery->setOperations, subquery,
1724 * We need to replace Vars in the qual (which must refer to outputs of
1725 * the subquery) with copies of the subquery's targetlist expressions.
1726 * Note that at this point, any uplevel Vars in the qual should have
1727 * been replaced with Params, so they need no work.
1729 * This step also ensures that when we are pushing into a setop tree,
1730 * each component query gets its own copy of the qual.
1732 qual = ResolveNew(qual, rti, 0, rte,
1733 subquery->targetList,
1735 &subquery->hasSubLinks);
1738 * Now attach the qual to the proper place: normally WHERE, but if the
1739 * subquery uses grouping or aggregation, put it in HAVING (since the
1740 * qual really refers to the group-result rows).
1742 if (subquery->hasAggs || subquery->groupClause || subquery->havingQual)
1743 subquery->havingQual = make_and_qual(subquery->havingQual, qual);
1745 subquery->jointree->quals =
1746 make_and_qual(subquery->jointree->quals, qual);
1749 * We need not change the subquery's hasAggs or hasSublinks flags,
1750 * since we can't be pushing down any aggregates that weren't there
1751 * before, and we don't push down subselects at all.
1757 * Helper routine to recurse through setOperations tree
1760 recurse_push_qual(Node *setOp, Query *topquery,
1761 RangeTblEntry *rte, Index rti, Node *qual)
1763 if (IsA(setOp, RangeTblRef))
1765 RangeTblRef *rtr = (RangeTblRef *) setOp;
1766 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
1767 Query *subquery = subrte->subquery;
1769 Assert(subquery != NULL);
1770 subquery_push_qual(subquery, rte, rti, qual);
1772 else if (IsA(setOp, SetOperationStmt))
1774 SetOperationStmt *op = (SetOperationStmt *) setOp;
1776 recurse_push_qual(op->larg, topquery, rte, rti, qual);
1777 recurse_push_qual(op->rarg, topquery, rte, rti, qual);
1781 elog(ERROR, "unrecognized node type: %d",
1782 (int) nodeTag(setOp));
1786 /*****************************************************************************
1788 *****************************************************************************/
1790 #ifdef OPTIMIZER_DEBUG
1793 print_relids(Relids relids)
1799 tmprelids = bms_copy(relids);
1800 while ((x = bms_first_member(tmprelids)) >= 0)
1807 bms_free(tmprelids);
1811 print_restrictclauses(PlannerInfo *root, List *clauses)
1817 RestrictInfo *c = lfirst(l);
1819 print_expr((Node *) c->clause, root->parse->rtable);
1826 print_path(PlannerInfo *root, Path *path, int indent)
1830 Path *subpath = NULL;
1833 switch (nodeTag(path))
1841 case T_BitmapHeapPath:
1842 ptype = "BitmapHeapScan";
1844 case T_BitmapAndPath:
1845 ptype = "BitmapAndPath";
1847 case T_BitmapOrPath:
1848 ptype = "BitmapOrPath";
1854 ptype = "ForeignScan";
1859 case T_MergeAppendPath:
1860 ptype = "MergeAppend";
1865 case T_MaterialPath:
1867 subpath = ((MaterialPath *) path)->subpath;
1871 subpath = ((UniquePath *) path)->subpath;
1878 ptype = "MergeJoin";
1890 for (i = 0; i < indent; i++)
1892 printf("%s", ptype);
1897 print_relids(path->parent->relids);
1898 printf(") rows=%.0f", path->parent->rows);
1900 printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
1904 for (i = 0; i < indent; i++)
1906 printf(" pathkeys: ");
1907 print_pathkeys(path->pathkeys, root->parse->rtable);
1912 JoinPath *jp = (JoinPath *) path;
1914 for (i = 0; i < indent; i++)
1916 printf(" clauses: ");
1917 print_restrictclauses(root, jp->joinrestrictinfo);
1920 if (IsA(path, MergePath))
1922 MergePath *mp = (MergePath *) path;
1924 for (i = 0; i < indent; i++)
1926 printf(" sortouter=%d sortinner=%d materializeinner=%d\n",
1927 ((mp->outersortkeys) ? 1 : 0),
1928 ((mp->innersortkeys) ? 1 : 0),
1929 ((mp->materialize_inner) ? 1 : 0));
1932 print_path(root, jp->outerjoinpath, indent + 1);
1933 print_path(root, jp->innerjoinpath, indent + 1);
1937 print_path(root, subpath, indent + 1);
1941 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
1945 printf("RELOPTINFO (");
1946 print_relids(rel->relids);
1947 printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
1949 if (rel->baserestrictinfo)
1951 printf("\tbaserestrictinfo: ");
1952 print_restrictclauses(root, rel->baserestrictinfo);
1958 printf("\tjoininfo: ");
1959 print_restrictclauses(root, rel->joininfo);
1963 printf("\tpath list:\n");
1964 foreach(l, rel->pathlist)
1965 print_path(root, lfirst(l), 1);
1966 printf("\n\tcheapest startup path:\n");
1967 print_path(root, rel->cheapest_startup_path, 1);
1968 printf("\n\tcheapest total path:\n");
1969 print_path(root, rel->cheapest_total_path, 1);
1974 #endif /* OPTIMIZER_DEBUG */