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