PostgreSQL Source Code git master
plpy_exec.h File Reference
#include "plpy_procedure.h"
Include dependency graph for plpy_exec.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

Datum PLy_exec_function (FunctionCallInfo fcinfo, PLyProcedure *proc)
 
HeapTuple PLy_exec_trigger (FunctionCallInfo fcinfo, PLyProcedure *proc)
 
void PLy_exec_event_trigger (FunctionCallInfo fcinfo, PLyProcedure *proc)
 

Function Documentation

◆ PLy_exec_event_trigger()

void PLy_exec_event_trigger ( FunctionCallInfo  fcinfo,
PLyProcedure proc 
)

Definition at line 435 of file plpy_exec.c.

436{
437 EventTriggerData *tdata;
438 PyObject *volatile pltdata = NULL;
439
441 tdata = (EventTriggerData *) fcinfo->context;
442
443 PG_TRY();
444 {
445 PyObject *pltevent,
446 *plttag;
447
448 pltdata = PyDict_New();
449 if (!pltdata)
450 PLy_elog(ERROR, NULL);
451
452 pltevent = PLyUnicode_FromString(tdata->event);
453 PyDict_SetItemString(pltdata, "event", pltevent);
454 Py_DECREF(pltevent);
455
457 PyDict_SetItemString(pltdata, "tag", plttag);
458 Py_DECREF(plttag);
459
460 PLy_procedure_call(proc, "TD", pltdata);
461
462 if (SPI_finish() != SPI_OK_FINISH)
463 elog(ERROR, "SPI_finish() failed");
464 }
465 PG_FINALLY();
466 {
467 Py_XDECREF(pltdata);
468 }
469 PG_END_TRY();
470}
const char * GetCommandTagName(CommandTag commandTag)
Definition: cmdtag.c:47
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
#define PG_FINALLY(...)
Definition: elog.h:389
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:49
Assert(PointerIsAligned(start, uint64))
#define PLy_elog
static PyObject * PLy_procedure_call(PLyProcedure *proc, const char *kargs, PyObject *vargs)
Definition: plpy_exec.c:1102
PyObject * PLyUnicode_FromString(const char *s)
Definition: plpy_util.c:116
int SPI_finish(void)
Definition: spi.c:182
#define SPI_OK_FINISH
Definition: spi.h:83
CommandTag tag
Definition: event_trigger.h:29
const char * event
Definition: event_trigger.h:27

References Assert(), CALLED_AS_EVENT_TRIGGER, FunctionCallInfoBaseData::context, elog, ERROR, EventTriggerData::event, GetCommandTagName(), PG_END_TRY, PG_FINALLY, PG_TRY, PLy_elog, PLy_procedure_call(), PLyUnicode_FromString(), SPI_finish(), SPI_OK_FINISH, and EventTriggerData::tag.

Referenced by plpython3_call_handler().

◆ PLy_exec_function()

Datum PLy_exec_function ( FunctionCallInfo  fcinfo,
PLyProcedure proc 
)

Definition at line 54 of file plpy_exec.c.

55{
56 bool is_setof = proc->is_setof;
57 Datum rv;
58 PyObject *volatile plargs = NULL;
59 PyObject *volatile plrv = NULL;
60 FuncCallContext *volatile funcctx = NULL;
61 PLySRFState *volatile srfstate = NULL;
62 ErrorContextCallback plerrcontext;
63
64 /*
65 * If the function is called recursively, we must push outer-level
66 * arguments into the stack. This must be immediately before the PG_TRY
67 * to ensure that the corresponding pop happens.
68 */
70
71 PG_TRY();
72 {
73 if (is_setof)
74 {
75 /* First Call setup */
76 if (SRF_IS_FIRSTCALL())
77 {
78 funcctx = SRF_FIRSTCALL_INIT();
79 srfstate = (PLySRFState *)
81 sizeof(PLySRFState));
82 /* Immediately register cleanup callback */
84 srfstate->callback.arg = srfstate;
86 &srfstate->callback);
87 funcctx->user_fctx = srfstate;
88 }
89 /* Every call setup */
90 funcctx = SRF_PERCALL_SETUP();
91 Assert(funcctx != NULL);
92 srfstate = (PLySRFState *) funcctx->user_fctx;
93 Assert(srfstate != NULL);
94 }
95
96 if (srfstate == NULL || srfstate->iter == NULL)
97 {
98 /*
99 * Non-SETOF function or first time for SETOF function: build
100 * args, then actually execute the function.
101 */
102 plargs = PLy_function_build_args(fcinfo, proc);
103 plrv = PLy_procedure_call(proc, "args", plargs);
104 Assert(plrv != NULL);
105 }
106 else
107 {
108 /*
109 * Second or later call for a SETOF function: restore arguments in
110 * globals dict to what they were when we left off. We must do
111 * this in case multiple evaluations of the same SETOF function
112 * are interleaved. It's a bit annoying, since the iterator may
113 * not look at the arguments at all, but we have no way to know
114 * that. Fortunately this isn't terribly expensive.
115 */
116 if (srfstate->savedargs)
117 PLy_function_restore_args(proc, srfstate->savedargs);
118 srfstate->savedargs = NULL; /* deleted by restore_args */
119 }
120
121 /*
122 * If it returns a set, call the iterator to get the next return item.
123 * We stay in the SPI context while doing this, because PyIter_Next()
124 * calls back into Python code which might contain SPI calls.
125 */
126 if (is_setof)
127 {
128 if (srfstate->iter == NULL)
129 {
130 /* first time -- do checks and setup */
131 ReturnSetInfo *rsi = (ReturnSetInfo *) fcinfo->resultinfo;
132
133 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
134 (rsi->allowedModes & SFRM_ValuePerCall) == 0)
135 {
137 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
138 errmsg("unsupported set function return mode"),
139 errdetail("PL/Python set-returning functions only support returning one value per call.")));
140 }
142
143 /* Make iterator out of returned object */
144 srfstate->iter = PyObject_GetIter(plrv);
145
146 Py_DECREF(plrv);
147 plrv = NULL;
148
149 if (srfstate->iter == NULL)
151 (errcode(ERRCODE_DATATYPE_MISMATCH),
152 errmsg("returned object cannot be iterated"),
153 errdetail("PL/Python set-returning functions must return an iterable object.")));
154 }
155
156 /* Fetch next from iterator */
157 plrv = PyIter_Next(srfstate->iter);
158 if (plrv == NULL)
159 {
160 /* Iterator is exhausted or error happened */
161 bool has_error = (PyErr_Occurred() != NULL);
162
163 Py_DECREF(srfstate->iter);
164 srfstate->iter = NULL;
165
166 if (has_error)
167 PLy_elog(ERROR, "error fetching next item from iterator");
168
169 /* Pass a null through the data-returning steps below */
170 Py_INCREF(Py_None);
171 plrv = Py_None;
172 }
173 else
174 {
175 /*
176 * This won't be last call, so save argument values. We do
177 * this again each time in case the iterator is changing those
178 * values.
179 */
180 srfstate->savedargs = PLy_function_save_args(proc);
181 }
182 }
183
184 /*
185 * Disconnect from SPI manager and then create the return values datum
186 * (if the input function does a palloc for it this must not be
187 * allocated in the SPI memory context because SPI_finish would free
188 * it).
189 */
190 if (SPI_finish() != SPI_OK_FINISH)
191 elog(ERROR, "SPI_finish failed");
192
194 plerrcontext.previous = error_context_stack;
195 error_context_stack = &plerrcontext;
196
197 /*
198 * For a procedure or function declared to return void, the Python
199 * return value must be None. For void-returning functions, we also
200 * treat a None return value as a special "void datum" rather than
201 * NULL (as is the case for non-void-returning functions).
202 */
203 if (proc->result.typoid == VOIDOID)
204 {
205 if (plrv != Py_None)
206 {
207 if (proc->is_procedure)
209 (errcode(ERRCODE_DATATYPE_MISMATCH),
210 errmsg("PL/Python procedure did not return None")));
211 else
213 (errcode(ERRCODE_DATATYPE_MISMATCH),
214 errmsg("PL/Python function with return type \"void\" did not return None")));
215 }
216
217 fcinfo->isnull = false;
218 rv = (Datum) 0;
219 }
220 else if (plrv == Py_None &&
221 srfstate && srfstate->iter == NULL)
222 {
223 /*
224 * In a SETOF function, the iteration-ending null isn't a real
225 * value; don't pass it through the input function, which might
226 * complain.
227 */
228 fcinfo->isnull = true;
229 rv = (Datum) 0;
230 }
231 else
232 {
233 /*
234 * Normal conversion of result. However, if the result is of type
235 * RECORD, we have to set up for that each time through, since it
236 * might be different from last time.
237 */
238 if (proc->result.typoid == RECORDOID)
239 {
240 TupleDesc desc;
241
242 if (get_call_result_type(fcinfo, NULL, &desc) != TYPEFUNC_COMPOSITE)
244 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
245 errmsg("function returning record called in context "
246 "that cannot accept type record")));
247 PLy_output_setup_record(&proc->result, desc, proc);
248 }
249
250 rv = PLy_output_convert(&proc->result, plrv,
251 &fcinfo->isnull);
252 }
253 }
254 PG_CATCH();
255 {
256 /* Pop old arguments from the stack if they were pushed above */
258
259 Py_XDECREF(plargs);
260 Py_XDECREF(plrv);
261
262 /*
263 * If there was an error within a SRF, the iterator might not have
264 * been exhausted yet. Clear it so the next invocation of the
265 * function will start the iteration again. (This code is probably
266 * unnecessary now; plpython_srf_cleanup_callback should take care of
267 * cleanup. But it doesn't hurt anything to do it here.)
268 */
269 if (srfstate)
270 {
271 Py_XDECREF(srfstate->iter);
272 srfstate->iter = NULL;
273 /* And drop any saved args; we won't need them */
274 if (srfstate->savedargs)
276 srfstate->savedargs = NULL;
277 }
278
279 PG_RE_THROW();
280 }
281 PG_END_TRY();
282
283 error_context_stack = plerrcontext.previous;
284
285 /* Pop old arguments from the stack if they were pushed above */
287
288 Py_XDECREF(plargs);
289 Py_DECREF(plrv);
290
291 if (srfstate)
292 {
293 /* We're in a SRF, exit appropriately */
294 if (srfstate->iter == NULL)
295 {
296 /* Iterator exhausted, so we're done */
297 SRF_RETURN_DONE(funcctx);
298 }
299 else if (fcinfo->isnull)
300 SRF_RETURN_NEXT_NULL(funcctx);
301 else
302 SRF_RETURN_NEXT(funcctx, rv);
303 }
304
305 /* Plain function, just return the Datum value (possibly null) */
306 return rv;
307}
int errdetail(const char *fmt,...)
Definition: elog.c:1216
ErrorContextCallback * error_context_stack
Definition: elog.c:95
int errcode(int sqlerrcode)
Definition: elog.c:863
int errmsg(const char *fmt,...)
Definition: elog.c:1080
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_CATCH(...)
Definition: elog.h:382
#define ereport(elevel,...)
Definition: elog.h:150
@ SFRM_ValuePerCall
Definition: execnodes.h:340
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_RETURN_NEXT_NULL(_funcctx)
Definition: funcapi.h:319
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
if(TABLE==NULL||TABLE_index==NULL)
Definition: isn.c:81
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition: mcxt.c:1263
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition: mcxt.c:579
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
static void PLy_function_drop_args(PLySavedArgs *savedargs)
Definition: plpy_exec.c:624
static void PLy_global_args_push(PLyProcedure *proc)
Definition: plpy_exec.c:653
static PyObject * PLy_function_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:475
static void PLy_function_restore_args(PLyProcedure *proc, PLySavedArgs *savedargs)
Definition: plpy_exec.c:584
static void PLy_global_args_pop(PLyProcedure *proc)
Definition: plpy_exec.c:683
static void plpython_srf_cleanup_callback(void *arg)
Definition: plpy_exec.c:721
static PLySavedArgs * PLy_function_save_args(PLyProcedure *proc)
Definition: plpy_exec.c:538
static void plpython_return_error_callback(void *arg)
Definition: plpy_exec.c:735
void PLy_output_setup_record(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:261
Datum PLy_output_convert(PLyObToDatum *arg, PyObject *val, bool *isnull)
Definition: plpy_typeio.c:120
uint64_t Datum
Definition: postgres.h:70
struct ErrorContextCallback * previous
Definition: elog.h:297
void(* callback)(void *arg)
Definition: elog.h:298
void * user_fctx
Definition: funcapi.h:82
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
MemoryContextCallbackFunction func
Definition: palloc.h:49
PLyObToDatum result
MemoryContextCallback callback
Definition: plpy_exec.c:30
PLySavedArgs * savedargs
Definition: plpy_exec.c:29
PyObject * iter
Definition: plpy_exec.c:28
SetFunctionReturnMode returnMode
Definition: execnodes.h:360
int allowedModes
Definition: execnodes.h:358

References ReturnSetInfo::allowedModes, MemoryContextCallback::arg, Assert(), ErrorContextCallback::callback, PLySRFState::callback, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, error_context_stack, MemoryContextCallback::func, get_call_result_type(), if(), PLyProcedure::is_procedure, PLyProcedure::is_setof, IsA, FunctionCallInfoBaseData::isnull, PLySRFState::iter, MemoryContextAllocZero(), MemoryContextRegisterResetCallback(), FuncCallContext::multi_call_memory_ctx, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, plpython_return_error_callback(), plpython_srf_cleanup_callback(), PLy_elog, PLy_function_build_args(), PLy_function_drop_args(), PLy_function_restore_args(), PLy_function_save_args(), PLy_global_args_pop(), PLy_global_args_push(), PLy_output_convert(), PLy_output_setup_record(), PLy_procedure_call(), ErrorContextCallback::previous, PLyProcedure::result, FunctionCallInfoBaseData::resultinfo, ReturnSetInfo::returnMode, PLySRFState::savedargs, SFRM_ValuePerCall, SPI_finish(), SPI_OK_FINISH, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, SRF_RETURN_NEXT_NULL, TYPEFUNC_COMPOSITE, PLyObToDatum::typoid, and FuncCallContext::user_fctx.

Referenced by plpython3_call_handler(), and plpython3_inline_handler().

◆ PLy_exec_trigger()

HeapTuple PLy_exec_trigger ( FunctionCallInfo  fcinfo,
PLyProcedure proc 
)

Definition at line 320 of file plpy_exec.c.

321{
322 HeapTuple rv = NULL;
323 PyObject *volatile plargs = NULL;
324 PyObject *volatile plrv = NULL;
325 TriggerData *tdata;
326 TupleDesc rel_descr;
327
328 Assert(CALLED_AS_TRIGGER(fcinfo));
329 tdata = (TriggerData *) fcinfo->context;
330
331 /*
332 * Input/output conversion for trigger tuples. We use the result and
333 * result_in fields to store the tuple conversion info. We do this over
334 * again on each call to cover the possibility that the relation's tupdesc
335 * changed since the trigger was last called. The PLy_xxx_setup_func
336 * calls should only happen once, but PLy_input_setup_tuple and
337 * PLy_output_setup_tuple are responsible for not doing repetitive work.
338 */
339 rel_descr = RelationGetDescr(tdata->tg_relation);
340 if (proc->result.typoid != rel_descr->tdtypeid)
341 PLy_output_setup_func(&proc->result, proc->mcxt,
342 rel_descr->tdtypeid,
343 rel_descr->tdtypmod,
344 proc);
345 if (proc->result_in.typoid != rel_descr->tdtypeid)
346 PLy_input_setup_func(&proc->result_in, proc->mcxt,
347 rel_descr->tdtypeid,
348 rel_descr->tdtypmod,
349 proc);
350 PLy_output_setup_tuple(&proc->result, rel_descr, proc);
351 PLy_input_setup_tuple(&proc->result_in, rel_descr, proc);
352
353 /*
354 * If the trigger is called recursively, we must push outer-level
355 * arguments into the stack. This must be immediately before the PG_TRY
356 * to ensure that the corresponding pop happens.
357 */
359
360 PG_TRY();
361 {
363
364 rc = SPI_register_trigger_data(tdata);
365 Assert(rc >= 0);
366
367 plargs = PLy_trigger_build_args(fcinfo, proc, &rv);
368 plrv = PLy_procedure_call(proc, "TD", plargs);
369
370 Assert(plrv != NULL);
371
372 /*
373 * Disconnect from SPI manager
374 */
375 if (SPI_finish() != SPI_OK_FINISH)
376 elog(ERROR, "SPI_finish failed");
377
378 /*
379 * return of None means we're happy with the tuple
380 */
381 if (plrv != Py_None)
382 {
383 char *srv;
384
385 if (PyUnicode_Check(plrv))
386 srv = PLyUnicode_AsString(plrv);
387 else
388 {
390 (errcode(ERRCODE_DATA_EXCEPTION),
391 errmsg("unexpected return value from trigger procedure"),
392 errdetail("Expected None or a string.")));
393 srv = NULL; /* keep compiler quiet */
394 }
395
396 if (pg_strcasecmp(srv, "SKIP") == 0)
397 rv = NULL;
398 else if (pg_strcasecmp(srv, "MODIFY") == 0)
399 {
400 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event) ||
402 rv = PLy_modify_tuple(proc, plargs, tdata, rv);
403 else
405 (errmsg("PL/Python trigger function returned \"MODIFY\" in a DELETE trigger -- ignored")));
406 }
407 else if (pg_strcasecmp(srv, "OK") != 0)
408 {
409 /*
410 * accept "OK" as an alternative to None; otherwise, raise an
411 * error
412 */
414 (errcode(ERRCODE_DATA_EXCEPTION),
415 errmsg("unexpected return value from trigger procedure"),
416 errdetail("Expected None, \"OK\", \"SKIP\", or \"MODIFY\".")));
417 }
418 }
419 }
420 PG_FINALLY();
421 {
423 Py_XDECREF(plargs);
424 Py_XDECREF(plrv);
425 }
426 PG_END_TRY();
427
428 return rv;
429}
#define PG_USED_FOR_ASSERTS_ONLY
Definition: c.h:228
#define WARNING
Definition: elog.h:36
static PyObject * PLy_trigger_build_args(FunctionCallInfo fcinfo, PLyProcedure *proc, HeapTuple *rv)
Definition: plpy_exec.c:745
static HeapTuple PLy_modify_tuple(PLyProcedure *proc, PyObject *pltd, TriggerData *tdata, HeapTuple otup)
Definition: plpy_exec.c:962
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:296
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:418
void PLy_input_setup_tuple(PLyDatumToOb *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:165
void PLy_output_setup_tuple(PLyObToDatum *arg, TupleDesc desc, PLyProcedure *proc)
Definition: plpy_typeio.c:215
char * PLyUnicode_AsString(PyObject *unicode)
Definition: plpy_util.c:81
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:32
#define RelationGetDescr(relation)
Definition: rel.h:541
int SPI_register_trigger_data(TriggerData *tdata)
Definition: spi.c:3364
PLyDatumToOb result_in
MemoryContext mcxt
Relation tg_relation
Definition: trigger.h:35
TriggerEvent tg_event
Definition: trigger.h:34
int32 tdtypmod
Definition: tupdesc.h:139
Oid tdtypeid
Definition: tupdesc.h:138
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:110
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:116

References Assert(), CALLED_AS_TRIGGER, FunctionCallInfoBaseData::context, elog, ereport, errcode(), errdetail(), errmsg(), ERROR, PLyProcedure::mcxt, PG_END_TRY, PG_FINALLY, pg_strcasecmp(), PG_TRY, PG_USED_FOR_ASSERTS_ONLY, PLy_global_args_pop(), PLy_global_args_push(), PLy_input_setup_func(), PLy_input_setup_tuple(), PLy_modify_tuple(), PLy_output_setup_func(), PLy_output_setup_tuple(), PLy_procedure_call(), PLy_trigger_build_args(), PLyUnicode_AsString(), RelationGetDescr, PLyProcedure::result, PLyProcedure::result_in, SPI_finish(), SPI_OK_FINISH, SPI_register_trigger_data(), TupleDescData::tdtypeid, TupleDescData::tdtypmod, TriggerData::tg_event, TriggerData::tg_relation, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_UPDATE, PLyDatumToOb::typoid, PLyObToDatum::typoid, and WARNING.

Referenced by plpython3_call_handler().