Skip to content

Commit 89d5351

Browse files
committed
In planner.c, avoid assuming that all PathTargets have sortgrouprefs.
The struct definition for PathTarget specifies that a NULL sortgrouprefs pointer means no sortgroupref labels. While it's likely that there should always be at least one labeled column in the places that were unconditionally fetching through the pointer, it seems wiser to adhere to the data structure specification and test first. Add a macro to make this convenient. Per experimentation with running the regression tests with a very small parallelization threshold --- the crash I observed may well represent a bug elsewhere, but still this coding was not very robust. Report: <20756.1465834072@sss.pgh.pa.us>
1 parent cd9b4f2 commit 89d5351

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4222,7 +4222,7 @@ make_group_input_target(PlannerInfo *root, PathTarget *final_target)
42224222
foreach(lc, final_target->exprs)
42234223
{
42244224
Expr *expr = (Expr *) lfirst(lc);
4225-
Index sgref = final_target->sortgrouprefs[i];
4225+
Index sgref = get_pathtarget_sortgroupref(final_target, i);
42264226

42274227
if (sgref && parse->groupClause &&
42284228
get_sortgroupref_clause_noerr(sgref, parse->groupClause) != NULL)
@@ -4304,7 +4304,7 @@ make_partialgroup_input_target(PlannerInfo *root, PathTarget *final_target)
43044304
foreach(lc, final_target->exprs)
43054305
{
43064306
Expr *expr = (Expr *) lfirst(lc);
4307-
Index sgref = final_target->sortgrouprefs[i];
4307+
Index sgref = get_pathtarget_sortgroupref(final_target, i);
43084308

43094309
if (sgref && parse->groupClause &&
43104310
get_sortgroupref_clause_noerr(sgref, parse->groupClause) != NULL)
@@ -4556,7 +4556,7 @@ make_window_input_target(PlannerInfo *root,
45564556
foreach(lc, final_target->exprs)
45574557
{
45584558
Expr *expr = (Expr *) lfirst(lc);
4559-
Index sgref = final_target->sortgrouprefs[i];
4559+
Index sgref = get_pathtarget_sortgroupref(final_target, i);
45604560

45614561
/*
45624562
* Don't want to deconstruct window clauses or GROUP BY items. (Note
@@ -4757,7 +4757,7 @@ make_sort_input_target(PlannerInfo *root,
47574757
* only be Vars anyway. There don't seem to be any cases where it
47584758
* would be worth the trouble to double-check.
47594759
*/
4760-
if (final_target->sortgrouprefs[i] == 0)
4760+
if (get_pathtarget_sortgroupref(final_target, i) == 0)
47614761
{
47624762
/*
47634763
* Check for SRF or volatile functions. Check the SRF case first
@@ -4847,7 +4847,7 @@ make_sort_input_target(PlannerInfo *root,
48474847
postponable_cols = lappend(postponable_cols, expr);
48484848
else
48494849
add_column_to_pathtarget(input_target, expr,
4850-
final_target->sortgrouprefs[i]);
4850+
get_pathtarget_sortgroupref(final_target, i));
48514851

48524852
i++;
48534853
}

src/include/nodes/relation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,6 +783,10 @@ typedef struct PathTarget
783783
int width; /* estimated avg width of result tuples */
784784
} PathTarget;
785785

786+
/* Convenience macro to get a sort/group refno from a PathTarget */
787+
#define get_pathtarget_sortgroupref(target, colno) \
788+
((target)->sortgrouprefs ? (target)->sortgrouprefs[colno] : (Index) 0)
789+
786790

787791
/*
788792
* ParamPathInfo

0 commit comments

Comments
 (0)