1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_proc relation
6 * Portions Copyright (c) 1996-2002, 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.95 2002/12/12 15:49:24 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/builtins.h"
31 #include "utils/lsyscache.h"
32 #include "utils/sets.h"
33 #include "utils/syscache.h"
36 static void checkretval(Oid rettype, char fn_typtype, List *queryTreeList);
37 Datum fmgr_internal_validator(PG_FUNCTION_ARGS);
38 Datum fmgr_c_validator(PG_FUNCTION_ARGS);
39 Datum fmgr_sql_validator(PG_FUNCTION_ARGS);
42 /* ----------------------------------------------------------------
44 * ----------------------------------------------------------------
47 ProcedureCreate(const char *procedureName,
53 Oid languageValidator,
57 bool security_definer,
61 const Oid *parameterTypes)
67 char nulls[Natts_pg_proc];
68 Datum values[Natts_pg_proc];
69 char replaces[Natts_pg_proc];
70 Oid typev[FUNC_MAX_ARGS];
82 Assert(PointerIsValid(prosrc));
83 Assert(PointerIsValid(probin));
85 if (parameterCount < 0 || parameterCount > FUNC_MAX_ARGS)
86 elog(ERROR, "functions cannot have more than %d arguments",
89 /* Make sure we have a zero-padded param type array */
90 MemSet(typev, 0, FUNC_MAX_ARGS * sizeof(Oid));
91 if (parameterCount > 0)
92 memcpy(typev, parameterTypes, parameterCount * sizeof(Oid));
94 if (languageObjectId == SQLlanguageId)
97 * If this call is defining a set, check if the set is already
98 * defined by looking to see whether this call's function text
99 * matches a function already in pg_proc. If so just return the
100 * OID of the existing set.
102 if (strcmp(procedureName, GENERICSETNAME) == 0)
107 * The code below doesn't work any more because the PROSRC
108 * system cache and the pg_proc_prosrc_index have been
109 * removed. Instead a sequential heap scan or something better
110 * must get implemented. The reason for removing is that
111 * nbtree index crashes if sources exceed 2K --- what's likely
112 * for procedural languages.
118 prosrctext = DatumGetTextP(DirectFunctionCall1(textin,
119 CStringGetDatum(prosrc)));
120 retval = GetSysCacheOid(PROSRC,
121 PointerGetDatum(prosrctext),
124 if (OidIsValid(retval))
127 elog(ERROR, "lookup for procedure by source needs fix (Jan)");
128 #endif /* SETS_FIXED */
133 * don't allow functions of complex types that have the same name as
134 * existing attributes of the type
136 if (parameterCount == 1 && OidIsValid(typev[0]) &&
137 (relid = typeidTypeRelid(typev[0])) != 0 &&
138 get_attnum(relid, (char *) procedureName) != InvalidAttrNumber)
139 elog(ERROR, "method %s already an attribute of type %s",
140 procedureName, format_type_be(typev[0]));
143 * All seems OK; prepare the data to be inserted into pg_proc.
146 for (i = 0; i < Natts_pg_proc; ++i)
149 values[i] = (Datum) NULL;
154 namestrcpy(&procname, procedureName);
155 values[i++] = NameGetDatum(&procname); /* proname */
156 values[i++] = ObjectIdGetDatum(procNamespace); /* pronamespace */
157 values[i++] = Int32GetDatum(GetUserId()); /* proowner */
158 values[i++] = ObjectIdGetDatum(languageObjectId); /* prolang */
159 values[i++] = BoolGetDatum(isAgg); /* proisagg */
160 values[i++] = BoolGetDatum(security_definer); /* prosecdef */
161 values[i++] = BoolGetDatum(isStrict); /* proisstrict */
162 values[i++] = BoolGetDatum(returnsSet); /* proretset */
163 values[i++] = CharGetDatum(volatility); /* provolatile */
164 values[i++] = UInt16GetDatum(parameterCount); /* pronargs */
165 values[i++] = ObjectIdGetDatum(returnType); /* prorettype */
166 values[i++] = PointerGetDatum(typev); /* proargtypes */
167 values[i++] = DirectFunctionCall1(textin, /* prosrc */
168 CStringGetDatum(prosrc));
169 values[i++] = DirectFunctionCall1(textin, /* probin */
170 CStringGetDatum(probin));
171 /* proacl will be handled below */
173 rel = heap_openr(ProcedureRelationName, RowExclusiveLock);
174 tupDesc = rel->rd_att;
176 /* Check for pre-existing definition */
177 oldtup = SearchSysCache(PROCNAMENSP,
178 PointerGetDatum(procedureName),
179 UInt16GetDatum(parameterCount),
180 PointerGetDatum(typev),
181 ObjectIdGetDatum(procNamespace));
183 if (HeapTupleIsValid(oldtup))
185 /* There is one; okay to replace it? */
186 Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
189 elog(ERROR, "function %s already exists with same argument types",
191 if (GetUserId() != oldproc->proowner && !superuser())
192 elog(ERROR, "ProcedureCreate: you do not have permission to replace function %s",
196 * Not okay to change the return type of the existing proc, since
197 * existing rules, views, etc may depend on the return type.
199 if (returnType != oldproc->prorettype ||
200 returnsSet != oldproc->proretset)
201 elog(ERROR, "ProcedureCreate: cannot change return type of existing function."
202 "\n\tUse DROP FUNCTION first.");
204 /* Can't change aggregate status, either */
205 if (oldproc->proisagg != isAgg)
207 if (oldproc->proisagg)
208 elog(ERROR, "function %s is an aggregate",
211 elog(ERROR, "function %s is not an aggregate",
215 /* do not change existing ownership or permissions, either */
216 replaces[Anum_pg_proc_proowner - 1] = ' ';
217 replaces[Anum_pg_proc_proacl - 1] = ' ';
220 tup = heap_modifytuple(oldtup, rel, values, nulls, replaces);
221 simple_heap_update(rel, &tup->t_self, tup);
223 ReleaseSysCache(oldtup);
228 /* Creating a new procedure */
230 /* start out with empty permissions */
231 nulls[Anum_pg_proc_proacl - 1] = 'n';
233 tup = heap_formtuple(tupDesc, values, nulls);
234 simple_heap_insert(rel, tup);
238 /* Need to update indexes for either the insert or update case */
239 CatalogUpdateIndexes(rel, tup);
241 retval = HeapTupleGetOid(tup);
244 * Create dependencies for the new function. If we are updating an
245 * existing function, first delete any existing pg_depend entries.
248 deleteDependencyRecordsFor(RelOid_pg_proc, retval);
250 myself.classId = RelOid_pg_proc;
251 myself.objectId = retval;
252 myself.objectSubId = 0;
254 /* dependency on namespace */
255 referenced.classId = get_system_catalog_relid(NamespaceRelationName);
256 referenced.objectId = procNamespace;
257 referenced.objectSubId = 0;
258 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
260 /* dependency on implementation language */
261 referenced.classId = get_system_catalog_relid(LanguageRelationName);
262 referenced.objectId = languageObjectId;
263 referenced.objectSubId = 0;
264 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
266 /* dependency on return type */
267 referenced.classId = RelOid_pg_type;
268 referenced.objectId = returnType;
269 referenced.objectSubId = 0;
270 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
272 /* dependency on input types */
273 for (i = 0; i < parameterCount; i++)
275 referenced.classId = RelOid_pg_type;
276 referenced.objectId = typev[i];
277 referenced.objectSubId = 0;
278 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
283 heap_close(rel, RowExclusiveLock);
285 /* Verify function body */
286 if (OidIsValid(languageValidator))
288 /* Advance command counter so new tuple can be seen by validator */
289 CommandCounterIncrement();
290 OidFunctionCall1(languageValidator, ObjectIdGetDatum(retval));
297 * checkretval() -- check return value of a list of sql parse trees.
299 * The return value of a sql function is the value returned by
300 * the final query in the function. We do some ad-hoc define-time
301 * type checking here to be sure that the user is returning the
305 checkretval(Oid rettype, char fn_typtype, List *queryTreeList)
315 int relnatts; /* physical number of columns in rel */
316 int rellogcols; /* # of nondeleted columns in rel */
317 int colindex; /* physical column index */
319 /* guard against empty function body; OK only if void return type */
320 if (queryTreeList == NIL)
322 if (rettype != VOIDOID)
323 elog(ERROR, "function declared to return %s, but no SELECT provided",
324 format_type_be(rettype));
328 /* find the final query */
329 parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
331 cmd = parse->commandType;
332 tlist = parse->targetList;
335 * The last query must be a SELECT if and only if return type isn't
338 if (rettype == VOIDOID)
340 if (cmd == CMD_SELECT)
341 elog(ERROR, "function declared to return void, but final statement is a SELECT");
345 /* by here, the function is declared to return some type */
346 if (cmd != CMD_SELECT)
347 elog(ERROR, "function declared to return %s, but final statement is not a SELECT",
348 format_type_be(rettype));
351 * Count the non-junk entries in the result targetlist.
353 tlistlen = ExecCleanTargetListLength(tlist);
355 typerelid = typeidTypeRelid(rettype);
357 if (fn_typtype == 'b' || fn_typtype == 'd')
359 /* Shouldn't have a typerelid */
360 Assert(typerelid == InvalidOid);
363 * For base-type returns, the target list should have exactly one
364 * entry, and its type should agree with what the user declared.
365 * (As of Postgres 7.2, we accept binary-compatible types too.)
368 elog(ERROR, "function declared to return %s returns multiple columns in final SELECT",
369 format_type_be(rettype));
371 restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
372 if (!IsBinaryCoercible(restype, rettype))
373 elog(ERROR, "return type mismatch in function: declared to return %s, returns %s",
374 format_type_be(rettype), format_type_be(restype));
376 else if (fn_typtype == 'c')
378 /* Must have a typerelid */
379 Assert(typerelid != InvalidOid);
382 * If the target list is of length 1, and the type of the varnode
383 * in the target list matches the declared return type, this is
384 * okay. This can happen, for example, where the body of the
385 * function is 'SELECT func2()', where func2 has the same return
386 * type as the function that's calling it.
390 restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
391 if (IsBinaryCoercible(restype, rettype))
396 * Otherwise verify that the targetlist matches the return tuple
397 * type. This part of the typechecking is a hack. We look up the
398 * relation that is the declared return type, and scan the
399 * non-deleted attributes to ensure that they match the datatypes
400 * of the non-resjunk columns.
402 reln = relation_open(typerelid, AccessShareLock);
403 relnatts = reln->rd_rel->relnatts;
404 rellogcols = 0; /* we'll count nondeleted cols as we go */
407 foreach(tlistitem, tlist)
409 TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
410 Form_pg_attribute attr;
414 if (tle->resdom->resjunk)
420 if (colindex > relnatts)
421 elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)",
422 format_type_be(rettype), rellogcols);
423 attr = reln->rd_att->attrs[colindex - 1];
424 } while (attr->attisdropped);
427 tletype = exprType((Node *) tle->expr);
428 atttype = attr->atttypid;
429 if (!IsBinaryCoercible(tletype, atttype))
430 elog(ERROR, "function declared to return %s returns %s instead of %s at column %d",
431 format_type_be(rettype),
432 format_type_be(tletype),
433 format_type_be(atttype),
440 if (colindex > relnatts)
442 if (!reln->rd_att->attrs[colindex - 1]->attisdropped)
446 if (tlistlen != rellogcols)
447 elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)",
448 format_type_be(rettype), rellogcols);
450 relation_close(reln, AccessShareLock);
452 else if (fn_typtype == 'p' && rettype == RECORDOID)
454 /* Shouldn't have a typerelid */
455 Assert(typerelid == InvalidOid);
458 * For RECORD return type, defer this check until we get the first
463 elog(ERROR, "Unknown kind of return type specified for function");
469 * Validator for internal functions
471 * Check that the given internal function name (the "prosrc" value) is
472 * a known builtin function.
475 fmgr_internal_validator(PG_FUNCTION_ARGS)
477 Oid funcoid = PG_GETARG_OID(0);
484 tuple = SearchSysCache(PROCOID, funcoid, 0, 0, 0);
485 if (!HeapTupleIsValid(tuple))
486 elog(ERROR, "cache lookup of function %u failed", funcoid);
487 proc = (Form_pg_proc) GETSTRUCT(tuple);
489 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
491 elog(ERROR, "null prosrc");
492 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
494 if (fmgr_internal_function(prosrc) == InvalidOid)
495 elog(ERROR, "there is no built-in function named \"%s\"", prosrc);
497 ReleaseSysCache(tuple);
505 * Validator for C language functions
507 * Make sure that the library file exists, is loadable, and contains
508 * the specified link symbol. Also check for a valid function
509 * information record.
512 fmgr_c_validator(PG_FUNCTION_ARGS)
514 Oid funcoid = PG_GETARG_OID(0);
523 tuple = SearchSysCache(PROCOID, funcoid, 0, 0, 0);
524 if (!HeapTupleIsValid(tuple))
525 elog(ERROR, "cache lookup of function %u failed", funcoid);
526 proc = (Form_pg_proc) GETSTRUCT(tuple);
528 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
530 elog(ERROR, "null prosrc");
531 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
533 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_probin, &isnull);
535 elog(ERROR, "null probin");
536 probin = DatumGetCString(DirectFunctionCall1(textout, tmp));
538 (void) load_external_function(probin, prosrc, true, &libraryhandle);
539 (void) fetch_finfo_record(libraryhandle, prosrc);
541 ReleaseSysCache(tuple);
548 * Validator for SQL language functions
550 * Parse it here in order to be sure that it contains no syntax
554 fmgr_sql_validator(PG_FUNCTION_ARGS)
556 Oid funcoid = PG_GETARG_OID(0);
559 List *querytree_list;
566 tuple = SearchSysCache(PROCOID, funcoid, 0, 0, 0);
567 if (!HeapTupleIsValid(tuple))
568 elog(ERROR, "cache lookup of function %u failed", funcoid);
569 proc = (Form_pg_proc) GETSTRUCT(tuple);
571 functyptype = get_typtype(proc->prorettype);
573 /* Disallow pseudotypes in arguments and result */
574 /* except that return type can be RECORD or VOID */
575 if (functyptype == 'p' &&
576 proc->prorettype != RECORDOID &&
577 proc->prorettype != VOIDOID)
578 elog(ERROR, "SQL functions cannot return type %s",
579 format_type_be(proc->prorettype));
581 for (i = 0; i < proc->pronargs; i++)
583 if (get_typtype(proc->proargtypes[i]) == 'p')
584 elog(ERROR, "SQL functions cannot have arguments of type %s",
585 format_type_be(proc->proargtypes[i]));
588 tmp = SysCacheGetAttr(PROCOID, tuple, Anum_pg_proc_prosrc, &isnull);
590 elog(ERROR, "null prosrc");
592 prosrc = DatumGetCString(DirectFunctionCall1(textout, tmp));
594 querytree_list = pg_parse_and_rewrite(prosrc, proc->proargtypes, proc->pronargs);
595 checkretval(proc->prorettype, functyptype, querytree_list);
597 ReleaseSysCache(tuple);