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