PostgreSQL Source Code git master
plpy_main.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/event_trigger.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "miscadmin.h"
#include "plpy_elog.h"
#include "plpy_exec.h"
#include "plpy_main.h"
#include "plpy_plpymodule.h"
#include "plpy_procedure.h"
#include "plpy_subxactobject.h"
#include "plpy_util.h"
#include "utils/guc.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
Include dependency graph for plpy_main.c:

Go to the source code of this file.

Functions

 PG_MODULE_MAGIC_EXT (.name="plpython",.version=PG_VERSION)
 
 PG_FUNCTION_INFO_V1 (plpython3_validator)
 
 PG_FUNCTION_INFO_V1 (plpython3_call_handler)
 
 PG_FUNCTION_INFO_V1 (plpython3_inline_handler)
 
static void PLy_initialize (void)
 
static PLyTrigType PLy_procedure_is_trigger (Form_pg_proc procStruct)
 
static void plpython_error_callback (void *arg)
 
static void plpython_inline_error_callback (void *arg)
 
static void PLy_init_interp (void)
 
static PLyExecutionContextPLy_push_execution_context (bool atomic_context)
 
static void PLy_pop_execution_context (void)
 
void _PG_init (void)
 
Datum plpython3_validator (PG_FUNCTION_ARGS)
 
Datum plpython3_call_handler (PG_FUNCTION_ARGS)
 
Datum plpython3_inline_handler (PG_FUNCTION_ARGS)
 
PLyExecutionContextPLy_current_execution_context (void)
 
MemoryContext PLy_get_scratch_context (PLyExecutionContext *context)
 

Variables

PyObject * PLy_interp_globals = NULL
 
static PLyExecutionContextPLy_execution_contexts = NULL
 

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 59 of file plpy_main.c.

60{
62
64}
#define TEXTDOMAIN
Definition: elog.h:153
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1888
static void PLy_initialize(void)
Definition: plpy_main.c:70

References pg_bindtextdomain(), PLy_initialize(), and TEXTDOMAIN.

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( plpython3_call_handler  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( plpython3_inline_handler  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( plpython3_validator  )

◆ PG_MODULE_MAGIC_EXT()

PG_MODULE_MAGIC_EXT ( name = "plpython",
version = PG_VERSION 
)

◆ plpython3_call_handler()

Datum plpython3_call_handler ( PG_FUNCTION_ARGS  )

Definition at line 142 of file plpy_main.c.

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}
ErrorContextCallback * error_context_stack
Definition: elog.c:95
#define PG_RE_THROW()
Definition: elog.h:405
#define PG_TRY(...)
Definition: elog.h:372
#define PG_END_TRY(...)
Definition: elog.h:397
#define PG_CATCH(...)
Definition: elog.h:382
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:49
#define IsA(nodeptr, _type_)
Definition: nodes.h:164
#define castNode(_type_, nodeptr)
Definition: nodes.h:182
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
static void plpython_error_callback(void *arg)
Definition: plpy_main.c:312
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
PLyProcedure * PLy_procedure_get(Oid fn_oid, Oid fn_rel, PLyTrigType is_trigger)
@ PLPY_EVENT_TRIGGER
@ PLPY_TRIGGER
@ PLPY_NOT_TRIGGER
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:332
uint64_t Datum
Definition: postgres.h:70
#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
PLyProcedure * curr_proc
Definition: plpy_main.h:20
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:26

References ErrorContextCallback::arg, ErrorContextCallback::callback, CALLED_AS_EVENT_TRIGGER, CALLED_AS_TRIGGER, castNode, PLyExecutionContext::curr_proc, error_context_stack, InvalidOid, IsA, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLPY_EVENT_TRIGGER, PLPY_NOT_TRIGGER, PLPY_TRIGGER, plpython_error_callback(), PLy_exec_event_trigger(), PLy_exec_function(), PLy_exec_trigger(), PLy_pop_execution_context(), PLy_procedure_get(), PLy_push_execution_context(), PointerGetDatum(), ErrorContextCallback::previous, RelationGetRelid, SPI_connect_ext(), and SPI_OPT_NONATOMIC.

◆ plpython3_inline_handler()

Datum plpython3_inline_handler ( PG_FUNCTION_ARGS  )

Definition at line 218 of file plpy_main.c.

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}
#define MemSet(start, val, len)
Definition: c.h:1022
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#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
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1746
MemoryContext TopMemoryContext
Definition: mcxt.c:166
MemoryContext CurrentMemoryContext
Definition: mcxt.c:160
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static void plpython_inline_error_callback(void *arg)
Definition: plpy_main.c:328
void PLy_procedure_compile(PLyProcedure *proc, const char *src)
void PLy_procedure_delete(PLyProcedure *proc)
static Pointer DatumGetPointer(Datum X)
Definition: postgres.h:322
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
PLyObToDatum result
MemoryContext mcxt

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ErrorContextCallback::arg, InlineCodeBlock::atomic, ErrorContextCallback::callback, PLyExecutionContext::curr_proc, CurrentMemoryContext, DatumGetPointer(), error_context_stack, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, InvalidOid, PLyProcedure::langid, InlineCodeBlock::langOid, LOCAL_FCINFO, PLyProcedure::mcxt, MemoryContextStrdup(), MemSet, PG_CATCH, PG_END_TRY, PG_GETARG_DATUM, PG_RE_THROW, PG_RETURN_VOID, PG_TRY, plpython_inline_error_callback(), PLy_exec_function(), PLy_pop_execution_context(), PLy_procedure_compile(), PLy_procedure_delete(), PLy_push_execution_context(), ErrorContextCallback::previous, PLyProcedure::pyname, PLyProcedure::result, SizeForFunctionCallInfo, InlineCodeBlock::source_text, SPI_connect_ext(), SPI_OPT_NONATOMIC, TopMemoryContext, and PLyObToDatum::typoid.

◆ plpython3_validator()

Datum plpython3_validator ( PG_FUNCTION_ARGS  )

Definition at line 112 of file plpy_main.c.

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}
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:226
bool CheckFunctionValidatorAccess(Oid validatorOid, Oid functionOid)
Definition: fmgr.c:2110
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
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
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
static PLyTrigType PLy_procedure_is_trigger(Form_pg_proc procStruct)
Definition: plpy_main.c:291
PLyTrigType
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:262
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:264
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:220

References check_function_bodies, CheckFunctionValidatorAccess(), elog, ERROR, GETSTRUCT(), HeapTupleIsValid, InvalidOid, ObjectIdGetDatum(), PG_GETARG_OID, PG_RETURN_VOID, PLy_procedure_get(), PLy_procedure_is_trigger(), ReleaseSysCache(), and SearchSysCache1().

◆ plpython_error_callback()

static void plpython_error_callback ( void *  arg)
static

Definition at line 312 of file plpy_main.c.

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}
#define errcontext
Definition: elog.h:198
void * arg
char * PLy_procedure_name(PLyProcedure *proc)

References arg, PLyExecutionContext::curr_proc, errcontext, PLyProcedure::is_procedure, and PLy_procedure_name().

Referenced by plpython3_call_handler().

◆ plpython_inline_error_callback()

static void plpython_inline_error_callback ( void *  arg)
static

Definition at line 328 of file plpy_main.c.

329{
330 errcontext("PL/Python anonymous code block");
331}

References errcontext.

Referenced by plpython3_inline_handler().

◆ PLy_current_execution_context()

◆ PLy_get_scratch_context()

MemoryContext PLy_get_scratch_context ( PLyExecutionContext context)

Definition at line 343 of file plpy_main.c.

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}
MemoryContext TopTransactionContext
Definition: mcxt.c:171
MemoryContext scratch_ctx
Definition: plpy_main.h:21

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, PLyExecutionContext::scratch_ctx, and TopTransactionContext.

Referenced by PLy_input_convert(), and PLy_input_from_tuple().

◆ PLy_init_interp()

static void PLy_init_interp ( void  )
static

Definition at line 92 of file plpy_main.c.

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}
#define PLy_elog
PyObject * PLy_interp_globals
Definition: plpy_main.c:52

References ERROR, PLy_elog, and PLy_interp_globals.

Referenced by PLy_initialize().

◆ PLy_initialize()

static void PLy_initialize ( void  )
static

Definition at line 70 of file plpy_main.c.

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}
#define FATAL
Definition: elog.h:41
#define NIL
Definition: pg_list.h:68
static void PLy_init_interp(void)
Definition: plpy_main.c:92
PyMODINIT_FUNC PyInit_plpy(void)
void PLy_init_plpy(void)
void init_procedure_caches(void)
List * explicit_subtransactions

References explicit_subtransactions, FATAL, init_procedure_caches(), NIL, PLy_elog, PLy_execution_contexts, PLy_init_interp(), PLy_init_plpy(), and PyInit_plpy().

Referenced by _PG_init().

◆ PLy_pop_execution_context()

static void PLy_pop_execution_context ( void  )
static

Definition at line 374 of file plpy_main.c.

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}
void pfree(void *pointer)
Definition: mcxt.c:1594
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:469
struct PLyExecutionContext * next
Definition: plpy_main.h:22

References elog, ERROR, MemoryContextDelete(), PLyExecutionContext::next, pfree(), PLy_execution_contexts, and PLyExecutionContext::scratch_ctx.

Referenced by plpython3_call_handler(), and plpython3_inline_handler().

◆ PLy_procedure_is_trigger()

static PLyTrigType PLy_procedure_is_trigger ( Form_pg_proc  procStruct)
static

Definition at line 291 of file plpy_main.c.

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}

References PLPY_EVENT_TRIGGER, PLPY_NOT_TRIGGER, and PLPY_TRIGGER.

Referenced by plpython3_validator().

◆ PLy_push_execution_context()

static PLyExecutionContext * PLy_push_execution_context ( bool  atomic_context)
static

Definition at line 358 of file plpy_main.c.

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}
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition: mcxt.c:1229
MemoryContext PortalContext
Definition: mcxt.c:175

References PLyExecutionContext::curr_proc, MemoryContextAlloc(), PLyExecutionContext::next, PLy_execution_contexts, PortalContext, PLyExecutionContext::scratch_ctx, and TopTransactionContext.

Referenced by plpython3_call_handler(), and plpython3_inline_handler().

Variable Documentation

◆ PLy_execution_contexts

PLyExecutionContext* PLy_execution_contexts = NULL
static

◆ PLy_interp_globals

PyObject* PLy_interp_globals = NULL

Definition at line 52 of file plpy_main.c.

Referenced by PLy_init_interp(), and PLy_procedure_compile().