PostgreSQL Source Code  git master
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_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 "perlchunks.h"
#include "plperl.h"
#include "plperl_helpers.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, bool include_generated)
 
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 void plperl_return_next_internal (SV *sv)
 
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, const 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 SvREFCNT_dec_current (SV *sv)
 
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)
 
void plperl_spi_commit (void)
 
void plperl_spi_rollback (void)
 
void plperl_util_elog (int level, SV *msg)
 

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

◆ decrement_prodesc_refcount

#define decrement_prodesc_refcount (   prodesc)
Value:
do { \
Assert((prodesc)->fn_refcount > 0); \
if (--((prodesc)->fn_refcount) == 0) \
free_plperl_function(prodesc); \
} while(0)

Definition at line 131 of file plperl.c.

Referenced by plperl_call_handler(), and validate_plperl_function().

◆ increment_prodesc_refcount

#define increment_prodesc_refcount (   prodesc)    ((prodesc)->fn_refcount++)

◆ TEXTDOMAIN

#define TEXTDOMAIN   PG_TEXTDOMAIN("plperl")

Definition at line 44 of file plperl.c.

Referenced by _PG_init().

Typedef Documentation

◆ plperl_array_info

◆ plperl_call_data

◆ plperl_interp_desc

◆ plperl_proc_desc

◆ plperl_proc_key

◆ plperl_proc_ptr

◆ plperl_query_desc

◆ plperl_query_entry

Function Documentation

◆ _PG_init()

void _PG_init ( void  )

Definition at line 380 of file plperl.c.

References DefineCustomBoolVariable(), DefineCustomStringVariable(), EmitWarningsOnPlaceholders(), HASHCTL::entrysize, gettext_noop, HASH_BLOBS, hash_create(), HASH_ELEM, HASHCTL::keysize, 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.

381 {
382  /*
383  * Be sure we do initialization only once.
384  *
385  * If initialization fails due to, e.g., plperl_init_interp() throwing an
386  * exception, then we'll return here on the next usage and the user will
387  * get a rather cryptic: ERROR: attempt to redefine parameter
388  * "plperl.use_strict"
389  */
390  static bool inited = false;
391  HASHCTL hash_ctl;
392 
393  if (inited)
394  return;
395 
396  /*
397  * Support localized messages.
398  */
400 
401  /*
402  * Initialize plperl's GUCs.
403  */
404  DefineCustomBoolVariable("plperl.use_strict",
405  gettext_noop("If true, trusted and untrusted Perl code will be compiled in strict mode."),
406  NULL,
408  false,
409  PGC_USERSET, 0,
410  NULL, NULL, NULL);
411 
412  /*
413  * plperl.on_init is marked PGC_SIGHUP to support the idea that it might
414  * be executed in the postmaster (if plperl is loaded into the postmaster
415  * via shared_preload_libraries). This isn't really right either way,
416  * though.
417  */
418  DefineCustomStringVariable("plperl.on_init",
419  gettext_noop("Perl initialization code to execute when a Perl interpreter is initialized."),
420  NULL,
422  NULL,
423  PGC_SIGHUP, 0,
424  NULL, NULL, NULL);
425 
426  /*
427  * plperl.on_plperl_init is marked PGC_SUSET to avoid issues whereby a
428  * user who might not even have USAGE privilege on the plperl language
429  * could nonetheless use SET plperl.on_plperl_init='...' to influence the
430  * behaviour of any existing plperl function that they can execute (which
431  * might be SECURITY DEFINER, leading to a privilege escalation). See
432  * http://archives.postgresql.org/pgsql-hackers/2010-02/msg00281.php and
433  * the overall thread.
434  *
435  * Note that because plperl.use_strict is USERSET, a nefarious user could
436  * set it to be applied against other people's functions. This is judged
437  * OK since the worst result would be an error. Your code oughta pass
438  * use_strict anyway ;-)
439  */
440  DefineCustomStringVariable("plperl.on_plperl_init",
441  gettext_noop("Perl initialization code to execute once when plperl is first used."),
442  NULL,
444  NULL,
445  PGC_SUSET, 0,
446  NULL, NULL, NULL);
447 
448  DefineCustomStringVariable("plperl.on_plperlu_init",
449  gettext_noop("Perl initialization code to execute once when plperlu is first used."),
450  NULL,
452  NULL,
453  PGC_SUSET, 0,
454  NULL, NULL, NULL);
455 
456  EmitWarningsOnPlaceholders("plperl");
457 
458  /*
459  * Create hash tables.
460  */
461  memset(&hash_ctl, 0, sizeof(hash_ctl));
462  hash_ctl.keysize = sizeof(Oid);
463  hash_ctl.entrysize = sizeof(plperl_interp_desc);
464  plperl_interp_hash = hash_create("PL/Perl interpreters",
465  8,
466  &hash_ctl,
468 
469  memset(&hash_ctl, 0, sizeof(hash_ctl));
470  hash_ctl.keysize = sizeof(plperl_proc_key);
471  hash_ctl.entrysize = sizeof(plperl_proc_ptr);
472  plperl_proc_hash = hash_create("PL/Perl procedures",
473  32,
474  &hash_ctl,
476 
477  /*
478  * Save the default opmask.
479  */
480  PLPERL_SET_OPMASK(plperl_opmask);
481 
482  /*
483  * Create the first Perl interpreter, but only partially initialize it.
484  */
486 
487  inited = true;
488 }
static char plperl_opmask[MAXO]
Definition: plperl.c:240
static PerlInterpreter * plperl_init_interp(void)
Definition: plperl.c:708
static HTAB * plperl_proc_hash
Definition: plperl.c:226
#define TEXTDOMAIN
Definition: plperl.c:44
#define HASH_ELEM
Definition: hsearch.h:85
static char * plperl_on_init
Definition: plperl.c:234
struct plperl_interp_desc plperl_interp_desc
Size entrysize
Definition: hsearch.h:72
#define gettext_noop(x)
Definition: c.h:1193
unsigned int Oid
Definition: postgres_ext.h:31
Definition: guc.h:75
void EmitWarningsOnPlaceholders(const char *className)
Definition: guc.c:9033
Definition: guc.h:72
#define HASH_BLOBS
Definition: hsearch.h:86
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:8980
HTAB * hash_create(const char *tabname, long nelem, HASHCTL *info, int flags)
Definition: dynahash.c:326
Size keysize
Definition: hsearch.h:71
static char * plperl_on_plperl_init
Definition: plperl.c:235
static bool plperl_use_strict
Definition: plperl.c:233
static char * plperl_on_plperlu_init
Definition: plperl.c:236
struct plperl_proc_key plperl_proc_key
static PerlInterpreter * plperl_held_interp
Definition: plperl.c:230
static HTAB * plperl_interp_hash
Definition: plperl.c:225
void pg_bindtextdomain(const char *domain)
Definition: miscinit.c:1681
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:8894
struct plperl_proc_ptr plperl_proc_ptr

◆ _sv_to_datum_finfo()

static void _sv_to_datum_finfo ( Oid  typid,
FmgrInfo finfo,
Oid typioparam 
)
static

Definition at line 1287 of file plperl.c.

References fmgr_info(), and getTypeInputInfo().

Referenced by plperl_array_to_datum(), and plperl_sv_to_datum().

1288 {
1289  Oid typinput;
1290 
1291  /* XXX would be better to cache these lookups */
1292  getTypeInputInfo(typid,
1293  &typinput, typioparam);
1294  fmgr_info(typinput, finfo);
1295 }
unsigned int Oid
Definition: postgres_ext.h:31
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2756

◆ activate_interpreter()

static void activate_interpreter ( plperl_interp_desc interp_desc)
static

Definition at line 687 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().

688 {
689  if (interp_desc && plperl_active_interp != interp_desc)
690  {
691  Assert(interp_desc->interp);
692  PERL_SET_CONTEXT(interp_desc->interp);
693  /* trusted iff user_id isn't InvalidOid */
694  set_interp_require(OidIsValid(interp_desc->user_id));
695  plperl_active_interp = interp_desc;
696  }
697 }
static void set_interp_require(bool trusted)
Definition: plperl.c:492
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
PerlInterpreter * interp
Definition: plperl.c:88
#define OidIsValid(objectId)
Definition: c.h:706
#define Assert(condition)
Definition: c.h:800

◆ array_to_datum_internal()

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 1168 of file plperl.c.

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

Referenced by plperl_array_to_datum().

1172 {
1173  dTHX;
1174  int i;
1175  int len = av_len(av) + 1;
1176 
1177  for (i = 0; i < len; i++)
1178  {
1179  /* fetch the array element */
1180  SV **svp = av_fetch(av, i, FALSE);
1181 
1182  /* see if this element is an array, if so get that */
1183  SV *sav = svp ? get_perl_array_ref(*svp) : NULL;
1184 
1185  /* multi-dimensional array? */
1186  if (sav)
1187  {
1188  AV *nav = (AV *) SvRV(sav);
1189 
1190  /* dimensionality checks */
1191  if (cur_depth + 1 > MAXDIM)
1192  ereport(ERROR,
1193  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1194  errmsg("number of array dimensions (%d) exceeds the maximum allowed (%d)",
1195  cur_depth + 1, MAXDIM)));
1196 
1197  /* set size when at first element in this level, else compare */
1198  if (i == 0 && *ndims == cur_depth)
1199  {
1200  dims[*ndims] = av_len(nav) + 1;
1201  (*ndims)++;
1202  }
1203  else if (av_len(nav) + 1 != dims[cur_depth])
1204  ereport(ERROR,
1205  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1206  errmsg("multidimensional arrays must have array expressions with matching dimensions")));
1207 
1208  /* recurse to fetch elements of this sub-array */
1209  array_to_datum_internal(nav, astate,
1210  ndims, dims, cur_depth + 1,
1211  arraytypid, elemtypid, typmod,
1212  finfo, typioparam);
1213  }
1214  else
1215  {
1216  Datum dat;
1217  bool isnull;
1218 
1219  /* scalar after some sub-arrays at same level? */
1220  if (*ndims != cur_depth)
1221  ereport(ERROR,
1222  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1223  errmsg("multidimensional arrays must have array expressions with matching dimensions")));
1224 
1225  dat = plperl_sv_to_datum(svp ? *svp : NULL,
1226  elemtypid,
1227  typmod,
1228  NULL,
1229  finfo,
1230  typioparam,
1231  &isnull);
1232 
1233  (void) accumArrayResult(astate, dat, isnull,
1234  elemtypid, CurrentMemoryContext);
1235  }
1236  }
1237 }
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1141
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
#define dTHX
Definition: ppport.h:3208
#define MAXDIM
Definition: c.h:597
int errcode(int sqlerrcode)
Definition: elog.c:691
#define ERROR
Definition: elog.h:43
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uintptr_t Datum
Definition: postgres.h:367
struct @17::@18 av[32]
#define ereport(elevel,...)
Definition: elog.h:155
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5080
int errmsg(const char *fmt,...)
Definition: elog.c:902
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:1168

◆ boot_DynaLoader()

EXTERN_C void boot_DynaLoader ( pTHX_ CV *  cv)

Referenced by plperl_init_shared_libs().

◆ boot_PostgreSQL__InServer__SPI()

EXTERN_C void boot_PostgreSQL__InServer__SPI ( pTHX_ CV *  cv)

Referenced by select_perl_context().

◆ boot_PostgreSQL__InServer__Util()

EXTERN_C void boot_PostgreSQL__InServer__Util ( pTHX_ CV *  cv)

Referenced by plperl_init_shared_libs().

◆ check_spi_usage_allowed()

static void check_spi_usage_allowed ( void  )
static

Definition at line 3092 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().

3093 {
3094  /* see comment in plperl_fini() */
3095  if (plperl_ending)
3096  {
3097  /* simple croak as we don't want to involve PostgreSQL code */
3098  croak("SPI functions can not be used in END blocks");
3099  }
3100 }
static bool plperl_ending
Definition: plperl.c:238

◆ compile_plperl_function()

static plperl_proc_desc * compile_plperl_function ( Oid  fn_oid,
bool  is_trigger,
bool  is_event_trigger 
)
static

Definition at line 2700 of file plperl.c.

References activate_interpreter(), ALLOCSET_SMALL_SIZES, AllocSetContextCreate, 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, 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(), 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(), MemoryContextSetIdentifier(), MemoryContextSwitchTo(), NameStr, plperl_proc_desc::nargs, NIL, 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, pstrdup(), 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, type_is_rowtype(), TYPEOID, plperl_proc_key::user_id, and validate_plperl_function().

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

2701 {
2702  HeapTuple procTup;
2703  Form_pg_proc procStruct;
2704  plperl_proc_key proc_key;
2705  plperl_proc_ptr *proc_ptr;
2706  plperl_proc_desc *volatile prodesc = NULL;
2707  volatile MemoryContext proc_cxt = NULL;
2709  ErrorContextCallback plperl_error_context;
2710 
2711  /* We'll need the pg_proc tuple in any case... */
2712  procTup = SearchSysCache1(PROCOID, ObjectIdGetDatum(fn_oid));
2713  if (!HeapTupleIsValid(procTup))
2714  elog(ERROR, "cache lookup failed for function %u", fn_oid);
2715  procStruct = (Form_pg_proc) GETSTRUCT(procTup);
2716 
2717  /*
2718  * Try to find function in plperl_proc_hash. The reason for this
2719  * overcomplicated-seeming lookup procedure is that we don't know whether
2720  * it's plperl or plperlu, and don't want to spend a lookup in pg_language
2721  * to find out.
2722  */
2723  proc_key.proc_id = fn_oid;
2724  proc_key.is_trigger = is_trigger;
2725  proc_key.user_id = GetUserId();
2726  proc_ptr = hash_search(plperl_proc_hash, &proc_key,
2727  HASH_FIND, NULL);
2728  if (validate_plperl_function(proc_ptr, procTup))
2729  {
2730  /* Found valid plperl entry */
2731  ReleaseSysCache(procTup);
2732  return proc_ptr->proc_ptr;
2733  }
2734 
2735  /* If not found or obsolete, maybe it's plperlu */
2736  proc_key.user_id = InvalidOid;
2737  proc_ptr = hash_search(plperl_proc_hash, &proc_key,
2738  HASH_FIND, NULL);
2739  if (validate_plperl_function(proc_ptr, procTup))
2740  {
2741  /* Found valid plperlu entry */
2742  ReleaseSysCache(procTup);
2743  return proc_ptr->proc_ptr;
2744  }
2745 
2746  /************************************************************
2747  * If we haven't found it in the hashtable, we analyze
2748  * the function's arguments and return type and store
2749  * the in-/out-functions in the prodesc block,
2750  * then we load the procedure into the Perl interpreter,
2751  * and last we create a new hashtable entry for it.
2752  ************************************************************/
2753 
2754  /* Set a callback for reporting compilation errors */
2755  plperl_error_context.callback = plperl_compile_callback;
2756  plperl_error_context.previous = error_context_stack;
2757  plperl_error_context.arg = NameStr(procStruct->proname);
2758  error_context_stack = &plperl_error_context;
2759 
2760  PG_TRY();
2761  {
2762  HeapTuple langTup;
2763  HeapTuple typeTup;
2764  Form_pg_language langStruct;
2765  Form_pg_type typeStruct;
2766  Datum protrftypes_datum;
2767  Datum prosrcdatum;
2768  bool isnull;
2769  char *proc_source;
2770  MemoryContext oldcontext;
2771 
2772  /************************************************************
2773  * Allocate a context that will hold all PG data for the procedure.
2774  ************************************************************/
2776  "PL/Perl function",
2778 
2779  /************************************************************
2780  * Allocate and fill a new procedure description block.
2781  * struct prodesc and subsidiary data must all live in proc_cxt.
2782  ************************************************************/
2783  oldcontext = MemoryContextSwitchTo(proc_cxt);
2784  prodesc = (plperl_proc_desc *) palloc0(sizeof(plperl_proc_desc));
2785  prodesc->proname = pstrdup(NameStr(procStruct->proname));
2786  MemoryContextSetIdentifier(proc_cxt, prodesc->proname);
2787  prodesc->fn_cxt = proc_cxt;
2788  prodesc->fn_refcount = 0;
2789  prodesc->fn_xmin = HeapTupleHeaderGetRawXmin(procTup->t_data);
2790  prodesc->fn_tid = procTup->t_self;
2791  prodesc->nargs = procStruct->pronargs;
2792  prodesc->arg_out_func = (FmgrInfo *) palloc0(prodesc->nargs * sizeof(FmgrInfo));
2793  prodesc->arg_is_rowtype = (bool *) palloc0(prodesc->nargs * sizeof(bool));
2794  prodesc->arg_arraytype = (Oid *) palloc0(prodesc->nargs * sizeof(Oid));
2795  MemoryContextSwitchTo(oldcontext);
2796 
2797  /* Remember if function is STABLE/IMMUTABLE */
2798  prodesc->fn_readonly =
2799  (procStruct->provolatile != PROVOLATILE_VOLATILE);
2800 
2801  /* Fetch protrftypes */
2802  protrftypes_datum = SysCacheGetAttr(PROCOID, procTup,
2803  Anum_pg_proc_protrftypes, &isnull);
2804  MemoryContextSwitchTo(proc_cxt);
2805  prodesc->trftypes = isnull ? NIL : oid_array_to_list(protrftypes_datum);
2806  MemoryContextSwitchTo(oldcontext);
2807 
2808  /************************************************************
2809  * Lookup the pg_language tuple by Oid
2810  ************************************************************/
2811  langTup = SearchSysCache1(LANGOID,
2812  ObjectIdGetDatum(procStruct->prolang));
2813  if (!HeapTupleIsValid(langTup))
2814  elog(ERROR, "cache lookup failed for language %u",
2815  procStruct->prolang);
2816  langStruct = (Form_pg_language) GETSTRUCT(langTup);
2817  prodesc->lang_oid = langStruct->oid;
2818  prodesc->lanpltrusted = langStruct->lanpltrusted;
2819  ReleaseSysCache(langTup);
2820 
2821  /************************************************************
2822  * Get the required information for input conversion of the
2823  * return value.
2824  ************************************************************/
2825  if (!is_trigger && !is_event_trigger)
2826  {
2827  Oid rettype = procStruct->prorettype;
2828 
2829  typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(rettype));
2830  if (!HeapTupleIsValid(typeTup))
2831  elog(ERROR, "cache lookup failed for type %u", rettype);
2832  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2833 
2834  /* Disallow pseudotype result, except VOID or RECORD */
2835  if (typeStruct->typtype == TYPTYPE_PSEUDO)
2836  {
2837  if (rettype == VOIDOID ||
2838  rettype == RECORDOID)
2839  /* okay */ ;
2840  else if (rettype == TRIGGEROID ||
2841  rettype == EVENT_TRIGGEROID)
2842  ereport(ERROR,
2843  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2844  errmsg("trigger functions can only be called "
2845  "as triggers")));
2846  else
2847  ereport(ERROR,
2848  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2849  errmsg("PL/Perl functions cannot return type %s",
2850  format_type_be(rettype))));
2851  }
2852 
2853  prodesc->result_oid = rettype;
2854  prodesc->fn_retisset = procStruct->proretset;
2855  prodesc->fn_retistuple = type_is_rowtype(rettype);
2856 
2857  prodesc->fn_retisarray =
2858  (typeStruct->typlen == -1 && typeStruct->typelem);
2859 
2860  fmgr_info_cxt(typeStruct->typinput,
2861  &(prodesc->result_in_func),
2862  proc_cxt);
2863  prodesc->result_typioparam = getTypeIOParam(typeTup);
2864 
2865  ReleaseSysCache(typeTup);
2866  }
2867 
2868  /************************************************************
2869  * Get the required information for output conversion
2870  * of all procedure arguments
2871  ************************************************************/
2872  if (!is_trigger && !is_event_trigger)
2873  {
2874  int i;
2875 
2876  for (i = 0; i < prodesc->nargs; i++)
2877  {
2878  Oid argtype = procStruct->proargtypes.values[i];
2879 
2880  typeTup = SearchSysCache1(TYPEOID, ObjectIdGetDatum(argtype));
2881  if (!HeapTupleIsValid(typeTup))
2882  elog(ERROR, "cache lookup failed for type %u", argtype);
2883  typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
2884 
2885  /* Disallow pseudotype argument, except RECORD */
2886  if (typeStruct->typtype == TYPTYPE_PSEUDO &&
2887  argtype != RECORDOID)
2888  ereport(ERROR,
2889  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2890  errmsg("PL/Perl functions cannot accept type %s",
2891  format_type_be(argtype))));
2892 
2893  if (type_is_rowtype(argtype))
2894  prodesc->arg_is_rowtype[i] = true;
2895  else
2896  {
2897  prodesc->arg_is_rowtype[i] = false;
2898  fmgr_info_cxt(typeStruct->typoutput,
2899  &(prodesc->arg_out_func[i]),
2900  proc_cxt);
2901  }
2902 
2903  /* Identify array-type arguments */
2904  if (typeStruct->typelem != 0 && typeStruct->typlen == -1)
2905  prodesc->arg_arraytype[i] = argtype;
2906  else
2907  prodesc->arg_arraytype[i] = InvalidOid;
2908 
2909  ReleaseSysCache(typeTup);
2910  }
2911  }
2912 
2913  /************************************************************
2914  * create the text of the anonymous subroutine.
2915  * we do not use a named subroutine so that we can call directly
2916  * through the reference.
2917  ************************************************************/
2918  prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
2919  Anum_pg_proc_prosrc, &isnull);
2920  if (isnull)
2921  elog(ERROR, "null prosrc");
2922  proc_source = TextDatumGetCString(prosrcdatum);
2923 
2924  /************************************************************
2925  * Create the procedure in the appropriate interpreter
2926  ************************************************************/
2927 
2929 
2930  prodesc->interp = plperl_active_interp;
2931 
2932  plperl_create_sub(prodesc, proc_source, fn_oid);
2933 
2934  activate_interpreter(oldinterp);
2935 
2936  pfree(proc_source);
2937 
2938  if (!prodesc->reference) /* can this happen? */
2939  elog(ERROR, "could not create PL/Perl internal procedure");
2940 
2941  /************************************************************
2942  * OK, link the procedure into the correct hashtable entry.
2943  * Note we assume that the hashtable entry either doesn't exist yet,
2944  * or we already cleared its proc_ptr during the validation attempts
2945  * above. So no need to decrement an old refcount here.
2946  ************************************************************/
2947  proc_key.user_id = prodesc->lanpltrusted ? GetUserId() : InvalidOid;
2948 
2949  proc_ptr = hash_search(plperl_proc_hash, &proc_key,
2950  HASH_ENTER, NULL);
2951  /* We assume these two steps can't throw an error: */
2952  proc_ptr->proc_ptr = prodesc;
2953  increment_prodesc_refcount(prodesc);
2954  }
2955  PG_CATCH();
2956  {
2957  /*
2958  * If we got as far as creating a reference, we should be able to use
2959  * free_plperl_function() to clean up. If not, then at most we have
2960  * some PG memory resources in proc_cxt, which we can just delete.
2961  */
2962  if (prodesc && prodesc->reference)
2963  free_plperl_function(prodesc);
2964  else if (proc_cxt)
2965  MemoryContextDelete(proc_cxt);
2966 
2967  /* Be sure to restore the previous interpreter, too, for luck */
2968  activate_interpreter(oldinterp);
2969 
2970  PG_RE_THROW();
2971  }
2972  PG_END_TRY();
2973 
2974  /* restore previous error callback */
2975  error_context_stack = plperl_error_context.previous;
2976 
2977  ReleaseSysCache(procTup);
2978 
2979  return prodesc;
2980 }
#define NIL
Definition: pg_list.h:65
Definition: fmgr.h:56
static bool validate_plperl_function(plperl_proc_ptr *proc_ptr, HeapTuple procTup)
Definition: plperl.c:2653
List * trftypes
Definition: plperl.c:113
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
static HTAB * plperl_proc_hash
Definition: plperl.c:226
bool * arg_is_rowtype
Definition: plperl.c:125
#define GETSTRUCT(TUP)
Definition: htup_details.h:655
static void select_perl_context(bool trusted)
Definition: plperl.c:555
Oid GetUserId(void)
Definition: miscinit.c:476
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:691
MemoryContext fn_cxt
Definition: plperl.c:105
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
unsigned int Oid
Definition: postgres_ext.h:31
void(* callback)(void *arg)
Definition: elog.h:243
FmgrInfo * arg_out_func
Definition: plperl.c:124
struct ErrorContextCallback * previous
Definition: elog.h:242
HeapTupleHeader t_data
Definition: htup.h:68
ErrorContextCallback * error_context_stack
Definition: elog.c:92
List * oid_array_to_list(Datum datum)
Definition: pg_proc.c:1137
void pfree(void *pointer)
Definition: mcxt.c:1057
bool fn_retisset
Definition: plperl.c:116
#define ObjectIdGetDatum(X)
Definition: postgres.h:507
#define ERROR
Definition: elog.h:43
plperl_proc_desc * proc_ptr
Definition: plperl.c:166
ItemPointerData fn_tid
Definition: plperl.c:108
ItemPointerData t_self
Definition: htup.h:65
FmgrInfo result_in_func
Definition: plperl.c:120
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:687
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2548
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:129
Oid result_typioparam
Definition: plperl.c:121
Oid * arg_arraytype
Definition: plperl.c:126
MemoryContext TopMemoryContext
Definition: mcxt.c:44
unsigned long fn_refcount
Definition: plperl.c:106
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1115
#define TextDatumGetCString(d)
Definition: builtins.h:87
Oid is_trigger
Definition: plperl.c:159
void * palloc0(Size size)
Definition: mcxt.c:981
uintptr_t Datum
Definition: postgres.h:367
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1163
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
Definition: syscache.c:1376
static void free_plperl_function(plperl_proc_desc *prodesc)
Definition: plperl.c:2682
FormData_pg_proc * Form_pg_proc
Definition: pg_proc.h:133
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
bool fn_readonly
Definition: plperl.c:111
#define PG_CATCH()
Definition: elog.h:319
#define HeapTupleIsValid(tuple)
Definition: htup.h:78
SV * reference
Definition: plperl.c:109
FormData_pg_type * Form_pg_type
Definition: pg_type.h:255
void MemoryContextSetIdentifier(MemoryContext context, const char *id)
Definition: mcxt.c:330
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:308
#define PG_RE_THROW()
Definition: elog.h:350
static void plperl_compile_callback(void *arg)
Definition: plperl.c:4117
FormData_pg_language * Form_pg_language
Definition: pg_language.h:65
int errmsg(const char *fmt,...)
Definition: elog.c:902
bool fn_retistuple
Definition: plperl.c:115
#define elog(elevel,...)
Definition: elog.h:228
int i
Oid getTypeIOParam(HeapTuple typeTuple)
Definition: lsyscache.c:2196
#define NameStr(name)
Definition: c.h:677
static void plperl_create_sub(plperl_proc_desc *desc, const char *s, Oid fn_oid)
Definition: plperl.c:2080
char * proname
Definition: plperl.c:104
TransactionId fn_xmin
Definition: plperl.c:107
#define PG_TRY()
Definition: elog.h:309
plperl_interp_desc * interp
Definition: plperl.c:110
#define PG_END_TRY()
Definition: elog.h:334
bool lanpltrusted
Definition: plperl.c:114
bool fn_retisarray
Definition: plperl.c:117

◆ free_plperl_function()

static void free_plperl_function ( plperl_proc_desc prodesc)
static

Definition at line 2682 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, plperl_proc_desc::reference, and SvREFCNT_dec_current().

Referenced by compile_plperl_function().

2683 {
2684  Assert(prodesc->fn_refcount == 0);
2685  /* Release CODE reference, if we have one, from the appropriate interp */
2686  if (prodesc->reference)
2687  {
2689 
2690  activate_interpreter(prodesc->interp);
2691  SvREFCNT_dec_current(prodesc->reference);
2692  activate_interpreter(oldinterp);
2693  }
2694  /* Release all PG-owned data for this proc */
2695  MemoryContextDelete(prodesc->fn_cxt);
2696 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
MemoryContext fn_cxt
Definition: plperl.c:105
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:687
unsigned long fn_refcount
Definition: plperl.c:106
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:312
#define Assert(condition)
Definition: c.h:800
SV * reference
Definition: plperl.c:109
plperl_interp_desc * interp
Definition: plperl.c:110

◆ get_perl_array_ref()

static SV * get_perl_array_ref ( SV *  sv)
static

Definition at line 1141 of file plperl.c.

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

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

1142 {
1143  dTHX;
1144 
1145  if (SvOK(sv) && SvROK(sv))
1146  {
1147  if (SvTYPE(SvRV(sv)) == SVt_PVAV)
1148  return sv;
1149  else if (sv_isa(sv, "PostgreSQL::InServer::ARRAY"))
1150  {
1151  HV *hv = (HV *) SvRV(sv);
1152  SV **sav = hv_fetch_string(hv, "array");
1153 
1154  if (*sav && SvOK(*sav) && SvROK(*sav) &&
1155  SvTYPE(SvRV(*sav)) == SVt_PVAV)
1156  return *sav;
1157 
1158  elog(ERROR, "could not get array reference from PostgreSQL::InServer::ARRAY object");
1159  }
1160  }
1161  return NULL;
1162 }
#define dTHX
Definition: ppport.h:3208
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:4082
#define ERROR
Definition: elog.h:43
#define elog(elevel,...)
Definition: elog.h:228

◆ hek2cstr()

static char * hek2cstr ( HE *  he)
static

Definition at line 323 of file plperl.c.

References dTHX, HeUTF8, and sv2cstr().

Referenced by plperl_build_tuple_result(), and plperl_modify_tuple().

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

◆ hv_fetch_string()

static SV ** hv_fetch_string ( HV *  hv,
const char *  key 
)
static

Definition at line 4082 of file plperl.c.

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

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

4083 {
4084  dTHX;
4085  int32 hlen;
4086  char *hkey;
4087  SV **ret;
4088 
4089  hkey = pg_server_to_any(key, strlen(key), PG_UTF8);
4090 
4091  /* See notes in hv_store_string */
4092  hlen = -(int) strlen(hkey);
4093  ret = hv_fetch(hv, hkey, hlen, 0);
4094 
4095  if (hkey != key)
4096  pfree(hkey);
4097 
4098  return ret;
4099 }
#define dTHX
Definition: ppport.h:3208
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:692
signed int int32
Definition: c.h:417
void pfree(void *pointer)
Definition: mcxt.c:1057

◆ hv_store_string()

static SV ** hv_store_string ( HV *  hv,
const char *  key,
SV *  val 
)
static

Definition at line 4055 of file plperl.c.

References dTHX, 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().

4056 {
4057  dTHX;
4058  int32 hlen;
4059  char *hkey;
4060  SV **ret;
4061 
4062  hkey = pg_server_to_any(key, strlen(key), PG_UTF8);
4063 
4064  /*
4065  * hv_store() recognizes a negative klen parameter as meaning a UTF-8
4066  * encoded key.
4067  */
4068  hlen = -(int) strlen(hkey);
4069  ret = hv_store(hv, hkey, hlen, val, 0);
4070 
4071  if (hkey != key)
4072  pfree(hkey);
4073 
4074  return ret;
4075 }
#define dTHX
Definition: ppport.h:3208
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:692
signed int int32
Definition: c.h:417
void pfree(void *pointer)
Definition: mcxt.c:1057
long val
Definition: informix.c:664

◆ make_array_ref()

static SV * make_array_ref ( plperl_array_info info,
int  first,
int  last 
)
static

Definition at line 1578 of file plperl.c.

References cstr2sv(), DatumGetPointer, dTHX, 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().

1579 {
1580  dTHX;
1581  int i;
1582  AV *result = newAV();
1583 
1584  for (i = first; i < last; i++)
1585  {
1586  if (info->nulls[i])
1587  {
1588  /*
1589  * We can't use &PL_sv_undef here. See "AVs, HVs and undefined
1590  * values" in perlguts.
1591  */
1592  av_push(result, newSV(0));
1593  }
1594  else
1595  {
1596  Datum itemvalue = info->elements[i];
1597 
1598  if (info->transform_proc.fn_oid)
1599  av_push(result, (SV *) DatumGetPointer(FunctionCall1(&info->transform_proc, itemvalue)));
1600  else if (info->elem_is_rowtype)
1601  /* Handle composite type elements */
1602  av_push(result, plperl_hash_from_datum(itemvalue));
1603  else
1604  {
1605  char *val = OutputFunctionCall(&info->proc, itemvalue);
1606 
1607  av_push(result, cstr2sv(val));
1608  }
1609  }
1610  }
1611  return newRV_noinc((SV *) result);
1612 }
FmgrInfo transform_proc
Definition: plperl.c:218
#define dTHX
Definition: ppport.h:3208
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2984
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1577
#define newRV_noinc(a)
Definition: ppport.h:4456
uintptr_t Datum
Definition: postgres.h:367
bool elem_is_rowtype
Definition: plperl.c:213
static SV * cstr2sv(const char *str)
Oid fn_oid
Definition: fmgr.h:59
FmgrInfo proc
Definition: plperl.c:217
#define DatumGetPointer(X)
Definition: postgres.h:549
bool * nulls
Definition: plperl.c:215
int i
Datum * elements
Definition: plperl.c:214
#define FunctionCall1(flinfo, arg1)
Definition: fmgr.h:644
long val
Definition: informix.c:664

◆ PG_FUNCTION_INFO_V1() [1/6]

◆ PG_FUNCTION_INFO_V1() [2/6]

PG_FUNCTION_INFO_V1 ( plperl_inline_handler  )

◆ PG_FUNCTION_INFO_V1() [3/6]

PG_FUNCTION_INFO_V1 ( plperl_validator  )

◆ PG_FUNCTION_INFO_V1() [4/6]

PG_FUNCTION_INFO_V1 ( plperlu_call_handler  )

◆ PG_FUNCTION_INFO_V1() [5/6]

PG_FUNCTION_INFO_V1 ( plperlu_inline_handler  )

◆ PG_FUNCTION_INFO_V1() [6/6]

PG_FUNCTION_INFO_V1 ( plperlu_validator  )

◆ plperl_array_to_datum()

static Datum plperl_array_to_datum ( SV *  src,
Oid  typid,
int32  typmod 
)
static

Definition at line 1243 of file plperl.c.

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

Referenced by plperl_sv_to_datum().

1244 {
1245  dTHX;
1246  ArrayBuildState *astate;
1247  Oid elemtypid;
1248  FmgrInfo finfo;
1249  Oid typioparam;
1250  int dims[MAXDIM];
1251  int lbs[MAXDIM];
1252  int ndims = 1;
1253  int i;
1254 
1255  elemtypid = get_element_type(typid);
1256  if (!elemtypid)
1257  ereport(ERROR,
1258  (errcode(ERRCODE_DATATYPE_MISMATCH),
1259  errmsg("cannot convert Perl array to non-array type %s",
1260  format_type_be(typid))));
1261 
1262  astate = initArrayResult(elemtypid, CurrentMemoryContext, true);
1263 
1264  _sv_to_datum_finfo(elemtypid, &finfo, &typioparam);
1265 
1266  memset(dims, 0, sizeof(dims));
1267  dims[0] = av_len((AV *) SvRV(src)) + 1;
1268 
1269  array_to_datum_internal((AV *) SvRV(src), astate,
1270  &ndims, dims, 1,
1271  typid, elemtypid, typmod,
1272  &finfo, typioparam);
1273 
1274  /* ensure we get zero-D array for no inputs, as per PG convention */
1275  if (dims[0] <= 0)
1276  ndims = 0;
1277 
1278  for (i = 0; i < ndims; i++)
1279  lbs[i] = 1;
1280 
1281  return makeMdArrayResult(astate, ndims, dims, lbs,
1282  CurrentMemoryContext, true);
1283 }
Datum makeMdArrayResult(ArrayBuildState *astate, int ndims, int *dims, int *lbs, MemoryContext rcontext, bool release)
Definition: arrayfuncs.c:5176
Definition: fmgr.h:56
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5041
#define dTHX
Definition: ppport.h:3208
#define MAXDIM
Definition: c.h:597
Oid get_element_type(Oid typid)
Definition: lsyscache.c:2641
int errcode(int sqlerrcode)
Definition: elog.c:691
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
#define ERROR
Definition: elog.h:43
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:902
int i
static void _sv_to_datum_finfo(Oid typid, FmgrInfo *finfo, Oid *typioparam)
Definition: plperl.c:1287
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:1168

◆ plperl_build_tuple_result()

static HeapTuple plperl_build_tuple_result ( HV *  perlhash,
TupleDesc  td 
)
static

Definition at line 1078 of file plperl.c.

References dTHX, ereport, errcode(), errmsg(), ERROR, heap_form_tuple(), hek2cstr(), InvalidOid, sort-test::key, TupleDescData::natts, palloc(), palloc0(), pfree(), plperl_sv_to_datum(), SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), TupleDescAttr, val, and values.

Referenced by plperl_hash_to_datum(), and plperl_return_next_internal().

1079 {
1080  dTHX;
1081  Datum *values;
1082  bool *nulls;
1083  HE *he;
1084  HeapTuple tup;
1085 
1086  values = palloc0(sizeof(Datum) * td->natts);
1087  nulls = palloc(sizeof(bool) * td->natts);
1088  memset(nulls, true, sizeof(bool) * td->natts);
1089 
1090  hv_iterinit(perlhash);
1091  while ((he = hv_iternext(perlhash)))
1092  {
1093  SV *val = HeVAL(he);
1094  char *key = hek2cstr(he);
1095  int attn = SPI_fnumber(td, key);
1096  Form_pg_attribute attr = TupleDescAttr(td, attn - 1);
1097 
1098  if (attn == SPI_ERROR_NOATTRIBUTE)
1099  ereport(ERROR,
1100  (errcode(ERRCODE_UNDEFINED_COLUMN),
1101  errmsg("Perl hash contains nonexistent column \"%s\"",
1102  key)));
1103  if (attn <= 0)
1104  ereport(ERROR,
1105  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1106  errmsg("cannot set system attribute \"%s\"",
1107  key)));
1108 
1109  values[attn - 1] = plperl_sv_to_datum(val,
1110  attr->atttypid,
1111  attr->atttypmod,
1112  NULL,
1113  NULL,
1114  InvalidOid,
1115  &nulls[attn - 1]);
1116 
1117  pfree(key);
1118  }
1119  hv_iterinit(perlhash);
1120 
1121  tup = heap_form_tuple(td, values, nulls);
1122  pfree(values);
1123  pfree(nulls);
1124  return tup;
1125 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1024
static char * hek2cstr(HE *he)
Definition: plperl.c:323
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
#define dTHX
Definition: ppport.h:3208
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:691
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void * palloc0(Size size)
Definition: mcxt.c:981
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
static Datum values[MAXATTR]
Definition: bootstrap.c:165
void * palloc(Size size)
Definition: mcxt.c:950
int errmsg(const char *fmt,...)
Definition: elog.c:902
long val
Definition: informix.c:664

◆ plperl_call_handler()

Datum plperl_call_handler ( PG_FUNCTION_ARGS  )

Definition at line 1837 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_END_TRY, PG_FINALLY, PG_FUNCTION_INFO_V1(), PG_TRY, plperl_active_interp, plperl_event_trigger_handler(), plperl_func_handler(), plperl_inline_handler(), plperl_trigger_handler(), PointerGetDatum, and plperl_call_data::prodesc.

Referenced by plperl_modify_tuple(), and plperlu_call_handler().

1838 {
1839  Datum retval = (Datum) 0;
1840  plperl_call_data *volatile save_call_data = current_call_data;
1841  plperl_interp_desc *volatile oldinterp = plperl_active_interp;
1842  plperl_call_data this_call_data;
1843 
1844  /* Initialize current-call status record */
1845  MemSet(&this_call_data, 0, sizeof(this_call_data));
1846  this_call_data.fcinfo = fcinfo;
1847 
1848  PG_TRY();
1849  {
1850  current_call_data = &this_call_data;
1851  if (CALLED_AS_TRIGGER(fcinfo))
1852  retval = PointerGetDatum(plperl_trigger_handler(fcinfo));
1853  else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
1854  {
1856  retval = (Datum) 0;
1857  }
1858  else
1859  retval = plperl_func_handler(fcinfo);
1860  }
1861  PG_FINALLY();
1862  {
1863  current_call_data = save_call_data;
1864  activate_interpreter(oldinterp);
1865  if (this_call_data.prodesc)
1866  decrement_prodesc_refcount(this_call_data.prodesc);
1867  }
1868  PG_END_TRY();
1869 
1870  return retval;
1871 }
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:40
static Datum plperl_func_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2387
#define decrement_prodesc_refcount(prodesc)
Definition: plperl.c:131
FunctionCallInfo fcinfo
Definition: plperl.c:176
#define PointerGetDatum(X)
Definition: postgres.h:556
#define MemSet(start, val, len)
Definition: c.h:1004
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
static void plperl_event_trigger_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2616
static Datum plperl_trigger_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2503
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:687
#define PG_FINALLY()
Definition: elog.h:326
uintptr_t Datum
Definition: postgres.h:367
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
plperl_proc_desc * prodesc
Definition: plperl.c:175
#define PG_TRY()
Definition: elog.h:309
static plperl_call_data * current_call_data
Definition: plperl.c:243
#define PG_END_TRY()
Definition: elog.h:334

◆ plperl_call_perl_event_trigger_func()

static void plperl_call_perl_event_trigger_func ( plperl_proc_desc desc,
FunctionCallInfo  fcinfo,
SV *  td 
)
static

Definition at line 2326 of file plperl.c.

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

Referenced by plperl_event_trigger_handler().

2329 {
2330  dTHX;
2331  dSP;
2332  SV *retval,
2333  *TDsv;
2334  int count;
2335 
2336  ENTER;
2337  SAVETMPS;
2338 
2339  TDsv = get_sv("main::_TD", 0);
2340  if (!TDsv)
2341  ereport(ERROR,
2342  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2343  errmsg("couldn't fetch $_TD")));
2344 
2345  save_item(TDsv); /* local $_TD */
2346  sv_setsv(TDsv, td);
2347 
2348  PUSHMARK(sp);
2349  PUTBACK;
2350 
2351  /* Do NOT use G_KEEPERR here */
2352  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2353 
2354  SPAGAIN;
2355 
2356  if (count != 1)
2357  {
2358  PUTBACK;
2359  FREETMPS;
2360  LEAVE;
2361  ereport(ERROR,
2362  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2363  errmsg("didn't get a return item from trigger function")));
2364  }
2365 
2366  if (SvTRUE(ERRSV))
2367  {
2368  (void) POPs;
2369  PUTBACK;
2370  FREETMPS;
2371  LEAVE;
2372  /* XXX need to find a way to determine a better errcode here */
2373  ereport(ERROR,
2374  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2376  }
2377 
2378  retval = newSVsv(POPs);
2379  (void) retval; /* silence compiler warning */
2380 
2381  PUTBACK;
2382  FREETMPS;
2383  LEAVE;
2384 }
#define dTHX
Definition: ppport.h:3208
int errcode(int sqlerrcode)
Definition: elog.c:691
#define ERRSV
Definition: ppport.h:3859
#define ERROR
Definition: elog.h:43
#define ereport(elevel,...)
Definition: elog.h:155
SV * reference
Definition: plperl.c:109
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1064
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define get_sv
Definition: ppport.h:3878
static char * sv2cstr(SV *sv)

◆ plperl_call_perl_func()

static SV * plperl_call_perl_func ( plperl_proc_desc desc,
FunctionCallInfo  fcinfo 
)
static

Definition at line 2165 of file plperl.c.

References plperl_proc_desc::arg_arraytype, plperl_proc_desc::arg_is_rowtype, plperl_proc_desc::arg_out_func, FunctionCallInfoBaseData::args, Assert, cstr2sv(), DatumGetPointer, dTHX, ereport, errcode(), errmsg(), ERROR, ERRSV, FunctionCallInfoBaseData::flinfo, FmgrInfo::fn_oid, get_func_signature(), get_transform_fromsql(), i, NullableDatum::isnull, plperl_proc_desc::lang_oid, plperl_proc_desc::nargs, 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(), plperl_proc_desc::trftypes, and NullableDatum::value.

Referenced by plperl_func_handler(), and plperl_inline_handler().

2166 {
2167  dTHX;
2168  dSP;
2169  SV *retval;
2170  int i;
2171  int count;
2172  Oid *argtypes = NULL;
2173  int nargs = 0;
2174 
2175  ENTER;
2176  SAVETMPS;
2177 
2178  PUSHMARK(SP);
2179  EXTEND(sp, desc->nargs);
2180 
2181  /* Get signature for true functions; inline blocks have no args. */
2182  if (fcinfo->flinfo->fn_oid)
2183  get_func_signature(fcinfo->flinfo->fn_oid, &argtypes, &nargs);
2184  Assert(nargs == desc->nargs);
2185 
2186  for (i = 0; i < desc->nargs; i++)
2187  {
2188  if (fcinfo->args[i].isnull)
2189  PUSHs(&PL_sv_undef);
2190  else if (desc->arg_is_rowtype[i])
2191  {
2192  SV *sv = plperl_hash_from_datum(fcinfo->args[i].value);
2193 
2194  PUSHs(sv_2mortal(sv));
2195  }
2196  else
2197  {
2198  SV *sv;
2199  Oid funcid;
2200 
2201  if (OidIsValid(desc->arg_arraytype[i]))
2202  sv = plperl_ref_from_pg_array(fcinfo->args[i].value, desc->arg_arraytype[i]);
2204  sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, fcinfo->args[i].value));
2205  else
2206  {
2207  char *tmp;
2208 
2209  tmp = OutputFunctionCall(&(desc->arg_out_func[i]),
2210  fcinfo->args[i].value);
2211  sv = cstr2sv(tmp);
2212  pfree(tmp);
2213  }
2214 
2215  PUSHs(sv_2mortal(sv));
2216  }
2217  }
2218  PUTBACK;
2219 
2220  /* Do NOT use G_KEEPERR here */
2221  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2222 
2223  SPAGAIN;
2224 
2225  if (count != 1)
2226  {
2227  PUTBACK;
2228  FREETMPS;
2229  LEAVE;
2230  ereport(ERROR,
2231  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2232  errmsg("didn't get a return item from function")));
2233  }
2234 
2235  if (SvTRUE(ERRSV))
2236  {
2237  (void) POPs;
2238  PUTBACK;
2239  FREETMPS;
2240  LEAVE;
2241  /* XXX need to find a way to determine a better errcode here */
2242  ereport(ERROR,
2243  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2245  }
2246 
2247  retval = newSVsv(POPs);
2248 
2249  PUTBACK;
2250  FREETMPS;
2251  LEAVE;
2252 
2253  return retval;
2254 }
static SV * plperl_ref_from_pg_array(Datum arg, Oid typid)
Definition: plperl.c:1465
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:2015
List * trftypes
Definition: plperl.c:113
bool * arg_is_rowtype
Definition: plperl.c:125
#define dTHX
Definition: ppport.h:3208
Oid get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
Definition: lsyscache.c:1613
#define PL_sv_undef
Definition: ppport.h:4129
int errcode(int sqlerrcode)
Definition: elog.c:691
#define ERRSV
Definition: ppport.h:3859
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2984
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo * arg_out_func
Definition: plperl.c:124
#define OidIsValid(objectId)
Definition: c.h:706
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1577
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
NullableDatum args[FLEXIBLE_ARRAY_MEMBER]
Definition: fmgr.h:95
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:664
Datum value
Definition: postgres.h:378
Oid * arg_arraytype
Definition: plperl.c:126
FmgrInfo * flinfo
Definition: fmgr.h:87
static SV * cstr2sv(const char *str)
Oid fn_oid
Definition: fmgr.h:59
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:800
SV * reference
Definition: plperl.c:109
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1064
#define DatumGetPointer(X)
Definition: postgres.h:549
int errmsg(const char *fmt,...)
Definition: elog.c:902
int i
plperl_proc_desc * prodesc
Definition: plperl.c:175
static plperl_call_data * current_call_data
Definition: plperl.c:243
static char * sv2cstr(SV *sv)

◆ plperl_call_perl_trigger_func()

static SV* plperl_call_perl_trigger_func ( plperl_proc_desc desc,
FunctionCallInfo  fcinfo,
SV *  td 
)
static

Definition at line 2258 of file plperl.c.

References FunctionCallInfoBaseData::context, cstr2sv(), dTHX, 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().

2260 {
2261  dTHX;
2262  dSP;
2263  SV *retval,
2264  *TDsv;
2265  int i,
2266  count;
2267  Trigger *tg_trigger = ((TriggerData *) fcinfo->context)->tg_trigger;
2268 
2269  ENTER;
2270  SAVETMPS;
2271 
2272  TDsv = get_sv("main::_TD", 0);
2273  if (!TDsv)
2274  ereport(ERROR,
2275  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2276  errmsg("couldn't fetch $_TD")));
2277 
2278  save_item(TDsv); /* local $_TD */
2279  sv_setsv(TDsv, td);
2280 
2281  PUSHMARK(sp);
2282  EXTEND(sp, tg_trigger->tgnargs);
2283 
2284  for (i = 0; i < tg_trigger->tgnargs; i++)
2285  PUSHs(sv_2mortal(cstr2sv(tg_trigger->tgargs[i])));
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 
2317  PUTBACK;
2318  FREETMPS;
2319  LEAVE;
2320 
2321  return retval;
2322 }
#define dTHX
Definition: ppport.h:3208
int errcode(int sqlerrcode)
Definition: elog.c:691
#define ERRSV
Definition: ppport.h:3859
fmNodePtr context
Definition: fmgr.h:88
#define ERROR
Definition: elog.h:43
char ** tgargs
Definition: reltrigger.h:41
static SV * cstr2sv(const char *str)
#define ereport(elevel,...)
Definition: elog.h:155
SV * reference
Definition: plperl.c:109
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1064
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define get_sv
Definition: ppport.h:3878
int i
int16 tgnargs
Definition: reltrigger.h:38
static char * sv2cstr(SV *sv)

◆ plperl_compile_callback()

static void plperl_compile_callback ( void *  arg)
static

Definition at line 4117 of file plperl.c.

References errcontext.

Referenced by compile_plperl_function().

4118 {
4119  char *procname = (char *) arg;
4120 
4121  if (procname)
4122  errcontext("compilation of PL/Perl function \"%s\"", procname);
4123 }
#define errcontext
Definition: elog.h:199
void * arg

◆ plperl_create_sub()

static void plperl_create_sub ( plperl_proc_desc desc,
const char *  s,
Oid  fn_oid 
)
static

Definition at line 2080 of file plperl.c.

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

Referenced by compile_plperl_function(), and plperl_inline_handler().

2081 {
2082  dTHX;
2083  dSP;
2084  char subname[NAMEDATALEN + 40];
2085  HV *pragma_hv = newHV();
2086  SV *subref = NULL;
2087  int count;
2088 
2089  sprintf(subname, "%s__%u", prodesc->proname, fn_oid);
2090 
2091  if (plperl_use_strict)
2092  hv_store_string(pragma_hv, "strict", (SV *) newAV());
2093 
2094  ENTER;
2095  SAVETMPS;
2096  PUSHMARK(SP);
2097  EXTEND(SP, 4);
2098  PUSHs(sv_2mortal(cstr2sv(subname)));
2099  PUSHs(sv_2mortal(newRV_noinc((SV *) pragma_hv)));
2100 
2101  /*
2102  * Use 'false' for $prolog in mkfunc, which is kept for compatibility in
2103  * case a module such as PostgreSQL::PLPerl::NYTprof replaces the function
2104  * compiler.
2105  */
2106  PUSHs(&PL_sv_no);
2107  PUSHs(sv_2mortal(cstr2sv(s)));
2108  PUTBACK;
2109 
2110  /*
2111  * G_KEEPERR seems to be needed here, else we don't recognize compile
2112  * errors properly. Perhaps it's because there's another level of eval
2113  * inside mksafefunc?
2114  */
2115  count = perl_call_pv("PostgreSQL::InServer::mkfunc",
2116  G_SCALAR | G_EVAL | G_KEEPERR);
2117  SPAGAIN;
2118 
2119  if (count == 1)
2120  {
2121  SV *sub_rv = (SV *) POPs;
2122 
2123  if (sub_rv && SvROK(sub_rv) && SvTYPE(SvRV(sub_rv)) == SVt_PVCV)
2124  {
2125  subref = newRV_inc(SvRV(sub_rv));
2126  }
2127  }
2128 
2129  PUTBACK;
2130  FREETMPS;
2131  LEAVE;
2132 
2133  if (SvTRUE(ERRSV))
2134  ereport(ERROR,
2135  (errcode(ERRCODE_SYNTAX_ERROR),
2137 
2138  if (!subref)
2139  ereport(ERROR,
2140  (errcode(ERRCODE_SYNTAX_ERROR),
2141  errmsg("didn't get a CODE reference from compiling function \"%s\"",
2142  prodesc->proname)));
2143 
2144  prodesc->reference = subref;
2145 }
#define dTHX
Definition: ppport.h:3208
int errcode(int sqlerrcode)
Definition: elog.c:691
#define ERRSV
Definition: ppport.h:3859
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:4055
NameData subname
#define NAMEDATALEN
#define sprintf
Definition: port.h:217
#define ERROR
Definition: elog.h:43
#define newRV_noinc(a)
Definition: ppport.h:4456
#define newRV_inc(sv)
Definition: ppport.h:4442
static SV * cstr2sv(const char *str)
#define ereport(elevel,...)
Definition: elog.h:155
static bool plperl_use_strict
Definition: plperl.c:233
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1064
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define PL_sv_no
Definition: ppport.h:4128
static char * sv2cstr(SV *sv)

◆ plperl_destroy_interp()

static void plperl_destroy_interp ( PerlInterpreter **  interp)
static

Definition at line 921 of file plperl.c.

References dTHX, and PERL_UNUSED_VAR.

Referenced by plperl_fini().

922 {
923  if (interp && *interp)
924  {
925  /*
926  * Only a very minimal destruction is performed: - just call END
927  * blocks.
928  *
929  * We could call perl_destruct() but we'd need to audit its actions
930  * very carefully and work-around any that impact us. (Calling
931  * sv_clean_objs() isn't an option because it's not part of perl's
932  * public API so isn't portably available.) Meanwhile END blocks can
933  * be used to perform manual cleanup.
934  */
935  dTHX;
936 
937  /* Run END blocks - based on perl's perl_destruct() */
938  if (PL_exit_flags & PERL_EXIT_DESTRUCT_END)
939  {
940  dJMPENV;
941  int x = 0;
942 
943  JMPENV_PUSH(x);
944  PERL_UNUSED_VAR(x);
945  if (PL_endav && !PL_minus_c)
946  call_list(PL_scopestack_ix, PL_endav);
947  JMPENV_POP;
948  }
949  LEAVE;
950  FREETMPS;
951 
952  *interp = NULL;
953  }
954 }
#define dTHX
Definition: ppport.h:3208
#define PERL_UNUSED_VAR(x)
Definition: ppport.h:3730

◆ plperl_event_trigger_build_args()

static SV* plperl_event_trigger_build_args ( FunctionCallInfo  fcinfo)
static

Definition at line 1729 of file plperl.c.

References FunctionCallInfoBaseData::context, cstr2sv(), dTHX, EventTriggerData::event, GetCommandTagName(), hv_store_string(), newRV_noinc, and EventTriggerData::tag.

Referenced by plperl_event_trigger_handler().

1730 {
1731  dTHX;
1732  EventTriggerData *tdata;
1733  HV *hv;
1734 
1735  hv = newHV();
1736 
1737  tdata = (EventTriggerData *) fcinfo->context;
1738 
1739  hv_store_string(hv, "event", cstr2sv(tdata->event));
1740  hv_store_string(hv, "tag", cstr2sv(GetCommandTagName(tdata->tag)));
1741 
1742  return newRV_noinc((SV *) hv);
1743 }
CommandTag tag
Definition: event_trigger.h:29
#define dTHX
Definition: ppport.h:3208
fmNodePtr context
Definition: fmgr.h:88
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:4055
const char * event
Definition: event_trigger.h:27
#define newRV_noinc(a)
Definition: ppport.h:4456
static SV * cstr2sv(const char *str)
const char * GetCommandTagName(CommandTag commandTag)
Definition: cmdtag.c:45

◆ plperl_event_trigger_handler()

static void plperl_event_trigger_handler ( PG_FUNCTION_ARGS  )
static

Definition at line 2616 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, SPI_OK_FINISH, and SvREFCNT_dec_current().

Referenced by plperl_call_handler().

2617 {
2618  plperl_proc_desc *prodesc;
2619  SV *svTD;
2620  ErrorContextCallback pl_error_context;
2621 
2622  /* Connect to SPI manager */
2623  if (SPI_connect() != SPI_OK_CONNECT)
2624  elog(ERROR, "could not connect to SPI manager");
2625 
2626  /* Find or compile the function */
2627  prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false, true);
2628  current_call_data->prodesc = prodesc;
2629  increment_prodesc_refcount(prodesc);
2630 
2631  /* Set a callback for error reporting */
2632  pl_error_context.callback = plperl_exec_callback;
2633  pl_error_context.previous = error_context_stack;
2634  pl_error_context.arg = prodesc->proname;
2635  error_context_stack = &pl_error_context;
2636 
2637  activate_interpreter(prodesc->interp);
2638 
2639  svTD = plperl_event_trigger_build_args(fcinfo);
2640  plperl_call_perl_event_trigger_func(prodesc, fcinfo, svTD);
2641 
2642  if (SPI_finish() != SPI_OK_FINISH)
2643  elog(ERROR, "SPI_finish() failed");
2644 
2645  /* Restore the previous error callback */
2646  error_context_stack = pl_error_context.previous;
2647 
2648  SvREFCNT_dec_current(svTD);
2649 }
#define SPI_OK_CONNECT
Definition: spi.h:53
int SPI_connect(void)
Definition: spi.c:90
int SPI_finish(void)
Definition: spi.c:177
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
static void plperl_call_perl_event_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo, SV *td)
Definition: plperl.c:2326
ErrorContextCallback * error_context_stack
Definition: elog.c:92
#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:2700
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:687
static void plperl_exec_callback(void *arg)
Definition: plperl.c:4105
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:129
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:312
#define SPI_OK_FINISH
Definition: spi.h:54
static SV * plperl_event_trigger_build_args(FunctionCallInfo fcinfo)
Definition: plperl.c:1729
#define elog(elevel,...)
Definition: elog.h:228
char * proname
Definition: plperl.c:104
plperl_proc_desc * prodesc
Definition: plperl.c:175
plperl_interp_desc * interp
Definition: plperl.c:110
static plperl_call_data * current_call_data
Definition: plperl.c:243

◆ plperl_exec_callback()

static void plperl_exec_callback ( void *  arg)
static

Definition at line 4105 of file plperl.c.

References errcontext.

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

4106 {
4107  char *procname = (char *) arg;
4108 
4109  if (procname)
4110  errcontext("PL/Perl function \"%s\"", procname);
4111 }
#define errcontext
Definition: elog.h:199
void * arg

◆ plperl_fini()

static void plperl_fini ( int  code,
Datum  arg 
)
static

Definition at line 511 of file plperl.c.

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

Referenced by select_perl_context().

512 {
513  HASH_SEQ_STATUS hash_seq;
514  plperl_interp_desc *interp_desc;
515 
516  elog(DEBUG3, "plperl_fini");
517 
518  /*
519  * Indicate that perl is terminating. Disables use of spi_* functions when
520  * running END/DESTROY code. See check_spi_usage_allowed(). Could be
521  * enabled in future, with care, using a transaction
522  * http://archives.postgresql.org/pgsql-hackers/2010-01/msg02743.php
523  */
524  plperl_ending = true;
525 
526  /* Only perform perl cleanup if we're exiting cleanly */
527  if (code)
528  {
529  elog(DEBUG3, "plperl_fini: skipped");
530  return;
531  }
532 
533  /* Zap the "held" interpreter, if we still have it */
535 
536  /* Zap any fully-initialized interpreters */
537  hash_seq_init(&hash_seq, plperl_interp_hash);
538  while ((interp_desc = hash_seq_search(&hash_seq)) != NULL)
539  {
540  if (interp_desc->interp)
541  {
542  activate_interpreter(interp_desc);
543  plperl_destroy_interp(&interp_desc->interp);
544  }
545  }
546 
547  elog(DEBUG3, "plperl_fini: done");
548 }
static bool plperl_ending
Definition: plperl.c:238
#define DEBUG3
Definition: elog.h:23
PerlInterpreter * interp
Definition: plperl.c:88
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:687
static void plperl_destroy_interp(PerlInterpreter **)
Definition: plperl.c:921
void * hash_seq_search(HASH_SEQ_STATUS *status)
Definition: dynahash.c:1401
void hash_seq_init(HASH_SEQ_STATUS *status, HTAB *hashp)
Definition: dynahash.c:1391
static PerlInterpreter * plperl_held_interp
Definition: plperl.c:230
static HTAB * plperl_interp_hash
Definition: plperl.c:225
#define elog(elevel,...)
Definition: elog.h:228

◆ plperl_func_handler()

static Datum plperl_func_handler ( PG_FUNCTION_ARGS  )
static

Definition at line 2387 of file plperl.c.

References activate_interpreter(), ReturnSetInfo::allowedModes, ErrorContextCallback::arg, ErrorContextCallback::callback, castNode, compile_plperl_function(), dTHX, elog, ereport, errcode(), errmsg(), ERROR, error_context_stack, ExprEndResult, plperl_proc_desc::fn_retisset, get_perl_array_ref(), i, increment_prodesc_refcount, plperl_proc_desc::interp, IsA, ReturnSetInfo::isDone, plperl_call_perl_func(), plperl_exec_callback(), plperl_return_next_internal(), 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_ext(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OPT_NONATOMIC, SvREFCNT_dec_current(), and plperl_call_data::tuple_store.

Referenced by plperl_call_handler().

2388 {
2389  bool nonatomic;
2390  plperl_proc_desc *prodesc;
2391  SV *perlret;
2392  Datum retval = 0;
2393  ReturnSetInfo *rsi;
2394  ErrorContextCallback pl_error_context;
2395 
2396  nonatomic = fcinfo->context &&
2397  IsA(fcinfo->context, CallContext) &&
2398  !castNode(CallContext, fcinfo->context)->atomic;
2399 
2400  if (SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0) != SPI_OK_CONNECT)
2401  elog(ERROR, "could not connect to SPI manager");
2402 
2403  prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false, false);
2404  current_call_data->prodesc = prodesc;
2405  increment_prodesc_refcount(prodesc);
2406 
2407  /* Set a callback for error reporting */
2408  pl_error_context.callback = plperl_exec_callback;
2409  pl_error_context.previous = error_context_stack;
2410  pl_error_context.arg = prodesc->proname;
2411  error_context_stack = &pl_error_context;
2412 
2413  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
2414 
2415  if (prodesc->fn_retisset)
2416  {
2417  /* Check context before allowing the call to go through */
2418  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2419  (rsi->allowedModes & SFRM_Materialize) == 0)
2420  ereport(ERROR,
2421  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2422  errmsg("set-valued function called in context that "
2423  "cannot accept a set")));
2424  }
2425 
2426  activate_interpreter(prodesc->interp);
2427 
2428  perlret = plperl_call_perl_func(prodesc, fcinfo);
2429 
2430  /************************************************************
2431  * Disconnect from SPI manager and then create the return
2432  * values datum (if the input function does a palloc for it
2433  * this must not be allocated in the SPI memory context
2434  * because SPI_finish would free it).
2435  ************************************************************/
2436  if (SPI_finish() != SPI_OK_FINISH)
2437  elog(ERROR, "SPI_finish() failed");
2438 
2439  if (prodesc->fn_retisset)
2440  {
2441  SV *sav;
2442 
2443  /*
2444  * If the Perl function returned an arrayref, we pretend that it
2445  * called return_next() for each element of the array, to handle old
2446  * SRFs that didn't know about return_next(). Any other sort of return
2447  * value is an error, except undef which means return an empty set.
2448  */
2449  sav = get_perl_array_ref(perlret);
2450  if (sav)
2451  {
2452  dTHX;
2453  int i = 0;
2454  SV **svp = 0;
2455  AV *rav = (AV *) SvRV(sav);
2456 
2457  while ((svp = av_fetch(rav, i, FALSE)) != NULL)
2458  {
2460  i++;
2461  }
2462  }
2463  else if (SvOK(perlret))
2464  {
2465  ereport(ERROR,
2466  (errcode(ERRCODE_DATATYPE_MISMATCH),
2467  errmsg("set-returning PL/Perl function must return "
2468  "reference to array or use return_next")));
2469  }
2470 
2473  {
2476  }
2477  retval = (Datum) 0;
2478  }
2479  else if (prodesc->result_oid)
2480  {
2481  retval = plperl_sv_to_datum(perlret,
2482  prodesc->result_oid,
2483  -1,
2484  fcinfo,
2485  &prodesc->result_in_func,
2486  prodesc->result_typioparam,
2487  &fcinfo->isnull);
2488 
2489  if (fcinfo->isnull && rsi && IsA(rsi, ReturnSetInfo))
2490  rsi->isDone = ExprEndResult;
2491  }
2492 
2493  /* Restore the previous error callback */
2494  error_context_stack = pl_error_context.previous;
2495 
2496  SvREFCNT_dec_current(perlret);
2497 
2498  return retval;
2499 }
#define SPI_OK_CONNECT
Definition: spi.h:53
#define IsA(nodeptr, _type_)
Definition: nodes.h:578
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1141
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
#define dTHX
Definition: ppport.h:3208
int SPI_connect_ext(int options)
Definition: spi.c:96
#define castNode(_type_, nodeptr)
Definition: nodes.h:596
int SPI_finish(void)
Definition: spi.c:177
int errcode(int sqlerrcode)
Definition: elog.c:691
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
ErrorContextCallback * error_context_stack
Definition: elog.c:92
bool fn_retisset
Definition: plperl.c:116
#define ERROR
Definition: elog.h:43
#define SPI_OPT_NONATOMIC
Definition: spi.h:71
FmgrInfo result_in_func
Definition: plperl.c:120
static plperl_proc_desc * compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
Definition: plperl.c:2700
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:687
static void plperl_exec_callback(void *arg)
Definition: plperl.c:4105
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:129
Oid result_typioparam
Definition: plperl.c:121
static void plperl_return_next_internal(SV *sv)
Definition: plperl.c:3244
uintptr_t Datum
Definition: postgres.h:367
Tuplestorestate * tuple_store
Definition: plperl.c:178
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:312
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
TupleDesc ret_tdesc
Definition: plperl.c:179
SetFunctionReturnMode returnMode
Definition: execnodes.h:306
#define SPI_OK_FINISH
Definition: spi.h:54
Tuplestorestate * setResult
Definition: execnodes.h:309
TupleDesc setDesc
Definition: execnodes.h:310
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define elog(elevel,...)
Definition: elog.h:228
int i
char * proname
Definition: plperl.c:104
plperl_proc_desc * prodesc
Definition: plperl.c:175
ExprDoneCond isDone
Definition: execnodes.h:307
plperl_interp_desc * interp
Definition: plperl.c:110
static plperl_call_data * current_call_data
Definition: plperl.c:243
static SV * plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
Definition: plperl.c:2165

◆ plperl_hash_from_datum()

static SV * plperl_hash_from_datum ( Datum  attr)
static

Definition at line 2984 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().

2985 {
2986  HeapTupleHeader td;
2987  Oid tupType;
2988  int32 tupTypmod;
2989  TupleDesc tupdesc;
2990  HeapTupleData tmptup;
2991  SV *sv;
2992 
2993  td = DatumGetHeapTupleHeader(attr);
2994 
2995  /* Extract rowtype info and find a tupdesc */
2996  tupType = HeapTupleHeaderGetTypeId(td);
2997  tupTypmod = HeapTupleHeaderGetTypMod(td);
2998  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
2999 
3000  /* Build a temporary HeapTuple control structure */
3001  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
3002  tmptup.t_data = td;
3003 
3004  sv = plperl_hash_from_tuple(&tmptup, tupdesc, true);
3005  ReleaseTupleDesc(tupdesc);
3006 
3007  return sv;
3008 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1741
unsigned int Oid
Definition: postgres_ext.h:31
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:295
signed int int32
Definition: c.h:417
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:468
uint32 t_len
Definition: htup.h:64
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:458
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc, bool include_generated)
Definition: plperl.c:3012
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:452

◆ plperl_hash_from_tuple()

static SV * plperl_hash_from_tuple ( HeapTuple  tuple,
TupleDesc  tupdesc,
bool  include_generated 
)
static

Definition at line 3012 of file plperl.c.

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

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

3013 {
3014  dTHX;
3015  HV *hv;
3016  int i;
3017 
3018  /* since this function recurses, it could be driven to stack overflow */
3020 
3021  hv = newHV();
3022  hv_ksplit(hv, tupdesc->natts); /* pre-grow the hash */
3023 
3024  for (i = 0; i < tupdesc->natts; i++)
3025  {
3026  Datum attr;
3027  bool isnull,
3028  typisvarlena;
3029  char *attname;
3030  Oid typoutput;
3031  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3032 
3033  if (att->attisdropped)
3034  continue;
3035 
3036  if (att->attgenerated)
3037  {
3038  /* don't include unless requested */
3039  if (!include_generated)
3040  continue;
3041  }
3042 
3043  attname = NameStr(att->attname);
3044  attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
3045 
3046  if (isnull)
3047  {
3048  /*
3049  * Store (attname => undef) and move on. Note we can't use
3050  * &PL_sv_undef here; see "AVs, HVs and undefined values" in
3051  * perlguts for an explanation.
3052  */
3053  hv_store_string(hv, attname, newSV(0));
3054  continue;
3055  }
3056 
3057  if (type_is_rowtype(att->atttypid))
3058  {
3059  SV *sv = plperl_hash_from_datum(attr);
3060 
3061  hv_store_string(hv, attname, sv);
3062  }
3063  else
3064  {
3065  SV *sv;
3066  Oid funcid;
3067 
3068  if (OidIsValid(get_base_element_type(att->atttypid)))
3069  sv = plperl_ref_from_pg_array(attr, att->atttypid);
3071  sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, attr));
3072  else
3073  {
3074  char *outputstr;
3075 
3076  /* XXX should have a way to cache these lookups */
3077  getTypeOutputInfo(att->atttypid, &typoutput, &typisvarlena);
3078 
3079  outputstr = OidOutputFunctionCall(typoutput, attr);
3080  sv = cstr2sv(outputstr);
3081  pfree(outputstr);
3082  }
3083 
3084  hv_store_string(hv, attname, sv);
3085  }
3086  }
3087  return newRV_noinc((SV *) hv);
3088 }
static SV * plperl_ref_from_pg_array(Datum arg, Oid typid)
Definition: plperl.c:1465
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:2015
List * trftypes
Definition: plperl.c:113
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2789
#define dTHX
Definition: ppport.h:3208
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2984
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:4055
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:706
void pfree(void *pointer)
Definition: mcxt.c:1057
NameData attname
Definition: pg_attribute.h:40
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:664
void check_stack_depth(void)
Definition: postgres.c:3312
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2548
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:762
#define newRV_noinc(a)
Definition: ppport.h:4456
uintptr_t Datum
Definition: postgres.h:367
static SV * cstr2sv(const char *str)
#define DatumGetPointer(X)
Definition: postgres.h:549
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2714
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1657
int i
#define NameStr(name)
Definition: c.h:677
plperl_proc_desc * prodesc
Definition: plperl.c:175
static plperl_call_data * current_call_data
Definition: plperl.c:243

◆ plperl_hash_to_datum()

static Datum plperl_hash_to_datum ( SV *  src,
TupleDesc  td 
)
static

Definition at line 1129 of file plperl.c.

References HeapTupleGetDatum, and plperl_build_tuple_result().

Referenced by plperl_sv_to_datum().

1130 {
1131  HeapTuple tup = plperl_build_tuple_result((HV *) SvRV(src), td);
1132 
1133  return HeapTupleGetDatum(tup);
1134 }
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
static HeapTuple plperl_build_tuple_result(HV *perlhash, TupleDesc td)
Definition: plperl.c:1078

◆ plperl_init_interp()

static PerlInterpreter * plperl_init_interp ( void  )
static

Definition at line 708 of file plperl.c.

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

Referenced by _PG_init(), and select_perl_context().

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

◆ plperl_init_shared_libs()

static void plperl_init_shared_libs ( pTHX  )
static

Definition at line 2153 of file plperl.c.

References boot_DynaLoader(), and boot_PostgreSQL__InServer__Util().

Referenced by plperl_init_interp().

2154 {
2155  char *file = __FILE__;
2156 
2157  newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2158  newXS("PostgreSQL::InServer::Util::bootstrap",
2160  /* newXS for...::SPI::bootstrap is in select_perl_context() */
2161 }
EXTERN_C void boot_DynaLoader(pTHX_ CV *cv)
EXTERN_C void boot_PostgreSQL__InServer__Util(pTHX_ CV *cv)

◆ plperl_inline_callback()

static void plperl_inline_callback ( void *  arg)
static

Definition at line 4129 of file plperl.c.

References dTHX, errcontext, locale, and setlocale.

Referenced by plperl_inline_handler().

4130 {
4131  errcontext("PL/Perl anonymous code block");
4132 }
#define errcontext
Definition: elog.h:199

◆ plperl_inline_handler()

Datum plperl_inline_handler ( PG_FUNCTION_ARGS  )

Definition at line 1879 of file plperl.c.

References activate_interpreter(), ErrorContextCallback::arg, InlineCodeBlock::atomic, ErrorContextCallback::callback, current_call_data, CurrentMemoryContext, elog, ERROR, error_context_stack, plperl_call_data::fcinfo, 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, LOCAL_FCINFO, MemSet, plperl_proc_desc::nargs, NIL, PG_END_TRY, PG_FINALLY, PG_FUNCTION_INFO_V1(), PG_GETARG_POINTER, PG_RETURN_VOID, PG_TRY, plperl_active_interp, plperl_call_perl_func(), plperl_create_sub(), plperl_inline_callback(), plperl_validator(), ErrorContextCallback::previous, plperl_call_data::prodesc, plperl_proc_desc::proname, plperl_proc_desc::reference, plperl_proc_desc::result_oid, select_perl_context(), SizeForFunctionCallInfo, InlineCodeBlock::source_text, SPI_connect_ext(), SPI_finish(), SPI_OK_CONNECT, SPI_OK_FINISH, SPI_OPT_NONATOMIC, SvREFCNT_dec_current(), and plperl_proc_desc::trftypes.

Referenced by plperl_call_handler(), and plperlu_inline_handler().

1880 {
1881  LOCAL_FCINFO(fake_fcinfo, 0);
1883  FmgrInfo flinfo;
1884  plperl_proc_desc desc;
1885  plperl_call_data *volatile save_call_data = current_call_data;
1886  plperl_interp_desc *volatile oldinterp = plperl_active_interp;
1887  plperl_call_data this_call_data;
1888  ErrorContextCallback pl_error_context;
1889 
1890  /* Initialize current-call status record */
1891  MemSet(&this_call_data, 0, sizeof(this_call_data));
1892 
1893  /* Set up a callback for error reporting */
1894  pl_error_context.callback = plperl_inline_callback;
1895  pl_error_context.previous = error_context_stack;
1896  pl_error_context.arg = NULL;
1897  error_context_stack = &pl_error_context;
1898 
1899  /*
1900  * Set up a fake fcinfo and descriptor with just enough info to satisfy
1901  * plperl_call_perl_func(). In particular note that this sets things up
1902  * with no arguments passed, and a result type of VOID.
1903  */
1904  MemSet(fake_fcinfo, 0, SizeForFunctionCallInfo(0));
1905  MemSet(&flinfo, 0, sizeof(flinfo));
1906  MemSet(&desc, 0, sizeof(desc));
1907  fake_fcinfo->flinfo = &flinfo;
1908  flinfo.fn_oid = InvalidOid;
1909  flinfo.fn_mcxt = CurrentMemoryContext;
1910 
1911  desc.proname = "inline_code_block";
1912  desc.fn_readonly = false;
1913 
1914  desc.lang_oid = codeblock->langOid;
1915  desc.trftypes = NIL;
1916  desc.lanpltrusted = codeblock->langIsTrusted;
1917 
1918  desc.fn_retistuple = false;
1919  desc.fn_retisset = false;
1920  desc.fn_retisarray = false;
1921  desc.result_oid = InvalidOid;
1922  desc.nargs = 0;
1923  desc.reference = NULL;
1924 
1925  this_call_data.fcinfo = fake_fcinfo;
1926  this_call_data.prodesc = &desc;
1927  /* we do not bother with refcounting the fake prodesc */
1928 
1929  PG_TRY();
1930  {
1931  SV *perlret;
1932 
1933  current_call_data = &this_call_data;
1934 
1935  if (SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC) != SPI_OK_CONNECT)
1936  elog(ERROR, "could not connect to SPI manager");
1937 
1939 
1940  plperl_create_sub(&desc, codeblock->source_text, 0);
1941 
1942  if (!desc.reference) /* can this happen? */
1943  elog(ERROR, "could not create internal procedure for anonymous code block");
1944 
1945  perlret = plperl_call_perl_func(&desc, fake_fcinfo);
1946 
1947  SvREFCNT_dec_current(perlret);
1948 
1949  if (SPI_finish() != SPI_OK_FINISH)
1950  elog(ERROR, "SPI_finish() failed");
1951  }
1952  PG_FINALLY();
1953  {
1954  if (desc.reference)
1956  current_call_data = save_call_data;
1957  activate_interpreter(oldinterp);
1958  }
1959  PG_END_TRY();
1960 
1961  error_context_stack = pl_error_context.previous;
1962 
1963  PG_RETURN_VOID();
1964 }
#define NIL
Definition: pg_list.h:65
#define SPI_OK_CONNECT
Definition: spi.h:53
Definition: fmgr.h:56
List * trftypes
Definition: plperl.c:113
MemoryContext fn_mcxt
Definition: fmgr.h:65
FunctionCallInfo fcinfo
Definition: plperl.c:176
static void select_perl_context(bool trusted)
Definition: plperl.c:555
int SPI_connect_ext(int options)
Definition: spi.c:96
#define SizeForFunctionCallInfo(nargs)
Definition: fmgr.h:102
int SPI_finish(void)
Definition: spi.c:177
#define MemSet(start, val, len)
Definition: c.h:1004
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:276
void(* callback)(void *arg)
Definition: elog.h:243
struct ErrorContextCallback * previous
Definition: elog.h:242
ErrorContextCallback * error_context_stack
Definition: elog.c:92
bool fn_retisset
Definition: plperl.c:116
#define ERROR
Definition: elog.h:43
#define SPI_OPT_NONATOMIC
Definition: spi.h:71
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:687
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define PG_FINALLY()
Definition: elog.h:326
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:312
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
bool fn_readonly
Definition: plperl.c:111
static void plperl_inline_callback(void *arg)
Definition: plperl.c:4129
#define PG_RETURN_VOID()
Definition: fmgr.h:349
#define LOCAL_FCINFO(name, nargs)
Definition: fmgr.h:110
SV * reference
Definition: plperl.c:109
#define SPI_OK_FINISH
Definition: spi.h:54
bool fn_retistuple
Definition: plperl.c:115
#define elog(elevel,...)
Definition: elog.h:228
static void plperl_create_sub(plperl_proc_desc *desc, const char *s, Oid fn_oid)
Definition: plperl.c:2080
char * proname
Definition: plperl.c:104
char * source_text
Definition: parsenodes.h:2885
plperl_proc_desc * prodesc
Definition: plperl.c:175
#define PG_TRY()
Definition: elog.h:309
static plperl_call_data * current_call_data
Definition: plperl.c:243
#define PG_END_TRY()
Definition: elog.h:334
bool lanpltrusted
Definition: plperl.c:114
static SV * plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
Definition: plperl.c:2165
bool fn_retisarray
Definition: plperl.c:117

◆ plperl_modify_tuple()

static HeapTuple plperl_modify_tuple ( HV *  hvTD,
TriggerData tdata,
HeapTuple  otup 
)
static

Definition at line 1747 of file plperl.c.

References dTHX, ereport, errcode(), errmsg(), ERROR, heap_modify_tuple(), hek2cstr(), hv_fetch_string(), InvalidOid, sort-test::key, TupleDescData::natts, palloc0(), pfree(), PG_FUNCTION_INFO_V1(), plperl_call_handler(), plperl_sv_to_datum(), RelationData::rd_att, SPI_ERROR_NOATTRIBUTE, SPI_fnumber(), TriggerData::tg_relation, TupleDescAttr, and val.

Referenced by plperl_trigger_handler().

1748 {
1749  dTHX;
1750  SV **svp;
1751  HV *hvNew;
1752  HE *he;
1753  HeapTuple rtup;
1754  TupleDesc tupdesc;
1755  int natts;
1756  Datum *modvalues;
1757  bool *modnulls;
1758  bool *modrepls;
1759 
1760  svp = hv_fetch_string(hvTD, "new");
1761  if (!svp)
1762  ereport(ERROR,
1763  (errcode(ERRCODE_UNDEFINED_COLUMN),
1764  errmsg("$_TD->{new} does not exist")));
1765  if (!SvOK(*svp) || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV)
1766  ereport(ERROR,
1767  (errcode(ERRCODE_DATATYPE_MISMATCH),
1768  errmsg("$_TD->{new} is not a hash reference")));
1769  hvNew = (HV *) SvRV(*svp);
1770 
1771  tupdesc = tdata->tg_relation->rd_att;
1772  natts = tupdesc->natts;
1773 
1774  modvalues = (Datum *) palloc0(natts * sizeof(Datum));
1775  modnulls = (bool *) palloc0(natts * sizeof(bool));
1776  modrepls = (bool *) palloc0(natts * sizeof(bool));
1777 
1778  hv_iterinit(hvNew);
1779  while ((he = hv_iternext(hvNew)))
1780  {
1781  char *key = hek2cstr(he);
1782  SV *val = HeVAL(he);
1783  int attn = SPI_fnumber(tupdesc, key);
1784  Form_pg_attribute attr = TupleDescAttr(tupdesc, attn - 1);
1785 
1786  if (attn == SPI_ERROR_NOATTRIBUTE)
1787  ereport(ERROR,
1788  (errcode(ERRCODE_UNDEFINED_COLUMN),
1789  errmsg("Perl hash contains nonexistent column \"%s\"",
1790  key)));
1791  if (attn <= 0)
1792  ereport(ERROR,
1793  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1794  errmsg("cannot set system attribute \"%s\"",
1795  key)));
1796  if (attr->attgenerated)
1797  ereport(ERROR,
1798  (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1799  errmsg("cannot set generated column \"%s\"",
1800  key)));
1801 
1802  modvalues[attn - 1] = plperl_sv_to_datum(val,
1803  attr->atttypid,
1804  attr->atttypmod,
1805  NULL,
1806  NULL,
1807  InvalidOid,
1808  &modnulls[attn - 1]);
1809  modrepls[attn - 1] = true;
1810 
1811  pfree(key);
1812  }
1813  hv_iterinit(hvNew);
1814 
1815  rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
1816 
1817  pfree(modvalues);
1818  pfree(modnulls);
1819  pfree(modrepls);
1820 
1821  return rtup;
1822 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:1024
static char * hek2cstr(HE *he)
Definition: plperl.c:323
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
#define dTHX
Definition: ppport.h:3208
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:691
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:4082
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:47
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:193
void * palloc0(Size size)
Definition: mcxt.c:981
uintptr_t Datum
Definition: postgres.h:367
TupleDesc rd_att
Definition: rel.h:111
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
int errmsg(const char *fmt,...)
Definition: elog.c:902
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1113
long val
Definition: informix.c:664
Relation tg_relation
Definition: trigger.h:34

◆ plperl_ref_from_pg_array()

static SV * plperl_ref_from_pg_array ( Datum  arg,
Oid  typid 
)
static

Definition at line 1465 of file plperl.c.

References ARR_DIMS, ARR_ELEMTYPE, ARR_NDIM, av, DatumGetArrayTypeP, deconstruct_array(), dTHX, 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, typalign, and type_is_rowtype().

Referenced by plperl_call_perl_func(), and plperl_hash_from_tuple().

1466 {
1467  dTHX;
1469  Oid elementtype = ARR_ELEMTYPE(ar);
1470  int16 typlen;
1471  bool typbyval;
1472  char typalign,
1473  typdelim;
1474  Oid typioparam;
1475  Oid typoutputfunc;
1476  Oid transform_funcid;
1477  int i,
1478  nitems,
1479  *dims;
1480  plperl_array_info *info;
1481  SV *av;
1482  HV *hv;
1483 
1484  /*
1485  * Currently we make no effort to cache any of the stuff we look up here,
1486  * which is bad.
1487  */
1488  info = palloc0(sizeof(plperl_array_info));
1489 
1490  /* get element type information, including output conversion function */
1491  get_type_io_data(elementtype, IOFunc_output,
1492  &typlen, &typbyval, &typalign,
1493  &typdelim, &typioparam, &typoutputfunc);
1494 
1495  /* Check for a transform function */
1496  transform_funcid = get_transform_fromsql(elementtype,
1499 
1500  /* Look up transform or output function as appropriate */
1501  if (OidIsValid(transform_funcid))
1502  fmgr_info(transform_funcid, &info->transform_proc);
1503  else
1504  fmgr_info(typoutputfunc, &info->proc);
1505 
1506  info->elem_is_rowtype = type_is_rowtype(elementtype);
1507 
1508  /* Get the number and bounds of array dimensions */
1509  info->ndims = ARR_NDIM(ar);
1510  dims = ARR_DIMS(ar);
1511 
1512  /* No dimensions? Return an empty array */
1513  if (info->ndims == 0)
1514  {
1515  av = newRV_noinc((SV *) newAV());
1516  }
1517  else
1518  {
1519  deconstruct_array(ar, elementtype, typlen, typbyval,
1520  typalign, &info->elements, &info->nulls,
1521  &nitems);
1522 
1523  /* Get total number of elements in each dimension */
1524  info->nelems = palloc(sizeof(int) * info->ndims);
1525  info->nelems[0] = nitems;
1526  for (i = 1; i < info->ndims; i++)
1527  info->nelems[i] = info->nelems[i - 1] / dims[i - 1];
1528 
1529  av = split_array(info, 0, nitems, 0);
1530  }
1531 
1532  hv = newHV();
1533  (void) hv_store(hv, "array", 5, av, 0);
1534  (void) hv_store(hv, "typeoid", 7, newSVuv(typid), 0);
1535 
1536  return sv_bless(newRV_noinc((SV *) hv),
1537  gv_stashpv("PostgreSQL::InServer::ARRAY", 0));
1538 }
signed short int16
Definition: c.h:416
FmgrInfo transform_proc
Definition: plperl.c:218
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:2015
List * trftypes
Definition: plperl.c:113
#define dTHX
Definition: ppport.h:3208
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:706
static SV * split_array(plperl_array_info *info, int first, int last, int nest)
Definition: plperl.c:1544
char typalign
Definition: pg_type.h:170
#define newSVuv(uv)
Definition: ppport.h:3596
#define ARR_DIMS(a)
Definition: array.h:282
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:126
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2548
#define newRV_noinc(a)
Definition: ppport.h:4456
void * palloc0(Size size)
Definition: mcxt.c:981
struct @17::@18 av[32]
bool elem_is_rowtype
Definition: plperl.c:213
#define ARR_NDIM(a)
Definition: array.h:278
FmgrInfo proc
Definition: plperl.c:217
void deconstruct_array(ArrayType *array, Oid elmtype, int elmlen, bool elmbyval, char elmalign, Datum **elemsp, bool **nullsp, int *nelemsp)
Definition: arrayfuncs.c:3483
bool * nulls
Definition: plperl.c:215
void * palloc(Size size)
Definition: mcxt.c:950
int i
Datum * elements
Definition: plperl.c:214
void * arg
plperl_proc_desc * prodesc
Definition: plperl.c:175
static plperl_call_data * current_call_data
Definition: plperl.c:243
#define ARR_ELEMTYPE(a)
Definition: array.h:280
#define DatumGetArrayTypeP(X)
Definition: array.h:249
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:2218

◆ plperl_return_next()

void plperl_return_next ( SV *  sv)

Definition at line 3216 of file plperl.c.

References CopyErrorData(), croak_cstr(), CurrentMemoryContext, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, PG_CATCH, PG_END_TRY, PG_TRY, and plperl_return_next_internal().

3217 {
3218  MemoryContext oldcontext = CurrentMemoryContext;
3219 
3220  PG_TRY();
3221  {
3223  }
3224  PG_CATCH();
3225  {
3226  ErrorData *edata;
3227 
3228  /* Must reset elog.c's state */
3229  MemoryContextSwitchTo(oldcontext);
3230  edata = CopyErrorData();
3231  FlushErrorState();
3232 
3233  /* Punt the error to Perl */
3234  croak_cstr(edata->message);
3235  }
3236  PG_END_TRY();
3237 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void FlushErrorState(void)
Definition: elog.c:1646
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void plperl_return_next_internal(SV *sv)
Definition: plperl.c:3244
static void croak_cstr(const char *str)
#define PG_CATCH()
Definition: elog.h:319
#define PG_TRY()
Definition: elog.h:309
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379

◆ plperl_return_next_internal()

static void plperl_return_next_internal ( SV *  sv)
static

Definition at line 3244 of file plperl.c.

References ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, ReturnSetInfo::allowedModes, Assert, plperl_call_data::cdomain_info, plperl_call_data::cdomain_oid, CreateTupleDescCopy(), CurrentMemoryContext, domain_check(), ReturnSetInfo::econtext, ExprContext::ecxt_per_query_memory, elog, ereport, errcode(), errmsg(), ERROR, ReturnSetInfo::expectedDesc, plperl_call_data::fcinfo, plperl_proc_desc::fn_retisset, plperl_proc_desc::fn_retistuple, get_call_result_type(), HeapTupleGetDatum, MemoryContextReset(), MemoryContextSwitchTo(), TupleDescData::natts, OidIsValid, 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, FunctionCallInfoBaseData::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(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and work_mem.

Referenced by plperl_func_handler(), and plperl_return_next().

3245 {
3246  plperl_proc_desc *prodesc;
3247  FunctionCallInfo fcinfo;
3248  ReturnSetInfo *rsi;
3249  MemoryContext old_cxt;
3250 
3251  if (!sv)
3252  return;
3253 
3254  prodesc = current_call_data->prodesc;
3255  fcinfo = current_call_data->fcinfo;
3256  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
3257 
3258  if (!prodesc->fn_retisset)
3259  ereport(ERROR,
3260  (errcode(ERRCODE_SYNTAX_ERROR),
3261  errmsg("cannot use return_next in a non-SETOF function")));
3262 
3264  {
3265  TupleDesc tupdesc;
3266 
3268 
3269  /*
3270  * This is the first call to return_next in the current PL/Perl
3271  * function call, so identify the output tuple type and create a
3272  * tuplestore to hold the result rows.
3273  */
3274  if (prodesc->fn_retistuple)
3275  {
3276  TypeFuncClass funcclass;
3277  Oid typid;
3278 
3279  funcclass = get_call_result_type(fcinfo, &typid, &tupdesc);
3280  if (funcclass != TYPEFUNC_COMPOSITE &&
3281  funcclass != TYPEFUNC_COMPOSITE_DOMAIN)
3282  ereport(ERROR,
3283  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3284  errmsg("function returning record called in context "
3285  "that cannot accept type record")));
3286  /* if domain-over-composite, remember the domain's type OID */
3287  if (funcclass == TYPEFUNC_COMPOSITE_DOMAIN)
3288  current_call_data->cdomain_oid = typid;
3289  }
3290  else
3291  {
3292  tupdesc = rsi->expectedDesc;
3293  /* Protect assumption below that we return exactly one column */
3294  if (tupdesc == NULL || tupdesc->natts != 1)
3295  elog(ERROR, "expected single-column result descriptor for non-composite SETOF result");
3296  }
3297 
3298  /*
3299  * Make sure the tuple_store and ret_tdesc are sufficiently
3300  * long-lived.
3301  */
3303 
3307  false, work_mem);
3308 
3309  MemoryContextSwitchTo(old_cxt);
3310  }
3311 
3312  /*
3313  * Producing the tuple we want to return requires making plenty of
3314  * palloc() allocations that are not cleaned up. Since this function can
3315  * be called many times before the current memory context is reset, we
3316  * need to do those allocations in a temporary context.
3317  */
3318  if (!current_call_data->tmp_cxt)
3319  {
3322  "PL/Perl return_next temporary cxt",
3324  }
3325 
3327 
3328  if (prodesc->fn_retistuple)
3329  {
3330  HeapTuple tuple;
3331 
3332  if (!(SvOK(sv) && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV))
3333  ereport(ERROR,
3334  (errcode(ERRCODE_DATATYPE_MISMATCH),
3335  errmsg("SETOF-composite-returning PL/Perl function "
3336  "must call return_next with reference to hash")));
3337 
3338  tuple = plperl_build_tuple_result((HV *) SvRV(sv),
3340 
3342  domain_check(HeapTupleGetDatum(tuple), false,
3346 
3348  }
3349  else if (prodesc->result_oid)
3350  {
3351  Datum ret[1];
3352  bool isNull[1];
3353 
3354  ret[0] = plperl_sv_to_datum(sv,
3355  prodesc->result_oid,
3356  -1,
3357  fcinfo,
3358  &prodesc->result_in_func,
3359  prodesc->result_typioparam,
3360  &isNull[0]);
3361 
3364  ret, isNull);
3365  }
3366 
3367  MemoryContextSwitchTo(old_cxt);
3369 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
#define AllocSetContextCreate
Definition: memutils.h:170
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
FunctionCallInfo fcinfo
Definition: plperl.c:176
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:691
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:137
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:706
void * cdomain_info
Definition: plperl.c:181
TupleDesc expectedDesc
Definition: execnodes.h:303
bool fn_retisset
Definition: plperl.c:116
#define ERROR
Definition: elog.h:43
fmNodePtr resultinfo
Definition: fmgr.h:89
FmgrInfo result_in_func
Definition: plperl.c:120
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TypeFuncClass
Definition: funcapi.h:146
Oid result_typioparam
Definition: plperl.c:121
Tuplestorestate * tuplestore_begin_heap(bool randomAccess, bool interXact, int maxKBytes)
Definition: tuplestore.c:318
uintptr_t Datum
Definition: postgres.h:367
int work_mem
Definition: globals.c:121
Tuplestorestate * tuple_store
Definition: plperl.c:178
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
#define ereport(elevel,...)
Definition: elog.h:155
int allowedModes
Definition: execnodes.h:304
TupleDesc ret_tdesc
Definition: plperl.c:179
MemoryContext tmp_cxt
Definition: plperl.c:182
#define Assert(condition)
Definition: c.h:800
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:232
static HeapTuple plperl_build_tuple_result(HV *perlhash, TupleDesc td)
Definition: plperl.c:1078
ExprContext * econtext
Definition: execnodes.h:302
int errmsg(const char *fmt,...)
Definition: elog.c:902
bool fn_retistuple
Definition: plperl.c:115
#define elog(elevel,...)
Definition: elog.h:228
plperl_proc_desc * prodesc
Definition: plperl.c:175
static plperl_call_data * current_call_data
Definition: plperl.c:243

◆ plperl_spi_commit()

void plperl_spi_commit ( void  )

Definition at line 3960 of file plperl.c.

References CopyErrorData(), croak_cstr(), CurrentMemoryContext, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, PG_CATCH, PG_END_TRY, PG_TRY, SPI_commit(), and SPI_start_transaction().

3961 {
3962  MemoryContext oldcontext = CurrentMemoryContext;
3963 
3964  PG_TRY();
3965  {
3966  SPI_commit();
3968  }
3969  PG_CATCH();
3970  {
3971  ErrorData *edata;
3972 
3973  /* Save error info */
3974  MemoryContextSwitchTo(oldcontext);
3975  edata = CopyErrorData();
3976  FlushErrorState();
3977 
3978  /* Punt the error to Perl */
3979  croak_cstr(edata->message);
3980  }
3981  PG_END_TRY();
3982 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void FlushErrorState(void)
Definition: elog.c:1646
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void SPI_commit(void)
Definition: spi.c:279
static void croak_cstr(const char *str)
#define PG_CATCH()
Definition: elog.h:319
void SPI_start_transaction(void)
Definition: spi.c:213
#define PG_TRY()
Definition: elog.h:309
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379

◆ plperl_spi_cursor_close()

void plperl_spi_cursor_close ( char *  cursor)

Definition at line 3520 of file plperl.c.

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

3521 {
3522  Portal p;
3523 
3525 
3526  p = SPI_cursor_find(cursor);
3527 
3528  if (p)
3529  {
3530  UnpinPortal(p);
3531  SPI_cursor_close(p);
3532  }
3533 }
void UnpinPortal(Portal portal)
Definition: portalmem.c:379
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1637
Definition: type.h:130
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
void SPI_cursor_close(Portal portal)
Definition: spi.c:1705

◆ plperl_spi_exec()

HV* plperl_spi_exec ( char *  query,
int  limit 
)

Definition at line 3104 of file plperl.c.

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, CurrentResourceOwner, FlushErrorState(), plperl_proc_desc::fn_readonly, MemoryContextSwitchTo(), ErrorData::message, 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.

3105 {
3106  HV *ret_hv;
3107 
3108  /*
3109  * Execute the query inside a sub-transaction, so we can cope with errors
3110  * sanely
3111  */
3112  MemoryContext oldcontext = CurrentMemoryContext;
3114 
3116 
3118  /* Want to run inside function's memory context */
3119  MemoryContextSwitchTo(oldcontext);
3120 
3121  PG_TRY();
3122  {
3123  int spi_rv;
3124 
3125  pg_verifymbstr(query, strlen(query), false);
3126 
3128  limit);
3130  spi_rv);
3131 
3132  /* Commit the inner transaction, return to outer xact context */
3134  MemoryContextSwitchTo(oldcontext);
3135  CurrentResourceOwner = oldowner;
3136  }
3137  PG_CATCH();
3138  {
3139  ErrorData *edata;
3140 
3141  /* Save error info */
3142  MemoryContextSwitchTo(oldcontext);
3143  edata = CopyErrorData();
3144  FlushErrorState();
3145 
3146  /* Abort the inner transaction */
3148  MemoryContextSwitchTo(oldcontext);
3149  CurrentResourceOwner = oldowner;
3150 
3151  /* Punt the error to Perl */
3152  croak_cstr(edata->message);
3153 
3154  /* Can't get here, but keep compiler quiet */
3155  return NULL;
3156  }
3157  PG_END_TRY();
3158 
3159  return ret_hv;
3160 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4480
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:46
void FlushErrorState(void)
Definition: elog.c:1646
uint64 SPI_processed
Definition: spi.c:45
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4514
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void croak_cstr(const char *str)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1446
bool fn_readonly
Definition: plperl.c:111
#define PG_CATCH()
Definition: elog.h:319
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4409
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition: plperl.c:3164
plperl_proc_desc * prodesc
Definition: plperl.c:175
#define PG_TRY()
Definition: elog.h:309
static plperl_call_data * current_call_data
Definition: plperl.c:243
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:497

◆ plperl_spi_exec_prepared()

HV* plperl_spi_exec_prepared ( char *  query,
HV *  attr,
int  argc,
SV **  argv 
)

Definition at line 3684 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, dTHX, elog, ERROR, FlushErrorState(), plperl_proc_desc::fn_readonly, HASH_FIND, hash_search(), hv_fetch_string(), i, MemoryContextSwitchTo(), ErrorData::message, plperl_query_desc::nargs, 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.

3685 {
3686  HV *ret_hv;
3687  SV **sv;
3688  int i,
3689  limit,
3690  spi_rv;
3691  char *nulls;
3692  Datum *argvalues;
3693  plperl_query_desc *qdesc;
3694  plperl_query_entry *hash_entry;
3695 
3696  /*
3697  * Execute the query inside a sub-transaction, so we can cope with errors
3698  * sanely
3699  */
3700  MemoryContext oldcontext = CurrentMemoryContext;
3702 
3704 
3706  /* Want to run inside function's memory context */
3707  MemoryContextSwitchTo(oldcontext);
3708 
3709  PG_TRY();
3710  {
3711  dTHX;
3712 
3713  /************************************************************
3714  * Fetch the saved plan descriptor, see if it's o.k.
3715  ************************************************************/
3716  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3717  HASH_FIND, NULL);
3718  if (hash_entry == NULL)
3719  elog(ERROR, "spi_exec_prepared: Invalid prepared query passed");
3720 
3721  qdesc = hash_entry->query_data;
3722  if (qdesc == NULL)
3723  elog(ERROR, "spi_exec_prepared: plperl query_hash value vanished");
3724 
3725  if (qdesc->nargs != argc)
3726  elog(ERROR, "spi_exec_prepared: expected %d argument(s), %d passed",
3727  qdesc->nargs, argc);
3728 
3729  /************************************************************
3730  * Parse eventual attributes
3731  ************************************************************/
3732  limit = 0;
3733  if (attr != NULL)
3734  {
3735  sv = hv_fetch_string(attr, "limit");
3736  if (sv && *sv && SvIOK(*sv))
3737  limit = SvIV(*sv);
3738  }
3739  /************************************************************
3740  * Set up arguments
3741  ************************************************************/
3742  if (argc > 0)
3743  {
3744  nulls = (char *) palloc(argc);
3745  argvalues = (Datum *) palloc(argc * sizeof(Datum));
3746  }
3747  else
3748  {
3749  nulls = NULL;
3750  argvalues = NULL;
3751  }
3752 
3753  for (i = 0; i < argc; i++)
3754  {
3755  bool isnull;
3756 
3757  argvalues[i] = plperl_sv_to_datum(argv[i],
3758  qdesc->argtypes[i],
3759  -1,
3760  NULL,
3761  &qdesc->arginfuncs[i],
3762  qdesc->argtypioparams[i],
3763  &isnull);
3764  nulls[i] = isnull ? 'n' : ' ';
3765  }
3766 
3767  /************************************************************
3768  * go
3769  ************************************************************/
3770  spi_rv = SPI_execute_plan(qdesc->plan, argvalues, nulls,
3773  spi_rv);
3774  if (argc > 0)
3775  {
3776  pfree(argvalues);
3777  pfree(nulls);
3778  }
3779 
3780  /* Commit the inner transaction, return to outer xact context */
3782  MemoryContextSwitchTo(oldcontext);
3783  CurrentResourceOwner = oldowner;
3784  }
3785  PG_CATCH();
3786  {
3787  ErrorData *edata;
3788 
3789  /* Save error info */
3790  MemoryContextSwitchTo(oldcontext);
3791  edata = CopyErrorData();
3792  FlushErrorState();
3793 
3794  /* Abort the inner transaction */
3796  MemoryContextSwitchTo(oldcontext);
3797  CurrentResourceOwner = oldowner;
3798 
3799  /* Punt the error to Perl */
3800  croak_cstr(edata->message);
3801 
3802  /* Can't get here, but keep compiler quiet */
3803  return NULL;
3804  }
3805  PG_END_TRY();
3806 
3807  return ret_hv;
3808 }
Definition: plperl.c:201
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
HTAB * query_hash
Definition: plperl.c:89
#define dTHX
Definition: ppport.h:3208
SPIPlanPtr plan
Definition: plperl.c:192
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
Oid * argtypioparams
Definition: plperl.c:196
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4480
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:46
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:4082
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
void FlushErrorState(void)
Definition: elog.c:1646
uint64 SPI_processed
Definition: spi.c:45
void pfree(void *pointer)
Definition: mcxt.c:1057
#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:532
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4514
Oid * argtypes
Definition: plperl.c:194
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * arginfuncs
Definition: plperl.c:195
static void croak_cstr(const char *str)
bool fn_readonly
Definition: plperl.c:111
#define PG_CATCH()
Definition: elog.h:319
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4409
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
int i
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition: plperl.c:3164
plperl_proc_desc * prodesc
Definition: plperl.c:175
#define PG_TRY()
Definition: elog.h:309
static plperl_call_data * current_call_data
Definition: plperl.c:243
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379
plperl_query_desc * query_data
Definition: plperl.c:204

◆ plperl_spi_execute_fetch_result()

static HV * plperl_spi_execute_fetch_result ( SPITupleTable tuptable,
uint64  processed,
int  status 
)
static

Definition at line 3164 of file plperl.c.

References AV_SIZE_MAX, check_spi_usage_allowed(), cstr2sv(), dTHX, 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().

3166 {
3167  dTHX;
3168  HV *result;
3169 
3171 
3172  result = newHV();
3173 
3174  hv_store_string(result, "status",
3176  hv_store_string(result, "processed",
3177  (processed > (uint64) UV_MAX) ?
3178  newSVnv((NV) processed) :
3179  newSVuv((UV) processed));
3180 
3181  if (status > 0 && tuptable)
3182  {
3183  AV *rows;
3184  SV *row;
3185  uint64 i;
3186 
3187  /* Prevent overflow in call to av_extend() */
3188  if (processed > (uint64) AV_SIZE_MAX)
3189  ereport(ERROR,
3190  (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
3191  errmsg("query result has too many rows to fit in a Perl array")));
3192 
3193  rows = newAV();
3194  av_extend(rows, processed);
3195  for (i = 0; i < processed; i++)
3196  {
3197  row = plperl_hash_from_tuple(tuptable->vals[i], tuptable->tupdesc, true);
3198  av_push(rows, row);
3199  }
3200  hv_store_string(result, "rows",
3201  newRV_noinc((SV *) rows));
3202  }
3203 
3204  SPI_freetuptable(tuptable);
3205 
3206  return result;
3207 }
#define dTHX
Definition: ppport.h:3208
#define AV_SIZE_MAX
Definition: plperl.h:201
int errcode(int sqlerrcode)
Definition: elog.c:691
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:4055
HeapTuple * vals
Definition: spi.h:26
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:1815
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1235
#define newRV_noinc(a)
Definition: ppport.h:4456
TupleDesc tupdesc
Definition: spi.h:25
static SV * cstr2sv(const char *str)
#define UV_MAX
Definition: ppport.h:3566
#define ereport(elevel,...)
Definition: elog.h:155
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
int errmsg(const char *fmt,...)
Definition: elog.c:902
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc, bool include_generated)
Definition: plperl.c:3012
int i
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:227

◆ plperl_spi_fetchrow()

SV* plperl_spi_fetchrow ( char *  cursor)

Definition at line 3445 of file plperl.c.

References BeginInternalSubTransaction(), check_spi_usage_allowed(), CopyErrorData(), croak_cstr(), CurrentMemoryContext, CurrentResourceOwner, dTHX, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, 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, UnpinPortal(), and SPITupleTable::vals.

3446 {
3447  SV *row;
3448 
3449  /*
3450  * Execute the FETCH inside a sub-transaction, so we can cope with errors
3451  * sanely
3452  */
3453  MemoryContext oldcontext = CurrentMemoryContext;
3455 
3457 
3459  /* Want to run inside function's memory context */
3460  MemoryContextSwitchTo(oldcontext);
3461 
3462  PG_TRY();
3463  {
3464  dTHX;
3466 
3467  if (!p)
3468  {
3469  row = &PL_sv_undef;
3470  }
3471  else
3472  {
3473  SPI_cursor_fetch(p, true, 1);
3474  if (SPI_processed == 0)
3475  {
3476  UnpinPortal(p);
3477  SPI_cursor_close(p);
3478  row = &PL_sv_undef;
3479  }
3480  else
3481  {
3484  true);
3485  }
3487  }
3488 
3489  /* Commit the inner transaction, return to outer xact context */
3491  MemoryContextSwitchTo(oldcontext);
3492  CurrentResourceOwner = oldowner;
3493  }
3494  PG_CATCH();
3495  {
3496  ErrorData *edata;
3497 
3498  /* Save error info */
3499  MemoryContextSwitchTo(oldcontext);
3500  edata = CopyErrorData();
3501  FlushErrorState();
3502 
3503  /* Abort the inner transaction */
3505  MemoryContextSwitchTo(oldcontext);
3506  CurrentResourceOwner = oldowner;
3507 
3508  /* Punt the error to Perl */
3509  croak_cstr(edata->message);
3510 
3511  /* Can't get here, but keep compiler quiet */
3512  return NULL;
3513  }
3514  PG_END_TRY();
3515 
3516  return row;
3517 }
void UnpinPortal(Portal portal)
Definition: portalmem.c:379
#define dTHX
Definition: ppport.h:3208
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4480
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:46
#define PL_sv_undef
Definition: ppport.h:4129
HeapTuple * vals
Definition: spi.h:26
void FlushErrorState(void)
Definition: elog.c:1646
uint64 SPI_processed
Definition: spi.c:45
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1637
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4514
Definition: type.h:130
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1235
static void croak_cstr(const char *str)
TupleDesc tupdesc
Definition: spi.h:25
#define PG_CATCH()
Definition: elog.h:319
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4409
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
void SPI_cursor_close(Portal portal)
Definition: spi.c:1705
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc, bool include_generated)
Definition: plperl.c:3012
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1649
#define PG_TRY()
Definition: elog.h:309
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379

◆ plperl_spi_freeplan()

void plperl_spi_freeplan ( char *  query)

Definition at line 3929 of file plperl.c.

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

3930 {
3931  SPIPlanPtr plan;
3932  plperl_query_desc *qdesc;
3933  plperl_query_entry *hash_entry;
3934 
3936 
3937  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3938  HASH_FIND, NULL);
3939  if (hash_entry == NULL)
3940  elog(ERROR, "spi_freeplan: Invalid prepared query passed");
3941 
3942  qdesc = hash_entry->query_data;
3943  if (qdesc == NULL)
3944  elog(ERROR, "spi_freeplan: plperl query_hash value vanished");
3945  plan = qdesc->plan;
3946 
3947  /*
3948  * free all memory before SPI_freeplan, so if it dies, nothing will be
3949  * left over
3950  */
3952  HASH_REMOVE, NULL);
3953 
3954  MemoryContextDelete(qdesc->plan_cxt);
3955 
3956  SPI_freeplan(plan);
3957 }
Definition: plperl.c:201
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
HTAB * query_hash
Definition: plperl.c:89
SPIPlanPtr plan
Definition: plperl.c:192
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
#define ERROR
Definition: elog.h:43
MemoryContext plan_cxt
Definition: plperl.c:191
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:874
#define elog(elevel,...)
Definition: elog.h:228
plperl_query_desc * query_data
Definition: plperl.c:204

◆ plperl_spi_prepare()

SV* plperl_spi_prepare ( char *  query,
int  argc,
SV **  argv 
)

Definition at line 3536 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, 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.

3537 {
3538  volatile SPIPlanPtr plan = NULL;
3539  volatile MemoryContext plan_cxt = NULL;
3540  plperl_query_desc *volatile qdesc = NULL;
3541  plperl_query_entry *volatile hash_entry = NULL;
3542  MemoryContext oldcontext = CurrentMemoryContext;
3544  MemoryContext work_cxt;
3545  bool found;
3546  int i;
3547 
3549 
3551  MemoryContextSwitchTo(oldcontext);
3552 
3553  PG_TRY();
3554  {
3556 
3557  /************************************************************
3558  * Allocate the new querydesc structure
3559  *
3560  * The qdesc struct, as well as all its subsidiary data, lives in its
3561  * plan_cxt. But note that the SPIPlan does not.
3562  ************************************************************/
3564  "PL/Perl spi_prepare query",
3566  MemoryContextSwitchTo(plan_cxt);
3567  qdesc = (plperl_query_desc *) palloc0(sizeof(plperl_query_desc));
3568  snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
3569  qdesc->plan_cxt = plan_cxt;
3570  qdesc->nargs = argc;
3571  qdesc->argtypes = (Oid *) palloc(argc * sizeof(Oid));
3572  qdesc->arginfuncs = (FmgrInfo *) palloc(argc * sizeof(FmgrInfo));
3573  qdesc->argtypioparams = (Oid *) palloc(argc * sizeof(Oid));
3574  MemoryContextSwitchTo(oldcontext);
3575 
3576  /************************************************************
3577  * Do the following work in a short-lived context so that we don't
3578  * leak a lot of memory in the PL/Perl function's SPI Proc context.
3579  ************************************************************/
3581  "PL/Perl spi_prepare workspace",
3583  MemoryContextSwitchTo(work_cxt);
3584 
3585  /************************************************************
3586  * Resolve argument type names and then look them up by oid
3587  * in the system cache, and remember the required information
3588  * for input conversion.
3589  ************************************************************/
3590  for (i = 0; i < argc; i++)
3591  {
3592  Oid typId,
3593  typInput,
3594  typIOParam;
3595  int32 typmod;
3596  char *typstr;
3597 
3598  typstr = sv2cstr(argv[i]);
3599  parseTypeString(typstr, &typId, &typmod, false);
3600  pfree(typstr);
3601 
3602  getTypeInputInfo(typId, &typInput, &typIOParam);
3603 
3604  qdesc->argtypes[i] = typId;
3605  fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
3606  qdesc->argtypioparams[i] = typIOParam;
3607  }
3608 
3609  /* Make sure the query is validly encoded */
3610  pg_verifymbstr(query, strlen(query), false);
3611 
3612  /************************************************************
3613  * Prepare the plan and check for errors
3614  ************************************************************/
3615  plan = SPI_prepare(query, argc, qdesc->argtypes);
3616 
3617  if (plan == NULL)
3618  elog(ERROR, "SPI_prepare() failed:%s",
3620 
3621  /************************************************************
3622  * Save the plan into permanent memory (right now it's in the
3623  * SPI procCxt, which will go away at function end).
3624  ************************************************************/
3625  if (SPI_keepplan(plan))
3626  elog(ERROR, "SPI_keepplan() failed");
3627  qdesc->plan = plan;
3628 
3629  /************************************************************
3630  * Insert a hashtable entry for the plan.
3631  ************************************************************/
3633  qdesc->qname,
3634  HASH_ENTER, &found);
3635  hash_entry->query_data = qdesc;
3636 
3637  /* Get rid of workspace */
3638  MemoryContextDelete(work_cxt);
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  /* Drop anything we managed to allocate */
3655  if (hash_entry)
3657  qdesc->qname,
3658  HASH_REMOVE, NULL);
3659  if (plan_cxt)
3660  MemoryContextDelete(plan_cxt);
3661  if (plan)
3662  SPI_freeplan(plan);
3663 
3664  /* Abort the inner transaction */
3666  MemoryContextSwitchTo(oldcontext);
3667  CurrentResourceOwner = oldowner;
3668 
3669  /* Punt the error to Perl */
3670  croak_cstr(edata->message);
3671 
3672  /* Can't get here, but keep compiler quiet */
3673  return NULL;
3674  }
3675  PG_END_TRY();
3676 
3677  /************************************************************
3678  * Return the query's hash key to the caller.
3679  ************************************************************/
3680  return cstr2sv(qdesc->qname);
3681 }
Definition: fmgr.h:56
Definition: plperl.c:201
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:212
#define AllocSetContextCreate
Definition: memutils.h:170
HTAB * query_hash
Definition: plperl.c:89
SPIPlanPtr plan
Definition: plperl.c:192
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
Oid * argtypioparams
Definition: plperl.c:196
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:747
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4480
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
unsigned int Oid
Definition: postgres_ext.h:31
void FlushErrorState(void)
Definition: elog.c:1646
signed int int32
Definition: c.h:417
int SPI_result
Definition: spi.c:47
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:825
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4514
Oid * argtypes
Definition: plperl.c:194
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
MemoryContext plan_cxt
Definition: plperl.c:191
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:136
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2756
MemoryContext TopMemoryContext
Definition: mcxt.c:44
void * palloc0(Size size)
Definition: mcxt.c:981
char qname[24]
Definition: plperl.c:190
FmgrInfo * arginfuncs
Definition: plperl.c:195
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:832
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1446
#define PG_CATCH()
Definition: elog.h:319
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4409
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:874
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
int i
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:99
#define PG_TRY()
Definition: elog.h:309
#define snprintf
Definition: port.h:215
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379
static char * sv2cstr(SV *sv)
plperl_query_desc * query_data
Definition: plperl.c:204

◆ plperl_spi_query()

SV* plperl_spi_query ( char *  query)

Definition at line 3373 of file plperl.c.

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

3374 {
3375  SV *cursor;
3376 
3377  /*
3378  * Execute the query inside a sub-transaction, so we can cope with errors
3379  * sanely
3380  */
3381  MemoryContext oldcontext = CurrentMemoryContext;
3383 
3385 
3387  /* Want to run inside function's memory context */
3388  MemoryContextSwitchTo(oldcontext);
3389 
3390  PG_TRY();
3391  {
3392  SPIPlanPtr plan;
3393  Portal portal;
3394 
3395  /* Make sure the query is validly encoded */
3396  pg_verifymbstr(query, strlen(query), false);
3397 
3398  /* Create a cursor for the query */
3399  plan = SPI_prepare(query, 0, NULL);
3400  if (plan == NULL)
3401  elog(ERROR, "SPI_prepare() failed:%s",
3403 
3404  portal = SPI_cursor_open(NULL, plan, NULL, NULL, false);
3405  SPI_freeplan(plan);
3406  if (portal == NULL)
3407  elog(ERROR, "SPI_cursor_open() failed:%s",
3409  cursor = cstr2sv(portal->name);
3410 
3411  PinPortal(portal);
3412 
3413  /* Commit the inner transaction, return to outer xact context */
3415  MemoryContextSwitchTo(oldcontext);
3416  CurrentResourceOwner = oldowner;
3417  }
3418  PG_CATCH();
3419  {
3420  ErrorData *edata;
3421 
3422  /* Save error info */
3423  MemoryContextSwitchTo(oldcontext);
3424  edata = CopyErrorData();
3425  FlushErrorState();
3426 
3427  /* Abort the inner transaction */
3429  MemoryContextSwitchTo(oldcontext);
3430  CurrentResourceOwner = oldowner;
3431 
3432  /* Punt the error to Perl */
3433  croak_cstr(edata->message);
3434 
3435  /* Can't get here, but keep compiler quiet */
3436  return NULL;
3437  }
3438  PG_END_TRY();
3439 
3440  return cursor;
3441 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:747
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4480
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:1294
void FlushErrorState(void)
Definition: elog.c:1646
int SPI_result
Definition: spi.c:47
const char * name
Definition: portal.h:118
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4514
void PinPortal(Portal portal)
Definition: portalmem.c:370
Definition: type.h:130
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void croak_cstr(const char *str)
static SV * cstr2sv(const char *str)
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: mbutils.c:1446
#define PG_CATCH()
Definition: elog.h:319
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4409
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:874
#define elog(elevel,...)
Definition: elog.h:228
#define PG_TRY()
Definition: elog.h:309
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379

◆ plperl_spi_query_prepared()

SV* plperl_spi_query_prepared ( char *  query,
int  argc,
SV **  argv 
)

Definition at line 3811 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, palloc(), pfree(), PG_CATCH, PG_END_TRY, PG_TRY, PinPortal(), 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().

3812 {
3813  int i;
3814  char *nulls;
3815  Datum *argvalues;
3816  plperl_query_desc *qdesc;
3817  plperl_query_entry *hash_entry;
3818  SV *cursor;
3819  Portal portal = NULL;
3820 
3821  /*
3822  * Execute the query inside a sub-transaction, so we can cope with errors
3823  * sanely
3824  */
3825  MemoryContext oldcontext = CurrentMemoryContext;
3827 
3829 
3831  /* Want to run inside function's memory context */
3832  MemoryContextSwitchTo(oldcontext);
3833 
3834  PG_TRY();
3835  {
3836  /************************************************************
3837  * Fetch the saved plan descriptor, see if it's o.k.
3838  ************************************************************/
3839  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3840  HASH_FIND, NULL);
3841  if (hash_entry == NULL)
3842  elog(ERROR, "spi_query_prepared: Invalid prepared query passed");
3843 
3844  qdesc = hash_entry->query_data;
3845  if (qdesc == NULL)
3846  elog(ERROR, "spi_query_prepared: plperl query_hash value vanished");
3847 
3848  if (qdesc->nargs != argc)
3849  elog(ERROR, "spi_query_prepared: expected %d argument(s), %d passed",
3850  qdesc->nargs, argc);
3851 
3852  /************************************************************
3853  * Set up arguments
3854  ************************************************************/
3855  if (argc > 0)
3856  {
3857  nulls = (char *) palloc(argc);
3858  argvalues = (Datum *) palloc(argc * sizeof(Datum));
3859  }
3860  else
3861  {
3862  nulls = NULL;
3863  argvalues = NULL;
3864  }
3865 
3866  for (i = 0; i < argc; i++)
3867  {
3868  bool isnull;
3869 
3870  argvalues[i] = plperl_sv_to_datum(argv[i],
3871  qdesc->argtypes[i],
3872  -1,
3873  NULL,
3874  &qdesc->arginfuncs[i],
3875  qdesc->argtypioparams[i],
3876  &isnull);
3877  nulls[i] = isnull ? 'n' : ' ';
3878  }
3879 
3880  /************************************************************
3881  * go
3882  ************************************************************/
3883  portal = SPI_cursor_open(NULL, qdesc->plan, argvalues, nulls,
3885  if (argc > 0)
3886  {
3887  pfree(argvalues);
3888  pfree(nulls);
3889  }
3890  if (portal == NULL)
3891  elog(ERROR, "SPI_cursor_open() failed:%s",
3893 
3894  cursor = cstr2sv(portal->name);
3895 
3896  PinPortal(portal);
3897 
3898  /* Commit the inner transaction, return to outer xact context */
3900  MemoryContextSwitchTo(oldcontext);
3901  CurrentResourceOwner = oldowner;
3902  }
3903  PG_CATCH();
3904  {
3905  ErrorData *edata;
3906 
3907  /* Save error info */
3908  MemoryContextSwitchTo(oldcontext);
3909  edata = CopyErrorData();
3910  FlushErrorState();
3911 
3912  /* Abort the inner transaction */
3914  MemoryContextSwitchTo(oldcontext);
3915  CurrentResourceOwner = oldowner;
3916 
3917  /* Punt the error to Perl */
3918  croak_cstr(edata->message);
3919 
3920  /* Can't get here, but keep compiler quiet */
3921  return NULL;
3922  }
3923  PG_END_TRY();
3924 
3925  return cursor;
3926 }
Definition: plperl.c:201
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
HTAB * query_hash
Definition: plperl.c:89
SPIPlanPtr plan
Definition: plperl.c:192
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
Oid * argtypioparams
Definition: plperl.c:196
ResourceOwner CurrentResourceOwner
Definition: resowner.c:142
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4480
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:227
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1294
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:919
void FlushErrorState(void)
Definition: elog.c:1646
int SPI_result
Definition: spi.c:47
void pfree(void *pointer)
Definition: mcxt.c:1057
const char * name
Definition: portal.h:118
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1815
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4514
void PinPortal(Portal portal)
Definition: portalmem.c:370
Definition: type.h:130
Oid * argtypes
Definition: plperl.c:194
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * arginfuncs
Definition: plperl.c:195
static void croak_cstr(const char *str)
static SV * cstr2sv(const char *str)
bool fn_readonly
Definition: plperl.c:111
#define PG_CATCH()
Definition: elog.h:319
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4409
static void check_spi_usage_allowed(void)
Definition: plperl.c:3092
void * palloc(Size size)
Definition: mcxt.c:950
#define elog(elevel,...)
Definition: elog.h:228
int i
plperl_proc_desc * prodesc
Definition: plperl.c:175
#define PG_TRY()
Definition: elog.h:309
static plperl_call_data * current_call_data
Definition: plperl.c:243
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379
plperl_query_desc * query_data
Definition: plperl.c:204

◆ plperl_spi_rollback()

void plperl_spi_rollback ( void  )

Definition at line 3985 of file plperl.c.

References CopyErrorData(), croak_cstr(), CurrentMemoryContext, FlushErrorState(), MemoryContextSwitchTo(), ErrorData::message, PG_CATCH, PG_END_TRY, PG_TRY, SPI_rollback(), and SPI_start_transaction().

3986 {
3987  MemoryContext oldcontext = CurrentMemoryContext;
3988 
3989  PG_TRY();
3990  {
3991  SPI_rollback();
3993  }
3994  PG_CATCH();
3995  {
3996  ErrorData *edata;
3997 
3998  /* Save error info */
3999  MemoryContextSwitchTo(oldcontext);
4000  edata = CopyErrorData();
4001  FlushErrorState();
4002 
4003  /* Punt the error to Perl */
4004  croak_cstr(edata->message);
4005  }
4006  PG_END_TRY();
4007 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1552
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void SPI_rollback(void)
Definition: spi.c:334
void FlushErrorState(void)
Definition: elog.c:1646
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void croak_cstr(const char *str)
#define PG_CATCH()
Definition: elog.h:319
void SPI_start_transaction(void)
Definition: spi.c:213
#define PG_TRY()
Definition: elog.h:309
#define PG_END_TRY()
Definition: elog.h:334
char * message
Definition: elog.h:379

◆ plperl_sv_to_datum()

static Datum plperl_sv_to_datum ( SV *  sv,
Oid  typid,
int32  typmod,
FunctionCallInfo  fcinfo,
FmgrInfo finfo,
Oid  typioparam,
bool isnull 
)
static

Definition at line 1310 of file plperl.c.

References _sv_to_datum_finfo(), Assert, check_stack_depth(), domain_check(), 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_domain(), OidFunctionCall1, pfree(), plperl_array_to_datum(), plperl_hash_to_datum(), PointerGetDatum, plperl_call_data::prodesc, ReleaseTupleDesc, generate_unaccent_rules::str, sv2cstr(), TupleDescData::tdtypeid, plperl_proc_desc::trftypes, type_is_rowtype(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and TYPEFUNC_OTHER.

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

1314 {
1315  FmgrInfo tmp;
1316  Oid funcid;
1317 
1318  /* we might recurse */
1320 
1321  *isnull = false;
1322 
1323  /*
1324  * Return NULL if result is undef, or if we're in a function returning
1325  * VOID. In the latter case, we should pay no attention to the last Perl
1326  * statement's result, and this is a convenient means to ensure that.
1327  */
1328  if (!sv || !SvOK(sv) || typid == VOIDOID)
1329  {
1330  /* look up type info if they did not pass it */
1331  if (!finfo)
1332  {
1333  _sv_to_datum_finfo(typid, &tmp, &typioparam);
1334  finfo = &tmp;
1335  }
1336  *isnull = true;
1337  /* must call typinput in case it wants to reject NULL */
1338  return InputFunctionCall(finfo, NULL, typioparam, typmod);
1339  }
1341  return OidFunctionCall1(funcid, PointerGetDatum(sv));
1342  else if (SvROK(sv))
1343  {
1344  /* handle references */
1345  SV *sav = get_perl_array_ref(sv);
1346 
1347  if (sav)
1348  {
1349  /* handle an arrayref */
1350  return plperl_array_to_datum(sav, typid, typmod);
1351  }
1352  else if (SvTYPE(SvRV(sv)) == SVt_PVHV)
1353  {
1354  /* handle a hashref */
1355  Datum ret;
1356  TupleDesc td;
1357  bool isdomain;
1358 
1359  if (!type_is_rowtype(typid))
1360  ereport(ERROR,
1361  (errcode(ERRCODE_DATATYPE_MISMATCH),
1362  errmsg("cannot convert Perl hash to non-composite type %s",
1363  format_type_be(typid))));
1364 
1365  td = lookup_rowtype_tupdesc_domain(typid, typmod, true);
1366  if (td != NULL)
1367  {
1368  /* Did we look through a domain? */
1369  isdomain = (typid != td->tdtypeid);
1370  }
1371  else
1372  {
1373  /* Must be RECORD, try to resolve based on call info */
1374  TypeFuncClass funcclass;
1375 
1376  if (fcinfo)
1377  funcclass = get_call_result_type(fcinfo, &typid, &td);
1378  else
1379  funcclass = TYPEFUNC_OTHER;
1380  if (funcclass != TYPEFUNC_COMPOSITE &&
1381  funcclass != TYPEFUNC_COMPOSITE_DOMAIN)
1382  ereport(ERROR,
1383  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1384  errmsg("function returning record called in context "
1385  "that cannot accept type record")));
1386  Assert(td);
1387  isdomain = (funcclass == TYPEFUNC_COMPOSITE_DOMAIN);
1388  }
1389 
1390  ret = plperl_hash_to_datum(sv, td);
1391 
1392  if (isdomain)
1393  domain_check(ret, false, typid, NULL, NULL);
1394 
1395  /* Release on the result of get_call_result_type is harmless */
1396  ReleaseTupleDesc(td);
1397 
1398  return ret;
1399  }
1400 
1401  /*
1402  * If it's a reference to something else, such as a scalar, just
1403  * recursively look through the reference.
1404  */
1405  return plperl_sv_to_datum(SvRV(sv), typid, typmod,
1406  fcinfo, finfo, typioparam,
1407  isnull);
1408  }
1409  else
1410  {
1411  /* handle a string/number */
1412  Datum ret;
1413  char *str = sv2cstr(sv);
1414 
1415  /* did not pass in any typeinfo? look it up */
1416  if (!finfo)
1417  {
1418  _sv_to_datum_finfo(typid, &tmp, &typioparam);
1419  finfo = &tmp;
1420  }
1421 
1422  ret = InputFunctionCall(finfo, str, typioparam, typmod);
1423  pfree(str);
1424 
1425  return ret;
1426  }
1427 }
Definition: fmgr.h:56
List * trftypes
Definition: plperl.c:113
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:205
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1141
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
static Datum plperl_array_to_datum(SV *src, Oid typid, int32 typmod)
Definition: plperl.c:1243
#define PointerGetDatum(X)
Definition: postgres.h:556
int errcode(int sqlerrcode)
Definition: elog.c:691
char * format_type_be(Oid type_oid)
Definition: format_type.c:339
unsigned int Oid
Definition: postgres_ext.h:31
TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1797
void pfree(void *pointer)
Definition: mcxt.c:1057
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:664
void check_stack_depth(void)
Definition: postgres.c:3312
static Datum plperl_hash_to_datum(SV *src, TupleDesc td)
Definition: plperl.c:1129
TypeFuncClass
Definition: funcapi.h:146
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2548
uintptr_t Datum
Definition: postgres.h:367
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
Datum InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
Definition: fmgr.c:1533
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:800
Oid get_transform_tosql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:2036
Oid tdtypeid
Definition: tupdesc.h:82
int errmsg(const char *fmt,...)
Definition: elog.c:902
plperl_proc_desc * prodesc
Definition: plperl.c:175
static void _sv_to_datum_finfo(Oid typid, FmgrInfo *finfo, Oid *typioparam)
Definition: plperl.c:1287
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:122
static plperl_call_data * current_call_data
Definition: plperl.c:243
static char * sv2cstr(SV *sv)

◆ plperl_sv_to_literal()

char* plperl_sv_to_literal ( SV *  sv,
char *  fqtypename 
)

Definition at line 1431 of file plperl.c.

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

1432 {
1433  Datum str = CStringGetDatum(fqtypename);
1434  Oid typid = DirectFunctionCall1(regtypein, str);
1435  Oid typoutput;
1436  Datum datum;
1437  bool typisvarlena,
1438  isnull;
1439 
1440  if (!OidIsValid(typid))
1441  ereport(ERROR,
1442  (errcode(ERRCODE_UNDEFINED_OBJECT),
1443  errmsg("lookup failed for type %s", fqtypename)));
1444 
1445  datum = plperl_sv_to_datum(sv,
1446  typid, -1,
1447  NULL, NULL, InvalidOid,
1448  &isnull);
1449 
1450  if (isnull)
1451  return NULL;
1452 
1453  getTypeOutputInfo(typid,
1454  &typoutput, &typisvarlena);
1455 
1456  return OidOutputFunctionCall(typoutput, datum);
1457 }
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2789
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1310
int errcode(int sqlerrcode)
Definition: elog.c:691
#define DirectFunctionCall1(func, arg1)
Definition: fmgr.h:626
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:706
#define ERROR
Definition: elog.h:43
#define CStringGetDatum(X)
Definition: postgres.h:578
Datum regtypein(PG_FUNCTION_ARGS)
Definition: regproc.c:1250
uintptr_t Datum
Definition: postgres.h:367
#define InvalidOid
Definition: postgres_ext.h:36
#define ereport(elevel,...)
Definition: elog.h:155
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1657
int errmsg(const char *fmt,...)
Definition: elog.c:902

◆ plperl_trigger_build_args()