pg_proc.c 20.5 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * pg_proc.c
4
 *	  routines to support manipulation of the pg_proc relation
5
 *
Bruce Momjian's avatar
Bruce Momjian committed
6
 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
Bruce Momjian's avatar
Add:  
Bruce Momjian committed
7
 * Portions Copyright (c) 1994, Regents of the University of California
8 9 10
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.101 2003/07/21 01:59:11 tgl Exp $
12 13 14
 *
 *-------------------------------------------------------------------------
 */
Bruce Momjian's avatar
Bruce Momjian committed
15 16 17 18
#include "postgres.h"

#include "access/heapam.h"
#include "catalog/catname.h"
19
#include "catalog/dependency.h"
Bruce Momjian's avatar
Bruce Momjian committed
20
#include "catalog/indexing.h"
21
#include "catalog/pg_language.h"
Bruce Momjian's avatar
Bruce Momjian committed
22
#include "catalog/pg_proc.h"
23
#include "executor/executor.h"
24
#include "fmgr.h"
Bruce Momjian's avatar
Bruce Momjian committed
25
#include "miscadmin.h"
26
#include "parser/parse_coerce.h"
27
#include "parser/parse_expr.h"
Bruce Momjian's avatar
Bruce Momjian committed
28
#include "parser/parse_type.h"
Bruce Momjian's avatar
Bruce Momjian committed
29 30 31 32 33 34
#include "tcop/tcopprot.h"
#include "utils/builtins.h"
#include "utils/lsyscache.h"
#include "utils/sets.h"
#include "utils/syscache.h"

35

Bruce Momjian's avatar
Bruce Momjian committed
36 37 38
Datum		fmgr_internal_validator(PG_FUNCTION_ARGS);
Datum		fmgr_c_validator(PG_FUNCTION_ARGS);
Datum		fmgr_sql_validator(PG_FUNCTION_ARGS);
39 40


41
/* ----------------------------------------------------------------
42
 *		ProcedureCreate
43 44 45
 * ----------------------------------------------------------------
 */
Oid
46
ProcedureCreate(const char *procedureName,
47
				Oid procNamespace,
48
				bool replace,
49
				bool returnsSet,
50
				Oid returnType,
51
				Oid languageObjectId,
52
				Oid languageValidator,
53 54
				const char *prosrc,
				const char *probin,
55
				bool isAgg,
56
				bool security_definer,
57
				bool isStrict,
58
				char volatility,
59 60
				int parameterCount,
				const Oid *parameterTypes)
61
{
62
	int			i;
63
	Relation	rel;
64
	HeapTuple	tup;
65
	HeapTuple	oldtup;
66 67
	char		nulls[Natts_pg_proc];
	Datum		values[Natts_pg_proc];
68
	char		replaces[Natts_pg_proc];
69
	Oid			typev[FUNC_MAX_ARGS];
70
	Oid			relid;
71
	NameData	procname;
72
	TupleDesc	tupDesc;
73
	Oid			retval;
74
	bool		is_update;
Bruce Momjian's avatar
Bruce Momjian committed
75 76
	ObjectAddress myself,
				referenced;
77

78 79
	/*
	 * sanity checks
80
	 */
81 82 83
	Assert(PointerIsValid(prosrc));
	Assert(PointerIsValid(probin));

84
	if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
85 86 87 88
		ereport(ERROR,
				(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
				 errmsg("functions cannot have more than %d arguments",
						FUNC_MAX_ARGS)));
89

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
	/*
	 * Do not allow return type ANYARRAY or ANYELEMENT unless at least one
	 * argument is also ANYARRAY or ANYELEMENT
	 */
	if (returnType == ANYARRAYOID || returnType == ANYELEMENTOID)
	{
		bool	genericParam = false;

		for (i = 0; i < parameterCount; i++)
		{
			if (parameterTypes[i] == ANYARRAYOID ||
				parameterTypes[i] == ANYELEMENTOID)
			{
				genericParam = true;
				break;
			}
		}

		if (!genericParam)
109 110
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
111 112
					 errmsg("cannot determine result datatype"),
					 errdetail("A function returning ANYARRAY or ANYELEMENT must have at least one argument of either type.")));
113 114
	}

115 116 117 118
	/* Make sure we have a zero-padded param type array */
	MemSet(typev, 0, FUNC_MAX_ARGS * sizeof(Oid));
	if (parameterCount > 0)
		memcpy(typev, parameterTypes, parameterCount * sizeof(Oid));
119

120
	if (languageObjectId == SQLlanguageId)
121 122 123 124 125 126 127
	{
		/*
		 * If this call is defining a set, check if the set is already
		 * defined by looking to see whether this call's function text
		 * matches a function already in pg_proc.  If so just return the
		 * OID of the existing set.
		 */
128
		if (strcmp(procedureName, GENERICSETNAME) == 0)
129
		{
130
#ifdef SETS_FIXED
131 132 133 134 135 136 137 138

			/*
			 * The code below doesn't work any more because the PROSRC
			 * system cache and the pg_proc_prosrc_index have been
			 * removed. Instead a sequential heap scan or something better
			 * must get implemented. The reason for removing is that
			 * nbtree index crashes if sources exceed 2K --- what's likely
			 * for procedural languages.
139 140 141 142 143
			 *
			 * 1999/09/30 Jan
			 */
			text	   *prosrctext;

144
			prosrctext = DatumGetTextP(DirectFunctionCall1(textin,
145
											   CStringGetDatum(prosrc)));
146 147 148
			retval = GetSysCacheOid(PROSRC,
									PointerGetDatum(prosrctext),
									0, 0, 0);
Bruce Momjian's avatar
Bruce Momjian committed
149
			pfree(prosrctext);
150 151
			if (OidIsValid(retval))
				return retval;
152 153
#else
			elog(ERROR, "lookup for procedure by source needs fix (Jan)");
154
#endif   /* SETS_FIXED */
155
		}
156
	}
157 158 159 160 161

	/*
	 * don't allow functions of complex types that have the same name as
	 * existing attributes of the type
	 */
162
	if (parameterCount == 1 && OidIsValid(typev[0]) &&
163
		(relid = typeidTypeRelid(typev[0])) != InvalidOid &&
164
		get_attnum(relid, (char *) procedureName) != InvalidAttrNumber)
165 166 167 168
		ereport(ERROR,
				(errcode(ERRCODE_DUPLICATE_COLUMN),
				 errmsg("\"%s\" is already an attribute of type %s",
						procedureName, format_type_be(typev[0]))));
169

170
	/*
171
	 * All seems OK; prepare the data to be inserted into pg_proc.
172 173
	 */

174
	for (i = 0; i < Natts_pg_proc; ++i)
175
	{
176 177
		nulls[i] = ' ';
		values[i] = (Datum) NULL;
178
		replaces[i] = 'r';
179 180
	}

181
	i = 0;
182
	namestrcpy(&procname, procedureName);
183
	values[i++] = NameGetDatum(&procname);		/* proname */
Bruce Momjian's avatar
Bruce Momjian committed
184
	values[i++] = ObjectIdGetDatum(procNamespace);		/* pronamespace */
185
	values[i++] = Int32GetDatum(GetUserId());	/* proowner */
Bruce Momjian's avatar
Bruce Momjian committed
186 187 188
	values[i++] = ObjectIdGetDatum(languageObjectId);	/* prolang */
	values[i++] = BoolGetDatum(isAgg);	/* proisagg */
	values[i++] = BoolGetDatum(security_definer);		/* prosecdef */
189
	values[i++] = BoolGetDatum(isStrict);		/* proisstrict */
190
	values[i++] = BoolGetDatum(returnsSet);		/* proretset */
191
	values[i++] = CharGetDatum(volatility);		/* provolatile */
Bruce Momjian's avatar
Bruce Momjian committed
192 193
	values[i++] = UInt16GetDatum(parameterCount);		/* pronargs */
	values[i++] = ObjectIdGetDatum(returnType); /* prorettype */
194
	values[i++] = PointerGetDatum(typev);		/* proargtypes */
195 196 197 198
	values[i++] = DirectFunctionCall1(textin,	/* prosrc */
									  CStringGetDatum(prosrc));
	values[i++] = DirectFunctionCall1(textin,	/* probin */
									  CStringGetDatum(probin));
199 200
	/* proacl will be handled below */

201
	rel = heap_openr(ProcedureRelationName, RowExclusiveLock);
202
	tupDesc = rel->rd_att;
203

204
	/* Check for pre-existing definition */
205
	oldtup = SearchSysCache(PROCNAMENSP,
206 207 208
							PointerGetDatum(procedureName),
							UInt16GetDatum(parameterCount),
							PointerGetDatum(typev),
209
							ObjectIdGetDatum(procNamespace));
210

211 212 213
	if (HeapTupleIsValid(oldtup))
	{
		/* There is one; okay to replace it? */
214
		Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
215 216

		if (!replace)
217 218 219 220
			ereport(ERROR,
					(errcode(ERRCODE_DUPLICATE_FUNCTION),
					 errmsg("function \"%s\" already exists with same argument types",
							procedureName)));
221
		if (GetUserId() != oldproc->proowner && !superuser())
222 223 224 225
			ereport(ERROR,
					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
					 errmsg("you do not have permission to replace function \"%s\"",
							procedureName)));
226

227 228 229 230
		/*
		 * Not okay to change the return type of the existing proc, since
		 * existing rules, views, etc may depend on the return type.
		 */
231
		if (returnType != oldproc->prorettype ||
232
			returnsSet != oldproc->proretset)
233 234 235 236
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
					 errmsg("cannot change return type of existing function"),
					 errhint("Use DROP FUNCTION first.")));
237

238 239 240 241
		/* Can't change aggregate status, either */
		if (oldproc->proisagg != isAgg)
		{
			if (oldproc->proisagg)
242 243 244 245
				ereport(ERROR,
						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
						 errmsg("function \"%s\" is an aggregate",
								procedureName)));
246
			else
247 248 249 250
				ereport(ERROR,
						(errcode(ERRCODE_WRONG_OBJECT_TYPE),
						 errmsg("function \"%s\" is not an aggregate",
								procedureName)));
251 252
		}

Tom Lane's avatar
Tom Lane committed
253
		/* do not change existing ownership or permissions, either */
Bruce Momjian's avatar
Bruce Momjian committed
254 255
		replaces[Anum_pg_proc_proowner - 1] = ' ';
		replaces[Anum_pg_proc_proacl - 1] = ' ';
256

257
		/* Okay, do it... */
258 259
		tup = heap_modifytuple(oldtup, rel, values, nulls, replaces);
		simple_heap_update(rel, &tup->t_self, tup);
260 261

		ReleaseSysCache(oldtup);
262
		is_update = true;
263 264 265 266
	}
	else
	{
		/* Creating a new procedure */
267 268

		/* start out with empty permissions */
Bruce Momjian's avatar
Bruce Momjian committed
269
		nulls[Anum_pg_proc_proacl - 1] = 'n';
270

271
		tup = heap_formtuple(tupDesc, values, nulls);
272
		simple_heap_insert(rel, tup);
273
		is_update = false;
274 275
	}

276 277
	/* Need to update indexes for either the insert or update case */
	CatalogUpdateIndexes(rel, tup);
278

279
	retval = HeapTupleGetOid(tup);
280 281 282 283 284 285 286 287 288 289 290 291

	/*
	 * Create dependencies for the new function.  If we are updating an
	 * existing function, first delete any existing pg_depend entries.
	 */
	if (is_update)
		deleteDependencyRecordsFor(RelOid_pg_proc, retval);

	myself.classId = RelOid_pg_proc;
	myself.objectId = retval;
	myself.objectSubId = 0;

292 293 294 295 296 297
	/* dependency on namespace */
	referenced.classId = get_system_catalog_relid(NamespaceRelationName);
	referenced.objectId = procNamespace;
	referenced.objectSubId = 0;
	recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);

298 299 300 301 302 303 304
	/* dependency on implementation language */
	referenced.classId = get_system_catalog_relid(LanguageRelationName);
	referenced.objectId = languageObjectId;
	referenced.objectSubId = 0;
	recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);

	/* dependency on return type */
305 306 307 308
	referenced.classId = RelOid_pg_type;
	referenced.objectId = returnType;
	referenced.objectSubId = 0;
	recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
309 310 311 312

	/* dependency on input types */
	for (i = 0; i < parameterCount; i++)
	{
313 314 315 316
		referenced.classId = RelOid_pg_type;
		referenced.objectId = typev[i];
		referenced.objectSubId = 0;
		recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
317 318
	}

319
	heap_freetuple(tup);
320 321 322

	heap_close(rel, RowExclusiveLock);

323 324 325
	/* Verify function body */
	if (OidIsValid(languageValidator))
	{
Tom Lane's avatar
Tom Lane committed
326
		/* Advance command counter so new tuple can be seen by validator */
327
		CommandCounterIncrement();
Tom Lane's avatar
Tom Lane committed
328
		OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
329 330
	}

331
	return retval;
332
}
333 334

/*
335
 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
336 337
 *
 * The return value of a sql function is the value returned by
338 339 340 341 342 343 344 345
 * the final query in the function.  We do some ad-hoc type checking here
 * to be sure that the user is returning the type he claims.
 *
 * This is normally applied during function definition, but in the case
 * of a function with polymorphic arguments, we instead apply it during
 * function execution startup.  The rettype is then the actual resolved
 * output type of the function, rather than the declared type.  (Therefore,
 * we should never see ANYARRAY or ANYELEMENT as rettype.)
346
 */
347 348
void
check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
349 350 351 352 353 354
{
	Query	   *parse;
	int			cmd;
	List	   *tlist;
	List	   *tlistitem;
	int			tlistlen;
355
	Oid			typerelid;
356
	Oid			restype;
357
	Relation	reln;
358 359 360
	int			relnatts;		/* physical number of columns in rel */
	int			rellogcols;		/* # of nondeleted columns in rel */
	int			colindex;		/* physical column index */
361

362
	/* guard against empty function body; OK only if void return type */
363 364
	if (queryTreeList == NIL)
	{
365
		if (rettype != VOIDOID)
366 367 368 369 370
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
					 errmsg("return type mismatch in function declared to return %s",
							format_type_be(rettype)),
					 errdetail("Function's final statement must be a SELECT.")));
371 372 373
		return;
	}

374 375 376 377 378 379 380
	/* find the final query */
	parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);

	cmd = parse->commandType;
	tlist = parse->targetList;

	/*
Bruce Momjian's avatar
Bruce Momjian committed
381 382
	 * The last query must be a SELECT if and only if return type isn't
	 * VOID.
383
	 */
384
	if (rettype == VOIDOID)
385 386
	{
		if (cmd == CMD_SELECT)
387 388 389 390 391
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
					 errmsg("return type mismatch in function declared to return %s",
							format_type_be(rettype)),
					 errdetail("Function's final statement must not be a SELECT.")));
392 393 394 395 396
		return;
	}

	/* by here, the function is declared to return some type */
	if (cmd != CMD_SELECT)
397 398 399 400 401
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
				 errmsg("return type mismatch in function declared to return %s",
						format_type_be(rettype)),
				 errdetail("Function's final statement must be a SELECT.")));
402 403 404 405 406 407

	/*
	 * Count the non-junk entries in the result targetlist.
	 */
	tlistlen = ExecCleanTargetListLength(tlist);

408
	typerelid = typeidTypeRelid(rettype);
409

410
	if (fn_typtype == 'b' || fn_typtype == 'd')
411
	{
412 413 414
		/* Shouldn't have a typerelid */
		Assert(typerelid == InvalidOid);

415 416
		/*
		 * For base-type returns, the target list should have exactly one
Bruce Momjian's avatar
Bruce Momjian committed
417 418
		 * entry, and its type should agree with what the user declared.
		 * (As of Postgres 7.2, we accept binary-compatible types too.)
419
		 */
420
		if (tlistlen != 1)
421 422 423 424 425
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
					 errmsg("return type mismatch in function declared to return %s",
							format_type_be(rettype)),
					 errdetail("Final SELECT must return exactly one column.")));
426

427
		restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
428
		if (!IsBinaryCoercible(restype, rettype))
429 430 431 432 433 434
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
					 errmsg("return type mismatch in function declared to return %s",
							format_type_be(rettype)),
					 errdetail("Actual return type is %s.",
							   format_type_be(restype))));
435 436 437 438 439
	}
	else if (fn_typtype == 'c')
	{
		/* Must have a typerelid */
		Assert(typerelid != InvalidOid);
440

441
		/*
Bruce Momjian's avatar
Bruce Momjian committed
442 443 444 445 446
		 * If the target list is of length 1, and the type of the varnode
		 * in the target list matches the declared return type, this is
		 * okay. This can happen, for example, where the body of the
		 * function is 'SELECT func2()', where func2 has the same return
		 * type as the function that's calling it.
447 448 449 450
		 */
		if (tlistlen == 1)
		{
			restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
451
			if (IsBinaryCoercible(restype, rettype))
452 453
				return;
		}
454

455
		/*
Bruce Momjian's avatar
Bruce Momjian committed
456 457 458 459 460
		 * Otherwise verify that the targetlist matches the return tuple
		 * type. This part of the typechecking is a hack. We look up the
		 * relation that is the declared return type, and scan the
		 * non-deleted attributes to ensure that they match the datatypes
		 * of the non-resjunk columns.
461
		 */
462
		reln = relation_open(typerelid, AccessShareLock);
463
		relnatts = reln->rd_rel->relnatts;
Bruce Momjian's avatar
Bruce Momjian committed
464
		rellogcols = 0;			/* we'll count nondeleted cols as we go */
465 466 467 468 469 470 471 472 473 474 475 476
		colindex = 0;

		foreach(tlistitem, tlist)
		{
			TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
			Form_pg_attribute attr;
			Oid			tletype;
			Oid			atttype;

			if (tle->resdom->resjunk)
				continue;

Bruce Momjian's avatar
Bruce Momjian committed
477 478
			do
			{
479 480
				colindex++;
				if (colindex > relnatts)
481 482 483 484 485
					ereport(ERROR,
							(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
							 errmsg("return type mismatch in function declared to return %s",
									format_type_be(rettype)),
							 errdetail("Final SELECT returns too many columns.")));
486 487 488
				attr = reln->rd_att->attrs[colindex - 1];
			} while (attr->attisdropped);
			rellogcols++;
489

490
			tletype = exprType((Node *) tle->expr);
491
			atttype = attr->atttypid;
492
			if (!IsBinaryCoercible(tletype, atttype))
493 494 495 496 497 498 499 500
				ereport(ERROR,
						(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
						 errmsg("return type mismatch in function declared to return %s",
								format_type_be(rettype)),
						 errdetail("Final SELECT returns %s instead of %s at column %d.",
								   format_type_be(tletype),
								   format_type_be(atttype),
								   rellogcols)));
501
		}
502

503 504
		for (;;)
		{
505 506
			colindex++;
			if (colindex > relnatts)
507 508 509 510
				break;
			if (!reln->rd_att->attrs[colindex - 1]->attisdropped)
				rellogcols++;
		}
511

512
		if (tlistlen != rellogcols)
513 514 515 516 517
			ereport(ERROR,
					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
					 errmsg("return type mismatch in function declared to return %s",
							format_type_be(rettype)),
					 errdetail("Final SELECT returns too few columns.")));
518

519
		relation_close(reln, AccessShareLock);
520
	}
521
	else if (rettype == RECORDOID)
522
	{
523 524 525
		/* Shouldn't have a typerelid */
		Assert(typerelid == InvalidOid);

526
		/*
Bruce Momjian's avatar
Bruce Momjian committed
527 528
		 * For RECORD return type, defer this check until we get the first
		 * tuple.
529 530
		 */
	}
531 532
	else if (rettype == ANYARRAYOID || rettype == ANYELEMENTOID)
	{
533 534 535
		/* This should already have been caught ... */
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
536 537
				 errmsg("cannot determine result datatype"),
				 errdetail("A function returning ANYARRAY or ANYELEMENT must have at least one argument of either type.")));
538
	}
539
	else
540 541 542 543
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
				 errmsg("return type %s is not supported for SQL functions",
						format_type_be(rettype))));
544
}
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563



/*
 * Validator for internal functions
 *
 * Check that the given internal function name (the "prosrc" value) is
 * a known builtin function.
 */
Datum
fmgr_internal_validator(PG_FUNCTION_ARGS)
{
	Oid			funcoid = PG_GETARG_OID(0);
	HeapTuple	tuple;
	Form_pg_proc proc;
	bool		isnull;
	Datum		tmp;
	char	   *prosrc;

564 565 566
	tuple = SearchSysCache(PROCOID,
						   ObjectIdGetDatum(funcoid),
						   0, 0, 0);
567
	if (!HeapTupleIsValid(tuple))
568
		elog(ERROR, "cache lookup failed for function %u", funcoid);
569 570 571 572 573 574 575 576
	proc = (Form_pg_proc) GETSTRUCT(tuple);

	tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
	if (isnull)
		elog(ERROR, "null prosrc");
	prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));

	if (fmgr_internal_function(prosrc) == InvalidOid)
577 578 579 580
		ereport(ERROR,
				(errcode(ERRCODE_UNDEFINED_FUNCTION),
				 errmsg("there is no built-in function named \"%s\"",
						prosrc)));
581 582

	ReleaseSysCache(tuple);
583 584

	PG_RETURN_VOID();
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
}



/*
 * Validator for C language functions
 *
 * Make sure that the library file exists, is loadable, and contains
 * the specified link symbol. Also check for a valid function
 * information record.
 */
Datum
fmgr_c_validator(PG_FUNCTION_ARGS)
{
	Oid			funcoid = PG_GETARG_OID(0);
	void	   *libraryhandle;
	HeapTuple	tuple;
	Form_pg_proc proc;
	bool		isnull;
	Datum		tmp;
	char	   *prosrc;
	char	   *probin;

608 609 610
	tuple = SearchSysCache(PROCOID,
						   ObjectIdGetDatum(funcoid),
						   0, 0, 0);
611
	if (!HeapTupleIsValid(tuple))
612
		elog(ERROR, "cache lookup failed for function %u", funcoid);
613 614 615 616 617 618 619 620 621 622 623
	proc = (Form_pg_proc) GETSTRUCT(tuple);

	tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
	if (isnull)
		elog(ERROR, "null prosrc");
	prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));

	tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_probin, &isnull);
	if (isnull)
		elog(ERROR, "null probin");
	probin = DatumGetCString(DirectFunctionCall1(textout, tmp));
Bruce Momjian's avatar
Bruce Momjian committed
624

625 626 627 628 629
	(void) load_external_function(probin, prosrc, true, &libraryhandle);
	(void) fetch_finfo_record(libraryhandle, prosrc);

	ReleaseSysCache(tuple);

630 631
	PG_RETURN_VOID();
}
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649


/*
 * Validator for SQL language functions
 *
 * Parse it here in order to be sure that it contains no syntax
 * errors.
 */
Datum
fmgr_sql_validator(PG_FUNCTION_ARGS)
{
	Oid			funcoid = PG_GETARG_OID(0);
	HeapTuple	tuple;
	Form_pg_proc proc;
	List	   *querytree_list;
	bool		isnull;
	Datum		tmp;
	char	   *prosrc;
650
	char		functyptype;
651
	bool		haspolyarg;
652
	int			i;
653

654 655 656
	tuple = SearchSysCache(PROCOID,
						   ObjectIdGetDatum(funcoid),
						   0, 0, 0);
657
	if (!HeapTupleIsValid(tuple))
658
		elog(ERROR, "cache lookup failed for function %u", funcoid);
659 660
	proc = (Form_pg_proc) GETSTRUCT(tuple);

661 662
	functyptype = get_typtype(proc->prorettype);

663 664
	/* Disallow pseudotype result */
	/* except for RECORD, VOID, ANYARRAY, or ANYELEMENT */
665 666
	if (functyptype == 'p' &&
		proc->prorettype != RECORDOID &&
667 668 669
		proc->prorettype != VOIDOID &&
		proc->prorettype != ANYARRAYOID &&
		proc->prorettype != ANYELEMENTOID)
670 671 672 673
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
				 errmsg("SQL functions cannot return type %s",
						format_type_be(proc->prorettype))));
674

675 676 677
	/* Disallow pseudotypes in arguments */
	/* except for ANYARRAY or ANYELEMENT */
	haspolyarg = false;
678 679 680
	for (i = 0; i < proc->pronargs; i++)
	{
		if (get_typtype(proc->proargtypes[i]) == 'p')
681 682 683 684 685
		{
			if (proc->proargtypes[i] == ANYARRAYOID ||
				proc->proargtypes[i] == ANYELEMENTOID)
				haspolyarg = true;
			else
686 687 688 689
				ereport(ERROR,
						(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
						 errmsg("SQL functions cannot have arguments of type %s",
								format_type_be(proc->proargtypes[i]))));
690
		}
691
	}
692

693 694 695 696 697 698
	tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
	if (isnull)
		elog(ERROR, "null prosrc");

	prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));

699
	/*
700 701 702 703 704 705 706
	 * We can't do full prechecking of the function definition if there are
	 * any polymorphic input types, because actual datatypes of expression
	 * results will be unresolvable.  The check will be done at runtime
	 * instead.
	 *
	 * We can run the text through the raw parser though; this will at
	 * least catch silly syntactic errors.
707 708 709 710 711 712 713 714
	 */
	if (!haspolyarg)
	{
		querytree_list = pg_parse_and_rewrite(prosrc,
											  proc->proargtypes,
											  proc->pronargs);
		check_sql_fn_retval(proc->prorettype, functyptype, querytree_list);
	}
715 716 717 718
	else
	{
		querytree_list = pg_parse_query(prosrc);
	}
719 720

	ReleaseSysCache(tuple);
721 722

	PG_RETURN_VOID();
723
}