pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
9+ messages / 4 participants
[nested] [flat]
Commits from this thread:
2026-11-18 34574ebad5 e755aff218 59f0c2be1e 64f45756ed 5c9da8b0e2 f76c8480f5 Don't crash on null pstate in coerce_record_to_complex().
* BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
@ 2026-11-15 18:38 PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: PG Bug reporting form @ 2026-11-15 18:38 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: murasovb@gmail.com
The following bug has been logged on the website:
Bug reference: 19906
Logged by: Boris
Email address: murasovb@gmail.com
PostgreSQL version: 18.7
Operating system: Debian 13 (trixie), x86_64, Docker image pgvector/
Description:
PostgreSQL version:
PostgreSQL 18.7 (Debian 18.7-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by
gcc (Debian 14.2.0-19) 14.2.0, 64-bit
(official Debian package in the pgvector/pgvector:0.8.7-pg18-trixie Docker
image; default configuration; empty database)
Minimal reproduction:
CREATE TABLE t (id int, v text);
CREATE FUNCTION m1() RETURNS TABLE (x t) LANGUAGE sql
AS $$ SELECT s FROM (SELECT t.* FROM t) s $$;
Result: "server closed the connection unexpectedly". The server log shows:
client backend (PID ...) was terminated by signal 11: Segmentation fault
and the postmaster runs crash recovery.
The CREATE FUNCTION alone crashes (check_function_bodies = on). The table
can be empty.
Variations (one change at a time):
1. RETURNS TABLE (x t), body SELECT s FROM (SELECT t.* FROM t) s
-> crash
2. RETURNS SETOF t, same body
-> crash
3. RETURNS TABLE (x t), body SELECT t FROM t (whole row of the table
itself) -> OK
4. RETURNS TABLE (n int, x t), body SELECT 1, s FROM (SELECT t.* FROM t) s
-> crash
5. RETURNS t (single row), body of 1
-> crash
6. SET check_function_bodies = off, create function 1, then SELECT * FROM
m1() -> crash at the call
7. Same body in LANGUAGE plpgsql with RETURN QUERY
-> error "structure of query does not match function result type", no crash
8. Plain statement SELECT s::t FROM (SELECT t.* FROM t) s
-> OK
9. RETURNS TABLE (x t), body SELECT ROW(s.*) FROM (SELECT t.* FROM t) s
-> OK
10. RETURNS TABLE (id int, v text), body SELECT s.id, s.v FROM (SELECT t.*
FROM t) s -> OK
11. RETURNS SETOF t, body SELECT s FROM (SELECT id, v FROM t) s
-> crash
Summary: a LANGUAGE sql function whose result (a column, SETOF, or a single
row) is a named composite type,
and whose final SELECT returns the whole-row reference of a sub-select in
FROM (a value of type record that
must be converted to that composite type), crashes the backend. PL/pgSQL
reports an error instead.
SET clauses, SECURITY INVOKER/DEFINER, volatility and ROWS are not needed to
trigger it.
Workaround: return explicit columns, or ROW(s.*), instead of the bare
whole-row reference.
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
@ 2026-11-16 18:56 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 9+ messages in thread
From: Ayush Tiwari @ 2026-11-16 18:56 UTC (permalink / raw)
To: murasovb@gmail.com, pgsql-bugs@lists.postgresql.org, Tom Lane <tgl@sss.pgh.pa.us>
Hi,
On Mon, 16 Nov 2026 at 20:47, PG Bug reporting form
<noreply@postgresql.org> wrote:
>
> The following bug has been logged on the website:
>
> Bug reference: 19906
> Logged by: Boris
> Email address: murasovb@gmail.com
> PostgreSQL version: 18.7
> Operating system: Debian 13 (trixie), x86_64, Docker image pgvector/
> Description:
>
> PostgreSQL version:
> PostgreSQL 18.7 (Debian 18.7-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by
> gcc (Debian 14.2.0-19) 14.2.0, 64-bit
> (official Debian package in the pgvector/pgvector:0.8.7-pg18-trixie Docker
> image; default configuration; empty database)
>
> Minimal reproduction:
>
> CREATE TABLE t (id int, v text);
> CREATE FUNCTION m1() RETURNS TABLE (x t) LANGUAGE sql
> AS $$ SELECT s FROM (SELECT t.* FROM t) s $$;
>
> Result: "server closed the connection unexpectedly". The server log shows:
> client backend (PID ...) was terminated by signal 11: Segmentation fault
> and the postmaster runs crash recovery.
>
Thanks for the report and repro.
The crash can be fixed by checking for pstate not being NULL in
coerce_record_to_complex(), which makes it error out with "cannot cast
type record to t". But I'm unsure if we should do just that, or rather
teach check_sql_fn_retval() to expand the whole-row Var so that this
works like s::t?
Regards,
Ayush
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
@ 2026-11-16 19:28 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 20:23 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Борис Мурашов <murasovb@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Tom Lane @ 2026-11-16 19:28 UTC (permalink / raw)
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: murasovb@gmail.com; pgsql-bugs@lists.postgresql.org
Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> The crash can be fixed by checking for pstate not being NULL in
> coerce_record_to_complex(), which makes it error out with "cannot cast
> type record to t". But I'm unsure if we should do just that, or rather
> teach check_sql_fn_retval() to expand the whole-row Var so that this
> works like s::t?
Yeah, I was just looking at this too. It's been a crash case since
v13, and before that you got
ERROR: return type mismatch in function declared to return t
DETAIL: Final statement returns record instead of integer at column 1.
CONTEXT: SQL function "m1"
So it's never worked, and there's been no complaints to speak of,
and it's easy to work around:
CREATE FUNCTION m1() RETURNS TABLE (x t) LANGUAGE sql
AS $$ SELECT s::t FROM (SELECT t.* FROM t) s $$;
So I think that putting in a defense against null pstate in
coerce_record_to_complex() is sufficient. Making it work would be
very very tedious (I don't think we have any prefab code that
would be suitable) and evidently there's no demand.
Actually the annoying part of this is that we ought to change the
comments elsewhere in parse_coerce.c that say that pstate is only
used to return data about Param types, because that's evidently a
lie (and has been since well before v13...).
regards, tom lane
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-11-16 20:23 ` Борис Мурашов <murasovb@gmail.com>
2026-11-16 22:46 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 9+ messages in thread
From: Борис Мурашов @ 2026-11-16 20:23 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; pgsql-bugs@lists.postgresql.org
Hi Ayush, Tom,
Thank you both for the quick analysis.
I understand the decision to add the NULL check in
coerce_record_to_complex(). This form has never worked, and the workaround
is simple.
One request from the user side: the new error "cannot cast type record to
t" does not help much here. The function body looks correct, and the same
query works as a plain SELECT, so it is not clear what to change. Is it
possible to add an errhint() that tells the user to cast the row
explicitly, for example "Cast the row value to type t explicitly"? This
hint shows users the s::t workaround at once.
If a patch is available, I can test it on 18.x with our original query.
Regards,
Boris
пн, 16 нояб. 2026 г. в 21:29, Tom Lane <tgl@sss.pgh.pa.us>:
> Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> > The crash can be fixed by checking for pstate not being NULL in
> > coerce_record_to_complex(), which makes it error out with "cannot cast
> > type record to t". But I'm unsure if we should do just that, or rather
> > teach check_sql_fn_retval() to expand the whole-row Var so that this
> > works like s::t?
>
> Yeah, I was just looking at this too. It's been a crash case since
> v13, and before that you got
>
> ERROR: return type mismatch in function declared to return t
> DETAIL: Final statement returns record instead of integer at column 1.
> CONTEXT: SQL function "m1"
>
> So it's never worked, and there's been no complaints to speak of,
> and it's easy to work around:
>
> CREATE FUNCTION m1() RETURNS TABLE (x t) LANGUAGE sql
> AS $$ SELECT s::t FROM (SELECT t.* FROM t) s $$;
>
> So I think that putting in a defense against null pstate in
> coerce_record_to_complex() is sufficient. Making it work would be
> very very tedious (I don't think we have any prefab code that
> would be suitable) and evidently there's no demand.
>
> Actually the annoying part of this is that we ought to change the
> comments elsewhere in parse_coerce.c that say that pstate is only
> used to return data about Param types, because that's evidently a
> lie (and has been since well before v13...).
>
> regards, tom lane
>
--
SDH "StudentDrawingHelper"
Aircraft engineer
Boris Murasov
Mob.: +370 63586809
E-mail: murasovb@gmail.com
E-mail2: sdh@drawinghelper.eu
WEB: www.drawinghelper.eu
<http://www.camcons.lt;
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 20:23 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Борис Мурашов <murasovb@gmail.com>
@ 2026-11-16 22:46 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 01:45 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Tom Lane @ 2026-11-16 22:46 UTC (permalink / raw)
To: Борис Мурашов <murasovb@gmail.com>; +Cc: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; pgsql-bugs@lists.postgresql.org
=?UTF-8?B?0JHQvtGA0LjRgSDQnNGD0YDQsNGI0L7Qsg==?= <murasovb@gmail.com> writes:
> One request from the user side: the new error "cannot cast type record to
> t" does not help much here. The function body looks correct, and the same
> query works as a plain SELECT, so it is not clear what to change. Is it
> possible to add an errhint() that tells the user to cast the row
> explicitly, for example "Cast the row value to type t explicitly"? This
> hint shows users the s::t workaround at once.
Okay, more or less as attached then.
regards, tom lane
Attachments:
draft-fix-coerce_record_to_complex.patch (text/x-diff, 6.3K) download
diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c
index d3240f4b265..bd4e12c9989 100644
--- a/src/backend/parser/parse_coerce.c
+++ b/src/backend/parser/parse_coerce.c
@@ -147,9 +147,14 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
* applies length conversion, and in those cases only, the result will
* already be properly coerced to the specified typmod.
*
- * pstate is only used in the case that we are able to resolve the type of
- * a previously UNKNOWN Param. It is okay to pass pstate = NULL if the
- * caller does not want type information updated for Params.
+ * pstate can be NULL in calls from outside the parser. This mostly works,
+ * but note the following restrictions:
+ * 1. No p_coerce_param_hook processing is possible, so Params receive
+ * no special treatment.
+ * 2. Whole-row Vars of type RECORD (those referring to non-table FROM items
+ * such as joins or sub-selects) cannot be cast to named composite types,
+ * since we can't resolve their columns.
+ * 3. No error position information will be provided.
*
* Note: this function must not modify the given expression tree, only add
* decoration on top of it. See transformSetOperationTree, for example.
@@ -1009,7 +1014,7 @@ build_coercion_expression(Node *node,
* Coerce a RECORD to a specific composite type.
*
* Currently we only support this for inputs that are RowExprs or whole-row
- * Vars.
+ * Vars. pstate == NULL prevents handling the latter case.
*/
static Node *
coerce_record_to_complex(ParseState *pstate, Node *node,
@@ -1039,12 +1044,22 @@ coerce_record_to_complex(ParseState *pstate, Node *node,
else if (node && IsA(node, Var) &&
((Var *) node)->varattno == InvalidAttrNumber)
{
- Var *var = (Var *) node;
- ParseNamespaceItem *nsitem;
+ if (pstate)
+ {
+ Var *var = (Var *) node;
+ ParseNamespaceItem *nsitem;
- nsitem = GetNSItemByVar(pstate, var);
- args = expandNSItemVars(pstate, nsitem, var->varlevelsup,
- var->location, NULL);
+ nsitem = GetNSItemByVar(pstate, var);
+ args = expandNSItemVars(pstate, nsitem, var->varlevelsup,
+ var->location, NULL);
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_CANNOT_COERCE),
+ errmsg("cannot cast type %s to %s",
+ format_type_be(RECORDOID),
+ format_type_be(targetTypeId)),
+ errhint("Cast the whole-row variable to the desired type explicitly.")));
}
else
ereport(ERROR,
@@ -1156,8 +1171,7 @@ coerce_record_to_complex(ParseState *pstate, Node *node,
*
* Returns the possibly-transformed node tree.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_boolean(ParseState *pstate, Node *node,
@@ -1203,8 +1217,7 @@ coerce_to_boolean(ParseState *pstate, Node *node,
*
* Returns the possibly-transformed node tree.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_specific_type_typmod(ParseState *pstate, Node *node,
@@ -1252,8 +1265,7 @@ coerce_to_specific_type_typmod(ParseState *pstate, Node *node,
*
* Returns the possibly-transformed node tree.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_specific_type(ParseState *pstate, Node *node,
@@ -1330,6 +1342,7 @@ parser_coercion_errposition(ParseState *pstate,
* This is used for determining the output type of CASE, UNION,
* and similar constructs.
*
+ * 'pstate' is used only for error reporting, and can be NULL.
* 'exprs' is a *nonempty* list of expressions. Note that earlier items
* in the list will be preferred if there is doubt.
* 'context' is a phrase to use in the error message if we fail to select
@@ -1569,8 +1582,7 @@ select_common_type_from_oids(int nargs, const Oid *typeids, bool noerror)
* expressions to the desired type. 'context' is a phrase to use in the
* error message if we fail to coerce.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_common_type(ParseState *pstate, Node *node,
diff --git a/src/test/regress/expected/rangefuncs.out b/src/test/regress/expected/rangefuncs.out
index a7cb1b5611d..1f52b149907 100644
--- a/src/test/regress/expected/rangefuncs.out
+++ b/src/test/regress/expected/rangefuncs.out
@@ -2,7 +2,14 @@ CREATE TABLE rngfunc2(rngfuncid int, f2 int);
INSERT INTO rngfunc2 VALUES(1, 11);
INSERT INTO rngfunc2 VALUES(2, 22);
INSERT INTO rngfunc2 VALUES(1, 111);
-CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as
+ 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+-- this variant doesn't work
+CREATE FUNCTION rngfunct2(int) returns setof rngfunc2 as
+ 'SELECT s FROM (select * from rngfunc2) s;' LANGUAGE SQL;
+ERROR: cannot cast type record to rngfunc2
+HINT: Cast the whole-row variable to the desired type explicitly.
+CONTEXT: SQL function "rngfunct2"
-- function with ORDINALITY
select * from rngfunct(1) with ordinality as z(a,b,ord);
a | b | ord
diff --git a/src/test/regress/sql/rangefuncs.sql b/src/test/regress/sql/rangefuncs.sql
index 3c47c98e113..4780c78395b 100644
--- a/src/test/regress/sql/rangefuncs.sql
+++ b/src/test/regress/sql/rangefuncs.sql
@@ -3,7 +3,12 @@ INSERT INTO rngfunc2 VALUES(1, 11);
INSERT INTO rngfunc2 VALUES(2, 22);
INSERT INTO rngfunc2 VALUES(1, 111);
-CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as
+ 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+
+-- this variant doesn't work
+CREATE FUNCTION rngfunct2(int) returns setof rngfunc2 as
+ 'SELECT s FROM (select * from rngfunc2) s;' LANGUAGE SQL;
-- function with ORDINALITY
select * from rngfunct(1) with ordinality as z(a,b,ord);
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 20:23 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Борис Мурашов <murasovb@gmail.com>
2026-11-16 22:46 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-11-17 01:45 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-17 16:34 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 9+ messages in thread
From: Ayush Tiwari @ 2026-11-17 01:45 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Борис Мурашов <murasovb@gmail.com>; pgsql-bugs@lists.postgresql.org
Hi,
On Tue, 17 Nov 2026 at 04:16, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> =?UTF-8?B?0JHQvtGA0LjRgSDQnNGD0YDQsNGI0L7Qsg==?= <murasovb@gmail.com> writes:
> > One request from the user side: the new error "cannot cast type record to
> > t" does not help much here. The function body looks correct, and the same
> > query works as a plain SELECT, so it is not clear what to change. Is it
> > possible to add an errhint() that tells the user to cast the row
> > explicitly, for example "Cast the row value to type t explicitly"? This
> > hint shows users the s::t workaround at once.
>
> Okay, more or less as attached then.
Thanks, LGTM. Tested the cases from the report, both at create time
and at call time with check_function_bodies = off, all of them give
the error with the hint now.
Post your last mail, I was auditing the places that need these comment
updates, and the difference between that list and your patch is:
1) make_fn_arguments() in parse_func.c still has the old comment, and
clauses.c calls it with NULL.
2) make_row_comparison_op() in parse_expr.c has it too, but it
dereferences pstate anyway, so that line can just go?
Regards,
Ayush
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 20:23 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Борис Мурашов <murasovb@gmail.com>
2026-11-16 22:46 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 01:45 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
@ 2026-11-17 16:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 16:59 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
0 siblings, 1 reply; 9+ messages in thread
From: Tom Lane @ 2026-11-17 16:34 UTC (permalink / raw)
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: Борис Мурашов <murasovb@gmail.com>; pgsql-bugs@lists.postgresql.org
Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> Post your last mail, I was auditing the places that need these comment
> updates, and the difference between that list and your patch is:
> 1) make_fn_arguments() in parse_func.c still has the old comment, and
> clauses.c calls it with NULL.
> 2) make_row_comparison_op() in parse_expr.c has it too, but it
> dereferences pstate anyway, so that line can just go?
Good catches. Incorporated in less-drafty v2.
regards, tom lane
Attachments:
v2-0001-Don-t-crash-on-null-pstate-in-coerce_record_to_co.patch (text/x-diff, 9.2K) download
From 308386876e33e0a7569c53372af1050cb4a5d8bc Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 17 Nov 2026 11:32:47 -0500
Subject: [PATCH v2] Don't crash on null pstate in coerce_record_to_complex().
Since commit 913bbd88d, this case is reachable if a SQL-language
function returns a whole-row Var of type RECORD when its declared
output type is a named composite type. We'd need the original
ParseState to find out what the Var's columns are, but we don't have
that structure at hand anymore. This didn't work before 913bbd88d
either, but you got a slightly off-point error message instead of a
crash.
Given that this has never worked and there's been no field complaints
about that before now, it doesn't seem worth doing major work to fix
this; let's just ensure that we throw a helpful error explaining what
to do to work around the limitation. (The workaround actually reaches
this same code, but does so while we still have the ParseState.)
While at it, update various badly-obsolete comments about what
coerce_type() uses its pstate argument for.
Bug: #19906
Reported-by: Boris Murasov <murasovb@gmail.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Reviewed-by: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Discussion: https://postgr.es/m/19906-229033275b593ef0@postgresql.org
Backpatch-through: 15
---
src/backend/parser/parse_coerce.c | 46 +++++++++++++++---------
src/backend/parser/parse_expr.c | 4 +--
src/backend/parser/parse_func.c | 3 +-
src/test/regress/expected/rangefuncs.out | 9 ++++-
src/test/regress/sql/rangefuncs.sql | 7 +++-
5 files changed, 45 insertions(+), 24 deletions(-)
diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c
index d3240f4b265..bd4e12c9989 100644
--- a/src/backend/parser/parse_coerce.c
+++ b/src/backend/parser/parse_coerce.c
@@ -147,9 +147,14 @@ coerce_to_target_type(ParseState *pstate, Node *expr, Oid exprtype,
* applies length conversion, and in those cases only, the result will
* already be properly coerced to the specified typmod.
*
- * pstate is only used in the case that we are able to resolve the type of
- * a previously UNKNOWN Param. It is okay to pass pstate = NULL if the
- * caller does not want type information updated for Params.
+ * pstate can be NULL in calls from outside the parser. This mostly works,
+ * but note the following restrictions:
+ * 1. No p_coerce_param_hook processing is possible, so Params receive
+ * no special treatment.
+ * 2. Whole-row Vars of type RECORD (those referring to non-table FROM items
+ * such as joins or sub-selects) cannot be cast to named composite types,
+ * since we can't resolve their columns.
+ * 3. No error position information will be provided.
*
* Note: this function must not modify the given expression tree, only add
* decoration on top of it. See transformSetOperationTree, for example.
@@ -1009,7 +1014,7 @@ build_coercion_expression(Node *node,
* Coerce a RECORD to a specific composite type.
*
* Currently we only support this for inputs that are RowExprs or whole-row
- * Vars.
+ * Vars. pstate == NULL prevents handling the latter case.
*/
static Node *
coerce_record_to_complex(ParseState *pstate, Node *node,
@@ -1039,12 +1044,22 @@ coerce_record_to_complex(ParseState *pstate, Node *node,
else if (node && IsA(node, Var) &&
((Var *) node)->varattno == InvalidAttrNumber)
{
- Var *var = (Var *) node;
- ParseNamespaceItem *nsitem;
+ if (pstate)
+ {
+ Var *var = (Var *) node;
+ ParseNamespaceItem *nsitem;
- nsitem = GetNSItemByVar(pstate, var);
- args = expandNSItemVars(pstate, nsitem, var->varlevelsup,
- var->location, NULL);
+ nsitem = GetNSItemByVar(pstate, var);
+ args = expandNSItemVars(pstate, nsitem, var->varlevelsup,
+ var->location, NULL);
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_CANNOT_COERCE),
+ errmsg("cannot cast type %s to %s",
+ format_type_be(RECORDOID),
+ format_type_be(targetTypeId)),
+ errhint("Cast the whole-row variable to the desired type explicitly.")));
}
else
ereport(ERROR,
@@ -1156,8 +1171,7 @@ coerce_record_to_complex(ParseState *pstate, Node *node,
*
* Returns the possibly-transformed node tree.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_boolean(ParseState *pstate, Node *node,
@@ -1203,8 +1217,7 @@ coerce_to_boolean(ParseState *pstate, Node *node,
*
* Returns the possibly-transformed node tree.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_specific_type_typmod(ParseState *pstate, Node *node,
@@ -1252,8 +1265,7 @@ coerce_to_specific_type_typmod(ParseState *pstate, Node *node,
*
* Returns the possibly-transformed node tree.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_specific_type(ParseState *pstate, Node *node,
@@ -1330,6 +1342,7 @@ parser_coercion_errposition(ParseState *pstate,
* This is used for determining the output type of CASE, UNION,
* and similar constructs.
*
+ * 'pstate' is used only for error reporting, and can be NULL.
* 'exprs' is a *nonempty* list of expressions. Note that earlier items
* in the list will be preferred if there is doubt.
* 'context' is a phrase to use in the error message if we fail to select
@@ -1569,8 +1582,7 @@ select_common_type_from_oids(int nargs, const Oid *typeids, bool noerror)
* expressions to the desired type. 'context' is a phrase to use in the
* error message if we fail to coerce.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
Node *
coerce_to_common_type(ParseState *pstate, Node *node,
diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c
index 728392f436f..e207b7a1978 100644
--- a/src/backend/parser/parse_expr.c
+++ b/src/backend/parser/parse_expr.c
@@ -2826,9 +2826,7 @@ transformCollateClause(ParseState *pstate, CollateClause *c)
/*
* Transform a "row compare-op row" construct
*
- * The inputs are lists of already-transformed expressions.
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * The argument lists are lists of already-transformed expressions.
*
* The output may be a single OpExpr, an AND or OR combination of OpExprs,
* or a RowCompareExpr. In all cases it is guaranteed to return boolean.
diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c
index 410e54accd8..c4ba284fa18 100644
--- a/src/backend/parser/parse_func.c
+++ b/src/backend/parser/parse_func.c
@@ -2005,8 +2005,7 @@ unify_hypothetical_args(ParseState *pstate,
*
* Caution: given argument list is modified in-place.
*
- * As with coerce_type, pstate may be NULL if no special unknown-Param
- * processing is wanted.
+ * As with coerce_type, pstate may be NULL for calls from outside the parser.
*/
void
make_fn_arguments(ParseState *pstate,
diff --git a/src/test/regress/expected/rangefuncs.out b/src/test/regress/expected/rangefuncs.out
index a7cb1b5611d..a9ad3687887 100644
--- a/src/test/regress/expected/rangefuncs.out
+++ b/src/test/regress/expected/rangefuncs.out
@@ -2,7 +2,14 @@ CREATE TABLE rngfunc2(rngfuncid int, f2 int);
INSERT INTO rngfunc2 VALUES(1, 11);
INSERT INTO rngfunc2 VALUES(2, 22);
INSERT INTO rngfunc2 VALUES(1, 111);
-CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as
+ 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+-- this variant doesn't work
+CREATE FUNCTION rngfunct2(int) returns setof rngfunc2 as
+ 'SELECT s FROM (SELECT * FROM rngfunc2) s;' LANGUAGE SQL;
+ERROR: cannot cast type record to rngfunc2
+HINT: Cast the whole-row variable to the desired type explicitly.
+CONTEXT: SQL function "rngfunct2"
-- function with ORDINALITY
select * from rngfunct(1) with ordinality as z(a,b,ord);
a | b | ord
diff --git a/src/test/regress/sql/rangefuncs.sql b/src/test/regress/sql/rangefuncs.sql
index 3c47c98e113..1d35f938fd4 100644
--- a/src/test/regress/sql/rangefuncs.sql
+++ b/src/test/regress/sql/rangefuncs.sql
@@ -3,7 +3,12 @@ INSERT INTO rngfunc2 VALUES(1, 11);
INSERT INTO rngfunc2 VALUES(2, 22);
INSERT INTO rngfunc2 VALUES(1, 111);
-CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+CREATE FUNCTION rngfunct(int) returns setof rngfunc2 as
+ 'SELECT * FROM rngfunc2 WHERE rngfuncid = $1 ORDER BY f2;' LANGUAGE SQL;
+
+-- this variant doesn't work
+CREATE FUNCTION rngfunct2(int) returns setof rngfunc2 as
+ 'SELECT s FROM (SELECT * FROM rngfunc2) s;' LANGUAGE SQL;
-- function with ORDINALITY
select * from rngfunct(1) with ordinality as z(a,b,ord);
--
2.52.0
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 20:23 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Борис Мурашов <murasovb@gmail.com>
2026-11-16 22:46 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 01:45 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-17 16:34 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
@ 2026-11-17 16:59 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-18 02:57 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 9+ messages in thread
From: Ayush Tiwari @ 2026-11-17 16:59 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Борис Мурашов <murasovb@gmail.com>; pgsql-bugs@lists.postgresql.org
Hi,
On Tue, 17 Nov 2026 at 22:04, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> > Post your last mail, I was auditing the places that need these comment
> > updates, and the difference between that list and your patch is:
> > 1) make_fn_arguments() in parse_func.c still has the old comment, and
> > clauses.c calls it with NULL.
> > 2) make_row_comparison_op() in parse_expr.c has it too, but it
> > dereferences pstate anyway, so that line can just go?
>
> Good catches. Incorporated in less-drafty v2.
Thanks for updating the patch, lgtm.
Regards,
Ayush
^ permalink raw reply [nested|flat] 9+ messages in thread
* Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub-
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 20:23 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Борис Мурашов <murasovb@gmail.com>
2026-11-16 22:46 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 01:45 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-17 16:34 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 16:59 ` Re: BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- Ayush Tiwari <ayushtiwari.slg01@gmail.com>
@ 2026-11-18 02:57 ` Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 9+ messages in thread
From: Tom Lane @ 2026-11-18 02:57 UTC (permalink / raw)
To: Ayush Tiwari <ayushtiwari.slg01@gmail.com>; +Cc: Борис Мурашов <murasovb@gmail.com>; pgsql-bugs@lists.postgresql.org
Ayush Tiwari <ayushtiwari.slg01@gmail.com> writes:
> Thanks for updating the patch, lgtm.
Pushed, thanks for reviewing.
regards, tom lane
^ permalink raw reply [nested|flat] 9+ messages in thread
end of thread, other threads:[~2026-11-18 02:57 UTC | newest]
Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-11-15 18:38 BUG #19906: Backend crash (signal 11) creating a LANGUAGE sql function that returns a composite type from a sub- PG Bug reporting form <noreply@postgresql.org>
2026-11-16 18:56 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-16 19:28 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-16 20:23 ` Борис Мурашов <murasovb@gmail.com>
2026-11-16 22:46 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 01:45 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-17 16:34 ` Tom Lane <tgl@sss.pgh.pa.us>
2026-11-17 16:59 ` Ayush Tiwari <ayushtiwari.slg01@gmail.com>
2026-11-18 02:57 ` Tom Lane <tgl@sss.pgh.pa.us>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox