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  {
348  plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
349  plrv = PLy_procedure_call(proc, "TD", plargs);
350 
351  Assert(plrv != NULL);
352 
353  /*
354  * Disconnect from SPI manager
355  */
356  if (SPI_finish() != SPI_OK_FINISH)
357  elog(ERROR, "SPI_finish failed");
358 
359  /*
360  * return of None means we're happy with the tuple
361  */
362  if (plrv != Py_None)
363  {
364  char *srv;
365 
366  if (PyString_Check(plrv))
367  srv = PyString_AsString(plrv);
368  else if (PyUnicode_Check(plrv))
369  srv = PLyUnicode_AsString(plrv);
370  else
371  {
372  ereport(ERROR,
373  (errcode(ERRCODE_DATA_EXCEPTION),
374  errmsg("unexpected return value from trigger procedure"),
375  errdetail("Expected None or a string.")));
376  srv = NULL; /* keep compiler quiet */
377  }
378 
379  if (pg_strcasecmp(srv, "SKIP") == 0)
380  rv = NULL;
381  else if (pg_strcasecmp(srv, "MODIFY") == 0)
382  {
383  TriggerData *tdata = (TriggerData *) fcinfo->context;
384 
385  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
387  rv = PLy_modify_tuple(proc, plargs, tdata, rv);
388  else
390  (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
391  }
392  else if (pg_strcasecmp(srv, "OK") != 0)
393  {
394  /*
395  * accept "OK" as an alternative to None; otherwise, raise an
396  * error
397  */
398  ereport(ERROR,
399  (errcode(ERRCODE_DATA_EXCEPTION),
400  errmsg("unexpected return value from trigger procedure"),
401  errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
402  }
403  }
404  }
405  PG_CATCH();
406  {
407  Py_XDECREF(plargs);
408  Py_XDECREF(plrv);
409 
410  PG_RE_THROW();
411  }
412  PG_END_TRY();
413 
414  Py_DECREF(plargs);
415  Py_DECREF(plrv);
416 
417  return rv;
418 }
419 
420 /* helper functions for Python code execution */
421 
422 static PyObject *
424 {
425  PyObject *volatile arg = NULL;
426  PyObject *volatile args = NULL;
427  int i;
428 
429  PG_TRY();
430  {
431  args = PyList_New(proc->nargs);
432  for (i = 0; i < proc->nargs; i++)
433  {
434  if (proc->args[i].is_rowtype > 0)
435  {
436  if (fcinfo->argnull[i])
437  arg = NULL;
438  else
439  {
440  HeapTupleHeader td;
441  Oid tupType;
442  int32 tupTypmod;
443  TupleDesc tupdesc;
444  HeapTupleData tmptup;
445 
446  td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
447  /* Extract rowtype info and find a tupdesc */
448  tupType = HeapTupleHeaderGetTypeId(td);
449  tupTypmod = HeapTupleHeaderGetTypMod(td);
450  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
451 
452  /* Set up I/O funcs if not done yet */
453  if (proc->args[i].is_rowtype != 1)
454  PLy_input_tuple_funcs(&(proc->args[i]), tupdesc);
455 
456  /* Build a temporary HeapTuple control structure */
458  tmptup.t_data = td;
459 
460  arg = PLyDict_FromTuple(&(proc->args[i]), &tmptup, tupdesc);
461  ReleaseTupleDesc(tupdesc);
462  }
463  }
464  else
465  {
466  if (fcinfo->argnull[i])
467  arg = NULL;
468  else
469  {
470  arg = (proc->args[i].in.d.func) (&(proc->args[i].in.d),
471  fcinfo->arg[i]);
472  }
473  }
474 
475  if (arg == NULL)
476  {
477  Py_INCREF(Py_None);
478  arg = Py_None;
479  }
480 
481  if (PyList_SetItem(args, i, arg) == -1)
482  PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments");
483 
484  if (proc->argnames && proc->argnames[i] &&
485  PyDict_SetItemString(proc->globals, proc->argnames[i], arg) == -1)
486  PLy_elog(ERROR, "PyDict_SetItemString() failed, while setting up arguments");
487  arg = NULL;
488  }
489 
490  /* Set up output conversion for functions returning RECORD */
491  if (proc->result.out.d.typoid == RECORDOID)
492  {
493  TupleDesc desc;
494 
495  if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
496  ereport(ERROR,
497  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
498  errmsg("function returning record called in context "
499  "that cannot accept type record")));
500 
501  /* cache the output conversion functions */
502  PLy_output_record_funcs(&(proc->result), desc);
503  }
504  }
505  PG_CATCH();
506  {
507  Py_XDECREF(arg);
508  Py_XDECREF(args);
509 
510  PG_RE_THROW();
511  }
512  PG_END_TRY();
513 
514  return args;
515 }
516 
517 /*
518  * Construct a PLySavedArgs struct representing the current values of the
519  * procedure's arguments in its globals dict. This can be used to restore
520  * those values when exiting a recursive call level or returning control to a
521  * set-returning function.
522  *
523  * This would not be necessary except for an ancient decision to make args
524  * available via the proc's globals :-( ... but we're stuck with that now.
525  */
526 static PLySavedArgs *
528 {
529  PLySavedArgs *result;
530 
531  /* saved args are always allocated in procedure's context */
532  result = (PLySavedArgs *)
534  offsetof(PLySavedArgs, namedargs) +
535  proc->nargs * sizeof(PyObject *));
536  result->nargs = proc->nargs;
537 
538  /* Fetch the "args" list */
539  result->args = PyDict_GetItemString(proc->globals, "args");
540  Py_XINCREF(result->args);
541 
542  /* Fetch all the named arguments */
543  if (proc->argnames)
544  {
545  int i;
546 
547  for (i = 0; i < result->nargs; i++)
548  {
549  if (proc->argnames[i])
550  {
551  result->namedargs[i] = PyDict_GetItemString(proc->globals,
552  proc->argnames[i]);
553  Py_XINCREF(result->namedargs[i]);
554  }
555  }
556  }
557 
558  return result;
559 }
560 
561 /*
562  * Restore procedure's arguments from a PLySavedArgs struct,
563  * then free the struct.
564  */
565 static void
567 {
568  /* Restore named arguments into their slots in the globals dict */
569  if (proc->argnames)
570  {
571  int i;
572 
573  for (i = 0; i < savedargs->nargs; i++)
574  {
575  if (proc->argnames[i] && savedargs->namedargs[i])
576  {
577  PyDict_SetItemString(proc->globals, proc->argnames[i],
578  savedargs->namedargs[i]);
579  Py_DECREF(savedargs->namedargs[i]);
580  }
581  }
582  }
583 
584  /* Restore the "args" object, too */
585  if (savedargs->args)
586  {
587  PyDict_SetItemString(proc->globals, "args", savedargs->args);
588  Py_DECREF(savedargs->args);
589  }
590 
591  /* And free the PLySavedArgs struct */
592  pfree(savedargs);
593 }
594 
595 /*
596  * Free a PLySavedArgs struct without restoring the values.
597  */
598 static void
600 {
601  int i;
602 
603  /* Drop references for named args */
604  for (i = 0; i < savedargs->nargs; i++)
605  {
606  Py_XDECREF(savedargs->namedargs[i]);
607  }
608 
609  /* Drop ref to the "args" object, too */
610  Py_XDECREF(savedargs->args);
611 
612  /* And free the PLySavedArgs struct */
613  pfree(savedargs);
614 }
615 
616 /*
617  * Save away any existing arguments for the given procedure, so that we can
618  * install new values for a recursive call. This should be invoked before
619  * doing PLy_function_build_args().
620  *
621  * NB: caller must ensure that PLy_global_args_pop gets invoked once, and
622  * only once, per successful completion of PLy_global_args_push. Otherwise
623  * we'll end up out-of-sync between the actual call stack and the contents
624  * of proc->argstack.
625  */
626 static void
628 {
629  /* We only need to push if we are already inside some active call */
630  if (proc->calldepth > 0)
631  {
632  PLySavedArgs *node;
633 
634  /* Build a struct containing current argument values */
635  node = PLy_function_save_args(proc);
636 
637  /*
638  * Push the saved argument values into the procedure's stack. Once we
639  * modify either proc->argstack or proc->calldepth, we had better
640  * return without the possibility of error.
641  */
642  node->next = proc->argstack;
643  proc->argstack = node;
644  }
645  proc->calldepth++;
646 }
647 
648 /*
649  * Pop old arguments when exiting a recursive call.
650  *
651  * Note: the idea here is to adjust the proc's callstack state before doing
652  * anything that could possibly fail. In event of any error, we want the
653  * callstack to look like we've done the pop. Leaking a bit of memory is
654  * tolerable.
655  */
656 static void
658 {
659  Assert(proc->calldepth > 0);
660  /* We only need to pop if we were already inside some active call */
661  if (proc->calldepth > 1)
662  {
663  PLySavedArgs *ptr = proc->argstack;
664 
665  /* Pop the callstack */
666  Assert(ptr != NULL);
667  proc->argstack = ptr->next;
668  proc->calldepth--;
669 
670  /* Restore argument values, then free ptr */
671  PLy_function_restore_args(proc, ptr);
672  }
673  else
674  {
675  /* Exiting call depth 1 */
676  Assert(proc->argstack == NULL);
677  proc->calldepth--;
678 
679  /*
680  * We used to delete the named arguments (but not "args") from the
681  * proc's globals dict when exiting the outermost call level for a
682  * function. This seems rather pointless though: nothing can see the
683  * dict until the function is called again, at which time we'll
684  * overwrite those dict entries. So don't bother with that.
685  */
686  }
687 }
688 
689 /*
690  * Memory context deletion callback for cleaning up a PLySRFState.
691  * We need this in case execution of the SRF is terminated early,
692  * due to error or the caller simply not running it to completion.
693  */
694 static void
696 {
697  PLySRFState *srfstate = (PLySRFState *) arg;
698 
699  /* Release refcount on the iter, if we still have one */
700  Py_XDECREF(srfstate->iter);
701  srfstate->iter = NULL;
702  /* And drop any saved args; we won't need them */
703  if (srfstate->savedargs)
705  srfstate->savedargs = NULL;
706 }
707 
708 static void
710 {
712 
713  if (exec_ctx->curr_proc)
714  errcontext("while creating return value");
715 }
716 
717 static PyObject *
719 {
720  TriggerData *tdata = (TriggerData *) fcinfo->context;
721  PyObject *pltname,
722  *pltevent,
723  *pltwhen,
724  *pltlevel,
725  *pltrelid,
726  *plttablename,
727  *plttableschema;
728  PyObject *pltargs,
729  *pytnew,
730  *pytold;
731  PyObject *volatile pltdata = NULL;
732  char *stroid;
733 
734  PG_TRY();
735  {
736  pltdata = PyDict_New();
737  if (!pltdata)
738  PLy_elog(ERROR, "could not create new dictionary while building trigger arguments");
739 
740  pltname = PyString_FromString(tdata->tg_trigger->tgname);
741  PyDict_SetItemString(pltdata, "name", pltname);
742  Py_DECREF(pltname);
743 
746  pltrelid = PyString_FromString(stroid);
747  PyDict_SetItemString(pltdata, "relid", pltrelid);
748  Py_DECREF(pltrelid);
749  pfree(stroid);
750 
751  stroid = SPI_getrelname(tdata->tg_relation);
752  plttablename = PyString_FromString(stroid);
753  PyDict_SetItemString(pltdata, "table_name", plttablename);
754  Py_DECREF(plttablename);
755  pfree(stroid);
756 
757  stroid = SPI_getnspname(tdata->tg_relation);
758  plttableschema = PyString_FromString(stroid);
759  PyDict_SetItemString(pltdata, "table_schema", plttableschema);
760  Py_DECREF(plttableschema);
761  pfree(stroid);
762 
763  if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
764  pltwhen = PyString_FromString("BEFORE");
765  else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
766  pltwhen = PyString_FromString("AFTER");
767  else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
768  pltwhen = PyString_FromString("INSTEAD OF");
769  else
770  {
771  elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
772  pltwhen = NULL; /* keep compiler quiet */
773  }
774  PyDict_SetItemString(pltdata, "when", pltwhen);
775  Py_DECREF(pltwhen);
776 
777  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
778  {
779  pltlevel = PyString_FromString("ROW");
780  PyDict_SetItemString(pltdata, "level", pltlevel);
781  Py_DECREF(pltlevel);
782 
783  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
784  {
785  pltevent = PyString_FromString("INSERT");
786 
787  PyDict_SetItemString(pltdata, "old", Py_None);
788  pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
789  tdata->tg_relation->rd_att);
790  PyDict_SetItemString(pltdata, "new", pytnew);
791  Py_DECREF(pytnew);
792  *rv = tdata->tg_trigtuple;
793  }
794  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
795  {
796  pltevent = PyString_FromString("DELETE");
797 
798  PyDict_SetItemString(pltdata, "new", Py_None);
799  pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
800  tdata->tg_relation->rd_att);
801  PyDict_SetItemString(pltdata, "old", pytold);
802  Py_DECREF(pytold);
803  *rv = tdata->tg_trigtuple;
804  }
805  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
806  {
807  pltevent = PyString_FromString("UPDATE");
808 
809  pytnew = PLyDict_FromTuple(&(proc->result), tdata->tg_newtuple,
810  tdata->tg_relation->rd_att);
811  PyDict_SetItemString(pltdata, "new", pytnew);
812  Py_DECREF(pytnew);
813  pytold = PLyDict_FromTuple(&(proc->result), tdata->tg_trigtuple,
814  tdata->tg_relation->rd_att);
815  PyDict_SetItemString(pltdata, "old", pytold);
816  Py_DECREF(pytold);
817  *rv = tdata->tg_newtuple;
818  }
819  else
820  {
821  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
822  pltevent = NULL; /* keep compiler quiet */
823  }
824 
825  PyDict_SetItemString(pltdata, "event", pltevent);
826  Py_DECREF(pltevent);
827  }
828  else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
829  {
830  pltlevel = PyString_FromString("STATEMENT");
831  PyDict_SetItemString(pltdata, "level", pltlevel);
832  Py_DECREF(pltlevel);
833 
834  PyDict_SetItemString(pltdata, "old", Py_None);
835  PyDict_SetItemString(pltdata, "new", Py_None);
836  *rv = NULL;
837 
838  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
839  pltevent = PyString_FromString("INSERT");
840  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
841  pltevent = PyString_FromString("DELETE");
842  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
843  pltevent = PyString_FromString("UPDATE");
844  else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
845  pltevent = PyString_FromString("TRUNCATE");
846  else
847  {
848  elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
849  pltevent = NULL; /* keep compiler quiet */
850  }
851 
852  PyDict_SetItemString(pltdata, "event", pltevent);
853  Py_DECREF(pltevent);
854  }
855  else
856  elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
857 
858  if (tdata->tg_trigger->tgnargs)
859  {
860  /*
861  * all strings...
862  */
863  int i;
864  PyObject *pltarg;
865 
866  pltargs = PyList_New(tdata->tg_trigger->tgnargs);
867  for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
868  {
869  pltarg = PyString_FromString(tdata->tg_trigger->tgargs[i]);
870 
871  /*
872  * stolen, don't Py_DECREF
873  */
874  PyList_SetItem(pltargs, i, pltarg);
875  }
876  }
877  else
878  {
879  Py_INCREF(Py_None);
880  pltargs = Py_None;
881  }
882  PyDict_SetItemString(pltdata, "args", pltargs);
883  Py_DECREF(pltargs);
884  }
885  PG_CATCH();
886  {
887  Py_XDECREF(pltdata);
888  PG_RE_THROW();
889  }
890  PG_END_TRY();
891 
892  return pltdata;
893 }
894 
895 static HeapTuple
896 PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata,
897  HeapTuple otup)
898 {
899  HeapTuple rtup;
900  PyObject *volatile plntup;
901  PyObject *volatile plkeys;
902  PyObject *volatile plval;
903  Datum *volatile modvalues;
904  bool *volatile modnulls;
905  bool *volatile modrepls;
906  ErrorContextCallback plerrcontext;
907 
909  plerrcontext.previous = error_context_stack;
910  error_context_stack = &plerrcontext;
911 
912  plntup = plkeys = plval = NULL;
913  modvalues = NULL;
914  modnulls = NULL;
915  modrepls = NULL;
916 
917  PG_TRY();
918  {
919  TupleDesc tupdesc;
920  int nkeys,
921  i;
922 
923  if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
924  ereport(ERROR,
925  (errcode(ERRCODE_UNDEFINED_OBJECT),
926  errmsg("TD[\"new\"] deleted, cannot modify row")));
927  Py_INCREF(plntup);
928  if (!PyDict_Check(plntup))
929  ereport(ERROR,
930  (errcode(ERRCODE_DATATYPE_MISMATCH),
931  errmsg("TD[\"new\"] is not a dictionary")));
932 
933  plkeys = PyDict_Keys(plntup);
934  nkeys = PyList_Size(plkeys);
935 
936  tupdesc = tdata->tg_relation->rd_att;
937 
938  modvalues = (Datum *) palloc0(tupdesc->natts * sizeof(Datum));
939  modnulls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
940  modrepls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
941 
942  for (i = 0; i < nkeys; i++)
943  {
944  PyObject *platt;
945  char *plattstr;
946  int attn;
947  PLyObToDatum *att;
948 
949  platt = PyList_GetItem(plkeys, i);
950  if (PyString_Check(platt))
951  plattstr = PyString_AsString(platt);
952  else if (PyUnicode_Check(platt))
953  plattstr = PLyUnicode_AsString(platt);
954  else
955  {
956  ereport(ERROR,
957  (errcode(ERRCODE_DATATYPE_MISMATCH),
958  errmsg("TD[\"new\"] dictionary key at ordinal position %d is not a string", i)));
959  plattstr = NULL; /* keep compiler quiet */
960  }
961  attn = SPI_fnumber(tupdesc, plattstr);
962  if (attn == SPI_ERROR_NOATTRIBUTE)
963  ereport(ERROR,
964  (errcode(ERRCODE_UNDEFINED_COLUMN),
965  errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
966  plattstr)));
967  if (attn <= 0)
968  ereport(ERROR,
969  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
970  errmsg("cannot set system attribute \"%s\"",
971  plattstr)));
972  att = &proc->result.out.r.atts[attn - 1];
973 
974  plval = PyDict_GetItem(plntup, platt);
975  if (plval == NULL)
976  elog(FATAL, "Python interpreter is probably corrupted");
977 
978  Py_INCREF(plval);
979 
980  if (plval != Py_None)
981  {
982  modvalues[attn - 1] =
983  (att->func) (att,
984  tupdesc->attrs[attn - 1]->atttypmod,
985  plval,
986  false);
987  modnulls[attn - 1] = false;
988  }
989  else
990  {
991  modvalues[attn - 1] =
993  NULL,
994  att->typioparam,
995  tupdesc->attrs[attn - 1]->atttypmod);
996  modnulls[attn - 1] = true;
997  }
998  modrepls[attn - 1] = true;
999 
1000  Py_DECREF(plval);
1001  plval = NULL;
1002  }
1003 
1004  rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
1005  }
1006  PG_CATCH();
1007  {
1008  Py_XDECREF(plntup);
1009  Py_XDECREF(plkeys);
1010  Py_XDECREF(plval);
1011 
1012  if (modvalues)
1013  pfree(modvalues);
1014  if (modnulls)
1015  pfree(modnulls);
1016  if (modrepls)
1017  pfree(modrepls);
1018 
1019  PG_RE_THROW();
1020  }
1021  PG_END_TRY();
1022 
1023  Py_DECREF(plntup);
1024  Py_DECREF(plkeys);
1025 
1026  pfree(modvalues);
1027  pfree(modnulls);
1028  pfree(modrepls);
1029 
1030  error_context_stack = plerrcontext.previous;
1031 
1032  return rtup;
1033 }
1034 
1035 static void
1037 {
1039 
1040  if (exec_ctx->curr_proc)
1041  errcontext("while modifying trigger row");
1042 }
1043 
1044 /* execute Python code, propagate Python errors to the backend */
1045 static PyObject *
1046 PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
1047 {
1048  PyObject *rv;
1049  int volatile save_subxact_level = list_length(explicit_subtransactions);
1050 
1051  PyDict_SetItemString(proc->globals, kargs, vargs);
1052 
1053  PG_TRY();
1054  {
1055 #if PY_VERSION_HEX >= 0x03020000
1056  rv = PyEval_EvalCode(proc->code,
1057  proc->globals, proc->globals);
1058 #else
1059  rv = PyEval_EvalCode((PyCodeObject *) proc->code,
1060  proc->globals, proc->globals);
1061 #endif
1062 
1063  /*
1064  * Since plpy will only let you close subtransactions that you
1065  * started, you cannot *unnest* subtransactions, only *nest* them
1066  * without closing.
1067  */
1068  Assert(list_length(explicit_subtransactions) >= save_subxact_level);
1069  }
1070  PG_CATCH();
1071  {
1072  PLy_abort_open_subtransactions(save_subxact_level);
1073  PG_RE_THROW();
1074  }
1075  PG_END_TRY();
1076 
1077  PLy_abort_open_subtransactions(save_subxact_level);
1078 
1079  /* If the Python code returned an error, propagate it */
1080  if (rv == NULL)
1081  PLy_elog(ERROR, NULL);
1082 
1083  return rv;
1084 }
1085 
1086 /*
1087  * Abort lingering subtransactions that have been explicitly started
1088  * by plpy.subtransaction().start() and not properly closed.
1089  */
1090 static void
1091 PLy_abort_open_subtransactions(int save_subxact_level)
1092 {
1093  Assert(save_subxact_level >= 0);
1094 
1095  while (list_length(explicit_subtransactions) > save_subxact_level)
1096  {
1097  PLySubtransactionData *subtransactiondata;
1098 
1100 
1101  ereport(WARNING,
1102  (errmsg("forcibly aborting a subtransaction that has not been exited")));
1103 
1105 
1106  subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
1108 
1109  MemoryContextSwitchTo(subtransactiondata->oldcontext);
1110  CurrentResourceOwner = subtransactiondata->oldowner;
1111  pfree(subtransactiondata);
1112  }
1113 }
HeapTuple PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:325
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:760
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:560
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:627
PLyTypeInfo args[FUNC_MAX_ARGS]
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1245
static void PLy_global_args_pop(PLyProcedure *proc)
Definition: plpy_exec.c:657
fmNodePtr context
Definition: fmgr.h:72
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
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:147
PyObject * args
Form_pg_attribute * attrs
Definition: tupdesc.h:74
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:566
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
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:896
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:91
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:127
struct ErrorContextCallback * previous
Definition: elog.h:238
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:254
#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:85
HeapTuple tg_trigtuple
Definition: trigger.h:35
signed int int32
Definition: c.h:253
static PyObject * PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:423
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:992
#define TRIGGER_FIRED_BY_TRUNCATE(event)
Definition: trigger.h:79
#define linitial(l)
Definition: pg_list.h:110
#define VOIDOID
Definition: pg_type.h:678
PLyTypeInput in
Definition: plpy_typeio.h:86
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
#define DatumGetCString(X)
Definition: postgres.h:574
static void plpython_return_error_callback(void *arg)
Definition: plpy_exec.c:709
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:527
PLyTypeOutput out
Definition: plpy_typeio.h:87
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:43
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:73
#define RECORDOID
Definition: pg_type.h:668
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:78
struct PLySRFState PLySRFState
#define ereport(elevel, rest)
Definition: elog.h:122
Oid rd_id
Definition: rel.h:115
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:599
char ** tgargs
Definition: reltrigger.h:40
PyObject * namedargs[FLEXIBLE_ARRAY_MEMBER]
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:73
PyObject * code
static void PLy_abort_open_subtransactions(int save_subxact_level)
Definition: plpy_exec.c:1091
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
Trigger * tg_trigger
Definition: trigger.h:37
TupleDesc rd_att
Definition: rel.h:114
HeapTuple tg_newtuple
Definition: trigger.h:36
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:784
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1882
char * SPI_getrelname(Relation rel)
Definition: spi.c:909
int allowedModes
Definition: execnodes.h:199
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:77
SetFunctionReturnMode returnMode
Definition: execnodes.h:201
#define PG_CATCH()
Definition: elog.h:293
void PLy_output_record_funcs(PLyTypeInfo *arg, TupleDesc desc)
Definition: plpy_typeio.c:251
#define NULL
Definition: c.h:226
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define Assert(condition)
Definition: c.h:671
TriggerEvent tg_event
Definition: trigger.h:33
int is_rowtype
Definition: plpy_typeio.h:93
char * SPI_getnspname(Relation rel)
Definition: spi.c:915
static void plpython_trigger_error_callback(void *arg)
Definition: plpy_exec.c:1036
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:48
#define PG_RE_THROW()
Definition: elog.h:314
PLySavedArgs * argstack
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:88
#define TRIGGER_FIRED_INSTEAD(event)
Definition: trigger.h:94
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:70
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:718
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum PLyObject_ToCompositeDatum(PLyTypeInfo *info, TupleDesc desc, PyObject *plrv, bool inarray)
Definition: plpy_typeio.c:345
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 ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:107
#define PG_TRY()
Definition: elog.h:284
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:82
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
PyObject * globals
PLyDatumToOb d
Definition: plpy_typeio.h:41
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:76
static PyObject * PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
Definition: plpy_exec.c:1046
#define PG_END_TRY()
Definition: elog.h:300
#define offsetof(type, field)
Definition: c.h:551
List * list_delete_first(List *list)
Definition: list.c:666
static void plpython_srf_cleanup_callback(void *arg)
Definition: plpy_exec.c:695
#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