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