1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_proc relation
6 * Portions Copyright (c) 1996-2001, 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.64 2002/02/18 23:11:08 petere Exp $
13 *-------------------------------------------------------------------------
17 #include "access/heapam.h"
18 #include "catalog/catname.h"
19 #include "catalog/indexing.h"
20 #include "catalog/pg_language.h"
21 #include "catalog/pg_proc.h"
22 #include "catalog/pg_type.h"
23 #include "executor/executor.h"
24 #include "miscadmin.h"
25 #include "parser/parse_coerce.h"
26 #include "parser/parse_expr.h"
27 #include "parser/parse_type.h"
28 #include "tcop/tcopprot.h"
29 #include "utils/builtins.h"
30 #include "utils/lsyscache.h"
31 #include "utils/sets.h"
32 #include "utils/syscache.h"
35 static void checkretval(Oid rettype, List *queryTreeList);
38 /* ----------------------------------------------------------------
40 * ----------------------------------------------------------------
43 ProcedureCreate(char *procedureName,
64 uint16 parameterCount;
65 char nulls[Natts_pg_proc];
66 Datum values[Natts_pg_proc];
67 char replaces[Natts_pg_proc];
71 Oid typev[FUNC_MAX_ARGS];
81 Assert(PointerIsValid(prosrc));
82 Assert(PointerIsValid(probin));
85 MemSet(typev, 0, FUNC_MAX_ARGS * sizeof(Oid));
88 TypeName *t = (TypeName *) lfirst(x);
89 char *typnam = TypeNameToInternalName(t);
91 if (parameterCount >= FUNC_MAX_ARGS)
92 elog(ERROR, "functions cannot have more than %d arguments",
95 if (strcmp(typnam, "opaque") == 0)
97 if (languageObjectId == SQLlanguageId)
98 elog(ERROR, "SQL functions cannot have arguments of type \"opaque\"");
103 toid = TypeGet(typnam, &defined);
105 if (!OidIsValid(toid))
106 elog(ERROR, "argument type %s does not exist",
109 elog(NOTICE, "argument type %s is only a shell",
114 elog(ERROR, "functions cannot accept set arguments");
116 typev[parameterCount++] = toid;
119 if (languageObjectId == SQLlanguageId)
122 * If this call is defining a set, check if the set is already
123 * defined by looking to see whether this call's function text
124 * matches a function already in pg_proc. If so just return the
125 * OID of the existing set.
127 if (strcmp(procedureName, GENERICSETNAME) == 0)
132 * The code below doesn't work any more because the PROSRC
133 * system cache and the pg_proc_prosrc_index have been
134 * removed. Instead a sequential heap scan or something better
135 * must get implemented. The reason for removing is that
136 * nbtree index crashes if sources exceed 2K --- what's likely
137 * for procedural languages.
143 prosrctext = DatumGetTextP(DirectFunctionCall1(textin,
144 CStringGetDatum(prosrc)));
145 retval = GetSysCacheOid(PROSRC,
146 PointerGetDatum(prosrctext),
149 if (OidIsValid(retval))
152 elog(ERROR, "lookup for procedure by source needs fix (Jan)");
153 #endif /* SETS_FIXED */
157 if (strcmp(returnTypeName, "opaque") == 0)
159 if (languageObjectId == SQLlanguageId)
160 elog(ERROR, "SQL functions cannot return type \"opaque\"");
161 typeObjectId = InvalidOid;
165 typeObjectId = TypeGet(returnTypeName, &defined);
167 if (!OidIsValid(typeObjectId))
169 elog(NOTICE, "ProcedureCreate: type %s is not yet defined",
171 typeObjectId = TypeShellMake(returnTypeName);
172 if (!OidIsValid(typeObjectId))
173 elog(ERROR, "could not create type %s",
177 elog(NOTICE, "return type %s is only a shell",
182 * don't allow functions of complex types that have the same name as
183 * existing attributes of the type
185 if (parameterCount == 1 &&
186 (toid = TypeGet(strVal(lfirst(argList)), &defined)) &&
188 (relid = typeidTypeRelid(toid)) != 0 &&
189 get_attnum(relid, procedureName) != InvalidAttrNumber)
190 elog(ERROR, "method %s already an attribute of type %s",
191 procedureName, strVal(lfirst(argList)));
194 * If this is a postquel procedure, we parse it here in order to be
195 * sure that it contains no syntax errors. We should store the plan
196 * in an Inversion file for use later, but for now, we just store the
197 * procedure's text in the prosrc attribute.
200 if (languageObjectId == SQLlanguageId)
202 querytree_list = pg_parse_and_rewrite(prosrc, typev, parameterCount);
203 /* typecheck return value */
204 checkretval(typeObjectId, querytree_list);
208 * If this is an internal procedure, check that the given internal
209 * function name (the 'prosrc' value) is a known builtin function.
211 * NOTE: in Postgres versions before 6.5, the SQL name of the created
212 * function could not be different from the internal name, and
213 * 'prosrc' wasn't used. So there is code out there that does CREATE
214 * FUNCTION xyz AS '' LANGUAGE 'internal'. To preserve some modicum
215 * of backwards compatibility, accept an empty 'prosrc' value as
216 * meaning the supplied SQL function name.
219 if (languageObjectId == INTERNALlanguageId)
221 if (strlen(prosrc) == 0)
222 prosrc = procedureName;
223 if (fmgr_internal_function(prosrc) == InvalidOid)
225 "there is no built-in function named \"%s\"",
230 * If this is a dynamically loadable procedure, make sure that the
231 * library file exists, is loadable, and contains the specified link
232 * symbol. Also check for a valid function information record.
234 * We used to perform these checks only when the function was first
235 * called, but it seems friendlier to verify the library's validity at
236 * CREATE FUNCTION time.
239 if (languageObjectId == ClanguageId)
243 /* If link symbol is specified as "-", substitute procedure name */
244 if (strcmp(prosrc, "-") == 0)
245 prosrc = procedureName;
246 (void) load_external_function(probin, prosrc, true,
248 (void) fetch_finfo_record(libraryhandle, prosrc);
252 * All seems OK; prepare the data to be inserted into pg_proc.
255 for (i = 0; i < Natts_pg_proc; ++i)
258 values[i] = (Datum) NULL;
263 namestrcpy(&procname, procedureName);
264 values[i++] = NameGetDatum(&procname);
265 values[i++] = Int32GetDatum(GetUserId());
266 values[i++] = ObjectIdGetDatum(languageObjectId);
267 /* XXX isinherited is always false for now */
268 values[i++] = BoolGetDatum(false);
269 values[i++] = BoolGetDatum(trusted);
270 values[i++] = BoolGetDatum(canCache);
271 values[i++] = BoolGetDatum(isStrict);
272 values[i++] = UInt16GetDatum(parameterCount);
273 values[i++] = BoolGetDatum(returnsSet);
274 values[i++] = ObjectIdGetDatum(typeObjectId);
275 values[i++] = PointerGetDatum(typev);
276 values[i++] = Int32GetDatum(byte_pct); /* probyte_pct */
277 values[i++] = Int32GetDatum(perbyte_cpu); /* properbyte_cpu */
278 values[i++] = Int32GetDatum(percall_cpu); /* propercall_cpu */
279 values[i++] = Int32GetDatum(outin_ratio); /* prooutin_ratio */
280 values[i++] = DirectFunctionCall1(textin, /* prosrc */
281 CStringGetDatum(prosrc));
282 values[i++] = DirectFunctionCall1(textin, /* probin */
283 CStringGetDatum(probin));
285 rel = heap_openr(ProcedureRelationName, RowExclusiveLock);
286 tupDesc = rel->rd_att;
288 /* Check for pre-existing definition */
289 oldtup = SearchSysCache(PROCNAME,
290 PointerGetDatum(procedureName),
291 UInt16GetDatum(parameterCount),
292 PointerGetDatum(typev),
295 if (HeapTupleIsValid(oldtup))
297 /* There is one; okay to replace it? */
298 Form_pg_proc oldproc = (Form_pg_proc) GETSTRUCT(oldtup);
301 elog(ERROR, "function %s already exists with same argument types",
303 if (GetUserId() != oldproc->proowner && !superuser())
304 elog(ERROR, "ProcedureCreate: you do not have permission to replace function %s",
308 * Not okay to change the return type of the existing proc, since
309 * existing rules, views, etc may depend on the return type.
311 if (typeObjectId != oldproc->prorettype ||
312 returnsSet != oldproc->proretset)
313 elog(ERROR, "ProcedureCreate: cannot change return type of existing function."
314 "\n\tUse DROP FUNCTION first.");
316 /* do not change existing permissions */
317 replaces[Anum_pg_proc_proacl-1] = ' ';
320 tup = heap_modifytuple(oldtup, rel, values, nulls, replaces);
321 simple_heap_update(rel, &tup->t_self, tup);
323 ReleaseSysCache(oldtup);
327 /* Creating a new procedure */
329 /* start out with empty permissions */
330 nulls[Anum_pg_proc_proacl-1] = 'n';
332 tup = heap_formtuple(tupDesc, values, nulls);
333 heap_insert(rel, tup);
336 /* Need to update indices for either the insert or update case */
337 if (RelationGetForm(rel)->relhasindex)
339 Relation idescs[Num_pg_proc_indices];
341 CatalogOpenIndices(Num_pg_proc_indices, Name_pg_proc_indices, idescs);
342 CatalogIndexInsert(idescs, Num_pg_proc_indices, rel, tup);
343 CatalogCloseIndices(Num_pg_proc_indices, idescs);
346 retval = tup->t_data->t_oid;
349 heap_close(rel, RowExclusiveLock);
355 * checkretval() -- check return value of a list of sql parse trees.
357 * The return value of a sql function is the value returned by
358 * the final query in the function. We do some ad-hoc define-time
359 * type checking here to be sure that the user is returning the
363 checkretval(Oid rettype, List *queryTreeList)
377 /* guard against empty function body; OK only if no return type */
378 if (queryTreeList == NIL)
380 if (rettype != InvalidOid)
381 elog(ERROR, "function declared to return %s, but no SELECT provided",
382 format_type_be(rettype));
386 /* find the final query */
387 parse = (Query *) nth(length(queryTreeList) - 1, queryTreeList);
389 cmd = parse->commandType;
390 tlist = parse->targetList;
393 * The last query must be a SELECT if and only if there is a return
396 if (rettype == InvalidOid)
398 if (cmd == CMD_SELECT)
399 elog(ERROR, "function declared with no return type, but final statement is a SELECT");
403 /* by here, the function is declared to return some type */
404 if (cmd != CMD_SELECT)
405 elog(ERROR, "function declared to return %s, but final statement is not a SELECT",
406 format_type_be(rettype));
409 * Count the non-junk entries in the result targetlist.
411 tlistlen = ExecCleanTargetListLength(tlist);
414 * For base-type returns, the target list should have exactly one
415 * entry, and its type should agree with what the user declared. (As
416 * of Postgres 7.2, we accept binary-compatible types too.)
418 typerelid = typeidTypeRelid(rettype);
419 if (typerelid == InvalidOid)
422 elog(ERROR, "function declared to return %s returns multiple columns in final SELECT",
423 format_type_be(rettype));
425 restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
426 if (restype != rettype && !IS_BINARY_COMPATIBLE(restype, rettype))
427 elog(ERROR, "return type mismatch in function: declared to return %s, returns %s",
428 format_type_be(rettype), format_type_be(restype));
434 * If the target list is of length 1, and the type of the varnode in
435 * the target list matches the declared return type, this is okay.
436 * This can happen, for example, where the body of the function is
437 * 'SELECT func2()', where func2 has the same return type as the
438 * function that's calling it.
442 restype = ((TargetEntry *) lfirst(tlist))->resdom->restype;
443 if (restype == rettype || IS_BINARY_COMPATIBLE(restype, rettype))
448 * By here, the procedure returns a tuple or set of tuples. This part
449 * of the typechecking is a hack. We look up the relation that is the
450 * declared return type, and be sure that attributes 1 .. n in the
451 * target list match the declared types.
453 reln = heap_open(typerelid, AccessShareLock);
455 relnatts = reln->rd_rel->relnatts;
457 if (tlistlen != relnatts)
458 elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)",
459 format_type_be(rettype), relnatts);
461 /* expect attributes 1 .. n in order */
463 foreach(tlistitem, tlist)
465 TargetEntry *tle = (TargetEntry *) lfirst(tlistitem);
469 if (tle->resdom->resjunk)
471 tletype = exprType(tle->expr);
472 atttype = reln->rd_att->attrs[i]->atttypid;
473 if (tletype != atttype && !IS_BINARY_COMPATIBLE(tletype, atttype))
474 elog(ERROR, "function declared to return %s returns %s instead of %s at column %d",
475 format_type_be(rettype),
476 format_type_be(tletype),
477 format_type_be(atttype),
482 /* this shouldn't happen, but let's just check... */
484 elog(ERROR, "function declared to return %s does not SELECT the right number of columns (%d)",
485 format_type_be(rettype), relnatts);
487 heap_close(reln, AccessShareLock);