PostgreSQL Source Code git master
plpy_procedure.c
Go to the documentation of this file.
1/*
2 * Python procedure manipulation for plpython
3 *
4 * src/pl/plpython/plpy_procedure.c
5 */
6
7#include "postgres.h"
8
10#include "catalog/pg_proc.h"
11#include "catalog/pg_type.h"
12#include "funcapi.h"
13#include "plpy_elog.h"
14#include "plpy_main.h"
15#include "plpy_procedure.h"
16#include "plpython.h"
17#include "utils/builtins.h"
18#include "utils/hsearch.h"
19#include "utils/memutils.h"
20#include "utils/syscache.h"
21
23
24static PLyProcedure *PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger);
25static bool PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup);
26static char *PLy_procedure_munge_source(const char *name, const char *src);
27
28
29void
31{
32 HASHCTL hash_ctl;
33
34 hash_ctl.keysize = sizeof(PLyProcedureKey);
35 hash_ctl.entrysize = sizeof(PLyProcedureEntry);
36 PLy_procedure_cache = hash_create("PL/Python procedures", 32, &hash_ctl,
38}
39
40/*
41 * PLy_procedure_name: get the name of the specified procedure.
42 *
43 * NB: this returns the SQL name, not the internal Python procedure name
44 */
45char *
47{
48 if (proc == NULL)
49 return "<unknown procedure>";
50 return proc->proname;
51}
52
53/*
54 * PLy_procedure_get: returns a cached PLyProcedure, or creates, stores and
55 * returns a new PLyProcedure.
56 *
57 * fn_oid is the OID of the function requested
58 * fn_rel is InvalidOid or the relation this function triggers on
59 * is_trigger denotes whether the function is a trigger function
60 *
61 * The reason that both fn_rel and is_trigger need to be passed is that when
62 * trigger functions get validated we don't know which relation(s) they'll
63 * be used with, so no sensible fn_rel can be passed.
64 */
66PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger)
67{
68 bool use_cache = !(is_trigger && fn_rel == InvalidOid);
69 HeapTuple procTup;
71 PLyProcedureEntry *volatile entry = NULL;
72 PLyProcedure *volatile proc = NULL;
73 bool found = false;
74
75 procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
76 if (!HeapTupleIsValid(procTup))
77 elog(ERROR, "cache lookup failed for function %u", fn_oid);
78
79 /*
80 * Look for the function in the cache, unless we don't have the necessary
81 * information (e.g. during validation). In that case we just don't cache
82 * anything.
83 */
84 if (use_cache)
85 {
86 key.fn_oid = fn_oid;
87 key.fn_rel = fn_rel;
89 proc = entry->proc;
90 }
91
92 PG_TRY();
93 {
94 if (!found)
95 {
96 /* Haven't found it, create a new procedure */
97 proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
98 if (use_cache)
99 entry->proc = proc;
100 }
101 else if (!PLy_procedure_valid(proc, procTup))
102 {
103 /* Found it, but it's invalid, free and reuse the cache entry */
104 entry->proc = NULL;
105 if (proc)
107 proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
108 entry->proc = proc;
109 }
110 /* Found it and it's valid, it's fine to use it */
111 }
112 PG_CATCH();
113 {
114 /* Do not leave an uninitialized entry in the cache */
115 if (use_cache)
117 PG_RE_THROW();
118 }
119 PG_END_TRY();
120
121 ReleaseSysCache(procTup);
122
123 return proc;
124}
125
126/*
127 * Create a new PLyProcedure structure
128 */
129static PLyProcedure *
130PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
131{
132 char procName[NAMEDATALEN + 256];
133 Form_pg_proc procStruct;
134 PLyProcedure *volatile proc;
135 MemoryContext cxt;
136 MemoryContext oldcxt;
137 int rv;
138 char *ptr;
139
140 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
141 rv = snprintf(procName, sizeof(procName),
142 "__plpython_procedure_%s_%u",
143 NameStr(procStruct->proname),
144 fn_oid);
145 if (rv >= sizeof(procName) || rv < 0)
146 elog(ERROR, "procedure name would overrun buffer");
147
148 /* Replace any not-legal-in-Python-names characters with '_' */
149 for (ptr = procName; *ptr; ptr++)
150 {
151 if (!((*ptr >= 'A' && *ptr <= 'Z') ||
152 (*ptr >= 'a' && *ptr <= 'z') ||
153 (*ptr >= '0' && *ptr <= '9')))
154 *ptr = '_';
155 }
156
157 /* Create long-lived context that all procedure info will live in */
159 "PL/Python function",
161
162 oldcxt = MemoryContextSwitchTo(cxt);
163
164 proc = (PLyProcedure *) palloc0(sizeof(PLyProcedure));
165 proc->mcxt = cxt;
166
167 PG_TRY();
168 {
169 Datum protrftypes_datum;
170 Datum prosrcdatum;
171 bool isnull;
172 char *procSource;
173 int i;
174
175 proc->proname = pstrdup(NameStr(procStruct->proname));
177 proc->pyname = pstrdup(procName);
178 proc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
179 proc->fn_tid = procTup->t_self;
180 proc->fn_readonly = (procStruct->provolatile != PROVOLATILE_VOLATILE);
181 proc->is_setof = procStruct->proretset;
182 proc->is_procedure = (procStruct->prokind == PROKIND_PROCEDURE);
183 proc->is_trigger = is_trigger;
184 proc->src = NULL;
185 proc->argnames = NULL;
186 proc->args = NULL;
187 proc->nargs = 0;
188 proc->langid = procStruct->prolang;
189 protrftypes_datum = SysCacheGetAttr(PROCOID, procTup,
190 Anum_pg_proc_protrftypes,
191 &isnull);
192 proc->trftypes = isnull ? NIL : oid_array_to_list(protrftypes_datum);
193 proc->code = NULL;
194 proc->statics = NULL;
195 proc->globals = NULL;
196 proc->calldepth = 0;
197 proc->argstack = NULL;
198
199 /*
200 * get information required for output conversion of the return value,
201 * but only if this isn't a trigger.
202 */
203 if (!is_trigger)
204 {
205 Oid rettype = procStruct->prorettype;
206 HeapTuple rvTypeTup;
207 Form_pg_type rvTypeStruct;
208
209 rvTypeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rettype));
210 if (!HeapTupleIsValid(rvTypeTup))
211 elog(ERROR, "cache lookup failed for type %u", rettype);
212 rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
213
214 /* Disallow pseudotype result, except for void or record */
215 if (rvTypeStruct->typtype == TYPTYPE_PSEUDO)
216 {
217 if (rettype == VOIDOID ||
218 rettype == RECORDOID)
219 /* okay */ ;
220 else if (rettype == TRIGGEROID || rettype == EVENT_TRIGGEROID)
222 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
223 errmsg("trigger functions can only be called as triggers")));
224 else
226 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
227 errmsg("PL/Python functions cannot return type %s",
228 format_type_be(rettype))));
229 }
230
231 /* set up output function for procedure result */
232 PLy_output_setup_func(&proc->result, proc->mcxt,
233 rettype, -1, proc);
234
235 ReleaseSysCache(rvTypeTup);
236 }
237 else
238 {
239 /*
240 * In a trigger function, we use proc->result and proc->result_in
241 * for converting tuples, but we don't yet have enough info to set
242 * them up. PLy_exec_trigger will deal with it.
243 */
244 proc->result.typoid = InvalidOid;
246 }
247
248 /*
249 * Now get information required for input conversion of the
250 * procedure's arguments. Note that we ignore output arguments here.
251 * If the function returns record, those I/O functions will be set up
252 * when the function is first called.
253 */
254 if (procStruct->pronargs)
255 {
256 Oid *types;
257 char **names,
258 *modes;
259 int pos,
260 total;
261
262 /* extract argument type info from the pg_proc tuple */
263 total = get_func_arg_info(procTup, &types, &names, &modes);
264
265 /* count number of in+inout args into proc->nargs */
266 if (modes == NULL)
267 proc->nargs = total;
268 else
269 {
270 /* proc->nargs was initialized to 0 above */
271 for (i = 0; i < total; i++)
272 {
273 if (modes[i] != PROARGMODE_OUT &&
274 modes[i] != PROARGMODE_TABLE)
275 (proc->nargs)++;
276 }
277 }
278
279 /* Allocate arrays for per-input-argument data */
280 proc->argnames = (char **) palloc0(sizeof(char *) * proc->nargs);
281 proc->args = (PLyDatumToOb *) palloc0(sizeof(PLyDatumToOb) * proc->nargs);
282
283 for (i = pos = 0; i < total; i++)
284 {
285 HeapTuple argTypeTup;
286 Form_pg_type argTypeStruct;
287
288 if (modes &&
289 (modes[i] == PROARGMODE_OUT ||
290 modes[i] == PROARGMODE_TABLE))
291 continue; /* skip OUT arguments */
292
293 Assert(types[i] == procStruct->proargtypes.values[pos]);
294
295 argTypeTup = SearchSysCache1(TYPEOID,
297 if (!HeapTupleIsValid(argTypeTup))
298 elog(ERROR, "cache lookup failed for type %u", types[i]);
299 argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
300
301 /* disallow pseudotype arguments */
302 if (argTypeStruct->typtype == TYPTYPE_PSEUDO)
304 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
305 errmsg("PL/Python functions cannot accept type %s",
307
308 /* set up I/O function info */
309 PLy_input_setup_func(&proc->args[pos], proc->mcxt,
310 types[i], -1, /* typmod not known */
311 proc);
312
313 /* get argument name */
314 proc->argnames[pos] = names ? pstrdup(names[i]) : NULL;
315
316 ReleaseSysCache(argTypeTup);
317
318 pos++;
319 }
320 }
321
322 /*
323 * get the text of the function.
324 */
325 prosrcdatum = SysCacheGetAttrNotNull(PROCOID, procTup,
326 Anum_pg_proc_prosrc);
327 procSource = TextDatumGetCString(prosrcdatum);
328
329 PLy_procedure_compile(proc, procSource);
330
331 pfree(procSource);
332 }
333 PG_CATCH();
334 {
335 MemoryContextSwitchTo(oldcxt);
337 PG_RE_THROW();
338 }
339 PG_END_TRY();
340
341 MemoryContextSwitchTo(oldcxt);
342 return proc;
343}
344
345/*
346 * Insert the procedure into the Python interpreter
347 */
348void
349PLy_procedure_compile(PLyProcedure *proc, const char *src)
350{
351 PyObject *crv = NULL;
352 char *msrc;
353
354 proc->globals = PyDict_Copy(PLy_interp_globals);
355
356 /*
357 * SD is private preserved data between calls. GD is global data shared by
358 * all functions
359 */
360 proc->statics = PyDict_New();
361 if (!proc->statics)
362 PLy_elog(ERROR, NULL);
363 PyDict_SetItemString(proc->globals, "SD", proc->statics);
364
365 /*
366 * insert the function code into the interpreter
367 */
368 msrc = PLy_procedure_munge_source(proc->pyname, src);
369 /* Save the mangled source for later inclusion in tracebacks */
370 proc->src = MemoryContextStrdup(proc->mcxt, msrc);
371 crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
372 pfree(msrc);
373
374 if (crv != NULL)
375 {
376 int clen;
377 char call[NAMEDATALEN + 256];
378
379 Py_DECREF(crv);
380
381 /*
382 * compile a call to the function
383 */
384 clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
385 if (clen < 0 || clen >= sizeof(call))
386 elog(ERROR, "string would overflow buffer");
387 proc->code = Py_CompileString(call, "<string>", Py_eval_input);
388 if (proc->code != NULL)
389 return;
390 }
391
392 if (proc->proname)
393 PLy_elog(ERROR, "could not compile PL/Python function \"%s\"",
394 proc->proname);
395 else
396 PLy_elog(ERROR, "could not compile anonymous PL/Python code block");
397}
398
399void
401{
402 Py_XDECREF(proc->code);
403 Py_XDECREF(proc->statics);
404 Py_XDECREF(proc->globals);
406}
407
408/*
409 * Decide whether a cached PLyProcedure struct is still valid
410 */
411static bool
413{
414 if (proc == NULL)
415 return false;
416
417 /* If the pg_proc tuple has changed, it's not valid */
418 if (!(proc->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
419 ItemPointerEquals(&proc->fn_tid, &procTup->t_self)))
420 return false;
421
422 return true;
423}
424
425static char *
426PLy_procedure_munge_source(const char *name, const char *src)
427{
428 char *mrc,
429 *mp;
430 const char *sp;
431 size_t mlen;
432 int plen;
433
434 /*
435 * room for function source and the def statement
436 */
437 mlen = (strlen(src) * 2) + strlen(name) + 16;
438
439 mrc = palloc(mlen);
440 plen = snprintf(mrc, mlen, "def %s():\n\t", name);
441 Assert(plen >= 0 && plen < mlen);
442
443 sp = src;
444 mp = mrc + plen;
445
446 while (*sp != '\0')
447 {
448 if (*sp == '\r' && *(sp + 1) == '\n')
449 sp++;
450
451 if (*sp == '\n' || *sp == '\r')
452 {
453 *mp++ = '\n';
454 *mp++ = '\t';
455 sp++;
456 }
457 else
458 *mp++ = *sp++;
459 }
460 *mp++ = '\n';
461 *mp++ = '\n';
462 *mp = '\0';
463
464 if (mp > (mrc + mlen))
465 elog(FATAL, "buffer overrun in PLy_procedure_munge_source");
466
467 return mrc;
468}
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:700
#define Assert(condition)
Definition: c.h:812
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
struct typedefs * types
Definition: ecpg.c:30
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define PG_RE_THROW()
Definition: elog.h:412
#define FATAL
Definition: elog.h:41
#define PG_TRY(...)
Definition: elog.h:371
#define PG_END_TRY(...)
Definition: elog.h:396
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:381
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
int get_func_arg_info(HeapTuple procTup, Oid **p_argtypes, char ***p_argnames, char **p_argmodes)
Definition: funcapi.c:1379
@ HASH_REMOVE
Definition: hsearch.h:115
@ HASH_ENTER
Definition: hsearch.h:114
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:304
#define GETSTRUCT(TUP)
Definition: htup_details.h:653
int i
Definition: isn.c:72
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:35
#define PLy_elog
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1683
char * pstrdup(const char *in)
Definition: mcxt.c:1696
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc(Size size)
Definition: mcxt.c:1317
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:612
#define AllocSetContextCreate
Definition: memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:160
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define NAMEDATALEN
#define NIL
Definition: pg_list.h:68
List * oid_array_to_list(Datum datum)
Definition: pg_proc.c:1184
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:136
FormData_pg_type * Form_pg_type
Definition: pg_type.h:261
PyObject * PLy_interp_globals
Definition: plpy_main.c:51
char * PLy_procedure_name(PLyProcedure *proc)
void init_procedure_caches(void)
static PLyProcedure * PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
static char * PLy_procedure_munge_source(const char *name, const char *src)
static bool PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup)
static HTAB * PLy_procedure_cache
void PLy_procedure_compile(PLyProcedure *proc, const char *src)
void PLy_procedure_delete(PLyProcedure *proc)
PLyProcedure * PLy_procedure_get(Oid fn_oid, Oid fn_rel, bool is_trigger)
struct PLyProcedureKey PLyProcedureKey
struct PLyProcedureEntry PLyProcedureEntry
void PLy_output_setup_func(PLyObToDatum *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:296
void PLy_input_setup_func(PLyDatumToOb *arg, MemoryContext arg_mcxt, Oid typeOid, int32 typmod, PLyProcedure *proc)
Definition: plpy_typeio.c:418
#define snprintf
Definition: port.h:238
uintptr_t Datum
Definition: postgres.h:64
static Datum ObjectIdGetDatum(Oid X)
Definition: postgres.h:252
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76
Definition: dynahash.c:220
ItemPointerData t_self
Definition: htup.h:65
HeapTupleHeader t_data
Definition: htup.h:68
PLyProcedure * proc
PLyDatumToOb * args
PLyObToDatum result
PyObject * code
PLyDatumToOb result_in
PLySavedArgs * argstack
MemoryContext mcxt
char ** argnames
PyObject * globals
ItemPointerData fn_tid
TransactionId fn_xmin
PyObject * statics
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:269
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:221
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:600
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:631
const char * name