OSDN Git Service

New tool to generate source files for copied functions
[pghintplan/pg_hint_plan.git] / core.c
diff --git a/core.c b/core.c
index dd5acd6..986ba97 100644 (file)
--- a/core.c
+++ b/core.c
@@ -3,29 +3,86 @@
  * core.c
  *       Routines copied from PostgreSQL core distribution.
  *
+ * The main purpose of this files is having access to static functions in core.
+ * Another purpose is tweaking functions behavior by replacing part of them by
+ * macro definitions. See at the end of pg_hint_plan.c for details. Anyway,
+ * this file *must* contain required functions without making any change.
+ *
+ * This file contains the following functions from corresponding files.
+ *
  * src/backend/optimizer/path/allpaths.c
- *     set_append_rel_pathlist()
- *     generate_mergeappend_paths()
- *     get_cheapest_parameterized_child_path()
- *     accumulate_append_subpath()
- *     standard_join_search()
+ *
+ *     static functions:
+ *        set_plain_rel_pathlist()
+ *     add_paths_to_append_rel()
+ *     try_partitionwise_join()
+ *
+ *  public functions:
+ *     standard_join_search(): This funcion is not static. The reason for
+ *        including this function is make_rels_by_clause_joins. In order to
+ *        avoid generating apparently unwanted join combination, we decided to
+ *        change the behavior of make_join_rel, which is called under this
+ *        function.
  *
  * src/backend/optimizer/path/joinrels.c
- *     join_search_one_level()
+ *
+ *     public functions:
+ *     join_search_one_level(): We have to modify this to call my definition of
+ *                 make_rels_by_clause_joins.
+ *
+ *     static functions:
  *     make_rels_by_clause_joins()
  *     make_rels_by_clauseless_joins()
  *     join_is_legal()
  *     has_join_restriction()
- *     is_dummy_rel()
- *     mark_dummy_rel()
  *     restriction_is_constant_false()
+ *     update_child_rel_info()
+ *     build_child_join_sjinfo()
+ *     get_matching_part_pairs()
+ *     compute_partition_bounds()
  *
- * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
+ *
+ * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *-------------------------------------------------------------------------
  */
 
+static void populate_joinrel_with_paths(PlannerInfo *root, RelOptInfo *rel1,
+                                                               RelOptInfo *rel2, RelOptInfo *joinrel,
+                                                               SpecialJoinInfo *sjinfo, List *restrictlist);
+
+/*
+ * set_plain_rel_pathlist
+ *       Build access paths for a plain relation (no subquery, no inheritance)
+ */
+static void
+set_plain_rel_pathlist(PlannerInfo *root, RelOptInfo *rel, RangeTblEntry *rte)
+{
+       Relids          required_outer;
+
+       /*
+        * We don't support pushing join clauses into the quals of a seqscan, but
+        * it could still have required parameterization due to LATERAL refs in
+        * its tlist.
+        */
+       required_outer = rel->lateral_relids;
+
+       /* Consider sequential scan */
+       add_path(rel, create_seqscan_path(root, rel, required_outer, 0));
+
+       /* If appropriate, consider parallel sequential scan */
+       if (rel->consider_parallel && required_outer == NULL)
+               create_plain_partial_paths(root, rel);
+
+       /* Consider index scans */
+       create_index_paths(root, rel);
+
+       /* Consider TID scans */
+       create_tidscan_paths(root, rel);
+}
+
+
 /*
  * set_append_rel_pathlist
  *       Build access paths for an "append relation"
@@ -36,17 +93,11 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
 {
        int                     parentRTindex = rti;
        List       *live_childrels = NIL;
-       List       *subpaths = NIL;
-       bool            subpaths_valid = true;
-       List       *all_child_pathkeys = NIL;
-       List       *all_child_outers = NIL;
        ListCell   *l;
 
        /*
         * Generate access paths for each member relation, and remember the
-        * cheapest path for each one.  Also, identify all pathkeys (orderings)
-        * and parameterizations (required_outer sets) available for the member
-        * relations.
+        * non-dummy children.
         */
        foreach(l, root->append_rel_list)
        {
@@ -54,7 +105,6 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                int                     childRTindex;
                RangeTblEntry *childRTE;
                RelOptInfo *childrel;
-               ListCell   *lcp;
 
                /* append_rel_list contains all append rels; ignore others */
                if (appinfo->parent_relid != parentRTindex)
@@ -66,6 +116,15 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                childrel = root->simple_rel_array[childRTindex];
 
                /*
+                * If set_append_rel_size() decided the parent appendrel was
+                * parallel-unsafe at some point after visiting this child rel, we
+                * need to propagate the unsafety marking down to the child, so that
+                * we don't generate useless partial paths for it.
+                */
+               if (!rel->consider_parallel)
+                       childrel->consider_parallel = false;
+
+               /*
                 * Compute the child's access paths.
                 */
                set_rel_pathlist(root, childrel, childRTindex, childRTE);
@@ -76,343 +135,22 @@ set_append_rel_pathlist(PlannerInfo *root, RelOptInfo *rel,
                if (IS_DUMMY_REL(childrel))
                        continue;
 
+               /* Bubble up childrel's partitioned children. */
+               if (rel->part_scheme)
+                       rel->partitioned_child_rels =
+                               list_concat(rel->partitioned_child_rels,
+                                                       childrel->partitioned_child_rels);
+
                /*
                 * Child is live, so add it to the live_childrels list for use below.
                 */
                live_childrels = lappend(live_childrels, childrel);
-
-               /*
-                * If child has an unparameterized cheapest-total path, add that to
-                * the unparameterized Append path we are constructing for the parent.
-                * If not, there's no workable unparameterized path.
-                */
-               if (childrel->cheapest_total_path->param_info == NULL)
-                       subpaths = accumulate_append_subpath(subpaths,
-                                                                                         childrel->cheapest_total_path);
-               else
-                       subpaths_valid = false;
-
-               /*
-                * Collect lists of all the available path orderings and
-                * parameterizations for all the children.      We use these as a
-                * heuristic to indicate which sort orderings and parameterizations we
-                * should build Append and MergeAppend paths for.
-                */
-               foreach(lcp, childrel->pathlist)
-               {
-                       Path       *childpath = (Path *) lfirst(lcp);
-                       List       *childkeys = childpath->pathkeys;
-                       Relids          childouter = PATH_REQ_OUTER(childpath);
-
-                       /* Unsorted paths don't contribute to pathkey list */
-                       if (childkeys != NIL)
-                       {
-                               ListCell   *lpk;
-                               bool            found = false;
-
-                               /* Have we already seen this ordering? */
-                               foreach(lpk, all_child_pathkeys)
-                               {
-                                       List       *existing_pathkeys = (List *) lfirst(lpk);
-
-                                       if (compare_pathkeys(existing_pathkeys,
-                                                                                childkeys) == PATHKEYS_EQUAL)
-                                       {
-                                               found = true;
-                                               break;
-                                       }
-                               }
-                               if (!found)
-                               {
-                                       /* No, so add it to all_child_pathkeys */
-                                       all_child_pathkeys = lappend(all_child_pathkeys,
-                                                                                                childkeys);
-                               }
-                       }
-
-                       /* Unparameterized paths don't contribute to param-set list */
-                       if (childouter)
-                       {
-                               ListCell   *lco;
-                               bool            found = false;
-
-                               /* Have we already seen this param set? */
-                               foreach(lco, all_child_outers)
-                               {
-                                       Relids          existing_outers = (Relids) lfirst(lco);
-
-                                       if (bms_equal(existing_outers, childouter))
-                                       {
-                                               found = true;
-                                               break;
-                                       }
-                               }
-                               if (!found)
-                               {
-                                       /* No, so add it to all_child_outers */
-                                       all_child_outers = lappend(all_child_outers,
-                                                                                          childouter);
-                               }
-                       }
-               }
-       }
-
-       /*
-        * If we found unparameterized paths for all children, build an unordered,
-        * unparameterized Append path for the rel.  (Note: this is correct even
-        * if we have zero or one live subpath due to constraint exclusion.)
-        */
-       if (subpaths_valid)
-               add_path(rel, (Path *) create_append_path(rel, subpaths, NULL));
-
-       /*
-        * Also build unparameterized MergeAppend paths based on the collected
-        * list of child pathkeys.
-        */
-       if (subpaths_valid)
-               generate_mergeappend_paths(root, rel, live_childrels,
-                                                                  all_child_pathkeys);
-
-       /*
-        * Build Append paths for each parameterization seen among the child rels.
-        * (This may look pretty expensive, but in most cases of practical
-        * interest, the child rels will expose mostly the same parameterizations,
-        * so that not that many cases actually get considered here.)
-        *
-        * The Append node itself cannot enforce quals, so all qual checking must
-        * be done in the child paths.  This means that to have a parameterized
-        * Append path, we must have the exact same parameterization for each
-        * child path; otherwise some children might be failing to check the
-        * moved-down quals.  To make them match up, we can try to increase the
-        * parameterization of lesser-parameterized paths.
-        */
-       foreach(l, all_child_outers)
-       {
-               Relids          required_outer = (Relids) lfirst(l);
-               ListCell   *lcr;
-
-               /* Select the child paths for an Append with this parameterization */
-               subpaths = NIL;
-               subpaths_valid = true;
-               foreach(lcr, live_childrels)
-               {
-                       RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
-                       Path       *subpath;
-
-                       subpath = get_cheapest_parameterized_child_path(root,
-                                                                                                                       childrel,
-                                                                                                                       required_outer);
-                       if (subpath == NULL)
-                       {
-                               /* failed to make a suitable path for this child */
-                               subpaths_valid = false;
-                               break;
-                       }
-                       subpaths = accumulate_append_subpath(subpaths, subpath);
-               }
-
-               if (subpaths_valid)
-                       add_path(rel, (Path *)
-                                        create_append_path(rel, subpaths, required_outer));
        }
 
-       /* Select cheapest paths */
-       set_cheapest(rel);
+       /* Add paths to the append relation. */
+       add_paths_to_append_rel(root, rel, live_childrels);
 }
 
-/*
- * generate_mergeappend_paths
- *             Generate MergeAppend paths for an append relation
- *
- * Generate a path for each ordering (pathkey list) appearing in
- * all_child_pathkeys.
- *
- * We consider both cheapest-startup and cheapest-total cases, ie, for each
- * interesting ordering, collect all the cheapest startup subpaths and all the
- * cheapest total paths, and build a MergeAppend path for each case.
- *
- * We don't currently generate any parameterized MergeAppend paths.  While
- * it would not take much more code here to do so, it's very unclear that it
- * is worth the planning cycles to investigate such paths: there's little
- * use for an ordered path on the inside of a nestloop.  In fact, it's likely
- * that the current coding of add_path would reject such paths out of hand,
- * because add_path gives no credit for sort ordering of parameterized paths,
- * and a parameterized MergeAppend is going to be more expensive than the
- * corresponding parameterized Append path.  If we ever try harder to support
- * parameterized mergejoin plans, it might be worth adding support for
- * parameterized MergeAppends to feed such joins.  (See notes in
- * optimizer/README for why that might not ever happen, though.)
- */
-static void
-generate_mergeappend_paths(PlannerInfo *root, RelOptInfo *rel,
-                                                  List *live_childrels,
-                                                  List *all_child_pathkeys)
-{
-       ListCell   *lcp;
-
-       foreach(lcp, all_child_pathkeys)
-       {
-               List       *pathkeys = (List *) lfirst(lcp);
-               List       *startup_subpaths = NIL;
-               List       *total_subpaths = NIL;
-               bool            startup_neq_total = false;
-               ListCell   *lcr;
-
-               /* Select the child paths for this ordering... */
-               foreach(lcr, live_childrels)
-               {
-                       RelOptInfo *childrel = (RelOptInfo *) lfirst(lcr);
-                       Path       *cheapest_startup,
-                                          *cheapest_total;
-
-                       /* Locate the right paths, if they are available. */
-                       cheapest_startup =
-                               get_cheapest_path_for_pathkeys(childrel->pathlist,
-                                                                                          pathkeys,
-                                                                                          NULL,
-                                                                                          STARTUP_COST);
-                       cheapest_total =
-                               get_cheapest_path_for_pathkeys(childrel->pathlist,
-                                                                                          pathkeys,
-                                                                                          NULL,
-                                                                                          TOTAL_COST);
-
-                       /*
-                        * If we can't find any paths with the right order just use the
-                        * cheapest-total path; we'll have to sort it later.
-                        */
-                       if (cheapest_startup == NULL || cheapest_total == NULL)
-                       {
-                               cheapest_startup = cheapest_total =
-                                       childrel->cheapest_total_path;
-                               /* Assert we do have an unparameterized path for this child */
-                               Assert(cheapest_total->param_info == NULL);
-                       }
-
-                       /*
-                        * Notice whether we actually have different paths for the
-                        * "cheapest" and "total" cases; frequently there will be no point
-                        * in two create_merge_append_path() calls.
-                        */
-                       if (cheapest_startup != cheapest_total)
-                               startup_neq_total = true;
-
-                       startup_subpaths =
-                               accumulate_append_subpath(startup_subpaths, cheapest_startup);
-                       total_subpaths =
-                               accumulate_append_subpath(total_subpaths, cheapest_total);
-               }
-
-               /* ... and build the MergeAppend paths */
-               add_path(rel, (Path *) create_merge_append_path(root,
-                                                                                                               rel,
-                                                                                                               startup_subpaths,
-                                                                                                               pathkeys,
-                                                                                                               NULL));
-               if (startup_neq_total)
-                       add_path(rel, (Path *) create_merge_append_path(root,
-                                                                                                                       rel,
-                                                                                                                       total_subpaths,
-                                                                                                                       pathkeys,
-                                                                                                                       NULL));
-       }
-}
-
-/*
- * get_cheapest_parameterized_child_path
- *             Get cheapest path for this relation that has exactly the requested
- *             parameterization.
- *
- * Returns NULL if unable to create such a path.
- */
-static Path *
-get_cheapest_parameterized_child_path(PlannerInfo *root, RelOptInfo *rel,
-                                                                         Relids required_outer)
-{
-       Path       *cheapest;
-       ListCell   *lc;
-
-       /*
-        * Look up the cheapest existing path with no more than the needed
-        * parameterization.  If it has exactly the needed parameterization, we're
-        * done.
-        */
-       cheapest = get_cheapest_path_for_pathkeys(rel->pathlist,
-                                                                                         NIL,
-                                                                                         required_outer,
-                                                                                         TOTAL_COST);
-       Assert(cheapest != NULL);
-       if (bms_equal(PATH_REQ_OUTER(cheapest), required_outer))
-               return cheapest;
-
-       /*
-        * Otherwise, we can "reparameterize" an existing path to match the given
-        * parameterization, which effectively means pushing down additional
-        * joinquals to be checked within the path's scan.  However, some existing
-        * paths might check the available joinquals already while others don't;
-        * therefore, it's not clear which existing path will be cheapest after
-        * reparameterization.  We have to go through them all and find out.
-        */
-       cheapest = NULL;
-       foreach(lc, rel->pathlist)
-       {
-               Path       *path = (Path *) lfirst(lc);
-
-               /* Can't use it if it needs more than requested parameterization */
-               if (!bms_is_subset(PATH_REQ_OUTER(path), required_outer))
-                       continue;
-
-               /*
-                * Reparameterization can only increase the path's cost, so if it's
-                * already more expensive than the current cheapest, forget it.
-                */
-               if (cheapest != NULL &&
-                       compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
-                       continue;
-
-               /* Reparameterize if needed, then recheck cost */
-               if (!bms_equal(PATH_REQ_OUTER(path), required_outer))
-               {
-                       path = reparameterize_path(root, path, required_outer, 1.0);
-                       if (path == NULL)
-                               continue;               /* failed to reparameterize this one */
-                       Assert(bms_equal(PATH_REQ_OUTER(path), required_outer));
-
-                       if (cheapest != NULL &&
-                               compare_path_costs(cheapest, path, TOTAL_COST) <= 0)
-                               continue;
-               }
-
-               /* We have a new best path */
-               cheapest = path;
-       }
-
-       /* Return the best path, or NULL if we found no suitable candidate */
-       return cheapest;
-}
-
-/*
- * accumulate_append_subpath
- *             Add a subpath to the list being built for an Append or MergeAppend
- *
- * It's possible that the child is itself an Append path, in which case
- * we can "cut out the middleman" and just add its child paths to our
- * own list.  (We don't try to do this earlier because we need to
- * apply both levels of transformation to the quals.)
- */
-static List *
-accumulate_append_subpath(List *subpaths, Path *path)
-{
-       if (IsA(path, AppendPath))
-       {
-               AppendPath *apath = (AppendPath *) path;
-
-               /* list_copy is important here to avoid sharing list substructure */
-               return list_concat(subpaths, list_copy(apath->subpaths));
-       }
-       else
-               return lappend(subpaths, path);
-}
 
 /*
  * standard_join_search
@@ -423,7 +161,7 @@ accumulate_append_subpath(List *subpaths, Path *path)
  *             independent jointree items in the query.  This is > 1.
  *
  * 'initial_rels' is a list of RelOptInfo nodes for each independent
- *             jointree item.  These are the components to be joined together.
+ *             jointree item.  These are the components to be joined together.
  *             Note that levels_needed == list_length(initial_rels).
  *
  * Returns the final level of join relations, i.e., the relation that is
@@ -439,7 +177,7 @@ accumulate_append_subpath(List *subpaths, Path *path)
  * needed for these paths need have been instantiated.
  *
  * Note to plugin authors: the functions invoked during standard_join_search()
- * modify root->join_rel_list and root->join_rel_hash. If you want to do more
+ * modify root->join_rel_list and root->join_rel_hash.  If you want to do more
  * than one join-order search, you'll probably need to save and restore the
  * original states of those data structures.  See geqo_eval() for an example.
  */
@@ -482,12 +220,29 @@ standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
                join_search_one_level(root, lev);
 
                /*
-                * Do cleanup work on each just-processed rel.
+                * Run generate_partitionwise_join_paths() and generate_gather_paths()
+                * for each just-processed joinrel.  We could not do this earlier
+                * because both regular and partial paths can get added to a
+                * particular joinrel at multiple times within join_search_one_level.
+                *
+                * After that, we're done creating paths for the joinrel, so run
+                * set_cheapest().
                 */
                foreach(lc, root->join_rel_level[lev])
                {
                        rel = (RelOptInfo *) lfirst(lc);
 
+                       /* Create paths for partitionwise joins. */
+                       generate_partitionwise_join_paths(root, rel);
+
+                       /*
+                        * Except for the topmost scan/join rel, consider gathering
+                        * partial paths.  We'll do the same for the topmost scan/join rel
+                        * once we know the final targetlist (see grouping_planner).
+                        */
+                       if (lev < levels_needed)
+                               generate_useful_gather_paths(root, rel, false);
+
                        /* Find and save the cheapest paths for this rel */
                        set_cheapest(rel);
 
@@ -512,6 +267,27 @@ standard_join_search(PlannerInfo *root, int levels_needed, List *initial_rels)
 }
 
 /*
+ * create_plain_partial_paths
+ *       Build partial access paths for parallel scan of a plain relation
+ */
+static void
+create_plain_partial_paths(PlannerInfo *root, RelOptInfo *rel)
+{
+       int                     parallel_workers;
+
+       parallel_workers = compute_parallel_worker(rel, rel->pages, -1,
+                                                                                          max_parallel_workers_per_gather);
+
+       /* If any limit was set to zero, the user doesn't want a parallel scan. */
+       if (parallel_workers <= 0)
+               return;
+
+       /* Add an unordered partial path based on a parallel sequential scan. */
+       add_partial_path(rel, create_seqscan_path(root, rel, NULL, parallel_workers));
+}
+
+
+/*
  * join_search_one_level
  *       Consider ways to produce join relations containing exactly 'level'
  *       jointree items.  (This is one step of the dynamic-programming method
@@ -563,15 +339,23 @@ join_search_one_level(PlannerInfo *root, int level)
                         * to each initial rel they don't already include but have a join
                         * clause or restriction with.
                         */
+                       List       *other_rels_list;
                        ListCell   *other_rels;
 
                        if (level == 2)         /* consider remaining initial rels */
-                               other_rels = lnext(r);
-                       else    /* consider all initial rels */
-                               other_rels = list_head(joinrels[1]);
+                       {
+                               other_rels_list = joinrels[level - 1];
+                               other_rels = lnext(other_rels_list, r);
+                       }
+                       else                            /* consider all initial rels */
+                       {
+                               other_rels_list = joinrels[1];
+                               other_rels = list_head(other_rels_list);
+                       }
 
                        make_rels_by_clause_joins(root,
                                                                          old_rel,
+                                                                         other_rels_list,
                                                                          other_rels);
                }
                else
@@ -590,7 +374,7 @@ join_search_one_level(PlannerInfo *root, int level)
                         */
                        make_rels_by_clauseless_joins(root,
                                                                                  old_rel,
-                                                                                 list_head(joinrels[1]));
+                                                                                 joinrels[1]);
                }
        }
 
@@ -616,6 +400,7 @@ join_search_one_level(PlannerInfo *root, int level)
                foreach(r, joinrels[k])
                {
                        RelOptInfo *old_rel = (RelOptInfo *) lfirst(r);
+                       List       *other_rels_list;
                        ListCell   *other_rels;
                        ListCell   *r2;
 
@@ -629,11 +414,18 @@ join_search_one_level(PlannerInfo *root, int level)
                                continue;
 
                        if (k == other_level)
-                               other_rels = lnext(r);  /* only consider remaining rels */
+                       {
+                               /* only consider remaining rels */
+                               other_rels_list = joinrels[k];
+                               other_rels = lnext(other_rels_list, r);
+                       }
                        else
-                               other_rels = list_head(joinrels[other_level]);
+                       {
+                               other_rels_list = joinrels[other_level];
+                               other_rels = list_head(other_rels_list);
+                       }
 
-                       for_each_cell(r2, other_rels)
+                       for_each_cell(r2, other_rels_list, other_rels)
                        {
                                RelOptInfo *new_rel = (RelOptInfo *) lfirst(r2);
 
@@ -685,12 +477,12 @@ join_search_one_level(PlannerInfo *root, int level)
 
                        make_rels_by_clauseless_joins(root,
                                                                                  old_rel,
-                                                                                 list_head(joinrels[1]));
+                                                                                 joinrels[1]);
                }
 
                /*----------
                 * When special joins are involved, there may be no legal way
-                * to make an N-way join for some values of N.  For example consider
+                * to make an N-way join for some values of N.  For example consider
                 *
                 * SELECT ... FROM t1 WHERE
                 *       x IN (SELECT ... FROM t2,t3 WHERE ...) AND
@@ -708,11 +500,12 @@ join_search_one_level(PlannerInfo *root, int level)
                 */
                if (joinrels[level] == NIL &&
                        root->join_info_list == NIL &&
-                       root->lateral_info_list == NIL)
+                       !root->hasLateralRTEs)
                        elog(ERROR, "failed to build any %d-way joins", level);
        }
 }
 
+
 /*
  * make_rels_by_clause_joins
  *       Build joins between the given relation 'old_rel' and other relations
@@ -727,8 +520,9 @@ join_search_one_level(PlannerInfo *root, int level)
  * automatically ensures that each new joinrel is only added to the list once.
  *
  * 'old_rel' is the relation entry for the relation to be joined
- * 'other_rels': the first cell in a linked list containing the other
+ * 'other_rels_list': a list containing the other
  * rels to be considered for joining
+ * 'other_rels': the first cell to be considered
  *
  * Currently, this is only used with initial rels in other_rels, but it
  * will work for joining to joinrels too.
@@ -736,11 +530,12 @@ join_search_one_level(PlannerInfo *root, int level)
 static void
 make_rels_by_clause_joins(PlannerInfo *root,
                                                  RelOptInfo *old_rel,
+                                                 List *other_rels_list,
                                                  ListCell *other_rels)
 {
        ListCell   *l;
 
-       for_each_cell(l, other_rels)
+       for_each_cell(l, other_rels_list, other_rels)
        {
                RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
 
@@ -753,6 +548,7 @@ make_rels_by_clause_joins(PlannerInfo *root,
        }
 }
 
+
 /*
  * make_rels_by_clauseless_joins
  *       Given a relation 'old_rel' and a list of other relations
@@ -761,8 +557,7 @@ make_rels_by_clause_joins(PlannerInfo *root,
  *       The join rels are returned in root->join_rel_level[join_cur_level].
  *
  * 'old_rel' is the relation entry for the relation to be joined
- * 'other_rels': the first cell of a linked list containing the
- * other rels to be considered for joining
+ * 'other_rels': a list containing the other rels to be considered for joining
  *
  * Currently, this is only used with initial rels in other_rels, but it would
  * work for joining to joinrels too.
@@ -770,11 +565,11 @@ make_rels_by_clause_joins(PlannerInfo *root,
 static void
 make_rels_by_clauseless_joins(PlannerInfo *root,
                                                          RelOptInfo *old_rel,
-                                                         ListCell *other_rels)
+                                                         List *other_rels)
 {
        ListCell   *l;
 
-       for_each_cell(l, other_rels)
+       foreach(l, other_rels)
        {
                RelOptInfo *other_rel = (RelOptInfo *) lfirst(l);
 
@@ -785,6 +580,7 @@ make_rels_by_clauseless_joins(PlannerInfo *root,
        }
 }
 
+
 /*
  * join_is_legal
  *        Determine whether a proposed join is legal given the query's
@@ -796,7 +592,7 @@ make_rels_by_clauseless_joins(PlannerInfo *root,
  *
  * On success, *sjinfo_p is set to NULL if this is to be a plain inner join,
  * else it's set to point to the associated SpecialJoinInfo node.  Also,
- * *reversed_p is set TRUE if the given relations need to be swapped to
+ * *reversed_p is set true if the given relations need to be swapped to
  * match the SpecialJoinInfo node.
  */
 static bool
@@ -807,13 +603,11 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
        SpecialJoinInfo *match_sjinfo;
        bool            reversed;
        bool            unique_ified;
-       bool            is_valid_inner;
-       bool            lateral_fwd;
-       bool            lateral_rev;
+       bool            must_be_leftjoin;
        ListCell   *l;
 
        /*
-        * Ensure output params are set on failure return.      This is just to
+        * Ensure output params are set on failure return.  This is just to
         * suppress uninitialized-variable warnings from overly anal compilers.
         */
        *sjinfo_p = NULL;
@@ -821,13 +615,13 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
 
        /*
         * If we have any special joins, the proposed join might be illegal; and
-        * in any case we have to determine its join type.      Scan the join info
-        * list for conflicts.
+        * in any case we have to determine its join type.  Scan the join info
+        * list for matches and conflicts.
         */
        match_sjinfo = NULL;
        reversed = false;
        unique_ified = false;
-       is_valid_inner = true;
+       must_be_leftjoin = false;
 
        foreach(l, root->join_info_list)
        {
@@ -878,7 +672,8 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
                 * If one input contains min_lefthand and the other contains
                 * min_righthand, then we can perform the SJ at this join.
                 *
-                * Barf if we get matches to more than one SJ (is that possible?)
+                * Reject if we get matches to more than one SJ; that implies we're
+                * considering something that's not really valid.
                 */
                if (bms_is_subset(sjinfo->min_lefthand, rel1->relids) &&
                        bms_is_subset(sjinfo->min_righthand, rel2->relids))
@@ -943,90 +738,163 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
                }
                else
                {
-                       /*----------
-                        * Otherwise, the proposed join overlaps the RHS but isn't
-                        * a valid implementation of this SJ.  It might still be
-                        * a legal join, however.  If both inputs overlap the RHS,
-                        * assume that it's OK.  Since the inputs presumably got past
-                        * this function's checks previously, they can't overlap the
-                        * LHS and their violations of the RHS boundary must represent
-                        * SJs that have been determined to commute with this one.
-                        * We have to allow this to work correctly in cases like
-                        *              (a LEFT JOIN (b JOIN (c LEFT JOIN d)))
-                        * when the c/d join has been determined to commute with the join
-                        * to a, and hence d is not part of min_righthand for the upper
-                        * join.  It should be legal to join b to c/d but this will appear
-                        * as a violation of the upper join's RHS.
-                        * Furthermore, if one input overlaps the RHS and the other does
-                        * not, we should still allow the join if it is a valid
-                        * implementation of some other SJ.  We have to allow this to
-                        * support the associative identity
-                        *              (a LJ b on Pab) LJ c ON Pbc = a LJ (b LJ c ON Pbc) on Pab
-                        * since joining B directly to C violates the lower SJ's RHS.
-                        * We assume that make_outerjoininfo() set things up correctly
-                        * so that we'll only match to some SJ if the join is valid.
-                        * Set flag here to check at bottom of loop.
-                        *----------
+                       /*
+                        * Otherwise, the proposed join overlaps the RHS but isn't a valid
+                        * implementation of this SJ.  But don't panic quite yet: the RHS
+                        * violation might have occurred previously, in one or both input
+                        * relations, in which case we must have previously decided that
+                        * it was OK to commute some other SJ with this one.  If we need
+                        * to perform this join to finish building up the RHS, rejecting
+                        * it could lead to not finding any plan at all.  (This can occur
+                        * because of the heuristics elsewhere in this file that postpone
+                        * clauseless joins: we might not consider doing a clauseless join
+                        * within the RHS until after we've performed other, validly
+                        * commutable SJs with one or both sides of the clauseless join.)
+                        * This consideration boils down to the rule that if both inputs
+                        * overlap the RHS, we can allow the join --- they are either
+                        * fully within the RHS, or represent previously-allowed joins to
+                        * rels outside it.
                         */
-                       if (sjinfo->jointype != JOIN_SEMI &&
-                               bms_overlap(rel1->relids, sjinfo->min_righthand) &&
+                       if (bms_overlap(rel1->relids, sjinfo->min_righthand) &&
                                bms_overlap(rel2->relids, sjinfo->min_righthand))
-                       {
-                               /* seems OK */
-                               Assert(!bms_overlap(joinrelids, sjinfo->min_lefthand));
-                       }
-                       else
-                               is_valid_inner = false;
+                               continue;               /* assume valid previous violation of RHS */
+
+                       /*
+                        * The proposed join could still be legal, but only if we're
+                        * allowed to associate it into the RHS of this SJ.  That means
+                        * this SJ must be a LEFT join (not SEMI or ANTI, and certainly
+                        * not FULL) and the proposed join must not overlap the LHS.
+                        */
+                       if (sjinfo->jointype != JOIN_LEFT ||
+                               bms_overlap(joinrelids, sjinfo->min_lefthand))
+                               return false;   /* invalid join path */
+
+                       /*
+                        * To be valid, the proposed join must be a LEFT join; otherwise
+                        * it can't associate into this SJ's RHS.  But we may not yet have
+                        * found the SpecialJoinInfo matching the proposed join, so we
+                        * can't test that yet.  Remember the requirement for later.
+                        */
+                       must_be_leftjoin = true;
                }
        }
 
        /*
-        * Fail if violated some SJ's RHS and didn't match to another SJ. However,
-        * "matching" to a semijoin we are implementing by unique-ification
-        * doesn't count (think: it's really an inner join).
+        * Fail if violated any SJ's RHS and didn't match to a LEFT SJ: the
+        * proposed join can't associate into an SJ's RHS.
+        *
+        * Also, fail if the proposed join's predicate isn't strict; we're
+        * essentially checking to see if we can apply outer-join identity 3, and
+        * that's a requirement.  (This check may be redundant with checks in
+        * make_outerjoininfo, but I'm not quite sure, and it's cheap to test.)
         */
-       if (!is_valid_inner &&
-               (match_sjinfo == NULL || unique_ified))
+       if (must_be_leftjoin &&
+               (match_sjinfo == NULL ||
+                match_sjinfo->jointype != JOIN_LEFT ||
+                !match_sjinfo->lhs_strict))
                return false;                   /* invalid join path */
 
        /*
         * We also have to check for constraints imposed by LATERAL references.
-        * The proposed rels could each contain lateral references to the other,
-        * in which case the join is impossible.  If there are lateral references
-        * in just one direction, then the join has to be done with a nestloop
-        * with the lateral referencer on the inside.  If the join matches an SJ
-        * that cannot be implemented by such a nestloop, the join is impossible.
         */
-       lateral_fwd = lateral_rev = false;
-       foreach(l, root->lateral_info_list)
+       if (root->hasLateralRTEs)
        {
-               LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
+               bool            lateral_fwd;
+               bool            lateral_rev;
+               Relids          join_lateral_rels;
 
-               if (bms_is_subset(ljinfo->lateral_rhs, rel2->relids) &&
-                       bms_overlap(ljinfo->lateral_lhs, rel1->relids))
+               /*
+                * The proposed rels could each contain lateral references to the
+                * other, in which case the join is impossible.  If there are lateral
+                * references in just one direction, then the join has to be done with
+                * a nestloop with the lateral referencer on the inside.  If the join
+                * matches an SJ that cannot be implemented by such a nestloop, the
+                * join is impossible.
+                *
+                * Also, if the lateral reference is only indirect, we should reject
+                * the join; whatever rel(s) the reference chain goes through must be
+                * joined to first.
+                *
+                * Another case that might keep us from building a valid plan is the
+                * implementation restriction described by have_dangerous_phv().
+                */
+               lateral_fwd = bms_overlap(rel1->relids, rel2->lateral_relids);
+               lateral_rev = bms_overlap(rel2->relids, rel1->lateral_relids);
+               if (lateral_fwd && lateral_rev)
+                       return false;           /* have lateral refs in both directions */
+               if (lateral_fwd)
                {
                        /* has to be implemented as nestloop with rel1 on left */
-                       if (lateral_rev)
-                               return false;   /* have lateral refs in both directions */
-                       lateral_fwd = true;
-                       if (!bms_is_subset(ljinfo->lateral_lhs, rel1->relids))
-                               return false;   /* rel1 can't compute the required parameter */
                        if (match_sjinfo &&
-                               (reversed || match_sjinfo->jointype == JOIN_FULL))
+                               (reversed ||
+                                unique_ified ||
+                                match_sjinfo->jointype == JOIN_FULL))
                                return false;   /* not implementable as nestloop */
+                       /* check there is a direct reference from rel2 to rel1 */
+                       if (!bms_overlap(rel1->relids, rel2->direct_lateral_relids))
+                               return false;   /* only indirect refs, so reject */
+                       /* check we won't have a dangerous PHV */
+                       if (have_dangerous_phv(root, rel1->relids, rel2->lateral_relids))
+                               return false;   /* might be unable to handle required PHV */
                }
-               if (bms_is_subset(ljinfo->lateral_rhs, rel1->relids) &&
-                       bms_overlap(ljinfo->lateral_lhs, rel2->relids))
+               else if (lateral_rev)
                {
                        /* has to be implemented as nestloop with rel2 on left */
-                       if (lateral_fwd)
-                               return false;   /* have lateral refs in both directions */
-                       lateral_rev = true;
-                       if (!bms_is_subset(ljinfo->lateral_lhs, rel2->relids))
-                               return false;   /* rel2 can't compute the required parameter */
                        if (match_sjinfo &&
-                               (!reversed || match_sjinfo->jointype == JOIN_FULL))
+                               (!reversed ||
+                                unique_ified ||
+                                match_sjinfo->jointype == JOIN_FULL))
                                return false;   /* not implementable as nestloop */
+                       /* check there is a direct reference from rel1 to rel2 */
+                       if (!bms_overlap(rel2->relids, rel1->direct_lateral_relids))
+                               return false;   /* only indirect refs, so reject */
+                       /* check we won't have a dangerous PHV */
+                       if (have_dangerous_phv(root, rel2->relids, rel1->lateral_relids))
+                               return false;   /* might be unable to handle required PHV */
+               }
+
+               /*
+                * LATERAL references could also cause problems later on if we accept
+                * this join: if the join's minimum parameterization includes any rels
+                * that would have to be on the inside of an outer join with this join
+                * rel, then it's never going to be possible to build the complete
+                * query using this join.  We should reject this join not only because
+                * it'll save work, but because if we don't, the clauseless-join
+                * heuristics might think that legality of this join means that some
+                * other join rel need not be formed, and that could lead to failure
+                * to find any plan at all.  We have to consider not only rels that
+                * are directly on the inner side of an OJ with the joinrel, but also
+                * ones that are indirectly so, so search to find all such rels.
+                */
+               join_lateral_rels = min_join_parameterization(root, joinrelids,
+                                                                                                         rel1, rel2);
+               if (join_lateral_rels)
+               {
+                       Relids          join_plus_rhs = bms_copy(joinrelids);
+                       bool            more;
+
+                       do
+                       {
+                               more = false;
+                               foreach(l, root->join_info_list)
+                               {
+                                       SpecialJoinInfo *sjinfo = (SpecialJoinInfo *) lfirst(l);
+
+                                       /* ignore full joins --- their ordering is predetermined */
+                                       if (sjinfo->jointype == JOIN_FULL)
+                                               continue;
+
+                                       if (bms_overlap(sjinfo->min_lefthand, join_plus_rhs) &&
+                                               !bms_is_subset(sjinfo->min_righthand, join_plus_rhs))
+                                       {
+                                               join_plus_rhs = bms_add_members(join_plus_rhs,
+                                                                                                               sjinfo->min_righthand);
+                                               more = true;
+                                       }
+                               }
+                       } while (more);
+                       if (bms_overlap(join_plus_rhs, join_lateral_rels))
+                               return false;   /* will not be able to join to some RHS rel */
                }
        }
 
@@ -1036,15 +904,16 @@ join_is_legal(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
        return true;
 }
 
+
 /*
  * has_join_restriction
  *             Detect whether the specified relation has join-order restrictions,
  *             due to being inside an outer join or an IN (sub-SELECT),
- *             or participating in any LATERAL references.
+ *             or participating in any LATERAL references or multi-rel PHVs.
  *
  * Essentially, this tests whether have_join_order_restriction() could
  * succeed with this rel and some other one.  It's OK if we sometimes
- * say "true" incorrectly.     (Therefore, we don't bother with the relatively
+ * say "true" incorrectly.  (Therefore, we don't bother with the relatively
  * expensive has_legal_joinclause test.)
  */
 static bool
@@ -1052,12 +921,15 @@ has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
 {
        ListCell   *l;
 
-       foreach(l, root->lateral_info_list)
+       if (rel->lateral_relids != NULL || rel->lateral_referencers != NULL)
+               return true;
+
+       foreach(l, root->placeholder_list)
        {
-               LateralJoinInfo *ljinfo = (LateralJoinInfo *) lfirst(l);
+               PlaceHolderInfo *phinfo = (PlaceHolderInfo *) lfirst(l);
 
-               if (bms_is_subset(ljinfo->lateral_rhs, rel->relids) ||
-                       bms_overlap(ljinfo->lateral_lhs, rel->relids))
+               if (bms_is_subset(rel->relids, phinfo->ph_eval_at) &&
+                       !bms_equal(rel->relids, phinfo->ph_eval_at))
                        return true;
        }
 
@@ -1084,57 +956,6 @@ has_join_restriction(PlannerInfo *root, RelOptInfo *rel)
 }
 
 /*
- * is_dummy_rel --- has relation been proven empty?
- */
-static bool
-is_dummy_rel(RelOptInfo *rel)
-{
-       return IS_DUMMY_REL(rel);
-}
-
-/*
- * Mark a relation as proven empty.
- *
- * During GEQO planning, this can get invoked more than once on the same
- * baserel struct, so it's worth checking to see if the rel is already marked
- * dummy.
- *
- * Also, when called during GEQO join planning, we are in a short-lived
- * memory context.     We must make sure that the dummy path attached to a
- * baserel survives the GEQO cycle, else the baserel is trashed for future
- * GEQO cycles.  On the other hand, when we are marking a joinrel during GEQO,
- * we don't want the dummy path to clutter the main planning context.  Upshot
- * is that the best solution is to explicitly make the dummy path in the same
- * context the given RelOptInfo is in.
- */
-static void
-mark_dummy_rel(RelOptInfo *rel)
-{
-       MemoryContext oldcontext;
-
-       /* Already marked? */
-       if (is_dummy_rel(rel))
-               return;
-
-       /* No, so choose correct context to make the dummy path in */
-       oldcontext = MemoryContextSwitchTo(GetMemoryChunkContext(rel));
-
-       /* Set dummy size estimate */
-       rel->rows = 0;
-
-       /* Evict any previously chosen paths */
-       rel->pathlist = NIL;
-
-       /* Set up the dummy path */
-       add_path(rel, (Path *) create_append_path(rel, NIL, NULL));
-
-       /* Set or update cheapest_total_path and related fields */
-       set_cheapest(rel);
-
-       MemoryContextSwitchTo(oldcontext);
-}
-
-/*
  * restriction_is_constant_false --- is a restrictlist just FALSE?
  *
  * In cases where a qual is provably constant FALSE, eval_const_expressions
@@ -1143,10 +964,13 @@ mark_dummy_rel(RelOptInfo *rel)
  * decide there's no match for an outer row, which is pretty stupid.  So,
  * we need to detect the case.
  *
- * If only_pushed_down is TRUE, then consider only pushed-down quals.
+ * If only_pushed_down is true, then consider only quals that are pushed-down
+ * from the point of view of the joinrel.
  */
 static bool
-restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
+restriction_is_constant_false(List *restrictlist,
+                                                         RelOptInfo *joinrel,
+                                                         bool only_pushed_down)
 {
        ListCell   *lc;
 
@@ -1158,10 +982,9 @@ restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
         */
        foreach(lc, restrictlist)
        {
-               RestrictInfo *rinfo = (RestrictInfo *) lfirst(lc);
+               RestrictInfo *rinfo = lfirst_node(RestrictInfo, lc);
 
-               Assert(IsA(rinfo, RestrictInfo));
-               if (only_pushed_down && !rinfo->is_pushed_down)
+               if (only_pushed_down && !RINFO_IS_PUSHED_DOWN(rinfo, joinrel->relids))
                        continue;
 
                if (rinfo->clause && IsA(rinfo->clause, Const))
@@ -1177,3 +1000,452 @@ restriction_is_constant_false(List *restrictlist, bool only_pushed_down)
        }
        return false;
 }
+
+/*
+ * Construct the SpecialJoinInfo for a child-join by translating
+ * SpecialJoinInfo for the join between parents. left_relids and right_relids
+ * are the relids of left and right side of the join respectively.
+ */
+static SpecialJoinInfo *
+build_child_join_sjinfo(PlannerInfo *root, SpecialJoinInfo *parent_sjinfo,
+                                               Relids left_relids, Relids right_relids)
+{
+       SpecialJoinInfo *sjinfo = makeNode(SpecialJoinInfo);
+       AppendRelInfo **left_appinfos;
+       int                     left_nappinfos;
+       AppendRelInfo **right_appinfos;
+       int                     right_nappinfos;
+
+       memcpy(sjinfo, parent_sjinfo, sizeof(SpecialJoinInfo));
+       left_appinfos = find_appinfos_by_relids(root, left_relids,
+                                                                                       &left_nappinfos);
+       right_appinfos = find_appinfos_by_relids(root, right_relids,
+                                                                                        &right_nappinfos);
+
+       sjinfo->min_lefthand = adjust_child_relids(sjinfo->min_lefthand,
+                                                                                          left_nappinfos, left_appinfos);
+       sjinfo->min_righthand = adjust_child_relids(sjinfo->min_righthand,
+                                                                                               right_nappinfos,
+                                                                                               right_appinfos);
+       sjinfo->syn_lefthand = adjust_child_relids(sjinfo->syn_lefthand,
+                                                                                          left_nappinfos, left_appinfos);
+       sjinfo->syn_righthand = adjust_child_relids(sjinfo->syn_righthand,
+                                                                                               right_nappinfos,
+                                                                                               right_appinfos);
+       sjinfo->semi_rhs_exprs = (List *) adjust_appendrel_attrs(root,
+                                                                                                                        (Node *) sjinfo->semi_rhs_exprs,
+                                                                                                                        right_nappinfos,
+                                                                                                                        right_appinfos);
+
+       pfree(left_appinfos);
+       pfree(right_appinfos);
+
+       return sjinfo;
+}
+
+/*
+ * get_matching_part_pairs
+ *             Generate pairs of partitions to be joined from inputs
+ */
+static void
+get_matching_part_pairs(PlannerInfo *root, RelOptInfo *joinrel,
+                                               RelOptInfo *rel1, RelOptInfo *rel2,
+                                               List **parts1, List **parts2)
+{
+       bool            rel1_is_simple = IS_SIMPLE_REL(rel1);
+       bool            rel2_is_simple = IS_SIMPLE_REL(rel2);
+       int                     cnt_parts;
+
+       *parts1 = NIL;
+       *parts2 = NIL;
+
+       for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
+       {
+               RelOptInfo *child_joinrel = joinrel->part_rels[cnt_parts];
+               RelOptInfo *child_rel1;
+               RelOptInfo *child_rel2;
+               Relids          child_relids1;
+               Relids          child_relids2;
+
+               /*
+                * If this segment of the join is empty, it means that this segment
+                * was ignored when previously creating child-join paths for it in
+                * try_partitionwise_join() as it would not contribute to the join
+                * result, due to one or both inputs being empty; add NULL to each of
+                * the given lists so that this segment will be ignored again in that
+                * function.
+                */
+               if (!child_joinrel)
+               {
+                       *parts1 = lappend(*parts1, NULL);
+                       *parts2 = lappend(*parts2, NULL);
+                       continue;
+               }
+
+               /*
+                * Get a relids set of partition(s) involved in this join segment that
+                * are from the rel1 side.
+                */
+               child_relids1 = bms_intersect(child_joinrel->relids,
+                                                                         rel1->all_partrels);
+               Assert(bms_num_members(child_relids1) == bms_num_members(rel1->relids));
+
+               /*
+                * Get a child rel for rel1 with the relids.  Note that we should have
+                * the child rel even if rel1 is a join rel, because in that case the
+                * partitions specified in the relids would have matching/overlapping
+                * boundaries, so the specified partitions should be considered as
+                * ones to be joined when planning partitionwise joins of rel1,
+                * meaning that the child rel would have been built by the time we get
+                * here.
+                */
+               if (rel1_is_simple)
+               {
+                       int                     varno = bms_singleton_member(child_relids1);
+
+                       child_rel1 = find_base_rel(root, varno);
+               }
+               else
+                       child_rel1 = find_join_rel(root, child_relids1);
+               Assert(child_rel1);
+
+               /*
+                * Get a relids set of partition(s) involved in this join segment that
+                * are from the rel2 side.
+                */
+               child_relids2 = bms_intersect(child_joinrel->relids,
+                                                                         rel2->all_partrels);
+               Assert(bms_num_members(child_relids2) == bms_num_members(rel2->relids));
+
+               /*
+                * Get a child rel for rel2 with the relids.  See above comments.
+                */
+               if (rel2_is_simple)
+               {
+                       int                     varno = bms_singleton_member(child_relids2);
+
+                       child_rel2 = find_base_rel(root, varno);
+               }
+               else
+                       child_rel2 = find_join_rel(root, child_relids2);
+               Assert(child_rel2);
+
+               /*
+                * The join of rel1 and rel2 is legal, so is the join of the child
+                * rels obtained above; add them to the given lists as a join pair
+                * producing this join segment.
+                */
+               *parts1 = lappend(*parts1, child_rel1);
+               *parts2 = lappend(*parts2, child_rel2);
+       }
+}
+
+
+/*
+ * compute_partition_bounds
+ *             Compute the partition bounds for a join rel from those for inputs
+ */
+static void
+compute_partition_bounds(PlannerInfo *root, RelOptInfo *rel1,
+                                                RelOptInfo *rel2, RelOptInfo *joinrel,
+                                                SpecialJoinInfo *parent_sjinfo,
+                                                List **parts1, List **parts2)
+{
+       /*
+        * If we don't have the partition bounds for the join rel yet, try to
+        * compute those along with pairs of partitions to be joined.
+        */
+       if (joinrel->nparts == -1)
+       {
+               PartitionScheme part_scheme = joinrel->part_scheme;
+               PartitionBoundInfo boundinfo = NULL;
+               int                     nparts = 0;
+
+               Assert(joinrel->boundinfo == NULL);
+               Assert(joinrel->part_rels == NULL);
+
+               /*
+                * See if the partition bounds for inputs are exactly the same, in
+                * which case we don't need to work hard: the join rel have the same
+                * partition bounds as inputs, and the partitions with the same
+                * cardinal positions form the pairs.
+                *
+                * Note: even in cases where one or both inputs have merged bounds, it
+                * would be possible for both the bounds to be exactly the same, but
+                * it seems unlikely to be worth the cycles to check.
+                */
+               if (!rel1->partbounds_merged &&
+                       !rel2->partbounds_merged &&
+                       rel1->nparts == rel2->nparts &&
+                       partition_bounds_equal(part_scheme->partnatts,
+                                                                  part_scheme->parttyplen,
+                                                                  part_scheme->parttypbyval,
+                                                                  rel1->boundinfo, rel2->boundinfo))
+               {
+                       boundinfo = rel1->boundinfo;
+                       nparts = rel1->nparts;
+               }
+               else
+               {
+                       /* Try merging the partition bounds for inputs. */
+                       boundinfo = partition_bounds_merge(part_scheme->partnatts,
+                                                                                          part_scheme->partsupfunc,
+                                                                                          part_scheme->partcollation,
+                                                                                          rel1, rel2,
+                                                                                          parent_sjinfo->jointype,
+                                                                                          parts1, parts2);
+                       if (boundinfo == NULL)
+                       {
+                               joinrel->nparts = 0;
+                               return;
+                       }
+                       nparts = list_length(*parts1);
+                       joinrel->partbounds_merged = true;
+               }
+
+               Assert(nparts > 0);
+               joinrel->boundinfo = boundinfo;
+               joinrel->nparts = nparts;
+               joinrel->part_rels =
+                       (RelOptInfo **) palloc0(sizeof(RelOptInfo *) * nparts);
+       }
+       else
+       {
+               Assert(joinrel->nparts > 0);
+               Assert(joinrel->boundinfo);
+               Assert(joinrel->part_rels);
+
+               /*
+                * If the join rel's partbounds_merged flag is true, it means inputs
+                * are not guaranteed to have the same partition bounds, therefore we
+                * can't assume that the partitions at the same cardinal positions
+                * form the pairs; let get_matching_part_pairs() generate the pairs.
+                * Otherwise, nothing to do since we can assume that.
+                */
+               if (joinrel->partbounds_merged)
+               {
+                       get_matching_part_pairs(root, joinrel, rel1, rel2,
+                                                                       parts1, parts2);
+                       Assert(list_length(*parts1) == joinrel->nparts);
+                       Assert(list_length(*parts2) == joinrel->nparts);
+               }
+       }
+}
+
+
+/*
+ * Assess whether join between given two partitioned relations can be broken
+ * down into joins between matching partitions; a technique called
+ * "partitionwise join"
+ *
+ * Partitionwise join is possible when a. Joining relations have same
+ * partitioning scheme b. There exists an equi-join between the partition keys
+ * of the two relations.
+ *
+ * Partitionwise join is planned as follows (details: optimizer/README.)
+ *
+ * 1. Create the RelOptInfos for joins between matching partitions i.e
+ * child-joins and add paths to them.
+ *
+ * 2. Construct Append or MergeAppend paths across the set of child joins.
+ * This second phase is implemented by generate_partitionwise_join_paths().
+ *
+ * The RelOptInfo, SpecialJoinInfo and restrictlist for each child join are
+ * obtained by translating the respective parent join structures.
+ */
+static void
+try_partitionwise_join(PlannerInfo *root, RelOptInfo *rel1, RelOptInfo *rel2,
+                                          RelOptInfo *joinrel, SpecialJoinInfo *parent_sjinfo,
+                                          List *parent_restrictlist)
+{
+       bool            rel1_is_simple = IS_SIMPLE_REL(rel1);
+       bool            rel2_is_simple = IS_SIMPLE_REL(rel2);
+       List       *parts1 = NIL;
+       List       *parts2 = NIL;
+       ListCell   *lcr1 = NULL;
+       ListCell   *lcr2 = NULL;
+       int                     cnt_parts;
+
+       /* Guard against stack overflow due to overly deep partition hierarchy. */
+       check_stack_depth();
+
+       /* Nothing to do, if the join relation is not partitioned. */
+       if (joinrel->part_scheme == NULL || joinrel->nparts == 0)
+               return;
+
+       /* The join relation should have consider_partitionwise_join set. */
+       Assert(joinrel->consider_partitionwise_join);
+
+       /*
+        * We can not perform partitionwise join if either of the joining
+        * relations is not partitioned.
+        */
+       if (!IS_PARTITIONED_REL(rel1) || !IS_PARTITIONED_REL(rel2))
+               return;
+
+       Assert(REL_HAS_ALL_PART_PROPS(rel1) && REL_HAS_ALL_PART_PROPS(rel2));
+
+       /* The joining relations should have consider_partitionwise_join set. */
+       Assert(rel1->consider_partitionwise_join &&
+                  rel2->consider_partitionwise_join);
+
+       /*
+        * The partition scheme of the join relation should match that of the
+        * joining relations.
+        */
+       Assert(joinrel->part_scheme == rel1->part_scheme &&
+                  joinrel->part_scheme == rel2->part_scheme);
+
+       Assert(!(joinrel->partbounds_merged && (joinrel->nparts <= 0)));
+
+       compute_partition_bounds(root, rel1, rel2, joinrel, parent_sjinfo,
+                                                        &parts1, &parts2);
+
+       if (joinrel->partbounds_merged)
+       {
+               lcr1 = list_head(parts1);
+               lcr2 = list_head(parts2);
+       }
+
+       /*
+        * Create child-join relations for this partitioned join, if those don't
+        * exist. Add paths to child-joins for a pair of child relations
+        * corresponding to the given pair of parent relations.
+        */
+       for (cnt_parts = 0; cnt_parts < joinrel->nparts; cnt_parts++)
+       {
+               RelOptInfo *child_rel1;
+               RelOptInfo *child_rel2;
+               bool            rel1_empty;
+               bool            rel2_empty;
+               SpecialJoinInfo *child_sjinfo;
+               List       *child_restrictlist;
+               RelOptInfo *child_joinrel;
+               Relids          child_joinrelids;
+               AppendRelInfo **appinfos;
+               int                     nappinfos;
+
+               if (joinrel->partbounds_merged)
+               {
+                       child_rel1 = lfirst_node(RelOptInfo, lcr1);
+                       child_rel2 = lfirst_node(RelOptInfo, lcr2);
+                       lcr1 = lnext(parts1, lcr1);
+                       lcr2 = lnext(parts2, lcr2);
+               }
+               else
+               {
+                       child_rel1 = rel1->part_rels[cnt_parts];
+                       child_rel2 = rel2->part_rels[cnt_parts];
+               }
+
+               rel1_empty = (child_rel1 == NULL || IS_DUMMY_REL(child_rel1));
+               rel2_empty = (child_rel2 == NULL || IS_DUMMY_REL(child_rel2));
+
+               /*
+                * Check for cases where we can prove that this segment of the join
+                * returns no rows, due to one or both inputs being empty (including
+                * inputs that have been pruned away entirely).  If so just ignore it.
+                * These rules are equivalent to populate_joinrel_with_paths's rules
+                * for dummy input relations.
+                */
+               switch (parent_sjinfo->jointype)
+               {
+                       case JOIN_INNER:
+                       case JOIN_SEMI:
+                               if (rel1_empty || rel2_empty)
+                                       continue;       /* ignore this join segment */
+                               break;
+                       case JOIN_LEFT:
+                       case JOIN_ANTI:
+                               if (rel1_empty)
+                                       continue;       /* ignore this join segment */
+                               break;
+                       case JOIN_FULL:
+                               if (rel1_empty && rel2_empty)
+                                       continue;       /* ignore this join segment */
+                               break;
+                       default:
+                               /* other values not expected here */
+                               elog(ERROR, "unrecognized join type: %d",
+                                        (int) parent_sjinfo->jointype);
+                               break;
+               }
+
+               /*
+                * If a child has been pruned entirely then we can't generate paths
+                * for it, so we have to reject partitionwise joining unless we were
+                * able to eliminate this partition above.
+                */
+               if (child_rel1 == NULL || child_rel2 == NULL)
+               {
+                       /*
+                        * Mark the joinrel as unpartitioned so that later functions treat
+                        * it correctly.
+                        */
+                       joinrel->nparts = 0;
+                       return;
+               }
+
+               /*
+                * If a leaf relation has consider_partitionwise_join=false, it means
+                * that it's a dummy relation for which we skipped setting up tlist
+                * expressions and adding EC members in set_append_rel_size(), so
+                * again we have to fail here.
+                */
+               if (rel1_is_simple && !child_rel1->consider_partitionwise_join)
+               {
+                       Assert(child_rel1->reloptkind == RELOPT_OTHER_MEMBER_REL);
+                       Assert(IS_DUMMY_REL(child_rel1));
+                       joinrel->nparts = 0;
+                       return;
+               }
+               if (rel2_is_simple && !child_rel2->consider_partitionwise_join)
+               {
+                       Assert(child_rel2->reloptkind == RELOPT_OTHER_MEMBER_REL);
+                       Assert(IS_DUMMY_REL(child_rel2));
+                       joinrel->nparts = 0;
+                       return;
+               }
+
+               /* We should never try to join two overlapping sets of rels. */
+               Assert(!bms_overlap(child_rel1->relids, child_rel2->relids));
+               child_joinrelids = bms_union(child_rel1->relids, child_rel2->relids);
+               appinfos = find_appinfos_by_relids(root, child_joinrelids, &nappinfos);
+
+               /*
+                * Construct SpecialJoinInfo from parent join relations's
+                * SpecialJoinInfo.
+                */
+               child_sjinfo = build_child_join_sjinfo(root, parent_sjinfo,
+                                                                                          child_rel1->relids,
+                                                                                          child_rel2->relids);
+
+               /*
+                * Construct restrictions applicable to the child join from those
+                * applicable to the parent join.
+                */
+               child_restrictlist =
+                       (List *) adjust_appendrel_attrs(root,
+                                                                                       (Node *) parent_restrictlist,
+                                                                                       nappinfos, appinfos);
+               pfree(appinfos);
+
+               child_joinrel = joinrel->part_rels[cnt_parts];
+               if (!child_joinrel)
+               {
+                       child_joinrel = build_child_join_rel(root, child_rel1, child_rel2,
+                                                                                                joinrel, child_restrictlist,
+                                                                                                child_sjinfo,
+                                                                                                child_sjinfo->jointype);
+                       joinrel->part_rels[cnt_parts] = child_joinrel;
+                       joinrel->all_partrels = bms_add_members(joinrel->all_partrels,
+                                                                                                       child_joinrel->relids);
+               }
+
+               Assert(bms_equal(child_joinrel->relids, child_joinrelids));
+
+               populate_joinrel_with_paths(root, child_rel1, child_rel2,
+                                                                       child_joinrel, child_sjinfo,
+                                                                       child_restrictlist);
+       }
+}