1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-18 13:44:19 +03:00

Fix erroneous construction of functions' dependencies on transforms.

The list of transform objects that a function should use is specified
in CREATE FUNCTION's TRANSFORM clause, and then represented indirectly
in pg_proc.protrftypes.  However, ProcedureCreate completely ignored
that for purposes of constructing pg_depend entries, and instead made
the function depend on any transforms that exist for its parameter or
return data types.  This is bad in both directions: the function could
be made dependent on a transform it does not actually use, or it
could try to use a transform that's since been dropped.  (The latter
scenario would require use of a transform that's not for any of the
parameter or return types, but that seems legit for cases where the
function performs SQL operations internally.)

To fix, pass in the list of transform objects that CreateFunction
identified, and build pg_depend entries from that not from the
parameter/return types.  This results in changes in the expected
test outputs in contrib/bool_plperl, which I guess are due to
different ordering of pg_depend entries -- that test case is
surely not exercising either of the problem scenarios.

This fix is not back-patchable as-is: changing the signature of
ProcedureCreate seems too risky in stable branches.  We could
do something like making ProcedureCreate a wrapper around
ProcedureCreateExt or so.  However, I'm more inclined to do
nothing in the back branches.  We had no field complaints up to
now, so the hazards don't seem to be a big issue in practice.
And we couldn't do anything about existing pg_depend entries,
so a back-patched fix would result in a mishmash of dependencies
created according to different rules.  That cure could be worse
than the disease, perhaps.

I bumped catversion just to lay down a marker that the expected
contents of pg_depend are a bit different than before.

Reported-by: Chapman Flack <jcflack@acm.org>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/3112950.1743984111@sss.pgh.pa.us
This commit is contained in:
Tom Lane 2025-04-07 13:31:27 -04:00
parent a379061a22
commit b73e6d71a8
8 changed files with 52 additions and 22 deletions

View File

@ -104,9 +104,9 @@ SELECT spi_test();
DROP EXTENSION plperl CASCADE;
NOTICE: drop cascades to 6 other objects
DETAIL: drop cascades to function spi_test()
drop cascades to extension bool_plperl
DETAIL: drop cascades to extension bool_plperl
drop cascades to function perl2int(integer)
drop cascades to function perl2text(text)
drop cascades to function perl2undef()
drop cascades to function bool2perl(boolean,boolean,boolean)
drop cascades to function spi_test()

View File

@ -104,9 +104,9 @@ SELECT spi_test();
DROP EXTENSION plperlu CASCADE;
NOTICE: drop cascades to 6 other objects
DETAIL: drop cascades to function spi_test()
drop cascades to extension bool_plperlu
DETAIL: drop cascades to extension bool_plperlu
drop cascades to function perl2int(integer)
drop cascades to function perl2text(text)
drop cascades to function perl2undef()
drop cascades to function bool2perl(boolean,boolean,boolean)
drop cascades to function spi_test()

View File

@ -637,6 +637,7 @@ AggregateCreate(const char *aggName,
parameterNames, /* parameterNames */
parameterDefaults, /* parameterDefaults */
PointerGetDatum(NULL), /* trftypes */
NIL, /* trfoids */
PointerGetDatum(NULL), /* proconfig */
InvalidOid, /* no prosupport */
1, /* procost */

View File

@ -26,7 +26,6 @@
#include "catalog/pg_proc.h"
#include "catalog/pg_transform.h"
#include "catalog/pg_type.h"
#include "commands/defrem.h"
#include "executor/functions.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
@ -61,6 +60,35 @@ static bool match_prosrc_to_literal(const char *prosrc, const char *literal,
/* ----------------------------------------------------------------
* ProcedureCreate
*
* procedureName: string name of routine (proname)
* procNamespace: OID of namespace (pronamespace)
* replace: true to allow replacement of an existing pg_proc entry
* returnsSet: returns set? (proretset)
* returnType: OID of result type (prorettype)
* proowner: OID of owner role (proowner)
* languageObjectId: OID of function language (prolang)
* languageValidator: OID of validator function to apply, if any
* prosrc: string form of function definition (prosrc)
* probin: string form of binary reference, or NULL (probin)
* prosqlbody: Node tree of pre-parsed SQL body, or NULL (prosqlbody)
* prokind: function/aggregate/procedure/etc code (prokind)
* security_definer: security definer? (prosecdef)
* isLeakProof: leak proof? (proleakproof)
* isStrict: strict? (proisstrict)
* volatility: volatility code (provolatile)
* parallel: parallel safety code (proparallel)
* parameterTypes: input parameter types, as an oidvector (proargtypes)
* allParameterTypes: all parameter types, as an OID array (proallargtypes)
* parameterModes: parameter modes, as a "char" array (proargmodes)
* parameterNames: parameter names, as a text array (proargnames)
* parameterDefaults: defaults, as a List of Node trees (proargdefaults)
* trftypes: transformable type OIDs, as an OID array (protrftypes)
* trfoids: List of transform OIDs that routine should depend on
* proconfig: GUC set clauses, as a text array (proconfig)
* prosupport: OID of support function, if any (prosupport)
* procost: cost factor (procost)
* prorows: estimated output rows for a SRF (prorows)
*
* Note: allParameterTypes, parameterModes, parameterNames, trftypes, and proconfig
* are either arrays of the proper types or NULL. We declare them Datum,
* not "ArrayType *", to avoid importing array.h into pg_proc.h.
@ -90,6 +118,7 @@ ProcedureCreate(const char *procedureName,
Datum parameterNames,
List *parameterDefaults,
Datum trftypes,
List *trfoids,
Datum proconfig,
Oid prosupport,
float4 procost,
@ -115,7 +144,6 @@ ProcedureCreate(const char *procedureName,
referenced;
char *detailmsg;
int i;
Oid trfid;
ObjectAddresses *addrs;
/*
@ -609,25 +637,18 @@ ProcedureCreate(const char *procedureName,
ObjectAddressSet(referenced, TypeRelationId, returnType);
add_exact_object_address(&referenced, addrs);
/* dependency on transform used by return type, if any */
if ((trfid = get_transform_oid(returnType, languageObjectId, true)))
{
ObjectAddressSet(referenced, TransformRelationId, trfid);
add_exact_object_address(&referenced, addrs);
}
/* dependency on parameter types */
for (i = 0; i < allParamCount; i++)
{
ObjectAddressSet(referenced, TypeRelationId, allParams[i]);
add_exact_object_address(&referenced, addrs);
}
/* dependency on transform used by parameter type, if any */
if ((trfid = get_transform_oid(allParams[i], languageObjectId, true)))
{
ObjectAddressSet(referenced, TransformRelationId, trfid);
add_exact_object_address(&referenced, addrs);
}
/* dependency on transforms, if any */
foreach_oid(transformid, trfoids)
{
ObjectAddressSet(referenced, TransformRelationId, transformid);
add_exact_object_address(&referenced, addrs);
}
/* dependency on support function, if any */

View File

@ -1046,6 +1046,7 @@ CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt)
List *parameterDefaults;
Oid variadicArgType;
List *trftypes_list = NIL;
List *trfoids_list = NIL;
ArrayType *trftypes;
Oid requiredResultType;
bool isWindowFunc,
@ -1157,11 +1158,12 @@ CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt)
Oid typeid = typenameTypeId(NULL,
lfirst_node(TypeName, lc));
Oid elt = get_base_element_type(typeid);
Oid transformid;
typeid = elt ? elt : typeid;
get_transform_oid(typeid, languageOid, false);
transformid = get_transform_oid(typeid, languageOid, false);
trftypes_list = lappend_oid(trftypes_list, typeid);
trfoids_list = lappend_oid(trfoids_list, transformid);
}
}
@ -1292,6 +1294,7 @@ CreateFunction(ParseState *pstate, CreateFunctionStmt *stmt)
PointerGetDatum(parameterNames),
parameterDefaults,
PointerGetDatum(trftypes),
trfoids_list,
PointerGetDatum(proconfig),
prosupport,
procost,

View File

@ -1810,6 +1810,7 @@ makeRangeConstructors(const char *name, Oid namespace,
PointerGetDatum(NULL), /* parameterNames */
NIL, /* parameterDefaults */
PointerGetDatum(NULL), /* trftypes */
NIL, /* trfoids */
PointerGetDatum(NULL), /* proconfig */
InvalidOid, /* prosupport */
1.0, /* procost */
@ -1875,6 +1876,7 @@ makeMultirangeConstructors(const char *name, Oid namespace,
PointerGetDatum(NULL), /* parameterNames */
NIL, /* parameterDefaults */
PointerGetDatum(NULL), /* trftypes */
NIL, /* trfoids */
PointerGetDatum(NULL), /* proconfig */
InvalidOid, /* prosupport */
1.0, /* procost */
@ -1919,6 +1921,7 @@ makeMultirangeConstructors(const char *name, Oid namespace,
PointerGetDatum(NULL), /* parameterNames */
NIL, /* parameterDefaults */
PointerGetDatum(NULL), /* trftypes */
NIL, /* trfoids */
PointerGetDatum(NULL), /* proconfig */
InvalidOid, /* prosupport */
1.0, /* procost */
@ -1957,6 +1960,7 @@ makeMultirangeConstructors(const char *name, Oid namespace,
PointerGetDatum(NULL), /* parameterNames */
NIL, /* parameterDefaults */
PointerGetDatum(NULL), /* trftypes */
NIL, /* trfoids */
PointerGetDatum(NULL), /* proconfig */
InvalidOid, /* prosupport */
1.0, /* procost */

View File

@ -57,6 +57,6 @@
*/
/* yyyymmddN */
#define CATALOG_VERSION_NO 202504041
#define CATALOG_VERSION_NO 202504071
#endif

View File

@ -211,6 +211,7 @@ extern ObjectAddress ProcedureCreate(const char *procedureName,
Datum parameterNames,
List *parameterDefaults,
Datum trftypes,
List *trfoids,
Datum proconfig,
Oid prosupport,
float4 procost,