plperl.c 23.9 KB
Newer Older
1 2 3 4 5 6
/**********************************************************************
 * plperl.c - perl as a procedural language for PostgreSQL
 *
 * IDENTIFICATION
 *
 *	  This software is copyrighted by Mark Hollomon
7
 *	 but is shameless cribbed from pltcl.c by Jan Weick.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 *	  The author hereby grants permission  to  use,  copy,	modify,
 *	  distribute,  and	license this software and its documentation
 *	  for any purpose, provided that existing copyright notices are
 *	  retained	in	all  copies  and  that	this notice is included
 *	  verbatim in any distributions. No written agreement, license,
 *	  or  royalty  fee	is required for any of the authorized uses.
 *	  Modifications to this software may be  copyrighted  by  their
 *	  author  and  need  not  follow  the licensing terms described
 *	  here, provided that the new terms are  clearly  indicated  on
 *	  the first page of each file where they apply.
 *
 *	  IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY
 *	  PARTY  FOR  DIRECT,	INDIRECT,	SPECIAL,   INCIDENTAL,	 OR
 *	  CONSEQUENTIAL   DAMAGES  ARISING	OUT  OF  THE  USE  OF  THIS
 *	  SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN
 *	  IF  THE  AUTHOR  HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
 *	  DAMAGE.
 *
 *	  THE  AUTHOR  AND	DISTRIBUTORS  SPECIFICALLY	 DISCLAIM	ANY
 *	  WARRANTIES,  INCLUDING,  BUT	NOT  LIMITED  TO,  THE	IMPLIED
 *	  WARRANTIES  OF  MERCHANTABILITY,	FITNESS  FOR  A  PARTICULAR
 *	  PURPOSE,	AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON
 *	  AN "AS IS" BASIS, AND THE AUTHOR	AND  DISTRIBUTORS  HAVE  NO
 *	  OBLIGATION   TO	PROVIDE   MAINTENANCE,	 SUPPORT,  UPDATES,
 *	  ENHANCEMENTS, OR MODIFICATIONS.
 *
35
 * IDENTIFICATION
P
 
PostgreSQL Daemon 已提交
36
 *	  $PostgreSQL: pgsql/src/pl/plperl/plperl.c,v 1.41 2003/11/29 19:52:12 pgsql Exp $
37
 *
38 39
 **********************************************************************/

40
#include "postgres.h"
41 42 43 44 45 46 47 48 49 50 51 52 53

/* system stuff */
#include <unistd.h>
#include <fcntl.h>
#include <setjmp.h>

/* postgreSQL stuff */
#include "executor/spi.h"
#include "commands/trigger.h"
#include "fmgr.h"
#include "access/heapam.h"
#include "tcop/tcopprot.h"
#include "utils/syscache.h"
54
#include "catalog/pg_language.h"
55 56 57 58
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"

/* perl stuff */
B
Bruce Momjian 已提交
59 60
#include "EXTERN.h"
#include "perl.h"
61
#include "XSUB.h"
62
#include "ppport.h"
63

64 65 66 67 68 69
/* just in case these symbols aren't provided */
#ifndef pTHX_
#define pTHX_
#define pTHX void
#endif

70 71 72 73 74 75 76

/**********************************************************************
 * The information we cache about loaded procedures
 **********************************************************************/
typedef struct plperl_proc_desc
{
	char	   *proname;
77 78 79
	TransactionId fn_xmin;
	CommandId	fn_cmin;
	bool		lanpltrusted;
80 81 82 83 84 85
	FmgrInfo	result_in_func;
	Oid			result_in_elem;
	int			nargs;
	FmgrInfo	arg_out_func[FUNC_MAX_ARGS];
	Oid			arg_out_elem[FUNC_MAX_ARGS];
	int			arg_is_rel[FUNC_MAX_ARGS];
86
	SV		   *reference;
87
}	plperl_proc_desc;
88 89 90 91 92 93


/**********************************************************************
 * Global data
 **********************************************************************/
static int	plperl_firstcall = 1;
94
static PerlInterpreter *plperl_interp = NULL;
95 96
static HV  *plperl_proc_hash = NULL;

97 98 99 100
/**********************************************************************
 * Forward declarations
 **********************************************************************/
static void plperl_init_all(void);
101
static void plperl_init_interp(void);
102

B
Bruce Momjian 已提交
103
Datum		plperl_call_handler(PG_FUNCTION_ARGS);
104
void		plperl_init(void);
105

106
static Datum plperl_func_handler(PG_FUNCTION_ARGS);
107

108 109
static plperl_proc_desc *compile_plperl_function(Oid fn_oid, bool is_trigger);

110
static SV  *plperl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc);
111
static void plperl_init_shared_libs(pTHX);
112 113


114 115 116 117 118 119 120
/*
 * 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,
121 122 123
 * 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.
124 125 126 127
 */
static void
perm_fmgr_info(Oid functionId, FmgrInfo *finfo)
{
128
	fmgr_info_cxt(functionId, finfo, TopMemoryContext);
129 130
}

131
/**********************************************************************
132 133 134 135 136
 * plperl_init()			- Initialize everything that can be
 *							  safely initialized during postmaster
 *							  startup.
 *
 * DO NOT make this static --- it has to be callable by preload
137
 **********************************************************************/
138 139
void
plperl_init(void)
140 141 142 143 144 145 146
{
	/************************************************************
	 * Do initialization only once
	 ************************************************************/
	if (!plperl_firstcall)
		return;

147 148 149 150 151 152 153 154 155
	/************************************************************
	 * Free the proc hash table
	 ************************************************************/
	if (plperl_proc_hash != NULL)
	{
		hv_undef(plperl_proc_hash);
		SvREFCNT_dec((SV *) plperl_proc_hash);
		plperl_proc_hash = NULL;
	}
156 157

	/************************************************************
158
	 * Destroy the existing Perl interpreter
159
	 ************************************************************/
160
	if (plperl_interp != NULL)
161
	{
162 163 164
		perl_destruct(plperl_interp);
		perl_free(plperl_interp);
		plperl_interp = NULL;
165 166 167
	}

	/************************************************************
168
	 * Now recreate a new Perl interpreter
169
	 ************************************************************/
170
	plperl_init_interp();
171 172 173 174

	plperl_firstcall = 0;
}

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
/**********************************************************************
 * plperl_init_all()		- Initialize all
 **********************************************************************/
static void
plperl_init_all(void)
{

	/************************************************************
	 * Execute postmaster-startup safe initialization
	 ************************************************************/
	if (plperl_firstcall)
		plperl_init();

	/************************************************************
	 * Any other initialization that must be done each time a new
	 * backend starts -- currently none
	 ************************************************************/

}

195 196

/**********************************************************************
197
 * plperl_init_interp() - Create the Perl interpreter
198 199
 **********************************************************************/
static void
200
plperl_init_interp(void)
201 202
{

B
Bruce Momjian 已提交
203
	char	   *embedding[3] = {
B
Bruce Momjian 已提交
204 205 206
		"", "-e",

		/*
207
		 * no commas between the next 5 please. They are supposed to be
B
Bruce Momjian 已提交
208
		 * one string
B
Bruce Momjian 已提交
209 210
		 */
		"require Safe; SPI::bootstrap();"
211
		"sub ::mksafefunc { my $x = new Safe; $x->permit_only(':default');$x->permit(':base_math');"
212
		"$x->share(qw[&elog &DEBUG &LOG &INFO &NOTICE &WARNING &ERROR]);"
B
Bruce Momjian 已提交
213
		" return $x->reval(qq[sub { $_[0] }]); }"
214
		"sub ::mkunsafefunc {return eval(qq[ sub { $_[0] } ]); }"
B
Bruce Momjian 已提交
215
	};
216

217 218
	plperl_interp = perl_alloc();
	if (!plperl_interp)
219
		elog(ERROR, "could not allocate perl interpreter");
220

221 222 223
	perl_construct(plperl_interp);
	perl_parse(plperl_interp, plperl_init_shared_libs, 3, embedding, NULL);
	perl_run(plperl_interp);
224 225 226

	/************************************************************
	 * Initialize the proc and query hash tables
227
	 ************************************************************/
228
	plperl_proc_hash = newHV();
229 230 231 232 233 234 235 236 237 238 239

}


/**********************************************************************
 * plperl_call_handler		- This is the only visible function
 *				  of the PL interpreter. The PostgreSQL
 *				  function manager and trigger manager
 *				  call this function for execution of
 *				  perl procedures.
 **********************************************************************/
240
PG_FUNCTION_INFO_V1(plperl_call_handler);
241 242 243

/* keep non-static */
Datum
244
plperl_call_handler(PG_FUNCTION_ARGS)
245 246 247 248
{
	Datum		retval;

	/************************************************************
249
	 * Initialize interpreter
250
	 ************************************************************/
251
	plperl_init_all();
252 253 254 255 256

	/************************************************************
	 * Connect to SPI manager
	 ************************************************************/
	if (SPI_connect() != SPI_OK_CONNECT)
257
		elog(ERROR, "could not connect to SPI manager");
258 259 260 261 262

	/************************************************************
	 * Determine if called as function or trigger and
	 * call appropriate subhandler
	 ************************************************************/
263
	if (CALLED_AS_TRIGGER(fcinfo))
264
	{
265 266 267
		ereport(ERROR,
				(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
				 errmsg("cannot use perl in triggers yet")));
268

269
		/*
270
		 * retval = PointerGetDatum(plperl_trigger_handler(fcinfo));
271
		 */
272 273 274
		/* make the compiler happy */
		retval = (Datum) 0;
	}
275 276
	else
		retval = plperl_func_handler(fcinfo);
277 278 279 280 281 282 283

	return retval;
}


/**********************************************************************
 * plperl_create_sub()		- calls the perl interpreter to
284 285
 *		create the anonymous subroutine whose text is in the SV.
 *		Returns the SV containing the RV to the closure.
286
 **********************************************************************/
B
Bruce Momjian 已提交
287
static SV  *
288
plperl_create_sub(char *s, bool trusted)
289
{
290
	dSP;
291
	SV		   *subref;
B
Bruce Momjian 已提交
292
	int			count;
293 294 295 296

	ENTER;
	SAVETMPS;
	PUSHMARK(SP);
B
Bruce Momjian 已提交
297
	XPUSHs(sv_2mortal(newSVpv(s, 0)));
B
Bruce Momjian 已提交
298
	PUTBACK;
B
Bruce Momjian 已提交
299

300 301
	/*
	 * G_KEEPERR seems to be needed here, else we don't recognize compile
B
Bruce Momjian 已提交
302 303
	 * errors properly.  Perhaps it's because there's another level of
	 * eval inside mksafefunc?
304
	 */
305 306
	count = perl_call_pv((trusted ? "mksafefunc" : "mkunsafefunc"),
						 G_SCALAR | G_EVAL | G_KEEPERR);
307 308
	SPAGAIN;

309 310 311 312 313
	if (count != 1)
	{
		PUTBACK;
		FREETMPS;
		LEAVE;
314
		elog(ERROR, "didn't get a return item from mksafefunc");
315 316
	}

317
	if (SvTRUE(ERRSV))
318
	{
319 320 321 322
		POPs;
		PUTBACK;
		FREETMPS;
		LEAVE;
323
		elog(ERROR, "creation of function failed: %s", SvPV(ERRSV, PL_na));
324 325 326
	}

	/*
327 328
	 * need to make a deep copy of the return. it comes off the stack as a
	 * temporary.
329 330 331
	 */
	subref = newSVsv(POPs);

332 333
	if (!SvROK(subref))
	{
334 335 336
		PUTBACK;
		FREETMPS;
		LEAVE;
337

338 339 340 341
		/*
		 * subref is our responsibility because it is not mortal
		 */
		SvREFCNT_dec(subref);
342
		elog(ERROR, "didn't get a code ref");
343 344 345 346 347
	}

	PUTBACK;
	FREETMPS;
	LEAVE;
348

349 350 351 352
	return subref;
}

/**********************************************************************
353
 * plperl_init_shared_libs()		-
354 355 356 357
 *
 * 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.
358
 *
359 360
 **********************************************************************/

B
Bruce Momjian 已提交
361 362
EXTERN_C void boot_DynaLoader(pTHX_ CV * cv);
EXTERN_C void boot_SPI(pTHX_ CV * cv);
363

364
static void
365
plperl_init_shared_libs(pTHX)
366
{
367 368
	char	   *file = __FILE__;

369
	newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
370
	newXS("SPI::bootstrap", boot_SPI, file);
371 372 373 374 375 376
}

/**********************************************************************
 * plperl_call_perl_func()		- calls a perl function through the RV
 *			stored in the prodesc structure. massages the input parms properly
 **********************************************************************/
B
Bruce Momjian 已提交
377
static SV  *
378
plperl_call_perl_func(plperl_proc_desc * desc, FunctionCallInfo fcinfo)
379 380
{
	dSP;
381 382 383
	SV		   *retval;
	int			i;
	int			count;
384 385 386 387

	ENTER;
	SAVETMPS;

388
	PUSHMARK(SP);
389 390 391 392
	for (i = 0; i < desc->nargs; i++)
	{
		if (desc->arg_is_rel[i])
		{
393 394
			TupleTableSlot *slot = (TupleTableSlot *) fcinfo->arg[i];
			SV		   *hashref;
395

B
Bruce Momjian 已提交
396 397
			Assert(slot != NULL && !fcinfo->argnull[i]);

398
			/*
399 400
			 * plperl_build_tuple_argument better return a mortal SV.
			 */
401
			hashref = plperl_build_tuple_argument(slot->val,
B
Bruce Momjian 已提交
402
											  slot->ttc_tupleDescriptor);
403
			XPUSHs(hashref);
404 405 406
		}
		else
		{
407 408 409 410 411 412
			if (fcinfo->argnull[i])
				XPUSHs(&PL_sv_undef);
			else
			{
				char	   *tmp;

413
				tmp = DatumGetCString(FunctionCall3(&(desc->arg_out_func[i]),
B
Bruce Momjian 已提交
414 415
													fcinfo->arg[i],
								 ObjectIdGetDatum(desc->arg_out_elem[i]),
416
													Int32GetDatum(-1)));
417 418 419
				XPUSHs(sv_2mortal(newSVpv(tmp, 0)));
				pfree(tmp);
			}
420 421 422
		}
	}
	PUTBACK;
423 424 425

	/* Do NOT use G_KEEPERR here */
	count = perl_call_sv(desc->reference, G_SCALAR | G_EVAL);
426 427 428

	SPAGAIN;

429 430 431 432
	if (count != 1)
	{
		PUTBACK;
		FREETMPS;
433
		LEAVE;
434
		elog(ERROR, "didn't get a return item from function");
435 436
	}

437
	if (SvTRUE(ERRSV))
438
	{
439
		POPs;
440 441
		PUTBACK;
		FREETMPS;
442
		LEAVE;
443
		elog(ERROR, "error from function: %s", SvPV(ERRSV, PL_na));
444 445 446 447
	}

	retval = newSVsv(POPs);

448 449 450
	PUTBACK;
	FREETMPS;
	LEAVE;
451 452 453 454

	return retval;
}

455

456 457 458 459
/**********************************************************************
 * plperl_func_handler()		- Handler for regular function calls
 **********************************************************************/
static Datum
460
plperl_func_handler(PG_FUNCTION_ARGS)
461 462
{
	plperl_proc_desc *prodesc;
463 464
	SV		   *perlret;
	Datum		retval;
465

466 467
	/* Find or compile the function */
	prodesc = compile_plperl_function(fcinfo->flinfo->fn_oid, false);
468 469 470 471

	/************************************************************
	 * Call the Perl function
	 ************************************************************/
472
	perlret = plperl_call_perl_func(prodesc, fcinfo);
473 474 475 476 477 478 479 480

	/************************************************************
	 * 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)
481
		elog(ERROR, "SPI_finish() failed");
482

483
	if (!(perlret && SvOK(perlret)))
484
	{
485
		/* return NULL if Perl code returned undef */
486 487 488 489 490 491
		retval = (Datum) 0;
		fcinfo->isnull = true;
	}
	else
	{
		retval = FunctionCall3(&prodesc->result_in_func,
492
							   PointerGetDatum(SvPV(perlret, PL_na)),
493
							   ObjectIdGetDatum(prodesc->result_in_elem),
494
							   Int32GetDatum(-1));
495
	}
496 497 498 499 500 501 502 503

	SvREFCNT_dec(perlret);

	return retval;
}


/**********************************************************************
504
 * compile_plperl_function	- compile (or hopefully just look up) function
505
 **********************************************************************/
506 507
static plperl_proc_desc *
compile_plperl_function(Oid fn_oid, bool is_trigger)
508
{
509 510 511 512 513
	HeapTuple	procTup;
	Form_pg_proc procStruct;
	char		internal_proname[64];
	int			proname_len;
	plperl_proc_desc *prodesc = NULL;
514 515
	int			i;

516 517 518 519 520
	/* We'll need the pg_proc tuple in any case... */
	procTup = SearchSysCache(PROCOID,
							 ObjectIdGetDatum(fn_oid),
							 0, 0, 0);
	if (!HeapTupleIsValid(procTup))
521
		elog(ERROR, "cache lookup failed for function %u", fn_oid);
522
	procStruct = (Form_pg_proc) GETSTRUCT(procTup);
523 524 525 526

	/************************************************************
	 * Build our internal proc name from the functions Oid
	 ************************************************************/
527 528 529 530 531
	if (!is_trigger)
		sprintf(internal_proname, "__PLPerl_proc_%u", fn_oid);
	else
		sprintf(internal_proname, "__PLPerl_proc_%u_trigger", fn_oid);
	proname_len = strlen(internal_proname);
532 533 534 535

	/************************************************************
	 * Lookup the internal proc name in the hashtable
	 ************************************************************/
536
	if (hv_exists(plperl_proc_hash, internal_proname, proname_len))
537
	{
538 539 540 541 542
		bool		uptodate;

		prodesc = (plperl_proc_desc *) SvIV(*hv_fetch(plperl_proc_hash,
									  internal_proname, proname_len, 0));

543
		/************************************************************
544 545 546
		 * 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.
547
		 ************************************************************/
548
		uptodate = (prodesc->fn_xmin == HeapTupleHeaderGetXmin(procTup->t_data) &&
B
Bruce Momjian 已提交
549
			prodesc->fn_cmin == HeapTupleHeaderGetCmin(procTup->t_data));
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571

		if (!uptodate)
		{
			/* need we delete old entry? */
			prodesc = NULL;
		}
	}

	/************************************************************
	 * If we haven't found it in the hashtable, we analyze
	 * the functions arguments and returntype and store
	 * 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;
572 573 574 575 576 577
		char	   *proc_source;

		/************************************************************
		 * Allocate a new procedure description block
		 ************************************************************/
		prodesc = (plperl_proc_desc *) malloc(sizeof(plperl_proc_desc));
578
		if (prodesc == NULL)
579 580 581
			ereport(ERROR,
					(errcode(ERRCODE_OUT_OF_MEMORY),
					 errmsg("out of memory")));
582 583
		MemSet(prodesc, 0, sizeof(plperl_proc_desc));
		prodesc->proname = strdup(internal_proname);
584 585
		prodesc->fn_xmin = HeapTupleHeaderGetXmin(procTup->t_data);
		prodesc->fn_cmin = HeapTupleHeaderGetCmin(procTup->t_data);
586 587

		/************************************************************
588
		 * Lookup the pg_language tuple by Oid
589
		 ************************************************************/
590 591
		langTup = SearchSysCache(LANGOID,
								 ObjectIdGetDatum(procStruct->prolang),
592
								 0, 0, 0);
593
		if (!HeapTupleIsValid(langTup))
594 595 596
		{
			free(prodesc->proname);
			free(prodesc);
597
			elog(ERROR, "cache lookup failed for language %u",
598
				 procStruct->prolang);
599
		}
600 601 602
		langStruct = (Form_pg_language) GETSTRUCT(langTup);
		prodesc->lanpltrusted = langStruct->lanpltrusted;
		ReleaseSysCache(langTup);
603 604

		/************************************************************
605 606
		 * Get the required information for input conversion of the
		 * return value.
607
		 ************************************************************/
608 609 610
		if (!is_trigger)
		{
			typeTup = SearchSysCache(TYPEOID,
611
								ObjectIdGetDatum(procStruct->prorettype),
612 613 614 615 616
									 0, 0, 0);
			if (!HeapTupleIsValid(typeTup))
			{
				free(prodesc->proname);
				free(prodesc);
617
				elog(ERROR, "cache lookup failed for type %u",
618
					 procStruct->prorettype);
619 620 621
			}
			typeStruct = (Form_pg_type) GETSTRUCT(typeTup);

622 623 624 625
			/* Disallow pseudotype result, except VOID */
			if (typeStruct->typtype == 'p')
			{
				if (procStruct->prorettype == VOIDOID)
B
Bruce Momjian 已提交
626
					 /* okay */ ;
627
				else if (procStruct->prorettype == TRIGGEROID)
628 629 630
				{
					free(prodesc->proname);
					free(prodesc);
631 632 633
					ereport(ERROR,
							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
							 errmsg("trigger functions may only be called as triggers")));
634 635 636 637 638
				}
				else
				{
					free(prodesc->proname);
					free(prodesc);
639 640
					ereport(ERROR,
							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
B
Bruce Momjian 已提交
641 642
						 errmsg("plperl functions cannot return type %s",
								format_type_be(procStruct->prorettype))));
643 644 645
				}
			}

646 647 648 649
			if (typeStruct->typrelid != InvalidOid)
			{
				free(prodesc->proname);
				free(prodesc);
650 651
				ereport(ERROR,
						(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
B
Bruce Momjian 已提交
652
				   errmsg("plperl functions cannot return tuples yet")));
653 654 655 656 657 658 659
			}

			perm_fmgr_info(typeStruct->typinput, &(prodesc->result_in_func));
			prodesc->result_in_elem = typeStruct->typelem;

			ReleaseSysCache(typeTup);
		}
660 661

		/************************************************************
662 663
		 * Get the required information for output conversion
		 * of all procedure arguments
664
		 ************************************************************/
665 666 667 668 669 670
		if (!is_trigger)
		{
			prodesc->nargs = procStruct->pronargs;
			for (i = 0; i < prodesc->nargs; i++)
			{
				typeTup = SearchSysCache(TYPEOID,
671
							ObjectIdGetDatum(procStruct->proargtypes[i]),
672 673 674 675 676
										 0, 0, 0);
				if (!HeapTupleIsValid(typeTup))
				{
					free(prodesc->proname);
					free(prodesc);
677
					elog(ERROR, "cache lookup failed for type %u",
678
						 procStruct->proargtypes[i]);
679 680 681
				}
				typeStruct = (Form_pg_type) GETSTRUCT(typeTup);

682 683 684 685 686
				/* Disallow pseudotype argument */
				if (typeStruct->typtype == 'p')
				{
					free(prodesc->proname);
					free(prodesc);
687 688
					ereport(ERROR,
							(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
B
Bruce Momjian 已提交
689 690
						   errmsg("plperl functions cannot take type %s",
						   format_type_be(procStruct->proargtypes[i]))));
691 692
				}

693 694 695 696 697 698 699 700 701 702
				if (typeStruct->typrelid != InvalidOid)
					prodesc->arg_is_rel[i] = 1;
				else
					prodesc->arg_is_rel[i] = 0;

				perm_fmgr_info(typeStruct->typoutput, &(prodesc->arg_out_func[i]));
				prodesc->arg_out_elem[i] = typeStruct->typelem;
				ReleaseSysCache(typeTup);
			}
		}
703

704 705 706 707 708 709
		/************************************************************
		 * create the text of the anonymous subroutine.
		 * we do not use a named subroutine so that we can call directly
		 * through the reference.
		 *
		 ************************************************************/
710
		proc_source = DatumGetCString(DirectFunctionCall1(textout,
B
Bruce Momjian 已提交
711
								  PointerGetDatum(&procStruct->prosrc)));
712 713

		/************************************************************
714
		 * Create the procedure in the interpreter
715
		 ************************************************************/
716 717 718
		prodesc->reference = plperl_create_sub(proc_source, prodesc->lanpltrusted);
		pfree(proc_source);
		if (!prodesc->reference)
719 720 721
		{
			free(prodesc->proname);
			free(prodesc);
722
			elog(ERROR, "could not create internal procedure \"%s\"",
723
				 internal_proname);
724 725 726 727 728
		}

		/************************************************************
		 * Add the proc description block to the hashtable
		 ************************************************************/
729 730
		hv_store(plperl_proc_hash, internal_proname, proname_len,
				 newSViv((IV) prodesc), 0);
731 732
	}

733
	ReleaseSysCache(procTup);
734

735 736
	return prodesc;
}
737 738 739 740 741 742


/**********************************************************************
 * plperl_build_tuple_argument() - Build a string for a ref to a hash
 *				  from all attributes of a given tuple
 **********************************************************************/
B
Bruce Momjian 已提交
743
static SV  *
744 745 746
plperl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc)
{
	int			i;
747
	SV		   *output;
748 749 750
	Datum		attr;
	bool		isnull;
	char	   *attname;
751
	char	   *outputstr;
752 753 754 755 756 757 758 759
	HeapTuple	typeTup;
	Oid			typoutput;
	Oid			typelem;

	output = sv_2mortal(newSVpv("{", 0));

	for (i = 0; i < tupdesc->natts; i++)
	{
760 761 762 763
		/* ignore dropped attributes */
		if (tupdesc->attrs[i]->attisdropped)
			continue;

764 765 766 767 768 769 770 771 772 773
		/************************************************************
		 * Get the attribute name
		 ************************************************************/
		attname = tupdesc->attrs[i]->attname.data;

		/************************************************************
		 * Get the attributes value
		 ************************************************************/
		attr = heap_getattr(tuple, i + 1, tupdesc, &isnull);

774 775 776 777 778 779 780 781 782
		/************************************************************
		 *	If it is null it will be set to undef in the hash.
		 ************************************************************/
		if (isnull)
		{
			sv_catpvf(output, "'%s' => undef,", attname);
			continue;
		}

783 784 785 786
		/************************************************************
		 * Lookup the attribute type in the syscache
		 * for the output function
		 ************************************************************/
787
		typeTup = SearchSysCache(TYPEOID,
788
						   ObjectIdGetDatum(tupdesc->attrs[i]->atttypid),
789
								 0, 0, 0);
790
		if (!HeapTupleIsValid(typeTup))
791 792
			elog(ERROR, "cache lookup failed for type %u",
				 tupdesc->attrs[i]->atttypid);
793

794 795
		typoutput = ((Form_pg_type) GETSTRUCT(typeTup))->typoutput;
		typelem = ((Form_pg_type) GETSTRUCT(typeTup))->typelem;
796
		ReleaseSysCache(typeTup);
797 798

		/************************************************************
799
		 * Append the attribute name and the value to the list.
800
		 ************************************************************/
801 802
		outputstr = DatumGetCString(OidFunctionCall3(typoutput,
													 attr,
803 804
											   ObjectIdGetDatum(typelem),
						   Int32GetDatum(tupdesc->attrs[i]->atttypmod)));
805 806
		sv_catpvf(output, "'%s' => '%s',", attname, outputstr);
		pfree(outputstr);
807
	}
808

809
	sv_catpv(output, "}");
810
	output = perl_eval_pv(SvPV(output, PL_na), TRUE);
811 812
	return output;
}