diff options
Diffstat (limited to 'src/backend/optimizer/util/pathnode.c')
-rw-r--r-- | src/backend/optimizer/util/pathnode.c | 292 |
1 files changed, 230 insertions, 62 deletions
diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index 246cd8f7476..0a7e5c2678f 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -52,6 +52,8 @@ static int append_startup_cost_compare(const ListCell *a, const ListCell *b); static List *reparameterize_pathlist_by_child(PlannerInfo *root, List *pathlist, RelOptInfo *child_rel); +static bool pathlist_is_reparameterizable_by_child(List *pathlist, + RelOptInfo *child_rel); /***************************************************************************** @@ -2463,6 +2465,16 @@ create_nestloop_path(PlannerInfo *root, { NestPath *pathnode = makeNode(NestPath); Relids inner_req_outer = PATH_REQ_OUTER(inner_path); + Relids outerrelids; + + /* + * Paths are parameterized by top-level parents, so run parameterization + * tests on the parent relids. + */ + if (outer_path->parent->top_parent_relids) + outerrelids = outer_path->parent->top_parent_relids; + else + outerrelids = outer_path->parent->relids; /* * If the inner path is parameterized by the outer, we must drop any @@ -2472,7 +2484,7 @@ create_nestloop_path(PlannerInfo *root, * estimates for this path. We detect such clauses by checking for serial * number match to clauses already enforced in the inner path. */ - if (bms_overlap(inner_req_outer, outer_path->parent->relids)) + if (bms_overlap(inner_req_outer, outerrelids)) { Bitmapset *enforced_serials = get_param_path_clause_serials(inner_path); List *jclauses = NIL; @@ -4072,34 +4084,39 @@ reparameterize_path(PlannerInfo *root, Path *path, * Given a path parameterized by the parent of the given child relation, * translate the path to be parameterized by the given child relation. * - * The function creates a new path of the same type as the given path, but - * parameterized by the given child relation. Most fields from the original - * path can simply be flat-copied, but any expressions must be adjusted to - * refer to the correct varnos, and any paths must be recursively - * reparameterized. Other fields that refer to specific relids also need - * adjustment. + * Most fields in the path are not changed, but any expressions must be + * adjusted to refer to the correct varnos, and any subpaths must be + * recursively reparameterized. Other fields that refer to specific relids + * also need adjustment. * * The cost, number of rows, width and parallel path properties depend upon - * path->parent, which does not change during the translation. Hence those - * members are copied as they are. + * path->parent, which does not change during the translation. So we need + * not change those. * * Currently, only a few path types are supported here, though more could be * added at need. We return NULL if we can't reparameterize the given path. + * + * Note that this function can change referenced RangeTblEntries, RelOptInfos + * and IndexOptInfos as well as the Path structures. Therefore, it's only safe + * to call during create_plan(), when we have made a final choice of which Path + * to use for each RangeTblEntry/RelOptInfo/IndexOptInfo. + * + * Keep this code in sync with path_is_reparameterizable_by_child()! */ Path * reparameterize_path_by_child(PlannerInfo *root, Path *path, RelOptInfo *child_rel) { - -#define FLAT_COPY_PATH(newnode, node, nodetype) \ - ( (newnode) = makeNode(nodetype), \ - memcpy((newnode), (node), sizeof(nodetype)) ) + Path *new_path; + ParamPathInfo *new_ppi; + ParamPathInfo *old_ppi; + Relids required_outer; #define ADJUST_CHILD_ATTRS(node) \ - ((node) = \ - (List *) adjust_appendrel_attrs_multilevel(root, (Node *) (node), \ - child_rel, \ - child_rel->top_parent)) + ((node) = (void *) adjust_appendrel_attrs_multilevel(root, \ + (Node *) (node), \ + child_rel, \ + child_rel->top_parent)) #define REPARAMETERIZE_CHILD_PATH(path) \ do { \ @@ -4119,21 +4136,16 @@ do { \ } \ } while(0) - Path *new_path; - ParamPathInfo *new_ppi; - ParamPathInfo *old_ppi; - Relids required_outer; - /* - * If the path is not parameterized by parent of the given relation, it - * doesn't need reparameterization. + * If the path is not parameterized by the parent of the given relation, + * it doesn't need reparameterization. */ if (!path->param_info || !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids)) return path; /* - * If possible, reparameterize the given path, making a copy. + * If possible, reparameterize the given path. * * This function is currently only applied to the inner side of a nestloop * join that is being partitioned by the partitionwise-join code. Hence, @@ -4147,14 +4159,28 @@ do { \ switch (nodeTag(path)) { case T_Path: - FLAT_COPY_PATH(new_path, path, Path); + new_path = path; + ADJUST_CHILD_ATTRS(new_path->parent->baserestrictinfo); + if (path->pathtype == T_SampleScan) + { + Index scan_relid = path->parent->relid; + RangeTblEntry *rte; + + /* it should be a base rel with a tablesample clause... */ + Assert(scan_relid > 0); + rte = planner_rt_fetch(scan_relid, root); + Assert(rte->rtekind == RTE_RELATION); + Assert(rte->tablesample != NULL); + + ADJUST_CHILD_ATTRS(rte->tablesample); + } break; case T_IndexPath: { - IndexPath *ipath; + IndexPath *ipath = (IndexPath *) path; - FLAT_COPY_PATH(ipath, path, IndexPath); + ADJUST_CHILD_ATTRS(ipath->indexinfo->indrestrictinfo); ADJUST_CHILD_ATTRS(ipath->indexclauses); new_path = (Path *) ipath; } @@ -4162,9 +4188,9 @@ do { \ case T_BitmapHeapPath: { - BitmapHeapPath *bhpath; + BitmapHeapPath *bhpath = (BitmapHeapPath *) path; - FLAT_COPY_PATH(bhpath, path, BitmapHeapPath); + ADJUST_CHILD_ATTRS(bhpath->path.parent->baserestrictinfo); REPARAMETERIZE_CHILD_PATH(bhpath->bitmapqual); new_path = (Path *) bhpath; } @@ -4172,9 +4198,8 @@ do { \ case T_BitmapAndPath: { - BitmapAndPath *bapath; + BitmapAndPath *bapath = (BitmapAndPath *) path; - FLAT_COPY_PATH(bapath, path, BitmapAndPath); REPARAMETERIZE_CHILD_PATH_LIST(bapath->bitmapquals); new_path = (Path *) bapath; } @@ -4182,9 +4207,8 @@ do { \ case T_BitmapOrPath: { - BitmapOrPath *bopath; + BitmapOrPath *bopath = (BitmapOrPath *) path; - FLAT_COPY_PATH(bopath, path, BitmapOrPath); REPARAMETERIZE_CHILD_PATH_LIST(bopath->bitmapquals); new_path = (Path *) bopath; } @@ -4192,10 +4216,10 @@ do { \ case T_ForeignPath: { - ForeignPath *fpath; + ForeignPath *fpath = (ForeignPath *) path; ReparameterizeForeignPathByChild_function rfpc_func; - FLAT_COPY_PATH(fpath, path, ForeignPath); + ADJUST_CHILD_ATTRS(fpath->path.parent->baserestrictinfo); if (fpath->fdw_outerpath) REPARAMETERIZE_CHILD_PATH(fpath->fdw_outerpath); if (fpath->fdw_restrictinfo) @@ -4213,9 +4237,9 @@ do { \ case T_CustomPath: { - CustomPath *cpath; + CustomPath *cpath = (CustomPath *) path; - FLAT_COPY_PATH(cpath, path, CustomPath); + ADJUST_CHILD_ATTRS(cpath->path.parent->baserestrictinfo); REPARAMETERIZE_CHILD_PATH_LIST(cpath->custom_paths); if (cpath->custom_restrictinfo) ADJUST_CHILD_ATTRS(cpath->custom_restrictinfo); @@ -4231,12 +4255,9 @@ do { \ case T_NestPath: { - JoinPath *jpath; - NestPath *npath; - - FLAT_COPY_PATH(npath, path, NestPath); + NestPath *npath = (NestPath *) path; + JoinPath *jpath = (JoinPath *) npath; - jpath = (JoinPath *) npath; REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath); REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath); ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo); @@ -4246,12 +4267,9 @@ do { \ case T_MergePath: { - JoinPath *jpath; - MergePath *mpath; + MergePath *mpath = (MergePath *) path; + JoinPath *jpath = (JoinPath *) mpath; - FLAT_COPY_PATH(mpath, path, MergePath); - - jpath = (JoinPath *) mpath; REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath); REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath); ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo); @@ -4262,12 +4280,9 @@ do { \ case T_HashPath: { - JoinPath *jpath; - HashPath *hpath; - - FLAT_COPY_PATH(hpath, path, HashPath); + HashPath *hpath = (HashPath *) path; + JoinPath *jpath = (JoinPath *) hpath; - jpath = (JoinPath *) hpath; REPARAMETERIZE_CHILD_PATH(jpath->outerjoinpath); REPARAMETERIZE_CHILD_PATH(jpath->innerjoinpath); ADJUST_CHILD_ATTRS(jpath->joinrestrictinfo); @@ -4278,9 +4293,8 @@ do { \ case T_AppendPath: { - AppendPath *apath; + AppendPath *apath = (AppendPath *) path; - FLAT_COPY_PATH(apath, path, AppendPath); REPARAMETERIZE_CHILD_PATH_LIST(apath->subpaths); new_path = (Path *) apath; } @@ -4288,9 +4302,8 @@ do { \ case T_MaterialPath: { - MaterialPath *mpath; + MaterialPath *mpath = (MaterialPath *) path; - FLAT_COPY_PATH(mpath, path, MaterialPath); REPARAMETERIZE_CHILD_PATH(mpath->subpath); new_path = (Path *) mpath; } @@ -4298,9 +4311,8 @@ do { \ case T_MemoizePath: { - MemoizePath *mpath; + MemoizePath *mpath = (MemoizePath *) path; - FLAT_COPY_PATH(mpath, path, MemoizePath); REPARAMETERIZE_CHILD_PATH(mpath->subpath); ADJUST_CHILD_ATTRS(mpath->param_exprs); new_path = (Path *) mpath; @@ -4309,16 +4321,14 @@ do { \ case T_GatherPath: { - GatherPath *gpath; + GatherPath *gpath = (GatherPath *) path; - FLAT_COPY_PATH(gpath, path, GatherPath); REPARAMETERIZE_CHILD_PATH(gpath->subpath); new_path = (Path *) gpath; } break; default: - /* We don't know how to reparameterize this path. */ return NULL; } @@ -4379,8 +4389,146 @@ do { \ } /* + * path_is_reparameterizable_by_child + * Given a path parameterized by the parent of the given child relation, + * see if it can be translated to be parameterized by the child relation. + * + * This must return true if and only if reparameterize_path_by_child() + * would succeed on this path. Currently it's sufficient to verify that + * the path and all of its subpaths (if any) are of the types handled by + * that function. However, subpaths that are not parameterized can be + * disregarded since they won't require translation. + */ +bool +path_is_reparameterizable_by_child(Path *path, RelOptInfo *child_rel) +{ +#define REJECT_IF_PATH_NOT_REPARAMETERIZABLE(path) \ +do { \ + if (!path_is_reparameterizable_by_child(path, child_rel)) \ + return false; \ +} while(0) + +#define REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(pathlist) \ +do { \ + if (!pathlist_is_reparameterizable_by_child(pathlist, child_rel)) \ + return false; \ +} while(0) + + /* + * If the path is not parameterized by the parent of the given relation, + * it doesn't need reparameterization. + */ + if (!path->param_info || + !bms_overlap(PATH_REQ_OUTER(path), child_rel->top_parent_relids)) + return true; + + /* + * Check that the path type is one that reparameterize_path_by_child() can + * handle, and recursively check subpaths. + */ + switch (nodeTag(path)) + { + case T_Path: + case T_IndexPath: + break; + + case T_BitmapHeapPath: + { + BitmapHeapPath *bhpath = (BitmapHeapPath *) path; + + REJECT_IF_PATH_NOT_REPARAMETERIZABLE(bhpath->bitmapqual); + } + break; + + case T_BitmapAndPath: + { + BitmapAndPath *bapath = (BitmapAndPath *) path; + + REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(bapath->bitmapquals); + } + break; + + case T_BitmapOrPath: + { + BitmapOrPath *bopath = (BitmapOrPath *) path; + + REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(bopath->bitmapquals); + } + break; + + case T_ForeignPath: + { + ForeignPath *fpath = (ForeignPath *) path; + + if (fpath->fdw_outerpath) + REJECT_IF_PATH_NOT_REPARAMETERIZABLE(fpath->fdw_outerpath); + } + break; + + case T_CustomPath: + { + CustomPath *cpath = (CustomPath *) path; + + REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(cpath->custom_paths); + } + break; + + case T_NestPath: + case T_MergePath: + case T_HashPath: + { + JoinPath *jpath = (JoinPath *) path; + + REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->outerjoinpath); + REJECT_IF_PATH_NOT_REPARAMETERIZABLE(jpath->innerjoinpath); + } + break; + + case T_AppendPath: + { + AppendPath *apath = (AppendPath *) path; + + REJECT_IF_PATH_LIST_NOT_REPARAMETERIZABLE(apath->subpaths); + } + break; + + case T_MaterialPath: + { + MaterialPath *mpath = (MaterialPath *) path; + + REJECT_IF_PATH_NOT_REPARAMETERIZABLE(mpath->subpath); + } + break; + + case T_MemoizePath: + { + MemoizePath *mpath = (MemoizePath *) path; + + REJECT_IF_PATH_NOT_REPARAMETERIZABLE(mpath->subpath); + } + break; + + case T_GatherPath: + { + GatherPath *gpath = (GatherPath *) path; + + REJECT_IF_PATH_NOT_REPARAMETERIZABLE(gpath->subpath); + } + break; + + default: + /* We don't know how to reparameterize this path. */ + return false; + } + + return true; +} + +/* * reparameterize_pathlist_by_child * Helper function to reparameterize a list of paths by given child rel. + * + * Returns NIL to indicate failure, so pathlist had better not be NIL. */ static List * reparameterize_pathlist_by_child(PlannerInfo *root, @@ -4406,3 +4554,23 @@ reparameterize_pathlist_by_child(PlannerInfo *root, return result; } + +/* + * pathlist_is_reparameterizable_by_child + * Helper function to check if a list of paths can be reparameterized. + */ +static bool +pathlist_is_reparameterizable_by_child(List *pathlist, RelOptInfo *child_rel) +{ + ListCell *lc; + + foreach(lc, pathlist) + { + Path *path = (Path *) lfirst(lc); + + if (!path_is_reparameterizable_by_child(path, child_rel)) + return false; + } + + return true; +} |