]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
PL/Python: Refactor for event trigger support
authorPeter Eisentraut <peter@eisentraut.org>
Thu, 21 Aug 2025 07:15:49 +0000 (09:15 +0200)
committerPeter Eisentraut <peter@eisentraut.org>
Thu, 21 Aug 2025 07:16:29 +0000 (09:16 +0200)
Change is_trigger type from boolean to enum.  That's a preparation for
adding event trigger support.

Author: Euler Taveira <euler@eulerto.com>
Co-authored-by: Dimitri Fontaine <dimitri@2ndQuadrant.fr>
Reviewed-by: Pavel Stehule <pavel.stehule@gmail.com>
Discussion: https://www.postgresql.org/message-id/flat/03f03515-2068-4f5b-b357-8fb540883c38%40app.fastmail.com

src/pl/plpython/plpy_exec.c
src/pl/plpython/plpy_main.c
src/pl/plpython/plpy_procedure.c
src/pl/plpython/plpy_procedure.h
src/tools/pgindent/typedefs.list

index 28fbd443b98c9a52799318920e16c9c9dcc09894..22835174b698778526ee9e48f62a94982de43cd3 100644 (file)
@@ -509,7 +509,7 @@ PLy_function_save_args(PLyProcedure *proc)
        Py_XINCREF(result->args);
 
        /* If it's a trigger, also save "TD" */
-       if (proc->is_trigger)
+       if (proc->is_trigger == PLPY_TRIGGER)
        {
                result->td = PyDict_GetItemString(proc->globals, "TD");
                Py_XINCREF(result->td);
index f36eadbadc66d7835f7d2a47104c9b48844f6c10..900c65e8da066d3581da30f8f455efbf259aaf4f 100644 (file)
@@ -38,7 +38,7 @@ PG_FUNCTION_INFO_V1(plpython3_call_handler);
 PG_FUNCTION_INFO_V1(plpython3_inline_handler);
 
 
-static bool PLy_procedure_is_trigger(Form_pg_proc procStruct);
+static PLyTrigType PLy_procedure_is_trigger(Form_pg_proc procStruct);
 static void plpython_error_callback(void *arg);
 static void plpython_inline_error_callback(void *arg);
 static void PLy_init_interp(void);
@@ -163,7 +163,7 @@ plpython3_validator(PG_FUNCTION_ARGS)
        Oid                     funcoid = PG_GETARG_OID(0);
        HeapTuple       tuple;
        Form_pg_proc procStruct;
-       bool            is_trigger;
+       PLyTrigType is_trigger;
 
        if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
                PG_RETURN_VOID();
@@ -235,14 +235,14 @@ plpython3_call_handler(PG_FUNCTION_ARGS)
                        Relation        tgrel = ((TriggerData *) fcinfo->context)->tg_relation;
                        HeapTuple       trv;
 
-                       proc = PLy_procedure_get(funcoid, RelationGetRelid(tgrel), true);
+                       proc = PLy_procedure_get(funcoid, RelationGetRelid(tgrel), PLPY_TRIGGER);
                        exec_ctx->curr_proc = proc;
                        trv = PLy_exec_trigger(fcinfo, proc);
                        retval = PointerGetDatum(trv);
                }
                else
                {
-                       proc = PLy_procedure_get(funcoid, InvalidOid, false);
+                       proc = PLy_procedure_get(funcoid, InvalidOid, PLPY_NOT_TRIGGER);
                        exec_ctx->curr_proc = proc;
                        retval = PLy_exec_function(fcinfo, proc);
                }
@@ -336,10 +336,22 @@ plpython3_inline_handler(PG_FUNCTION_ARGS)
        PG_RETURN_VOID();
 }
 
-static bool
+static PLyTrigType
 PLy_procedure_is_trigger(Form_pg_proc procStruct)
 {
-       return (procStruct->prorettype == TRIGGEROID);
+       PLyTrigType ret;
+
+       switch (procStruct->prorettype)
+       {
+               case TRIGGEROID:
+                       ret = PLPY_TRIGGER;
+                       break;
+               default:
+                       ret = PLPY_NOT_TRIGGER;
+                       break;
+       }
+
+       return ret;
 }
 
 static void
index c176d24e801183c1bd4b7c688be13bd0df9911eb..22d9ef0fe06773a4cb03e7ad5fe80069ef17e416 100644 (file)
@@ -21,7 +21,7 @@
 
 static HTAB *PLy_procedure_cache = NULL;
 
-static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger);
+static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid fn_oid, PLyTrigType is_trigger);
 static bool PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup);
 static char *PLy_procedure_munge_source(const char *name, const char *src);
 
@@ -63,15 +63,20 @@ PLy_procedure_name(PLyProcedure *proc)
  * be used with, so no sensible fn_rel can be passed.
  */
 PLyProcedure *
-PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger)
+PLy_procedure_get(Oid fn_oid, Oid fn_rel, PLyTrigType is_trigger)
 {
-       bool            use_cache = !(is_trigger && fn_rel == InvalidOid);
+       bool            use_cache;
        HeapTuple       procTup;
        PLyProcedureKey key;
        PLyProcedureEntry *volatile entry = NULL;
        PLyProcedure *volatile proc = NULL;
        bool            found = false;
 
+       if (is_trigger == PLPY_TRIGGER && fn_rel == InvalidOid)
+               use_cache = false;
+       else
+               use_cache = true;
+
        procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
        if (!HeapTupleIsValid(procTup))
                elog(ERROR, "cache lookup failed for function %u", fn_oid);
@@ -127,7 +132,7 @@ PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger)
  * Create a new PLyProcedure structure
  */
 static PLyProcedure *
-PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
+PLy_procedure_create(HeapTuple procTup, Oid fn_oid, PLyTrigType is_trigger)
 {
        char            procName[NAMEDATALEN + 256];
        Form_pg_proc procStruct;
@@ -200,7 +205,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
                 * get information required for output conversion of the return value,
                 * but only if this isn't a trigger.
                 */
-               if (!is_trigger)
+               if (is_trigger == PLPY_NOT_TRIGGER)
                {
                        Oid                     rettype = procStruct->prorettype;
                        HeapTuple       rvTypeTup;
index 5db854fc8bd2dfef857c72013f26e9552cfba330..601b91d5d94e960a101d0798c5e6e6ae812782c6 100644 (file)
 extern void init_procedure_caches(void);
 
 
+/*
+ * Trigger type
+ */
+typedef enum PLyTrigType
+{
+       PLPY_TRIGGER,
+       PLPY_NOT_TRIGGER,
+} PLyTrigType;
+
 /* saved arguments for outer recursion level or set-returning function */
 typedef struct PLySavedArgs
 {
@@ -33,7 +42,7 @@ typedef struct PLyProcedure
        bool            fn_readonly;
        bool            is_setof;               /* true, if function returns result set */
        bool            is_procedure;
-       bool            is_trigger;             /* called as trigger? */
+       PLyTrigType is_trigger;         /* called as trigger? */
        PLyObToDatum result;            /* Function result output conversion info */
        PLyDatumToOb result_in;         /* For converting input tuples in a trigger */
        char       *src;                        /* textual procedure code, after mangling */
@@ -65,7 +74,7 @@ typedef struct PLyProcedureEntry
 
 /* PLyProcedure manipulation */
 extern char *PLy_procedure_name(PLyProcedure *proc);
-extern PLyProcedure *PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger);
+extern PLyProcedure *PLy_procedure_get(Oid fn_oid, Oid fn_rel, PLyTrigType is_trigger);
 extern void PLy_procedure_compile(PLyProcedure *proc, const char *src);
 extern void PLy_procedure_delete(PLyProcedure *proc);
 
index e4a9ec65ab4daa4e174edb367a29190624e637da..a13e8162890233dd95bc299db71a8ef9128d5e15 100644 (file)
@@ -1990,6 +1990,7 @@ PLyScalarToOb
 PLySubtransactionData
 PLySubtransactionObject
 PLyTransformToOb
+PLyTrigType
 PLyTupleToOb
 PLyUnicode_FromStringAndSize_t
 PLy_elog_impl_t