Skip to content

Commit ae0944b

Browse files
jrgemignaniMuhammadTahaNaveed
authored andcommitted
Fix Issue 2256: segmentation fault when calling coalesce function (#2259)
Fixed issue 2256: A segmentation fault occurs when calling the coalesce function in PostgreSQL version 17. This likely predates 17 and includes other similar types of "functions". And other versions of PostgreSQL. See issues 1124 (PR 1125) and 1303 (PR 1317) for more details. This issue is due to coalesce() being processed differently from other functions. Additionally, greatest() was found to exhibit the same behavior. They were added to the list of types to ignore during the cypher analyze phase. A few others were added: CaseExpr, XmlExpr, ArrayExpr, & RowExpr. Although, I wasn't able to find cases where these caused crashes. Added regression tests. modified: regress/expected/cypher.out modified: regress/sql/cypher.sql modified: src/backend/parser/cypher_analyze.c Conflicts: src/backend/parser/cypher_analyze.c
1 parent 482d468 commit ae0944b

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

regress/expected/cypher.out

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ CREATE TABLE my_edges AS
169169
-- create a table of 4 columns, u, e, v, p. should be 5 rows
170170
CREATE TABLE my_detailed_paths AS
171171
(SELECT * FROM cypher('issue_1767', $$ MATCH p=(u)-[e]->(v) RETURN u,e,v,p $$) as (u agtype, e agtype, v agtype, p agtype));
172+
--
173+
-- Issue 2256: A segmentation fault occurs when calling the coalesce function
174+
-- This also occurs with the greatest function too.
175+
--
176+
SELECT * FROM coalesce(1, 0);
177+
coalesce
178+
----------
179+
1
180+
(1 row)
181+
182+
SELECT * FROM greatest(1, 0);
183+
greatest
184+
----------
185+
1
186+
(1 row)
187+
172188
-- dump out the tables
173189
SELECT * FROM my_vertices;
174190
u

regress/sql/cypher.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ CREATE TABLE my_edges AS
9494
CREATE TABLE my_detailed_paths AS
9595
(SELECT * FROM cypher('issue_1767', $$ MATCH p=(u)-[e]->(v) RETURN u,e,v,p $$) as (u agtype, e agtype, v agtype, p agtype));
9696

97+
--
98+
-- Issue 2256: A segmentation fault occurs when calling the coalesce function
99+
-- This also occurs with the greatest function too.
100+
--
101+
SELECT * FROM coalesce(1, 0);
102+
SELECT * FROM greatest(1, 0);
103+
97104
-- dump out the tables
98105
SELECT * FROM my_vertices;
99106
SELECT * FROM my_edges;

src/backend/parser/cypher_analyze.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,20 @@ static bool convert_cypher_walker(Node *node, ParseState *pstate)
169169
* Const - constant value or expression node
170170
* BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
171171
*
172-
* These are a special case that needs to be ignored.
172+
* Added the following, although only the first 2 caused crashes in tests -
173+
* CoalesceExpr, MinMaxExpr, CaseExpr, XmlExpr, ArrayExpr, RowExpr
174+
*
175+
* These are all special case that needs to be ignored.
173176
*
174177
*/
175178
if (IsA(funcexpr, SQLValueFunction)
176-
|| IsA(funcexpr, CoerceViaIO)
177-
|| IsA(funcexpr, Var) || IsA(funcexpr, OpExpr)
178-
|| IsA(funcexpr, Const) || IsA(funcexpr, BoolExpr))
179+
|| IsA(funcexpr, CoerceViaIO)
180+
|| IsA(funcexpr, Var) || IsA(funcexpr, OpExpr)
181+
|| IsA(funcexpr, Const) || IsA(funcexpr, BoolExpr)
182+
|| IsA(funcexpr, JsonConstructorExpr)
183+
|| IsA(funcexpr, CoalesceExpr) || IsA(funcexpr, MinMaxExpr)
184+
|| IsA(funcexpr, CaseExpr) || IsA(funcexpr, XmlExpr)
185+
|| IsA(funcexpr, ArrayExpr) || IsA(funcexpr, RowExpr))
179186
{
180187
return false;
181188
}
@@ -341,13 +348,20 @@ static bool is_func_cypher(FuncExpr *funcexpr)
341348
* Const - constant value or expression node
342349
* BoolExpr - expression node for the basic Boolean operators AND, OR, NOT
343350
*
344-
* These are a special case that needs to be ignored.
351+
* Added the following, although only the first 2 caused crashes in tests -
352+
* CoalesceExpr, MinMaxExpr, CaseExpr, XmlExpr, ArrayExpr, RowExpr
353+
*
354+
* These are all special case that needs to be ignored.
345355
*
346356
*/
347357
if (IsA(funcexpr, SQLValueFunction)
348358
|| IsA(funcexpr, CoerceViaIO)
349359
|| IsA(funcexpr, Var) || IsA(funcexpr, OpExpr)
350-
|| IsA(funcexpr, Const) || IsA(funcexpr, BoolExpr))
360+
|| IsA(funcexpr, Const) || IsA(funcexpr, BoolExpr)
361+
|| IsA(funcexpr, JsonConstructorExpr)
362+
|| IsA(funcexpr, CoalesceExpr) || IsA(funcexpr, MinMaxExpr)
363+
|| IsA(funcexpr, CaseExpr) || IsA(funcexpr, XmlExpr)
364+
|| IsA(funcexpr, ArrayExpr) || IsA(funcexpr, RowExpr))
351365
{
352366
return false;
353367
}

0 commit comments

Comments
 (0)