1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_proc relation
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.109 2003/10/03 19:26:49 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "access/heapam.h"
18 #include "catalog/catname.h"
19 #include "catalog/dependency.h"
20 #include "catalog/indexing.h"
21 #include "catalog/pg_language.h"
22 #include "catalog/pg_proc.h"
23 #include "executor/executor.h"
25 #include "miscadmin.h"
26 #include "parser/parse_coerce.h"
27 #include "parser/parse_expr.h"
28 #include "parser/parse_type.h"
29 #include "tcop/tcopprot.h"
30 #include "utils/acl.h"
31 #include "utils/builtins.h"
32 #include "utils/lsyscache.h"
33 #include "utils/sets.h"
34 #include "utils/syscache.h"
38 bool check_function_bodies = true;
41 Datum fmgr_internal_validator(PG_FUNCTION_ARGS);
42 Datum fmgr_c_validator(PG_FUNCTION_ARGS);
43 Datum fmgr_sql_validator(PG_FUNCTION_ARGS);
46 /* ----------------------------------------------------------------
48 * ----------------------------------------------------------------
51 ProcedureCreate(const char *procedureName,
57 Oid languageValidator,
61 bool security_definer,
65 const Oid *parameterTypes)
71 char nulls[Natts_pg_proc];
72 Datum values[Natts_pg_proc];
73 char replaces[Natts_pg_proc];
74 Oid typev[FUNC_MAX_ARGS];
86 Assert(PointerIsValid(prosrc));
87 Assert(PointerIsValid(probin));
89 if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
91 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
92 errmsg("functions cannot have more than %d arguments",
96 * Do not allow return type ANYARRAY or ANYELEMENT unless at least one
97 * argument is also ANYARRAY or ANYELEMENT
99 if (returnType == ANYARRAYOID || returnType == ANYELEMENTOID)
101 bool genericParam = false;
103 for (i = 0; i < parameterCount; i++)
105 if (parameterTypes[i] == ANYARRAYOID ||
106 parameterTypes[i] == ANYELEMENTOID)
115 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
116 errmsg("cannot determine result data type"),
117 errdetail("A function returning \"anyarray\" or \"anyelement\" must have at least one argument of either type.")));
120 /* Make sure we have a zero-padded param type array */
121 MemSet(typev, 0, FUNC_MAX_ARGS * sizeof(Oid));
122 if (parameterCount > 0)
123 memcpy(typev, parameterTypes, parameterCount * sizeof(Oid));
125 if (languageObjectId == SQLlanguageId)
128 * If this call is defining a set, check if the set is already
129 * defined by looking to see whether this call's function text
130 * matches a function already in pg_proc. If so just return the
131 * OID of the existing set.
133 if (strcmp(procedureName, GENERICSETNAME) == 0)
138 * The code below doesn't work any more because the PROSRC
139 * system cache and the pg_proc_prosrc_index have been
140 * removed. Instead a sequential heap scan or something better
141 * must get implemented. The reason for removing is that
142 * nbtree index crashes if sources exceed 2K --- what's likely
143 * for procedural languages.
149 prosrctext = DatumGetTextP(DirectFunctionCall1(textin,
150 CStringGetDatum(prosrc)));
151 retval = GetSysCacheOid(PROSRC,
152 PointerGetDatum(prosrctext),
155 if (OidIsValid(retval))
158 elog(ERROR, "lookup for procedure by source needs fix (Jan)");
159 #endif /* SETS_FIXED */
164 * don't allow functions of complex types that have the same name as
165 * existing attributes of the type
167 if (parameterCount == 1 && OidIsValid(typev[0]) &&
168 (relid = typeidTypeRelid(typev[0])) != InvalidOid &&
169 get_attnum(relid, (char *) procedureName) != InvalidAttrNumber)
171 (errcode(ERRCODE_DUPLICATE_COLUMN),
172 errmsg("\"%s\" is already an attribute of type %s",
173 procedureName, format_type_be(typev[0]))));
176 * All seems OK; prepare the data to be inserted into pg_proc.
179 for (i = 0; i < Natts_pg_proc; ++i)
182 values[i] = (Datum) NULL;
187 namestrcpy(&procname, procedureName);
188 values[i++] = NameGetDatum(&procname); /* proname */
189 values[i++] = ObjectIdGetDatum(procNamespace); /* pronamespace */
190 values[i++] = Int32GetDatum(GetUserId()); /* proowner */
191 values[i++] = ObjectIdGetDatum(languageObjectId); /* prolang */
192 values[i++] = BoolGetDatum(isAgg); /* proisagg */
193 values[i++] = BoolGetDatum(security_definer); /* prosecdef */
194 values[i++] = BoolGetDatum(isStrict); /* proisstrict */
195 values[i++] = BoolGetDatum(returnsSet); /* proretset */
196 values[i++] = CharGetDatum(volatility); /* provolatile */
197 values[i++] = UInt16GetDatum(parameterCount); /* pronargs */
198 values[i++] = ObjectIdGetDatum(returnType); /* prorettype */
199 values[i++] = PointerGetDatum(typev); /* proargtypes */
200 values[i++] = DirectFunctionCall1(textin, /* prosrc */
201 CStringGetDatum(prosrc));
202 values[i++] = DirectFunctionCall1(textin, /* probin */
203 CStringGetDatum(probin));
204 /* proacl will be handled below */
206 rel = heap_openr(ProcedureRelationName, RowExclusiveLock);
207 tupDesc = rel->rd_att;
209 /* Check for pre-existing definition */
210 oldtup = SearchSysCache(PROCNAMENSP,
211 PointerGetDatum(procedureName),
212 UInt16GetDatum(parameterCount),
213 PointerGetDatum(typev),
214 ObjectIdGetDatum(procNamespace));
216 if (HeapTupleIsValid(oldtup))
218 /* There is one; okay to replace it? */
219 Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
223 (errcode(ERRCODE_DUPLICATE_FUNCTION),
224 errmsg("function \"%s\" already exists with same argument types",
226 if (GetUserId() != oldproc->proowner && !superuser())
227 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC,
231 * Not okay to change the return type of the existing proc, since
232 * existing rules, views, etc may depend on the return type.
234 if (returnType != oldproc->prorettype ||
235 returnsSet != oldproc->proretset)
237 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
238 errmsg("cannot change return type of existing function"),
239 errhint("Use DROP FUNCTION first.")));
241 /* Can't change aggregate status, either */
242 if (oldproc->proisagg != isAgg)
244 if (oldproc->proisagg)
246 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
247 errmsg("function \"%s\" is an aggregate",
251 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
252 errmsg("function \"%s\" is not an aggregate",
256 /* do not change existing ownership or permissions, either */
257 replaces[Anum_pg_proc_proowner - 1] = ' ';
258 replaces[Anum_pg_proc_proacl - 1] = ' ';
261 tup = heap_modifytuple(oldtup, rel, values, nulls, replaces);
262 simple_heap_update(rel, &tup->t_self, tup);
264 ReleaseSysCache(oldtup);
269 /* Creating a new procedure */
271 /* start out with empty permissions */
272 nulls[Anum_pg_proc_proacl - 1] = 'n';
274 tup = heap_formtuple(tupDesc, values, nulls);
275 simple_heap_insert(rel, tup);
279 /* Need to update indexes for either the insert or update case */
280 CatalogUpdateIndexes(rel, tup);
282 retval = HeapTupleGetOid(tup);
285 * Create dependencies for the new function. If we are updating an
286 * existing function, first delete any existing pg_depend entries.
289 deleteDependencyRecordsFor(RelOid_pg_proc, retval);
291 myself.classId = RelOid_pg_proc;
292 myself.objectId = retval;
293 myself.objectSubId = 0;
295 /* dependency on namespace */
296 referenced.classId = get_system_catalog_relid(NamespaceRelationName);
297 referenced.objectId = procNamespace;
298 referenced.objectSubId = 0;
299 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
301 /* dependency on implementation language */
302 referenced.classId = get_system_catalog_relid(LanguageRelationName);
303 referenced.objectId = languageObjectId;
304 referenced.objectSubId = 0;
305 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
307 /* dependency on return type */
308 referenced.classId = RelOid_pg_type;
309 referenced.objectId = returnType;
310 referenced.objectSubId = 0;
311 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
313 /* dependency on input types */
314 for (i = 0; i < parameterCount; i++)
316 referenced.classId = RelOid_pg_type;
317 referenced.objectId = typev[i];
318 referenced.objectSubId = 0;
319 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
324 heap_close(rel, RowExclusiveLock);
326 /* Verify function body */
327 if (OidIsValid(languageValidator))
329 /* Advance command counter so new tuple can be seen by validator */
330 CommandCounterIncrement();
331 OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
338 * check_sql_fn_retval() -- check return value of a list of sql parse trees.
340 * The return value of a sql function is the value returned by
341 * the final query in the function. We do some ad-hoc type checking here
342 * to be sure that the user is returning the type he claims.
344 * This is normally applied during function definition, but in the case
345 * of a function with polymorphic arguments, we instead apply it during
346 * function execution startup. The rettype is then the actual resolved
347 * output type of the function, rather than the declared type. (Therefore,
348 * we should never see ANYARRAY or ANYELEMENT as rettype.)
351 check_sql_fn_retval(Oid rettype, char fn_typtype, List *queryTreeList)
361 int relnatts; /* physical number of columns in rel */
362 int rellogcols; /* # of nondeleted columns in rel */
363 int colindex; /* physical column index */
365 /* guard against empty function body; OK only if void return type */
366 if (queryTreeList == NIL)
368 if (rettype != VOIDOID)
370 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
371 errmsg("return type mismatch in function declared to return %s",
372 format_type_be(rettype)),
373 errdetail("Function's final statement must be a SELECT.")));
377 /* find the final query */
378 parse = (Query *) llast(queryTreeList);
380 cmd = parse->commandType;
381 tlist = parse->targetList;
384 * The last query must be a SELECT if and only if return type isn't
387 if (rettype == VOIDOID)
389 if (cmd == CMD_SELECT)
391 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
392 errmsg("return type mismatch in function declared to return %s",
393 format_type_be(rettype)),
394 errdetail("Function's final statement must not be a SELECT.")));
398 /* by here, the function is declared to return some type */
399 if (cmd != CMD_SELECT)
401 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
402 errmsg("return type mismatch in function declared to return %s",
403 format_type_be(rettype)),
404 errdetail("Function's final statement must be a SELECT.")));
407 * Count the non-junk entries in the result targetlist.
409 tlistlen = ExecCleanTargetListLength(tlist);
411 typerelid = typeidTypeRelid(rettype);
413 if (fn_typtype == 'b' || fn_typtype == 'd')
415 /* Shouldn't have a typerelid */
416 Assert(typerelid == InvalidOid);
419 * For base-type returns, the target list should have exactly one
420 * entry, and its type should agree with what the user declared.
421 * (As of Postgres 7.2, we accept binary-compatible types too.)
425 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
426 errmsg("return type mismatch in function declared to return %s",
427 format_type_be(rettype)),
428 errdetail("Final SELECT must return exactly one column.")));
430 restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
431 if (!IsBinaryCoercible(restype, rettype))
433 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
434 errmsg("return type mismatch in function declared to return %s",
435 format_type_be(rettype)),
436 errdetail("Actual return type is %s.",
437 format_type_be(restype))));
439 else if (fn_typtype == 'c')
441 /* Must have a typerelid */
442 Assert(typerelid != InvalidOid);
445 * If the target list is of length 1, and the type of the varnode
446 * in the target list matches the declared return type, this is
447 * okay. This can happen, for example, where the body of the
448 * function is 'SELECT func2()', where func2 has the same return
449 * type as the function that's calling it.
453 restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
454 if (IsBinaryCoercible(restype, rettype))
459 * Otherwise verify that the targetlist matches the return tuple
460 * type. This part of the typechecking is a hack. We look up the
461 * relation that is the declared return type, and scan the
462 * non-deleted attributes to ensure that they match the datatypes
463 * of the non-resjunk columns.
465 reln = relation_open(typerelid, AccessShareLock);
466 relnatts = reln->rd_rel->relnatts;
467 rellogcols = 0; /* we'll count nondeleted cols as we go */
470 foreach(tlistitem, tlist)
472 TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
473 Form_pg_attribute attr;
477 if (tle->resdom->resjunk)
483 if (colindex > relnatts)
485 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
486 errmsg("return type mismatch in function declared to return %s",
487 format_type_be(rettype)),
488 errdetail("Final SELECT returns too many columns.")));
489 attr = reln->rd_att->attrs[colindex - 1];
490 } while (attr->attisdropped);
493 tletype = exprType((Node *) tle->expr);
494 atttype = attr->atttypid;
495 if (!IsBinaryCoercible(tletype, atttype))
497 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
498 errmsg("return type mismatch in function declared to return %s",
499 format_type_be(rettype)),
500 errdetail("Final SELECT returns %s instead of %s at column %d.",
501 format_type_be(tletype),
502 format_type_be(atttype),
509 if (colindex > relnatts)
511 if (!reln->rd_att->attrs[colindex - 1]->attisdropped)
515 if (tlistlen != rellogcols)
517 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
518 errmsg("return type mismatch in function declared to return %s",
519 format_type_be(rettype)),
520 errdetail("Final SELECT returns too few columns.")));
522 relation_close(reln, AccessShareLock);
524 else if (rettype == RECORDOID)
526 /* Shouldn't have a typerelid */
527 Assert(typerelid == InvalidOid);
530 * For RECORD return type, defer this check until we get the first
534 else if (rettype == ANYARRAYOID || rettype == ANYELEMENTOID)
536 /* This should already have been caught ... */
538 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
539 errmsg("cannot determine result data type"),
540 errdetail("A function returning \"anyarray\" or \"anyelement\" must have at least one argument of either type.")));
544 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
545 errmsg("return type %s is not supported for SQL functions",
546 format_type_be(rettype))));
552 * Validator for internal functions
554 * Check that the given internal function name (the "prosrc" value) is
555 * a known builtin function.
558 fmgr_internal_validator(PG_FUNCTION_ARGS)
560 Oid funcoid = PG_GETARG_OID(0);
568 * We do not honor check_function_bodies since it's unlikely the
569 * function name will be found later if it isn't there now.
572 tuple = SearchSysCache(PROCOID,
573 ObjectIdGetDatum(funcoid),
575 if (!HeapTupleIsValid(tuple))
576 elog(ERROR, "cache lookup failed for function %u", funcoid);
577 proc = (Form_pg_proc) GETSTRUCT(tuple);
579 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
581 elog(ERROR, "null prosrc");
582 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
584 if (fmgr_internal_function(prosrc) == InvalidOid)
586 (errcode(ERRCODE_UNDEFINED_FUNCTION),
587 errmsg("there is no built-in function named \"%s\"",
590 ReleaseSysCache(tuple);
598 * Validator for C language functions
600 * Make sure that the library file exists, is loadable, and contains
601 * the specified link symbol. Also check for a valid function
602 * information record.
605 fmgr_c_validator(PG_FUNCTION_ARGS)
607 Oid funcoid = PG_GETARG_OID(0);
617 * It'd be most consistent to skip the check if !check_function_bodies,
618 * but the purpose of that switch is to be helpful for pg_dump loading,
619 * and for pg_dump loading it's much better if we *do* check.
622 tuple = SearchSysCache(PROCOID,
623 ObjectIdGetDatum(funcoid),
625 if (!HeapTupleIsValid(tuple))
626 elog(ERROR, "cache lookup failed for function %u", funcoid);
627 proc = (Form_pg_proc) GETSTRUCT(tuple);
629 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
631 elog(ERROR, "null prosrc");
632 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
634 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_probin, &isnull);
636 elog(ERROR, "null probin");
637 probin = DatumGetCString(DirectFunctionCall1(textout, tmp));
639 (void) load_external_function(probin, prosrc, true, &libraryhandle);
640 (void) fetch_finfo_record(libraryhandle, prosrc);
642 ReleaseSysCache(tuple);
649 * Validator for SQL language functions
651 * Parse it here in order to be sure that it contains no syntax errors.
654 fmgr_sql_validator(PG_FUNCTION_ARGS)
656 Oid funcoid = PG_GETARG_OID(0);
659 List *querytree_list;
667 tuple = SearchSysCache(PROCOID,
668 ObjectIdGetDatum(funcoid),
670 if (!HeapTupleIsValid(tuple))
671 elog(ERROR, "cache lookup failed for function %u", funcoid);
672 proc = (Form_pg_proc) GETSTRUCT(tuple);
674 functyptype = get_typtype(proc->prorettype);
676 /* Disallow pseudotype result */
677 /* except for RECORD, VOID, ANYARRAY, or ANYELEMENT */
678 if (functyptype == 'p' &&
679 proc->prorettype != RECORDOID &&
680 proc->prorettype != VOIDOID &&
681 proc->prorettype != ANYARRAYOID &&
682 proc->prorettype != ANYELEMENTOID)
684 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
685 errmsg("SQL functions cannot return type %s",
686 format_type_be(proc->prorettype))));
688 /* Disallow pseudotypes in arguments */
689 /* except for ANYARRAY or ANYELEMENT */
691 for (i = 0; i < proc->pronargs; i++)
693 if (get_typtype(proc->proargtypes[i]) == 'p')
695 if (proc->proargtypes[i] == ANYARRAYOID ||
696 proc->proargtypes[i] == ANYELEMENTOID)
700 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
701 errmsg("SQL functions cannot have arguments of type %s",
702 format_type_be(proc->proargtypes[i]))));
706 /* Postpone body checks if !check_function_bodies */
707 if (check_function_bodies)
709 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
711 elog(ERROR, "null prosrc");
713 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
716 * We can't do full prechecking of the function definition if there
717 * are any polymorphic input types, because actual datatypes of
718 * expression results will be unresolvable. The check will be done
719 * at runtime instead.
721 * We can run the text through the raw parser though; this will at
722 * least catch silly syntactic errors.
726 querytree_list = pg_parse_and_rewrite(prosrc,
729 check_sql_fn_retval(proc->prorettype, functyptype, querytree_list);
732 querytree_list = pg_parse_query(prosrc);
735 ReleaseSysCache(tuple);