OSDN Git Service

Remove mark_dummy_rel() from core.c
[pghintplan/pg_hint_plan.git] / core.c
1 /*-------------------------------------------------------------------------
2  *
3  * core.c
4  *        Routines copied from PostgreSQL core distribution.
5  *
6  * The main purpose of this files is having access to static functions in core.
7  * Another purpose is tweaking functions behavior by replacing part of them by
8  * macro definitions. See at the end of pg_hint_plan.c for details. Anyway,
9  * this file *must* contain required functions without making any change.
10  *
11  * This file contains the following functions from corresponding files.
12  *
13  * src/backend/optimizer/path/allpaths.c
14  *
15  *      static functions:
16  *         set_plain_rel_pathlist()
17  *     set_append_rel_pathlist()
18  *     add_paths_to_append_rel()
19  *     generate_mergeappend_paths()
20  *     get_cheapest_parameterized_child_path()
21  *     accumulate_append_subpath()
22  *
23  *  public functions:
24  *     standard_join_search(): This funcion is not static. The reason for
25  *        including this function is make_rels_by_clause_joins. In order to
26  *        avoid generating apparently unwanted join combination, we decided to
27  *        change the behavior of make_join_rel, which is called under this
28  *        function.
29  *
30  * src/backend/optimizer/path/joinrels.c
31  *
32  *      public functions:
33  *     join_search_one_level(): We have to modify this to call my definition of
34  *                  make_rels_by_clause_joins.
35  *
36  *      static functions:
37  *     make_rels_by_clause_joins()
38  *     make_rels_by_clauseless_joins()
39  *     join_is_legal()
40  *     has_join_restriction()
41  *     is_dummy_rel()
42  *     restriction_is_constant_false()
43  *
44  *
45  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
46  * Portions Copyright (c) 1994, Regents of the University of California
47  *
48  *-------------------------------------------------------------------------
49  */
50
51
52 /*
53  * set_plain_rel_pathlist
54  *        Build access paths for a plain relation (no subquery, no inheritance)
55  */
56 static void
57 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
58 {
59         Relids          required_outer;
60
61         /*
62          * We don't support pushing join clauses into the quals of a seqscan, but
63          * it could still have required parameterization due to LATERAL refs in
64          * its tlist.
65          */
66         required_outer = rel->lateral_relids;
67
68         /* Consider sequential scan */
69         add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
70
71         /* If appropriate, consider parallel sequential scan */
72         if (rel->consider_parallel && required_outer == NULL)
73                 create_plain_partial_paths(root, rel);
74
75         /* Consider index scans */
76         create_index_paths(root, rel);
77
78         /* Consider TID scans */
79         create_tidscan_paths(root, rel);
80 }
81
82
83 /*
84  * set_append_rel_pathlist
85  *        Build access paths for an "append relation"
86  */
87 static void
88 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
89                                                 Index rti, RangeTblEntry *rte)
90 {
91         int                     parentRTindex = rti;
92         List       *live_childrels = NIL;
93         ListCell   *l;
94
95         /*
96          * Generate access paths for each member relation, and remember the
97          * non-dummy children.
98          */
99         foreach(l, root->append_rel_list)
100         {
101                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
102                 int                     childRTindex;
103                 RangeTblEntry *childRTE;
104                 RelOptInfo *childrel;
105
106                 /* append_rel_list contains all append rels; ignore others */
107                 if (appinfo->parent_relid != parentRTindex)
108                         continue;
109
110                 /* Re-locate the child RTE and RelOptInfo */
111                 childRTindex = appinfo->child_relid;
112                 childRTE = root->simple_rte_array[childRTindex];
113                 childrel = root->simple_rel_array[childRTindex];
114
115                 /*
116                  * If set_append_rel_size() decided the parent appendrel was
117                  * parallel-unsafe at some point after visiting this child rel, we
118                  * need to propagate the unsafety marking down to the child, so that
119                  * we don't generate useless partial paths for it.
120                  */
121                 if (!rel->consider_parallel)
122                         childrel->consider_parallel = false;
123
124                 /*
125                  * Compute the child's access paths.
126                  */
127                 set_rel_pathlist(root, childrel, childRTindex, childRTE);
128
129                 /*
130                  * If child is dummy, ignore it.
131                  */
132                 if (IS_DUMMY_REL(childrel))
133                         continue;
134
135                 /*
136                  * Child is live, so add it to the live_childrels list for use below.
137                  */
138                 live_childrels = lappend(live_childrels, childrel);
139         }
140
141         /* Add paths to the "append" relation. */
142         add_paths_to_append_rel(root, rel, live_childrels);
143 }
144
145 /*
146  * add_paths_to_append_rel
147  *              Generate paths for given "append" relation given the set of non-dummy
148  *              child rels.
149  *
150  * The function collects all parameterizations and orderings supported by the
151  * non-dummy children. For every such parameterization or ordering, it creates
152  * an append path collecting one path from each non-dummy child with given
153  * parameterization or ordering. Similarly it collects partial paths from
154  * non-dummy children to create partial append paths.
155  */
156 static void
157 add_paths_to_append_rel(PlannerInfo *root, RelOptInfo *rel,
158                                                 List *live_childrels)
159 {
160         List       *subpaths = NIL;
161         bool            subpaths_valid = true;
162         List       *partial_subpaths = NIL;
163         bool            partial_subpaths_valid = true;
164         List       *all_child_pathkeys = NIL;
165         List       *all_child_outers = NIL;
166         ListCell   *l;
167         List       *partitioned_rels = NIL;
168         RangeTblEntry *rte;
169         bool            build_partitioned_rels = false;
170
171         /*
172          * A plain relation will already have a PartitionedChildRelInfo if it is
173          * partitioned.  For a subquery RTE, no PartitionedChildRelInfo exists; we
174          * collect all partitioned_rels associated with any child.  (This assumes
175          * that we don't need to look through multiple levels of subquery RTEs; if
176          * we ever do, we could create a PartitionedChildRelInfo with the
177          * accumulated list of partitioned_rels which would then be found when
178          * populated our parent rel with paths.  For the present, that appears to
179          * be unnecessary.)
180          */
181         rte = planner_rt_fetch(rel->relid, root);
182         switch (rte->rtekind)
183         {
184                 case RTE_RELATION:
185                         if (rte->relkind == RELKIND_PARTITIONED_TABLE)
186                         {
187                                 partitioned_rels =
188                                         get_partitioned_child_rels(root, rel->relid);
189                                 Assert(list_length(partitioned_rels) >= 1);
190                         }
191                         break;
192                 case RTE_SUBQUERY:
193                         build_partitioned_rels = true;
194                         break;
195                 default:
196                         elog(ERROR, "unexpcted rtekind: %d", (int) rte->rtekind);
197         }
198
199         /*
200          * For every non-dummy child, remember the cheapest path.  Also, identify
201          * all pathkeys (orderings) and parameterizations (required_outer sets)
202          * available for the non-dummy member relations.
203          */
204         foreach(l, live_childrels)
205         {
206                 RelOptInfo *childrel = lfirst(l);
207                 ListCell   *lcp;
208
209                 /*
210                  * If we need to build partitioned_rels, accumulate the partitioned
211                  * rels for this child.
212                  */
213                 if (build_partitioned_rels)
214                 {
215                         List       *cprels;
216
217                         cprels = get_partitioned_child_rels(root, childrel->relid);
218                         partitioned_rels = list_concat(partitioned_rels,
219                                                                                    list_copy(cprels));
220                 }
221
222                 /*
223                  * If child has an unparameterized cheapest-total path, add that to
224                  * the unparameterized Append path we are constructing for the parent.
225                  * If not, there's no workable unparameterized path.
226                  */
227                 if (childrel->cheapest_total_path->param_info == NULL)
228                         subpaths = accumulate_append_subpath(subpaths,
229                                                                                                  childrel->cheapest_total_path);
230                 else
231                         subpaths_valid = false;
232
233                 /* Same idea, but for a partial plan. */
234                 if (childrel->partial_pathlist != NIL)
235                         partial_subpaths = accumulate_append_subpath(partial_subpaths,
236                                                                                                                  linitial(childrel->partial_pathlist));
237                 else
238                         partial_subpaths_valid = false;
239
240                 /*
241                  * Collect lists of all the available path orderings and
242                  * parameterizations for all the children.  We use these as a
243                  * heuristic to indicate which sort orderings and parameterizations we
244                  * should build Append and MergeAppend paths for.
245                  */
246                 foreach(lcp, childrel->pathlist)
247                 {
248                         Path       *childpath = (Path *) lfirst(lcp);
249                         List       *childkeys = childpath->pathkeys;
250                         Relids          childouter = PATH_REQ_OUTER(childpath);
251
252                         /* Unsorted paths don't contribute to pathkey list */
253                         if (childkeys != NIL)
254                         {
255                                 ListCell   *lpk;
256                                 bool            found = false;
257
258                                 /* Have we already seen this ordering? */
259                                 foreach(lpk, all_child_pathkeys)
260                                 {
261                                         List       *existing_pathkeys = (List *) lfirst(lpk);
262
263                                         if (compare_pathkeys(existing_pathkeys,
264                                                                                  childkeys) == PATHKEYS_EQUAL)
265                                         {
266                                                 found = true;
267                                                 break;
268                                         }
269                                 }
270                                 if (!found)
271                                 {
272                                         /* No, so add it to all_child_pathkeys */
273                                         all_child_pathkeys = lappend(all_child_pathkeys,
274                                                                                                  childkeys);
275                                 }
276                         }
277
278                         /* Unparameterized paths don't contribute to param-set list */
279                         if (childouter)
280                         {
281                                 ListCell   *lco;
282                                 bool            found = false;
283
284                                 /* Have we already seen this param set? */
285                                 foreach(lco, all_child_outers)
286                                 {
287                                         Relids          existing_outers = (Relids) lfirst(lco);
288
289                                         if (bms_equal(existing_outers, childouter))
290                                         {
291                                                 found = true;
292                                                 break;
293                                         }
294                                 }
295                                 if (!found)
296                                 {
297                                         /* No, so add it to all_child_outers */
298                                         all_child_outers = lappend(all_child_outers,
299                                                                                            childouter);
300                                 }
301                         }
302                 }
303         }
304
305         /*
306          * If we found unparameterized paths for all children, build an unordered,
307          * unparameterized Append path for the rel.  (Note: this is correct even
308          * if we have zero or one live subpath due to constraint exclusion.)
309          */
310         if (subpaths_valid)
311                 add_path(rel, (Path *) create_append_path(rel, subpaths, NULL, 0,
312                                                                                                   partitioned_rels));
313
314         /*
315          * Consider an append of partial unordered, unparameterized partial paths.
316          */
317         if (partial_subpaths_valid)
318         {
319                 AppendPath *appendpath;
320                 ListCell   *lc;
321                 int                     parallel_workers = 0;
322
323                 /*
324                  * Decide on the number of workers to request for this append path.
325                  * For now, we just use the maximum value from among the members.  It
326                  * might be useful to use a higher number if the Append node were
327                  * smart enough to spread out the workers, but it currently isn't.
328                  */
329                 foreach(lc, partial_subpaths)
330                 {
331                         Path       *path = lfirst(lc);
332
333                         parallel_workers = Max(parallel_workers, path->parallel_workers);
334                 }
335                 Assert(parallel_workers > 0);
336
337                 /* Generate a partial append path. */
338                 appendpath = create_append_path(rel, partial_subpaths, NULL,
339                                                                                 parallel_workers, partitioned_rels);
340                 add_partial_path(rel, (Path *) appendpath);
341         }
342
343         /*
344          * Also build unparameterized MergeAppend paths based on the collected
345          * list of child pathkeys.
346          */
347         if (subpaths_valid)
348                 generate_mergeappend_paths(root, rel, live_childrels,
349                                                                    all_child_pathkeys,
350                                                                    partitioned_rels);
351
352         /*
353          * Build Append paths for each parameterization seen among the child rels.
354          * (This may look pretty expensive, but in most cases of practical
355          * interest, the child rels will expose mostly the same parameterizations,
356          * so that not that many cases actually get considered here.)
357          *
358          * The Append node itself cannot enforce quals, so all qual checking must
359          * be done in the child paths.  This means that to have a parameterized
360          * Append path, we must have the exact same parameterization for each
361          * child path; otherwise some children might be failing to check the
362          * moved-down quals.  To make them match up, we can try to increase the
363          * parameterization of lesser-parameterized paths.
364          */
365         foreach(l, all_child_outers)
366         {
367                 Relids          required_outer = (Relids) lfirst(l);
368                 ListCell   *lcr;
369
370                 /* Select the child paths for an Append with this parameterization */
371                 subpaths = NIL;
372                 subpaths_valid = true;
373                 foreach(lcr, live_childrels)
374                 {
375                         RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
376                         Path       *subpath;
377
378                         subpath = get_cheapest_parameterized_child_path(root,
379                                                                                                                         childrel,
380                                                                                                                         required_outer);
381                         if (subpath == NULL)
382                         {
383                                 /* failed to make a suitable path for this child */
384                                 subpaths_valid = false;
385                                 break;
386                         }
387                         subpaths = accumulate_append_subpath(subpaths, subpath);
388                 }
389
390                 if (subpaths_valid)
391                         add_path(rel, (Path *)
392                                          create_append_path(rel, subpaths, required_outer, 0,
393                                                                                 partitioned_rels));
394         }
395 }
396
397
398 /*
399  * generate_mergeappend_paths
400  *              Generate MergeAppend paths for an append relation
401  *
402  * Generate a path for each ordering (pathkey list) appearing in
403  * all_child_pathkeys.
404  *
405  * We consider both cheapest-startup and cheapest-total cases, ie, for each
406  * interesting ordering, collect all the cheapest startup subpaths and all the
407  * cheapest total paths, and build a MergeAppend path for each case.
408  *
409  * We don't currently generate any parameterized MergeAppend paths.  While
410  * it would not take much more code here to do so, it's very unclear that it
411  * is worth the planning cycles to investigate such paths: there's little
412  * use for an ordered path on the inside of a nestloop.  In fact, it's likely
413  * that the current coding of add_path would reject such paths out of hand,
414  * because add_path gives no credit for sort ordering of parameterized paths,
415  * and a parameterized MergeAppend is going to be more expensive than the
416  * corresponding parameterized Append path.  If we ever try harder to support
417  * parameterized mergejoin plans, it might be worth adding support for
418  * parameterized MergeAppends to feed such joins.  (See notes in
419  * optimizer/README for why that might not ever happen, though.)
420  */
421 static void
422 generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
423                                                    List *live_childrels,
424                                                    List *all_child_pathkeys,
425                                                    List *partitioned_rels)
426 {
427         ListCell   *lcp;
428
429         foreach(lcp, all_child_pathkeys)
430         {
431                 List       *pathkeys = (List *) lfirst(lcp);
432                 List       *startup_subpaths = NIL;
433                 List       *total_subpaths = NIL;
434                 bool            startup_neq_total = false;
435                 ListCell   *lcr;
436
437                 /* Select the child paths for this ordering... */
438                 foreach(lcr, live_childrels)
439                 {
440                         RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
441                         Path       *cheapest_startup,
442                                            *cheapest_total;
443
444                         /* Locate the right paths, if they are available. */
445                         cheapest_startup =
446                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
447                                                                                            pathkeys,
448                                                                                            NULL,
449                                                                                            STARTUP_COST,
450                                                                                            false);
451                         cheapest_total =
452                                 get_cheapest_path_for_pathkeys(childrel->pathlist,
453                                                                                            pathkeys,
454                                                                                            NULL,
455                                                                                            TOTAL_COST,
456                                                                                            false);
457
458                         /*
459                          * If we can't find any paths with the right order just use the
460                          * cheapest-total path; we'll have to sort it later.
461                          */
462                         if (cheapest_startup == NULL || cheapest_total == NULL)
463                         {
464                                 cheapest_startup = cheapest_total =
465                                         childrel->cheapest_total_path;
466                                 /* Assert we do have an unparameterized path for this child */
467                                 Assert(cheapest_total->param_info == NULL);
468                         }
469
470                         /*
471                          * Notice whether we actually have different paths for the
472                          * "cheapest" and "total" cases; frequently there will be no point
473                          * in two create_merge_append_path() calls.
474                          */
475                         if (cheapest_startup != cheapest_total)
476                                 startup_neq_total = true;
477
478                         startup_subpaths =
479                                 accumulate_append_subpath(startup_subpaths, cheapest_startup);
480                         total_subpaths =
481                                 accumulate_append_subpath(total_subpaths, cheapest_total);
482                 }
483
484                 /* ... and build the MergeAppend paths */
485                 add_path(rel, (Path *) create_merge_append_path(root,
486                                                                                                                 rel,
487                                                                                                                 startup_subpaths,
488                                                                                                                 pathkeys,
489                                                                                                                 NULL,
490                                                                                                                 partitioned_rels));
491                 if (startup_neq_total)
492                         add_path(rel, (Path *) create_merge_append_path(root,
493                                                                                                                         rel,
494                                                                                                                         total_subpaths,
495                                                                                                                         pathkeys,
496                                                                                                                         NULL,
497                                                                                                                         partitioned_rels));
498         }
499 }
500
501
502 /*
503  * get_cheapest_parameterized_child_path
504  *              Get cheapest path for this relation that has exactly the requested
505  *              parameterization.
506  *
507  * Returns NULL if unable to create such a path.
508  */
509 static Path *
510 get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
511                                                                           Relids required_outer)
512 {
513         Path       *cheapest;
514         ListCell   *lc;
515
516         /*
517          * Look up the cheapest existing path with no more than the needed
518          * parameterization.  If it has exactly the needed parameterization, we're
519          * done.
520          */
521         cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
522                                                                                           NIL,
523                                                                                           required_outer,
524                                                                                           TOTAL_COST,
525                                                                                           false);
526         Assert(cheapest != NULL);
527         if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
528                 return cheapest;
529
530         /*
531          * Otherwise, we can "reparameterize" an existing path to match the given
532          * parameterization, which effectively means pushing down additional
533          * joinquals to be checked within the path's scan.  However, some existing
534          * paths might check the available joinquals already while others don't;
535          * therefore, it's not clear which existing path will be cheapest after
536          * reparameterization.  We have to go through them all and find out.
537          */
538         cheapest = NULL;
539         foreach(lc, rel->pathlist)
540         {
541                 Path       *path = (Path *) lfirst(lc);
542
543                 /* Can't use it if it needs more than requested parameterization */
544                 if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
545                         continue;
546
547                 /*
548                  * Reparameterization can only increase the path's cost, so if it's
549                  * already more expensive than the current cheapest, forget it.
550                  */
551                 if (cheapest != NULL &&
552                         compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
553                         continue;
554
555                 /* Reparameterize if needed, then recheck cost */
556                 if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
557                 {
558                         path = reparameterize_path(root, path, required_outer, 1.0);
559                         if (path == NULL)
560                                 continue;               /* failed to reparameterize this one */
561                         Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
562
563                         if (cheapest != NULL &&
564                                 compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
565                                 continue;
566                 }
567
568                 /* We have a new best path */
569                 cheapest = path;
570         }
571
572         /* Return the best path, or NULL if we found no suitable candidate */
573         return cheapest;
574 }
575
576
577 /*
578  * accumulate_append_subpath
579  *              Add a subpath to the list being built for an Append or MergeAppend
580  *
581  * It's possible that the child is itself an Append or MergeAppend path, in
582  * which case we can "cut out the middleman" and just add its child paths to
583  * our own list.  (We don't try to do this earlier because we need to apply
584  * both levels of transformation to the quals.)
585  *
586  * Note that if we omit a child MergeAppend in this way, we are effectively
587  * omitting a sort step, which seems fine: if the parent is to be an Append,
588  * its result would be unsorted anyway, while if the parent is to be a
589  * MergeAppend, there's no point in a separate sort on a child.
590  */
591 static List *
592 accumulate_append_subpath(List *subpaths, Path *path)
593 {
594         if (IsA(path, AppendPath))
595         {
596                 AppendPath *apath = (AppendPath *) path;
597
598                 /* list_copy is important here to avoid sharing list substructure */
599                 return list_concat(subpaths, list_copy(apath->subpaths));
600         }
601         else if (IsA(path, MergeAppendPath))
602         {
603                 MergeAppendPath *mpath = (MergeAppendPath *) path;
604
605                 /* list_copy is important here to avoid sharing list substructure */
606                 return list_concat(subpaths, list_copy(mpath->subpaths));
607         }
608         else
609                 return lappend(subpaths, path);
610 }
611
612
613 /*
614  * standard_join_search
615  *        Find possible joinpaths for a query by successively finding ways
616  *        to join component relations into join relations.
617  *
618  * 'levels_needed' is the number of iterations needed, ie, the number of
619  *              independent jointree items in the query.  This is > 1.
620  *
621  * 'initial_rels' is a list of RelOptInfo nodes for each independent
622  *              jointree item.  These are the components to be joined together.
623  *              Note that levels_needed == list_length(initial_rels).
624  *
625  * Returns the final level of join relations, i.e., the relation that is
626  * the result of joining all the original relations together.
627  * At least one implementation path must be provided for this relation and
628  * all required sub-relations.
629  *
630  * To support loadable plugins that modify planner behavior by changing the
631  * join searching algorithm, we provide a hook variable that lets a plugin
632  * replace or supplement this function.  Any such hook must return the same
633  * final join relation as the standard code would, but it might have a
634  * different set of implementation paths attached, and only the sub-joinrels
635  * needed for these paths need have been instantiated.
636  *
637  * Note to plugin authors: the functions invoked during standard_join_search()
638  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
639  * than one join-order search, you'll probably need to save and restore the
640  * original states of those data structures.  See geqo_eval() for an example.
641  */
642 RelOptInfo *
643 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
644 {
645         int                     lev;
646         RelOptInfo *rel;
647
648         /*
649          * This function cannot be invoked recursively within any one planning
650          * problem, so join_rel_level[] can't be in use already.
651          */
652         Assert(root->join_rel_level == NULL);
653
654         /*
655          * We employ a simple "dynamic programming" algorithm: we first find all
656          * ways to build joins of two jointree items, then all ways to build joins
657          * of three items (from two-item joins and single items), then four-item
658          * joins, and so on until we have considered all ways to join all the
659          * items into one rel.
660          *
661          * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
662          * set root->join_rel_level[1] to represent all the single-jointree-item
663          * relations.
664          */
665         root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
666
667         root->join_rel_level[1] = initial_rels;
668
669         for (lev = 2; lev <= levels_needed; lev++)
670         {
671                 ListCell   *lc;
672
673                 /*
674                  * Determine all possible pairs of relations to be joined at this
675                  * level, and build paths for making each one from every available
676                  * pair of lower-level relations.
677                  */
678                 join_search_one_level(root, lev);
679
680                 /*
681                  * Run generate_gather_paths() for each just-processed joinrel.  We
682                  * could not do this earlier because both regular and partial paths
683                  * can get added to a particular joinrel at multiple times within
684                  * join_search_one_level.  After that, we're done creating paths for
685                  * the joinrel, so run set_cheapest().
686                  */
687                 foreach(lc, root->join_rel_level[lev])
688                 {
689                         rel = (RelOptInfo *) lfirst(lc);
690
691                         /* Create GatherPaths for any useful partial paths for rel */
692                         generate_gather_paths(root, rel);
693
694                         /* Find and save the cheapest paths for this rel */
695                         set_cheapest(rel);
696
697 #ifdef OPTIMIZER_DEBUG
698                         debug_print_rel(root, rel);
699 #endif
700                 }
701         }
702
703         /*
704          * We should have a single rel at the final level.
705          */
706         if (root->join_rel_level[levels_needed] == NIL)
707                 elog(ERROR, "failed to build any %d-way joins", levels_needed);
708         Assert(list_length(root->join_rel_level[levels_needed]) == 1);
709
710         rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
711
712         root->join_rel_level = NULL;
713
714         return rel;
715 }
716
717 /*
718  * create_plain_partial_paths
719  *        Build partial access paths for parallel scan of a plain relation
720  */
721 static void
722 create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
723 {
724         int                     parallel_workers;
725
726         parallel_workers = compute_parallel_worker(rel, rel->pages, -1);
727
728         /* If any limit was set to zero, the user doesn't want a parallel scan. */
729         if (parallel_workers <= 0)
730                 return;
731
732         /* Add an unordered partial path based on a parallel sequential scan. */
733         add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
734 }
735
736
737 /*
738  * join_search_one_level
739  *        Consider ways to produce join relations containing exactly 'level'
740  *        jointree items.  (This is one step of the dynamic-programming method
741  *        embodied in standard_join_search.)  Join rel nodes for each feasible
742  *        combination of lower-level rels are created and returned in a list.
743  *        Implementation paths are created for each such joinrel, too.
744  *
745  * level: level of rels we want to make this time
746  * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
747  *
748  * The result is returned in root->join_rel_level[level].
749  */
750 void
751 join_search_one_level(PlannerInfo *root, int level)
752 {
753         List      **joinrels = root->join_rel_level;
754         ListCell   *r;
755         int                     k;
756
757         Assert(joinrels[level] == NIL);
758
759         /* Set join_cur_level so that new joinrels are added to proper list */
760         root->join_cur_level = level;
761
762         /*
763          * First, consider left-sided and right-sided plans, in which rels of
764          * exactly level-1 member relations are joined against initial relations.
765          * We prefer to join using join clauses, but if we find a rel of level-1
766          * members that has no join clauses, we will generate Cartesian-product
767          * joins against all initial rels not already contained in it.
768          */
769         foreach(r, joinrels[level - 1])
770         {
771                 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
772
773                 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
774                         has_join_restriction(root, old_rel))
775                 {
776                         /*
777                          * There are join clauses or join order restrictions relevant to
778                          * this rel, so consider joins between this rel and (only) those
779                          * initial rels it is linked to by a clause or restriction.
780                          *
781                          * At level 2 this condition is symmetric, so there is no need to
782                          * look at initial rels before this one in the list; we already
783                          * considered such joins when we were at the earlier rel.  (The
784                          * mirror-image joins are handled automatically by make_join_rel.)
785                          * In later passes (level > 2), we join rels of the previous level
786                          * to each initial rel they don't already include but have a join
787                          * clause or restriction with.
788                          */
789                         ListCell   *other_rels;
790
791                         if (level == 2)         /* consider remaining initial rels */
792                                 other_rels = lnext(r);
793                         else                            /* consider all initial rels */
794                                 other_rels = list_head(joinrels[1]);
795
796                         make_rels_by_clause_joins(root,
797                                                                           old_rel,
798                                                                           other_rels);
799                 }
800                 else
801                 {
802                         /*
803                          * Oops, we have a relation that is not joined to any other
804                          * relation, either directly or by join-order restrictions.
805                          * Cartesian product time.
806                          *
807                          * We consider a cartesian product with each not-already-included
808                          * initial rel, whether it has other join clauses or not.  At
809                          * level 2, if there are two or more clauseless initial rels, we
810                          * will redundantly consider joining them in both directions; but
811                          * such cases aren't common enough to justify adding complexity to
812                          * avoid the duplicated effort.
813                          */
814                         make_rels_by_clauseless_joins(root,
815                                                                                   old_rel,
816                                                                                   list_head(joinrels[1]));
817                 }
818         }
819
820         /*
821          * Now, consider "bushy plans" in which relations of k initial rels are
822          * joined to relations of level-k initial rels, for 2 <= k <= level-2.
823          *
824          * We only consider bushy-plan joins for pairs of rels where there is a
825          * suitable join clause (or join order restriction), in order to avoid
826          * unreasonable growth of planning time.
827          */
828         for (k = 2;; k++)
829         {
830                 int                     other_level = level - k;
831
832                 /*
833                  * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
834                  * need to go as far as the halfway point.
835                  */
836                 if (k > other_level)
837                         break;
838
839                 foreach(r, joinrels[k])
840                 {
841                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
842                         ListCell   *other_rels;
843                         ListCell   *r2;
844
845                         /*
846                          * We can ignore relations without join clauses here, unless they
847                          * participate in join-order restrictions --- then we might have
848                          * to force a bushy join plan.
849                          */
850                         if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
851                                 !has_join_restriction(root, old_rel))
852                                 continue;
853
854                         if (k == other_level)
855                                 other_rels = lnext(r);  /* only consider remaining rels */
856                         else
857                                 other_rels = list_head(joinrels[other_level]);
858
859                         for_each_cell(r2, other_rels)
860                         {
861                                 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
862
863                                 if (!bms_overlap(old_rel->relids, new_rel->relids))
864                                 {
865                                         /*
866                                          * OK, we can build a rel of the right level from this
867                                          * pair of rels.  Do so if there is at least one relevant
868                                          * join clause or join order restriction.
869                                          */
870                                         if (have_relevant_joinclause(root, old_rel, new_rel) ||
871                                                 have_join_order_restriction(root, old_rel, new_rel))
872                                         {
873                                                 (void) make_join_rel(root, old_rel, new_rel);
874                                         }
875                                 }
876                         }
877                 }
878         }
879
880         /*----------
881          * Last-ditch effort: if we failed to find any usable joins so far, force
882          * a set of cartesian-product joins to be generated.  This handles the
883          * special case where all the available rels have join clauses but we
884          * cannot use any of those clauses yet.  This can only happen when we are
885          * considering a join sub-problem (a sub-joinlist) and all the rels in the
886          * sub-problem have only join clauses with rels outside the sub-problem.
887          * An example is
888          *
889          *              SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
890          *              WHERE a.w = c.x and b.y = d.z;
891          *
892          * If the "a INNER JOIN b" sub-problem does not get flattened into the
893          * upper level, we must be willing to make a cartesian join of a and b;
894          * but the code above will not have done so, because it thought that both
895          * a and b have joinclauses.  We consider only left-sided and right-sided
896          * cartesian joins in this case (no bushy).
897          *----------
898          */
899         if (joinrels[level] == NIL)
900         {
901                 /*
902                  * This loop is just like the first one, except we always call
903                  * make_rels_by_clauseless_joins().
904                  */
905                 foreach(r, joinrels[level - 1])
906                 {
907                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
908
909                         make_rels_by_clauseless_joins(root,
910                                                                                   old_rel,
911                                                                                   list_head(joinrels[1]));
912                 }
913
914                 /*----------
915                  * When special joins are involved, there may be no legal way
916                  * to make an N-way join for some values of N.  For example consider
917                  *
918                  * SELECT ... FROM t1 WHERE
919                  *       x IN (SELECT ... FROM t2,t3 WHERE ...) AND
920                  *       y IN (SELECT ... FROM t4,t5 WHERE ...)
921                  *
922                  * We will flatten this query to a 5-way join problem, but there are
923                  * no 4-way joins that join_is_legal() will consider legal.  We have
924                  * to accept failure at level 4 and go on to discover a workable
925                  * bushy plan at level 5.
926                  *
927                  * However, if there are no special joins and no lateral references
928                  * then join_is_legal() should never fail, and so the following sanity
929                  * check is useful.
930                  *----------
931                  */
932                 if (joinrels[level] == NIL &&
933                         root->join_info_list == NIL &&
934                         !root->hasLateralRTEs)
935                         elog(ERROR, "failed to build any %d-way joins", level);
936         }
937 }
938
939
940 /*
941  * make_rels_by_clause_joins
942  *        Build joins between the given relation 'old_rel' and other relations
943  *        that participate in join clauses that 'old_rel' also participates in
944  *        (or participate in join-order restrictions with it).
945  *        The join rels are returned in root->join_rel_level[join_cur_level].
946  *
947  * Note: at levels above 2 we will generate the same joined relation in
948  * multiple ways --- for example (a join b) join c is the same RelOptInfo as
949  * (b join c) join a, though the second case will add a different set of Paths
950  * to it.  This is the reason for using the join_rel_level mechanism, which
951  * automatically ensures that each new joinrel is only added to the list once.
952  *
953  * 'old_rel' is the relation entry for the relation to be joined
954  * 'other_rels': the first cell in a linked list containing the other
955  * rels to be considered for joining
956  *
957  * Currently, this is only used with initial rels in other_rels, but it
958  * will work for joining to joinrels too.
959  */
960 static void
961 make_rels_by_clause_joins(PlannerInfo *root,
962                                                   RelOptInfo *old_rel,
963                                                   ListCell *other_rels)
964 {
965         ListCell   *l;
966
967         for_each_cell(l, other_rels)
968         {
969                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
970
971                 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
972                         (have_relevant_joinclause(root, old_rel, other_rel) ||
973                          have_join_order_restriction(root, old_rel, other_rel)))
974                 {
975                         (void) make_join_rel(root, old_rel, other_rel);
976                 }
977         }
978 }
979
980
981 /*
982  * make_rels_by_clauseless_joins
983  *        Given a relation 'old_rel' and a list of other relations
984  *        'other_rels', create a join relation between 'old_rel' and each
985  *        member of 'other_rels' that isn't already included in 'old_rel'.
986  *        The join rels are returned in root->join_rel_level[join_cur_level].
987  *
988  * 'old_rel' is the relation entry for the relation to be joined
989  * 'other_rels': the first cell of a linked list containing the
990  * other rels to be considered for joining
991  *
992  * Currently, this is only used with initial rels in other_rels, but it would
993  * work for joining to joinrels too.
994  */
995 static void
996 make_rels_by_clauseless_joins(PlannerInfo *root,
997                                                           RelOptInfo *old_rel,
998                                                           ListCell *other_rels)
999 {
1000         ListCell   *l;
1001
1002         for_each_cell(l, other_rels)
1003         {
1004                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
1005
1006                 if (!bms_overlap(other_rel->relids, old_rel->relids))
1007                 {
1008                         (void) make_join_rel(root, old_rel, other_rel);
1009                 }
1010         }
1011 }
1012
1013
1014 /*
1015  * join_is_legal
1016  *         Determine whether a proposed join is legal given the query's
1017  *         join order constraints; and if it is, determine the join type.
1018  *
1019  * Caller must supply not only the two rels, but the union of their relids.
1020  * (We could simplify the API by computing joinrelids locally, but this
1021  * would be redundant work in the normal path through make_join_rel.)
1022  *
1023  * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
1024  * else it's set to point to the associated SpecialJoinInfo node.  Also,
1025  * *reversed_p is set TRUE if the given relations need to be swapped to
1026  * match the SpecialJoinInfo node.
1027  */
1028 static bool
1029 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
1030                           Relids joinrelids,
1031                           SpecialJoinInfo **sjinfo_p, bool *reversed_p)
1032 {
1033         SpecialJoinInfo *match_sjinfo;
1034         bool            reversed;
1035         bool            unique_ified;
1036         bool            must_be_leftjoin;
1037         ListCell   *l;
1038
1039         /*
1040          * Ensure output params are set on failure return.  This is just to
1041          * suppress uninitialized-variable warnings from overly anal compilers.
1042          */
1043         *sjinfo_p = NULL;
1044         *reversed_p = false;
1045
1046         /*
1047          * If we have any special joins, the proposed join might be illegal; and
1048          * in any case we have to determine its join type.  Scan the join info
1049          * list for matches and conflicts.
1050          */
1051         match_sjinfo = NULL;
1052         reversed = false;
1053         unique_ified = false;
1054         must_be_leftjoin = false;
1055
1056         foreach(l, root->join_info_list)
1057         {
1058                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1059
1060                 /*
1061                  * This special join is not relevant unless its RHS overlaps the
1062                  * proposed join.  (Check this first as a fast path for dismissing
1063                  * most irrelevant SJs quickly.)
1064                  */
1065                 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
1066                         continue;
1067
1068                 /*
1069                  * Also, not relevant if proposed join is fully contained within RHS
1070                  * (ie, we're still building up the RHS).
1071                  */
1072                 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
1073                         continue;
1074
1075                 /*
1076                  * Also, not relevant if SJ is already done within either input.
1077                  */
1078                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1079                         bms_is_subset(sjinfo->min_righthand, rel1->relids))
1080                         continue;
1081                 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
1082                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
1083                         continue;
1084
1085                 /*
1086                  * If it's a semijoin and we already joined the RHS to any other rels
1087                  * within either input, then we must have unique-ified the RHS at that
1088                  * point (see below).  Therefore the semijoin is no longer relevant in
1089                  * this join path.
1090                  */
1091                 if (sjinfo->jointype == JOIN_SEMI)
1092                 {
1093                         if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
1094                                 !bms_equal(sjinfo->syn_righthand, rel1->relids))
1095                                 continue;
1096                         if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
1097                                 !bms_equal(sjinfo->syn_righthand, rel2->relids))
1098                                 continue;
1099                 }
1100
1101                 /*
1102                  * If one input contains min_lefthand and the other contains
1103                  * min_righthand, then we can perform the SJ at this join.
1104                  *
1105                  * Reject if we get matches to more than one SJ; that implies we're
1106                  * considering something that's not really valid.
1107                  */
1108                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
1109                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
1110                 {
1111                         if (match_sjinfo)
1112                                 return false;   /* invalid join path */
1113                         match_sjinfo = sjinfo;
1114                         reversed = false;
1115                 }
1116                 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
1117                                  bms_is_subset(sjinfo->min_righthand, rel1->relids))
1118                 {
1119                         if (match_sjinfo)
1120                                 return false;   /* invalid join path */
1121                         match_sjinfo = sjinfo;
1122                         reversed = true;
1123                 }
1124                 else if (sjinfo->jointype == JOIN_SEMI &&
1125                                  bms_equal(sjinfo->syn_righthand, rel2->relids) &&
1126                                  create_unique_path(root, rel2, rel2->cheapest_total_path,
1127                                                                         sjinfo) != NULL)
1128                 {
1129                         /*----------
1130                          * For a semijoin, we can join the RHS to anything else by
1131                          * unique-ifying the RHS (if the RHS can be unique-ified).
1132                          * We will only get here if we have the full RHS but less
1133                          * than min_lefthand on the LHS.
1134                          *
1135                          * The reason to consider such a join path is exemplified by
1136                          *      SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
1137                          * If we insist on doing this as a semijoin we will first have
1138                          * to form the cartesian product of A*B.  But if we unique-ify
1139                          * C then the semijoin becomes a plain innerjoin and we can join
1140                          * in any order, eg C to A and then to B.  When C is much smaller
1141                          * than A and B this can be a huge win.  So we allow C to be
1142                          * joined to just A or just B here, and then make_join_rel has
1143                          * to handle the case properly.
1144                          *
1145                          * Note that actually we'll allow unique-ified C to be joined to
1146                          * some other relation D here, too.  That is legal, if usually not
1147                          * very sane, and this routine is only concerned with legality not
1148                          * with whether the join is good strategy.
1149                          *----------
1150                          */
1151                         if (match_sjinfo)
1152                                 return false;   /* invalid join path */
1153                         match_sjinfo = sjinfo;
1154                         reversed = false;
1155                         unique_ified = true;
1156                 }
1157                 else if (sjinfo->jointype == JOIN_SEMI &&
1158                                  bms_equal(sjinfo->syn_righthand, rel1->relids) &&
1159                                  create_unique_path(root, rel1, rel1->cheapest_total_path,
1160                                                                         sjinfo) != NULL)
1161                 {
1162                         /* Reversed semijoin case */
1163                         if (match_sjinfo)
1164                                 return false;   /* invalid join path */
1165                         match_sjinfo = sjinfo;
1166                         reversed = true;
1167                         unique_ified = true;
1168                 }
1169                 else
1170                 {
1171                         /*
1172                          * Otherwise, the proposed join overlaps the RHS but isn't a valid
1173                          * implementation of this SJ.  But don't panic quite yet: the RHS
1174                          * violation might have occurred previously, in one or both input
1175                          * relations, in which case we must have previously decided that
1176                          * it was OK to commute some other SJ with this one.  If we need
1177                          * to perform this join to finish building up the RHS, rejecting
1178                          * it could lead to not finding any plan at all.  (This can occur
1179                          * because of the heuristics elsewhere in this file that postpone
1180                          * clauseless joins: we might not consider doing a clauseless join
1181                          * within the RHS until after we've performed other, validly
1182                          * commutable SJs with one or both sides of the clauseless join.)
1183                          * This consideration boils down to the rule that if both inputs
1184                          * overlap the RHS, we can allow the join --- they are either
1185                          * fully within the RHS, or represent previously-allowed joins to
1186                          * rels outside it.
1187                          */
1188                         if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
1189                                 bms_overlap(rel2->relids, sjinfo->min_righthand))
1190                                 continue;               /* assume valid previous violation of RHS */
1191
1192                         /*
1193                          * The proposed join could still be legal, but only if we're
1194                          * allowed to associate it into the RHS of this SJ.  That means
1195                          * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
1196                          * not FULL) and the proposed join must not overlap the LHS.
1197                          */
1198                         if (sjinfo->jointype != JOIN_LEFT ||
1199                                 bms_overlap(joinrelids, sjinfo->min_lefthand))
1200                                 return false;   /* invalid join path */
1201
1202                         /*
1203                          * To be valid, the proposed join must be a LEFT join; otherwise
1204                          * it can't associate into this SJ's RHS.  But we may not yet have
1205                          * found the SpecialJoinInfo matching the proposed join, so we
1206                          * can't test that yet.  Remember the requirement for later.
1207                          */
1208                         must_be_leftjoin = true;
1209                 }
1210         }
1211
1212         /*
1213          * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
1214          * proposed join can't associate into an SJ's RHS.
1215          *
1216          * Also, fail if the proposed join's predicate isn't strict; we're
1217          * essentially checking to see if we can apply outer-join identity 3, and
1218          * that's a requirement.  (This check may be redundant with checks in
1219          * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
1220          */
1221         if (must_be_leftjoin &&
1222                 (match_sjinfo == NULL ||
1223                  match_sjinfo->jointype != JOIN_LEFT ||
1224                  !match_sjinfo->lhs_strict))
1225                 return false;                   /* invalid join path */
1226
1227         /*
1228          * We also have to check for constraints imposed by LATERAL references.
1229          */
1230         if (root->hasLateralRTEs)
1231         {
1232                 bool            lateral_fwd;
1233                 bool            lateral_rev;
1234                 Relids          join_lateral_rels;
1235
1236                 /*
1237                  * The proposed rels could each contain lateral references to the
1238                  * other, in which case the join is impossible.  If there are lateral
1239                  * references in just one direction, then the join has to be done with
1240                  * a nestloop with the lateral referencer on the inside.  If the join
1241                  * matches an SJ that cannot be implemented by such a nestloop, the
1242                  * join is impossible.
1243                  *
1244                  * Also, if the lateral reference is only indirect, we should reject
1245                  * the join; whatever rel(s) the reference chain goes through must be
1246                  * joined to first.
1247                  *
1248                  * Another case that might keep us from building a valid plan is the
1249                  * implementation restriction described by have_dangerous_phv().
1250                  */
1251                 lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
1252                 lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
1253                 if (lateral_fwd && lateral_rev)
1254                         return false;           /* have lateral refs in both directions */
1255                 if (lateral_fwd)
1256                 {
1257                         /* has to be implemented as nestloop with rel1 on left */
1258                         if (match_sjinfo &&
1259                                 (reversed ||
1260                                  unique_ified ||
1261                                  match_sjinfo->jointype == JOIN_FULL))
1262                                 return false;   /* not implementable as nestloop */
1263                         /* check there is a direct reference from rel2 to rel1 */
1264                         if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
1265                                 return false;   /* only indirect refs, so reject */
1266                         /* check we won't have a dangerous PHV */
1267                         if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
1268                                 return false;   /* might be unable to handle required PHV */
1269                 }
1270                 else if (lateral_rev)
1271                 {
1272                         /* has to be implemented as nestloop with rel2 on left */
1273                         if (match_sjinfo &&
1274                                 (!reversed ||
1275                                  unique_ified ||
1276                                  match_sjinfo->jointype == JOIN_FULL))
1277                                 return false;   /* not implementable as nestloop */
1278                         /* check there is a direct reference from rel1 to rel2 */
1279                         if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
1280                                 return false;   /* only indirect refs, so reject */
1281                         /* check we won't have a dangerous PHV */
1282                         if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
1283                                 return false;   /* might be unable to handle required PHV */
1284                 }
1285
1286                 /*
1287                  * LATERAL references could also cause problems later on if we accept
1288                  * this join: if the join's minimum parameterization includes any rels
1289                  * that would have to be on the inside of an outer join with this join
1290                  * rel, then it's never going to be possible to build the complete
1291                  * query using this join.  We should reject this join not only because
1292                  * it'll save work, but because if we don't, the clauseless-join
1293                  * heuristics might think that legality of this join means that some
1294                  * other join rel need not be formed, and that could lead to failure
1295                  * to find any plan at all.  We have to consider not only rels that
1296                  * are directly on the inner side of an OJ with the joinrel, but also
1297                  * ones that are indirectly so, so search to find all such rels.
1298                  */
1299                 join_lateral_rels = min_join_parameterization(root, joinrelids,
1300                                                                                                           rel1, rel2);
1301                 if (join_lateral_rels)
1302                 {
1303                         Relids          join_plus_rhs = bms_copy(joinrelids);
1304                         bool            more;
1305
1306                         do
1307                         {
1308                                 more = false;
1309                                 foreach(l, root->join_info_list)
1310                                 {
1311                                         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1312
1313                                         if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
1314                                                 !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
1315                                         {
1316                                                 join_plus_rhs = bms_add_members(join_plus_rhs,
1317                                                                                                                 sjinfo->min_righthand);
1318                                                 more = true;
1319                                         }
1320                                         /* full joins constrain both sides symmetrically */
1321                                         if (sjinfo->jointype == JOIN_FULL &&
1322                                                 bms_overlap(sjinfo->min_righthand, join_plus_rhs) &&
1323                                                 !bms_is_subset(sjinfo->min_lefthand, join_plus_rhs))
1324                                         {
1325                                                 join_plus_rhs = bms_add_members(join_plus_rhs,
1326                                                                                                                 sjinfo->min_lefthand);
1327                                                 more = true;
1328                                         }
1329                                 }
1330                         } while (more);
1331                         if (bms_overlap(join_plus_rhs, join_lateral_rels))
1332                                 return false;   /* will not be able to join to some RHS rel */
1333                 }
1334         }
1335
1336         /* Otherwise, it's a valid join */
1337         *sjinfo_p = match_sjinfo;
1338         *reversed_p = reversed;
1339         return true;
1340 }
1341
1342
1343 /*
1344  * has_join_restriction
1345  *              Detect whether the specified relation has join-order restrictions,
1346  *              due to being inside an outer join or an IN (sub-SELECT),
1347  *              or participating in any LATERAL references or multi-rel PHVs.
1348  *
1349  * Essentially, this tests whether have_join_order_restriction() could
1350  * succeed with this rel and some other one.  It's OK if we sometimes
1351  * say "true" incorrectly.  (Therefore, we don't bother with the relatively
1352  * expensive has_legal_joinclause test.)
1353  */
1354 static bool
1355 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
1356 {
1357         ListCell   *l;
1358
1359         if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
1360                 return true;
1361
1362         foreach(l, root->placeholder_list)
1363         {
1364                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
1365
1366                 if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
1367                         !bms_equal(rel->relids, phinfo->ph_eval_at))
1368                         return true;
1369         }
1370
1371         foreach(l, root->join_info_list)
1372         {
1373                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
1374
1375                 /* ignore full joins --- other mechanisms preserve their ordering */
1376                 if (sjinfo->jointype == JOIN_FULL)
1377                         continue;
1378
1379                 /* ignore if SJ is already contained in rel */
1380                 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
1381                         bms_is_subset(sjinfo->min_righthand, rel->relids))
1382                         continue;
1383
1384                 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
1385                 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
1386                         bms_overlap(sjinfo->min_righthand, rel->relids))
1387                         return true;
1388         }
1389
1390         return false;
1391 }
1392
1393
1394 /*
1395  * is_dummy_rel --- has relation been proven empty?
1396  */
1397 static bool
1398 is_dummy_rel(RelOptInfo *rel)
1399 {
1400         return IS_DUMMY_REL(rel);
1401 }
1402
1403
1404 /*
1405  * restriction_is_constant_false --- is a restrictlist just FALSE?
1406  *
1407  * In cases where a qual is provably constant FALSE, eval_const_expressions
1408  * will generally have thrown away anything that's ANDed with it.  In outer
1409  * join situations this will leave us computing cartesian products only to
1410  * decide there's no match for an outer row, which is pretty stupid.  So,
1411  * we need to detect the case.
1412  *
1413  * If only_pushed_down is TRUE, then consider only pushed-down quals.
1414  */
1415 static bool
1416 restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
1417 {
1418         ListCell   *lc;
1419
1420         /*
1421          * Despite the above comment, the restriction list we see here might
1422          * possibly have other members besides the FALSE constant, since other
1423          * quals could get "pushed down" to the outer join level.  So we check
1424          * each member of the list.
1425          */
1426         foreach(lc, restrictlist)
1427         {
1428                 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1429
1430                 if (only_pushed_down && !rinfo->is_pushed_down)
1431                         continue;
1432
1433                 if (rinfo->clause && IsA(rinfo->clause, Const))
1434                 {
1435                         Const      *con = (Const *) rinfo->clause;
1436
1437                         /* constant NULL is as good as constant FALSE for our purposes */
1438                         if (con->constisnull)
1439                                 return true;
1440                         if (!DatumGetBool(con->constvalue))
1441                                 return true;
1442                 }
1443         }
1444         return false;
1445 }