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