OSDN Git Service

Support PostgreSQL 13.
[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  *     restriction_is_constant_false()
39  *     update_child_rel_info()
40  *     build_child_join_sjinfo()
41  *     get_matching_part_pairs()
42  *     compute_partition_bounds()
43  *
44  *
45  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
46  * Portions Copyright (c) 1994, Regents of the University of California
47  *
48  *-------------------------------------------------------------------------
49  */
50
51 static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
52                                                                 RelOptInfo *rel2, RelOptInfo *joinrel,
53                                                                 SpecialJoinInfo *sjinfo, List *restrictlist);
54
55 /*
56  * set_plain_rel_pathlist
57  *        Build access paths for a plain relation (no subquery, no inheritance)
58  */
59 static void
60 set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
61 {
62         Relids          required_outer;
63
64         /*
65          * We don't support pushing join clauses into the quals of a seqscan, but
66          * it could still have required parameterization due to LATERAL refs in
67          * its tlist.
68          */
69         required_outer = rel->lateral_relids;
70
71         /* Consider sequential scan */
72         add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
73
74         /* If appropriate, consider parallel sequential scan */
75         if (rel->consider_parallel && required_outer == NULL)
76                 create_plain_partial_paths(root, rel);
77
78         /* Consider index scans */
79         create_index_paths(root, rel);
80
81         /* Consider TID scans */
82         create_tidscan_paths(root, rel);
83 }
84
85
86 /*
87  * set_append_rel_pathlist
88  *        Build access paths for an "append relation"
89  */
90 static void
91 set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
92                                                 Index rti, RangeTblEntry *rte)
93 {
94         int                     parentRTindex = rti;
95         List       *live_childrels = NIL;
96         ListCell   *l;
97
98         /*
99          * Generate access paths for each member relation, and remember the
100          * non-dummy children.
101          */
102         foreach(l, root->append_rel_list)
103         {
104                 AppendRelInfo *appinfo = (AppendRelInfo *) lfirst(l);
105                 int                     childRTindex;
106                 RangeTblEntry *childRTE;
107                 RelOptInfo *childrel;
108
109                 /* append_rel_list contains all append rels; ignore others */
110                 if (appinfo->parent_relid != parentRTindex)
111                         continue;
112
113                 /* Re-locate the child RTE and RelOptInfo */
114                 childRTindex = appinfo->child_relid;
115                 childRTE = root->simple_rte_array[childRTindex];
116                 childrel = root->simple_rel_array[childRTindex];
117
118                 /*
119                  * If set_append_rel_size() decided the parent appendrel was
120                  * parallel-unsafe at some point after visiting this child rel, we
121                  * need to propagate the unsafety marking down to the child, so that
122                  * we don't generate useless partial paths for it.
123                  */
124                 if (!rel->consider_parallel)
125                         childrel->consider_parallel = false;
126
127                 /*
128                  * Compute the child's access paths.
129                  */
130                 set_rel_pathlist(root, childrel, childRTindex, childRTE);
131
132                 /*
133                  * If child is dummy, ignore it.
134                  */
135                 if (IS_DUMMY_REL(childrel))
136                         continue;
137
138                 /* Bubble up childrel's partitioned children. */
139                 if (rel->part_scheme)
140                         rel->partitioned_child_rels =
141                                 list_concat(rel->partitioned_child_rels,
142                                                         childrel->partitioned_child_rels);
143
144                 /*
145                  * Child is live, so add it to the live_childrels list for use below.
146                  */
147                 live_childrels = lappend(live_childrels, childrel);
148         }
149
150         /* Add paths to the append relation. */
151         add_paths_to_append_rel(root, rel, live_childrels);
152 }
153
154
155 /*
156  * standard_join_search
157  *        Find possible joinpaths for a query by successively finding ways
158  *        to join component relations into join relations.
159  *
160  * 'levels_needed' is the number of iterations needed, ie, the number of
161  *              independent jointree items in the query.  This is > 1.
162  *
163  * 'initial_rels' is a list of RelOptInfo nodes for each independent
164  *              jointree item.  These are the components to be joined together.
165  *              Note that levels_needed == list_length(initial_rels).
166  *
167  * Returns the final level of join relations, i.e., the relation that is
168  * the result of joining all the original relations together.
169  * At least one implementation path must be provided for this relation and
170  * all required sub-relations.
171  *
172  * To support loadable plugins that modify planner behavior by changing the
173  * join searching algorithm, we provide a hook variable that lets a plugin
174  * replace or supplement this function.  Any such hook must return the same
175  * final join relation as the standard code would, but it might have a
176  * different set of implementation paths attached, and only the sub-joinrels
177  * needed for these paths need have been instantiated.
178  *
179  * Note to plugin authors: the functions invoked during standard_join_search()
180  * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
181  * than one join-order search, you'll probably need to save and restore the
182  * original states of those data structures.  See geqo_eval() for an example.
183  */
184 RelOptInfo *
185 standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
186 {
187         int                     lev;
188         RelOptInfo *rel;
189
190         /*
191          * This function cannot be invoked recursively within any one planning
192          * problem, so join_rel_level[] can't be in use already.
193          */
194         Assert(root->join_rel_level == NULL);
195
196         /*
197          * We employ a simple "dynamic programming" algorithm: we first find all
198          * ways to build joins of two jointree items, then all ways to build joins
199          * of three items (from two-item joins and single items), then four-item
200          * joins, and so on until we have considered all ways to join all the
201          * items into one rel.
202          *
203          * root->join_rel_level[j] is a list of all the j-item rels.  Initially we
204          * set root->join_rel_level[1] to represent all the single-jointree-item
205          * relations.
206          */
207         root->join_rel_level = (List **) palloc0((levels_needed + 1) * sizeof(List *));
208
209         root->join_rel_level[1] = initial_rels;
210
211         for (lev = 2; lev <= levels_needed; lev++)
212         {
213                 ListCell   *lc;
214
215                 /*
216                  * Determine all possible pairs of relations to be joined at this
217                  * level, and build paths for making each one from every available
218                  * pair of lower-level relations.
219                  */
220                 join_search_one_level(root, lev);
221
222                 /*
223                  * Run generate_partitionwise_join_paths() and generate_gather_paths()
224                  * for each just-processed joinrel.  We could not do this earlier
225                  * because both regular and partial paths can get added to a
226                  * particular joinrel at multiple times within join_search_one_level.
227                  *
228                  * After that, we're done creating paths for the joinrel, so run
229                  * set_cheapest().
230                  */
231                 foreach(lc, root->join_rel_level[lev])
232                 {
233                         rel = (RelOptInfo *) lfirst(lc);
234
235                         /* Create paths for partitionwise joins. */
236                         generate_partitionwise_join_paths(root, rel);
237
238                         /*
239                          * Except for the topmost scan/join rel, consider gathering
240                          * partial paths.  We'll do the same for the topmost scan/join rel
241                          * once we know the final targetlist (see grouping_planner).
242                          */
243                         if (lev < levels_needed)
244                                 generate_useful_gather_paths(root, rel, false);
245
246                         /* Find and save the cheapest paths for this rel */
247                         set_cheapest(rel);
248
249 #ifdef OPTIMIZER_DEBUG
250                         debug_print_rel(root, rel);
251 #endif
252                 }
253         }
254
255         /*
256          * We should have a single rel at the final level.
257          */
258         if (root->join_rel_level[levels_needed] == NIL)
259                 elog(ERROR, "failed to build any %d-way joins", levels_needed);
260         Assert(list_length(root->join_rel_level[levels_needed]) == 1);
261
262         rel = (RelOptInfo *) linitial(root->join_rel_level[levels_needed]);
263
264         root->join_rel_level = NULL;
265
266         return rel;
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  * restriction_is_constant_false --- is a restrictlist just FALSE?
960  *
961  * In cases where a qual is provably constant FALSE, eval_const_expressions
962  * will generally have thrown away anything that's ANDed with it.  In outer
963  * join situations this will leave us computing cartesian products only to
964  * decide there's no match for an outer row, which is pretty stupid.  So,
965  * we need to detect the case.
966  *
967  * If only_pushed_down is true, then consider only quals that are pushed-down
968  * from the point of view of the joinrel.
969  */
970 static bool
971 restriction_is_constant_false(List *restrictlist,
972                                                           RelOptInfo *joinrel,
973                                                           bool only_pushed_down)
974 {
975         ListCell   *lc;
976
977         /*
978          * Despite the above comment, the restriction list we see here might
979          * possibly have other members besides the FALSE constant, since other
980          * quals could get "pushed down" to the outer join level.  So we check
981          * each member of the list.
982          */
983         foreach(lc, restrictlist)
984         {
985                 RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
986
987                 if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
988                         continue;
989
990                 if (rinfo->clause && IsA(rinfo->clause, Const))
991                 {
992                         Const      *con = (Const *) rinfo->clause;
993
994                         /* constant NULL is as good as constant FALSE for our purposes */
995                         if (con->constisnull)
996                                 return true;
997                         if (!DatumGetBool(con->constvalue))
998                                 return true;
999                 }
1000         }
1001         return false;
1002 }
1003
1004 /*
1005  * Construct the SpecialJoinInfo for a child-join by translating
1006  * SpecialJoinInfo for the join between parents. left_relids and right_relids
1007  * are the relids of left and right side of the join respectively.
1008  */
1009 static SpecialJoinInfo *
1010 build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
1011                                                 Relids left_relids, Relids right_relids)
1012 {
1013         SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
1014         AppendRelInfo **left_appinfos;
1015         int                     left_nappinfos;
1016         AppendRelInfo **right_appinfos;
1017         int                     right_nappinfos;
1018
1019         memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
1020         left_appinfos = find_appinfos_by_relids(root, left_relids,
1021                                                                                         &left_nappinfos);
1022         right_appinfos = find_appinfos_by_relids(root, right_relids,
1023                                                                                          &right_nappinfos);
1024
1025         sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
1026                                                                                            left_nappinfos, left_appinfos);
1027         sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
1028                                                                                                 right_nappinfos,
1029                                                                                                 right_appinfos);
1030         sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
1031                                                                                            left_nappinfos, left_appinfos);
1032         sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
1033                                                                                                 right_nappinfos,
1034                                                                                                 right_appinfos);
1035         sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
1036                                                                                                                          (Node *) sjinfo->semi_rhs_exprs,
1037                                                                                                                          right_nappinfos,
1038                                                                                                                          right_appinfos);
1039
1040         pfree(left_appinfos);
1041         pfree(right_appinfos);
1042
1043         return sjinfo;
1044 }
1045
1046 /*
1047  * get_matching_part_pairs
1048  *              Generate pairs of partitions to be joined from inputs
1049  */
1050 static void
1051 get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
1052                                                 RelOptInfo *rel1, RelOptInfo *rel2,
1053                                                 List **parts1, List **parts2)
1054 {
1055         bool            rel1_is_simple = IS_SIMPLE_REL(rel1);
1056         bool            rel2_is_simple = IS_SIMPLE_REL(rel2);
1057         int                     cnt_parts;
1058
1059         *parts1 = NIL;
1060         *parts2 = NIL;
1061
1062         for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1063         {
1064                 RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
1065                 RelOptInfo *child_rel1;
1066                 RelOptInfo *child_rel2;
1067                 Relids          child_relids1;
1068                 Relids          child_relids2;
1069
1070                 /*
1071                  * If this segment of the join is empty, it means that this segment
1072                  * was ignored when previously creating child-join paths for it in
1073                  * try_partitionwise_join() as it would not contribute to the join
1074                  * result, due to one or both inputs being empty; add NULL to each of
1075                  * the given lists so that this segment will be ignored again in that
1076                  * function.
1077                  */
1078                 if (!child_joinrel)
1079                 {
1080                         *parts1 = lappend(*parts1, NULL);
1081                         *parts2 = lappend(*parts2, NULL);
1082                         continue;
1083                 }
1084
1085                 /*
1086                  * Get a relids set of partition(s) involved in this join segment that
1087                  * are from the rel1 side.
1088                  */
1089                 child_relids1 = bms_intersect(child_joinrel->relids,
1090                                                                           rel1->all_partrels);
1091                 Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
1092
1093                 /*
1094                  * Get a child rel for rel1 with the relids.  Note that we should have
1095                  * the child rel even if rel1 is a join rel, because in that case the
1096                  * partitions specified in the relids would have matching/overlapping
1097                  * boundaries, so the specified partitions should be considered as
1098                  * ones to be joined when planning partitionwise joins of rel1,
1099                  * meaning that the child rel would have been built by the time we get
1100                  * here.
1101                  */
1102                 if (rel1_is_simple)
1103                 {
1104                         int                     varno = bms_singleton_member(child_relids1);
1105
1106                         child_rel1 = find_base_rel(root, varno);
1107                 }
1108                 else
1109                         child_rel1 = find_join_rel(root, child_relids1);
1110                 Assert(child_rel1);
1111
1112                 /*
1113                  * Get a relids set of partition(s) involved in this join segment that
1114                  * are from the rel2 side.
1115                  */
1116                 child_relids2 = bms_intersect(child_joinrel->relids,
1117                                                                           rel2->all_partrels);
1118                 Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
1119
1120                 /*
1121                  * Get a child rel for rel2 with the relids.  See above comments.
1122                  */
1123                 if (rel2_is_simple)
1124                 {
1125                         int                     varno = bms_singleton_member(child_relids2);
1126
1127                         child_rel2 = find_base_rel(root, varno);
1128                 }
1129                 else
1130                         child_rel2 = find_join_rel(root, child_relids2);
1131                 Assert(child_rel2);
1132
1133                 /*
1134                  * The join of rel1 and rel2 is legal, so is the join of the child
1135                  * rels obtained above; add them to the given lists as a join pair
1136                  * producing this join segment.
1137                  */
1138                 *parts1 = lappend(*parts1, child_rel1);
1139                 *parts2 = lappend(*parts2, child_rel2);
1140         }
1141 }
1142
1143
1144 /*
1145  * compute_partition_bounds
1146  *              Compute the partition bounds for a join rel from those for inputs
1147  */
1148 static void
1149 compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
1150                                                  RelOptInfo *rel2, RelOptInfo *joinrel,
1151                                                  SpecialJoinInfo *parent_sjinfo,
1152                                                  List **parts1, List **parts2)
1153 {
1154         /*
1155          * If we don't have the partition bounds for the join rel yet, try to
1156          * compute those along with pairs of partitions to be joined.
1157          */
1158         if (joinrel->nparts == -1)
1159         {
1160                 PartitionScheme part_scheme = joinrel->part_scheme;
1161                 PartitionBoundInfo boundinfo = NULL;
1162                 int                     nparts = 0;
1163
1164                 Assert(joinrel->boundinfo == NULL);
1165                 Assert(joinrel->part_rels == NULL);
1166
1167                 /*
1168                  * See if the partition bounds for inputs are exactly the same, in
1169                  * which case we don't need to work hard: the join rel have the same
1170                  * partition bounds as inputs, and the partitions with the same
1171                  * cardinal positions form the pairs.
1172                  *
1173                  * Note: even in cases where one or both inputs have merged bounds, it
1174                  * would be possible for both the bounds to be exactly the same, but
1175                  * it seems unlikely to be worth the cycles to check.
1176                  */
1177                 if (!rel1->partbounds_merged &&
1178                         !rel2->partbounds_merged &&
1179                         rel1->nparts == rel2->nparts &&
1180                         partition_bounds_equal(part_scheme->partnatts,
1181                                                                    part_scheme->parttyplen,
1182                                                                    part_scheme->parttypbyval,
1183                                                                    rel1->boundinfo, rel2->boundinfo))
1184                 {
1185                         boundinfo = rel1->boundinfo;
1186                         nparts = rel1->nparts;
1187                 }
1188                 else
1189                 {
1190                         /* Try merging the partition bounds for inputs. */
1191                         boundinfo = partition_bounds_merge(part_scheme->partnatts,
1192                                                                                            part_scheme->partsupfunc,
1193                                                                                            part_scheme->partcollation,
1194                                                                                            rel1, rel2,
1195                                                                                            parent_sjinfo->jointype,
1196                                                                                            parts1, parts2);
1197                         if (boundinfo == NULL)
1198                         {
1199                                 joinrel->nparts = 0;
1200                                 return;
1201                         }
1202                         nparts = list_length(*parts1);
1203                         joinrel->partbounds_merged = true;
1204                 }
1205
1206                 Assert(nparts > 0);
1207                 joinrel->boundinfo = boundinfo;
1208                 joinrel->nparts = nparts;
1209                 joinrel->part_rels =
1210                         (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
1211         }
1212         else
1213         {
1214                 Assert(joinrel->nparts > 0);
1215                 Assert(joinrel->boundinfo);
1216                 Assert(joinrel->part_rels);
1217
1218                 /*
1219                  * If the join rel's partbounds_merged flag is true, it means inputs
1220                  * are not guaranteed to have the same partition bounds, therefore we
1221                  * can't assume that the partitions at the same cardinal positions
1222                  * form the pairs; let get_matching_part_pairs() generate the pairs.
1223                  * Otherwise, nothing to do since we can assume that.
1224                  */
1225                 if (joinrel->partbounds_merged)
1226                 {
1227                         get_matching_part_pairs(root, joinrel, rel1, rel2,
1228                                                                         parts1, parts2);
1229                         Assert(list_length(*parts1) == joinrel->nparts);
1230                         Assert(list_length(*parts2) == joinrel->nparts);
1231                 }
1232         }
1233 }
1234
1235
1236 /*
1237  * Assess whether join between given two partitioned relations can be broken
1238  * down into joins between matching partitions; a technique called
1239  * "partitionwise join"
1240  *
1241  * Partitionwise join is possible when a. Joining relations have same
1242  * partitioning scheme b. There exists an equi-join between the partition keys
1243  * of the two relations.
1244  *
1245  * Partitionwise join is planned as follows (details: optimizer/README.)
1246  *
1247  * 1. Create the RelOptInfos for joins between matching partitions i.e
1248  * child-joins and add paths to them.
1249  *
1250  * 2. Construct Append or MergeAppend paths across the set of child joins.
1251  * This second phase is implemented by generate_partitionwise_join_paths().
1252  *
1253  * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
1254  * obtained by translating the respective parent join structures.
1255  */
1256 static void
1257 try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
1258                                            RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
1259                                            List *parent_restrictlist)
1260 {
1261         bool            rel1_is_simple = IS_SIMPLE_REL(rel1);
1262         bool            rel2_is_simple = IS_SIMPLE_REL(rel2);
1263         List       *parts1 = NIL;
1264         List       *parts2 = NIL;
1265         ListCell   *lcr1 = NULL;
1266         ListCell   *lcr2 = NULL;
1267         int                     cnt_parts;
1268
1269         /* Guard against stack overflow due to overly deep partition hierarchy. */
1270         check_stack_depth();
1271
1272         /* Nothing to do, if the join relation is not partitioned. */
1273         if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
1274                 return;
1275
1276         /* The join relation should have consider_partitionwise_join set. */
1277         Assert(joinrel->consider_partitionwise_join);
1278
1279         /*
1280          * We can not perform partitionwise join if either of the joining
1281          * relations is not partitioned.
1282          */
1283         if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
1284                 return;
1285
1286         Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
1287
1288         /* The joining relations should have consider_partitionwise_join set. */
1289         Assert(rel1->consider_partitionwise_join &&
1290                    rel2->consider_partitionwise_join);
1291
1292         /*
1293          * The partition scheme of the join relation should match that of the
1294          * joining relations.
1295          */
1296         Assert(joinrel->part_scheme == rel1->part_scheme &&
1297                    joinrel->part_scheme == rel2->part_scheme);
1298
1299         Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
1300
1301         compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
1302                                                          &parts1, &parts2);
1303
1304         if (joinrel->partbounds_merged)
1305         {
1306                 lcr1 = list_head(parts1);
1307                 lcr2 = list_head(parts2);
1308         }
1309
1310         /*
1311          * Create child-join relations for this partitioned join, if those don't
1312          * exist. Add paths to child-joins for a pair of child relations
1313          * corresponding to the given pair of parent relations.
1314          */
1315         for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
1316         {
1317                 RelOptInfo *child_rel1;
1318                 RelOptInfo *child_rel2;
1319                 bool            rel1_empty;
1320                 bool            rel2_empty;
1321                 SpecialJoinInfo *child_sjinfo;
1322                 List       *child_restrictlist;
1323                 RelOptInfo *child_joinrel;
1324                 Relids          child_joinrelids;
1325                 AppendRelInfo **appinfos;
1326                 int                     nappinfos;
1327
1328                 if (joinrel->partbounds_merged)
1329                 {
1330                         child_rel1 = lfirst_node(RelOptInfo, lcr1);
1331                         child_rel2 = lfirst_node(RelOptInfo, lcr2);
1332                         lcr1 = lnext(parts1, lcr1);
1333                         lcr2 = lnext(parts2, lcr2);
1334                 }
1335                 else
1336                 {
1337                         child_rel1 = rel1->part_rels[cnt_parts];
1338                         child_rel2 = rel2->part_rels[cnt_parts];
1339                 }
1340
1341                 rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
1342                 rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
1343
1344                 /*
1345                  * Check for cases where we can prove that this segment of the join
1346                  * returns no rows, due to one or both inputs being empty (including
1347                  * inputs that have been pruned away entirely).  If so just ignore it.
1348                  * These rules are equivalent to populate_joinrel_with_paths's rules
1349                  * for dummy input relations.
1350                  */
1351                 switch (parent_sjinfo->jointype)
1352                 {
1353                         case JOIN_INNER:
1354                         case JOIN_SEMI:
1355                                 if (rel1_empty || rel2_empty)
1356                                         continue;       /* ignore this join segment */
1357                                 break;
1358                         case JOIN_LEFT:
1359                         case JOIN_ANTI:
1360                                 if (rel1_empty)
1361                                         continue;       /* ignore this join segment */
1362                                 break;
1363                         case JOIN_FULL:
1364                                 if (rel1_empty && rel2_empty)
1365                                         continue;       /* ignore this join segment */
1366                                 break;
1367                         default:
1368                                 /* other values not expected here */
1369                                 elog(ERROR, "unrecognized join type: %d",
1370                                          (int) parent_sjinfo->jointype);
1371                                 break;
1372                 }
1373
1374                 /*
1375                  * If a child has been pruned entirely then we can't generate paths
1376                  * for it, so we have to reject partitionwise joining unless we were
1377                  * able to eliminate this partition above.
1378                  */
1379                 if (child_rel1 == NULL || child_rel2 == NULL)
1380                 {
1381                         /*
1382                          * Mark the joinrel as unpartitioned so that later functions treat
1383                          * it correctly.
1384                          */
1385                         joinrel->nparts = 0;
1386                         return;
1387                 }
1388
1389                 /*
1390                  * If a leaf relation has consider_partitionwise_join=false, it means
1391                  * that it's a dummy relation for which we skipped setting up tlist
1392                  * expressions and adding EC members in set_append_rel_size(), so
1393                  * again we have to fail here.
1394                  */
1395                 if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
1396                 {
1397                         Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
1398                         Assert(IS_DUMMY_REL(child_rel1));
1399                         joinrel->nparts = 0;
1400                         return;
1401                 }
1402                 if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
1403                 {
1404                         Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
1405                         Assert(IS_DUMMY_REL(child_rel2));
1406                         joinrel->nparts = 0;
1407                         return;
1408                 }
1409
1410                 /* We should never try to join two overlapping sets of rels. */
1411                 Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
1412                 child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
1413                 appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
1414
1415                 /*
1416                  * Construct SpecialJoinInfo from parent join relations's
1417                  * SpecialJoinInfo.
1418                  */
1419                 child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
1420                                                                                            child_rel1->relids,
1421                                                                                            child_rel2->relids);
1422
1423                 /*
1424                  * Construct restrictions applicable to the child join from those
1425                  * applicable to the parent join.
1426                  */
1427                 child_restrictlist =
1428                         (List *) adjust_appendrel_attrs(root,
1429                                                                                         (Node *) parent_restrictlist,
1430                                                                                         nappinfos, appinfos);
1431                 pfree(appinfos);
1432
1433                 child_joinrel = joinrel->part_rels[cnt_parts];
1434                 if (!child_joinrel)
1435                 {
1436                         child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
1437                                                                                                  joinrel, child_restrictlist,
1438                                                                                                  child_sjinfo,
1439                                                                                                  child_sjinfo->jointype);
1440                         joinrel->part_rels[cnt_parts] = child_joinrel;
1441                         joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
1442                                                                                                         child_joinrel->relids);
1443                 }
1444
1445                 Assert(bms_equal(child_joinrel->relids, child_joinrelids));
1446
1447                 populate_joinrel_with_paths(root, child_rel1, child_rel2,
1448                                                                         child_joinrel, child_sjinfo,
1449                                                                         child_restrictlist);
1450         }
1451 }