PostgreSQL Source Code git master
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"
13#include "commands/trigger.h"
14#include "executor/spi.h"
15#include "miscadmin.h"
16#include "plpy_elog.h"
17#include "plpy_exec.h"
18#include "plpy_main.h"
19#include "plpy_plpymodule.h"
20#include "plpy_procedure.h"
21#include "plpy_subxactobject.h"
22#include "plpy_util.h"
23#include "utils/guc.h"
24#include "utils/memutils.h"
25#include "utils/rel.h"
26#include "utils/syscache.h"
27
28/*
29 * exported functions
30 */
31
33 .name = "plpython",
34 .version = PG_VERSION
35);
36
40
41
42static void PLy_initialize(void);
44static void plpython_error_callback(void *arg);
45static void plpython_inline_error_callback(void *arg);
46static void PLy_init_interp(void);
47
48static PLyExecutionContext *PLy_push_execution_context(bool atomic_context);
49static void PLy_pop_execution_context(void);
50
51/* initialize global variables */
52PyObject *PLy_interp_globals = NULL;
53
54/* this doesn't need to be global; use PLy_current_execution_context() */
56
57
58void
60{
62
64}
65
66/*
67 * Perform one-time setup of PL/Python.
68 */
69static void
71{
72 PyImport_AppendInittab("plpy", PyInit_plpy);
73 Py_Initialize();
74 PyImport_ImportModule("plpy");
77 if (PyErr_Occurred())
78 PLy_elog(FATAL, "untrapped error in initialization");
79
81
83
85}
86
87/*
88 * This should be called only once, from PLy_initialize. Initialize the Python
89 * interpreter and global data.
90 */
91static void
93{
94 static PyObject *PLy_interp_safe_globals = NULL;
95 PyObject *mainmod;
96
97 mainmod = PyImport_AddModule("__main__");
98 if (mainmod == NULL || PyErr_Occurred())
99 PLy_elog(ERROR, "could not import \"__main__\" module");
100 Py_INCREF(mainmod);
101 PLy_interp_globals = PyModule_GetDict(mainmod);
102 PLy_interp_safe_globals = PyDict_New();
103 if (PLy_interp_safe_globals == NULL)
104 PLy_elog(ERROR, NULL);
105 PyDict_SetItemString(PLy_interp_globals, "GD", PLy_interp_safe_globals);
106 Py_DECREF(mainmod);
107 if (PLy_interp_globals == NULL || PyErr_Occurred())
108 PLy_elog(ERROR, "could not initialize globals");
109}
110
111Datum
113{
114 Oid funcoid = PG_GETARG_OID(0);
115 HeapTuple tuple;
116 Form_pg_proc procStruct;
117 PLyTrigType is_trigger;
118
119 if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
121
124
125 /* Get the new function's pg_proc entry */
126 tuple = SearchSysCache1(PROCOID, ObjectIdGetDatum(funcoid));
127 if (!HeapTupleIsValid(tuple))
128 elog(ERROR, "cache lookup failed for function %u", funcoid);
129 procStruct = (Form_pg_proc) GETSTRUCT(tuple);
130
131 is_trigger = PLy_procedure_is_trigger(procStruct);
132
133 ReleaseSysCache(tuple);
134
135 /* We can't validate triggers against any particular table ... */
136 (void) PLy_procedure_get(funcoid, InvalidOid, is_trigger);
137
139}
140
141Datum
143{
144 bool nonatomic;
145 Datum retval;
146 PLyExecutionContext *exec_ctx;
147 ErrorContextCallback plerrcontext;
148
149 nonatomic = fcinfo->context &&
150 IsA(fcinfo->context, CallContext) &&
151 !castNode(CallContext, fcinfo->context)->atomic;
152
153 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
154 SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0);
155
156 /*
157 * Push execution context onto stack. It is important that this get
158 * popped again, so avoid putting anything that could throw error between
159 * here and the PG_TRY.
160 */
161 exec_ctx = PLy_push_execution_context(!nonatomic);
162
163 PG_TRY();
164 {
165 Oid funcoid = fcinfo->flinfo->fn_oid;
166 PLyProcedure *proc;
167
168 /*
169 * Setup error traceback support for ereport(). Note that the PG_TRY
170 * structure pops this for us again at exit, so we needn't do that
171 * explicitly, nor do we risk the callback getting called after we've
172 * destroyed the exec_ctx.
173 */
174 plerrcontext.callback = plpython_error_callback;
175 plerrcontext.arg = exec_ctx;
176 plerrcontext.previous = error_context_stack;
177 error_context_stack = &plerrcontext;
178
179 if (CALLED_AS_TRIGGER(fcinfo))
180 {
181 Relation tgrel = ((TriggerData *) fcinfo->context)->tg_relation;
182 HeapTuple trv;
183
184 proc = PLy_procedure_get(funcoid, RelationGetRelid(tgrel), PLPY_TRIGGER);
185 exec_ctx->curr_proc = proc;
186 trv = PLy_exec_trigger(fcinfo, proc);
187 retval = PointerGetDatum(trv);
188 }
189 else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
190 {
192 exec_ctx->curr_proc = proc;
193 PLy_exec_event_trigger(fcinfo, proc);
194 retval = (Datum) 0;
195 }
196 else
197 {
199 exec_ctx->curr_proc = proc;
200 retval = PLy_exec_function(fcinfo, proc);
201 }
202 }
203 PG_CATCH();
204 {
206 PyErr_Clear();
207 PG_RE_THROW();
208 }
209 PG_END_TRY();
210
211 /* Destroy the execution context */
213
214 return retval;
215}
216
217Datum
219{
220 LOCAL_FCINFO(fake_fcinfo, 0);
222 FmgrInfo flinfo;
223 PLyProcedure proc;
224 PLyExecutionContext *exec_ctx;
225 ErrorContextCallback plerrcontext;
226
227 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
228 SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC);
229
230 MemSet(fcinfo, 0, SizeForFunctionCallInfo(0));
231 MemSet(&flinfo, 0, sizeof(flinfo));
232 fake_fcinfo->flinfo = &flinfo;
233 flinfo.fn_oid = InvalidOid;
235
236 MemSet(&proc, 0, sizeof(PLyProcedure));
238 "__plpython_inline_block",
240 proc.pyname = MemoryContextStrdup(proc.mcxt, "__plpython_inline_block");
241 proc.langid = codeblock->langOid;
242
243 /*
244 * This is currently sufficient to get PLy_exec_function to work, but
245 * someday we might need to be honest and use PLy_output_setup_func.
246 */
247 proc.result.typoid = VOIDOID;
248
249 /*
250 * Push execution context onto stack. It is important that this get
251 * popped again, so avoid putting anything that could throw error between
252 * here and the PG_TRY.
253 */
254 exec_ctx = PLy_push_execution_context(codeblock->atomic);
255
256 PG_TRY();
257 {
258 /*
259 * Setup error traceback support for ereport().
260 * plpython_inline_error_callback doesn't currently need exec_ctx, but
261 * for consistency with plpython3_call_handler we do it the same way.
262 */
264 plerrcontext.arg = exec_ctx;
265 plerrcontext.previous = error_context_stack;
266 error_context_stack = &plerrcontext;
267
268 PLy_procedure_compile(&proc, codeblock->source_text);
269 exec_ctx->curr_proc = &proc;
270 PLy_exec_function(fake_fcinfo, &proc);
271 }
272 PG_CATCH();
273 {
276 PyErr_Clear();
277 PG_RE_THROW();
278 }
279 PG_END_TRY();
280
281 /* Destroy the execution context */
283
284 /* Now clean up the transient procedure we made */
286
288}
289
290static PLyTrigType
292{
293 PLyTrigType ret;
294
295 switch (procStruct->prorettype)
296 {
297 case TRIGGEROID:
298 ret = PLPY_TRIGGER;
299 break;
300 case EVENT_TRIGGEROID:
301 ret = PLPY_EVENT_TRIGGER;
302 break;
303 default:
304 ret = PLPY_NOT_TRIGGER;
305 break;
306 }
307
308 return ret;
309}
310
311static void
313{
315
316 if (exec_ctx->curr_proc)
317 {
318 if (exec_ctx->curr_proc->is_procedure)
319 errcontext("PL/Python procedure \"%s\"",
320 PLy_procedure_name(exec_ctx->curr_proc));
321 else
322 errcontext("PL/Python function \"%s\"",
323 PLy_procedure_name(exec_ctx->curr_proc));
324 }
325}
326
327static void
329{
330 errcontext("PL/Python anonymous code block");
331}
332
335{
336 if (PLy_execution_contexts == NULL)
337 elog(ERROR, "no Python function is currently executing");
338
340}
341
344{
345 /*
346 * A scratch context might never be needed in a given plpython procedure,
347 * so allocate it on first request.
348 */
349 if (context->scratch_ctx == NULL)
350 context->scratch_ctx =
352 "PL/Python scratch context",
354 return context->scratch_ctx;
355}
356
357static PLyExecutionContext *
358PLy_push_execution_context(bool atomic_context)
359{
360 PLyExecutionContext *context;
361
362 /* Pick a memory context similar to what SPI uses. */
363 context = (PLyExecutionContext *)
365 sizeof(PLyExecutionContext));
366 context->curr_proc = NULL;
367 context->scratch_ctx = NULL;
368 context->next = PLy_execution_contexts;
369 PLy_execution_contexts = context;
370 return context;
371}
372
373static void
375{
377
378 if (context == NULL)
379 elog(ERROR, "no Python function is currently executing");
380
381 PLy_execution_contexts = context->next;
382
383 if (context->scratch_ctx)
385 pfree(context);
386}
#define MemSet(start, val, len)
Definition: c.h:1024
ErrorContextCallback * error_context_stack
Definition: elog.c:95
#define PG_RE_THROW()
Definition: elog.h:405
#define errcontext
Definition: elog.h:198
#define FATAL
Definition: elog.h:41
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:382
#define TEXTDOMAIN
Definition: elog.h:153
#define elog(elevel,...)
Definition: elog.h:226
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:49
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition: fmgr.c:2110
#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:529
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
Definition: htup_details.h:728
#define PLy_elog
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1746
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
MemoryContext TopTransactionContext
Definition: mcxt.c:171
void pfree(void *pointer)
Definition: mcxt.c:1594
MemoryContext TopMemoryContext
Definition: mcxt.c:166
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
MemoryContext PortalContext
Definition: mcxt.c:175
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1888
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
void * arg
#define NIL
Definition: pg_list.h:68
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
void PLy_exec_event_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:435
Datum PLy_exec_function(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:54
HeapTuple PLy_exec_trigger(FunctionCallInfo fcinfo, PLyProcedure *proc)
Definition: plpy_exec.c:320
Datum plpython3_validator(PG_FUNCTION_ARGS)
Definition: plpy_main.c:112
PyObject * PLy_interp_globals
Definition: plpy_main.c:52
static void PLy_initialize(void)
Definition: plpy_main.c:70
void _PG_init(void)
Definition: plpy_main.c:59
PG_FUNCTION_INFO_V1(plpython3_validator)
static PLyTrigType PLy_procedure_is_trigger(Form_pg_proc procStruct)
Definition: plpy_main.c:291
static void plpython_inline_error_callback(void *arg)
Definition: plpy_main.c:328
static PLyExecutionContext * PLy_execution_contexts
Definition: plpy_main.c:55
Datum plpython3_inline_handler(PG_FUNCTION_ARGS)
Definition: plpy_main.c:218
PLyExecutionContext * PLy_current_execution_context(void)
Definition: plpy_main.c:334
static void plpython_error_callback(void *arg)
Definition: plpy_main.c:312
MemoryContext PLy_get_scratch_context(PLyExecutionContext *context)
Definition: plpy_main.c:343
PG_MODULE_MAGIC_EXT(.name="plpython",.version=PG_VERSION)
static PLyExecutionContext * PLy_push_execution_context(bool atomic_context)
Definition: plpy_main.c:358
static void PLy_pop_execution_context(void)
Definition: plpy_main.c:374
static void PLy_init_interp(void)
Definition: plpy_main.c:92
Datum plpython3_call_handler(PG_FUNCTION_ARGS)
Definition: plpy_main.c:142
PyMODINIT_FUNC PyInit_plpy(void)
void PLy_init_plpy(void)
char * PLy_procedure_name(PLyProcedure *proc)
void init_procedure_caches(void)
PLyProcedure * PLy_procedure_get(Oid fn_oid, Oid fn_rel, PLyTrigType is_trigger)
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 PointerGetDatum(const void *X)
Definition: postgres.h:332
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
uint64_t Datum
Definition: postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32
#define RelationGetRelid(relation)
Definition: rel.h:515
int SPI_connect_ext(int options)
Definition: spi.c:100
#define SPI_OPT_NONATOMIC
Definition: spi.h:102
struct ErrorContextCallback * previous
Definition: elog.h:297
void(* callback)(void *arg)
Definition: elog.h:298
Definition: fmgr.h:57
MemoryContext fn_mcxt
Definition: fmgr.h:65
Oid fn_oid
Definition: fmgr.h:59
char * source_text
Definition: parsenodes.h:3621
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
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26
const char * name