OSDN Git Service

Stabilize regression test.
[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  *  public functions:
16  *     standard_join_search(): This funcion is not static. The reason for
17  *        including this function is make_rels_by_clause_joins. In order to
18  *        avoid generating apparently unwanted join combination, we decided to
19  *        change the behavior of make_join_rel, which is called under this
20  *        function.
21  *
22  *      static functions:
23  *         set_plain_rel_pathlist()
24  *         set_append_rel_pathlist()
25  *         create_plain_partial_paths()
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  *     restriction_is_constant_false()
39  *     build_child_join_sjinfo()
40  *     get_matching_part_pairs()
41  *     compute_partition_bounds()
42  *     try_partitionwise_join()
43  *
44  * Portions Copyright (c) 1996-2020, 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                                                         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_useful_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 /*
270  * create_plain_partial_paths
271  *        Build partial access paths for parallel scan of a plain relation
272  */
273 static void
274 create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
275 {
276         int                     parallel_workers;
277
278         parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
279                                                                                            max_parallel_workers_per_gather);
280
281         /* If any limit was set to zero, the user doesn't want a parallel scan. */
282         if (parallel_workers <= 0)
283                 return;
284
285         /* Add an unordered partial path based on a parallel sequential scan. */
286         add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
287 }
288
289
290 /*
291  * join_search_one_level
292  *        Consider ways to produce join relations containing exactly 'level'
293  *        jointree items.  (This is one step of the dynamic-programming method
294  *        embodied in standard_join_search.)  Join rel nodes for each feasible
295  *        combination of lower-level rels are created and returned in a list.
296  *        Implementation paths are created for each such joinrel, too.
297  *
298  * level: level of rels we want to make this time
299  * root->join_rel_level[j], 1 <= j < level, is a list of rels containing j items
300  *
301  * The result is returned in root->join_rel_level[level].
302  */
303 void
304 join_search_one_level(PlannerInfo *root, int level)
305 {
306         List      **joinrels = root->join_rel_level;
307         ListCell   *r;
308         int                     k;
309
310         Assert(joinrels[level] == NIL);
311
312         /* Set join_cur_level so that new joinrels are added to proper list */
313         root->join_cur_level = level;
314
315         /*
316          * First, consider left-sided and right-sided plans, in which rels of
317          * exactly level-1 member relations are joined against initial relations.
318          * We prefer to join using join clauses, but if we find a rel of level-1
319          * members that has no join clauses, we will generate Cartesian-product
320          * joins against all initial rels not already contained in it.
321          */
322         foreach(r, joinrels[level - 1])
323         {
324                 RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
325
326                 if (old_rel->joininfo != NIL || old_rel->has_eclass_joins ||
327                         has_join_restriction(root, old_rel))
328                 {
329                         /*
330                          * There are join clauses or join order restrictions relevant to
331                          * this rel, so consider joins between this rel and (only) those
332                          * initial rels it is linked to by a clause or restriction.
333                          *
334                          * At level 2 this condition is symmetric, so there is no need to
335                          * look at initial rels before this one in the list; we already
336                          * considered such joins when we were at the earlier rel.  (The
337                          * mirror-image joins are handled automatically by make_join_rel.)
338                          * In later passes (level > 2), we join rels of the previous level
339                          * to each initial rel they don't already include but have a join
340                          * clause or restriction with.
341                          */
342                         List       *other_rels_list;
343                         ListCell   *other_rels;
344
345                         if (level == 2)         /* consider remaining initial rels */
346                         {
347                                 other_rels_list = joinrels[level - 1];
348                                 other_rels = lnext(other_rels_list, r);
349                         }
350                         else                            /* consider all initial rels */
351                         {
352                                 other_rels_list = joinrels[1];
353                                 other_rels = list_head(other_rels_list);
354                         }
355
356                         make_rels_by_clause_joins(root,
357                                                                           old_rel,
358                                                                           other_rels_list,
359                                                                           other_rels);
360                 }
361                 else
362                 {
363                         /*
364                          * Oops, we have a relation that is not joined to any other
365                          * relation, either directly or by join-order restrictions.
366                          * Cartesian product time.
367                          *
368                          * We consider a cartesian product with each not-already-included
369                          * initial rel, whether it has other join clauses or not.  At
370                          * level 2, if there are two or more clauseless initial rels, we
371                          * will redundantly consider joining them in both directions; but
372                          * such cases aren't common enough to justify adding complexity to
373                          * avoid the duplicated effort.
374                          */
375                         make_rels_by_clauseless_joins(root,
376                                                                                   old_rel,
377                                                                                   joinrels[1]);
378                 }
379         }
380
381         /*
382          * Now, consider "bushy plans" in which relations of k initial rels are
383          * joined to relations of level-k initial rels, for 2 <= k <= level-2.
384          *
385          * We only consider bushy-plan joins for pairs of rels where there is a
386          * suitable join clause (or join order restriction), in order to avoid
387          * unreasonable growth of planning time.
388          */
389         for (k = 2;; k++)
390         {
391                 int                     other_level = level - k;
392
393                 /*
394                  * Since make_join_rel(x, y) handles both x,y and y,x cases, we only
395                  * need to go as far as the halfway point.
396                  */
397                 if (k > other_level)
398                         break;
399
400                 foreach(r, joinrels[k])
401                 {
402                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
403                         List       *other_rels_list;
404                         ListCell   *other_rels;
405                         ListCell   *r2;
406
407                         /*
408                          * We can ignore relations without join clauses here, unless they
409                          * participate in join-order restrictions --- then we might have
410                          * to force a bushy join plan.
411                          */
412                         if (old_rel->joininfo == NIL && !old_rel->has_eclass_joins &&
413                                 !has_join_restriction(root, old_rel))
414                                 continue;
415
416                         if (k == other_level)
417                         {
418                                 /* only consider remaining rels */
419                                 other_rels_list = joinrels[k];
420                                 other_rels = lnext(other_rels_list, r);
421                         }
422                         else
423                         {
424                                 other_rels_list = joinrels[other_level];
425                                 other_rels = list_head(other_rels_list);
426                         }
427
428                         for_each_cell(r2, other_rels_list, other_rels)
429                         {
430                                 RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
431
432                                 if (!bms_overlap(old_rel->relids, new_rel->relids))
433                                 {
434                                         /*
435                                          * OK, we can build a rel of the right level from this
436                                          * pair of rels.  Do so if there is at least one relevant
437                                          * join clause or join order restriction.
438                                          */
439                                         if (have_relevant_joinclause(root, old_rel, new_rel) ||
440                                                 have_join_order_restriction(root, old_rel, new_rel))
441                                         {
442                                                 (void) make_join_rel(root, old_rel, new_rel);
443                                         }
444                                 }
445                         }
446                 }
447         }
448
449         /*----------
450          * Last-ditch effort: if we failed to find any usable joins so far, force
451          * a set of cartesian-product joins to be generated.  This handles the
452          * special case where all the available rels have join clauses but we
453          * cannot use any of those clauses yet.  This can only happen when we are
454          * considering a join sub-problem (a sub-joinlist) and all the rels in the
455          * sub-problem have only join clauses with rels outside the sub-problem.
456          * An example is
457          *
458          *              SELECT ... FROM a INNER JOIN b ON TRUE, c, d, ...
459          *              WHERE a.w = c.x and b.y = d.z;
460          *
461          * If the "a INNER JOIN b" sub-problem does not get flattened into the
462          * upper level, we must be willing to make a cartesian join of a and b;
463          * but the code above will not have done so, because it thought that both
464          * a and b have joinclauses.  We consider only left-sided and right-sided
465          * cartesian joins in this case (no bushy).
466          *----------
467          */
468         if (joinrels[level] == NIL)
469         {
470                 /*
471                  * This loop is just like the first one, except we always call
472                  * make_rels_by_clauseless_joins().
473                  */
474                 foreach(r, joinrels[level - 1])
475                 {
476                         RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
477
478                         make_rels_by_clauseless_joins(root,
479                                                                                   old_rel,
480                                                                                   joinrels[1]);
481                 }
482
483                 /*----------
484                  * When special joins are involved, there may be no legal way
485                  * to make an N-way join for some values of N.  For example consider
486                  *
487                  * SELECT ... FROM t1 WHERE
488                  *       x IN (SELECT ... FROM t2,t3 WHERE ...) AND
489                  *       y IN (SELECT ... FROM t4,t5 WHERE ...)
490                  *
491                  * We will flatten this query to a 5-way join problem, but there are
492                  * no 4-way joins that join_is_legal() will consider legal.  We have
493                  * to accept failure at level 4 and go on to discover a workable
494                  * bushy plan at level 5.
495                  *
496                  * However, if there are no special joins and no lateral references
497                  * then join_is_legal() should never fail, and so the following sanity
498                  * check is useful.
499                  *----------
500                  */
501                 if (joinrels[level] == NIL &&
502                         root->join_info_list == NIL &&
503                         !root->hasLateralRTEs)
504                         elog(ERROR, "failed to build any %d-way joins", level);
505         }
506 }
507
508
509 /*
510  * make_rels_by_clause_joins
511  *        Build joins between the given relation 'old_rel' and other relations
512  *        that participate in join clauses that 'old_rel' also participates in
513  *        (or participate in join-order restrictions with it).
514  *        The join rels are returned in root->join_rel_level[join_cur_level].
515  *
516  * Note: at levels above 2 we will generate the same joined relation in
517  * multiple ways --- for example (a join b) join c is the same RelOptInfo as
518  * (b join c) join a, though the second case will add a different set of Paths
519  * to it.  This is the reason for using the join_rel_level mechanism, which
520  * automatically ensures that each new joinrel is only added to the list once.
521  *
522  * 'old_rel' is the relation entry for the relation to be joined
523  * 'other_rels_list': a list containing the other
524  * rels to be considered for joining
525  * 'other_rels': the first cell to be considered
526  *
527  * Currently, this is only used with initial rels in other_rels, but it
528  * will work for joining to joinrels too.
529  */
530 static void
531 make_rels_by_clause_joins(PlannerInfo *root,
532                                                   RelOptInfo *old_rel,
533                                                   List *other_rels_list,
534                                                   ListCell *other_rels)
535 {
536         ListCell   *l;
537
538         for_each_cell(l, other_rels_list, other_rels)
539         {
540                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
541
542                 if (!bms_overlap(old_rel->relids, other_rel->relids) &&
543                         (have_relevant_joinclause(root, old_rel, other_rel) ||
544                          have_join_order_restriction(root, old_rel, other_rel)))
545                 {
546                         (void) make_join_rel(root, old_rel, other_rel);
547                 }
548         }
549 }
550
551
552 /*
553  * make_rels_by_clauseless_joins
554  *        Given a relation 'old_rel' and a list of other relations
555  *        'other_rels', create a join relation between 'old_rel' and each
556  *        member of 'other_rels' that isn't already included in 'old_rel'.
557  *        The join rels are returned in root->join_rel_level[join_cur_level].
558  *
559  * 'old_rel' is the relation entry for the relation to be joined
560  * 'other_rels': a list containing the other rels to be considered for joining
561  *
562  * Currently, this is only used with initial rels in other_rels, but it would
563  * work for joining to joinrels too.
564  */
565 static void
566 make_rels_by_clauseless_joins(PlannerInfo *root,
567                                                           RelOptInfo *old_rel,
568                                                           List *other_rels)
569 {
570         ListCell   *l;
571
572         foreach(l, other_rels)
573         {
574                 RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
575
576                 if (!bms_overlap(other_rel->relids, old_rel->relids))
577                 {
578                         (void) make_join_rel(root, old_rel, other_rel);
579                 }
580         }
581 }
582
583
584 /*
585  * join_is_legal
586  *         Determine whether a proposed join is legal given the query's
587  *         join order constraints; and if it is, determine the join type.
588  *
589  * Caller must supply not only the two rels, but the union of their relids.
590  * (We could simplify the API by computing joinrelids locally, but this
591  * would be redundant work in the normal path through make_join_rel.)
592  *
593  * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
594  * else it's set to point to the associated SpecialJoinInfo node.  Also,
595  * *reversed_p is set true if the given relations need to be swapped to
596  * match the SpecialJoinInfo node.
597  */
598 static bool
599 join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
600                           Relids joinrelids,
601                           SpecialJoinInfo **sjinfo_p, bool *reversed_p)
602 {
603         SpecialJoinInfo *match_sjinfo;
604         bool            reversed;
605         bool            unique_ified;
606         bool            must_be_leftjoin;
607         ListCell   *l;
608
609         /*
610          * Ensure output params are set on failure return.  This is just to
611          * suppress uninitialized-variable warnings from overly anal compilers.
612          */
613         *sjinfo_p = NULL;
614         *reversed_p = false;
615
616         /*
617          * If we have any special joins, the proposed join might be illegal; and
618          * in any case we have to determine its join type.  Scan the join info
619          * list for matches and conflicts.
620          */
621         match_sjinfo = NULL;
622         reversed = false;
623         unique_ified = false;
624         must_be_leftjoin = false;
625
626         foreach(l, root->join_info_list)
627         {
628                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
629
630                 /*
631                  * This special join is not relevant unless its RHS overlaps the
632                  * proposed join.  (Check this first as a fast path for dismissing
633                  * most irrelevant SJs quickly.)
634                  */
635                 if (!bms_overlap(sjinfo->min_righthand, joinrelids))
636                         continue;
637
638                 /*
639                  * Also, not relevant if proposed join is fully contained within RHS
640                  * (ie, we're still building up the RHS).
641                  */
642                 if (bms_is_subset(joinrelids, sjinfo->min_righthand))
643                         continue;
644
645                 /*
646                  * Also, not relevant if SJ is already done within either input.
647                  */
648                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
649                         bms_is_subset(sjinfo->min_righthand, rel1->relids))
650                         continue;
651                 if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
652                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
653                         continue;
654
655                 /*
656                  * If it's a semijoin and we already joined the RHS to any other rels
657                  * within either input, then we must have unique-ified the RHS at that
658                  * point (see below).  Therefore the semijoin is no longer relevant in
659                  * this join path.
660                  */
661                 if (sjinfo->jointype == JOIN_SEMI)
662                 {
663                         if (bms_is_subset(sjinfo->syn_righthand, rel1->relids) &&
664                                 !bms_equal(sjinfo->syn_righthand, rel1->relids))
665                                 continue;
666                         if (bms_is_subset(sjinfo->syn_righthand, rel2->relids) &&
667                                 !bms_equal(sjinfo->syn_righthand, rel2->relids))
668                                 continue;
669                 }
670
671                 /*
672                  * If one input contains min_lefthand and the other contains
673                  * min_righthand, then we can perform the SJ at this join.
674                  *
675                  * Reject if we get matches to more than one SJ; that implies we're
676                  * considering something that's not really valid.
677                  */
678                 if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
679                         bms_is_subset(sjinfo->min_righthand, rel2->relids))
680                 {
681                         if (match_sjinfo)
682                                 return false;   /* invalid join path */
683                         match_sjinfo = sjinfo;
684                         reversed = false;
685                 }
686                 else if (bms_is_subset(sjinfo->min_lefthand, rel2->relids) &&
687                                  bms_is_subset(sjinfo->min_righthand, rel1->relids))
688                 {
689                         if (match_sjinfo)
690                                 return false;   /* invalid join path */
691                         match_sjinfo = sjinfo;
692                         reversed = true;
693                 }
694                 else if (sjinfo->jointype == JOIN_SEMI &&
695                                  bms_equal(sjinfo->syn_righthand, rel2->relids) &&
696                                  create_unique_path(root, rel2, rel2->cheapest_total_path,
697                                                                         sjinfo) != NULL)
698                 {
699                         /*----------
700                          * For a semijoin, we can join the RHS to anything else by
701                          * unique-ifying the RHS (if the RHS can be unique-ified).
702                          * We will only get here if we have the full RHS but less
703                          * than min_lefthand on the LHS.
704                          *
705                          * The reason to consider such a join path is exemplified by
706                          *      SELECT ... FROM a,b WHERE (a.x,b.y) IN (SELECT c1,c2 FROM c)
707                          * If we insist on doing this as a semijoin we will first have
708                          * to form the cartesian product of A*B.  But if we unique-ify
709                          * C then the semijoin becomes a plain innerjoin and we can join
710                          * in any order, eg C to A and then to B.  When C is much smaller
711                          * than A and B this can be a huge win.  So we allow C to be
712                          * joined to just A or just B here, and then make_join_rel has
713                          * to handle the case properly.
714                          *
715                          * Note that actually we'll allow unique-ified C to be joined to
716                          * some other relation D here, too.  That is legal, if usually not
717                          * very sane, and this routine is only concerned with legality not
718                          * with whether the join is good strategy.
719                          *----------
720                          */
721                         if (match_sjinfo)
722                                 return false;   /* invalid join path */
723                         match_sjinfo = sjinfo;
724                         reversed = false;
725                         unique_ified = true;
726                 }
727                 else if (sjinfo->jointype == JOIN_SEMI &&
728                                  bms_equal(sjinfo->syn_righthand, rel1->relids) &&
729                                  create_unique_path(root, rel1, rel1->cheapest_total_path,
730                                                                         sjinfo) != NULL)
731                 {
732                         /* Reversed semijoin case */
733                         if (match_sjinfo)
734                                 return false;   /* invalid join path */
735                         match_sjinfo = sjinfo;
736                         reversed = true;
737                         unique_ified = true;
738                 }
739                 else
740                 {
741                         /*
742                          * Otherwise, the proposed join overlaps the RHS but isn't a valid
743                          * implementation of this SJ.  But don't panic quite yet: the RHS
744                          * violation might have occurred previously, in one or both input
745                          * relations, in which case we must have previously decided that
746                          * it was OK to commute some other SJ with this one.  If we need
747                          * to perform this join to finish building up the RHS, rejecting
748                          * it could lead to not finding any plan at all.  (This can occur
749                          * because of the heuristics elsewhere in this file that postpone
750                          * clauseless joins: we might not consider doing a clauseless join
751                          * within the RHS until after we've performed other, validly
752                          * commutable SJs with one or both sides of the clauseless join.)
753                          * This consideration boils down to the rule that if both inputs
754                          * overlap the RHS, we can allow the join --- they are either
755                          * fully within the RHS, or represent previously-allowed joins to
756                          * rels outside it.
757                          */
758                         if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
759                                 bms_overlap(rel2->relids, sjinfo->min_righthand))
760                                 continue;               /* assume valid previous violation of RHS */
761
762                         /*
763                          * The proposed join could still be legal, but only if we're
764                          * allowed to associate it into the RHS of this SJ.  That means
765                          * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
766                          * not FULL) and the proposed join must not overlap the LHS.
767                          */
768                         if (sjinfo->jointype != JOIN_LEFT ||
769                                 bms_overlap(joinrelids, sjinfo->min_lefthand))
770                                 return false;   /* invalid join path */
771
772                         /*
773                          * To be valid, the proposed join must be a LEFT join; otherwise
774                          * it can't associate into this SJ's RHS.  But we may not yet have
775                          * found the SpecialJoinInfo matching the proposed join, so we
776                          * can't test that yet.  Remember the requirement for later.
777                          */
778                         must_be_leftjoin = true;
779                 }
780         }
781
782         /*
783          * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
784          * proposed join can't associate into an SJ's RHS.
785          *
786          * Also, fail if the proposed join's predicate isn't strict; we're
787          * essentially checking to see if we can apply outer-join identity 3, and
788          * that's a requirement.  (This check may be redundant with checks in
789          * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
790          */
791         if (must_be_leftjoin &&
792                 (match_sjinfo == NULL ||
793                  match_sjinfo->jointype != JOIN_LEFT ||
794                  !match_sjinfo->lhs_strict))
795                 return false;                   /* invalid join path */
796
797         /*
798          * We also have to check for constraints imposed by LATERAL references.
799          */
800         if (root->hasLateralRTEs)
801         {
802                 bool            lateral_fwd;
803                 bool            lateral_rev;
804                 Relids          join_lateral_rels;
805
806                 /*
807                  * The proposed rels could each contain lateral references to the
808                  * other, in which case the join is impossible.  If there are lateral
809                  * references in just one direction, then the join has to be done with
810                  * a nestloop with the lateral referencer on the inside.  If the join
811                  * matches an SJ that cannot be implemented by such a nestloop, the
812                  * join is impossible.
813                  *
814                  * Also, if the lateral reference is only indirect, we should reject
815                  * the join; whatever rel(s) the reference chain goes through must be
816                  * joined to first.
817                  *
818                  * Another case that might keep us from building a valid plan is the
819                  * implementation restriction described by have_dangerous_phv().
820                  */
821                 lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
822                 lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
823                 if (lateral_fwd && lateral_rev)
824                         return false;           /* have lateral refs in both directions */
825                 if (lateral_fwd)
826                 {
827                         /* has to be implemented as nestloop with rel1 on left */
828                         if (match_sjinfo &&
829                                 (reversed ||
830                                  unique_ified ||
831                                  match_sjinfo->jointype == JOIN_FULL))
832                                 return false;   /* not implementable as nestloop */
833                         /* check there is a direct reference from rel2 to rel1 */
834                         if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
835                                 return false;   /* only indirect refs, so reject */
836                         /* check we won't have a dangerous PHV */
837                         if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
838                                 return false;   /* might be unable to handle required PHV */
839                 }
840                 else if (lateral_rev)
841                 {
842                         /* has to be implemented as nestloop with rel2 on left */
843                         if (match_sjinfo &&
844                                 (!reversed ||
845                                  unique_ified ||
846                                  match_sjinfo->jointype == JOIN_FULL))
847                                 return false;   /* not implementable as nestloop */
848                         /* check there is a direct reference from rel1 to rel2 */
849                         if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
850                                 return false;   /* only indirect refs, so reject */
851                         /* check we won't have a dangerous PHV */
852                         if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
853                                 return false;   /* might be unable to handle required PHV */
854                 }
855
856                 /*
857                  * LATERAL references could also cause problems later on if we accept
858                  * this join: if the join's minimum parameterization includes any rels
859                  * that would have to be on the inside of an outer join with this join
860                  * rel, then it's never going to be possible to build the complete
861                  * query using this join.  We should reject this join not only because
862                  * it'll save work, but because if we don't, the clauseless-join
863                  * heuristics might think that legality of this join means that some
864                  * other join rel need not be formed, and that could lead to failure
865                  * to find any plan at all.  We have to consider not only rels that
866                  * are directly on the inner side of an OJ with the joinrel, but also
867                  * ones that are indirectly so, so search to find all such rels.
868                  */
869                 join_lateral_rels = min_join_parameterization(root, joinrelids,
870                                                                                                           rel1, rel2);
871                 if (join_lateral_rels)
872                 {
873                         Relids          join_plus_rhs = bms_copy(joinrelids);
874                         bool            more;
875
876                         do
877                         {
878                                 more = false;
879                                 foreach(l, root->join_info_list)
880                                 {
881                                         SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
882
883                                         /* ignore full joins --- their ordering is predetermined */
884                                         if (sjinfo->jointype == JOIN_FULL)
885                                                 continue;
886
887                                         if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
888                                                 !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
889                                         {
890                                                 join_plus_rhs = bms_add_members(join_plus_rhs,
891                                                                                                                 sjinfo->min_righthand);
892                                                 more = true;
893                                         }
894                                 }
895                         } while (more);
896                         if (bms_overlap(join_plus_rhs, join_lateral_rels))
897                                 return false;   /* will not be able to join to some RHS rel */
898                 }
899         }
900
901         /* Otherwise, it's a valid join */
902         *sjinfo_p = match_sjinfo;
903         *reversed_p = reversed;
904         return true;
905 }
906
907
908 /*
909  * has_join_restriction
910  *              Detect whether the specified relation has join-order restrictions,
911  *              due to being inside an outer join or an IN (sub-SELECT),
912  *              or participating in any LATERAL references or multi-rel PHVs.
913  *
914  * Essentially, this tests whether have_join_order_restriction() could
915  * succeed with this rel and some other one.  It's OK if we sometimes
916  * say "true" incorrectly.  (Therefore, we don't bother with the relatively
917  * expensive has_legal_joinclause test.)
918  */
919 static bool
920 has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
921 {
922         ListCell   *l;
923
924         if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
925                 return true;
926
927         foreach(l, root->placeholder_list)
928         {
929                 PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
930
931                 if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
932                         !bms_equal(rel->relids, phinfo->ph_eval_at))
933                         return true;
934         }
935
936         foreach(l, root->join_info_list)
937         {
938                 SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
939
940                 /* ignore full joins --- other mechanisms preserve their ordering */
941                 if (sjinfo->jointype == JOIN_FULL)
942                         continue;
943
944                 /* ignore if SJ is already contained in rel */
945                 if (bms_is_subset(sjinfo->min_lefthand, rel->relids) &&
946                         bms_is_subset(sjinfo->min_righthand, rel->relids))
947                         continue;
948
949                 /* restricted if it overlaps LHS or RHS, but doesn't contain SJ */
950                 if (bms_overlap(sjinfo->min_lefthand, rel->relids) ||
951                         bms_overlap(sjinfo->min_righthand, rel->relids))
952                         return true;
953         }
954
955         return false;
956 }
957
958
959 /*
960  * restriction_is_constant_false --- is a restrictlist just FALSE?
961  *
962  * In cases where a qual is provably constant FALSE, eval_const_expressions
963  * will generally have thrown away anything that's ANDed with it.  In outer
964  * join situations this will leave us computing cartesian products only to
965  * decide there's no match for an outer row, which is pretty stupid.  So,
966  * we need to detect the case.
967  *
968  * If only_pushed_down is true, then consider only quals that are pushed-down
969  * from the point of view of the joinrel.
970  */
971 static bool
972 restriction_is_constant_false(List *restrictlist,
973                                                           RelOptInfo *joinrel,
974                                                           bool only_pushed_down)
975 {
976         ListCell   *lc;
977
978         /*
979          * Despite the above comment, the restriction list we see here might
980          * possibly have other members besides the FALSE constant, since other
981          * quals could get "pushed down" to the outer join level.  So we check
982          * each member of the list.
983          */
984         foreach(lc, restrictlist)
985         {
986                 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
987
988                 if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
989                         continue;
990
991                 if (rinfo->clause && IsA(rinfo->clause, Const))
992                 {
993                         Const      *con = (Const *) rinfo->clause;
994
995                         /* constant NULL is as good as constant FALSE for our purposes */
996                         if (con->constisnull)
997                                 return true;
998                         if (!DatumGetBool(con->constvalue))
999                                 return true;
1000                 }
1001         }
1002         return false;
1003 }
1004
1005
1006 /*
1007  * Construct the SpecialJoinInfo for a child-join by translating
1008  * SpecialJoinInfo for the join between parents. left_relids and right_relids
1009  * are the relids of left and right side of the join respectively.
1010  */
1011 static SpecialJoinInfo *
1012 build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
1013                                                 Relids left_relids, Relids right_relids)
1014 {
1015         SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
1016         AppendRelInfo **left_appinfos;
1017         int                     left_nappinfos;
1018         AppendRelInfo **right_appinfos;
1019         int                     right_nappinfos;
1020
1021         memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1022         left_appinfos = find_appinfos_by_relids(root, left_relids,
1023                                                                                         &left_nappinfos);
1024         right_appinfos = find_appinfos_by_relids(root, right_relids,
1025                                                                                          &right_nappinfos);
1026
1027         sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1028                                                                                            left_nappinfos, left_appinfos);
1029         sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
1030                                                                                                 right_nappinfos,
1031                                                                                                 right_appinfos);
1032         sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1033                                                                                            left_nappinfos, left_appinfos);
1034         sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
1035                                                                                                 right_nappinfos,
1036                                                                                                 right_appinfos);
1037         sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1038                                                                                                                          (Node *) sjinfo->semi_rhs_exprs,
1039                                                                                                                          right_nappinfos,
1040                                                                                                                          right_appinfos);
1041
1042         pfree(left_appinfos);
1043         pfree(right_appinfos);
1044
1045         return sjinfo;
1046 }
1047
1048
1049 /*
1050  * get_matching_part_pairs
1051  *              Generate pairs of partitions to be joined from inputs
1052  */
1053 static void
1054 get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
1055                                                 RelOptInfo *rel1, RelOptInfo *rel2,
1056                                                 List **parts1, List **parts2)
1057 {
1058         bool            rel1_is_simple = IS_SIMPLE_REL(rel1);
1059         bool            rel2_is_simple = IS_SIMPLE_REL(rel2);
1060         int                     cnt_parts;
1061
1062         *parts1 = NIL;
1063         *parts2 = NIL;
1064
1065         for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1066         {
1067                 RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1068                 RelOptInfo *child_rel1;
1069                 RelOptInfo *child_rel2;
1070                 Relids          child_relids1;
1071                 Relids          child_relids2;
1072
1073                 /*
1074                  * If this segment of the join is empty, it means that this segment
1075                  * was ignored when previously creating child-join paths for it in
1076                  * try_partitionwise_join() as it would not contribute to the join
1077                  * result, due to one or both inputs being empty; add NULL to each of
1078                  * the given lists so that this segment will be ignored again in that
1079                  * function.
1080                  */
1081                 if (!child_joinrel)
1082                 {
1083                         *parts1 = lappend(*parts1, NULL);
1084                         *parts2 = lappend(*parts2, NULL);
1085                         continue;
1086                 }
1087
1088                 /*
1089                  * Get a relids set of partition(s) involved in this join segment that
1090                  * are from the rel1 side.
1091                  */
1092                 child_relids1 = bms_intersect(child_joinrel->relids,
1093                                                                           rel1->all_partrels);
1094                 Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1095
1096                 /*
1097                  * Get a child rel for rel1 with the relids.  Note that we should have
1098                  * the child rel even if rel1 is a join rel, because in that case the
1099                  * partitions specified in the relids would have matching/overlapping
1100                  * boundaries, so the specified partitions should be considered as
1101                  * ones to be joined when planning partitionwise joins of rel1,
1102                  * meaning that the child rel would have been built by the time we get
1103                  * here.
1104                  */
1105                 if (rel1_is_simple)
1106                 {
1107                         int                     varno = bms_singleton_member(child_relids1);
1108
1109                         child_rel1 = find_base_rel(root, varno);
1110                 }
1111                 else
1112                         child_rel1 = find_join_rel(root, child_relids1);
1113                 Assert(child_rel1);
1114
1115                 /*
1116                  * Get a relids set of partition(s) involved in this join segment that
1117                  * are from the rel2 side.
1118                  */
1119                 child_relids2 = bms_intersect(child_joinrel->relids,
1120                                                                           rel2->all_partrels);
1121                 Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1122
1123                 /*
1124                  * Get a child rel for rel2 with the relids.  See above comments.
1125                  */
1126                 if (rel2_is_simple)
1127                 {
1128                         int                     varno = bms_singleton_member(child_relids2);
1129
1130                         child_rel2 = find_base_rel(root, varno);
1131                 }
1132                 else
1133                         child_rel2 = find_join_rel(root, child_relids2);
1134                 Assert(child_rel2);
1135
1136                 /*
1137                  * The join of rel1 and rel2 is legal, so is the join of the child
1138                  * rels obtained above; add them to the given lists as a join pair
1139                  * producing this join segment.
1140                  */
1141                 *parts1 = lappend(*parts1, child_rel1);
1142                 *parts2 = lappend(*parts2, child_rel2);
1143         }
1144 }
1145
1146
1147 /*
1148  * compute_partition_bounds
1149  *              Compute the partition bounds for a join rel from those for inputs
1150  */
1151 static void
1152 compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
1153                                                  RelOptInfo *rel2, RelOptInfo *joinrel,
1154                                                  SpecialJoinInfo *parent_sjinfo,
1155                                                  List **parts1, List **parts2)
1156 {
1157         /*
1158          * If we don't have the partition bounds for the join rel yet, try to
1159          * compute those along with pairs of partitions to be joined.
1160          */
1161         if (joinrel->nparts == -1)
1162         {
1163                 PartitionScheme part_scheme = joinrel->part_scheme;
1164                 PartitionBoundInfo boundinfo = NULL;
1165                 int                     nparts = 0;
1166
1167                 Assert(joinrel->boundinfo == NULL);
1168                 Assert(joinrel->part_rels == NULL);
1169
1170                 /*
1171                  * See if the partition bounds for inputs are exactly the same, in
1172                  * which case we don't need to work hard: the join rel have the same
1173                  * partition bounds as inputs, and the partitions with the same
1174                  * cardinal positions form the pairs.
1175                  *
1176                  * Note: even in cases where one or both inputs have merged bounds, it
1177                  * would be possible for both the bounds to be exactly the same, but
1178                  * it seems unlikely to be worth the cycles to check.
1179                  */
1180                 if (!rel1->partbounds_merged &&
1181                         !rel2->partbounds_merged &&
1182                         rel1->nparts == rel2->nparts &&
1183                         partition_bounds_equal(part_scheme->partnatts,
1184                                                                    part_scheme->parttyplen,
1185                                                                    part_scheme->parttypbyval,
1186                                                                    rel1->boundinfo, rel2->boundinfo))
1187                 {
1188                         boundinfo = rel1->boundinfo;
1189                         nparts = rel1->nparts;
1190                 }
1191                 else
1192                 {
1193                         /* Try merging the partition bounds for inputs. */
1194                         boundinfo = partition_bounds_merge(part_scheme->partnatts,
1195                                                                                            part_scheme->partsupfunc,
1196                                                                                            part_scheme->partcollation,
1197                                                                                            rel1, rel2,
1198                                                                                            parent_sjinfo->jointype,
1199                                                                                            parts1, parts2);
1200                         if (boundinfo == NULL)
1201                         {
1202                                 joinrel->nparts = 0;
1203                                 return;
1204                         }
1205                         nparts = list_length(*parts1);
1206                         joinrel->partbounds_merged = true;
1207                 }
1208
1209                 Assert(nparts > 0);
1210                 joinrel->boundinfo = boundinfo;
1211                 joinrel->nparts = nparts;
1212                 joinrel->part_rels =
1213                         (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1214         }
1215         else
1216         {
1217                 Assert(joinrel->nparts > 0);
1218                 Assert(joinrel->boundinfo);
1219                 Assert(joinrel->part_rels);
1220
1221                 /*
1222                  * If the join rel's partbounds_merged flag is true, it means inputs
1223                  * are not guaranteed to have the same partition bounds, therefore we
1224                  * can't assume that the partitions at the same cardinal positions
1225                  * form the pairs; let get_matching_part_pairs() generate the pairs.
1226                  * Otherwise, nothing to do since we can assume that.
1227                  */
1228                 if (joinrel->partbounds_merged)
1229                 {
1230                         get_matching_part_pairs(root, joinrel, rel1, rel2,
1231                                                                         parts1, parts2);
1232                         Assert(list_length(*parts1) == joinrel->nparts);
1233                         Assert(list_length(*parts2) == joinrel->nparts);
1234                 }
1235         }
1236 }
1237
1238
1239 /*
1240  * Assess whether join between given two partitioned relations can be broken
1241  * down into joins between matching partitions; a technique called
1242  * "partitionwise join"
1243  *
1244  * Partitionwise join is possible when a. Joining relations have same
1245  * partitioning scheme b. There exists an equi-join between the partition keys
1246  * of the two relations.
1247  *
1248  * Partitionwise join is planned as follows (details: optimizer/README.)
1249  *
1250  * 1. Create the RelOptInfos for joins between matching partitions i.e
1251  * child-joins and add paths to them.
1252  *
1253  * 2. Construct Append or MergeAppend paths across the set of child joins.
1254  * This second phase is implemented by generate_partitionwise_join_paths().
1255  *
1256  * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
1257  * obtained by translating the respective parent join structures.
1258  */
1259 static void
1260 try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
1261                                            RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
1262                                            List *parent_restrictlist)
1263 {
1264         bool            rel1_is_simple = IS_SIMPLE_REL(rel1);
1265         bool            rel2_is_simple = IS_SIMPLE_REL(rel2);
1266         List       *parts1 = NIL;
1267         List       *parts2 = NIL;
1268         ListCell   *lcr1 = NULL;
1269         ListCell   *lcr2 = NULL;
1270         int                     cnt_parts;
1271
1272         /* Guard against stack overflow due to overly deep partition hierarchy. */
1273         check_stack_depth();
1274
1275         /* Nothing to do, if the join relation is not partitioned. */
1276         if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1277                 return;
1278
1279         /* The join relation should have consider_partitionwise_join set. */
1280         Assert(joinrel->consider_partitionwise_join);
1281
1282         /*
1283          * We can not perform partitionwise join if either of the joining
1284          * relations is not partitioned.
1285          */
1286         if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1287                 return;
1288
1289         Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
1290
1291         /* The joining relations should have consider_partitionwise_join set. */
1292         Assert(rel1->consider_partitionwise_join &&
1293                    rel2->consider_partitionwise_join);
1294
1295         /*
1296          * The partition scheme of the join relation should match that of the
1297          * joining relations.
1298          */
1299         Assert(joinrel->part_scheme == rel1->part_scheme &&
1300                    joinrel->part_scheme == rel2->part_scheme);
1301
1302         Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1303
1304         compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1305                                                          &parts1, &parts2);
1306
1307         if (joinrel->partbounds_merged)
1308         {
1309                 lcr1 = list_head(parts1);
1310                 lcr2 = list_head(parts2);
1311         }
1312
1313         /*
1314          * Create child-join relations for this partitioned join, if those don't
1315          * exist. Add paths to child-joins for a pair of child relations
1316          * corresponding to the given pair of parent relations.
1317          */
1318         for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1319         {
1320                 RelOptInfo *child_rel1;
1321                 RelOptInfo *child_rel2;
1322                 bool            rel1_empty;
1323                 bool            rel2_empty;
1324                 SpecialJoinInfo *child_sjinfo;
1325                 List       *child_restrictlist;
1326                 RelOptInfo *child_joinrel;
1327                 Relids          child_joinrelids;
1328                 AppendRelInfo **appinfos;
1329                 int                     nappinfos;
1330
1331                 if (joinrel->partbounds_merged)
1332                 {
1333                         child_rel1 = lfirst_node(RelOptInfo, lcr1);
1334                         child_rel2 = lfirst_node(RelOptInfo, lcr2);
1335                         lcr1 = lnext(parts1, lcr1);
1336                         lcr2 = lnext(parts2, lcr2);
1337                 }
1338                 else
1339                 {
1340                         child_rel1 = rel1->part_rels[cnt_parts];
1341                         child_rel2 = rel2->part_rels[cnt_parts];
1342                 }
1343
1344                 rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1345                 rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1346
1347                 /*
1348                  * Check for cases where we can prove that this segment of the join
1349                  * returns no rows, due to one or both inputs being empty (including
1350                  * inputs that have been pruned away entirely).  If so just ignore it.
1351                  * These rules are equivalent to populate_joinrel_with_paths's rules
1352                  * for dummy input relations.
1353                  */
1354                 switch (parent_sjinfo->jointype)
1355                 {
1356                         case JOIN_INNER:
1357                         case JOIN_SEMI:
1358                                 if (rel1_empty || rel2_empty)
1359                                         continue;       /* ignore this join segment */
1360                                 break;
1361                         case JOIN_LEFT:
1362                         case JOIN_ANTI:
1363                                 if (rel1_empty)
1364                                         continue;       /* ignore this join segment */
1365                                 break;
1366                         case JOIN_FULL:
1367                                 if (rel1_empty && rel2_empty)
1368                                         continue;       /* ignore this join segment */
1369                                 break;
1370                         default:
1371                                 /* other values not expected here */
1372                                 elog(ERROR, "unrecognized join type: %d",
1373                                          (int) parent_sjinfo->jointype);
1374                                 break;
1375                 }
1376
1377                 /*
1378                  * If a child has been pruned entirely then we can't generate paths
1379                  * for it, so we have to reject partitionwise joining unless we were
1380                  * able to eliminate this partition above.
1381                  */
1382                 if (child_rel1 == NULL || child_rel2 == NULL)
1383                 {
1384                         /*
1385                          * Mark the joinrel as unpartitioned so that later functions treat
1386                          * it correctly.
1387                          */
1388                         joinrel->nparts = 0;
1389                         return;
1390                 }
1391
1392                 /*
1393                  * If a leaf relation has consider_partitionwise_join=false, it means
1394                  * that it's a dummy relation for which we skipped setting up tlist
1395                  * expressions and adding EC members in set_append_rel_size(), so
1396                  * again we have to fail here.
1397                  */
1398                 if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1399                 {
1400                         Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1401                         Assert(IS_DUMMY_REL(child_rel1));
1402                         joinrel->nparts = 0;
1403                         return;
1404                 }
1405                 if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1406                 {
1407                         Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1408                         Assert(IS_DUMMY_REL(child_rel2));
1409                         joinrel->nparts = 0;
1410                         return;
1411                 }
1412
1413                 /* We should never try to join two overlapping sets of rels. */
1414                 Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1415                 child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1416                 appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1417
1418                 /*
1419                  * Construct SpecialJoinInfo from parent join relations's
1420                  * SpecialJoinInfo.
1421                  */
1422                 child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1423                                                                                            child_rel1->relids,
1424                                                                                            child_rel2->relids);
1425
1426                 /*
1427                  * Construct restrictions applicable to the child join from those
1428                  * applicable to the parent join.
1429                  */
1430                 child_restrictlist =
1431                         (List *) adjust_appendrel_attrs(root,
1432                                                                                         (Node *) parent_restrictlist,
1433                                                                                         nappinfos, appinfos);
1434                 pfree(appinfos);
1435
1436                 child_joinrel = joinrel->part_rels[cnt_parts];
1437                 if (!child_joinrel)
1438                 {
1439                         child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1440                                                                                                  joinrel, child_restrictlist,
1441                                                                                                  child_sjinfo,
1442                                                                                                  child_sjinfo->jointype);
1443                         joinrel->part_rels[cnt_parts] = child_joinrel;
1444                         joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1445                                                                                                         child_joinrel->relids);
1446                 }
1447
1448                 Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1449
1450                 populate_joinrel_with_paths(root, child_rel1, child_rel2,
1451                                                                         child_joinrel, child_sjinfo,
1452                                                                         child_restrictlist);
1453         }
1454 }