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