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