]> granicus.if.org Git - postgresql/blob - src/backend/optimizer/path/allpaths.c
Revise FDW planning API, again.
[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-2012, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/backend/optimizer/path/allpaths.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "postgres.h"
17
18 #include <math.h>
19
20 #include "catalog/pg_class.h"
21 #include "foreign/fdwapi.h"
22 #include "nodes/nodeFuncs.h"
23 #ifdef OPTIMIZER_DEBUG
24 #include "nodes/print.h"
25 #endif
26 #include "optimizer/clauses.h"
27 #include "optimizer/cost.h"
28 #include "optimizer/geqo.h"
29 #include "optimizer/pathnode.h"
30 #include "optimizer/paths.h"
31 #include "optimizer/plancat.h"
32 #include "optimizer/planner.h"
33 #include "optimizer/prep.h"
34 #include "optimizer/restrictinfo.h"
35 #include "optimizer/var.h"
36 #include "parser/parse_clause.h"
37 #include "parser/parsetree.h"
38 #include "rewrite/rewriteManip.h"
39 #include "utils/lsyscache.h"
40
41
42 /* These parameters are set by GUC */
43 bool            enable_geqo = false;    /* just in case GUC doesn't set it */
44 int                     geqo_threshold;
45
46 /* Hook for plugins to replace standard_join_search() */
47 join_search_hook_type join_search_hook = NULL;
48
49
50 static void set_base_rel_sizes(PlannerInfo *root);
51 static void set_base_rel_pathlists(PlannerInfo *root);
52 static void set_rel_size(PlannerInfo *root, RelOptInfo *rel,
53                                  Index rti, RangeTblEntry *rte);
54 static void set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
55                                  Index rti, RangeTblEntry *rte);
56 static void set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel,
57                                            RangeTblEntry *rte);
58 static void set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
59                                            RangeTblEntry *rte);
60 static void set_foreign_size(PlannerInfo *root, RelOptInfo *rel,
61                                          RangeTblEntry *rte);
62 static void set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel,
63                                          RangeTblEntry *rte);
64 static void set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
65                                                 Index rti, RangeTblEntry *rte);
66 static void set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
67                                                 Index rti, RangeTblEntry *rte);
68 static void generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
69                                                    List *live_childrels,
70                                                    List *all_child_pathkeys,
71                                                    Relids required_outer);
72 static List *accumulate_append_subpath(List *subpaths, Path *path);
73 static void set_dummy_rel_pathlist(RelOptInfo *rel);
74 static void set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
75                                           Index rti, RangeTblEntry *rte);
76 static void set_function_pathlist(PlannerInfo *root, RelOptInfo *rel,
77                                           RangeTblEntry *rte);
78 static void set_values_pathlist(PlannerInfo *root, RelOptInfo *rel,
79                                         RangeTblEntry *rte);
80 static void set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel,
81                                  RangeTblEntry *rte);
82 static void set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel,
83                                            RangeTblEntry *rte);
84 static RelOptInfo *make_rel_from_joinlist(PlannerInfo *root, List *joinlist);
85 static bool subquery_is_pushdown_safe(Query *subquery, Query *topquery,
86                                                   bool *differentTypes);
87 static bool recurse_pushdown_safe(Node *setOp, Query *topquery,
88                                           bool *differentTypes);
89 static void compare_tlist_datatypes(List *tlist, List *colTypes,
90                                                 bool *differentTypes);
91 static bool qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
92                                           bool *differentTypes);
93 static void subquery_push_qual(Query *subquery,
94                                    RangeTblEntry *rte, Index rti, Node *qual);
95 static void recurse_push_qual(Node *setOp, Query *topquery,
96                                   RangeTblEntry *rte, Index rti, Node *qual);
97
98
99 /*
100  * make_one_rel
101  *        Finds all possible access paths for executing a query, returning a
102  *        single rel that represents the join of all base rels in the query.
103  */
104 RelOptInfo *
105 make_one_rel(PlannerInfo *root, List *joinlist)
106 {
107         RelOptInfo *rel;
108         Index           rti;
109
110         /*
111          * Construct the all_baserels Relids set.
112          */
113         root->all_baserels = NULL;
114         for (rti = 1; rti < root->simple_rel_array_size; rti++)
115         {
116                 RelOptInfo *brel = root->simple_rel_array[rti];
117
118                 /* there may be empty slots corresponding to non-baserel RTEs */
119                 if (brel == NULL)
120                         continue;
121
122                 Assert(brel->relid == rti); /* sanity check on array */
123
124                 /* ignore RTEs that are "other rels" */
125                 if (brel->reloptkind != RELOPT_BASEREL)
126                         continue;
127
128                 root->all_baserels = bms_add_member(root->all_baserels, brel->relid);
129         }
130
131         /*
132          * Generate access paths for the base rels.
133          */
134         set_base_rel_sizes(root);
135         set_base_rel_pathlists(root);
136
137         /*
138          * Generate access paths for the entire join tree.
139          */
140         rel = make_rel_from_joinlist(root, joinlist);
141
142         /*
143          * The result should join all and only the query's base rels.
144          */
145         Assert(bms_equal(rel->relids, root->all_baserels));
146
147         return rel;
148 }
149
150 /*
151  * set_base_rel_sizes
152  *        Set the size estimates (rows and widths) for each base-relation entry.
153  *
154  * We do this in a separate pass over the base rels so that rowcount
155  * estimates are available for parameterized path generation.
156  */
157 static void
158 set_base_rel_sizes(PlannerInfo *root)
159 {
160         Index           rti;
161
162         for (rti = 1; rti < root->simple_rel_array_size; rti++)
163         {
164                 RelOptInfo *rel = root->simple_rel_array[rti];
165
166                 /* there may be empty slots corresponding to non-baserel RTEs */
167                 if (rel == NULL)
168                         continue;
169
170                 Assert(rel->relid == rti);              /* sanity check on array */
171
172                 /* ignore RTEs that are "other rels" */
173                 if (rel->reloptkind != RELOPT_BASEREL)
174                         continue;
175
176                 set_rel_size(root, rel, rti, root->simple_rte_array[rti]);
177         }
178 }
179
180 /*
181  * set_base_rel_pathlists
182  *        Finds all paths available for scanning each base-relation entry.
183  *        Sequential scan and any available indices are considered.
184  *        Each useful path is attached to its relation's 'pathlist' field.
185  */
186 static void
187 set_base_rel_pathlists(PlannerInfo *root)
188 {
189         Index           rti;
190
191         for (rti = 1; rti < root->simple_rel_array_size; rti++)
192         {
193                 RelOptInfo *rel = root->simple_rel_array[rti];
194
195                 /* there may be empty slots corresponding to non-baserel RTEs */
196                 if (rel == NULL)
197                         continue;
198
199                 Assert(rel->relid == rti);              /* sanity check on array */
200
201                 /* ignore RTEs that are "other rels" */
202                 if (rel->reloptkind != RELOPT_BASEREL)
203                         continue;
204
205                 set_rel_pathlist(root, rel, rti, root->simple_rte_array[rti]);
206         }
207 }
208
209 /*
210  * set_rel_size
211  *        Set size estimates for a base relation
212  */
213 static void
214 set_rel_size(PlannerInfo *root, RelOptInfo *rel,
215                                  Index rti, RangeTblEntry *rte)
216 {
217         if (rel->reloptkind == RELOPT_BASEREL &&
218                 relation_excluded_by_constraints(root, rel, rte))
219         {
220                 /*
221                  * We proved we don't need to scan the rel via constraint exclusion,
222                  * so set up a single dummy path for it.  Here we only check this for
223                  * regular baserels; if it's an otherrel, CE was already checked in
224                  * set_append_rel_pathlist().
225                  *
226                  * In this case, we go ahead and set up the relation's path right away
227                  * instead of leaving it for set_rel_pathlist to do.  This is because
228                  * we don't have a convention for marking a rel as dummy except by
229                  * assigning a dummy path to it.
230                  */
231                 set_dummy_rel_pathlist(rel);
232         }
233         else if (rte->inh)
234         {
235                 /* It's an "append relation", process accordingly */
236                 set_append_rel_size(root, rel, rti, rte);
237         }
238         else
239         {
240                 switch (rel->rtekind)
241                 {
242                         case RTE_RELATION:
243                                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
244                                 {
245                                         /* Foreign table */
246                                         set_foreign_size(root, rel, rte);
247                                 }
248                                 else
249                                 {
250                                         /* Plain relation */
251                                         set_plain_rel_size(root, rel, rte);
252                                 }
253                                 break;
254                         case RTE_SUBQUERY:
255                                 /*
256                                  * Subqueries don't support parameterized paths, so just go
257                                  * ahead and build their paths immediately.
258                                  */
259                                 set_subquery_pathlist(root, rel, rti, rte);
260                                 break;
261                         case RTE_FUNCTION:
262                                 set_function_size_estimates(root, rel);
263                                 break;
264                         case RTE_VALUES:
265                                 set_values_size_estimates(root, rel);
266                                 break;
267                         case RTE_CTE:
268                                 /*
269                                  * CTEs don't support parameterized paths, so just go ahead
270                                  * and build their paths immediately.
271                                  */
272                                 if (rte->self_reference)
273                                         set_worktable_pathlist(root, rel, rte);
274                                 else
275                                         set_cte_pathlist(root, rel, rte);
276                                 break;
277                         default:
278                                 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
279                                 break;
280                 }
281         }
282 }
283
284 /*
285  * set_rel_pathlist
286  *        Build access paths for a base relation
287  */
288 static void
289 set_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
290                                  Index rti, RangeTblEntry *rte)
291 {
292         if (IS_DUMMY_REL(rel))
293         {
294                 /* We already proved the relation empty, so nothing more to do */
295         }
296         else if (rte->inh)
297         {
298                 /* It's an "append relation", process accordingly */
299                 set_append_rel_pathlist(root, rel, rti, rte);
300         }
301         else
302         {
303                 switch (rel->rtekind)
304                 {
305                         case RTE_RELATION:
306                                 if (rte->relkind == RELKIND_FOREIGN_TABLE)
307                                 {
308                                         /* Foreign table */
309                                         set_foreign_pathlist(root, rel, rte);
310                                 }
311                                 else
312                                 {
313                                         /* Plain relation */
314                                         set_plain_rel_pathlist(root, rel, rte);
315                                 }
316                                 break;
317                         case RTE_SUBQUERY:
318                                 /* Subquery --- fully handled during set_rel_size */
319                                 break;
320                         case RTE_FUNCTION:
321                                 /* RangeFunction */
322                                 set_function_pathlist(root, rel, rte);
323                                 break;
324                         case RTE_VALUES:
325                                 /* Values list */
326                                 set_values_pathlist(root, rel, rte);
327                                 break;
328                         case RTE_CTE:
329                                 /* CTE reference --- fully handled during set_rel_size */
330                                 break;
331                         default:
332                                 elog(ERROR, "unexpected rtekind: %d", (int) rel->rtekind);
333                                 break;
334                 }
335         }
336
337 #ifdef OPTIMIZER_DEBUG
338         debug_print_rel(root, rel);
339 #endif
340 }
341
342 /*
343  * set_plain_rel_size
344  *        Set size estimates for a plain relation (no subquery, no inheritance)
345  */
346 static void
347 set_plain_rel_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
348 {
349         /*
350          * Test any partial indexes of rel for applicability.  We must do this
351          * first since partial unique indexes can affect size estimates.
352          */
353         check_partial_indexes(root, rel);
354
355         /* Mark rel with estimated output rows, width, etc */
356         set_baserel_size_estimates(root, rel);
357
358         /*
359          * Check to see if we can extract any restriction conditions from join
360          * quals that are OR-of-AND structures.  If so, add them to the rel's
361          * restriction list, and redo the above steps.
362          */
363         if (create_or_index_quals(root, rel))
364         {
365                 check_partial_indexes(root, rel);
366                 set_baserel_size_estimates(root, rel);
367         }
368 }
369
370 /*
371  * set_plain_rel_pathlist
372  *        Build access paths for a plain relation (no subquery, no inheritance)
373  */
374 static void
375 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
376 {
377         /* Consider sequential scan */
378         add_path(rel, create_seqscan_path(root, rel));
379
380         /* Consider index scans */
381         create_index_paths(root, rel);
382
383         /* Consider TID scans */
384         create_tidscan_paths(root, rel);
385
386         /* Now find the cheapest of the paths for this rel */
387         set_cheapest(rel);
388 }
389
390 /*
391  * set_foreign_size
392  *              Set size estimates for a foreign table RTE
393  */
394 static void
395 set_foreign_size(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
396 {
397         /* Mark rel with estimated output rows, width, etc */
398         set_foreign_size_estimates(root, rel);
399
400         /* Get FDW routine pointers for the rel */
401         rel->fdwroutine = GetFdwRoutineByRelId(rte->relid);
402
403         /* Let FDW adjust the size estimates, if it can */
404         rel->fdwroutine->GetForeignRelSize(root, rel, rte->relid);
405 }
406
407 /*
408  * set_foreign_pathlist
409  *              Build access paths for a foreign table RTE
410  */
411 static void
412 set_foreign_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
413 {
414         /* Call the FDW's GetForeignPaths function to generate path(s) */
415         rel->fdwroutine->GetForeignPaths(root, rel, rte->relid);
416
417         /* Select cheapest path */
418         set_cheapest(rel);
419 }
420
421 /*
422  * set_append_rel_size
423  *        Set size estimates for an "append relation"
424  *
425  * The passed-in rel and RTE represent the entire append relation.      The
426  * relation's contents are computed by appending together the output of
427  * the individual member relations.  Note that in the inheritance case,
428  * the first member relation is actually the same table as is mentioned in
429  * the parent RTE ... but it has a different RTE and RelOptInfo.  This is
430  * a good thing because their outputs are not the same size.
431  */
432 static void
433 set_append_rel_size(PlannerInfo *root, RelOptInfo *rel,
434                                         Index rti, RangeTblEntry *rte)
435 {
436         int                     parentRTindex = rti;
437         double          parent_rows;
438         double          parent_size;
439         double     *parent_attrsizes;
440         int                     nattrs;
441         ListCell   *l;
442
443         /*
444          * Initialize to compute size estimates for whole append relation.
445          *
446          * We handle width estimates by weighting the widths of different child
447          * rels proportionally to their number of rows.  This is sensible because
448          * the use of width estimates is mainly to compute the total relation
449          * "footprint" if we have to sort or hash it.  To do this, we sum the
450          * total equivalent size (in "double" arithmetic) and then divide by the
451          * total rowcount estimate.  This is done separately for the total rel
452          * width and each attribute.
453          *
454          * Note: if you consider changing this logic, beware that child rels could
455          * have zero rows and/or width, if they were excluded by constraints.
456          */
457         parent_rows = 0;
458         parent_size = 0;
459         nattrs = rel->max_attr - rel->min_attr + 1;
460         parent_attrsizes = (double *) palloc0(nattrs * sizeof(double));
461
462         foreach(l, root->append_rel_list)
463         {
464                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
465                 int                     childRTindex;
466                 RangeTblEntry *childRTE;
467                 RelOptInfo *childrel;
468                 List       *childquals;
469                 Node       *childqual;
470                 ListCell   *parentvars;
471                 ListCell   *childvars;
472
473                 /* append_rel_list contains all append rels; ignore others */
474                 if (appinfo->parent_relid != parentRTindex)
475                         continue;
476
477                 childRTindex = appinfo->child_relid;
478                 childRTE = root->simple_rte_array[childRTindex];
479
480                 /*
481                  * The child rel's RelOptInfo was already created during
482                  * add_base_rels_to_query.
483                  */
484                 childrel = find_base_rel(root, childRTindex);
485                 Assert(childrel->reloptkind == RELOPT_OTHER_MEMBER_REL);
486
487                 /*
488                  * We have to copy the parent's targetlist and quals to the child,
489                  * with appropriate substitution of variables.  However, only the
490                  * baserestrictinfo quals are needed before we can check for
491                  * constraint exclusion; so do that first and then check to see if we
492                  * can disregard this child.
493                  *
494                  * As of 8.4, the child rel's targetlist might contain non-Var
495                  * expressions, which means that substitution into the quals could
496                  * produce opportunities for const-simplification, and perhaps even
497                  * pseudoconstant quals.  To deal with this, we strip the RestrictInfo
498                  * nodes, do the substitution, do const-simplification, and then
499                  * reconstitute the RestrictInfo layer.
500                  */
501                 childquals = get_all_actual_clauses(rel->baserestrictinfo);
502                 childquals = (List *) adjust_appendrel_attrs(root,
503                                                                                                          (Node *) childquals,
504                                                                                                          appinfo);
505                 childqual = eval_const_expressions(root, (Node *)
506                                                                                    make_ands_explicit(childquals));
507                 if (childqual && IsA(childqual, Const) &&
508                         (((Const *) childqual)->constisnull ||
509                          !DatumGetBool(((Const *) childqual)->constvalue)))
510                 {
511                         /*
512                          * Restriction reduces to constant FALSE or constant NULL after
513                          * substitution, so this child need not be scanned.
514                          */
515                         set_dummy_rel_pathlist(childrel);
516                         continue;
517                 }
518                 childquals = make_ands_implicit((Expr *) childqual);
519                 childquals = make_restrictinfos_from_actual_clauses(root,
520                                                                                                                         childquals);
521                 childrel->baserestrictinfo = childquals;
522
523                 if (relation_excluded_by_constraints(root, childrel, childRTE))
524                 {
525                         /*
526                          * This child need not be scanned, so we can omit it from the
527                          * appendrel.
528                          */
529                         set_dummy_rel_pathlist(childrel);
530                         continue;
531                 }
532
533                 /*
534                  * CE failed, so finish copying/modifying targetlist and join quals.
535                  *
536                  * Note: the resulting childrel->reltargetlist may contain arbitrary
537                  * expressions, which normally would not occur in a reltargetlist.
538                  * That is okay because nothing outside of this routine will look at
539                  * the child rel's reltargetlist.  We do have to cope with the case
540                  * while constructing attr_widths estimates below, though.
541                  */
542                 childrel->joininfo = (List *)
543                         adjust_appendrel_attrs(root,
544                                                                    (Node *) rel->joininfo,
545                                                                    appinfo);
546                 childrel->reltargetlist = (List *)
547                         adjust_appendrel_attrs(root,
548                                                                    (Node *) rel->reltargetlist,
549                                                                    appinfo);
550
551                 /*
552                  * We have to make child entries in the EquivalenceClass data
553                  * structures as well.  This is needed either if the parent
554                  * participates in some eclass joins (because we will want to consider
555                  * inner-indexscan joins on the individual children) or if the parent
556                  * has useful pathkeys (because we should try to build MergeAppend
557                  * paths that produce those sort orderings).
558                  */
559                 if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
560                         add_child_rel_equivalences(root, appinfo, rel, childrel);
561                 childrel->has_eclass_joins = rel->has_eclass_joins;
562
563                 /*
564                  * Note: we could compute appropriate attr_needed data for the child's
565                  * variables, by transforming the parent's attr_needed through the
566                  * translated_vars mapping.  However, currently there's no need
567                  * because attr_needed is only examined for base relations not
568                  * otherrels.  So we just leave the child's attr_needed empty.
569                  */
570
571                 /*
572                  * Compute the child's size.
573                  */
574                 set_rel_size(root, childrel, childRTindex, childRTE);
575
576                 /*
577                  * It is possible that constraint exclusion detected a contradiction
578                  * within a child subquery, even though we didn't prove one above.
579                  * If so, we can skip this child.
580                  */
581                 if (IS_DUMMY_REL(childrel))
582                         continue;
583
584                 /*
585                  * Accumulate size information from each live child.
586                  */
587                 if (childrel->rows > 0)
588                 {
589                         parent_rows += childrel->rows;
590                         parent_size += childrel->width * childrel->rows;
591
592                         /*
593                          * Accumulate per-column estimates too.  We need not do anything
594                          * for PlaceHolderVars in the parent list.  If child expression
595                          * isn't a Var, or we didn't record a width estimate for it, we
596                          * have to fall back on a datatype-based estimate.
597                          *
598                          * By construction, child's reltargetlist is 1-to-1 with parent's.
599                          */
600                         forboth(parentvars, rel->reltargetlist,
601                                         childvars, childrel->reltargetlist)
602                         {
603                                 Var                *parentvar = (Var *) lfirst(parentvars);
604                                 Node       *childvar = (Node *) lfirst(childvars);
605
606                                 if (IsA(parentvar, Var))
607                                 {
608                                         int                     pndx = parentvar->varattno - rel->min_attr;
609                                         int32           child_width = 0;
610
611                                         if (IsA(childvar, Var))
612                                         {
613                                                 int             cndx = ((Var *) childvar)->varattno - childrel->min_attr;
614
615                                                 child_width = childrel->attr_widths[cndx];
616                                         }
617                                         if (child_width <= 0)
618                                                 child_width = get_typavgwidth(exprType(childvar),
619                                                                                                           exprTypmod(childvar));
620                                         Assert(child_width > 0);
621                                         parent_attrsizes[pndx] += child_width * childrel->rows;
622                                 }
623                         }
624                 }
625         }
626
627         /*
628          * Save the finished size estimates.
629          */
630         rel->rows = parent_rows;
631         if (parent_rows > 0)
632         {
633                 int                     i;
634
635                 rel->width = rint(parent_size / parent_rows);
636                 for (i = 0; i < nattrs; i++)
637                         rel->attr_widths[i] = rint(parent_attrsizes[i] / parent_rows);
638         }
639         else
640                 rel->width = 0;                 /* attr_widths should be zero already */
641
642         /*
643          * Set "raw tuples" count equal to "rows" for the appendrel; needed
644          * because some places assume rel->tuples is valid for any baserel.
645          */
646         rel->tuples = parent_rows;
647
648         pfree(parent_attrsizes);
649 }
650
651 /*
652  * set_append_rel_pathlist
653  *        Build access paths for an "append relation"
654  */
655 static void
656 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
657                                                 Index rti, RangeTblEntry *rte)
658 {
659         int                     parentRTindex = rti;
660         List       *live_childrels = NIL;
661         List       *subpaths = NIL;
662         List       *all_child_pathkeys = NIL;
663         List       *all_child_outers = NIL;
664         ListCell   *l;
665
666         /*
667          * Generate access paths for each member relation, and remember the
668          * cheapest path for each one.  Also, identify all pathkeys (orderings)
669          * and parameterizations (required_outer sets) available for the member
670          * relations.
671          */
672         foreach(l, root->append_rel_list)
673         {
674                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
675                 int                     childRTindex;
676                 RangeTblEntry *childRTE;
677                 RelOptInfo *childrel;
678                 ListCell   *lcp;
679
680                 /* append_rel_list contains all append rels; ignore others */
681                 if (appinfo->parent_relid != parentRTindex)
682                         continue;
683
684                 /* Re-locate the child RTE and RelOptInfo */
685                 childRTindex = appinfo->child_relid;
686                 childRTE = root->simple_rte_array[childRTindex];
687                 childrel = root->simple_rel_array[childRTindex];
688
689                 /*
690                  * Compute the child's access paths.
691                  */
692                 set_rel_pathlist(root, childrel, childRTindex, childRTE);
693
694                 /*
695                  * If child is dummy, ignore it.
696                  */
697                 if (IS_DUMMY_REL(childrel))
698                         continue;
699
700                 /*
701                  * Child is live, so add its cheapest access path to the Append path
702                  * we are constructing for the parent.
703                  */
704                 subpaths = accumulate_append_subpath(subpaths,
705                                                                                          childrel->cheapest_total_path);
706
707                 /* Remember which childrels are live, for logic below */
708                 live_childrels = lappend(live_childrels, childrel);
709
710                 /*
711                  * Collect lists of all the available path orderings and
712                  * parameterizations for all the children.  We use these as a
713                  * heuristic to indicate which sort orderings and parameterizations we
714                  * should build Append and MergeAppend paths for.
715                  */
716                 foreach(lcp, childrel->pathlist)
717                 {
718                         Path       *childpath = (Path *) lfirst(lcp);
719                         List       *childkeys = childpath->pathkeys;
720                         Relids          childouter = childpath->required_outer;
721
722                         /* Unsorted paths don't contribute to pathkey list */
723                         if (childkeys != NIL)
724                         {
725                                 ListCell   *lpk;
726                                 bool            found = false;
727
728                                 /* Have we already seen this ordering? */
729                                 foreach(lpk, all_child_pathkeys)
730                                 {
731                                         List       *existing_pathkeys = (List *) lfirst(lpk);
732
733                                         if (compare_pathkeys(existing_pathkeys,
734                                                                                  childkeys) == PATHKEYS_EQUAL)
735                                         {
736                                                 found = true;
737                                                 break;
738                                         }
739                                 }
740                                 if (!found)
741                                 {
742                                         /* No, so add it to all_child_pathkeys */
743                                         all_child_pathkeys = lappend(all_child_pathkeys,
744                                                                                                  childkeys);
745                                 }
746                         }
747
748                         /* Unparameterized paths don't contribute to param-set list */
749                         if (childouter)
750                         {
751                                 ListCell   *lco;
752                                 bool            found = false;
753
754                                 /* Have we already seen this param set? */
755                                 foreach(lco, all_child_outers)
756                                 {
757                                         Relids  existing_outers = (Relids) lfirst(lco);
758
759                                         if (bms_equal(existing_outers, childouter))
760                                         {
761                                                 found = true;
762                                                 break;
763                                         }
764                                 }
765                                 if (!found)
766                                 {
767                                         /* No, so add it to all_child_outers */
768                                         all_child_outers = lappend(all_child_outers,
769                                                                                            childouter);
770                                 }
771                         }
772                 }
773         }
774
775         /*
776          * Next, build an unordered, unparameterized Append path for the rel.
777          * (Note: this is correct even if we have zero or one live subpath due to
778          * constraint exclusion.)
779          */
780         add_path(rel, (Path *) create_append_path(rel, subpaths));
781
782         /*
783          * Build unparameterized MergeAppend paths based on the collected list of
784          * child pathkeys.
785          */
786         generate_mergeappend_paths(root, rel, live_childrels,
787                                                            all_child_pathkeys, NULL);
788
789         /*
790          * Build Append and MergeAppend paths for each parameterization seen
791          * among the child rels.  (This may look pretty expensive, but in most
792          * cases of practical interest, the child relations will tend to expose
793          * the same parameterizations and pathkeys, so that not that many cases
794          * actually get considered here.)
795          */
796         foreach(l, all_child_outers)
797         {
798                 Relids  required_outer = (Relids) lfirst(l);
799                 ListCell   *lcr;
800
801                 /* Select the child paths for an Append with this parameterization */
802                 subpaths = NIL;
803                 foreach(lcr, live_childrels)
804                 {
805                         RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
806                         Path       *cheapest_total;
807
808                         cheapest_total =
809                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
810                                                                                            NIL,
811                                                                                            required_outer,
812                                                                                            TOTAL_COST);
813                         Assert(cheapest_total != NULL);
814
815                         subpaths = accumulate_append_subpath(subpaths, cheapest_total);
816                 }
817                 add_path(rel, (Path *) create_append_path(rel, subpaths));
818
819                 /* And build parameterized MergeAppend paths */
820                 generate_mergeappend_paths(root, rel, live_childrels,
821                                                                    all_child_pathkeys, required_outer);
822         }
823
824         /* Select cheapest paths */
825         set_cheapest(rel);
826 }
827
828 /*
829  * generate_mergeappend_paths
830  *              Generate MergeAppend paths for an append relation
831  *
832  * Generate a path for each ordering (pathkey list) appearing in
833  * all_child_pathkeys.  If required_outer isn't NULL, accept paths having
834  * those relations as required outer relations.
835  *
836  * We consider both cheapest-startup and cheapest-total cases, ie, for each
837  * interesting ordering, collect all the cheapest startup subpaths and all the
838  * cheapest total paths, and build a MergeAppend path for each case.
839  */
840 static void
841 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
842                                                    List *live_childrels,
843                                                    List *all_child_pathkeys,
844                                                    Relids required_outer)
845 {
846         ListCell   *lcp;
847
848         foreach(lcp, all_child_pathkeys)
849         {
850                 List       *pathkeys = (List *) lfirst(lcp);
851                 List       *startup_subpaths = NIL;
852                 List       *total_subpaths = NIL;
853                 bool            startup_neq_total = false;
854                 ListCell   *lcr;
855
856                 /* Select the child paths for this ordering... */
857                 foreach(lcr, live_childrels)
858                 {
859                         RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
860                         Path       *cheapest_startup,
861                                            *cheapest_total;
862
863                         /* Locate the right paths, if they are available. */
864                         cheapest_startup =
865                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
866                                                                                            pathkeys,
867                                                                                            required_outer,
868                                                                                            STARTUP_COST);
869                         cheapest_total =
870                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
871                                                                                            pathkeys,
872                                                                                            required_outer,
873                                                                                            TOTAL_COST);
874
875                         /*
876                          * If we can't find any paths with the right order just use the
877                          * cheapest-total path; we'll have to sort it later.  We can
878                          * use the cheapest path for the parameterization, though.
879                          */
880                         if (cheapest_startup == NULL || cheapest_total == NULL)
881                         {
882                                 if (required_outer)
883                                         cheapest_startup = cheapest_total =
884                                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
885                                                                                                            NIL,
886                                                                                                            required_outer,
887                                                                                                            TOTAL_COST);
888                                 else
889                                         cheapest_startup = cheapest_total =
890                                                 childrel->cheapest_total_path;
891                                 Assert(cheapest_total != NULL);
892                         }
893
894                         /*
895                          * Notice whether we actually have different paths for the
896                          * "cheapest" and "total" cases; frequently there will be no point
897                          * in two create_merge_append_path() calls.
898                          */
899                         if (cheapest_startup != cheapest_total)
900                                 startup_neq_total = true;
901
902                         startup_subpaths =
903                                 accumulate_append_subpath(startup_subpaths, cheapest_startup);
904                         total_subpaths =
905                                 accumulate_append_subpath(total_subpaths, cheapest_total);
906                 }
907
908                 /* ... and build the MergeAppend paths */
909                 add_path(rel, (Path *) create_merge_append_path(root,
910                                                                                                                 rel,
911                                                                                                                 startup_subpaths,
912                                                                                                                 pathkeys));
913                 if (startup_neq_total)
914                         add_path(rel, (Path *) create_merge_append_path(root,
915                                                                                                                         rel,
916                                                                                                                         total_subpaths,
917                                                                                                                         pathkeys));
918         }
919 }
920
921 /*
922  * accumulate_append_subpath
923  *              Add a subpath to the list being built for an Append or MergeAppend
924  *
925  * It's possible that the child is itself an Append path, in which case
926  * we can "cut out the middleman" and just add its child paths to our
927  * own list.  (We don't try to do this earlier because we need to
928  * apply both levels of transformation to the quals.)
929  */
930 static List *
931 accumulate_append_subpath(List *subpaths, Path *path)
932 {
933         if (IsA(path, AppendPath))
934         {
935                 AppendPath *apath = (AppendPath *) path;
936
937                 /* list_copy is important here to avoid sharing list substructure */
938                 return list_concat(subpaths, list_copy(apath->subpaths));
939         }
940         else
941                 return lappend(subpaths, path);
942 }
943
944 /*
945  * set_dummy_rel_pathlist
946  *        Build a dummy path for a relation that's been excluded by constraints
947  *
948  * Rather than inventing a special "dummy" path type, we represent this as an
949  * AppendPath with no members (see also IS_DUMMY_PATH/IS_DUMMY_REL macros).
950  */
951 static void
952 set_dummy_rel_pathlist(RelOptInfo *rel)
953 {
954         /* Set dummy size estimates --- we leave attr_widths[] as zeroes */
955         rel->rows = 0;
956         rel->width = 0;
957
958         /* Discard any pre-existing paths; no further need for them */
959         rel->pathlist = NIL;
960
961         add_path(rel, (Path *) create_append_path(rel, NIL));
962
963         /* Select cheapest path (pretty easy in this case...) */
964         set_cheapest(rel);
965 }
966
967 /* quick-and-dirty test to see if any joining is needed */
968 static bool
969 has_multiple_baserels(PlannerInfo *root)
970 {
971         int                     num_base_rels = 0;
972         Index           rti;
973
974         for (rti = 1; rti < root->simple_rel_array_size; rti++)
975         {
976                 RelOptInfo *brel = root->simple_rel_array[rti];
977
978                 if (brel == NULL)
979                         continue;
980
981                 /* ignore RTEs that are "other rels" */
982                 if (brel->reloptkind == RELOPT_BASEREL)
983                         if (++num_base_rels > 1)
984                                 return true;
985         }
986         return false;
987 }
988
989 /*
990  * set_subquery_pathlist
991  *              Build the (single) access path for a subquery RTE
992  *
993  * There's no need for a separate set_subquery_size phase, since we don't
994  * support parameterized paths for subqueries.
995  */
996 static void
997 set_subquery_pathlist(PlannerInfo *root, RelOptInfo *rel,
998                                           Index rti, RangeTblEntry *rte)
999 {
1000         Query      *parse = root->parse;
1001         Query      *subquery = rte->subquery;
1002         bool       *differentTypes;
1003         double          tuple_fraction;
1004         PlannerInfo *subroot;
1005         List       *pathkeys;
1006
1007         /*
1008          * Must copy the Query so that planning doesn't mess up the RTE contents
1009          * (really really need to fix the planner to not scribble on its input,
1010          * someday).
1011          */
1012         subquery = copyObject(subquery);
1013
1014         /* We need a workspace for keeping track of set-op type coercions */
1015         differentTypes = (bool *)
1016                 palloc0((list_length(subquery->targetList) + 1) * sizeof(bool));
1017
1018         /*
1019          * If there are any restriction clauses that have been attached to the
1020          * subquery relation, consider pushing them down to become WHERE or HAVING
1021          * quals of the subquery itself.  This transformation is useful because it
1022          * may allow us to generate a better plan for the subquery than evaluating
1023          * all the subquery output rows and then filtering them.
1024          *
1025          * There are several cases where we cannot push down clauses. Restrictions
1026          * involving the subquery are checked by subquery_is_pushdown_safe().
1027          * Restrictions on individual clauses are checked by
1028          * qual_is_pushdown_safe().  Also, we don't want to push down
1029          * pseudoconstant clauses; better to have the gating node above the
1030          * subquery.
1031          *
1032          * Also, if the sub-query has "security_barrier" flag, it means the
1033          * sub-query originated from a view that must enforce row-level security.
1034          * We must not push down quals in order to avoid information leaks, either
1035          * via side-effects or error output.
1036          *
1037          * Non-pushed-down clauses will get evaluated as qpquals of the
1038          * SubqueryScan node.
1039          *
1040          * XXX Are there any cases where we want to make a policy decision not to
1041          * push down a pushable qual, because it'd result in a worse plan?
1042          */
1043         if (rel->baserestrictinfo != NIL &&
1044                 subquery_is_pushdown_safe(subquery, subquery, differentTypes))
1045         {
1046                 /* OK to consider pushing down individual quals */
1047                 List       *upperrestrictlist = NIL;
1048                 ListCell   *l;
1049
1050                 foreach(l, rel->baserestrictinfo)
1051                 {
1052                         RestrictInfo *rinfo = (RestrictInfo *) lfirst(l);
1053                         Node       *clause = (Node *) rinfo->clause;
1054
1055                         if (!rinfo->pseudoconstant &&
1056                                 (!rte->security_barrier ||
1057                                  !contain_leaky_functions(clause)) &&
1058                                 qual_is_pushdown_safe(subquery, rti, clause, differentTypes))
1059                         {
1060                                 /* Push it down */
1061                                 subquery_push_qual(subquery, rte, rti, clause);
1062                         }
1063                         else
1064                         {
1065                                 /* Keep it in the upper query */
1066                                 upperrestrictlist = lappend(upperrestrictlist, rinfo);
1067                         }
1068                 }
1069                 rel->baserestrictinfo = upperrestrictlist;
1070         }
1071
1072         pfree(differentTypes);
1073
1074         /*
1075          * We can safely pass the outer tuple_fraction down to the subquery if the
1076          * outer level has no joining, aggregation, or sorting to do. Otherwise
1077          * we'd better tell the subquery to plan for full retrieval. (XXX This
1078          * could probably be made more intelligent ...)
1079          */
1080         if (parse->hasAggs ||
1081                 parse->groupClause ||
1082                 parse->havingQual ||
1083                 parse->distinctClause ||
1084                 parse->sortClause ||
1085                 has_multiple_baserels(root))
1086                 tuple_fraction = 0.0;   /* default case */
1087         else
1088                 tuple_fraction = root->tuple_fraction;
1089
1090         /* Generate the plan for the subquery */
1091         rel->subplan = subquery_planner(root->glob, subquery,
1092                                                                         root,
1093                                                                         false, tuple_fraction,
1094                                                                         &subroot);
1095         rel->subroot = subroot;
1096
1097         /*
1098          * It's possible that constraint exclusion proved the subquery empty.
1099          * If so, it's convenient to turn it back into a dummy path so that we
1100          * will recognize appropriate optimizations at this level.
1101          */
1102         if (is_dummy_plan(rel->subplan))
1103         {
1104                 set_dummy_rel_pathlist(rel);
1105                 return;
1106         }
1107
1108         /* Mark rel with estimated output rows, width, etc */
1109         set_subquery_size_estimates(root, rel);
1110
1111         /* Convert subquery pathkeys to outer representation */
1112         pathkeys = convert_subquery_pathkeys(root, rel, subroot->query_pathkeys);
1113
1114         /* Generate appropriate path */
1115         add_path(rel, create_subqueryscan_path(rel, pathkeys));
1116
1117         /* Select cheapest path (pretty easy in this case...) */
1118         set_cheapest(rel);
1119 }
1120
1121 /*
1122  * set_function_pathlist
1123  *              Build the (single) access path for a function RTE
1124  */
1125 static void
1126 set_function_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1127 {
1128         /* Generate appropriate path */
1129         add_path(rel, create_functionscan_path(root, rel));
1130
1131         /* Select cheapest path (pretty easy in this case...) */
1132         set_cheapest(rel);
1133 }
1134
1135 /*
1136  * set_values_pathlist
1137  *              Build the (single) access path for a VALUES RTE
1138  */
1139 static void
1140 set_values_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1141 {
1142         /* Generate appropriate path */
1143         add_path(rel, create_valuesscan_path(root, rel));
1144
1145         /* Select cheapest path (pretty easy in this case...) */
1146         set_cheapest(rel);
1147 }
1148
1149 /*
1150  * set_cte_pathlist
1151  *              Build the (single) access path for a non-self-reference CTE RTE
1152  *
1153  * There's no need for a separate set_cte_size phase, since we don't
1154  * support parameterized paths for CTEs.
1155  */
1156 static void
1157 set_cte_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1158 {
1159         Plan       *cteplan;
1160         PlannerInfo *cteroot;
1161         Index           levelsup;
1162         int                     ndx;
1163         ListCell   *lc;
1164         int                     plan_id;
1165
1166         /*
1167          * Find the referenced CTE, and locate the plan previously made for it.
1168          */
1169         levelsup = rte->ctelevelsup;
1170         cteroot = root;
1171         while (levelsup-- > 0)
1172         {
1173                 cteroot = cteroot->parent_root;
1174                 if (!cteroot)                   /* shouldn't happen */
1175                         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1176         }
1177
1178         /*
1179          * Note: cte_plan_ids can be shorter than cteList, if we are still working
1180          * on planning the CTEs (ie, this is a side-reference from another CTE).
1181          * So we mustn't use forboth here.
1182          */
1183         ndx = 0;
1184         foreach(lc, cteroot->parse->cteList)
1185         {
1186                 CommonTableExpr *cte = (CommonTableExpr *) lfirst(lc);
1187
1188                 if (strcmp(cte->ctename, rte->ctename) == 0)
1189                         break;
1190                 ndx++;
1191         }
1192         if (lc == NULL)                         /* shouldn't happen */
1193                 elog(ERROR, "could not find CTE \"%s\"", rte->ctename);
1194         if (ndx >= list_length(cteroot->cte_plan_ids))
1195                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1196         plan_id = list_nth_int(cteroot->cte_plan_ids, ndx);
1197         Assert(plan_id > 0);
1198         cteplan = (Plan *) list_nth(root->glob->subplans, plan_id - 1);
1199
1200         /* Mark rel with estimated output rows, width, etc */
1201         set_cte_size_estimates(root, rel, cteplan);
1202
1203         /* Generate appropriate path */
1204         add_path(rel, create_ctescan_path(root, rel));
1205
1206         /* Select cheapest path (pretty easy in this case...) */
1207         set_cheapest(rel);
1208 }
1209
1210 /*
1211  * set_worktable_pathlist
1212  *              Build the (single) access path for a self-reference CTE RTE
1213  *
1214  * There's no need for a separate set_worktable_size phase, since we don't
1215  * support parameterized paths for CTEs.
1216  */
1217 static void
1218 set_worktable_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
1219 {
1220         Plan       *cteplan;
1221         PlannerInfo *cteroot;
1222         Index           levelsup;
1223
1224         /*
1225          * We need to find the non-recursive term's plan, which is in the plan
1226          * level that's processing the recursive UNION, which is one level *below*
1227          * where the CTE comes from.
1228          */
1229         levelsup = rte->ctelevelsup;
1230         if (levelsup == 0)                      /* shouldn't happen */
1231                 elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1232         levelsup--;
1233         cteroot = root;
1234         while (levelsup-- > 0)
1235         {
1236                 cteroot = cteroot->parent_root;
1237                 if (!cteroot)                   /* shouldn't happen */
1238                         elog(ERROR, "bad levelsup for CTE \"%s\"", rte->ctename);
1239         }
1240         cteplan = cteroot->non_recursive_plan;
1241         if (!cteplan)                           /* shouldn't happen */
1242                 elog(ERROR, "could not find plan for CTE \"%s\"", rte->ctename);
1243
1244         /* Mark rel with estimated output rows, width, etc */
1245         set_cte_size_estimates(root, rel, cteplan);
1246
1247         /* Generate appropriate path */
1248         add_path(rel, create_worktablescan_path(root, rel));
1249
1250         /* Select cheapest path (pretty easy in this case...) */
1251         set_cheapest(rel);
1252 }
1253
1254 /*
1255  * make_rel_from_joinlist
1256  *        Build access paths using a "joinlist" to guide the join path search.
1257  *
1258  * See comments for deconstruct_jointree() for definition of the joinlist
1259  * data structure.
1260  */
1261 static RelOptInfo *
1262 make_rel_from_joinlist(PlannerInfo *root, List *joinlist)
1263 {
1264         int                     levels_needed;
1265         List       *initial_rels;
1266         ListCell   *jl;
1267
1268         /*
1269          * Count the number of child joinlist nodes.  This is the depth of the
1270          * dynamic-programming algorithm we must employ to consider all ways of
1271          * joining the child nodes.
1272          */
1273         levels_needed = list_length(joinlist);
1274
1275         if (levels_needed <= 0)
1276                 return NULL;                    /* nothing to do? */
1277
1278         /*
1279          * Construct a list of rels corresponding to the child joinlist nodes.
1280          * This may contain both base rels and rels constructed according to
1281          * sub-joinlists.
1282          */
1283         initial_rels = NIL;
1284         foreach(jl, joinlist)
1285         {
1286                 Node       *jlnode = (Node *) lfirst(jl);
1287                 RelOptInfo *thisrel;
1288
1289                 if (IsA(jlnode, RangeTblRef))
1290                 {
1291                         int                     varno = ((RangeTblRef *) jlnode)->rtindex;
1292
1293                         thisrel = find_base_rel(root, varno);
1294                 }
1295                 else if (IsA(jlnode, List))
1296                 {
1297                         /* Recurse to handle subproblem */
1298                         thisrel = make_rel_from_joinlist(root, (List *) jlnode);
1299                 }
1300                 else
1301                 {
1302                         elog(ERROR, "unrecognized joinlist node type: %d",
1303                                  (int) nodeTag(jlnode));
1304                         thisrel = NULL;         /* keep compiler quiet */
1305                 }
1306
1307                 initial_rels = lappend(initial_rels, thisrel);
1308         }
1309
1310         if (levels_needed == 1)
1311         {
1312                 /*
1313                  * Single joinlist node, so we're done.
1314                  */
1315                 return (RelOptInfo *) linitial(initial_rels);
1316         }
1317         else
1318         {
1319                 /*
1320                  * Consider the different orders in which we could join the rels,
1321                  * using a plugin, GEQO, or the regular join search code.
1322                  *
1323                  * We put the initial_rels list into a PlannerInfo field because
1324                  * has_legal_joinclause() needs to look at it (ugly :-().
1325                  */
1326                 root->initial_rels = initial_rels;
1327
1328                 if (join_search_hook)
1329                         return (*join_search_hook) (root, levels_needed, initial_rels);
1330                 else if (enable_geqo && levels_needed >= geqo_threshold)
1331                         return geqo(root, levels_needed, initial_rels);
1332                 else
1333                         return standard_join_search(root, levels_needed, initial_rels);
1334         }
1335 }
1336
1337 /*
1338  * standard_join_search
1339  *        Find possible joinpaths for a query by successively finding ways
1340  *        to join component relations into join relations.
1341  *
1342  * 'levels_needed' is the number of iterations needed, ie, the number of
1343  *              independent jointree items in the query.  This is > 1.
1344  *
1345  * 'initial_rels' is a list of RelOptInfo nodes for each independent
1346  *              jointree item.  These are the components to be joined together.
1347  *              Note that levels_needed == list_length(initial_rels).
1348  *
1349  * Returns the final level of join relations, i.e., the relation that is
1350  * the result of joining all the original relations together.
1351  * At least one implementation path must be provided for this relation and
1352  * all required sub-relations.
1353  *
1354  * To support loadable plugins that modify planner behavior by changing the
1355  * join searching algorithm, we provide a hook variable that lets a plugin
1356  * replace or supplement this function.  Any such hook must return the same
1357  * final join relation as the standard code would, but it might have a
1358  * different set of implementation paths attached, and only the sub-joinrels
1359  * needed for these paths need have been instantiated.
1360  *
1361  * Note to plugin authors: the functions invoked during standard_join_search()
1362  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
1363  * than one join-order search, you'll probably need to save and restore the
1364  * original states of those data structures.  See geqo_eval() for an example.
1365  */
1366 RelOptInfo *
1367 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
1368 {
1369         int                     lev;
1370         RelOptInfo *rel;
1371
1372         /*
1373          * This function cannot be invoked recursively within any one planning
1374          * problem, so join_rel_level[] can't be in use already.
1375          */
1376         Assert(root->join_rel_level == NULL);
1377
1378         /*
1379          * We employ a simple "dynamic programming" algorithm: we first find all
1380          * ways to build joins of two jointree items, then all ways to build joins
1381          * of three items (from two-item joins and single items), then four-item
1382          * joins, and so on until we have considered all ways to join all the
1383          * items into one rel.
1384          *
1385          * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
1386          * set root->join_rel_level[1] to represent all the single-jointree-item
1387          * relations.
1388          */
1389         root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
1390
1391         root->join_rel_level[1] = initial_rels;
1392
1393         for (lev = 2; lev <= levels_needed; lev++)
1394         {
1395                 ListCell   *lc;
1396
1397                 /*
1398                  * Determine all possible pairs of relations to be joined at this
1399                  * level, and build paths for making each one from every available
1400                  * pair of lower-level relations.
1401                  */
1402                 join_search_one_level(root, lev);
1403
1404                 /*
1405                  * Do cleanup work on each just-processed rel.
1406                  */
1407                 foreach(lc, root->join_rel_level[lev])
1408                 {
1409                         rel = (RelOptInfo *) lfirst(lc);
1410
1411                         /* Find and save the cheapest paths for this rel */
1412                         set_cheapest(rel);
1413
1414 #ifdef OPTIMIZER_DEBUG
1415                         debug_print_rel(root, rel);
1416 #endif
1417                 }
1418         }
1419
1420         /*
1421          * We should have a single rel at the final level.
1422          */
1423         if (root->join_rel_level[levels_needed] == NIL)
1424                 elog(ERROR, "failed to build any %d-way joins", levels_needed);
1425         Assert(list_length(root->join_rel_level[levels_needed]) == 1);
1426
1427         rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
1428
1429         root->join_rel_level = NULL;
1430
1431         return rel;
1432 }
1433
1434 /*****************************************************************************
1435  *                      PUSHING QUALS DOWN INTO SUBQUERIES
1436  *****************************************************************************/
1437
1438 /*
1439  * subquery_is_pushdown_safe - is a subquery safe for pushing down quals?
1440  *
1441  * subquery is the particular component query being checked.  topquery
1442  * is the top component of a set-operations tree (the same Query if no
1443  * set-op is involved).
1444  *
1445  * Conditions checked here:
1446  *
1447  * 1. If the subquery has a LIMIT clause, we must not push down any quals,
1448  * since that could change the set of rows returned.
1449  *
1450  * 2. If the subquery contains any window functions, we can't push quals
1451  * into it, because that could change the results.
1452  *
1453  * 3. If the subquery contains EXCEPT or EXCEPT ALL set ops we cannot push
1454  * quals into it, because that could change the results.
1455  *
1456  * 4. For subqueries using UNION/UNION ALL/INTERSECT/INTERSECT ALL, we can
1457  * push quals into each component query, but the quals can only reference
1458  * subquery columns that suffer no type coercions in the set operation.
1459  * Otherwise there are possible semantic gotchas.  So, we check the
1460  * component queries to see if any of them have different output types;
1461  * differentTypes[k] is set true if column k has different type in any
1462  * component.
1463  */
1464 static bool
1465 subquery_is_pushdown_safe(Query *subquery, Query *topquery,
1466                                                   bool *differentTypes)
1467 {
1468         SetOperationStmt *topop;
1469
1470         /* Check point 1 */
1471         if (subquery->limitOffset != NULL || subquery->limitCount != NULL)
1472                 return false;
1473
1474         /* Check point 2 */
1475         if (subquery->hasWindowFuncs)
1476                 return false;
1477
1478         /* Are we at top level, or looking at a setop component? */
1479         if (subquery == topquery)
1480         {
1481                 /* Top level, so check any component queries */
1482                 if (subquery->setOperations != NULL)
1483                         if (!recurse_pushdown_safe(subquery->setOperations, topquery,
1484                                                                            differentTypes))
1485                                 return false;
1486         }
1487         else
1488         {
1489                 /* Setop component must not have more components (too weird) */
1490                 if (subquery->setOperations != NULL)
1491                         return false;
1492                 /* Check whether setop component output types match top level */
1493                 topop = (SetOperationStmt *) topquery->setOperations;
1494                 Assert(topop && IsA(topop, SetOperationStmt));
1495                 compare_tlist_datatypes(subquery->targetList,
1496                                                                 topop->colTypes,
1497                                                                 differentTypes);
1498         }
1499         return true;
1500 }
1501
1502 /*
1503  * Helper routine to recurse through setOperations tree
1504  */
1505 static bool
1506 recurse_pushdown_safe(Node *setOp, Query *topquery,
1507                                           bool *differentTypes)
1508 {
1509         if (IsA(setOp, RangeTblRef))
1510         {
1511                 RangeTblRef *rtr = (RangeTblRef *) setOp;
1512                 RangeTblEntry *rte = rt_fetch(rtr->rtindex, topquery->rtable);
1513                 Query      *subquery = rte->subquery;
1514
1515                 Assert(subquery != NULL);
1516                 return subquery_is_pushdown_safe(subquery, topquery, differentTypes);
1517         }
1518         else if (IsA(setOp, SetOperationStmt))
1519         {
1520                 SetOperationStmt *op = (SetOperationStmt *) setOp;
1521
1522                 /* EXCEPT is no good */
1523                 if (op->op == SETOP_EXCEPT)
1524                         return false;
1525                 /* Else recurse */
1526                 if (!recurse_pushdown_safe(op->larg, topquery, differentTypes))
1527                         return false;
1528                 if (!recurse_pushdown_safe(op->rarg, topquery, differentTypes))
1529                         return false;
1530         }
1531         else
1532         {
1533                 elog(ERROR, "unrecognized node type: %d",
1534                          (int) nodeTag(setOp));
1535         }
1536         return true;
1537 }
1538
1539 /*
1540  * Compare tlist's datatypes against the list of set-operation result types.
1541  * For any items that are different, mark the appropriate element of
1542  * differentTypes[] to show that this column will have type conversions.
1543  *
1544  * We don't have to care about typmods here: the only allowed difference
1545  * between set-op input and output typmods is input is a specific typmod
1546  * and output is -1, and that does not require a coercion.
1547  */
1548 static void
1549 compare_tlist_datatypes(List *tlist, List *colTypes,
1550                                                 bool *differentTypes)
1551 {
1552         ListCell   *l;
1553         ListCell   *colType = list_head(colTypes);
1554
1555         foreach(l, tlist)
1556         {
1557                 TargetEntry *tle = (TargetEntry *) lfirst(l);
1558
1559                 if (tle->resjunk)
1560                         continue;                       /* ignore resjunk columns */
1561                 if (colType == NULL)
1562                         elog(ERROR, "wrong number of tlist entries");
1563                 if (exprType((Node *) tle->expr) != lfirst_oid(colType))
1564                         differentTypes[tle->resno] = true;
1565                 colType = lnext(colType);
1566         }
1567         if (colType != NULL)
1568                 elog(ERROR, "wrong number of tlist entries");
1569 }
1570
1571 /*
1572  * qual_is_pushdown_safe - is a particular qual safe to push down?
1573  *
1574  * qual is a restriction clause applying to the given subquery (whose RTE
1575  * has index rti in the parent query).
1576  *
1577  * Conditions checked here:
1578  *
1579  * 1. The qual must not contain any subselects (mainly because I'm not sure
1580  * it will work correctly: sublinks will already have been transformed into
1581  * subplans in the qual, but not in the subquery).
1582  *
1583  * 2. The qual must not refer to the whole-row output of the subquery
1584  * (since there is no easy way to name that within the subquery itself).
1585  *
1586  * 3. The qual must not refer to any subquery output columns that were
1587  * found to have inconsistent types across a set operation tree by
1588  * subquery_is_pushdown_safe().
1589  *
1590  * 4. If the subquery uses DISTINCT ON, we must not push down any quals that
1591  * refer to non-DISTINCT output columns, because that could change the set
1592  * of rows returned.  (This condition is vacuous for DISTINCT, because then
1593  * there are no non-DISTINCT output columns, so we needn't check.  But note
1594  * we are assuming that the qual can't distinguish values that the DISTINCT
1595  * operator sees as equal.      This is a bit shaky but we have no way to test
1596  * for the case, and it's unlikely enough that we shouldn't refuse the
1597  * optimization just because it could theoretically happen.)
1598  *
1599  * 5. We must not push down any quals that refer to subselect outputs that
1600  * return sets, else we'd introduce functions-returning-sets into the
1601  * subquery's WHERE/HAVING quals.
1602  *
1603  * 6. We must not push down any quals that refer to subselect outputs that
1604  * contain volatile functions, for fear of introducing strange results due
1605  * to multiple evaluation of a volatile function.
1606  */
1607 static bool
1608 qual_is_pushdown_safe(Query *subquery, Index rti, Node *qual,
1609                                           bool *differentTypes)
1610 {
1611         bool            safe = true;
1612         List       *vars;
1613         ListCell   *vl;
1614         Bitmapset  *tested = NULL;
1615
1616         /* Refuse subselects (point 1) */
1617         if (contain_subplans(qual))
1618                 return false;
1619
1620         /*
1621          * It would be unsafe to push down window function calls, but at least for
1622          * the moment we could never see any in a qual anyhow.  (The same applies
1623          * to aggregates, which we check for in pull_var_clause below.)
1624          */
1625         Assert(!contain_window_function(qual));
1626
1627         /*
1628          * Examine all Vars used in clause; since it's a restriction clause, all
1629          * such Vars must refer to subselect output columns.
1630          */
1631         vars = pull_var_clause(qual,
1632                                                    PVC_REJECT_AGGREGATES,
1633                                                    PVC_INCLUDE_PLACEHOLDERS);
1634         foreach(vl, vars)
1635         {
1636                 Var                *var = (Var *) lfirst(vl);
1637                 TargetEntry *tle;
1638
1639                 /*
1640                  * XXX Punt if we find any PlaceHolderVars in the restriction clause.
1641                  * It's not clear whether a PHV could safely be pushed down, and even
1642                  * less clear whether such a situation could arise in any cases of
1643                  * practical interest anyway.  So for the moment, just refuse to push
1644                  * down.
1645                  */
1646                 if (!IsA(var, Var))
1647                 {
1648                         safe = false;
1649                         break;
1650                 }
1651
1652                 Assert(var->varno == rti);
1653
1654                 /* Check point 2 */
1655                 if (var->varattno == 0)
1656                 {
1657                         safe = false;
1658                         break;
1659                 }
1660
1661                 /*
1662                  * We use a bitmapset to avoid testing the same attno more than once.
1663                  * (NB: this only works because subquery outputs can't have negative
1664                  * attnos.)
1665                  */
1666                 if (bms_is_member(var->varattno, tested))
1667                         continue;
1668                 tested = bms_add_member(tested, var->varattno);
1669
1670                 /* Check point 3 */
1671                 if (differentTypes[var->varattno])
1672                 {
1673                         safe = false;
1674                         break;
1675                 }
1676
1677                 /* Must find the tlist element referenced by the Var */
1678                 tle = get_tle_by_resno(subquery->targetList, var->varattno);
1679                 Assert(tle != NULL);
1680                 Assert(!tle->resjunk);
1681
1682                 /* If subquery uses DISTINCT ON, check point 4 */
1683                 if (subquery->hasDistinctOn &&
1684                         !targetIsInSortList(tle, InvalidOid, subquery->distinctClause))
1685                 {
1686                         /* non-DISTINCT column, so fail */
1687                         safe = false;
1688                         break;
1689                 }
1690
1691                 /* Refuse functions returning sets (point 5) */
1692                 if (expression_returns_set((Node *) tle->expr))
1693                 {
1694                         safe = false;
1695                         break;
1696                 }
1697
1698                 /* Refuse volatile functions (point 6) */
1699                 if (contain_volatile_functions((Node *) tle->expr))
1700                 {
1701                         safe = false;
1702                         break;
1703                 }
1704         }
1705
1706         list_free(vars);
1707         bms_free(tested);
1708
1709         return safe;
1710 }
1711
1712 /*
1713  * subquery_push_qual - push down a qual that we have determined is safe
1714  */
1715 static void
1716 subquery_push_qual(Query *subquery, RangeTblEntry *rte, Index rti, Node *qual)
1717 {
1718         if (subquery->setOperations != NULL)
1719         {
1720                 /* Recurse to push it separately to each component query */
1721                 recurse_push_qual(subquery->setOperations, subquery,
1722                                                   rte, rti, qual);
1723         }
1724         else
1725         {
1726                 /*
1727                  * We need to replace Vars in the qual (which must refer to outputs of
1728                  * the subquery) with copies of the subquery's targetlist expressions.
1729                  * Note that at this point, any uplevel Vars in the qual should have
1730                  * been replaced with Params, so they need no work.
1731                  *
1732                  * This step also ensures that when we are pushing into a setop tree,
1733                  * each component query gets its own copy of the qual.
1734                  */
1735                 qual = ResolveNew(qual, rti, 0, rte,
1736                                                   subquery->targetList,
1737                                                   CMD_SELECT, 0,
1738                                                   &subquery->hasSubLinks);
1739
1740                 /*
1741                  * Now attach the qual to the proper place: normally WHERE, but if the
1742                  * subquery uses grouping or aggregation, put it in HAVING (since the
1743                  * qual really refers to the group-result rows).
1744                  */
1745                 if (subquery->hasAggs || subquery->groupClause || subquery->havingQual)
1746                         subquery->havingQual = make_and_qual(subquery->havingQual, qual);
1747                 else
1748                         subquery->jointree->quals =
1749                                 make_and_qual(subquery->jointree->quals, qual);
1750
1751                 /*
1752                  * We need not change the subquery's hasAggs or hasSublinks flags,
1753                  * since we can't be pushing down any aggregates that weren't there
1754                  * before, and we don't push down subselects at all.
1755                  */
1756         }
1757 }
1758
1759 /*
1760  * Helper routine to recurse through setOperations tree
1761  */
1762 static void
1763 recurse_push_qual(Node *setOp, Query *topquery,
1764                                   RangeTblEntry *rte, Index rti, Node *qual)
1765 {
1766         if (IsA(setOp, RangeTblRef))
1767         {
1768                 RangeTblRef *rtr = (RangeTblRef *) setOp;
1769                 RangeTblEntry *subrte = rt_fetch(rtr->rtindex, topquery->rtable);
1770                 Query      *subquery = subrte->subquery;
1771
1772                 Assert(subquery != NULL);
1773                 subquery_push_qual(subquery, rte, rti, qual);
1774         }
1775         else if (IsA(setOp, SetOperationStmt))
1776         {
1777                 SetOperationStmt *op = (SetOperationStmt *) setOp;
1778
1779                 recurse_push_qual(op->larg, topquery, rte, rti, qual);
1780                 recurse_push_qual(op->rarg, topquery, rte, rti, qual);
1781         }
1782         else
1783         {
1784                 elog(ERROR, "unrecognized node type: %d",
1785                          (int) nodeTag(setOp));
1786         }
1787 }
1788
1789 /*****************************************************************************
1790  *                      DEBUG SUPPORT
1791  *****************************************************************************/
1792
1793 #ifdef OPTIMIZER_DEBUG
1794
1795 static void
1796 print_relids(Relids relids)
1797 {
1798         Relids          tmprelids;
1799         int                     x;
1800         bool            first = true;
1801
1802         tmprelids = bms_copy(relids);
1803         while ((x = bms_first_member(tmprelids)) >= 0)
1804         {
1805                 if (!first)
1806                         printf(" ");
1807                 printf("%d", x);
1808                 first = false;
1809         }
1810         bms_free(tmprelids);
1811 }
1812
1813 static void
1814 print_restrictclauses(PlannerInfo *root, List *clauses)
1815 {
1816         ListCell   *l;
1817
1818         foreach(l, clauses)
1819         {
1820                 RestrictInfo *c = lfirst(l);
1821
1822                 print_expr((Node *) c->clause, root->parse->rtable);
1823                 if (lnext(l))
1824                         printf(", ");
1825         }
1826 }
1827
1828 static void
1829 print_path(PlannerInfo *root, Path *path, int indent)
1830 {
1831         const char *ptype;
1832         bool            join = false;
1833         Path       *subpath = NULL;
1834         int                     i;
1835
1836         switch (nodeTag(path))
1837         {
1838                 case T_Path:
1839                         ptype = "SeqScan";
1840                         break;
1841                 case T_IndexPath:
1842                         ptype = "IdxScan";
1843                         break;
1844                 case T_BitmapHeapPath:
1845                         ptype = "BitmapHeapScan";
1846                         break;
1847                 case T_BitmapAndPath:
1848                         ptype = "BitmapAndPath";
1849                         break;
1850                 case T_BitmapOrPath:
1851                         ptype = "BitmapOrPath";
1852                         break;
1853                 case T_TidPath:
1854                         ptype = "TidScan";
1855                         break;
1856                 case T_ForeignPath:
1857                         ptype = "ForeignScan";
1858                         break;
1859                 case T_AppendPath:
1860                         ptype = "Append";
1861                         break;
1862                 case T_MergeAppendPath:
1863                         ptype = "MergeAppend";
1864                         break;
1865                 case T_ResultPath:
1866                         ptype = "Result";
1867                         break;
1868                 case T_MaterialPath:
1869                         ptype = "Material";
1870                         subpath = ((MaterialPath *) path)->subpath;
1871                         break;
1872                 case T_UniquePath:
1873                         ptype = "Unique";
1874                         subpath = ((UniquePath *) path)->subpath;
1875                         break;
1876                 case T_NestPath:
1877                         ptype = "NestLoop";
1878                         join = true;
1879                         break;
1880                 case T_MergePath:
1881                         ptype = "MergeJoin";
1882                         join = true;
1883                         break;
1884                 case T_HashPath:
1885                         ptype = "HashJoin";
1886                         join = true;
1887                         break;
1888                 default:
1889                         ptype = "???Path";
1890                         break;
1891         }
1892
1893         for (i = 0; i < indent; i++)
1894                 printf("\t");
1895         printf("%s", ptype);
1896
1897         if (path->parent)
1898         {
1899                 printf("(");
1900                 print_relids(path->parent->relids);
1901                 printf(") rows=%.0f", path->parent->rows);
1902         }
1903         printf(" cost=%.2f..%.2f\n", path->startup_cost, path->total_cost);
1904
1905         if (path->pathkeys)
1906         {
1907                 for (i = 0; i < indent; i++)
1908                         printf("\t");
1909                 printf("  pathkeys: ");
1910                 print_pathkeys(path->pathkeys, root->parse->rtable);
1911         }
1912
1913         if (join)
1914         {
1915                 JoinPath   *jp = (JoinPath *) path;
1916
1917                 for (i = 0; i < indent; i++)
1918                         printf("\t");
1919                 printf("  clauses: ");
1920                 print_restrictclauses(root, jp->joinrestrictinfo);
1921                 printf("\n");
1922
1923                 if (IsA(path, MergePath))
1924                 {
1925                         MergePath  *mp = (MergePath *) path;
1926
1927                         for (i = 0; i < indent; i++)
1928                                 printf("\t");
1929                         printf("  sortouter=%d sortinner=%d materializeinner=%d\n",
1930                                    ((mp->outersortkeys) ? 1 : 0),
1931                                    ((mp->innersortkeys) ? 1 : 0),
1932                                    ((mp->materialize_inner) ? 1 : 0));
1933                 }
1934
1935                 print_path(root, jp->outerjoinpath, indent + 1);
1936                 print_path(root, jp->innerjoinpath, indent + 1);
1937         }
1938
1939         if (subpath)
1940                 print_path(root, subpath, indent + 1);
1941 }
1942
1943 void
1944 debug_print_rel(PlannerInfo *root, RelOptInfo *rel)
1945 {
1946         ListCell   *l;
1947
1948         printf("RELOPTINFO (");
1949         print_relids(rel->relids);
1950         printf("): rows=%.0f width=%d\n", rel->rows, rel->width);
1951
1952         if (rel->baserestrictinfo)
1953         {
1954                 printf("\tbaserestrictinfo: ");
1955                 print_restrictclauses(root, rel->baserestrictinfo);
1956                 printf("\n");
1957         }
1958
1959         if (rel->joininfo)
1960         {
1961                 printf("\tjoininfo: ");
1962                 print_restrictclauses(root, rel->joininfo);
1963                 printf("\n");
1964         }
1965
1966         printf("\tpath list:\n");
1967         foreach(l, rel->pathlist)
1968                 print_path(root, lfirst(l), 1);
1969         printf("\n\tcheapest startup path:\n");
1970         print_path(root, rel->cheapest_startup_path, 1);
1971         printf("\n\tcheapest total path:\n");
1972         print_path(root, rel->cheapest_total_path, 1);
1973         printf("\n");
1974         fflush(stdout);
1975 }
1976
1977 #endif   /* OPTIMIZER_DEBUG */