diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index 211ba65389..6c801ccf1e 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -3766,6 +3766,92 @@ create_limit_path(PlannerInfo *root, RelOptInfo *rel, LimitOption limitOption, int64 offset_est, int64 count_est) { + /* + Pushdown limit to children of Append or MergeAppend. + This is safe only when there is no offset or offset is 0 + Ignore the case when offset is 0 for now + */ + if (limitOffset == NULL) { + switch (subpath->pathtype) + { + case T_Append: + { + AppendPath *appendPath = (AppendPath *) subpath; + List *children = NIL; + List *partialChildren = NIL; + ListCell *l; + int i; + i = 0; + foreach(l, appendPath->subpaths) { + Path *child = (Path*) lfirst(l); + child = create_limit_path( + root, + child->parent, + child, + limitOffset, + limitCount, + limitOption, + offset_est, + count_est); + if (i < appendPath->first_partial_path) { + children = lappend(children, child); + } + else { + partialChildren = lappend(partialChildren, child); + } + i++; + } + Relids required_outer = NULL; + if (appendPath->path.param_info != NULL) { + required_outer = appendPath->path.param_info->ppi_req_outer; + } + subpath = (Path *) create_append_path( + root, + subpath->parent, + children, + partialChildren, + appendPath->path.pathkeys, + required_outer, + appendPath->path.parallel_workers, + appendPath->path.parallel_aware, + -1); + break; + } + case T_MergeAppend: + { + MergeAppendPath *mergeAppendPath = (MergeAppendPath*) subpath; + List *children = NIL; + ListCell *l; + foreach(l, mergeAppendPath->subpaths) { + Path *child = (Path*) lfirst(l); + child = create_limit_path( + root, + child->parent, + child, + limitOffset, + limitCount, + limitOption, + offset_est, + count_est); + children = lappend(children, child); + } + Relids required_outer = NULL; + if (mergeAppendPath->path.param_info != NULL) { + required_outer = mergeAppendPath->path.param_info->ppi_req_outer; + } + subpath = (Path *) create_merge_append_path( + root, + mergeAppendPath->path.parent, + children, + mergeAppendPath->path.pathkeys, + required_outer); + break; + } + default: + break; + } + } + LimitPath *pathnode = makeNode(LimitPath); pathnode->path.pathtype = T_Limit;