PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 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 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

PyObjectPLy_interp_globals = NULL
 
static PLyExecutionContextPLy_execution_contexts = NULL
 

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 57 of file plpy_main.c.

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}
#define FATAL
Definition elog.h:41
#define ERROR
Definition elog.h:39
#define TEXTDOMAIN
Definition elog.h:153
#define PLy_elog
void pg_bindtextdomain(const char *domain)
Definition miscinit.c:1888
#define NIL
Definition pg_list.h:68
PyObject * PLy_interp_globals
Definition plpy_main.c:50
static PLyExecutionContext * PLy_execution_contexts
Definition plpy_main.c:53
PyMODINIT_FUNC PyInit_plpy(void)
void init_procedure_caches(void)
List * explicit_subtransactions
static int fb(int x)

References ERROR, explicit_subtransactions, FATAL, fb(), init_procedure_caches(), NIL, pg_bindtextdomain(), PLy_elog, PLy_execution_contexts, PLy_interp_globals, PyInit_plpy(), 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 144 of file plpy_main.c.

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}
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)
#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:314
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
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:352
uint64_t Datum
Definition postgres.h:70
#define InvalidOid
unsigned int Oid
#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
#define CALLED_AS_TRIGGER(fcinfo)
Definition trigger.h:26

References CALLED_AS_EVENT_TRIGGER, CALLED_AS_TRIGGER, castNode, error_context_stack, fb(), 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(), RelationGetRelid, SPI_connect_ext(), and SPI_OPT_NONATOMIC.

◆ plpython3_inline_handler()

Datum plpython3_inline_handler ( PG_FUNCTION_ARGS  )

Definition at line 220 of file plpy_main.c.

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}
#define MemSet(start, val, len)
Definition c.h:1013
#define PG_RETURN_VOID()
Definition fmgr.h:350
#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:1768
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:330
void PLy_procedure_compile(PLyProcedure *proc, const char *src)
void PLy_procedure_delete(PLyProcedure *proc)
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:342
MemoryContext fn_mcxt
Definition fmgr.h:65
Oid fn_oid
Definition fmgr.h:59
PLyObToDatum result
MemoryContext mcxt

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, CurrentMemoryContext, DatumGetPointer(), error_context_stack, fb(), FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, InvalidOid, PLyProcedure::langid, 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(), PLyProcedure::pyname, PLyProcedure::result, SizeForFunctionCallInfo, SPI_connect_ext(), SPI_OPT_NONATOMIC, TopMemoryContext, and PLyObToDatum::typoid.

◆ plpython3_validator()

Datum plpython3_validator ( PG_FUNCTION_ARGS  )

Definition at line 114 of file plpy_main.c.

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}
#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)
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:293
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, fb(), 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 314 of file plpy_main.c.

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

References arg, errcontext, fb(), 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 330 of file plpy_main.c.

331{
332 errcontext("PL/Python anonymous code block");
333}

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 345 of file plpy_main.c.

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

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, fb(), PLyExecutionContext::scratch_ctx, and TopTransactionContext.

Referenced by PLy_input_convert(), and PLy_input_from_tuple().

◆ PLy_pop_execution_context()

static void PLy_pop_execution_context ( void  )
static

Definition at line 376 of file plpy_main.c.

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

References elog, ERROR, fb(), 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 293 of file plpy_main.c.

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}

References fb(), 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 360 of file plpy_main.c.

361{
362 PLyExecutionContext *context;
363
364 /* Pick a memory context similar to what SPI uses. */
365 context = (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}
void * MemoryContextAlloc(MemoryContext context, Size size)
Definition mcxt.c:1232
MemoryContext PortalContext
Definition mcxt.c:175
PLyProcedure * curr_proc
Definition plpy_main.h:20

References PLyExecutionContext::curr_proc, fb(), 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 50 of file plpy_main.c.

Referenced by _PG_init(), and PLy_procedure_compile().