PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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
10#include "catalog/pg_proc.h"
11#include "catalog/pg_type.h"
12#include "commands/trigger.h"
13#include "executor/spi.h"
14#include "miscadmin.h"
15#include "plpy_elog.h"
16#include "plpy_exec.h"
17#include "plpy_main.h"
18#include "plpy_plpymodule.h"
19#include "plpy_procedure.h"
20#include "plpy_subxactobject.h"
21#include "plpython.h"
22#include "utils/guc.h"
23#include "utils/memutils.h"
24#include "utils/rel.h"
25#include "utils/syscache.h"
26
27/*
28 * exported functions
29 */
30
32
36
37
38static bool PLy_procedure_is_trigger(Form_pg_proc procStruct);
39static void plpython_error_callback(void *arg);
40static void plpython_inline_error_callback(void *arg);
41static void PLy_init_interp(void);
42
43static PLyExecutionContext *PLy_push_execution_context(bool atomic_context);
44static void PLy_pop_execution_context(void);
45
46/* static state for Python library conflict detection */
49
50/* initialize global variables */
51PyObject *PLy_interp_globals = NULL;
52
53/* this doesn't need to be global; use PLy_current_execution_context() */
55
56
57void
59{
60 int **bitmask_ptr;
61
62 /*
63 * Set up a shared bitmask variable telling which Python version(s) are
64 * loaded into this process's address space. If there's more than one, we
65 * cannot call into libpython for fear of causing crashes. But postpone
66 * the actual failure for later, so that operations like pg_restore can
67 * load more than one plpython library so long as they don't try to do
68 * anything much with the language.
69 *
70 * While we only support Python 3 these days, somebody might create an
71 * out-of-tree version adding back support for Python 2. Conflicts with
72 * such an extension should be detected.
73 */
74 bitmask_ptr = (int **) find_rendezvous_variable("plpython_version_bitmask");
75 if (!(*bitmask_ptr)) /* am I the first? */
76 *bitmask_ptr = &plpython_version_bitmask;
77 /* Retain pointer to the agreed-on shared variable ... */
78 plpython_version_bitmask_ptr = *bitmask_ptr;
79 /* ... and announce my presence */
80 *plpython_version_bitmask_ptr |= (1 << PY_MAJOR_VERSION);
81
82 /*
83 * This should be safe even in the presence of conflicting plpythons, and
84 * it's necessary to do it before possibly throwing a conflict error, or
85 * the error message won't get localized.
86 */
88}
89
90/*
91 * Perform one-time setup of PL/Python, after checking for a conflict
92 * with other versions of Python.
93 */
94static void
96{
97 static bool inited = false;
98
99 /*
100 * Check for multiple Python libraries before actively doing anything with
101 * libpython. This must be repeated on each entry to PL/Python, in case a
102 * conflicting library got loaded since we last looked.
103 *
104 * It is attractive to weaken this error from FATAL to ERROR, but there
105 * would be corner cases, so it seems best to be conservative.
106 */
107 if (*plpython_version_bitmask_ptr != (1 << PY_MAJOR_VERSION))
109 (errmsg("multiple Python libraries are present in session"),
110 errdetail("Only one Python major version can be used in one session.")));
111
112 /* The rest should only be done once per session */
113 if (inited)
114 return;
115
116 PyImport_AppendInittab("plpy", PyInit_plpy);
117 Py_Initialize();
118 PyImport_ImportModule("plpy");
121 if (PyErr_Occurred())
122 PLy_elog(FATAL, "untrapped error in initialization");
123
125
127
129
130 inited = true;
131}
132
133/*
134 * This should be called only once, from PLy_initialize. Initialize the Python
135 * interpreter and global data.
136 */
137static void
139{
140 static PyObject *PLy_interp_safe_globals = NULL;
141 PyObject *mainmod;
142
143 mainmod = PyImport_AddModule("__main__");
144 if (mainmod == NULL || PyErr_Occurred())
145 PLy_elog(ERROR, "could not import \"__main__\" module");
146 Py_INCREF(mainmod);
147 PLy_interp_globals = PyModule_GetDict(mainmod);
148 PLy_interp_safe_globals = PyDict_New();
149 if (PLy_interp_safe_globals == NULL)
150 PLy_elog(ERROR, NULL);
151 PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
152 Py_DECREF(mainmod);
153 if (PLy_interp_globals == NULL || PyErr_Occurred())
154 PLy_elog(ERROR, "could not initialize globals");
155}
156
157Datum
159{
160 Oid funcoid = PG_GETARG_OID(0);
161 HeapTuple tuple;
162 Form_pg_proc procStruct;
163 bool is_trigger;
164
165 if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
167
170
171 /* Do this only after making sure we need to do something */
173
174 /* Get the new function's pg_proc entry */
175 tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
176 if (!HeapTupleIsValid(tuple))
177 elog(ERROR, "cache lookup failed for function %u", funcoid);
178 procStruct = (Form_pg_proc) GETSTRUCT(tuple);
179
180 is_trigger = PLy_procedure_is_trigger(procStruct);
181
182 ReleaseSysCache(tuple);
183
184 /* We can't validate triggers against any particular table ... */
185 PLy_procedure_get(funcoid, InvalidOid, is_trigger);
186
188}
189
190Datum
192{
193 bool nonatomic;
194 Datum retval;
195 PLyExecutionContext *exec_ctx;
196 ErrorContextCallback plerrcontext;
197
199
200 nonatomic = fcinfo->context &&
201 IsA(fcinfo->context, CallContext) &&
202 !castNode(CallContext, fcinfo->context)->atomic;
203
204 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
205 SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0);
206
207 /*
208 * Push execution context onto stack. It is important that this get
209 * popped again, so avoid putting anything that could throw error between
210 * here and the PG_TRY.
211 */
212 exec_ctx = PLy_push_execution_context(!nonatomic);
213
214 PG_TRY();
215 {
216 Oid funcoid = fcinfo->flinfo->fn_oid;
217 PLyProcedure *proc;
218
219 /*
220 * Setup error traceback support for ereport(). Note that the PG_TRY
221 * structure pops this for us again at exit, so we needn't do that
222 * explicitly, nor do we risk the callback getting called after we've
223 * destroyed the exec_ctx.
224 */
225 plerrcontext.callback = plpython_error_callback;
226 plerrcontext.arg = exec_ctx;
227 plerrcontext.previous = error_context_stack;
228 error_context_stack = &plerrcontext;
229
230 if (CALLED_AS_TRIGGER(fcinfo))
231 {
232 Relation tgrel = ((TriggerData *) fcinfo->context)->tg_relation;
233 HeapTuple trv;
234
235 proc = PLy_procedure_get(funcoid, RelationGetRelid(tgrel), true);
236 exec_ctx->curr_proc = proc;
237 trv = PLy_exec_trigger(fcinfo, proc);
238 retval = PointerGetDatum(trv);
239 }
240 else
241 {
242 proc = PLy_procedure_get(funcoid, InvalidOid, false);
243 exec_ctx->curr_proc = proc;
244 retval = PLy_exec_function(fcinfo, proc);
245 }
246 }
247 PG_CATCH();
248 {
250 PyErr_Clear();
251 PG_RE_THROW();
252 }
253 PG_END_TRY();
254
255 /* Destroy the execution context */
257
258 return retval;
259}
260
261Datum
263{
264 LOCAL_FCINFO(fake_fcinfo, 0);
266 FmgrInfo flinfo;
267 PLyProcedure proc;
268 PLyExecutionContext *exec_ctx;
269 ErrorContextCallback plerrcontext;
270
272
273 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
274 SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC);
275
276 MemSet(fcinfo, 0, SizeForFunctionCallInfo(0));
277 MemSet(&flinfo, 0, sizeof(flinfo));
278 fake_fcinfo->flinfo = &flinfo;
279 flinfo.fn_oid = InvalidOid;
281
282 MemSet(&proc, 0, sizeof(PLyProcedure));
284 "__plpython_inline_block",
286 proc.pyname = MemoryContextStrdup(proc.mcxt, "__plpython_inline_block");
287 proc.langid = codeblock->langOid;
288
289 /*
290 * This is currently sufficient to get PLy_exec_function to work, but
291 * someday we might need to be honest and use PLy_output_setup_func.
292 */
293 proc.result.typoid = VOIDOID;
294
295 /*
296 * Push execution context onto stack. It is important that this get
297 * popped again, so avoid putting anything that could throw error between
298 * here and the PG_TRY.
299 */
300 exec_ctx = PLy_push_execution_context(codeblock->atomic);
301
302 PG_TRY();
303 {
304 /*
305 * Setup error traceback support for ereport().
306 * plpython_inline_error_callback doesn't currently need exec_ctx, but
307 * for consistency with plpython3_call_handler we do it the same way.
308 */
310 plerrcontext.arg = exec_ctx;
311 plerrcontext.previous = error_context_stack;
312 error_context_stack = &plerrcontext;
313
314 PLy_procedure_compile(&proc, codeblock->source_text);
315 exec_ctx->curr_proc = &proc;
316 PLy_exec_function(fake_fcinfo, &proc);
317 }
318 PG_CATCH();
319 {
322 PyErr_Clear();
323 PG_RE_THROW();
324 }
325 PG_END_TRY();
326
327 /* Destroy the execution context */
329
330 /* Now clean up the transient procedure we made */
332
334}
335
336static bool
338{
339 return (procStruct->prorettype == TRIGGEROID);
340}
341
342static void
344{
346
347 if (exec_ctx->curr_proc)
348 {
349 if (exec_ctx->curr_proc->is_procedure)
350 errcontext("PL/Python procedure \"%s\"",
351 PLy_procedure_name(exec_ctx->curr_proc));
352 else
353 errcontext("PL/Python function \"%s\"",
354 PLy_procedure_name(exec_ctx->curr_proc));
355 }
356}
357
358static void
360{
361 errcontext("PL/Python anonymous code block");
362}
363
366{
367 if (PLy_execution_contexts == NULL)
368 elog(ERROR, "no Python function is currently executing");
369
371}
372
375{
376 /*
377 * A scratch context might never be needed in a given plpython procedure,
378 * so allocate it on first request.
379 */
380 if (context->scratch_ctx == NULL)
381 context->scratch_ctx =
383 "PL/Python scratch context",
385 return context->scratch_ctx;
386}
387
388static PLyExecutionContext *
389PLy_push_execution_context(bool atomic_context)
390{
392
393 /* Pick a memory context similar to what SPI uses. */
396 sizeof(PLyExecutionContext));
397 context->curr_proc = NULL;
398 context->scratch_ctx = NULL;
401 return context;
402}
403
404static void
406{
408
409 if (context == NULL)
410 elog(ERROR, "no Python function is currently executing");
411
413
414 if (context->scratch_ctx)
415 MemoryContextDelete(context->scratch_ctx);
416 pfree(context);
417}
#define MemSet(start, val, len)
Definition: c.h:974
void ** find_rendezvous_variable(const char *varName)
Definition: dfmgr.c:593
int errdetail(const char *fmt,...)
Definition: elog.c:1203
ErrorContextCallback * error_context_stack
Definition: elog.c:94
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define PG_RE_THROW()
Definition: elog.h:412
#define errcontext
Definition: elog.h:196
#define FATAL
Definition: elog.h:41
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:381
#define TEXTDOMAIN
Definition: elog.h:152
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition: fmgr.c:2145
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#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_ARGS
Definition: fmgr.h:193
bool check_function_bodies
Definition: guc_tables.c:511
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
#define PLy_elog
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1683
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1181
MemoryContext TopTransactionContext
Definition: mcxt.c:154
void pfree(void *pointer)
Definition: mcxt.c:1521
MemoryContext TopMemoryContext
Definition: mcxt.c:149
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
MemoryContext PortalContext
Definition: mcxt.c:158
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1936
#define IsA(nodeptr, _type_)
Definition: nodes.h:158
#define castNode(_type_, nodeptr)
Definition: nodes.h:176
void * arg
#define NIL
Definition: pg_list.h:68
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
Datum PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:53
HeapTuple PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:319
static int plpython_version_bitmask
Definition: plpy_main.c:48
Datum plpython3_validator(PG_FUNCTION_ARGS)
Definition: plpy_main.c:158
PyObject * PLy_interp_globals
Definition: plpy_main.c:51
static int * plpython_version_bitmask_ptr
Definition: plpy_main.c:47
static void PLy_initialize(void)
Definition: plpy_main.c:95
void _PG_init(void)
Definition: plpy_main.c:58
PG_FUNCTION_INFO_V1(plpython3_validator)
static void plpython_inline_error_callback(void *arg)
Definition: plpy_main.c:359
PG_MODULE_MAGIC
Definition: plpy_main.c:31
static PLyExecutionContext * PLy_execution_contexts
Definition: plpy_main.c:54
Datum plpython3_inline_handler(PG_FUNCTION_ARGS)
Definition: plpy_main.c:262
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:365
static bool PLy_procedure_is_trigger(Form_pg_proc procStruct)
Definition: plpy_main.c:337
static void plpython_error_callback(void *arg)
Definition: plpy_main.c:343
MemoryContext PLy_get_scratch_context(PLyExecutionContext *context)
Definition: plpy_main.c:374
static PLyExecutionContext * PLy_push_execution_context(bool atomic_context)
Definition: plpy_main.c:389
static void PLy_pop_execution_context(void)
Definition: plpy_main.c:405
static void PLy_init_interp(void)
Definition: plpy_main.c:138
Datum plpython3_call_handler(PG_FUNCTION_ARGS)
Definition: plpy_main.c:191
PyMODINIT_FUNC PyInit_plpy(void)
void PLy_init_plpy(void)
char * PLy_procedure_name(PLyProcedure *proc)
void init_procedure_caches(void)
void PLy_procedure_compile(PLyProcedure *proc, const char *src)
void PLy_procedure_delete(PLyProcedure *proc)
PLyProcedure * PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger)
List * explicit_subtransactions
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:312
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
tree context
Definition: radixtree.h:1837
#define RelationGetRelid(relation)
Definition: rel.h:505
int SPI_connect_ext(int options)
Definition: spi.c:100
#define SPI_OPT_NONATOMIC
Definition: spi.h:102
struct ErrorContextCallback * previous
Definition: elog.h:296
void(* callback)(void *arg)
Definition: elog.h:297
Definition: fmgr.h:57
MemoryContext fn_mcxt
Definition: fmgr.h:65
Oid fn_oid
Definition: fmgr.h:59
char * source_text
Definition: parsenodes.h:3513
PLyProcedure * curr_proc
Definition: plpy_main.h:20
PLyObToDatum result
MemoryContext mcxt
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26