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