PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
plperl.c File Reference
#include "postgres.h"
#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <unistd.h>
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/pg_language.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_proc_fn.h"
#include "catalog/pg_type.h"
#include "commands/event_trigger.h"
#include "commands/trigger.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
#include "storage/ipc.h"
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/fmgroids.h"
#include "utils/guc.h"
#include "utils/hsearch.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/rel.h"
#include "utils/syscache.h"
#include "utils/typcache.h"
#include "plperl.h"
#include "plperl_helpers.h"
#include "perlchunks.h"
#include "plperl_opmask.h"
Include dependency graph for plperl.c:

Go to the source code of this file.

Data Structures

struct  plperl_interp_desc
 
struct  plperl_proc_desc
 
struct  plperl_proc_key
 
struct  plperl_proc_ptr
 
struct  plperl_call_data
 
struct  plperl_query_desc
 
struct  plperl_query_entry
 
struct  plperl_array_info
 

Macros

#define TEXTDOMAIN   PG_TEXTDOMAIN("plperl")
 
#define increment_prodesc_refcount(prodesc)   ((prodesc)->fn_refcount++)
 
#define decrement_prodesc_refcount(prodesc)
 

Typedefs

typedef struct plperl_interp_desc plperl_interp_desc
 
typedef struct plperl_proc_desc plperl_proc_desc
 
typedef struct plperl_proc_key plperl_proc_key
 
typedef struct plperl_proc_ptr plperl_proc_ptr
 
typedef struct plperl_call_data plperl_call_data
 
typedef struct plperl_query_desc plperl_query_desc
 
typedef struct plperl_query_entry plperl_query_entry
 
typedef struct plperl_array_info plperl_array_info
 

Functions

EXTERN_C void boot_DynaLoader (pTHX_ CV *cv)
 
EXTERN_C void boot_PostgreSQL__InServer__Util (pTHX_ CV *cv)
 
EXTERN_C void boot_PostgreSQL__InServer__SPI (pTHX_ CV *cv)
 
void _PG_init (void)
 
static PerlInterpreter * plperl_init_interp (void)
 
static void plperl_destroy_interp (PerlInterpreter **)
 
static void plperl_fini (int code, Datum arg)
 
static void set_interp_require (bool trusted)
 
static Datum plperl_func_handler (PG_FUNCTION_ARGS)
 
static Datum plperl_trigger_handler (PG_FUNCTION_ARGS)
 
static void plperl_event_trigger_handler (PG_FUNCTION_ARGS)
 
static void free_plperl_function (plperl_proc_desc *prodesc)
 
static plperl_proc_desccompile_plperl_function (Oid fn_oid, bool is_trigger, bool is_event_trigger)
 
static SV * plperl_hash_from_tuple (HeapTuple tuple, TupleDesc tupdesc)
 
static SV * plperl_hash_from_datum (Datum attr)
 
static SV * plperl_ref_from_pg_array (Datum arg, Oid typid)
 
static SV * split_array (plperl_array_info *info, int first, int last, int nest)
 
static SV * make_array_ref (plperl_array_info *info, int first, int last)
 
static SV * get_perl_array_ref (SV *sv)
 
static Datum plperl_sv_to_datum (SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
 
static void _sv_to_datum_finfo (Oid typid, FmgrInfo *finfo, Oid *typioparam)
 
static Datum plperl_array_to_datum (SV *src, Oid typid, int32 typmod)
 
static void array_to_datum_internal (AV *av, ArrayBuildState *astate, int *ndims, int *dims, int cur_depth, Oid arraytypid, Oid elemtypid, int32 typmod, FmgrInfo *finfo, Oid typioparam)
 
static Datum plperl_hash_to_datum (SV *src, TupleDesc td)
 
static void plperl_init_shared_libs (pTHX)
 
static void plperl_trusted_init (void)
 
static void plperl_untrusted_init (void)
 
static HV * plperl_spi_execute_fetch_result (SPITupleTable *, uint64, int)
 
static char * hek2cstr (HE *he)
 
static SV ** hv_store_string (HV *hv, const char *key, SV *val)
 
static SV ** hv_fetch_string (HV *hv, const char *key)
 
static void plperl_create_sub (plperl_proc_desc *desc, char *s, Oid fn_oid)
 
static SV * plperl_call_perl_func (plperl_proc_desc *desc, FunctionCallInfo fcinfo)
 
static void plperl_compile_callback (void *arg)
 
static void plperl_exec_callback (void *arg)
 
static void plperl_inline_callback (void *arg)
 
static char * strip_trailing_ws (const char *msg)
 
static OP * pp_require_safe (pTHX)
 
static void activate_interpreter (plperl_interp_desc *interp_desc)
 
static void select_perl_context (bool trusted)
 
static HeapTuple plperl_build_tuple_result (HV *perlhash, TupleDesc td)
 
char * plperl_sv_to_literal (SV *sv, char *fqtypename)
 
static SV * plperl_trigger_build_args (FunctionCallInfo fcinfo)
 
static SV * plperl_event_trigger_build_args (FunctionCallInfo fcinfo)
 
static HeapTuple plperl_modify_tuple (HV *hvTD, TriggerData *tdata, HeapTuple otup)
 
 PG_FUNCTION_INFO_V1 (plperl_call_handler)
 
Datum plperl_call_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plperl_inline_handler)
 
Datum plperl_inline_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plperl_validator)
 
Datum plperl_validator (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plperlu_call_handler)
 
Datum plperlu_call_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plperlu_inline_handler)
 
Datum plperlu_inline_handler (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (plperlu_validator)
 
Datum plperlu_validator (PG_FUNCTION_ARGS)
 
static SV * plperl_call_perl_trigger_func (plperl_proc_desc *desc, FunctionCallInfo fcinfo, SV *td)
 
static void plperl_call_perl_event_trigger_func (plperl_proc_desc *desc, FunctionCallInfo fcinfo, SV *td)
 
static bool validate_plperl_function (plperl_proc_ptr *proc_ptr, HeapTuple procTup)
 
static void check_spi_usage_allowed (void)
 
HV * plperl_spi_exec (char *query, int limit)
 
void plperl_return_next (SV *sv)
 
SV * plperl_spi_query (char *query)
 
SV * plperl_spi_fetchrow (char *cursor)
 
void plperl_spi_cursor_close (char *cursor)
 
SV * plperl_spi_prepare (char *query, int argc, SV **argv)
 
HV * plperl_spi_exec_prepared (char *query, HV *attr, int argc, SV **argv)
 
SV * plperl_spi_query_prepared (char *query, int argc, SV **argv)
 
void plperl_spi_freeplan (char *query)
 

Variables

 PG_MODULE_MAGIC
 
static HTABplperl_interp_hash = NULL
 
static HTABplperl_proc_hash = NULL
 
static plperl_interp_descplperl_active_interp = NULL
 
static PerlInterpreter * plperl_held_interp = NULL
 
static bool plperl_use_strict = false
 
static char * plperl_on_init = NULL
 
static char * plperl_on_plperl_init = NULL
 
static char * plperl_on_plperlu_init = NULL
 
static bool plperl_ending = false
 
static OP *(* pp_require_orig )(pTHX) = NULL
 
static char plperl_opmask [MAXO]
 
static plperl_call_datacurrent_call_data = NULL
 

Macro Definition Documentation

#define decrement_prodesc_refcount (   prodesc)
Value:
do { \
Assert((prodesc)->fn_refcount > 0); \
if (--((prodesc)->fn_refcount) == 0) \
} while(0)
static void free_plperl_function(plperl_proc_desc *prodesc)
Definition: plperl.c:2611
#define Assert(condition)
Definition: c.h:671

Definition at line 135 of file plperl.c.

Referenced by plperl_call_handler(), and validate_plperl_function().

#define increment_prodesc_refcount (   prodesc)    ((prodesc)->fn_refcount++)
#define TEXTDOMAIN   PG_TEXTDOMAIN("plperl")

Definition at line 47 of file plperl.c.

Referenced by _PG_init().

Typedef Documentation

Function Documentation

void _PG_init ( void  )

Definition at line 365 of file plperl.c.

References DefineCustomBoolVariable(), DefineCustomStringVariable(), EmitWarningsOnPlaceholders(), HASHCTL::entrysize, gettext_noop, HASH_BLOBS, hash_create(), HASH_ELEM, HASHCTL::keysize, NULL, pg_bindtextdomain(), PGC_SIGHUP, PGC_SUSET, PGC_USERSET, plperl_held_interp, plperl_init_interp(), plperl_on_init, plperl_on_plperl_init, plperl_on_plperlu_init, plperl_opmask, plperl_use_strict, and TEXTDOMAIN.

366 {
367  /*
368  * Be sure we do initialization only once.
369  *
370  * If initialization fails due to, e.g., plperl_init_interp() throwing an
371  * exception, then we'll return here on the next usage and the user will
372  * get a rather cryptic: ERROR: attempt to redefine parameter
373  * "plperl.use_strict"
374  */
375  static bool inited = false;
376  HASHCTL hash_ctl;
377 
378  if (inited)
379  return;
380 
381  /*
382  * Support localized messages.
383  */
385 
386  /*
387  * Initialize plperl's GUCs.
388  */
389  DefineCustomBoolVariable("plperl.use_strict",
390  gettext_noop("If true, trusted and untrusted Perl code will be compiled in strict mode."),
391  NULL,
393  false,
394  PGC_USERSET, 0,
395  NULL, NULL, NULL);
396 
397  /*
398  * plperl.on_init is marked PGC_SIGHUP to support the idea that it might
399  * be executed in the postmaster (if plperl is loaded into the postmaster
400  * via shared_preload_libraries). This isn't really right either way,
401  * though.
402  */
403  DefineCustomStringVariable("plperl.on_init",
404  gettext_noop("Perl initialization code to execute when a Perl interpreter is initialized."),
405  NULL,
407  NULL,
408  PGC_SIGHUP, 0,
409  NULL, NULL, NULL);
410 
411  /*
412  * plperl.on_plperl_init is marked PGC_SUSET to avoid issues whereby a
413  * user who might not even have USAGE privilege on the plperl language
414  * could nonetheless use SET plperl.on_plperl_init='...' to influence the
415  * behaviour of any existing plperl function that they can execute (which
416  * might be SECURITY DEFINER, leading to a privilege escalation). See
417  * http://archives.postgresql.org/pgsql-hackers/2010-02/msg00281.php and
418  * the overall thread.
419  *
420  * Note that because plperl.use_strict is USERSET, a nefarious user could
421  * set it to be applied against other people's functions. This is judged
422  * OK since the worst result would be an error. Your code oughta pass
423  * use_strict anyway ;-)
424  */
425  DefineCustomStringVariable("plperl.on_plperl_init",
426  gettext_noop("Perl initialization code to execute once when plperl is first used."),
427  NULL,
429  NULL,
430  PGC_SUSET, 0,
431  NULL, NULL, NULL);
432 
433  DefineCustomStringVariable("plperl.on_plperlu_init",
434  gettext_noop("Perl initialization code to execute once when plperlu is first used."),
435  NULL,
437  NULL,
438  PGC_SUSET, 0,
439  NULL, NULL, NULL);
440 
441  EmitWarningsOnPlaceholders("plperl");
442 
443  /*
444  * Create hash tables.
445  */
446  memset(&hash_ctl, 0, sizeof(hash_ctl));
447  hash_ctl.keysize = sizeof(Oid);
448  hash_ctl.entrysize = sizeof(plperl_interp_desc);
449  plperl_interp_hash = hash_create("PL/Perl interpreters",
450  8,
451  &hash_ctl,
453 
454  memset(&hash_ctl, 0, sizeof(hash_ctl));
455  hash_ctl.keysize = sizeof(plperl_proc_key);
456  hash_ctl.entrysize = sizeof(plperl_proc_ptr);
457  plperl_proc_hash = hash_create("PL/Perl procedures",
458  32,
459  &hash_ctl,
461 
462  /*
463  * Save the default opmask.
464  */
465  PLPERL_SET_OPMASK(plperl_opmask);
466 
467  /*
468  * Create the first Perl interpreter, but only partially initialize it.
469  */
471 
472  inited = true;
473 }
static char plperl_opmask[MAXO]
Definition: plperl.c:241
static PerlInterpreter * plperl_init_interp(void)
Definition: plperl.c:689
static HTAB * plperl_proc_hash
Definition: plperl.c:227
#define TEXTDOMAIN
Definition: plperl.c:47
#define HASH_ELEM
Definition: hsearch.h:87
static char * plperl_on_init
Definition: plperl.c:235
struct plperl_interp_desc plperl_interp_desc
Size entrysize
Definition: hsearch.h:73
#define gettext_noop(x)
Definition: c.h:139
unsigned int Oid
Definition: postgres_ext.h:31
Definition: guc.h:75
void EmitWarningsOnPlaceholders(const char *className)
Definition: guc.c:7822
Definition: guc.h:72
#define HASH_BLOBS
Definition: hsearch.h:88
void DefineCustomStringVariable(const char *name, const char *short_desc, const char *long_desc, char **valueAddr, const char *bootValue, GucContext context, int flags, GucStringCheckHook check_hook, GucStringAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:7769
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:301
Size keysize
Definition: hsearch.h:72
static char * plperl_on_plperl_init
Definition: plperl.c:236
#define NULL
Definition: c.h:226
static bool plperl_use_strict
Definition: plperl.c:234
static char * plperl_on_plperlu_init
Definition: plperl.c:237
struct plperl_proc_key plperl_proc_key
static PerlInterpreter * plperl_held_interp
Definition: plperl.c:231
static HTAB * plperl_interp_hash
Definition: plperl.c:226
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1488
void DefineCustomBoolVariable(const char *name, const char *short_desc, const char *long_desc, bool *valueAddr, bool bootValue, GucContext context, int flags, GucBoolCheckHook check_hook, GucBoolAssignHook assign_hook, GucShowHook show_hook)
Definition: guc.c:7683
struct plperl_proc_ptr plperl_proc_ptr
static void _sv_to_datum_finfo ( Oid  typid,
FmgrInfo finfo,
Oid typioparam 
)
static

Definition at line 1249 of file plperl.c.

References fmgr_info(), and getTypeInputInfo().

Referenced by plperl_array_to_datum(), and plperl_sv_to_datum().

1250 {
1251  Oid typinput;
1252 
1253  /* XXX would be better to cache these lookups */
1254  getTypeInputInfo(typid,
1255  &typinput, typioparam);
1256  fmgr_info(typinput, finfo);
1257 }
unsigned int Oid
Definition: postgres_ext.h:31
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2567
static void activate_interpreter ( plperl_interp_desc interp_desc)
static

Definition at line 668 of file plperl.c.

References Assert, plperl_interp_desc::interp, OidIsValid, set_interp_require(), and plperl_interp_desc::user_id.

Referenced by compile_plperl_function(), free_plperl_function(), plperl_call_handler(), plperl_event_trigger_handler(), plperl_fini(), plperl_func_handler(), plperl_inline_handler(), plperl_trigger_handler(), and select_perl_context().

669 {
670  if (interp_desc && plperl_active_interp != interp_desc)
671  {
672  Assert(interp_desc->interp);
673  PERL_SET_CONTEXT(interp_desc->interp);
674  /* trusted iff user_id isn't InvalidOid */
675  set_interp_require(OidIsValid(interp_desc->user_id));
676  plperl_active_interp = interp_desc;
677  }
678 }
static void set_interp_require(bool trusted)
Definition: plperl.c:477
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
PerlInterpreter * interp
Definition: plperl.c:92
#define OidIsValid(objectId)
Definition: c.h:534
#define Assert(condition)
Definition: c.h:671
static void array_to_datum_internal ( AV *  av,
ArrayBuildState astate,
int *  ndims,
int *  dims,
int  cur_depth,
Oid  arraytypid,
Oid  elemtypid,
int32  typmod,
FmgrInfo finfo,
Oid  typioparam 
)
static

Definition at line 1132 of file plperl.c.

References accumArrayResult(), CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, FALSE, get_perl_array_ref(), i, MAXDIM, NULL, and plperl_sv_to_datum().

Referenced by plperl_array_to_datum().

1136 {
1137  int i;
1138  int len = av_len(av) + 1;
1139 
1140  for (i = 0; i < len; i++)
1141  {
1142  /* fetch the array element */
1143  SV **svp = av_fetch(av, i, FALSE);
1144 
1145  /* see if this element is an array, if so get that */
1146  SV *sav = svp ? get_perl_array_ref(*svp) : NULL;
1147 
1148  /* multi-dimensional array? */
1149  if (sav)
1150  {
1151  AV *nav = (AV *) SvRV(sav);
1152 
1153  /* dimensionality checks */
1154  if (cur_depth + 1 > MAXDIM)
1155  ereport(ERROR,
1156  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1157  errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
1158  cur_depth + 1, MAXDIM)));
1159 
1160  /* set size when at first element in this level, else compare */
1161  if (i == 0 && *ndims == cur_depth)
1162  {
1163  dims[*ndims] = av_len(nav) + 1;
1164  (*ndims)++;
1165  }
1166  else if (av_len(nav) + 1 != dims[cur_depth])
1167  ereport(ERROR,
1168  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1169  errmsg("multidimensional arrays must have array expressions with matching dimensions")));
1170 
1171  /* recurse to fetch elements of this sub-array */
1172  array_to_datum_internal(nav, astate,
1173  ndims, dims, cur_depth + 1,
1174  arraytypid, elemtypid, typmod,
1175  finfo, typioparam);
1176  }
1177  else
1178  {
1179  Datum dat;
1180  bool isnull;
1181 
1182  /* scalar after some sub-arrays at same level? */
1183  if (*ndims != cur_depth)
1184  ereport(ERROR,
1185  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1186  errmsg("multidimensional arrays must have array expressions with matching dimensions")));
1187 
1188  dat = plperl_sv_to_datum(svp ? *svp : NULL,
1189  elemtypid,
1190  typmod,
1191  NULL,
1192  finfo,
1193  typioparam,
1194  &isnull);
1195 
1196  (void) accumArrayResult(astate, dat, isnull,
1197  elemtypid, CurrentMemoryContext);
1198  }
1199  }
1200 }
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1107
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
#define MAXDIM
Definition: c.h:416
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:218
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
struct @18::@19 av[32]
uintptr_t Datum
Definition: postgres.h:374
#define NULL
Definition: c.h:226
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:4991
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static void array_to_datum_internal(AV *av, ArrayBuildState *astate, int *ndims, int *dims, int cur_depth, Oid arraytypid, Oid elemtypid, int32 typmod, FmgrInfo *finfo, Oid typioparam)
Definition: plperl.c:1132
EXTERN_C void boot_DynaLoader ( pTHX_ CV *  cv)

Referenced by plperl_init_shared_libs().

EXTERN_C void boot_PostgreSQL__InServer__SPI ( pTHX_ CV *  cv)

Referenced by select_perl_context().

EXTERN_C void boot_PostgreSQL__InServer__Util ( pTHX_ CV *  cv)

Referenced by plperl_init_shared_libs().

static void check_spi_usage_allowed ( void  )
static

Definition at line 3015 of file plperl.c.

References plperl_ending.

Referenced by plperl_spi_cursor_close(), plperl_spi_exec(), plperl_spi_exec_prepared(), plperl_spi_execute_fetch_result(), plperl_spi_fetchrow(), plperl_spi_freeplan(), plperl_spi_prepare(), plperl_spi_query(), and plperl_spi_query_prepared().

3016 {
3017  /* see comment in plperl_fini() */
3018  if (plperl_ending)
3019  {
3020  /* simple croak as we don't want to involve PostgreSQL code */
3021  croak("SPI functions can not be used in END blocks");
3022  }
3023 }
static bool plperl_ending
Definition: plperl.c:239
static plperl_proc_desc * compile_plperl_function ( Oid  fn_oid,
bool  is_trigger,
bool  is_event_trigger 
)
static

Definition at line 2629 of file plperl.c.

References activate_interpreter(), ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), Anum_pg_proc_prosrc, Anum_pg_proc_protrftypes, ErrorContextCallback::arg, plperl_proc_desc::arg_arraytype, plperl_proc_desc::arg_is_rowtype, plperl_proc_desc::arg_out_func, ErrorContextCallback::callback, elog, ereport, errcode(), errmsg(), ERROR, error_context_stack, EVTTRIGGEROID, fmgr_info_cxt(), plperl_proc_desc::fn_cxt, plperl_proc_desc::fn_readonly, plperl_proc_desc::fn_refcount, plperl_proc_desc::fn_retisarray, plperl_proc_desc::fn_retisset, plperl_proc_desc::fn_retistuple, plperl_proc_desc::fn_tid, plperl_proc_desc::fn_xmin, format_type_be(), free_plperl_function(), GETSTRUCT, getTypeIOParam(), GetUserId(), HASH_ENTER, HASH_FIND, hash_search(), HeapTupleGetOid, HeapTupleHeaderGetRawXmin, HeapTupleIsValid, i, increment_prodesc_refcount, plperl_proc_desc::interp, InvalidOid, plperl_proc_key::is_trigger, plperl_proc_desc::lang_oid, LANGOID, plperl_proc_desc::lanpltrusted, MemoryContextDelete(), MemoryContextSwitchTo(), NameStr, plperl_proc_desc::nargs, NIL, NULL, ObjectIdGetDatum, oid_array_to_list(), palloc0(), pfree(), PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, plperl_active_interp, plperl_compile_callback(), plperl_create_sub(), ErrorContextCallback::previous, plperl_proc_key::proc_id, plperl_proc_ptr::proc_ptr, PROCOID, plperl_proc_desc::proname, PROVOLATILE_VOLATILE, pstrdup(), RECORDOID, plperl_proc_desc::reference, ReleaseSysCache(), plperl_proc_desc::result_in_func, plperl_proc_desc::result_oid, plperl_proc_desc::result_typioparam, SearchSysCache1, select_perl_context(), SysCacheGetAttr(), HeapTupleData::t_data, HeapTupleData::t_self, TextDatumGetCString, TopMemoryContext, plperl_proc_desc::trftypes, TRIGGEROID, TYPEOID, TYPTYPE_COMPOSITE, TYPTYPE_PSEUDO, plperl_proc_key::user_id, validate_plperl_function(), and VOIDOID.

Referenced by plperl_event_trigger_handler(), plperl_func_handler(), plperl_trigger_handler(), and plperl_validator().

2630 {
2631  HeapTuple procTup;
2632  Form_pg_proc procStruct;
2633  plperl_proc_key proc_key;
2634  plperl_proc_ptr *proc_ptr;
2635  plperl_proc_desc *volatile prodesc = NULL;
2636  volatile MemoryContext proc_cxt = NULL;
2638  ErrorContextCallback plperl_error_context;
2639 
2640  /* We'll need the pg_proc tuple in any case... */
2641  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
2642  if (!HeapTupleIsValid(procTup))
2643  elog(ERROR, "cache lookup failed for function %u", fn_oid);
2644  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
2645 
2646  /*
2647  * Try to find function in plperl_proc_hash. The reason for this
2648  * overcomplicated-seeming lookup procedure is that we don't know whether
2649  * it's plperl or plperlu, and don't want to spend a lookup in pg_language
2650  * to find out.
2651  */
2652  proc_key.proc_id = fn_oid;
2653  proc_key.is_trigger = is_trigger;
2654  proc_key.user_id = GetUserId();
2655  proc_ptr = hash_search(plperl_proc_hash, &proc_key,
2656  HASH_FIND, NULL);
2657  if (validate_plperl_function(proc_ptr, procTup))
2658  {
2659  /* Found valid plperl entry */
2660  ReleaseSysCache(procTup);
2661  return proc_ptr->proc_ptr;
2662  }
2663 
2664  /* If not found or obsolete, maybe it's plperlu */
2665  proc_key.user_id = InvalidOid;
2666  proc_ptr = hash_search(plperl_proc_hash, &proc_key,
2667  HASH_FIND, NULL);
2668  if (validate_plperl_function(proc_ptr, procTup))
2669  {
2670  /* Found valid plperlu entry */
2671  ReleaseSysCache(procTup);
2672  return proc_ptr->proc_ptr;
2673  }
2674 
2675  /************************************************************
2676  * If we haven't found it in the hashtable, we analyze
2677  * the function's arguments and return type and store
2678  * the in-/out-functions in the prodesc block,
2679  * then we load the procedure into the Perl interpreter,
2680  * and last we create a new hashtable entry for it.
2681  ************************************************************/
2682 
2683  /* Set a callback for reporting compilation errors */
2684  plperl_error_context.callback = plperl_compile_callback;
2685  plperl_error_context.previous = error_context_stack;
2686  plperl_error_context.arg = NameStr(procStruct->proname);
2687  error_context_stack = &plperl_error_context;
2688 
2689  PG_TRY();
2690  {
2691  HeapTuple langTup;
2692  HeapTuple typeTup;
2693  Form_pg_language langStruct;
2694  Form_pg_type typeStruct;
2695  Datum protrftypes_datum;
2696  Datum prosrcdatum;
2697  bool isnull;
2698  char *proc_source;
2699  MemoryContext oldcontext;
2700 
2701  /************************************************************
2702  * Allocate a context that will hold all PG data for the procedure.
2703  ************************************************************/
2705  NameStr(procStruct->proname),
2707 
2708  /************************************************************
2709  * Allocate and fill a new procedure description block.
2710  * struct prodesc and subsidiary data must all live in proc_cxt.
2711  ************************************************************/
2712  oldcontext = MemoryContextSwitchTo(proc_cxt);
2713  prodesc = (plperl_proc_desc *) palloc0(sizeof(plperl_proc_desc));
2714  prodesc->proname = pstrdup(NameStr(procStruct->proname));
2715  prodesc->fn_cxt = proc_cxt;
2716  prodesc->fn_refcount = 0;
2717  prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
2718  prodesc->fn_tid = procTup->t_self;
2719  prodesc->nargs = procStruct->pronargs;
2720  prodesc->arg_out_func = (FmgrInfo *) palloc0(prodesc->nargs * sizeof(FmgrInfo));
2721  prodesc->arg_is_rowtype = (bool *) palloc0(prodesc->nargs * sizeof(bool));
2722  prodesc->arg_arraytype = (Oid *) palloc0(prodesc->nargs * sizeof(Oid));
2723  MemoryContextSwitchTo(oldcontext);
2724 
2725  /* Remember if function is STABLE/IMMUTABLE */
2726  prodesc->fn_readonly =
2727  (procStruct->provolatile != PROVOLATILE_VOLATILE);
2728 
2729  /* Fetch protrftypes */
2730  protrftypes_datum = SysCacheGetAttr(PROCOID, procTup,
2731  Anum_pg_proc_protrftypes, &isnull);
2732  MemoryContextSwitchTo(proc_cxt);
2733  prodesc->trftypes = isnull ? NIL : oid_array_to_list(protrftypes_datum);
2734  MemoryContextSwitchTo(oldcontext);
2735 
2736  /************************************************************
2737  * Lookup the pg_language tuple by Oid
2738  ************************************************************/
2739  langTup = SearchSysCache1(LANGOID,
2740  ObjectIdGetDatum(procStruct->prolang));
2741  if (!HeapTupleIsValid(langTup))
2742  elog(ERROR, "cache lookup failed for language %u",
2743  procStruct->prolang);
2744  langStruct = (Form_pg_language) GETSTRUCT(langTup);
2745  prodesc->lang_oid = HeapTupleGetOid(langTup);
2746  prodesc->lanpltrusted = langStruct->lanpltrusted;
2747  ReleaseSysCache(langTup);
2748 
2749  /************************************************************
2750  * Get the required information for input conversion of the
2751  * return value.
2752  ************************************************************/
2753  if (!is_trigger && !is_event_trigger)
2754  {
2755  typeTup =
2757  ObjectIdGetDatum(procStruct->prorettype));
2758  if (!HeapTupleIsValid(typeTup))
2759  elog(ERROR, "cache lookup failed for type %u",
2760  procStruct->prorettype);
2761  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2762 
2763  /* Disallow pseudotype result, except VOID or RECORD */
2764  if (typeStruct->typtype == TYPTYPE_PSEUDO)
2765  {
2766  if (procStruct->prorettype == VOIDOID ||
2767  procStruct->prorettype == RECORDOID)
2768  /* okay */ ;
2769  else if (procStruct->prorettype == TRIGGEROID ||
2770  procStruct->prorettype == EVTTRIGGEROID)
2771  ereport(ERROR,
2772  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2773  errmsg("trigger functions can only be called "
2774  "as triggers")));
2775  else
2776  ereport(ERROR,
2777  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2778  errmsg("PL/Perl functions cannot return type %s",
2779  format_type_be(procStruct->prorettype))));
2780  }
2781 
2782  prodesc->result_oid = procStruct->prorettype;
2783  prodesc->fn_retisset = procStruct->proretset;
2784  prodesc->fn_retistuple = (procStruct->prorettype == RECORDOID ||
2785  typeStruct->typtype == TYPTYPE_COMPOSITE);
2786 
2787  prodesc->fn_retisarray =
2788  (typeStruct->typlen == -1 && typeStruct->typelem);
2789 
2790  fmgr_info_cxt(typeStruct->typinput,
2791  &(prodesc->result_in_func),
2792  proc_cxt);
2793  prodesc->result_typioparam = getTypeIOParam(typeTup);
2794 
2795  ReleaseSysCache(typeTup);
2796  }
2797 
2798  /************************************************************
2799  * Get the required information for output conversion
2800  * of all procedure arguments
2801  ************************************************************/
2802  if (!is_trigger && !is_event_trigger)
2803  {
2804  int i;
2805 
2806  for (i = 0; i < prodesc->nargs; i++)
2807  {
2808  typeTup = SearchSysCache1(TYPEOID,
2809  ObjectIdGetDatum(procStruct->proargtypes.values[i]));
2810  if (!HeapTupleIsValid(typeTup))
2811  elog(ERROR, "cache lookup failed for type %u",
2812  procStruct->proargtypes.values[i]);
2813  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2814 
2815  /* Disallow pseudotype argument */
2816  if (typeStruct->typtype == TYPTYPE_PSEUDO &&
2817  procStruct->proargtypes.values[i] != RECORDOID)
2818  ereport(ERROR,
2819  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2820  errmsg("PL/Perl functions cannot accept type %s",
2821  format_type_be(procStruct->proargtypes.values[i]))));
2822 
2823  if (typeStruct->typtype == TYPTYPE_COMPOSITE ||
2824  procStruct->proargtypes.values[i] == RECORDOID)
2825  prodesc->arg_is_rowtype[i] = true;
2826  else
2827  {
2828  prodesc->arg_is_rowtype[i] = false;
2829  fmgr_info_cxt(typeStruct->typoutput,
2830  &(prodesc->arg_out_func[i]),
2831  proc_cxt);
2832  }
2833 
2834  /* Identify array attributes */
2835  if (typeStruct->typelem != 0 && typeStruct->typlen == -1)
2836  prodesc->arg_arraytype[i] = procStruct->proargtypes.values[i];
2837  else
2838  prodesc->arg_arraytype[i] = InvalidOid;
2839 
2840  ReleaseSysCache(typeTup);
2841  }
2842  }
2843 
2844  /************************************************************
2845  * create the text of the anonymous subroutine.
2846  * we do not use a named subroutine so that we can call directly
2847  * through the reference.
2848  ************************************************************/
2849  prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
2850  Anum_pg_proc_prosrc, &isnull);
2851  if (isnull)
2852  elog(ERROR, "null prosrc");
2853  proc_source = TextDatumGetCString(prosrcdatum);
2854 
2855  /************************************************************
2856  * Create the procedure in the appropriate interpreter
2857  ************************************************************/
2858 
2860 
2861  prodesc->interp = plperl_active_interp;
2862 
2863  plperl_create_sub(prodesc, proc_source, fn_oid);
2864 
2865  activate_interpreter(oldinterp);
2866 
2867  pfree(proc_source);
2868 
2869  if (!prodesc->reference) /* can this happen? */
2870  elog(ERROR, "could not create PL/Perl internal procedure");
2871 
2872  /************************************************************
2873  * OK, link the procedure into the correct hashtable entry.
2874  * Note we assume that the hashtable entry either doesn't exist yet,
2875  * or we already cleared its proc_ptr during the validation attempts
2876  * above. So no need to decrement an old refcount here.
2877  ************************************************************/
2878  proc_key.user_id = prodesc->lanpltrusted ? GetUserId() : InvalidOid;
2879 
2880  proc_ptr = hash_search(plperl_proc_hash, &proc_key,
2881  HASH_ENTER, NULL);
2882  /* We assume these two steps can't throw an error: */
2883  proc_ptr->proc_ptr = prodesc;
2884  increment_prodesc_refcount(prodesc);
2885  }
2886  PG_CATCH();
2887  {
2888  /*
2889  * If we got as far as creating a reference, we should be able to use
2890  * free_plperl_function() to clean up. If not, then at most we have
2891  * some PG memory resources in proc_cxt, which we can just delete.
2892  */
2893  if (prodesc && prodesc->reference)
2894  free_plperl_function(prodesc);
2895  else if (proc_cxt)
2896  MemoryContextDelete(proc_cxt);
2897 
2898  /* Be sure to restore the previous interpreter, too, for luck */
2899  activate_interpreter(oldinterp);
2900 
2901  PG_RE_THROW();
2902  }
2903  PG_END_TRY();
2904 
2905  /* restore previous error callback */
2906  error_context_stack = plperl_error_context.previous;
2907 
2908  ReleaseSysCache(procTup);
2909 
2910  return prodesc;
2911 }
#define NIL
Definition: pg_list.h:69
Definition: fmgr.h:53
static bool validate_plperl_function(plperl_proc_ptr *proc_ptr, HeapTuple procTup)
Definition: plperl.c:2582
List * trftypes
Definition: plperl.c:117
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
static HTAB * plperl_proc_hash
Definition: plperl.c:227
bool * arg_is_rowtype
Definition: plperl.c:129
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
static void select_perl_context(bool trusted)
Definition: plperl.c:540
Oid GetUserId(void)
Definition: miscinit.c:282
#define TYPTYPE_COMPOSITE
Definition: pg_type.h:709
char * pstrdup(const char *in)
Definition: mcxt.c:1165
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:155
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define Anum_pg_proc_prosrc
Definition: pg_proc.h:115
int errcode(int sqlerrcode)
Definition: elog.c:575
MemoryContext fn_cxt
Definition: plperl.c:109
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
#define Anum_pg_proc_protrftypes
Definition: pg_proc.h:114
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo * arg_out_func
Definition: plperl.c:128
struct ErrorContextCallback * previous
Definition: elog.h:238
#define SearchSysCache1(cacheId, key1)
Definition: syscache.h:149
HeapTupleHeader t_data
Definition: htup.h:67
ErrorContextCallback * error_context_stack
Definition: elog.c:88
List * oid_array_to_list(Datum datum)
Definition: pg_proc.c:1155
void pfree(void *pointer)
Definition: mcxt.c:992
bool fn_retisset
Definition: plperl.c:120
#define VOIDOID
Definition: pg_type.h:678
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
plperl_proc_desc * proc_ptr
Definition: plperl.c:170
ItemPointerData fn_tid
Definition: plperl.c:112
#define TRIGGEROID
Definition: pg_type.h:680
ItemPointerData t_self
Definition: htup.h:65
FmgrInfo result_in_func
Definition: plperl.c:124
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:668
#define RECORDOID
Definition: pg_type.h:668
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:169
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:133
Oid result_typioparam
Definition: plperl.c:125
#define ereport(elevel, rest)
Definition: elog.h:122
Oid * arg_arraytype
Definition: plperl.c:130
MemoryContext TopMemoryContext
Definition: mcxt.c:43
unsigned long fn_refcount
Definition: plperl.c:110
#define PROVOLATILE_VOLATILE
Definition: pg_proc.h:5374
#define TextDatumGetCString(d)
Definition: builtins.h:91
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
Oid is_trigger
Definition: plperl.c:163
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1083
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1245
static void free_plperl_function(plperl_proc_desc *prodesc)
Definition: plperl.c:2611
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:83
#define InvalidOid
Definition: postgres_ext.h:36
static void plperl_create_sub(plperl_proc_desc *desc, char *s, Oid fn_oid)
Definition: plperl.c:2019
bool fn_readonly
Definition: plperl.c:115
#define PG_CATCH()
Definition: elog.h:293
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define EVTTRIGGEROID
Definition: pg_type.h:682
#define NULL
Definition: c.h:226
SV * reference
Definition: plperl.c:113
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:302
#define PG_RE_THROW()
Definition: elog.h:314
#define TYPTYPE_PSEUDO
Definition: pg_type.h:712
static void plperl_compile_callback(void *arg)
Definition: plperl.c:3882
void(* callback)(void *arg)
Definition: elog.h:239
FormData_pg_language * Form_pg_language
Definition: pg_language.h:51
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool fn_retistuple
Definition: plperl.c:119
int i
Oid getTypeIOParam(HeapTuple typeTuple)
Definition: lsyscache.c:2021
#define NameStr(name)
Definition: c.h:495
char * proname
Definition: plperl.c:108
TransactionId fn_xmin
Definition: plperl.c:111
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
#define PG_TRY()
Definition: elog.h:284
plperl_interp_desc * interp
Definition: plperl.c:114
#define PG_END_TRY()
Definition: elog.h:300
bool lanpltrusted
Definition: plperl.c:118
bool fn_retisarray
Definition: plperl.c:121
static void free_plperl_function ( plperl_proc_desc prodesc)
static

Definition at line 2611 of file plperl.c.

References activate_interpreter(), Assert, plperl_proc_desc::fn_cxt, plperl_proc_desc::fn_refcount, plperl_proc_desc::interp, MemoryContextDelete(), plperl_active_interp, and plperl_proc_desc::reference.

Referenced by compile_plperl_function().

2612 {
2613  Assert(prodesc->fn_refcount == 0);
2614  /* Release CODE reference, if we have one, from the appropriate interp */
2615  if (prodesc->reference)
2616  {
2618 
2619  activate_interpreter(prodesc->interp);
2620  SvREFCNT_dec(prodesc->reference);
2621  activate_interpreter(oldinterp);
2622  }
2623  /* Release all PG-owned data for this proc */
2624  MemoryContextDelete(prodesc->fn_cxt);
2625 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
MemoryContext fn_cxt
Definition: plperl.c:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:668
unsigned long fn_refcount
Definition: plperl.c:110
#define Assert(condition)
Definition: c.h:671
SV * reference
Definition: plperl.c:113
plperl_interp_desc * interp
Definition: plperl.c:114
static SV * get_perl_array_ref ( SV *  sv)
static

Definition at line 1107 of file plperl.c.

References elog, ERROR, hv_fetch_string(), and NULL.

Referenced by array_to_datum_internal(), plperl_func_handler(), and plperl_sv_to_datum().

1108 {
1109  if (SvOK(sv) && SvROK(sv))
1110  {
1111  if (SvTYPE(SvRV(sv)) == SVt_PVAV)
1112  return sv;
1113  else if (sv_isa(sv, "PostgreSQL::InServer::ARRAY"))
1114  {
1115  HV *hv = (HV *) SvRV(sv);
1116  SV **sav = hv_fetch_string(hv, "array");
1117 
1118  if (*sav && SvOK(*sav) && SvROK(*sav) &&
1119  SvTYPE(SvRV(*sav)) == SVt_PVAV)
1120  return *sav;
1121 
1122  elog(ERROR, "could not get array reference from PostgreSQL::InServer::ARRAY object");
1123  }
1124  }
1125  return NULL;
1126 }
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:3848
#define ERROR
Definition: elog.h:43
#define NULL
Definition: c.h:226
#define elog
Definition: elog.h:219
static char * hek2cstr ( HE *  he)
static

Definition at line 309 of file plperl.c.

References HeUTF8, and sv2cstr().

Referenced by plperl_build_tuple_result(), and plperl_modify_tuple().

310 {
311  char *ret;
312  SV *sv;
313 
314  /*
315  * HeSVKEY_force will return a temporary mortal SV*, so we need to make
316  * sure to free it with ENTER/SAVE/FREE/LEAVE
317  */
318  ENTER;
319  SAVETMPS;
320 
321  /*-------------------------
322  * Unfortunately, while HeUTF8 is true for most things > 256, for values
323  * 128..255 it's not, but perl will treat them as unicode code points if
324  * the utf8 flag is not set ( see The "Unicode Bug" in perldoc perlunicode
325  * for more)
326  *
327  * So if we did the expected:
328  * if (HeUTF8(he))
329  * utf_u2e(key...);
330  * else // must be ascii
331  * return HePV(he);
332  * we won't match columns with codepoints from 128..255
333  *
334  * For a more concrete example given a column with the name of the unicode
335  * codepoint U+00ae (registered sign) and a UTF8 database and the perl
336  * return_next { "\N{U+00ae}=>'text } would always fail as heUTF8 returns
337  * 0 and HePV() would give us a char * with 1 byte contains the decimal
338  * value 174
339  *
340  * Perl has the brains to know when it should utf8 encode 174 properly, so
341  * here we force it into an SV so that perl will figure it out and do the
342  * right thing
343  *-------------------------
344  */
345 
346  sv = HeSVKEY_force(he);
347  if (HeUTF8(he))
348  SvUTF8_on(sv);
349  ret = sv2cstr(sv);
350 
351  /* free sv */
352  FREETMPS;
353  LEAVE;
354 
355  return ret;
356 }
#define HeUTF8(he)
Definition: plperl.h:81
static char * sv2cstr(SV *sv)
static SV ** hv_fetch_string ( HV *  hv,
const char *  key 
)
static

Definition at line 3848 of file plperl.c.

References pfree(), pg_server_to_any(), and PG_UTF8.

Referenced by get_perl_array_ref(), plperl_modify_tuple(), and plperl_spi_exec_prepared().

3849 {
3850  int32 hlen;
3851  char *hkey;
3852  SV **ret;
3853 
3854  hkey = pg_server_to_any(key, strlen(key), PG_UTF8);
3855 
3856  /* See notes in hv_store_string */
3857  hlen = -(int) strlen(hkey);
3858  ret = hv_fetch(hv, hkey, hlen, 0);
3859 
3860  if (hkey != key)
3861  pfree(hkey);
3862 
3863  return ret;
3864 }
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:645
signed int int32
Definition: c.h:253
void pfree(void *pointer)
Definition: mcxt.c:992
static SV ** hv_store_string ( HV *  hv,
const char *  key,
SV *  val 
)
static

Definition at line 3822 of file plperl.c.

References pfree(), pg_server_to_any(), and PG_UTF8.

Referenced by plperl_create_sub(), plperl_event_trigger_build_args(), plperl_hash_from_tuple(), plperl_spi_execute_fetch_result(), and plperl_trigger_build_args().

3823 {
3824  int32 hlen;
3825  char *hkey;
3826  SV **ret;
3827 
3828  hkey = pg_server_to_any(key, strlen(key), PG_UTF8);
3829 
3830  /*
3831  * hv_store() recognizes a negative klen parameter as meaning a UTF-8
3832  * encoded key.
3833  */
3834  hlen = -(int) strlen(hkey);
3835  ret = hv_store(hv, hkey, hlen, val, 0);
3836 
3837  if (hkey != key)
3838  pfree(hkey);
3839 
3840  return ret;
3841 }
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:645
signed int int32
Definition: c.h:253
void pfree(void *pointer)
Definition: mcxt.c:992
long val
Definition: informix.c:689
static SV * make_array_ref ( plperl_array_info info,
int  first,
int  last 
)
static

Definition at line 1519 of file plperl.c.

References cstr2sv(), DatumGetPointer, plperl_array_info::elem_is_rowtype, plperl_array_info::elements, FmgrInfo::fn_oid, FunctionCall1, i, newRV_noinc, plperl_array_info::nulls, OutputFunctionCall(), plperl_hash_from_datum(), plperl_array_info::proc, plperl_array_info::transform_proc, and val.

Referenced by split_array().

1520 {
1521  int i;
1522  AV *result = newAV();
1523 
1524  for (i = first; i < last; i++)
1525  {
1526  if (info->nulls[i])
1527  {
1528  /*
1529  * We can't use &PL_sv_undef here. See "AVs, HVs and undefined
1530  * values" in perlguts.
1531  */
1532  av_push(result, newSV(0));
1533  }
1534  else
1535  {
1536  Datum itemvalue = info->elements[i];
1537 
1538  if (info->transform_proc.fn_oid)
1539  av_push(result, (SV *) DatumGetPointer(FunctionCall1(&info->transform_proc, itemvalue)));
1540  else if (info->elem_is_rowtype)
1541  /* Handle composite type elements */
1542  av_push(result, plperl_hash_from_datum(itemvalue));
1543  else
1544  {
1545  char *val = OutputFunctionCall(&info->proc, itemvalue);
1546 
1547  av_push(result, cstr2sv(val));
1548  }
1549  }
1550  }
1551  return newRV_noinc((SV *) result);
1552 }
FmgrInfo transform_proc
Definition: plperl.c:219
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2915
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1926
#define newRV_noinc(a)
Definition: ppport.h:4456
uintptr_t Datum
Definition: postgres.h:374
bool elem_is_rowtype
Definition: plperl.c:214
static SV * cstr2sv(const char *str)
Oid fn_oid
Definition: fmgr.h:56
FmgrInfo proc
Definition: plperl.c:218
#define DatumGetPointer(X)
Definition: postgres.h:557
bool * nulls
Definition: plperl.c:216
int i
Datum * elements
Definition: plperl.c:215
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:573
long val
Definition: informix.c:689
PG_FUNCTION_INFO_V1 ( plperl_call_handler  )
PG_FUNCTION_INFO_V1 ( plperl_inline_handler  )
PG_FUNCTION_INFO_V1 ( plperl_validator  )
PG_FUNCTION_INFO_V1 ( plperlu_call_handler  )
PG_FUNCTION_INFO_V1 ( plperlu_inline_handler  )
PG_FUNCTION_INFO_V1 ( plperlu_validator  )
static Datum plperl_array_to_datum ( SV *  src,
Oid  typid,
int32  typmod 
)
static

Definition at line 1206 of file plperl.c.

References _sv_to_datum_finfo(), array_to_datum_internal(), CurrentMemoryContext, ereport, errcode(), errmsg(), ERROR, format_type_be(), get_element_type(), i, initArrayResult(), makeMdArrayResult(), and MAXDIM.

Referenced by plperl_sv_to_datum().

1207 {
1208  ArrayBuildState *astate;
1209  Oid elemtypid;
1210  FmgrInfo finfo;
1211  Oid typioparam;
1212  int dims[MAXDIM];
1213  int lbs[MAXDIM];
1214  int ndims = 1;
1215  int i;
1216 
1217  elemtypid = get_element_type(typid);
1218  if (!elemtypid)
1219  ereport(ERROR,
1220  (errcode(ERRCODE_DATATYPE_MISMATCH),
1221  errmsg("cannot convert Perl array to non-array type %s",
1222  format_type_be(typid))));
1223 
1224  astate = initArrayResult(elemtypid, CurrentMemoryContext, true);
1225 
1226  _sv_to_datum_finfo(elemtypid, &finfo, &typioparam);
1227 
1228  memset(dims, 0, sizeof(dims));
1229  dims[0] = av_len((AV *) SvRV(src)) + 1;
1230 
1231  array_to_datum_internal((AV *) SvRV(src), astate,
1232  &ndims, dims, 1,
1233  typid, elemtypid, typmod,
1234  &finfo, typioparam);
1235 
1236  /* ensure we get zero-D array for no inputs, as per PG convention */
1237  if (dims[0] <= 0)
1238  ndims = 0;
1239 
1240  for (i = 0; i < ndims; i++)
1241  lbs[i] = 1;
1242 
1243  return makeMdArrayResult(astate, ndims, dims, lbs,
1244  CurrentMemoryContext, true);
1245 }
Datum makeMdArrayResult(ArrayBuildState *astate, int ndims, int *dims, int *lbs, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5087
Definition: fmgr.h:53
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:4951
#define MAXDIM
Definition: c.h:416
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2452
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define ereport(elevel, rest)
Definition: elog.h:122
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static void _sv_to_datum_finfo(Oid typid, FmgrInfo *finfo, Oid *typioparam)
Definition: plperl.c:1249
static void array_to_datum_internal(AV *av, ArrayBuildState *astate, int *ndims, int *dims, int cur_depth, Oid arraytypid, Oid elemtypid, int32 typmod, FmgrInfo *finfo, Oid typioparam)
Definition: plperl.c:1132
static HeapTuple plperl_build_tuple_result ( HV *  perlhash,
TupleDesc  td 
)
static

Definition at line 1046 of file plperl.c.

References tupleDesc::attrs, ereport, errcode(), errmsg(), ERROR, heap_form_tuple(), hek2cstr(), InvalidOid, tupleDesc::natts, NULL, palloc(), palloc0(), pfree(), plperl_sv_to_datum(), SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), val, and values.

Referenced by plperl_hash_to_datum(), and plperl_return_next().

1047 {
1048  Datum *values;
1049  bool *nulls;
1050  HE *he;
1051  HeapTuple tup;
1052 
1053  values = palloc0(sizeof(Datum) * td->natts);
1054  nulls = palloc(sizeof(bool) * td->natts);
1055  memset(nulls, true, sizeof(bool) * td->natts);
1056 
1057  hv_iterinit(perlhash);
1058  while ((he = hv_iternext(perlhash)))
1059  {
1060  SV *val = HeVAL(he);
1061  char *key = hek2cstr(he);
1062  int attn = SPI_fnumber(td, key);
1063 
1064  if (attn == SPI_ERROR_NOATTRIBUTE)
1065  ereport(ERROR,
1066  (errcode(ERRCODE_UNDEFINED_COLUMN),
1067  errmsg("Perl hash contains nonexistent column \"%s\"",
1068  key)));
1069  if (attn <= 0)
1070  ereport(ERROR,
1071  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1072  errmsg("cannot set system attribute \"%s\"",
1073  key)));
1074 
1075  values[attn - 1] = plperl_sv_to_datum(val,
1076  td->attrs[attn - 1]->atttypid,
1077  td->attrs[attn - 1]->atttypmod,
1078  NULL,
1079  NULL,
1080  InvalidOid,
1081  &nulls[attn - 1]);
1082 
1083  pfree(key);
1084  }
1085  hv_iterinit(perlhash);
1086 
1087  tup = heap_form_tuple(td, values, nulls);
1088  pfree(values);
1089  pfree(nulls);
1090  return tup;
1091 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:760
static char * hek2cstr(HE *he)
Definition: plperl.c:309
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
static Datum values[MAXATTR]
Definition: bootstrap.c:162
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
long val
Definition: informix.c:689
Datum plperl_call_handler ( PG_FUNCTION_ARGS  )

Definition at line 1762 of file plperl.c.

References activate_interpreter(), CALLED_AS_EVENT_TRIGGER, CALLED_AS_TRIGGER, current_call_data, decrement_prodesc_refcount, plperl_call_data::fcinfo, MemSet, PG_CATCH, PG_END_TRY, PG_RE_THROW, PG_TRY, plperl_active_interp, plperl_event_trigger_handler(), plperl_func_handler(), plperl_trigger_handler(), PointerGetDatum, and plperl_call_data::prodesc.

Referenced by plperlu_call_handler().

1763 {
1764  Datum retval;
1765  plperl_call_data *volatile save_call_data = current_call_data;
1766  plperl_interp_desc *volatile oldinterp = plperl_active_interp;
1767  plperl_call_data this_call_data;
1768 
1769  /* Initialize current-call status record */
1770  MemSet(&this_call_data, 0, sizeof(this_call_data));
1771  this_call_data.fcinfo = fcinfo;
1772 
1773  PG_TRY();
1774  {
1775  current_call_data = &this_call_data;
1776  if (CALLED_AS_TRIGGER(fcinfo))
1777  retval = PointerGetDatum(plperl_trigger_handler(fcinfo));
1778  else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
1779  {
1781  retval = (Datum) 0;
1782  }
1783  else
1784  retval = plperl_func_handler(fcinfo);
1785  }
1786  PG_CATCH();
1787  {
1788  current_call_data = save_call_data;
1789  activate_interpreter(oldinterp);
1790  if (this_call_data.prodesc)
1791  decrement_prodesc_refcount(this_call_data.prodesc);
1792  PG_RE_THROW();
1793  }
1794  PG_END_TRY();
1795 
1796  current_call_data = save_call_data;
1797  activate_interpreter(oldinterp);
1798  if (this_call_data.prodesc)
1799  decrement_prodesc_refcount(this_call_data.prodesc);
1800  return retval;
1801 }
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:40
static Datum plperl_func_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2326
#define decrement_prodesc_refcount(prodesc)
Definition: plperl.c:135
FunctionCallInfo fcinfo
Definition: plperl.c:180
#define PointerGetDatum(X)
Definition: postgres.h:564
#define MemSet(start, val, len)
Definition: c.h:853
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
static void plperl_event_trigger_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2543
static Datum plperl_trigger_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2437
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:668
uintptr_t Datum
Definition: postgres.h:374
#define PG_CATCH()
Definition: elog.h:293
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define PG_RE_THROW()
Definition: elog.h:314
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:244
#define PG_END_TRY()
Definition: elog.h:300
static void plperl_call_perl_event_trigger_func ( plperl_proc_desc desc,
FunctionCallInfo  fcinfo,
SV *  td 
)
static

Definition at line 2264 of file plperl.c.

References ereport, errcode(), errmsg(), ERROR, ERRSV, get_sv, plperl_proc_desc::reference, strip_trailing_ws(), and sv2cstr().

Referenced by plperl_event_trigger_handler().

2267 {
2268  dSP;
2269  SV *retval,
2270  *TDsv;
2271  int count;
2272 
2273  ENTER;
2274  SAVETMPS;
2275 
2276  TDsv = get_sv("main::_TD", 0);
2277  if (!TDsv)
2278  ereport(ERROR,
2279  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2280  errmsg("couldn't fetch $_TD")));
2281 
2282  save_item(TDsv); /* local $_TD */
2283  sv_setsv(TDsv, td);
2284 
2285  PUSHMARK(sp);
2286  PUTBACK;
2287 
2288  /* Do NOT use G_KEEPERR here */
2289  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2290 
2291  SPAGAIN;
2292 
2293  if (count != 1)
2294  {
2295  PUTBACK;
2296  FREETMPS;
2297  LEAVE;
2298  ereport(ERROR,
2299  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2300  errmsg("didn't get a return item from trigger function")));
2301  }
2302 
2303  if (SvTRUE(ERRSV))
2304  {
2305  (void) POPs;
2306  PUTBACK;
2307  FREETMPS;
2308  LEAVE;
2309  /* XXX need to find a way to determine a better errcode here */
2310  ereport(ERROR,
2311  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2313  }
2314 
2315  retval = newSVsv(POPs);
2316  (void) retval; /* silence compiler warning */
2317 
2318  PUTBACK;
2319  FREETMPS;
2320  LEAVE;
2321 
2322  return;
2323 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
SV * reference
Definition: plperl.c:113
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1032
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define get_sv
Definition: ppport.h:3878
static char * sv2cstr(SV *sv)
static SV * plperl_call_perl_func ( plperl_proc_desc desc,
FunctionCallInfo  fcinfo 
)
static

Definition at line 2105 of file plperl.c.

References FunctionCallInfoData::arg, plperl_proc_desc::arg_arraytype, plperl_proc_desc::arg_is_rowtype, plperl_proc_desc::arg_out_func, FunctionCallInfoData::argnull, Assert, cstr2sv(), DatumGetPointer, ereport, errcode(), errmsg(), ERROR, ERRSV, FunctionCallInfoData::flinfo, FmgrInfo::fn_oid, get_func_signature(), get_transform_fromsql(), i, plperl_proc_desc::lang_oid, plperl_proc_desc::nargs, NULL, OidFunctionCall1, OidIsValid, OutputFunctionCall(), pfree(), PL_sv_undef, plperl_hash_from_datum(), plperl_ref_from_pg_array(), plperl_call_data::prodesc, plperl_proc_desc::reference, strip_trailing_ws(), sv2cstr(), and plperl_proc_desc::trftypes.

Referenced by plperl_func_handler(), and plperl_inline_handler().

2106 {
2107  dSP;
2108  SV *retval;
2109  int i;
2110  int count;
2111  Oid *argtypes = NULL;
2112  int nargs = 0;
2113 
2114  ENTER;
2115  SAVETMPS;
2116 
2117  PUSHMARK(SP);
2118  EXTEND(sp, desc->nargs);
2119 
2120  /* Get signature for true functions; inline blocks have no args. */
2121  if (fcinfo->flinfo->fn_oid)
2122  get_func_signature(fcinfo->flinfo->fn_oid, &argtypes, &nargs);
2123  Assert(nargs == desc->nargs);
2124 
2125  for (i = 0; i < desc->nargs; i++)
2126  {
2127  if (fcinfo->argnull[i])
2128  PUSHs(&PL_sv_undef);
2129  else if (desc->arg_is_rowtype[i])
2130  {
2131  SV *sv = plperl_hash_from_datum(fcinfo->arg[i]);
2132 
2133  PUSHs(sv_2mortal(sv));
2134  }
2135  else
2136  {
2137  SV *sv;
2138  Oid funcid;
2139 
2140  if (OidIsValid(desc->arg_arraytype[i]))
2141  sv = plperl_ref_from_pg_array(fcinfo->arg[i], desc->arg_arraytype[i]);
2143  sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, fcinfo->arg[i]));
2144  else
2145  {
2146  char *tmp;
2147 
2148  tmp = OutputFunctionCall(&(desc->arg_out_func[i]),
2149  fcinfo->arg[i]);
2150  sv = cstr2sv(tmp);
2151  pfree(tmp);
2152  }
2153 
2154  PUSHs(sv_2mortal(sv));
2155  }
2156  }
2157  PUTBACK;
2158 
2159  /* Do NOT use G_KEEPERR here */
2160  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2161 
2162  SPAGAIN;
2163 
2164  if (count != 1)
2165  {
2166  PUTBACK;
2167  FREETMPS;
2168  LEAVE;
2169  ereport(ERROR,
2170  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2171  errmsg("didn't get a return item from function")));
2172  }
2173 
2174  if (SvTRUE(ERRSV))
2175  {
2176  (void) POPs;
2177  PUTBACK;
2178  FREETMPS;
2179  LEAVE;
2180  /* XXX need to find a way to determine a better errcode here */
2181  ereport(ERROR,
2182  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2184  }
2185 
2186  retval = newSVsv(POPs);
2187 
2188  PUTBACK;
2189  FREETMPS;
2190  LEAVE;
2191 
2192  return retval;
2193 }
static SV * plperl_ref_from_pg_array(Datum arg, Oid typid)
Definition: plperl.c:1408
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1840
List * trftypes
Definition: plperl.c:117
bool * arg_is_rowtype
Definition: plperl.c:129
Oid get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
Definition: lsyscache.c:1468
#define PL_sv_undef
Definition: ppport.h:4129
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2915
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo * arg_out_func
Definition: plperl.c:128
#define OidIsValid(objectId)
Definition: c.h:534
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1926
FmgrInfo * flinfo
Definition: fmgr.h:71
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:593
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:78
#define ereport(elevel, rest)
Definition: elog.h:122
Oid * arg_arraytype
Definition: plperl.c:130
static SV * cstr2sv(const char *str)
Oid fn_oid
Definition: fmgr.h:56
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
SV * reference
Definition: plperl.c:113
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1032
#define DatumGetPointer(X)
Definition: postgres.h:557
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
plperl_proc_desc * prodesc
Definition: plperl.c:179
static plperl_call_data * current_call_data
Definition: plperl.c:244
static char * sv2cstr(SV *sv)
static SV* plperl_call_perl_trigger_func ( plperl_proc_desc desc,
FunctionCallInfo  fcinfo,
SV *  td 
)
static

Definition at line 2197 of file plperl.c.

References FunctionCallInfoData::context, cstr2sv(), ereport, errcode(), errmsg(), ERROR, ERRSV, get_sv, i, plperl_proc_desc::reference, strip_trailing_ws(), sv2cstr(), Trigger::tgargs, and Trigger::tgnargs.

Referenced by plperl_trigger_handler().

2199 {
2200  dSP;
2201  SV *retval,
2202  *TDsv;
2203  int i,
2204  count;
2205  Trigger *tg_trigger = ((TriggerData *) fcinfo->context)->tg_trigger;
2206 
2207  ENTER;
2208  SAVETMPS;
2209 
2210  TDsv = get_sv("main::_TD", 0);
2211  if (!TDsv)
2212  ereport(ERROR,
2213  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2214  errmsg("couldn't fetch $_TD")));
2215 
2216  save_item(TDsv); /* local $_TD */
2217  sv_setsv(TDsv, td);
2218 
2219  PUSHMARK(sp);
2220  EXTEND(sp, tg_trigger->tgnargs);
2221 
2222  for (i = 0; i < tg_trigger->tgnargs; i++)
2223  PUSHs(sv_2mortal(cstr2sv(tg_trigger->tgargs[i])));
2224  PUTBACK;
2225 
2226  /* Do NOT use G_KEEPERR here */
2227  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2228 
2229  SPAGAIN;
2230 
2231  if (count != 1)
2232  {
2233  PUTBACK;
2234  FREETMPS;
2235  LEAVE;
2236  ereport(ERROR,
2237  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2238  errmsg("didn't get a return item from trigger function")));
2239  }
2240 
2241  if (SvTRUE(ERRSV))
2242  {
2243  (void) POPs;
2244  PUTBACK;
2245  FREETMPS;
2246  LEAVE;
2247  /* XXX need to find a way to determine a better errcode here */
2248  ereport(ERROR,
2249  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2251  }
2252 
2253  retval = newSVsv(POPs);
2254 
2255  PUTBACK;
2256  FREETMPS;
2257  LEAVE;
2258 
2259  return retval;
2260 }
fmNodePtr context
Definition: fmgr.h:72
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
char ** tgargs
Definition: reltrigger.h:40
static SV * cstr2sv(const char *str)
SV * reference
Definition: plperl.c:113
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1032
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define get_sv
Definition: ppport.h:3878
int i
int16 tgnargs
Definition: reltrigger.h:37
static char * sv2cstr(SV *sv)
static void plperl_compile_callback ( void *  arg)
static

Definition at line 3882 of file plperl.c.

References errcontext.

Referenced by compile_plperl_function().

3883 {
3884  char *procname = (char *) arg;
3885 
3886  if (procname)
3887  errcontext("compilation of PL/Perl function \"%s\"", procname);
3888 }
#define errcontext
Definition: elog.h:164
void * arg
static void plperl_create_sub ( plperl_proc_desc desc,
char *  s,
Oid  fn_oid 
)
static

Definition at line 2019 of file plperl.c.

References cstr2sv(), ereport, errcode(), errmsg(), ERROR, ERRSV, hv_store_string(), NAMEDATALEN, newRV_inc, newRV_noinc, NULL, PL_sv_no, plperl_use_strict, plperl_proc_desc::proname, plperl_proc_desc::reference, strip_trailing_ws(), and sv2cstr().

Referenced by compile_plperl_function(), and plperl_inline_handler().

2020 {
2021  dSP;
2022  char subname[NAMEDATALEN + 40];
2023  HV *pragma_hv = newHV();
2024  SV *subref = NULL;
2025  int count;
2026 
2027  sprintf(subname, "%s__%u", prodesc->proname, fn_oid);
2028 
2029  if (plperl_use_strict)
2030  hv_store_string(pragma_hv, "strict", (SV *) newAV());
2031 
2032  ENTER;
2033  SAVETMPS;
2034  PUSHMARK(SP);
2035  EXTEND(SP, 4);
2036  PUSHs(sv_2mortal(cstr2sv(subname)));
2037  PUSHs(sv_2mortal(newRV_noinc((SV *) pragma_hv)));
2038 
2039  /*
2040  * Use 'false' for $prolog in mkfunc, which is kept for compatibility in
2041  * case a module such as PostgreSQL::PLPerl::NYTprof replaces the function
2042  * compiler.
2043  */
2044  PUSHs(&PL_sv_no);
2045  PUSHs(sv_2mortal(cstr2sv(s)));
2046  PUTBACK;
2047 
2048  /*
2049  * G_KEEPERR seems to be needed here, else we don't recognize compile
2050  * errors properly. Perhaps it's because there's another level of eval
2051  * inside mksafefunc?
2052  */
2053  count = perl_call_pv("PostgreSQL::InServer::mkfunc",
2054  G_SCALAR | G_EVAL | G_KEEPERR);
2055  SPAGAIN;
2056 
2057  if (count == 1)
2058  {
2059  SV *sub_rv = (SV *) POPs;
2060 
2061  if (sub_rv && SvROK(sub_rv) && SvTYPE(SvRV(sub_rv)) == SVt_PVCV)
2062  {
2063  subref = newRV_inc(SvRV(sub_rv));
2064  }
2065  }
2066 
2067  PUTBACK;
2068  FREETMPS;
2069  LEAVE;
2070 
2071  if (SvTRUE(ERRSV))
2072  ereport(ERROR,
2073  (errcode(ERRCODE_SYNTAX_ERROR),
2075 
2076  if (!subref)
2077  ereport(ERROR,
2078  (errcode(ERRCODE_SYNTAX_ERROR),
2079  errmsg("didn't get a CODE reference from compiling function \"%s\"",
2080  prodesc->proname)));
2081 
2082  prodesc->reference = subref;
2083 
2084  return;
2085 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:3822
#define NAMEDATALEN
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define newRV_noinc(a)
Definition: ppport.h:4456
#define newRV_inc(sv)
Definition: ppport.h:4442
static SV * cstr2sv(const char *str)
#define NULL
Definition: c.h:226
static bool plperl_use_strict
Definition: plperl.c:234
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1032
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define PL_sv_no
Definition: ppport.h:4128
static char * sv2cstr(SV *sv)
static void plperl_destroy_interp ( PerlInterpreter **  interp)
static

Definition at line 893 of file plperl.c.

References NULL, and PERL_UNUSED_VAR.

Referenced by plperl_fini().

894 {
895  if (interp && *interp)
896  {
897  /*
898  * Only a very minimal destruction is performed: - just call END
899  * blocks.
900  *
901  * We could call perl_destruct() but we'd need to audit its actions
902  * very carefully and work-around any that impact us. (Calling
903  * sv_clean_objs() isn't an option because it's not part of perl's
904  * public API so isn't portably available.) Meanwhile END blocks can
905  * be used to perform manual cleanup.
906  */
907 
908  /* Run END blocks - based on perl's perl_destruct() */
909  if (PL_exit_flags & PERL_EXIT_DESTRUCT_END)
910  {
911  dJMPENV;
912  int x = 0;
913 
914  JMPENV_PUSH(x);
915  PERL_UNUSED_VAR(x);
916  if (PL_endav && !PL_minus_c)
917  call_list(PL_scopestack_ix, PL_endav);
918  JMPENV_POP;
919  }
920  LEAVE;
921  FREETMPS;
922 
923  *interp = NULL;
924  }
925 }
#define PERL_UNUSED_VAR(x)
Definition: ppport.h:3730
#define NULL
Definition: c.h:226
static SV* plperl_event_trigger_build_args ( FunctionCallInfo  fcinfo)
static

Definition at line 1662 of file plperl.c.

References FunctionCallInfoData::context, cstr2sv(), EventTriggerData::event, hv_store_string(), newRV_noinc, and EventTriggerData::tag.

Referenced by plperl_event_trigger_handler().

1663 {
1664  EventTriggerData *tdata;
1665  HV *hv;
1666 
1667  hv = newHV();
1668 
1669  tdata = (EventTriggerData *) fcinfo->context;
1670 
1671  hv_store_string(hv, "event", cstr2sv(tdata->event));
1672  hv_store_string(hv, "tag", cstr2sv(tdata->tag));
1673 
1674  return newRV_noinc((SV *) hv);
1675 }
fmNodePtr context
Definition: fmgr.h:72
const char * tag
Definition: event_trigger.h:28
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:3822
const char * event
Definition: event_trigger.h:26
#define newRV_noinc(a)
Definition: ppport.h:4456
static SV * cstr2sv(const char *str)
static void plperl_event_trigger_handler ( PG_FUNCTION_ARGS  )
static

Definition at line 2543 of file plperl.c.

References activate_interpreter(), ErrorContextCallback::arg, ErrorContextCallback::callback, compile_plperl_function(), elog, ERROR, error_context_stack, increment_prodesc_refcount, plperl_proc_desc::interp, plperl_call_perl_event_trigger_func(), plperl_event_trigger_build_args(), plperl_exec_callback(), ErrorContextCallback::previous, plperl_call_data::prodesc, plperl_proc_desc::proname, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, and SPI_OK_FINISH.

Referenced by plperl_call_handler().

2544 {
2545  plperl_proc_desc *prodesc;
2546  SV *svTD;
2547  ErrorContextCallback pl_error_context;
2548 
2549  /* Connect to SPI manager */
2550  if (SPI_connect() != SPI_OK_CONNECT)
2551  elog(ERROR, "could not connect to SPI manager");
2552 
2553  /* Find or compile the function */
2554  prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false, true);
2555  current_call_data->prodesc = prodesc;
2556  increment_prodesc_refcount(prodesc);
2557 
2558  /* Set a callback for error reporting */
2559  pl_error_context.callback = plperl_exec_callback;
2560  pl_error_context.previous = error_context_stack;
2561  pl_error_context.arg = prodesc->proname;
2562  error_context_stack = &pl_error_context;
2563 
2564  activate_interpreter(prodesc->interp);
2565 
2566  svTD = plperl_event_trigger_build_args(fcinfo);
2567  plperl_call_perl_event_trigger_func(prodesc, fcinfo, svTD);
2568 
2569  if (SPI_finish() != SPI_OK_FINISH)
2570  elog(ERROR, "SPI_finish() failed");
2571 
2572  /* Restore the previous error callback */
2573  error_context_stack = pl_error_context.previous;
2574 
2575  SvREFCNT_dec(svTD);
2576 
2577  return;
2578 }
#define SPI_OK_CONNECT
Definition: spi.h:47
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
struct ErrorContextCallback * previous
Definition: elog.h:238
static void plperl_call_perl_event_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo, SV *td)
Definition: plperl.c:2264
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define ERROR
Definition: elog.h:43
static plperl_proc_desc * compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
Definition: plperl.c:2629
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:668
static void plperl_exec_callback(void *arg)
Definition: plperl.c:3870
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:133
#define SPI_OK_FINISH
Definition: spi.h:48
static SV * plperl_event_trigger_build_args(FunctionCallInfo fcinfo)
Definition: plperl.c:1662
void(* callback)(void *arg)
Definition: elog.h:239
char * proname
Definition: plperl.c:108
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
plperl_interp_desc * interp
Definition: plperl.c:114
static plperl_call_data * current_call_data
Definition: plperl.c:244
static void plperl_exec_callback ( void *  arg)
static

Definition at line 3870 of file plperl.c.

References errcontext.

Referenced by plperl_event_trigger_handler(), plperl_func_handler(), and plperl_trigger_handler().

3871 {
3872  char *procname = (char *) arg;
3873 
3874  if (procname)
3875  errcontext("PL/Perl function \"%s\"", procname);
3876 }
#define errcontext
Definition: elog.h:164
void * arg
static void plperl_fini ( int  code,
Datum  arg 
)
static

Definition at line 496 of file plperl.c.

References activate_interpreter(), DEBUG3, elog, hash_seq_init(), hash_seq_search(), plperl_interp_desc::interp, NULL, plperl_destroy_interp(), plperl_ending, and plperl_held_interp.

Referenced by select_perl_context().

497 {
498  HASH_SEQ_STATUS hash_seq;
499  plperl_interp_desc *interp_desc;
500 
501  elog(DEBUG3, "plperl_fini");
502 
503  /*
504  * Indicate that perl is terminating. Disables use of spi_* functions when
505  * running END/DESTROY code. See check_spi_usage_allowed(). Could be
506  * enabled in future, with care, using a transaction
507  * http://archives.postgresql.org/pgsql-hackers/2010-01/msg02743.php
508  */
509  plperl_ending = true;
510 
511  /* Only perform perl cleanup if we're exiting cleanly */
512  if (code)
513  {
514  elog(DEBUG3, "plperl_fini: skipped");
515  return;
516  }
517 
518  /* Zap the "held" interpreter, if we still have it */
520 
521  /* Zap any fully-initialized interpreters */
522  hash_seq_init(&hash_seq, plperl_interp_hash);
523  while ((interp_desc = hash_seq_search(&hash_seq)) != NULL)
524  {
525  if (interp_desc->interp)
526  {
527  activate_interpreter(interp_desc);
528  plperl_destroy_interp(&interp_desc->interp);
529  }
530  }
531 
532  elog(DEBUG3, "plperl_fini: done");
533 }
static bool plperl_ending
Definition: plperl.c:239
#define DEBUG3
Definition: elog.h:23
PerlInterpreter * interp
Definition: plperl.c:92
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:668
static void plperl_destroy_interp(PerlInterpreter **)
Definition: plperl.c:893
#define NULL
Definition: c.h:226
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1353
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1343
static PerlInterpreter * plperl_held_interp
Definition: plperl.c:231
static HTAB * plperl_interp_hash
Definition: plperl.c:226
#define elog
Definition: elog.h:219
static Datum plperl_func_handler ( PG_FUNCTION_ARGS  )
static

Definition at line 2326 of file plperl.c.

References activate_interpreter(), ReturnSetInfo::allowedModes, ErrorContextCallback::arg, ErrorContextCallback::callback, compile_plperl_function(), elog, ereport, errcode(), errmsg(), ERROR, error_context_stack, ReturnSetInfo::expectedDesc, ExprEndResult, FALSE, plperl_proc_desc::fn_retisset, get_perl_array_ref(), i, increment_prodesc_refcount, plperl_proc_desc::interp, IsA, ReturnSetInfo::isDone, NULL, plperl_call_perl_func(), plperl_exec_callback(), plperl_return_next(), plperl_sv_to_datum(), ErrorContextCallback::previous, plperl_call_data::prodesc, plperl_proc_desc::proname, plperl_proc_desc::result_in_func, plperl_proc_desc::result_oid, plperl_proc_desc::result_typioparam, plperl_call_data::ret_tdesc, ReturnSetInfo::returnMode, ReturnSetInfo::setDesc, ReturnSetInfo::setResult, SFRM_Materialize, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, and plperl_call_data::tuple_store.

Referenced by plperl_call_handler().

2327 {
2328  plperl_proc_desc *prodesc;
2329  SV *perlret;
2330  Datum retval = 0;
2331  ReturnSetInfo *rsi;
2332  ErrorContextCallback pl_error_context;
2333 
2334  if (SPI_connect() != SPI_OK_CONNECT)
2335  elog(ERROR, "could not connect to SPI manager");
2336 
2337  prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false, false);
2338  current_call_data->prodesc = prodesc;
2339  increment_prodesc_refcount(prodesc);
2340 
2341  /* Set a callback for error reporting */
2342  pl_error_context.callback = plperl_exec_callback;
2343  pl_error_context.previous = error_context_stack;
2344  pl_error_context.arg = prodesc->proname;
2345  error_context_stack = &pl_error_context;
2346 
2347  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
2348 
2349  if (prodesc->fn_retisset)
2350  {
2351  /* Check context before allowing the call to go through */
2352  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2353  (rsi->allowedModes & SFRM_Materialize) == 0 ||
2354  rsi->expectedDesc == NULL)
2355  ereport(ERROR,
2356  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2357  errmsg("set-valued function called in context that "
2358  "cannot accept a set")));
2359  }
2360 
2361  activate_interpreter(prodesc->interp);
2362 
2363  perlret = plperl_call_perl_func(prodesc, fcinfo);
2364 
2365  /************************************************************
2366  * Disconnect from SPI manager and then create the return
2367  * values datum (if the input function does a palloc for it
2368  * this must not be allocated in the SPI memory context
2369  * because SPI_finish would free it).
2370  ************************************************************/
2371  if (SPI_finish() != SPI_OK_FINISH)
2372  elog(ERROR, "SPI_finish() failed");
2373 
2374  if (prodesc->fn_retisset)
2375  {
2376  SV *sav;
2377 
2378  /*
2379  * If the Perl function returned an arrayref, we pretend that it
2380  * called return_next() for each element of the array, to handle old
2381  * SRFs that didn't know about return_next(). Any other sort of return
2382  * value is an error, except undef which means return an empty set.
2383  */
2384  sav = get_perl_array_ref(perlret);
2385  if (sav)
2386  {
2387  int i = 0;
2388  SV **svp = 0;
2389  AV *rav = (AV *) SvRV(sav);
2390 
2391  while ((svp = av_fetch(rav, i, FALSE)) != NULL)
2392  {
2393  plperl_return_next(*svp);
2394  i++;
2395  }
2396  }
2397  else if (SvOK(perlret))
2398  {
2399  ereport(ERROR,
2400  (errcode(ERRCODE_DATATYPE_MISMATCH),
2401  errmsg("set-returning PL/Perl function must return "
2402  "reference to array or use return_next")));
2403  }
2404 
2407  {
2410  }
2411  retval = (Datum) 0;
2412  }
2413  else
2414  {
2415  retval = plperl_sv_to_datum(perlret,
2416  prodesc->result_oid,
2417  -1,
2418  fcinfo,
2419  &prodesc->result_in_func,
2420  prodesc->result_typioparam,
2421  &fcinfo->isnull);
2422 
2423  if (fcinfo->isnull && rsi && IsA(rsi, ReturnSetInfo))
2424  rsi->isDone = ExprEndResult;
2425  }
2426 
2427  /* Restore the previous error callback */
2428  error_context_stack = pl_error_context.previous;
2429 
2430  SvREFCNT_dec(perlret);
2431 
2432  return retval;
2433 }
#define SPI_OK_CONNECT
Definition: spi.h:47
#define IsA(nodeptr, _type_)
Definition: nodes.h:559
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1107
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
int errcode(int sqlerrcode)
Definition: elog.c:575
struct ErrorContextCallback * previous
Definition: elog.h:238
ErrorContextCallback * error_context_stack
Definition: elog.c:88
TupleDesc expectedDesc
Definition: execnodes.h:198
bool fn_retisset
Definition: plperl.c:120
#define ERROR
Definition: elog.h:43
#define FALSE
Definition: c.h:218
FmgrInfo result_in_func
Definition: plperl.c:124
static plperl_proc_desc * compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
Definition: plperl.c:2629
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:668
static void plperl_exec_callback(void *arg)
Definition: plperl.c:3870
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:133
Oid result_typioparam
Definition: plperl.c:125
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:374
Tuplestorestate * tuple_store
Definition: plperl.c:181
int allowedModes
Definition: execnodes.h:199
TupleDesc ret_tdesc
Definition: plperl.c:182
SetFunctionReturnMode returnMode
Definition: execnodes.h:201
#define NULL
Definition: c.h:226
void plperl_return_next(SV *sv)
Definition: plperl.c:3141
#define SPI_OK_FINISH
Definition: spi.h:48
Tuplestorestate * setResult
Definition: execnodes.h:204
TupleDesc setDesc
Definition: execnodes.h:205
void(* callback)(void *arg)
Definition: elog.h:239
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
char * proname
Definition: plperl.c:108
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
ExprDoneCond isDone
Definition: execnodes.h:202
plperl_interp_desc * interp
Definition: plperl.c:114
static plperl_call_data * current_call_data
Definition: plperl.c:244
static SV * plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
Definition: plperl.c:2105
static SV * plperl_hash_from_datum ( Datum  attr)
static

Definition at line 2915 of file plperl.c.

References DatumGetHeapTupleHeader, HeapTupleHeaderGetDatumLength, HeapTupleHeaderGetTypeId, HeapTupleHeaderGetTypMod, lookup_rowtype_tupdesc(), plperl_hash_from_tuple(), ReleaseTupleDesc, HeapTupleData::t_data, and HeapTupleData::t_len.

Referenced by make_array_ref(), plperl_call_perl_func(), and plperl_hash_from_tuple().

2916 {
2917  HeapTupleHeader td;
2918  Oid tupType;
2919  int32 tupTypmod;
2920  TupleDesc tupdesc;
2921  HeapTupleData tmptup;
2922  SV *sv;
2923 
2924  td = DatumGetHeapTupleHeader(attr);
2925 
2926  /* Extract rowtype info and find a tupdesc */
2927  tupType = HeapTupleHeaderGetTypeId(td);
2928  tupTypmod = HeapTupleHeaderGetTypMod(td);
2929  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
2930 
2931  /* Build a temporary HeapTuple control structure */
2932  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
2933  tmptup.t_data = td;
2934 
2935  sv = plperl_hash_from_tuple(&tmptup, tupdesc);
2936  ReleaseTupleDesc(tupdesc);
2937 
2938  return sv;
2939 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1245
unsigned int Oid
Definition: postgres_ext.h:31
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:254
signed int int32
Definition: c.h:253
HeapTupleHeader t_data
Definition: htup.h:67
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:455
uint32 t_len
Definition: htup.h:64
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: plperl.c:2943
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:445
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:107
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:439
static SV * plperl_hash_from_tuple ( HeapTuple  tuple,
TupleDesc  tupdesc 
)
static

Definition at line 2943 of file plperl.c.

References tupleDesc::attrs, check_stack_depth(), cstr2sv(), DatumGetPointer, get_base_element_type(), get_transform_fromsql(), getTypeOutputInfo(), heap_getattr, hv_store_string(), i, plperl_proc_desc::lang_oid, NameStr, tupleDesc::natts, newRV_noinc, OidFunctionCall1, OidIsValid, OidOutputFunctionCall(), pfree(), plperl_hash_from_datum(), plperl_ref_from_pg_array(), plperl_call_data::prodesc, plperl_proc_desc::trftypes, and type_is_rowtype().

Referenced by plperl_hash_from_datum(), plperl_spi_execute_fetch_result(), plperl_spi_fetchrow(), and plperl_trigger_build_args().

2944 {
2945  HV *hv;
2946  int i;
2947 
2948  /* since this function recurses, it could be driven to stack overflow */
2950 
2951  hv = newHV();
2952  hv_ksplit(hv, tupdesc->natts); /* pre-grow the hash */
2953 
2954  for (i = 0; i < tupdesc->natts; i++)
2955  {
2956  Datum attr;
2957  bool isnull,
2958  typisvarlena;
2959  char *attname;
2960  Oid typoutput;
2961 
2962  if (tupdesc->attrs[i]->attisdropped)
2963  continue;
2964 
2965  attname = NameStr(tupdesc->attrs[i]->attname);
2966  attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
2967 
2968  if (isnull)
2969  {
2970  /*
2971  * Store (attname => undef) and move on. Note we can't use
2972  * &PL_sv_undef here; see "AVs, HVs and undefined values" in
2973  * perlguts for an explanation.
2974  */
2975  hv_store_string(hv, attname, newSV(0));
2976  continue;
2977  }
2978 
2979  if (type_is_rowtype(tupdesc->attrs[i]->atttypid))
2980  {
2981  SV *sv = plperl_hash_from_datum(attr);
2982 
2983  hv_store_string(hv, attname, sv);
2984  }
2985  else
2986  {
2987  SV *sv;
2988  Oid funcid;
2989 
2990  if (OidIsValid(get_base_element_type(tupdesc->attrs[i]->atttypid)))
2991  sv = plperl_ref_from_pg_array(attr, tupdesc->attrs[i]->atttypid);
2992  else if ((funcid = get_transform_fromsql(tupdesc->attrs[i]->atttypid, current_call_data->prodesc->lang_oid, current_call_data->prodesc->trftypes)))
2993  sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, attr));
2994  else
2995  {
2996  char *outputstr;
2997 
2998  /* XXX should have a way to cache these lookups */
2999  getTypeOutputInfo(tupdesc->attrs[i]->atttypid,
3000  &typoutput, &typisvarlena);
3001 
3002  outputstr = OidOutputFunctionCall(typoutput, attr);
3003  sv = cstr2sv(outputstr);
3004  pfree(outputstr);
3005  }
3006 
3007  hv_store_string(hv, attname, sv);
3008  }
3009  }
3010  return newRV_noinc((SV *) hv);
3011 }
static SV * plperl_ref_from_pg_array(Datum arg, Oid typid)
Definition: plperl.c:1408
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1840
List * trftypes
Definition: plperl.c:117
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2915
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:3822
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:992
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:593
void check_stack_depth(void)
Definition: postgres.c:3096
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2372
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:769
#define newRV_noinc(a)
Definition: ppport.h:4456
uintptr_t Datum
Definition: postgres.h:374
static SV * cstr2sv(const char *str)
#define DatumGetPointer(X)
Definition: postgres.h:557
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2525
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:2006
int i
#define NameStr(name)
Definition: c.h:495
plperl_proc_desc * prodesc
Definition: plperl.c:179
static plperl_call_data * current_call_data
Definition: plperl.c:244
static Datum plperl_hash_to_datum ( SV *  src,
TupleDesc  td 
)
static

Definition at line 1095 of file plperl.c.

References HeapTupleGetDatum, and plperl_build_tuple_result().

Referenced by plperl_sv_to_datum().

1096 {
1097  HeapTuple tup = plperl_build_tuple_result((HV *) SvRV(src), td);
1098 
1099  return HeapTupleGetDatum(tup);
1100 }
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static HeapTuple plperl_build_tuple_result(HV *perlhash, TupleDesc td)
Definition: plperl.c:1046
static PerlInterpreter * plperl_init_interp ( void  )
static

Definition at line 689 of file plperl.c.

References elog, ereport, errcode(), errcontext, errmsg(), ERROR, ERRSV, FloatExceptionHandler(), NULL, PL_ppaddr, plperl_init_shared_libs(), plperl_on_init, plperl_opmask, pp_require_orig, pqsignal(), pstrdup(), strip_trailing_ws(), and sv2cstr().

Referenced by _PG_init(), and select_perl_context().

690 {
691  PerlInterpreter *plperl;
692 
693  static char *embedding[3 + 2] = {
694  "", "-e", PLC_PERLBOOT
695  };
696  int nargs = 3;
697 
698 #ifdef WIN32
699 
700  /*
701  * The perl library on startup does horrible things like call
702  * setlocale(LC_ALL,""). We have protected against that on most platforms
703  * by setting the environment appropriately. However, on Windows,
704  * setlocale() does not consult the environment, so we need to save the
705  * existing locale settings before perl has a chance to mangle them and
706  * restore them after its dirty deeds are done.
707  *
708  * MSDN ref:
709  * http://msdn.microsoft.com/library/en-us/vclib/html/_crt_locale.asp
710  *
711  * It appears that we only need to do this on interpreter startup, and
712  * subsequent calls to the interpreter don't mess with the locale
713  * settings.
714  *
715  * We restore them using setlocale_perl(), defined below, so that Perl
716  * doesn't have a different idea of the locale from Postgres.
717  *
718  */
719 
720  char *loc;
721  char *save_collate,
722  *save_ctype,
723  *save_monetary,
724  *save_numeric,
725  *save_time;
726 
727  loc = setlocale(LC_COLLATE, NULL);
728  save_collate = loc ? pstrdup(loc) : NULL;
729  loc = setlocale(LC_CTYPE, NULL);
730  save_ctype = loc ? pstrdup(loc) : NULL;
731  loc = setlocale(LC_MONETARY, NULL);
732  save_monetary = loc ? pstrdup(loc) : NULL;
733  loc = setlocale(LC_NUMERIC, NULL);
734  save_numeric = loc ? pstrdup(loc) : NULL;
735  loc = setlocale(LC_TIME, NULL);
736  save_time = loc ? pstrdup(loc) : NULL;
737 
738 #define PLPERL_RESTORE_LOCALE(name, saved) \
739  STMT_START { \
740  if (saved != NULL) { setlocale_perl(name, saved); pfree(saved); } \
741  } STMT_END
742 #endif /* WIN32 */
743 
745  {
746  embedding[nargs++] = "-e";
747  embedding[nargs++] = plperl_on_init;
748  }
749 
750  /*
751  * The perl API docs state that PERL_SYS_INIT3 should be called before
752  * allocating interpreters. Unfortunately, on some platforms this fails in
753  * the Perl_do_taint() routine, which is called when the platform is using
754  * the system's malloc() instead of perl's own. Other platforms, notably
755  * Windows, fail if PERL_SYS_INIT3 is not called. So we call it if it's
756  * available, unless perl is using the system malloc(), which is true when
757  * MYMALLOC is set.
758  */
759 #if defined(PERL_SYS_INIT3) && !defined(MYMALLOC)
760  {
761  static int perl_sys_init_done;
762 
763  /* only call this the first time through, as per perlembed man page */
764  if (!perl_sys_init_done)
765  {
766  char *dummy_env[1] = {NULL};
767 
768  PERL_SYS_INIT3(&nargs, (char ***) &embedding, (char ***) &dummy_env);
769 
770  /*
771  * For unclear reasons, PERL_SYS_INIT3 sets the SIGFPE handler to
772  * SIG_IGN. Aside from being extremely unfriendly behavior for a
773  * library, this is dumb on the grounds that the results of a
774  * SIGFPE in this state are undefined according to POSIX, and in
775  * fact you get a forced process kill at least on Linux. Hence,
776  * restore the SIGFPE handler to the backend's standard setting.
777  * (See Perl bug 114574 for more information.)
778  */
780 
781  perl_sys_init_done = 1;
782  /* quiet warning if PERL_SYS_INIT3 doesn't use the third argument */
783  dummy_env[0] = NULL;
784  }
785  }
786 #endif
787 
788  plperl = perl_alloc();
789  if (!plperl)
790  elog(ERROR, "could not allocate Perl interpreter");
791 
792  PERL_SET_CONTEXT(plperl);
793  perl_construct(plperl);
794 
795  /* run END blocks in perl_destruct instead of perl_run */
796  PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
797 
798  /*
799  * Record the original function for the 'require' and 'dofile' opcodes.
800  * (They share the same implementation.) Ensure it's used for new
801  * interpreters.
802  */
803  if (!pp_require_orig)
804  pp_require_orig = PL_ppaddr[OP_REQUIRE];
805  else
806  {
807  PL_ppaddr[OP_REQUIRE] = pp_require_orig;
808  PL_ppaddr[OP_DOFILE] = pp_require_orig;
809  }
810 
811 #ifdef PLPERL_ENABLE_OPMASK_EARLY
812 
813  /*
814  * For regression testing to prove that the PLC_PERLBOOT and PLC_TRUSTED
815  * code doesn't even compile any unsafe ops. In future there may be a
816  * valid need for them to do so, in which case this could be softened
817  * (perhaps moved to plperl_trusted_init()) or removed.
818  */
819  PL_op_mask = plperl_opmask;
820 #endif
821 
822  if (perl_parse(plperl, plperl_init_shared_libs,
823  nargs, embedding, NULL) != 0)
824  ereport(ERROR,
825  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
827  errcontext("while parsing Perl initialization")));
828 
829  if (perl_run(plperl) != 0)
830  ereport(ERROR,
831  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
833  errcontext("while running Perl initialization")));
834 
835 #ifdef PLPERL_RESTORE_LOCALE
836  PLPERL_RESTORE_LOCALE(LC_COLLATE, save_collate);
837  PLPERL_RESTORE_LOCALE(LC_CTYPE, save_ctype);
838  PLPERL_RESTORE_LOCALE(LC_MONETARY, save_monetary);
839  PLPERL_RESTORE_LOCALE(LC_NUMERIC, save_numeric);
840  PLPERL_RESTORE_LOCALE(LC_TIME, save_time);
841 #endif
842 
843  return plperl;
844 }
static char plperl_opmask[MAXO]
Definition: plperl.c:241
#define PL_ppaddr
Definition: ppport.h:4085
static char * plperl_on_init
Definition: plperl.c:235
static void plperl_init_shared_libs(pTHX)
Definition: plperl.c:2093
char * pstrdup(const char *in)
Definition: mcxt.c:1165
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
#define ERROR
Definition: elog.h:43
static OP *(* pp_require_orig)(pTHX)
Definition: plperl.c:240
#define ereport(elevel, rest)
Definition: elog.h:122
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:168
#define NULL
Definition: c.h:226
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1032
int errmsg(const char *fmt,...)
Definition: elog.c:797
void FloatExceptionHandler(SIGNAL_ARGS)
Definition: postgres.c:2669
#define errcontext
Definition: elog.h:164
#define elog
Definition: elog.h:219
static char * sv2cstr(SV *sv)
static void plperl_init_shared_libs ( pTHX  )
static

Definition at line 2093 of file plperl.c.

References boot_DynaLoader(), and boot_PostgreSQL__InServer__Util().

Referenced by plperl_init_interp().

2094 {
2095  char *file = __FILE__;
2096 
2097  newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2098  newXS("PostgreSQL::InServer::Util::bootstrap",
2100  /* newXS for...::SPI::bootstrap is in select_perl_context() */
2101 }
EXTERN_C void boot_DynaLoader(pTHX_ CV *cv)
EXTERN_C void boot_PostgreSQL__InServer__Util(pTHX_ CV *cv)
static void plperl_inline_callback ( void *  arg)
static

Definition at line 3894 of file plperl.c.

References errcontext.

Referenced by plperl_inline_handler().

3895 {
3896  errcontext("PL/Perl anonymous code block");
3897 }
#define errcontext
Definition: elog.h:164
Datum plperl_inline_handler ( PG_FUNCTION_ARGS  )

Definition at line 1809 of file plperl.c.

References activate_interpreter(), ErrorContextCallback::arg, ErrorContextCallback::callback, current_call_data, CurrentMemoryContext, elog, ERROR, error_context_stack, plperl_call_data::fcinfo, FunctionCallInfoData::flinfo, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, plperl_proc_desc::fn_readonly, plperl_proc_desc::fn_retisarray, plperl_proc_desc::fn_retisset, plperl_proc_desc::fn_retistuple, InvalidOid, plperl_proc_desc::lang_oid, InlineCodeBlock::langIsTrusted, InlineCodeBlock::langOid, plperl_proc_desc::lanpltrusted, MemSet, plperl_proc_desc::nargs, NIL, NULL, PG_CATCH, PG_END_TRY, PG_GETARG_POINTER, PG_RE_THROW, PG_RETURN_VOID, PG_TRY, plperl_active_interp, plperl_call_perl_func(), plperl_create_sub(), plperl_inline_callback(), ErrorContextCallback::previous, plperl_call_data::prodesc, plperl_proc_desc::proname, plperl_proc_desc::reference, plperl_proc_desc::result_oid, select_perl_context(), InlineCodeBlock::source_text, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, plperl_proc_desc::trftypes, and VOIDOID.

Referenced by plperlu_inline_handler().

1810 {
1812  FunctionCallInfoData fake_fcinfo;
1813  FmgrInfo flinfo;
1814  plperl_proc_desc desc;
1815  plperl_call_data *volatile save_call_data = current_call_data;
1816  plperl_interp_desc *volatile oldinterp = plperl_active_interp;
1817  plperl_call_data this_call_data;
1818  ErrorContextCallback pl_error_context;
1819 
1820  /* Initialize current-call status record */
1821  MemSet(&this_call_data, 0, sizeof(this_call_data));
1822 
1823  /* Set up a callback for error reporting */
1824  pl_error_context.callback = plperl_inline_callback;
1825  pl_error_context.previous = error_context_stack;
1826  pl_error_context.arg = NULL;
1827  error_context_stack = &pl_error_context;
1828 
1829  /*
1830  * Set up a fake fcinfo and descriptor with just enough info to satisfy
1831  * plperl_call_perl_func(). In particular note that this sets things up
1832  * with no arguments passed, and a result type of VOID.
1833  */
1834  MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
1835  MemSet(&flinfo, 0, sizeof(flinfo));
1836  MemSet(&desc, 0, sizeof(desc));
1837  fake_fcinfo.flinfo = &flinfo;
1838  flinfo.fn_oid = InvalidOid;
1839  flinfo.fn_mcxt = CurrentMemoryContext;
1840 
1841  desc.proname = "inline_code_block";
1842  desc.fn_readonly = false;
1843 
1844  desc.lang_oid = codeblock->langOid;
1845  desc.trftypes = NIL;
1846  desc.lanpltrusted = codeblock->langIsTrusted;
1847 
1848  desc.fn_retistuple = false;
1849  desc.fn_retisset = false;
1850  desc.fn_retisarray = false;
1851  desc.result_oid = VOIDOID;
1852  desc.nargs = 0;
1853  desc.reference = NULL;
1854 
1855  this_call_data.fcinfo = &fake_fcinfo;
1856  this_call_data.prodesc = &desc;
1857  /* we do not bother with refcounting the fake prodesc */
1858 
1859  PG_TRY();
1860  {
1861  SV *perlret;
1862 
1863  current_call_data = &this_call_data;
1864 
1865  if (SPI_connect() != SPI_OK_CONNECT)
1866  elog(ERROR, "could not connect to SPI manager");
1867 
1869 
1870  plperl_create_sub(&desc, codeblock->source_text, 0);
1871 
1872  if (!desc.reference) /* can this happen? */
1873  elog(ERROR, "could not create internal procedure for anonymous code block");
1874 
1875  perlret = plperl_call_perl_func(&desc, &fake_fcinfo);
1876 
1877  SvREFCNT_dec(perlret);
1878 
1879  if (SPI_finish() != SPI_OK_FINISH)
1880  elog(ERROR, "SPI_finish() failed");
1881  }
1882  PG_CATCH();
1883  {
1884  if (desc.reference)
1885  SvREFCNT_dec(desc.reference);
1886  current_call_data = save_call_data;
1887  activate_interpreter(oldinterp);
1888  PG_RE_THROW();
1889  }
1890  PG_END_TRY();
1891 
1892  if (desc.reference)
1893  SvREFCNT_dec(desc.reference);
1894 
1895  current_call_data = save_call_data;
1896  activate_interpreter(oldinterp);
1897 
1898  error_context_stack = pl_error_context.previous;
1899 
1900  PG_RETURN_VOID();
1901 }
#define NIL
Definition: pg_list.h:69
#define SPI_OK_CONNECT
Definition: spi.h:47
Definition: fmgr.h:53
List * trftypes
Definition: plperl.c:117
MemoryContext fn_mcxt
Definition: fmgr.h:62
FunctionCallInfo fcinfo
Definition: plperl.c:180
static void select_perl_context(bool trusted)
Definition: plperl.c:540
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:147
#define MemSet(start, val, len)
Definition: c.h:853
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:232
struct ErrorContextCallback * previous
Definition: elog.h:238
ErrorContextCallback * error_context_stack
Definition: elog.c:88
FmgrInfo * flinfo
Definition: fmgr.h:71
bool fn_retisset
Definition: plperl.c:120
#define VOIDOID
Definition: pg_type.h:678
#define ERROR
Definition: elog.h:43
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:668
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:56
static void plperl_create_sub(plperl_proc_desc *desc, char *s, Oid fn_oid)
Definition: plperl.c:2019
bool fn_readonly
Definition: plperl.c:115
static void plperl_inline_callback(void *arg)
Definition: plperl.c:3894
#define PG_RETURN_VOID()
Definition: fmgr.h:293
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
SV * reference
Definition: plperl.c:113
#define SPI_OK_FINISH
Definition: spi.h:48
#define PG_RE_THROW()
Definition: elog.h:314
void(* callback)(void *arg)
Definition: elog.h:239
bool fn_retistuple
Definition: plperl.c:119
char * proname
Definition: plperl.c:108
char * source_text
Definition: parsenodes.h:2665
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:244
#define PG_END_TRY()
Definition: elog.h:300
bool lanpltrusted
Definition: plperl.c:118
static SV * plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
Definition: plperl.c:2105
bool fn_retisarray
Definition: plperl.c:121
static HeapTuple plperl_modify_tuple ( HV *  hvTD,
TriggerData tdata,
HeapTuple  otup 
)
static

Definition at line 1679 of file plperl.c.

References tupleDesc::attrs, ereport, errcode(), errmsg(), ERROR, heap_modify_tuple(), hek2cstr(), hv_fetch_string(), InvalidOid, tupleDesc::natts, NULL, palloc0(), pfree(), plperl_sv_to_datum(), RelationData::rd_att, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), TriggerData::tg_relation, and val.

Referenced by plperl_trigger_handler().

1680 {
1681  SV **svp;
1682  HV *hvNew;
1683  HE *he;
1684  HeapTuple rtup;
1685  TupleDesc tupdesc;
1686  int natts;
1687  Datum *modvalues;
1688  bool *modnulls;
1689  bool *modrepls;
1690 
1691  svp = hv_fetch_string(hvTD, "new");
1692  if (!svp)
1693  ereport(ERROR,
1694  (errcode(ERRCODE_UNDEFINED_COLUMN),
1695  errmsg("$_TD->{new} does not exist")));
1696  if (!SvOK(*svp) || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV)
1697  ereport(ERROR,
1698  (errcode(ERRCODE_DATATYPE_MISMATCH),
1699  errmsg("$_TD->{new} is not a hash reference")));
1700  hvNew = (HV *) SvRV(*svp);
1701 
1702  tupdesc = tdata->tg_relation->rd_att;
1703  natts = tupdesc->natts;
1704 
1705  modvalues = (Datum *) palloc0(natts * sizeof(Datum));
1706  modnulls = (bool *) palloc0(natts * sizeof(bool));
1707  modrepls = (bool *) palloc0(natts * sizeof(bool));
1708 
1709  hv_iterinit(hvNew);
1710  while ((he = hv_iternext(hvNew)))
1711  {
1712  char *key = hek2cstr(he);
1713  SV *val = HeVAL(he);
1714  int attn = SPI_fnumber(tupdesc, key);
1715 
1716  if (attn == SPI_ERROR_NOATTRIBUTE)
1717  ereport(ERROR,
1718  (errcode(ERRCODE_UNDEFINED_COLUMN),
1719  errmsg("Perl hash contains nonexistent column \"%s\"",
1720  key)));
1721  if (attn <= 0)
1722  ereport(ERROR,
1723  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1724  errmsg("cannot set system attribute \"%s\"",
1725  key)));
1726 
1727  modvalues[attn - 1] = plperl_sv_to_datum(val,
1728  tupdesc->attrs[attn - 1]->atttypid,
1729  tupdesc->attrs[attn - 1]->atttypmod,
1730  NULL,
1731  NULL,
1732  InvalidOid,
1733  &modnulls[attn - 1]);
1734  modrepls[attn - 1] = true;
1735 
1736  pfree(key);
1737  }
1738  hv_iterinit(hvNew);
1739 
1740  rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
1741 
1742  pfree(modvalues);
1743  pfree(modnulls);
1744  pfree(modrepls);
1745 
1746  return rtup;
1747 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:760
static char * hek2cstr(HE *he)
Definition: plperl.c:309
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:3848
int natts
Definition: tupdesc.h:73
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:920
uintptr_t Datum
Definition: postgres.h:374
TupleDesc rd_att
Definition: rel.h:114
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
int errmsg(const char *fmt,...)
Definition: elog.c:797
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:793
long val
Definition: informix.c:689
Relation tg_relation
Definition: trigger.h:34
static SV * plperl_ref_from_pg_array ( Datum  arg,
Oid  typid 
)
static

Definition at line 1408 of file plperl.c.

References ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, av, DatumGetArrayTypeP, deconstruct_array(), plperl_array_info::elem_is_rowtype, plperl_array_info::elements, fmgr_info(), get_transform_fromsql(), get_type_io_data(), i, IOFunc_output, plperl_proc_desc::lang_oid, plperl_array_info::ndims, plperl_array_info::nelems, newRV_noinc, newSVuv, plperl_array_info::nulls, OidIsValid, palloc(), palloc0(), plperl_array_info::proc, plperl_call_data::prodesc, split_array(), plperl_array_info::transform_proc, plperl_proc_desc::trftypes, and type_is_rowtype().

Referenced by plperl_call_perl_func(), and plperl_hash_from_tuple().

1409 {
1411  Oid elementtype = ARR_ELEMTYPE(ar);
1412  int16 typlen;
1413  bool typbyval;
1414  char typalign,
1415  typdelim;
1416  Oid typioparam;
1417  Oid typoutputfunc;
1418  Oid transform_funcid;
1419  int i,
1420  nitems,
1421  *dims;
1422  plperl_array_info *info;
1423  SV *av;
1424  HV *hv;
1425 
1426  /*
1427  * Currently we make no effort to cache any of the stuff we look up here,
1428  * which is bad.
1429  */
1430  info = palloc0(sizeof(plperl_array_info));
1431 
1432  /* get element type information, including output conversion function */
1433  get_type_io_data(elementtype, IOFunc_output,
1434  &typlen, &typbyval, &typalign,
1435  &typdelim, &typioparam, &typoutputfunc);
1436 
1437  /* Check for a transform function */
1438  transform_funcid = get_transform_fromsql(elementtype,
1441 
1442  /* Look up transform or output function as appropriate */
1443  if (OidIsValid(transform_funcid))
1444  fmgr_info(transform_funcid, &info->transform_proc);
1445  else
1446  fmgr_info(typoutputfunc, &info->proc);
1447 
1448  info->elem_is_rowtype = type_is_rowtype(elementtype);
1449 
1450  /* Get the number and bounds of array dimensions */
1451  info->ndims = ARR_NDIM(ar);
1452  dims = ARR_DIMS(ar);
1453 
1454  /* No dimensions? Return an empty array */
1455  if (info->ndims == 0)
1456  {
1457  av = newRV_noinc((SV *) newAV());
1458  }
1459  else
1460  {
1461  deconstruct_array(ar, elementtype, typlen, typbyval,
1462  typalign, &info->elements, &info->nulls,
1463  &nitems);
1464 
1465  /* Get total number of elements in each dimension */
1466  info->nelems = palloc(sizeof(int) * info->ndims);
1467  info->nelems[0] = nitems;
1468  for (i = 1; i < info->ndims; i++)
1469  info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
1470 
1471  av = split_array(info, 0, nitems, 0);
1472  }
1473 
1474  hv = newHV();
1475  (void) hv_store(hv, "array", 5, av, 0);
1476  (void) hv_store(hv, "typeoid", 7, newSVuv(typid), 0);
1477 
1478  return sv_bless(newRV_noinc((SV *) hv),
1479  gv_stashpv("PostgreSQL::InServer::ARRAY", 0));
1480 }
signed short int16
Definition: c.h:252
FmgrInfo transform_proc
Definition: plperl.c:219
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1840
List * trftypes
Definition: plperl.c:117
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
static SV * split_array(plperl_array_info *info, int first, int last, int nest)
Definition: plperl.c:1486
#define newSVuv(uv)
Definition: ppport.h:3596
#define ARR_DIMS(a)
Definition: array.h:275
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:159
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2372
#define newRV_noinc(a)
Definition: ppport.h:4456
struct @18::@19 av[32]
void * palloc0(Size size)
Definition: mcxt.c:920
bool elem_is_rowtype
Definition: plperl.c:214
#define ARR_NDIM(a)
Definition: array.h:271
FmgrInfo proc
Definition: plperl.c:218
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3475
bool * nulls
Definition: plperl.c:216
void * palloc(Size size)
Definition: mcxt.c:891
int i
Datum * elements
Definition: plperl.c:215
void * arg
plperl_proc_desc * prodesc
Definition: plperl.c:179
static plperl_call_data * current_call_data
Definition: plperl.c:244
#define ARR_ELEMTYPE(a)
Definition: array.h:273
#define DatumGetArrayTypeP(X)
Definition: array.h:242
void get_type_io_data(Oid typid, IOFuncSelector which_func, int16 *typlen, bool *typbyval, char *typalign, char *typdelim, Oid *typioparam, Oid *func)
Definition: lsyscache.c:2043
void plperl_return_next ( SV *  sv)

Definition at line 3141 of file plperl.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate(), ReturnSetInfo::allowedModes, Assert, CreateTupleDescCopy(), CurrentMemoryContext, ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, plperl_call_data::fcinfo, plperl_proc_desc::fn_retisset, plperl_proc_desc::fn_retistuple, get_call_result_type(), MemoryContextReset(), MemoryContextSwitchTo(), NULL, plperl_build_tuple_result(), plperl_sv_to_datum(), plperl_call_data::prodesc, plperl_proc_desc::result_in_func, plperl_proc_desc::result_oid, plperl_proc_desc::result_typioparam, FunctionCallInfoData::resultinfo, plperl_call_data::ret_tdesc, SFRM_Materialize_Random, plperl_call_data::tmp_cxt, plperl_call_data::tuple_store, tuplestore_begin_heap(), tuplestore_puttuple(), tuplestore_putvalues(), and work_mem.

Referenced by plperl_func_handler().

3142 {
3143  plperl_proc_desc *prodesc;
3144  FunctionCallInfo fcinfo;
3145  ReturnSetInfo *rsi;
3146  MemoryContext old_cxt;
3147 
3148  if (!sv)
3149  return;
3150 
3151  prodesc = current_call_data->prodesc;
3152  fcinfo = current_call_data->fcinfo;
3153  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
3154 
3155  if (!prodesc->fn_retisset)
3156  ereport(ERROR,
3157  (errcode(ERRCODE_SYNTAX_ERROR),
3158  errmsg("cannot use return_next in a non-SETOF function")));
3159 
3161  {
3162  TupleDesc tupdesc;
3163 
3165 
3166  /*
3167  * This is the first call to return_next in the current PL/Perl
3168  * function call, so memoize some lookups
3169  */
3170  if (prodesc->fn_retistuple)
3171  (void) get_call_result_type(fcinfo, NULL, &tupdesc);
3172  else
3173  tupdesc = rsi->expectedDesc;
3174 
3175  /*
3176  * Make sure the tuple_store and ret_tdesc are sufficiently
3177  * long-lived.
3178  */
3180 
3184  false, work_mem);
3185 
3186  MemoryContextSwitchTo(old_cxt);
3187  }
3188 
3189  /*
3190  * Producing the tuple we want to return requires making plenty of
3191  * palloc() allocations that are not cleaned up. Since this function can
3192  * be called many times before the current memory context is reset, we
3193  * need to do those allocations in a temporary context.
3194  */
3195  if (!current_call_data->tmp_cxt)
3196  {
3199  "PL/Perl return_next temporary cxt",
3201  }
3202 
3204 
3205  if (prodesc->fn_retistuple)
3206  {
3207  HeapTuple tuple;
3208 
3209  if (!(SvOK(sv) && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV))
3210  ereport(ERROR,
3211  (errcode(ERRCODE_DATATYPE_MISMATCH),
3212  errmsg("SETOF-composite-returning PL/Perl function "
3213  "must call return_next with reference to hash")));
3214 
3215  tuple = plperl_build_tuple_result((HV *) SvRV(sv),
3218  }
3219  else
3220  {
3221  Datum ret;
3222  bool isNull;
3223 
3224  ret = plperl_sv_to_datum(sv,
3225  prodesc->result_oid,
3226  -1,
3227  fcinfo,
3228  &prodesc->result_in_func,
3229  prodesc->result_typioparam,
3230  &isNull);
3231 
3234  &ret, &isNull);
3235  }
3236 
3237  MemoryContextSwitchTo(old_cxt);
3239 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:735
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:141
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
FunctionCallInfo fcinfo
Definition: plperl.c:180
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:135
TupleDesc expectedDesc
Definition: execnodes.h:198
bool fn_retisset
Definition: plperl.c:120
#define ERROR
Definition: elog.h:43
FmgrInfo result_in_func
Definition: plperl.c:124
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:715
fmNodePtr resultinfo
Definition: fmgr.h:73
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
Oid result_typioparam
Definition: plperl.c:125
#define ereport(elevel, rest)
Definition: elog.h:122
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:316
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
uintptr_t Datum
Definition: postgres.h:374
int work_mem
Definition: globals.c:112
Tuplestorestate * tuple_store
Definition: plperl.c:181
int allowedModes
Definition: execnodes.h:199
TupleDesc ret_tdesc
Definition: plperl.c:182
MemoryContext tmp_cxt
Definition: plperl.c:183
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:133
static HeapTuple plperl_build_tuple_result(HV *perlhash, TupleDesc td)
Definition: plperl.c:1046
ExprContext * econtext
Definition: execnodes.h:197
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool fn_retistuple
Definition: plperl.c:119
plperl_proc_desc * prodesc
Definition: plperl.c:179
static plperl_call_data * current_call_data
Definition: plperl.c:244
void plperl_spi_cursor_close ( char *  cursor)

Definition at line 3385 of file plperl.c.

References check_spi_usage_allowed(), SPI_cursor_close(), and SPI_cursor_find().

3386 {
3387  Portal p;
3388 
3390 
3391  p = SPI_cursor_find(cursor);
3392 
3393  if (p)
3394  SPI_cursor_close(p);
3395 }
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1331
Definition: type.h:125
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
void SPI_cursor_close(Portal portal)
Definition: spi.c:1399
HV* plperl_spi_exec ( char *  query,
int  limit 
)

Definition at line 3027 of file plperl.c.

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, CurrentResourceOwner, FlushErrorState(), plperl_proc_desc::fn_readonly, MemoryContextSwitchTo(), ErrorData::message, NULL, PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), plperl_spi_execute_fetch_result(), plperl_call_data::prodesc, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_execute(), SPI_processed, and SPI_tuptable.

3028 {
3029  HV *ret_hv;
3030 
3031  /*
3032  * Execute the query inside a sub-transaction, so we can cope with errors
3033  * sanely
3034  */
3035  MemoryContext oldcontext = CurrentMemoryContext;
3037 
3039 
3041  /* Want to run inside function's memory context */
3042  MemoryContextSwitchTo(oldcontext);
3043 
3044  PG_TRY();
3045  {
3046  int spi_rv;
3047 
3048  pg_verifymbstr(query, strlen(query), false);
3049 
3051  limit);
3053  spi_rv);
3054 
3055  /* Commit the inner transaction, return to outer xact context */
3057  MemoryContextSwitchTo(oldcontext);
3058  CurrentResourceOwner = oldowner;
3059  }
3060  PG_CATCH();
3061  {
3062  ErrorData *edata;
3063 
3064  /* Save error info */
3065  MemoryContextSwitchTo(oldcontext);
3066  edata = CopyErrorData();
3067  FlushErrorState();
3068 
3069  /* Abort the inner transaction */
3071  MemoryContextSwitchTo(oldcontext);
3072  CurrentResourceOwner = oldowner;
3073 
3074  /* Punt the error to Perl */
3075  croak_cstr(edata->message);
3076 
3077  /* Can't get here, but keep compiler quiet */
3078  return NULL;
3079  }
3080  PG_END_TRY();
3081 
3082  return ret_hv;
3083 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:41
void FlushErrorState(void)
Definition: elog.c:1587
uint64 SPI_processed
Definition: spi.c:39
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
static void croak_cstr(const char *str)
bool fn_readonly
Definition: plperl.c:115
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition: plperl.c:3087
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:244
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:303
HV* plperl_spi_exec_prepared ( char *  query,
HV *  attr,
int  argc,
SV **  argv 
)

Definition at line 3546 of file plperl.c.

References plperl_query_desc::arginfuncs, plperl_query_desc::argtypes, plperl_query_desc::argtypioparams, BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, FlushErrorState(), plperl_proc_desc::fn_readonly, HASH_FIND, hash_search(), hv_fetch_string(), i, MemoryContextSwitchTo(), ErrorData::message, plperl_query_desc::nargs, NULL, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, plperl_query_desc::plan, plperl_spi_execute_fetch_result(), plperl_sv_to_datum(), plperl_call_data::prodesc, plperl_query_entry::query_data, plperl_interp_desc::query_hash, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_execute_plan(), SPI_processed, and SPI_tuptable.

3547 {
3548  HV *ret_hv;
3549  SV **sv;
3550  int i,
3551  limit,
3552  spi_rv;
3553  char *nulls;
3554  Datum *argvalues;
3555  plperl_query_desc *qdesc;
3556  plperl_query_entry *hash_entry;
3557 
3558  /*
3559  * Execute the query inside a sub-transaction, so we can cope with errors
3560  * sanely
3561  */
3562  MemoryContext oldcontext = CurrentMemoryContext;
3564 
3566 
3568  /* Want to run inside function's memory context */
3569  MemoryContextSwitchTo(oldcontext);
3570 
3571  PG_TRY();
3572  {
3573  /************************************************************
3574  * Fetch the saved plan descriptor, see if it's o.k.
3575  ************************************************************/
3576  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3577  HASH_FIND, NULL);
3578  if (hash_entry == NULL)
3579  elog(ERROR, "spi_exec_prepared: Invalid prepared query passed");
3580 
3581  qdesc = hash_entry->query_data;
3582  if (qdesc == NULL)
3583  elog(ERROR, "spi_exec_prepared: plperl query_hash value vanished");
3584 
3585  if (qdesc->nargs != argc)
3586  elog(ERROR, "spi_exec_prepared: expected %d argument(s), %d passed",
3587  qdesc->nargs, argc);
3588 
3589  /************************************************************
3590  * Parse eventual attributes
3591  ************************************************************/
3592  limit = 0;
3593  if (attr != NULL)
3594  {
3595  sv = hv_fetch_string(attr, "limit");
3596  if (sv && *sv && SvIOK(*sv))
3597  limit = SvIV(*sv);
3598  }
3599  /************************************************************
3600  * Set up arguments
3601  ************************************************************/
3602  if (argc > 0)
3603  {
3604  nulls = (char *) palloc(argc);
3605  argvalues = (Datum *) palloc(argc * sizeof(Datum));
3606  }
3607  else
3608  {
3609  nulls = NULL;
3610  argvalues = NULL;
3611  }
3612 
3613  for (i = 0; i < argc; i++)
3614  {
3615  bool isnull;
3616 
3617  argvalues[i] = plperl_sv_to_datum(argv[i],
3618  qdesc->argtypes[i],
3619  -1,
3620  NULL,
3621  &qdesc->arginfuncs[i],
3622  qdesc->argtypioparams[i],
3623  &isnull);
3624  nulls[i] = isnull ? 'n' : ' ';
3625  }
3626 
3627  /************************************************************
3628  * go
3629  ************************************************************/
3630  spi_rv = SPI_execute_plan(qdesc->plan, argvalues, nulls,
3633  spi_rv);
3634  if (argc > 0)
3635  {
3636  pfree(argvalues);
3637  pfree(nulls);
3638  }
3639 
3640  /* Commit the inner transaction, return to outer xact context */
3642  MemoryContextSwitchTo(oldcontext);
3643  CurrentResourceOwner = oldowner;
3644  }
3645  PG_CATCH();
3646  {
3647  ErrorData *edata;
3648 
3649  /* Save error info */
3650  MemoryContextSwitchTo(oldcontext);
3651  edata = CopyErrorData();
3652  FlushErrorState();
3653 
3654  /* Abort the inner transaction */
3656  MemoryContextSwitchTo(oldcontext);
3657  CurrentResourceOwner = oldowner;
3658 
3659  /* Punt the error to Perl */
3660  croak_cstr(edata->message);
3661 
3662  /* Can't get here, but keep compiler quiet */
3663  return NULL;
3664  }
3665  PG_END_TRY();
3666 
3667  return ret_hv;
3668 }
Definition: plperl.c:202
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
HTAB * query_hash
Definition: plperl.c:93
SPIPlanPtr plan
Definition: plperl.c:193
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
Oid * argtypioparams
Definition: plperl.c:197
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:41
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:3848
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
void FlushErrorState(void)
Definition: elog.c:1587
uint64 SPI_processed
Definition: spi.c:39
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
int SPI_execute_plan(SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only, long tcount)
Definition: spi.c:338
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
Oid * argtypes
Definition: plperl.c:195
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
uintptr_t Datum
Definition: postgres.h:374
FmgrInfo * arginfuncs
Definition: plperl.c:196
static void croak_cstr(const char *str)
bool fn_readonly
Definition: plperl.c:115
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
void * palloc(Size size)
Definition: mcxt.c:891
int i
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition: plperl.c:3087
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:244
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
plperl_query_desc * query_data
Definition: plperl.c:205
static HV * plperl_spi_execute_fetch_result ( SPITupleTable tuptable,
uint64  processed,
int  status 
)
static

Definition at line 3087 of file plperl.c.

References AV_SIZE_MAX, check_spi_usage_allowed(), cstr2sv(), ereport, errcode(), errmsg(), ERROR, hv_store_string(), i, newRV_noinc, newSVuv, plperl_hash_from_tuple(), SPI_freetuptable(), SPI_result_code_string(), SPITupleTable::tupdesc, UV_MAX, and SPITupleTable::vals.

Referenced by plperl_spi_exec(), and plperl_spi_exec_prepared().

3089 {
3090  HV *result;
3091 
3093 
3094  result = newHV();
3095 
3096  hv_store_string(result, "status",
3098  hv_store_string(result, "processed",
3099  (processed > (uint64) UV_MAX) ?
3100  newSVnv((NV) processed) :
3101  newSVuv((UV) processed));
3102 
3103  if (status > 0 && tuptable)
3104  {
3105  AV *rows;
3106  SV *row;
3107  uint64 i;
3108 
3109  /* Prevent overflow in call to av_extend() */
3110  if (processed > (uint64) AV_SIZE_MAX)
3111  ereport(ERROR,
3112  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3113  errmsg("query result has too many rows to fit in a Perl array")));
3114 
3115  rows = newAV();
3116  av_extend(rows, processed);
3117  for (i = 0; i < processed; i++)
3118  {
3119  row = plperl_hash_from_tuple(tuptable->vals[i], tuptable->tupdesc);
3120  av_push(rows, row);
3121  }
3122  hv_store_string(result, "rows",
3123  newRV_noinc((SV *) rows));
3124  }
3125 
3126  SPI_freetuptable(tuptable);
3127 
3128  return result;
3129 }
#define AV_SIZE_MAX
Definition: plperl.h:95
int errcode(int sqlerrcode)
Definition: elog.c:575
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:3822
HeapTuple * vals
Definition: spi.h:27
NVTYPE NV
Definition: ppport.h:3754
#define ERROR
Definition: elog.h:43
#define newSVuv(uv)
Definition: ppport.h:3596
const char * SPI_result_code_string(int code)
Definition: spi.c:1509
#define ereport(elevel, rest)
Definition: elog.h:122
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: plperl.c:2943
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:969
#define newRV_noinc(a)
Definition: ppport.h:4456
TupleDesc tupdesc
Definition: spi.h:26
static SV * cstr2sv(const char *str)
#define UV_MAX
Definition: ppport.h:3566
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:222
SV* plperl_spi_fetchrow ( char *  cursor)

Definition at line 3313 of file plperl.c.

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, CurrentResourceOwner, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, NULL, PG_CATCH, PG_END_TRY, PG_TRY, PL_sv_undef, plperl_hash_from_tuple(), ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_cursor_close(), SPI_cursor_fetch(), SPI_cursor_find(), SPI_freetuptable(), SPI_processed, SPI_tuptable, SPITupleTable::tupdesc, and SPITupleTable::vals.

3314 {
3315  SV *row;
3316 
3317  /*
3318  * Execute the FETCH inside a sub-transaction, so we can cope with errors
3319  * sanely
3320  */
3321  MemoryContext oldcontext = CurrentMemoryContext;
3323 
3325 
3327  /* Want to run inside function's memory context */
3328  MemoryContextSwitchTo(oldcontext);
3329 
3330  PG_TRY();
3331  {
3333 
3334  if (!p)
3335  {
3336  row = &PL_sv_undef;
3337  }
3338  else
3339  {
3340  SPI_cursor_fetch(p, true, 1);
3341  if (SPI_processed == 0)
3342  {
3343  SPI_cursor_close(p);
3344  row = &PL_sv_undef;
3345  }
3346  else
3347  {
3350  }
3352  }
3353 
3354  /* Commit the inner transaction, return to outer xact context */
3356  MemoryContextSwitchTo(oldcontext);
3357  CurrentResourceOwner = oldowner;
3358  }
3359  PG_CATCH();
3360  {
3361  ErrorData *edata;
3362 
3363  /* Save error info */
3364  MemoryContextSwitchTo(oldcontext);
3365  edata = CopyErrorData();
3366  FlushErrorState();
3367 
3368  /* Abort the inner transaction */
3370  MemoryContextSwitchTo(oldcontext);
3371  CurrentResourceOwner = oldowner;
3372 
3373  /* Punt the error to Perl */
3374  croak_cstr(edata->message);
3375 
3376  /* Can't get here, but keep compiler quiet */
3377  return NULL;
3378  }
3379  PG_END_TRY();
3380 
3381  return row;
3382 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:41
#define PL_sv_undef
Definition: ppport.h:4129
HeapTuple * vals
Definition: spi.h:27
void FlushErrorState(void)
Definition: elog.c:1587
uint64 SPI_processed
Definition: spi.c:39
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1331
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
Definition: type.h:125
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: plperl.c:2943
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:969
static void croak_cstr(const char *str)
TupleDesc tupdesc
Definition: spi.h:26
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
void SPI_cursor_close(Portal portal)
Definition: spi.c:1399
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1343
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
void plperl_spi_freeplan ( char *  query)

Definition at line 3787 of file plperl.c.

References check_spi_usage_allowed(), elog, ERROR, HASH_FIND, HASH_REMOVE, hash_search(), MemoryContextDelete(), NULL, plperl_query_desc::plan, plperl_query_desc::plan_cxt, plperl_query_entry::query_data, plperl_interp_desc::query_hash, and SPI_freeplan().

3788 {
3789  SPIPlanPtr plan;
3790  plperl_query_desc *qdesc;
3791  plperl_query_entry *hash_entry;
3792 
3794 
3795  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3796  HASH_FIND, NULL);
3797  if (hash_entry == NULL)
3798  elog(ERROR, "spi_freeplan: Invalid prepared query passed");
3799 
3800  qdesc = hash_entry->query_data;
3801  if (qdesc == NULL)
3802  elog(ERROR, "spi_freeplan: plperl query_hash value vanished");
3803  plan = qdesc->plan;
3804 
3805  /*
3806  * free all memory before SPI_freeplan, so if it dies, nothing will be
3807  * left over
3808  */
3810  HASH_REMOVE, NULL);
3811 
3812  MemoryContextDelete(qdesc->plan_cxt);
3813 
3814  SPI_freeplan(plan);
3815 }
Definition: plperl.c:202
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
HTAB * query_hash
Definition: plperl.c:93
SPIPlanPtr plan
Definition: plperl.c:193
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
#define ERROR
Definition: elog.h:43
MemoryContext plan_cxt
Definition: plperl.c:192
#define NULL
Definition: c.h:226
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:608
#define elog
Definition: elog.h:219
plperl_query_desc * query_data
Definition: plperl.c:205
SV* plperl_spi_prepare ( char *  query,
int  argc,
SV **  argv 
)

Definition at line 3398 of file plperl.c.

References ALLOCSET_DEFAULT_SIZES, ALLOCSET_SMALL_SIZES, AllocSetContextCreate(), plperl_query_desc::arginfuncs, plperl_query_desc::argtypes, plperl_query_desc::argtypioparams, BeginInternalSubTransaction(), CHECK_FOR_INTERRUPTS, check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, FlushErrorState(), fmgr_info_cxt(), getTypeInputInfo(), HASH_ENTER, HASH_REMOVE, hash_search(), i, MemoryContextDelete(), MemoryContextSwitchTo(), ErrorData::message, plperl_query_desc::nargs, NULL, palloc(), palloc0(), parseTypeString(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), plperl_query_desc::plan, plperl_query_desc::plan_cxt, plperl_query_desc::qname, plperl_query_entry::query_data, plperl_interp_desc::query_hash, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), snprintf(), SPI_freeplan(), SPI_keepplan(), SPI_prepare(), SPI_result, SPI_result_code_string(), sv2cstr(), and TopMemoryContext.

3399 {
3400  volatile SPIPlanPtr plan = NULL;
3401  volatile MemoryContext plan_cxt = NULL;
3402  plperl_query_desc *volatile qdesc = NULL;
3403  plperl_query_entry *volatile hash_entry = NULL;
3404  MemoryContext oldcontext = CurrentMemoryContext;
3406  MemoryContext work_cxt;
3407  bool found;
3408  int i;
3409 
3411 
3413  MemoryContextSwitchTo(oldcontext);
3414 
3415  PG_TRY();
3416  {
3418 
3419  /************************************************************
3420  * Allocate the new querydesc structure
3421  *
3422  * The qdesc struct, as well as all its subsidiary data, lives in its
3423  * plan_cxt. But note that the SPIPlan does not.
3424  ************************************************************/
3426  "PL/Perl spi_prepare query",
3428  MemoryContextSwitchTo(plan_cxt);
3429  qdesc = (plperl_query_desc *) palloc0(sizeof(plperl_query_desc));
3430  snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
3431  qdesc->plan_cxt = plan_cxt;
3432  qdesc->nargs = argc;
3433  qdesc->argtypes = (Oid *) palloc(argc * sizeof(Oid));
3434  qdesc->arginfuncs = (FmgrInfo *) palloc(argc * sizeof(FmgrInfo));
3435  qdesc->argtypioparams = (Oid *) palloc(argc * sizeof(Oid));
3436  MemoryContextSwitchTo(oldcontext);
3437 
3438  /************************************************************
3439  * Do the following work in a short-lived context so that we don't
3440  * leak a lot of memory in the PL/Perl function's SPI Proc context.
3441  ************************************************************/
3443  "PL/Perl spi_prepare workspace",
3445  MemoryContextSwitchTo(work_cxt);
3446 
3447  /************************************************************
3448  * Resolve argument type names and then look them up by oid
3449  * in the system cache, and remember the required information
3450  * for input conversion.
3451  ************************************************************/
3452  for (i = 0; i < argc; i++)
3453  {
3454  Oid typId,
3455  typInput,
3456  typIOParam;
3457  int32 typmod;
3458  char *typstr;
3459 
3460  typstr = sv2cstr(argv[i]);
3461  parseTypeString(typstr, &typId, &typmod, false);
3462  pfree(typstr);
3463 
3464  getTypeInputInfo(typId, &typInput, &typIOParam);
3465 
3466  qdesc->argtypes[i] = typId;
3467  fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
3468  qdesc->argtypioparams[i] = typIOParam;
3469  }
3470 
3471  /* Make sure the query is validly encoded */
3472  pg_verifymbstr(query, strlen(query), false);
3473 
3474  /************************************************************
3475  * Prepare the plan and check for errors
3476  ************************************************************/
3477  plan = SPI_prepare(query, argc, qdesc->argtypes);
3478 
3479  if (plan == NULL)
3480  elog(ERROR, "SPI_prepare() failed:%s",
3482 
3483  /************************************************************
3484  * Save the plan into permanent memory (right now it's in the
3485  * SPI procCxt, which will go away at function end).
3486  ************************************************************/
3487  if (SPI_keepplan(plan))
3488  elog(ERROR, "SPI_keepplan() failed");
3489  qdesc->plan = plan;
3490 
3491  /************************************************************
3492  * Insert a hashtable entry for the plan.
3493  ************************************************************/
3495  qdesc->qname,
3496  HASH_ENTER, &found);
3497  hash_entry->query_data = qdesc;
3498 
3499  /* Get rid of workspace */
3500  MemoryContextDelete(work_cxt);
3501 
3502  /* Commit the inner transaction, return to outer xact context */
3504  MemoryContextSwitchTo(oldcontext);
3505  CurrentResourceOwner = oldowner;
3506  }
3507  PG_CATCH();
3508  {
3509  ErrorData *edata;
3510 
3511  /* Save error info */
3512  MemoryContextSwitchTo(oldcontext);
3513  edata = CopyErrorData();
3514  FlushErrorState();
3515 
3516  /* Drop anything we managed to allocate */
3517  if (hash_entry)
3519  qdesc->qname,
3520  HASH_REMOVE, NULL);
3521  if (plan_cxt)
3522  MemoryContextDelete(plan_cxt);
3523  if (plan)
3524  SPI_freeplan(plan);
3525 
3526  /* Abort the inner transaction */
3528  MemoryContextSwitchTo(oldcontext);
3529  CurrentResourceOwner = oldowner;
3530 
3531  /* Punt the error to Perl */
3532  croak_cstr(edata->message);
3533 
3534  /* Can't get here, but keep compiler quiet */
3535  return NULL;
3536  }
3537  PG_END_TRY();
3538 
3539  /************************************************************
3540  * Return the query's hash key to the caller.
3541  ************************************************************/
3542  return cstr2sv(qdesc->qname);
3543 }
Definition: fmgr.h:53
Definition: plperl.c:202
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
HTAB * query_hash
Definition: plperl.c:93
SPIPlanPtr plan
Definition: plperl.c:193
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
Oid * argtypioparams
Definition: plperl.c:197
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:481
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:155
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
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:885
unsigned int Oid
Definition: postgres_ext.h:31
void FlushErrorState(void)
Definition: elog.c:1587
signed int int32
Definition: c.h:253
int SPI_result
Definition: spi.c:42
void pfree(void *pointer)
Definition: mcxt.c:992
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1509
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:559
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
Oid * argtypes
Definition: plperl.c:195
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
MemoryContext plan_cxt
Definition: plperl.c:192
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:169
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2567
MemoryContext TopMemoryContext
Definition: mcxt.c:43
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
void * palloc0(Size size)
Definition: mcxt.c:920
char qname[24]
Definition: plperl.c:191
FmgrInfo * arginfuncs
Definition: plperl.c:196
static void croak_cstr(const char *str)
static SV * cstr2sv(const char *str)
void parseTypeString(const char *str, Oid *typeid_p, int32 *typmod_p, bool missing_ok)
Definition: parse_type.c:781
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:608
void * palloc(Size size)
Definition: mcxt.c:891
int i
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
static char * sv2cstr(SV *sv)
plperl_query_desc * query_data
Definition: plperl.c:205
SV* plperl_spi_query ( char *  query)

Definition at line 3243 of file plperl.c.

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, PortalData::name, NULL, PG_CATCH, PG_END_TRY, PG_TRY, pg_verifymbstr(), ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_cursor_open(), SPI_freeplan(), SPI_prepare(), SPI_result, and SPI_result_code_string().

3244 {
3245  SV *cursor;
3246 
3247  /*
3248  * Execute the query inside a sub-transaction, so we can cope with errors
3249  * sanely
3250  */
3251  MemoryContext oldcontext = CurrentMemoryContext;
3253 
3255 
3257  /* Want to run inside function's memory context */
3258  MemoryContextSwitchTo(oldcontext);
3259 
3260  PG_TRY();
3261  {
3262  SPIPlanPtr plan;
3263  Portal portal;
3264 
3265  /* Make sure the query is validly encoded */
3266  pg_verifymbstr(query, strlen(query), false);
3267 
3268  /* Create a cursor for the query */
3269  plan = SPI_prepare(query, 0, NULL);
3270  if (plan == NULL)
3271  elog(ERROR, "SPI_prepare() failed:%s",
3273 
3274  portal = SPI_cursor_open(NULL, plan, NULL, NULL, false);
3275  SPI_freeplan(plan);
3276  if (portal == NULL)
3277  elog(ERROR, "SPI_cursor_open() failed:%s",
3279  cursor = cstr2sv(portal->name);
3280 
3281  /* Commit the inner transaction, return to outer xact context */
3283  MemoryContextSwitchTo(oldcontext);
3284  CurrentResourceOwner = oldowner;
3285  }
3286  PG_CATCH();
3287  {
3288  ErrorData *edata;
3289 
3290  /* Save error info */
3291  MemoryContextSwitchTo(oldcontext);
3292  edata = CopyErrorData();
3293  FlushErrorState();
3294 
3295  /* Abort the inner transaction */
3297  MemoryContextSwitchTo(oldcontext);
3298  CurrentResourceOwner = oldowner;
3299 
3300  /* Punt the error to Perl */
3301  croak_cstr(edata->message);
3302 
3303  /* Can't get here, but keep compiler quiet */
3304  return NULL;
3305  }
3306  PG_END_TRY();
3307 
3308  return cursor;
3309 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:481
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1028
void FlushErrorState(void)
Definition: elog.c:1587
int SPI_result
Definition: spi.c:42
const char * name
Definition: portal.h:117
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1509
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
Definition: type.h:125
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
static void croak_cstr(const char *str)
static SV * cstr2sv(const char *str)
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:608
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
SV* plperl_spi_query_prepared ( char *  query,
int  argc,
SV **  argv 
)

Definition at line 3671 of file plperl.c.

References plperl_query_desc::arginfuncs, plperl_query_desc::argtypes, plperl_query_desc::argtypioparams, BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), cstr2sv(), CurrentMemoryContext, CurrentResourceOwner, elog, ERROR, FlushErrorState(), plperl_proc_desc::fn_readonly, HASH_FIND, hash_search(), i, MemoryContextSwitchTo(), ErrorData::message, PortalData::name, plperl_query_desc::nargs, NULL, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, plperl_query_desc::plan, plperl_sv_to_datum(), plperl_call_data::prodesc, plperl_query_entry::query_data, plperl_interp_desc::query_hash, ReleaseCurrentSubTransaction(), RollbackAndReleaseCurrentSubTransaction(), SPI_cursor_open(), SPI_result, and SPI_result_code_string().

3672 {
3673  int i;
3674  char *nulls;
3675  Datum *argvalues;
3676  plperl_query_desc *qdesc;
3677  plperl_query_entry *hash_entry;
3678  SV *cursor;
3679  Portal portal = NULL;
3680 
3681  /*
3682  * Execute the query inside a sub-transaction, so we can cope with errors
3683  * sanely
3684  */
3685  MemoryContext oldcontext = CurrentMemoryContext;
3687 
3689 
3691  /* Want to run inside function's memory context */
3692  MemoryContextSwitchTo(oldcontext);
3693 
3694  PG_TRY();
3695  {
3696  /************************************************************
3697  * Fetch the saved plan descriptor, see if it's o.k.
3698  ************************************************************/
3699  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3700  HASH_FIND, NULL);
3701  if (hash_entry == NULL)
3702  elog(ERROR, "spi_query_prepared: Invalid prepared query passed");
3703 
3704  qdesc = hash_entry->query_data;
3705  if (qdesc == NULL)
3706  elog(ERROR, "spi_query_prepared: plperl query_hash value vanished");
3707 
3708  if (qdesc->nargs != argc)
3709  elog(ERROR, "spi_query_prepared: expected %d argument(s), %d passed",
3710  qdesc->nargs, argc);
3711 
3712  /************************************************************
3713  * Set up arguments
3714  ************************************************************/
3715  if (argc > 0)
3716  {
3717  nulls = (char *) palloc(argc);
3718  argvalues = (Datum *) palloc(argc * sizeof(Datum));
3719  }
3720  else
3721  {
3722  nulls = NULL;
3723  argvalues = NULL;
3724  }
3725 
3726  for (i = 0; i < argc; i++)
3727  {
3728  bool isnull;
3729 
3730  argvalues[i] = plperl_sv_to_datum(argv[i],
3731  qdesc->argtypes[i],
3732  -1,
3733  NULL,
3734  &qdesc->arginfuncs[i],
3735  qdesc->argtypioparams[i],
3736  &isnull);
3737  nulls[i] = isnull ? 'n' : ' ';
3738  }
3739 
3740  /************************************************************
3741  * go
3742  ************************************************************/
3743  portal = SPI_cursor_open(NULL, qdesc->plan, argvalues, nulls,
3745  if (argc > 0)
3746  {
3747  pfree(argvalues);
3748  pfree(nulls);
3749  }
3750  if (portal == NULL)
3751  elog(ERROR, "SPI_cursor_open() failed:%s",
3753 
3754  cursor = cstr2sv(portal->name);
3755 
3756  /* Commit the inner transaction, return to outer xact context */
3758  MemoryContextSwitchTo(oldcontext);
3759  CurrentResourceOwner = oldowner;
3760  }
3761  PG_CATCH();
3762  {
3763  ErrorData *edata;
3764 
3765  /* Save error info */
3766  MemoryContextSwitchTo(oldcontext);
3767  edata = CopyErrorData();
3768  FlushErrorState();
3769 
3770  /* Abort the inner transaction */
3772  MemoryContextSwitchTo(oldcontext);
3773  CurrentResourceOwner = oldowner;
3774 
3775  /* Punt the error to Perl */
3776  croak_cstr(edata->message);
3777 
3778  /* Can't get here, but keep compiler quiet */
3779  return NULL;
3780  }
3781  PG_END_TRY();
3782 
3783  return cursor;
3784 }
Definition: plperl.c:202
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
HTAB * query_hash
Definition: plperl.c:93
SPIPlanPtr plan
Definition: plperl.c:193
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
Oid * argtypioparams
Definition: plperl.c:197
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4121
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:228
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1028
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:885
void FlushErrorState(void)
Definition: elog.c:1587
int SPI_result
Definition: spi.c:42
void pfree(void *pointer)
Definition: mcxt.c:992
const char * name
Definition: portal.h:117
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1509
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4155
Definition: type.h:125
Oid * argtypes
Definition: plperl.c:195
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
uintptr_t Datum
Definition: postgres.h:374
FmgrInfo * arginfuncs
Definition: plperl.c:196
static void croak_cstr(const char *str)
static SV * cstr2sv(const char *str)
bool fn_readonly
Definition: plperl.c:115
#define PG_CATCH()
Definition: elog.h:293
#define NULL
Definition: c.h:226
void BeginInternalSubTransaction(char *name)
Definition: xact.c:4051
static void check_spi_usage_allowed(void)
Definition: plperl.c:3015
void * palloc(Size size)
Definition: mcxt.c:891
int i
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:244
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
plperl_query_desc * query_data
Definition: plperl.c:205
static Datum plperl_sv_to_datum ( SV *  sv,
Oid  typid,
int32  typmod,
FunctionCallInfo  fcinfo,
FmgrInfo finfo,
Oid  typioparam,
bool isnull 
)
static

Definition at line 1272 of file plperl.c.

References _sv_to_datum_finfo(), check_stack_depth(), ereport, errcode(), errmsg(), ERROR, format_type_be(), get_call_result_type(), get_perl_array_ref(), get_transform_tosql(), InputFunctionCall(), plperl_proc_desc::lang_oid, lookup_rowtype_tupdesc_noerror(), NULL, OidFunctionCall1, pfree(), plperl_array_to_datum(), plperl_hash_to_datum(), PointerGetDatum, plperl_call_data::prodesc, ReleaseTupleDesc, sv2cstr(), plperl_proc_desc::trftypes, type_is_rowtype(), TYPEFUNC_COMPOSITE, and VOIDOID.

Referenced by array_to_datum_internal(), plperl_build_tuple_result(), plperl_func_handler(), plperl_modify_tuple(), plperl_return_next(), plperl_spi_exec_prepared(), plperl_spi_query_prepared(), and plperl_sv_to_literal().

1276 {
1277  FmgrInfo tmp;
1278  Oid funcid;
1279 
1280  /* we might recurse */
1282 
1283  *isnull = false;
1284 
1285  /*
1286  * Return NULL if result is undef, or if we're in a function returning
1287  * VOID. In the latter case, we should pay no attention to the last Perl
1288  * statement's result, and this is a convenient means to ensure that.
1289  */
1290  if (!sv || !SvOK(sv) || typid == VOIDOID)
1291  {
1292  /* look up type info if they did not pass it */
1293  if (!finfo)
1294  {
1295  _sv_to_datum_finfo(typid, &tmp, &typioparam);
1296  finfo = &tmp;
1297  }
1298  *isnull = true;
1299  /* must call typinput in case it wants to reject NULL */
1300  return InputFunctionCall(finfo, NULL, typioparam, typmod);
1301  }
1303  return OidFunctionCall1(funcid, PointerGetDatum(sv));
1304  else if (SvROK(sv))
1305  {
1306  /* handle references */
1307  SV *sav = get_perl_array_ref(sv);
1308 
1309  if (sav)
1310  {
1311  /* handle an arrayref */
1312  return plperl_array_to_datum(sav, typid, typmod);
1313  }
1314  else if (SvTYPE(SvRV(sv)) == SVt_PVHV)
1315  {
1316  /* handle a hashref */
1317  Datum ret;
1318  TupleDesc td;
1319 
1320  if (!type_is_rowtype(typid))
1321  ereport(ERROR,
1322  (errcode(ERRCODE_DATATYPE_MISMATCH),
1323  errmsg("cannot convert Perl hash to non-composite type %s",
1324  format_type_be(typid))));
1325 
1326  td = lookup_rowtype_tupdesc_noerror(typid, typmod, true);
1327  if (td == NULL)
1328  {
1329  /* Try to look it up based on our result type */
1330  if (fcinfo == NULL ||
1331  get_call_result_type(fcinfo, NULL, &td) != TYPEFUNC_COMPOSITE)
1332  ereport(ERROR,
1333  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1334  errmsg("function returning record called in context "
1335  "that cannot accept type record")));
1336  }
1337 
1338  ret = plperl_hash_to_datum(sv, td);
1339 
1340  /* Release on the result of get_call_result_type is harmless */
1341  ReleaseTupleDesc(td);
1342 
1343  return ret;
1344  }
1345 
1346  /* Reference, but not reference to hash or array ... */
1347  ereport(ERROR,
1348  (errcode(ERRCODE_DATATYPE_MISMATCH),
1349  errmsg("PL/Perl function must return reference to hash or array")));
1350  return (Datum) 0; /* shut up compiler */
1351  }
1352  else
1353  {
1354  /* handle a string/number */
1355  Datum ret;
1356  char *str = sv2cstr(sv);
1357 
1358  /* did not pass in any typeinfo? look it up */
1359  if (!finfo)
1360  {
1361  _sv_to_datum_finfo(typid, &tmp, &typioparam);
1362  finfo = &tmp;
1363  }
1364 
1365  ret = InputFunctionCall(finfo, str, typioparam, typmod);
1366  pfree(str);
1367 
1368  return ret;
1369  }
1370 }
Definition: fmgr.h:53
List * trftypes
Definition: plperl.c:117
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1107
static Datum plperl_array_to_datum(SV *src, Oid typid, int32 typmod)
Definition: plperl.c:1206
#define PointerGetDatum(X)
Definition: postgres.h:564
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:94
unsigned int Oid
Definition: postgres_ext.h:31
void pfree(void *pointer)
Definition: mcxt.c:992
#define VOIDOID
Definition: pg_type.h:678
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:593
void check_stack_depth(void)
Definition: postgres.c:3096
static Datum plperl_hash_to_datum(SV *src, TupleDesc td)
Definition: plperl.c:1095
TupleDesc lookup_rowtype_tupdesc_noerror(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1262
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2372
#define ereport(elevel, rest)
Definition: elog.h:122
uintptr_t Datum
Definition: postgres.h:374
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1882
#define NULL
Definition: c.h:226
Oid get_transform_tosql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1861
int errmsg(const char *fmt,...)
Definition: elog.c:797
plperl_proc_desc * prodesc
Definition: plperl.c:179
static void _sv_to_datum_finfo(Oid typid, FmgrInfo *finfo, Oid *typioparam)
Definition: plperl.c:1249
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:107
static plperl_call_data * current_call_data
Definition: plperl.c:244
static char * sv2cstr(SV *sv)
char* plperl_sv_to_literal ( SV *  sv,
char *  fqtypename 
)

Definition at line 1374 of file plperl.c.

References CStringGetDatum, DirectFunctionCall1, ereport, errcode(), errmsg(), ERROR, getTypeOutputInfo(), InvalidOid, NULL, OidIsValid, OidOutputFunctionCall(), plperl_sv_to_datum(), and regtypein().

1375 {
1376  Datum str = CStringGetDatum(fqtypename);
1377  Oid typid = DirectFunctionCall1(regtypein, str);
1378  Oid typoutput;
1379  Datum datum;
1380  bool typisvarlena,
1381  isnull;
1382 
1383  if (!OidIsValid(typid))
1384  ereport(ERROR,
1385  (errcode(ERRCODE_UNDEFINED_OBJECT),
1386  errmsg("lookup failed for type %s", fqtypename)));
1387 
1388  datum = plperl_sv_to_datum(sv,
1389  typid, -1,
1390  NULL, NULL, InvalidOid,
1391  &isnull);
1392 
1393  if (isnull)
1394  return NULL;
1395 
1396  getTypeOutputInfo(typid,
1397  &typoutput, &typisvarlena);
1398 
1399  return OidOutputFunctionCall(typoutput, datum);
1400 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2600
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1272
int errcode(int sqlerrcode)
Definition: elog.c:575
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:534
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:586
#define ereport(elevel, rest)
Definition: elog.h:122
Datum regtypein(PG_FUNCTION_ARGS)
Definition: regproc.c:1178
uintptr_t Datum
Definition: postgres.h:374
#define InvalidOid
Definition: postgres_ext.h:36
#define NULL
Definition: c.h:226
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:2006
int errmsg(const char *fmt,...)
Definition: elog.c:797
static SV* plperl_trigger_build_args ( FunctionCallInfo  fcinfo)
static

Definition at line 1556 of file plperl.c.

References av, FunctionCallInfoData::context, cstr2sv(), DatumGetCString, DirectFunctionCall1, hv_store_string(), i, newRV_noinc, ObjectIdGetDatum, oidout(), plperl_hash_from_tuple(), RelationData::rd_att, RelationData::rd_id, SPI_getnspname(), SPI_getrelname(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_relation, TriggerData::tg_trigger, TriggerData::tg_trigtuple, Trigger::tgargs, Trigger::tgname, Trigger::tgnargs, TRIGGER_FIRED_AFTER, TRIGGER_FIRED_BEFORE, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_TRUNCATE, TRIGGER_FIRED_BY_UPDATE, TRIGGER_FIRED_FOR_ROW, TRIGGER_FIRED_FOR_STATEMENT, and TRIGGER_FIRED_INSTEAD.

Referenced by plperl_trigger_handler().

1557 {
1558  TriggerData *tdata;
1559  TupleDesc tupdesc;
1560  int i;
1561  char *level;
1562  char *event;
1563  char *relid;
1564  char *when;
1565  HV *hv;
1566 
1567  hv = newHV();
1568  hv_ksplit(hv, 12); /* pre-grow the hash */
1569 
1570  tdata = (TriggerData *) fcinfo->context;
1571  tupdesc = tdata->tg_relation->rd_att;
1572 
1573  relid = DatumGetCString(
1576  )
1577  );
1578 
1579  hv_store_string(hv, "name", cstr2sv(tdata->tg_trigger->tgname));
1580  hv_store_string(hv, "relid", cstr2sv(relid));
1581 
1582  if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
1583  {
1584  event = "INSERT";
1585  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
1586  hv_store_string(hv, "new",
1588  tupdesc));
1589  }
1590  else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
1591  {
1592  event = "DELETE";
1593  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
1594  hv_store_string(hv, "old",
1596  tupdesc));
1597  }
1598  else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
1599  {
1600  event = "UPDATE";
1601  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
1602  {
1603  hv_store_string(hv, "old",
1605  tupdesc));
1606  hv_store_string(hv, "new",
1608  tupdesc));
1609  }
1610  }
1611  else if (TRIGGER_FIRED_BY_TRUNCATE(tdata->tg_event))
1612  event = "TRUNCATE";
1613  else
1614  event = "UNKNOWN";
1615 
1616  hv_store_string(hv, "event", cstr2sv(event));
1617  hv_store_string(hv, "argc", newSViv(tdata->tg_trigger->tgnargs));
1618 
1619  if (tdata->tg_trigger->tgnargs > 0)
1620  {
1621  AV *av = newAV();
1622 
1623  av_extend(av, tdata->tg_trigger->tgnargs);
1624  for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
1625  av_push(av, cstr2sv(tdata->tg_trigger->tgargs[i]));
1626  hv_store_string(hv, "args", newRV_noinc((SV *) av));
1627  }
1628 
1629  hv_store_string(hv, "relname",
1631 
1632  hv_store_string(hv, "table_name",
1634 
1635  hv_store_string(hv, "table_schema",
1637 
1638  if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
1639  when = "BEFORE";
1640  else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
1641  when = "AFTER";
1642  else if (TRIGGER_FIRED_INSTEAD(tdata->tg_event))
1643  when = "INSTEAD OF";
1644  else
1645  when = "UNKNOWN";
1646  hv_store_string(hv, "when", cstr2sv(when));
1647 
1648  if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
1649  level = "ROW";
1650  else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
1651  level = "STATEMENT";
1652  else
1653  level = "UNKNOWN";
1654  hv_store_string(hv, "level", cstr2sv(level));
1655 
1656  return newRV_noinc((SV *) hv);
1657 }
fmNodePtr context
Definition: fmgr.h:72
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:555
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:3822
#define TRIGGER_FIRED_AFTER(event)
Definition: trigger.h:91
Datum oidout(PG_FUNCTION_ARGS)
Definition: oid.c:127
#define TRIGGER_FIRED_FOR_STATEMENT(event)
Definition: trigger.h:85
HeapTuple tg_trigtuple
Definition: trigger.h:35
#define TRIGGER_FIRED_BY_TRUNCATE(event)
Definition: trigger.h:79
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define DatumGetCString(X)
Definition: postgres.h:574
char * tgname
Definition: reltrigger.h:27
Definition: type.h:83
Oid rd_id
Definition: rel.h:115
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: plperl.c:2943
char ** tgargs
Definition: reltrigger.h:40
#define newRV_noinc(a)
Definition: ppport.h:4456
#define TRIGGER_FIRED_BY_DELETE(event)
Definition: trigger.h:73
struct @18::@19 av[32]
Trigger * tg_trigger
Definition: trigger.h:37
TupleDesc rd_att
Definition: rel.h:114
HeapTuple tg_newtuple
Definition: trigger.h:36
static SV * cstr2sv(const char *str)
char * SPI_getrelname(Relation rel)
Definition: spi.c:909
TriggerEvent tg_event
Definition: trigger.h:33
char * SPI_getnspname(Relation rel)
Definition: spi.c:915
#define TRIGGER_FIRED_BEFORE(event)
Definition: trigger.h:88
#define TRIGGER_FIRED_INSTEAD(event)
Definition: trigger.h:94
#define TRIGGER_FIRED_BY_INSERT(event)
Definition: trigger.h:70
int i
int16 tgnargs
Definition: reltrigger.h:37
#define TRIGGER_FIRED_FOR_ROW(event)
Definition: trigger.h:82
#define TRIGGER_FIRED_BY_UPDATE(event)
Definition: trigger.h:76
Relation tg_relation
Definition: trigger.h:34
static Datum plperl_trigger_handler ( PG_FUNCTION_ARGS  )
static

Definition at line 2437 of file plperl.c.

References activate_interpreter(), ErrorContextCallback::arg, ErrorContextCallback::callback, compile_plperl_function(), elog, ereport, errcode(), errmsg(), ERROR, error_context_stack, increment_prodesc_refcount, plperl_proc_desc::interp, NULL, pfree(), pg_strcasecmp(), plperl_call_perl_trigger_func(), plperl_exec_callback(), plperl_modify_tuple(), plperl_trigger_build_args(), PointerGetDatum, ErrorContextCallback::previous, plperl_call_data::prodesc, plperl_proc_desc::proname, SPI_connect(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, sv2cstr(), TriggerData::tg_event, TriggerData::tg_newtuple, TriggerData::tg_trigtuple, TRIGGER_FIRED_BY_DELETE, TRIGGER_FIRED_BY_INSERT, TRIGGER_FIRED_BY_TRUNCATE, TRIGGER_FIRED_BY_UPDATE, and WARNING.

Referenced by plperl_call_handler().

2438 {
2439  plperl_proc_desc *prodesc;
2440  SV *perlret;
2441  Datum retval;
2442  SV *svTD;
2443  HV *hvTD;
2444  ErrorContextCallback pl_error_context;
2445 
2446  /* Connect to SPI manager */
2447  if (SPI_connect() != SPI_OK_CONNECT)
2448  elog(ERROR, "could not connect to SPI manager");
2449 
2450  /* Find or compile the function */
2451  prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, true, false);
2452  current_call_data->prodesc = prodesc;
2453  increment_prodesc_refcount(prodesc);
2454 
2455  /* Set a callback for error reporting */
2456  pl_error_context.callback = plperl_exec_callback;
2457  pl_error_context.previous = error_context_stack;
2458  pl_error_context.arg = prodesc->proname;
2459  error_context_stack = &pl_error_context;
2460 
2461  activate_interpreter(prodesc->interp);
2462 
2463  svTD = plperl_trigger_build_args(fcinfo);
2464  perlret = plperl_call_perl_trigger_func(prodesc, fcinfo, svTD);
2465  hvTD = (HV *) SvRV(svTD);
2466 
2467  /************************************************************
2468  * Disconnect from SPI manager and then create the return
2469  * values datum (if the input function does a palloc for it
2470  * this must not be allocated in the SPI memory context
2471  * because SPI_finish would free it).
2472  ************************************************************/
2473  if (SPI_finish() != SPI_OK_FINISH)
2474  elog(ERROR, "SPI_finish() failed");
2475 
2476  if (perlret == NULL || !SvOK(perlret))
2477  {
2478  /* undef result means go ahead with original tuple */
2479  TriggerData *trigdata = ((TriggerData *) fcinfo->context);
2480 
2481  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2482  retval = (Datum) trigdata->tg_trigtuple;
2483  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2484  retval = (Datum) trigdata->tg_newtuple;
2485  else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
2486  retval = (Datum) trigdata->tg_trigtuple;
2487  else if (TRIGGER_FIRED_BY_TRUNCATE(trigdata->tg_event))
2488  retval = (Datum) trigdata->tg_trigtuple;
2489  else
2490  retval = (Datum) 0; /* can this happen? */
2491  }
2492  else
2493  {
2494  HeapTuple trv;
2495  char *tmp;
2496 
2497  tmp = sv2cstr(perlret);
2498 
2499  if (pg_strcasecmp(tmp, "SKIP") == 0)
2500  trv = NULL;
2501  else if (pg_strcasecmp(tmp, "MODIFY") == 0)
2502  {
2503  TriggerData *trigdata = (TriggerData *) fcinfo->context;
2504 
2505  if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
2506  trv = plperl_modify_tuple(hvTD, trigdata,
2507  trigdata->tg_trigtuple);
2508  else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
2509  trv = plperl_modify_tuple(hvTD, trigdata,
2510  trigdata->tg_newtuple);
2511  else
2512  {
2513  ereport(WARNING,
2514  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2515  errmsg("ignoring modified row in DELETE trigger")));
2516  trv = NULL;
2517  }
2518  }
2519  else
2520  {
2521  ereport(ERROR,
2522  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2523  errmsg("result of PL/Perl trigger function must be undef, "
2524  "\"SKIP\", or \"MODIFY\"")));
2525  trv = NULL;
2526  }
2527  retval = PointerGetDatum(trv);
2528  pfree(tmp);
2529  }
2530 
2531  /* Restore the previous error callback */
2532  error_context_stack = pl_error_context.previous;
2533 
2534  SvREFCNT_dec(svTD);
2535  if (perlret)
2536  SvREFCNT_dec(perlret);
2537 
2538  return retval;
2539 }
#define SPI_OK_CONNECT
Definition: spi.h:47
int SPI_connect(void)
Definition: spi.c:84
static HeapTuple plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup)
Definition: plperl.c:1679
#define PointerGetDatum(X)
Definition: postgres.h:564
int SPI_finish(void)
Definition: spi.c:147
int errcode(int sqlerrcode)
Definition: elog.c:575
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
struct ErrorContextCallback * previou