PostgreSQL Source Code git master
Loading...
Searching...
No Matches
plpy_procedure.h File Reference
#include "plpy_typeio.h"
#include "utils/funccache.h"
Include dependency graph for plpy_procedure.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  PLySavedArgs
 
struct  PLySRFState
 
struct  PLyProcedure
 
struct  PLyProcedureCache
 

Typedefs

typedef enum PLyTrigType PLyTrigType
 
typedef struct PLySavedArgs PLySavedArgs
 
typedef struct PLySRFState PLySRFState
 
typedef struct PLyProcedure PLyProcedure
 
typedef struct PLyProcedureCache PLyProcedureCache
 

Enumerations

enum  PLyTrigType { PLPY_TRIGGER , PLPY_EVENT_TRIGGER , PLPY_NOT_TRIGGER }
 

Functions

charPLy_procedure_name (PLyProcedure *proc)
 
PLyProcedureCachePLy_procedure_get (FunctionCallInfo fcinfo, bool forValidator)
 
void PLy_procedure_compile (PLyProcedure *proc, const char *src)
 
void PLy_procedure_delete (PLyProcedure *proc)
 

Typedef Documentation

◆ PLyProcedure

◆ PLyProcedureCache

◆ PLySavedArgs

◆ PLySRFState

◆ PLyTrigType

Enumeration Type Documentation

◆ PLyTrigType

Enumerator
PLPY_TRIGGER 
PLPY_EVENT_TRIGGER 
PLPY_NOT_TRIGGER 

Definition at line 15 of file plpy_procedure.h.

16{
PLyTrigType
@ PLPY_EVENT_TRIGGER
@ PLPY_TRIGGER
@ PLPY_NOT_TRIGGER

Function Documentation

◆ PLy_procedure_compile()

void PLy_procedure_compile ( PLyProcedure proc,
const char src 
)
extern

Definition at line 362 of file plpy_procedure.c.

363{
364 PyObject *crv = NULL;
365 char *msrc;
367
369
370 /*
371 * SD is private preserved data between calls. GD is global data shared by
372 * all functions
373 */
374 proc->statics = PyDict_New();
375 if (!proc->statics)
377 PyDict_SetItemString(proc->globals, "SD", proc->statics);
378
379 /*
380 * insert the function code into the interpreter
381 */
383 /* Save the mangled source for later inclusion in tracebacks */
384 proc->src = MemoryContextStrdup(proc->mcxt, msrc);
386 if (code0)
388 pfree(msrc);
389
390 if (crv != NULL)
391 {
392 int clen;
393 char call[NAMEDATALEN + 256];
394
395 Py_DECREF(crv);
396
397 /*
398 * compile a call to the function
399 */
400 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
401 if (clen < 0 || clen >= sizeof(call))
402 elog(ERROR, "string would overflow buffer");
403 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
404 if (proc->code != NULL)
405 return;
406 }
407
408 if (proc->proname)
409 PLy_elog(ERROR, "could not compile PL/Python function \"%s\"",
410 proc->proname);
411 else
412 PLy_elog(ERROR, "could not compile anonymous PL/Python code block");
413}
#define ERROR
Definition elog.h:40
#define elog(elevel,...)
Definition elog.h:228
#define PLy_elog
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition mcxt.c:1897
void pfree(void *pointer)
Definition mcxt.c:1619
#define NAMEDATALEN
PyObject * PLy_interp_globals
Definition plpy_main.c:46
static char * PLy_procedure_munge_source(const char *name, const char *src)
#define snprintf
Definition port.h:261
static int fb(int x)
PyObject * code
MemoryContext mcxt
PyObject * globals
PyObject * statics

References PLyProcedure::code, elog, ERROR, fb(), PLyProcedure::globals, PLyProcedure::mcxt, MemoryContextStrdup(), NAMEDATALEN, pfree(), PLy_elog, PLy_interp_globals, PLy_procedure_munge_source(), PLyProcedure::proname, PLyProcedure::pyname, snprintf, PLyProcedure::src, and PLyProcedure::statics.

Referenced by plpython3_inline_handler(), and PLy_procedure_create().

◆ PLy_procedure_delete()

void PLy_procedure_delete ( PLyProcedure proc)
extern

Definition at line 416 of file plpy_procedure.c.

417{
418 Py_XDECREF(proc->code);
419 Py_XDECREF(proc->statics);
420 Py_XDECREF(proc->globals);
422}
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:475

References PLyProcedure::code, fb(), PLyProcedure::globals, PLyProcedure::mcxt, MemoryContextDelete(), and PLyProcedure::statics.

Referenced by plpython3_inline_handler(), PLy_delete_callback(), and PLy_procedure_create().

◆ PLy_procedure_get()

PLyProcedureCache * PLy_procedure_get ( FunctionCallInfo  fcinfo,
bool  forValidator 
)
extern

Definition at line 62 of file plpy_procedure.c.

63{
64 FmgrInfo *finfo = fcinfo->flinfo;
66 PLyProcedure *proc;
67
68 /*
69 * If this is the first execution for this FmgrInfo, set up a cache struct
70 * (initially containing null pointers). The cache must live as long as
71 * the FmgrInfo, so it goes in fn_mcxt. Also set up a memory context
72 * callback that will be invoked when fn_mcxt is reset/deleted.
73 */
74 pcache = finfo->fn_extra;
75 if (pcache == NULL)
76 {
79
80 pcache->fcontext = finfo->fn_mcxt;
82 pcache->mcb.arg = pcache;
83
85
86 finfo->fn_extra = pcache;
87 }
88
89 /*
90 * If we are resuming execution of a set-returning function, just keep
91 * using the same cache. We do not ask funccache.c to re-validate the
92 * PLyProcedure: we want to run to completion using the function's initial
93 * definition.
94 *
95 * A live iterator (srfstate->iter != NULL) reliably means a genuine
96 * resume: when an iteration ends for any reason, srfstate->iter is reset
97 * to NULL (see comments for PLy_function_cleanup_srfstate).
98 */
99 if (pcache->srfstate != NULL && pcache->srfstate->iter != NULL)
100 {
101 Assert(pcache->proc != NULL);
102 return pcache;
103 }
104
105 /*
106 * Look up, or re-validate, the long-lived hash entry. Like SQL-language
107 * functions, make the hash key depend on the result of
108 * get_call_result_type() when that's composite, so that we can safely
109 * assume that we'll build a new hash entry if the composite rowtype
110 * changes.
111 */
112 proc = (PLyProcedure *)
114 (CachedFunction *) pcache->proc,
117 sizeof(PLyProcedure),
118 true,
120
121 /*
122 * Install the hash pointer in the PLyProcedureCache, and increment its
123 * use count to reflect that. If cached_function_compile gave us back a
124 * different hash entry than we were using before, we must decrement that
125 * one's use count.
126 */
127 if (proc != pcache->proc)
128 {
129 if (pcache->proc != NULL)
130 {
131 Assert(pcache->proc->cfunc.use_count > 0);
132 pcache->proc->cfunc.use_count--;
133 }
134 pcache->proc = proc;
135 proc->cfunc.use_count++;
136 }
137
138 return pcache;
139}
#define Assert(condition)
Definition c.h:999
CachedFunction * cached_function_compile(FunctionCallInfo fcinfo, CachedFunction *function, CachedFunctionCompileCallback ccallback, CachedFunctionDeleteCallback dcallback, Size cacheEntrySize, bool includeResultType, bool forValidator)
Definition funccache.c:480
void * MemoryContextAllocZero(MemoryContext context, Size size)
Definition mcxt.c:1269
void MemoryContextRegisterResetCallback(MemoryContext context, MemoryContextCallback *cb)
Definition mcxt.c:585
static void PLy_compile_callback(FunctionCallInfo fcinfo, HeapTuple procTup, const CachedFunctionHashKey *hashkey, CachedFunction *cfunc, bool forValidator)
static void PLy_delete_callback(CachedFunction *cfunc)
static void RemovePLyProcedureCache(void *arg)
uint64 use_count
Definition funccache.h:117
void * fn_extra
Definition fmgr.h:64
MemoryContext fn_mcxt
Definition fmgr.h:65
FmgrInfo * flinfo
Definition fmgr.h:87
CachedFunction cfunc

References Assert, cached_function_compile(), PLyProcedure::cfunc, fb(), FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_extra, FmgrInfo::fn_mcxt, MemoryContextAllocZero(), MemoryContextRegisterResetCallback(), PLy_compile_callback(), PLy_delete_callback(), RemovePLyProcedureCache(), and CachedFunction::use_count.

Referenced by plpython3_call_handler(), and plpython3_validator().

◆ PLy_procedure_name()

char * PLy_procedure_name ( PLyProcedure proc)
extern

Definition at line 43 of file plpy_procedure.c.

44{
45 if (proc == NULL)
46 return "<unknown procedure>";
47 return proc->proname;
48}

References fb(), and PLyProcedure::proname.

Referenced by plpython_error_callback(), and PLy_traceback().