diff --git a/src/backend/optimizer/util/tlist.c b/src/backend/optimizer/util/tlist.c index aa2c2f8..c6ec785 100644 --- a/src/backend/optimizer/util/tlist.c +++ b/src/backend/optimizer/util/tlist.c @@ -553,15 +553,24 @@ make_pathtarget_from_tlist(List *tlist) int i; ListCell *lc; - target->sortgrouprefs = (Index *) palloc(list_length(tlist) * sizeof(Index)); - i = 0; foreach(lc, tlist) { TargetEntry *tle = (TargetEntry *) lfirst(lc); target->exprs = lappend(target->exprs, tle->expr); - target->sortgrouprefs[i] = tle->ressortgroupref; + + if (tle->ressortgroupref) + { + /* + * We only allocate this array if there's a valid sortgroupref to + * store. + */ + if (!target->sortgrouprefs) + target->sortgrouprefs = (Index *) palloc0(list_length(tlist) * sizeof(Index)); + + target->sortgrouprefs[i] = tle->ressortgroupref; + } i++; }