mirror of
https://github.com/postgres/postgres.git
synced 2025-07-07 00:36:50 +03:00
Since commit 913bbd88d
, check_sql_fn_retval() can either insert type
coercion steps in-line in the Query that produces the SQL function's
results, or generate a new top-level Query to perform the coercions,
if modifying the Query's output in-place wouldn't be safe. However,
it appears that the latter case has never actually worked, because
the code tried to inject the new Query back into the query list it was
passed ... which is not the list that will be used for later processing
when we execute the SQL function "normally" (without inlining it).
So we ended up with no coercion happening at run-time, leading to
wrong results or crashes depending on the datatypes involved.
While the regression tests look like they cover this area well enough,
through a huge bit of bad luck all the test cases that exercise the
separate-Query path were checking either inline-able cases (which
accidentally didn't have the bug) or cases that are no-ops at runtime
(e.g., varchar to text), so that the failure to perform the coercion
wasn't obvious. The fact that the cases that don't work weren't
allowed at all before v13 probably contributed to not noticing the
problem sooner, too.
To fix, get rid of the separate "flat" list of Query nodes and instead
pass the real two-level list that is going to be used later. I chose
to make the same change in check_sql_fn_statements(), although that has
no actual bug, just so that we don't need that data structure at all.
This is an API change, as evidenced by the adjustments needed to
callers outside functions.c. That's a bit scary to be doing in a
released branch, but so far as I can tell from a quick search,
there are no outside callers of these functions (and they are
sufficiently specific to our semantics for SQL-language functions that
it's not apparent why any extension would need to call them). In any
case, v13 already changed the API of check_sql_fn_retval() compared to
prior branches.
Per report from pinker. Back-patch to v13 where this code came in.
Discussion: https://postgr.es/m/1603050466566-0.post@n3.nabble.com
42 lines
1.2 KiB
C
42 lines
1.2 KiB
C
/*-------------------------------------------------------------------------
|
|
*
|
|
* functions.h
|
|
* Declarations for execution of SQL-language functions.
|
|
*
|
|
*
|
|
* Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
*
|
|
* src/include/executor/functions.h
|
|
*
|
|
*-------------------------------------------------------------------------
|
|
*/
|
|
#ifndef FUNCTIONS_H
|
|
#define FUNCTIONS_H
|
|
|
|
#include "nodes/execnodes.h"
|
|
#include "tcop/dest.h"
|
|
|
|
/* This struct is known only within executor/functions.c */
|
|
typedef struct SQLFunctionParseInfo *SQLFunctionParseInfoPtr;
|
|
|
|
extern Datum fmgr_sql(PG_FUNCTION_ARGS);
|
|
|
|
extern SQLFunctionParseInfoPtr prepare_sql_fn_parse_info(HeapTuple procedureTuple,
|
|
Node *call_expr,
|
|
Oid inputCollation);
|
|
|
|
extern void sql_fn_parser_setup(struct ParseState *pstate,
|
|
SQLFunctionParseInfoPtr pinfo);
|
|
|
|
extern void check_sql_fn_statements(List *queryTreeLists);
|
|
|
|
extern bool check_sql_fn_retval(List *queryTreeLists,
|
|
Oid rettype, TupleDesc rettupdesc,
|
|
bool insertDroppedCols,
|
|
List **resultTargetList);
|
|
|
|
extern DestReceiver *CreateSQLFunctionDestReceiver(void);
|
|
|
|
#endif /* FUNCTIONS_H */
|