PostgreSQL Source Code git master
Loading...
Searching...
No Matches
plpy_main.c
Go to the documentation of this file.
1/*
2 * PL/Python main entry points
3 *
4 * src/pl/plpython/plpy_main.c
5 */
6
7#include "postgres.h"
8
9#include "catalog/pg_proc.h"
11#include "commands/trigger.h"
12#include "executor/spi.h"
13#include "miscadmin.h"
14#include "plpy_elog.h"
15#include "plpy_exec.h"
16#include "plpy_main.h"
17#include "plpy_plpymodule.h"
18#include "plpy_subxactobject.h"
19#include "plpy_util.h"
20#include "utils/guc.h"
21#include "utils/memutils.h"
22#include "utils/syscache.h"
23
24/*
25 * exported functions
26 */
27
29 .name = "plpython",
30 .version = PG_VERSION
31);
32
36
37
39static void plpython_error_callback(void *arg);
40static void plpython_inline_error_callback(void *arg);
41
43static void PLy_pop_execution_context(void);
44
45/* initialize global variables */
47
48/* this doesn't need to be global; use PLy_current_execution_context() */
50
51
52void
54{
57 PyObject *GD;
59
61
62 /* Add plpy to table of built-in modules. */
64
65 /* Initialize Python interpreter. */
67
68 main_mod = PyImport_AddModule("__main__");
69 if (main_mod == NULL || PyErr_Occurred())
70 PLy_elog(ERROR, "could not import \"%s\" module", "__main__");
72
74 if (main_dict == NULL)
76
77 /*
78 * Set up GD.
79 */
80 GD = PyDict_New();
81 if (GD == NULL)
84
85 /*
86 * Import plpy.
87 */
89 if (plpy_mod == NULL)
90 PLy_elog(ERROR, "could not import \"%s\" module", "plpy");
91 if (PyDict_SetItemString(main_dict, "plpy", plpy_mod) == -1)
93
94 if (PyErr_Occurred())
95 PLy_elog(FATAL, "untrapped error in initialization");
96
99
101
103
105}
106
107Datum
109{
112 HeapTuple tuple;
114 PLyTrigType is_trigger;
115 TriggerData trigdata;
117 FmgrInfo flinfo;
119
120 if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
122
125
126 /* Get the new function's pg_proc entry */
128 if (!HeapTupleIsValid(tuple))
129 elog(ERROR, "cache lookup failed for function %u", funcoid);
131
133
134 ReleaseSysCache(tuple);
135
136 /*
137 * Set up a fake flinfo/fcinfo with just enough info to satisfy
138 * PLy_procedure_get(). That function derives the call context (plain
139 * function, DML trigger, or event trigger) from the fcinfo, so we have to
140 * construct matching context here.
141 */
143 MemSet(&flinfo, 0, sizeof(flinfo));
144 fake_fcinfo->flinfo = &flinfo;
145 flinfo.fn_oid = funcoid;
147
148 if (is_trigger == PLPY_TRIGGER)
149 {
150 MemSet(&trigdata, 0, sizeof(trigdata));
151 trigdata.type = T_TriggerData;
152 /* We can't validate triggers against any particular table ... */
153 fake_fcinfo->context = (Node *) &trigdata;
154 }
155 else if (is_trigger == PLPY_EVENT_TRIGGER)
156 {
157 MemSet(&etrigdata, 0, sizeof(etrigdata));
159 fake_fcinfo->context = (Node *) &etrigdata;
160 }
161
163
164 /*
165 * Release the reference count that PLy_procedure_get acquired; the
166 * PLyProcedure object remains valid for possible future use. (We could
167 * leave this to be done when the calling memory context is cleaned up,
168 * but it seems neater to do it right away. Note we mustn't release the
169 * pcache object, since the memory-context reset callback has a reference
170 * to it.)
171 */
172 Assert(pcache->proc->cfunc.use_count > 0);
173 pcache->proc->cfunc.use_count--;
174 pcache->proc = NULL;
175
177}
178
179Datum
181{
182 bool nonatomic;
183 Datum retval;
186
187 nonatomic = fcinfo->context &&
188 IsA(fcinfo->context, CallContext) &&
189 !castNode(CallContext, fcinfo->context)->atomic;
190
191 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
193
194 /*
195 * Push execution context onto stack. It is important that this get
196 * popped again, so avoid putting anything that could throw error between
197 * here and the PG_TRY.
198 */
200
201 PG_TRY();
202 {
204
205 /*
206 * Setup error traceback support for ereport(). Note that the PG_TRY
207 * structure pops this for us again at exit, so we needn't do that
208 * explicitly, nor do we risk the callback getting called after we've
209 * destroyed the exec_ctx.
210 */
215
216 /*
217 * Look up (and if necessary compile) the procedure. This can throw
218 * an error, so it must happen inside the PG_TRY so that the execution
219 * context gets popped on the way out.
220 */
221 pcache = PLy_procedure_get(fcinfo, false);
222 exec_ctx->curr_proc = pcache->proc;
223
224 if (CALLED_AS_TRIGGER(fcinfo))
225 {
227
228 trv = PLy_exec_trigger(fcinfo, pcache->proc);
229 retval = PointerGetDatum(trv);
230 }
231 else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
232 {
233 PLy_exec_event_trigger(fcinfo, pcache->proc);
234 retval = (Datum) 0;
235 }
236 else
237 retval = PLy_exec_function(fcinfo, pcache);
238 }
239 PG_CATCH();
240 {
241 /* Destroy the execution context */
243 PyErr_Clear();
244
245 PG_RE_THROW();
246 }
247 PG_END_TRY();
248
249 /* Destroy the execution context */
251
252 return retval;
253}
254
255Datum
257{
260 FmgrInfo flinfo;
261 PLyProcedure proc;
265
266 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
268
269 MemSet(fcinfo, 0, SizeForFunctionCallInfo(0));
270 MemSet(&flinfo, 0, sizeof(flinfo));
271 fake_fcinfo->flinfo = &flinfo;
272 flinfo.fn_oid = InvalidOid;
274
275 MemSet(&proc, 0, sizeof(PLyProcedure));
277 "__plpython_inline_block",
279 proc.pyname = MemoryContextStrdup(proc.mcxt, "__plpython_inline_block");
280 proc.langid = codeblock->langOid;
281
282 /*
283 * This is currently sufficient to get PLy_exec_function to work, but
284 * someday we might need to be honest and use PLy_output_setup_func.
285 */
286 proc.result.typoid = VOIDOID;
287
288 /* Set up a minimal PLyProcedureCache for the inline block */
289 MemSet(&pcache, 0, sizeof(PLyProcedureCache));
290 pcache.proc = &proc;
291 pcache.fcontext = CurrentMemoryContext;
292
293 /*
294 * Push execution context onto stack. It is important that this get
295 * popped again, so avoid putting anything that could throw error between
296 * here and the PG_TRY.
297 */
299
300 PG_TRY();
301 {
302 /*
303 * Setup error traceback support for ereport().
304 * plpython_inline_error_callback doesn't currently need exec_ctx, but
305 * for consistency with plpython3_call_handler we do it the same way.
306 */
311
312 PLy_procedure_compile(&proc, codeblock->source_text);
313 exec_ctx->curr_proc = &proc;
315 }
316 PG_CATCH();
317 {
320 PyErr_Clear();
321 PG_RE_THROW();
322 }
323 PG_END_TRY();
324
325 /* Destroy the execution context */
327
328 /* Now clean up the transient procedure we made */
330
332}
333
334/*
335 * Determine whether a function is a (DML or event) trigger from its pg_proc
336 * result type. This is used by the validator, which has no call context to
337 * inspect; the call handler instead relies on the fcinfo's call context.
338 */
339static PLyTrigType
341{
342 PLyTrigType ret;
343
344 switch (procStruct->prorettype)
345 {
346 case TRIGGEROID:
347 ret = PLPY_TRIGGER;
348 break;
349 case EVENT_TRIGGEROID:
350 ret = PLPY_EVENT_TRIGGER;
351 break;
352 default:
353 ret = PLPY_NOT_TRIGGER;
354 break;
355 }
356
357 return ret;
358}
359
360static void
362{
364
365 if (exec_ctx->curr_proc)
366 {
367 if (exec_ctx->curr_proc->is_procedure)
368 errcontext("PL/Python procedure \"%s\"",
369 PLy_procedure_name(exec_ctx->curr_proc));
370 else
371 errcontext("PL/Python function \"%s\"",
372 PLy_procedure_name(exec_ctx->curr_proc));
373 }
374}
375
376static void
378{
379 errcontext("PL/Python anonymous code block");
380}
381
384{
386 elog(ERROR, "no Python function is currently executing");
387
389}
390
393{
394 /*
395 * A scratch context might never be needed in a given plpython procedure,
396 * so allocate it on first request.
397 */
398 if (context->scratch_ctx == NULL)
399 context->scratch_ctx =
401 "PL/Python scratch context",
403 return context->scratch_ctx;
404}
405
406static PLyExecutionContext *
408{
409 PLyExecutionContext *context;
410
411 /* Pick a memory context similar to what SPI uses. */
412 context = (PLyExecutionContext *)
414 sizeof(PLyExecutionContext));
415 context->curr_proc = NULL;
416 context->scratch_ctx = NULL;
417 context->next = PLy_execution_contexts;
418 PLy_execution_contexts = context;
419 return context;
420}
421
422static void
424{
426
427 if (context == NULL)
428 elog(ERROR, "no Python function is currently executing");
429
430 PLy_execution_contexts = context->next;
431
432 if (context->scratch_ctx)
434 pfree(context);
435}
#define Assert(condition)
Definition c.h:943
#define MemSet(start, val, len)
Definition c.h:1107
Datum arg
Definition elog.c:1323
ErrorContextCallback * error_context_stack
Definition elog.c:100
#define PG_RE_THROW()
Definition elog.h:407
#define errcontext
Definition elog.h:200
#define FATAL
Definition elog.h:42
#define PG_TRY(...)
Definition elog.h:374
#define PG_END_TRY(...)
Definition elog.h:399
#define ERROR
Definition elog.h:40
#define PG_CATCH(...)
Definition elog.h:384
#define TEXTDOMAIN
Definition elog.h:155
#define elog(elevel,...)
Definition elog.h:228
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition fmgr.c:2111
#define PG_RETURN_VOID()
Definition fmgr.h:350
#define PG_GETARG_OID(n)
Definition fmgr.h:275
#define PG_MODULE_MAGIC_EXT(...)
Definition fmgr.h:540
#define SizeForFunctionCallInfo(nargs)
Definition fmgr.h:102
#define PG_GETARG_DATUM(n)
Definition fmgr.h:268
#define LOCAL_FCINFO(name, nargs)
Definition fmgr.h:110
#define PG_FUNCTION_INFO_V1(funcname)
Definition fmgr.h:417
#define PG_FUNCTION_ARGS
Definition fmgr.h:193
bool check_function_bodies
Definition guc_tables.c:557
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
#define PLy_elog
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition mcxt.c:1897
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1235
MemoryContext TopTransactionContext
Definition mcxt.c:172
void pfree(void *pointer)
Definition mcxt.c:1619
MemoryContext TopMemoryContext
Definition mcxt.c:167
MemoryContext CurrentMemoryContext
Definition mcxt.c:161
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:475
MemoryContext PortalContext
Definition mcxt.c:176
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
void pg_bindtextdomain(const char *domain)
Definition miscinit.c:1890
#define IsA(nodeptr, _type_)
Definition nodes.h:162
#define castNode(_type_, nodeptr)
Definition nodes.h:180
#define NIL
Definition pg_list.h:68
END_CATALOG_STRUCT typedef FormData_pg_proc * Form_pg_proc
Definition pg_proc.h:140
void PLy_exec_event_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition plpy_exec.c:504
Datum PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedureCache *pcache)
Definition plpy_exec.c:41
HeapTuple PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition plpy_exec.c:389
Datum plpython3_validator(PG_FUNCTION_ARGS)
Definition plpy_main.c:108
PyObject * PLy_interp_globals
Definition plpy_main.c:46
void _PG_init(void)
Definition plpy_main.c:53
static PLyTrigType PLy_procedure_is_trigger(Form_pg_proc procStruct)
Definition plpy_main.c:340
static void plpython_inline_error_callback(void *arg)
Definition plpy_main.c:377
static PLyExecutionContext * PLy_execution_contexts
Definition plpy_main.c:49
Datum plpython3_inline_handler(PG_FUNCTION_ARGS)
Definition plpy_main.c:256
PLyExecutionContext * PLy_current_execution_context(void)
Definition plpy_main.c:383
static void plpython_error_callback(void *arg)
Definition plpy_main.c:361
MemoryContext PLy_get_scratch_context(PLyExecutionContext *context)
Definition plpy_main.c:392
static PLyExecutionContext * PLy_push_execution_context(bool atomic_context)
Definition plpy_main.c:407
static void PLy_pop_execution_context(void)
Definition plpy_main.c:423
Datum plpython3_call_handler(PG_FUNCTION_ARGS)
Definition plpy_main.c:180
PyMODINIT_FUNC PyInit_plpy(void)
char * PLy_procedure_name(PLyProcedure *proc)
PLyProcedureCache * PLy_procedure_get(FunctionCallInfo fcinfo, bool forValidator)
void PLy_procedure_compile(PLyProcedure *proc, const char *src)
void PLy_procedure_delete(PLyProcedure *proc)
PLyTrigType
@ PLPY_EVENT_TRIGGER
@ PLPY_TRIGGER
@ PLPY_NOT_TRIGGER
List * explicit_subtransactions
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
#define PointerGetDatum(X)
Definition postgres.h:354
#define InvalidOid
unsigned int Oid
static int fb(int x)
int SPI_connect_ext(int options)
Definition spi.c:101
#define SPI_OPT_NONATOMIC
Definition spi.h:102
MemoryContext fn_mcxt
Definition fmgr.h:65
Oid fn_oid
Definition fmgr.h:59
Definition nodes.h:133
struct PLyExecutionContext * next
Definition plpy_main.h:22
PLyProcedure * curr_proc
Definition plpy_main.h:20
MemoryContext scratch_ctx
Definition plpy_main.h:21
PLyObToDatum result
MemoryContext mcxt
NodeTag type
Definition trigger.h:33
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:265
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:221
#define CALLED_AS_TRIGGER(fcinfo)
Definition trigger.h:26
const char * name