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

669 {
671 
672  if (exec_ctx->curr_proc &&
673  !exec_ctx->curr_proc->is_procedure)
674  errcontext("while creating return value");
675 }
#define errcontext
Definition: elog.h:190
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:369
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 654 of file plpy_exec.c.

655 {
656  PLySRFState *srfstate = (PLySRFState *) arg;
657 
658  /* Release refcount on the iter, if we still have one */
659  Py_XDECREF(srfstate->iter);
660  srfstate->iter = NULL;
661  /* And drop any saved args; we won't need them */
662  if (srfstate->savedargs)
664  srfstate->savedargs = NULL;
665 }
void * arg
static void PLy_function_drop_args(PLySavedArgs *savedargs)
Definition: plpy_exec.c:558
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 1008 of file plpy_exec.c.

1009 {
1011 
1012  if (exec_ctx->curr_proc)
1013  errcontext("while modifying trigger row");
1014 }

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

1061 {
1062  Assert(save_subxact_level >= 0);
1063 
1064  while (list_length(explicit_subtransactions) > save_subxact_level)
1065  {
1066  PLySubtransactionData *subtransactiondata;
1067 
1069 
1070  ereport(WARNING,
1071  (errmsg("forcibly aborting a subtransaction that has not been exited")));
1072 
1074 
1075  subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
1077 
1078  MemoryContextSwitchTo(subtransactiondata->oldcontext);
1079  CurrentResourceOwner = subtransactiondata->oldowner;
1080  pfree(subtransactiondata);
1081  }
1082 }
int errmsg(const char *fmt,...)
Definition: elog.c:904
#define WARNING
Definition: elog.h:30
#define ereport(elevel,...)
Definition: elog.h:143
Assert(fmt[strlen(fmt) - 1] !='\n')
List * list_delete_first(List *list)
Definition: list.c:942
void pfree(void *pointer)
Definition: mcxt.c:1175
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static int list_length(const List *l)
Definition: pg_list.h:150
#define NIL
Definition: pg_list.h:66
#define linitial(l)
Definition: pg_list.h:176
List * explicit_subtransactions
ResourceOwner CurrentResourceOwner
Definition: resowner.c:146
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4595

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  /* Normal conversion of result */
235  rv = PLy_output_convert(&proc->result, plrv,
236  &fcinfo->isnull);
237  }
238  }
239  PG_CATCH();
240  {
241  /* Pop old arguments from the stack if they were pushed above */
242  PLy_global_args_pop(proc);
243 
244  Py_XDECREF(plargs);
245  Py_XDECREF(plrv);
246 
247  /*
248  * If there was an error within a SRF, the iterator might not have
249  * been exhausted yet. Clear it so the next invocation of the
250  * function will start the iteration again. (This code is probably
251  * unnecessary now; plpython_srf_cleanup_callback should take care of
252  * cleanup. But it doesn't hurt anything to do it here.)
253  */
254  if (srfstate)
255  {
256  Py_XDECREF(srfstate->iter);
257  srfstate->iter = NULL;
258  /* And drop any saved args; we won't need them */
259  if (srfstate->savedargs)
261  srfstate->savedargs = NULL;
262  }
263 
264  PG_RE_THROW();
265  }
266  PG_END_TRY();
267 
268  error_context_stack = plerrcontext.previous;
269 
270  /* Pop old arguments from the stack if they were pushed above */
271  PLy_global_args_pop(proc);
272 
273  Py_XDECREF(plargs);
274  Py_DECREF(plrv);
275 
276  if (srfstate)
277  {
278  /* We're in a SRF, exit appropriately */
279  if (srfstate->iter == NULL)
280  {
281  /* Iterator exhausted, so we're done */
282  SRF_RETURN_DONE(funcctx);
283  }
284  else if (fcinfo->isnull)
285  SRF_RETURN_NEXT_NULL(funcctx);
286  else
287  SRF_RETURN_NEXT(funcctx, rv);
288  }
289 
290  /* Plain function, just return the Datum value (possibly null) */
291  return rv;
292 }
int errdetail(const char *fmt,...)
Definition: elog.c:1037
ErrorContextCallback * error_context_stack
Definition: elog.c:93
int errcode(int sqlerrcode)
Definition: elog.c:693
#define PG_RE_THROW()
Definition: elog.h:340
#define PG_END_TRY()
Definition: elog.h:324
#define PG_TRY()
Definition: elog.h:299
#define ERROR
Definition: elog.h:33
#define PG_CATCH()
Definition: elog.h:309
@ SFRM_ValuePerCall
Definition: execnodes.h:293
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:299
#define SRF_RETURN_NEXT_NULL(_funcctx)
Definition: funcapi.h:314
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:303
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:305
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:301
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:323
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:77
#define PLy_elog
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:292
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:906
#define IsA(nodeptr, _type_)
Definition: nodes.h:625
static void PLy_global_args_push(PLyProcedure *proc)
Definition: plpy_exec.c:586
static PyObject * PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:413
static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs)
Definition: plpy_exec.c:525
static PyObject * PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
Definition: plpy_exec.c:1018
static void PLy_global_args_pop(PLyProcedure *proc)
Definition: plpy_exec.c:616
static void plpython_srf_cleanup_callback(void *arg)
Definition: plpy_exec.c:654
static PLySavedArgs * PLy_function_save_args(PLyProcedure *proc)
Definition: plpy_exec.c:486
static void plpython_return_error_callback(void *arg)
Definition: plpy_exec.c:668
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
Definition: plpy_typeio.c:119
uintptr_t Datum
Definition: postgres.h:411
int SPI_finish(void)
Definition: spi.c:183
#define SPI_OK_FINISH
Definition: spi.h:83
struct ErrorContextCallback * previous
Definition: elog.h:232
void(* callback)(void *arg)
Definition: elog.h:233
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:313
int allowedModes
Definition: execnodes.h:311

References ReturnSetInfo::allowedModes, MemoryContextCallback::arg, Assert(), ErrorContextCallback::callback, PLySRFState::callback, elog(), ereport, errcode(), errdetail(), errmsg(), ERROR, error_context_stack, MemoryContextCallback::func, 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_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, 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 305 of file plpy_exec.c.

306 {
307  HeapTuple rv = NULL;
308  PyObject *volatile plargs = NULL;
309  PyObject *volatile plrv = NULL;
310  TriggerData *tdata;
311  TupleDesc rel_descr;
312 
313  Assert(CALLED_AS_TRIGGER(fcinfo));
314  tdata = (TriggerData *) fcinfo->context;
315 
316  /*
317  * Input/output conversion for trigger tuples. We use the result and
318  * result_in fields to store the tuple conversion info. We do this over
319  * again on each call to cover the possibility that the relation's tupdesc
320  * changed since the trigger was last called. The PLy_xxx_setup_func
321  * calls should only happen once, but PLy_input_setup_tuple and
322  * PLy_output_setup_tuple are responsible for not doing repetitive work.
323  */
324  rel_descr = RelationGetDescr(tdata->tg_relation);
325  if (proc->result.typoid != rel_descr->tdtypeid)
326  PLy_output_setup_func(&proc->result, proc->mcxt,
327  rel_descr->tdtypeid,
328  rel_descr->tdtypmod,
329  proc);
330  if (proc->result_in.typoid != rel_descr->tdtypeid)
331  PLy_input_setup_func(&proc->result_in, proc->mcxt,
332  rel_descr->tdtypeid,
333  rel_descr->tdtypmod,
334  proc);
335  PLy_output_setup_tuple(&proc->result, rel_descr, proc);
336  PLy_input_setup_tuple(&proc->result_in, rel_descr, proc);
337 
338  PG_TRY();
339  {
341 
342  rc = SPI_register_trigger_data(tdata);
343  Assert(rc >= 0);
344 
345  plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
346  plrv = PLy_procedure_call(proc, "TD", plargs);
347 
348  Assert(plrv != NULL);
349 
350  /*
351  * Disconnect from SPI manager
352  */
353  if (SPI_finish() != SPI_OK_FINISH)
354  elog(ERROR, "SPI_finish failed");
355 
356  /*
357  * return of None means we're happy with the tuple
358  */
359  if (plrv != Py_None)
360  {
361  char *srv;
362 
363  if (PyUnicode_Check(plrv))
364  srv = PLyUnicode_AsString(plrv);
365  else
366  {
367  ereport(ERROR,
368  (errcode(ERRCODE_DATA_EXCEPTION),
369  errmsg("unexpected return value from trigger procedure"),
370  errdetail("Expected None or a string.")));
371  srv = NULL; /* keep compiler quiet */
372  }
373 
374  if (pg_strcasecmp(srv, "SKIP") == 0)
375  rv = NULL;
376  else if (pg_strcasecmp(srv, "MODIFY") == 0)
377  {
378  TriggerData *tdata = (TriggerData *) fcinfo->context;
379 
382  rv = PLy_modify_tuple(proc, plargs, tdata, rv);
383  else
385  (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
386  }
387  else if (pg_strcasecmp(srv, "OK") != 0)
388  {
389  /*
390  * accept "OK" as an alternative to None; otherwise, raise an
391  * error
392  */
393  ereport(ERROR,
394  (errcode(ERRCODE_DATA_EXCEPTION),
395  errmsg("unexpected return value from trigger procedure"),
396  errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
397  }
398  }
399  }
400  PG_FINALLY();
401  {
402  Py_XDECREF(plargs);
403  Py_XDECREF(plrv);
404  }
405  PG_END_TRY();
406 
407  return rv;
408 }
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:166
#define PG_FINALLY()
Definition: elog.h:316
static PyObject * PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
Definition: plpy_exec.c:678
static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, HeapTuple otup)
Definition: plpy_exec.c:878
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:295
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:417
void PLy_input_setup_tuple(PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:164
void PLy_output_setup_tuple(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:214
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:514
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:3336
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, if(), PLyProcedure::mcxt, PG_END_TRY, PG_FINALLY, pg_strcasecmp(), PG_TRY, PG_USED_FOR_ASSERTS_ONLY, 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 413 of file plpy_exec.c.

414 {
415  PyObject *volatile arg = NULL;
416  PyObject *volatile args = NULL;
417  int i;
418 
419  PG_TRY();
420  {
421  args = PyList_New(proc->nargs);
422  if (!args)
423  return NULL;
424 
425  for (i = 0; i < proc->nargs; i++)
426  {
427  PLyDatumToOb *arginfo = &proc->args[i];
428 
429  if (fcinfo->args[i].isnull)
430  arg = NULL;
431  else
432  arg = PLy_input_convert(arginfo, fcinfo->args[i].value);
433 
434  if (arg == NULL)
435  {
436  Py_INCREF(Py_None);
437  arg = Py_None;
438  }
439 
440  if (PyList_SetItem(args, i, arg) == -1)
441  PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments");
442 
443  if (proc->argnames && proc->argnames[i] &&
444  PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
445  PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
446  arg = NULL;
447  }
448 
449  /* Set up output conversion for functions returning RECORD */
450  if (proc->result.typoid == RECORDOID)
451  {
452  TupleDesc desc;
453 
454  if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
455  ereport(ERROR,
456  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
457  errmsg("function returning record called in context "
458  "that cannot accept type record")));
459 
460  /* cache the output conversion functions */
461  PLy_output_setup_record(&proc->result, desc, proc);
462  }
463  }
464  PG_CATCH();
465  {
466  Py_XDECREF(arg);
467  Py_XDECREF(args);
468 
469  PG_RE_THROW();
470  }
471  PG_END_TRY();
472 
473  return args;
474 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
int i
Definition: isn.c:73
PyObject * PLy_input_convert(PLyDatumToOb *arg, Datum val)
Definition: plpy_typeio.c:80
void PLy_output_setup_record(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:260
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
Datum value
Definition: postgres.h:422
PLyDatumToOb * args
char ** argnames
PyObject * globals

References arg, PLyProcedure::argnames, generate_unaccent_rules::args, FunctionCallInfoBaseData::args, PLyProcedure::args, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), PLyProcedure::globals, i, NullableDatum::isnull, PLyProcedure::nargs, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLy_elog, PLy_input_convert(), PLy_output_setup_record(), PLyProcedure::result, TYPEFUNC_COMPOSITE, PLyObToDatum::typoid, and NullableDatum::value.

Referenced by PLy_exec_function().

◆ PLy_function_drop_args()

static void PLy_function_drop_args ( PLySavedArgs savedargs)
static

Definition at line 558 of file plpy_exec.c.

559 {
560  int i;
561 
562  /* Drop references for named args */
563  for (i = 0; i < savedargs->nargs; i++)
564  {
565  Py_XDECREF(savedargs->namedargs[i]);
566  }
567 
568  /* Drop ref to the "args" object, too */
569  Py_XDECREF(savedargs->args);
570 
571  /* And free the PLySavedArgs struct */
572  pfree(savedargs);
573 }
PyObject * args
PyObject * namedargs[FLEXIBLE_ARRAY_MEMBER]

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

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

526 {
527  /* Restore named arguments into their slots in the globals dict */
528  if (proc->argnames)
529  {
530  int i;
531 
532  for (i = 0; i < savedargs->nargs; i++)
533  {
534  if (proc->argnames[i] && savedargs->namedargs[i])
535  {
536  PyDict_SetItemString(proc->globals, proc->argnames[i],
537  savedargs->namedargs[i]);
538  Py_DECREF(savedargs->namedargs[i]);
539  }
540  }
541  }
542 
543  /* Restore the "args" object, too */
544  if (savedargs->args)
545  {
546  PyDict_SetItemString(proc->globals, "args", savedargs->args);
547  Py_DECREF(savedargs->args);
548  }
549 
550  /* And free the PLySavedArgs struct */
551  pfree(savedargs);
552 }

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

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

487 {
488  PLySavedArgs *result;
489 
490  /* saved args are always allocated in procedure's context */
491  result = (PLySavedArgs *)
493  offsetof(PLySavedArgs, namedargs) +
494  proc->nargs * sizeof(PyObject *));
495  result->nargs = proc->nargs;
496 
497  /* Fetch the "args" list */
498  result->args = PyDict_GetItemString(proc->globals, "args");
499  Py_XINCREF(result->args);
500 
501  /* Fetch all the named arguments */
502  if (proc->argnames)
503  {
504  int i;
505 
506  for (i = 0; i < result->nargs; i++)
507  {
508  if (proc->argnames[i])
509  {
510  result->namedargs[i] = PyDict_GetItemString(proc->globals,
511  proc->argnames[i]);
512  Py_XINCREF(result->namedargs[i]);
513  }
514  }
515  }
516 
517  return result;
518 }
#define offsetof(type, field)
Definition: c.h:738

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

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

617 {
618  Assert(proc->calldepth > 0);
619  /* We only need to pop if we were already inside some active call */
620  if (proc->calldepth > 1)
621  {
622  PLySavedArgs *ptr = proc->argstack;
623 
624  /* Pop the callstack */
625  Assert(ptr != NULL);
626  proc->argstack = ptr->next;
627  proc->calldepth--;
628 
629  /* Restore argument values, then free ptr */
630  PLy_function_restore_args(proc, ptr);
631  }
632  else
633  {
634  /* Exiting call depth 1 */
635  Assert(proc->argstack == NULL);
636  proc->calldepth--;
637 
638  /*
639  * We used to delete the named arguments (but not "args") from the
640  * proc's globals dict when exiting the outermost call level for a
641  * function. This seems rather pointless though: nothing can see the
642  * dict until the function is called again, at which time we'll
643  * overwrite those dict entries. So don't bother with that.
644  */
645  }
646 }
PLySavedArgs * argstack
struct PLySavedArgs * next

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

Referenced by PLy_exec_function().

◆ PLy_global_args_push()

static void PLy_global_args_push ( PLyProcedure proc)
static

Definition at line 586 of file plpy_exec.c.

587 {
588  /* We only need to push if we are already inside some active call */
589  if (proc->calldepth > 0)
590  {
591  PLySavedArgs *node;
592 
593  /* Build a struct containing current argument values */
594  node = PLy_function_save_args(proc);
595 
596  /*
597  * Push the saved argument values into the procedure's stack. Once we
598  * modify either proc->argstack or proc->calldepth, we had better
599  * return without the possibility of error.
600  */
601  node->next = proc->argstack;
602  proc->argstack = node;
603  }
604  proc->calldepth++;
605 }

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

Referenced by PLy_exec_function().

◆ PLy_modify_tuple()

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

Definition at line 878 of file plpy_exec.c.

880 {
881  HeapTuple rtup;
882  PyObject *volatile plntup;
883  PyObject *volatile plkeys;
884  PyObject *volatile plval;
885  Datum *volatile modvalues;
886  bool *volatile modnulls;
887  bool *volatile modrepls;
888  ErrorContextCallback plerrcontext;
889 
891  plerrcontext.previous = error_context_stack;
892  error_context_stack = &plerrcontext;
893 
894  plntup = plkeys = plval = NULL;
895  modvalues = NULL;
896  modnulls = NULL;
897  modrepls = NULL;
898 
899  PG_TRY();
900  {
901  TupleDesc tupdesc;
902  int nkeys,
903  i;
904 
905  if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
906  ereport(ERROR,
907  (errcode(ERRCODE_UNDEFINED_OBJECT),
908  errmsg("TD[\"new\"] deleted, cannot modify row")));
909  Py_INCREF(plntup);
910  if (!PyDict_Check(plntup))
911  ereport(ERROR,
912  (errcode(ERRCODE_DATATYPE_MISMATCH),
913  errmsg("TD[\"new\"] is not a dictionary")));
914 
915  plkeys = PyDict_Keys(plntup);
916  nkeys = PyList_Size(plkeys);
917 
918  tupdesc = RelationGetDescr(tdata->tg_relation);
919 
920  modvalues = (Datum *) palloc0(tupdesc->natts * sizeof(Datum));
921  modnulls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
922  modrepls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
923 
924  for (i = 0; i < nkeys; i++)
925  {
926  PyObject *platt;
927  char *plattstr;
928  int attn;
929  PLyObToDatum *att;
930 
931  platt = PyList_GetItem(plkeys, i);
932  if (PyUnicode_Check(platt))
933  plattstr = PLyUnicode_AsString(platt);
934  else
935  {
936  ereport(ERROR,
937  (errcode(ERRCODE_DATATYPE_MISMATCH),
938  errmsg("TD[\"new\"] dictionary key at ordinal position %d is not a string", i)));
939  plattstr = NULL; /* keep compiler quiet */
940  }
941  attn = SPI_fnumber(tupdesc, plattstr);
942  if (attn == SPI_ERROR_NOATTRIBUTE)
943  ereport(ERROR,
944  (errcode(ERRCODE_UNDEFINED_COLUMN),
945  errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
946  plattstr)));
947  if (attn <= 0)
948  ereport(ERROR,
949  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
950  errmsg("cannot set system attribute \"%s\"",
951  plattstr)));
952  if (TupleDescAttr(tupdesc, attn - 1)->attgenerated)
953  ereport(ERROR,
954  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
955  errmsg("cannot set generated column \"%s\"",
956  plattstr)));
957 
958  plval = PyDict_GetItem(plntup, platt);
959  if (plval == NULL)
960  elog(FATAL, "Python interpreter is probably corrupted");
961 
962  Py_INCREF(plval);
963 
964  /* We assume proc->result is set up to convert tuples properly */
965  att = &proc->result.u.tuple.atts[attn - 1];
966 
967  modvalues[attn - 1] = PLy_output_convert(att,
968  plval,
969  &modnulls[attn - 1]);
970  modrepls[attn - 1] = true;
971 
972  Py_DECREF(plval);
973  plval = NULL;
974  }
975 
976  rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
977  }
978  PG_CATCH();
979  {
980  Py_XDECREF(plntup);
981  Py_XDECREF(plkeys);
982  Py_XDECREF(plval);
983 
984  if (modvalues)
985  pfree(modvalues);
986  if (modnulls)
987  pfree(modnulls);
988  if (modrepls)
989  pfree(modrepls);
990 
991  PG_RE_THROW();
992  }
993  PG_END_TRY();
994 
995  Py_DECREF(plntup);
996  Py_DECREF(plkeys);
997 
998  pfree(modvalues);
999  pfree(modnulls);
1000  pfree(modrepls);
1001 
1002  error_context_stack = plerrcontext.previous;
1003 
1004  return rtup;
1005 }
#define FATAL
Definition: elog.h:35
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
void * palloc0(Size size)
Definition: mcxt.c:1099
static void plpython_trigger_error_callback(void *arg)
Definition: plpy_exec.c:1008
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1173
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:76
PLyObToTuple tuple
Definition: plpy_typeio.h:143
union PLyObToDatum::@173 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 1018 of file plpy_exec.c.

1019 {
1020  PyObject *rv = NULL;
1021  int volatile save_subxact_level = list_length(explicit_subtransactions);
1022 
1023  PyDict_SetItemString(proc->globals, kargs, vargs);
1024 
1025  PG_TRY();
1026  {
1027 #if PY_VERSION_HEX >= 0x03020000
1028  rv = PyEval_EvalCode(proc->code,
1029  proc->globals, proc->globals);
1030 #else
1031  rv = PyEval_EvalCode((PyCodeObject *) proc->code,
1032  proc->globals, proc->globals);
1033 #endif
1034 
1035  /*
1036  * Since plpy will only let you close subtransactions that you
1037  * started, you cannot *unnest* subtransactions, only *nest* them
1038  * without closing.
1039  */
1040  Assert(list_length(explicit_subtransactions) >= save_subxact_level);
1041  }
1042  PG_FINALLY();
1043  {
1044  PLy_abort_open_subtransactions(save_subxact_level);
1045  }
1046  PG_END_TRY();
1047 
1048  /* If the Python code returned an error, propagate it */
1049  if (rv == NULL)
1050  PLy_elog(ERROR, NULL);
1051 
1052  return rv;
1053 }
static void PLy_abort_open_subtransactions(int save_subxact_level)
Definition: plpy_exec.c:1060
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 678 of file plpy_exec.c.

679 {
680  TriggerData *tdata = (TriggerData *) fcinfo->context;
681  TupleDesc rel_descr = RelationGetDescr(tdata->tg_relation);
682  PyObject *pltname,
683  *pltevent,
684  *pltwhen,
685  *pltlevel,
686  *pltrelid,
687  *plttablename,
688  *plttableschema;
689  PyObject *pltargs,
690  *pytnew,
691  *pytold;
692  PyObject *volatile pltdata = NULL;
693  char *stroid;
694 
695  PG_TRY();
696  {
697  pltdata = PyDict_New();
698  if (!pltdata)
699  return NULL;
700 
701  pltname = PLyUnicode_FromString(tdata->tg_trigger->tgname);
702  PyDict_SetItemString(pltdata, "name", pltname);
703  Py_DECREF(pltname);
704 
707  pltrelid = PLyUnicode_FromString(stroid);
708  PyDict_SetItemString(pltdata, "relid", pltrelid);
709  Py_DECREF(pltrelid);
710  pfree(stroid);
711 
712  stroid = SPI_getrelname(tdata->tg_relation);
713  plttablename = PLyUnicode_FromString(stroid);
714  PyDict_SetItemString(pltdata, "table_name", plttablename);
715  Py_DECREF(plttablename);
716  pfree(stroid);
717 
718  stroid = SPI_getnspname(tdata->tg_relation);
719  plttableschema = PLyUnicode_FromString(stroid);
720  PyDict_SetItemString(pltdata, "table_schema", plttableschema);
721  Py_DECREF(plttableschema);
722  pfree(stroid);
723 
724  if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
725  pltwhen = PLyUnicode_FromString("BEFORE");
726  else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
727  pltwhen = PLyUnicode_FromString("AFTER");
728  else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
729  pltwhen = PLyUnicode_FromString("INSTEAD OF");
730  else
731  {
732  elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
733  pltwhen = NULL; /* keep compiler quiet */
734  }
735  PyDict_SetItemString(pltdata, "when", pltwhen);
736  Py_DECREF(pltwhen);
737 
738  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
739  {
740  pltlevel = PLyUnicode_FromString("ROW");
741  PyDict_SetItemString(pltdata, "level", pltlevel);
742  Py_DECREF(pltlevel);
743 
744  /*
745  * Note: In BEFORE trigger, stored generated columns are not
746  * computed yet, so don't make them accessible in NEW row.
747  */
748 
749  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
750  {
751  pltevent = PLyUnicode_FromString("INSERT");
752 
753  PyDict_SetItemString(pltdata, "old", Py_None);
754  pytnew = PLy_input_from_tuple(&proc->result_in,
755  tdata->tg_trigtuple,
756  rel_descr,
757  !TRIGGER_FIRED_BEFORE(tdata->tg_event));
758  PyDict_SetItemString(pltdata, "new", pytnew);
759  Py_DECREF(pytnew);
760  *rv = tdata->tg_trigtuple;
761  }
762  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
763  {
764  pltevent = PLyUnicode_FromString("DELETE");
765 
766  PyDict_SetItemString(pltdata, "new", Py_None);
767  pytold = PLy_input_from_tuple(&proc->result_in,
768  tdata->tg_trigtuple,
769  rel_descr,
770  true);
771  PyDict_SetItemString(pltdata, "old", pytold);
772  Py_DECREF(pytold);
773  *rv = tdata->tg_trigtuple;
774  }
775  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
776  {
777  pltevent = PLyUnicode_FromString("UPDATE");
778 
779  pytnew = PLy_input_from_tuple(&proc->result_in,
780  tdata->tg_newtuple,
781  rel_descr,
782  !TRIGGER_FIRED_BEFORE(tdata->tg_event));
783  PyDict_SetItemString(pltdata, "new", pytnew);
784  Py_DECREF(pytnew);
785  pytold = PLy_input_from_tuple(&proc->result_in,
786  tdata->tg_trigtuple,
787  rel_descr,
788  true);
789  PyDict_SetItemString(pltdata, "old", pytold);
790  Py_DECREF(pytold);
791  *rv = tdata->tg_newtuple;
792  }
793  else
794  {
795  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
796  pltevent = NULL; /* keep compiler quiet */
797  }
798 
799  PyDict_SetItemString(pltdata, "event", pltevent);
800  Py_DECREF(pltevent);
801  }
802  else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
803  {
804  pltlevel = PLyUnicode_FromString("STATEMENT");
805  PyDict_SetItemString(pltdata, "level", pltlevel);
806  Py_DECREF(pltlevel);
807 
808  PyDict_SetItemString(pltdata, "old", Py_None);
809  PyDict_SetItemString(pltdata, "new", Py_None);
810  *rv = NULL;
811 
812  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
813  pltevent = PLyUnicode_FromString("INSERT");
814  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
815  pltevent = PLyUnicode_FromString("DELETE");
816  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
817  pltevent = PLyUnicode_FromString("UPDATE");
818  else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
819  pltevent = PLyUnicode_FromString("TRUNCATE");
820  else
821  {
822  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
823  pltevent = NULL; /* keep compiler quiet */
824  }
825 
826  PyDict_SetItemString(pltdata, "event", pltevent);
827  Py_DECREF(pltevent);
828  }
829  else
830  elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
831 
832  if (tdata->tg_trigger->tgnargs)
833  {
834  /*
835  * all strings...
836  */
837  int i;
838  PyObject *pltarg;
839 
840  pltargs = PyList_New(tdata->tg_trigger->tgnargs);
841  if (!pltargs)
842  {
843  Py_DECREF(pltdata);
844  return NULL;
845  }
846  for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
847  {
848  pltarg = PLyUnicode_FromString(tdata->tg_trigger->tgargs[i]);
849 
850  /*
851  * stolen, don't Py_DECREF
852  */
853  PyList_SetItem(pltargs, i, pltarg);
854  }
855  }
856  else
857  {
858  Py_INCREF(Py_None);
859  pltargs = Py_None;
860  }
861  PyDict_SetItemString(pltdata, "args", pltargs);
862  Py_DECREF(pltargs);
863  }
864  PG_CATCH();
865  {
866  Py_XDECREF(pltdata);
867  PG_RE_THROW();
868  }
869  PG_END_TRY();
870 
871  return pltdata;
872 }
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:631
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:127
PyObject * PLy_input_from_tuple(PLyDatumToOb *arg, HeapTuple tuple, TupleDesc desc, bool include_generated)
Definition: plpy_typeio.c:133
PyObject * PLyUnicode_FromString(const char *s)
Definition: plpy_util.c:118
#define ObjectIdGetDatum(X)
Definition: postgres.h:551
#define DatumGetCString(X)
Definition: postgres.h:610
char * SPI_getrelname(Relation rel)
Definition: spi.c:1324
char * SPI_getnspname(Relation rel)
Definition: spi.c:1330
Oid rd_id
Definition: rel.h:111
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 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().