postgres.git / summary / log / commit / refs

commit    59f0c2be1eccf49069dd74b11b5fa9f1e05d24ce
Author:   Tom Lane <tgl@sss.pgh.pa.us>
Date:     Wed Nov 18 02:55:24 2026 +0000

    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 | 49 ++++++++++++++++++++------------ 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, 47 insertions(+), 25 deletions(-) diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index 51204b7cfba..757e1124669 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -146,9 +146,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. @@ -1010,7 +1015,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, @@ -1040,13 +1045,24 @@ coerce_record_to_complex(ParseState *pstate, Node *node, else if (node && IsA(node, Var) && ((Var *) node)->varattno == InvalidAttrNumber) { - int rtindex = ((Var *) node)->varno; - int sublevels_up = ((Var *) node)->varlevelsup; - int vlocation = ((Var *) node)->location; - ParseNamespaceItem *nsitem; + if (pstate) + { + int rtindex = ((Var *) node)->varno; + int sublevels_up = ((Var *) node)->varlevelsup; + int vlocation = ((Var *) node)->location; + ParseNamespaceItem *nsitem; - nsitem = GetNSItemByRangeTablePosn(pstate, rtindex, sublevels_up); - args = expandNSItemVars(pstate, nsitem, sublevels_up, vlocation, NULL); + nsitem = GetNSItemByRangeTablePosn(pstate, rtindex, sublevels_up); + args = expandNSItemVars(pstate, nsitem, sublevels_up, + vlocation, 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, @@ -1158,8 +1174,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, @@ -1205,8 +1220,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, @@ -1254,8 +1268,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, @@ -1332,6 +1345,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 @@ -1571,8 +1585,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 63938892780..abf0e38a9ae 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -2804,9 +2804,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 3fcea0dcdbc..1c0ebeaa8de 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -1873,8 +1873,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 397a8b35d6d..ee4a4612a83 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); [parent: f9c88051d646]