Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMw7A-00000002HLv-2PjB for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Oct 2026 23:34:29 +0000 Received: from localhost ([127.0.0.1]:34406 helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1xMw69-0000000CVn8-3fU5 for pgsql-hackers@arkaria.postgresql.org; Fri, 30 Oct 2026 23:33:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]:50244) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMw69-0000000CVmy-1o0v for pgsql-hackers@lists.postgresql.org; Fri, 30 Oct 2026 23:33:25 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1xMw66-00000001lra-2c39 for pgsql-hackers@lists.postgresql.org; Fri, 30 Oct 2026 23:33:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.18.1/8.18.1) with ESMTP id 69UNXLNF2865289 for ; Fri, 30 Oct 2026 19:33:21 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 sss.pgh.pa.us 69UNXLNF2865289 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sss.pgh.pa.us; s=default; t=1793403201; bh=SmLggXbs3fc3pcCyrTW6RZWoH8Q/sPfZpIesH6XFPJo=; h=From:To:Subject:Date:From; b=aPSDmzxBaEl0PsnMcw7TGUkI1FHrqX9f409f0OhVZ2VqveqtEcyLdznA9vE+8ArbT wQcCG1zNUECKX/1kAWVsW0cEChPZze0hllqetJUbdDv6Q1M0otBPw3g3C1Y5jEtkli iWGC0noKWw+C9H0m5LXpM85ALLMYq7WC862IVooE= From: Tom Lane To: pgsql-hackers@lists.postgresql.org Subject: [PG19] Bogus errors for polymorphic plpython functions MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <2865256.1793403139.0@sss.pgh.pa.us> Date: Fri, 30 Oct 2026 19:33:21 -0400 Message-ID: <2865288.1793403201@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2865256.1793403139.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable PL/Python doesn't make any effort to support polymorphic arguments or results. (It should, but that's a task for some other day.) However, I happened to notice that while previous releases throw a useful error: regression=3D# CREATE FUNCTION python_poly(x anyelement) RETURNS anyelemen= t AS $$ return x $$ LANGUAGE plpython3u; ERROR: PL/Python functions cannot return type anyelement in v19/master you get something pretty off-point: regression=3D# CREATE FUNCTION python_poly(x anyelement) RETURNS anyelemen= t AS $$ return x $$ LANGUAGE plpython3u; ERROR: could not determine actual result type for function "python_poly" = declared to return type anyelement The reason for this is that commit 9bcb8a694 restructured things to let plpython use the funccache.c infrastructure. That means that the argument-type checks done in PLy_procedure_create are now inside a callback function that funccache.c calls after it's discovered it lacks a cache entry for the function. And in order to lookup a cache entry, it has to resolve polymorphic types to real types, and the fake fcinfo that plpython3_validator makes doesn't contain enough data to support that. I don't want to resolve that conundrum right now --- for one thing, I've not made up my mind whether it's more plpython's fault or more funccache.c's fault. What I think we should do about this is to move the no-pseudotype-argument checks from where they are into the validator's startup section, where they will get enforced whether check_function_bodies is on or not. (It's really a bug that right now you don't hear about this if !check_function_bodies.) In that way, they'll happen before funccache.c is asked to do anything, so we don't encounter the underlying problem. Even when we do resolve the problem on the way to fixing plpython to support polymorphics, this is a better code structure: it reports errors sooner and takes some catalog lookups out of the normal function call path. Comments? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-plpython-polymorphic-check.patch"; charset="us-ascii" Content-ID: <2865256.1793403139.2@sss.pgh.pa.us> Content-Description: fix-plpython-polymorphic-check.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/pl/plpython/expected/plpython_error.out b/src/pl/plpython= /expected/plpython_error.out index 96bbdfb9586..42003ffd69b 100644 --- a/src/pl/plpython/expected/plpython_error.out +++ b/src/pl/plpython/expected/plpython_error.out @@ -458,3 +458,13 @@ ERROR: error fetching next item from iterator DETAIL: spiexceptions.InvalidDatetimeFormat: invalid value "xy" for "DD" CONTEXT: Traceback (most recent call last): PL/Python function "python_error_detail" +/* test the presently-unsupported case of polymorphic functions + */ +CREATE FUNCTION python_poly_ret(x anyelement) RETURNS anyelement AS $$ + return x +$$ LANGUAGE plpython3u; +ERROR: PL/Python functions cannot return type anyelement +CREATE FUNCTION python_poly_arg(x anyelement) RETURNS int AS $$ + return x +$$ LANGUAGE plpython3u; +ERROR: PL/Python functions cannot accept type anyelement diff --git a/src/pl/plpython/plpy_main.c b/src/pl/plpython/plpy_main.c index df08d3fbd1b..2a9c2e6d85c 100644 --- a/src/pl/plpython/plpy_main.c +++ b/src/pl/plpython/plpy_main.c @@ -10,6 +10,7 @@ #include "commands/event_trigger.h" #include "commands/trigger.h" #include "executor/spi.h" +#include "funcapi.h" #include "miscadmin.h" #include "plpy_elog.h" #include "plpy_exec.h" @@ -17,7 +18,9 @@ #include "plpy_plpymodule.h" #include "plpy_subxactobject.h" #include "plpy_util.h" +#include "utils/builtins.h" #include "utils/guc.h" +#include "utils/lsyscache.h" #include "utils/memutils.h" #include "utils/syscache.h" = @@ -111,6 +114,11 @@ plpython3_validator(PG_FUNCTION_ARGS) Oid funcoid =3D PG_GETARG_OID(0); HeapTuple tuple; Form_pg_proc procStruct; + Oid rettype; + Oid *argtypes; + char **argnames, + *argmodes; + int nargs; PLyTrigType is_trigger; TriggerData trigdata; EventTriggerData etrigdata; @@ -120,19 +128,56 @@ plpython3_validator(PG_FUNCTION_ARGS) if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid)) PG_RETURN_VOID(); = - if (!check_function_bodies) - PG_RETURN_VOID(); - /* Get the new function's pg_proc entry */ tuple =3D SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid)); if (!HeapTupleIsValid(tuple)) elog(ERROR, "cache lookup failed for function %u", funcoid); procStruct =3D (Form_pg_proc) GETSTRUCT(tuple); = + /* Disallow pseudotype result, except for void or record */ + rettype =3D procStruct->prorettype; + if (get_typtype(rettype) =3D=3D TYPTYPE_PSEUDO) + { + if (rettype =3D=3D VOIDOID || rettype =3D=3D RECORDOID) + /* okay */ ; + else if (rettype =3D=3D TRIGGEROID || rettype =3D=3D EVENT_TRIGGEROID) + /* okay */ ; + else + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("PL/Python functions cannot return type %s", + format_type_be(rettype)))); + } + + /* Disallow pseudotype arguments */ + nargs =3D get_func_arg_info(tuple, &argtypes, &argnames, &argmodes); + for (int i =3D 0; i < nargs; i++) + { + if (get_typtype(argtypes[i]) =3D=3D TYPTYPE_PSEUDO) + { + if (argmodes && + (argmodes[i] =3D=3D PROARGMODE_OUT || + argmodes[i] =3D=3D PROARGMODE_TABLE)) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("PL/Python functions cannot return type %s", + format_type_be(argtypes[i])))); + else + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("PL/Python functions cannot accept type %s", + format_type_be(argtypes[i])))); + } + } + is_trigger =3D PLy_procedure_is_trigger(procStruct); = ReleaseSysCache(tuple); = + /* The rest is not needed/wanted if check_function_bodies is off */ + if (!check_function_bodies) + PG_RETURN_VOID(); + /* * Set up a fake flinfo/fcinfo with just enough info to satisfy * PLy_procedure_get(). That function derives the call context (plain diff --git a/src/pl/plpython/plpy_procedure.c b/src/pl/plpython/plpy_proce= dure.c index acfc0d8f0f0..ce8b1e099e9 100644 --- a/src/pl/plpython/plpy_procedure.c +++ b/src/pl/plpython/plpy_procedure.c @@ -217,36 +217,16 @@ PLy_procedure_create(PLyProcedure *proc, if (is_trigger =3D=3D PLPY_NOT_TRIGGER) { Oid rettype =3D procStruct->prorettype; - HeapTuple rvTypeTup; - Form_pg_type rvTypeStruct; = - rvTypeTup =3D SearchSysCache1(TYPEOID, ObjectIdGetDatum(rettype)); - if (!HeapTupleIsValid(rvTypeTup)) - elog(ERROR, "cache lookup failed for type %u", rettype); - rvTypeStruct =3D (Form_pg_type) GETSTRUCT(rvTypeTup); - - /* Disallow pseudotype result, except for void or record */ - if (rvTypeStruct->typtype =3D=3D TYPTYPE_PSEUDO) - { - if (rettype =3D=3D VOIDOID || - rettype =3D=3D RECORDOID) - /* okay */ ; - else if (rettype =3D=3D TRIGGEROID || rettype =3D=3D EVENT_TRIGGEROID= ) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("trigger functions can only be called as triggers"))); - else - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("PL/Python functions cannot return type %s", - format_type_be(rettype)))); - } + /* reject if function is in fact a trigger function */ + if (rettype =3D=3D TRIGGEROID || rettype =3D=3D EVENT_TRIGGEROID) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("trigger functions can only be called as triggers"))); = /* set up output function for procedure result */ PLy_output_setup_func(&proc->result, proc->mcxt, rettype, -1, proc); - - ReleaseSysCache(rvTypeTup); } else { @@ -296,9 +276,6 @@ PLy_procedure_create(PLyProcedure *proc, = for (i =3D pos =3D 0; i < total; i++) { - HeapTuple argTypeTup; - Form_pg_type argTypeStruct; - if (modes && (modes[i] =3D=3D PROARGMODE_OUT || modes[i] =3D=3D PROARGMODE_TABLE)) @@ -306,19 +283,6 @@ PLy_procedure_create(PLyProcedure *proc, = Assert(types[i] =3D=3D procStruct->proargtypes.values[pos]); = - argTypeTup =3D SearchSysCache1(TYPEOID, - ObjectIdGetDatum(types[i])); - if (!HeapTupleIsValid(argTypeTup)) - elog(ERROR, "cache lookup failed for type %u", types[i]); - argTypeStruct =3D (Form_pg_type) GETSTRUCT(argTypeTup); - - /* disallow pseudotype arguments */ - if (argTypeStruct->typtype =3D=3D TYPTYPE_PSEUDO) - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("PL/Python functions cannot accept type %s", - format_type_be(types[i])))); - /* set up I/O function info */ PLy_input_setup_func(&proc->args[pos], proc->mcxt, types[i], -1, /* typmod not known */ @@ -327,8 +291,6 @@ PLy_procedure_create(PLyProcedure *proc, /* get argument name */ proc->argnames[pos] =3D names ? pstrdup(names[i]) : NULL; = - ReleaseSysCache(argTypeTup); - pos++; } } diff --git a/src/pl/plpython/sql/plpython_error.sql b/src/pl/plpython/sql/= plpython_error.sql index 9bb1c0b085a..35dd16440c4 100644 --- a/src/pl/plpython/sql/plpython_error.sql +++ b/src/pl/plpython/sql/plpython_error.sql @@ -355,3 +355,13 @@ CREATE FUNCTION python_error_detail() RETURNS SETOF t= ext AS $$ $$ LANGUAGE plpython3u; = SELECT python_error_detail(); + +/* test the presently-unsupported case of polymorphic functions + */ +CREATE FUNCTION python_poly_ret(x anyelement) RETURNS anyelement AS $$ + return x +$$ LANGUAGE plpython3u; + +CREATE FUNCTION python_poly_arg(x anyelement) RETURNS int AS $$ + return x +$$ LANGUAGE plpython3u; ------- =_aaaaaaaaaa0--