plperl.c 67.4 KB
Newer Older
1 2 3
/**********************************************************************
 * plperl.c - perl as a procedural language for PostgreSQL
 *
4
 *	  $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.136.2.3 2009/09/28 17:30:41 adunstan Exp $
5
 *
6 7
 **********************************************************************/

8
#include "postgres.h"
9
/* Defined by Perl */
10
#undef _
11 12

/* system stuff */
13
#include <ctype.h>
14
#include <fcntl.h>
15
#include <unistd.h>
A
 
Andrew Dunstan 已提交
16
#include <locale.h>
17 18

/* postgreSQL stuff */
19 20
#include "commands/trigger.h"
#include "executor/spi.h"
21
#include "funcapi.h"
22 23 24 25
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "parser/parse_type.h"
26
#include "utils/fmgroids.h"
27
#include "utils/guc.h"
28
#include "utils/lsyscache.h"
29
#include "utils/memutils.h"
30
#include "utils/typcache.h"
31
#include "utils/hsearch.h"
32 33

/* perl stuff */
A
 
Andrew Dunstan 已提交
34
#include "plperl.h"
35

36 37
PG_MODULE_MAGIC;

38 39 40 41 42
/**********************************************************************
 * The information we cache about loaded procedures
 **********************************************************************/
typedef struct plperl_proc_desc
{
43
	char	   *proname;		/* user name of procedure */
44
	TransactionId fn_xmin;
45
	ItemPointerData fn_tid;
46
	bool		fn_readonly;
47
	bool		lanpltrusted;
48
	bool		fn_retistuple;	/* true, if function returns tuple */
B
Bruce Momjian 已提交
49
	bool		fn_retisset;	/* true, if function returns set */
B
Bruce Momjian 已提交
50
	bool		fn_retisarray;	/* true if function returns array */
51
	Oid			result_oid;		/* Oid of result type */
B
Bruce Momjian 已提交
52
	FmgrInfo	result_in_func; /* I/O function and arg for result type */
53
	Oid			result_typioparam;
54 55
	int			nargs;
	FmgrInfo	arg_out_func[FUNC_MAX_ARGS];
56
	bool		arg_is_rowtype[FUNC_MAX_ARGS];
57
	SV		   *reference;
58
} plperl_proc_desc;
59

60 61 62 63
/* hash table entry for proc desc  */

typedef struct plperl_proc_entry
{
B
Bruce Momjian 已提交
64 65
	char		proc_name[NAMEDATALEN]; /* internal name, eg
										 * __PLPerl_proc_39987 */
66
	plperl_proc_desc *proc_data;
67
} plperl_proc_entry;
68

69 70 71 72 73 74 75
/*
 * The information we cache for the duration of a single call to a
 * function.
 */
typedef struct plperl_call_data
{
	plperl_proc_desc *prodesc;
B
Bruce Momjian 已提交
76 77 78 79 80
	FunctionCallInfo fcinfo;
	Tuplestorestate *tuple_store;
	TupleDesc	ret_tdesc;
	AttInMetadata *attinmeta;
	MemoryContext tmp_cxt;
81 82
} plperl_call_data;

A
 
Andrew Dunstan 已提交
83 84 85 86 87 88 89 90 91 92 93 94
/**********************************************************************
 * The information we cache about prepared and saved plans
 **********************************************************************/
typedef struct plperl_query_desc
{
	char		qname[sizeof(long) * 2 + 1];
	void	   *plan;
	int			nargs;
	Oid		   *argtypes;
	FmgrInfo   *arginfuncs;
	Oid		   *argtypioparams;
} plperl_query_desc;
95

B
Bruce Momjian 已提交
96
/* hash table entry for query desc	*/
97 98 99

typedef struct plperl_query_entry
{
B
Bruce Momjian 已提交
100
	char		query_name[NAMEDATALEN];
101
	plperl_query_desc *query_data;
102
} plperl_query_entry;
103

104 105 106
/**********************************************************************
 * Global data
 **********************************************************************/
107 108 109 110 111 112 113 114

typedef enum
{
	INTERP_NONE,
	INTERP_HELD,
	INTERP_TRUSTED,
	INTERP_UNTRUSTED,
	INTERP_BOTH
115
} InterpState;
116 117 118 119

static InterpState interp_state = INTERP_NONE;
static bool can_run_two = false;

120
static bool plperl_safe_init_done = false;
121 122 123 124
static PerlInterpreter *plperl_trusted_interp = NULL;
static PerlInterpreter *plperl_untrusted_interp = NULL;
static PerlInterpreter *plperl_held_interp = NULL;
static bool trusted_context;
B
Bruce Momjian 已提交
125 126
static HTAB *plperl_proc_hash = NULL;
static HTAB *plperl_query_hash = NULL;
127

128 129
static bool plperl_use_strict = false;

130 131
/* this is saved and restored by plperl_call_handler */
static plperl_call_data *current_call_data = NULL;
132

133 134 135
/**********************************************************************
 * Forward declarations
 **********************************************************************/
B
Bruce Momjian 已提交
136
Datum		plperl_call_handler(PG_FUNCTION_ARGS);
137
Datum		plperl_validator(PG_FUNCTION_ARGS);
138 139 140
void		_PG_init(void);

static void plperl_init_interp(void);
141

142
static Datum plperl_func_handler(PG_FUNCTION_ARGS);
143

144
static Datum plperl_trigger_handler(PG_FUNCTION_ARGS);
145 146
static plperl_proc_desc *compile_plperl_function(Oid fn_oid, bool is_trigger);

147
static SV  *plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc);
148
static void plperl_init_shared_libs(pTHX);
149
static HV  *plperl_spi_execute_fetch_result(SPITupleTable *, int, int);
150 151 152
static SV  *newSVstring(const char *str);
static SV **hv_store_string(HV *hv, const char *key, SV *val);
static SV **hv_fetch_string(HV *hv, const char *key);
153 154
static SV  *plperl_create_sub(char *proname, char *s, bool trusted);
static SV  *plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo);
155

156 157 158 159 160 161 162
/*
 * This routine is a crock, and so is everyplace that calls it.  The problem
 * is that the cached form of plperl functions/queries is allocated permanently
 * (mostly via malloc()) and never released until backend exit.  Subsidiary
 * data structures such as fmgr info records therefore must live forever
 * as well.  A better implementation would store all this stuff in a per-
 * function memory context that could be reclaimed at need.  In the meantime,
163 164 165
 * fmgr_info_cxt must be called specifying TopMemoryContext so that whatever
 * it might allocate, and whatever the eventual function might allocate using
 * fn_mcxt, will live forever too.
166 167 168 169
 */
static void
perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
{
170
	fmgr_info_cxt(functionId, finfo, TopMemoryContext);
171 172
}

173

174 175 176 177 178
/*
 * _PG_init()			- library load-time initialization
 *
 * DO NOT make this static nor change its name!
 */
179
void
180
_PG_init(void)
181
{
182 183
	/* Be sure we do initialization only once (should be redundant now) */
	static bool inited = false;
B
Bruce Momjian 已提交
184
	HASHCTL		hash_ctl;
185 186

	if (inited)
187 188
		return;

189
	DefineCustomBoolVariable("plperl.use_strict",
B
Bruce Momjian 已提交
190 191 192 193 194
	  "If true, will compile trusted and untrusted perl code in strict mode",
							 NULL,
							 &plperl_use_strict,
							 PGC_USERSET,
							 NULL, NULL);
195 196

	EmitWarningsOnPlaceholders("plperl");
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
	MemSet(&hash_ctl, 0, sizeof(hash_ctl));

	hash_ctl.keysize = NAMEDATALEN;
	hash_ctl.entrysize = sizeof(plperl_proc_entry);

	plperl_proc_hash = hash_create("PLPerl Procedures",
								   32,
								   &hash_ctl,
								   HASH_ELEM);

	hash_ctl.entrysize = sizeof(plperl_query_entry);
	plperl_query_hash = hash_create("PLPerl Queries",
									32,
									&hash_ctl,
									HASH_ELEM);

214 215
	plperl_init_interp();

216
	inited = true;
217 218
}

219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
/* Each of these macros must represent a single string literal */

#define PERLBOOT \
	"SPI::bootstrap(); use vars qw(%_SHARED);" \
	"sub ::plperl_warn { my $msg = shift; " \
	"       $msg =~ s/\\(eval \\d+\\) //g; &elog(&NOTICE, $msg); } " \
	"$SIG{__WARN__} = \\&::plperl_warn; " \
	"sub ::plperl_die { my $msg = shift; " \
	"       $msg =~ s/\\(eval \\d+\\) //g; die $msg; } " \
	"$SIG{__DIE__} = \\&::plperl_die; " \
	"sub ::mkunsafefunc {" \
	"      my $ret = eval(qq[ sub { $_[0] $_[1] } ]); " \
	"      $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; }" \
	"use strict; " \
	"sub ::mk_strict_unsafefunc {" \
	"      my $ret = eval(qq[ sub { use strict; $_[0] $_[1] } ]); " \
	"      $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; } " \
	"sub ::_plperl_to_pg_array {" \
	"  my $arg = shift; ref $arg eq 'ARRAY' || return $arg; " \
	"  my $res = ''; my $first = 1; " \
	"  foreach my $elem (@$arg) " \
	"  { " \
	"    $res .= ', ' unless $first; $first = undef; " \
	"    if (ref $elem) " \
	"    { " \
	"      $res .= _plperl_to_pg_array($elem); " \
	"    } " \
A
 
Andrew Dunstan 已提交
246
	"    elsif (defined($elem)) " \
247 248 249 250 251
	"    { " \
	"      my $str = qq($elem); " \
	"      $str =~ s/([\"\\\\])/\\\\$1/g; " \
	"      $res .= qq(\"$str\"); " \
	"    } " \
A
 
Andrew Dunstan 已提交
252 253 254 255
	"    else " \
	"    { "\
	"      $res .= 'NULL' ; " \
	"    } "\
256 257 258 259 260 261 262
	"  } " \
	"  return qq({$res}); " \
	"} "

#define SAFE_MODULE \
	"require Safe; $Safe::VERSION"

263 264 265 266 267 268 269
/* 
 * The temporary enabling of the caller opcode here is to work around a
 * bug in perl 5.10, which unkindly changed the way its Safe.pm works, without
 * notice. It is quite safe, as caller is informational only, and in any case
 * we only enable it while we load the 'strict' module.
 */

270 271 272 273 274
#define SAFE_OK \
	"use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');" \
	"$PLContainer->permit_only(':default');" \
	"$PLContainer->permit(qw[:base_math !:base_io sort time]);" \
	"$PLContainer->share(qw[&elog &spi_exec_query &return_next " \
A
 
Andrew Dunstan 已提交
275 276
	"&spi_query &spi_fetchrow &spi_cursor_close " \
	"&spi_prepare &spi_exec_prepared &spi_query_prepared &spi_freeplan " \
277 278 279 280 281
	"&_plperl_to_pg_array " \
	"&DEBUG &LOG &INFO &NOTICE &WARNING &ERROR %_SHARED ]);" \
	"sub ::mksafefunc {" \
	"      my $ret = $PLContainer->reval(qq[sub { $_[0] $_[1] }]); " \
	"      $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; }" \
282 283
	"$PLContainer->permit(qw[require caller]); $PLContainer->reval('use strict;');" \
	"$PLContainer->deny(qw[require caller]); " \
284 285
	"sub ::mk_strict_safefunc {" \
	"      my $ret = $PLContainer->reval(qq[sub { BEGIN { strict->import(); } $_[0] $_[1] }]); " \
B
Bruce Momjian 已提交
286
	"      $@ =~ s/\\(eval \\d+\\) //g if $@; return $ret; }"
287 288 289 290 291 292 293 294 295 296 297 298

#define SAFE_BAD \
	"use vars qw($PLContainer); $PLContainer = new Safe('PLPerl');" \
	"$PLContainer->permit_only(':default');" \
	"$PLContainer->share(qw[&elog &ERROR ]);" \
	"sub ::mksafefunc { return $PLContainer->reval(qq[sub { " \
	"      elog(ERROR,'trusted Perl functions disabled - " \
	"      please upgrade Perl Safe module to version 2.09 or later');}]); }" \
	"sub ::mk_strict_safefunc { return $PLContainer->reval(qq[sub { " \
	"      elog(ERROR,'trusted Perl functions disabled - " \
	"      please upgrade Perl Safe module to version 2.09 or later');}]); }"

299 300
#define TEST_FOR_MULTI \
	"use Config; " \
B
Bruce Momjian 已提交
301 302
	"$Config{usemultiplicity} eq 'define' or "	\
	"($Config{usethreads} eq 'define' " \
303 304 305 306 307 308 309
	" and $Config{useithreads} eq 'define')"


/********************************************************************
 *
 * We start out by creating a "held" interpreter that we can use in
 * trusted or untrusted mode (but not both) as the need arises. Later, we
310
 * assign that interpreter if it is available to either the trusted or
311 312 313 314 315 316 317
 * untrusted interpreter. If it has already been assigned, and we need to
 * create the other interpreter, we do that if we can, or error out.
 * We detect if it is safe to run two interpreters during the setup of the
 * dummy interpreter.
 */


318
static void
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
check_interp(bool trusted)
{
	if (interp_state == INTERP_HELD)
	{
		if (trusted)
		{
			plperl_trusted_interp = plperl_held_interp;
			interp_state = INTERP_TRUSTED;
		}
		else
		{
			plperl_untrusted_interp = plperl_held_interp;
			interp_state = INTERP_UNTRUSTED;
		}
		plperl_held_interp = NULL;
		trusted_context = trusted;
	}
336
	else if (interp_state == INTERP_BOTH ||
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
			 (trusted && interp_state == INTERP_TRUSTED) ||
			 (!trusted && interp_state == INTERP_UNTRUSTED))
	{
		if (trusted_context != trusted)
		{
			if (trusted)
				PERL_SET_CONTEXT(plperl_trusted_interp);
			else
				PERL_SET_CONTEXT(plperl_untrusted_interp);
			trusted_context = trusted;
		}
	}
	else if (can_run_two)
	{
		PERL_SET_CONTEXT(plperl_held_interp);
		plperl_init_interp();
		if (trusted)
			plperl_trusted_interp = plperl_held_interp;
		else
			plperl_untrusted_interp = plperl_held_interp;
		interp_state = INTERP_BOTH;
		plperl_held_interp = NULL;
		trusted_context = trusted;
	}
	else
	{
363
		elog(ERROR,
364
			 "cannot allocate second Perl interpreter on this platform");
365 366 367 368 369
	}
}


static void
B
Bruce Momjian 已提交
370
restore_context(bool old_context)
371 372 373 374 375 376 377 378 379 380
{
	if (trusted_context != old_context)
	{
		if (old_context)
			PERL_SET_CONTEXT(plperl_trusted_interp);
		else
			PERL_SET_CONTEXT(plperl_untrusted_interp);
		trusted_context = old_context;
	}
}
381 382

static void
383
plperl_init_interp(void)
384
{
B
Bruce Momjian 已提交
385
	static char *embedding[3] = {
386
		"", "-e", PERLBOOT
387 388
	};

389 390
	int nargs = 3;

391 392
	char *dummy_perl_env[1] = { NULL }; 

A
 
Andrew Dunstan 已提交
393 394
#ifdef WIN32

B
Bruce Momjian 已提交
395
	/*
A
 
Andrew Dunstan 已提交
396
	 * The perl library on startup does horrible things like call
B
Bruce Momjian 已提交
397 398 399 400 401
	 * setlocale(LC_ALL,""). We have protected against that on most platforms
	 * by setting the environment appropriately. However, on Windows,
	 * setlocale() does not consult the environment, so we need to save the
	 * existing locale settings before perl has a chance to mangle them and
	 * restore them after its dirty deeds are done.
A
 
Andrew Dunstan 已提交
402 403 404 405 406 407 408 409
	 *
	 * MSDN ref:
	 * http://msdn.microsoft.com/library/en-us/vclib/html/_crt_locale.asp
	 *
	 * It appears that we only need to do this on interpreter startup, and
	 * subsequent calls to the interpreter don't mess with the locale
	 * settings.
	 *
B
Bruce Momjian 已提交
410 411
	 * We restore them using Perl's POSIX::setlocale() function so that Perl
	 * doesn't have a different idea of the locale from Postgres.
A
 
Andrew Dunstan 已提交
412 413 414
	 *
	 */

B
Bruce Momjian 已提交
415 416 417 418 419 420 421
	char	   *loc;
	char	   *save_collate,
			   *save_ctype,
			   *save_monetary,
			   *save_numeric,
			   *save_time;
	char		buf[1024];
A
 
Andrew Dunstan 已提交
422

B
Bruce Momjian 已提交
423
	loc = setlocale(LC_COLLATE, NULL);
A
 
Andrew Dunstan 已提交
424
	save_collate = loc ? pstrdup(loc) : NULL;
B
Bruce Momjian 已提交
425
	loc = setlocale(LC_CTYPE, NULL);
A
 
Andrew Dunstan 已提交
426
	save_ctype = loc ? pstrdup(loc) : NULL;
B
Bruce Momjian 已提交
427
	loc = setlocale(LC_MONETARY, NULL);
A
 
Andrew Dunstan 已提交
428
	save_monetary = loc ? pstrdup(loc) : NULL;
B
Bruce Momjian 已提交
429
	loc = setlocale(LC_NUMERIC, NULL);
A
 
Andrew Dunstan 已提交
430
	save_numeric = loc ? pstrdup(loc) : NULL;
B
Bruce Momjian 已提交
431
	loc = setlocale(LC_TIME, NULL);
A
 
Andrew Dunstan 已提交
432 433 434
	save_time = loc ? pstrdup(loc) : NULL;
#endif

435 436 437 438 439 440 441 442 443 444
	/****
	 * The perl API docs state that PERL_SYS_INIT3 should be called before
	 * allocating interprters. Unfortunately, on some platforms this fails
	 * in the Perl_do_taint() routine, which is called when the platform is
	 * using the system's malloc() instead of perl's own. Other platforms,
	 * notably Windows, fail if PERL_SYS_INIT3 is not called. So we call it
	 * if it's available, unless perl is using the system malloc(), which is
	 * true when MYMALLOC is set.
	 */
#if defined(PERL_SYS_INIT3) && !defined(MYMALLOC)
445 446
	/* only call this the first time through, as per perlembed man page */
	if (interp_state == INTERP_NONE)
447
		PERL_SYS_INIT3(&nargs, (char ***) &embedding, (char***)&dummy_perl_env);
448 449
#endif

450 451
	plperl_held_interp = perl_alloc();
	if (!plperl_held_interp)
452
		elog(ERROR, "could not allocate Perl interpreter");
453

454
	perl_construct(plperl_held_interp);
455
	perl_parse(plperl_held_interp, plperl_init_shared_libs,
456
			   nargs, embedding, NULL);
457
	perl_run(plperl_held_interp);
458

459 460
	if (interp_state == INTERP_NONE)
	{
B
Bruce Momjian 已提交
461
		SV		   *res;
462

B
Bruce Momjian 已提交
463
		res = eval_pv(TEST_FOR_MULTI, TRUE);
464
		can_run_two = SvIV(res);
465 466
		interp_state = INTERP_HELD;
	}
A
 
Andrew Dunstan 已提交
467 468 469

#ifdef WIN32

B
Bruce Momjian 已提交
470
	eval_pv("use POSIX qw(locale_h);", TRUE);	/* croak on failure */
A
 
Andrew Dunstan 已提交
471 472 473

	if (save_collate != NULL)
	{
B
Bruce Momjian 已提交
474 475 476
		snprintf(buf, sizeof(buf), "setlocale(%s,'%s');",
				 "LC_COLLATE", save_collate);
		eval_pv(buf, TRUE);
A
 
Andrew Dunstan 已提交
477 478 479 480
		pfree(save_collate);
	}
	if (save_ctype != NULL)
	{
B
Bruce Momjian 已提交
481 482 483
		snprintf(buf, sizeof(buf), "setlocale(%s,'%s');",
				 "LC_CTYPE", save_ctype);
		eval_pv(buf, TRUE);
A
 
Andrew Dunstan 已提交
484 485 486 487
		pfree(save_ctype);
	}
	if (save_monetary != NULL)
	{
B
Bruce Momjian 已提交
488 489 490
		snprintf(buf, sizeof(buf), "setlocale(%s,'%s');",
				 "LC_MONETARY", save_monetary);
		eval_pv(buf, TRUE);
A
 
Andrew Dunstan 已提交
491 492 493 494
		pfree(save_monetary);
	}
	if (save_numeric != NULL)
	{
B
Bruce Momjian 已提交
495 496 497
		snprintf(buf, sizeof(buf), "setlocale(%s,'%s');",
				 "LC_NUMERIC", save_numeric);
		eval_pv(buf, TRUE);
A
 
Andrew Dunstan 已提交
498 499 500 501
		pfree(save_numeric);
	}
	if (save_time != NULL)
	{
B
Bruce Momjian 已提交
502 503 504
		snprintf(buf, sizeof(buf), "setlocale(%s,'%s');",
				 "LC_TIME", save_time);
		eval_pv(buf, TRUE);
A
 
Andrew Dunstan 已提交
505 506 507 508
		pfree(save_time);
	}
#endif

509 510
}

511 512 513 514

static void
plperl_safe_init(void)
{
B
Bruce Momjian 已提交
515
	SV		   *res;
516
	double		safe_version;
517

518
	res = eval_pv(SAFE_MODULE, FALSE);	/* TRUE = croak if failure */
519 520 521

	safe_version = SvNV(res);

522 523 524 525 526
	/*
	 * We actually want to reject safe_version < 2.09, but it's risky to
	 * assume that floating-point comparisons are exact, so use a slightly
	 * smaller comparison value.
	 */
B
Bruce Momjian 已提交
527
	if (safe_version < 2.0899)
528 529
	{
		/* not safe, so disallow all trusted funcs */
530
		eval_pv(SAFE_BAD, FALSE);
531 532 533
	}
	else
	{
534
		eval_pv(SAFE_OK, FALSE);
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
		if (GetDatabaseEncoding() == PG_UTF8)
		{
			/* 
			 * Fill in just enough information to set up this perl
			 * function in the safe container and call it.
			 * For some reason not entirely clear, it prevents errors that
			 * can arise from the regex code later trying to load
			 * utf8 modules.
			 */
			plperl_proc_desc desc;			
			FunctionCallInfoData fcinfo;
			SV *ret;
			SV *func;

			/* make sure we don't call ourselves recursively */
			plperl_safe_init_done = true;

			/* compile the function */
553 554 555
			func = plperl_create_sub("utf8fix",
							 "return shift =~ /\\xa9/i ? 'true' : 'false' ;",
									 true);
556 557 558 559 560

			/* set up to call the function with a single text argument 'a' */
			desc.reference = func;
			desc.nargs = 1;
			desc.arg_is_rowtype[0] = false;
561 562 563
			fmgr_info(F_TEXTOUT, &(desc.arg_out_func[0]));

			fcinfo.arg[0] = DirectFunctionCall1(textin, CStringGetDatum("a"));
564 565 566
			fcinfo.argnull[0] = false;
			
			/* and make the call */
567
			ret = plperl_call_perl_func(&desc, &fcinfo);
568
		}
569
	}
570 571 572 573

	plperl_safe_init_done = true;
}

574 575 576 577 578 579
/*
 * Perl likes to put a newline after its error messages; clean up such
 */
static char *
strip_trailing_ws(const char *msg)
{
B
Bruce Momjian 已提交
580 581
	char	   *res = pstrdup(msg);
	int			len = strlen(res);
582

B
Bruce Momjian 已提交
583
	while (len > 0 && isspace((unsigned char) res[len - 1]))
584 585 586 587 588
		res[--len] = '\0';
	return res;
}


589 590
/* Build a tuple from a hash. */

591
static HeapTuple
592
plperl_build_tuple_result(HV *perlhash, AttInMetadata *attinmeta)
593
{
594 595 596 597 598 599
	TupleDesc	td = attinmeta->tupdesc;
	char	  **values;
	SV		   *val;
	char	   *key;
	I32			klen;
	HeapTuple	tup;
600

601
	values = (char **) palloc0(td->natts * sizeof(char *));
602

603 604 605
	hv_iterinit(perlhash);
	while ((val = hv_iternextsv(perlhash, &key, &klen)))
	{
B
Bruce Momjian 已提交
606
		int			attn = SPI_fnumber(td, key);
607

608
		if (attn <= 0 || td->attrs[attn - 1]->attisdropped)
609 610 611 612
			ereport(ERROR,
					(errcode(ERRCODE_UNDEFINED_COLUMN),
					 errmsg("Perl hash contains nonexistent column \"%s\"",
							key)));
613
		if (SvOK(val))
614
			values[attn - 1] = SvPV(val, PL_na);
615
	}
616 617 618 619 620
	hv_iterinit(perlhash);

	tup = BuildTupleFromCStrings(attinmeta, values);
	pfree(values);
	return tup;
621 622
}

623 624 625
/*
 * convert perl array to postgres string representation
 */
B
Bruce Momjian 已提交
626
static SV  *
627
plperl_convert_to_pg_array(SV *src)
628
{
B
Bruce Momjian 已提交
629 630 631 632
	SV		   *rv;
	int			count;

	dSP;
633

B
Bruce Momjian 已提交
634
	PUSHMARK(SP);
635
	XPUSHs(src);
B
Bruce Momjian 已提交
636
	PUTBACK;
637

638
	count = call_pv("::_plperl_to_pg_array", G_SCALAR);
639

B
Bruce Momjian 已提交
640
	SPAGAIN;
641 642

	if (count != 1)
643
		elog(ERROR, "unexpected _plperl_to_pg_array failure");
644 645 646

	rv = POPs;

B
Bruce Momjian 已提交
647 648 649
	PUTBACK;

	return rv;
650 651
}

652

653 654
/* Set up the arguments for a trigger call. */

655 656 657 658 659
static SV  *
plperl_trigger_build_args(FunctionCallInfo fcinfo)
{
	TriggerData *tdata;
	TupleDesc	tupdesc;
660
	int			i;
661 662 663 664 665
	char	   *level;
	char	   *event;
	char	   *relid;
	char	   *when;
	HV		   *hv;
666

667
	hv = newHV();
668 669 670 671

	tdata = (TriggerData *) fcinfo->context;
	tupdesc = tdata->tg_relation->rd_att;

672
	relid = DatumGetCString(
B
Bruce Momjian 已提交
673 674 675 676
							DirectFunctionCall1(oidout,
								  ObjectIdGetDatum(tdata->tg_relation->rd_id)
												)
		);
677

678 679
	hv_store_string(hv, "name", newSVstring(tdata->tg_trigger->tgname));
	hv_store_string(hv, "relid", newSVstring(relid));
680 681 682

	if (TRIGGER_FIRED_BY_INSERT(tdata->tg_event))
	{
683
		event = "INSERT";
684
		if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
685 686 687
			hv_store_string(hv, "new",
							plperl_hash_from_tuple(tdata->tg_trigtuple,
												   tupdesc));
688 689 690
	}
	else if (TRIGGER_FIRED_BY_DELETE(tdata->tg_event))
	{
691
		event = "DELETE";
692
		if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
693 694 695
			hv_store_string(hv, "old",
							plperl_hash_from_tuple(tdata->tg_trigtuple,
												   tupdesc));
696 697 698
	}
	else if (TRIGGER_FIRED_BY_UPDATE(tdata->tg_event))
	{
699
		event = "UPDATE";
700 701
		if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
		{
702 703 704 705 706 707
			hv_store_string(hv, "old",
							plperl_hash_from_tuple(tdata->tg_trigtuple,
												   tupdesc));
			hv_store_string(hv, "new",
							plperl_hash_from_tuple(tdata->tg_newtuple,
												   tupdesc));
708
		}
709
	}
710
	else
711
		event = "UNKNOWN";
712

713 714
	hv_store_string(hv, "event", newSVstring(event));
	hv_store_string(hv, "argc", newSViv(tdata->tg_trigger->tgnargs));
715

716
	if (tdata->tg_trigger->tgnargs > 0)
717
	{
B
Bruce Momjian 已提交
718 719 720
		AV		   *av = newAV();

		for (i = 0; i < tdata->tg_trigger->tgnargs; i++)
721 722
			av_push(av, newSVstring(tdata->tg_trigger->tgargs[i]));
		hv_store_string(hv, "args", newRV_noinc((SV *) av));
723
	}
724

725 726
	hv_store_string(hv, "relname",
					newSVstring(SPI_getrelname(tdata->tg_relation)));
727

728 729
	hv_store_string(hv, "table_name",
					newSVstring(SPI_getrelname(tdata->tg_relation)));
A
 
Andrew Dunstan 已提交
730

731 732
	hv_store_string(hv, "table_schema",
					newSVstring(SPI_getnspname(tdata->tg_relation)));
A
 
Andrew Dunstan 已提交
733

734
	if (TRIGGER_FIRED_BEFORE(tdata->tg_event))
735
		when = "BEFORE";
736
	else if (TRIGGER_FIRED_AFTER(tdata->tg_event))
737
		when = "AFTER";
738
	else
739
		when = "UNKNOWN";
740
	hv_store_string(hv, "when", newSVstring(when));
741 742

	if (TRIGGER_FIRED_FOR_ROW(tdata->tg_event))
743
		level = "ROW";
744
	else if (TRIGGER_FIRED_FOR_STATEMENT(tdata->tg_event))
745
		level = "STATEMENT";
746
	else
747
		level = "UNKNOWN";
748
	hv_store_string(hv, "level", newSVstring(level));
749

B
Bruce Momjian 已提交
750
	return newRV_noinc((SV *) hv);
751 752 753
}


754
/* Set up the new tuple returned from a trigger. */
755

756
static HeapTuple
757
plperl_modify_tuple(HV *hvTD, TriggerData *tdata, HeapTuple otup)
758 759 760 761
{
	SV		  **svp;
	HV		   *hvNew;
	HeapTuple	rtup;
762 763 764 765 766 767 768 769
	SV		   *val;
	char	   *key;
	I32			klen;
	int			slotsused;
	int		   *modattrs;
	Datum	   *modvalues;
	char	   *modnulls;

770 771 772 773
	TupleDesc	tupdesc;

	tupdesc = tdata->tg_relation->rd_att;

774
	svp = hv_fetch_string(hvTD, "new");
775
	if (!svp)
776 777 778
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_COLUMN),
				 errmsg("$_TD->{new} does not exist")));
779
	if (!SvOK(*svp) || SvTYPE(*svp) != SVt_RV || SvTYPE(SvRV(*svp)) != SVt_PVHV)
780 781 782
		ereport(ERROR,
				(errcode(ERRCODE_DATATYPE_MISMATCH),
				 errmsg("$_TD->{new} is not a hash reference")));
783 784
	hvNew = (HV *) SvRV(*svp);

785 786 787 788
	modattrs = palloc(tupdesc->natts * sizeof(int));
	modvalues = palloc(tupdesc->natts * sizeof(Datum));
	modnulls = palloc(tupdesc->natts * sizeof(char));
	slotsused = 0;
789

790 791
	hv_iterinit(hvNew);
	while ((val = hv_iternextsv(hvNew, &key, &klen)))
792
	{
793
		int			attn = SPI_fnumber(tupdesc, key);
794 795 796 797
		Oid			typinput;
		Oid			typioparam;
		int32		atttypmod;
		FmgrInfo	finfo;
798

799
		if (attn <= 0 || tupdesc->attrs[attn - 1]->attisdropped)
800 801 802 803
			ereport(ERROR,
					(errcode(ERRCODE_UNDEFINED_COLUMN),
					 errmsg("Perl hash contains nonexistent column \"%s\"",
							key)));
804 805 806 807 808
		/* XXX would be better to cache these lookups */
		getTypeInputInfo(tupdesc->attrs[attn - 1]->atttypid,
						 &typinput, &typioparam);
		fmgr_info(typinput, &finfo);
		atttypmod = tupdesc->attrs[attn - 1]->atttypmod;
809
		if (SvOK(val))
810
		{
811 812 813 814
			modvalues[slotsused] = InputFunctionCall(&finfo,
													 SvPV(val, PL_na),
													 typioparam,
													 atttypmod);
815
			modnulls[slotsused] = ' ';
816 817 818
		}
		else
		{
819 820 821 822
			modvalues[slotsused] = InputFunctionCall(&finfo,
													 NULL,
													 typioparam,
													 atttypmod);
823
			modnulls[slotsused] = 'n';
824
		}
825 826
		modattrs[slotsused] = attn;
		slotsused++;
827
	}
828 829 830 831
	hv_iterinit(hvNew);

	rtup = SPI_modifytuple(tdata->tg_relation, otup, slotsused,
						   modattrs, modvalues, modnulls);
832 833 834 835

	pfree(modattrs);
	pfree(modvalues);
	pfree(modnulls);
836

837
	if (rtup == NULL)
838
		elog(ERROR, "SPI_modifytuple failed: %s",
839
			 SPI_result_code_string(SPI_result));
840 841 842

	return rtup;
}
843

844

845 846
/*
 * This is the only externally-visible part of the plperl call interface.
847
 * The Postgres function and trigger managers call it to execute a
848 849
 * perl function.
 */
850
PG_FUNCTION_INFO_V1(plperl_call_handler);
851 852

Datum
853
plperl_call_handler(PG_FUNCTION_ARGS)
854
{
B
Bruce Momjian 已提交
855
	Datum		retval;
856
	plperl_call_data *save_call_data;
857

858
	save_call_data = current_call_data;
859 860 861 862 863 864 865 866 867
	PG_TRY();
	{
		if (CALLED_AS_TRIGGER(fcinfo))
			retval = PointerGetDatum(plperl_trigger_handler(fcinfo));
		else
			retval = plperl_func_handler(fcinfo);
	}
	PG_CATCH();
	{
868
		current_call_data = save_call_data;
869 870 871 872
		PG_RE_THROW();
	}
	PG_END_TRY();

873
	current_call_data = save_call_data;
874 875 876
	return retval;
}

877 878 879 880 881 882 883 884 885 886 887 888
/*
 * This is the other externally visible function - it is called when CREATE
 * FUNCTION is issued to validate the function being created/replaced.
 */
PG_FUNCTION_INFO_V1(plperl_validator);

Datum
plperl_validator(PG_FUNCTION_ARGS)
{
	Oid			funcoid = PG_GETARG_OID(0);
	HeapTuple	tuple;
	Form_pg_proc proc;
889
	char		functyptype;
890 891 892 893
	int			numargs;
	Oid		   *argtypes;
	char	  **argnames;
	char	   *argmodes;
894
	bool		istrigger = false;
895
	int			i;
896 897 898 899 900 901 902 903 904

	/* Get the new function's pg_proc entry */
	tuple = SearchSysCache(PROCOID,
						   ObjectIdGetDatum(funcoid),
						   0, 0, 0);
	if (!HeapTupleIsValid(tuple))
		elog(ERROR, "cache lookup failed for function %u", funcoid);
	proc = (Form_pg_proc) GETSTRUCT(tuple);

905 906 907 908
	functyptype = get_typtype(proc->prorettype);

	/* Disallow pseudotype result */
	/* except for TRIGGER, RECORD, or VOID */
909
	if (functyptype == TYPTYPE_PSEUDO)
910 911 912 913 914 915 916 917 918 919 920 921 922
	{
		/* we assume OPAQUE with no arguments means a trigger */
		if (proc->prorettype == TRIGGEROID ||
			(proc->prorettype == OPAQUEOID && proc->pronargs == 0))
			istrigger = true;
		else if (proc->prorettype != RECORDOID &&
				 proc->prorettype != VOIDOID)
			ereport(ERROR,
					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
					 errmsg("plperl functions cannot return type %s",
							format_type_be(proc->prorettype))));
	}

923 924 925 926 927
	/* Disallow pseudotypes in arguments (either IN or OUT) */
	numargs = get_func_arg_info(tuple,
								&argtypes, &argnames, &argmodes);
	for (i = 0; i < numargs; i++)
	{
928
		if (get_typtype(argtypes[i]) == TYPTYPE_PSEUDO)
929 930 931 932 933 934
			ereport(ERROR,
					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
					 errmsg("plperl functions cannot take type %s",
							format_type_be(argtypes[i]))));
	}

935 936
	ReleaseSysCache(tuple);

937 938 939
	/* Postpone body checks if !check_function_bodies */
	if (check_function_bodies)
	{
940
		(void) compile_plperl_function(funcoid, istrigger);
941
	}
942 943 944 945 946

	/* the result of a validator is ignored */
	PG_RETURN_VOID();
}

947

948 949 950 951
/*
 * Uses mksafefunc/mkunsafefunc to create an anonymous sub whose text is
 * supplied in s, and returns a reference to the closure.
 */
B
Bruce Momjian 已提交
952
static SV  *
953
plperl_create_sub(char *proname, char *s, bool trusted)
954
{
955
	dSP;
956
	SV		   *subref;
B
Bruce Momjian 已提交
957
	int			count;
B
Bruce Momjian 已提交
958
	char	   *compile_sub;
959

B
Bruce Momjian 已提交
960
	if (trusted && !plperl_safe_init_done)
961
	{
962
		plperl_safe_init();
963 964
		SPAGAIN;
	}
965

966 967 968
	ENTER;
	SAVETMPS;
	PUSHMARK(SP);
969 970
	XPUSHs(sv_2mortal(newSVstring("our $_TD; local $_TD=$_[0]; shift;")));
	XPUSHs(sv_2mortal(newSVstring(s)));
B
Bruce Momjian 已提交
971
	PUTBACK;
B
Bruce Momjian 已提交
972

973 974
	/*
	 * G_KEEPERR seems to be needed here, else we don't recognize compile
B
Bruce Momjian 已提交
975 976
	 * errors properly.  Perhaps it's because there's another level of eval
	 * inside mksafefunc?
977
	 */
978 979 980 981 982 983 984 985 986 987 988

	if (trusted && plperl_use_strict)
		compile_sub = "::mk_strict_safefunc";
	else if (plperl_use_strict)
		compile_sub = "::mk_strict_unsafefunc";
	else if (trusted)
		compile_sub = "::mksafefunc";
	else
		compile_sub = "::mkunsafefunc";

	count = perl_call_pv(compile_sub, G_SCALAR | G_EVAL | G_KEEPERR);
989 990
	SPAGAIN;

991 992 993 994 995
	if (count != 1)
	{
		PUTBACK;
		FREETMPS;
		LEAVE;
996
		elog(ERROR, "didn't get a return item from mksafefunc");
997 998
	}

999
	if (SvTRUE(ERRSV))
1000
	{
1001
		(void) POPs;
1002 1003 1004
		PUTBACK;
		FREETMPS;
		LEAVE;
1005 1006
		ereport(ERROR,
				(errcode(ERRCODE_SYNTAX_ERROR),
1007 1008
				 errmsg("creation of Perl function \"%s\" failed: %s",
						proname,
1009
						strip_trailing_ws(SvPV(ERRSV, PL_na)))));
1010 1011 1012
	}

	/*
1013 1014
	 * need to make a deep copy of the return. it comes off the stack as a
	 * temporary.
1015 1016 1017
	 */
	subref = newSVsv(POPs);

1018
	if (!SvROK(subref) || SvTYPE(SvRV(subref)) != SVt_PVCV)
1019
	{
1020 1021 1022
		PUTBACK;
		FREETMPS;
		LEAVE;
1023

1024 1025 1026 1027
		/*
		 * subref is our responsibility because it is not mortal
		 */
		SvREFCNT_dec(subref);
1028
		elog(ERROR, "didn't get a code ref");
1029 1030 1031 1032 1033
	}

	PUTBACK;
	FREETMPS;
	LEAVE;
1034

1035 1036 1037
	return subref;
}

1038

1039
/**********************************************************************
1040
 * plperl_init_shared_libs()		-
1041 1042 1043 1044
 *
 * We cannot use the DynaLoader directly to get at the Opcode
 * module (used by Safe.pm). So, we link Opcode into ourselves
 * and do the initialization behind perl's back.
1045
 *
1046 1047
 **********************************************************************/

1048 1049
EXTERN_C void boot_DynaLoader(pTHX_ CV *cv);
EXTERN_C void boot_SPI(pTHX_ CV *cv);
1050

1051
static void
1052
plperl_init_shared_libs(pTHX)
1053
{
1054 1055
	char	   *file = __FILE__;

1056
	newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
1057
	newXS("SPI::bootstrap", boot_SPI, file);
1058 1059
}

1060

B
Bruce Momjian 已提交
1061
static SV  *
1062
plperl_call_perl_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo)
1063 1064
{
	dSP;
1065 1066 1067
	SV		   *retval;
	int			i;
	int			count;
B
Bruce Momjian 已提交
1068
	SV		   *sv;
1069 1070 1071 1072

	ENTER;
	SAVETMPS;

1073
	PUSHMARK(SP);
1074

B
Bruce Momjian 已提交
1075
	XPUSHs(&PL_sv_undef);		/* no trigger data */
1076

1077 1078
	for (i = 0; i < desc->nargs; i++)
	{
1079 1080 1081
		if (fcinfo->argnull[i])
			XPUSHs(&PL_sv_undef);
		else if (desc->arg_is_rowtype[i])
1082
		{
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
			HeapTupleHeader td;
			Oid			tupType;
			int32		tupTypmod;
			TupleDesc	tupdesc;
			HeapTupleData tmptup;
			SV		   *hashref;

			td = DatumGetHeapTupleHeader(fcinfo->arg[i]);
			/* Extract rowtype info and find a tupdesc */
			tupType = HeapTupleHeaderGetTypeId(td);
			tupTypmod = HeapTupleHeaderGetTypMod(td);
			tupdesc = lookup_rowtype_tupdesc(tupType, tupTypmod);
			/* Build a temporary HeapTuple control structure */
			tmptup.t_len = HeapTupleHeaderGetDatumLength(td);
			tmptup.t_data = td;

1099 1100
			hashref = plperl_hash_from_tuple(&tmptup, tupdesc);
			XPUSHs(sv_2mortal(hashref));
1101
			ReleaseTupleDesc(tupdesc);
1102 1103 1104
		}
		else
		{
1105 1106
			char	   *tmp;

1107 1108
			tmp = OutputFunctionCall(&(desc->arg_out_func[i]),
									 fcinfo->arg[i]);
1109
			sv = newSVstring(tmp);
1110
			XPUSHs(sv_2mortal(sv));
1111
			pfree(tmp);
1112 1113 1114
		}
	}
	PUTBACK;
1115 1116 1117

	/* Do NOT use G_KEEPERR here */
	count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
1118 1119 1120

	SPAGAIN;

1121 1122 1123 1124
	if (count != 1)
	{
		PUTBACK;
		FREETMPS;
1125
		LEAVE;
1126
		elog(ERROR, "didn't get a return item from function");
1127 1128
	}

1129
	if (SvTRUE(ERRSV))
1130
	{
1131
		(void) POPs;
1132 1133
		PUTBACK;
		FREETMPS;
1134
		LEAVE;
1135 1136
		/* XXX need to find a way to assign an errcode here */
		ereport(ERROR,
1137 1138
				(errmsg("error from Perl function \"%s\": %s",
						desc->proname,
1139
						strip_trailing_ws(SvPV(ERRSV, PL_na)))));
1140 1141 1142 1143
	}

	retval = newSVsv(POPs);

1144 1145 1146
	PUTBACK;
	FREETMPS;
	LEAVE;
1147 1148 1149 1150

	return retval;
}

1151

1152
static SV  *
1153 1154
plperl_call_perl_trigger_func(plperl_proc_desc *desc, FunctionCallInfo fcinfo,
							  SV *td)
1155 1156 1157
{
	dSP;
	SV		   *retval;
1158
	Trigger    *tg_trigger;
1159 1160 1161 1162 1163 1164 1165
	int			i;
	int			count;

	ENTER;
	SAVETMPS;

	PUSHMARK(sp);
1166

1167
	XPUSHs(td);
1168

1169 1170
	tg_trigger = ((TriggerData *) fcinfo->context)->tg_trigger;
	for (i = 0; i < tg_trigger->tgnargs; i++)
1171
		XPUSHs(sv_2mortal(newSVstring(tg_trigger->tgargs[i])));
1172 1173
	PUTBACK;

1174 1175
	/* Do NOT use G_KEEPERR here */
	count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
1176 1177 1178 1179 1180 1181 1182 1183

	SPAGAIN;

	if (count != 1)
	{
		PUTBACK;
		FREETMPS;
		LEAVE;
1184
		elog(ERROR, "didn't get a return item from trigger function");
1185 1186 1187 1188
	}

	if (SvTRUE(ERRSV))
	{
1189
		(void) POPs;
1190 1191 1192
		PUTBACK;
		FREETMPS;
		LEAVE;
1193 1194
		/* XXX need to find a way to assign an errcode here */
		ereport(ERROR,
1195 1196
				(errmsg("error from Perl function \"%s\": %s",
						desc->proname,
1197
						strip_trailing_ws(SvPV(ERRSV, PL_na)))));
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
	}

	retval = newSVsv(POPs);

	PUTBACK;
	FREETMPS;
	LEAVE;

	return retval;
}
1208

1209

1210
static Datum
1211
plperl_func_handler(PG_FUNCTION_ARGS)
1212 1213
{
	plperl_proc_desc *prodesc;
1214 1215
	SV		   *perlret;
	Datum		retval;
1216
	ReturnSetInfo *rsi;
B
Bruce Momjian 已提交
1217
	SV		   *array_ret = NULL;
B
Bruce Momjian 已提交
1218
	bool		oldcontext = trusted_context;
1219

1220
	/*
B
Bruce Momjian 已提交
1221 1222
	 * Create the call_data beforing connecting to SPI, so that it is not
	 * allocated in the SPI memory context
1223 1224 1225 1226
	 */
	current_call_data = (plperl_call_data *) palloc0(sizeof(plperl_call_data));
	current_call_data->fcinfo = fcinfo;

1227 1228 1229
	if (SPI_connect() != SPI_OK_CONNECT)
		elog(ERROR, "could not connect to SPI manager");

1230
	prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false);
1231
	current_call_data->prodesc = prodesc;
1232

B
Bruce Momjian 已提交
1233
	rsi = (ReturnSetInfo *) fcinfo->resultinfo;
B
Bruce Momjian 已提交
1234

T
Tom Lane 已提交
1235
	if (prodesc->fn_retisset)
1236
	{
T
Tom Lane 已提交
1237 1238 1239 1240 1241 1242 1243 1244
		/* Check context before allowing the call to go through */
		if (!rsi || !IsA(rsi, ReturnSetInfo) ||
			(rsi->allowedModes & SFRM_Materialize) == 0 ||
			rsi->expectedDesc == NULL)
			ereport(ERROR,
					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
					 errmsg("set-valued function called in context that "
							"cannot accept a set")));
1245 1246
	}

1247 1248
	check_interp(prodesc->lanpltrusted);

1249
	perlret = plperl_call_perl_func(prodesc, fcinfo);
1250 1251 1252 1253 1254 1255 1256 1257

	/************************************************************
	 * Disconnect from SPI manager and then create the return
	 * values datum (if the input function does a palloc for it
	 * this must not be allocated in the SPI memory context
	 * because SPI_finish would free it).
	 ************************************************************/
	if (SPI_finish() != SPI_OK_FINISH)
1258
		elog(ERROR, "SPI_finish() failed");
1259

T
Tom Lane 已提交
1260
	if (prodesc->fn_retisset)
1261
	{
T
Tom Lane 已提交
1262 1263
		/*
		 * If the Perl function returned an arrayref, we pretend that it
B
Bruce Momjian 已提交
1264 1265
		 * called return_next() for each element of the array, to handle old
		 * SRFs that didn't know about return_next(). Any other sort of return
1266
		 * value is an error, except undef which means return an empty set.
T
Tom Lane 已提交
1267
		 */
1268 1269
		if (SvOK(perlret) &&
			SvTYPE(perlret) == SVt_RV &&
1270
			SvTYPE(SvRV(perlret)) == SVt_PVAV)
1271
		{
B
Bruce Momjian 已提交
1272 1273 1274 1275 1276
			int			i = 0;
			SV		  **svp = 0;
			AV		   *rav = (AV *) SvRV(perlret);

			while ((svp = av_fetch(rav, i, FALSE)) != NULL)
1277
			{
1278 1279 1280
				plperl_return_next(*svp);
				i++;
			}
1281
		}
1282
		else if (SvOK(perlret))
1283
		{
1284 1285
			ereport(ERROR,
					(errcode(ERRCODE_DATATYPE_MISMATCH),
1286 1287
					 errmsg("set-returning Perl function must return "
							"reference to array or use return_next")));
1288
		}
B
Bruce Momjian 已提交
1289

1290
		rsi->returnMode = SFRM_Materialize;
1291
		if (current_call_data->tuple_store)
1292
		{
1293 1294
			rsi->setResult = current_call_data->tuple_store;
			rsi->setDesc = current_call_data->ret_tdesc;
1295
		}
B
Bruce Momjian 已提交
1296
		retval = (Datum) 0;
1297
	}
1298
	else if (!SvOK(perlret))
1299 1300 1301 1302
	{
		/* Return NULL if Perl code returned undef */
		if (rsi && IsA(rsi, ReturnSetInfo))
			rsi->isDone = ExprEndResult;
1303 1304
		retval = InputFunctionCall(&prodesc->result_in_func, NULL,
								   prodesc->result_typioparam, -1);
1305
		fcinfo->isnull = true;
B
Bruce Momjian 已提交
1306
	}
1307
	else if (prodesc->fn_retistuple)
1308
	{
1309
		/* Return a perl hash converted to a Datum */
B
Bruce Momjian 已提交
1310
		TupleDesc	td;
1311
		AttInMetadata *attinmeta;
B
Bruce Momjian 已提交
1312
		HeapTuple	tup;
1313

1314 1315 1316
		if (!SvOK(perlret) || SvTYPE(perlret) != SVt_RV ||
			SvTYPE(SvRV(perlret)) != SVt_PVHV)
		{
1317 1318
			ereport(ERROR,
					(errcode(ERRCODE_DATATYPE_MISMATCH),
1319 1320 1321
					 errmsg("composite-returning Perl function "
							"must return reference to hash")));
		}
1322

1323 1324 1325 1326 1327 1328 1329 1330
		/* XXX should cache the attinmeta data instead of recomputing */
		if (get_call_result_type(fcinfo, NULL, &td) != TYPEFUNC_COMPOSITE)
		{
			ereport(ERROR,
					(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
					 errmsg("function returning record called in context "
							"that cannot accept type record")));
		}
1331

1332
		attinmeta = TupleDescGetAttInMetadata(td);
B
Bruce Momjian 已提交
1333
		tup = plperl_build_tuple_result((HV *) SvRV(perlret), attinmeta);
1334 1335 1336 1337
		retval = HeapTupleGetDatum(tup);
	}
	else
	{
B
Bruce Momjian 已提交
1338 1339 1340 1341
		/* Return a perl string converted to a Datum */
		char	   *val;

		if (prodesc->fn_retisarray && SvROK(perlret) &&
1342
			SvTYPE(SvRV(perlret)) == SVt_PVAV)
B
Bruce Momjian 已提交
1343 1344 1345 1346 1347
		{
			array_ret = plperl_convert_to_pg_array(perlret);
			SvREFCNT_dec(perlret);
			perlret = array_ret;
		}
1348 1349 1350

		val = SvPV(perlret, PL_na);

1351 1352
		retval = InputFunctionCall(&prodesc->result_in_func, val,
								   prodesc->result_typioparam, -1);
1353
	}
1354

1355
	if (array_ret == NULL)
B
Bruce Momjian 已提交
1356
		SvREFCNT_dec(perlret);
1357

1358
	current_call_data = NULL;
1359 1360
	restore_context(oldcontext);

1361 1362 1363
	return retval;
}

1364

1365 1366 1367 1368 1369 1370 1371 1372
static Datum
plperl_trigger_handler(PG_FUNCTION_ARGS)
{
	plperl_proc_desc *prodesc;
	SV		   *perlret;
	Datum		retval;
	SV		   *svTD;
	HV		   *hvTD;
B
Bruce Momjian 已提交
1373
	bool		oldcontext = trusted_context;
1374

1375
	/*
B
Bruce Momjian 已提交
1376 1377
	 * Create the call_data beforing connecting to SPI, so that it is not
	 * allocated in the SPI memory context
1378 1379 1380 1381
	 */
	current_call_data = (plperl_call_data *) palloc0(sizeof(plperl_call_data));
	current_call_data->fcinfo = fcinfo;

1382 1383 1384 1385
	/* Connect to SPI manager */
	if (SPI_connect() != SPI_OK_CONNECT)
		elog(ERROR, "could not connect to SPI manager");

1386 1387
	/* Find or compile the function */
	prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, true);
1388
	current_call_data->prodesc = prodesc;
1389

1390 1391
	check_interp(prodesc->lanpltrusted);

1392 1393
	svTD = plperl_trigger_build_args(fcinfo);
	perlret = plperl_call_perl_trigger_func(prodesc, fcinfo, svTD);
1394
	hvTD = (HV *) SvRV(svTD);
1395 1396 1397 1398 1399 1400 1401 1402

	/************************************************************
	* Disconnect from SPI manager and then create the return
	* values datum (if the input function does a palloc for it
	* this must not be allocated in the SPI memory context
	* because SPI_finish would free it).
	************************************************************/
	if (SPI_finish() != SPI_OK_FINISH)
1403
		elog(ERROR, "SPI_finish() failed");
1404

1405
	if (perlret == NULL || !SvOK(perlret))
1406
	{
1407
		/* undef result means go ahead with original tuple */
1408 1409 1410 1411 1412 1413 1414 1415
		TriggerData *trigdata = ((TriggerData *) fcinfo->context);

		if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
			retval = (Datum) trigdata->tg_trigtuple;
		else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
			retval = (Datum) trigdata->tg_newtuple;
		else if (TRIGGER_FIRED_BY_DELETE(trigdata->tg_event))
			retval = (Datum) trigdata->tg_trigtuple;
1416
		else
B
Bruce Momjian 已提交
1417
			retval = (Datum) 0; /* can this happen? */
1418 1419 1420
	}
	else
	{
1421 1422
		HeapTuple	trv;
		char	   *tmp;
1423

1424
		tmp = SvPV(perlret, PL_na);
1425

1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437
		if (pg_strcasecmp(tmp, "SKIP") == 0)
			trv = NULL;
		else if (pg_strcasecmp(tmp, "MODIFY") == 0)
		{
			TriggerData *trigdata = (TriggerData *) fcinfo->context;

			if (TRIGGER_FIRED_BY_INSERT(trigdata->tg_event))
				trv = plperl_modify_tuple(hvTD, trigdata,
										  trigdata->tg_trigtuple);
			else if (TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
				trv = plperl_modify_tuple(hvTD, trigdata,
										  trigdata->tg_newtuple);
1438 1439
			else
			{
1440 1441
				ereport(WARNING,
						(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
B
Bruce Momjian 已提交
1442
					   errmsg("ignoring modified tuple in DELETE trigger")));
1443 1444 1445
				trv = NULL;
			}
		}
1446
		else
1447
		{
1448 1449
			ereport(ERROR,
					(errcode(ERRCODE_E_R_I_E_TRIGGER_PROTOCOL_VIOLATED),
1450 1451
					 errmsg("result of Perl trigger function must be undef, "
							"\"SKIP\" or \"MODIFY\"")));
1452 1453 1454
			trv = NULL;
		}
		retval = PointerGetDatum(trv);
1455 1456
	}

1457 1458 1459
	SvREFCNT_dec(svTD);
	if (perlret)
		SvREFCNT_dec(perlret);
1460

1461
	current_call_data = NULL;
1462
	restore_context(oldcontext);
1463 1464
	return retval;
}
1465

1466

1467 1468
static plperl_proc_desc *
compile_plperl_function(Oid fn_oid, bool is_trigger)
1469
{
1470 1471
	HeapTuple	procTup;
	Form_pg_proc procStruct;
1472
	char		internal_proname[NAMEDATALEN];
1473
	plperl_proc_desc *prodesc = NULL;
1474
	int			i;
1475
	plperl_proc_entry *hash_entry;
B
Bruce Momjian 已提交
1476 1477
	bool		found;
	bool		oldcontext = trusted_context;
1478

1479 1480 1481 1482 1483
	/* We'll need the pg_proc tuple in any case... */
	procTup = SearchSysCache(PROCOID,
							 ObjectIdGetDatum(fn_oid),
							 0, 0, 0);
	if (!HeapTupleIsValid(procTup))
1484
		elog(ERROR, "cache lookup failed for function %u", fn_oid);
1485
	procStruct = (Form_pg_proc) GETSTRUCT(procTup);
1486 1487

	/************************************************************
1488
	 * Build our internal proc name from the function's Oid
1489
	 ************************************************************/
1490 1491 1492 1493
	if (!is_trigger)
		sprintf(internal_proname, "__PLPerl_proc_%u", fn_oid);
	else
		sprintf(internal_proname, "__PLPerl_proc_%u_trigger", fn_oid);
1494

1495 1496 1497
	/************************************************************
	 * Lookup the internal proc name in the hashtable
	 ************************************************************/
1498
	hash_entry = hash_search(plperl_proc_hash, internal_proname,
1499 1500 1501
							 HASH_FIND, NULL);

	if (hash_entry)
1502
	{
1503 1504
		bool		uptodate;

1505
		prodesc = hash_entry->proc_data;
1506

1507
		/************************************************************
1508 1509 1510
		 * If it's present, must check whether it's still up to date.
		 * This is needed because CREATE OR REPLACE FUNCTION can modify the
		 * function's pg_proc entry without changing its OID.
1511
		 ************************************************************/
1512
		uptodate = (prodesc->fn_xmin == HeapTupleHeaderGetXmin(procTup->t_data) &&
B
Bruce Momjian 已提交
1513
					ItemPointerEquals(&prodesc->fn_tid, &procTup->t_self));
1514 1515 1516

		if (!uptodate)
		{
1517 1518
			free(prodesc->proname);
			free(prodesc);
1519
			prodesc = NULL;
1520
			hash_search(plperl_proc_hash, internal_proname,
1521
						HASH_REMOVE, NULL);
1522 1523 1524 1525 1526
		}
	}

	/************************************************************
	 * If we haven't found it in the hashtable, we analyze
1527
	 * the function's arguments and return type and store
1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
	 * the in-/out-functions in the prodesc block and create
	 * a new hashtable entry for it.
	 *
	 * Then we load the procedure into the Perl interpreter.
	 ************************************************************/
	if (prodesc == NULL)
	{
		HeapTuple	langTup;
		HeapTuple	typeTup;
		Form_pg_language langStruct;
		Form_pg_type typeStruct;
1539 1540
		Datum		prosrcdatum;
		bool		isnull;
1541 1542 1543 1544 1545 1546
		char	   *proc_source;

		/************************************************************
		 * Allocate a new procedure description block
		 ************************************************************/
		prodesc = (plperl_proc_desc *) malloc(sizeof(plperl_proc_desc));
1547
		if (prodesc == NULL)
1548 1549 1550
			ereport(ERROR,
					(errcode(ERRCODE_OUT_OF_MEMORY),
					 errmsg("out of memory")));
1551
		MemSet(prodesc, 0, sizeof(plperl_proc_desc));
1552
		prodesc->proname = strdup(NameStr(procStruct->proname));
1553
		prodesc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
1554
		prodesc->fn_tid = procTup->t_self;
1555

1556 1557 1558 1559
		/* Remember if function is STABLE/IMMUTABLE */
		prodesc->fn_readonly =
			(procStruct->provolatile != PROVOLATILE_VOLATILE);

1560
		/************************************************************
1561
		 * Lookup the pg_language tuple by Oid
1562
		 ************************************************************/
1563 1564
		langTup = SearchSysCache(LANGOID,
								 ObjectIdGetDatum(procStruct->prolang),
1565
								 0, 0, 0);
1566
		if (!HeapTupleIsValid(langTup))
1567 1568 1569
		{
			free(prodesc->proname);
			free(prodesc);
1570
			elog(ERROR, "cache lookup failed for language %u",
1571
				 procStruct->prolang);
1572
		}
1573 1574 1575
		langStruct = (Form_pg_language) GETSTRUCT(langTup);
		prodesc->lanpltrusted = langStruct->lanpltrusted;
		ReleaseSysCache(langTup);
1576 1577

		/************************************************************
1578 1579
		 * Get the required information for input conversion of the
		 * return value.
1580
		 ************************************************************/
1581 1582 1583
		if (!is_trigger)
		{
			typeTup = SearchSysCache(TYPEOID,
B
Bruce Momjian 已提交
1584
									 ObjectIdGetDatum(procStruct->prorettype),
1585 1586 1587 1588 1589
									 0, 0, 0);
			if (!HeapTupleIsValid(typeTup))
			{
				free(prodesc->proname);
				free(prodesc);
1590
				elog(ERROR, "cache lookup failed for type %u",
1591
					 procStruct->prorettype);
1592 1593 1594
			}
			typeStruct = (Form_pg_type) GETSTRUCT(typeTup);

1595
			/* Disallow pseudotype result, except VOID or RECORD */
1596
			if (typeStruct->typtype == TYPTYPE_PSEUDO)
1597
			{
1598 1599
				if (procStruct->prorettype == VOIDOID ||
					procStruct->prorettype == RECORDOID)
B
Bruce Momjian 已提交
1600
					 /* okay */ ;
1601
				else if (procStruct->prorettype == TRIGGEROID)
1602 1603 1604
				{
					free(prodesc->proname);
					free(prodesc);
1605 1606
					ereport(ERROR,
							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1607
							 errmsg("trigger functions can only be called "
1608
									"as triggers")));
1609 1610 1611 1612 1613
				}
				else
				{
					free(prodesc->proname);
					free(prodesc);
1614 1615
					ereport(ERROR,
							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
B
Bruce Momjian 已提交
1616 1617
							 errmsg("plperl functions cannot return type %s",
									format_type_be(procStruct->prorettype))));
1618 1619 1620
				}
			}

1621 1622
			prodesc->result_oid = procStruct->prorettype;
			prodesc->fn_retisset = procStruct->proretset;
1623
			prodesc->fn_retistuple = (procStruct->prorettype == RECORDOID ||
B
Bruce Momjian 已提交
1624
								   typeStruct->typtype == TYPTYPE_COMPOSITE);
1625

B
Bruce Momjian 已提交
1626 1627
			prodesc->fn_retisarray =
				(typeStruct->typlen == -1 && typeStruct->typelem);
1628

1629
			perm_fmgr_info(typeStruct->typinput, &(prodesc->result_in_func));
1630
			prodesc->result_typioparam = getTypeIOParam(typeTup);
1631 1632 1633

			ReleaseSysCache(typeTup);
		}
1634 1635

		/************************************************************
1636 1637
		 * Get the required information for output conversion
		 * of all procedure arguments
1638
		 ************************************************************/
1639 1640 1641 1642 1643 1644
		if (!is_trigger)
		{
			prodesc->nargs = procStruct->pronargs;
			for (i = 0; i < prodesc->nargs; i++)
			{
				typeTup = SearchSysCache(TYPEOID,
B
Bruce Momjian 已提交
1645
						 ObjectIdGetDatum(procStruct->proargtypes.values[i]),
1646 1647 1648 1649 1650
										 0, 0, 0);
				if (!HeapTupleIsValid(typeTup))
				{
					free(prodesc->proname);
					free(prodesc);
1651
					elog(ERROR, "cache lookup failed for type %u",
1652
						 procStruct->proargtypes.values[i]);
1653 1654 1655
				}
				typeStruct = (Form_pg_type) GETSTRUCT(typeTup);

1656
				/* Disallow pseudotype argument */
1657
				if (typeStruct->typtype == TYPTYPE_PSEUDO)
1658 1659 1660
				{
					free(prodesc->proname);
					free(prodesc);
1661 1662
					ereport(ERROR,
							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
B
Bruce Momjian 已提交
1663 1664
							 errmsg("plperl functions cannot take type %s",
						format_type_be(procStruct->proargtypes.values[i]))));
1665 1666
				}

1667
				if (typeStruct->typtype == TYPTYPE_COMPOSITE)
1668
					prodesc->arg_is_rowtype[i] = true;
1669
				else
1670 1671 1672 1673 1674
				{
					prodesc->arg_is_rowtype[i] = false;
					perm_fmgr_info(typeStruct->typoutput,
								   &(prodesc->arg_out_func[i]));
				}
1675 1676 1677 1678

				ReleaseSysCache(typeTup);
			}
		}
1679

1680 1681 1682 1683 1684
		/************************************************************
		 * create the text of the anonymous subroutine.
		 * we do not use a named subroutine so that we can call directly
		 * through the reference.
		 ************************************************************/
1685 1686 1687 1688
		prosrcdatum = SysCacheGetAttr(PROCOID, procTup,
									  Anum_pg_proc_prosrc, &isnull);
		if (isnull)
			elog(ERROR, "null prosrc");
1689
		proc_source = DatumGetCString(DirectFunctionCall1(textout,
1690
														  prosrcdatum));
1691 1692

		/************************************************************
1693
		 * Create the procedure in the interpreter
1694
		 ************************************************************/
1695 1696 1697

		check_interp(prodesc->lanpltrusted);

1698 1699 1700
		prodesc->reference = plperl_create_sub(prodesc->proname,
											   proc_source,
											   prodesc->lanpltrusted);
1701 1702 1703

		restore_context(oldcontext);

1704
		pfree(proc_source);
B
Bruce Momjian 已提交
1705
		if (!prodesc->reference)	/* can this happen? */
1706 1707 1708
		{
			free(prodesc->proname);
			free(prodesc);
1709
			elog(ERROR, "could not create internal procedure \"%s\"",
1710
				 internal_proname);
1711 1712
		}

1713 1714 1715
		hash_entry = hash_search(plperl_proc_hash, internal_proname,
								 HASH_ENTER, &found);
		hash_entry->proc_data = prodesc;
1716 1717
	}

1718
	ReleaseSysCache(procTup);
1719

1720 1721
	return prodesc;
}
1722 1723


1724 1725
/* Build a hash from all attributes of a given tuple. */

B
Bruce Momjian 已提交
1726
static SV  *
1727
plperl_hash_from_tuple(HeapTuple tuple, TupleDesc tupdesc)
1728
{
1729
	HV		   *hv;
1730
	int			i;
1731

1732
	hv = newHV();
1733 1734 1735

	for (i = 0; i < tupdesc->natts; i++)
	{
1736 1737 1738 1739 1740 1741 1742
		Datum		attr;
		bool		isnull;
		char	   *attname;
		char	   *outputstr;
		Oid			typoutput;
		bool		typisvarlena;

1743 1744 1745
		if (tupdesc->attrs[i]->attisdropped)
			continue;

1746
		attname = NameStr(tupdesc->attrs[i]->attname);
1747 1748
		attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);

B
Bruce Momjian 已提交
1749 1750
		if (isnull)
		{
1751
			/* Store (attname => undef) and move on. */
1752
			hv_store_string(hv, attname, newSV(0));
1753 1754 1755
			continue;
		}

1756 1757
		/* XXX should have a way to cache these lookups */
		getTypeOutputInfo(tupdesc->attrs[i]->atttypid,
1758
						  &typoutput, &typisvarlena);
1759

1760
		outputstr = OidOutputFunctionCall(typoutput, attr);
1761

1762
		hv_store_string(hv, attname, newSVstring(outputstr));
1763 1764

		pfree(outputstr);
1765
	}
1766

1767
	return newRV_noinc((SV *) hv);
1768
}
1769 1770 1771 1772 1773 1774 1775


HV *
plperl_spi_exec(char *query, int limit)
{
	HV		   *ret_hv;

1776
	/*
B
Bruce Momjian 已提交
1777 1778
	 * Execute the query inside a sub-transaction, so we can cope with errors
	 * sanely
1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790
	 */
	MemoryContext oldcontext = CurrentMemoryContext;
	ResourceOwner oldowner = CurrentResourceOwner;

	BeginInternalSubTransaction(NULL);
	/* Want to run inside function's memory context */
	MemoryContextSwitchTo(oldcontext);

	PG_TRY();
	{
		int			spi_rv;

1791
		spi_rv = SPI_execute(query, current_call_data->prodesc->fn_readonly,
1792 1793 1794 1795 1796 1797 1798 1799
							 limit);
		ret_hv = plperl_spi_execute_fetch_result(SPI_tuptable, SPI_processed,
												 spi_rv);

		/* Commit the inner transaction, return to outer xact context */
		ReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;
B
Bruce Momjian 已提交
1800

1801
		/*
B
Bruce Momjian 已提交
1802 1803
		 * AtEOSubXact_SPI() should not have popped any SPI context, but just
		 * in case it did, make sure we remain connected.
1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
		 */
		SPI_restore_connection();
	}
	PG_CATCH();
	{
		ErrorData  *edata;

		/* Save error info */
		MemoryContextSwitchTo(oldcontext);
		edata = CopyErrorData();
		FlushErrorState();

		/* Abort the inner transaction */
		RollbackAndReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;

		/*
B
Bruce Momjian 已提交
1822 1823 1824
		 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
		 * have left us in a disconnected state.  We need this hack to return
		 * to connected state.
1825 1826 1827 1828 1829 1830 1831 1832 1833 1834
		 */
		SPI_restore_connection();

		/* Punt the error to Perl */
		croak("%s", edata->message);

		/* Can't get here, but keep compiler quiet */
		return NULL;
	}
	PG_END_TRY();
1835 1836 1837 1838

	return ret_hv;
}

1839

1840
static HV  *
1841 1842
plperl_spi_execute_fetch_result(SPITupleTable *tuptable, int processed,
								int status)
1843 1844 1845 1846 1847
{
	HV		   *result;

	result = newHV();

1848 1849 1850 1851
	hv_store_string(result, "status",
					newSVstring(SPI_result_code_string(status)));
	hv_store_string(result, "processed",
					newSViv(processed));
1852

1853
	if (status > 0 && tuptable)
1854
	{
1855
		AV		   *rows;
1856
		SV		   *row;
1857
		int			i;
1858

1859 1860 1861 1862
		rows = newAV();
		for (i = 0; i < processed; i++)
		{
			row = plperl_hash_from_tuple(tuptable->vals[i], tuptable->tupdesc);
1863
			av_push(rows, row);
1864
		}
1865 1866
		hv_store_string(result, "rows",
						newRV_noinc((SV *) rows));
1867 1868 1869 1870 1871 1872
	}

	SPI_freetuptable(tuptable);

	return result;
}
1873 1874


1875 1876
/*
 * Note: plperl_return_next is called both in Postgres and Perl contexts.
1877
 * We report any errors in Postgres fashion (via ereport).	If called in
1878 1879 1880 1881 1882
 * Perl context, it is SPI.xs's responsibility to catch the error and
 * convert to a Perl error.  We assume (perhaps without adequate justification)
 * that we need not abort the current transaction if the Perl code traps the
 * error.
 */
1883
void
1884
plperl_return_next(SV *sv)
1885
{
1886 1887 1888 1889
	plperl_proc_desc *prodesc;
	FunctionCallInfo fcinfo;
	ReturnSetInfo *rsi;
	MemoryContext old_cxt;
B
Bruce Momjian 已提交
1890
	HeapTuple	tuple;
1891 1892 1893 1894

	if (!sv)
		return;

1895 1896 1897 1898
	prodesc = current_call_data->prodesc;
	fcinfo = current_call_data->fcinfo;
	rsi = (ReturnSetInfo *) fcinfo->resultinfo;

1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910
	if (!prodesc->fn_retisset)
		ereport(ERROR,
				(errcode(ERRCODE_SYNTAX_ERROR),
				 errmsg("cannot use return_next in a non-SETOF function")));

	if (prodesc->fn_retistuple &&
		!(SvOK(sv) && SvTYPE(sv) == SVt_RV && SvTYPE(SvRV(sv)) == SVt_PVHV))
		ereport(ERROR,
				(errcode(ERRCODE_DATATYPE_MISMATCH),
				 errmsg("setof-composite-returning Perl function "
						"must call return_next with reference to hash")));

1911 1912
	if (!current_call_data->ret_tdesc)
	{
B
Bruce Momjian 已提交
1913
		TupleDesc	tupdesc;
1914 1915 1916

		Assert(!current_call_data->tuple_store);
		Assert(!current_call_data->attinmeta);
1917

1918
		/*
B
Bruce Momjian 已提交
1919 1920
		 * This is the first call to return_next in the current PL/Perl
		 * function call, so memoize some lookups
1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934
		 */
		if (prodesc->fn_retistuple)
			(void) get_call_result_type(fcinfo, NULL, &tupdesc);
		else
			tupdesc = rsi->expectedDesc;

		/*
		 * Make sure the tuple_store and ret_tdesc are sufficiently
		 * long-lived.
		 */
		old_cxt = MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);

		current_call_data->ret_tdesc = CreateTupleDescCopy(tupdesc);
		current_call_data->tuple_store =
1935
			tuplestore_begin_heap(true, false, work_mem);
1936 1937 1938 1939 1940
		if (prodesc->fn_retistuple)
		{
			current_call_data->attinmeta =
				TupleDescGetAttInMetadata(current_call_data->ret_tdesc);
		}
1941

1942
		MemoryContextSwitchTo(old_cxt);
B
Bruce Momjian 已提交
1943
	}
1944 1945 1946

	/*
	 * Producing the tuple we want to return requires making plenty of
B
Bruce Momjian 已提交
1947 1948 1949
	 * palloc() allocations that are not cleaned up. Since this function can
	 * be called many times before the current memory context is reset, we
	 * need to do those allocations in a temporary context.
1950 1951
	 */
	if (!current_call_data->tmp_cxt)
1952
	{
1953 1954 1955 1956 1957 1958 1959 1960 1961
		current_call_data->tmp_cxt =
			AllocSetContextCreate(rsi->econtext->ecxt_per_tuple_memory,
								  "PL/Perl return_next temporary cxt",
								  ALLOCSET_DEFAULT_MINSIZE,
								  ALLOCSET_DEFAULT_INITSIZE,
								  ALLOCSET_DEFAULT_MAXSIZE);
	}

	old_cxt = MemoryContextSwitchTo(current_call_data->tmp_cxt);
1962

1963 1964 1965 1966
	if (prodesc->fn_retistuple)
	{
		tuple = plperl_build_tuple_result((HV *) SvRV(sv),
										  current_call_data->attinmeta);
1967 1968 1969
	}
	else
	{
1970 1971
		Datum		ret;
		bool		isNull;
1972

1973
		if (SvOK(sv))
1974
		{
1975 1976 1977 1978 1979 1980 1981 1982 1983
			char	   *val;

			if (prodesc->fn_retisarray && SvROK(sv) &&
				SvTYPE(SvRV(sv)) == SVt_PVAV)
			{
				sv = plperl_convert_to_pg_array(sv);
			}

			val = SvPV(sv, PL_na);
B
Bruce Momjian 已提交
1984

1985 1986
			ret = InputFunctionCall(&prodesc->result_in_func, val,
									prodesc->result_typioparam, -1);
1987 1988
			isNull = false;
		}
1989 1990 1991 1992 1993 1994
		else
		{
			ret = InputFunctionCall(&prodesc->result_in_func, NULL,
									prodesc->result_typioparam, -1);
			isNull = true;
		}
1995

1996
		tuple = heap_form_tuple(current_call_data->ret_tdesc, &ret, &isNull);
1997 1998
	}

1999 2000 2001 2002
	/* Make sure to store the tuple in a long-lived memory context */
	MemoryContextSwitchTo(rsi->econtext->ecxt_per_query_memory);
	tuplestore_puttuple(current_call_data->tuple_store, tuple);
	MemoryContextSwitchTo(old_cxt);
2003

2004
	MemoryContextReset(current_call_data->tmp_cxt);
2005
}
2006 2007 2008 2009 2010


SV *
plperl_spi_query(char *query)
{
B
Bruce Momjian 已提交
2011
	SV		   *cursor;
2012

2013 2014 2015 2016
	/*
	 * Execute the query inside a sub-transaction, so we can cope with errors
	 * sanely
	 */
2017 2018 2019 2020
	MemoryContext oldcontext = CurrentMemoryContext;
	ResourceOwner oldowner = CurrentResourceOwner;

	BeginInternalSubTransaction(NULL);
2021
	/* Want to run inside function's memory context */
2022 2023 2024 2025
	MemoryContextSwitchTo(oldcontext);

	PG_TRY();
	{
B
Bruce Momjian 已提交
2026
		void	   *plan;
A
 
Andrew Dunstan 已提交
2027
		Portal		portal;
2028

2029
		/* Create a cursor for the query */
2030
		plan = SPI_prepare(query, 0, NULL);
B
Bruce Momjian 已提交
2031
		if (plan == NULL)
A
 
Andrew Dunstan 已提交
2032
			elog(ERROR, "SPI_prepare() failed:%s",
B
Bruce Momjian 已提交
2033
				 SPI_result_code_string(SPI_result));
A
 
Andrew Dunstan 已提交
2034 2035

		portal = SPI_cursor_open(NULL, plan, NULL, NULL, false);
B
Bruce Momjian 已提交
2036 2037
		SPI_freeplan(plan);
		if (portal == NULL)
A
 
Andrew Dunstan 已提交
2038
			elog(ERROR, "SPI_cursor_open() failed:%s",
B
Bruce Momjian 已提交
2039
				 SPI_result_code_string(SPI_result));
2040
		cursor = newSVstring(portal->name);
2041

2042
		/* Commit the inner transaction, return to outer xact context */
2043 2044 2045
		ReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;
2046 2047 2048 2049 2050

		/*
		 * AtEOSubXact_SPI() should not have popped any SPI context, but just
		 * in case it did, make sure we remain connected.
		 */
2051 2052 2053 2054 2055 2056
		SPI_restore_connection();
	}
	PG_CATCH();
	{
		ErrorData  *edata;

2057
		/* Save error info */
2058 2059 2060 2061
		MemoryContextSwitchTo(oldcontext);
		edata = CopyErrorData();
		FlushErrorState();

2062
		/* Abort the inner transaction */
2063 2064 2065 2066
		RollbackAndReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;

2067 2068 2069 2070 2071
		/*
		 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
		 * have left us in a disconnected state.  We need this hack to return
		 * to connected state.
		 */
2072
		SPI_restore_connection();
2073 2074

		/* Punt the error to Perl */
2075
		croak("%s", edata->message);
2076 2077

		/* Can't get here, but keep compiler quiet */
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088
		return NULL;
	}
	PG_END_TRY();

	return cursor;
}


SV *
plperl_spi_fetchrow(char *cursor)
{
2089 2090 2091 2092 2093 2094 2095 2096
	SV		   *row;

	/*
	 * Execute the FETCH inside a sub-transaction, so we can cope with errors
	 * sanely
	 */
	MemoryContext oldcontext = CurrentMemoryContext;
	ResourceOwner oldowner = CurrentResourceOwner;
2097

2098 2099 2100
	BeginInternalSubTransaction(NULL);
	/* Want to run inside function's memory context */
	MemoryContextSwitchTo(oldcontext);
2101

2102
	PG_TRY();
B
Bruce Momjian 已提交
2103
	{
2104 2105 2106
		Portal		p = SPI_cursor_find(cursor);

		if (!p)
A
 
Andrew Dunstan 已提交
2107 2108 2109
		{
			row = &PL_sv_undef;
		}
2110 2111 2112 2113 2114 2115
		else
		{
			SPI_cursor_fetch(p, true, 1);
			if (SPI_processed == 0)
			{
				SPI_cursor_close(p);
A
 
Andrew Dunstan 已提交
2116
				row = &PL_sv_undef;
2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135
			}
			else
			{
				row = plperl_hash_from_tuple(SPI_tuptable->vals[0],
											 SPI_tuptable->tupdesc);
			}
			SPI_freetuptable(SPI_tuptable);
		}

		/* Commit the inner transaction, return to outer xact context */
		ReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;

		/*
		 * AtEOSubXact_SPI() should not have popped any SPI context, but just
		 * in case it did, make sure we remain connected.
		 */
		SPI_restore_connection();
2136
	}
2137 2138 2139
	PG_CATCH();
	{
		ErrorData  *edata;
2140

2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
		/* Save error info */
		MemoryContextSwitchTo(oldcontext);
		edata = CopyErrorData();
		FlushErrorState();

		/* Abort the inner transaction */
		RollbackAndReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;

		/*
		 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
		 * have left us in a disconnected state.  We need this hack to return
		 * to connected state.
		 */
		SPI_restore_connection();

		/* Punt the error to Perl */
		croak("%s", edata->message);

		/* Can't get here, but keep compiler quiet */
		return NULL;
	}
	PG_END_TRY();
2165 2166 2167

	return row;
}
A
 
Andrew Dunstan 已提交
2168 2169 2170 2171

void
plperl_spi_cursor_close(char *cursor)
{
B
Bruce Momjian 已提交
2172 2173
	Portal		p = SPI_cursor_find(cursor);

A
 
Andrew Dunstan 已提交
2174 2175 2176 2177 2178
	if (p)
		SPI_cursor_close(p);
}

SV *
B
Bruce Momjian 已提交
2179
plperl_spi_prepare(char *query, int argc, SV **argv)
A
 
Andrew Dunstan 已提交
2180 2181
{
	plperl_query_desc *qdesc;
2182
	plperl_query_entry *hash_entry;
B
Bruce Momjian 已提交
2183
	bool		found;
A
 
Andrew Dunstan 已提交
2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
	void	   *plan;
	int			i;

	MemoryContext oldcontext = CurrentMemoryContext;
	ResourceOwner oldowner = CurrentResourceOwner;

	BeginInternalSubTransaction(NULL);
	MemoryContextSwitchTo(oldcontext);

	/************************************************************
	 * Allocate the new querydesc structure
	 ************************************************************/
	qdesc = (plperl_query_desc *) malloc(sizeof(plperl_query_desc));
	MemSet(qdesc, 0, sizeof(plperl_query_desc));
B
Bruce Momjian 已提交
2198 2199 2200 2201 2202
	snprintf(qdesc->qname, sizeof(qdesc->qname), "%lx", (long) qdesc);
	qdesc->nargs = argc;
	qdesc->argtypes = (Oid *) malloc(argc * sizeof(Oid));
	qdesc->arginfuncs = (FmgrInfo *) malloc(argc * sizeof(FmgrInfo));
	qdesc->argtypioparams = (Oid *) malloc(argc * sizeof(Oid));
A
 
Andrew Dunstan 已提交
2203 2204 2205 2206

	PG_TRY();
	{
		/************************************************************
2207 2208 2209
		 * Resolve argument type names and then look them up by oid
		 * in the system cache, and remember the required information
		 * for input conversion.
A
 
Andrew Dunstan 已提交
2210 2211 2212
		 ************************************************************/
		for (i = 0; i < argc; i++)
		{
B
Bruce Momjian 已提交
2213 2214 2215 2216
			Oid			typId,
						typInput,
						typIOParam;
			int32		typmod;
2217 2218 2219 2220 2221 2222

			parseTypeString(SvPV(argv[i], PL_na), &typId, &typmod);

			getTypeInputInfo(typId, &typInput, &typIOParam);

			qdesc->argtypes[i] = typId;
2223
			perm_fmgr_info(typInput, &(qdesc->arginfuncs[i]));
2224
			qdesc->argtypioparams[i] = typIOParam;
A
 
Andrew Dunstan 已提交
2225 2226 2227 2228 2229 2230 2231 2232 2233
		}

		/************************************************************
		 * Prepare the plan and check for errors
		 ************************************************************/
		plan = SPI_prepare(query, argc, qdesc->argtypes);

		if (plan == NULL)
			elog(ERROR, "SPI_prepare() failed:%s",
B
Bruce Momjian 已提交
2234
				 SPI_result_code_string(SPI_result));
A
 
Andrew Dunstan 已提交
2235 2236 2237 2238 2239 2240 2241

		/************************************************************
		 * Save the plan into permanent memory (right now it's in the
		 * SPI procCxt, which will go away at function end).
		 ************************************************************/
		qdesc->plan = SPI_saveplan(plan);
		if (qdesc->plan == NULL)
B
Bruce Momjian 已提交
2242 2243
			elog(ERROR, "SPI_saveplan() failed: %s",
				 SPI_result_code_string(SPI_result));
A
 
Andrew Dunstan 已提交
2244 2245 2246 2247 2248 2249 2250 2251

		/* Release the procCxt copy to avoid within-function memory leak */
		SPI_freeplan(plan);

		/* Commit the inner transaction, return to outer xact context */
		ReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;
B
Bruce Momjian 已提交
2252

A
 
Andrew Dunstan 已提交
2253
		/*
B
Bruce Momjian 已提交
2254 2255
		 * AtEOSubXact_SPI() should not have popped any SPI context, but just
		 * in case it did, make sure we remain connected.
A
 
Andrew Dunstan 已提交
2256 2257 2258 2259 2260 2261
		 */
		SPI_restore_connection();
	}
	PG_CATCH();
	{
		ErrorData  *edata;
B
Bruce Momjian 已提交
2262 2263 2264 2265

		free(qdesc->argtypes);
		free(qdesc->arginfuncs);
		free(qdesc->argtypioparams);
A
 
Andrew Dunstan 已提交
2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278
		free(qdesc);

		/* Save error info */
		MemoryContextSwitchTo(oldcontext);
		edata = CopyErrorData();
		FlushErrorState();

		/* Abort the inner transaction */
		RollbackAndReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;

		/*
B
Bruce Momjian 已提交
2279 2280 2281
		 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
		 * have left us in a disconnected state.  We need this hack to return
		 * to connected state.
A
 
Andrew Dunstan 已提交
2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296
		 */
		SPI_restore_connection();

		/* Punt the error to Perl */
		croak("%s", edata->message);

		/* Can't get here, but keep compiler quiet */
		return NULL;
	}
	PG_END_TRY();

	/************************************************************
	 * Insert a hashtable entry for the plan and return
	 * the key to the caller.
	 ************************************************************/
2297 2298

	hash_entry = hash_search(plperl_query_hash, qdesc->qname,
B
Bruce Momjian 已提交
2299
							 HASH_ENTER, &found);
2300
	hash_entry->query_data = qdesc;
A
 
Andrew Dunstan 已提交
2301

2302
	return newSVstring(qdesc->qname);
B
Bruce Momjian 已提交
2303
}
A
 
Andrew Dunstan 已提交
2304 2305

HV *
B
Bruce Momjian 已提交
2306
plperl_spi_exec_prepared(char *query, HV *attr, int argc, SV **argv)
A
 
Andrew Dunstan 已提交
2307 2308
{
	HV		   *ret_hv;
B
Bruce Momjian 已提交
2309 2310 2311 2312 2313
	SV		  **sv;
	int			i,
				limit,
				spi_rv;
	char	   *nulls;
A
 
Andrew Dunstan 已提交
2314 2315
	Datum	   *argvalues;
	plperl_query_desc *qdesc;
2316
	plperl_query_entry *hash_entry;
A
 
Andrew Dunstan 已提交
2317 2318

	/*
B
Bruce Momjian 已提交
2319 2320
	 * Execute the query inside a sub-transaction, so we can cope with errors
	 * sanely
A
 
Andrew Dunstan 已提交
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
	 */
	MemoryContext oldcontext = CurrentMemoryContext;
	ResourceOwner oldowner = CurrentResourceOwner;

	BeginInternalSubTransaction(NULL);
	/* Want to run inside function's memory context */
	MemoryContextSwitchTo(oldcontext);

	PG_TRY();
	{
		/************************************************************
		 * Fetch the saved plan descriptor, see if it's o.k.
		 ************************************************************/
2334 2335

		hash_entry = hash_search(plperl_query_hash, query,
B
Bruce Momjian 已提交
2336
								 HASH_FIND, NULL);
2337
		if (hash_entry == NULL)
A
 
Andrew Dunstan 已提交
2338 2339
			elog(ERROR, "spi_exec_prepared: Invalid prepared query passed");

2340 2341
		qdesc = hash_entry->query_data;

B
Bruce Momjian 已提交
2342
		if (qdesc == NULL)
A
 
Andrew Dunstan 已提交
2343 2344
			elog(ERROR, "spi_exec_prepared: panic - plperl_query_hash value vanished");

B
Bruce Momjian 已提交
2345 2346 2347 2348
		if (qdesc->nargs != argc)
			elog(ERROR, "spi_exec_prepared: expected %d argument(s), %d passed",
				 qdesc->nargs, argc);

A
 
Andrew Dunstan 已提交
2349 2350 2351 2352
		/************************************************************
		 * Parse eventual attributes
		 ************************************************************/
		limit = 0;
B
Bruce Momjian 已提交
2353
		if (attr != NULL)
A
 
Andrew Dunstan 已提交
2354
		{
2355
			sv = hv_fetch_string(attr, "limit");
B
Bruce Momjian 已提交
2356 2357
			if (*sv && SvIOK(*sv))
				limit = SvIV(*sv);
A
 
Andrew Dunstan 已提交
2358 2359 2360 2361
		}
		/************************************************************
		 * Set up arguments
		 ************************************************************/
B
Bruce Momjian 已提交
2362
		if (argc > 0)
A
 
Andrew Dunstan 已提交
2363
		{
2364
			nulls = (char *) palloc(argc);
A
 
Andrew Dunstan 已提交
2365
			argvalues = (Datum *) palloc(argc * sizeof(Datum));
B
Bruce Momjian 已提交
2366 2367
		}
		else
A
 
Andrew Dunstan 已提交
2368 2369 2370 2371 2372
		{
			nulls = NULL;
			argvalues = NULL;
		}

B
Bruce Momjian 已提交
2373
		for (i = 0; i < argc; i++)
A
 
Andrew Dunstan 已提交
2374
		{
2375
			if (SvOK(argv[i]))
A
 
Andrew Dunstan 已提交
2376
			{
2377 2378 2379 2380
				argvalues[i] = InputFunctionCall(&qdesc->arginfuncs[i],
												 SvPV(argv[i], PL_na),
												 qdesc->argtypioparams[i],
												 -1);
A
 
Andrew Dunstan 已提交
2381
				nulls[i] = ' ';
B
Bruce Momjian 已提交
2382 2383
			}
			else
A
 
Andrew Dunstan 已提交
2384
			{
2385 2386 2387 2388
				argvalues[i] = InputFunctionCall(&qdesc->arginfuncs[i],
												 NULL,
												 qdesc->argtypioparams[i],
												 -1);
A
 
Andrew Dunstan 已提交
2389 2390 2391 2392 2393 2394 2395
				nulls[i] = 'n';
			}
		}

		/************************************************************
		 * go
		 ************************************************************/
B
Bruce Momjian 已提交
2396
		spi_rv = SPI_execute_plan(qdesc->plan, argvalues, nulls,
A
 
Andrew Dunstan 已提交
2397 2398 2399
							 current_call_data->prodesc->fn_readonly, limit);
		ret_hv = plperl_spi_execute_fetch_result(SPI_tuptable, SPI_processed,
												 spi_rv);
B
Bruce Momjian 已提交
2400
		if (argc > 0)
A
 
Andrew Dunstan 已提交
2401
		{
B
Bruce Momjian 已提交
2402 2403
			pfree(argvalues);
			pfree(nulls);
A
 
Andrew Dunstan 已提交
2404 2405 2406 2407 2408 2409
		}

		/* Commit the inner transaction, return to outer xact context */
		ReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;
B
Bruce Momjian 已提交
2410

A
 
Andrew Dunstan 已提交
2411
		/*
B
Bruce Momjian 已提交
2412 2413
		 * AtEOSubXact_SPI() should not have popped any SPI context, but just
		 * in case it did, make sure we remain connected.
A
 
Andrew Dunstan 已提交
2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
		 */
		SPI_restore_connection();
	}
	PG_CATCH();
	{
		ErrorData  *edata;

		/* Save error info */
		MemoryContextSwitchTo(oldcontext);
		edata = CopyErrorData();
		FlushErrorState();

		/* Abort the inner transaction */
		RollbackAndReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;

		/*
B
Bruce Momjian 已提交
2432 2433 2434
		 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
		 * have left us in a disconnected state.  We need this hack to return
		 * to connected state.
A
 
Andrew Dunstan 已提交
2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449
		 */
		SPI_restore_connection();

		/* Punt the error to Perl */
		croak("%s", edata->message);

		/* Can't get here, but keep compiler quiet */
		return NULL;
	}
	PG_END_TRY();

	return ret_hv;
}

SV *
B
Bruce Momjian 已提交
2450
plperl_spi_query_prepared(char *query, int argc, SV **argv)
A
 
Andrew Dunstan 已提交
2451
{
B
Bruce Momjian 已提交
2452 2453
	int			i;
	char	   *nulls;
A
 
Andrew Dunstan 已提交
2454 2455
	Datum	   *argvalues;
	plperl_query_desc *qdesc;
2456
	plperl_query_entry *hash_entry;
B
Bruce Momjian 已提交
2457 2458
	SV		   *cursor;
	Portal		portal = NULL;
A
 
Andrew Dunstan 已提交
2459 2460

	/*
B
Bruce Momjian 已提交
2461 2462
	 * Execute the query inside a sub-transaction, so we can cope with errors
	 * sanely
A
 
Andrew Dunstan 已提交
2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475
	 */
	MemoryContext oldcontext = CurrentMemoryContext;
	ResourceOwner oldowner = CurrentResourceOwner;

	BeginInternalSubTransaction(NULL);
	/* Want to run inside function's memory context */
	MemoryContextSwitchTo(oldcontext);

	PG_TRY();
	{
		/************************************************************
		 * Fetch the saved plan descriptor, see if it's o.k.
		 ************************************************************/
2476
		hash_entry = hash_search(plperl_query_hash, query,
B
Bruce Momjian 已提交
2477
								 HASH_FIND, NULL);
2478 2479 2480 2481
		if (hash_entry == NULL)
			elog(ERROR, "spi_exec_prepared: Invalid prepared query passed");

		qdesc = hash_entry->query_data;
A
 
Andrew Dunstan 已提交
2482

B
Bruce Momjian 已提交
2483
		if (qdesc == NULL)
A
 
Andrew Dunstan 已提交
2484 2485
			elog(ERROR, "spi_query_prepared: panic - plperl_query_hash value vanished");

B
Bruce Momjian 已提交
2486 2487 2488 2489
		if (qdesc->nargs != argc)
			elog(ERROR, "spi_query_prepared: expected %d argument(s), %d passed",
				 qdesc->nargs, argc);

A
 
Andrew Dunstan 已提交
2490 2491 2492
		/************************************************************
		 * Set up arguments
		 ************************************************************/
B
Bruce Momjian 已提交
2493
		if (argc > 0)
A
 
Andrew Dunstan 已提交
2494
		{
2495
			nulls = (char *) palloc(argc);
A
 
Andrew Dunstan 已提交
2496
			argvalues = (Datum *) palloc(argc * sizeof(Datum));
B
Bruce Momjian 已提交
2497 2498
		}
		else
A
 
Andrew Dunstan 已提交
2499 2500 2501 2502 2503
		{
			nulls = NULL;
			argvalues = NULL;
		}

B
Bruce Momjian 已提交
2504
		for (i = 0; i < argc; i++)
A
 
Andrew Dunstan 已提交
2505
		{
2506
			if (SvOK(argv[i]))
A
 
Andrew Dunstan 已提交
2507
			{
2508 2509 2510 2511
				argvalues[i] = InputFunctionCall(&qdesc->arginfuncs[i],
												 SvPV(argv[i], PL_na),
												 qdesc->argtypioparams[i],
												 -1);
A
 
Andrew Dunstan 已提交
2512
				nulls[i] = ' ';
B
Bruce Momjian 已提交
2513 2514
			}
			else
A
 
Andrew Dunstan 已提交
2515
			{
2516 2517 2518 2519
				argvalues[i] = InputFunctionCall(&qdesc->arginfuncs[i],
												 NULL,
												 qdesc->argtypioparams[i],
												 -1);
A
 
Andrew Dunstan 已提交
2520 2521 2522 2523 2524 2525 2526
				nulls[i] = 'n';
			}
		}

		/************************************************************
		 * go
		 ************************************************************/
B
Bruce Momjian 已提交
2527 2528 2529
		portal = SPI_cursor_open(NULL, qdesc->plan, argvalues, nulls,
								 current_call_data->prodesc->fn_readonly);
		if (argc > 0)
A
 
Andrew Dunstan 已提交
2530
		{
B
Bruce Momjian 已提交
2531 2532
			pfree(argvalues);
			pfree(nulls);
A
 
Andrew Dunstan 已提交
2533
		}
B
Bruce Momjian 已提交
2534
		if (portal == NULL)
A
 
Andrew Dunstan 已提交
2535
			elog(ERROR, "SPI_cursor_open() failed:%s",
B
Bruce Momjian 已提交
2536
				 SPI_result_code_string(SPI_result));
A
 
Andrew Dunstan 已提交
2537

2538
		cursor = newSVstring(portal->name);
A
 
Andrew Dunstan 已提交
2539 2540 2541 2542 2543

		/* Commit the inner transaction, return to outer xact context */
		ReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;
B
Bruce Momjian 已提交
2544

A
 
Andrew Dunstan 已提交
2545
		/*
B
Bruce Momjian 已提交
2546 2547
		 * AtEOSubXact_SPI() should not have popped any SPI context, but just
		 * in case it did, make sure we remain connected.
A
 
Andrew Dunstan 已提交
2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565
		 */
		SPI_restore_connection();
	}
	PG_CATCH();
	{
		ErrorData  *edata;

		/* Save error info */
		MemoryContextSwitchTo(oldcontext);
		edata = CopyErrorData();
		FlushErrorState();

		/* Abort the inner transaction */
		RollbackAndReleaseCurrentSubTransaction();
		MemoryContextSwitchTo(oldcontext);
		CurrentResourceOwner = oldowner;

		/*
B
Bruce Momjian 已提交
2566 2567 2568
		 * If AtEOSubXact_SPI() popped any SPI context of the subxact, it will
		 * have left us in a disconnected state.  We need this hack to return
		 * to connected state.
A
 
Andrew Dunstan 已提交
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
		 */
		SPI_restore_connection();

		/* Punt the error to Perl */
		croak("%s", edata->message);

		/* Can't get here, but keep compiler quiet */
		return NULL;
	}
	PG_END_TRY();

	return cursor;
}

void
plperl_spi_freeplan(char *query)
{
B
Bruce Momjian 已提交
2586
	void	   *plan;
A
 
Andrew Dunstan 已提交
2587
	plperl_query_desc *qdesc;
2588
	plperl_query_entry *hash_entry;
A
 
Andrew Dunstan 已提交
2589

2590
	hash_entry = hash_search(plperl_query_hash, query,
B
Bruce Momjian 已提交
2591
							 HASH_FIND, NULL);
2592 2593 2594 2595
	if (hash_entry == NULL)
		elog(ERROR, "spi_exec_prepared: Invalid prepared query passed");

	qdesc = hash_entry->query_data;
A
 
Andrew Dunstan 已提交
2596

B
Bruce Momjian 已提交
2597
	if (qdesc == NULL)
A
 
Andrew Dunstan 已提交
2598 2599 2600
		elog(ERROR, "spi_exec_freeplan: panic - plperl_query_hash value vanished");

	/*
B
Bruce Momjian 已提交
2601 2602 2603
	 * free all memory before SPI_freeplan, so if it dies, nothing will be
	 * left over
	 */
2604 2605
	hash_search(plperl_query_hash, query,
				HASH_REMOVE, NULL);
2606

B
Bruce Momjian 已提交
2607 2608 2609 2610
	plan = qdesc->plan;
	free(qdesc->argtypes);
	free(qdesc->arginfuncs);
	free(qdesc->argtypioparams);
A
 
Andrew Dunstan 已提交
2611 2612
	free(qdesc);

B
Bruce Momjian 已提交
2613
	SPI_freeplan(plan);
A
 
Andrew Dunstan 已提交
2614
}
2615 2616 2617 2618 2619

/*
 * Create a new SV from a string assumed to be in the current database's
 * encoding.
 */
B
Bruce Momjian 已提交
2620
static SV  *
2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639
newSVstring(const char *str)
{
	SV		   *sv;

	sv = newSVpv(str, 0);
#if PERL_BCDVERSION >= 0x5006000L
	if (GetDatabaseEncoding() == PG_UTF8)
		SvUTF8_on(sv);
#endif
	return sv;
}

/*
 * Store an SV into a hash table under a key that is a string assumed to be
 * in the current database's encoding.
 */
static SV **
hv_store_string(HV *hv, const char *key, SV *val)
{
B
Bruce Momjian 已提交
2640
	int32		klen = strlen(key);
2641 2642

	/*
B
Bruce Momjian 已提交
2643
	 * This seems nowhere documented, but under Perl 5.8.0 and up, hv_store()
2644 2645
	 * recognizes a negative klen parameter as meaning a UTF-8 encoded key. It
	 * does not appear that hashes track UTF-8-ness of keys at all in Perl
B
Bruce Momjian 已提交
2646
	 * 5.6.
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661
	 */
#if PERL_BCDVERSION >= 0x5008000L
	if (GetDatabaseEncoding() == PG_UTF8)
		klen = -klen;
#endif
	return hv_store(hv, key, klen, val, 0);
}

/*
 * Fetch an SV from a hash table under a key that is a string assumed to be
 * in the current database's encoding.
 */
static SV **
hv_fetch_string(HV *hv, const char *key)
{
B
Bruce Momjian 已提交
2662
	int32		klen = strlen(key);
2663 2664 2665 2666 2667 2668 2669 2670

	/* See notes in hv_store_string */
#if PERL_BCDVERSION >= 0x5008000L
	if (GetDatabaseEncoding() == PG_UTF8)
		klen = -klen;
#endif
	return hv_fetch(hv, key, klen, 0);
}