Disallow empty passwords in LDAP authentication, the same way
[PostgreSQL.git] / src / backend / optimizer / path / allpaths.c
blob942ab465973a0f2e0b6f26a79831559a1803a9ec
1 /*-------------------------------------------------------------------------
3 * allpaths.c
4 * Routines to find possible search paths for processing a query
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
16 #include "postgres.h"
18 #include <math.h>
20 #include "nodes/nodeFuncs.h"
21 #ifdef OPTIMIZER_DEBUG
22 #include "nodes/print.h"
23 #endif
24 #include "optimizer/clauses.h"
25 #include "optimizer/cost.h"
26 #include "optimizer/geqo.h"
27 #include "optimizer/pathnode.h"
28 #include "optimizer/paths.h"
29 #include "optimizer/plancat.h"
30 #include "optimizer/planner.h"
31 #include "optimizer/prep.h"
32 #include "optimizer/var.h"
33 #include "parser/parse_clause.h"
34 #include "parser/parsetree.h"
35 #include "rewrite/rewriteManip.h"
38 /* These parameters are set by GUC */
39 bool enable_geqo = false; /* just in case GUC doesn't set it */
40 int geqo_threshold;
42 /* Hook for plugins to replace standard_join_search() */
43 join_search_hook_type join_search_hook = NULL;
46 static void set_base_rel_pathlists(PlannerInfo *root);
47 static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
48 Index rti, RangeTblEntry *rte);
49 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
50 RangeTblEntry *rte);
51 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
52 Index rti, RangeTblEntry *rte);
53 static void set_dummy_rel_pathlist(RelOptInfo *rel);
54 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
55 Index rti, RangeTblEntry *rte);
56 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
57 RangeTblEntry *rte);
58 static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
59 RangeTblEntry *rte);
60 static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
61 RangeTblEntry *rte);
62 static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
63 RangeTblEntry *rte);
64 static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
65 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
66 bool *differentTypes);
67 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
68 bool *differentTypes);
69 static void compare_tlist_datatypes(List *tlist, List *colTypes,
70 bool *differentTypes);
71 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
72 bool *differentTypes);
73 static void subquery_push_qual(Query *subquery,
74 RangeTblEntry *rte, Index rti, Node *qual);
75 static void recurse_push_qual(Node *setOp, Query *topquery,
76 RangeTblEntry *rte, Index rti, Node *qual);
80 * make_one_rel
81 * Finds all possible access paths for executing a query, returning a
82 * single rel that represents the join of all base rels in the query.
84 RelOptInfo *
85 make_one_rel(PlannerInfo *root, List *joinlist)
87 RelOptInfo *rel;
90 * Generate access paths for the base rels.
92 set_base_rel_pathlists(root);
95 * Generate access paths for the entire join tree.
97 rel = make_rel_from_joinlist(root, joinlist);
100 * The result should join all and only the query's base rels.
102 #ifdef USE_ASSERT_CHECKING
104 int num_base_rels = 0;
105 Index rti;
107 for (rti = 1; rti < root->simple_rel_array_size; rti++)
109 RelOptInfo *brel = root->simple_rel_array[rti];
111 if (brel == NULL)
112 continue;
114 Assert(brel->relid == rti); /* sanity check on array */
116 /* ignore RTEs that are "other rels" */
117 if (brel->reloptkind != RELOPT_BASEREL)
118 continue;
120 Assert(bms_is_member(rti, rel->relids));
121 num_base_rels++;
124 Assert(bms_num_members(rel->relids) == num_base_rels);
126 #endif
128 return rel;
132 * set_base_rel_pathlists
133 * Finds all paths available for scanning each base-relation entry.
134 * Sequential scan and any available indices are considered.
135 * Each useful path is attached to its relation's 'pathlist' field.
137 static void
138 set_base_rel_pathlists(PlannerInfo *root)
140 Index rti;
142 for (rti = 1; rti < root->simple_rel_array_size; rti++)
144 RelOptInfo *rel = root->simple_rel_array[rti];
146 /* there may be empty slots corresponding to non-baserel RTEs */
147 if (rel == NULL)
148 continue;
150 Assert(rel->relid == rti); /* sanity check on array */
152 /* ignore RTEs that are "other rels" */
153 if (rel->reloptkind != RELOPT_BASEREL)
154 continue;
156 set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
161 * set_rel_pathlist
162 * Build access paths for a base relation
164 static void
165 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
166 Index rti, RangeTblEntry *rte)
168 if (rte->inh)
170 /* It's an "append relation", process accordingly */
171 set_append_rel_pathlist(root, rel, rti, rte);
173 else if (rel->rtekind == RTE_SUBQUERY)
175 /* Subquery --- generate a separate plan for it */
176 set_subquery_pathlist(root, rel, rti, rte);
178 else if (rel->rtekind == RTE_FUNCTION)
180 /* RangeFunction --- generate a suitable path for it */
181 set_function_pathlist(root, rel, rte);
183 else if (rel->rtekind == RTE_VALUES)
185 /* Values list --- generate a suitable path for it */
186 set_values_pathlist(root, rel, rte);
188 else if (rel->rtekind == RTE_CTE)
190 /* CTE reference --- generate a suitable path for it */
191 if (rte->self_reference)
192 set_worktable_pathlist(root, rel, rte);
193 else
194 set_cte_pathlist(root, rel, rte);
196 else
198 /* Plain relation */
199 Assert(rel->rtekind == RTE_RELATION);
200 set_plain_rel_pathlist(root, rel, rte);
203 #ifdef OPTIMIZER_DEBUG
204 debug_print_rel(root, rel);
205 #endif
209 * set_plain_rel_pathlist
210 * Build access paths for a plain relation (no subquery, no inheritance)
212 static void
213 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
216 * If we can prove we don't need to scan the rel via constraint exclusion,
217 * set up a single dummy path for it. We only need to check for regular
218 * baserels; if it's an otherrel, CE was already checked in
219 * set_append_rel_pathlist().
221 if (rel->reloptkind == RELOPT_BASEREL &&
222 relation_excluded_by_constraints(root, rel, rte))
224 set_dummy_rel_pathlist(rel);
225 return;
229 * Test any partial indexes of rel for applicability. We must do this
230 * first since partial unique indexes can affect size estimates.
232 check_partial_indexes(root, rel);
234 /* Mark rel with estimated output rows, width, etc */
235 set_baserel_size_estimates(root, rel);
238 * Check to see if we can extract any restriction conditions from join
239 * quals that are OR-of-AND structures. If so, add them to the rel's
240 * restriction list, and redo the above steps.
242 if (create_or_index_quals(root, rel))
244 check_partial_indexes(root, rel);
245 set_baserel_size_estimates(root, rel);
249 * Generate paths and add them to the rel's pathlist.
251 * Note: add_path() will discard any paths that are dominated by another
252 * available path, keeping only those paths that are superior along at
253 * least one dimension of cost or sortedness.
256 /* Consider sequential scan */
257 add_path(rel, create_seqscan_path(root, rel));
259 /* Consider index scans */
260 create_index_paths(root, rel);
262 /* Consider TID scans */
263 create_tidscan_paths(root, rel);
265 /* Now find the cheapest of the paths for this rel */
266 set_cheapest(rel);
270 * set_append_rel_pathlist
271 * Build access paths for an "append relation"
273 * The passed-in rel and RTE represent the entire append relation. The
274 * relation's contents are computed by appending together the output of
275 * the individual member relations. Note that in the inheritance case,
276 * the first member relation is actually the same table as is mentioned in
277 * the parent RTE ... but it has a different RTE and RelOptInfo. This is
278 * a good thing because their outputs are not the same size.
280 static void
281 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
282 Index rti, RangeTblEntry *rte)
284 int parentRTindex = rti;
285 List *subpaths = NIL;
286 double parent_rows;
287 double parent_size;
288 double *parent_attrsizes;
289 int nattrs;
290 ListCell *l;
293 * Initialize to compute size estimates for whole append relation.
295 * We handle width estimates by weighting the widths of different child
296 * rels proportionally to their number of rows. This is sensible because
297 * the use of width estimates is mainly to compute the total relation
298 * "footprint" if we have to sort or hash it. To do this, we sum the
299 * total equivalent size (in "double" arithmetic) and then divide by the
300 * total rowcount estimate. This is done separately for the total rel
301 * width and each attribute.
303 * Note: if you consider changing this logic, beware that child rels could
304 * have zero rows and/or width, if they were excluded by constraints.
306 parent_rows = 0;
307 parent_size = 0;
308 nattrs = rel->max_attr - rel->min_attr + 1;
309 parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
312 * Generate access paths for each member relation, and pick the cheapest
313 * path for each one.
315 foreach(l, root->append_rel_list)
317 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
318 int childRTindex;
319 RangeTblEntry *childRTE;
320 RelOptInfo *childrel;
321 Path *childpath;
322 ListCell *parentvars;
323 ListCell *childvars;
325 /* append_rel_list contains all append rels; ignore others */
326 if (appinfo->parent_relid != parentRTindex)
327 continue;
329 childRTindex = appinfo->child_relid;
330 childRTE = root->simple_rte_array[childRTindex];
333 * The child rel's RelOptInfo was already created during
334 * add_base_rels_to_query.
336 childrel = find_base_rel(root, childRTindex);
337 Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
340 * We have to copy the parent's targetlist and quals to the child,
341 * with appropriate substitution of variables. However, only the
342 * baserestrictinfo quals are needed before we can check for
343 * constraint exclusion; so do that first and then check to see if we
344 * can disregard this child.
346 childrel->baserestrictinfo = (List *)
347 adjust_appendrel_attrs((Node *) rel->baserestrictinfo,
348 appinfo);
350 if (relation_excluded_by_constraints(root, childrel, childRTE))
353 * This child need not be scanned, so we can omit it from the
354 * appendrel. Mark it with a dummy cheapest-path though, in case
355 * best_appendrel_indexscan() looks at it later.
357 set_dummy_rel_pathlist(childrel);
358 continue;
361 /* CE failed, so finish copying targetlist and join quals */
362 childrel->joininfo = (List *)
363 adjust_appendrel_attrs((Node *) rel->joininfo,
364 appinfo);
365 childrel->reltargetlist = (List *)
366 adjust_appendrel_attrs((Node *) rel->reltargetlist,
367 appinfo);
370 * We have to make child entries in the EquivalenceClass data
371 * structures as well.
373 if (rel->has_eclass_joins)
375 add_child_rel_equivalences(root, appinfo, rel, childrel);
376 childrel->has_eclass_joins = true;
380 * Note: we could compute appropriate attr_needed data for the child's
381 * variables, by transforming the parent's attr_needed through the
382 * translated_vars mapping. However, currently there's no need
383 * because attr_needed is only examined for base relations not
384 * otherrels. So we just leave the child's attr_needed empty.
388 * Compute the child's access paths, and add the cheapest one to the
389 * Append path we are constructing for the parent.
391 * It's possible that the child is itself an appendrel, in which case
392 * we can "cut out the middleman" and just add its child paths to our
393 * own list. (We don't try to do this earlier because we need to
394 * apply both levels of transformation to the quals.)
396 set_rel_pathlist(root, childrel, childRTindex, childRTE);
398 childpath = childrel->cheapest_total_path;
399 if (IsA(childpath, AppendPath))
400 subpaths = list_concat(subpaths,
401 ((AppendPath *) childpath)->subpaths);
402 else
403 subpaths = lappend(subpaths, childpath);
406 * Accumulate size information from each child.
408 if (childrel->rows > 0)
410 parent_rows += childrel->rows;
411 parent_size += childrel->width * childrel->rows;
413 forboth(parentvars, rel->reltargetlist,
414 childvars, childrel->reltargetlist)
416 Var *parentvar = (Var *) lfirst(parentvars);
417 Var *childvar = (Var *) lfirst(childvars);
420 * Accumulate per-column estimates too. Whole-row Vars and
421 * PlaceHolderVars can be ignored here.
423 if (IsA(parentvar, Var) &&
424 IsA(childvar, Var))
426 int pndx = parentvar->varattno - rel->min_attr;
427 int cndx = childvar->varattno - childrel->min_attr;
429 parent_attrsizes[pndx] += childrel->attr_widths[cndx] * childrel->rows;
436 * Save the finished size estimates.
438 rel->rows = parent_rows;
439 if (parent_rows > 0)
441 int i;
443 rel->width = rint(parent_size / parent_rows);
444 for (i = 0; i < nattrs; i++)
445 rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
447 else
448 rel->width = 0; /* attr_widths should be zero already */
451 * Set "raw tuples" count equal to "rows" for the appendrel; needed
452 * because some places assume rel->tuples is valid for any baserel.
454 rel->tuples = parent_rows;
456 pfree(parent_attrsizes);
459 * Finally, build Append path and install it as the only access path for
460 * the parent rel. (Note: this is correct even if we have zero or one
461 * live subpath due to constraint exclusion.)
463 add_path(rel, (Path *) create_append_path(rel, subpaths));
465 /* Select cheapest path (pretty easy in this case...) */
466 set_cheapest(rel);
470 * set_dummy_rel_pathlist
471 * Build a dummy path for a relation that's been excluded by constraints
473 * Rather than inventing a special "dummy" path type, we represent this as an
474 * AppendPath with no members (see also IS_DUMMY_PATH macro).
476 static void
477 set_dummy_rel_pathlist(RelOptInfo *rel)
479 /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
480 rel->rows = 0;
481 rel->width = 0;
483 add_path(rel, (Path *) create_append_path(rel, NIL));
485 /* Select cheapest path (pretty easy in this case...) */
486 set_cheapest(rel);
489 /* quick-and-dirty test to see if any joining is needed */
490 static bool
491 has_multiple_baserels(PlannerInfo *root)
493 int num_base_rels = 0;
494 Index rti;
496 for (rti = 1; rti < root->simple_rel_array_size; rti++)
498 RelOptInfo *brel = root->simple_rel_array[rti];
500 if (brel == NULL)
501 continue;
503 /* ignore RTEs that are "other rels" */
504 if (brel->reloptkind == RELOPT_BASEREL)
505 if (++num_base_rels > 1)
506 return true;
508 return false;
512 * set_subquery_pathlist
513 * Build the (single) access path for a subquery RTE
515 static void
516 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
517 Index rti, RangeTblEntry *rte)
519 Query *parse = root->parse;
520 Query *subquery = rte->subquery;
521 bool *differentTypes;
522 double tuple_fraction;
523 PlannerInfo *subroot;
524 List *pathkeys;
527 * Must copy the Query so that planning doesn't mess up the RTE contents
528 * (really really need to fix the planner to not scribble on its input,
529 * someday).
531 subquery = copyObject(subquery);
533 /* We need a workspace for keeping track of set-op type coercions */
534 differentTypes = (bool *)
535 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
538 * If there are any restriction clauses that have been attached to the
539 * subquery relation, consider pushing them down to become WHERE or HAVING
540 * quals of the subquery itself. This transformation is useful because it
541 * may allow us to generate a better plan for the subquery than evaluating
542 * all the subquery output rows and then filtering them.
544 * There are several cases where we cannot push down clauses. Restrictions
545 * involving the subquery are checked by subquery_is_pushdown_safe().
546 * Restrictions on individual clauses are checked by
547 * qual_is_pushdown_safe(). Also, we don't want to push down
548 * pseudoconstant clauses; better to have the gating node above the
549 * subquery.
551 * Non-pushed-down clauses will get evaluated as qpquals of the
552 * SubqueryScan node.
554 * XXX Are there any cases where we want to make a policy decision not to
555 * push down a pushable qual, because it'd result in a worse plan?
557 if (rel->baserestrictinfo != NIL &&
558 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
560 /* OK to consider pushing down individual quals */
561 List *upperrestrictlist = NIL;
562 ListCell *l;
564 foreach(l, rel->baserestrictinfo)
566 RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
567 Node *clause = (Node *) rinfo->clause;
569 if (!rinfo->pseudoconstant &&
570 qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
572 /* Push it down */
573 subquery_push_qual(subquery, rte, rti, clause);
575 else
577 /* Keep it in the upper query */
578 upperrestrictlist = lappend(upperrestrictlist, rinfo);
581 rel->baserestrictinfo = upperrestrictlist;
584 pfree(differentTypes);
587 * We can safely pass the outer tuple_fraction down to the subquery if the
588 * outer level has no joining, aggregation, or sorting to do. Otherwise
589 * we'd better tell the subquery to plan for full retrieval. (XXX This
590 * could probably be made more intelligent ...)
592 if (parse->hasAggs ||
593 parse->groupClause ||
594 parse->havingQual ||
595 parse->distinctClause ||
596 parse->sortClause ||
597 has_multiple_baserels(root))
598 tuple_fraction = 0.0; /* default case */
599 else
600 tuple_fraction = root->tuple_fraction;
602 /* Generate the plan for the subquery */
603 rel->subplan = subquery_planner(root->glob, subquery,
604 root,
605 false, tuple_fraction,
606 &subroot);
607 rel->subrtable = subroot->parse->rtable;
609 /* Copy number of output rows from subplan */
610 rel->tuples = rel->subplan->plan_rows;
612 /* Mark rel with estimated output rows, width, etc */
613 set_baserel_size_estimates(root, rel);
615 /* Convert subquery pathkeys to outer representation */
616 pathkeys = convert_subquery_pathkeys(root, rel, subroot->query_pathkeys);
618 /* Generate appropriate path */
619 add_path(rel, create_subqueryscan_path(rel, pathkeys));
621 /* Select cheapest path (pretty easy in this case...) */
622 set_cheapest(rel);
626 * set_function_pathlist
627 * Build the (single) access path for a function RTE
629 static void
630 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
632 /* Mark rel with estimated output rows, width, etc */
633 set_function_size_estimates(root, rel);
635 /* Generate appropriate path */
636 add_path(rel, create_functionscan_path(root, rel));
638 /* Select cheapest path (pretty easy in this case...) */
639 set_cheapest(rel);
643 * set_values_pathlist
644 * Build the (single) access path for a VALUES RTE
646 static void
647 set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
649 /* Mark rel with estimated output rows, width, etc */
650 set_values_size_estimates(root, rel);
652 /* Generate appropriate path */
653 add_path(rel, create_valuesscan_path(root, rel));
655 /* Select cheapest path (pretty easy in this case...) */
656 set_cheapest(rel);
660 * set_cte_pathlist
661 * Build the (single) access path for a non-self-reference CTE RTE
663 static void
664 set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
666 Plan *cteplan;
667 PlannerInfo *cteroot;
668 Index levelsup;
669 int ndx;
670 ListCell *lc;
671 int plan_id;
674 * Find the referenced CTE, and locate the plan previously made for it.
676 levelsup = rte->ctelevelsup;
677 cteroot = root;
678 while (levelsup-- > 0)
680 cteroot = cteroot->parent_root;
681 if (!cteroot) /* shouldn't happen */
682 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
686 * Note: cte_plan_ids can be shorter than cteList, if we are still working
687 * on planning the CTEs (ie, this is a side-reference from another CTE).
688 * So we mustn't use forboth here.
690 ndx = 0;
691 foreach(lc, cteroot->parse->cteList)
693 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
695 if (strcmp(cte->ctename, rte->ctename) == 0)
696 break;
697 ndx++;
699 if (lc == NULL) /* shouldn't happen */
700 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
701 if (ndx >= list_length(cteroot->cte_plan_ids))
702 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
703 plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
704 Assert(plan_id > 0);
705 cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
707 /* Mark rel with estimated output rows, width, etc */
708 set_cte_size_estimates(root, rel, cteplan);
710 /* Generate appropriate path */
711 add_path(rel, create_ctescan_path(root, rel));
713 /* Select cheapest path (pretty easy in this case...) */
714 set_cheapest(rel);
718 * set_worktable_pathlist
719 * Build the (single) access path for a self-reference CTE RTE
721 static void
722 set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
724 Plan *cteplan;
725 PlannerInfo *cteroot;
726 Index levelsup;
729 * We need to find the non-recursive term's plan, which is in the plan
730 * level that's processing the recursive UNION, which is one level *below*
731 * where the CTE comes from.
733 levelsup = rte->ctelevelsup;
734 if (levelsup == 0) /* shouldn't happen */
735 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
736 levelsup--;
737 cteroot = root;
738 while (levelsup-- > 0)
740 cteroot = cteroot->parent_root;
741 if (!cteroot) /* shouldn't happen */
742 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
744 cteplan = cteroot->non_recursive_plan;
745 if (!cteplan) /* shouldn't happen */
746 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
748 /* Mark rel with estimated output rows, width, etc */
749 set_cte_size_estimates(root, rel, cteplan);
751 /* Generate appropriate path */
752 add_path(rel, create_worktablescan_path(root, rel));
754 /* Select cheapest path (pretty easy in this case...) */
755 set_cheapest(rel);
759 * make_rel_from_joinlist
760 * Build access paths using a "joinlist" to guide the join path search.
762 * See comments for deconstruct_jointree() for definition of the joinlist
763 * data structure.
765 static RelOptInfo *
766 make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
768 int levels_needed;
769 List *initial_rels;
770 ListCell *jl;
773 * Count the number of child joinlist nodes. This is the depth of the
774 * dynamic-programming algorithm we must employ to consider all ways of
775 * joining the child nodes.
777 levels_needed = list_length(joinlist);
779 if (levels_needed <= 0)
780 return NULL; /* nothing to do? */
783 * Construct a list of rels corresponding to the child joinlist nodes.
784 * This may contain both base rels and rels constructed according to
785 * sub-joinlists.
787 initial_rels = NIL;
788 foreach(jl, joinlist)
790 Node *jlnode = (Node *) lfirst(jl);
791 RelOptInfo *thisrel;
793 if (IsA(jlnode, RangeTblRef))
795 int varno = ((RangeTblRef *) jlnode)->rtindex;
797 thisrel = find_base_rel(root, varno);
799 else if (IsA(jlnode, List))
801 /* Recurse to handle subproblem */
802 thisrel = make_rel_from_joinlist(root, (List *) jlnode);
804 else
806 elog(ERROR, "unrecognized joinlist node type: %d",
807 (int) nodeTag(jlnode));
808 thisrel = NULL; /* keep compiler quiet */
811 initial_rels = lappend(initial_rels, thisrel);
814 if (levels_needed == 1)
817 * Single joinlist node, so we're done.
819 return (RelOptInfo *) linitial(initial_rels);
821 else
824 * Consider the different orders in which we could join the rels,
825 * using a plugin, GEQO, or the regular join search code.
827 * We put the initial_rels list into a PlannerInfo field because
828 * has_legal_joinclause() needs to look at it (ugly :-().
830 root->initial_rels = initial_rels;
832 if (join_search_hook)
833 return (*join_search_hook) (root, levels_needed, initial_rels);
834 else if (enable_geqo && levels_needed >= geqo_threshold)
835 return geqo(root, levels_needed, initial_rels);
836 else
837 return standard_join_search(root, levels_needed, initial_rels);
842 * standard_join_search
843 * Find possible joinpaths for a query by successively finding ways
844 * to join component relations into join relations.
846 * 'levels_needed' is the number of iterations needed, ie, the number of
847 * independent jointree items in the query. This is > 1.
849 * 'initial_rels' is a list of RelOptInfo nodes for each independent
850 * jointree item. These are the components to be joined together.
851 * Note that levels_needed == list_length(initial_rels).
853 * Returns the final level of join relations, i.e., the relation that is
854 * the result of joining all the original relations together.
855 * At least one implementation path must be provided for this relation and
856 * all required sub-relations.
858 * To support loadable plugins that modify planner behavior by changing the
859 * join searching algorithm, we provide a hook variable that lets a plugin
860 * replace or supplement this function. Any such hook must return the same
861 * final join relation as the standard code would, but it might have a
862 * different set of implementation paths attached, and only the sub-joinrels
863 * needed for these paths need have been instantiated.
865 * Note to plugin authors: the functions invoked during standard_join_search()
866 * modify root->join_rel_list and root->join_rel_hash. If you want to do more
867 * than one join-order search, you'll probably need to save and restore the
868 * original states of those data structures. See geqo_eval() for an example.
870 RelOptInfo *
871 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
873 List **joinitems;
874 int lev;
875 RelOptInfo *rel;
878 * We employ a simple "dynamic programming" algorithm: we first find all
879 * ways to build joins of two jointree items, then all ways to build joins
880 * of three items (from two-item joins and single items), then four-item
881 * joins, and so on until we have considered all ways to join all the
882 * items into one rel.
884 * joinitems[j] is a list of all the j-item rels. Initially we set
885 * joinitems[1] to represent all the single-jointree-item relations.
887 joinitems = (List **) palloc0((levels_needed + 1) * sizeof(List *));
889 joinitems[1] = initial_rels;
891 for (lev = 2; lev <= levels_needed; lev++)
893 ListCell *x;
896 * Determine all possible pairs of relations to be joined at this
897 * level, and build paths for making each one from every available
898 * pair of lower-level relations.
900 joinitems[lev] = join_search_one_level(root, lev, joinitems);
903 * Do cleanup work on each just-processed rel.
905 foreach(x, joinitems[lev])
907 rel = (RelOptInfo *) lfirst(x);
909 /* Find and save the cheapest paths for this rel */
910 set_cheapest(rel);
912 #ifdef OPTIMIZER_DEBUG
913 debug_print_rel(root, rel);
914 #endif
919 * We should have a single rel at the final level.
921 if (joinitems[levels_needed] == NIL)
922 elog(ERROR, "failed to build any %d-way joins", levels_needed);
923 Assert(list_length(joinitems[levels_needed]) == 1);
925 rel = (RelOptInfo *) linitial(joinitems[levels_needed]);
927 return rel;
930 /*****************************************************************************
931 * PUSHING QUALS DOWN INTO SUBQUERIES
932 *****************************************************************************/
935 * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
937 * subquery is the particular component query being checked. topquery
938 * is the top component of a set-operations tree (the same Query if no
939 * set-op is involved).
941 * Conditions checked here:
943 * 1. If the subquery has a LIMIT clause, we must not push down any quals,
944 * since that could change the set of rows returned.
946 * 2. If the subquery contains any window functions, we can't push quals
947 * into it, because that would change the results.
949 * 3. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
950 * quals into it, because that would change the results.
952 * 4. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
953 * push quals into each component query, but the quals can only reference
954 * subquery columns that suffer no type coercions in the set operation.
955 * Otherwise there are possible semantic gotchas. So, we check the
956 * component queries to see if any of them have different output types;
957 * differentTypes[k] is set true if column k has different type in any
958 * component.
960 static bool
961 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
962 bool *differentTypes)
964 SetOperationStmt *topop;
966 /* Check point 1 */
967 if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
968 return false;
970 /* Check point 2 */
971 if (subquery->hasWindowFuncs)
972 return false;
974 /* Are we at top level, or looking at a setop component? */
975 if (subquery == topquery)
977 /* Top level, so check any component queries */
978 if (subquery->setOperations != NULL)
979 if (!recurse_pushdown_safe(subquery->setOperations, topquery,
980 differentTypes))
981 return false;
983 else
985 /* Setop component must not have more components (too weird) */
986 if (subquery->setOperations != NULL)
987 return false;
988 /* Check whether setop component output types match top level */
989 topop = (SetOperationStmt *) topquery->setOperations;
990 Assert(topop && IsA(topop, SetOperationStmt));
991 compare_tlist_datatypes(subquery->targetList,
992 topop->colTypes,
993 differentTypes);
995 return true;
999 * Helper routine to recurse through setOperations tree
1001 static bool
1002 recurse_pushdown_safe(Node *setOp, Query *topquery,
1003 bool *differentTypes)
1005 if (IsA(setOp, RangeTblRef))
1007 RangeTblRef *rtr = (RangeTblRef *) setOp;
1008 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
1009 Query *subquery = rte->subquery;
1011 Assert(subquery != NULL);
1012 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
1014 else if (IsA(setOp, SetOperationStmt))
1016 SetOperationStmt *op = (SetOperationStmt *) setOp;
1018 /* EXCEPT is no good */
1019 if (op->op == SETOP_EXCEPT)
1020 return false;
1021 /* Else recurse */
1022 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
1023 return false;
1024 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
1025 return false;
1027 else
1029 elog(ERROR, "unrecognized node type: %d",
1030 (int) nodeTag(setOp));
1032 return true;
1036 * Compare tlist's datatypes against the list of set-operation result types.
1037 * For any items that are different, mark the appropriate element of
1038 * differentTypes[] to show that this column will have type conversions.
1040 * We don't have to care about typmods here: the only allowed difference
1041 * between set-op input and output typmods is input is a specific typmod
1042 * and output is -1, and that does not require a coercion.
1044 static void
1045 compare_tlist_datatypes(List *tlist, List *colTypes,
1046 bool *differentTypes)
1048 ListCell *l;
1049 ListCell *colType = list_head(colTypes);
1051 foreach(l, tlist)
1053 TargetEntry *tle = (TargetEntry *) lfirst(l);
1055 if (tle->resjunk)
1056 continue; /* ignore resjunk columns */
1057 if (colType == NULL)
1058 elog(ERROR, "wrong number of tlist entries");
1059 if (exprType((Node *) tle->expr) != lfirst_oid(colType))
1060 differentTypes[tle->resno] = true;
1061 colType = lnext(colType);
1063 if (colType != NULL)
1064 elog(ERROR, "wrong number of tlist entries");
1068 * qual_is_pushdown_safe - is a particular qual safe to push down?
1070 * qual is a restriction clause applying to the given subquery (whose RTE
1071 * has index rti in the parent query).
1073 * Conditions checked here:
1075 * 1. The qual must not contain any subselects (mainly because I'm not sure
1076 * it will work correctly: sublinks will already have been transformed into
1077 * subplans in the qual, but not in the subquery).
1079 * 2. The qual must not refer to the whole-row output of the subquery
1080 * (since there is no easy way to name that within the subquery itself).
1082 * 3. The qual must not refer to any subquery output columns that were
1083 * found to have inconsistent types across a set operation tree by
1084 * subquery_is_pushdown_safe().
1086 * 4. If the subquery uses DISTINCT ON, we must not push down any quals that
1087 * refer to non-DISTINCT output columns, because that could change the set
1088 * of rows returned. (This condition is vacuous for DISTINCT, because then
1089 * there are no non-DISTINCT output columns, so we needn't check. But note
1090 * we are assuming that the qual can't distinguish values that the DISTINCT
1091 * operator sees as equal. This is a bit shaky but we have no way to test
1092 * for the case, and it's unlikely enough that we shouldn't refuse the
1093 * optimization just because it could theoretically happen.)
1095 * 5. We must not push down any quals that refer to subselect outputs that
1096 * return sets, else we'd introduce functions-returning-sets into the
1097 * subquery's WHERE/HAVING quals.
1099 * 6. We must not push down any quals that refer to subselect outputs that
1100 * contain volatile functions, for fear of introducing strange results due
1101 * to multiple evaluation of a volatile function.
1103 static bool
1104 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
1105 bool *differentTypes)
1107 bool safe = true;
1108 List *vars;
1109 ListCell *vl;
1110 Bitmapset *tested = NULL;
1112 /* Refuse subselects (point 1) */
1113 if (contain_subplans(qual))
1114 return false;
1117 * It would be unsafe to push down window function calls, but at least for
1118 * the moment we could never see any in a qual anyhow.
1120 Assert(!contain_window_function(qual));
1123 * Examine all Vars used in clause; since it's a restriction clause, all
1124 * such Vars must refer to subselect output columns.
1126 vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
1127 foreach(vl, vars)
1129 Var *var = (Var *) lfirst(vl);
1130 TargetEntry *tle;
1133 * XXX Punt if we find any PlaceHolderVars in the restriction clause.
1134 * It's not clear whether a PHV could safely be pushed down, and even
1135 * less clear whether such a situation could arise in any cases of
1136 * practical interest anyway. So for the moment, just refuse to push
1137 * down.
1139 if (!IsA(var, Var))
1141 safe = false;
1142 break;
1145 Assert(var->varno == rti);
1147 /* Check point 2 */
1148 if (var->varattno == 0)
1150 safe = false;
1151 break;
1155 * We use a bitmapset to avoid testing the same attno more than once.
1156 * (NB: this only works because subquery outputs can't have negative
1157 * attnos.)
1159 if (bms_is_member(var->varattno, tested))
1160 continue;
1161 tested = bms_add_member(tested, var->varattno);
1163 /* Check point 3 */
1164 if (differentTypes[var->varattno])
1166 safe = false;
1167 break;
1170 /* Must find the tlist element referenced by the Var */
1171 tle = get_tle_by_resno(subquery->targetList, var->varattno);
1172 Assert(tle != NULL);
1173 Assert(!tle->resjunk);
1175 /* If subquery uses DISTINCT ON, check point 4 */
1176 if (subquery->hasDistinctOn &&
1177 !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
1179 /* non-DISTINCT column, so fail */
1180 safe = false;
1181 break;
1184 /* Refuse functions returning sets (point 5) */
1185 if (expression_returns_set((Node *) tle->expr))
1187 safe = false;
1188 break;
1191 /* Refuse volatile functions (point 6) */
1192 if (contain_volatile_functions((Node *) tle->expr))
1194 safe = false;
1195 break;
1199 list_free(vars);
1200 bms_free(tested);
1202 return safe;
1206 * subquery_push_qual - push down a qual that we have determined is safe
1208 static void
1209 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
1211 if (subquery->setOperations != NULL)
1213 /* Recurse to push it separately to each component query */
1214 recurse_push_qual(subquery->setOperations, subquery,
1215 rte, rti, qual);
1217 else
1220 * We need to replace Vars in the qual (which must refer to outputs of
1221 * the subquery) with copies of the subquery's targetlist expressions.
1222 * Note that at this point, any uplevel Vars in the qual should have
1223 * been replaced with Params, so they need no work.
1225 * This step also ensures that when we are pushing into a setop tree,
1226 * each component query gets its own copy of the qual.
1228 qual = ResolveNew(qual, rti, 0, rte,
1229 subquery->targetList,
1230 CMD_SELECT, 0);
1233 * Now attach the qual to the proper place: normally WHERE, but if the
1234 * subquery uses grouping or aggregation, put it in HAVING (since the
1235 * qual really refers to the group-result rows).
1237 if (subquery->hasAggs || subquery->groupClause || subquery->havingQual)
1238 subquery->havingQual = make_and_qual(subquery->havingQual, qual);
1239 else
1240 subquery->jointree->quals =
1241 make_and_qual(subquery->jointree->quals, qual);
1244 * We need not change the subquery's hasAggs or hasSublinks flags,
1245 * since we can't be pushing down any aggregates that weren't there
1246 * before, and we don't push down subselects at all.
1252 * Helper routine to recurse through setOperations tree
1254 static void
1255 recurse_push_qual(Node *setOp, Query *topquery,
1256 RangeTblEntry *rte, Index rti, Node *qual)
1258 if (IsA(setOp, RangeTblRef))
1260 RangeTblRef *rtr = (RangeTblRef *) setOp;
1261 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
1262 Query *subquery = subrte->subquery;
1264 Assert(subquery != NULL);
1265 subquery_push_qual(subquery, rte, rti, qual);
1267 else if (IsA(setOp, SetOperationStmt))
1269 SetOperationStmt *op = (SetOperationStmt *) setOp;
1271 recurse_push_qual(op->larg, topquery, rte, rti, qual);
1272 recurse_push_qual(op->rarg, topquery, rte, rti, qual);
1274 else
1276 elog(ERROR, "unrecognized node type: %d",
1277 (int) nodeTag(setOp));
1281 /*****************************************************************************
1282 * DEBUG SUPPORT
1283 *****************************************************************************/
1285 #ifdef OPTIMIZER_DEBUG
1287 static void
1288 print_relids(Relids relids)
1290 Relids tmprelids;
1291 int x;
1292 bool first = true;
1294 tmprelids = bms_copy(relids);
1295 while ((x = bms_first_member(tmprelids)) >= 0)
1297 if (!first)
1298 printf(" ");
1299 printf("%d", x);
1300 first = false;
1302 bms_free(tmprelids);
1305 static void
1306 print_restrictclauses(PlannerInfo *root, List *clauses)
1308 ListCell *l;
1310 foreach(l, clauses)
1312 RestrictInfo *c = lfirst(l);
1314 print_expr((Node *) c->clause, root->parse->rtable);
1315 if (lnext(l))
1316 printf(", ");
1320 static void
1321 print_path(PlannerInfo *root, Path *path, int indent)
1323 const char *ptype;
1324 bool join = false;
1325 Path *subpath = NULL;
1326 int i;
1328 switch (nodeTag(path))
1330 case T_Path:
1331 ptype = "SeqScan";
1332 break;
1333 case T_IndexPath:
1334 ptype = "IdxScan";
1335 break;
1336 case T_BitmapHeapPath:
1337 ptype = "BitmapHeapScan";
1338 break;
1339 case T_BitmapAndPath:
1340 ptype = "BitmapAndPath";
1341 break;
1342 case T_BitmapOrPath:
1343 ptype = "BitmapOrPath";
1344 break;
1345 case T_TidPath:
1346 ptype = "TidScan";
1347 break;
1348 case T_AppendPath:
1349 ptype = "Append";
1350 break;
1351 case T_ResultPath:
1352 ptype = "Result";
1353 break;
1354 case T_MaterialPath:
1355 ptype = "Material";
1356 subpath = ((MaterialPath *) path)->subpath;
1357 break;
1358 case T_UniquePath:
1359 ptype = "Unique";
1360 subpath = ((UniquePath *) path)->subpath;
1361 break;
1362 case T_NestPath:
1363 ptype = "NestLoop";
1364 join = true;
1365 break;
1366 case T_MergePath:
1367 ptype = "MergeJoin";
1368 join = true;
1369 break;
1370 case T_HashPath:
1371 ptype = "HashJoin";
1372 join = true;
1373 break;
1374 default:
1375 ptype = "???Path";
1376 break;
1379 for (i = 0; i < indent; i++)
1380 printf("\t");
1381 printf("%s", ptype);
1383 if (path->parent)
1385 printf("(");
1386 print_relids(path->parent->relids);
1387 printf(") rows=%.0f", path->parent->rows);
1389 printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
1391 if (path->pathkeys)
1393 for (i = 0; i < indent; i++)
1394 printf("\t");
1395 printf(" pathkeys: ");
1396 print_pathkeys(path->pathkeys, root->parse->rtable);
1399 if (join)
1401 JoinPath *jp = (JoinPath *) path;
1403 for (i = 0; i < indent; i++)
1404 printf("\t");
1405 printf(" clauses: ");
1406 print_restrictclauses(root, jp->joinrestrictinfo);
1407 printf("\n");
1409 if (IsA(path, MergePath))
1411 MergePath *mp = (MergePath *) path;
1413 if (mp->outersortkeys || mp->innersortkeys)
1415 for (i = 0; i < indent; i++)
1416 printf("\t");
1417 printf(" sortouter=%d sortinner=%d\n",
1418 ((mp->outersortkeys) ? 1 : 0),
1419 ((mp->innersortkeys) ? 1 : 0));
1423 print_path(root, jp->outerjoinpath, indent + 1);
1424 print_path(root, jp->innerjoinpath, indent + 1);
1427 if (subpath)
1428 print_path(root, subpath, indent + 1);
1431 void
1432 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
1434 ListCell *l;
1436 printf("RELOPTINFO (");
1437 print_relids(rel->relids);
1438 printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
1440 if (rel->baserestrictinfo)
1442 printf("\tbaserestrictinfo: ");
1443 print_restrictclauses(root, rel->baserestrictinfo);
1444 printf("\n");
1447 if (rel->joininfo)
1449 printf("\tjoininfo: ");
1450 print_restrictclauses(root, rel->joininfo);
1451 printf("\n");
1454 printf("\tpath list:\n");
1455 foreach(l, rel->pathlist)
1456 print_path(root, lfirst(l), 1);
1457 printf("\n\tcheapest startup path:\n");
1458 print_path(root, rel->cheapest_startup_path, 1);
1459 printf("\n\tcheapest total path:\n");
1460 print_path(root, rel->cheapest_total_path, 1);
1461 printf("\n");
1462 fflush(stdout);
1465 #endif /* OPTIMIZER_DEBUG */