OSDN Git Service

Follow core's cobe/havor chages
[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  *     add_paths_to_append_rel()
18  *     try_partitionwise_join()
19  *
20  *  public functions:
21  *     standard_join_search(): This funcion is not static. The reason for
22  *        including this function is make_rels_by_clause_joins. In order to
23  *        avoid generating apparently unwanted join combination, we decided to
24  *        change the behavior of make_join_rel, which is called under this
25  *        function.
26  *
27  * src/backend/optimizer/path/joinrels.c
28  *
29  *      public functions:
30  *     join_search_one_level(): We have to modify this to call my definition of
31  *                  make_rels_by_clause_joins.
32  *
33  *      static functions:
34  *     make_rels_by_clause_joins()
35  *     make_rels_by_clauseless_joins()
36  *     join_is_legal()
37  *     has_join_restriction()
38  *     is_dummy_rel()
39  *     restriction_is_constant_false()
40  *     update_child_rel_info()
41  *     build_child_join_sjinfo()
42  *
43  *
44  * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
45  * Portions Copyright (c) 1994, Regents of the University of California
46  *
47  *-------------------------------------------------------------------------
48  */
49
50 static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
51                                                                 RelOptInfo *rel2, RelOptInfo *joinrel,
52                                                                 SpecialJoinInfo *sjinfo, List *restrictlist);
53
54 /*
55  * set_plain_rel_pathlist
56  *        Build access paths for a plain relation (no subquery, no inheritance)
57  */
58 static void
59 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
60 {
61         Relids          required_outer;
62
63         /*
64          * We don't support pushing join clauses into the quals of a seqscan, but
65          * it could still have required parameterization due to LATERAL refs in
66          * its tlist.
67          */
68         required_outer = rel->lateral_relids;
69
70         /* Consider sequential scan */
71         add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
72
73         /* If appropriate, consider parallel sequential scan */
74         if (rel->consider_parallel && required_outer == NULL)
75                 create_plain_partial_paths(root, rel);
76
77         /* Consider index scans */
78         create_index_paths(root, rel);
79
80         /* Consider TID scans */
81         create_tidscan_paths(root, rel);
82 }
83
84
85 /*
86  * set_append_rel_pathlist
87  *        Build access paths for an "append relation"
88  */
89 static void
90 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
91                                                 Index rti, RangeTblEntry *rte)
92 {
93         int                     parentRTindex = rti;
94         List       *live_childrels = NIL;
95         ListCell   *l;
96
97         /*
98          * Generate access paths for each member relation, and remember the
99          * non-dummy children.
100          */
101         foreach(l, root->append_rel_list)
102         {
103                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
104                 int                     childRTindex;
105                 RangeTblEntry *childRTE;
106                 RelOptInfo *childrel;
107
108                 /* append_rel_list contains all append rels; ignore others */
109                 if (appinfo->parent_relid != parentRTindex)
110                         continue;
111
112                 /* Re-locate the child RTE and RelOptInfo */
113                 childRTindex = appinfo->child_relid;
114                 childRTE = root->simple_rte_array[childRTindex];
115                 childrel = root->simple_rel_array[childRTindex];
116
117                 /*
118                  * If set_append_rel_size() decided the parent appendrel was
119                  * parallel-unsafe at some point after visiting this child rel, we
120                  * need to propagate the unsafety marking down to the child, so that
121                  * we don't generate useless partial paths for it.
122                  */
123                 if (!rel->consider_parallel)
124                         childrel->consider_parallel = false;
125
126                 /*
127                  * Compute the child's access paths.
128                  */
129                 set_rel_pathlist(root, childrel, childRTindex, childRTE);
130
131                 /*
132                  * If child is dummy, ignore it.
133                  */
134                 if (IS_DUMMY_REL(childrel))
135                         continue;
136
137                 /* Bubble up childrel's partitioned children. */
138                 if (rel->part_scheme)
139                         rel->partitioned_child_rels =
140                                 list_concat(rel->partitioned_child_rels,
141                                                         list_copy(childrel->partitioned_child_rels));
142
143                 /*
144                  * Child is live, so add it to the live_childrels list for use below.
145                  */
146                 live_childrels = lappend(live_childrels, childrel);
147         }
148
149         /* Add paths to the append relation. */
150         add_paths_to_append_rel(root, rel, live_childrels);
151 }
152
153
154 /*
155  * standard_join_search
156  *        Find possible joinpaths for a query by successively finding ways
157  *        to join component relations into join relations.
158  *
159  * 'levels_needed' is the number of iterations needed, ie, the number of
160  *              independent jointree items in the query.  This is > 1.
161  *
162  * 'initial_rels' is a list of RelOptInfo nodes for each independent
163  *              jointree item.  These are the components to be joined together.
164  *              Note that levels_needed == list_length(initial_rels).
165  *
166  * Returns the final level of join relations, i.e., the relation that is
167  * the result of joining all the original relations together.
168  * At least one implementation path must be provided for this relation and
169  * all required sub-relations.
170  *
171  * To support loadable plugins that modify planner behavior by changing the
172  * join searching algorithm, we provide a hook variable that lets a plugin
173  * replace or supplement this function.  Any such hook must return the same
174  * final join relation as the standard code would, but it might have a
175  * different set of implementation paths attached, and only the sub-joinrels
176  * needed for these paths need have been instantiated.
177  *
178  * Note to plugin authors: the functions invoked during standard_join_search()
179  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
180  * than one join-order search, you'll probably need to save and restore the
181  * original states of those data structures.  See geqo_eval() for an example.
182  */
183 RelOptInfo *
184 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
185 {
186         int                     lev;
187         RelOptInfo *rel;
188
189         /*
190          * This function cannot be invoked recursively within any one planning
191          * problem, so join_rel_level[] can't be in use already.
192          */
193         Assert(root->join_rel_level == NULL);
194
195         /*
196          * We employ a simple "dynamic programming" algorithm: we first find all
197          * ways to build joins of two jointree items, then all ways to build joins
198          * of three items (from two-item joins and single items), then four-item
199          * joins, and so on until we have considered all ways to join all the
200          * items into one rel.
201          *
202          * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
203          * set root->join_rel_level[1] to represent all the single-jointree-item
204          * relations.
205          */
206         root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
207
208         root->join_rel_level[1] = initial_rels;
209
210         for (lev = 2; lev <= levels_needed; lev++)
211         {
212                 ListCell   *lc;
213
214                 /*
215                  * Determine all possible pairs of relations to be joined at this
216                  * level, and build paths for making each one from every available
217                  * pair of lower-level relations.
218                  */
219                 join_search_one_level(root, lev);
220
221                 /*
222                  * Run generate_partitionwise_join_paths() and generate_gather_paths()
223                  * for each just-processed joinrel.  We could not do this earlier
224                  * because both regular and partial paths can get added to a
225                  * particular joinrel at multiple times within join_search_one_level.
226                  *
227                  * After that, we're done creating paths for the joinrel, so run
228                  * set_cheapest().
229                  */
230                 foreach(lc, root->join_rel_level[lev])
231                 {
232                         rel = (RelOptInfo *) lfirst(lc);
233
234                         /* Create paths for partitionwise joins. */
235                         generate_partitionwise_join_paths(root, rel);
236
237                         /*
238                          * Except for the topmost scan/join rel, consider gathering
239                          * partial paths.  We'll do the same for the topmost scan/join rel
240                          * once we know the final targetlist (see grouping_planner).
241                          */
242                         if (lev < levels_needed)
243                                 generate_gather_paths(root, rel, false);
244
245                         /* Find and save the cheapest paths for this rel */
246                         set_cheapest(rel);
247
248 #ifdef OPTIMIZER_DEBUG
249                         debug_print_rel(root, rel);
250 #endif
251                 }
252         }
253
254         /*
255          * We should have a single rel at the final level.
256          */
257         if (root->join_rel_level[levels_needed] == NIL)
258                 elog(ERROR, "failed to build any %d-way joins", levels_needed);
259         Assert(list_length(root->join_rel_level[levels_needed]) == 1);
260
261         rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
262
263         root->join_rel_level = NULL;
264
265         return rel;
266 }
267
268 /*
269  * create_plain_partial_paths
270  *        Build partial access paths for parallel scan of a plain relation
271  */
272 static void
273 create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
274 {
275         int                     parallel_workers;
276
277         parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
278                                                                                            max_parallel_workers_per_gather);
279
280         /* If any limit was set to zero, the user doesn't want a parallel scan. */
281         if (parallel_workers <= 0)
282                 return;
283
284         /* Add an unordered partial path based on a parallel sequential scan. */
285         add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
286 }
287
288
289 /*
290  * join_search_one_level
291  *        Consider ways to produce join relations containing exactly 'level'
292  *        jointree items.  (This is one step of the dynamic-programming method
293  *        embodied in standard_join_search.)  Join rel nodes for each feasible
294  *        combination of lower-level rels are created and returned in a list.
295  *        Implementation paths are created for each such joinrel, too.
296  *
297  * level: level of rels we want to make this time
298  * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
299  *
300  * The result is returned in root->join_rel_level[level].
301  */
302 void
303 join_search_one_level(PlannerInfo *root, int level)
304 {
305         List      **joinrels = root->join_rel_level;
306         ListCell   *r;
307         int                     k;
308
309         Assert(joinrels[level] == NIL);
310
311         /* Set join_cur_level so that new joinrels are added to proper list */
312         root->join_cur_level = level;
313
314         /*
315          * First, consider left-sided and right-sided plans, in which rels of
316          * exactly level-1 member relations are joined against initial relations.
317          * We prefer to join using join clauses, but if we find a rel of level-1
318          * members that has no join clauses, we will generate Cartesian-product
319          * joins against all initial rels not already contained in it.
320          */
321         foreach(r, joinrels[level - 1])
322         {
323                 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
324
325                 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
326                         has_join_restriction(root, old_rel))
327                 {
328                         /*
329                          * There are join clauses or join order restrictions relevant to
330                          * this rel, so consider joins between this rel and (only) those
331                          * initial rels it is linked to by a clause or restriction.
332                          *
333                          * At level 2 this condition is symmetric, so there is no need to
334                          * look at initial rels before this one in the list; we already
335                          * considered such joins when we were at the earlier rel.  (The
336                          * mirror-image joins are handled automatically by make_join_rel.)
337                          * In later passes (level > 2), we join rels of the previous level
338                          * to each initial rel they don't already include but have a join
339                          * clause or restriction with.
340                          */
341                         ListCell   *other_rels;
342
343                         if (level == 2)         /* consider remaining initial rels */
344                                 other_rels = lnext(r);
345                         else                            /* consider all initial rels */
346                                 other_rels = list_head(joinrels[1]);
347
348                         make_rels_by_clause_joins(root,
349                                                                           old_rel,
350                                                                           other_rels);
351                 }
352                 else
353                 {
354                         /*
355                          * Oops, we have a relation that is not joined to any other
356                          * relation, either directly or by join-order restrictions.
357                          * Cartesian product time.
358                          *
359                          * We consider a cartesian product with each not-already-included
360                          * initial rel, whether it has other join clauses or not.  At
361                          * level 2, if there are two or more clauseless initial rels, we
362                          * will redundantly consider joining them in both directions; but
363                          * such cases aren't common enough to justify adding complexity to
364                          * avoid the duplicated effort.
365                          */
366                         make_rels_by_clauseless_joins(root,
367                                                                                   old_rel,
368                                                                                   list_head(joinrels[1]));
369                 }
370         }
371
372         /*
373          * Now, consider "bushy plans" in which relations of k initial rels are
374          * joined to relations of level-k initial rels, for 2 <= k <= level-2.
375          *
376          * We only consider bushy-plan joins for pairs of rels where there is a
377          * suitable join clause (or join order restriction), in order to avoid
378          * unreasonable growth of planning time.
379          */
380         for (k = 2;; k++)
381         {
382                 int                     other_level = level - k;
383
384                 /*
385                  * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
386                  * need to go as far as the halfway point.
387                  */
388                 if (k > other_level)
389                         break;
390
391                 foreach(r, joinrels[k])
392                 {
393                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
394                         ListCell   *other_rels;
395                         ListCell   *r2;
396
397                         /*
398                          * We can ignore relations without join clauses here, unless they
399                          * participate in join-order restrictions --- then we might have
400                          * to force a bushy join plan.
401                          */
402                         if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
403                                 !has_join_restriction(root, old_rel))
404                                 continue;
405
406                         if (k == other_level)
407                                 other_rels = lnext(r);  /* only consider remaining rels */
408                         else
409                                 other_rels = list_head(joinrels[other_level]);
410
411                         for_each_cell(r2, other_rels)
412                         {
413                                 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
414
415                                 if (!bms_overlap(old_rel->relids, new_rel->relids))
416                                 {
417                                         /*
418                                          * OK, we can build a rel of the right level from this
419                                          * pair of rels.  Do so if there is at least one relevant
420                                          * join clause or join order restriction.
421                                          */
422                                         if (have_relevant_joinclause(root, old_rel, new_rel) ||
423                                                 have_join_order_restriction(root, old_rel, new_rel))
424                                         {
425                                                 (void) make_join_rel(root, old_rel, new_rel);
426                                         }
427                                 }
428                         }
429                 }
430         }
431
432         /*----------
433          * Last-ditch effort: if we failed to find any usable joins so far, force
434          * a set of cartesian-product joins to be generated.  This handles the
435          * special case where all the available rels have join clauses but we
436          * cannot use any of those clauses yet.  This can only happen when we are
437          * considering a join sub-problem (a sub-joinlist) and all the rels in the
438          * sub-problem have only join clauses with rels outside the sub-problem.
439          * An example is
440          *
441          *              SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
442          *              WHERE a.w = c.x and b.y = d.z;
443          *
444          * If the "a INNER JOIN b" sub-problem does not get flattened into the
445          * upper level, we must be willing to make a cartesian join of a and b;
446          * but the code above will not have done so, because it thought that both
447          * a and b have joinclauses.  We consider only left-sided and right-sided
448          * cartesian joins in this case (no bushy).
449          *----------
450          */
451         if (joinrels[level] == NIL)
452         {
453                 /*
454                  * This loop is just like the first one, except we always call
455                  * make_rels_by_clauseless_joins().
456                  */
457                 foreach(r, joinrels[level - 1])
458                 {
459                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
460
461                         make_rels_by_clauseless_joins(root,
462                                                                                   old_rel,
463                                                                                   list_head(joinrels[1]));
464                 }
465
466                 /*----------
467                  * When special joins are involved, there may be no legal way
468                  * to make an N-way join for some values of N.  For example consider
469                  *
470                  * SELECT ... FROM t1 WHERE
471                  *       x IN (SELECT ... FROM t2,t3 WHERE ...) AND
472                  *       y IN (SELECT ... FROM t4,t5 WHERE ...)
473                  *
474                  * We will flatten this query to a 5-way join problem, but there are
475                  * no 4-way joins that join_is_legal() will consider legal.  We have
476                  * to accept failure at level 4 and go on to discover a workable
477                  * bushy plan at level 5.
478                  *
479                  * However, if there are no special joins and no lateral references
480                  * then join_is_legal() should never fail, and so the following sanity
481                  * check is useful.
482                  *----------
483                  */
484                 if (joinrels[level] == NIL &&
485                         root->join_info_list == NIL &&
486                         !root->hasLateralRTEs)
487                         elog(ERROR, "failed to build any %d-way joins", level);
488         }
489 }
490
491
492 /*
493  * make_rels_by_clause_joins
494  *        Build joins between the given relation 'old_rel' and other relations
495  *        that participate in join clauses that 'old_rel' also participates in
496  *        (or participate in join-order restrictions with it).
497  *        The join rels are returned in root->join_rel_level[join_cur_level].
498  *
499  * Note: at levels above 2 we will generate the same joined relation in
500  * multiple ways --- for example (a join b) join c is the same RelOptInfo as
501  * (b join c) join a, though the second case will add a different set of Paths
502  * to it.  This is the reason for using the join_rel_level mechanism, which
503  * automatically ensures that each new joinrel is only added to the list once.
504  *
505  * 'old_rel' is the relation entry for the relation to be joined
506  * 'other_rels': the first cell in a linked list containing the other
507  * rels to be considered for joining
508  *
509  * Currently, this is only used with initial rels in other_rels, but it
510  * will work for joining to joinrels too.
511  */
512 static void
513 make_rels_by_clause_joins(PlannerInfo *root,
514                                                   RelOptInfo *old_rel,
515                                                   ListCell *other_rels)
516 {
517         ListCell   *l;
518
519         for_each_cell(l, other_rels)
520         {
521                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
522
523                 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
524                         (have_relevant_joinclause(root, old_rel, other_rel) ||
525                          have_join_order_restriction(root, old_rel, other_rel)))
526                 {
527                         (void) make_join_rel(root, old_rel, other_rel);
528                 }
529         }
530 }
531
532
533 /*
534  * make_rels_by_clauseless_joins
535  *        Given a relation 'old_rel' and a list of other relations
536  *        'other_rels', create a join relation between 'old_rel' and each
537  *        member of 'other_rels' that isn't already included in 'old_rel'.
538  *        The join rels are returned in root->join_rel_level[join_cur_level].
539  *
540  * 'old_rel' is the relation entry for the relation to be joined
541  * 'other_rels': the first cell of a linked list containing the
542  * other rels to be considered for joining
543  *
544  * Currently, this is only used with initial rels in other_rels, but it would
545  * work for joining to joinrels too.
546  */
547 static void
548 make_rels_by_clauseless_joins(PlannerInfo *root,
549                                                           RelOptInfo *old_rel,
550                                                           ListCell *other_rels)
551 {
552         ListCell   *l;
553
554         for_each_cell(l, other_rels)
555         {
556                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
557
558                 if (!bms_overlap(other_rel->relids, old_rel->relids))
559                 {
560                         (void) make_join_rel(root, old_rel, other_rel);
561                 }
562         }
563 }
564
565
566 /*
567  * join_is_legal
568  *         Determine whether a proposed join is legal given the query's
569  *         join order constraints; and if it is, determine the join type.
570  *
571  * Caller must supply not only the two rels, but the union of their relids.
572  * (We could simplify the API by computing joinrelids locally, but this
573  * would be redundant work in the normal path through make_join_rel.)
574  *
575  * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
576  * else it's set to point to the associated SpecialJoinInfo node.  Also,
577  * *reversed_p is set true if the given relations need to be swapped to
578  * match the SpecialJoinInfo node.
579  */
580 static bool
581 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
582                           Relids joinrelids,
583                           SpecialJoinInfo **sjinfo_p, bool *reversed_p)
584 {
585         SpecialJoinInfo *match_sjinfo;
586         bool            reversed;
587         bool            unique_ified;
588         bool            must_be_leftjoin;
589         ListCell   *l;
590
591         /*
592          * Ensure output params are set on failure return.  This is just to
593          * suppress uninitialized-variable warnings from overly anal compilers.
594          */
595         *sjinfo_p = NULL;
596         *reversed_p = false;
597
598         /*
599          * If we have any special joins, the proposed join might be illegal; and
600          * in any case we have to determine its join type.  Scan the join info
601          * list for matches and conflicts.
602          */
603         match_sjinfo = NULL;
604         reversed = false;
605         unique_ified = false;
606         must_be_leftjoin = false;
607
608         foreach(l, root->join_info_list)
609         {
610                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
611
612                 /*
613                  * This special join is not relevant unless its RHS overlaps the
614                  * proposed join.  (Check this first as a fast path for dismissing
615                  * most irrelevant SJs quickly.)
616                  */
617                 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
618                         continue;
619
620                 /*
621                  * Also, not relevant if proposed join is fully contained within RHS
622                  * (ie, we're still building up the RHS).
623                  */
624                 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
625                         continue;
626
627                 /*
628                  * Also, not relevant if SJ is already done within either input.
629                  */
630                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
631                         bms_is_subset(sjinfo->min_righthand, rel1->relids))
632                         continue;
633                 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
634                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
635                         continue;
636
637                 /*
638                  * If it's a semijoin and we already joined the RHS to any other rels
639                  * within either input, then we must have unique-ified the RHS at that
640                  * point (see below).  Therefore the semijoin is no longer relevant in
641                  * this join path.
642                  */
643                 if (sjinfo->jointype == JOIN_SEMI)
644                 {
645                         if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
646                                 !bms_equal(sjinfo->syn_righthand, rel1->relids))
647                                 continue;
648                         if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
649                                 !bms_equal(sjinfo->syn_righthand, rel2->relids))
650                                 continue;
651                 }
652
653                 /*
654                  * If one input contains min_lefthand and the other contains
655                  * min_righthand, then we can perform the SJ at this join.
656                  *
657                  * Reject if we get matches to more than one SJ; that implies we're
658                  * considering something that's not really valid.
659                  */
660                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
661                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
662                 {
663                         if (match_sjinfo)
664                                 return false;   /* invalid join path */
665                         match_sjinfo = sjinfo;
666                         reversed = false;
667                 }
668                 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
669                                  bms_is_subset(sjinfo->min_righthand, rel1->relids))
670                 {
671                         if (match_sjinfo)
672                                 return false;   /* invalid join path */
673                         match_sjinfo = sjinfo;
674                         reversed = true;
675                 }
676                 else if (sjinfo->jointype == JOIN_SEMI &&
677                                  bms_equal(sjinfo->syn_righthand, rel2->relids) &&
678                                  create_unique_path(root, rel2, rel2->cheapest_total_path,
679                                                                         sjinfo) != NULL)
680                 {
681                         /*----------
682                          * For a semijoin, we can join the RHS to anything else by
683                          * unique-ifying the RHS (if the RHS can be unique-ified).
684                          * We will only get here if we have the full RHS but less
685                          * than min_lefthand on the LHS.
686                          *
687                          * The reason to consider such a join path is exemplified by
688                          *      SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
689                          * If we insist on doing this as a semijoin we will first have
690                          * to form the cartesian product of A*B.  But if we unique-ify
691                          * C then the semijoin becomes a plain innerjoin and we can join
692                          * in any order, eg C to A and then to B.  When C is much smaller
693                          * than A and B this can be a huge win.  So we allow C to be
694                          * joined to just A or just B here, and then make_join_rel has
695                          * to handle the case properly.
696                          *
697                          * Note that actually we'll allow unique-ified C to be joined to
698                          * some other relation D here, too.  That is legal, if usually not
699                          * very sane, and this routine is only concerned with legality not
700                          * with whether the join is good strategy.
701                          *----------
702                          */
703                         if (match_sjinfo)
704                                 return false;   /* invalid join path */
705                         match_sjinfo = sjinfo;
706                         reversed = false;
707                         unique_ified = true;
708                 }
709                 else if (sjinfo->jointype == JOIN_SEMI &&
710                                  bms_equal(sjinfo->syn_righthand, rel1->relids) &&
711                                  create_unique_path(root, rel1, rel1->cheapest_total_path,
712                                                                         sjinfo) != NULL)
713                 {
714                         /* Reversed semijoin case */
715                         if (match_sjinfo)
716                                 return false;   /* invalid join path */
717                         match_sjinfo = sjinfo;
718                         reversed = true;
719                         unique_ified = true;
720                 }
721                 else
722                 {
723                         /*
724                          * Otherwise, the proposed join overlaps the RHS but isn't a valid
725                          * implementation of this SJ.  But don't panic quite yet: the RHS
726                          * violation might have occurred previously, in one or both input
727                          * relations, in which case we must have previously decided that
728                          * it was OK to commute some other SJ with this one.  If we need
729                          * to perform this join to finish building up the RHS, rejecting
730                          * it could lead to not finding any plan at all.  (This can occur
731                          * because of the heuristics elsewhere in this file that postpone
732                          * clauseless joins: we might not consider doing a clauseless join
733                          * within the RHS until after we've performed other, validly
734                          * commutable SJs with one or both sides of the clauseless join.)
735                          * This consideration boils down to the rule that if both inputs
736                          * overlap the RHS, we can allow the join --- they are either
737                          * fully within the RHS, or represent previously-allowed joins to
738                          * rels outside it.
739                          */
740                         if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
741                                 bms_overlap(rel2->relids, sjinfo->min_righthand))
742                                 continue;               /* assume valid previous violation of RHS */
743
744                         /*
745                          * The proposed join could still be legal, but only if we're
746                          * allowed to associate it into the RHS of this SJ.  That means
747                          * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
748                          * not FULL) and the proposed join must not overlap the LHS.
749                          */
750                         if (sjinfo->jointype != JOIN_LEFT ||
751                                 bms_overlap(joinrelids, sjinfo->min_lefthand))
752                                 return false;   /* invalid join path */
753
754                         /*
755                          * To be valid, the proposed join must be a LEFT join; otherwise
756                          * it can't associate into this SJ's RHS.  But we may not yet have
757                          * found the SpecialJoinInfo matching the proposed join, so we
758                          * can't test that yet.  Remember the requirement for later.
759                          */
760                         must_be_leftjoin = true;
761                 }
762         }
763
764         /*
765          * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
766          * proposed join can't associate into an SJ's RHS.
767          *
768          * Also, fail if the proposed join's predicate isn't strict; we're
769          * essentially checking to see if we can apply outer-join identity 3, and
770          * that's a requirement.  (This check may be redundant with checks in
771          * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
772          */
773         if (must_be_leftjoin &&
774                 (match_sjinfo == NULL ||
775                  match_sjinfo->jointype != JOIN_LEFT ||
776                  !match_sjinfo->lhs_strict))
777                 return false;                   /* invalid join path */
778
779         /*
780          * We also have to check for constraints imposed by LATERAL references.
781          */
782         if (root->hasLateralRTEs)
783         {
784                 bool            lateral_fwd;
785                 bool            lateral_rev;
786                 Relids          join_lateral_rels;
787
788                 /*
789                  * The proposed rels could each contain lateral references to the
790                  * other, in which case the join is impossible.  If there are lateral
791                  * references in just one direction, then the join has to be done with
792                  * a nestloop with the lateral referencer on the inside.  If the join
793                  * matches an SJ that cannot be implemented by such a nestloop, the
794                  * join is impossible.
795                  *
796                  * Also, if the lateral reference is only indirect, we should reject
797                  * the join; whatever rel(s) the reference chain goes through must be
798                  * joined to first.
799                  *
800                  * Another case that might keep us from building a valid plan is the
801                  * implementation restriction described by have_dangerous_phv().
802                  */
803                 lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
804                 lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
805                 if (lateral_fwd && lateral_rev)
806                         return false;           /* have lateral refs in both directions */
807                 if (lateral_fwd)
808                 {
809                         /* has to be implemented as nestloop with rel1 on left */
810                         if (match_sjinfo &&
811                                 (reversed ||
812                                  unique_ified ||
813                                  match_sjinfo->jointype == JOIN_FULL))
814                                 return false;   /* not implementable as nestloop */
815                         /* check there is a direct reference from rel2 to rel1 */
816                         if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
817                                 return false;   /* only indirect refs, so reject */
818                         /* check we won't have a dangerous PHV */
819                         if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
820                                 return false;   /* might be unable to handle required PHV */
821                 }
822                 else if (lateral_rev)
823                 {
824                         /* has to be implemented as nestloop with rel2 on left */
825                         if (match_sjinfo &&
826                                 (!reversed ||
827                                  unique_ified ||
828                                  match_sjinfo->jointype == JOIN_FULL))
829                                 return false;   /* not implementable as nestloop */
830                         /* check there is a direct reference from rel1 to rel2 */
831                         if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
832                                 return false;   /* only indirect refs, so reject */
833                         /* check we won't have a dangerous PHV */
834                         if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
835                                 return false;   /* might be unable to handle required PHV */
836                 }
837
838                 /*
839                  * LATERAL references could also cause problems later on if we accept
840                  * this join: if the join's minimum parameterization includes any rels
841                  * that would have to be on the inside of an outer join with this join
842                  * rel, then it's never going to be possible to build the complete
843                  * query using this join.  We should reject this join not only because
844                  * it'll save work, but because if we don't, the clauseless-join
845                  * heuristics might think that legality of this join means that some
846                  * other join rel need not be formed, and that could lead to failure
847                  * to find any plan at all.  We have to consider not only rels that
848                  * are directly on the inner side of an OJ with the joinrel, but also
849                  * ones that are indirectly so, so search to find all such rels.
850                  */
851                 join_lateral_rels = min_join_parameterization(root, joinrelids,
852                                                                                                           rel1, rel2);
853                 if (join_lateral_rels)
854                 {
855                         Relids          join_plus_rhs = bms_copy(joinrelids);
856                         bool            more;
857
858                         do
859                         {
860                                 more = false;
861                                 foreach(l, root->join_info_list)
862                                 {
863                                         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
864
865                                         if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
866                                                 !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
867                                         {
868                                                 join_plus_rhs = bms_add_members(join_plus_rhs,
869                                                                                                                 sjinfo->min_righthand);
870                                                 more = true;
871                                         }
872                                         /* full joins constrain both sides symmetrically */
873                                         if (sjinfo->jointype == JOIN_FULL &&
874                                                 bms_overlap(sjinfo->min_righthand, join_plus_rhs) &&
875                                                 !bms_is_subset(sjinfo->min_lefthand, join_plus_rhs))
876                                         {
877                                                 join_plus_rhs = bms_add_members(join_plus_rhs,
878                                                                                                                 sjinfo->min_lefthand);
879                                                 more = true;
880                                         }
881                                 }
882                         } while (more);
883                         if (bms_overlap(join_plus_rhs, join_lateral_rels))
884                                 return false;   /* will not be able to join to some RHS rel */
885                 }
886         }
887
888         /* Otherwise, it's a valid join */
889         *sjinfo_p = match_sjinfo;
890         *reversed_p = reversed;
891         return true;
892 }
893
894
895 /*
896  * has_join_restriction
897  *              Detect whether the specified relation has join-order restrictions,
898  *              due to being inside an outer join or an IN (sub-SELECT),
899  *              or participating in any LATERAL references or multi-rel PHVs.
900  *
901  * Essentially, this tests whether have_join_order_restriction() could
902  * succeed with this rel and some other one.  It's OK if we sometimes
903  * say "true" incorrectly.  (Therefore, we don't bother with the relatively
904  * expensive has_legal_joinclause test.)
905  */
906 static bool
907 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
908 {
909         ListCell   *l;
910
911         if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
912                 return true;
913
914         foreach(l, root->placeholder_list)
915         {
916                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
917
918                 if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
919                         !bms_equal(rel->relids, phinfo->ph_eval_at))
920                         return true;
921         }
922
923         foreach(l, root->join_info_list)
924         {
925                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
926
927                 /* ignore full joins --- other mechanisms preserve their ordering */
928                 if (sjinfo->jointype == JOIN_FULL)
929                         continue;
930
931                 /* ignore if SJ is already contained in rel */
932                 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
933                         bms_is_subset(sjinfo->min_righthand, rel->relids))
934                         continue;
935
936                 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
937                 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
938                         bms_overlap(sjinfo->min_righthand, rel->relids))
939                         return true;
940         }
941
942         return false;
943 }
944
945
946 /*
947  * is_dummy_rel --- has relation been proven empty?
948  */
949 static bool
950 is_dummy_rel(RelOptInfo *rel)
951 {
952         return IS_DUMMY_REL(rel);
953 }
954
955 /*
956  * Mark a relation as proven empty.
957  *
958  * During GEQO planning, this can get invoked more than once on the same
959  * baserel struct, so it's worth checking to see if the rel is already marked
960  * dummy.
961  *
962  * Also, when called during GEQO join planning, we are in a short-lived
963  * memory context.  We must make sure that the dummy path attached to a
964  * baserel survives the GEQO cycle, else the baserel is trashed for future
965  * GEQO cycles.  On the other hand, when we are marking a joinrel during GEQO,
966  * we don't want the dummy path to clutter the main planning context.  Upshot
967  * is that the best solution is to explicitly make the dummy path in the same
968  * context the given RelOptInfo is in.
969  */
970 void
971 mark_dummy_rel(RelOptInfo *rel)
972 {
973         MemoryContext oldcontext;
974
975         /* Already marked? */
976         if (is_dummy_rel(rel))
977                 return;
978
979         /* No, so choose correct context to make the dummy path in */
980         oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
981
982         /* Set dummy size estimate */
983         rel->rows = 0;
984
985         /* Evict any previously chosen paths */
986         rel->pathlist = NIL;
987         rel->partial_pathlist = NIL;
988
989         /* Set up the dummy path */
990         add_path(rel, (Path *) create_append_path(NULL, rel, NIL, NIL, NULL,
991                                                                                           0, false, NIL, -1));
992
993         /* Set or update cheapest_total_path and related fields */
994         set_cheapest(rel);
995
996         MemoryContextSwitchTo(oldcontext);
997 }
998
999
1000 /*
1001  * restriction_is_constant_false --- is a restrictlist just FALSE?
1002  *
1003  * In cases where a qual is provably constant FALSE, eval_const_expressions
1004  * will generally have thrown away anything that's ANDed with it.  In outer
1005  * join situations this will leave us computing cartesian products only to
1006  * decide there's no match for an outer row, which is pretty stupid.  So,
1007  * we need to detect the case.
1008  *
1009  * If only_pushed_down is true, then consider only quals that are pushed-down
1010  * from the point of view of the joinrel.
1011  */
1012 static bool
1013 restriction_is_constant_false(List *restrictlist,
1014                                                           RelOptInfo *joinrel,
1015                                                           bool only_pushed_down)
1016 {
1017         ListCell   *lc;
1018
1019         /*
1020          * Despite the above comment, the restriction list we see here might
1021          * possibly have other members besides the FALSE constant, since other
1022          * quals could get "pushed down" to the outer join level.  So we check
1023          * each member of the list.
1024          */
1025         foreach(lc, restrictlist)
1026         {
1027                 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
1028
1029                 if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
1030                         continue;
1031
1032                 if (rinfo->clause && IsA(rinfo->clause, Const))
1033                 {
1034                         Const      *con = (Const *) rinfo->clause;
1035
1036                         /* constant NULL is as good as constant FALSE for our purposes */
1037                         if (con->constisnull)
1038                                 return true;
1039                         if (!DatumGetBool(con->constvalue))
1040                                 return true;
1041                 }
1042         }
1043         return false;
1044 }
1045
1046
1047 /*
1048  * Set up tlist expressions for the childrel, and add EC members referencing
1049  * the childrel.
1050  */
1051 static void
1052 update_child_rel_info(PlannerInfo *root,
1053                                           RelOptInfo *rel, RelOptInfo *childrel)
1054 {
1055         AppendRelInfo *appinfo = root->append_rel_array[childrel->relid];
1056
1057         /* Make child tlist expressions */
1058         childrel->reltarget->exprs = (List *)
1059                 adjust_appendrel_attrs(root,
1060                                                            (Node *) rel->reltarget->exprs,
1061                                                            1, &appinfo);
1062
1063         /* Make child entries in the EquivalenceClass as well */
1064         if (rel->has_eclass_joins || has_useful_pathkeys(root, rel))
1065                 add_child_rel_equivalences(root, appinfo, rel, childrel);
1066         childrel->has_eclass_joins = rel->has_eclass_joins;
1067 }
1068
1069 /*
1070  * Construct the SpecialJoinInfo for a child-join by translating
1071  * SpecialJoinInfo for the join between parents. left_relids and right_relids
1072  * are the relids of left and right side of the join respectively.
1073  */
1074 static SpecialJoinInfo *
1075 build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
1076                                                 Relids left_relids, Relids right_relids)
1077 {
1078         SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
1079         AppendRelInfo **left_appinfos;
1080         int                     left_nappinfos;
1081         AppendRelInfo **right_appinfos;
1082         int                     right_nappinfos;
1083
1084         memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1085         left_appinfos = find_appinfos_by_relids(root, left_relids,
1086                                                                                         &left_nappinfos);
1087         right_appinfos = find_appinfos_by_relids(root, right_relids,
1088                                                                                          &right_nappinfos);
1089
1090         sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1091                                                                                            left_nappinfos, left_appinfos);
1092         sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
1093                                                                                                 right_nappinfos,
1094                                                                                                 right_appinfos);
1095         sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1096                                                                                            left_nappinfos, left_appinfos);
1097         sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
1098                                                                                                 right_nappinfos,
1099                                                                                                 right_appinfos);
1100         sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1101                                                                                                                          (Node *) sjinfo->semi_rhs_exprs,
1102                                                                                                                          right_nappinfos,
1103                                                                                                                          right_appinfos);
1104
1105         pfree(left_appinfos);
1106         pfree(right_appinfos);
1107
1108         return sjinfo;
1109 }
1110
1111 /*
1112  * Assess whether join between given two partitioned relations can be broken
1113  * down into joins between matching partitions; a technique called
1114  * "partitionwise join"
1115  *
1116  * Partitionwise join is possible when a. Joining relations have same
1117  * partitioning scheme b. There exists an equi-join between the partition keys
1118  * of the two relations.
1119  *
1120  * Partitionwise join is planned as follows (details: optimizer/README.)
1121  *
1122  * 1. Create the RelOptInfos for joins between matching partitions i.e
1123  * child-joins and add paths to them.
1124  *
1125  * 2. Construct Append or MergeAppend paths across the set of child joins.
1126  * This second phase is implemented by generate_partitionwise_join_paths().
1127  *
1128  * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
1129  * obtained by translating the respective parent join structures.
1130  */
1131 static void
1132 try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
1133                                            RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
1134                                            List *parent_restrictlist)
1135 {
1136         bool            rel1_is_simple = IS_SIMPLE_REL(rel1);
1137         bool            rel2_is_simple = IS_SIMPLE_REL(rel2);
1138         int                     nparts;
1139         int                     cnt_parts;
1140
1141         /* Guard against stack overflow due to overly deep partition hierarchy. */
1142         check_stack_depth();
1143
1144         /* Nothing to do, if the join relation is not partitioned. */
1145         if (!IS_PARTITIONED_REL(joinrel))
1146                 return;
1147
1148         /* The join relation should have consider_partitionwise_join set. */
1149         Assert(joinrel->consider_partitionwise_join);
1150
1151         /*
1152          * Since this join relation is partitioned, all the base relations
1153          * participating in this join must be partitioned and so are all the
1154          * intermediate join relations.
1155          */
1156         Assert(IS_PARTITIONED_REL(rel1) && IS_PARTITIONED_REL(rel2));
1157         Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
1158
1159         /* The joining relations should have consider_partitionwise_join set. */
1160         Assert(rel1->consider_partitionwise_join &&
1161                    rel2->consider_partitionwise_join);
1162
1163         /*
1164          * The partition scheme of the join relation should match that of the
1165          * joining relations.
1166          */
1167         Assert(joinrel->part_scheme == rel1->part_scheme &&
1168                    joinrel->part_scheme == rel2->part_scheme);
1169
1170         /*
1171          * Since we allow partitionwise join only when the partition bounds of the
1172          * joining relations exactly match, the partition bounds of the join
1173          * should match those of the joining relations.
1174          */
1175         Assert(partition_bounds_equal(joinrel->part_scheme->partnatts,
1176                                                                   joinrel->part_scheme->parttyplen,
1177                                                                   joinrel->part_scheme->parttypbyval,
1178                                                                   joinrel->boundinfo, rel1->boundinfo));
1179         Assert(partition_bounds_equal(joinrel->part_scheme->partnatts,
1180                                                                   joinrel->part_scheme->parttyplen,
1181                                                                   joinrel->part_scheme->parttypbyval,
1182                                                                   joinrel->boundinfo, rel2->boundinfo));
1183
1184         nparts = joinrel->nparts;
1185
1186         /*
1187          * Create child-join relations for this partitioned join, if those don't
1188          * exist. Add paths to child-joins for a pair of child relations
1189          * corresponding to the given pair of parent relations.
1190          */
1191         for (cnt_parts = 0; cnt_parts < nparts; cnt_parts++)
1192         {
1193                 RelOptInfo *child_rel1 = rel1->part_rels[cnt_parts];
1194                 RelOptInfo *child_rel2 = rel2->part_rels[cnt_parts];
1195                 SpecialJoinInfo *child_sjinfo;
1196                 List       *child_restrictlist;
1197                 RelOptInfo *child_joinrel;
1198                 Relids          child_joinrelids;
1199                 AppendRelInfo **appinfos;
1200                 int                     nappinfos;
1201
1202                 /*
1203                  * If a child table has consider_partitionwise_join=false, it means
1204                  * that it's a dummy relation for which we skipped setting up tlist
1205                  * expressions and adding EC members in set_append_rel_size(), so do
1206                  * that now for use later.
1207                  */
1208                 if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1209                 {
1210                         Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1211                         Assert(IS_DUMMY_REL(child_rel1));
1212                         update_child_rel_info(root, rel1, child_rel1);
1213                         child_rel1->consider_partitionwise_join = true;
1214                 }
1215                 if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1216                 {
1217                         Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1218                         Assert(IS_DUMMY_REL(child_rel2));
1219                         update_child_rel_info(root, rel2, child_rel2);
1220                         child_rel2->consider_partitionwise_join = true;
1221                 }
1222
1223                 /* We should never try to join two overlapping sets of rels. */
1224                 Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1225                 child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1226                 appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1227
1228                 /*
1229                  * Construct SpecialJoinInfo from parent join relations's
1230                  * SpecialJoinInfo.
1231                  */
1232                 child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1233                                                                                            child_rel1->relids,
1234                                                                                            child_rel2->relids);
1235
1236                 /*
1237                  * Construct restrictions applicable to the child join from those
1238                  * applicable to the parent join.
1239                  */
1240                 child_restrictlist =
1241                         (List *) adjust_appendrel_attrs(root,
1242                                                                                         (Node *) parent_restrictlist,
1243                                                                                         nappinfos, appinfos);
1244                 pfree(appinfos);
1245
1246                 child_joinrel = joinrel->part_rels[cnt_parts];
1247                 if (!child_joinrel)
1248                 {
1249                         child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1250                                                                                                  joinrel, child_restrictlist,
1251                                                                                                  child_sjinfo,
1252                                                                                                  child_sjinfo->jointype);
1253                         joinrel->part_rels[cnt_parts] = child_joinrel;
1254                 }
1255
1256                 Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1257
1258                 populate_joinrel_with_paths(root, child_rel1, child_rel2,
1259                                                                         child_joinrel, child_sjinfo,
1260                                                                         child_restrictlist);
1261         }
1262 }