35 #include "utils/fmgroids.h"
46 #define TEXTDOMAIN PG_TEXTDOMAIN("plperl")
53 #include "perlchunks.h"
55 #include "plperl_opmask.h"
129 #define increment_prodesc_refcount(prodesc) \
130 ((prodesc)->refcount++)
131 #define decrement_prodesc_refcount(prodesc) \
133 if (--((prodesc)->refcount) <= 0) \
134 free_plperl_function(prodesc); \
264 bool is_event_trigger);
279 int *ndims,
int *dims,
int cur_depth,
302 static char *setlocale_perl(
int category,
char *
locale);
335 SV *sv = HeSVKEY_force(he);
376 static bool inited =
false;
391 gettext_noop(
"If true, trusted and untrusted Perl code will be compiled in strict mode."),
405 gettext_noop(
"Perl initialization code to execute when a Perl interpreter is initialized."),
427 gettext_noop(
"Perl initialization code to execute once when plperl is first used."),
435 gettext_noop(
"Perl initialization code to execute once when plperlu is first used."),
447 memset(&hash_ctl, 0,
sizeof(hash_ctl));
451 plperl_interp_hash =
hash_create(
"PL/Perl interpreters",
456 memset(&hash_ctl, 0,
sizeof(hash_ctl));
460 plperl_proc_hash =
hash_create(
"PL/Perl procedures",
548 PerlInterpreter *interp =
NULL;
556 interp_desc =
hash_search(plperl_interp_hash, &user_id,
571 memset(&hash_ctl, 0,
sizeof(hash_ctl));
622 plperl_active_interp =
NULL;
633 "cannot allocate multiple Perl interpreters on this platform");
646 newXS(
"PostgreSQL::InServer::SPI::bootstrap",
649 eval_pv(
"PostgreSQL::InServer::SPI::bootstrap()",
FALSE);
653 errcontext(
"while executing PostgreSQL::InServer::SPI::bootstrap")));
656 interp_desc->
interp = interp;
659 plperl_active_interp = interp_desc;
671 if (interp_desc && plperl_active_interp != interp_desc)
674 PERL_SET_CONTEXT(interp_desc->
interp);
677 plperl_active_interp = interp_desc;
689 static PerlInterpreter *
692 PerlInterpreter *plperl;
694 static char *embedding[3 + 2] = {
695 "",
"-e", PLC_PERLBOOT
728 loc = setlocale(LC_COLLATE,
NULL);
730 loc = setlocale(LC_CTYPE,
NULL);
732 loc = setlocale(LC_MONETARY,
NULL);
734 loc = setlocale(LC_NUMERIC,
NULL);
736 loc = setlocale(LC_TIME,
NULL);
739 #define PLPERL_RESTORE_LOCALE(name, saved) \
741 if (saved != NULL) { setlocale_perl(name, saved); pfree(saved); } \
747 embedding[nargs++] =
"-e";
760 #if defined(PERL_SYS_INIT3) && !defined(MYMALLOC)
762 static int perl_sys_init_done;
765 if (!perl_sys_init_done)
767 char *dummy_env[1] = {
NULL};
769 PERL_SYS_INIT3(&nargs, (
char ***) &embedding, (
char ***) &dummy_env);
782 perl_sys_init_done = 1;
789 plperl = perl_alloc();
791 elog(
ERROR,
"could not allocate Perl interpreter");
793 PERL_SET_CONTEXT(plperl);
794 perl_construct(plperl);
797 PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
812 #ifdef PLPERL_ENABLE_OPMASK_EARLY
824 nargs, embedding,
NULL) != 0)
827 errcontext(
"while parsing Perl initialization")));
829 if (perl_run(plperl) != 0)
832 errcontext(
"while running Perl initialization")));
834 #ifdef PLPERL_RESTORE_LOCALE
835 PLPERL_RESTORE_LOCALE(LC_COLLATE, save_collate);
836 PLPERL_RESTORE_LOCALE(LC_CTYPE, save_ctype);
837 PLPERL_RESTORE_LOCALE(LC_MONETARY, save_monetary);
838 PLPERL_RESTORE_LOCALE(LC_NUMERIC, save_numeric);
839 PLPERL_RESTORE_LOCALE(LC_TIME, save_time);
864 name = SvPV(sv, len);
865 if (!(name && len > 0 && *name))
868 svp = hv_fetch(GvHVn(PL_incgv), name, len, 0);
872 DIE(
aTHX_ "Unable to load %s into plperl", name);
894 if (interp && *interp)
908 if (PL_exit_flags & PERL_EXIT_DESTRUCT_END)
915 if (PL_endav && !PL_minus_c)
916 call_list(PL_scopestack_ix, PL_endav);
952 eval_pv(
"my $a=chr(0x100); return $a =~ /\\xa9/i",
FALSE);
973 stash = gv_stashpv(
"DynaLoader", GV_ADDWARN);
975 while ((sv = hv_iternextsv(stash, &key, &klen)))
979 SvREFCNT_dec(GvCV(sv));
986 hv_clear(PL_stashcache);
997 errcontext(
"while executing plperl.on_plperl_init")));
1018 errcontext(
"while executing plperl.on_plperlu_init")));
1030 int len = strlen(res);
1032 while (len > 0 && isspace((
unsigned char) res[len - 1]))
1050 memset(nulls,
true,
sizeof(
bool) * td->
natts);
1052 hv_iterinit(perlhash);
1053 while ((he = hv_iternext(perlhash)))
1055 SV *
val = HeVAL(he);
1059 if (attn <= 0 || td->attrs[attn - 1]->attisdropped)
1061 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1062 errmsg(
"Perl hash contains nonexistent column \"%s\"",
1066 td->
attrs[attn - 1]->atttypid,
1067 td->
attrs[attn - 1]->atttypmod,
1075 hv_iterinit(perlhash);
1099 if (SvOK(sv) && SvROK(sv))
1101 if (SvTYPE(SvRV(sv)) == SVt_PVAV)
1103 else if (sv_isa(sv,
"PostgreSQL::InServer::ARRAY"))
1105 HV *hv = (HV *) SvRV(sv);
1108 if (*sav && SvOK(*sav) && SvROK(*sav) &&
1109 SvTYPE(SvRV(*sav)) == SVt_PVAV)
1112 elog(
ERROR,
"could not get array reference from PostgreSQL::InServer::ARRAY object");
1123 int *ndims,
int *dims,
int cur_depth,
1128 int len = av_len(av) + 1;
1130 for (i = 0; i < len; i++)
1133 SV **svp = av_fetch(av, i,
FALSE);
1141 AV *nav = (AV *) SvRV(sav);
1144 if (cur_depth + 1 >
MAXDIM)
1146 (
errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
1147 errmsg(
"number of array dimensions (%d) exceeds the maximum allowed (%d)",
1148 cur_depth + 1,
MAXDIM)));
1151 if (i == 0 && *ndims == cur_depth)
1153 dims[*ndims] = av_len(nav) + 1;
1156 else if (av_len(nav) + 1 != dims[cur_depth])
1158 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1159 errmsg(
"multidimensional arrays must have array expressions with matching dimensions")));
1163 ndims, dims, cur_depth + 1,
1164 arraytypid, elemtypid, typmod,
1173 if (*ndims != cur_depth)
1175 (
errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1176 errmsg(
"multidimensional arrays must have array expressions with matching dimensions")));
1212 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1213 errmsg(
"cannot convert Perl array to non-array type %s",
1218 memset(dims, 0,
sizeof(dims));
1219 dims[0] = av_len((AV *) SvRV(src)) + 1;
1223 typid, elemtypid, typmod,
1224 &finfo, typioparam);
1229 for (i = 0; i < ndims; i++)
1244 &typinput, typioparam);
1278 if (!sv || !SvOK(sv) || typid ==
VOIDOID)
1300 else if (SvTYPE(SvRV(sv)) == SVt_PVHV)
1308 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1309 errmsg(
"cannot convert Perl hash to non-composite type %s",
1316 if (fcinfo ==
NULL ||
1319 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1320 errmsg(
"function returning record called in context "
1321 "that cannot accept type record")));
1334 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1335 errmsg(
"PL/Perl function must return reference to hash or array")));
1370 elog(
ERROR,
"lookup failed for type %s", fqtypename);
1381 &typoutput, &typisvarlena);
1413 &typlen, &typbyval, &typalign,
1414 &typdelim, &typioparam, &typoutputfunc);
1430 info->
nelems[0] = nitems;
1431 for (i = 1; i < info->
ndims; i++)
1437 (void) hv_store(hv,
"array", 5, av, 0);
1438 (void) hv_store(hv,
"typeoid", 7, newSViv(typid), 0);
1441 gv_stashpv(
"PostgreSQL::InServer::ARRAY", 0));
1459 if (nest >= info->
ndims - 1)
1463 for (i = first; i < last; i += info->
nelems[nest + 1])
1468 av_push(result, ref);
1481 AV *result = newAV();
1483 for (i = first; i < last; i++)
1491 av_push(result, newSV(0));
1504 av_push(result,
cstr2sv(val));
1600 when =
"INSTEAD OF";
1608 level =
"STATEMENT";
1655 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1656 errmsg(
"$_TD->{new} does not exist")));
1657 if (!SvOK(*svp) || !SvROK(*svp) || SvTYPE(SvRV(*svp)) != SVt_PVHV)
1659 (
errcode(ERRCODE_DATATYPE_MISMATCH),
1660 errmsg(
"$_TD->{new} is not a hash reference")));
1661 hvNew = (HV *) SvRV(*svp);
1669 while ((he = hv_iternext(hvNew)))
1673 SV *
val = HeVAL(he);
1676 if (attn <= 0 || tupdesc->attrs[attn - 1]->attisdropped)
1678 (
errcode(ERRCODE_UNDEFINED_COLUMN),
1679 errmsg(
"Perl hash contains nonexistent column \"%s\"",
1683 tupdesc->
attrs[attn - 1]->atttypid,
1684 tupdesc->
attrs[attn - 1]->atttypmod,
1690 modnulls[slotsused] = isnull ?
'n' :
' ';
1691 modattrs[slotsused] = attn;
1699 modattrs, modvalues, modnulls);
1706 elog(
ERROR,
"SPI_modifytuple failed: %s",
1733 MemSet(&this_call_data, 0,
sizeof(this_call_data));
1734 this_call_data.
fcinfo = fcinfo;
1738 current_call_data = &this_call_data;
1753 current_call_data = save_call_data;
1761 current_call_data = save_call_data;
1784 MemSet(&this_call_data, 0,
sizeof(this_call_data));
1797 MemSet(&fake_fcinfo, 0,
sizeof(fake_fcinfo));
1798 MemSet(&flinfo, 0,
sizeof(flinfo));
1799 MemSet(&desc, 0,
sizeof(desc));
1800 fake_fcinfo.
flinfo = &flinfo;
1804 desc.
proname =
"inline_code_block";
1816 this_call_data.
fcinfo = &fake_fcinfo;
1817 this_call_data.
prodesc = &desc;
1824 current_call_data = &this_call_data;
1827 elog(
ERROR,
"could not connect to SPI manager");
1834 elog(
ERROR,
"could not create internal procedure for anonymous code block");
1838 SvREFCNT_dec(perlret);
1847 current_call_data = save_call_data;
1856 current_call_data = save_call_data;
1882 bool is_trigger =
false;
1883 bool is_event_trigger =
false;
1889 elog(
ERROR,
"cache lookup failed for function %u", funcoid);
1900 (proc->prorettype ==
OPAQUEOID && proc->pronargs == 0))
1903 is_event_trigger =
true;
1904 else if (proc->prorettype !=
RECORDOID &&
1907 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1908 errmsg(
"PL/Perl functions cannot return type %s",
1914 &argtypes, &argnames, &argmodes);
1915 for (i = 0; i < numargs; i++)
1920 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1921 errmsg(
"PL/Perl functions cannot accept type %s",
1980 HV *pragma_hv = newHV();
1984 sprintf(subname,
"%s__%u", prodesc->
proname, fn_oid);
1993 PUSHs(sv_2mortal(
cstr2sv(subname)));
2002 PUSHs(sv_2mortal(
cstr2sv(s)));
2010 count = perl_call_pv(
"PostgreSQL::InServer::mkfunc",
2011 G_SCALAR | G_EVAL | G_KEEPERR);
2016 SV *sub_rv = (SV *) POPs;
2018 if (sub_rv && SvROK(sub_rv) && SvTYPE(SvRV(sub_rv)) == SVt_PVCV)
2030 (
errcode(ERRCODE_SYNTAX_ERROR),
2035 (
errmsg(
"didn't get a CODE reference from compiling function \"%s\"",
2051 char *file = __FILE__;
2054 newXS(
"PostgreSQL::InServer::Util::bootstrap",
2072 EXTEND(sp, desc->
nargs);
2074 for (i = 0; i < desc->
nargs; i++)
2082 PUSHs(sv_2mortal(sv));
2100 PUSHs(sv_2mortal(sv));
2106 count = perl_call_sv(desc->
reference, G_SCALAR | G_EVAL);
2115 elog(
ERROR,
"didn't get a return item from function");
2129 retval = newSVsv(POPs);
2153 TDsv =
get_sv(
"main::_TD", 0);
2161 EXTEND(sp, tg_trigger->
tgnargs);
2163 for (i = 0; i < tg_trigger->
tgnargs; i++)
2168 count = perl_call_sv(desc->
reference, G_SCALAR | G_EVAL);
2177 elog(
ERROR,
"didn't get a return item from trigger function");
2191 retval = newSVsv(POPs);
2214 TDsv =
get_sv(
"main::_TD", 0);
2225 count = perl_call_sv(desc->
reference, G_SCALAR | G_EVAL);
2234 elog(
ERROR,
"didn't get a return item from trigger function");
2248 retval = newSVsv(POPs);
2268 elog(
ERROR,
"could not connect to SPI manager");
2271 current_call_data->
prodesc = prodesc;
2289 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2290 errmsg(
"set-valued function called in context that "
2291 "cannot accept a set")));
2322 AV *rav = (AV *) SvRV(sav);
2324 while ((svp = av_fetch(rav, i,
FALSE)) !=
NULL)
2330 else if (SvOK(perlret))
2333 (
errcode(ERRCODE_DATATYPE_MISMATCH),
2334 errmsg(
"set-returning PL/Perl function must return "
2335 "reference to array or use return_next")));
2363 SvREFCNT_dec(perlret);
2381 elog(
ERROR,
"could not connect to SPI manager");
2385 current_call_data->
prodesc = prodesc;
2398 hvTD = (HV *) SvRV(svTD);
2409 if (perlret ==
NULL || !SvOK(perlret))
2447 (
errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2448 errmsg(
"ignoring modified row in DELETE trigger")));
2455 (
errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
2456 errmsg(
"result of PL/Perl trigger function must be undef, "
2457 "\"SKIP\", or \"MODIFY\"")));
2469 SvREFCNT_dec(perlret);
2484 elog(
ERROR,
"could not connect to SPI manager");
2488 current_call_data->
prodesc = prodesc;
2517 if (proc_ptr && proc_ptr->
proc_ptr)
2579 elog(
ERROR,
"cache lookup failed for function %u", fn_oid);
2585 plperl_error_context.
arg =
NameStr(procStruct->proname);
2593 proc_ptr =
hash_search(plperl_proc_hash, &proc_key,
2602 proc_ptr =
hash_search(plperl_proc_hash, &proc_key,
2616 if (prodesc ==
NULL)
2630 if (prodesc ==
NULL)
2632 (
errcode(ERRCODE_OUT_OF_MEMORY),
2633 errmsg(
"out of memory")));
2642 (
errcode(ERRCODE_OUT_OF_MEMORY),
2643 errmsg(
"out of memory")));
2660 elog(
ERROR,
"cache lookup failed for language %u",
2661 procStruct->prolang);
2671 if (!is_trigger && !is_event_trigger)
2679 elog(
ERROR,
"cache lookup failed for type %u",
2680 procStruct->prorettype);
2687 if (procStruct->prorettype ==
VOIDOID ||
2690 else if (procStruct->prorettype ==
TRIGGEROID ||
2695 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2696 errmsg(
"trigger functions can only be called "
2703 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2704 errmsg(
"PL/Perl functions cannot return type %s",
2709 prodesc->
result_oid = procStruct->prorettype;
2715 (typeStruct->typlen == -1 && typeStruct->typelem);
2727 if (!is_trigger && !is_event_trigger)
2729 prodesc->
nargs = procStruct->pronargs;
2730 for (i = 0; i < prodesc->
nargs; i++)
2737 elog(
ERROR,
"cache lookup failed for type %u",
2738 procStruct->proargtypes.values[i]);
2744 procStruct->proargtypes.values[i] !=
RECORDOID)
2748 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2749 errmsg(
"PL/Perl functions cannot accept type %s",
2754 procStruct->proargtypes.values[i] ==
RECORDOID)
2764 if (typeStruct->typelem != 0 && typeStruct->typlen == -1)
2800 elog(
ERROR,
"could not create PL/Perl internal procedure");
2808 proc_ptr =
hash_search(plperl_proc_hash, &proc_key,
2861 hv_ksplit(hv, tupdesc->
natts);
2863 for (i = 0; i < tupdesc->
natts; i++)
2871 if (tupdesc->
attrs[i]->attisdropped)
2906 &typoutput, &typisvarlena);
2927 croak(
"SPI functions can not be used in END blocks");
3018 newSViv(processed));
3020 if (status > 0 && tuptable)
3027 av_extend(rows, processed);
3028 for (i = 0; i < processed; i++)
3062 prodesc = current_call_data->
prodesc;
3063 fcinfo = current_call_data->
fcinfo;
3068 (
errcode(ERRCODE_SYNTAX_ERROR),
3069 errmsg(
"cannot use return_next in a non-SETOF function")));
3106 if (!current_call_data->
tmp_cxt)
3110 "PL/Perl return_next temporary cxt",
3122 if (!(SvOK(sv) && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV))
3124 (
errcode(ERRCODE_DATATYPE_MISMATCH),
3125 errmsg(
"SETOF-composite-returning PL/Perl function "
3126 "must call return_next with reference to hash")));
3190 elog(
ERROR,
"SPI_cursor_open() failed:%s",
3365 "PL/Perl spi_prepare query",
3373 qdesc->
nargs = argc;
3384 "PL/Perl spi_prepare workspace",
3395 for (i = 0; i < argc; i++)
3534 if (hash_entry ==
NULL)
3535 elog(
ERROR,
"spi_exec_prepared: Invalid prepared query passed");
3539 elog(
ERROR,
"spi_exec_prepared: plperl query_hash value vanished");
3541 if (qdesc->
nargs != argc)
3542 elog(
ERROR,
"spi_exec_prepared: expected %d argument(s), %d passed",
3543 qdesc->
nargs, argc);
3552 if (sv && *sv && SvIOK(*sv))
3560 nulls = (
char *)
palloc(argc);
3569 for (i = 0; i < argc; i++)
3580 nulls[
i] = isnull ?
'n' :
' ';
3670 if (hash_entry ==
NULL)
3671 elog(
ERROR,
"spi_query_prepared: Invalid prepared query passed");
3675 elog(
ERROR,
"spi_query_prepared: plperl query_hash value vanished");
3677 if (qdesc->
nargs != argc)
3678 elog(
ERROR,
"spi_query_prepared: expected %d argument(s), %d passed",
3679 qdesc->
nargs, argc);
3686 nulls = (
char *)
palloc(argc);
3695 for (i = 0; i < argc; i++)
3706 nulls[
i] = isnull ?
'n' :
' ';
3720 elog(
ERROR,
"SPI_cursor_open() failed:%s",
3779 if (hash_entry ==
NULL)
3780 elog(
ERROR,
"spi_freeplan: Invalid prepared query passed");
3784 elog(
ERROR,
"spi_freeplan: plperl query_hash value vanished");
3820 hlen = -(int) strlen(hkey);
3821 ret = hv_store(hv, hkey, hlen, val, 0);
3845 hlen = -(int) strlen(hkey);
3846 ret = hv_fetch(hv, hkey, hlen, 0);
3860 char *procname = (
char *) arg;
3863 errcontext(
"PL/Perl function \"%s\"", procname);
3872 char *procname = (
char *) arg;
3875 errcontext(
"compilation of PL/Perl function \"%s\"", procname);
3894 setlocale_perl(
int category,
char *
locale)
3896 char *RETVAL = setlocale(category, locale);
3900 #ifdef USE_LOCALE_CTYPE
3901 if (category == LC_CTYPE
3903 || category == LC_ALL
3910 if (category == LC_ALL)
3911 newctype = setlocale(LC_CTYPE,
NULL);
3915 new_ctype(newctype);
3918 #ifdef USE_LOCALE_COLLATE
3919 if (category == LC_COLLATE
3921 || category == LC_ALL
3928 if (category == LC_ALL)
3929 newcoll = setlocale(LC_COLLATE,
NULL);
3933 new_collate(newcoll);
3937 #ifdef USE_LOCALE_NUMERIC
3938 if (category == LC_NUMERIC
3940 || category == LC_ALL
3947 if (category == LC_ALL)
3948 newnum = setlocale(LC_NUMERIC,
NULL);
3952 new_numeric(newnum);