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 "plperl.h"
#include "plperl_helpers.h"
#include "perlchunks.h"
#include "plperl_opmask.h"
Include dependency graph for plperl.c:

Go to the source code of this file.

Data Structures

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

Macros

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

Typedefs

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

Functions

EXTERN_C void boot_DynaLoader (pTHX_ CV *cv)
 
EXTERN_C void boot_PostgreSQL__InServer__Util (pTHX_ CV *cv)
 
EXTERN_C void boot_PostgreSQL__InServer__SPI (pTHX_ CV *cv)
 
void _PG_init (void)
 
static PerlInterpreter * plperl_init_interp (void)
 
static void plperl_destroy_interp (PerlInterpreter **)
 
static void plperl_fini (int code, Datum arg)
 
static void set_interp_require (bool trusted)
 
static Datum plperl_func_handler (PG_FUNCTION_ARGS)
 
static Datum plperl_trigger_handler (PG_FUNCTION_ARGS)
 
static void plperl_event_trigger_handler (PG_FUNCTION_ARGS)
 
static void free_plperl_function (plperl_proc_desc *prodesc)
 
static plperl_proc_desccompile_plperl_function (Oid fn_oid, bool is_trigger, bool is_event_trigger)
 
static SV * plperl_hash_from_tuple (HeapTuple tuple, TupleDesc tupdesc)
 
static SV * plperl_hash_from_datum (Datum attr)
 
static SV * plperl_ref_from_pg_array (Datum arg, Oid typid)
 
static SV * split_array (plperl_array_info *info, int first, int last, int nest)
 
static SV * make_array_ref (plperl_array_info *info, int first, int last)
 
static SV * get_perl_array_ref (SV *sv)
 
static Datum plperl_sv_to_datum (SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
 
static void _sv_to_datum_finfo (Oid typid, FmgrInfo *finfo, Oid *typioparam)
 
static Datum plperl_array_to_datum (SV *src, Oid typid, int32 typmod)
 
static void array_to_datum_internal (AV *av, ArrayBuildState *astate, int *ndims, int *dims, int cur_depth, Oid arraytypid, Oid elemtypid, int32 typmod, FmgrInfo *finfo, Oid typioparam)
 
static Datum plperl_hash_to_datum (SV *src, TupleDesc td)
 
static void plperl_init_shared_libs (pTHX)
 
static void plperl_trusted_init (void)
 
static void plperl_untrusted_init (void)
 
static HV * plperl_spi_execute_fetch_result (SPITupleTable *, uint64, int)
 
static 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 135 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 47 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 384 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.

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

References fmgr_info(), and getTypeInputInfo().

Referenced by plperl_array_to_datum(), and plperl_sv_to_datum().

1292 {
1293  Oid typinput;
1294 
1295  /* XXX would be better to cache these lookups */
1296  getTypeInputInfo(typid,
1297  &typinput, typioparam);
1298  fmgr_info(typinput, finfo);
1299 }
unsigned int Oid
Definition: postgres_ext.h:31
void fmgr_info(Oid functionId, FmgrInfo *finfo)
Definition: fmgr.c:124
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2617

◆ activate_interpreter()

static void activate_interpreter ( plperl_interp_desc interp_desc)
static

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

692 {
693  if (interp_desc && plperl_active_interp != interp_desc)
694  {
695  Assert(interp_desc->interp);
696  PERL_SET_CONTEXT(interp_desc->interp);
697  /* trusted iff user_id isn't InvalidOid */
698  set_interp_require(OidIsValid(interp_desc->user_id));
699  plperl_active_interp = interp_desc;
700  }
701 }
static void set_interp_require(bool trusted)
Definition: plperl.c:496
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
PerlInterpreter * interp
Definition: plperl.c:92
#define OidIsValid(objectId)
Definition: c.h:605
#define Assert(condition)
Definition: c.h:699

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

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

Referenced by plperl_array_to_datum().

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

◆ 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 3094 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().

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

◆ compile_plperl_function()

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

Definition at line 2709 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(), HeapTupleGetOid, HeapTupleHeaderGetRawXmin, HeapTupleIsValid, i, increment_prodesc_refcount, plperl_proc_desc::interp, InvalidOid, plperl_proc_key::is_trigger, plperl_proc_desc::lang_oid, LANGOID, plperl_proc_desc::lanpltrusted, MemoryContextDelete(), 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().

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

◆ free_plperl_function()

static void free_plperl_function ( plperl_proc_desc prodesc)
static

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

2692 {
2693  Assert(prodesc->fn_refcount == 0);
2694  /* Release CODE reference, if we have one, from the appropriate interp */
2695  if (prodesc->reference)
2696  {
2698 
2699  activate_interpreter(prodesc->interp);
2700  SvREFCNT_dec_current(prodesc->reference);
2701  activate_interpreter(oldinterp);
2702  }
2703  /* Release all PG-owned data for this proc */
2704  MemoryContextDelete(prodesc->fn_cxt);
2705 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
MemoryContext fn_cxt
Definition: plperl.c:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:691
unsigned long fn_refcount
Definition: plperl.c:110
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:316
#define Assert(condition)
Definition: c.h:699
SV * reference
Definition: plperl.c:113
plperl_interp_desc * interp
Definition: plperl.c:114

◆ get_perl_array_ref()

static SV * get_perl_array_ref ( SV *  sv)
static

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

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

◆ hek2cstr()

static char * hek2cstr ( HE *  he)
static

Definition at line 327 of file plperl.c.

References dTHX, HeUTF8, and sv2cstr().

Referenced by plperl_build_tuple_result(), and plperl_modify_tuple().

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

4088 {
4089  dTHX;
4090  int32 hlen;
4091  char *hkey;
4092  SV **ret;
4093 
4094  hkey = pg_server_to_any(key, strlen(key), PG_UTF8);
4095 
4096  /* See notes in hv_store_string */
4097  hlen = -(int) strlen(hkey);
4098  ret = hv_fetch(hv, hkey, hlen, 0);
4099 
4100  if (hkey != key)
4101  pfree(hkey);
4102 
4103  return ret;
4104 }
#define dTHX
Definition: ppport.h:3208
char * pg_server_to_any(const char *s, int len, int encoding)
Definition: mbutils.c:634
signed int int32
Definition: c.h:313
void pfree(void *pointer)
Definition: mcxt.c:1031

◆ hv_store_string()

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

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

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

◆ make_array_ref()

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

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

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

◆ 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 1247 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().

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

◆ plperl_build_tuple_result()

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

Definition at line 1082 of file plperl.c.

References dTHX, ereport, errcode(), errmsg(), ERROR, heap_form_tuple(), hek2cstr(), InvalidOid, tupleDesc::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().

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

◆ plperl_call_handler()

Datum plperl_call_handler ( PG_FUNCTION_ARGS  )

Definition at line 1828 of file plperl.c.

References activate_interpreter(), CALLED_AS_EVENT_TRIGGER, CALLED_AS_TRIGGER, current_call_data, decrement_prodesc_refcount, plperl_call_data::fcinfo, MemSet, PG_CATCH, PG_END_TRY, PG_FUNCTION_INFO_V1(), PG_RE_THROW, 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().

1829 {
1830  Datum retval;
1831  plperl_call_data *volatile save_call_data = current_call_data;
1832  plperl_interp_desc *volatile oldinterp = plperl_active_interp;
1833  plperl_call_data this_call_data;
1834 
1835  /* Initialize current-call status record */
1836  MemSet(&this_call_data, 0, sizeof(this_call_data));
1837  this_call_data.fcinfo = fcinfo;
1838 
1839  PG_TRY();
1840  {
1841  current_call_data = &this_call_data;
1842  if (CALLED_AS_TRIGGER(fcinfo))
1843  retval = PointerGetDatum(plperl_trigger_handler(fcinfo));
1844  else if (CALLED_AS_EVENT_TRIGGER(fcinfo))
1845  {
1847  retval = (Datum) 0;
1848  }
1849  else
1850  retval = plperl_func_handler(fcinfo);
1851  }
1852  PG_CATCH();
1853  {
1854  current_call_data = save_call_data;
1855  activate_interpreter(oldinterp);
1856  if (this_call_data.prodesc)
1857  decrement_prodesc_refcount(this_call_data.prodesc);
1858  PG_RE_THROW();
1859  }
1860  PG_END_TRY();
1861 
1862  current_call_data = save_call_data;
1863  activate_interpreter(oldinterp);
1864  if (this_call_data.prodesc)
1865  decrement_prodesc_refcount(this_call_data.prodesc);
1866  return retval;
1867 }
#define CALLED_AS_EVENT_TRIGGER(fcinfo)
Definition: event_trigger.h:40
static Datum plperl_func_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2396
#define decrement_prodesc_refcount(prodesc)
Definition: plperl.c:135
FunctionCallInfo fcinfo
Definition: plperl.c:180
#define PointerGetDatum(X)
Definition: postgres.h:541
#define MemSet(start, val, len)
Definition: c.h:908
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
static void plperl_event_trigger_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2625
static Datum plperl_trigger_handler(PG_FUNCTION_ARGS)
Definition: plperl.c:2512
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:691
uintptr_t Datum
Definition: postgres.h:367
#define PG_CATCH()
Definition: elog.h:293
#define CALLED_AS_TRIGGER(fcinfo)
Definition: trigger.h:25
#define PG_RE_THROW()
Definition: elog.h:314
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:247
#define PG_END_TRY()
Definition: elog.h:300

◆ 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 2333 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().

2336 {
2337  dTHX;
2338  dSP;
2339  SV *retval,
2340  *TDsv;
2341  int count;
2342 
2343  ENTER;
2344  SAVETMPS;
2345 
2346  TDsv = get_sv("main::_TD", 0);
2347  if (!TDsv)
2348  ereport(ERROR,
2349  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2350  errmsg("couldn't fetch $_TD")));
2351 
2352  save_item(TDsv); /* local $_TD */
2353  sv_setsv(TDsv, td);
2354 
2355  PUSHMARK(sp);
2356  PUTBACK;
2357 
2358  /* Do NOT use G_KEEPERR here */
2359  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2360 
2361  SPAGAIN;
2362 
2363  if (count != 1)
2364  {
2365  PUTBACK;
2366  FREETMPS;
2367  LEAVE;
2368  ereport(ERROR,
2369  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2370  errmsg("didn't get a return item from trigger function")));
2371  }
2372 
2373  if (SvTRUE(ERRSV))
2374  {
2375  (void) POPs;
2376  PUTBACK;
2377  FREETMPS;
2378  LEAVE;
2379  /* XXX need to find a way to determine a better errcode here */
2380  ereport(ERROR,
2381  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2383  }
2384 
2385  retval = newSVsv(POPs);
2386  (void) retval; /* silence compiler warning */
2387 
2388  PUTBACK;
2389  FREETMPS;
2390  LEAVE;
2391 
2392  return;
2393 }
#define dTHX
Definition: ppport.h:3208
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
SV * reference
Definition: plperl.c:113
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1068
int errmsg(const char *fmt,...)
Definition: elog.c:797
#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 2172 of file plperl.c.

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

Referenced by plperl_func_handler(), and plperl_inline_handler().

2173 {
2174  dTHX;
2175  dSP;
2176  SV *retval;
2177  int i;
2178  int count;
2179  Oid *argtypes = NULL;
2180  int nargs = 0;
2181 
2182  ENTER;
2183  SAVETMPS;
2184 
2185  PUSHMARK(SP);
2186  EXTEND(sp, desc->nargs);
2187 
2188  /* Get signature for true functions; inline blocks have no args. */
2189  if (fcinfo->flinfo->fn_oid)
2190  get_func_signature(fcinfo->flinfo->fn_oid, &argtypes, &nargs);
2191  Assert(nargs == desc->nargs);
2192 
2193  for (i = 0; i < desc->nargs; i++)
2194  {
2195  if (fcinfo->argnull[i])
2196  PUSHs(&PL_sv_undef);
2197  else if (desc->arg_is_rowtype[i])
2198  {
2199  SV *sv = plperl_hash_from_datum(fcinfo->arg[i]);
2200 
2201  PUSHs(sv_2mortal(sv));
2202  }
2203  else
2204  {
2205  SV *sv;
2206  Oid funcid;
2207 
2208  if (OidIsValid(desc->arg_arraytype[i]))
2209  sv = plperl_ref_from_pg_array(fcinfo->arg[i], desc->arg_arraytype[i]);
2211  sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, fcinfo->arg[i]));
2212  else
2213  {
2214  char *tmp;
2215 
2216  tmp = OutputFunctionCall(&(desc->arg_out_func[i]),
2217  fcinfo->arg[i]);
2218  sv = cstr2sv(tmp);
2219  pfree(tmp);
2220  }
2221 
2222  PUSHs(sv_2mortal(sv));
2223  }
2224  }
2225  PUTBACK;
2226 
2227  /* Do NOT use G_KEEPERR here */
2228  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2229 
2230  SPAGAIN;
2231 
2232  if (count != 1)
2233  {
2234  PUTBACK;
2235  FREETMPS;
2236  LEAVE;
2237  ereport(ERROR,
2238  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2239  errmsg("didn't get a return item from function")));
2240  }
2241 
2242  if (SvTRUE(ERRSV))
2243  {
2244  (void) POPs;
2245  PUTBACK;
2246  FREETMPS;
2247  LEAVE;
2248  /* XXX need to find a way to determine a better errcode here */
2249  ereport(ERROR,
2250  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2252  }
2253 
2254  retval = newSVsv(POPs);
2255 
2256  PUTBACK;
2257  FREETMPS;
2258  LEAVE;
2259 
2260  return retval;
2261 }
static SV * plperl_ref_from_pg_array(Datum arg, Oid typid)
Definition: plperl.c:1467
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1876
List * trftypes
Definition: plperl.c:117
bool * arg_is_rowtype
Definition: plperl.c:129
#define dTHX
Definition: ppport.h:3208
Oid get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
Definition: lsyscache.c:1485
#define PL_sv_undef
Definition: ppport.h:4129
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2993
unsigned int Oid
Definition: postgres_ext.h:31
FmgrInfo * arg_out_func
Definition: plperl.c:128
#define OidIsValid(objectId)
Definition: c.h:605
char * OutputFunctionCall(FmgrInfo *flinfo, Datum val)
Definition: fmgr.c:1753
FmgrInfo * flinfo
Definition: fmgr.h:79
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:628
bool argnull[FUNC_MAX_ARGS]
Definition: fmgr.h:89
#define ereport(elevel, rest)
Definition: elog.h:122
Oid * arg_arraytype
Definition: plperl.c:130
static SV * cstr2sv(const char *str)
Oid fn_oid
Definition: fmgr.h:59
Datum arg[FUNC_MAX_ARGS]
Definition: fmgr.h:87
#define Assert(condition)
Definition: c.h:699
SV * reference
Definition: plperl.c:113
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1068
#define DatumGetPointer(X)
Definition: postgres.h:534
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
plperl_proc_desc * prodesc
Definition: plperl.c:179
static plperl_call_data * current_call_data
Definition: plperl.c:247
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 2265 of file plperl.c.

References FunctionCallInfoData::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().

2267 {
2268  dTHX;
2269  dSP;
2270  SV *retval,
2271  *TDsv;
2272  int i,
2273  count;
2274  Trigger *tg_trigger = ((TriggerData *) fcinfo->context)->tg_trigger;
2275 
2276  ENTER;
2277  SAVETMPS;
2278 
2279  TDsv = get_sv("main::_TD", 0);
2280  if (!TDsv)
2281  ereport(ERROR,
2282  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2283  errmsg("couldn't fetch $_TD")));
2284 
2285  save_item(TDsv); /* local $_TD */
2286  sv_setsv(TDsv, td);
2287 
2288  PUSHMARK(sp);
2289  EXTEND(sp, tg_trigger->tgnargs);
2290 
2291  for (i = 0; i < tg_trigger->tgnargs; i++)
2292  PUSHs(sv_2mortal(cstr2sv(tg_trigger->tgargs[i])));
2293  PUTBACK;
2294 
2295  /* Do NOT use G_KEEPERR here */
2296  count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
2297 
2298  SPAGAIN;
2299 
2300  if (count != 1)
2301  {
2302  PUTBACK;
2303  FREETMPS;
2304  LEAVE;
2305  ereport(ERROR,
2306  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2307  errmsg("didn't get a return item from trigger function")));
2308  }
2309 
2310  if (SvTRUE(ERRSV))
2311  {
2312  (void) POPs;
2313  PUTBACK;
2314  FREETMPS;
2315  LEAVE;
2316  /* XXX need to find a way to determine a better errcode here */
2317  ereport(ERROR,
2318  (errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
2320  }
2321 
2322  retval = newSVsv(POPs);
2323 
2324  PUTBACK;
2325  FREETMPS;
2326  LEAVE;
2327 
2328  return retval;
2329 }
#define dTHX
Definition: ppport.h:3208
fmNodePtr context
Definition: fmgr.h:80
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERRSV
Definition: ppport.h:3859
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
char ** tgargs
Definition: reltrigger.h:40
static SV * cstr2sv(const char *str)
SV * reference
Definition: plperl.c:113
static char * strip_trailing_ws(const char *msg)
Definition: plperl.c:1068
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define get_sv
Definition: ppport.h:3878
int i
int16 tgnargs
Definition: reltrigger.h:37
static char * sv2cstr(SV *sv)

◆ plperl_compile_callback()

static void plperl_compile_callback ( void *  arg)
static

Definition at line 4122 of file plperl.c.

References errcontext.

Referenced by compile_plperl_function().

4123 {
4124  char *procname = (char *) arg;
4125 
4126  if (procname)
4127  errcontext("compilation of PL/Perl function \"%s\"", procname);
4128 }
#define errcontext
Definition: elog.h:164
void * arg

◆ plperl_create_sub()

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

Definition at line 2085 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, strip_trailing_ws(), subname, and sv2cstr().

Referenced by compile_plperl_function(), and plperl_inline_handler().

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

References dTHX, and PERL_UNUSED_VAR.

Referenced by plperl_fini().

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

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

Referenced by plperl_event_trigger_handler().

1726 {
1727  dTHX;
1728  EventTriggerData *tdata;
1729  HV *hv;
1730 
1731  hv = newHV();
1732 
1733  tdata = (EventTriggerData *) fcinfo->context;
1734 
1735  hv_store_string(hv, "event", cstr2sv(tdata->event));
1736  hv_store_string(hv, "tag", cstr2sv(tdata->tag));
1737 
1738  return newRV_noinc((SV *) hv);
1739 }
#define dTHX
Definition: ppport.h:3208
fmNodePtr context
Definition: fmgr.h:80
const char * tag
Definition: event_trigger.h:28
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:4060
const char * event
Definition: event_trigger.h:26
#define newRV_noinc(a)
Definition: ppport.h:4456
static SV * cstr2sv(const char *str)

◆ plperl_event_trigger_handler()

static void plperl_event_trigger_handler ( PG_FUNCTION_ARGS  )
static

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

2626 {
2627  plperl_proc_desc *prodesc;
2628  SV *svTD;
2629  ErrorContextCallback pl_error_context;
2630 
2631  /* Connect to SPI manager */
2632  if (SPI_connect() != SPI_OK_CONNECT)
2633  elog(ERROR, "could not connect to SPI manager");
2634 
2635  /* Find or compile the function */
2636  prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false, true);
2637  current_call_data->prodesc = prodesc;
2638  increment_prodesc_refcount(prodesc);
2639 
2640  /* Set a callback for error reporting */
2641  pl_error_context.callback = plperl_exec_callback;
2642  pl_error_context.previous = error_context_stack;
2643  pl_error_context.arg = prodesc->proname;
2644  error_context_stack = &pl_error_context;
2645 
2646  activate_interpreter(prodesc->interp);
2647 
2648  svTD = plperl_event_trigger_build_args(fcinfo);
2649  plperl_call_perl_event_trigger_func(prodesc, fcinfo, svTD);
2650 
2651  if (SPI_finish() != SPI_OK_FINISH)
2652  elog(ERROR, "SPI_finish() failed");
2653 
2654  /* Restore the previous error callback */
2655  error_context_stack = pl_error_context.previous;
2656 
2657  SvREFCNT_dec_current(svTD);
2658 }
#define SPI_OK_CONNECT
Definition: spi.h:50
int SPI_connect(void)
Definition: spi.c:84
int SPI_finish(void)
Definition: spi.c:161
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
static void plperl_call_perl_event_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo, SV *td)
Definition: plperl.c:2333
ErrorContextCallback * error_context_stack
Definition: elog.c:88
#define ERROR
Definition: elog.h:43
static plperl_proc_desc * compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
Definition: plperl.c:2709
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:691
static void plperl_exec_callback(void *arg)
Definition: plperl.c:4110
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:133
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:316
#define SPI_OK_FINISH
Definition: spi.h:51
static SV * plperl_event_trigger_build_args(FunctionCallInfo fcinfo)
Definition: plperl.c:1725
char * proname
Definition: plperl.c:108
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
plperl_interp_desc * interp
Definition: plperl.c:114
static plperl_call_data * current_call_data
Definition: plperl.c:247

◆ plperl_exec_callback()

static void plperl_exec_callback ( void *  arg)
static

Definition at line 4110 of file plperl.c.

References errcontext.

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

4111 {
4112  char *procname = (char *) arg;
4113 
4114  if (procname)
4115  errcontext("PL/Perl function \"%s\"", procname);
4116 }
#define errcontext
Definition: elog.h:164
void * arg

◆ plperl_fini()

static void plperl_fini ( int  code,
Datum  arg 
)
static

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

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

◆ plperl_func_handler()

static Datum plperl_func_handler ( PG_FUNCTION_ARGS  )
static

Definition at line 2396 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, FALSE, 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().

2397 {
2398  bool nonatomic;
2399  plperl_proc_desc *prodesc;
2400  SV *perlret;
2401  Datum retval = 0;
2402  ReturnSetInfo *rsi;
2403  ErrorContextCallback pl_error_context;
2404 
2405  nonatomic = fcinfo->context &&
2406  IsA(fcinfo->context, CallContext) &&
2407  !castNode(CallContext, fcinfo->context)->atomic;
2408 
2409  if (SPI_connect_ext(nonatomic ? SPI_OPT_NONATOMIC : 0) != SPI_OK_CONNECT)
2410  elog(ERROR, "could not connect to SPI manager");
2411 
2412  prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false, false);
2413  current_call_data->prodesc = prodesc;
2414  increment_prodesc_refcount(prodesc);
2415 
2416  /* Set a callback for error reporting */
2417  pl_error_context.callback = plperl_exec_callback;
2418  pl_error_context.previous = error_context_stack;
2419  pl_error_context.arg = prodesc->proname;
2420  error_context_stack = &pl_error_context;
2421 
2422  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
2423 
2424  if (prodesc->fn_retisset)
2425  {
2426  /* Check context before allowing the call to go through */
2427  if (!rsi || !IsA(rsi, ReturnSetInfo) ||
2428  (rsi->allowedModes & SFRM_Materialize) == 0)
2429  ereport(ERROR,
2430  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2431  errmsg("set-valued function called in context that "
2432  "cannot accept a set")));
2433  }
2434 
2435  activate_interpreter(prodesc->interp);
2436 
2437  perlret = plperl_call_perl_func(prodesc, fcinfo);
2438 
2439  /************************************************************
2440  * Disconnect from SPI manager and then create the return
2441  * values datum (if the input function does a palloc for it
2442  * this must not be allocated in the SPI memory context
2443  * because SPI_finish would free it).
2444  ************************************************************/
2445  if (SPI_finish() != SPI_OK_FINISH)
2446  elog(ERROR, "SPI_finish() failed");
2447 
2448  if (prodesc->fn_retisset)
2449  {
2450  SV *sav;
2451 
2452  /*
2453  * If the Perl function returned an arrayref, we pretend that it
2454  * called return_next() for each element of the array, to handle old
2455  * SRFs that didn't know about return_next(). Any other sort of return
2456  * value is an error, except undef which means return an empty set.
2457  */
2458  sav = get_perl_array_ref(perlret);
2459  if (sav)
2460  {
2461  dTHX;
2462  int i = 0;
2463  SV **svp = 0;
2464  AV *rav = (AV *) SvRV(sav);
2465 
2466  while ((svp = av_fetch(rav, i, FALSE)) != NULL)
2467  {
2469  i++;
2470  }
2471  }
2472  else if (SvOK(perlret))
2473  {
2474  ereport(ERROR,
2475  (errcode(ERRCODE_DATATYPE_MISMATCH),
2476  errmsg("set-returning PL/Perl function must return "
2477  "reference to array or use return_next")));
2478  }
2479 
2482  {
2485  }
2486  retval = (Datum) 0;
2487  }
2488  else if (prodesc->result_oid)
2489  {
2490  retval = plperl_sv_to_datum(perlret,
2491  prodesc->result_oid,
2492  -1,
2493  fcinfo,
2494  &prodesc->result_in_func,
2495  prodesc->result_typioparam,
2496  &fcinfo->isnull);
2497 
2498  if (fcinfo->isnull && rsi && IsA(rsi, ReturnSetInfo))
2499  rsi->isDone = ExprEndResult;
2500  }
2501 
2502  /* Restore the previous error callback */
2503  error_context_stack = pl_error_context.previous;
2504 
2505  SvREFCNT_dec_current(perlret);
2506 
2507  return retval;
2508 }
#define SPI_OK_CONNECT
Definition: spi.h:50
#define IsA(nodeptr, _type_)
Definition: nodes.h:568
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1145
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1314
#define dTHX
Definition: ppport.h:3208
int SPI_connect_ext(int options)
Definition: spi.c:90
#define castNode(_type_, nodeptr)
Definition: nodes.h:586
#define FALSE
Definition: ecpglib.h:39
int SPI_finish(void)
Definition: spi.c:161
int errcode(int sqlerrcode)
Definition: elog.c:575
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
ErrorContextCallback * error_context_stack
Definition: elog.c:88
bool fn_retisset
Definition: plperl.c:120
#define ERROR
Definition: elog.h:43
#define SPI_OPT_NONATOMIC
Definition: spi.h:68
FmgrInfo result_in_func
Definition: plperl.c:124
static plperl_proc_desc * compile_plperl_function(Oid fn_oid, bool is_trigger, bool is_event_trigger)
Definition: plperl.c:2709
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:691
static void plperl_exec_callback(void *arg)
Definition: plperl.c:4110
#define increment_prodesc_refcount(prodesc)
Definition: plperl.c:133
Oid result_typioparam
Definition: plperl.c:125
#define ereport(elevel, rest)
Definition: elog.h:122
static void plperl_return_next_internal(SV *sv)
Definition: plperl.c:3246
uintptr_t Datum
Definition: postgres.h:367
Tuplestorestate * tuple_store
Definition: plperl.c:182
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:316
int allowedModes
Definition: execnodes.h:297
TupleDesc ret_tdesc
Definition: plperl.c:183
SetFunctionReturnMode returnMode
Definition: execnodes.h:299
#define SPI_OK_FINISH
Definition: spi.h:51
Tuplestorestate * setResult
Definition: execnodes.h:302
TupleDesc setDesc
Definition: execnodes.h:303
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
char * proname
Definition: plperl.c:108
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
ExprDoneCond isDone
Definition: execnodes.h:300
plperl_interp_desc * interp
Definition: plperl.c:114
static plperl_call_data * current_call_data
Definition: plperl.c:247
static SV * plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
Definition: plperl.c:2172

◆ plperl_hash_from_datum()

static SV * plperl_hash_from_datum ( Datum  attr)
static

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

2994 {
2995  HeapTupleHeader td;
2996  Oid tupType;
2997  int32 tupTypmod;
2998  TupleDesc tupdesc;
2999  HeapTupleData tmptup;
3000  SV *sv;
3001 
3002  td = DatumGetHeapTupleHeader(attr);
3003 
3004  /* Extract rowtype info and find a tupdesc */
3005  tupType = HeapTupleHeaderGetTypeId(td);
3006  tupTypmod = HeapTupleHeaderGetTypMod(td);
3007  tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
3008 
3009  /* Build a temporary HeapTuple control structure */
3010  tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
3011  tmptup.t_data = td;
3012 
3013  sv = plperl_hash_from_tuple(&tmptup, tupdesc);
3014  ReleaseTupleDesc(tupdesc);
3015 
3016  return sv;
3017 }
TupleDesc lookup_rowtype_tupdesc(Oid type_id, int32 typmod)
Definition: typcache.c:1641
unsigned int Oid
Definition: postgres_ext.h:31
#define DatumGetHeapTupleHeader(X)
Definition: fmgr.h:264
signed int int32
Definition: c.h:313
HeapTupleHeader t_data
Definition: htup.h:68
#define HeapTupleHeaderGetTypMod(tup)
Definition: htup_details.h:467
uint32 t_len
Definition: htup.h:64
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: plperl.c:3021
#define HeapTupleHeaderGetTypeId(tup)
Definition: htup_details.h:457
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:124
#define HeapTupleHeaderGetDatumLength(tup)
Definition: htup_details.h:451

◆ plperl_hash_from_tuple()

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

Definition at line 3021 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, tupleDesc::natts, newRV_noinc, OidFunctionCall1, OidIsValid, OidOutputFunctionCall(), pfree(), plperl_hash_from_datum(), plperl_ref_from_pg_array(), plperl_call_data::prodesc, plperl_proc_desc::trftypes, TupleDescAttr, and type_is_rowtype().

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

3022 {
3023  dTHX;
3024  HV *hv;
3025  int i;
3026 
3027  /* since this function recurses, it could be driven to stack overflow */
3029 
3030  hv = newHV();
3031  hv_ksplit(hv, tupdesc->natts); /* pre-grow the hash */
3032 
3033  for (i = 0; i < tupdesc->natts; i++)
3034  {
3035  Datum attr;
3036  bool isnull,
3037  typisvarlena;
3038  char *attname;
3039  Oid typoutput;
3040  Form_pg_attribute att = TupleDescAttr(tupdesc, i);
3041 
3042  if (att->attisdropped)
3043  continue;
3044 
3045  attname = NameStr(att->attname);
3046  attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
3047 
3048  if (isnull)
3049  {
3050  /*
3051  * Store (attname => undef) and move on. Note we can't use
3052  * &PL_sv_undef here; see "AVs, HVs and undefined values" in
3053  * perlguts for an explanation.
3054  */
3055  hv_store_string(hv, attname, newSV(0));
3056  continue;
3057  }
3058 
3059  if (type_is_rowtype(att->atttypid))
3060  {
3061  SV *sv = plperl_hash_from_datum(attr);
3062 
3063  hv_store_string(hv, attname, sv);
3064  }
3065  else
3066  {
3067  SV *sv;
3068  Oid funcid;
3069 
3070  if (OidIsValid(get_base_element_type(att->atttypid)))
3071  sv = plperl_ref_from_pg_array(attr, att->atttypid);
3073  sv = (SV *) DatumGetPointer(OidFunctionCall1(funcid, attr));
3074  else
3075  {
3076  char *outputstr;
3077 
3078  /* XXX should have a way to cache these lookups */
3079  getTypeOutputInfo(att->atttypid, &typoutput, &typisvarlena);
3080 
3081  outputstr = OidOutputFunctionCall(typoutput, attr);
3082  sv = cstr2sv(outputstr);
3083  pfree(outputstr);
3084  }
3085 
3086  hv_store_string(hv, attname, sv);
3087  }
3088  }
3089  return newRV_noinc((SV *) hv);
3090 }
static SV * plperl_ref_from_pg_array(Datum arg, Oid typid)
Definition: plperl.c:1467
Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1876
List * trftypes
Definition: plperl.c:117
void getTypeOutputInfo(Oid type, Oid *typOutput, bool *typIsVarlena)
Definition: lsyscache.c:2650
#define dTHX
Definition: ppport.h:3208
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
static SV * plperl_hash_from_datum(Datum attr)
Definition: plperl.c:2993
static SV ** hv_store_string(HV *hv, const char *key, SV *val)
Definition: plperl.c:4060
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
int natts
Definition: tupdesc.h:82
void pfree(void *pointer)
Definition: mcxt.c:1031
NameData attname
Definition: pg_attribute.h:40
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:628
void check_stack_depth(void)
Definition: postgres.c:3159
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2409
#define heap_getattr(tup, attnum, tupleDesc, isnull)
Definition: htup_details.h:781
#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:534
Oid get_base_element_type(Oid typid)
Definition: lsyscache.c:2575
char * OidOutputFunctionCall(Oid functionId, Datum val)
Definition: fmgr.c:1833
int i
#define NameStr(name)
Definition: c.h:576
plperl_proc_desc * prodesc
Definition: plperl.c:179
static plperl_call_data * current_call_data
Definition: plperl.c:247

◆ plperl_hash_to_datum()

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

Definition at line 1133 of file plperl.c.

References HeapTupleGetDatum, and plperl_build_tuple_result().

Referenced by plperl_sv_to_datum().

1134 {
1135  HeapTuple tup = plperl_build_tuple_result((HV *) SvRV(src), td);
1136 
1137  return HeapTupleGetDatum(tup);
1138 }
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:231
static HeapTuple plperl_build_tuple_result(HV *perlhash, TupleDesc td)
Definition: plperl.c:1082

◆ plperl_init_interp()

static PerlInterpreter * plperl_init_interp ( void  )
static

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

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

◆ plperl_init_shared_libs()

static void plperl_init_shared_libs ( pTHX  )
static

Definition at line 2160 of file plperl.c.

References boot_DynaLoader(), and boot_PostgreSQL__InServer__Util().

Referenced by plperl_init_interp().

2161 {
2162  char *file = __FILE__;
2163 
2164  newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
2165  newXS("PostgreSQL::InServer::Util::bootstrap",
2167  /* newXS for...::SPI::bootstrap is in select_perl_context() */
2168 }
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 4134 of file plperl.c.

References dTHX, errcontext, locale, and setlocale.

Referenced by plperl_inline_handler().

4135 {
4136  errcontext("PL/Perl anonymous code block");
4137 }
#define errcontext
Definition: elog.h:164

◆ plperl_inline_handler()

Datum plperl_inline_handler ( PG_FUNCTION_ARGS  )

Definition at line 1875 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, FunctionCallInfoData::flinfo, FmgrInfo::fn_mcxt, FmgrInfo::fn_oid, plperl_proc_desc::fn_readonly, plperl_proc_desc::fn_retisarray, plperl_proc_desc::fn_retisset, plperl_proc_desc::fn_retistuple, InvalidOid, plperl_proc_desc::lang_oid, InlineCodeBlock::langIsTrusted, InlineCodeBlock::langOid, plperl_proc_desc::lanpltrusted, MemSet, plperl_proc_desc::nargs, NIL, PG_CATCH, PG_END_TRY, PG_FUNCTION_INFO_V1(), PG_GETARG_POINTER, PG_RE_THROW, 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(), 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().

1876 {
1878  FunctionCallInfoData fake_fcinfo;
1879  FmgrInfo flinfo;
1880  plperl_proc_desc desc;
1881  plperl_call_data *volatile save_call_data = current_call_data;
1882  plperl_interp_desc *volatile oldinterp = plperl_active_interp;
1883  plperl_call_data this_call_data;
1884  ErrorContextCallback pl_error_context;
1885 
1886  /* Initialize current-call status record */
1887  MemSet(&this_call_data, 0, sizeof(this_call_data));
1888 
1889  /* Set up a callback for error reporting */
1890  pl_error_context.callback = plperl_inline_callback;
1891  pl_error_context.previous = error_context_stack;
1892  pl_error_context.arg = NULL;
1893  error_context_stack = &pl_error_context;
1894 
1895  /*
1896  * Set up a fake fcinfo and descriptor with just enough info to satisfy
1897  * plperl_call_perl_func(). In particular note that this sets things up
1898  * with no arguments passed, and a result type of VOID.
1899  */
1900  MemSet(&fake_fcinfo, 0, sizeof(fake_fcinfo));
1901  MemSet(&flinfo, 0, sizeof(flinfo));
1902  MemSet(&desc, 0, sizeof(desc));
1903  fake_fcinfo.flinfo = &flinfo;
1904  flinfo.fn_oid = InvalidOid;
1905  flinfo.fn_mcxt = CurrentMemoryContext;
1906 
1907  desc.proname = "inline_code_block";
1908  desc.fn_readonly = false;
1909 
1910  desc.lang_oid = codeblock->langOid;
1911  desc.trftypes = NIL;
1912  desc.lanpltrusted = codeblock->langIsTrusted;
1913 
1914  desc.fn_retistuple = false;
1915  desc.fn_retisset = false;
1916  desc.fn_retisarray = false;
1917  desc.result_oid = InvalidOid;
1918  desc.nargs = 0;
1919  desc.reference = NULL;
1920 
1921  this_call_data.fcinfo = &fake_fcinfo;
1922  this_call_data.prodesc = &desc;
1923  /* we do not bother with refcounting the fake prodesc */
1924 
1925  PG_TRY();
1926  {
1927  SV *perlret;
1928 
1929  current_call_data = &this_call_data;
1930 
1931  if (SPI_connect_ext(codeblock->atomic ? 0 : SPI_OPT_NONATOMIC) != SPI_OK_CONNECT)
1932  elog(ERROR, "could not connect to SPI manager");
1933 
1935 
1936  plperl_create_sub(&desc, codeblock->source_text, 0);
1937 
1938  if (!desc.reference) /* can this happen? */
1939  elog(ERROR, "could not create internal procedure for anonymous code block");
1940 
1941  perlret = plperl_call_perl_func(&desc, &fake_fcinfo);
1942 
1943  SvREFCNT_dec_current(perlret);
1944 
1945  if (SPI_finish() != SPI_OK_FINISH)
1946  elog(ERROR, "SPI_finish() failed");
1947  }
1948  PG_CATCH();
1949  {
1950  if (desc.reference)
1952  current_call_data = save_call_data;
1953  activate_interpreter(oldinterp);
1954  PG_RE_THROW();
1955  }
1956  PG_END_TRY();
1957 
1958  if (desc.reference)
1960 
1961  current_call_data = save_call_data;
1962  activate_interpreter(oldinterp);
1963 
1964  error_context_stack = pl_error_context.previous;
1965 
1966  PG_RETURN_VOID();
1967 }
#define NIL
Definition: pg_list.h:69
#define SPI_OK_CONNECT
Definition: spi.h:50
Definition: fmgr.h:56
List * trftypes
Definition: plperl.c:117
MemoryContext fn_mcxt
Definition: fmgr.h:65
FunctionCallInfo fcinfo
Definition: plperl.c:180
static void select_perl_context(bool trusted)
Definition: plperl.c:559
int SPI_connect_ext(int options)
Definition: spi.c:90
int SPI_finish(void)
Definition: spi.c:161
#define MemSet(start, val, len)
Definition: c.h:908
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
#define PG_GETARG_POINTER(n)
Definition: fmgr.h:246
void(* callback)(void *arg)
Definition: elog.h:239
struct ErrorContextCallback * previous
Definition: elog.h:238
ErrorContextCallback * error_context_stack
Definition: elog.c:88
FmgrInfo * flinfo
Definition: fmgr.h:79
bool fn_retisset
Definition: plperl.c:120
#define ERROR
Definition: elog.h:43
#define SPI_OPT_NONATOMIC
Definition: spi.h:68
static void activate_interpreter(plperl_interp_desc *interp_desc)
Definition: plperl.c:691
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void SvREFCNT_dec_current(SV *sv)
Definition: plperl.c:316
#define InvalidOid
Definition: postgres_ext.h:36
Oid fn_oid
Definition: fmgr.h:59
bool fn_readonly
Definition: plperl.c:115
static void plperl_inline_callback(void *arg)
Definition: plperl.c:4134
#define PG_RETURN_VOID()
Definition: fmgr.h:314
#define PG_CATCH()
Definition: elog.h:293
SV * reference
Definition: plperl.c:113
#define SPI_OK_FINISH
Definition: spi.h:51
#define PG_RE_THROW()
Definition: elog.h:314
bool fn_retistuple
Definition: plperl.c:119
static void plperl_create_sub(plperl_proc_desc *desc, const char *s, Oid fn_oid)
Definition: plperl.c:2085
char * proname
Definition: plperl.c:108
char * source_text
Definition: parsenodes.h:2813
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:247
#define PG_END_TRY()
Definition: elog.h:300
bool lanpltrusted
Definition: plperl.c:118
static SV * plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
Definition: plperl.c:2172
bool fn_retisarray
Definition: plperl.c:121

◆ plperl_modify_tuple()

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

Definition at line 1743 of file plperl.c.

References dTHX, ereport, errcode(), errmsg(), ERROR, heap_modify_tuple(), hek2cstr(), hv_fetch_string(), InvalidOid, tupleDesc::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().

1744 {
1745  dTHX;
1746  SV **svp;
1747  HV *hvNew;
1748  HE *he;
1749  HeapTuple rtup;
1750  TupleDesc tupdesc;
1751  int natts;
1752  Datum *modvalues;
1753  bool *modnulls;
1754  bool *modrepls;
1755 
1756  svp = hv_fetch_string(hvTD, "new");
1757  if (!svp)
1758  ereport(ERROR,
1759  (errcode(ERRCODE_UNDEFINED_COLUMN),
1760  errmsg("$_TD->{new} does not exist")));
1761  if (!SvOK(*svp) || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV)
1762  ereport(ERROR,
1763  (errcode(ERRCODE_DATATYPE_MISMATCH),
1764  errmsg("$_TD->{new} is not a hash reference")));
1765  hvNew = (HV *) SvRV(*svp);
1766 
1767  tupdesc = tdata->tg_relation->rd_att;
1768  natts = tupdesc->natts;
1769 
1770  modvalues = (Datum *) palloc0(natts * sizeof(Datum));
1771  modnulls = (bool *) palloc0(natts * sizeof(bool));
1772  modrepls = (bool *) palloc0(natts * sizeof(bool));
1773 
1774  hv_iterinit(hvNew);
1775  while ((he = hv_iternext(hvNew)))
1776  {
1777  char *key = hek2cstr(he);
1778  SV *val = HeVAL(he);
1779  int attn = SPI_fnumber(tupdesc, key);
1780  Form_pg_attribute attr = TupleDescAttr(tupdesc, attn - 1);
1781 
1782  if (attn == SPI_ERROR_NOATTRIBUTE)
1783  ereport(ERROR,
1784  (errcode(ERRCODE_UNDEFINED_COLUMN),
1785  errmsg("Perl hash contains nonexistent column \"%s\"",
1786  key)));
1787  if (attn <= 0)
1788  ereport(ERROR,
1789  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1790  errmsg("cannot set system attribute \"%s\"",
1791  key)));
1792 
1793  modvalues[attn - 1] = plperl_sv_to_datum(val,
1794  attr->atttypid,
1795  attr->atttypmod,
1796  NULL,
1797  NULL,
1798  InvalidOid,
1799  &modnulls[attn - 1]);
1800  modrepls[attn - 1] = true;
1801 
1802  pfree(key);
1803  }
1804  hv_iterinit(hvNew);
1805 
1806  rtup = heap_modify_tuple(otup, tupdesc, modvalues, modnulls, modrepls);
1807 
1808  pfree(modvalues);
1809  pfree(modnulls);
1810  pfree(modrepls);
1811 
1812  return rtup;
1813 }
int SPI_fnumber(TupleDesc tupdesc, const char *fname)
Definition: spi.c:857
static char * hek2cstr(HE *he)
Definition: plperl.c:327
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1314
#define dTHX
Definition: ppport.h:3208
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
int errcode(int sqlerrcode)
Definition: elog.c:575
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:4087
int natts
Definition: tupdesc.h:82
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define SPI_ERROR_NOATTRIBUTE
Definition: spi.h:44
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:197
#define ereport(elevel, rest)
Definition: elog.h:122
void * palloc0(Size size)
Definition: mcxt.c:955
uintptr_t Datum
Definition: postgres.h:367
TupleDesc rd_att
Definition: rel.h:85
#define InvalidOid
Definition: postgres_ext.h:36
int errmsg(const char *fmt,...)
Definition: elog.c:797
HeapTuple heap_modify_tuple(HeapTuple tuple, TupleDesc tupleDesc, Datum *replValues, bool *replIsnull, bool *doReplace)
Definition: heaptuple.c:1173
long val
Definition: informix.c:689
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 1467 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, typbyval, type_is_rowtype(), and typlen.

Referenced by plperl_call_perl_func(), and plperl_hash_from_tuple().

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

◆ plperl_return_next()

void plperl_return_next ( SV *  sv)

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

3219 {
3220  MemoryContext oldcontext = CurrentMemoryContext;
3221 
3222  PG_TRY();
3223  {
3225  }
3226  PG_CATCH();
3227  {
3228  ErrorData *edata;
3229 
3230  /* Must reset elog.c's state */
3231  MemoryContextSwitchTo(oldcontext);
3232  edata = CopyErrorData();
3233  FlushErrorState();
3234 
3235  /* Punt the error to Perl */
3236  croak_cstr(edata->message);
3237  }
3238  PG_END_TRY();
3239 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void FlushErrorState(void)
Definition: elog.c:1587
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void plperl_return_next_internal(SV *sv)
Definition: plperl.c:3246
static void croak_cstr(const char *str)
#define PG_CATCH()
Definition: elog.h:293
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343

◆ plperl_return_next_internal()

static void plperl_return_next_internal ( SV *  sv)
static

Definition at line 3246 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(), tupleDesc::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, FunctionCallInfoData::resultinfo, plperl_call_data::ret_tdesc, SFRM_Materialize_Random, plperl_call_data::tmp_cxt, plperl_call_data::tuple_store, tuplestore_begin_heap(), tuplestore_puttuple(), tuplestore_putvalues(), TYPEFUNC_COMPOSITE, TYPEFUNC_COMPOSITE_DOMAIN, and work_mem.

Referenced by plperl_func_handler(), and plperl_return_next().

3247 {
3248  plperl_proc_desc *prodesc;
3249  FunctionCallInfo fcinfo;
3250  ReturnSetInfo *rsi;
3251  MemoryContext old_cxt;
3252 
3253  if (!sv)
3254  return;
3255 
3256  prodesc = current_call_data->prodesc;
3257  fcinfo = current_call_data->fcinfo;
3258  rsi = (ReturnSetInfo *) fcinfo->resultinfo;
3259 
3260  if (!prodesc->fn_retisset)
3261  ereport(ERROR,
3262  (errcode(ERRCODE_SYNTAX_ERROR),
3263  errmsg("cannot use return_next in a non-SETOF function")));
3264 
3266  {
3267  TupleDesc tupdesc;
3268 
3270 
3271  /*
3272  * This is the first call to return_next in the current PL/Perl
3273  * function call, so identify the output tuple type and create a
3274  * tuplestore to hold the result rows.
3275  */
3276  if (prodesc->fn_retistuple)
3277  {
3278  TypeFuncClass funcclass;
3279  Oid typid;
3280 
3281  funcclass = get_call_result_type(fcinfo, &typid, &tupdesc);
3282  if (funcclass != TYPEFUNC_COMPOSITE &&
3283  funcclass != TYPEFUNC_COMPOSITE_DOMAIN)
3284  ereport(ERROR,
3285  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3286  errmsg("function returning record called in context "
3287  "that cannot accept type record")));
3288  /* if domain-over-composite, remember the domain's type OID */
3289  if (funcclass == TYPEFUNC_COMPOSITE_DOMAIN)
3290  current_call_data->cdomain_oid = typid;
3291  }
3292  else
3293  {
3294  tupdesc = rsi->expectedDesc;
3295  /* Protect assumption below that we return exactly one column */
3296  if (tupdesc == NULL || tupdesc->natts != 1)
3297  elog(ERROR, "expected single-column result descriptor for non-composite SETOF result");
3298  }
3299 
3300  /*
3301  * Make sure the tuple_store and ret_tdesc are sufficiently
3302  * long-lived.
3303  */
3305 
3309  false, work_mem);
3310 
3311  MemoryContextSwitchTo(old_cxt);
3312  }
3313 
3314  /*
3315  * Producing the tuple we want to return requires making plenty of
3316  * palloc() allocations that are not cleaned up. Since this function can
3317  * be called many times before the current memory context is reset, we
3318  * need to do those allocations in a temporary context.
3319  */
3320  if (!current_call_data->tmp_cxt)
3321  {
3324  "PL/Perl return_next temporary cxt",
3326  }
3327 
3329 
3330  if (prodesc->fn_retistuple)
3331  {
3332  HeapTuple tuple;
3333 
3334  if (!(SvOK(sv) && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV))
3335  ereport(ERROR,
3336  (errcode(ERRCODE_DATATYPE_MISMATCH),
3337  errmsg("SETOF-composite-returning PL/Perl function "
3338  "must call return_next with reference to hash")));
3339 
3340  tuple = plperl_build_tuple_result((HV *) SvRV(sv),
3342 
3344  domain_check(HeapTupleGetDatum(tuple), false,
3348 
3350  }
3351  else if (prodesc->result_oid)
3352  {
3353  Datum ret[1];
3354  bool isNull[1];
3355 
3356  ret[0] = plperl_sv_to_datum(sv,
3357  prodesc->result_oid,
3358  -1,
3359  fcinfo,
3360  &prodesc->result_in_func,
3361  prodesc->result_typioparam,
3362  &isNull[0]);
3363 
3366  ret, isNull);
3367  }
3368 
3369  MemoryContextSwitchTo(old_cxt);
3371 }
void tuplestore_putvalues(Tuplestorestate *state, TupleDesc tdesc, Datum *values, bool *isnull)
Definition: tuplestore.c:750
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:112
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
static Datum plperl_sv_to_datum(SV *sv, Oid typid, int32 typmod, FunctionCallInfo fcinfo, FmgrInfo *finfo, Oid typioparam, bool *isnull)
Definition: plperl.c:1314
FunctionCallInfo fcinfo
Definition: plperl.c:180
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
void MemoryContextReset(MemoryContext context)
Definition: mcxt.c:136
unsigned int Oid
Definition: postgres_ext.h:31
#define OidIsValid(objectId)
Definition: c.h:605
int natts
Definition: tupdesc.h:82
void * cdomain_info
Definition: plperl.c:185
TupleDesc expectedDesc
Definition: execnodes.h:296
bool fn_retisset
Definition: plperl.c:120
#define ERROR
Definition: elog.h:43
FmgrInfo result_in_func
Definition: plperl.c:124
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
void tuplestore_puttuple(Tuplestorestate *state, HeapTuple tuple)
Definition: tuplestore.c:730
fmNodePtr resultinfo
Definition: fmgr.h:81
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
TypeFuncClass
Definition: funcapi.h:155
Oid result_typioparam
Definition: plperl.c:125
#define ereport(elevel, rest)
Definition: elog.h:122
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
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:122
Tuplestorestate * tuple_store
Definition: plperl.c:182
void domain_check(Datum value, bool isnull, Oid domainType, void **extra, MemoryContext mcxt)
Definition: domains.c:327
int allowedModes
Definition: execnodes.h:297
TupleDesc ret_tdesc
Definition: plperl.c:183
MemoryContext tmp_cxt
Definition: plperl.c:186
#define Assert(condition)
Definition: c.h:699
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:231
MemoryContext ecxt_per_query_memory
Definition: execnodes.h:225
static HeapTuple plperl_build_tuple_result(HV *perlhash, TupleDesc td)
Definition: plperl.c:1082
ExprContext * econtext
Definition: execnodes.h:295
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool fn_retistuple
Definition: plperl.c:119
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
static plperl_call_data * current_call_data
Definition: plperl.c:247

◆ plperl_spi_commit()

void plperl_spi_commit ( void  )

Definition at line 3961 of file plperl.c.

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

3962 {
3963  MemoryContext oldcontext = CurrentMemoryContext;
3964 
3965  PG_TRY();
3966  {
3968 
3969  SPI_commit();
3971  }
3972  PG_CATCH();
3973  {
3974  ErrorData *edata;
3975 
3976  /* Save error info */
3977  MemoryContextSwitchTo(oldcontext);
3978  edata = CopyErrorData();
3979  FlushErrorState();
3980 
3981  /* Punt the error to Perl */
3982  croak_cstr(edata->message);
3983  }
3984  PG_END_TRY();
3985 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void FlushErrorState(void)
Definition: elog.c:1587
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void SPI_commit(void)
Definition: spi.c:206
static void croak_cstr(const char *str)
#define PG_CATCH()
Definition: elog.h:293
void HoldPinnedPortals(void)
Definition: portalmem.c:1228
void SPI_start_transaction(void)
Definition: spi.c:197
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343

◆ plperl_spi_cursor_close()

void plperl_spi_cursor_close ( char *  cursor)

Definition at line 3521 of file plperl.c.

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

3522 {
3523  Portal p;
3524 
3526 
3527  p = SPI_cursor_find(cursor);
3528 
3529  if (p)
3530  {
3531  UnpinPortal(p);
3532  SPI_cursor_close(p);
3533  }
3534 }
void UnpinPortal(Portal portal)
Definition: portalmem.c:377
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1433
Definition: type.h:124
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
void SPI_cursor_close(Portal portal)
Definition: spi.c:1501

◆ plperl_spi_exec()

HV* plperl_spi_exec ( char *  query,
int  limit 
)

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

3107 {
3108  HV *ret_hv;
3109 
3110  /*
3111  * Execute the query inside a sub-transaction, so we can cope with errors
3112  * sanely
3113  */
3114  MemoryContext oldcontext = CurrentMemoryContext;
3116 
3118 
3120  /* Want to run inside function's memory context */
3121  MemoryContextSwitchTo(oldcontext);
3122 
3123  PG_TRY();
3124  {
3125  int spi_rv;
3126 
3127  pg_verifymbstr(query, strlen(query), false);
3128 
3130  limit);
3132  spi_rv);
3133 
3134  /* Commit the inner transaction, return to outer xact context */
3136  MemoryContextSwitchTo(oldcontext);
3137  CurrentResourceOwner = oldowner;
3138  }
3139  PG_CATCH();
3140  {
3141  ErrorData *edata;
3142 
3143  /* Save error info */
3144  MemoryContextSwitchTo(oldcontext);
3145  edata = CopyErrorData();
3146  FlushErrorState();
3147 
3148  /* Abort the inner transaction */
3150  MemoryContextSwitchTo(oldcontext);
3151  CurrentResourceOwner = oldowner;
3152 
3153  /* Punt the error to Perl */
3154  croak_cstr(edata->message);
3155 
3156  /* Can't get here, but keep compiler quiet */
3157  return NULL;
3158  }
3159  PG_END_TRY();
3160 
3161  return ret_hv;
3162 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4217
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:41
void FlushErrorState(void)
Definition: elog.c:1587
uint64 SPI_processed
Definition: spi.c:39
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4251
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void croak_cstr(const char *str)
bool fn_readonly
Definition: plperl.c:115
#define PG_CATCH()
Definition: elog.h:293
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4146
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition: plperl.c:3166
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:247
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
int SPI_execute(const char *src, bool read_only, long tcount)
Definition: spi.c:400

◆ plperl_spi_exec_prepared()

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

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

3686 {
3687  HV *ret_hv;
3688  SV **sv;
3689  int i,
3690  limit,
3691  spi_rv;
3692  char *nulls;
3693  Datum *argvalues;
3694  plperl_query_desc *qdesc;
3695  plperl_query_entry *hash_entry;
3696 
3697  /*
3698  * Execute the query inside a sub-transaction, so we can cope with errors
3699  * sanely
3700  */
3701  MemoryContext oldcontext = CurrentMemoryContext;
3703 
3705 
3707  /* Want to run inside function's memory context */
3708  MemoryContextSwitchTo(oldcontext);
3709 
3710  PG_TRY();
3711  {
3712  dTHX;
3713 
3714  /************************************************************
3715  * Fetch the saved plan descriptor, see if it's o.k.
3716  ************************************************************/
3717  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3718  HASH_FIND, NULL);
3719  if (hash_entry == NULL)
3720  elog(ERROR, "spi_exec_prepared: Invalid prepared query passed");
3721 
3722  qdesc = hash_entry->query_data;
3723  if (qdesc == NULL)
3724  elog(ERROR, "spi_exec_prepared: plperl query_hash value vanished");
3725 
3726  if (qdesc->nargs != argc)
3727  elog(ERROR, "spi_exec_prepared: expected %d argument(s), %d passed",
3728  qdesc->nargs, argc);
3729 
3730  /************************************************************
3731  * Parse eventual attributes
3732  ************************************************************/
3733  limit = 0;
3734  if (attr != NULL)
3735  {
3736  sv = hv_fetch_string(attr, "limit");
3737  if (sv && *sv && SvIOK(*sv))
3738  limit = SvIV(*sv);
3739  }
3740  /************************************************************
3741  * Set up arguments
3742  ************************************************************/
3743  if (argc > 0)
3744  {
3745  nulls = (char *) palloc(argc);
3746  argvalues = (Datum *) palloc(argc * sizeof(Datum));
3747  }
3748  else
3749  {
3750  nulls = NULL;
3751  argvalues = NULL;
3752  }
3753 
3754  for (i = 0; i < argc; i++)
3755  {
3756  bool isnull;
3757 
3758  argvalues[i] = plperl_sv_to_datum(argv[i],
3759  qdesc->argtypes[i],
3760  -1,
3761  NULL,
3762  &qdesc->arginfuncs[i],
3763  qdesc->argtypioparams[i],
3764  &isnull);
3765  nulls[i] = isnull ? 'n' : ' ';
3766  }
3767 
3768  /************************************************************
3769  * go
3770  ************************************************************/
3771  spi_rv = SPI_execute_plan(qdesc->plan, argvalues, nulls,
3774  spi_rv);
3775  if (argc > 0)
3776  {
3777  pfree(argvalues);
3778  pfree(nulls);
3779  }
3780 
3781  /* Commit the inner transaction, return to outer xact context */
3783  MemoryContextSwitchTo(oldcontext);
3784  CurrentResourceOwner = oldowner;
3785  }
3786  PG_CATCH();
3787  {
3788  ErrorData *edata;
3789 
3790  /* Save error info */
3791  MemoryContextSwitchTo(oldcontext);
3792  edata = CopyErrorData();
3793  FlushErrorState();
3794 
3795  /* Abort the inner transaction */
3797  MemoryContextSwitchTo(oldcontext);
3798  CurrentResourceOwner = oldowner;
3799 
3800  /* Punt the error to Perl */
3801  croak_cstr(edata->message);
3802 
3803  /* Can't get here, but keep compiler quiet */
3804  return NULL;
3805  }
3806  PG_END_TRY();
3807 
3808  return ret_hv;
3809 }
Definition: plperl.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:1314
HTAB * query_hash
Definition: plperl.c:93
#define dTHX
Definition: ppport.h:3208
SPIPlanPtr plan
Definition: plperl.c:196
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
Oid * argtypioparams
Definition: plperl.c:200
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4217
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:41
static SV ** hv_fetch_string(HV *hv, const char *key)
Definition: plperl.c:4087
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
void FlushErrorState(void)
Definition: elog.c:1587
uint64 SPI_processed
Definition: spi.c:39
void pfree(void *pointer)
Definition: mcxt.c:1031
#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:435
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4251
Oid * argtypes
Definition: plperl.c:198
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * arginfuncs
Definition: plperl.c:199
static void croak_cstr(const char *str)
bool fn_readonly
Definition: plperl.c:115
#define PG_CATCH()
Definition: elog.h:293
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4146
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
void * palloc(Size size)
Definition: mcxt.c:924
int i
static HV * plperl_spi_execute_fetch_result(SPITupleTable *, uint64, int)
Definition: plperl.c:3166
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:247
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
plperl_query_desc * query_data
Definition: plperl.c:208

◆ plperl_spi_execute_fetch_result()

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

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

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

◆ plperl_spi_fetchrow()

SV* plperl_spi_fetchrow ( char *  cursor)

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

3448 {
3449  SV *row;
3450 
3451  /*
3452  * Execute the FETCH inside a sub-transaction, so we can cope with errors
3453  * sanely
3454  */
3455  MemoryContext oldcontext = CurrentMemoryContext;
3457 
3459 
3461  /* Want to run inside function's memory context */
3462  MemoryContextSwitchTo(oldcontext);
3463 
3464  PG_TRY();
3465  {
3466  dTHX;
3468 
3469  if (!p)
3470  {
3471  row = &PL_sv_undef;
3472  }
3473  else
3474  {
3475  SPI_cursor_fetch(p, true, 1);
3476  if (SPI_processed == 0)
3477  {
3478  UnpinPortal(p);
3479  SPI_cursor_close(p);
3480  row = &PL_sv_undef;
3481  }
3482  else
3483  {
3486  }
3488  }
3489 
3490  /* Commit the inner transaction, return to outer xact context */
3492  MemoryContextSwitchTo(oldcontext);
3493  CurrentResourceOwner = oldowner;
3494  }
3495  PG_CATCH();
3496  {
3497  ErrorData *edata;
3498 
3499  /* Save error info */
3500  MemoryContextSwitchTo(oldcontext);
3501  edata = CopyErrorData();
3502  FlushErrorState();
3503 
3504  /* Abort the inner transaction */
3506  MemoryContextSwitchTo(oldcontext);
3507  CurrentResourceOwner = oldowner;
3508 
3509  /* Punt the error to Perl */
3510  croak_cstr(edata->message);
3511 
3512  /* Can't get here, but keep compiler quiet */
3513  return NULL;
3514  }
3515  PG_END_TRY();
3516 
3517  return row;
3518 }
void UnpinPortal(Portal portal)
Definition: portalmem.c:377
#define dTHX
Definition: ppport.h:3208
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4217
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
SPITupleTable * SPI_tuptable
Definition: spi.c:41
#define PL_sv_undef
Definition: ppport.h:4129
HeapTuple * vals
Definition: spi.h:28
void FlushErrorState(void)
Definition: elog.c:1587
uint64 SPI_processed
Definition: spi.c:39
Portal SPI_cursor_find(const char *name)
Definition: spi.c:1433
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4251
Definition: type.h:124
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static SV * plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
Definition: plperl.c:3021
void SPI_freetuptable(SPITupleTable *tuptable)
Definition: spi.c:1068
static void croak_cstr(const char *str)
TupleDesc tupdesc
Definition: spi.h:27
#define PG_CATCH()
Definition: elog.h:293
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4146
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
void SPI_cursor_close(Portal portal)
Definition: spi.c:1501
void SPI_cursor_fetch(Portal portal, bool forward, long count)
Definition: spi.c:1445
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343

◆ plperl_spi_freeplan()

void plperl_spi_freeplan ( char *  query)

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

3931 {
3932  SPIPlanPtr plan;
3933  plperl_query_desc *qdesc;
3934  plperl_query_entry *hash_entry;
3935 
3937 
3938  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3939  HASH_FIND, NULL);
3940  if (hash_entry == NULL)
3941  elog(ERROR, "spi_freeplan: Invalid prepared query passed");
3942 
3943  qdesc = hash_entry->query_data;
3944  if (qdesc == NULL)
3945  elog(ERROR, "spi_freeplan: plperl query_hash value vanished");
3946  plan = qdesc->plan;
3947 
3948  /*
3949  * free all memory before SPI_freeplan, so if it dies, nothing will be
3950  * left over
3951  */
3953  HASH_REMOVE, NULL);
3954 
3955  MemoryContextDelete(qdesc->plan_cxt);
3956 
3957  SPI_freeplan(plan);
3958 }
Definition: plperl.c:205
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
HTAB * query_hash
Definition: plperl.c:93
SPIPlanPtr plan
Definition: plperl.c:196
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
#define ERROR
Definition: elog.h:43
MemoryContext plan_cxt
Definition: plperl.c:195
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:705
#define elog
Definition: elog.h:219
plperl_query_desc * query_data
Definition: plperl.c:208

◆ plperl_spi_prepare()

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

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

3538 {
3539  volatile SPIPlanPtr plan = NULL;
3540  volatile MemoryContext plan_cxt = NULL;
3541  plperl_query_desc *volatile qdesc = NULL;
3542  plperl_query_entry *volatile hash_entry = NULL;
3543  MemoryContext oldcontext = CurrentMemoryContext;
3545  MemoryContext work_cxt;
3546  bool found;
3547  int i;
3548 
3550 
3552  MemoryContextSwitchTo(oldcontext);
3553 
3554  PG_TRY();
3555  {
3557 
3558  /************************************************************
3559  * Allocate the new querydesc structure
3560  *
3561  * The qdesc struct, as well as all its subsidiary data, lives in its
3562  * plan_cxt. But note that the SPIPlan does not.
3563  ************************************************************/
3565  "PL/Perl spi_prepare query",
3567  MemoryContextSwitchTo(plan_cxt);
3568  qdesc = (plperl_query_desc *) palloc0(sizeof(plperl_query_desc));
3569  snprintf(qdesc->qname, sizeof(qdesc->qname), "%p", qdesc);
3570  qdesc->plan_cxt = plan_cxt;
3571  qdesc->nargs = argc;
3572  qdesc->argtypes = (Oid *) palloc(argc * sizeof(Oid));
3573  qdesc->arginfuncs = (FmgrInfo *) palloc(argc * sizeof(FmgrInfo));
3574  qdesc->argtypioparams = (Oid *) palloc(argc * sizeof(Oid));
3575  MemoryContextSwitchTo(oldcontext);
3576 
3577  /************************************************************
3578  * Do the following work in a short-lived context so that we don't
3579  * leak a lot of memory in the PL/Perl function's SPI Proc context.
3580  ************************************************************/
3582  "PL/Perl spi_prepare workspace",
3584  MemoryContextSwitchTo(work_cxt);
3585 
3586  /************************************************************
3587  * Resolve argument type names and then look them up by oid
3588  * in the system cache, and remember the required information
3589  * for input conversion.
3590  ************************************************************/
3591  for (i = 0; i < argc; i++)
3592  {
3593  Oid typId,
3594  typInput,
3595  typIOParam;
3596  int32 typmod;
3597  char *typstr;
3598 
3599  typstr = sv2cstr(argv[i]);
3600  parseTypeString(typstr, &typId, &typmod, false);
3601  pfree(typstr);
3602 
3603  getTypeInputInfo(typId, &typInput, &typIOParam);
3604 
3605  qdesc->argtypes[i] = typId;
3606  fmgr_info_cxt(typInput, &(qdesc->arginfuncs[i]), plan_cxt);
3607  qdesc->argtypioparams[i] = typIOParam;
3608  }
3609 
3610  /* Make sure the query is validly encoded */
3611  pg_verifymbstr(query, strlen(query), false);
3612 
3613  /************************************************************
3614  * Prepare the plan and check for errors
3615  ************************************************************/
3616  plan = SPI_prepare(query, argc, qdesc->argtypes);
3617 
3618  if (plan == NULL)
3619  elog(ERROR, "SPI_prepare() failed:%s",
3621 
3622  /************************************************************
3623  * Save the plan into permanent memory (right now it's in the
3624  * SPI procCxt, which will go away at function end).
3625  ************************************************************/
3626  if (SPI_keepplan(plan))
3627  elog(ERROR, "SPI_keepplan() failed");
3628  qdesc->plan = plan;
3629 
3630  /************************************************************
3631  * Insert a hashtable entry for the plan.
3632  ************************************************************/
3634  qdesc->qname,
3635  HASH_ENTER, &found);
3636  hash_entry->query_data = qdesc;
3637 
3638  /* Get rid of workspace */
3639  MemoryContextDelete(work_cxt);
3640 
3641  /* Commit the inner transaction, return to outer xact context */
3643  MemoryContextSwitchTo(oldcontext);
3644  CurrentResourceOwner = oldowner;
3645  }
3646  PG_CATCH();
3647  {
3648  ErrorData *edata;
3649 
3650  /* Save error info */
3651  MemoryContextSwitchTo(oldcontext);
3652  edata = CopyErrorData();
3653  FlushErrorState();
3654 
3655  /* Drop anything we managed to allocate */
3656  if (hash_entry)
3658  qdesc->qname,
3659  HASH_REMOVE, NULL);
3660  if (plan_cxt)
3661  MemoryContextDelete(plan_cxt);
3662  if (plan)
3663  SPI_freeplan(plan);
3664 
3665  /* Abort the inner transaction */
3667  MemoryContextSwitchTo(oldcontext);
3668  CurrentResourceOwner = oldowner;
3669 
3670  /* Punt the error to Perl */
3671  croak_cstr(edata->message);
3672 
3673  /* Can't get here, but keep compiler quiet */
3674  return NULL;
3675  }
3676  PG_END_TRY();
3677 
3678  /************************************************************
3679  * Return the query's hash key to the caller.
3680  ************************************************************/
3681  return cstr2sv(qdesc->qname);
3682 }
Definition: fmgr.h:56
Definition: plperl.c:205
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
HTAB * query_hash
Definition: plperl.c:93
SPIPlanPtr plan
Definition: plperl.c:196
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
Oid * argtypioparams
Definition: plperl.c:200
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:578
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4217
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
unsigned int Oid
Definition: postgres_ext.h:31
void FlushErrorState(void)
Definition: elog.c:1587
signed int int32
Definition: c.h:313
int SPI_result
Definition: spi.c:42
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1611
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:192
int SPI_keepplan(SPIPlanPtr plan)
Definition: spi.c:656
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4251
Oid * argtypes
Definition: plperl.c:198
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
MemoryContext plan_cxt
Definition: plperl.c:195
void fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
Definition: fmgr.c:134
void getTypeInputInfo(Oid type, Oid *typInput, Oid *typIOParam)
Definition: lsyscache.c:2617
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSetContextCreate(parent, name, allocparams)
Definition: memutils.h:170
void * palloc0(Size size)
Definition: mcxt.c:955
char qname[24]
Definition: plperl.c:194
FmgrInfo * arginfuncs
Definition: plperl.c:199
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:815
#define PG_CATCH()
Definition: elog.h:293
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4146
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:705
void * palloc(Size size)
Definition: mcxt.c:924
int i
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:98
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
static char * sv2cstr(SV *sv)
plperl_query_desc * query_data
Definition: plperl.c:208

◆ plperl_spi_query()

SV* plperl_spi_query ( char *  query)

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

3376 {
3377  SV *cursor;
3378 
3379  /*
3380  * Execute the query inside a sub-transaction, so we can cope with errors
3381  * sanely
3382  */
3383  MemoryContext oldcontext = CurrentMemoryContext;
3385 
3387 
3389  /* Want to run inside function's memory context */
3390  MemoryContextSwitchTo(oldcontext);
3391 
3392  PG_TRY();
3393  {
3394  SPIPlanPtr plan;
3395  Portal portal;
3396 
3397  /* Make sure the query is validly encoded */
3398  pg_verifymbstr(query, strlen(query), false);
3399 
3400  /* Create a cursor for the query */
3401  plan = SPI_prepare(query, 0, NULL);
3402  if (plan == NULL)
3403  elog(ERROR, "SPI_prepare() failed:%s",
3405 
3406  portal = SPI_cursor_open(NULL, plan, NULL, NULL, false);
3407  SPI_freeplan(plan);
3408  if (portal == NULL)
3409  elog(ERROR, "SPI_cursor_open() failed:%s",
3411  cursor = cstr2sv(portal->name);
3412 
3413  PinPortal(portal);
3414 
3415  /* Commit the inner transaction, return to outer xact context */
3417  MemoryContextSwitchTo(oldcontext);
3418  CurrentResourceOwner = oldowner;
3419  }
3420  PG_CATCH();
3421  {
3422  ErrorData *edata;
3423 
3424  /* Save error info */
3425  MemoryContextSwitchTo(oldcontext);
3426  edata = CopyErrorData();
3427  FlushErrorState();
3428 
3429  /* Abort the inner transaction */
3431  MemoryContextSwitchTo(oldcontext);
3432  CurrentResourceOwner = oldowner;
3433 
3434  /* Punt the error to Perl */
3435  croak_cstr(edata->message);
3436 
3437  /* Can't get here, but keep compiler quiet */
3438  return NULL;
3439  }
3440  PG_END_TRY();
3441 
3442  return cursor;
3443 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
SPIPlanPtr SPI_prepare(const char *src, int nargs, Oid *argtypes)
Definition: spi.c:578
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4217
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:1127
void FlushErrorState(void)
Definition: elog.c:1587
int SPI_result
Definition: spi.c:42
const char * name
Definition: portal.h:117
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1611
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4251
void PinPortal(Portal portal)
Definition: portalmem.c:368
Definition: type.h:124
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void croak_cstr(const char *str)
static SV * cstr2sv(const char *str)
#define PG_CATCH()
Definition: elog.h:293
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4146
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
int SPI_freeplan(SPIPlanPtr plan)
Definition: spi.c:705
bool pg_verifymbstr(const char *mbstr, int len, bool noError)
Definition: wchar.c:1866
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343

◆ plperl_spi_query_prepared()

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

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

3813 {
3814  int i;
3815  char *nulls;
3816  Datum *argvalues;
3817  plperl_query_desc *qdesc;
3818  plperl_query_entry *hash_entry;
3819  SV *cursor;
3820  Portal portal = NULL;
3821 
3822  /*
3823  * Execute the query inside a sub-transaction, so we can cope with errors
3824  * sanely
3825  */
3826  MemoryContext oldcontext = CurrentMemoryContext;
3828 
3830 
3832  /* Want to run inside function's memory context */
3833  MemoryContextSwitchTo(oldcontext);
3834 
3835  PG_TRY();
3836  {
3837  /************************************************************
3838  * Fetch the saved plan descriptor, see if it's o.k.
3839  ************************************************************/
3840  hash_entry = hash_search(plperl_active_interp->query_hash, query,
3841  HASH_FIND, NULL);
3842  if (hash_entry == NULL)
3843  elog(ERROR, "spi_query_prepared: Invalid prepared query passed");
3844 
3845  qdesc = hash_entry->query_data;
3846  if (qdesc == NULL)
3847  elog(ERROR, "spi_query_prepared: plperl query_hash value vanished");
3848 
3849  if (qdesc->nargs != argc)
3850  elog(ERROR, "spi_query_prepared: expected %d argument(s), %d passed",
3851  qdesc->nargs, argc);
3852 
3853  /************************************************************
3854  * Set up arguments
3855  ************************************************************/
3856  if (argc > 0)
3857  {
3858  nulls = (char *) palloc(argc);
3859  argvalues = (Datum *) palloc(argc * sizeof(Datum));
3860  }
3861  else
3862  {
3863  nulls = NULL;
3864  argvalues = NULL;
3865  }
3866 
3867  for (i = 0; i < argc; i++)
3868  {
3869  bool isnull;
3870 
3871  argvalues[i] = plperl_sv_to_datum(argv[i],
3872  qdesc->argtypes[i],
3873  -1,
3874  NULL,
3875  &qdesc->arginfuncs[i],
3876  qdesc->argtypioparams[i],
3877  &isnull);
3878  nulls[i] = isnull ? 'n' : ' ';
3879  }
3880 
3881  /************************************************************
3882  * go
3883  ************************************************************/
3884  portal = SPI_cursor_open(NULL, qdesc->plan, argvalues, nulls,
3886  if (argc > 0)
3887  {
3888  pfree(argvalues);
3889  pfree(nulls);
3890  }
3891  if (portal == NULL)
3892  elog(ERROR, "SPI_cursor_open() failed:%s",
3894 
3895  cursor = cstr2sv(portal->name);
3896 
3897  PinPortal(portal);
3898 
3899  /* Commit the inner transaction, return to outer xact context */
3901  MemoryContextSwitchTo(oldcontext);
3902  CurrentResourceOwner = oldowner;
3903  }
3904  PG_CATCH();
3905  {
3906  ErrorData *edata;
3907 
3908  /* Save error info */
3909  MemoryContextSwitchTo(oldcontext);
3910  edata = CopyErrorData();
3911  FlushErrorState();
3912 
3913  /* Abort the inner transaction */
3915  MemoryContextSwitchTo(oldcontext);
3916  CurrentResourceOwner = oldowner;
3917 
3918  /* Punt the error to Perl */
3919  croak_cstr(edata->message);
3920 
3921  /* Can't get here, but keep compiler quiet */
3922  return NULL;
3923  }
3924  PG_END_TRY();
3925 
3926  return cursor;
3927 }
Definition: plperl.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:1314
HTAB * query_hash
Definition: plperl.c:93
SPIPlanPtr plan
Definition: plperl.c:196
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
Oid * argtypioparams
Definition: plperl.c:200
ResourceOwner CurrentResourceOwner
Definition: resowner.c:140
void ReleaseCurrentSubTransaction(void)
Definition: xact.c:4217
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
static plperl_interp_desc * plperl_active_interp
Definition: plperl.c:231
Portal SPI_cursor_open(const char *name, SPIPlanPtr plan, Datum *Values, const char *Nulls, bool read_only)
Definition: spi.c:1127
void * hash_search(HTAB *hashp, const void *keyPtr, HASHACTION action, bool *foundPtr)
Definition: dynahash.c:906
void FlushErrorState(void)
Definition: elog.c:1587
int SPI_result
Definition: spi.c:42
void pfree(void *pointer)
Definition: mcxt.c:1031
const char * name
Definition: portal.h:117
#define ERROR
Definition: elog.h:43
const char * SPI_result_code_string(int code)
Definition: spi.c:1611
void RollbackAndReleaseCurrentSubTransaction(void)
Definition: xact.c:4251
void PinPortal(Portal portal)
Definition: portalmem.c:368
Definition: type.h:124
Oid * argtypes
Definition: plperl.c:198
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
uintptr_t Datum
Definition: postgres.h:367
FmgrInfo * arginfuncs
Definition: plperl.c:199
static void croak_cstr(const char *str)
static SV * cstr2sv(const char *str)
bool fn_readonly
Definition: plperl.c:115
#define PG_CATCH()
Definition: elog.h:293
void BeginInternalSubTransaction(const char *name)
Definition: xact.c:4146
static void check_spi_usage_allowed(void)
Definition: plperl.c:3094
void * palloc(Size size)
Definition: mcxt.c:924
int i
plperl_proc_desc * prodesc
Definition: plperl.c:179
#define elog
Definition: elog.h:219
#define PG_TRY()
Definition: elog.h:284
static plperl_call_data * current_call_data
Definition: plperl.c:247
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343
plperl_query_desc * query_data
Definition: plperl.c:208

◆ plperl_spi_rollback()

void plperl_spi_rollback ( void  )

Definition at line 3988 of file plperl.c.

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

3989 {
3990  MemoryContext oldcontext = CurrentMemoryContext;
3991 
3992  PG_TRY();
3993  {
3995 
3996  SPI_rollback();
3998  }
3999  PG_CATCH();
4000  {
4001  ErrorData *edata;
4002 
4003  /* Save error info */
4004  MemoryContextSwitchTo(oldcontext);
4005  edata = CopyErrorData();
4006  FlushErrorState();
4007 
4008  /* Punt the error to Perl */
4009  croak_cstr(edata->message);
4010  }
4011  PG_END_TRY();
4012 }
ErrorData * CopyErrorData(void)
Definition: elog.c:1497
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
void SPI_rollback(void)
Definition: spi.c:240
void FlushErrorState(void)
Definition: elog.c:1587
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
static void croak_cstr(const char *str)
#define PG_CATCH()
Definition: elog.h:293
void HoldPinnedPortals(void)
Definition: portalmem.c:1228
void SPI_start_transaction(void)
Definition: spi.c:197
#define PG_TRY()
Definition: elog.h:284
#define PG_END_TRY()
Definition: elog.h:300
char * message
Definition: elog.h:343

◆ 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 1314 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(), tupleDesc::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().

1318 {
1319  FmgrInfo tmp;
1320  Oid funcid;
1321 
1322  /* we might recurse */
1324 
1325  *isnull = false;
1326 
1327  /*
1328  * Return NULL if result is undef, or if we're in a function returning
1329  * VOID. In the latter case, we should pay no attention to the last Perl
1330  * statement's result, and this is a convenient means to ensure that.
1331  */
1332  if (!sv || !SvOK(sv) || typid == VOIDOID)
1333  {
1334  /* look up type info if they did not pass it */
1335  if (!finfo)
1336  {
1337  _sv_to_datum_finfo(typid, &tmp, &typioparam);
1338  finfo = &tmp;
1339  }
1340  *isnull = true;
1341  /* must call typinput in case it wants to reject NULL */
1342  return InputFunctionCall(finfo, NULL, typioparam, typmod);
1343  }
1345  return OidFunctionCall1(funcid, PointerGetDatum(sv));
1346  else if (SvROK(sv))
1347  {
1348  /* handle references */
1349  SV *sav = get_perl_array_ref(sv);
1350 
1351  if (sav)
1352  {
1353  /* handle an arrayref */
1354  return plperl_array_to_datum(sav, typid, typmod);
1355  }
1356  else if (SvTYPE(SvRV(sv)) == SVt_PVHV)
1357  {
1358  /* handle a hashref */
1359  Datum ret;
1360  TupleDesc td;
1361  bool isdomain;
1362 
1363  if (!type_is_rowtype(typid))
1364  ereport(ERROR,
1365  (errcode(ERRCODE_DATATYPE_MISMATCH),
1366  errmsg("cannot convert Perl hash to non-composite type %s",
1367  format_type_be(typid))));
1368 
1369  td = lookup_rowtype_tupdesc_domain(typid, typmod, true);
1370  if (td != NULL)
1371  {
1372  /* Did we look through a domain? */
1373  isdomain = (typid != td->tdtypeid);
1374  }
1375  else
1376  {
1377  /* Must be RECORD, try to resolve based on call info */
1378  TypeFuncClass funcclass;
1379 
1380  if (fcinfo)
1381  funcclass = get_call_result_type(fcinfo, &typid, &td);
1382  else
1383  funcclass = TYPEFUNC_OTHER;
1384  if (funcclass != TYPEFUNC_COMPOSITE &&
1385  funcclass != TYPEFUNC_COMPOSITE_DOMAIN)
1386  ereport(ERROR,
1387  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1388  errmsg("function returning record called in context "
1389  "that cannot accept type record")));
1390  Assert(td);
1391  isdomain = (funcclass == TYPEFUNC_COMPOSITE_DOMAIN);
1392  }
1393 
1394  ret = plperl_hash_to_datum(sv, td);
1395 
1396  if (isdomain)
1397  domain_check(ret, false, typid, NULL, NULL);
1398 
1399  /* Release on the result of get_call_result_type is harmless */
1400  ReleaseTupleDesc(td);
1401 
1402  return ret;
1403  }
1404 
1405  /* Reference, but not reference to hash or array ... */
1406  ereport(ERROR,
1407  (errcode(ERRCODE_DATATYPE_MISMATCH),
1408  errmsg("PL/Perl function must return reference to hash or array")));
1409  return (Datum) 0; /* shut up compiler */
1410  }
1411  else
1412  {
1413  /* handle a string/number */
1414  Datum ret;
1415  char *str = sv2cstr(sv);
1416 
1417  /* did not pass in any typeinfo? look it up */
1418  if (!finfo)
1419  {
1420  _sv_to_datum_finfo(typid, &tmp, &typioparam);
1421  finfo = &tmp;
1422  }
1423 
1424  ret = InputFunctionCall(finfo, str, typioparam, typmod);
1425  pfree(str);
1426 
1427  return ret;
1428  }
1429 }
Definition: fmgr.h:56
List * trftypes
Definition: plperl.c:117
Oid tdtypeid
Definition: tupdesc.h:83
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
static SV * get_perl_array_ref(SV *sv)
Definition: plperl.c:1145
static Datum plperl_array_to_datum(SV *src, Oid typid, int32 typmod)
Definition: plperl.c:1247
#define PointerGetDatum(X)
Definition: postgres.h:541
int errcode(int sqlerrcode)
Definition: elog.c:575
char * format_type_be(Oid type_oid)
Definition: format_type.c:328
unsigned int Oid
Definition: postgres_ext.h:31
TupleDesc lookup_rowtype_tupdesc_domain(Oid type_id, int32 typmod, bool noError)
Definition: typcache.c:1697
void pfree(void *pointer)
Definition: mcxt.c:1031
#define ERROR
Definition: elog.h:43
#define OidFunctionCall1(functionId, arg1)
Definition: fmgr.h:628
void check_stack_depth(void)
Definition: postgres.c:3159
static Datum plperl_hash_to_datum(SV *src, TupleDesc td)
Definition: plperl.c:1133
TypeFuncClass
Definition: funcapi.h:155
bool type_is_rowtype(Oid typid)
Definition: lsyscache.c:2409
#define ereport(elevel, rest)
Definition: elog.h:122
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:1709
#define Assert(condition)
Definition: c.h:699
Oid get_transform_tosql(Oid typid, Oid langid, List *trftypes)
Definition: lsyscache.c:1897
int errmsg(const char *fmt,...)
Definition: elog.c:797
plperl_proc_desc * prodesc
Definition: plperl.c:179
static void _sv_to_datum_finfo(Oid typid, FmgrInfo *finfo, Oid *typioparam)
Definition: plperl.c:1291
#define ReleaseTupleDesc(tupdesc)
Definition: tupdesc.h:124
static plperl_call_data * current_call_data
Definition: plperl.c:247
static char * sv2cstr(SV *sv)

◆ plperl_sv_to_literal()

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

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

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

◆ plperl_trigger_build_args()