Skip to content

Commit 6e09c96

Browse files
petereDimitri Fontaine
andcommitted
PL/Python: Refactor for event trigger support
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
1 parent e8eb987 commit 6e09c96

File tree

5 files changed

+41
-14
lines changed

5 files changed

+41
-14
lines changed

src/pl/plpython/plpy_exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ PLy_function_save_args(PLyProcedure *proc)
509509
Py_XINCREF(result->args);
510510

511511
/* If it's a trigger, also save "TD" */
512-
if (proc->is_trigger)
512+
if (proc->is_trigger == PLPY_TRIGGER)
513513
{
514514
result->td = PyDict_GetItemString(proc->globals, "TD");
515515
Py_XINCREF(result->td);

src/pl/plpython/plpy_main.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ PG_FUNCTION_INFO_V1(plpython3_call_handler);
3838
PG_FUNCTION_INFO_V1(plpython3_inline_handler);
3939

4040

41-
static bool PLy_procedure_is_trigger(Form_pg_proc procStruct);
41+
static PLyTrigType PLy_procedure_is_trigger(Form_pg_proc procStruct);
4242
static void plpython_error_callback(void *arg);
4343
static void plpython_inline_error_callback(void *arg);
4444
static void PLy_init_interp(void);
@@ -163,7 +163,7 @@ plpython3_validator(PG_FUNCTION_ARGS)
163163
Oid funcoid = PG_GETARG_OID(0);
164164
HeapTuple tuple;
165165
Form_pg_proc procStruct;
166-
bool is_trigger;
166+
PLyTrigType is_trigger;
167167

168168
if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
169169
PG_RETURN_VOID();
@@ -235,14 +235,14 @@ plpython3_call_handler(PG_FUNCTION_ARGS)
235235
Relation tgrel = ((TriggerData *) fcinfo->context)->tg_relation;
236236
HeapTuple trv;
237237

238-
proc = PLy_procedure_get(funcoid, RelationGetRelid(tgrel), true);
238+
proc = PLy_procedure_get(funcoid, RelationGetRelid(tgrel), PLPY_TRIGGER);
239239
exec_ctx->curr_proc = proc;
240240
trv = PLy_exec_trigger(fcinfo, proc);
241241
retval = PointerGetDatum(trv);
242242
}
243243
else
244244
{
245-
proc = PLy_procedure_get(funcoid, InvalidOid, false);
245+
proc = PLy_procedure_get(funcoid, InvalidOid, PLPY_NOT_TRIGGER);
246246
exec_ctx->curr_proc = proc;
247247
retval = PLy_exec_function(fcinfo, proc);
248248
}
@@ -336,10 +336,22 @@ plpython3_inline_handler(PG_FUNCTION_ARGS)
336336
PG_RETURN_VOID();
337337
}
338338

339-
static bool
339+
static PLyTrigType
340340
PLy_procedure_is_trigger(Form_pg_proc procStruct)
341341
{
342-
return (procStruct->prorettype == TRIGGEROID);
342+
PLyTrigType ret;
343+
344+
switch (procStruct->prorettype)
345+
{
346+
case TRIGGEROID:
347+
ret = PLPY_TRIGGER;
348+
break;
349+
default:
350+
ret = PLPY_NOT_TRIGGER;
351+
break;
352+
}
353+
354+
return ret;
343355
}
344356

345357
static void

src/pl/plpython/plpy_procedure.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
static HTAB *PLy_procedure_cache = NULL;
2323

24-
static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger);
24+
static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid fn_oid, PLyTrigType is_trigger);
2525
static bool PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup);
2626
static char *PLy_procedure_munge_source(const char *name, const char *src);
2727

@@ -63,15 +63,20 @@ PLy_procedure_name(PLyProcedure *proc)
6363
* be used with, so no sensible fn_rel can be passed.
6464
*/
6565
PLyProcedure *
66-
PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger)
66+
PLy_procedure_get(Oid fn_oid, Oid fn_rel, PLyTrigType is_trigger)
6767
{
68-
bool use_cache = !(is_trigger && fn_rel == InvalidOid);
68+
bool use_cache;
6969
HeapTuple procTup;
7070
PLyProcedureKey key;
7171
PLyProcedureEntry *volatile entry = NULL;
7272
PLyProcedure *volatile proc = NULL;
7373
bool found = false;
7474

75+
if (is_trigger == PLPY_TRIGGER && fn_rel == InvalidOid)
76+
use_cache = false;
77+
else
78+
use_cache = true;
79+
7580
procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
7681
if (!HeapTupleIsValid(procTup))
7782
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)
127132
* Create a new PLyProcedure structure
128133
*/
129134
static PLyProcedure *
130-
PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
135+
PLy_procedure_create(HeapTuple procTup, Oid fn_oid, PLyTrigType is_trigger)
131136
{
132137
char procName[NAMEDATALEN + 256];
133138
Form_pg_proc procStruct;
@@ -200,7 +205,7 @@ PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
200205
* get information required for output conversion of the return value,
201206
* but only if this isn't a trigger.
202207
*/
203-
if (!is_trigger)
208+
if (is_trigger == PLPY_NOT_TRIGGER)
204209
{
205210
Oid rettype = procStruct->prorettype;
206211
HeapTuple rvTypeTup;

src/pl/plpython/plpy_procedure.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111
extern void init_procedure_caches(void);
1212

1313

14+
/*
15+
* Trigger type
16+
*/
17+
typedef enum PLyTrigType
18+
{
19+
PLPY_TRIGGER,
20+
PLPY_NOT_TRIGGER,
21+
} PLyTrigType;
22+
1423
/* saved arguments for outer recursion level or set-returning function */
1524
typedef struct PLySavedArgs
1625
{
@@ -33,7 +42,7 @@ typedef struct PLyProcedure
3342
bool fn_readonly;
3443
bool is_setof; /* true, if function returns result set */
3544
bool is_procedure;
36-
bool is_trigger; /* called as trigger? */
45+
PLyTrigType is_trigger; /* called as trigger? */
3746
PLyObToDatum result; /* Function result output conversion info */
3847
PLyDatumToOb result_in; /* For converting input tuples in a trigger */
3948
char *src; /* textual procedure code, after mangling */
@@ -65,7 +74,7 @@ typedef struct PLyProcedureEntry
6574

6675
/* PLyProcedure manipulation */
6776
extern char *PLy_procedure_name(PLyProcedure *proc);
68-
extern PLyProcedure *PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger);
77+
extern PLyProcedure *PLy_procedure_get(Oid fn_oid, Oid fn_rel, PLyTrigType is_trigger);
6978
extern void PLy_procedure_compile(PLyProcedure *proc, const char *src);
7079
extern void PLy_procedure_delete(PLyProcedure *proc);
7180

src/tools/pgindent/typedefs.list

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,6 +1990,7 @@ PLyScalarToOb
19901990
PLySubtransactionData
19911991
PLySubtransactionObject
19921992
PLyTransformToOb
1993+
PLyTrigType
19931994
PLyTupleToOb
19941995
PLyUnicode_FromStringAndSize_t
19951996
PLy_elog_impl_t

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy