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
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
43static void plpython_error_callback(void *arg);
44static void plpython_inline_error_callback(void *arg);
45
47static void PLy_pop_execution_context(void);
48
49/* initialize global variables */
51
52/* this doesn't need to be global; use PLy_current_execution_context() */
54
55
56void
58{
61 PyObject *GD;
63
65
66 /* Add plpy to table of built-in modules. */
68
69 /* Initialize Python interpreter. */
71
72 main_mod = PyImport_AddModule("__main__");
73 if (main_mod == NULL || PyErr_Occurred())
74 PLy_elog(ERROR, "could not import \"%s\" module", "__main__");
76
78 if (main_dict == NULL)
80
81 /*
82 * Set up GD.
83 */
84 GD = PyDict_New();
85 if (GD == NULL)
88
89 /*
90 * Import plpy.
91 */
93 if (plpy_mod == NULL)
94 PLy_elog(ERROR, "could not import \"%s\" module", "plpy");
95 if (PyDict_SetItemString(main_dict, "plpy", plpy_mod) == -1)
97
98 if (PyErr_Occurred())
99 PLy_elog(FATAL, "untrapped error in initialization");
100
103
105
107
109
111}
112
113Datum
115{
117 HeapTuple tuple;
119 PLyTrigType is_trigger;
120
121 if (!CheckFunctionValidatorAccess(fcinfo->flinfo->fn_oid, funcoid))
123
126
127 /* Get the new function's pg_proc entry */
129 if (!HeapTupleIsValid(tuple))
130 elog(ERROR, "cache lookup failed for function %u", funcoid);
132
134
135 ReleaseSysCache(tuple);
136
137 /* We can't validate triggers against any particular table ... */
139
141}
142
143Datum
145{
146 bool nonatomic;
147 Datum retval;
150
151 nonatomic = fcinfo->context &&
152 IsA(fcinfo->context, CallContext) &&
153 !castNode(CallContext, fcinfo->context)->atomic;
154
155 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
157
158 /*
159 * Push execution context onto stack. It is important that this get
160 * popped again, so avoid putting anything that could throw error between
161 * here and the PG_TRY.
162 */
164
165 PG_TRY();
166 {
167 Oid funcoid = fcinfo->flinfo->fn_oid;
168 PLyProcedure *proc;
169
170 /*
171 * Setup error traceback support for ereport(). Note that the PG_TRY
172 * structure pops this for us again at exit, so we needn't do that
173 * explicitly, nor do we risk the callback getting called after we've
174 * destroyed the exec_ctx.
175 */
180
181 if (CALLED_AS_TRIGGER(fcinfo))
182 {
183 Relation tgrel = ((TriggerData *) fcinfo->context)->tg_relation;
185
187 exec_ctx->curr_proc = proc;
188 trv = PLy_exec_trigger(fcinfo, proc);
189 retval = PointerGetDatum(trv);
190 }
191 else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
192 {
194 exec_ctx->curr_proc = proc;
195 PLy_exec_event_trigger(fcinfo, proc);
196 retval = (Datum) 0;
197 }
198 else
199 {
201 exec_ctx->curr_proc = proc;
202 retval = PLy_exec_function(fcinfo, proc);
203 }
204 }
205 PG_CATCH();
206 {
208 PyErr_Clear();
209 PG_RE_THROW();
210 }
211 PG_END_TRY();
212
213 /* Destroy the execution context */
215
216 return retval;
217}
218
219Datum
221{
224 FmgrInfo flinfo;
225 PLyProcedure proc;
228
229 /* Note: SPI_finish() happens in plpy_exec.c, which is dubious design */
231
232 MemSet(fcinfo, 0, SizeForFunctionCallInfo(0));
233 MemSet(&flinfo, 0, sizeof(flinfo));
234 fake_fcinfo->flinfo = &flinfo;
235 flinfo.fn_oid = InvalidOid;
237
238 MemSet(&proc, 0, sizeof(PLyProcedure));
240 "__plpython_inline_block",
242 proc.pyname = MemoryContextStrdup(proc.mcxt, "__plpython_inline_block");
243 proc.langid = codeblock->langOid;
244
245 /*
246 * This is currently sufficient to get PLy_exec_function to work, but
247 * someday we might need to be honest and use PLy_output_setup_func.
248 */
249 proc.result.typoid = VOIDOID;
250
251 /*
252 * Push execution context onto stack. It is important that this get
253 * popped again, so avoid putting anything that could throw error between
254 * here and the PG_TRY.
255 */
257
258 PG_TRY();
259 {
260 /*
261 * Setup error traceback support for ereport().
262 * plpython_inline_error_callback doesn't currently need exec_ctx, but
263 * for consistency with plpython3_call_handler we do it the same way.
264 */
269
270 PLy_procedure_compile(&proc, codeblock->source_text);
271 exec_ctx->curr_proc = &proc;
273 }
274 PG_CATCH();
275 {
278 PyErr_Clear();
279 PG_RE_THROW();
280 }
281 PG_END_TRY();
282
283 /* Destroy the execution context */
285
286 /* Now clean up the transient procedure we made */
288
290}
291
292static PLyTrigType
294{
295 PLyTrigType ret;
296
297 switch (procStruct->prorettype)
298 {
299 case TRIGGEROID:
300 ret = PLPY_TRIGGER;
301 break;
302 case EVENT_TRIGGEROID:
303 ret = PLPY_EVENT_TRIGGER;
304 break;
305 default:
306 ret = PLPY_NOT_TRIGGER;
307 break;
308 }
309
310 return ret;
311}
312
313static void
315{
317
318 if (exec_ctx->curr_proc)
319 {
320 if (exec_ctx->curr_proc->is_procedure)
321 errcontext("PL/Python procedure \"%s\"",
322 PLy_procedure_name(exec_ctx->curr_proc));
323 else
324 errcontext("PL/Python function \"%s\"",
325 PLy_procedure_name(exec_ctx->curr_proc));
326 }
327}
328
329static void
331{
332 errcontext("PL/Python anonymous code block");
333}
334
337{
339 elog(ERROR, "no Python function is currently executing");
340
342}
343
346{
347 /*
348 * A scratch context might never be needed in a given plpython procedure,
349 * so allocate it on first request.
350 */
351 if (context->scratch_ctx == NULL)
352 context->scratch_ctx =
354 "PL/Python scratch context",
356 return context->scratch_ctx;
357}
358
359static PLyExecutionContext *
361{
362 PLyExecutionContext *context;
363
364 /* Pick a memory context similar to what SPI uses. */
365 context = (PLyExecutionContext *)
367 sizeof(PLyExecutionContext));
368 context->curr_proc = NULL;
369 context->scratch_ctx = NULL;
370 context->next = PLy_execution_contexts;
371 PLy_execution_contexts = context;
372 return context;
373}
374
375static void
377{
379
380 if (context == NULL)
381 elog(ERROR, "no Python function is currently executing");
382
383 PLy_execution_contexts = context->next;
384
385 if (context->scratch_ctx)
387 pfree(context);
388}
#define MemSet(start, val, len)
Definition c.h:1013
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)
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition fmgr.c:2110
#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:529
#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:1768
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
MemoryContext TopTransactionContext
Definition mcxt.c:171
void pfree(void *pointer)
Definition mcxt.c:1616
MemoryContext TopMemoryContext
Definition mcxt.c:166
MemoryContext CurrentMemoryContext
Definition mcxt.c:160
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
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:114
PyObject * PLy_interp_globals
Definition plpy_main.c:50
void _PG_init(void)
Definition plpy_main.c:57
static PLyTrigType PLy_procedure_is_trigger(Form_pg_proc procStruct)
Definition plpy_main.c:293
static void plpython_inline_error_callback(void *arg)
Definition plpy_main.c:330
static PLyExecutionContext * PLy_execution_contexts
Definition plpy_main.c:53
Datum plpython3_inline_handler(PG_FUNCTION_ARGS)
Definition plpy_main.c:220
PLyExecutionContext * PLy_current_execution_context(void)
Definition plpy_main.c:336
static void plpython_error_callback(void *arg)
Definition plpy_main.c:314
MemoryContext PLy_get_scratch_context(PLyExecutionContext *context)
Definition plpy_main.c:345
static PLyExecutionContext * PLy_push_execution_context(bool atomic_context)
Definition plpy_main.c:360
static void PLy_pop_execution_context(void)
Definition plpy_main.c:376
Datum plpython3_call_handler(PG_FUNCTION_ARGS)
Definition plpy_main.c:144
PyMODINIT_FUNC PyInit_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:352
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:262
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
#define InvalidOid
unsigned int Oid
static int fb(int x)
#define RelationGetRelid(relation)
Definition rel.h:514
int SPI_connect_ext(int options)
Definition spi.c:100
#define SPI_OPT_NONATOMIC
Definition spi.h:102
MemoryContext fn_mcxt
Definition fmgr.h:65
Oid fn_oid
Definition fmgr.h:59
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