Fix erroneous construction of functions' dependencies on transforms.
authorTom Lane <[email protected]>
Mon, 7 Apr 2025 17:31:27 +0000 (13:31 -0400)
committerTom Lane <[email protected]>
Mon, 7 Apr 2025 17:31:37 +0000 (13:31 -0400)
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 <[email protected]>
Author: Tom Lane <[email protected]>
Discussion: https://postgr.es/m/3112950.1743984111@sss.pgh.pa.us

contrib/bool_plperl/expected/bool_plperl.out
contrib/bool_plperl/expected/bool_plperlu.out
src/backend/catalog/pg_aggregate.c
src/backend/catalog/pg_proc.c
src/backend/commands/functioncmds.c
src/backend/commands/typecmds.c
src/include/catalog/catversion.h
src/include/catalog/pg_proc.h

index 187df8db96f9ee1de3074369ace8f2ba0823f131..183dc07b3fbc3605c1a49afef78cee355bc717c1 100644 (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()
index 8337d337e992efaf668989f03498f3d2f95a40a6..1496bbafac8e02eea45329c25edc4454e441059c 100644 (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()
index bcf4050f5b1619f65ec4bf3312bd336b05f6b130..a05f8a87c1f8353a12a679baebefb4ec6761ccb2 100644 (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 */
index 880b597fb3ac8425292e63c17e33a40f6f08be22..5fdcf24d5f8de29db33bf017965b63569b7c9402 100644 (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 */
index b9fd7683abb4219706f2073c90028fad0b9d3c8d..0335e982b318b6a447b50cbc2a4969377080c85a 100644 (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,
index 3cb3ca1cca1cd0a23d43d5bf7109f2fd24ea4a13..45ae7472ab5ad08393285268bf3c5d2d7178041d 100644 (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 */
index 208936962efcac5fc9746de68541c1ae93b5d98a..014aefc91a7e5f8febeccc285b88385db7cc9521 100644 (file)
@@ -57,6 +57,6 @@
  */
 
 /*                         yyyymmddN */
-#define CATALOG_VERSION_NO 202504041
+#define CATALOG_VERSION_NO 202504071
 
 #endif
index 45f593fc958325efad3ed4411985a9a7a93847e4..d7353e7a088ff2335af779ad50e3b13b84b20f41 100644 (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,