PostgreSQL Source Code  git master
plpy_exec.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "plpy_elog.h"
#include "plpy_exec.h"
#include "plpy_main.h"
#include "plpy_procedure.h"
#include "plpy_subxactobject.h"
#include "plpython.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/rel.h"
#include "utils/typcache.h"
Include dependency graph for plpy_exec.c:

Go to the source code of this file.

Data Structures

struct  PLySRFState
 

Typedefs

typedef struct PLySRFState PLySRFState
 

Functions

static PyObject * PLy_function_build_args (FunctionCallInfo fcinfo, PLyProcedure *proc)
 
static PLySavedArgsPLy_function_save_args (PLyProcedure *proc)
 
static void PLy_function_restore_args (PLyProcedure *proc, PLySavedArgs *savedargs)
 
static void PLy_function_drop_args (PLySavedArgs *savedargs)
 
static void PLy_global_args_push (PLyProcedure *proc)
 
static void PLy_global_args_pop (PLyProcedure *proc)
 
static void plpython_srf_cleanup_callback (void *arg)
 
static void plpython_return_error_callback (void *arg)
 
static PyObject * PLy_trigger_build_args (FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
 
static HeapTuple PLy_modify_tuple (PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, HeapTuple otup)
 
static void plpython_trigger_error_callback (void *arg)
 
static PyObject * PLy_procedure_call (PLyProcedure *proc, const char *kargs, PyObject *vargs)
 
static void PLy_abort_open_subtransactions (int save_subxact_level)
 
Datum PLy_exec_function (FunctionCallInfo fcinfo, PLyProcedure *proc)
 
HeapTuple PLy_exec_trigger (FunctionCallInfo fcinfo, PLyProcedure *proc)
 

Typedef Documentation

◆ PLySRFState

typedef struct PLySRFState PLySRFState

Function Documentation

◆ plpython_return_error_callback()

static void plpython_return_error_callback ( void *  arg)
static

Definition at line 695 of file plpy_exec.c.

696 {
698 
699  if (exec_ctx->curr_proc &&
700  !exec_ctx->curr_proc->is_procedure)
701  errcontext("while creating return value");
702 }
#define errcontext
Definition: elog.h:196
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:367
PLyProcedure * curr_proc
Definition: plpy_main.h:20

References PLyExecutionContext::curr_proc, errcontext, PLyProcedure::is_procedure, and PLy_current_execution_context().

Referenced by PLy_exec_function().

◆ plpython_srf_cleanup_callback()

static void plpython_srf_cleanup_callback ( void *  arg)
static

Definition at line 681 of file plpy_exec.c.

682 {
683  PLySRFState *srfstate = (PLySRFState *) arg;
684 
685  /* Release refcount on the iter, if we still have one */
686  Py_XDECREF(srfstate->iter);
687  srfstate->iter = NULL;
688  /* And drop any saved args; we won't need them */
689  if (srfstate->savedargs)
691  srfstate->savedargs = NULL;
692 }
void * arg
static void PLy_function_drop_args(PLySavedArgs *savedargs)
Definition: plpy_exec.c:584
PLySavedArgs * savedargs
Definition: plpy_exec.c:30
PyObject * iter
Definition: plpy_exec.c:29

References arg, PLySRFState::iter, PLy_function_drop_args(), and PLySRFState::savedargs.

Referenced by PLy_exec_function().

◆ plpython_trigger_error_callback()

static void plpython_trigger_error_callback ( void *  arg)
static

Definition at line 1052 of file plpy_exec.c.

1053 {
1055 
1056  if (exec_ctx->curr_proc)
1057  errcontext("while modifying trigger row");
1058 }

References PLyExecutionContext::curr_proc, errcontext, and PLy_current_execution_context().

Referenced by PLy_modify_tuple().

◆ PLy_abort_open_subtransactions()

static void PLy_abort_open_subtransactions ( int  save_subxact_level)
static

Definition at line 1104 of file plpy_exec.c.

1105 {
1106  Assert(save_subxact_level >= 0);
1107 
1108  while (list_length(explicit_subtransactions) > save_subxact_level)
1109  {
1110  PLySubtransactionData *subtransactiondata;
1111 
1113 
1114  ereport(WARNING,
1115  (errmsg("forcibly aborting a subtransaction that has not been exited")));
1116 
1118 
1119  subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
1121 
1122  MemoryContextSwitchTo(subtransactiondata->oldcontext);
1123  CurrentResourceOwner = subtransactiondata->oldowner;
1124  pfree(subtransactiondata);
1125  }
1126 }
#define Assert(condition)
Definition: c.h:858
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define WARNING
Definition: elog.h:36
#define ereport(elevel,...)
Definition: elog.h:149
List * list_delete_first(List *list)
Definition: list.c:943
void pfree(void *pointer)
Definition: mcxt.c:1521
static int list_length(const List *l)
Definition: pg_list.h:152
#define NIL
Definition: pg_list.h:68
#define linitial(l)
Definition: pg_list.h:178
List * explicit_subtransactions
MemoryContextSwitchTo(old_ctx)
ResourceOwner CurrentResourceOwner
Definition: resowner.c:165
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4783

References Assert, CurrentResourceOwner, ereport, errmsg(), explicit_subtransactions, linitial, list_delete_first(), list_length(), MemoryContextSwitchTo(), NIL, PLySubtransactionData::oldcontext, PLySubtransactionData::oldowner, pfree(), RollbackAndReleaseCurrentSubTransaction(), and WARNING.

Referenced by PLy_procedure_call().

◆ PLy_exec_function()

Datum PLy_exec_function ( FunctionCallInfo  fcinfo,
PLyProcedure proc 
)

Definition at line 55 of file plpy_exec.c.

56 {
57  bool is_setof = proc->is_setof;
58  Datum rv;
59  PyObject *volatile plargs = NULL;
60  PyObject *volatile plrv = NULL;
61  FuncCallContext *volatile funcctx = NULL;
62  PLySRFState *volatile srfstate = NULL;
63  ErrorContextCallback plerrcontext;
64 
65  /*
66  * If the function is called recursively, we must push outer-level
67  * arguments into the stack. This must be immediately before the PG_TRY
68  * to ensure that the corresponding pop happens.
69  */
71 
72  PG_TRY();
73  {
74  if (is_setof)
75  {
76  /* First Call setup */
77  if (SRF_IS_FIRSTCALL())
78  {
79  funcctx = SRF_FIRSTCALL_INIT();
80  srfstate = (PLySRFState *)
82  sizeof(PLySRFState));
83  /* Immediately register cleanup callback */
85  srfstate->callback.arg = (void *) srfstate;
87  &srfstate->callback);
88  funcctx->user_fctx = (void *) srfstate;
89  }
90  /* Every call setup */
91  funcctx = SRF_PERCALL_SETUP();
92  Assert(funcctx != NULL);
93  srfstate = (PLySRFState *) funcctx->user_fctx;
94  Assert(srfstate != NULL);
95  }
96 
97  if (srfstate == NULL || srfstate->iter == NULL)
98  {
99  /*
100  * Non-SETOF function or first time for SETOF function: build
101  * args, then actually execute the function.
102  */
103  plargs = PLy_function_build_args(fcinfo, proc);
104  plrv = PLy_procedure_call(proc, "args", plargs);
105  Assert(plrv != NULL);
106  }
107  else
108  {
109  /*
110  * Second or later call for a SETOF function: restore arguments in
111  * globals dict to what they were when we left off. We must do
112  * this in case multiple evaluations of the same SETOF function
113  * are interleaved. It's a bit annoying, since the iterator may
114  * not look at the arguments at all, but we have no way to know
115  * that. Fortunately this isn't terribly expensive.
116  */
117  if (srfstate->savedargs)
118  PLy_function_restore_args(proc, srfstate->savedargs);
119  srfstate->savedargs = NULL; /* deleted by restore_args */
120  }
121 
122  /*
123  * If it returns a set, call the iterator to get the next return item.
124  * We stay in the SPI context while doing this, because PyIter_Next()
125  * calls back into Python code which might contain SPI calls.
126  */
127  if (is_setof)
128  {
129  if (srfstate->iter == NULL)
130  {
131  /* first time -- do checks and setup */
132  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
133 
134  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
135  (rsi->allowedModes & SFRM_ValuePerCall) == 0)
136  {
137  ereport(ERROR,
138  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
139  errmsg("unsupported set function return mode"),
140  errdetail("PL/Python set-returning functions only support returning one value per call.")));
141  }
143 
144  /* Make iterator out of returned object */
145  srfstate->iter = PyObject_GetIter(plrv);
146 
147  Py_DECREF(plrv);
148  plrv = NULL;
149 
150  if (srfstate->iter == NULL)
151  ereport(ERROR,
152  (errcode(ERRCODE_DATATYPE_MISMATCH),
153  errmsg("returned object cannot be iterated"),
154  errdetail("PL/Python set-returning functions must return an iterable object.")));
155  }
156 
157  /* Fetch next from iterator */
158  plrv = PyIter_Next(srfstate->iter);
159  if (plrv == NULL)
160  {
161  /* Iterator is exhausted or error happened */
162  bool has_error = (PyErr_Occurred() != NULL);
163 
164  Py_DECREF(srfstate->iter);
165  srfstate->iter = NULL;
166 
167  if (has_error)
168  PLy_elog(ERROR, "error fetching next item from iterator");
169 
170  /* Pass a null through the data-returning steps below */
171  Py_INCREF(Py_None);
172  plrv = Py_None;
173  }
174  else
175  {
176  /*
177  * This won't be last call, so save argument values. We do
178  * this again each time in case the iterator is changing those
179  * values.
180  */
181  srfstate->savedargs = PLy_function_save_args(proc);
182  }
183  }
184 
185  /*
186  * Disconnect from SPI manager and then create the return values datum
187  * (if the input function does a palloc for it this must not be
188  * allocated in the SPI memory context because SPI_finish would free
189  * it).
190  */
191  if (SPI_finish() != SPI_OK_FINISH)
192  elog(ERROR, "SPI_finish failed");
193 
195  plerrcontext.previous = error_context_stack;
196  error_context_stack = &plerrcontext;
197 
198  /*
199  * For a procedure or function declared to return void, the Python
200  * return value must be None. For void-returning functions, we also
201  * treat a None return value as a special "void datum" rather than
202  * NULL (as is the case for non-void-returning functions).
203  */
204  if (proc->result.typoid == VOIDOID)
205  {
206  if (plrv != Py_None)
207  {
208  if (proc->is_procedure)
209  ereport(ERROR,
210  (errcode(ERRCODE_DATATYPE_MISMATCH),
211  errmsg("PL/Python procedure did not return None")));
212  else
213  ereport(ERROR,
214  (errcode(ERRCODE_DATATYPE_MISMATCH),
215  errmsg("PL/Python function with return type \"void\" did not return None")));
216  }
217 
218  fcinfo->isnull = false;
219  rv = (Datum) 0;
220  }
221  else if (plrv == Py_None &&
222  srfstate && srfstate->iter == NULL)
223  {
224  /*
225  * In a SETOF function, the iteration-ending null isn't a real
226  * value; don't pass it through the input function, which might
227  * complain.
228  */
229  fcinfo->isnull = true;
230  rv = (Datum) 0;
231  }
232  else
233  {
234  /*
235  * Normal conversion of result. However, if the result is of type
236  * RECORD, we have to set up for that each time through, since it
237  * might be different from last time.
238  */
239  if (proc->result.typoid == RECORDOID)
240  {
241  TupleDesc desc;
242 
243  if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
244  ereport(ERROR,
245  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
246  errmsg("function returning record called in context "
247  "that cannot accept type record")));
248  PLy_output_setup_record(&proc->result, desc, proc);
249  }
250 
251  rv = PLy_output_convert(&proc->result, plrv,
252  &fcinfo->isnull);
253  }
254  }
255  PG_CATCH();
256  {
257  /* Pop old arguments from the stack if they were pushed above */
258  PLy_global_args_pop(proc);
259 
260  Py_XDECREF(plargs);
261  Py_XDECREF(plrv);
262 
263  /*
264  * If there was an error within a SRF, the iterator might not have
265  * been exhausted yet. Clear it so the next invocation of the
266  * function will start the iteration again. (This code is probably
267  * unnecessary now; plpython_srf_cleanup_callback should take care of
268  * cleanup. But it doesn't hurt anything to do it here.)
269  */
270  if (srfstate)
271  {
272  Py_XDECREF(srfstate->iter);
273  srfstate->iter = NULL;
274  /* And drop any saved args; we won't need them */
275  if (srfstate->savedargs)
277  srfstate->savedargs = NULL;
278  }
279 
280  PG_RE_THROW();
281  }
282  PG_END_TRY();
283 
284  error_context_stack = plerrcontext.previous;
285 
286  /* Pop old arguments from the stack if they were pushed above */
287  PLy_global_args_pop(proc);
288 
289  Py_XDECREF(plargs);
290  Py_DECREF(plrv);
291 
292  if (srfstate)
293  {
294  /* We're in a SRF, exit appropriately */
295  if (srfstate->iter == NULL)
296  {
297  /* Iterator exhausted, so we're done */
298  SRF_RETURN_DONE(funcctx);
299  }
300  else if (fcinfo->isnull)
301  SRF_RETURN_NEXT_NULL(funcctx);
302  else
303  SRF_RETURN_NEXT(funcctx, rv);
304  }
305 
306  /* Plain function, just return the Datum value (possibly null) */
307  return rv;
308 }
int errdetail(const char *fmt,...)
Definition: elog.c:1203
ErrorContextCallback * error_context_stack
Definition: elog.c:94
int errcode(int sqlerrcode)
Definition: elog.c:853
#define PG_RE_THROW()
Definition: elog.h:411
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:380
#define elog(elevel,...)
Definition: elog.h:224
@ SFRM_ValuePerCall
Definition: execnodes.h:316
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_RETURN_NEXT_NULL(_funcctx)
Definition: funcapi.h:319
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define PLy_elog
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:568
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1215
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
static void PLy_global_args_push(PLyProcedure *proc)
Definition: plpy_exec.c:613
static PyObject * PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:435
static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs)
Definition: plpy_exec.c:544
static PyObject * PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
Definition: plpy_exec.c:1062
static void PLy_global_args_pop(PLyProcedure *proc)
Definition: plpy_exec.c:643
static void plpython_srf_cleanup_callback(void *arg)
Definition: plpy_exec.c:681
static PLySavedArgs * PLy_function_save_args(PLyProcedure *proc)
Definition: plpy_exec.c:498
static void plpython_return_error_callback(void *arg)
Definition: plpy_exec.c:695
void PLy_output_setup_record(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:261
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
Definition: plpy_typeio.c:120
uintptr_t Datum
Definition: postgres.h:64
int SPI_finish(void)
Definition: spi.c:182
#define SPI_OK_FINISH
Definition: spi.h:83
struct ErrorContextCallback * previous
Definition: elog.h:295
void(* callback)(void *arg)
Definition: elog.h:296
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
fmNodePtr resultinfo
Definition: fmgr.h:89
MemoryContextCallbackFunction func
Definition: palloc.h:49
PLyObToDatum result
MemoryContextCallback callback
Definition: plpy_exec.c:31
SetFunctionReturnMode returnMode
Definition: execnodes.h:336
int allowedModes
Definition: execnodes.h:334

References ReturnSetInfo::allowedModes, MemoryContextCallback::arg, Assert, ErrorContextCallback::callback, PLySRFState::callback, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, error_context_stack, MemoryContextCallback::func, get_call_result_type(), if(), PLyProcedure::is_procedure, PLyProcedure::is_setof, IsA, FunctionCallInfoBaseData::isnull, PLySRFState::iter, MemoryContextAllocZero(), MemoryContextRegisterResetCallback(), FuncCallContext::multi_call_memory_ctx, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, plpython_return_error_callback(), plpython_srf_cleanup_callback(), PLy_elog, PLy_function_build_args(), PLy_function_drop_args(), PLy_function_restore_args(), PLy_function_save_args(), PLy_global_args_pop(), PLy_global_args_push(), PLy_output_convert(), PLy_output_setup_record(), PLy_procedure_call(), ErrorContextCallback::previous, PLyProcedure::result, FunctionCallInfoBaseData::resultinfo, ReturnSetInfo::returnMode, PLySRFState::savedargs, SFRM_ValuePerCall, SPI_finish(), SPI_OK_FINISH, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, SRF_RETURN_NEXT_NULL, TYPEFUNC_COMPOSITE, PLyObToDatum::typoid, and FuncCallContext::user_fctx.

Referenced by plpython3_call_handler(), and plpython3_inline_handler().

◆ PLy_exec_trigger()

HeapTuple PLy_exec_trigger ( FunctionCallInfo  fcinfo,
PLyProcedure proc 
)

Definition at line 321 of file plpy_exec.c.

322 {
323  HeapTuple rv = NULL;
324  PyObject *volatile plargs = NULL;
325  PyObject *volatile plrv = NULL;
326  TriggerData *tdata;
327  TupleDesc rel_descr;
328 
329  Assert(CALLED_AS_TRIGGER(fcinfo));
330  tdata = (TriggerData *) fcinfo->context;
331 
332  /*
333  * Input/output conversion for trigger tuples. We use the result and
334  * result_in fields to store the tuple conversion info. We do this over
335  * again on each call to cover the possibility that the relation's tupdesc
336  * changed since the trigger was last called. The PLy_xxx_setup_func
337  * calls should only happen once, but PLy_input_setup_tuple and
338  * PLy_output_setup_tuple are responsible for not doing repetitive work.
339  */
340  rel_descr = RelationGetDescr(tdata->tg_relation);
341  if (proc->result.typoid != rel_descr->tdtypeid)
342  PLy_output_setup_func(&proc->result, proc->mcxt,
343  rel_descr->tdtypeid,
344  rel_descr->tdtypmod,
345  proc);
346  if (proc->result_in.typoid != rel_descr->tdtypeid)
347  PLy_input_setup_func(&proc->result_in, proc->mcxt,
348  rel_descr->tdtypeid,
349  rel_descr->tdtypmod,
350  proc);
351  PLy_output_setup_tuple(&proc->result, rel_descr, proc);
352  PLy_input_setup_tuple(&proc->result_in, rel_descr, proc);
353 
354  /*
355  * If the trigger is called recursively, we must push outer-level
356  * arguments into the stack. This must be immediately before the PG_TRY
357  * to ensure that the corresponding pop happens.
358  */
359  PLy_global_args_push(proc);
360 
361  PG_TRY();
362  {
364 
365  rc = SPI_register_trigger_data(tdata);
366  Assert(rc >= 0);
367 
368  plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
369  plrv = PLy_procedure_call(proc, "TD", plargs);
370 
371  Assert(plrv != NULL);
372 
373  /*
374  * Disconnect from SPI manager
375  */
376  if (SPI_finish() != SPI_OK_FINISH)
377  elog(ERROR, "SPI_finish failed");
378 
379  /*
380  * return of None means we're happy with the tuple
381  */
382  if (plrv != Py_None)
383  {
384  char *srv;
385 
386  if (PyUnicode_Check(plrv))
387  srv = PLyUnicode_AsString(plrv);
388  else
389  {
390  ereport(ERROR,
391  (errcode(ERRCODE_DATA_EXCEPTION),
392  errmsg("unexpected return value from trigger procedure"),
393  errdetail("Expected None or a string.")));
394  srv = NULL; /* keep compiler quiet */
395  }
396 
397  if (pg_strcasecmp(srv, "SKIP") == 0)
398  rv = NULL;
399  else if (pg_strcasecmp(srv, "MODIFY") == 0)
400  {
401  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
403  rv = PLy_modify_tuple(proc, plargs, tdata, rv);
404  else
406  (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
407  }
408  else if (pg_strcasecmp(srv, "OK") != 0)
409  {
410  /*
411  * accept "OK" as an alternative to None; otherwise, raise an
412  * error
413  */
414  ereport(ERROR,
415  (errcode(ERRCODE_DATA_EXCEPTION),
416  errmsg("unexpected return value from trigger procedure"),
417  errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
418  }
419  }
420  }
421  PG_FINALLY();
422  {
423  PLy_global_args_pop(proc);
424  Py_XDECREF(plargs);
425  Py_XDECREF(plrv);
426  }
427  PG_END_TRY();
428 
429  return rv;
430 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:182
#define PG_FINALLY(...)
Definition: elog.h:387
static PyObject * PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
Definition: plpy_exec.c:705
static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, HeapTuple otup)
Definition: plpy_exec.c:922
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:296
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:418
void PLy_input_setup_tuple(PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:165
void PLy_output_setup_tuple(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:215
char * PLyUnicode_AsString(PyObject *unicode)
Definition: plpy_util.c:83
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
#define RelationGetDescr(relation)
Definition: rel.h:531
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:3356
fmNodePtr context
Definition: fmgr.h:88
PLyDatumToOb result_in
MemoryContext mcxt
Relation tg_relation
Definition: trigger.h:35
TriggerEvent tg_event
Definition: trigger.h:34
int32 tdtypmod
Definition: tupdesc.h:83
Oid tdtypeid
Definition: tupdesc.h:82
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116

References Assert, CALLED_AS_TRIGGER, FunctionCallInfoBaseData::context, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, PLyProcedure::mcxt, PG_END_TRY, PG_FINALLY, pg_strcasecmp(), PG_TRY, PG_USED_FOR_ASSERTS_ONLY, PLy_global_args_pop(), PLy_global_args_push(), PLy_input_setup_func(), PLy_input_setup_tuple(), PLy_modify_tuple(), PLy_output_setup_func(), PLy_output_setup_tuple(), PLy_procedure_call(), PLy_trigger_build_args(), PLyUnicode_AsString(), RelationGetDescr, PLyProcedure::result, PLyProcedure::result_in, SPI_finish(), SPI_OK_FINISH, SPI_register_trigger_data(), TupleDescData::tdtypeid, TupleDescData::tdtypmod, TriggerData::tg_event, TriggerData::tg_relation, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, PLyDatumToOb::typoid, PLyObToDatum::typoid, and WARNING.

Referenced by plpython3_call_handler().

◆ PLy_function_build_args()

static PyObject * PLy_function_build_args ( FunctionCallInfo  fcinfo,
PLyProcedure proc 
)
static

Definition at line 435 of file plpy_exec.c.

436 {
437  PyObject *volatile arg = NULL;
438  PyObject *args;
439  int i;
440 
441  /*
442  * Make any Py*_New() calls before the PG_TRY block so that we can quickly
443  * return NULL on failure. We can't return within the PG_TRY block, else
444  * we'd miss unwinding the exception stack.
445  */
446  args = PyList_New(proc->nargs);
447  if (!args)
448  return NULL;
449 
450  PG_TRY();
451  {
452  for (i = 0; i < proc->nargs; i++)
453  {
454  PLyDatumToOb *arginfo = &proc->args[i];
455 
456  if (fcinfo->args[i].isnull)
457  arg = NULL;
458  else
459  arg = PLy_input_convert(arginfo, fcinfo->args[i].value);
460 
461  if (arg == NULL)
462  {
463  Py_INCREF(Py_None);
464  arg = Py_None;
465  }
466 
467  if (PyList_SetItem(args, i, arg) == -1)
468  PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments");
469 
470  if (proc->argnames && proc->argnames[i] &&
471  PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
472  PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
473  arg = NULL;
474  }
475  }
476  PG_CATCH();
477  {
478  Py_XDECREF(arg);
479  Py_XDECREF(args);
480 
481  PG_RE_THROW();
482  }
483  PG_END_TRY();
484 
485  return args;
486 }
int i
Definition: isn.c:73
PyObject * PLy_input_convert(PLyDatumToOb *arg, Datum val)
Definition: plpy_typeio.c:81
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum value
Definition: postgres.h:75
bool isnull
Definition: postgres.h:77
PLyDatumToOb * args
char ** argnames
PyObject * globals

References arg, PLyProcedure::argnames, generate_unaccent_rules::args, FunctionCallInfoBaseData::args, PLyProcedure::args, ERROR, PLyProcedure::globals, i, NullableDatum::isnull, PLyProcedure::nargs, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLy_elog, PLy_input_convert(), and NullableDatum::value.

Referenced by PLy_exec_function().

◆ PLy_function_drop_args()

static void PLy_function_drop_args ( PLySavedArgs savedargs)
static

Definition at line 584 of file plpy_exec.c.

585 {
586  int i;
587 
588  /* Drop references for named args */
589  for (i = 0; i < savedargs->nargs; i++)
590  {
591  Py_XDECREF(savedargs->namedargs[i]);
592  }
593 
594  /* Drop refs to the "args" and "TD" objects, too */
595  Py_XDECREF(savedargs->args);
596  Py_XDECREF(savedargs->td);
597 
598  /* And free the PLySavedArgs struct */
599  pfree(savedargs);
600 }
PyObject * args
PyObject * namedargs[FLEXIBLE_ARRAY_MEMBER]
PyObject * td

References PLySavedArgs::args, i, PLySavedArgs::namedargs, PLySavedArgs::nargs, pfree(), and PLySavedArgs::td.

Referenced by plpython_srf_cleanup_callback(), and PLy_exec_function().

◆ PLy_function_restore_args()

static void PLy_function_restore_args ( PLyProcedure proc,
PLySavedArgs savedargs 
)
static

Definition at line 544 of file plpy_exec.c.

545 {
546  /* Restore named arguments into their slots in the globals dict */
547  if (proc->argnames)
548  {
549  int i;
550 
551  for (i = 0; i < savedargs->nargs; i++)
552  {
553  if (proc->argnames[i] && savedargs->namedargs[i])
554  {
555  PyDict_SetItemString(proc->globals, proc->argnames[i],
556  savedargs->namedargs[i]);
557  Py_DECREF(savedargs->namedargs[i]);
558  }
559  }
560  }
561 
562  /* Restore the "args" object, too */
563  if (savedargs->args)
564  {
565  PyDict_SetItemString(proc->globals, "args", savedargs->args);
566  Py_DECREF(savedargs->args);
567  }
568 
569  /* Restore the "TD" object, too */
570  if (savedargs->td)
571  {
572  PyDict_SetItemString(proc->globals, "TD", savedargs->td);
573  Py_DECREF(savedargs->td);
574  }
575 
576  /* And free the PLySavedArgs struct */
577  pfree(savedargs);
578 }

References PLyProcedure::argnames, PLySavedArgs::args, PLyProcedure::globals, i, PLySavedArgs::namedargs, PLySavedArgs::nargs, pfree(), and PLySavedArgs::td.

Referenced by PLy_exec_function(), and PLy_global_args_pop().

◆ PLy_function_save_args()

static PLySavedArgs * PLy_function_save_args ( PLyProcedure proc)
static

Definition at line 498 of file plpy_exec.c.

499 {
500  PLySavedArgs *result;
501 
502  /* saved args are always allocated in procedure's context */
503  result = (PLySavedArgs *)
505  offsetof(PLySavedArgs, namedargs) +
506  proc->nargs * sizeof(PyObject *));
507  result->nargs = proc->nargs;
508 
509  /* Fetch the "args" list */
510  result->args = PyDict_GetItemString(proc->globals, "args");
511  Py_XINCREF(result->args);
512 
513  /* If it's a trigger, also save "TD" */
514  if (proc->is_trigger)
515  {
516  result->td = PyDict_GetItemString(proc->globals, "TD");
517  Py_XINCREF(result->td);
518  }
519 
520  /* Fetch all the named arguments */
521  if (proc->argnames)
522  {
523  int i;
524 
525  for (i = 0; i < result->nargs; i++)
526  {
527  if (proc->argnames[i])
528  {
529  result->namedargs[i] = PyDict_GetItemString(proc->globals,
530  proc->argnames[i]);
531  Py_XINCREF(result->namedargs[i]);
532  }
533  }
534  }
535 
536  return result;
537 }

References PLyProcedure::argnames, PLySavedArgs::args, PLyProcedure::globals, i, PLyProcedure::is_trigger, PLyProcedure::mcxt, MemoryContextAllocZero(), PLySavedArgs::namedargs, PLySavedArgs::nargs, PLyProcedure::nargs, and PLySavedArgs::td.

Referenced by PLy_exec_function(), and PLy_global_args_push().

◆ PLy_global_args_pop()

static void PLy_global_args_pop ( PLyProcedure proc)
static

Definition at line 643 of file plpy_exec.c.

644 {
645  Assert(proc->calldepth > 0);
646  /* We only need to pop if we were already inside some active call */
647  if (proc->calldepth > 1)
648  {
649  PLySavedArgs *ptr = proc->argstack;
650 
651  /* Pop the callstack */
652  Assert(ptr != NULL);
653  proc->argstack = ptr->next;
654  proc->calldepth--;
655 
656  /* Restore argument values, then free ptr */
657  PLy_function_restore_args(proc, ptr);
658  }
659  else
660  {
661  /* Exiting call depth 1 */
662  Assert(proc->argstack == NULL);
663  proc->calldepth--;
664 
665  /*
666  * We used to delete the named arguments (but not "args") from the
667  * proc's globals dict when exiting the outermost call level for a
668  * function. This seems rather pointless though: nothing can see the
669  * dict until the function is called again, at which time we'll
670  * overwrite those dict entries. So don't bother with that.
671  */
672  }
673 }
PLySavedArgs * argstack
struct PLySavedArgs * next

References PLyProcedure::argstack, Assert, PLyProcedure::calldepth, PLySavedArgs::next, and PLy_function_restore_args().

Referenced by PLy_exec_function(), and PLy_exec_trigger().

◆ PLy_global_args_push()

static void PLy_global_args_push ( PLyProcedure proc)
static

Definition at line 613 of file plpy_exec.c.

614 {
615  /* We only need to push if we are already inside some active call */
616  if (proc->calldepth > 0)
617  {
618  PLySavedArgs *node;
619 
620  /* Build a struct containing current argument values */
621  node = PLy_function_save_args(proc);
622 
623  /*
624  * Push the saved argument values into the procedure's stack. Once we
625  * modify either proc->argstack or proc->calldepth, we had better
626  * return without the possibility of error.
627  */
628  node->next = proc->argstack;
629  proc->argstack = node;
630  }
631  proc->calldepth++;
632 }

References PLyProcedure::argstack, PLyProcedure::calldepth, PLySavedArgs::next, and PLy_function_save_args().

Referenced by PLy_exec_function(), and PLy_exec_trigger().

◆ PLy_modify_tuple()

static HeapTuple PLy_modify_tuple ( PLyProcedure proc,
PyObject *  pltd,
TriggerData tdata,
HeapTuple  otup 
)
static

Definition at line 922 of file plpy_exec.c.

924 {
925  HeapTuple rtup;
926  PyObject *volatile plntup;
927  PyObject *volatile plkeys;
928  PyObject *volatile plval;
929  Datum *volatile modvalues;
930  bool *volatile modnulls;
931  bool *volatile modrepls;
932  ErrorContextCallback plerrcontext;
933 
935  plerrcontext.previous = error_context_stack;
936  error_context_stack = &plerrcontext;
937 
938  plntup = plkeys = plval = NULL;
939  modvalues = NULL;
940  modnulls = NULL;
941  modrepls = NULL;
942 
943  PG_TRY();
944  {
945  TupleDesc tupdesc;
946  int nkeys,
947  i;
948 
949  if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
950  ereport(ERROR,
951  (errcode(ERRCODE_UNDEFINED_OBJECT),
952  errmsg("TD[\"new\"] deleted, cannot modify row")));
953  Py_INCREF(plntup);
954  if (!PyDict_Check(plntup))
955  ereport(ERROR,
956  (errcode(ERRCODE_DATATYPE_MISMATCH),
957  errmsg("TD[\"new\"] is not a dictionary")));
958 
959  plkeys = PyDict_Keys(plntup);
960  nkeys = PyList_Size(plkeys);
961 
962  tupdesc = RelationGetDescr(tdata->tg_relation);
963 
964  modvalues = (Datum *) palloc0(tupdesc->natts * sizeof(Datum));
965  modnulls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
966  modrepls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
967 
968  for (i = 0; i < nkeys; i++)
969  {
970  PyObject *platt;
971  char *plattstr;
972  int attn;
973  PLyObToDatum *att;
974 
975  platt = PyList_GetItem(plkeys, i);
976  if (PyUnicode_Check(platt))
977  plattstr = PLyUnicode_AsString(platt);
978  else
979  {
980  ereport(ERROR,
981  (errcode(ERRCODE_DATATYPE_MISMATCH),
982  errmsg("TD[\"new\"] dictionary key at ordinal position %d is not a string", i)));
983  plattstr = NULL; /* keep compiler quiet */
984  }
985  attn = SPI_fnumber(tupdesc, plattstr);
986  if (attn == SPI_ERROR_NOATTRIBUTE)
987  ereport(ERROR,
988  (errcode(ERRCODE_UNDEFINED_COLUMN),
989  errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
990  plattstr)));
991  if (attn <= 0)
992  ereport(ERROR,
993  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
994  errmsg("cannot set system attribute \"%s\"",
995  plattstr)));
996  if (TupleDescAttr(tupdesc, attn - 1)->attgenerated)
997  ereport(ERROR,
998  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
999  errmsg("cannot set generated column \"%s\"",
1000  plattstr)));
1001 
1002  plval = PyDict_GetItem(plntup, platt);
1003  if (plval == NULL)
1004  elog(FATAL, "Python interpreter is probably corrupted");
1005 
1006  Py_INCREF(plval);
1007 
1008  /* We assume proc->result is set up to convert tuples properly */
1009  att = &proc->result.u.tuple.atts[attn - 1];
1010 
1011  modvalues[attn - 1] = PLy_output_convert(att,
1012  plval,
1013  &modnulls[attn - 1]);
1014  modrepls[attn - 1] = true;
1015 
1016  Py_DECREF(plval);
1017  plval = NULL;
1018  }
1019 
1020  rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
1021  }
1022  PG_CATCH();
1023  {
1024  Py_XDECREF(plntup);
1025  Py_XDECREF(plkeys);
1026  Py_XDECREF(plval);
1027 
1028  if (modvalues)
1029  pfree(modvalues);
1030  if (modnulls)
1031  pfree(modnulls);
1032  if (modrepls)
1033  pfree(modrepls);
1034 
1035  PG_RE_THROW();
1036  }
1037  PG_END_TRY();
1038 
1039  Py_DECREF(plntup);
1040  Py_DECREF(plkeys);
1041 
1042  pfree(modvalues);
1043  pfree(modnulls);
1044  pfree(modrepls);
1045 
1046  error_context_stack = plerrcontext.previous;
1047 
1048  return rtup;
1049 }
#define FATAL
Definition: elog.h:41
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, const Datum *replValues, const bool *replIsnull, const bool *doReplace)
Definition: heaptuple.c:1209
void * palloc0(Size size)
Definition: mcxt.c:1347
static void plpython_trigger_error_callback(void *arg)
Definition: plpy_exec.c:1052
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1172
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
union PLyObToDatum::@181 u
PLyObToTuple tuple
Definition: plpy_typeio.h:143
PLyObToDatum * atts
Definition: plpy_typeio.h:113
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92

References PLyObToTuple::atts, ErrorContextCallback::callback, elog, ereport, errcode(), errmsg(), ERROR, error_context_stack, FATAL, heap_modify_tuple(), i, TupleDescData::natts, palloc0(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, plpython_trigger_error_callback(), PLy_output_convert(), PLyUnicode_AsString(), ErrorContextCallback::previous, RelationGetDescr, PLyProcedure::result, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), TriggerData::tg_relation, PLyObToDatum::tuple, TupleDescAttr, and PLyObToDatum::u.

Referenced by PLy_exec_trigger().

◆ PLy_procedure_call()

static PyObject * PLy_procedure_call ( PLyProcedure proc,
const char *  kargs,
PyObject *  vargs 
)
static

Definition at line 1062 of file plpy_exec.c.

1063 {
1064  PyObject *rv = NULL;
1065  int volatile save_subxact_level = list_length(explicit_subtransactions);
1066 
1067  PyDict_SetItemString(proc->globals, kargs, vargs);
1068 
1069  PG_TRY();
1070  {
1071 #if PY_VERSION_HEX >= 0x03020000
1072  rv = PyEval_EvalCode(proc->code,
1073  proc->globals, proc->globals);
1074 #else
1075  rv = PyEval_EvalCode((PyCodeObject *) proc->code,
1076  proc->globals, proc->globals);
1077 #endif
1078 
1079  /*
1080  * Since plpy will only let you close subtransactions that you
1081  * started, you cannot *unnest* subtransactions, only *nest* them
1082  * without closing.
1083  */
1084  Assert(list_length(explicit_subtransactions) >= save_subxact_level);
1085  }
1086  PG_FINALLY();
1087  {
1088  PLy_abort_open_subtransactions(save_subxact_level);
1089  }
1090  PG_END_TRY();
1091 
1092  /* If the Python code returned an error, propagate it */
1093  if (rv == NULL)
1094  PLy_elog(ERROR, NULL);
1095 
1096  return rv;
1097 }
static void PLy_abort_open_subtransactions(int save_subxact_level)
Definition: plpy_exec.c:1104
PyObject * code

References Assert, PLyProcedure::code, ERROR, explicit_subtransactions, PLyProcedure::globals, list_length(), PG_END_TRY, PG_FINALLY, PG_TRY, PLy_abort_open_subtransactions(), and PLy_elog.

Referenced by PLy_exec_function(), and PLy_exec_trigger().

◆ PLy_trigger_build_args()

static PyObject * PLy_trigger_build_args ( FunctionCallInfo  fcinfo,
PLyProcedure proc,
HeapTuple rv 
)
static

Definition at line 705 of file plpy_exec.c.

706 {
707  TriggerData *tdata = (TriggerData *) fcinfo->context;
708  TupleDesc rel_descr = RelationGetDescr(tdata->tg_relation);
709  PyObject *pltname,
710  *pltevent,
711  *pltwhen,
712  *pltlevel,
713  *pltrelid,
714  *plttablename,
715  *plttableschema,
716  *pltargs,
717  *pytnew,
718  *pytold,
719  *pltdata;
720  char *stroid;
721 
722  /*
723  * Make any Py*_New() calls before the PG_TRY block so that we can quickly
724  * return NULL on failure. We can't return within the PG_TRY block, else
725  * we'd miss unwinding the exception stack.
726  */
727  pltdata = PyDict_New();
728  if (!pltdata)
729  return NULL;
730 
731  if (tdata->tg_trigger->tgnargs)
732  {
733  pltargs = PyList_New(tdata->tg_trigger->tgnargs);
734  if (!pltargs)
735  {
736  Py_DECREF(pltdata);
737  return NULL;
738  }
739  }
740  else
741  {
742  Py_INCREF(Py_None);
743  pltargs = Py_None;
744  }
745 
746  PG_TRY();
747  {
748  pltname = PLyUnicode_FromString(tdata->tg_trigger->tgname);
749  PyDict_SetItemString(pltdata, "name", pltname);
750  Py_DECREF(pltname);
751 
754  pltrelid = PLyUnicode_FromString(stroid);
755  PyDict_SetItemString(pltdata, "relid", pltrelid);
756  Py_DECREF(pltrelid);
757  pfree(stroid);
758 
759  stroid = SPI_getrelname(tdata->tg_relation);
760  plttablename = PLyUnicode_FromString(stroid);
761  PyDict_SetItemString(pltdata, "table_name", plttablename);
762  Py_DECREF(plttablename);
763  pfree(stroid);
764 
765  stroid = SPI_getnspname(tdata->tg_relation);
766  plttableschema = PLyUnicode_FromString(stroid);
767  PyDict_SetItemString(pltdata, "table_schema", plttableschema);
768  Py_DECREF(plttableschema);
769  pfree(stroid);
770 
771  if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
772  pltwhen = PLyUnicode_FromString("BEFORE");
773  else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
774  pltwhen = PLyUnicode_FromString("AFTER");
775  else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
776  pltwhen = PLyUnicode_FromString("INSTEAD OF");
777  else
778  {
779  elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
780  pltwhen = NULL; /* keep compiler quiet */
781  }
782  PyDict_SetItemString(pltdata, "when", pltwhen);
783  Py_DECREF(pltwhen);
784 
785  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
786  {
787  pltlevel = PLyUnicode_FromString("ROW");
788  PyDict_SetItemString(pltdata, "level", pltlevel);
789  Py_DECREF(pltlevel);
790 
791  /*
792  * Note: In BEFORE trigger, stored generated columns are not
793  * computed yet, so don't make them accessible in NEW row.
794  */
795 
796  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
797  {
798  pltevent = PLyUnicode_FromString("INSERT");
799 
800  PyDict_SetItemString(pltdata, "old", Py_None);
801  pytnew = PLy_input_from_tuple(&proc->result_in,
802  tdata->tg_trigtuple,
803  rel_descr,
804  !TRIGGER_FIRED_BEFORE(tdata->tg_event));
805  PyDict_SetItemString(pltdata, "new", pytnew);
806  Py_DECREF(pytnew);
807  *rv = tdata->tg_trigtuple;
808  }
809  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
810  {
811  pltevent = PLyUnicode_FromString("DELETE");
812 
813  PyDict_SetItemString(pltdata, "new", Py_None);
814  pytold = PLy_input_from_tuple(&proc->result_in,
815  tdata->tg_trigtuple,
816  rel_descr,
817  true);
818  PyDict_SetItemString(pltdata, "old", pytold);
819  Py_DECREF(pytold);
820  *rv = tdata->tg_trigtuple;
821  }
822  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
823  {
824  pltevent = PLyUnicode_FromString("UPDATE");
825 
826  pytnew = PLy_input_from_tuple(&proc->result_in,
827  tdata->tg_newtuple,
828  rel_descr,
829  !TRIGGER_FIRED_BEFORE(tdata->tg_event));
830  PyDict_SetItemString(pltdata, "new", pytnew);
831  Py_DECREF(pytnew);
832  pytold = PLy_input_from_tuple(&proc->result_in,
833  tdata->tg_trigtuple,
834  rel_descr,
835  true);
836  PyDict_SetItemString(pltdata, "old", pytold);
837  Py_DECREF(pytold);
838  *rv = tdata->tg_newtuple;
839  }
840  else
841  {
842  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
843  pltevent = NULL; /* keep compiler quiet */
844  }
845 
846  PyDict_SetItemString(pltdata, "event", pltevent);
847  Py_DECREF(pltevent);
848  }
849  else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
850  {
851  pltlevel = PLyUnicode_FromString("STATEMENT");
852  PyDict_SetItemString(pltdata, "level", pltlevel);
853  Py_DECREF(pltlevel);
854 
855  PyDict_SetItemString(pltdata, "old", Py_None);
856  PyDict_SetItemString(pltdata, "new", Py_None);
857  *rv = NULL;
858 
859  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
860  pltevent = PLyUnicode_FromString("INSERT");
861  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
862  pltevent = PLyUnicode_FromString("DELETE");
863  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
864  pltevent = PLyUnicode_FromString("UPDATE");
865  else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
866  pltevent = PLyUnicode_FromString("TRUNCATE");
867  else
868  {
869  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
870  pltevent = NULL; /* keep compiler quiet */
871  }
872 
873  PyDict_SetItemString(pltdata, "event", pltevent);
874  Py_DECREF(pltevent);
875  }
876  else
877  elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
878 
879  if (tdata->tg_trigger->tgnargs)
880  {
881  /*
882  * all strings...
883  */
884  int i;
885  PyObject *pltarg;
886 
887  /* pltargs should have been allocated before the PG_TRY block. */
888  Assert(pltargs && pltargs != Py_None);
889 
890  for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
891  {
892  pltarg = PLyUnicode_FromString(tdata->tg_trigger->tgargs[i]);
893 
894  /*
895  * stolen, don't Py_DECREF
896  */
897  PyList_SetItem(pltargs, i, pltarg);
898  }
899  }
900  else
901  {
902  Assert(pltargs == Py_None);
903  }
904  PyDict_SetItemString(pltdata, "args", pltargs);
905  Py_DECREF(pltargs);
906  }
907  PG_CATCH();
908  {
909  Py_XDECREF(pltargs);
910  Py_XDECREF(pltdata);
911  PG_RE_THROW();
912  }
913  PG_END_TRY();
914 
915  return pltdata;
916 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:642
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:47
PyObject * PLy_input_from_tuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
Definition: plpy_typeio.c:134
PyObject * PLyUnicode_FromString(const char *s)
Definition: plpy_util.c:118
static char * DatumGetCString(Datum X)
Definition: postgres.h:335
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
char * SPI_getrelname(Relation rel)
Definition: spi.c:1323
char * SPI_getnspname(Relation rel)
Definition: spi.c:1329
Oid rd_id
Definition: rel.h:113
HeapTuple tg_newtuple
Definition: trigger.h:37
Trigger * tg_trigger
Definition: trigger.h:38
HeapTuple tg_trigtuple
Definition: trigger.h:36
char * tgname
Definition: reltrigger.h:27
int16 tgnargs
Definition: reltrigger.h:38
char ** tgargs
Definition: reltrigger.h:41
#define TRIGGER_FIRED_FOR_STATEMENT(event)
Definition: trigger.h:125
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:113
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:128
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:122
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:131
#define TRIGGER_FIRED_BY_TRUNCATE(event)
Definition: trigger.h:119
#define TRIGGER_FIRED_INSTEAD(event)
Definition: trigger.h:134

References Assert, FunctionCallInfoBaseData::context, DatumGetCString(), DirectFunctionCall1, elog, ERROR, i, ObjectIdGetDatum(), oidout(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLy_input_from_tuple(), PLyUnicode_FromString(), RelationData::rd_id, RelationGetDescr, PLyProcedure::result_in, SPI_getnspname(), SPI_getrelname(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, Trigger::tgargs, Trigger::tgname, Trigger::tgnargs, TRIGGER_FIRED_AFTER, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_TRUNCATE, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TRIGGER_FIRED_FOR_STATEMENT, and TRIGGER_FIRED_INSTEAD.

Referenced by PLy_exec_trigger().