PostgreSQL Source Code  git master
plpy_procedure.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/transam.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "plpy_elog.h"
#include "plpy_main.h"
#include "plpy_procedure.h"
#include "plpython.h"
#include "utils/builtins.h"
#include "utils/hsearch.h"
#include "utils/inval.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/syscache.h"
Include dependency graph for plpy_procedure.c:

Go to the source code of this file.

Functions

static PLyProcedurePLy_procedure_create (HeapTuple procTup, Oid fn_oid, bool is_trigger)
 
static bool PLy_procedure_valid (PLyProcedure *proc, HeapTuple procTup)
 
static char * PLy_procedure_munge_source (const char *name, const char *src)
 
void init_procedure_caches (void)
 
char * PLy_procedure_name (PLyProcedure *proc)
 
PLyProcedurePLy_procedure_get (Oid fn_oid, Oid fn_rel, bool is_trigger)
 
void PLy_procedure_compile (PLyProcedure *proc, const char *src)
 
void PLy_procedure_delete (PLyProcedure *proc)
 

Variables

static HTABPLy_procedure_cache = NULL
 

Function Documentation

◆ init_procedure_caches()

void init_procedure_caches ( void  )

Definition at line 33 of file plpy_procedure.c.

34 {
35  HASHCTL hash_ctl;
36 
37  hash_ctl.keysize = sizeof(PLyProcedureKey);
38  hash_ctl.entrysize = sizeof(PLyProcedureEntry);
39  PLy_procedure_cache = hash_create("PL/Python procedures", 32, &hash_ctl,
41 }
HTAB * hash_create(const char *tabname, long nelem, const HASHCTL *info, int flags)
Definition: dynahash.c:352
#define HASH_ELEM
Definition: hsearch.h:95
#define HASH_BLOBS
Definition: hsearch.h:97
static HTAB * PLy_procedure_cache
struct PLyProcedureKey PLyProcedureKey
struct PLyProcedureEntry PLyProcedureEntry
Size keysize
Definition: hsearch.h:75
Size entrysize
Definition: hsearch.h:76

References HASHCTL::entrysize, HASH_BLOBS, hash_create(), HASH_ELEM, HASHCTL::keysize, and PLy_procedure_cache.

Referenced by PLy_initialize().

◆ PLy_procedure_compile()

void PLy_procedure_compile ( PLyProcedure proc,
const char *  src 
)

Definition at line 351 of file plpy_procedure.c.

352 {
353  PyObject *crv = NULL;
354  char *msrc;
355 
356  proc->globals = PyDict_Copy(PLy_interp_globals);
357 
358  /*
359  * SD is private preserved data between calls. GD is global data shared by
360  * all functions
361  */
362  proc->statics = PyDict_New();
363  if (!proc->statics)
364  PLy_elog(ERROR, NULL);
365  PyDict_SetItemString(proc->globals, "SD", proc->statics);
366 
367  /*
368  * insert the function code into the interpreter
369  */
370  msrc = PLy_procedure_munge_source(proc->pyname, src);
371  /* Save the mangled source for later inclusion in tracebacks */
372  proc->src = MemoryContextStrdup(proc->mcxt, msrc);
373  crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
374  pfree(msrc);
375 
376  if (crv != NULL)
377  {
378  int clen;
379  char call[NAMEDATALEN + 256];
380 
381  Py_DECREF(crv);
382 
383  /*
384  * compile a call to the function
385  */
386  clen = snprintf(call, sizeof(call), "%s()", proc->pyname);
387  if (clen < 0 || clen >= sizeof(call))
388  elog(ERROR, "string would overflow buffer");
389  proc->code = Py_CompileString(call, "<string>", Py_eval_input);
390  if (proc->code != NULL)
391  return;
392  }
393 
394  if (proc->proname)
395  PLy_elog(ERROR, "could not compile PL/Python function \"%s\"",
396  proc->proname);
397  else
398  PLy_elog(ERROR, "could not compile anonymous PL/Python code block");
399 }
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:224
#define PLy_elog
void pfree(void *pointer)
Definition: mcxt.c:1520
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1682
#define NAMEDATALEN
PyObject * PLy_interp_globals
Definition: plpy_main.c:51
static char * PLy_procedure_munge_source(const char *name, const char *src)
#define snprintf
Definition: port.h:238
PyObject * code
MemoryContext mcxt
PyObject * globals
PyObject * statics

References PLyProcedure::code, elog, ERROR, 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_create()

static PLyProcedure * PLy_procedure_create ( HeapTuple  procTup,
Oid  fn_oid,
bool  is_trigger 
)
static

Definition at line 133 of file plpy_procedure.c.

134 {
135  char procName[NAMEDATALEN + 256];
136  Form_pg_proc procStruct;
137  PLyProcedure *volatile proc;
138  MemoryContext cxt;
139  MemoryContext oldcxt;
140  int rv;
141  char *ptr;
142 
143  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
144  rv = snprintf(procName, sizeof(procName),
145  "__plpython_procedure_%s_%u",
146  NameStr(procStruct->proname),
147  fn_oid);
148  if (rv >= sizeof(procName) || rv < 0)
149  elog(ERROR, "procedure name would overrun buffer");
150 
151  /* Replace any not-legal-in-Python-names characters with '_' */
152  for (ptr = procName; *ptr; ptr++)
153  {
154  if (!((*ptr >= 'A' && *ptr <= 'Z') ||
155  (*ptr >= 'a' && *ptr <= 'z') ||
156  (*ptr >= '0' && *ptr <= '9')))
157  *ptr = '_';
158  }
159 
160  /* Create long-lived context that all procedure info will live in */
162  "PL/Python function",
164 
165  oldcxt = MemoryContextSwitchTo(cxt);
166 
167  proc = (PLyProcedure *) palloc0(sizeof(PLyProcedure));
168  proc->mcxt = cxt;
169 
170  PG_TRY();
171  {
172  Datum protrftypes_datum;
173  Datum prosrcdatum;
174  bool isnull;
175  char *procSource;
176  int i;
177 
178  proc->proname = pstrdup(NameStr(procStruct->proname));
180  proc->pyname = pstrdup(procName);
181  proc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
182  proc->fn_tid = procTup->t_self;
183  proc->fn_readonly = (procStruct->provolatile != PROVOLATILE_VOLATILE);
184  proc->is_setof = procStruct->proretset;
185  proc->is_procedure = (procStruct->prokind == PROKIND_PROCEDURE);
186  proc->src = NULL;
187  proc->argnames = NULL;
188  proc->args = NULL;
189  proc->nargs = 0;
190  proc->langid = procStruct->prolang;
191  protrftypes_datum = SysCacheGetAttr(PROCOID, procTup,
192  Anum_pg_proc_protrftypes,
193  &isnull);
194  proc->trftypes = isnull ? NIL : oid_array_to_list(protrftypes_datum);
195  proc->code = NULL;
196  proc->statics = NULL;
197  proc->globals = NULL;
198  proc->calldepth = 0;
199  proc->argstack = NULL;
200 
201  /*
202  * get information required for output conversion of the return value,
203  * but only if this isn't a trigger.
204  */
205  if (!is_trigger)
206  {
207  Oid rettype = procStruct->prorettype;
208  HeapTuple rvTypeTup;
209  Form_pg_type rvTypeStruct;
210 
211  rvTypeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rettype));
212  if (!HeapTupleIsValid(rvTypeTup))
213  elog(ERROR, "cache lookup failed for type %u", rettype);
214  rvTypeStruct = (Form_pg_type) GETSTRUCT(rvTypeTup);
215 
216  /* Disallow pseudotype result, except for void or record */
217  if (rvTypeStruct->typtype == TYPTYPE_PSEUDO)
218  {
219  if (rettype == VOIDOID ||
220  rettype == RECORDOID)
221  /* okay */ ;
222  else if (rettype == TRIGGEROID || rettype == EVENT_TRIGGEROID)
223  ereport(ERROR,
224  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
225  errmsg("trigger functions can only be called as triggers")));
226  else
227  ereport(ERROR,
228  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
229  errmsg("PL/Python functions cannot return type %s",
230  format_type_be(rettype))));
231  }
232 
233  /* set up output function for procedure result */
234  PLy_output_setup_func(&proc->result, proc->mcxt,
235  rettype, -1, proc);
236 
237  ReleaseSysCache(rvTypeTup);
238  }
239  else
240  {
241  /*
242  * In a trigger function, we use proc->result and proc->result_in
243  * for converting tuples, but we don't yet have enough info to set
244  * them up. PLy_exec_trigger will deal with it.
245  */
246  proc->result.typoid = InvalidOid;
247  proc->result_in.typoid = InvalidOid;
248  }
249 
250  /*
251  * Now get information required for input conversion of the
252  * procedure's arguments. Note that we ignore output arguments here.
253  * If the function returns record, those I/O functions will be set up
254  * when the function is first called.
255  */
256  if (procStruct->pronargs)
257  {
258  Oid *types;
259  char **names,
260  *modes;
261  int pos,
262  total;
263 
264  /* extract argument type info from the pg_proc tuple */
265  total = get_func_arg_info(procTup, &types, &names, &modes);
266 
267  /* count number of in+inout args into proc->nargs */
268  if (modes == NULL)
269  proc->nargs = total;
270  else
271  {
272  /* proc->nargs was initialized to 0 above */
273  for (i = 0; i < total; i++)
274  {
275  if (modes[i] != PROARGMODE_OUT &&
276  modes[i] != PROARGMODE_TABLE)
277  (proc->nargs)++;
278  }
279  }
280 
281  /* Allocate arrays for per-input-argument data */
282  proc->argnames = (char **) palloc0(sizeof(char *) * proc->nargs);
283  proc->args = (PLyDatumToOb *) palloc0(sizeof(PLyDatumToOb) * proc->nargs);
284 
285  for (i = pos = 0; i < total; i++)
286  {
287  HeapTuple argTypeTup;
288  Form_pg_type argTypeStruct;
289 
290  if (modes &&
291  (modes[i] == PROARGMODE_OUT ||
292  modes[i] == PROARGMODE_TABLE))
293  continue; /* skip OUT arguments */
294 
295  Assert(types[i] == procStruct->proargtypes.values[pos]);
296 
297  argTypeTup = SearchSysCache1(TYPEOID,
299  if (!HeapTupleIsValid(argTypeTup))
300  elog(ERROR, "cache lookup failed for type %u", types[i]);
301  argTypeStruct = (Form_pg_type) GETSTRUCT(argTypeTup);
302 
303  /* disallow pseudotype arguments */
304  if (argTypeStruct->typtype == TYPTYPE_PSEUDO)
305  ereport(ERROR,
306  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
307  errmsg("PL/Python functions cannot accept type %s",
308  format_type_be(types[i]))));
309 
310  /* set up I/O function info */
311  PLy_input_setup_func(&proc->args[pos], proc->mcxt,
312  types[i], -1, /* typmod not known */
313  proc);
314 
315  /* get argument name */
316  proc->argnames[pos] = names ? pstrdup(names[i]) : NULL;
317 
318  ReleaseSysCache(argTypeTup);
319 
320  pos++;
321  }
322  }
323 
324  /*
325  * get the text of the function.
326  */
327  prosrcdatum = SysCacheGetAttrNotNull(PROCOID, procTup,
328  Anum_pg_proc_prosrc);
329  procSource = TextDatumGetCString(prosrcdatum);
330 
331  PLy_procedure_compile(proc, procSource);
332 
333  pfree(procSource);
334  }
335  PG_CATCH();
336  {
337  MemoryContextSwitchTo(oldcxt);
338  PLy_procedure_delete(proc);
339  PG_RE_THROW();
340  }
341  PG_END_TRY();
342 
343  MemoryContextSwitchTo(oldcxt);
344  return proc;
345 }
#define TextDatumGetCString(d)
Definition: builtins.h:98
#define NameStr(name)
Definition: c.h:746
#define Assert(condition)
Definition: c.h:858
struct typedefs * types
Definition: ecpg.c:29
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define PG_RE_THROW()
Definition: elog.h:411
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define PG_CATCH(...)
Definition: elog.h:380
#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
#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:73
char * pstrdup(const char *in)
Definition: mcxt.c:1695
MemoryContext TopMemoryContext
Definition: mcxt.c:149
void * palloc0(Size size)
Definition: mcxt.c:1346
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
#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
void PLy_procedure_compile(PLyProcedure *proc, const char *src)
void PLy_procedure_delete(PLyProcedure *proc)
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
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
MemoryContextSwitchTo(old_ctx)
ItemPointerData t_self
Definition: htup.h:65
HeapTupleHeader t_data
Definition: htup.h:68
PLyDatumToOb * args
PLyObToDatum result
PLyDatumToOb result_in
PLySavedArgs * argstack
char ** argnames
ItemPointerData fn_tid
TransactionId fn_xmin
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:266
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:218
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:479
Datum SysCacheGetAttrNotNull(int cacheId, HeapTuple tup, AttrNumber attributeNumber)
Definition: syscache.c:510

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, PLyProcedure::argnames, PLyProcedure::args, PLyProcedure::argstack, Assert, PLyProcedure::calldepth, PLyProcedure::code, elog, ereport, errcode(), errmsg(), ERROR, PLyProcedure::fn_readonly, PLyProcedure::fn_tid, PLyProcedure::fn_xmin, format_type_be(), get_func_arg_info(), GETSTRUCT, PLyProcedure::globals, HeapTupleHeaderGetRawXmin, HeapTupleIsValid, i, InvalidOid, PLyProcedure::is_procedure, PLyProcedure::is_setof, PLyProcedure::langid, PLyProcedure::mcxt, MemoryContextSetIdentifier(), MemoryContextSwitchTo(), NAMEDATALEN, NameStr, PLyProcedure::nargs, NIL, ObjectIdGetDatum(), oid_array_to_list(), palloc0(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLy_input_setup_func(), PLy_output_setup_func(), PLy_procedure_compile(), PLy_procedure_delete(), PLyProcedure::proname, pstrdup(), PLyProcedure::pyname, ReleaseSysCache(), PLyProcedure::result, PLyProcedure::result_in, SearchSysCache1(), snprintf, PLyProcedure::src, PLyProcedure::statics, SysCacheGetAttr(), SysCacheGetAttrNotNull(), HeapTupleData::t_data, HeapTupleData::t_self, TextDatumGetCString, TopMemoryContext, PLyProcedure::trftypes, types, PLyDatumToOb::typoid, and PLyObToDatum::typoid.

Referenced by PLy_procedure_get().

◆ PLy_procedure_delete()

void PLy_procedure_delete ( PLyProcedure proc)

Definition at line 402 of file plpy_procedure.c.

403 {
404  Py_XDECREF(proc->code);
405  Py_XDECREF(proc->statics);
406  Py_XDECREF(proc->globals);
407  MemoryContextDelete(proc->mcxt);
408 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:454

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

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

◆ PLy_procedure_get()

PLyProcedure* PLy_procedure_get ( Oid  fn_oid,
Oid  fn_rel,
bool  is_trigger 
)

Definition at line 69 of file plpy_procedure.c.

70 {
71  bool use_cache = !(is_trigger && fn_rel == InvalidOid);
72  HeapTuple procTup;
74  PLyProcedureEntry *volatile entry = NULL;
75  PLyProcedure *volatile proc = NULL;
76  bool found = false;
77 
78  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
79  if (!HeapTupleIsValid(procTup))
80  elog(ERROR, "cache lookup failed for function %u", fn_oid);
81 
82  /*
83  * Look for the function in the cache, unless we don't have the necessary
84  * information (e.g. during validation). In that case we just don't cache
85  * anything.
86  */
87  if (use_cache)
88  {
89  key.fn_oid = fn_oid;
90  key.fn_rel = fn_rel;
91  entry = hash_search(PLy_procedure_cache, &key, HASH_ENTER, &found);
92  proc = entry->proc;
93  }
94 
95  PG_TRY();
96  {
97  if (!found)
98  {
99  /* Haven't found it, create a new procedure */
100  proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
101  if (use_cache)
102  entry->proc = proc;
103  }
104  else if (!PLy_procedure_valid(proc, procTup))
105  {
106  /* Found it, but it's invalid, free and reuse the cache entry */
107  entry->proc = NULL;
108  if (proc)
109  PLy_procedure_delete(proc);
110  proc = PLy_procedure_create(procTup, fn_oid, is_trigger);
111  entry->proc = proc;
112  }
113  /* Found it and it's valid, it's fine to use it */
114  }
115  PG_CATCH();
116  {
117  /* Do not leave an uninitialized entry in the cache */
118  if (use_cache)
120  PG_RE_THROW();
121  }
122  PG_END_TRY();
123 
124  ReleaseSysCache(procTup);
125 
126  return proc;
127 }
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:955
@ HASH_REMOVE
Definition: hsearch.h:115
@ HASH_ENTER
Definition: hsearch.h:114
static PLyProcedure * PLy_procedure_create(HeapTuple procTup, Oid fn_oid, bool is_trigger)
static bool PLy_procedure_valid(PLyProcedure *proc, HeapTuple procTup)
PLyProcedure * proc

References elog, ERROR, HASH_ENTER, HASH_REMOVE, hash_search(), HeapTupleIsValid, InvalidOid, sort-test::key, ObjectIdGetDatum(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, PLy_procedure_cache, PLy_procedure_create(), PLy_procedure_delete(), PLy_procedure_valid(), PLyProcedureEntry::proc, ReleaseSysCache(), and SearchSysCache1().

Referenced by plpython3_call_handler(), and plpython3_validator().

◆ PLy_procedure_munge_source()

static char * PLy_procedure_munge_source ( const char *  name,
const char *  src 
)
static

Definition at line 428 of file plpy_procedure.c.

429 {
430  char *mrc,
431  *mp;
432  const char *sp;
433  size_t mlen;
434  int plen;
435 
436  /*
437  * room for function source and the def statement
438  */
439  mlen = (strlen(src) * 2) + strlen(name) + 16;
440 
441  mrc = palloc(mlen);
442  plen = snprintf(mrc, mlen, "def %s():\n\t", name);
443  Assert(plen >= 0 && plen < mlen);
444 
445  sp = src;
446  mp = mrc + plen;
447 
448  while (*sp != '\0')
449  {
450  if (*sp == '\r' && *(sp + 1) == '\n')
451  sp++;
452 
453  if (*sp == '\n' || *sp == '\r')
454  {
455  *mp++ = '\n';
456  *mp++ = '\t';
457  sp++;
458  }
459  else
460  *mp++ = *sp++;
461  }
462  *mp++ = '\n';
463  *mp++ = '\n';
464  *mp = '\0';
465 
466  if (mp > (mrc + mlen))
467  elog(FATAL, "buffer overrun in PLy_procedure_munge_source");
468 
469  return mrc;
470 }
#define FATAL
Definition: elog.h:41
void * palloc(Size size)
Definition: mcxt.c:1316
const char * name

References Assert, elog, FATAL, name, palloc(), and snprintf.

Referenced by PLy_procedure_compile().

◆ PLy_procedure_name()

char* PLy_procedure_name ( PLyProcedure proc)

Definition at line 49 of file plpy_procedure.c.

50 {
51  if (proc == NULL)
52  return "<unknown procedure>";
53  return proc->proname;
54 }

References PLyProcedure::proname.

Referenced by plpython_error_callback(), and PLy_traceback().

◆ PLy_procedure_valid()

static bool PLy_procedure_valid ( PLyProcedure proc,
HeapTuple  procTup 
)
static

Definition at line 414 of file plpy_procedure.c.

415 {
416  if (proc == NULL)
417  return false;
418 
419  /* If the pg_proc tuple has changed, it's not valid */
420  if (!(proc->fn_xmin == HeapTupleHeaderGetRawXmin(procTup->t_data) &&
421  ItemPointerEquals(&proc->fn_tid, &procTup->t_self)))
422  return false;
423 
424  return true;
425 }
bool ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
Definition: itemptr.c:35

References PLyProcedure::fn_tid, PLyProcedure::fn_xmin, HeapTupleHeaderGetRawXmin, ItemPointerEquals(), HeapTupleData::t_data, and HeapTupleData::t_self.

Referenced by PLy_procedure_get().

Variable Documentation

◆ PLy_procedure_cache

HTAB* PLy_procedure_cache = NULL
static

Definition at line 25 of file plpy_procedure.c.

Referenced by init_procedure_caches(), and PLy_procedure_get().