Skip to content

Commit

Permalink
[Bug Fix] SEGV on query with Left Outer Join (#7787)
Browse files Browse the repository at this point in the history
Fix the SEGV seen in #7787; it occurs because a column in the targetlist
of a worker subquery can contain a non-empty varnullingrels field if the
column is from the inner side of a left outer join.
The issue can also occur with the columns in the HAVING clause, and this
is also tested in the fix. The issue was triggered by the introduction
of the varnullingrels to Vars in Postgres 16 (2489d76c)

There is a related issue, #7705, where a non-empty varnullingrels
was incorrectly copied into the query tree for the combine query.
Here, a non-empty varnullingrels field of a var is incorrectly copied
into the query tree for a worker subquery.

The regress file from #7705 is used (and renamed) to also test this
(#7787). An alternative test output file is required for Postgres 15
because of an optimization to DISTINCT in Postgres 16 (1349d2790bf).
  • Loading branch information
colm-mchugh committed Dec 19, 2024
1 parent 0355b12 commit 8b9d71b
Show file tree
Hide file tree
Showing 5 changed files with 677 additions and 34 deletions.
10 changes: 10 additions & 0 deletions src/backend/distributed/planner/query_pushdown_planning.c
Original file line number Diff line number Diff line change
Expand Up @@ -2049,6 +2049,16 @@ CreateSubqueryTargetListAndAdjustVars(List *columnList)
*/
column->varno = 1;
column->varattno = resNo;

/*
* 1 subquery means there is one range table entry so with Postgres 16+ we need
* to ensure that column's varnullingrels - the set of join rels that can null
* the var - is empty. Otherwise, when given the query, the Postgres planner
* may attempt to access a non-existent range table and segfault, as in #7787.
*/
#if PG_VERSION_NUM >= PG_VERSION_16
column->varnullingrels = NULL;
#endif
}

return subqueryTargetEntryList;
Expand Down
Loading

0 comments on commit 8b9d71b

Please sign in to comment.