1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-22 12:22:45 +03:00

Improve the handling of result type coercions in SQL functions.

Use the parser's standard type coercion machinery to convert the
output column(s) of a SQL function's final SELECT or RETURNING
to the type(s) they should have according to the function's declared
result type.  We'll allow any case where an assignment-level
coercion is available.  Previously, we failed unless the required
coercion was a binary-compatible one (and the documentation ignored
this, falsely claiming that the types must match exactly).

Notably, the coercion now accounts for typmods, so that cases where
a SQL function is declared to return a composite type whose columns
are typmod-constrained now behave as one would expect.  Arguably
this aspect is a bug fix, but the overall behavioral change here
seems too large to consider back-patching.

A nice side-effect is that functions can now be inlined in a
few cases where we previously failed to do so because of type
mismatches.

Discussion: https://postgr.es/m/18929.1574895430@sss.pgh.pa.us
This commit is contained in:
Tom Lane
2020-01-08 11:07:53 -05:00
parent 8dd1511e39
commit 913bbd88dc
7 changed files with 656 additions and 332 deletions

View File

@@ -923,6 +923,8 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
* verify the result type.
*/
SQLFunctionParseInfoPtr pinfo;
Oid rettype;
TupleDesc rettupdesc;
/* But first, set up parameter information */
pinfo = prepare_sql_fn_parse_info(tuple, NULL, InvalidOid);
@@ -943,9 +945,12 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
}
check_sql_fn_statements(querytree_list);
(void) check_sql_fn_retval(funcoid, proc->prorettype,
querytree_list,
NULL, NULL);
(void) get_func_result_type(funcoid, &rettype, &rettupdesc);
(void) check_sql_fn_retval(querytree_list,
rettype, rettupdesc,
false, NULL);
}
error_context_stack = sqlerrcontext.previous;