fmgr.c 53.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * fmgr.c
4
 *	  The Postgres function manager.
5
 *
6
 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
B
Add:  
Bruce Momjian 已提交
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $PostgreSQL: pgsql/src/backend/utils/fmgr/fmgr.c,v 1.101 2006/05/30 21:21:30 tgl Exp $
12 13 14 15 16 17
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"

18
#include "access/tuptoaster.h"
19
#include "catalog/pg_language.h"
B
Bruce Momjian 已提交
20
#include "catalog/pg_proc.h"
21
#include "executor/functions.h"
22
#include "miscadmin.h"
23
#include "parser/parse_expr.h"
B
Bruce Momjian 已提交
24 25
#include "utils/builtins.h"
#include "utils/fmgrtab.h"
26
#include "utils/lsyscache.h"
B
Bruce Momjian 已提交
27
#include "utils/syscache.h"
28

29 30 31 32 33 34 35 36
/*
 * Declaration for old-style function pointer type.  This is now used only
 * in fmgr_oldstyle() and is no longer exported.
 *
 * The m68k SVR4 ABI defines that pointers are returned in %a0 instead of
 * %d0. So if a function pointer is declared to return a pointer, the
 * compiler may look only into %a0, but if the called function was declared
 * to return an integer type, it puts its value only into %d0. So the
N
Neil Conway 已提交
37
 * caller doesn't pick up the correct return value. The solution is to
38 39 40 41 42
 * declare the function pointer to return int, so the compiler picks up the
 * return value from %d0. (Functions returning pointers put their value
 * *additionally* into %d0 for compatibility.) The price is that there are
 * some warnings about int->pointer conversions...
 */
43
#if (defined(__mc68000__) || (defined(__m68k__))) && defined(__ELF__)
B
Bruce Momjian 已提交
44
typedef int32 (*func_ptr) ();
45
#else
B
Bruce Momjian 已提交
46
typedef char *(*func_ptr) ();
47 48
#endif

49 50 51 52 53 54
/*
 * For an oldstyle function, fn_extra points to a record like this:
 */
typedef struct
{
	func_ptr	func;			/* Address of the oldstyle function */
B
Bruce Momjian 已提交
55 56
	bool		arg_toastable[FUNC_MAX_ARGS];	/* is n'th arg of a toastable
												 * datatype? */
57
} Oldstyle_fnextra;
58

59 60 61 62 63 64 65 66 67 68
/*
 * Hashtable for fast lookup of external C functions
 */
typedef struct
{
	/* fn_oid is the hash key and so must be first! */
	Oid			fn_oid;			/* OID of an external C function */
	TransactionId fn_xmin;		/* for checking up-to-dateness */
	CommandId	fn_cmin;
	PGFunction	user_fn;		/* the function's address */
69
	const Pg_finfo_record *inforec;		/* address of its info record */
70 71 72 73
} CFuncHashTabEntry;

static HTAB *CFuncHash = NULL;

74

75
static void fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
B
Bruce Momjian 已提交
76
					   bool ignore_security);
77 78
static void fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
static void fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple);
79 80
static CFuncHashTabEntry *lookup_C_func(HeapTuple procedureTuple);
static void record_C_func(HeapTuple procedureTuple,
81
			  PGFunction user_fn, const Pg_finfo_record *inforec);
82
static Datum fmgr_oldstyle(PG_FUNCTION_ARGS);
83
static Datum fmgr_security_definer(PG_FUNCTION_ARGS);
84

85

86
/*
B
Bruce Momjian 已提交
87
 * Lookup routines for builtin-function table.	We can search by either Oid
88
 * or name, but search by Oid is much faster.
89 90
 */

91 92
static const FmgrBuiltin *
fmgr_isbuiltin(Oid id)
93
{
B
Bruce Momjian 已提交
94 95
	int			low = 0;
	int			high = fmgr_nbuiltins - 1;
96

B
Bruce Momjian 已提交
97
	/*
B
Bruce Momjian 已提交
98 99
	 * Loop invariant: low is the first index that could contain target entry,
	 * and high is the last index that could contain it.
100 101 102
	 */
	while (low <= high)
	{
B
Bruce Momjian 已提交
103 104
		int			i = (high + low) / 2;
		const FmgrBuiltin *ptr = &fmgr_builtins[i];
105

106 107 108 109 110 111 112
		if (id == ptr->foid)
			return ptr;
		else if (id > ptr->foid)
			low = i + 1;
		else
			high = i - 1;
	}
113
	return NULL;
114 115 116 117 118 119 120 121
}

/*
 * Lookup a builtin by name.  Note there can be more than one entry in
 * the array with the same name, but they should all point to the same
 * routine.
 */
static const FmgrBuiltin *
B
Bruce Momjian 已提交
122
fmgr_lookupByName(const char *name)
123
{
B
Bruce Momjian 已提交
124
	int			i;
125 126

	for (i = 0; i < fmgr_nbuiltins; i++)
127
	{
128 129
		if (strcmp(name, fmgr_builtins[i].funcName) == 0)
			return fmgr_builtins + i;
B
Bruce Momjian 已提交
130
	}
131
	return NULL;
132 133 134 135 136
}

/*
 * This routine fills a FmgrInfo struct, given the OID
 * of the function to be called.
137 138 139 140 141 142 143
 *
 * The caller's CurrentMemoryContext is used as the fn_mcxt of the info
 * struct; this means that any subsidiary data attached to the info struct
 * (either by fmgr_info itself, or later on by a function call handler)
 * will be allocated in that context.  The caller must ensure that this
 * context is at least as long-lived as the info struct itself.  This is
 * not a problem in typical cases where the info struct is on the stack or
144 145
 * in freshly-palloc'd space.  However, if one intends to store an info
 * struct in a long-lived table, it's better to use fmgr_info_cxt.
146 147 148
 */
void
fmgr_info(Oid functionId, FmgrInfo *finfo)
149 150 151 152 153 154 155 156 157 158
{
	fmgr_info_cxt(functionId, finfo, CurrentMemoryContext);
}

/*
 * Fill a FmgrInfo struct, specifying a memory context in which its
 * subsidiary data should go.
 */
void
fmgr_info_cxt(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt)
159 160 161 162 163 164 165 166 167 168 169 170
{
	fmgr_info_cxt_security(functionId, finfo, mcxt, false);
}

/*
 * This one does the actual work.  ignore_security is ordinarily false
 * but is set to true by fmgr_security_definer to avoid infinite
 * recursive lookups.
 */
static void
fmgr_info_cxt_security(Oid functionId, FmgrInfo *finfo, MemoryContext mcxt,
					   bool ignore_security)
171 172 173 174
{
	const FmgrBuiltin *fbp;
	HeapTuple	procedureTuple;
	Form_pg_proc procedureStruct;
175 176
	Datum		prosrcdatum;
	bool		isnull;
177 178
	char	   *prosrc;

179
	/*
B
Bruce Momjian 已提交
180 181 182
	 * fn_oid *must* be filled in last.  Some code assumes that if fn_oid is
	 * valid, the whole struct is valid.  Some FmgrInfo struct's do survive
	 * elogs.
183 184
	 */
	finfo->fn_oid = InvalidOid;
185
	finfo->fn_extra = NULL;
186
	finfo->fn_mcxt = mcxt;
187
	finfo->fn_expr = NULL;		/* caller may set this later */
188 189 190 191

	if ((fbp = fmgr_isbuiltin(functionId)) != NULL)
	{
		/*
B
Bruce Momjian 已提交
192
		 * Fast path for builtin functions: don't bother consulting pg_proc
193 194 195
		 */
		finfo->fn_nargs = fbp->nargs;
		finfo->fn_strict = fbp->strict;
196 197
		finfo->fn_retset = fbp->retset;
		finfo->fn_addr = fbp->func;
198
		finfo->fn_oid = functionId;
199
		return;
200 201
	}

202
	/* Otherwise we need the pg_proc entry */
203 204 205
	procedureTuple = SearchSysCache(PROCOID,
									ObjectIdGetDatum(functionId),
									0, 0, 0);
206
	if (!HeapTupleIsValid(procedureTuple))
207
		elog(ERROR, "cache lookup failed for function %u", functionId);
208
	procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
209

210 211
	finfo->fn_nargs = procedureStruct->pronargs;
	finfo->fn_strict = procedureStruct->proisstrict;
212
	finfo->fn_retset = procedureStruct->proretset;
213

214
	if (procedureStruct->prosecdef && !ignore_security)
215
	{
216
		finfo->fn_addr = fmgr_security_definer;
217
		finfo->fn_oid = functionId;
218
		ReleaseSysCache(procedureTuple);
219 220
		return;
	}
221

222
	switch (procedureStruct->prolang)
223 224
	{
		case INTERNALlanguageId:
225

226
			/*
B
Bruce Momjian 已提交
227 228
			 * For an ordinary builtin function, we should never get here
			 * because the isbuiltin() search above will have succeeded.
B
Bruce Momjian 已提交
229 230 231 232 233
			 * However, if the user has done a CREATE FUNCTION to create an
			 * alias for a builtin function, we can end up here.  In that case
			 * we have to look up the function by name.  The name of the
			 * internal function is stored in prosrc (it doesn't have to be
			 * the same as the name of the alias!)
234
			 */
235 236 237 238
			prosrcdatum = SysCacheGetAttr(PROCOID, procedureTuple,
										  Anum_pg_proc_prosrc, &isnull);
			if (isnull)
				elog(ERROR, "null prosrc");
239
			prosrc = DatumGetCString(DirectFunctionCall1(textout,
240
														 prosrcdatum));
241 242
			fbp = fmgr_lookupByName(prosrc);
			if (fbp == NULL)
243 244
				ereport(ERROR,
						(errcode(ERRCODE_UNDEFINED_FUNCTION),
245 246
						 errmsg("internal function \"%s\" is not in internal lookup table",
								prosrc)));
247
			pfree(prosrc);
248 249
			/* Should we check that nargs, strict, retset match the table? */
			finfo->fn_addr = fbp->func;
250
			break;
B
Bruce Momjian 已提交
251

252
		case ClanguageId:
253
			fmgr_info_C_lang(functionId, finfo, procedureTuple);
254 255 256 257 258 259 260
			break;

		case SQLlanguageId:
			finfo->fn_addr = fmgr_sql;
			break;

		default:
261
			fmgr_info_other_lang(functionId, finfo, procedureTuple);
262 263 264
			break;
	}

265
	finfo->fn_oid = functionId;
266 267 268 269
	ReleaseSysCache(procedureTuple);
}

/*
270 271
 * Special fmgr_info processing for C-language functions.  Note that
 * finfo->fn_oid is not valid yet.
272 273
 */
static void
274
fmgr_info_C_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
275 276
{
	Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
277
	CFuncHashTabEntry *hashentry;
278
	PGFunction	user_fn;
279
	const Pg_finfo_record *inforec;
280 281 282 283
	Oldstyle_fnextra *fnextra;
	bool		isnull;
	int			i;

284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
	/*
	 * See if we have the function address cached already
	 */
	hashentry = lookup_C_func(procedureTuple);
	if (hashentry)
	{
		user_fn = hashentry->user_fn;
		inforec = hashentry->inforec;
	}
	else
	{
		Datum		prosrcattr,
					probinattr;
		char	   *prosrcstring,
				   *probinstring;
		void	   *libraryhandle;

301
		/*
B
Bruce Momjian 已提交
302
		 * Get prosrc and probin strings (link symbol and library filename)
303
		 */
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
		prosrcattr = SysCacheGetAttr(PROCOID, procedureTuple,
									 Anum_pg_proc_prosrc, &isnull);
		if (isnull)
			elog(ERROR, "null prosrc for function %u", functionId);
		prosrcstring = DatumGetCString(DirectFunctionCall1(textout,
														   prosrcattr));

		probinattr = SysCacheGetAttr(PROCOID, procedureTuple,
									 Anum_pg_proc_probin, &isnull);
		if (isnull)
			elog(ERROR, "null probin for function %u", functionId);
		probinstring = DatumGetCString(DirectFunctionCall1(textout,
														   probinattr));

		/* Look up the function itself */
		user_fn = load_external_function(probinstring, prosrcstring, true,
										 &libraryhandle);

		/* Get the function information record (real or default) */
		inforec = fetch_finfo_record(libraryhandle, prosrcstring);

		/* Cache the addresses for later calls */
		record_C_func(procedureTuple, user_fn, inforec);

		pfree(prosrcstring);
		pfree(probinstring);
	}
331 332 333 334 335 336

	switch (inforec->api_version)
	{
		case 0:
			/* Old style: need to use a handler */
			finfo->fn_addr = fmgr_oldstyle;
337
			fnextra = (Oldstyle_fnextra *)
338 339
				MemoryContextAllocZero(finfo->fn_mcxt,
									   sizeof(Oldstyle_fnextra));
340 341 342
			finfo->fn_extra = (void *) fnextra;
			fnextra->func = (func_ptr) user_fn;
			for (i = 0; i < procedureStruct->pronargs; i++)
343
			{
344
				fnextra->arg_toastable[i] =
345
					TypeIsToastable(procedureStruct->proargtypes.values[i]);
346
			}
347 348 349 350 351 352 353
			break;
		case 1:
			/* New style: call directly */
			finfo->fn_addr = user_fn;
			break;
		default:
			/* Shouldn't get here if fetch_finfo_record did its job */
354
			elog(ERROR, "unrecognized function API version: %d",
355
				 inforec->api_version);
356 357
			break;
	}
358 359
}

360
/*
361 362
 * Special fmgr_info processing for other-language functions.  Note
 * that finfo->fn_oid is not valid yet.
363 364
 */
static void
365
fmgr_info_other_lang(Oid functionId, FmgrInfo *finfo, HeapTuple procedureTuple)
366 367 368 369 370
{
	Form_pg_proc procedureStruct = (Form_pg_proc) GETSTRUCT(procedureTuple);
	Oid			language = procedureStruct->prolang;
	HeapTuple	languageTuple;
	Form_pg_language languageStruct;
371
	FmgrInfo	plfinfo;
372 373 374 375 376

	languageTuple = SearchSysCache(LANGOID,
								   ObjectIdGetDatum(language),
								   0, 0, 0);
	if (!HeapTupleIsValid(languageTuple))
377
		elog(ERROR, "cache lookup failed for language %u", language);
378 379
	languageStruct = (Form_pg_language) GETSTRUCT(languageTuple);

380 381 382 383 384
	fmgr_info(languageStruct->lanplcallfoid, &plfinfo);
	finfo->fn_addr = plfinfo.fn_addr;

	/*
	 * If lookup of the PL handler function produced nonnull fn_extra,
B
Bruce Momjian 已提交
385 386
	 * complain --- it must be an oldstyle function! We no longer support
	 * oldstyle PL handlers.
387 388
	 */
	if (plfinfo.fn_extra != NULL)
389
		elog(ERROR, "language %u has old-style handler", language);
B
Bruce Momjian 已提交
390

391 392
	ReleaseSysCache(languageTuple);
}
393 394

/*
395
 * Fetch and validate the information record for the given external function.
396 397
 * The function is specified by a handle for the containing library
 * (obtained from load_external_function) as well as the function name.
398 399 400 401 402 403
 *
 * If no info function exists for the given name, it is not an error.
 * Instead we return a default info record for a version-0 function.
 * We want to raise an error here only if the info function returns
 * something bogus.
 *
404
 * This function is broken out of fmgr_info_C_lang so that fmgr_c_validator
405 406 407
 * can validate the information record for a function not yet entered into
 * pg_proc.
 */
408
const Pg_finfo_record *
409
fetch_finfo_record(void *filehandle, char *funcname)
410 411 412
{
	char	   *infofuncname;
	PGFInfoFunction infofunc;
413
	const Pg_finfo_record *inforec;
B
Bruce Momjian 已提交
414
	static Pg_finfo_record default_inforec = {0};
415 416 417

	/* Compute name of info func */
	infofuncname = (char *) palloc(strlen(funcname) + 10);
418 419
	strcpy(infofuncname, "pg_finfo_");
	strcat(infofuncname, funcname);
420 421

	/* Try to look up the info function */
422 423
	infofunc = (PGFInfoFunction) lookup_external_function(filehandle,
														  infofuncname);
424
	if (infofunc == NULL)
425 426 427 428 429 430 431
	{
		/* Not found --- assume version 0 */
		pfree(infofuncname);
		return &default_inforec;
	}

	/* Found, so call it */
B
Bruce Momjian 已提交
432
	inforec = (*infofunc) ();
433 434 435

	/* Validate result as best we can */
	if (inforec == NULL)
436
		elog(ERROR, "null result from info function \"%s\"", infofuncname);
437 438 439 440 441 442 443
	switch (inforec->api_version)
	{
		case 0:
		case 1:
			/* OK, no additional fields to validate */
			break;
		default:
444 445 446 447
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
					 errmsg("unrecognized API version %d reported by info function \"%s\"",
							inforec->api_version, infofuncname)));
448 449 450 451 452 453 454 455
			break;
	}

	pfree(infofuncname);
	return inforec;
}


456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
/*-------------------------------------------------------------------------
 *		Routines for caching lookup information for external C functions.
 *
 * The routines in dfmgr.c are relatively slow, so we try to avoid running
 * them more than once per external function per session.  We use a hash table
 * with the function OID as the lookup key.
 *-------------------------------------------------------------------------
 */

/*
 * lookup_C_func: try to find a C function in the hash table
 *
 * If an entry exists and is up to date, return it; else return NULL
 */
static CFuncHashTabEntry *
lookup_C_func(HeapTuple procedureTuple)
{
473
	Oid			fn_oid = HeapTupleGetOid(procedureTuple);
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	CFuncHashTabEntry *entry;

	if (CFuncHash == NULL)
		return NULL;			/* no table yet */
	entry = (CFuncHashTabEntry *)
		hash_search(CFuncHash,
					&fn_oid,
					HASH_FIND,
					NULL);
	if (entry == NULL)
		return NULL;			/* no such entry */
	if (entry->fn_xmin == HeapTupleHeaderGetXmin(procedureTuple->t_data) &&
		entry->fn_cmin == HeapTupleHeaderGetCmin(procedureTuple->t_data))
		return entry;			/* OK */
	return NULL;				/* entry is out of date */
}

/*
 * record_C_func: enter (or update) info about a C function in the hash table
 */
static void
record_C_func(HeapTuple procedureTuple,
496
			  PGFunction user_fn, const Pg_finfo_record *inforec)
497
{
498
	Oid			fn_oid = HeapTupleGetOid(procedureTuple);
499
	CFuncHashTabEntry *entry;
500
	bool		found;
501 502 503 504 505 506 507 508 509

	/* Create the hash table if it doesn't exist yet */
	if (CFuncHash == NULL)
	{
		HASHCTL		hash_ctl;

		MemSet(&hash_ctl, 0, sizeof(hash_ctl));
		hash_ctl.keysize = sizeof(Oid);
		hash_ctl.entrysize = sizeof(CFuncHashTabEntry);
510
		hash_ctl.hash = oid_hash;
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
		CFuncHash = hash_create("CFuncHash",
								100,
								&hash_ctl,
								HASH_ELEM | HASH_FUNCTION);
	}

	entry = (CFuncHashTabEntry *)
		hash_search(CFuncHash,
					&fn_oid,
					HASH_ENTER,
					&found);
	/* OID is already filled in */
	entry->fn_xmin = HeapTupleHeaderGetXmin(procedureTuple->t_data);
	entry->fn_cmin = HeapTupleHeaderGetCmin(procedureTuple->t_data);
	entry->user_fn = user_fn;
	entry->inforec = inforec;
}

/*
 * clear_external_function_hash: remove entries for a library being closed
 *
 * Presently we just zap the entire hash table, but later it might be worth
 * the effort to remove only the entries associated with the given handle.
 */
void
clear_external_function_hash(void *filehandle)
{
	if (CFuncHash)
		hash_destroy(CFuncHash);
	CFuncHash = NULL;
}


544 545 546 547
/*
 * Copy an FmgrInfo struct
 *
 * This is inherently somewhat bogus since we can't reliably duplicate
548
 * language-dependent subsidiary info.	We cheat by zeroing fn_extra,
549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
 * instead, meaning that subsidiary info will have to be recomputed.
 */
void
fmgr_info_copy(FmgrInfo *dstinfo, FmgrInfo *srcinfo,
			   MemoryContext destcxt)
{
	memcpy(dstinfo, srcinfo, sizeof(FmgrInfo));
	dstinfo->fn_mcxt = destcxt;
	if (dstinfo->fn_addr == fmgr_oldstyle)
	{
		/* For oldstyle functions we must copy fn_extra */
		Oldstyle_fnextra *fnextra;

		fnextra = (Oldstyle_fnextra *)
			MemoryContextAlloc(destcxt, sizeof(Oldstyle_fnextra));
		memcpy(fnextra, srcinfo->fn_extra, sizeof(Oldstyle_fnextra));
		dstinfo->fn_extra = (void *) fnextra;
	}
	else
		dstinfo->fn_extra = NULL;
}


572
/*
573 574
 * Specialized lookup routine for fmgr_internal_validator: given the alleged
 * name of an internal function, return the OID of the function.
575
 * If the name is not recognized, return InvalidOid.
576
 */
577
Oid
578
fmgr_internal_function(const char *proname)
579
{
580
	const FmgrBuiltin *fbp = fmgr_lookupByName(proname);
B
Bruce Momjian 已提交
581

582 583
	if (fbp == NULL)
		return InvalidOid;
584
	return fbp->foid;
585 586 587 588
}


/*
589
 * Handler for old-style "C" language functions
590
 */
591 592
static Datum
fmgr_oldstyle(PG_FUNCTION_ARGS)
593
{
594
	Oldstyle_fnextra *fnextra;
595 596 597 598
	int			n_arguments = fcinfo->nargs;
	int			i;
	bool		isnull;
	func_ptr	user_fn;
599
	char	   *returnValue;
600 601

	if (fcinfo->flinfo == NULL || fcinfo->flinfo->fn_extra == NULL)
602
		elog(ERROR, "fmgr_oldstyle received NULL pointer");
603
	fnextra = (Oldstyle_fnextra *) fcinfo->flinfo->fn_extra;
604

605
	/*
B
Bruce Momjian 已提交
606 607 608 609
	 * Result is NULL if any argument is NULL, but we still call the function
	 * (peculiar, but that's the way it worked before, and after all this is a
	 * backwards-compatibility wrapper).  Note, however, that we'll never get
	 * here with NULL arguments if the function is marked strict.
610
	 *
611 612
	 * We also need to detoast any TOAST-ed inputs, since it's unlikely that
	 * an old-style function knows about TOASTing.
613
	 */
614 615
	isnull = false;
	for (i = 0; i < n_arguments; i++)
616 617 618 619 620 621
	{
		if (PG_ARGISNULL(i))
			isnull = true;
		else if (fnextra->arg_toastable[i])
			fcinfo->arg[i] = PointerGetDatum(PG_DETOAST_DATUM(fcinfo->arg[i]));
	}
622
	fcinfo->isnull = isnull;
623

624
	user_fn = fnextra->func;
625

626 627
	switch (n_arguments)
	{
628 629 630 631
		case 0:
			returnValue = (*user_fn) ();
			break;
		case 1:
B
Bruce Momjian 已提交
632

633
			/*
B
Bruce Momjian 已提交
634 635 636
			 * nullvalue() used to use isNull to check if arg is NULL; perhaps
			 * there are other functions still out there that also rely on
			 * this undocumented hack?
637
			 */
B
Bruce Momjian 已提交
638
			returnValue = (*user_fn) (fcinfo->arg[0], &fcinfo->isnull);
639 640
			break;
		case 2:
641
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1]);
642 643
			break;
		case 3:
644 645
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2]);
646 647
			break;
		case 4:
648 649
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3]);
650 651
			break;
		case 5:
652 653 654
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4]);
655 656
			break;
		case 6:
657 658 659
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5]);
660 661
			break;
		case 7:
662 663 664 665
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6]);
666 667
			break;
		case 8:
668 669 670 671
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7]);
672 673
			break;
		case 9:
674 675 676 677 678
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8]);
679 680
			break;
		case 10:
681 682 683 684 685
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8], fcinfo->arg[9]);
686 687
			break;
		case 11:
688 689 690 691 692 693
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8], fcinfo->arg[9],
									  fcinfo->arg[10]);
694 695
			break;
		case 12:
696 697 698 699 700 701
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8], fcinfo->arg[9],
									  fcinfo->arg[10], fcinfo->arg[11]);
702 703
			break;
		case 13:
704 705 706 707 708 709 710
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8], fcinfo->arg[9],
									  fcinfo->arg[10], fcinfo->arg[11],
									  fcinfo->arg[12]);
711 712
			break;
		case 14:
713 714 715 716 717 718 719
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8], fcinfo->arg[9],
									  fcinfo->arg[10], fcinfo->arg[11],
									  fcinfo->arg[12], fcinfo->arg[13]);
720 721
			break;
		case 15:
722 723 724 725 726 727 728 729
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8], fcinfo->arg[9],
									  fcinfo->arg[10], fcinfo->arg[11],
									  fcinfo->arg[12], fcinfo->arg[13],
									  fcinfo->arg[14]);
730 731
			break;
		case 16:
732 733 734 735 736 737 738 739
			returnValue = (*user_fn) (fcinfo->arg[0], fcinfo->arg[1],
									  fcinfo->arg[2], fcinfo->arg[3],
									  fcinfo->arg[4], fcinfo->arg[5],
									  fcinfo->arg[6], fcinfo->arg[7],
									  fcinfo->arg[8], fcinfo->arg[9],
									  fcinfo->arg[10], fcinfo->arg[11],
									  fcinfo->arg[12], fcinfo->arg[13],
									  fcinfo->arg[14], fcinfo->arg[15]);
740
			break;
741
		default:
B
Bruce Momjian 已提交
742

743
			/*
B
Bruce Momjian 已提交
744 745 746 747 748
			 * Increasing FUNC_MAX_ARGS doesn't automatically add cases to the
			 * above code, so mention the actual value in this error not
			 * FUNC_MAX_ARGS.  You could add cases to the above if you needed
			 * to support old-style functions with many arguments, but making
			 * 'em be new-style is probably a better idea.
749
			 */
750 751
			ereport(ERROR,
					(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
B
Bruce Momjian 已提交
752 753
			 errmsg("function %u has too many arguments (%d, maximum is %d)",
					fcinfo->flinfo->fn_oid, n_arguments, 16)));
B
Bruce Momjian 已提交
754
			returnValue = NULL; /* keep compiler quiet */
755
			break;
756
	}
757 758

	return (Datum) returnValue;
759 760
}

761

762
/*
763 764 765 766 767 768
 * Support for security definer functions
 */

struct fmgr_security_definer_cache
{
	FmgrInfo	flinfo;
B
Bruce Momjian 已提交
769
	Oid			userid;
770 771 772 773 774 775 776 777 778
};

/*
 * Function handler for security definer functions.  We extract the
 * OID of the actual function and do a fmgr lookup again.  Then we
 * look up the owner of the function and cache both the fmgr info and
 * the owner ID.  During the call we temporarily replace the flinfo
 * with the cached/looked-up one, while keeping the outer fcinfo
 * (which contains all the actual arguments, etc.) intact.
779
 */
780
static Datum
781
fmgr_security_definer(PG_FUNCTION_ARGS)
782
{
783 784
	Datum		result;
	FmgrInfo   *save_flinfo;
B
Bruce Momjian 已提交
785 786
	struct fmgr_security_definer_cache *volatile fcache;
	Oid			save_userid;
787 788 789 790
	HeapTuple	tuple;

	if (!fcinfo->flinfo->fn_extra)
	{
791 792
		fcache = MemoryContextAllocZero(fcinfo->flinfo->fn_mcxt,
										sizeof(*fcache));
793 794 795 796

		fmgr_info_cxt_security(fcinfo->flinfo->fn_oid, &fcache->flinfo,
							   fcinfo->flinfo->fn_mcxt, true);

797 798 799
		tuple = SearchSysCache(PROCOID,
							   ObjectIdGetDatum(fcinfo->flinfo->fn_oid),
							   0, 0, 0);
800
		if (!HeapTupleIsValid(tuple))
801
			elog(ERROR, "cache lookup failed for function %u",
802 803 804 805 806 807 808 809 810 811 812
				 fcinfo->flinfo->fn_oid);
		fcache->userid = ((Form_pg_proc) GETSTRUCT(tuple))->proowner;
		ReleaseSysCache(tuple);

		fcinfo->flinfo->fn_extra = fcache;
	}
	else
		fcache = fcinfo->flinfo->fn_extra;

	save_flinfo = fcinfo->flinfo;
	save_userid = GetUserId();
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827

	PG_TRY();
	{
		fcinfo->flinfo = &fcache->flinfo;
		SetUserId(fcache->userid);

		result = FunctionCallInvoke(fcinfo);
	}
	PG_CATCH();
	{
		fcinfo->flinfo = save_flinfo;
		SetUserId(save_userid);
		PG_RE_THROW();
	}
	PG_END_TRY();
828 829

	fcinfo->flinfo = save_flinfo;
830
	SetUserId(save_userid);
831 832

	return result;
833
}
834

835 836 837 838

/*-------------------------------------------------------------------------
 *		Support routines for callers of fmgr-compatible functions
 *-------------------------------------------------------------------------
839
 */
840

841 842
/*
 * These are for invocation of a specifically named function with a
843
 * directly-computed parameter list.  Note that neither arguments nor result
B
Bruce Momjian 已提交
844
 * are allowed to be NULL.	Also, the function cannot be one that needs to
845 846 847 848
 * look at FmgrInfo, since there won't be any.
 */
Datum
DirectFunctionCall1(PGFunction func, Datum arg1)
849
{
B
Bruce Momjian 已提交
850 851
	FunctionCallInfoData fcinfo;
	Datum		result;
852

853
	InitFunctionCallInfoData(fcinfo, NULL, 1, NULL, NULL);
854

855
	fcinfo.arg[0] = arg1;
856
	fcinfo.argnull[0] = false;
857

B
Bruce Momjian 已提交
858
	result = (*func) (&fcinfo);
859 860 861

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
862
		elog(ERROR, "function %p returned NULL", (void *) func);
863 864

	return result;
865
}
866

867 868 869
Datum
DirectFunctionCall2(PGFunction func, Datum arg1, Datum arg2)
{
B
Bruce Momjian 已提交
870 871
	FunctionCallInfoData fcinfo;
	Datum		result;
872

873
	InitFunctionCallInfoData(fcinfo, NULL, 2, NULL, NULL);
874

875 876
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
877 878
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
879

B
Bruce Momjian 已提交
880
	result = (*func) (&fcinfo);
881 882 883

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
884
		elog(ERROR, "function %p returned NULL", (void *) func);
885 886 887 888 889 890 891 892

	return result;
}

Datum
DirectFunctionCall3(PGFunction func, Datum arg1, Datum arg2,
					Datum arg3)
{
B
Bruce Momjian 已提交
893 894
	FunctionCallInfoData fcinfo;
	Datum		result;
895

896
	InitFunctionCallInfoData(fcinfo, NULL, 3, NULL, NULL);
897

898 899 900
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
901 902 903
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
904

B
Bruce Momjian 已提交
905
	result = (*func) (&fcinfo);
906 907 908

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
909
		elog(ERROR, "function %p returned NULL", (void *) func);
910 911 912 913 914 915 916 917

	return result;
}

Datum
DirectFunctionCall4(PGFunction func, Datum arg1, Datum arg2,
					Datum arg3, Datum arg4)
{
B
Bruce Momjian 已提交
918 919
	FunctionCallInfoData fcinfo;
	Datum		result;
920

921
	InitFunctionCallInfoData(fcinfo, NULL, 4, NULL, NULL);
922

923 924 925 926
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
927 928 929 930
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
931

B
Bruce Momjian 已提交
932
	result = (*func) (&fcinfo);
933 934 935

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
936
		elog(ERROR, "function %p returned NULL", (void *) func);
937 938 939 940 941 942 943 944

	return result;
}

Datum
DirectFunctionCall5(PGFunction func, Datum arg1, Datum arg2,
					Datum arg3, Datum arg4, Datum arg5)
{
B
Bruce Momjian 已提交
945 946
	FunctionCallInfoData fcinfo;
	Datum		result;
947

948
	InitFunctionCallInfoData(fcinfo, NULL, 5, NULL, NULL);
949

950 951 952 953 954
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
955 956 957 958 959
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
960

B
Bruce Momjian 已提交
961
	result = (*func) (&fcinfo);
962 963 964

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
965
		elog(ERROR, "function %p returned NULL", (void *) func);
966 967 968 969 970 971 972 973 974

	return result;
}

Datum
DirectFunctionCall6(PGFunction func, Datum arg1, Datum arg2,
					Datum arg3, Datum arg4, Datum arg5,
					Datum arg6)
{
B
Bruce Momjian 已提交
975 976
	FunctionCallInfoData fcinfo;
	Datum		result;
977

978
	InitFunctionCallInfoData(fcinfo, NULL, 6, NULL, NULL);
979

980 981 982 983 984 985
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
986 987 988 989 990 991
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
992

B
Bruce Momjian 已提交
993
	result = (*func) (&fcinfo);
994 995 996

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
997
		elog(ERROR, "function %p returned NULL", (void *) func);
998 999 1000 1001 1002 1003 1004 1005 1006

	return result;
}

Datum
DirectFunctionCall7(PGFunction func, Datum arg1, Datum arg2,
					Datum arg3, Datum arg4, Datum arg5,
					Datum arg6, Datum arg7)
{
B
Bruce Momjian 已提交
1007 1008
	FunctionCallInfoData fcinfo;
	Datum		result;
1009

1010
	InitFunctionCallInfoData(fcinfo, NULL, 7, NULL, NULL);
1011

1012 1013 1014 1015 1016 1017 1018
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
1019 1020 1021 1022 1023 1024 1025
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
1026

B
Bruce Momjian 已提交
1027
	result = (*func) (&fcinfo);
1028 1029 1030

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1031
		elog(ERROR, "function %p returned NULL", (void *) func);
1032 1033 1034 1035 1036 1037 1038 1039 1040

	return result;
}

Datum
DirectFunctionCall8(PGFunction func, Datum arg1, Datum arg2,
					Datum arg3, Datum arg4, Datum arg5,
					Datum arg6, Datum arg7, Datum arg8)
{
B
Bruce Momjian 已提交
1041 1042
	FunctionCallInfoData fcinfo;
	Datum		result;
1043

1044
	InitFunctionCallInfoData(fcinfo, NULL, 8, NULL, NULL);
1045

1046 1047 1048 1049 1050 1051 1052 1053
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
	fcinfo.arg[7] = arg8;
1054 1055 1056 1057 1058 1059 1060 1061
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
	fcinfo.argnull[7] = false;
1062

B
Bruce Momjian 已提交
1063
	result = (*func) (&fcinfo);
1064 1065 1066

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1067
		elog(ERROR, "function %p returned NULL", (void *) func);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

	return result;
}

Datum
DirectFunctionCall9(PGFunction func, Datum arg1, Datum arg2,
					Datum arg3, Datum arg4, Datum arg5,
					Datum arg6, Datum arg7, Datum arg8,
					Datum arg9)
{
B
Bruce Momjian 已提交
1078 1079
	FunctionCallInfoData fcinfo;
	Datum		result;
1080

1081
	InitFunctionCallInfoData(fcinfo, NULL, 9, NULL, NULL);
1082

1083 1084 1085 1086 1087 1088 1089 1090 1091
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
	fcinfo.arg[7] = arg8;
	fcinfo.arg[8] = arg9;
1092 1093 1094 1095 1096 1097 1098 1099 1100
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
	fcinfo.argnull[7] = false;
	fcinfo.argnull[8] = false;
1101

B
Bruce Momjian 已提交
1102
	result = (*func) (&fcinfo);
1103 1104 1105

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1106
		elog(ERROR, "function %p returned NULL", (void *) func);
1107 1108 1109 1110 1111

	return result;
}


1112 1113
/*
 * These are for invocation of a previously-looked-up function with a
1114 1115
 * directly-computed parameter list.  Note that neither arguments nor result
 * are allowed to be NULL.
1116
 */
1117 1118 1119
Datum
FunctionCall1(FmgrInfo *flinfo, Datum arg1)
{
B
Bruce Momjian 已提交
1120 1121
	FunctionCallInfoData fcinfo;
	Datum		result;
1122

1123
	InitFunctionCallInfoData(fcinfo, flinfo, 1, NULL, NULL);
1124

1125
	fcinfo.arg[0] = arg1;
1126
	fcinfo.argnull[0] = false;
1127 1128 1129 1130 1131

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1132
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1133 1134 1135 1136 1137 1138 1139

	return result;
}

Datum
FunctionCall2(FmgrInfo *flinfo, Datum arg1, Datum arg2)
{
1140 1141 1142 1143
	/*
	 * XXX if you change this routine, see also the inlined version in
	 * utils/sort/tuplesort.c!
	 */
B
Bruce Momjian 已提交
1144 1145
	FunctionCallInfoData fcinfo;
	Datum		result;
1146

1147
	InitFunctionCallInfoData(fcinfo, flinfo, 2, NULL, NULL);
1148

1149 1150
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
1151 1152
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
1153 1154 1155 1156 1157

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1158
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1159 1160 1161 1162 1163 1164 1165 1166

	return result;
}

Datum
FunctionCall3(FmgrInfo *flinfo, Datum arg1, Datum arg2,
			  Datum arg3)
{
B
Bruce Momjian 已提交
1167 1168
	FunctionCallInfoData fcinfo;
	Datum		result;
1169

1170
	InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);
1171

1172 1173 1174
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
1175 1176 1177
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
1178 1179 1180 1181 1182

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1183
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1184 1185 1186 1187 1188 1189 1190 1191

	return result;
}

Datum
FunctionCall4(FmgrInfo *flinfo, Datum arg1, Datum arg2,
			  Datum arg3, Datum arg4)
{
B
Bruce Momjian 已提交
1192 1193
	FunctionCallInfoData fcinfo;
	Datum		result;
1194

1195
	InitFunctionCallInfoData(fcinfo, flinfo, 4, NULL, NULL);
1196

1197 1198 1199 1200
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
1201 1202 1203 1204
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
1205 1206 1207 1208 1209

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1210
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1211 1212 1213 1214 1215 1216 1217 1218

	return result;
}

Datum
FunctionCall5(FmgrInfo *flinfo, Datum arg1, Datum arg2,
			  Datum arg3, Datum arg4, Datum arg5)
{
B
Bruce Momjian 已提交
1219 1220
	FunctionCallInfoData fcinfo;
	Datum		result;
1221

1222
	InitFunctionCallInfoData(fcinfo, flinfo, 5, NULL, NULL);
1223

1224 1225 1226 1227 1228
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
1229 1230 1231 1232 1233
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
1234 1235 1236 1237 1238

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1239
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1240 1241 1242 1243 1244 1245 1246 1247 1248

	return result;
}

Datum
FunctionCall6(FmgrInfo *flinfo, Datum arg1, Datum arg2,
			  Datum arg3, Datum arg4, Datum arg5,
			  Datum arg6)
{
B
Bruce Momjian 已提交
1249 1250
	FunctionCallInfoData fcinfo;
	Datum		result;
1251

1252
	InitFunctionCallInfoData(fcinfo, flinfo, 6, NULL, NULL);
1253

1254 1255 1256 1257 1258 1259
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
1260 1261 1262 1263 1264 1265
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
1266 1267 1268 1269 1270

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1271
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1272 1273 1274 1275 1276 1277 1278 1279

	return result;
}

Datum
FunctionCall7(FmgrInfo *flinfo, Datum arg1, Datum arg2,
			  Datum arg3, Datum arg4, Datum arg5,
			  Datum arg6, Datum arg7)
1280
{
B
Bruce Momjian 已提交
1281 1282
	FunctionCallInfoData fcinfo;
	Datum		result;
1283

1284
	InitFunctionCallInfoData(fcinfo, flinfo, 7, NULL, NULL);
1285

1286 1287 1288 1289 1290 1291 1292
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
1293 1294 1295 1296 1297 1298 1299
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
1300 1301 1302 1303 1304

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1305
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1306 1307 1308 1309 1310 1311 1312 1313 1314

	return result;
}

Datum
FunctionCall8(FmgrInfo *flinfo, Datum arg1, Datum arg2,
			  Datum arg3, Datum arg4, Datum arg5,
			  Datum arg6, Datum arg7, Datum arg8)
{
B
Bruce Momjian 已提交
1315 1316
	FunctionCallInfoData fcinfo;
	Datum		result;
1317

1318
	InitFunctionCallInfoData(fcinfo, flinfo, 8, NULL, NULL);
1319

1320 1321 1322 1323 1324 1325 1326 1327
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
	fcinfo.arg[7] = arg8;
1328 1329 1330 1331 1332 1333 1334 1335
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
	fcinfo.argnull[7] = false;
1336 1337 1338 1339 1340

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1341
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351

	return result;
}

Datum
FunctionCall9(FmgrInfo *flinfo, Datum arg1, Datum arg2,
			  Datum arg3, Datum arg4, Datum arg5,
			  Datum arg6, Datum arg7, Datum arg8,
			  Datum arg9)
{
B
Bruce Momjian 已提交
1352 1353
	FunctionCallInfoData fcinfo;
	Datum		result;
1354

1355
	InitFunctionCallInfoData(fcinfo, flinfo, 9, NULL, NULL);
1356

1357 1358 1359 1360 1361 1362 1363 1364 1365
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
	fcinfo.arg[7] = arg8;
	fcinfo.arg[8] = arg9;
1366 1367 1368 1369 1370 1371 1372 1373 1374
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
	fcinfo.argnull[7] = false;
	fcinfo.argnull[8] = false;
1375 1376 1377 1378 1379

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1380
		elog(ERROR, "function %u returned NULL", fcinfo.flinfo->fn_oid);
1381

1382 1383 1384 1385
	return result;
}


1386 1387
/*
 * These are for invocation of a function identified by OID with a
1388
 * directly-computed parameter list.  Note that neither arguments nor result
B
Bruce Momjian 已提交
1389 1390
 * are allowed to be NULL.	These are essentially fmgr_info() followed
 * by FunctionCallN().	If the same function is to be invoked repeatedly,
1391 1392 1393 1394 1395
 * do the fmgr_info() once and then use FunctionCallN().
 */
Datum
OidFunctionCall1(Oid functionId, Datum arg1)
{
B
Bruce Momjian 已提交
1396 1397 1398
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1399 1400 1401

	fmgr_info(functionId, &flinfo);

1402
	InitFunctionCallInfoData(fcinfo, &flinfo, 1, NULL, NULL);
1403

1404
	fcinfo.arg[0] = arg1;
1405
	fcinfo.argnull[0] = false;
1406 1407 1408 1409 1410

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1411
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1412 1413 1414 1415 1416 1417 1418

	return result;
}

Datum
OidFunctionCall2(Oid functionId, Datum arg1, Datum arg2)
{
B
Bruce Momjian 已提交
1419 1420 1421
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1422 1423 1424

	fmgr_info(functionId, &flinfo);

1425
	InitFunctionCallInfoData(fcinfo, &flinfo, 2, NULL, NULL);
1426

1427 1428
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
1429 1430
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
1431 1432 1433 1434 1435

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1436
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1437 1438 1439 1440 1441 1442 1443 1444

	return result;
}

Datum
OidFunctionCall3(Oid functionId, Datum arg1, Datum arg2,
				 Datum arg3)
{
B
Bruce Momjian 已提交
1445 1446 1447
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1448 1449 1450

	fmgr_info(functionId, &flinfo);

1451
	InitFunctionCallInfoData(fcinfo, &flinfo, 3, NULL, NULL);
1452

1453 1454 1455
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
1456 1457 1458
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
1459 1460 1461 1462 1463

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1464
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1465 1466 1467 1468 1469 1470 1471 1472

	return result;
}

Datum
OidFunctionCall4(Oid functionId, Datum arg1, Datum arg2,
				 Datum arg3, Datum arg4)
{
B
Bruce Momjian 已提交
1473 1474 1475
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1476 1477 1478

	fmgr_info(functionId, &flinfo);

1479
	InitFunctionCallInfoData(fcinfo, &flinfo, 4, NULL, NULL);
1480

1481 1482 1483 1484
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
1485 1486 1487 1488
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
1489 1490 1491 1492 1493

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1494
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1495 1496 1497 1498 1499 1500 1501 1502

	return result;
}

Datum
OidFunctionCall5(Oid functionId, Datum arg1, Datum arg2,
				 Datum arg3, Datum arg4, Datum arg5)
{
B
Bruce Momjian 已提交
1503 1504 1505
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1506 1507 1508

	fmgr_info(functionId, &flinfo);

1509
	InitFunctionCallInfoData(fcinfo, &flinfo, 5, NULL, NULL);
1510

1511 1512 1513 1514 1515
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
1516 1517 1518 1519 1520
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
1521 1522 1523 1524 1525

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1526
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1527 1528 1529 1530 1531 1532 1533 1534 1535

	return result;
}

Datum
OidFunctionCall6(Oid functionId, Datum arg1, Datum arg2,
				 Datum arg3, Datum arg4, Datum arg5,
				 Datum arg6)
{
B
Bruce Momjian 已提交
1536 1537 1538
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1539 1540 1541

	fmgr_info(functionId, &flinfo);

1542
	InitFunctionCallInfoData(fcinfo, &flinfo, 6, NULL, NULL);
1543

1544 1545 1546 1547 1548 1549
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
1550 1551 1552 1553 1554 1555
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
1556 1557 1558 1559 1560

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1561
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1562 1563 1564 1565 1566 1567 1568 1569 1570

	return result;
}

Datum
OidFunctionCall7(Oid functionId, Datum arg1, Datum arg2,
				 Datum arg3, Datum arg4, Datum arg5,
				 Datum arg6, Datum arg7)
{
B
Bruce Momjian 已提交
1571 1572 1573
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1574 1575 1576

	fmgr_info(functionId, &flinfo);

1577
	InitFunctionCallInfoData(fcinfo, &flinfo, 7, NULL, NULL);
1578

1579 1580 1581 1582 1583 1584 1585
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
1586 1587 1588 1589 1590 1591 1592
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
1593 1594 1595 1596 1597

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1598
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1599 1600 1601 1602 1603 1604 1605 1606 1607

	return result;
}

Datum
OidFunctionCall8(Oid functionId, Datum arg1, Datum arg2,
				 Datum arg3, Datum arg4, Datum arg5,
				 Datum arg6, Datum arg7, Datum arg8)
{
B
Bruce Momjian 已提交
1608 1609 1610
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1611 1612 1613

	fmgr_info(functionId, &flinfo);

1614
	InitFunctionCallInfoData(fcinfo, &flinfo, 8, NULL, NULL);
1615

1616 1617 1618 1619 1620 1621 1622 1623
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
	fcinfo.arg[7] = arg8;
1624 1625 1626 1627 1628 1629 1630 1631
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
	fcinfo.argnull[7] = false;
1632 1633 1634 1635 1636

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1637
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647

	return result;
}

Datum
OidFunctionCall9(Oid functionId, Datum arg1, Datum arg2,
				 Datum arg3, Datum arg4, Datum arg5,
				 Datum arg6, Datum arg7, Datum arg8,
				 Datum arg9)
{
B
Bruce Momjian 已提交
1648 1649 1650
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	Datum		result;
1651 1652 1653

	fmgr_info(functionId, &flinfo);

1654
	InitFunctionCallInfoData(fcinfo, &flinfo, 9, NULL, NULL);
1655

1656 1657 1658 1659 1660 1661 1662 1663 1664
	fcinfo.arg[0] = arg1;
	fcinfo.arg[1] = arg2;
	fcinfo.arg[2] = arg3;
	fcinfo.arg[3] = arg4;
	fcinfo.arg[4] = arg5;
	fcinfo.arg[5] = arg6;
	fcinfo.arg[6] = arg7;
	fcinfo.arg[7] = arg8;
	fcinfo.arg[8] = arg9;
1665 1666 1667 1668 1669 1670 1671 1672 1673
	fcinfo.argnull[0] = false;
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;
	fcinfo.argnull[3] = false;
	fcinfo.argnull[4] = false;
	fcinfo.argnull[5] = false;
	fcinfo.argnull[6] = false;
	fcinfo.argnull[7] = false;
	fcinfo.argnull[8] = false;
1674 1675 1676 1677 1678

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1679
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1680 1681 1682 1683 1684

	return result;
}


1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
/*
 * Special cases for convenient invocation of datatype I/O functions.
 */

/*
 * Call a previously-looked-up datatype input function.
 *
 * "str" may be NULL to indicate we are reading a NULL.  In this case
 * the caller should assume the result is NULL, but we'll call the input
 * function anyway if it's not strict.  So this is almost but not quite
 * the same as FunctionCall3.
 */
Datum
InputFunctionCall(FmgrInfo *flinfo, char *str, Oid typioparam, int32 typmod)
{
	FunctionCallInfoData fcinfo;
	Datum		result;

	if (str == NULL && flinfo->fn_strict)
		return (Datum) 0;		/* just return null result */

	InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);

	fcinfo.arg[0] = CStringGetDatum(str);
	fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
	fcinfo.arg[2] = Int32GetDatum(typmod);
	fcinfo.argnull[0] = (str == NULL);
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;

	result = FunctionCallInvoke(&fcinfo);

	/* Should get null result if and only if str is NULL */
	if (str == NULL)
	{
		if (!fcinfo.isnull)
			elog(ERROR, "input function %u returned non-NULL",
				 fcinfo.flinfo->fn_oid);
	}
	else
	{
		if (fcinfo.isnull)
			elog(ERROR, "input function %u returned NULL",
				 fcinfo.flinfo->fn_oid);
	}

	return result;
}

/*
 * Call a previously-looked-up datatype output function.
 *
 * Do not call this on NULL datums.
 *
 * This is mere window dressing for FunctionCall1, but its use is recommended
 * anyway so that code invoking output functions can be identified easily.
 */
char *
OutputFunctionCall(FmgrInfo *flinfo, Datum val)
{
	return DatumGetCString(FunctionCall1(flinfo, val));
}

/*
 * Call a previously-looked-up datatype binary-input function.
 *
 * "buf" may be NULL to indicate we are reading a NULL.  In this case
 * the caller should assume the result is NULL, but we'll call the receive
 * function anyway if it's not strict.  So this is almost but not quite
 * the same as FunctionCall3.
 */
Datum
ReceiveFunctionCall(FmgrInfo *flinfo, StringInfo buf,
					Oid typioparam, int32 typmod)
{
	FunctionCallInfoData fcinfo;
	Datum		result;

	if (buf == NULL && flinfo->fn_strict)
		return (Datum) 0;		/* just return null result */

	InitFunctionCallInfoData(fcinfo, flinfo, 3, NULL, NULL);

	fcinfo.arg[0] = PointerGetDatum(buf);
	fcinfo.arg[1] = ObjectIdGetDatum(typioparam);
	fcinfo.arg[2] = Int32GetDatum(typmod);
	fcinfo.argnull[0] = (buf == NULL);
	fcinfo.argnull[1] = false;
	fcinfo.argnull[2] = false;

	result = FunctionCallInvoke(&fcinfo);

	/* Should get null result if and only if buf is NULL */
	if (buf == NULL)
	{
		if (!fcinfo.isnull)
			elog(ERROR, "receive function %u returned non-NULL",
				 fcinfo.flinfo->fn_oid);
	}
	else
	{
		if (fcinfo.isnull)
			elog(ERROR, "receive function %u returned NULL",
				 fcinfo.flinfo->fn_oid);
	}

	return result;
}

/*
 * Call a previously-looked-up datatype binary-output function.
 *
 * Do not call this on NULL datums.
 *
 * This is little more than window dressing for FunctionCall1, but its use is
 * recommended anyway so that code invoking output functions can be identified
 * easily.  Note however that it does guarantee a non-toasted result.
 */
bytea *
SendFunctionCall(FmgrInfo *flinfo, Datum val)
{
	return DatumGetByteaP(FunctionCall1(flinfo, val));
}

/*
 * As above, for I/O functions identified by OID.  These are only to be used
 * in seldom-executed code paths.  They are not only slow but leak memory.
 */
Datum
OidInputFunctionCall(Oid functionId, char *str, Oid typioparam, int32 typmod)
{
	FmgrInfo	flinfo;

	fmgr_info(functionId, &flinfo);
	return InputFunctionCall(&flinfo, str, typioparam, typmod);
}

char *
OidOutputFunctionCall(Oid functionId, Datum val)
{
	FmgrInfo	flinfo;

	fmgr_info(functionId, &flinfo);
	return OutputFunctionCall(&flinfo, val);
}

Datum
OidReceiveFunctionCall(Oid functionId, StringInfo buf,
					   Oid typioparam, int32 typmod)
{
	FmgrInfo	flinfo;

	fmgr_info(functionId, &flinfo);
	return ReceiveFunctionCall(&flinfo, buf, typioparam, typmod);
}

bytea *
OidSendFunctionCall(Oid functionId, Datum val)
{
	FmgrInfo	flinfo;

	fmgr_info(functionId, &flinfo);
	return SendFunctionCall(&flinfo, val);
}


1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
/*
 * !!! OLD INTERFACE !!!
 *
 * fmgr() is the only remaining vestige of the old-style caller support
 * functions.  It's no longer used anywhere in the Postgres distribution,
 * but we should leave it around for a release or two to ease the transition
 * for user-supplied C functions.  OidFunctionCallN() replaces it for new
 * code.
 *
 * DEPRECATED, DO NOT USE IN NEW CODE
 */
char *
fmgr(Oid procedureId,...)
{
B
Bruce Momjian 已提交
1865 1866 1867 1868
	FmgrInfo	flinfo;
	FunctionCallInfoData fcinfo;
	int			n_arguments;
	Datum		result;
1869 1870 1871 1872

	fmgr_info(procedureId, &flinfo);

	MemSet(&fcinfo, 0, sizeof(fcinfo));
B
Bruce Momjian 已提交
1873
	fcinfo.flinfo = &flinfo;
1874 1875 1876 1877 1878 1879 1880 1881 1882
	fcinfo.nargs = flinfo.fn_nargs;
	n_arguments = fcinfo.nargs;

	if (n_arguments > 0)
	{
		va_list		pvar;
		int			i;

		if (n_arguments > FUNC_MAX_ARGS)
1883 1884
			ereport(ERROR,
					(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
B
Bruce Momjian 已提交
1885 1886
			 errmsg("function %u has too many arguments (%d, maximum is %d)",
					flinfo.fn_oid, n_arguments, FUNC_MAX_ARGS)));
1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
		va_start(pvar, procedureId);
		for (i = 0; i < n_arguments; i++)
			fcinfo.arg[i] = (Datum) va_arg(pvar, char *);
		va_end(pvar);
	}

	result = FunctionCallInvoke(&fcinfo);

	/* Check for null result, since caller is clearly not expecting one */
	if (fcinfo.isnull)
1897
		elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
1898 1899 1900 1901 1902

	return (char *) result;
}


1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
/*-------------------------------------------------------------------------
 *		Support routines for standard pass-by-reference datatypes
 *
 * Note: at some point, at least on some platforms, these might become
 * pass-by-value types.  Obviously Datum must be >= 8 bytes to allow
 * int64 or float8 to be pass-by-value.  I think that Float4GetDatum
 * and Float8GetDatum will need to be out-of-line routines anyway,
 * since just casting from float to Datum will not do the right thing;
 * some kind of trick with pointer-casting or a union will be needed.
 *-------------------------------------------------------------------------
 */

Datum
Int64GetDatum(int64 X)
{
1918
#ifndef INT64_IS_BUSTED
1919 1920 1921 1922
	int64	   *retval = (int64 *) palloc(sizeof(int64));

	*retval = X;
	return PointerGetDatum(retval);
1923 1924
#else							/* INT64_IS_BUSTED */

1925
	/*
B
Bruce Momjian 已提交
1926 1927 1928 1929
	 * On a machine with no 64-bit-int C datatype, sizeof(int64) will not be
	 * 8, but we want Int64GetDatum to return an 8-byte object anyway, with
	 * zeroes in the unused bits.  This is needed so that, for example, hash
	 * join of int8 will behave properly.
1930
	 */
1931
	int64	   *retval = (int64 *) palloc0(Max(sizeof(int64), 8));
1932 1933 1934

	*retval = X;
	return PointerGetDatum(retval);
1935
#endif   /* INT64_IS_BUSTED */
1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950
}

Datum
Float4GetDatum(float4 X)
{
	float4	   *retval = (float4 *) palloc(sizeof(float4));

	*retval = X;
	return PointerGetDatum(retval);
}

Datum
Float8GetDatum(float8 X)
{
	float8	   *retval = (float8 *) palloc(sizeof(float8));
1951

1952 1953
	*retval = X;
	return PointerGetDatum(retval);
1954
}
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977

/*-------------------------------------------------------------------------
 *		Support routines for toastable datatypes
 *-------------------------------------------------------------------------
 */

struct varlena *
pg_detoast_datum(struct varlena * datum)
{
	if (VARATT_IS_EXTENDED(datum))
		return (struct varlena *) heap_tuple_untoast_attr((varattrib *) datum);
	else
		return datum;
}

struct varlena *
pg_detoast_datum_copy(struct varlena * datum)
{
	if (VARATT_IS_EXTENDED(datum))
		return (struct varlena *) heap_tuple_untoast_attr((varattrib *) datum);
	else
	{
		/* Make a modifiable copy of the varlena object */
B
Bruce Momjian 已提交
1978
		Size		len = VARSIZE(datum);
1979 1980 1981 1982 1983 1984
		struct varlena *result = (struct varlena *) palloc(len);

		memcpy(result, datum, len);
		return result;
	}
}
1985 1986 1987 1988 1989 1990 1991

struct varlena *
pg_detoast_datum_slice(struct varlena * datum, int32 first, int32 count)
{
	/* Only get the specified portion from the toast rel */
	return (struct varlena *) heap_tuple_untoast_attr_slice((varattrib *) datum, first, count);
}
1992 1993 1994

/*-------------------------------------------------------------------------
 *		Support routines for extracting info from fn_expr parse tree
1995 1996 1997
 *
 * These are needed by polymorphic functions, which accept multiple possible
 * input types and need help from the parser to know what they've got.
1998 1999 2000 2001
 *-------------------------------------------------------------------------
 */

/*
2002
 * Get the actual type OID of the function return type
2003 2004 2005 2006
 *
 * Returns InvalidOid if information is not available
 */
Oid
2007
get_fn_expr_rettype(FmgrInfo *flinfo)
2008
{
B
Bruce Momjian 已提交
2009
	Node	   *expr;
2010 2011

	/*
B
Bruce Momjian 已提交
2012 2013
	 * can't return anything useful if we have no FmgrInfo or if its fn_expr
	 * node has not been initialized
2014
	 */
2015
	if (!flinfo || !flinfo->fn_expr)
2016 2017
		return InvalidOid;

2018
	expr = flinfo->fn_expr;
2019 2020 2021 2022 2023

	return exprType(expr);
}

/*
2024
 * Get the actual type OID of a specific function argument (counting from 0)
2025 2026 2027 2028
 *
 * Returns InvalidOid if information is not available
 */
Oid
2029
get_fn_expr_argtype(FmgrInfo *flinfo, int argnum)
2030 2031
{
	/*
B
Bruce Momjian 已提交
2032 2033
	 * can't return anything useful if we have no FmgrInfo or if its fn_expr
	 * node has not been initialized
2034
	 */
2035
	if (!flinfo || !flinfo->fn_expr)
2036 2037
		return InvalidOid;

2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054
	return get_call_expr_argtype(flinfo->fn_expr, argnum);
}

/*
 * Get the actual type OID of a specific function argument (counting from 0),
 * but working from the calling expression tree instead of FmgrInfo
 *
 * Returns InvalidOid if information is not available
 */
Oid
get_call_expr_argtype(Node *expr, int argnum)
{
	List	   *args;
	Oid			argtype;

	if (expr == NULL)
		return InvalidOid;
2055 2056 2057 2058 2059

	if (IsA(expr, FuncExpr))
		args = ((FuncExpr *) expr)->args;
	else if (IsA(expr, OpExpr))
		args = ((OpExpr *) expr)->args;
2060 2061 2062 2063 2064 2065
	else if (IsA(expr, DistinctExpr))
		args = ((DistinctExpr *) expr)->args;
	else if (IsA(expr, ScalarArrayOpExpr))
		args = ((ScalarArrayOpExpr *) expr)->args;
	else if (IsA(expr, NullIfExpr))
		args = ((NullIfExpr *) expr)->args;
2066 2067 2068
	else
		return InvalidOid;

2069
	if (argnum < 0 || argnum >= list_length(args))
2070 2071
		return InvalidOid;

2072
	argtype = exprType((Node *) list_nth(args, argnum));
2073 2074

	/*
B
Bruce Momjian 已提交
2075 2076
	 * special hack for ScalarArrayOpExpr: what the underlying function will
	 * actually get passed is the element type of the array.
2077 2078 2079 2080 2081 2082
	 */
	if (IsA(expr, ScalarArrayOpExpr) &&
		argnum == 1)
		argtype = get_element_type(argtype);

	return argtype;
2083
}