PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
plpy_exec.c
Go to the documentation of this file.
1 /*
2  * executing Python code
3  *
4  * src/pl/plpython/plpy_exec.c
5  */
6 
7 #include "postgres.h"
8 
9 #include "access/htup_details.h"
10 #include "access/xact.h"
11 #include "catalog/pg_type.h"
12 #include "commands/trigger.h"
13 #include "executor/spi.h"
14 #include "funcapi.h"
15 #include "utils/builtins.h"
16 #include "utils/rel.h"
17 #include "utils/typcache.h"
18 
19 #include "plpython.h"
20 
21 #include "plpy_exec.h"
22 
23 #include "plpy_elog.h"
24 #include "plpy_main.h"
25 #include "plpy_procedure.h"
26 #include "plpy_subxactobject.h"
27 
28 
29 /* saved state for a set-returning function */
30 typedef struct PLySRFState
31 {
32  PyObject *iter; /* Python iterator producing results */
33  PLySavedArgs *savedargs; /* function argument values */
34  MemoryContextCallback callback; /* for releasing refcounts when done */
35 } PLySRFState;
36 
37 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc);
39 static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs);
40 static void PLy_function_drop_args(PLySavedArgs *savedargs);
41 static void PLy_global_args_push(PLyProcedure *proc);
42 static void PLy_global_args_pop(PLyProcedure *proc);
43 static void plpython_srf_cleanup_callback(void *arg);
44 static void plpython_return_error_callback(void *arg);
45 
46 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc,
47  HeapTuple *rv);
48 static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd,
49  TriggerData *tdata, HeapTuple otup);
50 static void plpython_trigger_error_callback(void *arg);
51 
52 static PyObject *PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs);
53 static void PLy_abort_open_subtransactions(int save_subxact_level);
54 
55 
56 /* function subhandler */
57 Datum
59 {
60  Datum rv;
61  PyObject *volatile plargs = NULL;
62  PyObject *volatile plrv = NULL;
63  FuncCallContext *volatile funcctx = NULL;
64  PLySRFState *volatile srfstate = NULL;
65  ErrorContextCallback plerrcontext;
66 
67  /*
68  * If the function is called recursively, we must push outer-level
69  * arguments into the stack. This must be immediately before the PG_TRY
70  * to ensure that the corresponding pop happens.
71  */
73 
74  PG_TRY();
75  {
76  if (proc->is_setof)
77  {
78  /* First Call setup */
79  if (SRF_IS_FIRSTCALL())
80  {
81  funcctx = SRF_FIRSTCALL_INIT();
82  srfstate = (PLySRFState *)
84  sizeof(PLySRFState));
85  /* Immediately register cleanup callback */
87  srfstate->callback.arg = (void *) srfstate;
89  &srfstate->callback);
90  funcctx->user_fctx = (void *) srfstate;
91  }
92  /* Every call setup */
93  funcctx = SRF_PERCALL_SETUP();
94  Assert(funcctx != NULL);
95  srfstate = (PLySRFState *) funcctx->user_fctx;
96  }
97 
98  if (srfstate == NULL || srfstate->iter == NULL)
99  {
100  /*
101  * Non-SETOF function or first time for SETOF function: build
102  * args, then actually execute the function.
103  */
104  plargs = PLy_function_build_args(fcinfo, proc);
105  plrv = PLy_procedure_call(proc, "args", plargs);
106  Assert(plrv != NULL);
107  }
108  else
109  {
110  /*
111  * Second or later call for a SETOF function: restore arguments in
112  * globals dict to what they were when we left off. We must do
113  * this in case multiple evaluations of the same SETOF function
114  * are interleaved. It's a bit annoying, since the iterator may
115  * not look at the arguments at all, but we have no way to know
116  * that. Fortunately this isn't terribly expensive.
117  */
118  if (srfstate->savedargs)
119  PLy_function_restore_args(proc, srfstate->savedargs);
120  srfstate->savedargs = NULL; /* deleted by restore_args */
121  }
122 
123  /*
124  * If it returns a set, call the iterator to get the next return item.
125  * We stay in the SPI context while doing this, because PyIter_Next()
126  * calls back into Python code which might contain SPI calls.
127  */
128  if (proc->is_setof)
129  {
130  if (srfstate->iter == NULL)
131  {
132  /* first time -- do checks and setup */
133  ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
134 
135  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
136  (rsi->allowedModes & SFRM_ValuePerCall) == 0)
137  {
138  ereport(ERROR,
139  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
140  errmsg("unsupported set function return mode"),
141  errdetail("PL/Python set-returning functions only support returning one value per call.")));
142  }
144 
145  /* Make iterator out of returned object */
146  srfstate->iter = PyObject_GetIter(plrv);
147 
148  Py_DECREF(plrv);
149  plrv = NULL;
150 
151  if (srfstate->iter == NULL)
152  ereport(ERROR,
153  (errcode(ERRCODE_DATATYPE_MISMATCH),
154  errmsg("returned object cannot be iterated"),
155  errdetail("PL/Python set-returning functions must return an iterable object.")));
156  }
157 
158  /* Fetch next from iterator */
159  plrv = PyIter_Next(srfstate->iter);
160  if (plrv == NULL)
161  {
162  /* Iterator is exhausted or error happened */
163  bool has_error = (PyErr_Occurred() != NULL);
164 
165  Py_DECREF(srfstate->iter);
166  srfstate->iter = NULL;
167 
168  if (has_error)
169  PLy_elog(ERROR, "error fetching next item from iterator");
170 
171  /* Pass a null through the data-returning steps below */
172  Py_INCREF(Py_None);
173  plrv = Py_None;
174  }
175  else
176  {
177  /*
178  * This won't be last call, so save argument values. We do
179  * this again each time in case the iterator is changing those
180  * values.
181  */
182  srfstate->savedargs = PLy_function_save_args(proc);
183  }
184  }
185 
186  /*
187  * Disconnect from SPI manager and then create the return values datum
188  * (if the input function does a palloc for it this must not be
189  * allocated in the SPI memory context because SPI_finish would free
190  * it).
191  */
192  if (SPI_finish() != SPI_OK_FINISH)
193  elog(ERROR, "SPI_finish failed");
194 
196  plerrcontext.previous = error_context_stack;
197  error_context_stack = &plerrcontext;
198 
199  /*
200  * If the function is declared to return void, the Python return value
201  * must be None. For void-returning functions, we also treat a None
202  * return value as a special "void datum" rather than NULL (as is the
203  * case for non-void-returning functions).
204  */
205  if (proc->result.out.d.typoid == VOIDOID)
206  {
207  if (plrv != Py_None)
208  ereport(ERROR,
209  (errcode(ERRCODE_DATATYPE_MISMATCH),
210  errmsg("PL/Python function with return type \"void\" did not return None")));
211 
212  fcinfo->isnull = false;
213  rv = (Datum) 0;
214  }
215  else if (plrv == Py_None)
216  {
217  fcinfo->isnull = true;
218 
219  /*
220  * In a SETOF function, the iteration-ending null isn't a real
221  * value; don't pass it through the input function, which might
222  * complain.
223  */
224  if (srfstate && srfstate->iter == NULL)
225  rv = (Datum) 0;
226  else if (proc->result.is_rowtype < 1)
227  rv = InputFunctionCall(&proc->result.out.d.typfunc,
228  NULL,
229  proc->result.out.d.typioparam,
230  -1);
231  else
232  /* Tuple as None */
233  rv = (Datum) NULL;
234  }
235  else if (proc->result.is_rowtype >= 1)
236  {
237  TupleDesc desc;
238 
239  /* make sure it's not an unnamed record */
240  Assert((proc->result.out.d.typoid == RECORDOID &&
241  proc->result.out.d.typmod != -1) ||
242  (proc->result.out.d.typoid != RECORDOID &&
243  proc->result.out.d.typmod == -1));
244 
246  proc->result.out.d.typmod);
247 
248  rv = PLyObject_ToCompositeDatum(&proc->result, desc, plrv, false);
249  fcinfo->isnull = (rv == (Datum) NULL);
250 
251  ReleaseTupleDesc(desc);
252  }
253  else
254  {
255  fcinfo->isnull = false;
256  rv = (proc->result.out.d.func) (&proc->result.out.d, -1, plrv, false);
257  }
258  }
259  PG_CATCH();
260  {
261  /* Pop old arguments from the stack if they were pushed above */
262  PLy_global_args_pop(proc);
263 
264  Py_XDECREF(plargs);
265  Py_XDECREF(plrv);
266 
267  /*
268  * If there was an error within a SRF, the iterator might not have
269  * been exhausted yet. Clear it so the next invocation of the
270  * function will start the iteration again. (This code is probably
271  * unnecessary now; plpython_srf_cleanup_callback should take care of
272  * cleanup. But it doesn't hurt anything to do it here.)
273  */
274  if (srfstate)
275  {
276  Py_XDECREF(srfstate->iter);
277  srfstate->iter = NULL;
278  /* And drop any saved args; we won't need them */
279  if (srfstate->savedargs)
281  srfstate->savedargs = NULL;
282  }
283 
284  PG_RE_THROW();
285  }
286  PG_END_TRY();
287 
288  error_context_stack = plerrcontext.previous;
289 
290  /* Pop old arguments from the stack if they were pushed above */
291  PLy_global_args_pop(proc);
292 
293  Py_XDECREF(plargs);
294  Py_DECREF(plrv);
295 
296  if (srfstate)
297  {
298  /* We're in a SRF, exit appropriately */
299  if (srfstate->iter == NULL)
300  {
301  /* Iterator exhausted, so we're done */
302  SRF_RETURN_DONE(funcctx);
303  }
304  else if (fcinfo->isnull)
305  SRF_RETURN_NEXT_NULL(funcctx);
306  else
307  SRF_RETURN_NEXT(funcctx, rv);
308  }
309 
310  /* Plain function, just return the Datum value (possibly null) */
311  return rv;
312 }
313 
314 /* trigger subhandler
315  *
316  * the python function is expected to return Py_None if the tuple is
317  * acceptable and unmodified. Otherwise it should return a PyString
318  * object who's value is SKIP, or MODIFY. SKIP means don't perform
319  * this action. MODIFY means the tuple has been modified, so update
320  * tuple and perform action. SKIP and MODIFY assume the trigger fires
321  * BEFORE the event and is ROW level. postgres expects the function
322  * to take no arguments and return an argument of type trigger.
323  */
324 HeapTuple
326 {
327  HeapTuple rv = NULL;
328  PyObject *volatile plargs = NULL;
329  PyObject *volatile plrv = NULL;
330  TriggerData *tdata;
331 
332  Assert(CALLED_AS_TRIGGER(fcinfo));
333 
334  /*
335  * Input/output conversion for trigger tuples. Use the result TypeInfo
336  * variable to store the tuple conversion info. We do this over again on
337  * each call to cover the possibility that the relation's tupdesc changed
338  * since the trigger was last called. PLy_input_tuple_funcs and
339  * PLy_output_tuple_funcs are responsible for not doing repetitive work.
340  */
341  tdata = (TriggerData *) fcinfo->context;
342 
343  PLy_input_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
344  PLy_output_tuple_funcs(&(proc->result), tdata->tg_relation->rd_att);
345 
346  PG_TRY();
347  {
349 
350  rc = SPI_register_trigger_data(tdata);
351  Assert(rc >= 0);
352 
353  plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
354  plrv = PLy_procedure_call(proc, "TD", plargs);
355 
356  Assert(plrv != NULL);
357 
358  /*
359  * Disconnect from SPI manager
360  */
361  if (SPI_finish() != SPI_OK_FINISH)
362  elog(ERROR, "SPI_finish failed");
363 
364  /*
365  * return of None means we're happy with the tuple
366  */
367  if (plrv != Py_None)
368  {
369  char *srv;
370 
371  if (PyString_Check(plrv))
372  srv = PyString_AsString(plrv);
373  else if (PyUnicode_Check(plrv))
374  srv = PLyUnicode_AsString(plrv);
375  else
376  {
377  ereport(ERROR,
378  (errcode(ERRCODE_DATA_EXCEPTION),
379  errmsg("unexpected return value from trigger procedure"),
380  errdetail("Expected None or a string.")));
381  srv = NULL; /* keep compiler quiet */
382  }
383 
384  if (pg_strcasecmp(srv, "SKIP") == 0)
385  rv = NULL;
386  else if (pg_strcasecmp(srv, "MODIFY") == 0)
387  {
388  TriggerData *tdata = (TriggerData *) fcinfo->context;
389 
390  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
392  rv = PLy_modify_tuple(proc, plargs, tdata, rv);
393  else
395  (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
396  }
397  else if (pg_strcasecmp(srv, "OK") != 0)
398  {
399  /*
400  * accept "OK" as an alternative to None; otherwise, raise an
401  * error
402  */
403  ereport(ERROR,
404  (errcode(ERRCODE_DATA_EXCEPTION),
405  errmsg("unexpected return value from trigger procedure"),
406  errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
407  }
408  }
409  }
410  PG_CATCH();
411  {
412  Py_XDECREF(plargs);
413  Py_XDECREF(plrv);
414 
415  PG_RE_THROW();
416  }
417  PG_END_TRY();
418 
419  Py_DECREF(plargs);
420  Py_DECREF(plrv);
421 
422  return rv;
423 }
424 
425 /* helper functions for Python code execution */
426 
427 static PyObject *
429 {
430  PyObject *volatile arg = NULL;
431  PyObject *volatile args = NULL;
432  int i;
433 
434  PG_TRY();
435  {
436  args = PyList_New(proc->nargs);
437  for (i = 0; i < proc->nargs; i++)
438  {
439  if (proc->args[i].is_rowtype > 0)
440  {
441  if (fcinfo->argnull[i])
442  arg = NULL;
443  else
444  {
445  HeapTupleHeader td;
446  Oid tupType;
447  int32 tupTypmod;
448  TupleDesc tupdesc;
449  HeapTupleData tmptup;
450 
451  td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
452  /* Extract rowtype info and find a tupdesc */
453  tupType = HeapTupleHeaderGetTypeId(td);
454  tupTypmod = HeapTupleHeaderGetTypMod(td);
455  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
456 
457  /* Set up I/O funcs if not done yet */
458  if (proc->args[i].is_rowtype != 1)
459  PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
460 
461  /* Build a temporary HeapTuple control structure */
463  tmptup.t_data = td;
464 
465  arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
466  ReleaseTupleDesc(tupdesc);
467  }
468  }
469  else
470  {
471  if (fcinfo->argnull[i])
472  arg = NULL;
473  else
474  {
475  arg = (proc->args[i].in.d.func) (&(proc->args[i].in.d),
476  fcinfo->arg[i]);
477  }
478  }
479 
480  if (arg == NULL)
481  {
482  Py_INCREF(Py_None);
483  arg = Py_None;
484  }
485 
486  if (PyList_SetItem(args, i, arg) == -1)
487  PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments");
488 
489  if (proc->argnames && proc->argnames[i] &&
490  PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
491  PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
492  arg = NULL;
493  }
494 
495  /* Set up output conversion for functions returning RECORD */
496  if (proc->result.out.d.typoid == RECORDOID)
497  {
498  TupleDesc desc;
499 
500  if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
501  ereport(ERROR,
502  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
503  errmsg("function returning record called in context "
504  "that cannot accept type record")));
505 
506  /* cache the output conversion functions */
507  PLy_output_record_funcs(&(proc->result), desc);
508  }
509  }
510  PG_CATCH();
511  {
512  Py_XDECREF(arg);
513  Py_XDECREF(args);
514 
515  PG_RE_THROW();
516  }
517  PG_END_TRY();
518 
519  return args;
520 }
521 
522 /*
523  * Construct a PLySavedArgs struct representing the current values of the
524  * procedure's arguments in its globals dict. This can be used to restore
525  * those values when exiting a recursive call level or returning control to a
526  * set-returning function.
527  *
528  * This would not be necessary except for an ancient decision to make args
529  * available via the proc's globals :-( ... but we're stuck with that now.
530  */
531 static PLySavedArgs *
533 {
534  PLySavedArgs *result;
535 
536  /* saved args are always allocated in procedure's context */
537  result = (PLySavedArgs *)
539  offsetof(PLySavedArgs, namedargs) +
540  proc->nargs * sizeof(PyObject *));
541  result->nargs = proc->nargs;
542 
543  /* Fetch the "args" list */
544  result->args = PyDict_GetItemString(proc->globals, "args");
545  Py_XINCREF(result->args);
546 
547  /* Fetch all the named arguments */
548  if (proc->argnames)
549  {
550  int i;
551 
552  for (i = 0; i < result->nargs; i++)
553  {
554  if (proc->argnames[i])
555  {
556  result->namedargs[i] = PyDict_GetItemString(proc->globals,
557  proc->argnames[i]);
558  Py_XINCREF(result->namedargs[i]);
559  }
560  }
561  }
562 
563  return result;
564 }
565 
566 /*
567  * Restore procedure's arguments from a PLySavedArgs struct,
568  * then free the struct.
569  */
570 static void
572 {
573  /* Restore named arguments into their slots in the globals dict */
574  if (proc->argnames)
575  {
576  int i;
577 
578  for (i = 0; i < savedargs->nargs; i++)
579  {
580  if (proc->argnames[i] && savedargs->namedargs[i])
581  {
582  PyDict_SetItemString(proc->globals, proc->argnames[i],
583  savedargs->namedargs[i]);
584  Py_DECREF(savedargs->namedargs[i]);
585  }
586  }
587  }
588 
589  /* Restore the "args" object, too */
590  if (savedargs->args)
591  {
592  PyDict_SetItemString(proc->globals, "args", savedargs->args);
593  Py_DECREF(savedargs->args);
594  }
595 
596  /* And free the PLySavedArgs struct */
597  pfree(savedargs);
598 }
599 
600 /*
601  * Free a PLySavedArgs struct without restoring the values.
602  */
603 static void
605 {
606  int i;
607 
608  /* Drop references for named args */
609  for (i = 0; i < savedargs->nargs; i++)
610  {
611  Py_XDECREF(savedargs->namedargs[i]);
612  }
613 
614  /* Drop ref to the "args" object, too */
615  Py_XDECREF(savedargs->args);
616 
617  /* And free the PLySavedArgs struct */
618  pfree(savedargs);
619 }
620 
621 /*
622  * Save away any existing arguments for the given procedure, so that we can
623  * install new values for a recursive call. This should be invoked before
624  * doing PLy_function_build_args().
625  *
626  * NB: caller must ensure that PLy_global_args_pop gets invoked once, and
627  * only once, per successful completion of PLy_global_args_push. Otherwise
628  * we'll end up out-of-sync between the actual call stack and the contents
629  * of proc->argstack.
630  */
631 static void
633 {
634  /* We only need to push if we are already inside some active call */
635  if (proc->calldepth > 0)
636  {
637  PLySavedArgs *node;
638 
639  /* Build a struct containing current argument values */
640  node = PLy_function_save_args(proc);
641 
642  /*
643  * Push the saved argument values into the procedure's stack. Once we
644  * modify either proc->argstack or proc->calldepth, we had better
645  * return without the possibility of error.
646  */
647  node->next = proc->argstack;
648  proc->argstack = node;
649  }
650  proc->calldepth++;
651 }
652 
653 /*
654  * Pop old arguments when exiting a recursive call.
655  *
656  * Note: the idea here is to adjust the proc's callstack state before doing
657  * anything that could possibly fail. In event of any error, we want the
658  * callstack to look like we've done the pop. Leaking a bit of memory is
659  * tolerable.
660  */
661 static void
663 {
664  Assert(proc->calldepth > 0);
665  /* We only need to pop if we were already inside some active call */
666  if (proc->calldepth > 1)
667  {
668  PLySavedArgs *ptr = proc->argstack;
669 
670  /* Pop the callstack */
671  Assert(ptr != NULL);
672  proc->argstack = ptr->next;
673  proc->calldepth--;
674 
675  /* Restore argument values, then free ptr */
676  PLy_function_restore_args(proc, ptr);
677  }
678  else
679  {
680  /* Exiting call depth 1 */
681  Assert(proc->argstack == NULL);
682  proc->calldepth--;
683 
684  /*
685  * We used to delete the named arguments (but not "args") from the
686  * proc's globals dict when exiting the outermost call level for a
687  * function. This seems rather pointless though: nothing can see the
688  * dict until the function is called again, at which time we'll
689  * overwrite those dict entries. So don't bother with that.
690  */
691  }
692 }
693 
694 /*
695  * Memory context deletion callback for cleaning up a PLySRFState.
696  * We need this in case execution of the SRF is terminated early,
697  * due to error or the caller simply not running it to completion.
698  */
699 static void
701 {
702  PLySRFState *srfstate = (PLySRFState *) arg;
703 
704  /* Release refcount on the iter, if we still have one */
705  Py_XDECREF(srfstate->iter);
706  srfstate->iter = NULL;
707  /* And drop any saved args; we won't need them */
708  if (srfstate->savedargs)
710  srfstate->savedargs = NULL;
711 }
712 
713 static void
715 {
717 
718  if (exec_ctx->curr_proc)
719  errcontext("while creating return value");
720 }
721 
722 static PyObject *
724 {
725  TriggerData *tdata = (TriggerData *) fcinfo->context;
726  PyObject *pltname,
727  *pltevent,
728  *pltwhen,
729  *pltlevel,
730  *pltrelid,
731  *plttablename,
732  *plttableschema;
733  PyObject *pltargs,
734  *pytnew,
735  *pytold;
736  PyObject *volatile pltdata = NULL;
737  char *stroid;
738 
739  PG_TRY();
740  {
741  pltdata = PyDict_New();
742  if (!pltdata)
743  PLy_elog(ERROR, "could not create new dictionary while building trigger arguments");
744 
745  pltname = PyString_FromString(tdata->tg_trigger->tgname);
746  PyDict_SetItemString(pltdata, "name", pltname);
747  Py_DECREF(pltname);
748 
751  pltrelid = PyString_FromString(stroid);
752  PyDict_SetItemString(pltdata, "relid", pltrelid);
753  Py_DECREF(pltrelid);
754  pfree(stroid);
755 
756  stroid = SPI_getrelname(tdata->tg_relation);
757  plttablename = PyString_FromString(stroid);
758  PyDict_SetItemString(pltdata, "table_name", plttablename);
759  Py_DECREF(plttablename);
760  pfree(stroid);
761 
762  stroid = SPI_getnspname(tdata->tg_relation);
763  plttableschema = PyString_FromString(stroid);
764  PyDict_SetItemString(pltdata, "table_schema", plttableschema);
765  Py_DECREF(plttableschema);
766  pfree(stroid);
767 
768  if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
769  pltwhen = PyString_FromString("BEFORE");
770  else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
771  pltwhen = PyString_FromString("AFTER");
772  else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
773  pltwhen = PyString_FromString("INSTEAD OF");
774  else
775  {
776  elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
777  pltwhen = NULL; /* keep compiler quiet */
778  }
779  PyDict_SetItemString(pltdata, "when", pltwhen);
780  Py_DECREF(pltwhen);
781 
782  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
783  {
784  pltlevel = PyString_FromString("ROW");
785  PyDict_SetItemString(pltdata, "level", pltlevel);
786  Py_DECREF(pltlevel);
787 
788  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
789  {
790  pltevent = PyString_FromString("INSERT");
791 
792  PyDict_SetItemString(pltdata, "old", Py_None);
793  pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
794  tdata->tg_relation->rd_att);
795  PyDict_SetItemString(pltdata, "new", pytnew);
796  Py_DECREF(pytnew);
797  *rv = tdata->tg_trigtuple;
798  }
799  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
800  {
801  pltevent = PyString_FromString("DELETE");
802 
803  PyDict_SetItemString(pltdata, "new", Py_None);
804  pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
805  tdata->tg_relation->rd_att);
806  PyDict_SetItemString(pltdata, "old", pytold);
807  Py_DECREF(pytold);
808  *rv = tdata->tg_trigtuple;
809  }
810  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
811  {
812  pltevent = PyString_FromString("UPDATE");
813 
814  pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
815  tdata->tg_relation->rd_att);
816  PyDict_SetItemString(pltdata, "new", pytnew);
817  Py_DECREF(pytnew);
818  pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
819  tdata->tg_relation->rd_att);
820  PyDict_SetItemString(pltdata, "old", pytold);
821  Py_DECREF(pytold);
822  *rv = tdata->tg_newtuple;
823  }
824  else
825  {
826  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
827  pltevent = NULL; /* keep compiler quiet */
828  }
829 
830  PyDict_SetItemString(pltdata, "event", pltevent);
831  Py_DECREF(pltevent);
832  }
833  else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
834  {
835  pltlevel = PyString_FromString("STATEMENT");
836  PyDict_SetItemString(pltdata, "level", pltlevel);
837  Py_DECREF(pltlevel);
838 
839  PyDict_SetItemString(pltdata, "old", Py_None);
840  PyDict_SetItemString(pltdata, "new", Py_None);
841  *rv = NULL;
842 
843  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
844  pltevent = PyString_FromString("INSERT");
845  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
846  pltevent = PyString_FromString("DELETE");
847  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
848  pltevent = PyString_FromString("UPDATE");
849  else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
850  pltevent = PyString_FromString("TRUNCATE");
851  else
852  {
853  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
854  pltevent = NULL; /* keep compiler quiet */
855  }
856 
857  PyDict_SetItemString(pltdata, "event", pltevent);
858  Py_DECREF(pltevent);
859  }
860  else
861  elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
862 
863  if (tdata->tg_trigger->tgnargs)
864  {
865  /*
866  * all strings...
867  */
868  int i;
869  PyObject *pltarg;
870 
871  pltargs = PyList_New(tdata->tg_trigger->tgnargs);
872  for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
873  {
874  pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
875 
876  /*
877  * stolen, don't Py_DECREF
878  */
879  PyList_SetItem(pltargs, i, pltarg);
880  }
881  }
882  else
883  {
884  Py_INCREF(Py_None);
885  pltargs = Py_None;
886  }
887  PyDict_SetItemString(pltdata, "args", pltargs);
888  Py_DECREF(pltargs);
889  }
890  PG_CATCH();
891  {
892  Py_XDECREF(pltdata);
893  PG_RE_THROW();
894  }
895  PG_END_TRY();
896 
897  return pltdata;
898 }
899 
900 static HeapTuple
901 PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata,
902  HeapTuple otup)
903 {
904  HeapTuple rtup;
905  PyObject *volatile plntup;
906  PyObject *volatile plkeys;
907  PyObject *volatile plval;
908  Datum *volatile modvalues;
909  bool *volatile modnulls;
910  bool *volatile modrepls;
911  ErrorContextCallback plerrcontext;
912 
914  plerrcontext.previous = error_context_stack;
915  error_context_stack = &plerrcontext;
916 
917  plntup = plkeys = plval = NULL;
918  modvalues = NULL;
919  modnulls = NULL;
920  modrepls = NULL;
921 
922  PG_TRY();
923  {
924  TupleDesc tupdesc;
925  int nkeys,
926  i;
927 
928  if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
929  ereport(ERROR,
930  (errcode(ERRCODE_UNDEFINED_OBJECT),
931  errmsg("TD[\"new\"] deleted, cannot modify row")));
932  Py_INCREF(plntup);
933  if (!PyDict_Check(plntup))
934  ereport(ERROR,
935  (errcode(ERRCODE_DATATYPE_MISMATCH),
936  errmsg("TD[\"new\"] is not a dictionary")));
937 
938  plkeys = PyDict_Keys(plntup);
939  nkeys = PyList_Size(plkeys);
940 
941  tupdesc = tdata->tg_relation->rd_att;
942 
943  modvalues = (Datum *) palloc0(tupdesc->natts * sizeof(Datum));
944  modnulls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
945  modrepls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
946 
947  for (i = 0; i < nkeys; i++)
948  {
949  PyObject *platt;
950  char *plattstr;
951  int attn;
952  PLyObToDatum *att;
953  Form_pg_attribute attr;
954 
955  platt = PyList_GetItem(plkeys, i);
956  if (PyString_Check(platt))
957  plattstr = PyString_AsString(platt);
958  else if (PyUnicode_Check(platt))
959  plattstr = PLyUnicode_AsString(platt);
960  else
961  {
962  ereport(ERROR,
963  (errcode(ERRCODE_DATATYPE_MISMATCH),
964  errmsg("TD[\"new\"] dictionary key at ordinal position %d is not a string", i)));
965  plattstr = NULL; /* keep compiler quiet */
966  }
967  attn = SPI_fnumber(tupdesc, plattstr);
968  if (attn == SPI_ERROR_NOATTRIBUTE)
969  ereport(ERROR,
970  (errcode(ERRCODE_UNDEFINED_COLUMN),
971  errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
972  plattstr)));
973  if (attn <= 0)
974  ereport(ERROR,
975  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
976  errmsg("cannot set system attribute \"%s\"",
977  plattstr)));
978  att = &proc->result.out.r.atts[attn - 1];
979 
980  plval = PyDict_GetItem(plntup, platt);
981  if (plval == NULL)
982  elog(FATAL, "Python interpreter is probably corrupted");
983 
984  Py_INCREF(plval);
985 
986  attr = TupleDescAttr(tupdesc, attn - 1);
987  if (plval != Py_None)
988  {
989  modvalues[attn - 1] =
990  (att->func) (att,
991  attr->atttypmod,
992  plval,
993  false);
994  modnulls[attn - 1] = false;
995  }
996  else
997  {
998  modvalues[attn - 1] =
1000  NULL,
1001  att->typioparam,
1002  attr->atttypmod);
1003  modnulls[attn - 1] = true;
1004  }
1005  modrepls[attn - 1] = true;
1006 
1007  Py_DECREF(plval);
1008  plval = NULL;
1009  }
1010 
1011  rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
1012  }
1013  PG_CATCH();
1014  {
1015  Py_XDECREF(plntup);
1016  Py_XDECREF(plkeys);
1017  Py_XDECREF(plval);
1018 
1019  if (modvalues)
1020  pfree(modvalues);
1021  if (modnulls)
1022  pfree(modnulls);
1023  if (modrepls)
1024  pfree(modrepls);
1025 
1026  PG_RE_THROW();
1027  }
1028  PG_END_TRY();
1029 
1030  Py_DECREF(plntup);
1031  Py_DECREF(plkeys);
1032 
1033  pfree(modvalues);
1034  pfree(modnulls);
1035  pfree(modrepls);
1036 
1037  error_context_stack = plerrcontext.previous;
1038 
1039  return rtup;
1040 }
1041 
1042 static void
1044 {
1046 
1047  if (exec_ctx->curr_proc)
1048  errcontext("while modifying trigger row");
1049 }
1050 
1051 /* execute Python code, propagate Python errors to the backend */
1052 static PyObject *
1053 PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
1054 {
1055  PyObject *rv;
1056  int volatile save_subxact_level = list_length(explicit_subtransactions);
1057 
1058  PyDict_SetItemString(proc->globals, kargs, vargs);
1059 
1060  PG_TRY();
1061  {
1062 #if PY_VERSION_HEX >= 0x03020000
1063  rv = PyEval_EvalCode(proc->code,
1064  proc->globals, proc->globals);
1065 #else
1066  rv = PyEval_EvalCode((PyCodeObject *) proc->code,
1067  proc->globals, proc->globals);
1068 #endif
1069 
1070  /*
1071  * Since plpy will only let you close subtransactions that you
1072  * started, you cannot *unnest* subtransactions, only *nest* them
1073  * without closing.
1074  */
1075  Assert(list_length(explicit_subtransactions) >= save_subxact_level);
1076  }
1077  PG_CATCH();
1078  {
1079  PLy_abort_open_subtransactions(save_subxact_level);
1080  PG_RE_THROW();
1081  }
1082  PG_END_TRY();
1083 
1084  PLy_abort_open_subtransactions(save_subxact_level);
1085 
1086  /* If the Python code returned an error, propagate it */
1087  if (rv == NULL)
1088  PLy_elog(ERROR, NULL);
1089 
1090  return rv;
1091 }
1092 
1093 /*
1094  * Abort lingering subtransactions that have been explicitly started
1095  * by plpy.subtransaction().start() and not properly closed.
1096  */
1097 static void
1098 PLy_abort_open_subtransactions(int save_subxact_level)
1099 {
1100  Assert(save_subxact_level >= 0);
1101 
1102  while (list_length(explicit_subtransactions) > save_subxact_level)
1103  {
1104  PLySubtransactionData *subtransactiondata;
1105 
1107 
1108  ereport(WARNING,
1109  (errmsg("forcibly aborting a subtransaction that has not been exited")));
1110 
1112 
1113  subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
1115 
1116  MemoryContextSwitchTo(subtransactiondata->oldcontext);
1117  CurrentResourceOwner = subtransactiondata->oldowner;
1118  pfree(subtransactiondata);
1119  }
1120 }
HeapTuple PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:325
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:767
MemoryContextCallbackFunction func
Definition: palloc.h:49
FmgrInfo typfunc
Definition: plpy_typeio.h:58
#define NIL
Definition: pg_list.h:69
struct PLySavedArgs * next
void PLy_output_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc)
Definition: plpy_typeio.c:181
#define IsA(nodeptr, _type_)
Definition: nodes.h:561
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
static void PLy_global_args_push(PLyProcedure *proc)
Definition: plpy_exec.c:632
PLyTypeInfo args[FUNC_MAX_ARGS]
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1579
static void PLy_global_args_pop(PLyProcedure *proc)
Definition: plpy_exec.c:662
fmNodePtr context
Definition: fmgr.h:80
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:84
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void PLy_elog(int elevel, const char *fmt,...)
Definition: plpy_elog.c:47
int SPI_finish(void)
Definition: spi.c:149
PyObject * args
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
void PLy_input_tuple_funcs(PLyTypeInfo *arg, TupleDesc desc)
Definition: plpy_typeio.c:105
static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs)
Definition: plpy_exec.c:571
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:585
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, HeapTuple otup)
Definition: plpy_exec.c:901
unsigned int Oid
Definition: postgres_ext.h:31
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:398
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:137
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:127
struct ErrorContextCallback * previous
Definition: elog.h:238
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:259
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
int natts
Definition: tupdesc.h:73
PLyObToDatumFunc func
Definition: plpy_typeio.h:57
int32 typmod
Definition: plpy_typeio.h:61
#define TRIGGER_FIRED_FOR_STATEMENT(event)
Definition: trigger.h:131
HeapTuple tg_trigtuple
Definition: trigger.h:35
signed int int32
Definition: c.h:246
static PyObject * PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:428
char * PLyUnicode_AsString(PyObject *unicode)
Definition: plpy_util.c:94
HeapTupleHeader t_data
Definition: htup.h:67
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:455
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
char ** argnames
PLyTypeInfo result
void pfree(void *pointer)
Definition: mcxt.c:949
#define TRIGGER_FIRED_BY_TRUNCATE(event)
Definition: trigger.h:125
#define linitial(l)
Definition: pg_list.h:111
#define VOIDOID
Definition: pg_type.h:690
PLyTypeInput in
Definition: plpy_typeio.h:86
#define ObjectIdGetDatum(X)
Definition: postgres.h:513
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:572
static void plpython_return_error_callback(void *arg)
Definition: plpy_exec.c:714
char * tgname
Definition: reltrigger.h:27
PLyObToDatum d
Definition: plpy_typeio.h:77
#define FATAL
Definition: elog.h:52
PyObject * PLyDict_FromTuple(PLyTypeInfo *info, HeapTuple tuple, TupleDesc desc)
Definition: plpy_typeio.c:280
PLyDatumToObFunc func
Definition: plpy_typeio.h:21
MemoryContext mcxt
uint32 t_len
Definition: htup.h:64
static PLySavedArgs * PLy_function_save_args(PLyProcedure *proc)
Definition: plpy_exec.c:532
PLyTypeOutput out
Definition: plpy_typeio.h:87
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4276
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
int errdetail(const char *fmt,...)
Definition: elog.c:873
List * explicit_subtransactions
#define SRF_RETURN_NEXT_NULL(_funcctx)
Definition: funcapi.h:300
fmNodePtr resultinfo
Definition: fmgr.h:81
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
#define RECORDOID
Definition: pg_type.h:680
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:86
struct PLySRFState PLySRFState
#define ereport(elevel, rest)
Definition: elog.h:122
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:2752
Oid rd_id
Definition: rel.h:116
PLyProcedure * curr_proc
Definition: plpy_main.h:20
#define WARNING
Definition: elog.h:40
static void PLy_function_drop_args(PLySavedArgs *savedargs)
Definition: plpy_exec.c:604
char ** tgargs
Definition: reltrigger.h:40
PyObject * namedargs[FLEXIBLE_ARRAY_MEMBER]
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:119
PyObject * code
static void PLy_abort_open_subtransactions(int save_subxact_level)
Definition: plpy_exec.c:1098
void * palloc0(Size size)
Definition: mcxt.c:877
uintptr_t Datum
Definition: postgres.h:372
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
Trigger * tg_trigger
Definition: trigger.h:37
TupleDesc rd_att
Definition: rel.h:115
HeapTuple tg_newtuple
Definition: trigger.h:36
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:741
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1618
char * SPI_getrelname(Relation rel)
Definition: spi.c:918
int allowedModes
Definition: execnodes.h:268
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:85
SetFunctionReturnMode returnMode
Definition: execnodes.h:270
#define PG_CATCH()
Definition: elog.h:293
void PLy_output_record_funcs(PLyTypeInfo *arg, TupleDesc desc)
Definition: plpy_typeio.c:251
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define Assert(condition)
Definition: c.h:681
TriggerEvent tg_event
Definition: trigger.h:33
int is_rowtype
Definition: plpy_typeio.h:93
char * SPI_getnspname(Relation rel)
Definition: spi.c:924
static void plpython_trigger_error_callback(void *arg)
Definition: plpy_exec.c:1043
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
static int list_length(const List *l)
Definition: pg_list.h:89
PLyObToTuple r
Definition: plpy_typeio.h:78
#define SPI_OK_FINISH
Definition: spi.h:51
#define PG_RE_THROW()
Definition: elog.h:314
PLySavedArgs * argstack
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:134
#define TRIGGER_FIRED_INSTEAD(event)
Definition: trigger.h:140
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:116
void * user_fctx
Definition: funcapi.h:90
void(* callback)(void *arg)
Definition: elog.h:239
static PyObject * PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
Definition: plpy_exec.c:723
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum PLyObject_ToCompositeDatum(PLyTypeInfo *info, TupleDesc desc, PyObject *plrv, bool inarray)
Definition: plpy_typeio.c:346
int i
int16 tgnargs
Definition: reltrigger.h:37
#define errcontext
Definition: elog.h:164
void * arg
PyObject * iter
Definition: plpy_exec.c:32
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:265
PLyObToDatum * atts
Definition: plpy_typeio.h:71
PLySavedArgs * savedargs
Definition: plpy_exec.c:33
MemoryContextCallback callback
Definition: plpy_exec.c:34
#define elog
Definition: elog.h:219
Datum PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:58
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:996
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:115
#define PG_TRY()
Definition: elog.h:284
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:128
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:794
PyObject * globals
PLyDatumToOb d
Definition: plpy_typeio.h:41
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:122
static PyObject * PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
Definition: plpy_exec.c:1053
#define PG_END_TRY()
Definition: elog.h:300
#define offsetof(type, field)
Definition: c.h:549
List * list_delete_first(List *list)
Definition: list.c:666
static void plpython_srf_cleanup_callback(void *arg)
Definition: plpy_exec.c:700
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
Relation tg_relation
Definition: trigger.h:34
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:439
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287