1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* pg_proc.c
|
1997-09-07 07:04:48 +02:00
|
|
|
* routines to support manipulation of the pg_proc relation
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1999-07-17 22:18:55 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/catalog/pg_proc.c,v 1.33 1999/07/17 20:16:49 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/catname.h"
|
|
|
|
#include "catalog/indexing.h"
|
|
|
|
#include "catalog/pg_proc.h"
|
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "miscadmin.h"
|
|
|
|
#include "optimizer/planner.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parse_type.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "tcop/tcopprot.h"
|
|
|
|
#include "utils/builtins.h"
|
1999-04-18 04:57:22 +02:00
|
|
|
#include "utils/fmgrtab.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/sets.h"
|
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
1996-11-04 00:27:08 +01:00
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1999-04-18 04:57:22 +02:00
|
|
|
* ProcedureCreate
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
ProcedureCreate(char *procedureName,
|
1997-09-07 07:04:48 +02:00
|
|
|
bool returnsSet,
|
|
|
|
char *returnTypeName,
|
|
|
|
char *languageName,
|
|
|
|
char *prosrc,
|
|
|
|
char *probin,
|
|
|
|
bool canCache,
|
|
|
|
bool trusted,
|
|
|
|
int32 byte_pct,
|
|
|
|
int32 perbyte_cpu,
|
|
|
|
int32 percall_cpu,
|
|
|
|
int32 outin_ratio,
|
1997-09-08 23:56:23 +02:00
|
|
|
List *argList,
|
1997-09-07 07:04:48 +02:00
|
|
|
CommandDest dest)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-02-11 20:14:04 +01:00
|
|
|
int i;
|
1998-08-19 04:04:17 +02:00
|
|
|
Relation rel;
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple tup;
|
|
|
|
bool defined;
|
|
|
|
uint16 parameterCount;
|
|
|
|
char nulls[Natts_pg_proc];
|
|
|
|
Datum values[Natts_pg_proc];
|
|
|
|
Oid languageObjectId;
|
|
|
|
Oid typeObjectId;
|
|
|
|
List *x;
|
1999-05-13 09:29:22 +02:00
|
|
|
List *querytree_list;
|
1997-09-08 04:41:22 +02:00
|
|
|
List *plan_list;
|
|
|
|
Oid typev[8];
|
|
|
|
Oid relid;
|
|
|
|
Oid toid;
|
|
|
|
text *prosrctext;
|
1998-04-01 17:35:33 +02:00
|
|
|
NameData procname;
|
1997-09-08 04:41:22 +02:00
|
|
|
TupleDesc tupDesc;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* sanity checks
|
|
|
|
* ----------------
|
1996-07-09 08:22:35 +02:00
|
|
|
*/
|
1997-09-07 07:04:48 +02:00
|
|
|
Assert(PointerIsValid(prosrc));
|
|
|
|
Assert(PointerIsValid(probin));
|
|
|
|
|
|
|
|
parameterCount = 0;
|
1997-09-18 22:22:58 +02:00
|
|
|
MemSet(typev, 0, 8 * sizeof(Oid));
|
1997-09-07 07:04:48 +02:00
|
|
|
foreach(x, argList)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Value *t = lfirst(x);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (parameterCount == 8)
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "Procedures cannot take more than 8 arguments");
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (strcmp(strVal(t), "opaque") == 0)
|
|
|
|
{
|
|
|
|
if (strcmp(languageName, "sql") == 0)
|
1999-04-18 04:57:22 +02:00
|
|
|
elog(ERROR, "ProcedureCreate: sql functions cannot take type \"opaque\"");
|
1997-09-07 07:04:48 +02:00
|
|
|
toid = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
toid = TypeGet(strVal(t), &defined);
|
|
|
|
|
|
|
|
if (!OidIsValid(toid))
|
|
|
|
{
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "ProcedureCreate: arg type '%s' is not defined",
|
1997-09-07 07:04:48 +02:00
|
|
|
strVal(t));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!defined)
|
|
|
|
{
|
|
|
|
elog(NOTICE, "ProcedureCreate: arg type '%s' is only a shell",
|
|
|
|
strVal(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typev[parameterCount++] = toid;
|
|
|
|
}
|
|
|
|
|
|
|
|
tup = SearchSysCacheTuple(PRONAME,
|
|
|
|
PointerGetDatum(procedureName),
|
|
|
|
UInt16GetDatum(parameterCount),
|
|
|
|
PointerGetDatum(typev),
|
|
|
|
0);
|
|
|
|
|
|
|
|
if (HeapTupleIsValid(tup))
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "ProcedureCreate: procedure %s already exists with same arguments",
|
1997-09-07 07:04:48 +02:00
|
|
|
procedureName);
|
|
|
|
|
|
|
|
if (!strcmp(languageName, "sql"))
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (!strcmp(procedureName, GENERICSETNAME))
|
|
|
|
{
|
|
|
|
prosrctext = textin(prosrc);
|
|
|
|
tup = SearchSysCacheTuple(PROSRC,
|
|
|
|
PointerGetDatum(prosrctext),
|
|
|
|
0, 0, 0);
|
1998-08-31 19:49:18 +02:00
|
|
|
pfree(prosrctext);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (HeapTupleIsValid(tup))
|
1998-11-27 20:52:36 +01:00
|
|
|
return tup->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
tup = SearchSysCacheTuple(LANNAME,
|
|
|
|
PointerGetDatum(languageName),
|
|
|
|
0, 0, 0);
|
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
1998-08-31 19:49:18 +02:00
|
|
|
elog(ERROR, "ProcedureCreate: no such language %s", languageName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-11-27 20:52:36 +01:00
|
|
|
languageObjectId = tup->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (strcmp(returnTypeName, "opaque") == 0)
|
|
|
|
{
|
|
|
|
if (strcmp(languageName, "sql") == 0)
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "ProcedureCreate: sql functions cannot return type \"opaque\"");
|
1997-09-07 07:04:48 +02:00
|
|
|
typeObjectId = 0;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
typeObjectId = TypeGet(returnTypeName, &defined);
|
|
|
|
|
|
|
|
if (!OidIsValid(typeObjectId))
|
|
|
|
{
|
|
|
|
elog(NOTICE, "ProcedureCreate: type '%s' is not yet defined",
|
|
|
|
returnTypeName);
|
1999-02-21 04:49:55 +01:00
|
|
|
#ifdef NOT_USED
|
1997-09-07 07:04:48 +02:00
|
|
|
elog(NOTICE, "ProcedureCreate: creating a shell for type '%s'",
|
|
|
|
returnTypeName);
|
|
|
|
#endif
|
|
|
|
typeObjectId = TypeShellMake(returnTypeName);
|
|
|
|
if (!OidIsValid(typeObjectId))
|
|
|
|
{
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "ProcedureCreate: could not create type '%s'",
|
1997-09-07 07:04:48 +02:00
|
|
|
returnTypeName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (!defined)
|
|
|
|
{
|
|
|
|
elog(NOTICE, "ProcedureCreate: return type '%s' is only a shell",
|
|
|
|
returnTypeName);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* don't allow functions of complex types that have the same name as
|
|
|
|
* existing attributes of the type
|
|
|
|
*/
|
|
|
|
if (parameterCount == 1 &&
|
|
|
|
(toid = TypeGet(strVal(lfirst(argList)), &defined)) &&
|
|
|
|
defined &&
|
1997-11-25 23:07:18 +01:00
|
|
|
(relid = typeidTypeRelid(toid)) != 0 &&
|
1997-09-07 07:04:48 +02:00
|
|
|
get_attnum(relid, procedureName) != InvalidAttrNumber)
|
1998-01-06 20:42:33 +01:00
|
|
|
elog(ERROR, "method %s already an attribute of type %s",
|
1997-09-07 07:04:48 +02:00
|
|
|
procedureName, strVal(lfirst(argList)));
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a postquel procedure, we parse it here in order to be
|
|
|
|
* sure that it contains no syntax errors. We should store the plan
|
|
|
|
* in an Inversion file for use later, but for now, we just store the
|
|
|
|
* procedure's text in the prosrc attribute.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (strcmp(languageName, "sql") == 0)
|
|
|
|
{
|
1997-12-11 18:36:58 +01:00
|
|
|
plan_list = pg_parse_and_plan(prosrc, typev, parameterCount,
|
1998-08-24 03:38:11 +02:00
|
|
|
&querytree_list, dest, FALSE);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* typecheck return value */
|
|
|
|
pg_checkretval(typeObjectId, querytree_list);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-04-18 04:57:22 +02:00
|
|
|
/*
|
|
|
|
* If this is an internal procedure, check that the given internal
|
|
|
|
* function name (the 'prosrc' value) is a known builtin function.
|
|
|
|
*
|
|
|
|
* NOTE: in Postgres versions before 6.5, the SQL name of the created
|
1999-05-25 18:15:34 +02:00
|
|
|
* function could not be different from the internal name, and
|
|
|
|
* 'prosrc' wasn't used. So there is code out there that does CREATE
|
|
|
|
* FUNCTION xyz AS '' LANGUAGE 'internal'. To preserve some modicum
|
|
|
|
* of backwards compatibility, accept an empty 'prosrc' value as
|
|
|
|
* meaning the supplied SQL function name.
|
1999-04-18 04:57:22 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (strcmp(languageName, "internal") == 0)
|
|
|
|
{
|
|
|
|
if (strlen(prosrc) == 0)
|
|
|
|
prosrc = procedureName;
|
|
|
|
if (fmgr_lookupByName(prosrc) == (func_ptr) NULL)
|
|
|
|
elog(ERROR,
|
1999-05-25 18:15:34 +02:00
|
|
|
"ProcedureCreate: there is no builtin function named \"%s\"",
|
1999-04-18 04:57:22 +02:00
|
|
|
prosrc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All seems OK; prepare the tuple to be inserted into pg_proc.
|
|
|
|
*/
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < Natts_pg_proc; ++i)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
nulls[i] = ' ';
|
|
|
|
values[i] = (Datum) NULL;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
i = 0;
|
1998-04-01 17:35:33 +02:00
|
|
|
namestrcpy(&procname, procedureName);
|
|
|
|
values[i++] = NameGetDatum(&procname);
|
1997-09-07 07:04:48 +02:00
|
|
|
values[i++] = Int32GetDatum(GetUserId());
|
|
|
|
values[i++] = ObjectIdGetDatum(languageObjectId);
|
|
|
|
|
|
|
|
/* XXX isinherited is always false for now */
|
|
|
|
|
|
|
|
values[i++] = Int8GetDatum((bool) 0);
|
|
|
|
|
|
|
|
/* XXX istrusted is always false for now */
|
|
|
|
|
|
|
|
values[i++] = Int8GetDatum(trusted);
|
|
|
|
values[i++] = Int8GetDatum(canCache);
|
|
|
|
values[i++] = UInt16GetDatum(parameterCount);
|
|
|
|
values[i++] = Int8GetDatum(returnsSet);
|
|
|
|
values[i++] = ObjectIdGetDatum(typeObjectId);
|
|
|
|
|
|
|
|
values[i++] = (Datum) typev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following assignments of constants are made. The real values
|
|
|
|
* will have to be extracted from the arglist someday soon.
|
|
|
|
*/
|
|
|
|
values[i++] = Int32GetDatum(byte_pct); /* probyte_pct */
|
|
|
|
values[i++] = Int32GetDatum(perbyte_cpu); /* properbyte_cpu */
|
|
|
|
values[i++] = Int32GetDatum(percall_cpu); /* propercall_cpu */
|
|
|
|
values[i++] = Int32GetDatum(outin_ratio); /* prooutin_ratio */
|
|
|
|
|
1998-04-27 06:08:07 +02:00
|
|
|
values[i++] = (Datum) fmgr(F_TEXTIN, prosrc); /* prosrc */
|
|
|
|
values[i++] = (Datum) fmgr(F_TEXTIN, probin); /* probin */
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
rel = heap_openr(ProcedureRelationName);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
tupDesc = rel->rd_att;
|
1997-09-07 07:04:48 +02:00
|
|
|
tup = heap_formtuple(tupDesc,
|
|
|
|
values,
|
|
|
|
nulls);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_insert(rel, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
if (RelationGetForm(rel)->relhasindex)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
Relation idescs[Num_pg_proc_indices];
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
CatalogOpenIndices(Num_pg_proc_indices, Name_pg_proc_indices, idescs);
|
1998-08-19 04:04:17 +02:00
|
|
|
CatalogIndexInsert(idescs, Num_pg_proc_indices, rel, tup);
|
1997-09-07 07:04:48 +02:00
|
|
|
CatalogCloseIndices(Num_pg_proc_indices, idescs);
|
|
|
|
}
|
1998-08-19 04:04:17 +02:00
|
|
|
heap_close(rel);
|
1998-11-27 20:52:36 +01:00
|
|
|
return tup->t_data->t_oid;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|