OSDN Git Service

core.cの各関数をPG93向けに更新した。
[pghintplan/pg_hint_plan.git] / core.c
1 /*-------------------------------------------------------------------------
2  *
3  * core.c
4  *        Routines copied from PostgreSQL core distribution.
5  *
6  * src/backend/optimizer/path/allpaths.c
7  *     set_append_rel_pathlist()
8  *     generate_mergeappend_paths()
9  *     get_cheapest_parameterized_child_path()
10  *     accumulate_append_subpath()
11  *     standard_join_search()
12  *
13  * src/backend/optimizer/path/joinrels.c
14  *     join_search_one_level()
15  *     make_rels_by_clause_joins()
16  *     make_rels_by_clauseless_joins()
17  *     join_is_legal()
18  *     has_join_restriction()
19  *     is_dummy_rel()
20  *     mark_dummy_rel()
21  *     restriction_is_constant_false()
22  *
23  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
24  * Portions Copyright (c) 1994, Regents of the University of California
25  *
26  *-------------------------------------------------------------------------
27  */
28
29 /*
30  * set_append_rel_pathlist
31  *        Build access paths for an "append relation"
32  */
33 static void
34 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
35                                                 Index rti, RangeTblEntry *rte)
36 {
37         int                     parentRTindex = rti;
38         List       *live_childrels = NIL;
39         List       *subpaths = NIL;
40         bool            subpaths_valid = true;
41         List       *all_child_pathkeys = NIL;
42         List       *all_child_outers = NIL;
43         ListCell   *l;
44
45         /*
46          * Generate access paths for each member relation, and remember the
47          * cheapest path for each one.  Also, identify all pathkeys (orderings)
48          * and parameterizations (required_outer sets) available for the member
49          * relations.
50          */
51         foreach(l, root->append_rel_list)
52         {
53                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
54                 int                     childRTindex;
55                 RangeTblEntry *childRTE;
56                 RelOptInfo *childrel;
57                 ListCell   *lcp;
58
59                 /* append_rel_list contains all append rels; ignore others */
60                 if (appinfo->parent_relid != parentRTindex)
61                         continue;
62
63                 /* Re-locate the child RTE and RelOptInfo */
64                 childRTindex = appinfo->child_relid;
65                 childRTE = root->simple_rte_array[childRTindex];
66                 childrel = root->simple_rel_array[childRTindex];
67
68                 /*
69                  * Compute the child's access paths.
70                  */
71                 set_rel_pathlist(root, childrel, childRTindex, childRTE);
72
73                 /*
74                  * If child is dummy, ignore it.
75                  */
76                 if (IS_DUMMY_REL(childrel))
77                         continue;
78
79                 /*
80                  * Child is live, so add it to the live_childrels list for use below.
81                  */
82                 live_childrels = lappend(live_childrels, childrel);
83
84                 /*
85                  * If child has an unparameterized cheapest-total path, add that to
86                  * the unparameterized Append path we are constructing for the parent.
87                  * If not, there's no workable unparameterized path.
88                  */
89                 if (childrel->cheapest_total_path->param_info == NULL)
90                         subpaths = accumulate_append_subpath(subpaths,
91                                                                                           childrel->cheapest_total_path);
92                 else
93                         subpaths_valid = false;
94
95                 /*
96                  * Collect lists of all the available path orderings and
97                  * parameterizations for all the children.      We use these as a
98                  * heuristic to indicate which sort orderings and parameterizations we
99                  * should build Append and MergeAppend paths for.
100                  */
101                 foreach(lcp, childrel->pathlist)
102                 {
103                         Path       *childpath = (Path *) lfirst(lcp);
104                         List       *childkeys = childpath->pathkeys;
105                         Relids          childouter = PATH_REQ_OUTER(childpath);
106
107                         /* Unsorted paths don't contribute to pathkey list */
108                         if (childkeys != NIL)
109                         {
110                                 ListCell   *lpk;
111                                 bool            found = false;
112
113                                 /* Have we already seen this ordering? */
114                                 foreach(lpk, all_child_pathkeys)
115                                 {
116                                         List       *existing_pathkeys = (List *) lfirst(lpk);
117
118                                         if (compare_pathkeys(existing_pathkeys,
119                                                                                  childkeys) == PATHKEYS_EQUAL)
120                                         {
121                                                 found = true;
122                                                 break;
123                                         }
124                                 }
125                                 if (!found)
126                                 {
127                                         /* No, so add it to all_child_pathkeys */
128                                         all_child_pathkeys = lappend(all_child_pathkeys,
129                                                                                                  childkeys);
130                                 }
131                         }
132
133                         /* Unparameterized paths don't contribute to param-set list */
134                         if (childouter)
135                         {
136                                 ListCell   *lco;
137                                 bool            found = false;
138
139                                 /* Have we already seen this param set? */
140                                 foreach(lco, all_child_outers)
141                                 {
142                                         Relids          existing_outers = (Relids) lfirst(lco);
143
144                                         if (bms_equal(existing_outers, childouter))
145                                         {
146                                                 found = true;
147                                                 break;
148                                         }
149                                 }
150                                 if (!found)
151                                 {
152                                         /* No, so add it to all_child_outers */
153                                         all_child_outers = lappend(all_child_outers,
154                                                                                            childouter);
155                                 }
156                         }
157                 }
158         }
159
160         /*
161          * If we found unparameterized paths for all children, build an unordered,
162          * unparameterized Append path for the rel.  (Note: this is correct even
163          * if we have zero or one live subpath due to constraint exclusion.)
164          */
165         if (subpaths_valid)
166                 add_path(rel, (Path *) create_append_path(rel, subpaths, NULL));
167
168         /*
169          * Also build unparameterized MergeAppend paths based on the collected
170          * list of child pathkeys.
171          */
172         if (subpaths_valid)
173                 generate_mergeappend_paths(root, rel, live_childrels,
174                                                                    all_child_pathkeys);
175
176         /*
177          * Build Append paths for each parameterization seen among the child rels.
178          * (This may look pretty expensive, but in most cases of practical
179          * interest, the child rels will expose mostly the same parameterizations,
180          * so that not that many cases actually get considered here.)
181          *
182          * The Append node itself cannot enforce quals, so all qual checking must
183          * be done in the child paths.  This means that to have a parameterized
184          * Append path, we must have the exact same parameterization for each
185          * child path; otherwise some children might be failing to check the
186          * moved-down quals.  To make them match up, we can try to increase the
187          * parameterization of lesser-parameterized paths.
188          */
189         foreach(l, all_child_outers)
190         {
191                 Relids          required_outer = (Relids) lfirst(l);
192                 ListCell   *lcr;
193
194                 /* Select the child paths for an Append with this parameterization */
195                 subpaths = NIL;
196                 subpaths_valid = true;
197                 foreach(lcr, live_childrels)
198                 {
199                         RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
200                         Path       *subpath;
201
202                         subpath = get_cheapest_parameterized_child_path(root,
203                                                                                                                         childrel,
204                                                                                                                         required_outer);
205                         if (subpath == NULL)
206                         {
207                                 /* failed to make a suitable path for this child */
208                                 subpaths_valid = false;
209                                 break;
210                         }
211                         subpaths = accumulate_append_subpath(subpaths, subpath);
212                 }
213
214                 if (subpaths_valid)
215                         add_path(rel, (Path *)
216                                          create_append_path(rel, subpaths, required_outer));
217         }
218
219         /* Select cheapest paths */
220         set_cheapest(rel);
221 }
222
223 /*
224  * generate_mergeappend_paths
225  *              Generate MergeAppend paths for an append relation
226  *
227  * Generate a path for each ordering (pathkey list) appearing in
228  * all_child_pathkeys.
229  *
230  * We consider both cheapest-startup and cheapest-total cases, ie, for each
231  * interesting ordering, collect all the cheapest startup subpaths and all the
232  * cheapest total paths, and build a MergeAppend path for each case.
233  *
234  * We don't currently generate any parameterized MergeAppend paths.  While
235  * it would not take much more code here to do so, it's very unclear that it
236  * is worth the planning cycles to investigate such paths: there's little
237  * use for an ordered path on the inside of a nestloop.  In fact, it's likely
238  * that the current coding of add_path would reject such paths out of hand,
239  * because add_path gives no credit for sort ordering of parameterized paths,
240  * and a parameterized MergeAppend is going to be more expensive than the
241  * corresponding parameterized Append path.  If we ever try harder to support
242  * parameterized mergejoin plans, it might be worth adding support for
243  * parameterized MergeAppends to feed such joins.  (See notes in
244  * optimizer/README for why that might not ever happen, though.)
245  */
246 static void
247 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
248                                                    List *live_childrels,
249                                                    List *all_child_pathkeys)
250 {
251         ListCell   *lcp;
252
253         foreach(lcp, all_child_pathkeys)
254         {
255                 List       *pathkeys = (List *) lfirst(lcp);
256                 List       *startup_subpaths = NIL;
257                 List       *total_subpaths = NIL;
258                 bool            startup_neq_total = false;
259                 ListCell   *lcr;
260
261                 /* Select the child paths for this ordering... */
262                 foreach(lcr, live_childrels)
263                 {
264                         RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
265                         Path       *cheapest_startup,
266                                            *cheapest_total;
267
268                         /* Locate the right paths, if they are available. */
269                         cheapest_startup =
270                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
271                                                                                            pathkeys,
272                                                                                            NULL,
273                                                                                            STARTUP_COST);
274                         cheapest_total =
275                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
276                                                                                            pathkeys,
277                                                                                            NULL,
278                                                                                            TOTAL_COST);
279
280                         /*
281                          * If we can't find any paths with the right order just use the
282                          * cheapest-total path; we'll have to sort it later.
283                          */
284                         if (cheapest_startup == NULL || cheapest_total == NULL)
285                         {
286                                 cheapest_startup = cheapest_total =
287                                         childrel->cheapest_total_path;
288                                 /* Assert we do have an unparameterized path for this child */
289                                 Assert(cheapest_total->param_info == NULL);
290                         }
291
292                         /*
293                          * Notice whether we actually have different paths for the
294                          * "cheapest" and "total" cases; frequently there will be no point
295                          * in two create_merge_append_path() calls.
296                          */
297                         if (cheapest_startup != cheapest_total)
298                                 startup_neq_total = true;
299
300                         startup_subpaths =
301                                 accumulate_append_subpath(startup_subpaths, cheapest_startup);
302                         total_subpaths =
303                                 accumulate_append_subpath(total_subpaths, cheapest_total);
304                 }
305
306                 /* ... and build the MergeAppend paths */
307                 add_path(rel, (Path *) create_merge_append_path(root,
308                                                                                                                 rel,
309                                                                                                                 startup_subpaths,
310                                                                                                                 pathkeys,
311                                                                                                                 NULL));
312                 if (startup_neq_total)
313                         add_path(rel, (Path *) create_merge_append_path(root,
314                                                                                                                         rel,
315                                                                                                                         total_subpaths,
316                                                                                                                         pathkeys,
317                                                                                                                         NULL));
318         }
319 }
320
321 /*
322  * get_cheapest_parameterized_child_path
323  *              Get cheapest path for this relation that has exactly the requested
324  *              parameterization.
325  *
326  * Returns NULL if unable to create such a path.
327  */
328 static Path *
329 get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
330                                                                           Relids required_outer)
331 {
332         Path       *cheapest;
333         ListCell   *lc;
334
335         /*
336          * Look up the cheapest existing path with no more than the needed
337          * parameterization.  If it has exactly the needed parameterization, we're
338          * done.
339          */
340         cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
341                                                                                           NIL,
342                                                                                           required_outer,
343                                                                                           TOTAL_COST);
344         Assert(cheapest != NULL);
345         if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
346                 return cheapest;
347
348         /*
349          * Otherwise, we can "reparameterize" an existing path to match the given
350          * parameterization, which effectively means pushing down additional
351          * joinquals to be checked within the path's scan.  However, some existing
352          * paths might check the available joinquals already while others don't;
353          * therefore, it's not clear which existing path will be cheapest after
354          * reparameterization.  We have to go through them all and find out.
355          */
356         cheapest = NULL;
357         foreach(lc, rel->pathlist)
358         {
359                 Path       *path = (Path *) lfirst(lc);
360
361                 /* Can't use it if it needs more than requested parameterization */
362                 if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
363                         continue;
364
365                 /*
366                  * Reparameterization can only increase the path's cost, so if it's
367                  * already more expensive than the current cheapest, forget it.
368                  */
369                 if (cheapest != NULL &&
370                         compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
371                         continue;
372
373                 /* Reparameterize if needed, then recheck cost */
374                 if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
375                 {
376                         path = reparameterize_path(root, path, required_outer, 1.0);
377                         if (path == NULL)
378                                 continue;               /* failed to reparameterize this one */
379                         Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
380
381                         if (cheapest != NULL &&
382                                 compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
383                                 continue;
384                 }
385
386                 /* We have a new best path */
387                 cheapest = path;
388         }
389
390         /* Return the best path, or NULL if we found no suitable candidate */
391         return cheapest;
392 }
393
394 /*
395  * accumulate_append_subpath
396  *              Add a subpath to the list being built for an Append or MergeAppend
397  *
398  * It's possible that the child is itself an Append path, in which case
399  * we can "cut out the middleman" and just add its child paths to our
400  * own list.  (We don't try to do this earlier because we need to
401  * apply both levels of transformation to the quals.)
402  */
403 static List *
404 accumulate_append_subpath(List *subpaths, Path *path)
405 {
406         if (IsA(path, AppendPath))
407         {
408                 AppendPath *apath = (AppendPath *) path;
409
410                 /* list_copy is important here to avoid sharing list substructure */
411                 return list_concat(subpaths, list_copy(apath->subpaths));
412         }
413         else
414                 return lappend(subpaths, path);
415 }
416
417 /*
418  * standard_join_search
419  *        Find possible joinpaths for a query by successively finding ways
420  *        to join component relations into join relations.
421  *
422  * 'levels_needed' is the number of iterations needed, ie, the number of
423  *              independent jointree items in the query.  This is > 1.
424  *
425  * 'initial_rels' is a list of RelOptInfo nodes for each independent
426  *              jointree item.  These are the components to be joined together.
427  *              Note that levels_needed == list_length(initial_rels).
428  *
429  * Returns the final level of join relations, i.e., the relation that is
430  * the result of joining all the original relations together.
431  * At least one implementation path must be provided for this relation and
432  * all required sub-relations.
433  *
434  * To support loadable plugins that modify planner behavior by changing the
435  * join searching algorithm, we provide a hook variable that lets a plugin
436  * replace or supplement this function.  Any such hook must return the same
437  * final join relation as the standard code would, but it might have a
438  * different set of implementation paths attached, and only the sub-joinrels
439  * needed for these paths need have been instantiated.
440  *
441  * Note to plugin authors: the functions invoked during standard_join_search()
442  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
443  * than one join-order search, you'll probably need to save and restore the
444  * original states of those data structures.  See geqo_eval() for an example.
445  */
446 RelOptInfo *
447 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
448 {
449         int                     lev;
450         RelOptInfo *rel;
451
452         /*
453          * This function cannot be invoked recursively within any one planning
454          * problem, so join_rel_level[] can't be in use already.
455          */
456         Assert(root->join_rel_level == NULL);
457
458         /*
459          * We employ a simple "dynamic programming" algorithm: we first find all
460          * ways to build joins of two jointree items, then all ways to build joins
461          * of three items (from two-item joins and single items), then four-item
462          * joins, and so on until we have considered all ways to join all the
463          * items into one rel.
464          *
465          * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
466          * set root->join_rel_level[1] to represent all the single-jointree-item
467          * relations.
468          */
469         root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
470
471         root->join_rel_level[1] = initial_rels;
472
473         for (lev = 2; lev <= levels_needed; lev++)
474         {
475                 ListCell   *lc;
476
477                 /*
478                  * Determine all possible pairs of relations to be joined at this
479                  * level, and build paths for making each one from every available
480                  * pair of lower-level relations.
481                  */
482                 join_search_one_level(root, lev);
483
484                 /*
485                  * Do cleanup work on each just-processed rel.
486                  */
487                 foreach(lc, root->join_rel_level[lev])
488                 {
489                         rel = (RelOptInfo *) lfirst(lc);
490
491                         /* Find and save the cheapest paths for this rel */
492                         set_cheapest(rel);
493
494 #ifdef OPTIMIZER_DEBUG
495                         debug_print_rel(root, rel);
496 #endif
497                 }
498         }
499
500         /*
501          * We should have a single rel at the final level.
502          */
503         if (root->join_rel_level[levels_needed] == NIL)
504                 elog(ERROR, "failed to build any %d-way joins", levels_needed);
505         Assert(list_length(root->join_rel_level[levels_needed]) == 1);
506
507         rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
508
509         root->join_rel_level = NULL;
510
511         return rel;
512 }
513
514 /*
515  * join_search_one_level
516  *        Consider ways to produce join relations containing exactly 'level'
517  *        jointree items.  (This is one step of the dynamic-programming method
518  *        embodied in standard_join_search.)  Join rel nodes for each feasible
519  *        combination of lower-level rels are created and returned in a list.
520  *        Implementation paths are created for each such joinrel, too.
521  *
522  * level: level of rels we want to make this time
523  * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
524  *
525  * The result is returned in root->join_rel_level[level].
526  */
527 void
528 join_search_one_level(PlannerInfo *root, int level)
529 {
530         List      **joinrels = root->join_rel_level;
531         ListCell   *r;
532         int                     k;
533
534         Assert(joinrels[level] == NIL);
535
536         /* Set join_cur_level so that new joinrels are added to proper list */
537         root->join_cur_level = level;
538
539         /*
540          * First, consider left-sided and right-sided plans, in which rels of
541          * exactly level-1 member relations are joined against initial relations.
542          * We prefer to join using join clauses, but if we find a rel of level-1
543          * members that has no join clauses, we will generate Cartesian-product
544          * joins against all initial rels not already contained in it.
545          */
546         foreach(r, joinrels[level - 1])
547         {
548                 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
549
550                 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
551                         has_join_restriction(root, old_rel))
552                 {
553                         /*
554                          * There are join clauses or join order restrictions relevant to
555                          * this rel, so consider joins between this rel and (only) those
556                          * initial rels it is linked to by a clause or restriction.
557                          *
558                          * At level 2 this condition is symmetric, so there is no need to
559                          * look at initial rels before this one in the list; we already
560                          * considered such joins when we were at the earlier rel.  (The
561                          * mirror-image joins are handled automatically by make_join_rel.)
562                          * In later passes (level > 2), we join rels of the previous level
563                          * to each initial rel they don't already include but have a join
564                          * clause or restriction with.
565                          */
566                         ListCell   *other_rels;
567
568                         if (level == 2)         /* consider remaining initial rels */
569                                 other_rels = lnext(r);
570                         else    /* consider all initial rels */
571                                 other_rels = list_head(joinrels[1]);
572
573                         make_rels_by_clause_joins(root,
574                                                                           old_rel,
575                                                                           other_rels);
576                 }
577                 else
578                 {
579                         /*
580                          * Oops, we have a relation that is not joined to any other
581                          * relation, either directly or by join-order restrictions.
582                          * Cartesian product time.
583                          *
584                          * We consider a cartesian product with each not-already-included
585                          * initial rel, whether it has other join clauses or not.  At
586                          * level 2, if there are two or more clauseless initial rels, we
587                          * will redundantly consider joining them in both directions; but
588                          * such cases aren't common enough to justify adding complexity to
589                          * avoid the duplicated effort.
590                          */
591                         make_rels_by_clauseless_joins(root,
592                                                                                   old_rel,
593                                                                                   list_head(joinrels[1]));
594                 }
595         }
596
597         /*
598          * Now, consider "bushy plans" in which relations of k initial rels are
599          * joined to relations of level-k initial rels, for 2 <= k <= level-2.
600          *
601          * We only consider bushy-plan joins for pairs of rels where there is a
602          * suitable join clause (or join order restriction), in order to avoid
603          * unreasonable growth of planning time.
604          */
605         for (k = 2;; k++)
606         {
607                 int                     other_level = level - k;
608
609                 /*
610                  * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
611                  * need to go as far as the halfway point.
612                  */
613                 if (k > other_level)
614                         break;
615
616                 foreach(r, joinrels[k])
617                 {
618                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
619                         ListCell   *other_rels;
620                         ListCell   *r2;
621
622                         /*
623                          * We can ignore relations without join clauses here, unless they
624                          * participate in join-order restrictions --- then we might have
625                          * to force a bushy join plan.
626                          */
627                         if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
628                                 !has_join_restriction(root, old_rel))
629                                 continue;
630
631                         if (k == other_level)
632                                 other_rels = lnext(r);  /* only consider remaining rels */
633                         else
634                                 other_rels = list_head(joinrels[other_level]);
635
636                         for_each_cell(r2, other_rels)
637                         {
638                                 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
639
640                                 if (!bms_overlap(old_rel->relids, new_rel->relids))
641                                 {
642                                         /*
643                                          * OK, we can build a rel of the right level from this
644                                          * pair of rels.  Do so if there is at least one relevant
645                                          * join clause or join order restriction.
646                                          */
647                                         if (have_relevant_joinclause(root, old_rel, new_rel) ||
648                                                 have_join_order_restriction(root, old_rel, new_rel))
649                                         {
650                                                 (void) make_join_rel(root, old_rel, new_rel);
651                                         }
652                                 }
653                         }
654                 }
655         }
656
657         /*----------
658          * Last-ditch effort: if we failed to find any usable joins so far, force
659          * a set of cartesian-product joins to be generated.  This handles the
660          * special case where all the available rels have join clauses but we
661          * cannot use any of those clauses yet.  This can only happen when we are
662          * considering a join sub-problem (a sub-joinlist) and all the rels in the
663          * sub-problem have only join clauses with rels outside the sub-problem.
664          * An example is
665          *
666          *              SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
667          *              WHERE a.w = c.x and b.y = d.z;
668          *
669          * If the "a INNER JOIN b" sub-problem does not get flattened into the
670          * upper level, we must be willing to make a cartesian join of a and b;
671          * but the code above will not have done so, because it thought that both
672          * a and b have joinclauses.  We consider only left-sided and right-sided
673          * cartesian joins in this case (no bushy).
674          *----------
675          */
676         if (joinrels[level] == NIL)
677         {
678                 /*
679                  * This loop is just like the first one, except we always call
680                  * make_rels_by_clauseless_joins().
681                  */
682                 foreach(r, joinrels[level - 1])
683                 {
684                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
685
686                         make_rels_by_clauseless_joins(root,
687                                                                                   old_rel,
688                                                                                   list_head(joinrels[1]));
689                 }
690
691                 /*----------
692                  * When special joins are involved, there may be no legal way
693                  * to make an N-way join for some values of N.  For example consider
694                  *
695                  * SELECT ... FROM t1 WHERE
696                  *       x IN (SELECT ... FROM t2,t3 WHERE ...) AND
697                  *       y IN (SELECT ... FROM t4,t5 WHERE ...)
698                  *
699                  * We will flatten this query to a 5-way join problem, but there are
700                  * no 4-way joins that join_is_legal() will consider legal.  We have
701                  * to accept failure at level 4 and go on to discover a workable
702                  * bushy plan at level 5.
703                  *
704                  * However, if there are no special joins and no lateral references
705                  * then join_is_legal() should never fail, and so the following sanity
706                  * check is useful.
707                  *----------
708                  */
709                 if (joinrels[level] == NIL &&
710                         root->join_info_list == NIL &&
711                         root->lateral_info_list == NIL)
712                         elog(ERROR, "failed to build any %d-way joins", level);
713         }
714 }
715
716 /*
717  * make_rels_by_clause_joins
718  *        Build joins between the given relation 'old_rel' and other relations
719  *        that participate in join clauses that 'old_rel' also participates in
720  *        (or participate in join-order restrictions with it).
721  *        The join rels are returned in root->join_rel_level[join_cur_level].
722  *
723  * Note: at levels above 2 we will generate the same joined relation in
724  * multiple ways --- for example (a join b) join c is the same RelOptInfo as
725  * (b join c) join a, though the second case will add a different set of Paths
726  * to it.  This is the reason for using the join_rel_level mechanism, which
727  * automatically ensures that each new joinrel is only added to the list once.
728  *
729  * 'old_rel' is the relation entry for the relation to be joined
730  * 'other_rels': the first cell in a linked list containing the other
731  * rels to be considered for joining
732  *
733  * Currently, this is only used with initial rels in other_rels, but it
734  * will work for joining to joinrels too.
735  */
736 static void
737 make_rels_by_clause_joins(PlannerInfo *root,
738                                                   RelOptInfo *old_rel,
739                                                   ListCell *other_rels)
740 {
741         ListCell   *l;
742
743         for_each_cell(l, other_rels)
744         {
745                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
746
747                 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
748                         (have_relevant_joinclause(root, old_rel, other_rel) ||
749                          have_join_order_restriction(root, old_rel, other_rel)))
750                 {
751                         (void) make_join_rel(root, old_rel, other_rel);
752                 }
753         }
754 }
755
756 /*
757  * make_rels_by_clauseless_joins
758  *        Given a relation 'old_rel' and a list of other relations
759  *        'other_rels', create a join relation between 'old_rel' and each
760  *        member of 'other_rels' that isn't already included in 'old_rel'.
761  *        The join rels are returned in root->join_rel_level[join_cur_level].
762  *
763  * 'old_rel' is the relation entry for the relation to be joined
764  * 'other_rels': the first cell of a linked list containing the
765  * other rels to be considered for joining
766  *
767  * Currently, this is only used with initial rels in other_rels, but it would
768  * work for joining to joinrels too.
769  */
770 static void
771 make_rels_by_clauseless_joins(PlannerInfo *root,
772                                                           RelOptInfo *old_rel,
773                                                           ListCell *other_rels)
774 {
775         ListCell   *l;
776
777         for_each_cell(l, other_rels)
778         {
779                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
780
781                 if (!bms_overlap(other_rel->relids, old_rel->relids))
782                 {
783                         (void) make_join_rel(root, old_rel, other_rel);
784                 }
785         }
786 }
787
788 /*
789  * join_is_legal
790  *         Determine whether a proposed join is legal given the query's
791  *         join order constraints; and if it is, determine the join type.
792  *
793  * Caller must supply not only the two rels, but the union of their relids.
794  * (We could simplify the API by computing joinrelids locally, but this
795  * would be redundant work in the normal path through make_join_rel.)
796  *
797  * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
798  * else it's set to point to the associated SpecialJoinInfo node.  Also,
799  * *reversed_p is set TRUE if the given relations need to be swapped to
800  * match the SpecialJoinInfo node.
801  */
802 static bool
803 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
804                           Relids joinrelids,
805                           SpecialJoinInfo **sjinfo_p, bool *reversed_p)
806 {
807         SpecialJoinInfo *match_sjinfo;
808         bool            reversed;
809         bool            unique_ified;
810         bool            is_valid_inner;
811         bool            lateral_fwd;
812         bool            lateral_rev;
813         ListCell   *l;
814
815         /*
816          * Ensure output params are set on failure return.      This is just to
817          * suppress uninitialized-variable warnings from overly anal compilers.
818          */
819         *sjinfo_p = NULL;
820         *reversed_p = false;
821
822         /*
823          * If we have any special joins, the proposed join might be illegal; and
824          * in any case we have to determine its join type.      Scan the join info
825          * list for conflicts.
826          */
827         match_sjinfo = NULL;
828         reversed = false;
829         unique_ified = false;
830         is_valid_inner = true;
831
832         foreach(l, root->join_info_list)
833         {
834                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
835
836                 /*
837                  * This special join is not relevant unless its RHS overlaps the
838                  * proposed join.  (Check this first as a fast path for dismissing
839                  * most irrelevant SJs quickly.)
840                  */
841                 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
842                         continue;
843
844                 /*
845                  * Also, not relevant if proposed join is fully contained within RHS
846                  * (ie, we're still building up the RHS).
847                  */
848                 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
849                         continue;
850
851                 /*
852                  * Also, not relevant if SJ is already done within either input.
853                  */
854                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
855                         bms_is_subset(sjinfo->min_righthand, rel1->relids))
856                         continue;
857                 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
858                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
859                         continue;
860
861                 /*
862                  * If it's a semijoin and we already joined the RHS to any other rels
863                  * within either input, then we must have unique-ified the RHS at that
864                  * point (see below).  Therefore the semijoin is no longer relevant in
865                  * this join path.
866                  */
867                 if (sjinfo->jointype == JOIN_SEMI)
868                 {
869                         if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
870                                 !bms_equal(sjinfo->syn_righthand, rel1->relids))
871                                 continue;
872                         if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
873                                 !bms_equal(sjinfo->syn_righthand, rel2->relids))
874                                 continue;
875                 }
876
877                 /*
878                  * If one input contains min_lefthand and the other contains
879                  * min_righthand, then we can perform the SJ at this join.
880                  *
881                  * Barf if we get matches to more than one SJ (is that possible?)
882                  */
883                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
884                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
885                 {
886                         if (match_sjinfo)
887                                 return false;   /* invalid join path */
888                         match_sjinfo = sjinfo;
889                         reversed = false;
890                 }
891                 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
892                                  bms_is_subset(sjinfo->min_righthand, rel1->relids))
893                 {
894                         if (match_sjinfo)
895                                 return false;   /* invalid join path */
896                         match_sjinfo = sjinfo;
897                         reversed = true;
898                 }
899                 else if (sjinfo->jointype == JOIN_SEMI &&
900                                  bms_equal(sjinfo->syn_righthand, rel2->relids) &&
901                                  create_unique_path(root, rel2, rel2->cheapest_total_path,
902                                                                         sjinfo) != NULL)
903                 {
904                         /*----------
905                          * For a semijoin, we can join the RHS to anything else by
906                          * unique-ifying the RHS (if the RHS can be unique-ified).
907                          * We will only get here if we have the full RHS but less
908                          * than min_lefthand on the LHS.
909                          *
910                          * The reason to consider such a join path is exemplified by
911                          *      SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
912                          * If we insist on doing this as a semijoin we will first have
913                          * to form the cartesian product of A*B.  But if we unique-ify
914                          * C then the semijoin becomes a plain innerjoin and we can join
915                          * in any order, eg C to A and then to B.  When C is much smaller
916                          * than A and B this can be a huge win.  So we allow C to be
917                          * joined to just A or just B here, and then make_join_rel has
918                          * to handle the case properly.
919                          *
920                          * Note that actually we'll allow unique-ified C to be joined to
921                          * some other relation D here, too.  That is legal, if usually not
922                          * very sane, and this routine is only concerned with legality not
923                          * with whether the join is good strategy.
924                          *----------
925                          */
926                         if (match_sjinfo)
927                                 return false;   /* invalid join path */
928                         match_sjinfo = sjinfo;
929                         reversed = false;
930                         unique_ified = true;
931                 }
932                 else if (sjinfo->jointype == JOIN_SEMI &&
933                                  bms_equal(sjinfo->syn_righthand, rel1->relids) &&
934                                  create_unique_path(root, rel1, rel1->cheapest_total_path,
935                                                                         sjinfo) != NULL)
936                 {
937                         /* Reversed semijoin case */
938                         if (match_sjinfo)
939                                 return false;   /* invalid join path */
940                         match_sjinfo = sjinfo;
941                         reversed = true;
942                         unique_ified = true;
943                 }
944                 else
945                 {
946                         /*----------
947                          * Otherwise, the proposed join overlaps the RHS but isn't
948                          * a valid implementation of this SJ.  It might still be
949                          * a legal join, however.  If both inputs overlap the RHS,
950                          * assume that it's OK.  Since the inputs presumably got past
951                          * this function's checks previously, they can't overlap the
952                          * LHS and their violations of the RHS boundary must represent
953                          * SJs that have been determined to commute with this one.
954                          * We have to allow this to work correctly in cases like
955                          *              (a LEFT JOIN (b JOIN (c LEFT JOIN d)))
956                          * when the c/d join has been determined to commute with the join
957                          * to a, and hence d is not part of min_righthand for the upper
958                          * join.  It should be legal to join b to c/d but this will appear
959                          * as a violation of the upper join's RHS.
960                          * Furthermore, if one input overlaps the RHS and the other does
961                          * not, we should still allow the join if it is a valid
962                          * implementation of some other SJ.  We have to allow this to
963                          * support the associative identity
964                          *              (a LJ b on Pab) LJ c ON Pbc = a LJ (b LJ c ON Pbc) on Pab
965                          * since joining B directly to C violates the lower SJ's RHS.
966                          * We assume that make_outerjoininfo() set things up correctly
967                          * so that we'll only match to some SJ if the join is valid.
968                          * Set flag here to check at bottom of loop.
969                          *----------
970                          */
971                         if (sjinfo->jointype != JOIN_SEMI &&
972                                 bms_overlap(rel1->relids, sjinfo->min_righthand) &&
973                                 bms_overlap(rel2->relids, sjinfo->min_righthand))
974                         {
975                                 /* seems OK */
976                                 Assert(!bms_overlap(joinrelids, sjinfo->min_lefthand));
977                         }
978                         else
979                                 is_valid_inner = false;
980                 }
981         }
982
983         /*
984          * Fail if violated some SJ's RHS and didn't match to another SJ. However,
985          * "matching" to a semijoin we are implementing by unique-ification
986          * doesn't count (think: it's really an inner join).
987          */
988         if (!is_valid_inner &&
989                 (match_sjinfo == NULL || unique_ified))
990                 return false;                   /* invalid join path */
991
992         /*
993          * We also have to check for constraints imposed by LATERAL references.
994          * The proposed rels could each contain lateral references to the other,
995          * in which case the join is impossible.  If there are lateral references
996          * in just one direction, then the join has to be done with a nestloop
997          * with the lateral referencer on the inside.  If the join matches an SJ
998          * that cannot be implemented by such a nestloop, the join is impossible.
999          */
1000         lateral_fwd = lateral_rev = false;
1001         foreach(l, root->lateral_info_list)
1002         {
1003                 LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
1004
1005                 if (bms_is_subset(ljinfo->lateral_rhs, rel2->relids) &&
1006                         bms_overlap(ljinfo->lateral_lhs, rel1->relids))
1007                 {
1008                         /* has to be implemented as nestloop with rel1 on left */
1009                         if (lateral_rev)
1010                                 return false;   /* have lateral refs in both directions */
1011                         lateral_fwd = true;
1012                         if (!bms_is_subset(ljinfo->lateral_lhs, rel1->relids))
1013                                 return false;   /* rel1 can't compute the required parameter */
1014                         if (match_sjinfo &&
1015                                 (reversed || match_sjinfo->jointype == JOIN_FULL))
1016                                 return false;   /* not implementable as nestloop */
1017                 }
1018                 if (bms_is_subset(ljinfo->lateral_rhs, rel1->relids) &&
1019                         bms_overlap(ljinfo->lateral_lhs, rel2->relids))
1020                 {
1021                         /* has to be implemented as nestloop with rel2 on left */
1022                         if (lateral_fwd)
1023                                 return false;   /* have lateral refs in both directions */
1024                         lateral_rev = true;
1025                         if (!bms_is_subset(ljinfo->lateral_lhs, rel2->relids))
1026                                 return false;   /* rel2 can't compute the required parameter */
1027                         if (match_sjinfo &&
1028                                 (!reversed || match_sjinfo->jointype == JOIN_FULL))
1029                                 return false;   /* not implementable as nestloop */
1030                 }
1031         }
1032
1033         /* Otherwise, it's a valid join */
1034         *sjinfo_p = match_sjinfo;
1035         *reversed_p = reversed;
1036         return true;
1037 }
1038
1039 /*
1040  * has_join_restriction
1041  *              Detect whether the specified relation has join-order restrictions,
1042  *              due to being inside an outer join or an IN (sub-SELECT),
1043  *              or participating in any LATERAL references.
1044  *
1045  * Essentially, this tests whether have_join_order_restriction() could
1046  * succeed with this rel and some other one.  It's OK if we sometimes
1047  * say "true" incorrectly.      (Therefore, we don't bother with the relatively
1048  * expensive has_legal_joinclause test.)
1049  */
1050 static bool
1051 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
1052 {
1053         ListCell   *l;
1054
1055         foreach(l, root->lateral_info_list)
1056         {
1057                 LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
1058
1059                 if (bms_is_subset(ljinfo->lateral_rhs, rel->relids) ||
1060                         bms_overlap(ljinfo->lateral_lhs, rel->relids))
1061                         return true;
1062         }
1063
1064         foreach(l, root->join_info_list)
1065         {
1066                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1067
1068                 /* ignore full joins --- other mechanisms preserve their ordering */
1069                 if (sjinfo->jointype == JOIN_FULL)
1070                         continue;
1071
1072                 /* ignore if SJ is already contained in rel */
1073                 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1074                         bms_is_subset(sjinfo->min_righthand, rel->relids))
1075                         continue;
1076
1077                 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1078                 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1079                         bms_overlap(sjinfo->min_righthand, rel->relids))
1080                         return true;
1081         }
1082
1083         return false;
1084 }
1085
1086 /*
1087  * is_dummy_rel --- has relation been proven empty?
1088  */
1089 static bool
1090 is_dummy_rel(RelOptInfo *rel)
1091 {
1092         return IS_DUMMY_REL(rel);
1093 }
1094
1095 /*
1096  * Mark a relation as proven empty.
1097  *
1098  * During GEQO planning, this can get invoked more than once on the same
1099  * baserel struct, so it's worth checking to see if the rel is already marked
1100  * dummy.
1101  *
1102  * Also, when called during GEQO join planning, we are in a short-lived
1103  * memory context.      We must make sure that the dummy path attached to a
1104  * baserel survives the GEQO cycle, else the baserel is trashed for future
1105  * GEQO cycles.  On the other hand, when we are marking a joinrel during GEQO,
1106  * we don't want the dummy path to clutter the main planning context.  Upshot
1107  * is that the best solution is to explicitly make the dummy path in the same
1108  * context the given RelOptInfo is in.
1109  */
1110 static void
1111 mark_dummy_rel(RelOptInfo *rel)
1112 {
1113         MemoryContext oldcontext;
1114
1115         /* Already marked? */
1116         if (is_dummy_rel(rel))
1117                 return;
1118
1119         /* No, so choose correct context to make the dummy path in */
1120         oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
1121
1122         /* Set dummy size estimate */
1123         rel->rows = 0;
1124
1125         /* Evict any previously chosen paths */
1126         rel->pathlist = NIL;
1127
1128         /* Set up the dummy path */
1129         add_path(rel, (Path *) create_append_path(rel, NIL, NULL));
1130
1131         /* Set or update cheapest_total_path and related fields */
1132         set_cheapest(rel);
1133
1134         MemoryContextSwitchTo(oldcontext);
1135 }
1136
1137 /*
1138  * restriction_is_constant_false --- is a restrictlist just FALSE?
1139  *
1140  * In cases where a qual is provably constant FALSE, eval_const_expressions
1141  * will generally have thrown away anything that's ANDed with it.  In outer
1142  * join situations this will leave us computing cartesian products only to
1143  * decide there's no match for an outer row, which is pretty stupid.  So,
1144  * we need to detect the case.
1145  *
1146  * If only_pushed_down is TRUE, then consider only pushed-down quals.
1147  */
1148 static bool
1149 restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
1150 {
1151         ListCell   *lc;
1152
1153         /*
1154          * Despite the above comment, the restriction list we see here might
1155          * possibly have other members besides the FALSE constant, since other
1156          * quals could get "pushed down" to the outer join level.  So we check
1157          * each member of the list.
1158          */
1159         foreach(lc, restrictlist)
1160         {
1161                 RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
1162
1163                 Assert(IsA(rinfo, RestrictInfo));
1164                 if (only_pushed_down && !rinfo->is_pushed_down)
1165                         continue;
1166
1167                 if (rinfo->clause && IsA(rinfo->clause, Const))
1168                 {
1169                         Const      *con = (Const *) rinfo->clause;
1170
1171                         /* constant NULL is as good as constant FALSE for our purposes */
1172                         if (con->constisnull)
1173                                 return true;
1174                         if (!DatumGetBool(con->constvalue))
1175                                 return true;
1176                 }
1177         }
1178         return false;
1179 }