Fix assorted confusion about parameter names. New-style SQL-language functions failed quite badly if named input parameters were declared after any output parameters, which is unusual style but perfectly legal. The parser failed to recognize some of the input parameter names, and could instead accept output parameter names if there were any, and assigned the wrong parameter numbers in any case. ruleutils.c was equally confused and would print the wrong parameter names when deparsing a new-style function.
This mostly comes down to using get_func_arg_info() and failing to account for the fact that its output covers both input and output parameters. In some places the right answer is to use get_func_input_arg_names() instead, although that has an API just different enough to trip you up. (I added some comments about that.) Examining other callers of get_func_arg_info() found some related breakage: PL/Python didn't do quite the right thing with unnamed input parameters, and plsample.c was completely broken for any case involving output parameters at all. A remaining issue, not dealt with here, is that ruleutils' habit of deparsing Param references using qualified parameter names is vulnerable to aliasing problems: if the function name matches some table alias used within the function then we can emit output that means something different than we intended. Bug: #19848 Reported-by: Atish Prakash <[email protected]> Diagnosed-by: Atish Prakash <[email protected]> Author: Tom Lane <[email protected]> Discussion: https://postgr.es/m/[email protected] Backpatch-through: 15 Branch ------ REL_15_STABLE Details ------- https://git.postgresql.org/pg/commitdiff/0f351673b5749c782c35d3627a9ae09dcc62bf98 Modified Files -------------- src/backend/commands/functioncmds.c | 9 +-- src/backend/utils/adt/ruleutils.c | 29 +++++---- src/backend/utils/fmgr/funcapi.c | 6 +- src/pl/plpython/plpy_procedure.c | 3 +- src/test/modules/plsample/plsample.c | 20 +++++-- src/test/regress/expected/create_function_sql.out | 73 ++++++++++++++++++++++- src/test/regress/sql/create_function_sql.sql | 32 ++++++++++ 7 files changed, 151 insertions(+), 21 deletions(-)
