]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/path/allpaths.c
4d402ca7202a3a18897ef17bad91b54ea237ea62
[postgresql] / src / backend / optimizer / path / allpaths.c
1 /*-------------------------------------------------------------------------
2  *
3  * allpaths.c
4  *        Routines to find possible search paths for processing a query
5  *
6  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $PostgreSQL: pgsql/src/backend/optimizer/path/allpaths.c,v 1.188 2009/10/26 02:26:33 tgl Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include <math.h>
19
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/restrictinfo.h"
33 #include "optimizer/var.h"
34 #include "parser/parse_clause.h"
35 #include "parser/parsetree.h"
36 #include "rewrite/rewriteManip.h"
37
38
39 /* These parameters are set by GUC */
40 bool            enable_geqo = false;    /* just in case GUC doesn't set it */
41 int                     geqo_threshold;
42
43 /* Hook for plugins to replace standard_join_search() */
44 join_search_hook_type join_search_hook = NULL;
45
46
47 static void set_base_rel_pathlists(PlannerInfo *root);
48 static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
49                                  Index rti, RangeTblEntry *rte);
50 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
51                                            RangeTblEntry *rte);
52 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
53                                                 Index rti, RangeTblEntry *rte);
54 static void set_dummy_rel_pathlist(RelOptInfo *rel);
55 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
56                                           Index rti, RangeTblEntry *rte);
57 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
58                                           RangeTblEntry *rte);
59 static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
60                                         RangeTblEntry *rte);
61 static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
62                                  RangeTblEntry *rte);
63 static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
64                                            RangeTblEntry *rte);
65 static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
66 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
67                                                   bool *differentTypes);
68 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
69                                           bool *differentTypes);
70 static void compare_tlist_datatypes(List *tlist, List *colTypes,
71                                                 bool *differentTypes);
72 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
73                                           bool *differentTypes);
74 static void subquery_push_qual(Query *subquery,
75                                    RangeTblEntry *rte, Index rti, Node *qual);
76 static void recurse_push_qual(Node *setOp, Query *topquery,
77                                   RangeTblEntry *rte, Index rti, Node *qual);
78
79
80 /*
81  * make_one_rel
82  *        Finds all possible access paths for executing a query, returning a
83  *        single rel that represents the join of all base rels in the query.
84  */
85 RelOptInfo *
86 make_one_rel(PlannerInfo *root, List *joinlist)
87 {
88         RelOptInfo *rel;
89
90         /*
91          * Generate access paths for the base rels.
92          */
93         set_base_rel_pathlists(root);
94
95         /*
96          * Generate access paths for the entire join tree.
97          */
98         rel = make_rel_from_joinlist(root, joinlist);
99
100         /*
101          * The result should join all and only the query's base rels.
102          */
103 #ifdef USE_ASSERT_CHECKING
104         {
105                 int                     num_base_rels = 0;
106                 Index           rti;
107
108                 for (rti = 1; rti < root->simple_rel_array_size; rti++)
109                 {
110                         RelOptInfo *brel = root->simple_rel_array[rti];
111
112                         if (brel == NULL)
113                                 continue;
114
115                         Assert(brel->relid == rti); /* sanity check on array */
116
117                         /* ignore RTEs that are "other rels" */
118                         if (brel->reloptkind != RELOPT_BASEREL)
119                                 continue;
120
121                         Assert(bms_is_member(rti, rel->relids));
122                         num_base_rels++;
123                 }
124
125                 Assert(bms_num_members(rel->relids) == num_base_rels);
126         }
127 #endif
128
129         return rel;
130 }
131
132 /*
133  * set_base_rel_pathlists
134  *        Finds all paths available for scanning each base-relation entry.
135  *        Sequential scan and any available indices are considered.
136  *        Each useful path is attached to its relation's 'pathlist' field.
137  */
138 static void
139 set_base_rel_pathlists(PlannerInfo *root)
140 {
141         Index           rti;
142
143         for (rti = 1; rti < root->simple_rel_array_size; rti++)
144         {
145                 RelOptInfo *rel = root->simple_rel_array[rti];
146
147                 /* there may be empty slots corresponding to non-baserel RTEs */
148                 if (rel == NULL)
149                         continue;
150
151                 Assert(rel->relid == rti);              /* sanity check on array */
152
153                 /* ignore RTEs that are "other rels" */
154                 if (rel->reloptkind != RELOPT_BASEREL)
155                         continue;
156
157                 set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
158         }
159 }
160
161 /*
162  * set_rel_pathlist
163  *        Build access paths for a base relation
164  */
165 static void
166 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
167                                  Index rti, RangeTblEntry *rte)
168 {
169         if (rte->inh)
170         {
171                 /* It's an "append relation", process accordingly */
172                 set_append_rel_pathlist(root, rel, rti, rte);
173         }
174         else if (rel->rtekind == RTE_SUBQUERY)
175         {
176                 /* Subquery --- generate a separate plan for it */
177                 set_subquery_pathlist(root, rel, rti, rte);
178         }
179         else if (rel->rtekind == RTE_FUNCTION)
180         {
181                 /* RangeFunction --- generate a suitable path for it */
182                 set_function_pathlist(root, rel, rte);
183         }
184         else if (rel->rtekind == RTE_VALUES)
185         {
186                 /* Values list --- generate a suitable path for it */
187                 set_values_pathlist(root, rel, rte);
188         }
189         else if (rel->rtekind == RTE_CTE)
190         {
191                 /* CTE reference --- generate a suitable path for it */
192                 if (rte->self_reference)
193                         set_worktable_pathlist(root, rel, rte);
194                 else
195                         set_cte_pathlist(root, rel, rte);
196         }
197         else
198         {
199                 /* Plain relation */
200                 Assert(rel->rtekind == RTE_RELATION);
201                 set_plain_rel_pathlist(root, rel, rte);
202         }
203
204 #ifdef OPTIMIZER_DEBUG
205         debug_print_rel(root, rel);
206 #endif
207 }
208
209 /*
210  * set_plain_rel_pathlist
211  *        Build access paths for a plain relation (no subquery, no inheritance)
212  */
213 static void
214 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
215 {
216         /*
217          * If we can prove we don't need to scan the rel via constraint exclusion,
218          * set up a single dummy path for it.  We only need to check for regular
219          * baserels; if it's an otherrel, CE was already checked in
220          * set_append_rel_pathlist().
221          */
222         if (rel->reloptkind == RELOPT_BASEREL &&
223                 relation_excluded_by_constraints(root, rel, rte))
224         {
225                 set_dummy_rel_pathlist(rel);
226                 return;
227         }
228
229         /*
230          * Test any partial indexes of rel for applicability.  We must do this
231          * first since partial unique indexes can affect size estimates.
232          */
233         check_partial_indexes(root, rel);
234
235         /* Mark rel with estimated output rows, width, etc */
236         set_baserel_size_estimates(root, rel);
237
238         /*
239          * Check to see if we can extract any restriction conditions from join
240          * quals that are OR-of-AND structures.  If so, add them to the rel's
241          * restriction list, and redo the above steps.
242          */
243         if (create_or_index_quals(root, rel))
244         {
245                 check_partial_indexes(root, rel);
246                 set_baserel_size_estimates(root, rel);
247         }
248
249         /*
250          * Generate paths and add them to the rel's pathlist.
251          *
252          * Note: add_path() will discard any paths that are dominated by another
253          * available path, keeping only those paths that are superior along at
254          * least one dimension of cost or sortedness.
255          */
256
257         /* Consider sequential scan */
258         add_path(rel, create_seqscan_path(root, rel));
259
260         /* Consider index scans */
261         create_index_paths(root, rel);
262
263         /* Consider TID scans */
264         create_tidscan_paths(root, rel);
265
266         /* Now find the cheapest of the paths for this rel */
267         set_cheapest(rel);
268 }
269
270 /*
271  * set_append_rel_pathlist
272  *        Build access paths for an "append relation"
273  *
274  * The passed-in rel and RTE represent the entire append relation.      The
275  * relation's contents are computed by appending together the output of
276  * the individual member relations.  Note that in the inheritance case,
277  * the first member relation is actually the same table as is mentioned in
278  * the parent RTE ... but it has a different RTE and RelOptInfo.  This is
279  * a good thing because their outputs are not the same size.
280  */
281 static void
282 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
283                                                 Index rti, RangeTblEntry *rte)
284 {
285         int                     parentRTindex = rti;
286         List       *subpaths = NIL;
287         double          parent_rows;
288         double          parent_size;
289         double     *parent_attrsizes;
290         int                     nattrs;
291         ListCell   *l;
292
293         /*
294          * Initialize to compute size estimates for whole append relation.
295          *
296          * We handle width estimates by weighting the widths of different child
297          * rels proportionally to their number of rows.  This is sensible because
298          * the use of width estimates is mainly to compute the total relation
299          * "footprint" if we have to sort or hash it.  To do this, we sum the
300          * total equivalent size (in "double" arithmetic) and then divide by the
301          * total rowcount estimate.  This is done separately for the total rel
302          * width and each attribute.
303          *
304          * Note: if you consider changing this logic, beware that child rels could
305          * have zero rows and/or width, if they were excluded by constraints.
306          */
307         parent_rows = 0;
308         parent_size = 0;
309         nattrs = rel->max_attr - rel->min_attr + 1;
310         parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
311
312         /*
313          * Generate access paths for each member relation, and pick the cheapest
314          * path for each one.
315          */
316         foreach(l, root->append_rel_list)
317         {
318                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
319                 int                     childRTindex;
320                 RangeTblEntry *childRTE;
321                 RelOptInfo *childrel;
322                 List       *childquals;
323                 Node       *childqual;
324                 Path       *childpath;
325                 ListCell   *parentvars;
326                 ListCell   *childvars;
327
328                 /* append_rel_list contains all append rels; ignore others */
329                 if (appinfo->parent_relid != parentRTindex)
330                         continue;
331
332                 childRTindex = appinfo->child_relid;
333                 childRTE = root->simple_rte_array[childRTindex];
334
335                 /*
336                  * The child rel's RelOptInfo was already created during
337                  * add_base_rels_to_query.
338                  */
339                 childrel = find_base_rel(root, childRTindex);
340                 Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
341
342                 /*
343                  * We have to copy the parent's targetlist and quals to the child,
344                  * with appropriate substitution of variables.  However, only the
345                  * baserestrictinfo quals are needed before we can check for
346                  * constraint exclusion; so do that first and then check to see if we
347                  * can disregard this child.
348                  *
349                  * As of 8.4, the child rel's targetlist might contain non-Var
350                  * expressions, which means that substitution into the quals
351                  * could produce opportunities for const-simplification, and perhaps
352                  * even pseudoconstant quals.  To deal with this, we strip the
353                  * RestrictInfo nodes, do the substitution, do const-simplification,
354                  * and then reconstitute the RestrictInfo layer.
355                  */
356                 childquals = get_all_actual_clauses(rel->baserestrictinfo);
357                 childquals = (List *) adjust_appendrel_attrs((Node *) childquals,
358                                                                                                          appinfo);
359                 childqual = eval_const_expressions(root, (Node *)
360                                                                                    make_ands_explicit(childquals));
361                 if (childqual && IsA(childqual, Const) &&
362                         (((Const *) childqual)->constisnull ||
363                          !DatumGetBool(((Const *) childqual)->constvalue)))
364                 {
365                         /*
366                          * Restriction reduces to constant FALSE or constant NULL after
367                          * substitution, so this child need not be scanned.
368                          */
369                         set_dummy_rel_pathlist(childrel);
370                         continue;
371                 }
372                 childquals = make_ands_implicit((Expr *) childqual);
373                 childquals = make_restrictinfos_from_actual_clauses(root,
374                                                                                                                         childquals);
375                 childrel->baserestrictinfo = childquals;
376
377                 if (relation_excluded_by_constraints(root, childrel, childRTE))
378                 {
379                         /*
380                          * This child need not be scanned, so we can omit it from the
381                          * appendrel.  Mark it with a dummy cheapest-path though, in case
382                          * best_appendrel_indexscan() looks at it later.
383                          */
384                         set_dummy_rel_pathlist(childrel);
385                         continue;
386                 }
387
388                 /* CE failed, so finish copying targetlist and join quals */
389                 childrel->joininfo = (List *)
390                         adjust_appendrel_attrs((Node *) rel->joininfo,
391                                                                    appinfo);
392                 childrel->reltargetlist = (List *)
393                         adjust_appendrel_attrs((Node *) rel->reltargetlist,
394                                                                    appinfo);
395
396                 /*
397                  * We have to make child entries in the EquivalenceClass data
398                  * structures as well.
399                  */
400                 if (rel->has_eclass_joins)
401                 {
402                         add_child_rel_equivalences(root, appinfo, rel, childrel);
403                         childrel->has_eclass_joins = true;
404                 }
405
406                 /*
407                  * Note: we could compute appropriate attr_needed data for the child's
408                  * variables, by transforming the parent's attr_needed through the
409                  * translated_vars mapping.  However, currently there's no need
410                  * because attr_needed is only examined for base relations not
411                  * otherrels.  So we just leave the child's attr_needed empty.
412                  */
413
414                 /*
415                  * Compute the child's access paths, and add the cheapest one to the
416                  * Append path we are constructing for the parent.
417                  *
418                  * It's possible that the child is itself an appendrel, in which case
419                  * we can "cut out the middleman" and just add its child paths to our
420                  * own list.  (We don't try to do this earlier because we need to
421                  * apply both levels of transformation to the quals.)
422                  */
423                 set_rel_pathlist(root, childrel, childRTindex, childRTE);
424
425                 childpath = childrel->cheapest_total_path;
426                 if (IsA(childpath, AppendPath))
427                         subpaths = list_concat(subpaths,
428                                                                    ((AppendPath *) childpath)->subpaths);
429                 else
430                         subpaths = lappend(subpaths, childpath);
431
432                 /*
433                  * Accumulate size information from each child.
434                  */
435                 if (childrel->rows > 0)
436                 {
437                         parent_rows += childrel->rows;
438                         parent_size += childrel->width * childrel->rows;
439
440                         forboth(parentvars, rel->reltargetlist,
441                                         childvars, childrel->reltargetlist)
442                         {
443                                 Var                *parentvar = (Var *) lfirst(parentvars);
444                                 Var                *childvar = (Var *) lfirst(childvars);
445
446                                 /*
447                                  * Accumulate per-column estimates too.  Whole-row Vars and
448                                  * PlaceHolderVars can be ignored here.
449                                  */
450                                 if (IsA(parentvar, Var) &&
451                                         IsA(childvar, Var))
452                                 {
453                                         int                     pndx = parentvar->varattno - rel->min_attr;
454                                         int                     cndx = childvar->varattno - childrel->min_attr;
455
456                                         parent_attrsizes[pndx] += childrel->attr_widths[cndx] * childrel->rows;
457                                 }
458                         }
459                 }
460         }
461
462         /*
463          * Save the finished size estimates.
464          */
465         rel->rows = parent_rows;
466         if (parent_rows > 0)
467         {
468                 int                     i;
469
470                 rel->width = rint(parent_size / parent_rows);
471                 for (i = 0; i < nattrs; i++)
472                         rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
473         }
474         else
475                 rel->width = 0;                 /* attr_widths should be zero already */
476
477         /*
478          * Set "raw tuples" count equal to "rows" for the appendrel; needed
479          * because some places assume rel->tuples is valid for any baserel.
480          */
481         rel->tuples = parent_rows;
482
483         pfree(parent_attrsizes);
484
485         /*
486          * Finally, build Append path and install it as the only access path for
487          * the parent rel.      (Note: this is correct even if we have zero or one
488          * live subpath due to constraint exclusion.)
489          */
490         add_path(rel, (Path *) create_append_path(rel, subpaths));
491
492         /* Select cheapest path (pretty easy in this case...) */
493         set_cheapest(rel);
494 }
495
496 /*
497  * set_dummy_rel_pathlist
498  *        Build a dummy path for a relation that's been excluded by constraints
499  *
500  * Rather than inventing a special "dummy" path type, we represent this as an
501  * AppendPath with no members (see also IS_DUMMY_PATH macro).
502  */
503 static void
504 set_dummy_rel_pathlist(RelOptInfo *rel)
505 {
506         /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
507         rel->rows = 0;
508         rel->width = 0;
509
510         add_path(rel, (Path *) create_append_path(rel, NIL));
511
512         /* Select cheapest path (pretty easy in this case...) */
513         set_cheapest(rel);
514 }
515
516 /* quick-and-dirty test to see if any joining is needed */
517 static bool
518 has_multiple_baserels(PlannerInfo *root)
519 {
520         int                     num_base_rels = 0;
521         Index           rti;
522
523         for (rti = 1; rti < root->simple_rel_array_size; rti++)
524         {
525                 RelOptInfo *brel = root->simple_rel_array[rti];
526
527                 if (brel == NULL)
528                         continue;
529
530                 /* ignore RTEs that are "other rels" */
531                 if (brel->reloptkind == RELOPT_BASEREL)
532                         if (++num_base_rels > 1)
533                                 return true;
534         }
535         return false;
536 }
537
538 /*
539  * set_subquery_pathlist
540  *              Build the (single) access path for a subquery RTE
541  */
542 static void
543 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
544                                           Index rti, RangeTblEntry *rte)
545 {
546         Query      *parse = root->parse;
547         Query      *subquery = rte->subquery;
548         bool       *differentTypes;
549         double          tuple_fraction;
550         PlannerInfo *subroot;
551         List       *pathkeys;
552
553         /*
554          * Must copy the Query so that planning doesn't mess up the RTE contents
555          * (really really need to fix the planner to not scribble on its input,
556          * someday).
557          */
558         subquery = copyObject(subquery);
559
560         /* We need a workspace for keeping track of set-op type coercions */
561         differentTypes = (bool *)
562                 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
563
564         /*
565          * If there are any restriction clauses that have been attached to the
566          * subquery relation, consider pushing them down to become WHERE or HAVING
567          * quals of the subquery itself.  This transformation is useful because it
568          * may allow us to generate a better plan for the subquery than evaluating
569          * all the subquery output rows and then filtering them.
570          *
571          * There are several cases where we cannot push down clauses. Restrictions
572          * involving the subquery are checked by subquery_is_pushdown_safe().
573          * Restrictions on individual clauses are checked by
574          * qual_is_pushdown_safe().  Also, we don't want to push down
575          * pseudoconstant clauses; better to have the gating node above the
576          * subquery.
577          *
578          * Non-pushed-down clauses will get evaluated as qpquals of the
579          * SubqueryScan node.
580          *
581          * XXX Are there any cases where we want to make a policy decision not to
582          * push down a pushable qual, because it'd result in a worse plan?
583          */
584         if (rel->baserestrictinfo != NIL &&
585                 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
586         {
587                 /* OK to consider pushing down individual quals */
588                 List       *upperrestrictlist = NIL;
589                 ListCell   *l;
590
591                 foreach(l, rel->baserestrictinfo)
592                 {
593                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
594                         Node       *clause = (Node *) rinfo->clause;
595
596                         if (!rinfo->pseudoconstant &&
597                                 qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
598                         {
599                                 /* Push it down */
600                                 subquery_push_qual(subquery, rte, rti, clause);
601                         }
602                         else
603                         {
604                                 /* Keep it in the upper query */
605                                 upperrestrictlist = lappend(upperrestrictlist, rinfo);
606                         }
607                 }
608                 rel->baserestrictinfo = upperrestrictlist;
609         }
610
611         pfree(differentTypes);
612
613         /*
614          * We can safely pass the outer tuple_fraction down to the subquery if the
615          * outer level has no joining, aggregation, or sorting to do. Otherwise
616          * we'd better tell the subquery to plan for full retrieval. (XXX This
617          * could probably be made more intelligent ...)
618          */
619         if (parse->hasAggs ||
620                 parse->groupClause ||
621                 parse->havingQual ||
622                 parse->distinctClause ||
623                 parse->sortClause ||
624                 has_multiple_baserels(root))
625                 tuple_fraction = 0.0;   /* default case */
626         else
627                 tuple_fraction = root->tuple_fraction;
628
629         /* Generate the plan for the subquery */
630         rel->subplan = subquery_planner(root->glob, subquery,
631                                                                         root,
632                                                                         false, tuple_fraction,
633                                                                         &subroot);
634         rel->subrtable = subroot->parse->rtable;
635         rel->subrowmark = subroot->rowMarks;
636
637         /* Copy number of output rows from subplan */
638         rel->tuples = rel->subplan->plan_rows;
639
640         /* Mark rel with estimated output rows, width, etc */
641         set_baserel_size_estimates(root, rel);
642
643         /* Convert subquery pathkeys to outer representation */
644         pathkeys = convert_subquery_pathkeys(root, rel, subroot->query_pathkeys);
645
646         /* Generate appropriate path */
647         add_path(rel, create_subqueryscan_path(rel, pathkeys));
648
649         /* Select cheapest path (pretty easy in this case...) */
650         set_cheapest(rel);
651 }
652
653 /*
654  * set_function_pathlist
655  *              Build the (single) access path for a function RTE
656  */
657 static void
658 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
659 {
660         /* Mark rel with estimated output rows, width, etc */
661         set_function_size_estimates(root, rel);
662
663         /* Generate appropriate path */
664         add_path(rel, create_functionscan_path(root, rel));
665
666         /* Select cheapest path (pretty easy in this case...) */
667         set_cheapest(rel);
668 }
669
670 /*
671  * set_values_pathlist
672  *              Build the (single) access path for a VALUES RTE
673  */
674 static void
675 set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
676 {
677         /* Mark rel with estimated output rows, width, etc */
678         set_values_size_estimates(root, rel);
679
680         /* Generate appropriate path */
681         add_path(rel, create_valuesscan_path(root, rel));
682
683         /* Select cheapest path (pretty easy in this case...) */
684         set_cheapest(rel);
685 }
686
687 /*
688  * set_cte_pathlist
689  *              Build the (single) access path for a non-self-reference CTE RTE
690  */
691 static void
692 set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
693 {
694         Plan       *cteplan;
695         PlannerInfo *cteroot;
696         Index           levelsup;
697         int                     ndx;
698         ListCell   *lc;
699         int                     plan_id;
700
701         /*
702          * Find the referenced CTE, and locate the plan previously made for it.
703          */
704         levelsup = rte->ctelevelsup;
705         cteroot = root;
706         while (levelsup-- > 0)
707         {
708                 cteroot = cteroot->parent_root;
709                 if (!cteroot)                   /* shouldn't happen */
710                         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
711         }
712
713         /*
714          * Note: cte_plan_ids can be shorter than cteList, if we are still working
715          * on planning the CTEs (ie, this is a side-reference from another CTE).
716          * So we mustn't use forboth here.
717          */
718         ndx = 0;
719         foreach(lc, cteroot->parse->cteList)
720         {
721                 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
722
723                 if (strcmp(cte->ctename, rte->ctename) == 0)
724                         break;
725                 ndx++;
726         }
727         if (lc == NULL)                         /* shouldn't happen */
728                 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
729         if (ndx >= list_length(cteroot->cte_plan_ids))
730                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
731         plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
732         Assert(plan_id > 0);
733         cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
734
735         /* Mark rel with estimated output rows, width, etc */
736         set_cte_size_estimates(root, rel, cteplan);
737
738         /* Generate appropriate path */
739         add_path(rel, create_ctescan_path(root, rel));
740
741         /* Select cheapest path (pretty easy in this case...) */
742         set_cheapest(rel);
743 }
744
745 /*
746  * set_worktable_pathlist
747  *              Build the (single) access path for a self-reference CTE RTE
748  */
749 static void
750 set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
751 {
752         Plan       *cteplan;
753         PlannerInfo *cteroot;
754         Index           levelsup;
755
756         /*
757          * We need to find the non-recursive term's plan, which is in the plan
758          * level that's processing the recursive UNION, which is one level *below*
759          * where the CTE comes from.
760          */
761         levelsup = rte->ctelevelsup;
762         if (levelsup == 0)                      /* shouldn't happen */
763                 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
764         levelsup--;
765         cteroot = root;
766         while (levelsup-- > 0)
767         {
768                 cteroot = cteroot->parent_root;
769                 if (!cteroot)                   /* shouldn't happen */
770                         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
771         }
772         cteplan = cteroot->non_recursive_plan;
773         if (!cteplan)                           /* shouldn't happen */
774                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
775
776         /* Mark rel with estimated output rows, width, etc */
777         set_cte_size_estimates(root, rel, cteplan);
778
779         /* Generate appropriate path */
780         add_path(rel, create_worktablescan_path(root, rel));
781
782         /* Select cheapest path (pretty easy in this case...) */
783         set_cheapest(rel);
784 }
785
786 /*
787  * make_rel_from_joinlist
788  *        Build access paths using a "joinlist" to guide the join path search.
789  *
790  * See comments for deconstruct_jointree() for definition of the joinlist
791  * data structure.
792  */
793 static RelOptInfo *
794 make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
795 {
796         int                     levels_needed;
797         List       *initial_rels;
798         ListCell   *jl;
799
800         /*
801          * Count the number of child joinlist nodes.  This is the depth of the
802          * dynamic-programming algorithm we must employ to consider all ways of
803          * joining the child nodes.
804          */
805         levels_needed = list_length(joinlist);
806
807         if (levels_needed <= 0)
808                 return NULL;                    /* nothing to do? */
809
810         /*
811          * Construct a list of rels corresponding to the child joinlist nodes.
812          * This may contain both base rels and rels constructed according to
813          * sub-joinlists.
814          */
815         initial_rels = NIL;
816         foreach(jl, joinlist)
817         {
818                 Node       *jlnode = (Node *) lfirst(jl);
819                 RelOptInfo *thisrel;
820
821                 if (IsA(jlnode, RangeTblRef))
822                 {
823                         int                     varno = ((RangeTblRef *) jlnode)->rtindex;
824
825                         thisrel = find_base_rel(root, varno);
826                 }
827                 else if (IsA(jlnode, List))
828                 {
829                         /* Recurse to handle subproblem */
830                         thisrel = make_rel_from_joinlist(root, (List *) jlnode);
831                 }
832                 else
833                 {
834                         elog(ERROR, "unrecognized joinlist node type: %d",
835                                  (int) nodeTag(jlnode));
836                         thisrel = NULL;         /* keep compiler quiet */
837                 }
838
839                 initial_rels = lappend(initial_rels, thisrel);
840         }
841
842         if (levels_needed == 1)
843         {
844                 /*
845                  * Single joinlist node, so we're done.
846                  */
847                 return (RelOptInfo *) linitial(initial_rels);
848         }
849         else
850         {
851                 /*
852                  * Consider the different orders in which we could join the rels,
853                  * using a plugin, GEQO, or the regular join search code.
854                  *
855                  * We put the initial_rels list into a PlannerInfo field because
856                  * has_legal_joinclause() needs to look at it (ugly :-().
857                  */
858                 root->initial_rels = initial_rels;
859
860                 if (join_search_hook)
861                         return (*join_search_hook) (root, levels_needed, initial_rels);
862                 else if (enable_geqo && levels_needed >= geqo_threshold)
863                         return geqo(root, levels_needed, initial_rels);
864                 else
865                         return standard_join_search(root, levels_needed, initial_rels);
866         }
867 }
868
869 /*
870  * standard_join_search
871  *        Find possible joinpaths for a query by successively finding ways
872  *        to join component relations into join relations.
873  *
874  * 'levels_needed' is the number of iterations needed, ie, the number of
875  *              independent jointree items in the query.  This is > 1.
876  *
877  * 'initial_rels' is a list of RelOptInfo nodes for each independent
878  *              jointree item.  These are the components to be joined together.
879  *              Note that levels_needed == list_length(initial_rels).
880  *
881  * Returns the final level of join relations, i.e., the relation that is
882  * the result of joining all the original relations together.
883  * At least one implementation path must be provided for this relation and
884  * all required sub-relations.
885  *
886  * To support loadable plugins that modify planner behavior by changing the
887  * join searching algorithm, we provide a hook variable that lets a plugin
888  * replace or supplement this function.  Any such hook must return the same
889  * final join relation as the standard code would, but it might have a
890  * different set of implementation paths attached, and only the sub-joinrels
891  * needed for these paths need have been instantiated.
892  *
893  * Note to plugin authors: the functions invoked during standard_join_search()
894  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
895  * than one join-order search, you'll probably need to save and restore the
896  * original states of those data structures.  See geqo_eval() for an example.
897  */
898 RelOptInfo *
899 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
900 {
901         List      **joinitems;
902         int                     lev;
903         RelOptInfo *rel;
904
905         /*
906          * We employ a simple "dynamic programming" algorithm: we first find all
907          * ways to build joins of two jointree items, then all ways to build joins
908          * of three items (from two-item joins and single items), then four-item
909          * joins, and so on until we have considered all ways to join all the
910          * items into one rel.
911          *
912          * joinitems[j] is a list of all the j-item rels.  Initially we set
913          * joinitems[1] to represent all the single-jointree-item relations.
914          */
915         joinitems = (List **) palloc0((levels_needed + 1) * sizeof(List *));
916
917         joinitems[1] = initial_rels;
918
919         for (lev = 2; lev <= levels_needed; lev++)
920         {
921                 ListCell   *x;
922
923                 /*
924                  * Determine all possible pairs of relations to be joined at this
925                  * level, and build paths for making each one from every available
926                  * pair of lower-level relations.
927                  */
928                 joinitems[lev] = join_search_one_level(root, lev, joinitems);
929
930                 /*
931                  * Do cleanup work on each just-processed rel.
932                  */
933                 foreach(x, joinitems[lev])
934                 {
935                         rel = (RelOptInfo *) lfirst(x);
936
937                         /* Find and save the cheapest paths for this rel */
938                         set_cheapest(rel);
939
940 #ifdef OPTIMIZER_DEBUG
941                         debug_print_rel(root, rel);
942 #endif
943                 }
944         }
945
946         /*
947          * We should have a single rel at the final level.
948          */
949         if (joinitems[levels_needed] == NIL)
950                 elog(ERROR, "failed to build any %d-way joins", levels_needed);
951         Assert(list_length(joinitems[levels_needed]) == 1);
952
953         rel = (RelOptInfo *) linitial(joinitems[levels_needed]);
954
955         return rel;
956 }
957
958 /*****************************************************************************
959  *                      PUSHING QUALS DOWN INTO SUBQUERIES
960  *****************************************************************************/
961
962 /*
963  * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
964  *
965  * subquery is the particular component query being checked.  topquery
966  * is the top component of a set-operations tree (the same Query if no
967  * set-op is involved).
968  *
969  * Conditions checked here:
970  *
971  * 1. If the subquery has a LIMIT clause, we must not push down any quals,
972  * since that could change the set of rows returned.
973  *
974  * 2. If the subquery contains any window functions, we can't push quals
975  * into it, because that could change the results.
976  *
977  * 3. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
978  * quals into it, because that could change the results.
979  *
980  * 4. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
981  * push quals into each component query, but the quals can only reference
982  * subquery columns that suffer no type coercions in the set operation.
983  * Otherwise there are possible semantic gotchas.  So, we check the
984  * component queries to see if any of them have different output types;
985  * differentTypes[k] is set true if column k has different type in any
986  * component.
987  */
988 static bool
989 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
990                                                   bool *differentTypes)
991 {
992         SetOperationStmt *topop;
993
994         /* Check point 1 */
995         if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
996                 return false;
997
998         /* Check point 2 */
999         if (subquery->hasWindowFuncs)
1000                 return false;
1001
1002         /* Are we at top level, or looking at a setop component? */
1003         if (subquery == topquery)
1004         {
1005                 /* Top level, so check any component queries */
1006                 if (subquery->setOperations != NULL)
1007                         if (!recurse_pushdown_safe(subquery->setOperations, topquery,
1008                                                                            differentTypes))
1009                                 return false;
1010         }
1011         else
1012         {
1013                 /* Setop component must not have more components (too weird) */
1014                 if (subquery->setOperations != NULL)
1015                         return false;
1016                 /* Check whether setop component output types match top level */
1017                 topop = (SetOperationStmt *) topquery->setOperations;
1018                 Assert(topop && IsA(topop, SetOperationStmt));
1019                 compare_tlist_datatypes(subquery->targetList,
1020                                                                 topop->colTypes,
1021                                                                 differentTypes);
1022         }
1023         return true;
1024 }
1025
1026 /*
1027  * Helper routine to recurse through setOperations tree
1028  */
1029 static bool
1030 recurse_pushdown_safe(Node *setOp, Query *topquery,
1031                                           bool *differentTypes)
1032 {
1033         if (IsA(setOp, RangeTblRef))
1034         {
1035                 RangeTblRef *rtr = (RangeTblRef *) setOp;
1036                 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
1037                 Query      *subquery = rte->subquery;
1038
1039                 Assert(subquery != NULL);
1040                 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
1041         }
1042         else if (IsA(setOp, SetOperationStmt))
1043         {
1044                 SetOperationStmt *op = (SetOperationStmt *) setOp;
1045
1046                 /* EXCEPT is no good */
1047                 if (op->op == SETOP_EXCEPT)
1048                         return false;
1049                 /* Else recurse */
1050                 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
1051                         return false;
1052                 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
1053                         return false;
1054         }
1055         else
1056         {
1057                 elog(ERROR, "unrecognized node type: %d",
1058                          (int) nodeTag(setOp));
1059         }
1060         return true;
1061 }
1062
1063 /*
1064  * Compare tlist's datatypes against the list of set-operation result types.
1065  * For any items that are different, mark the appropriate element of
1066  * differentTypes[] to show that this column will have type conversions.
1067  *
1068  * We don't have to care about typmods here: the only allowed difference
1069  * between set-op input and output typmods is input is a specific typmod
1070  * and output is -1, and that does not require a coercion.
1071  */
1072 static void
1073 compare_tlist_datatypes(List *tlist, List *colTypes,
1074                                                 bool *differentTypes)
1075 {
1076         ListCell   *l;
1077         ListCell   *colType = list_head(colTypes);
1078
1079         foreach(l, tlist)
1080         {
1081                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1082
1083                 if (tle->resjunk)
1084                         continue;                       /* ignore resjunk columns */
1085                 if (colType == NULL)
1086                         elog(ERROR, "wrong number of tlist entries");
1087                 if (exprType((Node *) tle->expr) != lfirst_oid(colType))
1088                         differentTypes[tle->resno] = true;
1089                 colType = lnext(colType);
1090         }
1091         if (colType != NULL)
1092                 elog(ERROR, "wrong number of tlist entries");
1093 }
1094
1095 /*
1096  * qual_is_pushdown_safe - is a particular qual safe to push down?
1097  *
1098  * qual is a restriction clause applying to the given subquery (whose RTE
1099  * has index rti in the parent query).
1100  *
1101  * Conditions checked here:
1102  *
1103  * 1. The qual must not contain any subselects (mainly because I'm not sure
1104  * it will work correctly: sublinks will already have been transformed into
1105  * subplans in the qual, but not in the subquery).
1106  *
1107  * 2. The qual must not refer to the whole-row output of the subquery
1108  * (since there is no easy way to name that within the subquery itself).
1109  *
1110  * 3. The qual must not refer to any subquery output columns that were
1111  * found to have inconsistent types across a set operation tree by
1112  * subquery_is_pushdown_safe().
1113  *
1114  * 4. If the subquery uses DISTINCT ON, we must not push down any quals that
1115  * refer to non-DISTINCT output columns, because that could change the set
1116  * of rows returned.  (This condition is vacuous for DISTINCT, because then
1117  * there are no non-DISTINCT output columns, so we needn't check.  But note
1118  * we are assuming that the qual can't distinguish values that the DISTINCT
1119  * operator sees as equal.      This is a bit shaky but we have no way to test
1120  * for the case, and it's unlikely enough that we shouldn't refuse the
1121  * optimization just because it could theoretically happen.)
1122  *
1123  * 5. We must not push down any quals that refer to subselect outputs that
1124  * return sets, else we'd introduce functions-returning-sets into the
1125  * subquery's WHERE/HAVING quals.
1126  *
1127  * 6. We must not push down any quals that refer to subselect outputs that
1128  * contain volatile functions, for fear of introducing strange results due
1129  * to multiple evaluation of a volatile function.
1130  */
1131 static bool
1132 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
1133                                           bool *differentTypes)
1134 {
1135         bool            safe = true;
1136         List       *vars;
1137         ListCell   *vl;
1138         Bitmapset  *tested = NULL;
1139
1140         /* Refuse subselects (point 1) */
1141         if (contain_subplans(qual))
1142                 return false;
1143
1144         /*
1145          * It would be unsafe to push down window function calls, but at least for
1146          * the moment we could never see any in a qual anyhow.
1147          */
1148         Assert(!contain_window_function(qual));
1149
1150         /*
1151          * Examine all Vars used in clause; since it's a restriction clause, all
1152          * such Vars must refer to subselect output columns.
1153          */
1154         vars = pull_var_clause(qual, PVC_INCLUDE_PLACEHOLDERS);
1155         foreach(vl, vars)
1156         {
1157                 Var                *var = (Var *) lfirst(vl);
1158                 TargetEntry *tle;
1159
1160                 /*
1161                  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
1162                  * It's not clear whether a PHV could safely be pushed down, and even
1163                  * less clear whether such a situation could arise in any cases of
1164                  * practical interest anyway.  So for the moment, just refuse to push
1165                  * down.
1166                  */
1167                 if (!IsA(var, Var))
1168                 {
1169                         safe = false;
1170                         break;
1171                 }
1172
1173                 Assert(var->varno == rti);
1174
1175                 /* Check point 2 */
1176                 if (var->varattno == 0)
1177                 {
1178                         safe = false;
1179                         break;
1180                 }
1181
1182                 /*
1183                  * We use a bitmapset to avoid testing the same attno more than once.
1184                  * (NB: this only works because subquery outputs can't have negative
1185                  * attnos.)
1186                  */
1187                 if (bms_is_member(var->varattno, tested))
1188                         continue;
1189                 tested = bms_add_member(tested, var->varattno);
1190
1191                 /* Check point 3 */
1192                 if (differentTypes[var->varattno])
1193                 {
1194                         safe = false;
1195                         break;
1196                 }
1197
1198                 /* Must find the tlist element referenced by the Var */
1199                 tle = get_tle_by_resno(subquery->targetList, var->varattno);
1200                 Assert(tle != NULL);
1201                 Assert(!tle->resjunk);
1202
1203                 /* If subquery uses DISTINCT ON, check point 4 */
1204                 if (subquery->hasDistinctOn &&
1205                         !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
1206                 {
1207                         /* non-DISTINCT column, so fail */
1208                         safe = false;
1209                         break;
1210                 }
1211
1212                 /* Refuse functions returning sets (point 5) */
1213                 if (expression_returns_set((Node *) tle->expr))
1214                 {
1215                         safe = false;
1216                         break;
1217                 }
1218
1219                 /* Refuse volatile functions (point 6) */
1220                 if (contain_volatile_functions((Node *) tle->expr))
1221                 {
1222                         safe = false;
1223                         break;
1224                 }
1225         }
1226
1227         list_free(vars);
1228         bms_free(tested);
1229
1230         return safe;
1231 }
1232
1233 /*
1234  * subquery_push_qual - push down a qual that we have determined is safe
1235  */
1236 static void
1237 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
1238 {
1239         if (subquery->setOperations != NULL)
1240         {
1241                 /* Recurse to push it separately to each component query */
1242                 recurse_push_qual(subquery->setOperations, subquery,
1243                                                   rte, rti, qual);
1244         }
1245         else
1246         {
1247                 /*
1248                  * We need to replace Vars in the qual (which must refer to outputs of
1249                  * the subquery) with copies of the subquery's targetlist expressions.
1250                  * Note that at this point, any uplevel Vars in the qual should have
1251                  * been replaced with Params, so they need no work.
1252                  *
1253                  * This step also ensures that when we are pushing into a setop tree,
1254                  * each component query gets its own copy of the qual.
1255                  */
1256                 qual = ResolveNew(qual, rti, 0, rte,
1257                                                   subquery->targetList,
1258                                                   CMD_SELECT, 0,
1259                                                   &subquery->hasSubLinks);
1260
1261                 /*
1262                  * Now attach the qual to the proper place: normally WHERE, but if the
1263                  * subquery uses grouping or aggregation, put it in HAVING (since the
1264                  * qual really refers to the group-result rows).
1265                  */
1266                 if (subquery->hasAggs || subquery->groupClause || subquery->havingQual)
1267                         subquery->havingQual = make_and_qual(subquery->havingQual, qual);
1268                 else
1269                         subquery->jointree->quals =
1270                                 make_and_qual(subquery->jointree->quals, qual);
1271
1272                 /*
1273                  * We need not change the subquery's hasAggs or hasSublinks flags,
1274                  * since we can't be pushing down any aggregates that weren't there
1275                  * before, and we don't push down subselects at all.
1276                  */
1277         }
1278 }
1279
1280 /*
1281  * Helper routine to recurse through setOperations tree
1282  */
1283 static void
1284 recurse_push_qual(Node *setOp, Query *topquery,
1285                                   RangeTblEntry *rte, Index rti, Node *qual)
1286 {
1287         if (IsA(setOp, RangeTblRef))
1288         {
1289                 RangeTblRef *rtr = (RangeTblRef *) setOp;
1290                 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
1291                 Query      *subquery = subrte->subquery;
1292
1293                 Assert(subquery != NULL);
1294                 subquery_push_qual(subquery, rte, rti, qual);
1295         }
1296         else if (IsA(setOp, SetOperationStmt))
1297         {
1298                 SetOperationStmt *op = (SetOperationStmt *) setOp;
1299
1300                 recurse_push_qual(op->larg, topquery, rte, rti, qual);
1301                 recurse_push_qual(op->rarg, topquery, rte, rti, qual);
1302         }
1303         else
1304         {
1305                 elog(ERROR, "unrecognized node type: %d",
1306                          (int) nodeTag(setOp));
1307         }
1308 }
1309
1310 /*****************************************************************************
1311  *                      DEBUG SUPPORT
1312  *****************************************************************************/
1313
1314 #ifdef OPTIMIZER_DEBUG
1315
1316 static void
1317 print_relids(Relids relids)
1318 {
1319         Relids          tmprelids;
1320         int                     x;
1321         bool            first = true;
1322
1323         tmprelids = bms_copy(relids);
1324         while ((x = bms_first_member(tmprelids)) >= 0)
1325         {
1326                 if (!first)
1327                         printf(" ");
1328                 printf("%d", x);
1329                 first = false;
1330         }
1331         bms_free(tmprelids);
1332 }
1333
1334 static void
1335 print_restrictclauses(PlannerInfo *root, List *clauses)
1336 {
1337         ListCell   *l;
1338
1339         foreach(l, clauses)
1340         {
1341                 RestrictInfo *c = lfirst(l);
1342
1343                 print_expr((Node *) c->clause, root->parse->rtable);
1344                 if (lnext(l))
1345                         printf(", ");
1346         }
1347 }
1348
1349 static void
1350 print_path(PlannerInfo *root, Path *path, int indent)
1351 {
1352         const char *ptype;
1353         bool            join = false;
1354         Path       *subpath = NULL;
1355         int                     i;
1356
1357         switch (nodeTag(path))
1358         {
1359                 case T_Path:
1360                         ptype = "SeqScan";
1361                         break;
1362                 case T_IndexPath:
1363                         ptype = "IdxScan";
1364                         break;
1365                 case T_BitmapHeapPath:
1366                         ptype = "BitmapHeapScan";
1367                         break;
1368                 case T_BitmapAndPath:
1369                         ptype = "BitmapAndPath";
1370                         break;
1371                 case T_BitmapOrPath:
1372                         ptype = "BitmapOrPath";
1373                         break;
1374                 case T_TidPath:
1375                         ptype = "TidScan";
1376                         break;
1377                 case T_AppendPath:
1378                         ptype = "Append";
1379                         break;
1380                 case T_ResultPath:
1381                         ptype = "Result";
1382                         break;
1383                 case T_MaterialPath:
1384                         ptype = "Material";
1385                         subpath = ((MaterialPath *) path)->subpath;
1386                         break;
1387                 case T_UniquePath:
1388                         ptype = "Unique";
1389                         subpath = ((UniquePath *) path)->subpath;
1390                         break;
1391                 case T_NoOpPath:
1392                         ptype = "NoOp";
1393                         subpath = ((NoOpPath *) path)->subpath;
1394                         break;
1395                 case T_NestPath:
1396                         ptype = "NestLoop";
1397                         join = true;
1398                         break;
1399                 case T_MergePath:
1400                         ptype = "MergeJoin";
1401                         join = true;
1402                         break;
1403                 case T_HashPath:
1404                         ptype = "HashJoin";
1405                         join = true;
1406                         break;
1407                 default:
1408                         ptype = "???Path";
1409                         break;
1410         }
1411
1412         for (i = 0; i < indent; i++)
1413                 printf("\t");
1414         printf("%s", ptype);
1415
1416         if (path->parent)
1417         {
1418                 printf("(");
1419                 print_relids(path->parent->relids);
1420                 printf(") rows=%.0f", path->parent->rows);
1421         }
1422         printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
1423
1424         if (path->pathkeys)
1425         {
1426                 for (i = 0; i < indent; i++)
1427                         printf("\t");
1428                 printf("  pathkeys: ");
1429                 print_pathkeys(path->pathkeys, root->parse->rtable);
1430         }
1431
1432         if (join)
1433         {
1434                 JoinPath   *jp = (JoinPath *) path;
1435
1436                 for (i = 0; i < indent; i++)
1437                         printf("\t");
1438                 printf("  clauses: ");
1439                 print_restrictclauses(root, jp->joinrestrictinfo);
1440                 printf("\n");
1441
1442                 if (IsA(path, MergePath))
1443                 {
1444                         MergePath  *mp = (MergePath *) path;
1445
1446                         if (mp->outersortkeys || mp->innersortkeys)
1447                         {
1448                                 for (i = 0; i < indent; i++)
1449                                         printf("\t");
1450                                 printf("  sortouter=%d sortinner=%d\n",
1451                                            ((mp->outersortkeys) ? 1 : 0),
1452                                            ((mp->innersortkeys) ? 1 : 0));
1453                         }
1454                 }
1455
1456                 print_path(root, jp->outerjoinpath, indent + 1);
1457                 print_path(root, jp->innerjoinpath, indent + 1);
1458         }
1459
1460         if (subpath)
1461                 print_path(root, subpath, indent + 1);
1462 }
1463
1464 void
1465 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
1466 {
1467         ListCell   *l;
1468
1469         printf("RELOPTINFO (");
1470         print_relids(rel->relids);
1471         printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
1472
1473         if (rel->baserestrictinfo)
1474         {
1475                 printf("\tbaserestrictinfo: ");
1476                 print_restrictclauses(root, rel->baserestrictinfo);
1477                 printf("\n");
1478         }
1479
1480         if (rel->joininfo)
1481         {
1482                 printf("\tjoininfo: ");
1483                 print_restrictclauses(root, rel->joininfo);
1484                 printf("\n");
1485         }
1486
1487         printf("\tpath list:\n");
1488         foreach(l, rel->pathlist)
1489                 print_path(root, lfirst(l), 1);
1490         printf("\n\tcheapest startup path:\n");
1491         print_path(root, rel->cheapest_startup_path, 1);
1492         printf("\n\tcheapest total path:\n");
1493         print_path(root, rel->cheapest_total_path, 1);
1494         printf("\n");
1495         fflush(stdout);
1496 }
1497
1498 #endif   /* OPTIMIZER_DEBUG */