Don't corrupt plpython's "TD" dictionary in a recursive trigger call.
[pgsql.git] / src / pl / plpython / plpy_exec.c
blob6f7b5e121d69a8e111e3705de7a1d944328a798f
1 /*
2 * executing Python code
4 * src/pl/plpython/plpy_exec.c
5 */
7 #include "postgres.h"
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 "plpy_elog.h"
16 #include "plpy_exec.h"
17 #include "plpy_main.h"
18 #include "plpy_procedure.h"
19 #include "plpy_subxactobject.h"
20 #include "plpython.h"
21 #include "utils/builtins.h"
22 #include "utils/lsyscache.h"
23 #include "utils/rel.h"
24 #include "utils/typcache.h"
26 /* saved state for a set-returning function */
27 typedef struct PLySRFState
29 PyObject *iter; /* Python iterator producing results */
30 PLySavedArgs *savedargs; /* function argument values */
31 MemoryContextCallback callback; /* for releasing refcounts when done */
32 } PLySRFState;
34 static PyObject *PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc);
35 static PLySavedArgs *PLy_function_save_args(PLyProcedure *proc);
36 static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs);
37 static void PLy_function_drop_args(PLySavedArgs *savedargs);
38 static void PLy_global_args_push(PLyProcedure *proc);
39 static void PLy_global_args_pop(PLyProcedure *proc);
40 static void plpython_srf_cleanup_callback(void *arg);
41 static void plpython_return_error_callback(void *arg);
43 static PyObject *PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc,
44 HeapTuple *rv);
45 static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd,
46 TriggerData *tdata, HeapTuple otup);
47 static void plpython_trigger_error_callback(void *arg);
49 static PyObject *PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs);
50 static void PLy_abort_open_subtransactions(int save_subxact_level);
53 /* function subhandler */
54 Datum
55 PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
57 bool is_setof = proc->is_setof;
58 Datum rv;
59 PyObject *volatile plargs = NULL;
60 PyObject *volatile plrv = NULL;
61 FuncCallContext *volatile funcctx = NULL;
62 PLySRFState *volatile srfstate = NULL;
63 ErrorContextCallback plerrcontext;
66 * If the function is called recursively, we must push outer-level
67 * arguments into the stack. This must be immediately before the PG_TRY
68 * to ensure that the corresponding pop happens.
70 PLy_global_args_push(proc);
72 PG_TRY();
74 if (is_setof)
76 /* First Call setup */
77 if (SRF_IS_FIRSTCALL())
79 funcctx = SRF_FIRSTCALL_INIT();
80 srfstate = (PLySRFState *)
81 MemoryContextAllocZero(funcctx->multi_call_memory_ctx,
82 sizeof(PLySRFState));
83 /* Immediately register cleanup callback */
84 srfstate->callback.func = plpython_srf_cleanup_callback;
85 srfstate->callback.arg = (void *) srfstate;
86 MemoryContextRegisterResetCallback(funcctx->multi_call_memory_ctx,
87 &srfstate->callback);
88 funcctx->user_fctx = (void *) srfstate;
90 /* Every call setup */
91 funcctx = SRF_PERCALL_SETUP();
92 Assert(funcctx != NULL);
93 srfstate = (PLySRFState *) funcctx->user_fctx;
94 Assert(srfstate != NULL);
97 if (srfstate == NULL || srfstate->iter == NULL)
100 * Non-SETOF function or first time for SETOF function: build
101 * args, then actually execute the function.
103 plargs = PLy_function_build_args(fcinfo, proc);
104 plrv = PLy_procedure_call(proc, "args", plargs);
105 Assert(plrv != NULL);
107 else
110 * Second or later call for a SETOF function: restore arguments in
111 * globals dict to what they were when we left off. We must do
112 * this in case multiple evaluations of the same SETOF function
113 * are interleaved. It's a bit annoying, since the iterator may
114 * not look at the arguments at all, but we have no way to know
115 * that. Fortunately this isn't terribly expensive.
117 if (srfstate->savedargs)
118 PLy_function_restore_args(proc, srfstate->savedargs);
119 srfstate->savedargs = NULL; /* deleted by restore_args */
123 * If it returns a set, call the iterator to get the next return item.
124 * We stay in the SPI context while doing this, because PyIter_Next()
125 * calls back into Python code which might contain SPI calls.
127 if (is_setof)
129 if (srfstate->iter == NULL)
131 /* first time -- do checks and setup */
132 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
134 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
135 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
137 ereport(ERROR,
138 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
139 errmsg("unsupported set function return mode"),
140 errdetail("PL/Python set-returning functions only support returning one value per call.")));
142 rsi->returnMode = SFRM_ValuePerCall;
144 /* Make iterator out of returned object */
145 srfstate->iter = PyObject_GetIter(plrv);
147 Py_DECREF(plrv);
148 plrv = NULL;
150 if (srfstate->iter == NULL)
151 ereport(ERROR,
152 (errcode(ERRCODE_DATATYPE_MISMATCH),
153 errmsg("returned object cannot be iterated"),
154 errdetail("PL/Python set-returning functions must return an iterable object.")));
157 /* Fetch next from iterator */
158 plrv = PyIter_Next(srfstate->iter);
159 if (plrv == NULL)
161 /* Iterator is exhausted or error happened */
162 bool has_error = (PyErr_Occurred() != NULL);
164 Py_DECREF(srfstate->iter);
165 srfstate->iter = NULL;
167 if (has_error)
168 PLy_elog(ERROR, "error fetching next item from iterator");
170 /* Pass a null through the data-returning steps below */
171 Py_INCREF(Py_None);
172 plrv = Py_None;
174 else
177 * This won't be last call, so save argument values. We do
178 * this again each time in case the iterator is changing those
179 * values.
181 srfstate->savedargs = PLy_function_save_args(proc);
186 * Disconnect from SPI manager and then create the return values datum
187 * (if the input function does a palloc for it this must not be
188 * allocated in the SPI memory context because SPI_finish would free
189 * it).
191 if (SPI_finish() != SPI_OK_FINISH)
192 elog(ERROR, "SPI_finish failed");
194 plerrcontext.callback = plpython_return_error_callback;
195 plerrcontext.previous = error_context_stack;
196 error_context_stack = &plerrcontext;
199 * For a procedure or function declared to return void, the Python
200 * return value must be None. For void-returning functions, we also
201 * treat a None return value as a special "void datum" rather than
202 * NULL (as is the case for non-void-returning functions).
204 if (proc->result.typoid == VOIDOID)
206 if (plrv != Py_None)
208 if (proc->is_procedure)
209 ereport(ERROR,
210 (errcode(ERRCODE_DATATYPE_MISMATCH),
211 errmsg("PL/Python procedure did not return None")));
212 else
213 ereport(ERROR,
214 (errcode(ERRCODE_DATATYPE_MISMATCH),
215 errmsg("PL/Python function with return type \"void\" did not return None")));
218 fcinfo->isnull = false;
219 rv = (Datum) 0;
221 else if (plrv == Py_None &&
222 srfstate && srfstate->iter == NULL)
225 * In a SETOF function, the iteration-ending null isn't a real
226 * value; don't pass it through the input function, which might
227 * complain.
229 fcinfo->isnull = true;
230 rv = (Datum) 0;
232 else
234 /* Normal conversion of result */
235 rv = PLy_output_convert(&proc->result, plrv,
236 &fcinfo->isnull);
239 PG_CATCH();
241 /* Pop old arguments from the stack if they were pushed above */
242 PLy_global_args_pop(proc);
244 Py_XDECREF(plargs);
245 Py_XDECREF(plrv);
248 * If there was an error within a SRF, the iterator might not have
249 * been exhausted yet. Clear it so the next invocation of the
250 * function will start the iteration again. (This code is probably
251 * unnecessary now; plpython_srf_cleanup_callback should take care of
252 * cleanup. But it doesn't hurt anything to do it here.)
254 if (srfstate)
256 Py_XDECREF(srfstate->iter);
257 srfstate->iter = NULL;
258 /* And drop any saved args; we won't need them */
259 if (srfstate->savedargs)
260 PLy_function_drop_args(srfstate->savedargs);
261 srfstate->savedargs = NULL;
264 PG_RE_THROW();
266 PG_END_TRY();
268 error_context_stack = plerrcontext.previous;
270 /* Pop old arguments from the stack if they were pushed above */
271 PLy_global_args_pop(proc);
273 Py_XDECREF(plargs);
274 Py_DECREF(plrv);
276 if (srfstate)
278 /* We're in a SRF, exit appropriately */
279 if (srfstate->iter == NULL)
281 /* Iterator exhausted, so we're done */
282 SRF_RETURN_DONE(funcctx);
284 else if (fcinfo->isnull)
285 SRF_RETURN_NEXT_NULL(funcctx);
286 else
287 SRF_RETURN_NEXT(funcctx, rv);
290 /* Plain function, just return the Datum value (possibly null) */
291 return rv;
294 /* trigger subhandler
296 * the python function is expected to return Py_None if the tuple is
297 * acceptable and unmodified. Otherwise it should return a PyUnicode
298 * object who's value is SKIP, or MODIFY. SKIP means don't perform
299 * this action. MODIFY means the tuple has been modified, so update
300 * tuple and perform action. SKIP and MODIFY assume the trigger fires
301 * BEFORE the event and is ROW level. postgres expects the function
302 * to take no arguments and return an argument of type trigger.
304 HeapTuple
305 PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
307 HeapTuple rv = NULL;
308 PyObject *volatile plargs = NULL;
309 PyObject *volatile plrv = NULL;
310 TriggerData *tdata;
311 TupleDesc rel_descr;
313 Assert(CALLED_AS_TRIGGER(fcinfo));
314 tdata = (TriggerData *) fcinfo->context;
317 * Input/output conversion for trigger tuples. We use the result and
318 * result_in fields to store the tuple conversion info. We do this over
319 * again on each call to cover the possibility that the relation's tupdesc
320 * changed since the trigger was last called. The PLy_xxx_setup_func
321 * calls should only happen once, but PLy_input_setup_tuple and
322 * PLy_output_setup_tuple are responsible for not doing repetitive work.
324 rel_descr = RelationGetDescr(tdata->tg_relation);
325 if (proc->result.typoid != rel_descr->tdtypeid)
326 PLy_output_setup_func(&proc->result, proc->mcxt,
327 rel_descr->tdtypeid,
328 rel_descr->tdtypmod,
329 proc);
330 if (proc->result_in.typoid != rel_descr->tdtypeid)
331 PLy_input_setup_func(&proc->result_in, proc->mcxt,
332 rel_descr->tdtypeid,
333 rel_descr->tdtypmod,
334 proc);
335 PLy_output_setup_tuple(&proc->result, rel_descr, proc);
336 PLy_input_setup_tuple(&proc->result_in, rel_descr, proc);
339 * If the trigger is called recursively, we must push outer-level
340 * arguments into the stack. This must be immediately before the PG_TRY
341 * to ensure that the corresponding pop happens.
343 PLy_global_args_push(proc);
345 PG_TRY();
347 int rc PG_USED_FOR_ASSERTS_ONLY;
349 rc = SPI_register_trigger_data(tdata);
350 Assert(rc >= 0);
352 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
353 plrv = PLy_procedure_call(proc, "TD", plargs);
355 Assert(plrv != NULL);
358 * Disconnect from SPI manager
360 if (SPI_finish() != SPI_OK_FINISH)
361 elog(ERROR, "SPI_finish failed");
364 * return of None means we're happy with the tuple
366 if (plrv != Py_None)
368 char *srv;
370 if (PyUnicode_Check(plrv))
371 srv = PLyUnicode_AsString(plrv);
372 else
374 ereport(ERROR,
375 (errcode(ERRCODE_DATA_EXCEPTION),
376 errmsg("unexpected return value from trigger procedure"),
377 errdetail("Expected None or a string.")));
378 srv = NULL; /* keep compiler quiet */
381 if (pg_strcasecmp(srv, "SKIP") == 0)
382 rv = NULL;
383 else if (pg_strcasecmp(srv, "MODIFY") == 0)
385 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
386 TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
387 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
388 else
389 ereport(WARNING,
390 (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
392 else if (pg_strcasecmp(srv, "OK") != 0)
395 * accept "OK" as an alternative to None; otherwise, raise an
396 * error
398 ereport(ERROR,
399 (errcode(ERRCODE_DATA_EXCEPTION),
400 errmsg("unexpected return value from trigger procedure"),
401 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
405 PG_FINALLY();
407 PLy_global_args_pop(proc);
408 Py_XDECREF(plargs);
409 Py_XDECREF(plrv);
411 PG_END_TRY();
413 return rv;
416 /* helper functions for Python code execution */
418 static PyObject *
419 PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
421 PyObject *volatile arg = NULL;
422 PyObject *args;
423 int i;
426 * Make any Py*_New() calls before the PG_TRY block so that we can quickly
427 * return NULL on failure. We can't return within the PG_TRY block, else
428 * we'd miss unwinding the exception stack.
430 args = PyList_New(proc->nargs);
431 if (!args)
432 return NULL;
434 PG_TRY();
436 for (i = 0; i < proc->nargs; i++)
438 PLyDatumToOb *arginfo = &proc->args[i];
440 if (fcinfo->args[i].isnull)
441 arg = NULL;
442 else
443 arg = PLy_input_convert(arginfo, fcinfo->args[i].value);
445 if (arg == NULL)
447 Py_INCREF(Py_None);
448 arg = Py_None;
451 if (PyList_SetItem(args, i, arg) == -1)
452 PLy_elog(ERROR, "PyList_SetItem() failed, while setting up arguments");
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;
460 /* Set up output conversion for functions returning RECORD */
461 if (proc->result.typoid == RECORDOID)
463 TupleDesc desc;
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")));
471 /* cache the output conversion functions */
472 PLy_output_setup_record(&proc->result, desc, proc);
475 PG_CATCH();
477 Py_XDECREF(arg);
478 Py_XDECREF(args);
480 PG_RE_THROW();
482 PG_END_TRY();
484 return args;
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.
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.
496 static PLySavedArgs *
497 PLy_function_save_args(PLyProcedure *proc)
499 PLySavedArgs *result;
501 /* saved args are always allocated in procedure's context */
502 result = (PLySavedArgs *)
503 MemoryContextAllocZero(proc->mcxt,
504 offsetof(PLySavedArgs, namedargs) +
505 proc->nargs * sizeof(PyObject *));
506 result->nargs = proc->nargs;
508 /* Fetch the "args" list */
509 result->args = PyDict_GetItemString(proc->globals, "args");
510 Py_XINCREF(result->args);
512 /* If it's a trigger, also save "TD" */
513 if (proc->is_trigger)
515 result->td = PyDict_GetItemString(proc->globals, "TD");
516 Py_XINCREF(result->td);
519 /* Fetch all the named arguments */
520 if (proc->argnames)
522 int i;
524 for (i = 0; i < result->nargs; i++)
526 if (proc->argnames[i])
528 result->namedargs[i] = PyDict_GetItemString(proc->globals,
529 proc->argnames[i]);
530 Py_XINCREF(result->namedargs[i]);
535 return result;
539 * Restore procedure's arguments from a PLySavedArgs struct,
540 * then free the struct.
542 static void
543 PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs)
545 /* Restore named arguments into their slots in the globals dict */
546 if (proc->argnames)
548 int i;
550 for (i = 0; i < savedargs->nargs; i++)
552 if (proc->argnames[i] && savedargs->namedargs[i])
554 PyDict_SetItemString(proc->globals, proc->argnames[i],
555 savedargs->namedargs[i]);
556 Py_DECREF(savedargs->namedargs[i]);
561 /* Restore the "args" object, too */
562 if (savedargs->args)
564 PyDict_SetItemString(proc->globals, "args", savedargs->args);
565 Py_DECREF(savedargs->args);
568 /* Restore the "TD" object, too */
569 if (savedargs->td)
571 PyDict_SetItemString(proc->globals, "TD", savedargs->td);
572 Py_DECREF(savedargs->td);
575 /* And free the PLySavedArgs struct */
576 pfree(savedargs);
580 * Free a PLySavedArgs struct without restoring the values.
582 static void
583 PLy_function_drop_args(PLySavedArgs *savedargs)
585 int i;
587 /* Drop references for named args */
588 for (i = 0; i < savedargs->nargs; i++)
590 Py_XDECREF(savedargs->namedargs[i]);
593 /* Drop refs to the "args" and "TD" objects, too */
594 Py_XDECREF(savedargs->args);
595 Py_XDECREF(savedargs->td);
597 /* And free the PLySavedArgs struct */
598 pfree(savedargs);
602 * Save away any existing arguments for the given procedure, so that we can
603 * install new values for a recursive call. This should be invoked before
604 * doing PLy_function_build_args() or PLy_trigger_build_args().
606 * NB: callers must ensure that PLy_global_args_pop gets invoked once, and
607 * only once, per successful completion of PLy_global_args_push. Otherwise
608 * we'll end up out-of-sync between the actual call stack and the contents
609 * of proc->argstack.
611 static void
612 PLy_global_args_push(PLyProcedure *proc)
614 /* We only need to push if we are already inside some active call */
615 if (proc->calldepth > 0)
617 PLySavedArgs *node;
619 /* Build a struct containing current argument values */
620 node = PLy_function_save_args(proc);
623 * Push the saved argument values into the procedure's stack. Once we
624 * modify either proc->argstack or proc->calldepth, we had better
625 * return without the possibility of error.
627 node->next = proc->argstack;
628 proc->argstack = node;
630 proc->calldepth++;
634 * Pop old arguments when exiting a recursive call.
636 * Note: the idea here is to adjust the proc's callstack state before doing
637 * anything that could possibly fail. In event of any error, we want the
638 * callstack to look like we've done the pop. Leaking a bit of memory is
639 * tolerable.
641 static void
642 PLy_global_args_pop(PLyProcedure *proc)
644 Assert(proc->calldepth > 0);
645 /* We only need to pop if we were already inside some active call */
646 if (proc->calldepth > 1)
648 PLySavedArgs *ptr = proc->argstack;
650 /* Pop the callstack */
651 Assert(ptr != NULL);
652 proc->argstack = ptr->next;
653 proc->calldepth--;
655 /* Restore argument values, then free ptr */
656 PLy_function_restore_args(proc, ptr);
658 else
660 /* Exiting call depth 1 */
661 Assert(proc->argstack == NULL);
662 proc->calldepth--;
665 * We used to delete the named arguments (but not "args") from the
666 * proc's globals dict when exiting the outermost call level for a
667 * function. This seems rather pointless though: nothing can see the
668 * dict until the function is called again, at which time we'll
669 * overwrite those dict entries. So don't bother with that.
675 * Memory context deletion callback for cleaning up a PLySRFState.
676 * We need this in case execution of the SRF is terminated early,
677 * due to error or the caller simply not running it to completion.
679 static void
680 plpython_srf_cleanup_callback(void *arg)
682 PLySRFState *srfstate = (PLySRFState *) arg;
684 /* Release refcount on the iter, if we still have one */
685 Py_XDECREF(srfstate->iter);
686 srfstate->iter = NULL;
687 /* And drop any saved args; we won't need them */
688 if (srfstate->savedargs)
689 PLy_function_drop_args(srfstate->savedargs);
690 srfstate->savedargs = NULL;
693 static void
694 plpython_return_error_callback(void *arg)
696 PLyExecutionContext *exec_ctx = PLy_current_execution_context();
698 if (exec_ctx->curr_proc &&
699 !exec_ctx->curr_proc->is_procedure)
700 errcontext("while creating return value");
703 static PyObject *
704 PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
706 TriggerData *tdata = (TriggerData *) fcinfo->context;
707 TupleDesc rel_descr = RelationGetDescr(tdata->tg_relation);
708 PyObject *pltname,
709 *pltevent,
710 *pltwhen,
711 *pltlevel,
712 *pltrelid,
713 *plttablename,
714 *plttableschema,
715 *pltargs,
716 *pytnew,
717 *pytold,
718 *pltdata;
719 char *stroid;
722 * Make any Py*_New() calls before the PG_TRY block so that we can quickly
723 * return NULL on failure. We can't return within the PG_TRY block, else
724 * we'd miss unwinding the exception stack.
726 pltdata = PyDict_New();
727 if (!pltdata)
728 return NULL;
730 if (tdata->tg_trigger->tgnargs)
732 pltargs = PyList_New(tdata->tg_trigger->tgnargs);
733 if (!pltargs)
735 Py_DECREF(pltdata);
736 return NULL;
739 else
741 Py_INCREF(Py_None);
742 pltargs = Py_None;
745 PG_TRY();
747 pltname = PLyUnicode_FromString(tdata->tg_trigger->tgname);
748 PyDict_SetItemString(pltdata, "name", pltname);
749 Py_DECREF(pltname);
751 stroid = DatumGetCString(DirectFunctionCall1(oidout,
752 ObjectIdGetDatum(tdata->tg_relation->rd_id)));
753 pltrelid = PLyUnicode_FromString(stroid);
754 PyDict_SetItemString(pltdata, "relid", pltrelid);
755 Py_DECREF(pltrelid);
756 pfree(stroid);
758 stroid = SPI_getrelname(tdata->tg_relation);
759 plttablename = PLyUnicode_FromString(stroid);
760 PyDict_SetItemString(pltdata, "table_name", plttablename);
761 Py_DECREF(plttablename);
762 pfree(stroid);
764 stroid = SPI_getnspname(tdata->tg_relation);
765 plttableschema = PLyUnicode_FromString(stroid);
766 PyDict_SetItemString(pltdata, "table_schema", plttableschema);
767 Py_DECREF(plttableschema);
768 pfree(stroid);
770 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
771 pltwhen = PLyUnicode_FromString("BEFORE");
772 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
773 pltwhen = PLyUnicode_FromString("AFTER");
774 else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
775 pltwhen = PLyUnicode_FromString("INSTEAD OF");
776 else
778 elog(ERROR, "unrecognized WHEN tg_event: %u", tdata->tg_event);
779 pltwhen = NULL; /* keep compiler quiet */
781 PyDict_SetItemString(pltdata, "when", pltwhen);
782 Py_DECREF(pltwhen);
784 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
786 pltlevel = PLyUnicode_FromString("ROW");
787 PyDict_SetItemString(pltdata, "level", pltlevel);
788 Py_DECREF(pltlevel);
791 * Note: In BEFORE trigger, stored generated columns are not
792 * computed yet, so don't make them accessible in NEW row.
795 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
797 pltevent = PLyUnicode_FromString("INSERT");
799 PyDict_SetItemString(pltdata, "old", Py_None);
800 pytnew = PLy_input_from_tuple(&proc->result_in,
801 tdata->tg_trigtuple,
802 rel_descr,
803 !TRIGGER_FIRED_BEFORE(tdata->tg_event));
804 PyDict_SetItemString(pltdata, "new", pytnew);
805 Py_DECREF(pytnew);
806 *rv = tdata->tg_trigtuple;
808 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
810 pltevent = PLyUnicode_FromString("DELETE");
812 PyDict_SetItemString(pltdata, "new", Py_None);
813 pytold = PLy_input_from_tuple(&proc->result_in,
814 tdata->tg_trigtuple,
815 rel_descr,
816 true);
817 PyDict_SetItemString(pltdata, "old", pytold);
818 Py_DECREF(pytold);
819 *rv = tdata->tg_trigtuple;
821 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
823 pltevent = PLyUnicode_FromString("UPDATE");
825 pytnew = PLy_input_from_tuple(&proc->result_in,
826 tdata->tg_newtuple,
827 rel_descr,
828 !TRIGGER_FIRED_BEFORE(tdata->tg_event));
829 PyDict_SetItemString(pltdata, "new", pytnew);
830 Py_DECREF(pytnew);
831 pytold = PLy_input_from_tuple(&proc->result_in,
832 tdata->tg_trigtuple,
833 rel_descr,
834 true);
835 PyDict_SetItemString(pltdata, "old", pytold);
836 Py_DECREF(pytold);
837 *rv = tdata->tg_newtuple;
839 else
841 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
842 pltevent = NULL; /* keep compiler quiet */
845 PyDict_SetItemString(pltdata, "event", pltevent);
846 Py_DECREF(pltevent);
848 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
850 pltlevel = PLyUnicode_FromString("STATEMENT");
851 PyDict_SetItemString(pltdata, "level", pltlevel);
852 Py_DECREF(pltlevel);
854 PyDict_SetItemString(pltdata, "old", Py_None);
855 PyDict_SetItemString(pltdata, "new", Py_None);
856 *rv = NULL;
858 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
859 pltevent = PLyUnicode_FromString("INSERT");
860 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
861 pltevent = PLyUnicode_FromString("DELETE");
862 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
863 pltevent = PLyUnicode_FromString("UPDATE");
864 else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
865 pltevent = PLyUnicode_FromString("TRUNCATE");
866 else
868 elog(ERROR, "unrecognized OP tg_event: %u", tdata->tg_event);
869 pltevent = NULL; /* keep compiler quiet */
872 PyDict_SetItemString(pltdata, "event", pltevent);
873 Py_DECREF(pltevent);
875 else
876 elog(ERROR, "unrecognized LEVEL tg_event: %u", tdata->tg_event);
878 if (tdata->tg_trigger->tgnargs)
881 * all strings...
883 int i;
884 PyObject *pltarg;
886 /* pltargs should have been allocated before the PG_TRY block. */
887 Assert(pltargs && pltargs != Py_None);
889 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
891 pltarg = PLyUnicode_FromString(tdata->tg_trigger->tgargs[i]);
894 * stolen, don't Py_DECREF
896 PyList_SetItem(pltargs, i, pltarg);
899 else
901 Assert(pltargs == Py_None);
903 PyDict_SetItemString(pltdata, "args", pltargs);
904 Py_DECREF(pltargs);
906 PG_CATCH();
908 Py_XDECREF(pltargs);
909 Py_XDECREF(pltdata);
910 PG_RE_THROW();
912 PG_END_TRY();
914 return pltdata;
918 * Apply changes requested by a MODIFY return from a trigger function.
920 static HeapTuple
921 PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata,
922 HeapTuple otup)
924 HeapTuple rtup;
925 PyObject *volatile plntup;
926 PyObject *volatile plkeys;
927 PyObject *volatile plval;
928 Datum *volatile modvalues;
929 bool *volatile modnulls;
930 bool *volatile modrepls;
931 ErrorContextCallback plerrcontext;
933 plerrcontext.callback = plpython_trigger_error_callback;
934 plerrcontext.previous = error_context_stack;
935 error_context_stack = &plerrcontext;
937 plntup = plkeys = plval = NULL;
938 modvalues = NULL;
939 modnulls = NULL;
940 modrepls = NULL;
942 PG_TRY();
944 TupleDesc tupdesc;
945 int nkeys,
948 if ((plntup = PyDict_GetItemString(pltd, "new")) == NULL)
949 ereport(ERROR,
950 (errcode(ERRCODE_UNDEFINED_OBJECT),
951 errmsg("TD[\"new\"] deleted, cannot modify row")));
952 Py_INCREF(plntup);
953 if (!PyDict_Check(plntup))
954 ereport(ERROR,
955 (errcode(ERRCODE_DATATYPE_MISMATCH),
956 errmsg("TD[\"new\"] is not a dictionary")));
958 plkeys = PyDict_Keys(plntup);
959 nkeys = PyList_Size(plkeys);
961 tupdesc = RelationGetDescr(tdata->tg_relation);
963 modvalues = (Datum *) palloc0(tupdesc->natts * sizeof(Datum));
964 modnulls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
965 modrepls = (bool *) palloc0(tupdesc->natts * sizeof(bool));
967 for (i = 0; i < nkeys; i++)
969 PyObject *platt;
970 char *plattstr;
971 int attn;
972 PLyObToDatum *att;
974 platt = PyList_GetItem(plkeys, i);
975 if (PyUnicode_Check(platt))
976 plattstr = PLyUnicode_AsString(platt);
977 else
979 ereport(ERROR,
980 (errcode(ERRCODE_DATATYPE_MISMATCH),
981 errmsg("TD[\"new\"] dictionary key at ordinal position %d is not a string", i)));
982 plattstr = NULL; /* keep compiler quiet */
984 attn = SPI_fnumber(tupdesc, plattstr);
985 if (attn == SPI_ERROR_NOATTRIBUTE)
986 ereport(ERROR,
987 (errcode(ERRCODE_UNDEFINED_COLUMN),
988 errmsg("key \"%s\" found in TD[\"new\"] does not exist as a column in the triggering row",
989 plattstr)));
990 if (attn <= 0)
991 ereport(ERROR,
992 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
993 errmsg("cannot set system attribute \"%s\"",
994 plattstr)));
995 if (TupleDescAttr(tupdesc, attn - 1)->attgenerated)
996 ereport(ERROR,
997 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
998 errmsg("cannot set generated column \"%s\"",
999 plattstr)));
1001 plval = PyDict_GetItem(plntup, platt);
1002 if (plval == NULL)
1003 elog(FATAL, "Python interpreter is probably corrupted");
1005 Py_INCREF(plval);
1007 /* We assume proc->result is set up to convert tuples properly */
1008 att = &proc->result.u.tuple.atts[attn - 1];
1010 modvalues[attn - 1] = PLy_output_convert(att,
1011 plval,
1012 &modnulls[attn - 1]);
1013 modrepls[attn - 1] = true;
1015 Py_DECREF(plval);
1016 plval = NULL;
1019 rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
1021 PG_CATCH();
1023 Py_XDECREF(plntup);
1024 Py_XDECREF(plkeys);
1025 Py_XDECREF(plval);
1027 if (modvalues)
1028 pfree(modvalues);
1029 if (modnulls)
1030 pfree(modnulls);
1031 if (modrepls)
1032 pfree(modrepls);
1034 PG_RE_THROW();
1036 PG_END_TRY();
1038 Py_DECREF(plntup);
1039 Py_DECREF(plkeys);
1041 pfree(modvalues);
1042 pfree(modnulls);
1043 pfree(modrepls);
1045 error_context_stack = plerrcontext.previous;
1047 return rtup;
1050 static void
1051 plpython_trigger_error_callback(void *arg)
1053 PLyExecutionContext *exec_ctx = PLy_current_execution_context();
1055 if (exec_ctx->curr_proc)
1056 errcontext("while modifying trigger row");
1059 /* execute Python code, propagate Python errors to the backend */
1060 static PyObject *
1061 PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
1063 PyObject *rv = NULL;
1064 int volatile save_subxact_level = list_length(explicit_subtransactions);
1066 PyDict_SetItemString(proc->globals, kargs, vargs);
1068 PG_TRY();
1070 #if PY_VERSION_HEX >= 0x03020000
1071 rv = PyEval_EvalCode(proc->code,
1072 proc->globals, proc->globals);
1073 #else
1074 rv = PyEval_EvalCode((PyCodeObject *) proc->code,
1075 proc->globals, proc->globals);
1076 #endif
1079 * Since plpy will only let you close subtransactions that you
1080 * started, you cannot *unnest* subtransactions, only *nest* them
1081 * without closing.
1083 Assert(list_length(explicit_subtransactions) >= save_subxact_level);
1085 PG_FINALLY();
1087 PLy_abort_open_subtransactions(save_subxact_level);
1089 PG_END_TRY();
1091 /* If the Python code returned an error, propagate it */
1092 if (rv == NULL)
1093 PLy_elog(ERROR, NULL);
1095 return rv;
1099 * Abort lingering subtransactions that have been explicitly started
1100 * by plpy.subtransaction().start() and not properly closed.
1102 static void
1103 PLy_abort_open_subtransactions(int save_subxact_level)
1105 Assert(save_subxact_level >= 0);
1107 while (list_length(explicit_subtransactions) > save_subxact_level)
1109 PLySubtransactionData *subtransactiondata;
1111 Assert(explicit_subtransactions != NIL);
1113 ereport(WARNING,
1114 (errmsg("forcibly aborting a subtransaction that has not been exited")));
1116 RollbackAndReleaseCurrentSubTransaction();
1118 subtransactiondata = (PLySubtransactionData *) linitial(explicit_subtransactions);
1119 explicit_subtransactions = list_delete_first(explicit_subtransactions);
1121 MemoryContextSwitchTo(subtransactiondata->oldcontext);
1122 CurrentResourceOwner = subtransactiondata->oldowner;
1123 pfree(subtransactiondata);