Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6f66fad

Browse files
committed
Fix confusion about the return rowtype of SQL-language procedures.
There is a very ancient hack in check_sql_fn_retval that allows asingle SELECT targetlist entry of composite type to be taken assupplying all the output columns of a function returning composite.(This is grotty and fundamentally ambiguous, but it's really hardto do nested composite-returning functions without it.)As far as I know, that doesn't cause any problems in ordinaryfunctions. It's disastrous for procedures however. All proceduresthat have any output parameters are labeled with prorettype RECORD,and the CALL code expects it will get back a record with one columnper output parameter, regardless of whether any of those parametersis composite. Doing something else leads to an assertion failureor core dump.This is simple enough to fix: we just need to not apply that rulewhen considering procedures. However, that requires adding anotherargument to check_sql_fn_retval, which at least in principle might begetting called by external callers. Therefore, in the back branchesconvert check_sql_fn_retval into an ABI-preserving wrapper around anew function check_sql_fn_retval_ext.Per report from Yahor Yuzefovich. This has been broken since weimplemented procedures, so back-patch to all supported branches.Discussion:https://postgr.es/m/CABz5gWHSjj2df6uG0NRiDhZ_Uz=Y8t0FJP-_SVSsRsnrQT76Gg@mail.gmail.com
1 parent4fce5f9 commit6f66fad

File tree

6 files changed

+69
-18
lines changed

6 files changed

+69
-18
lines changed

‎src/backend/catalog/pg_proc.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -972,9 +972,10 @@ fmgr_sql_validator(PG_FUNCTION_ARGS)
972972

973973
(void)get_func_result_type(funcoid,&rettype,&rettupdesc);
974974

975-
(void)check_sql_fn_retval(querytree_list,
976-
rettype,rettupdesc,
977-
false,NULL);
975+
(void)check_sql_fn_retval_ext(querytree_list,
976+
rettype,rettupdesc,
977+
proc->prokind,
978+
false,NULL);
978979
}
979980

980981
error_context_stack=sqlerrcontext.previous;

‎src/backend/executor/functions.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -750,11 +750,12 @@ init_sql_fcache(FunctionCallInfo fcinfo, Oid collation, bool lazyEvalOK)
750750
* the rowtype column into multiple columns, since we have no way to
751751
* notify the caller that it should do that.)
752752
*/
753-
fcache->returnsTuple=check_sql_fn_retval(queryTree_list,
754-
rettype,
755-
rettupdesc,
756-
false,
757-
&resulttlist);
753+
fcache->returnsTuple=check_sql_fn_retval_ext(queryTree_list,
754+
rettype,
755+
rettupdesc,
756+
procedureStruct->prokind,
757+
false,
758+
&resulttlist);
758759

759760
/*
760761
* Construct a JunkFilter we can use to coerce the returned rowtype to the
@@ -1615,6 +1616,21 @@ check_sql_fn_retval(List *queryTreeLists,
16151616
Oidrettype,TupleDescrettupdesc,
16161617
boolinsertDroppedCols,
16171618
List**resultTargetList)
1619+
{
1620+
/* Wrapper function to preserve ABI compatibility in released branches */
1621+
returncheck_sql_fn_retval_ext(queryTreeLists,
1622+
rettype,rettupdesc,
1623+
PROKIND_FUNCTION,
1624+
insertDroppedCols,
1625+
resultTargetList);
1626+
}
1627+
1628+
bool
1629+
check_sql_fn_retval_ext(List*queryTreeLists,
1630+
Oidrettype,TupleDescrettupdesc,
1631+
charprokind,
1632+
boolinsertDroppedCols,
1633+
List**resultTargetList)
16181634
{
16191635
boolis_tuple_result= false;
16201636
Query*parse;
@@ -1632,7 +1648,7 @@ check_sql_fn_retval(List *queryTreeLists,
16321648

16331649
/*
16341650
* If it's declared to return VOID, we don't care what's in the function.
1635-
* (This takes care ofthe procedure case, as well.)
1651+
* (This takes care ofprocedures with no output parameters, as well.)
16361652
*/
16371653
if (rettype==VOIDOID)
16381654
return false;
@@ -1787,8 +1803,13 @@ check_sql_fn_retval(List *queryTreeLists,
17871803
* or not the record type really matches. For the moment we rely on
17881804
* runtime type checking to catch any discrepancy, but it'd be nice to
17891805
* do better at parse time.
1806+
*
1807+
* We must *not* do this for a procedure, however. Procedures with
1808+
* output parameter(s) have rettype RECORD, and the CALL code expects
1809+
* to get results corresponding to the list of output parameters, even
1810+
* when there's just one parameter that's composite.
17901811
*/
1791-
if (tlistlen==1)
1812+
if (tlistlen==1&&prokind!=PROKIND_PROCEDURE)
17921813
{
17931814
TargetEntry*tle= (TargetEntry*)linitial(tlist);
17941815

‎src/backend/optimizer/util/clauses.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4622,9 +4622,10 @@ inline_function(Oid funcid, Oid result_type, Oid result_collid,
46224622
* needed; that's probably not important, but let's be careful.
46234623
*/
46244624
querytree_list=list_make1(querytree);
4625-
if (check_sql_fn_retval(list_make1(querytree_list),
4626-
result_type,rettupdesc,
4627-
false,NULL))
4625+
if (check_sql_fn_retval_ext(list_make1(querytree_list),
4626+
result_type,rettupdesc,
4627+
funcform->prokind,
4628+
false,NULL))
46284629
gotofail;/* reject whole-tuple-result cases */
46294630

46304631
/*
@@ -5173,9 +5174,10 @@ inline_set_returning_function(PlannerInfo *root, RangeTblEntry *rte)
51735174
* shows it's returning a whole tuple result; otherwise what it's
51745175
* returning is a single composite column which is not what we need.
51755176
*/
5176-
if (!check_sql_fn_retval(list_make1(querytree_list),
5177-
fexpr->funcresulttype,rettupdesc,
5178-
true,NULL)&&
5177+
if (!check_sql_fn_retval_ext(list_make1(querytree_list),
5178+
fexpr->funcresulttype,rettupdesc,
5179+
funcform->prokind,
5180+
true,NULL)&&
51795181
(functypclass==TYPEFUNC_COMPOSITE||
51805182
functypclass==TYPEFUNC_COMPOSITE_DOMAIN||
51815183
functypclass==TYPEFUNC_RECORD))

‎src/include/executor/functions.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ extern bool check_sql_fn_retval(List *queryTreeLists,
5050
boolinsertDroppedCols,
5151
List**resultTargetList);
5252

53+
externboolcheck_sql_fn_retval_ext(List*queryTreeLists,
54+
Oidrettype,TupleDescrettupdesc,
55+
charprokind,
56+
boolinsertDroppedCols,
57+
List**resultTargetList);
58+
5359
externDestReceiver*CreateSQLFunctionDestReceiver(void);
5460

5561
#endif/* FUNCTIONS_H */

‎src/test/regress/expected/create_procedure.out

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,19 @@ CALL ptest4a(a, b); -- error, not supported
148148
$$;
149149
ERROR: calling procedures with output arguments is not supported in SQL functions
150150
CONTEXT: SQL function "ptest4b"
151-
DROP PROCEDURE ptest4a;
151+
-- we used to get confused by a single output argument that is composite
152+
CREATE PROCEDURE ptest4c(INOUT comp int8_tbl)
153+
LANGUAGE SQL
154+
AS $$
155+
SELECT ROW(1, 2);
156+
$$;
157+
CALL ptest4c(NULL);
158+
comp
159+
-------
160+
(1,2)
161+
(1 row)
162+
163+
DROP PROCEDURE ptest4a, ptest4c;
152164
-- named and default parameters
153165
CREATE OR REPLACE PROCEDURE ptest5(a int, b text, c int default 100)
154166
LANGUAGE SQL

‎src/test/regress/sql/create_procedure.sql

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,16 @@ AS $$
9090
CALL ptest4a(a, b);-- error, not supported
9191
$$;
9292

93-
DROP PROCEDURE ptest4a;
93+
-- we used to get confused by a single output argument that is composite
94+
CREATE PROCEDURE ptest4c(INOUT comp int8_tbl)
95+
LANGUAGE SQL
96+
AS $$
97+
SELECT ROW(1,2);
98+
$$;
99+
100+
CALL ptest4c(NULL);
101+
102+
DROP PROCEDURE ptest4a, ptest4c;
94103

95104

96105
-- named and default parameters

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp