PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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/fmgrprotos.h"
#include "utils/rel.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 693 of file plpy_exec.c.

694 {
696 
697  if (exec_ctx->curr_proc &&
698  !exec_ctx->curr_proc->is_procedure)
699  errcontext("while creating return value");
700 }
#define errcontext
Definition: elog.h:196
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:365
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 679 of file plpy_exec.c.

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

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 1050 of file plpy_exec.c.

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

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 1102 of file plpy_exec.c.

1103 {
1104  Assert(save_subxact_level >= 0);
1105 
1106  while (list_length(explicit_subtransactions) > save_subxact_level)
1107  {
1108  PLySubtransactionData *subtransactiondata;
1109 
1111 
1112  ereport(WARNING,
1113  (errmsg("forcibly aborting a subtransaction that has not been exited")));
1114 
1116 
1117  subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
1119 
1120  MemoryContextSwitchTo(subtransactiondata->oldcontext);
1121  CurrentResourceOwner = subtransactiondata->oldowner;
1122  pfree(subtransactiondata);
1123  }
1124 }
#define Assert(condition)
Definition: c.h:837
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:4788

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 53 of file plpy_exec.c.

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

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 319 of file plpy_exec.c.

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

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

583 {
584  int i;
585 
586  /* Drop references for named args */
587  for (i = 0; i < savedargs->nargs; i++)
588  {
589  Py_XDECREF(savedargs->namedargs[i]);
590  }
591 
592  /* Drop refs to the "args" and "TD" objects, too */
593  Py_XDECREF(savedargs->args);
594  Py_XDECREF(savedargs->td);
595 
596  /* And free the PLySavedArgs struct */
597  pfree(savedargs);
598 }
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 542 of file plpy_exec.c.

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

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 496 of file plpy_exec.c.

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

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 641 of file plpy_exec.c.

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

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

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 920 of file plpy_exec.c.

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

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

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