1 /**********************************************************************
2 * plperl.c - perl as a procedural language for PostgreSQL
6 * This software is copyrighted by Mark Hollomon
7 * but is shamelessly cribbed from pltcl.c by Jan Wieck.
9 * The author hereby grants permission to use, copy, modify,
10 * distribute, and license this software and its documentation
11 * for any purpose, provided that existing copyright notices are
12 * retained in all copies and that this notice is included
13 * verbatim in any distributions. No written agreement, license,
14 * or royalty fee is required for any of the authorized uses.
15 * Modifications to this software may be copyrighted by their
16 * author and need not follow the licensing terms described
17 * here, provided that the new terms are clearly indicated on
18 * the first page of each file where they apply.
20 * IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY
21 * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR
22 * CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS
23 * SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN
24 * IF THE AUTHOR HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
27 * THE AUTHOR AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
30 * PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON
31 * AN "AS IS" BASIS, AND THE AUTHOR AND DISTRIBUTORS HAVE NO
32 * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
33 * ENHANCEMENTS, OR MODIFICATIONS.
36 * $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.103 2006/02/28 23:38:13 neilc Exp $
38 **********************************************************************/
50 /* postgreSQL stuff */
51 #include "commands/trigger.h"
52 #include "executor/spi.h"
54 #include "utils/lsyscache.h"
55 #include "utils/memutils.h"
56 #include "utils/typcache.h"
57 #include "miscadmin.h"
58 #include "mb/pg_wchar.h"
60 /* define this before the perl headers get a chance to mangle DLLIMPORT */
61 extern DLLIMPORT bool check_function_bodies;
66 /**********************************************************************
67 * The information we cache about loaded procedures
68 **********************************************************************/
69 typedef struct plperl_proc_desc
72 TransactionId fn_xmin;
76 bool fn_retistuple; /* true, if function returns tuple */
77 bool fn_retisset; /* true, if function returns set */
78 bool fn_retisarray; /* true if function returns array */
79 Oid result_oid; /* Oid of result type */
80 FmgrInfo result_in_func; /* I/O function and arg for result type */
81 Oid result_typioparam;
83 FmgrInfo arg_out_func[FUNC_MAX_ARGS];
84 bool arg_is_rowtype[FUNC_MAX_ARGS];
89 * The information we cache for the duration of a single call to a
92 typedef struct plperl_call_data
94 plperl_proc_desc *prodesc;
95 FunctionCallInfo fcinfo;
96 Tuplestorestate *tuple_store;
98 AttInMetadata *attinmeta;
99 MemoryContext tmp_cxt;
103 /**********************************************************************
105 **********************************************************************/
106 static bool plperl_firstcall = true;
107 static bool plperl_safe_init_done = false;
108 static PerlInterpreter *plperl_interp = NULL;
109 static HV *plperl_proc_hash = NULL;
111 static bool plperl_use_strict = false;
113 /* this is saved and restored by plperl_call_handler */
114 static plperl_call_data *current_call_data = NULL;
116 /**********************************************************************
117 * Forward declarations
118 **********************************************************************/
119 static void plperl_init_all(void);
120 static void plperl_init_interp(void);
122 Datum plperl_call_handler(PG_FUNCTION_ARGS);
123 Datum plperl_validator(PG_FUNCTION_ARGS);
124 void plperl_init(void);
126 static Datum plperl_func_handler(PG_FUNCTION_ARGS);
128 static Datum plperl_trigger_handler(PG_FUNCTION_ARGS);
129 static plperl_proc_desc *compile_plperl_function(Oid fn_oid, bool is_trigger);
131 static SV *plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc);
132 static void plperl_init_shared_libs(pTHX);
133 static HV *plperl_spi_execute_fetch_result(SPITupleTable *, int, int);
136 * This routine is a crock, and so is everyplace that calls it. The problem
137 * is that the cached form of plperl functions/queries is allocated permanently
138 * (mostly via malloc()) and never released until backend exit. Subsidiary
139 * data structures such as fmgr info records therefore must live forever
140 * as well. A better implementation would store all this stuff in a per-
141 * function memory context that could be reclaimed at need. In the meantime,
142 * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
143 * it might allocate, and whatever the eventual function might allocate using
144 * fn_mcxt, will live forever too.
147 perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
149 fmgr_info_cxt(functionId, finfo, TopMemoryContext);
153 /* Perform initialization during postmaster startup. */
158 if (!plperl_firstcall)
161 DefineCustomBoolVariable(
163 "If true, will compile trusted and untrusted perl code in strict mode",
169 EmitWarningsOnPlaceholders("plperl");
171 plperl_init_interp();
172 plperl_firstcall = false;
176 /* Perform initialization during backend startup. */
179 plperl_init_all(void)
181 if (plperl_firstcall)
184 /* We don't need to do anything yet when a new backend starts. */
187 /* Each of these macros must represent a single string literal */
190 "SPI::bootstrap(); use vars qw(%_SHARED);" \
191 "sub ::plperl_warn { my $msg = shift; " \
192 " $msg =~ s/\\(eval \\d+\\) //g; &elog(&NOTICE, $msg); } " \
193 "$SIG{__WARN__} = \\&::plperl_warn; " \
194 "sub ::plperl_die { my $msg = shift; " \
195 " $msg =~ s/\\(eval \\d+\\) //g; die $msg; } " \
196 "$SIG{__DIE__} = \\&::plperl_die; " \
197 "sub ::mkunsafefunc {" \
198 " my $ret = eval(qq[ sub { $_[0] $_[1] } ]); " \
199 " $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; }" \
201 "sub ::mk_strict_unsafefunc {" \
202 " my $ret = eval(qq[ sub { use strict; $_[0] $_[1] } ]); " \
203 " $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; } " \
204 "sub ::_plperl_to_pg_array {" \
205 " my $arg = shift; ref $arg eq 'ARRAY' || return $arg; " \
206 " my $res = ''; my $first = 1; " \
207 " foreach my $elem (@$arg) " \
209 " $res .= ', ' unless $first; $first = undef; " \
212 " $res .= _plperl_to_pg_array($elem); " \
214 " elsif (defined($elem)) " \
216 " my $str = qq($elem); " \
217 " $str =~ s/([\"\\\\])/\\\\$1/g; " \
218 " $res .= qq(\"$str\"); " \
222 " $res .= 'NULL' ; " \
225 " return qq({$res}); " \
228 #define SAFE_MODULE \
229 "require Safe; $Safe::VERSION"
232 "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');" \
233 "$PLContainer->permit_only(':default');" \
234 "$PLContainer->permit(qw[:base_math !:base_io sort time]);" \
235 "$PLContainer->share(qw[&elog &spi_exec_query &return_next " \
236 "&spi_query &spi_fetchrow " \
237 "&_plperl_to_pg_array " \
238 "&DEBUG &LOG &INFO &NOTICE &WARNING &ERROR %_SHARED ]);" \
239 "sub ::mksafefunc {" \
240 " my $ret = $PLContainer->reval(qq[sub { $_[0] $_[1] }]); " \
241 " $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; }" \
242 "$PLContainer->permit('require'); $PLContainer->reval('use strict;');" \
243 "$PLContainer->deny('require');" \
244 "sub ::mk_strict_safefunc {" \
245 " my $ret = $PLContainer->reval(qq[sub { BEGIN { strict->import(); } $_[0] $_[1] }]); " \
246 " $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; }"
249 "use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');" \
250 "$PLContainer->permit_only(':default');" \
251 "$PLContainer->share(qw[&elog &ERROR ]);" \
252 "sub ::mksafefunc { return $PLContainer->reval(qq[sub { " \
253 " elog(ERROR,'trusted Perl functions disabled - " \
254 " please upgrade Perl Safe module to version 2.09 or later');}]); }" \
255 "sub ::mk_strict_safefunc { return $PLContainer->reval(qq[sub { " \
256 " elog(ERROR,'trusted Perl functions disabled - " \
257 " please upgrade Perl Safe module to version 2.09 or later');}]); }"
261 plperl_init_interp(void)
263 static char *embedding[3] = {
270 * The perl library on startup does horrible things like call
271 * setlocale(LC_ALL,""). We have protected against that on most
272 * platforms by setting the environment appropriately. However, on
273 * Windows, setlocale() does not consult the environment, so we need
274 * to save the existing locale settings before perl has a chance to
275 * mangle them and restore them after its dirty deeds are done.
278 * http://msdn.microsoft.com/library/en-us/vclib/html/_crt_locale.asp
280 * It appears that we only need to do this on interpreter startup, and
281 * subsequent calls to the interpreter don't mess with the locale
284 * We restore them using Perl's POSIX::setlocale() function so that
285 * Perl doesn't have a different idea of the locale from Postgres.
290 char *save_collate, *save_ctype, *save_monetary, *save_numeric, *save_time;
293 loc = setlocale(LC_COLLATE,NULL);
294 save_collate = loc ? pstrdup(loc) : NULL;
295 loc = setlocale(LC_CTYPE,NULL);
296 save_ctype = loc ? pstrdup(loc) : NULL;
297 loc = setlocale(LC_MONETARY,NULL);
298 save_monetary = loc ? pstrdup(loc) : NULL;
299 loc = setlocale(LC_NUMERIC,NULL);
300 save_numeric = loc ? pstrdup(loc) : NULL;
301 loc = setlocale(LC_TIME,NULL);
302 save_time = loc ? pstrdup(loc) : NULL;
306 plperl_interp = perl_alloc();
308 elog(ERROR, "could not allocate Perl interpreter");
310 perl_construct(plperl_interp);
311 perl_parse(plperl_interp, plperl_init_shared_libs, 3, embedding, NULL);
312 perl_run(plperl_interp);
314 plperl_proc_hash = newHV();
318 eval_pv("use POSIX qw(locale_h);", TRUE); /* croak on failure */
320 if (save_collate != NULL)
322 snprintf(buf, sizeof(buf),"setlocale(%s,'%s');",
323 "LC_COLLATE",save_collate);
327 if (save_ctype != NULL)
329 snprintf(buf, sizeof(buf),"setlocale(%s,'%s');",
330 "LC_CTYPE",save_ctype);
334 if (save_monetary != NULL)
336 snprintf(buf, sizeof(buf),"setlocale(%s,'%s');",
337 "LC_MONETARY",save_monetary);
339 pfree(save_monetary);
341 if (save_numeric != NULL)
343 snprintf(buf, sizeof(buf),"setlocale(%s,'%s');",
344 "LC_NUMERIC",save_numeric);
348 if (save_time != NULL)
350 snprintf(buf, sizeof(buf),"setlocale(%s,'%s');",
351 "LC_TIME",save_time);
362 plperl_safe_init(void)
367 res = eval_pv(SAFE_MODULE, FALSE); /* TRUE = croak if failure */
369 safe_version = SvNV(res);
372 * We actually want to reject safe_version < 2.09, but it's risky to
373 * assume that floating-point comparisons are exact, so use a slightly
374 * smaller comparison value.
376 if (safe_version < 2.0899)
378 /* not safe, so disallow all trusted funcs */
379 eval_pv(SAFE_BAD, FALSE);
383 eval_pv(SAFE_OK, FALSE);
386 plperl_safe_init_done = true;
390 * Perl likes to put a newline after its error messages; clean up such
393 strip_trailing_ws(const char *msg)
395 char *res = pstrdup(msg);
396 int len = strlen(res);
398 while (len > 0 && isspace((unsigned char) res[len - 1]))
404 /* Build a tuple from a hash. */
407 plperl_build_tuple_result(HV *perlhash, AttInMetadata *attinmeta)
409 TupleDesc td = attinmeta->tupdesc;
416 values = (char **) palloc0(td->natts * sizeof(char *));
418 hv_iterinit(perlhash);
419 while ((val = hv_iternextsv(perlhash, &key, &klen)))
421 int attn = SPI_fnumber(td, key);
423 if (attn <= 0 || td->attrs[attn - 1]->attisdropped)
425 (errcode(ERRCODE_UNDEFINED_COLUMN),
426 errmsg("Perl hash contains nonexistent column \"%s\"",
428 if (SvOK(val) && SvTYPE(val) != SVt_NULL)
429 values[attn - 1] = SvPV(val, PL_na);
431 hv_iterinit(perlhash);
433 tup = BuildTupleFromCStrings(attinmeta, values);
439 * convert perl array to postgres string representation
442 plperl_convert_to_pg_array(SV *src)
453 count = call_pv("::_plperl_to_pg_array", G_SCALAR);
458 elog(ERROR, "unexpected _plperl_to_pg_array failure");
468 /* Set up the arguments for a trigger call. */
471 plperl_trigger_build_args(FunctionCallInfo fcinfo)
484 tdata = (TriggerData *) fcinfo->context;
485 tupdesc = tdata->tg_relation->rd_att;
487 relid = DatumGetCString(
488 DirectFunctionCall1(oidout,
489 ObjectIdGetDatum(tdata->tg_relation->rd_id)
493 hv_store(hv, "name", 4, newSVpv(tdata->tg_trigger->tgname, 0), 0);
494 hv_store(hv, "relid", 5, newSVpv(relid, 0), 0);
496 if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
499 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
500 hv_store(hv, "new", 3,
501 plperl_hash_from_tuple(tdata->tg_trigtuple, tupdesc),
504 else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
507 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
508 hv_store(hv, "old", 3,
509 plperl_hash_from_tuple(tdata->tg_trigtuple, tupdesc),
512 else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
515 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
517 hv_store(hv, "old", 3,
518 plperl_hash_from_tuple(tdata->tg_trigtuple, tupdesc),
520 hv_store(hv, "new", 3,
521 plperl_hash_from_tuple(tdata->tg_newtuple, tupdesc),
528 hv_store(hv, "event", 5, newSVpv(event, 0), 0);
529 hv_store(hv, "argc", 4, newSViv(tdata->tg_trigger->tgnargs), 0);
531 if (tdata->tg_trigger->tgnargs > 0)
535 for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
536 av_push(av, newSVpv(tdata->tg_trigger->tgargs[i], 0));
537 hv_store(hv, "args", 4, newRV_noinc((SV *) av), 0);
540 hv_store(hv, "relname", 7,
541 newSVpv(SPI_getrelname(tdata->tg_relation), 0), 0);
543 if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
545 else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
549 hv_store(hv, "when", 4, newSVpv(when, 0), 0);
551 if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
553 else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
557 hv_store(hv, "level", 5, newSVpv(level, 0), 0);
559 return newRV_noinc((SV *) hv);
563 /* Set up the new tuple returned from a trigger. */
566 plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup)
581 tupdesc = tdata->tg_relation->rd_att;
583 svp = hv_fetch(hvTD, "new", 3, FALSE);
586 (errcode(ERRCODE_UNDEFINED_COLUMN),
587 errmsg("$_TD->{new} does not exist")));
588 if (!SvOK(*svp) || SvTYPE(*svp) != SVt_RV || SvTYPE(SvRV(*svp)) != SVt_PVHV)
590 (errcode(ERRCODE_DATATYPE_MISMATCH),
591 errmsg("$_TD->{new} is not a hash reference")));
592 hvNew = (HV *) SvRV(*svp);
594 modattrs = palloc(tupdesc->natts * sizeof(int));
595 modvalues = palloc(tupdesc->natts * sizeof(Datum));
596 modnulls = palloc(tupdesc->natts * sizeof(char));
600 while ((val = hv_iternextsv(hvNew, &key, &klen)))
602 int attn = SPI_fnumber(tupdesc, key);
604 if (attn <= 0 || tupdesc->attrs[attn - 1]->attisdropped)
606 (errcode(ERRCODE_UNDEFINED_COLUMN),
607 errmsg("Perl hash contains nonexistent column \"%s\"",
609 if (SvOK(val) && SvTYPE(val) != SVt_NULL)
615 /* XXX would be better to cache these lookups */
616 getTypeInputInfo(tupdesc->attrs[attn - 1]->atttypid,
617 &typinput, &typioparam);
618 fmgr_info(typinput, &finfo);
619 modvalues[slotsused] = FunctionCall3(&finfo,
620 CStringGetDatum(SvPV(val, PL_na)),
621 ObjectIdGetDatum(typioparam),
622 Int32GetDatum(tupdesc->attrs[attn - 1]->atttypmod));
623 modnulls[slotsused] = ' ';
627 modvalues[slotsused] = (Datum) 0;
628 modnulls[slotsused] = 'n';
630 modattrs[slotsused] = attn;
635 rtup = SPI_modifytuple(tdata->tg_relation, otup, slotsused,
636 modattrs, modvalues, modnulls);
643 elog(ERROR, "SPI_modifytuple failed: %s",
644 SPI_result_code_string(SPI_result));
651 * This is the only externally-visible part of the plperl call interface.
652 * The Postgres function and trigger managers call it to execute a
655 PG_FUNCTION_INFO_V1(plperl_call_handler);
658 plperl_call_handler(PG_FUNCTION_ARGS)
661 plperl_call_data *save_call_data;
665 save_call_data = current_call_data;
668 if (CALLED_AS_TRIGGER(fcinfo))
669 retval = PointerGetDatum(plperl_trigger_handler(fcinfo));
671 retval = plperl_func_handler(fcinfo);
675 current_call_data = save_call_data;
680 current_call_data = save_call_data;
685 * This is the other externally visible function - it is called when CREATE
686 * FUNCTION is issued to validate the function being created/replaced.
688 PG_FUNCTION_INFO_V1(plperl_validator);
691 plperl_validator(PG_FUNCTION_ARGS)
693 Oid funcoid = PG_GETARG_OID(0);
701 bool istrigger = false;
704 /* Get the new function's pg_proc entry */
705 tuple = SearchSysCache(PROCOID,
706 ObjectIdGetDatum(funcoid),
708 if (!HeapTupleIsValid(tuple))
709 elog(ERROR, "cache lookup failed for function %u", funcoid);
710 proc = (Form_pg_proc) GETSTRUCT(tuple);
712 functyptype = get_typtype(proc->prorettype);
714 /* Disallow pseudotype result */
715 /* except for TRIGGER, RECORD, or VOID */
716 if (functyptype == 'p')
718 /* we assume OPAQUE with no arguments means a trigger */
719 if (proc->prorettype == TRIGGEROID ||
720 (proc->prorettype == OPAQUEOID && proc->pronargs == 0))
722 else if (proc->prorettype != RECORDOID &&
723 proc->prorettype != VOIDOID)
725 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
726 errmsg("plperl functions cannot return type %s",
727 format_type_be(proc->prorettype))));
730 /* Disallow pseudotypes in arguments (either IN or OUT) */
731 numargs = get_func_arg_info(tuple,
732 &argtypes, &argnames, &argmodes);
733 for (i = 0; i < numargs; i++)
735 if (get_typtype(argtypes[i]) == 'p')
737 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
738 errmsg("plperl functions cannot take type %s",
739 format_type_be(argtypes[i]))));
742 ReleaseSysCache(tuple);
744 /* Postpone body checks if !check_function_bodies */
745 if (check_function_bodies)
747 plperl_proc_desc *prodesc;
751 prodesc = compile_plperl_function(funcoid, istrigger);
754 /* the result of a validator is ignored */
759 /* Uses mksafefunc/mkunsafefunc to create an anonymous sub whose text is
760 * supplied in s, and returns a reference to the closure. */
763 plperl_create_sub(char *s, bool trusted)
770 if (trusted && !plperl_safe_init_done)
779 XPUSHs(sv_2mortal(newSVpv("my $_TD=$_[0]; shift;", 0)));
780 XPUSHs(sv_2mortal(newSVpv(s, 0)));
784 * G_KEEPERR seems to be needed here, else we don't recognize compile
785 * errors properly. Perhaps it's because there's another level of eval
789 if (trusted && plperl_use_strict)
790 compile_sub = "::mk_strict_safefunc";
791 else if (plperl_use_strict)
792 compile_sub = "::mk_strict_unsafefunc";
794 compile_sub = "::mksafefunc";
796 compile_sub = "::mkunsafefunc";
798 count = perl_call_pv(compile_sub, G_SCALAR | G_EVAL | G_KEEPERR);
806 elog(ERROR, "didn't get a return item from mksafefunc");
816 (errcode(ERRCODE_SYNTAX_ERROR),
817 errmsg("creation of Perl function failed: %s",
818 strip_trailing_ws(SvPV(ERRSV, PL_na)))));
822 * need to make a deep copy of the return. it comes off the stack as a
825 subref = newSVsv(POPs);
827 if (!SvROK(subref) || SvTYPE(SvRV(subref)) != SVt_PVCV)
834 * subref is our responsibility because it is not mortal
836 SvREFCNT_dec(subref);
837 elog(ERROR, "didn't get a code ref");
848 /**********************************************************************
849 * plperl_init_shared_libs() -
851 * We cannot use the DynaLoader directly to get at the Opcode
852 * module (used by Safe.pm). So, we link Opcode into ourselves
853 * and do the initialization behind perl's back.
855 **********************************************************************/
857 EXTERN_C void boot_DynaLoader(pTHX_ CV *cv);
858 EXTERN_C void boot_SPI(pTHX_ CV *cv);
861 plperl_init_shared_libs(pTHX)
863 char *file = __FILE__;
865 newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
866 newXS("SPI::bootstrap", boot_SPI, file);
871 plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
884 XPUSHs(&PL_sv_undef); /* no trigger data */
886 for (i = 0; i < desc->nargs; i++)
888 if (fcinfo->argnull[i])
889 XPUSHs(&PL_sv_undef);
890 else if (desc->arg_is_rowtype[i])
896 HeapTupleData tmptup;
899 td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
900 /* Extract rowtype info and find a tupdesc */
901 tupType = HeapTupleHeaderGetTypeId(td);
902 tupTypmod = HeapTupleHeaderGetTypMod(td);
903 tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
904 /* Build a temporary HeapTuple control structure */
905 tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
908 hashref = plperl_hash_from_tuple(&tmptup, tupdesc);
909 XPUSHs(sv_2mortal(hashref));
915 tmp = DatumGetCString(FunctionCall1(&(desc->arg_out_func[i]),
917 sv = newSVpv(tmp, 0);
918 #if PERL_BCDVERSION >= 0x5006000L
919 if (GetDatabaseEncoding() == PG_UTF8)
922 XPUSHs(sv_2mortal(sv));
928 /* Do NOT use G_KEEPERR here */
929 count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
938 elog(ERROR, "didn't get a return item from function");
947 /* XXX need to find a way to assign an errcode here */
949 (errmsg("error from Perl function: %s",
950 strip_trailing_ws(SvPV(ERRSV, PL_na)))));
953 retval = newSVsv(POPs);
964 plperl_call_perl_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo,
980 tg_trigger = ((TriggerData *) fcinfo->context)->tg_trigger;
981 for (i = 0; i < tg_trigger->tgnargs; i++)
982 XPUSHs(sv_2mortal(newSVpv(tg_trigger->tgargs[i], 0)));
985 /* Do NOT use G_KEEPERR here */
986 count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
995 elog(ERROR, "didn't get a return item from trigger function");
1004 /* XXX need to find a way to assign an errcode here */
1006 (errmsg("error from Perl trigger function: %s",
1007 strip_trailing_ws(SvPV(ERRSV, PL_na)))));
1010 retval = newSVsv(POPs);
1021 plperl_func_handler(PG_FUNCTION_ARGS)
1023 plperl_proc_desc *prodesc;
1027 SV *array_ret = NULL;
1030 * Create the call_data beforing connecting to SPI, so that it is
1031 * not allocated in the SPI memory context
1033 current_call_data = (plperl_call_data *) palloc0(sizeof(plperl_call_data));
1034 current_call_data->fcinfo = fcinfo;
1036 if (SPI_connect() != SPI_OK_CONNECT)
1037 elog(ERROR, "could not connect to SPI manager");
1039 prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false);
1040 current_call_data->prodesc = prodesc;
1042 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1044 if (prodesc->fn_retisset)
1046 /* Check context before allowing the call to go through */
1047 if (!rsi || !IsA(rsi, ReturnSetInfo) ||
1048 (rsi->allowedModes & SFRM_Materialize) == 0 ||
1049 rsi->expectedDesc == NULL)
1051 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1052 errmsg("set-valued function called in context that "
1053 "cannot accept a set")));
1056 perlret = plperl_call_perl_func(prodesc, fcinfo);
1058 /************************************************************
1059 * Disconnect from SPI manager and then create the return
1060 * values datum (if the input function does a palloc for it
1061 * this must not be allocated in the SPI memory context
1062 * because SPI_finish would free it).
1063 ************************************************************/
1064 if (SPI_finish() != SPI_OK_FINISH)
1065 elog(ERROR, "SPI_finish() failed");
1067 if (prodesc->fn_retisset)
1070 * If the Perl function returned an arrayref, we pretend that it
1071 * called return_next() for each element of the array, to handle old
1072 * SRFs that didn't know about return_next(). Any other sort of return
1073 * value is an error.
1075 if (SvTYPE(perlret) == SVt_RV &&
1076 SvTYPE(SvRV(perlret)) == SVt_PVAV)
1080 AV *rav = (AV *) SvRV(perlret);
1082 while ((svp = av_fetch(rav, i, FALSE)) != NULL)
1084 plperl_return_next(*svp);
1088 else if (SvTYPE(perlret) != SVt_NULL)
1091 (errcode(ERRCODE_DATATYPE_MISMATCH),
1092 errmsg("set-returning Perl function must return "
1093 "reference to array or use return_next")));
1096 rsi->returnMode = SFRM_Materialize;
1097 if (current_call_data->tuple_store)
1099 rsi->setResult = current_call_data->tuple_store;
1100 rsi->setDesc = current_call_data->ret_tdesc;
1104 else if (SvTYPE(perlret) == SVt_NULL)
1106 /* Return NULL if Perl code returned undef */
1107 if (rsi && IsA(rsi, ReturnSetInfo))
1108 rsi->isDone = ExprEndResult;
1109 fcinfo->isnull = true;
1112 else if (prodesc->fn_retistuple)
1114 /* Return a perl hash converted to a Datum */
1116 AttInMetadata *attinmeta;
1119 if (!SvOK(perlret) || SvTYPE(perlret) != SVt_RV ||
1120 SvTYPE(SvRV(perlret)) != SVt_PVHV)
1123 (errcode(ERRCODE_DATATYPE_MISMATCH),
1124 errmsg("composite-returning Perl function "
1125 "must return reference to hash")));
1128 /* XXX should cache the attinmeta data instead of recomputing */
1129 if (get_call_result_type(fcinfo, NULL, &td) != TYPEFUNC_COMPOSITE)
1132 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1133 errmsg("function returning record called in context "
1134 "that cannot accept type record")));
1137 attinmeta = TupleDescGetAttInMetadata(td);
1138 tup = plperl_build_tuple_result((HV *) SvRV(perlret), attinmeta);
1139 retval = HeapTupleGetDatum(tup);
1143 /* Return a perl string converted to a Datum */
1146 if (prodesc->fn_retisarray && SvROK(perlret) &&
1147 SvTYPE(SvRV(perlret)) == SVt_PVAV)
1149 array_ret = plperl_convert_to_pg_array(perlret);
1150 SvREFCNT_dec(perlret);
1151 perlret = array_ret;
1154 val = SvPV(perlret, PL_na);
1156 retval = FunctionCall3(&prodesc->result_in_func,
1157 CStringGetDatum(val),
1158 ObjectIdGetDatum(prodesc->result_typioparam),
1162 if (array_ret == NULL)
1163 SvREFCNT_dec(perlret);
1165 current_call_data = NULL;
1171 plperl_trigger_handler(PG_FUNCTION_ARGS)
1173 plperl_proc_desc *prodesc;
1180 * Create the call_data beforing connecting to SPI, so that it is
1181 * not allocated in the SPI memory context
1183 current_call_data = (plperl_call_data *) palloc0(sizeof(plperl_call_data));
1184 current_call_data->fcinfo = fcinfo;
1186 /* Connect to SPI manager */
1187 if (SPI_connect() != SPI_OK_CONNECT)
1188 elog(ERROR, "could not connect to SPI manager");
1190 /* Find or compile the function */
1191 prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, true);
1192 current_call_data->prodesc = prodesc;
1194 svTD = plperl_trigger_build_args(fcinfo);
1195 perlret = plperl_call_perl_trigger_func(prodesc, fcinfo, svTD);
1196 hvTD = (HV *) SvRV(svTD);
1198 /************************************************************
1199 * Disconnect from SPI manager and then create the return
1200 * values datum (if the input function does a palloc for it
1201 * this must not be allocated in the SPI memory context
1202 * because SPI_finish would free it).
1203 ************************************************************/
1204 if (SPI_finish() != SPI_OK_FINISH)
1205 elog(ERROR, "SPI_finish() failed");
1207 if (!(perlret && SvOK(perlret) && SvTYPE(perlret) != SVt_NULL))
1209 /* undef result means go ahead with original tuple */
1210 TriggerData *trigdata = ((TriggerData *) fcinfo->context);
1212 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1213 retval = (Datum) trigdata->tg_trigtuple;
1214 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1215 retval = (Datum) trigdata->tg_newtuple;
1216 else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
1217 retval = (Datum) trigdata->tg_trigtuple;
1219 retval = (Datum) 0; /* can this happen? */
1226 tmp = SvPV(perlret, PL_na);
1228 if (pg_strcasecmp(tmp, "SKIP") == 0)
1230 else if (pg_strcasecmp(tmp, "MODIFY") == 0)
1232 TriggerData *trigdata = (TriggerData *) fcinfo->context;
1234 if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
1235 trv = plperl_modify_tuple(hvTD, trigdata,
1236 trigdata->tg_trigtuple);
1237 else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
1238 trv = plperl_modify_tuple(hvTD, trigdata,
1239 trigdata->tg_newtuple);
1243 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1244 errmsg("ignoring modified tuple in DELETE trigger")));
1251 (errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1252 errmsg("result of Perl trigger function must be undef, "
1253 "\"SKIP\" or \"MODIFY\"")));
1256 retval = PointerGetDatum(trv);
1261 SvREFCNT_dec(perlret);
1263 current_call_data = NULL;
1268 static plperl_proc_desc *
1269 compile_plperl_function(Oid fn_oid, bool is_trigger)
1272 Form_pg_proc procStruct;
1273 char internal_proname[64];
1275 plperl_proc_desc *prodesc = NULL;
1279 /* We'll need the pg_proc tuple in any case... */
1280 procTup = SearchSysCache(PROCOID,
1281 ObjectIdGetDatum(fn_oid),
1283 if (!HeapTupleIsValid(procTup))
1284 elog(ERROR, "cache lookup failed for function %u", fn_oid);
1285 procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1287 /************************************************************
1288 * Build our internal proc name from the function's Oid
1289 ************************************************************/
1291 sprintf(internal_proname, "__PLPerl_proc_%u", fn_oid);
1293 sprintf(internal_proname, "__PLPerl_proc_%u_trigger", fn_oid);
1295 proname_len = strlen(internal_proname);
1297 /************************************************************
1298 * Lookup the internal proc name in the hashtable
1299 ************************************************************/
1300 svp = hv_fetch(plperl_proc_hash, internal_proname, proname_len, FALSE);
1305 prodesc = (plperl_proc_desc *) SvIV(*svp);
1307 /************************************************************
1308 * If it's present, must check whether it's still up to date.
1309 * This is needed because CREATE OR REPLACE FUNCTION can modify the
1310 * function's pg_proc entry without changing its OID.
1311 ************************************************************/
1312 uptodate = (prodesc->fn_xmin == HeapTupleHeaderGetXmin(procTup->t_data) &&
1313 prodesc->fn_cmin == HeapTupleHeaderGetCmin(procTup->t_data));
1317 /* need we delete old entry? */
1322 /************************************************************
1323 * If we haven't found it in the hashtable, we analyze
1324 * the function's arguments and return type and store
1325 * the in-/out-functions in the prodesc block and create
1326 * a new hashtable entry for it.
1328 * Then we load the procedure into the Perl interpreter.
1329 ************************************************************/
1330 if (prodesc == NULL)
1334 Form_pg_language langStruct;
1335 Form_pg_type typeStruct;
1340 /************************************************************
1341 * Allocate a new procedure description block
1342 ************************************************************/
1343 prodesc = (plperl_proc_desc *) malloc(sizeof(plperl_proc_desc));
1344 if (prodesc == NULL)
1346 (errcode(ERRCODE_OUT_OF_MEMORY),
1347 errmsg("out of memory")));
1348 MemSet(prodesc, 0, sizeof(plperl_proc_desc));
1349 prodesc->proname = strdup(internal_proname);
1350 prodesc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1351 prodesc->fn_cmin = HeapTupleHeaderGetCmin(procTup->t_data);
1353 /* Remember if function is STABLE/IMMUTABLE */
1354 prodesc->fn_readonly =
1355 (procStruct->provolatile != PROVOLATILE_VOLATILE);
1357 /************************************************************
1358 * Lookup the pg_language tuple by Oid
1359 ************************************************************/
1360 langTup = SearchSysCache(LANGOID,
1361 ObjectIdGetDatum(procStruct->prolang),
1363 if (!HeapTupleIsValid(langTup))
1365 free(prodesc->proname);
1367 elog(ERROR, "cache lookup failed for language %u",
1368 procStruct->prolang);
1370 langStruct = (Form_pg_language) GETSTRUCT(langTup);
1371 prodesc->lanpltrusted = langStruct->lanpltrusted;
1372 ReleaseSysCache(langTup);
1374 /************************************************************
1375 * Get the required information for input conversion of the
1377 ************************************************************/
1380 typeTup = SearchSysCache(TYPEOID,
1381 ObjectIdGetDatum(procStruct->prorettype),
1383 if (!HeapTupleIsValid(typeTup))
1385 free(prodesc->proname);
1387 elog(ERROR, "cache lookup failed for type %u",
1388 procStruct->prorettype);
1390 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1392 /* Disallow pseudotype result, except VOID or RECORD */
1393 if (typeStruct->typtype == 'p')
1395 if (procStruct->prorettype == VOIDOID ||
1396 procStruct->prorettype == RECORDOID)
1398 else if (procStruct->prorettype == TRIGGEROID)
1400 free(prodesc->proname);
1403 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1404 errmsg("trigger functions may only be called "
1409 free(prodesc->proname);
1412 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1413 errmsg("plperl functions cannot return type %s",
1414 format_type_be(procStruct->prorettype))));
1418 prodesc->result_oid = procStruct->prorettype;
1419 prodesc->fn_retisset = procStruct->proretset;
1420 prodesc->fn_retistuple = (typeStruct->typtype == 'c' ||
1421 procStruct->prorettype == RECORDOID);
1423 prodesc->fn_retisarray =
1424 (typeStruct->typlen == -1 && typeStruct->typelem);
1426 perm_fmgr_info(typeStruct->typinput, &(prodesc->result_in_func));
1427 prodesc->result_typioparam = getTypeIOParam(typeTup);
1429 ReleaseSysCache(typeTup);
1432 /************************************************************
1433 * Get the required information for output conversion
1434 * of all procedure arguments
1435 ************************************************************/
1438 prodesc->nargs = procStruct->pronargs;
1439 for (i = 0; i < prodesc->nargs; i++)
1441 typeTup = SearchSysCache(TYPEOID,
1442 ObjectIdGetDatum(procStruct->proargtypes.values[i]),
1444 if (!HeapTupleIsValid(typeTup))
1446 free(prodesc->proname);
1448 elog(ERROR, "cache lookup failed for type %u",
1449 procStruct->proargtypes.values[i]);
1451 typeStruct = (Form_pg_type) GETSTRUCT(typeTup);
1453 /* Disallow pseudotype argument */
1454 if (typeStruct->typtype == 'p')
1456 free(prodesc->proname);
1459 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1460 errmsg("plperl functions cannot take type %s",
1461 format_type_be(procStruct->proargtypes.values[i]))));
1464 if (typeStruct->typtype == 'c')
1465 prodesc->arg_is_rowtype[i] = true;
1468 prodesc->arg_is_rowtype[i] = false;
1469 perm_fmgr_info(typeStruct->typoutput,
1470 &(prodesc->arg_out_func[i]));
1473 ReleaseSysCache(typeTup);
1477 /************************************************************
1478 * create the text of the anonymous subroutine.
1479 * we do not use a named subroutine so that we can call directly
1480 * through the reference.
1481 ************************************************************/
1482 prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
1483 Anum_pg_proc_prosrc, &isnull);
1485 elog(ERROR, "null prosrc");
1486 proc_source = DatumGetCString(DirectFunctionCall1(textout,
1489 /************************************************************
1490 * Create the procedure in the interpreter
1491 ************************************************************/
1492 prodesc->reference = plperl_create_sub(proc_source, prodesc->lanpltrusted);
1494 if (!prodesc->reference) /* can this happen? */
1496 free(prodesc->proname);
1498 elog(ERROR, "could not create internal procedure \"%s\"",
1502 hv_store(plperl_proc_hash, internal_proname, proname_len,
1503 newSViv((IV) prodesc), 0);
1506 ReleaseSysCache(procTup);
1512 /* Build a hash from all attributes of a given tuple. */
1515 plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
1522 for (i = 0; i < tupdesc->natts; i++)
1533 if (tupdesc->attrs[i]->attisdropped)
1536 attname = NameStr(tupdesc->attrs[i]->attname);
1537 namelen = strlen(attname);
1538 attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);
1542 /* Store (attname => undef) and move on. */
1543 hv_store(hv, attname, namelen, newSV(0), 0);
1547 /* XXX should have a way to cache these lookups */
1549 getTypeOutputInfo(tupdesc->attrs[i]->atttypid,
1550 &typoutput, &typisvarlena);
1552 outputstr = DatumGetCString(OidFunctionCall1(typoutput, attr));
1554 sv = newSVpv(outputstr, 0);
1555 #if PERL_BCDVERSION >= 0x5006000L
1556 if (GetDatabaseEncoding() == PG_UTF8)
1559 hv_store(hv, attname, namelen, sv, 0);
1564 return newRV_noinc((SV *) hv);
1569 plperl_spi_exec(char *query, int limit)
1574 * Execute the query inside a sub-transaction, so we can cope with errors
1577 MemoryContext oldcontext = CurrentMemoryContext;
1578 ResourceOwner oldowner = CurrentResourceOwner;
1580 BeginInternalSubTransaction(NULL);
1581 /* Want to run inside function's memory context */
1582 MemoryContextSwitchTo(oldcontext);
1588 spi_rv = SPI_execute(query, current_call_data->prodesc->fn_readonly,
1590 ret_hv = plperl_spi_execute_fetch_result(SPI_tuptable, SPI_processed,
1593 /* Commit the inner transaction, return to outer xact context */
1594 ReleaseCurrentSubTransaction();
1595 MemoryContextSwitchTo(oldcontext);
1596 CurrentResourceOwner = oldowner;
1599 * AtEOSubXact_SPI() should not have popped any SPI context, but just
1600 * in case it did, make sure we remain connected.
1602 SPI_restore_connection();
1608 /* Save error info */
1609 MemoryContextSwitchTo(oldcontext);
1610 edata = CopyErrorData();
1613 /* Abort the inner transaction */
1614 RollbackAndReleaseCurrentSubTransaction();
1615 MemoryContextSwitchTo(oldcontext);
1616 CurrentResourceOwner = oldowner;
1619 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
1620 * have left us in a disconnected state. We need this hack to return
1621 * to connected state.
1623 SPI_restore_connection();
1625 /* Punt the error to Perl */
1626 croak("%s", edata->message);
1628 /* Can't get here, but keep compiler quiet */
1638 plperl_spi_execute_fetch_result(SPITupleTable *tuptable, int processed,
1645 hv_store(result, "status", strlen("status"),
1646 newSVpv((char *) SPI_result_code_string(status), 0), 0);
1647 hv_store(result, "processed", strlen("processed"),
1648 newSViv(processed), 0);
1650 if (status == SPI_OK_SELECT)
1657 for (i = 0; i < processed; i++)
1659 row = plperl_hash_from_tuple(tuptable->vals[i], tuptable->tupdesc);
1662 hv_store(result, "rows", strlen("rows"),
1663 newRV_noinc((SV *) rows), 0);
1666 SPI_freetuptable(tuptable);
1673 * Note: plperl_return_next is called both in Postgres and Perl contexts.
1674 * We report any errors in Postgres fashion (via ereport). If called in
1675 * Perl context, it is SPI.xs's responsibility to catch the error and
1676 * convert to a Perl error. We assume (perhaps without adequate justification)
1677 * that we need not abort the current transaction if the Perl code traps the
1681 plperl_return_next(SV *sv)
1683 plperl_proc_desc *prodesc;
1684 FunctionCallInfo fcinfo;
1686 MemoryContext old_cxt;
1692 prodesc = current_call_data->prodesc;
1693 fcinfo = current_call_data->fcinfo;
1694 rsi = (ReturnSetInfo *) fcinfo->resultinfo;
1696 if (!prodesc->fn_retisset)
1698 (errcode(ERRCODE_SYNTAX_ERROR),
1699 errmsg("cannot use return_next in a non-SETOF function")));
1701 if (prodesc->fn_retistuple &&
1702 !(SvOK(sv) && SvTYPE(sv) == SVt_RV && SvTYPE(SvRV(sv)) == SVt_PVHV))
1704 (errcode(ERRCODE_DATATYPE_MISMATCH),
1705 errmsg("setof-composite-returning Perl function "
1706 "must call return_next with reference to hash")));
1708 if (!current_call_data->ret_tdesc)
1712 Assert(!current_call_data->tuple_store);
1713 Assert(!current_call_data->attinmeta);
1716 * This is the first call to return_next in the current
1717 * PL/Perl function call, so memoize some lookups
1719 if (prodesc->fn_retistuple)
1720 (void) get_call_result_type(fcinfo, NULL, &tupdesc);
1722 tupdesc = rsi->expectedDesc;
1725 * Make sure the tuple_store and ret_tdesc are sufficiently
1728 old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
1730 current_call_data->ret_tdesc = CreateTupleDescCopy(tupdesc);
1731 current_call_data->tuple_store =
1732 tuplestore_begin_heap(true, false, work_mem);
1733 if (prodesc->fn_retistuple)
1735 current_call_data->attinmeta =
1736 TupleDescGetAttInMetadata(current_call_data->ret_tdesc);
1739 MemoryContextSwitchTo(old_cxt);
1743 * Producing the tuple we want to return requires making plenty of
1744 * palloc() allocations that are not cleaned up. Since this
1745 * function can be called many times before the current memory
1746 * context is reset, we need to do those allocations in a
1747 * temporary context.
1749 if (!current_call_data->tmp_cxt)
1751 current_call_data->tmp_cxt =
1752 AllocSetContextCreate(rsi->econtext->ecxt_per_tuple_memory,
1753 "PL/Perl return_next temporary cxt",
1754 ALLOCSET_DEFAULT_MINSIZE,
1755 ALLOCSET_DEFAULT_INITSIZE,
1756 ALLOCSET_DEFAULT_MAXSIZE);
1759 old_cxt = MemoryContextSwitchTo(current_call_data->tmp_cxt);
1761 if (prodesc->fn_retistuple)
1763 tuple = plperl_build_tuple_result((HV *) SvRV(sv),
1764 current_call_data->attinmeta);
1768 Datum ret = (Datum) 0;
1771 if (SvOK(sv) && SvTYPE(sv) != SVt_NULL)
1773 char *val = SvPV(sv, PL_na);
1775 ret = FunctionCall3(&prodesc->result_in_func,
1776 PointerGetDatum(val),
1777 ObjectIdGetDatum(prodesc->result_typioparam),
1782 tuple = heap_form_tuple(current_call_data->ret_tdesc, &ret, &isNull);
1785 /* Make sure to store the tuple in a long-lived memory context */
1786 MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
1787 tuplestore_puttuple(current_call_data->tuple_store, tuple);
1788 MemoryContextSwitchTo(old_cxt);
1790 MemoryContextReset(current_call_data->tmp_cxt);
1795 plperl_spi_query(char *query)
1800 * Execute the query inside a sub-transaction, so we can cope with errors
1803 MemoryContext oldcontext = CurrentMemoryContext;
1804 ResourceOwner oldowner = CurrentResourceOwner;
1806 BeginInternalSubTransaction(NULL);
1807 /* Want to run inside function's memory context */
1808 MemoryContextSwitchTo(oldcontext);
1813 Portal portal = NULL;
1815 /* Create a cursor for the query */
1816 plan = SPI_prepare(query, 0, NULL);
1818 portal = SPI_cursor_open(NULL, plan, NULL, NULL, false);
1820 cursor = newSVpv(portal->name, 0);
1824 /* Commit the inner transaction, return to outer xact context */
1825 ReleaseCurrentSubTransaction();
1826 MemoryContextSwitchTo(oldcontext);
1827 CurrentResourceOwner = oldowner;
1830 * AtEOSubXact_SPI() should not have popped any SPI context, but just
1831 * in case it did, make sure we remain connected.
1833 SPI_restore_connection();
1839 /* Save error info */
1840 MemoryContextSwitchTo(oldcontext);
1841 edata = CopyErrorData();
1844 /* Abort the inner transaction */
1845 RollbackAndReleaseCurrentSubTransaction();
1846 MemoryContextSwitchTo(oldcontext);
1847 CurrentResourceOwner = oldowner;
1850 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
1851 * have left us in a disconnected state. We need this hack to return
1852 * to connected state.
1854 SPI_restore_connection();
1856 /* Punt the error to Perl */
1857 croak("%s", edata->message);
1859 /* Can't get here, but keep compiler quiet */
1869 plperl_spi_fetchrow(char *cursor)
1874 * Execute the FETCH inside a sub-transaction, so we can cope with errors
1877 MemoryContext oldcontext = CurrentMemoryContext;
1878 ResourceOwner oldowner = CurrentResourceOwner;
1880 BeginInternalSubTransaction(NULL);
1881 /* Want to run inside function's memory context */
1882 MemoryContextSwitchTo(oldcontext);
1886 Portal p = SPI_cursor_find(cursor);
1892 SPI_cursor_fetch(p, true, 1);
1893 if (SPI_processed == 0)
1895 SPI_cursor_close(p);
1900 row = plperl_hash_from_tuple(SPI_tuptable->vals[0],
1901 SPI_tuptable->tupdesc);
1903 SPI_freetuptable(SPI_tuptable);
1906 /* Commit the inner transaction, return to outer xact context */
1907 ReleaseCurrentSubTransaction();
1908 MemoryContextSwitchTo(oldcontext);
1909 CurrentResourceOwner = oldowner;
1912 * AtEOSubXact_SPI() should not have popped any SPI context, but just
1913 * in case it did, make sure we remain connected.
1915 SPI_restore_connection();
1921 /* Save error info */
1922 MemoryContextSwitchTo(oldcontext);
1923 edata = CopyErrorData();
1926 /* Abort the inner transaction */
1927 RollbackAndReleaseCurrentSubTransaction();
1928 MemoryContextSwitchTo(oldcontext);
1929 CurrentResourceOwner = oldowner;
1932 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
1933 * have left us in a disconnected state. We need this hack to return
1934 * to connected state.
1936 SPI_restore_connection();
1938 /* Punt the error to Perl */
1939 croak("%s", edata->message);
1941 /* Can't get here, but keep compiler quiet */