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